Douglas Gregor | 8e9bebd | 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 | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 15 | #include "SemaInherit.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeOrdering.h" |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 22 | #include "clang/Basic/PartialDiagnostic.h" |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallPtrSet.h" |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/STLExtras.h" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
| 26 | #include <algorithm> |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 27 | #include <cstdio> |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 33 | ImplicitConversionCategory |
Douglas Gregor | 8e9bebd | 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 | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 44 | ICC_Promotion, |
| 45 | ICC_Conversion, |
| 46 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 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 | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 52 | ICC_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 53 | ICC_Conversion, |
Douglas Gregor | 8e9bebd | 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 | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 71 | ICR_Promotion, |
| 72 | ICR_Conversion, |
| 73 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 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 | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 79 | ICR_Conversion, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 80 | ICR_Conversion, |
Douglas Gregor | 8e9bebd | 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 | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 97 | "Complex promotion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 98 | "Integral conversion", |
| 99 | "Floating conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 100 | "Complex conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 101 | "Floating-integral conversion", |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 102 | "Complex-real conversion", |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 103 | "Pointer conversion", |
| 104 | "Pointer-to-member conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 105 | "Boolean conversion", |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 106 | "Compatible-types conversion", |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 107 | "Derived-to-base conversion" |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 108 | }; |
| 109 | return Name[Kind]; |
| 110 | } |
| 111 | |
Douglas Gregor | 60d62c2 | 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 | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 121 | RRefBinding = false; |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 122 | CopyConstructor = 0; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | /// used as part of the ranking of standard conversion sequences |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 142 | /// (C++ 13.3.3.2p4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | bool StandardConversionSequence::isPointerConversionToBool() const { |
Douglas Gregor | 8e9bebd | 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 | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 152 | (FromType->isPointerType() || FromType->isBlockPointerType() || |
Douglas Gregor | 8e9bebd | 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 | bc0805a | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | bool |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 164 | StandardConversionSequence:: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | isPointerConversionToVoidPointer(ASTContext& Context) const { |
Douglas Gregor | bc0805a | 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 | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 176 | if (const PointerType* ToPtrType = ToType->getAs<PointerType>()) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 177 | return ToPtrType->getPointeeType()->isVoidType(); |
| 178 | |
| 179 | return false; |
| 180 | } |
| 181 | |
Douglas Gregor | 8e9bebd | 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 | 225c41e | 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 | 8e9bebd | 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 | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 227 | fprintf(stderr, "'%s'", ConversionFunction->getNameAsString().c_str()); |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | // FunctionDecl that New cannot be overloaded with. |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 272 | // so IsOverload will not be used. |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | Sema::IsOverload(FunctionDecl *New, Decl* OldD, |
| 287 | OverloadedFunctionDecl::function_iterator& MatchedDecl) { |
Douglas Gregor | 8e9bebd | 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 | e53060f | 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 | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 305 | FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate(); |
| 307 | |
Douglas Gregor | 34d1dc9 | 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 | 8e9bebd | 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 | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 324 | if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) || |
| 325 | isa<FunctionNoProtoType>(NewQType.getTypePtr())) |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 326 | return false; |
| 327 | |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 328 | FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType); |
| 329 | FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType); |
Douglas Gregor | 8e9bebd | 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 | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 341 | // C++ [temp.over.link]p4: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | // The signature of a function template consists of its function |
Douglas Gregor | 34d1dc9 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | // relationship between the template parameters and the rest of the |
Douglas Gregor | 34d1dc9 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(), |
| 352 | OldTemplate->getTemplateParameters(), |
Douglas Gregor | 34d1dc9 | 2009-06-24 16:50:40 +0000 | [diff] [blame] | 353 | false, false, SourceLocation()) || |
| 354 | OldType->getResultType() != NewType->getResultType())) |
| 355 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | if (OldMethod && NewMethod && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 368 | !OldMethod->isStatic() && !NewMethod->isStatic() && |
Douglas Gregor | 1ca50c3 | 2008-11-21 15:36:28 +0000 | [diff] [blame] | 369 | OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers()) |
Douglas Gregor | 8e9bebd | 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 | 27c8dc0 | 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 | 8e9bebd | 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 | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 400 | /// |
| 401 | /// If @p SuppressUserConversions, then user-defined conversions are |
| 402 | /// not permitted. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 403 | /// If @p AllowExplicit, then explicit user-defined conversions are |
| 404 | /// permitted. |
Sebastian Redl | e2b6833 | 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 | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 407 | ImplicitConversionSequence |
Anders Carlsson | 2974b5c | 2009-08-27 17:14:02 +0000 | [diff] [blame] | 408 | Sema::TryImplicitConversion(Expr* From, QualType ToType, |
| 409 | bool SuppressUserConversions, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 410 | bool AllowExplicit, bool ForceRValue, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | bool InOverloadResolution) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 412 | ImplicitConversionSequence ICS; |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 413 | if (IsStandardConversion(From, ToType, InOverloadResolution, ICS.Standard)) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 414 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 415 | else if (getLangOptions().CPlusPlus && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | IsUserDefinedConversion(From, ToType, ICS.UserDefined, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 417 | !SuppressUserConversions, AllowExplicit, |
| 418 | ForceRValue)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 419 | ICS.ConversionKind = ImplicitConversionSequence::UserDefinedConversion; |
Douglas Gregor | 396b7cd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 428 | = dyn_cast<CXXConstructorDecl>(ICS.UserDefined.ConversionFunction)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | QualType FromCanon |
Douglas Gregor | 2b1e003 | 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 | 225c41e | 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 | 396b7cd | 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 | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 439 | ICS.Standard.CopyConstructor = Constructor; |
Douglas Gregor | 2b1e003 | 2009-02-02 22:11:10 +0000 | [diff] [blame] | 440 | if (ToCanon != FromCanon) |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 441 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 442 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 443 | } |
Douglas Gregor | 734d986 | 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 | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 455 | } else |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 456 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
Douglas Gregor | 60d62c2 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | bool |
| 470 | Sema::IsStandardConversion(Expr* From, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 471 | bool InOverloadResolution, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | StandardConversionSequence &SCS) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 473 | QualType FromType = From->getType(); |
| 474 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 475 | // Standard conversions (C++ [conv]) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 476 | SCS.setAsIdentityConversion(); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 477 | SCS.Deprecated = false; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 478 | SCS.IncompatibleObjC = false; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 479 | SCS.FromTypePtr = FromType.getAsOpaquePtr(); |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 480 | SCS.CopyConstructor = 0; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 481 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 482 | // There are no standard conversions for class types in C++, so |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | // abort early. When overloading in C, however, we do permit |
Douglas Gregor | f9201e0 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 | // When we're overloading in C, we allow, as standard conversions, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | // Lvalue-to-rvalue conversion (C++ 4.1): |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 499 | if (argIsLvalue == Expr::LV_Valid && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 500 | !FromType->isFunctionType() && !FromType->isArrayType() && |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 501 | Context.getCanonicalType(FromType) != Context.OverloadTy) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 502 | SCS.First = ICK_Lvalue_To_Rvalue; |
Douglas Gregor | 8e9bebd | 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 | f9201e0 | 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 | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 510 | FromType = FromType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 511 | } else if (FromType->isArrayType()) { |
| 512 | // Array-to-pointer conversion (C++ 4.2) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 513 | SCS.First = ICK_Array_To_Pointer; |
Douglas Gregor | 8e9bebd | 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 | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 522 | SCS.Deprecated = true; |
Douglas Gregor | 8e9bebd | 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 | 60d62c2 | 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 | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 532 | } |
Mike Stump | ac5fc7c | 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 | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 535 | SCS.First = ICK_Function_To_Pointer; |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 541 | } else if (FunctionDecl *Fn |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 542 | = ResolveAddressOfOverloadedFunction(From, ToType, false)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 543 | // Address of overloaded function (C++ [over.over]). |
Douglas Gregor | 904eed3 | 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 | 7c80bd6 | 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 | 33b399a | 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 | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 564 | FromType = Context.getPointerType(FromType); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 565 | } else { |
| 566 | // We don't require any conversions for the first step. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 567 | SCS.First = ICK_Identity; |
Douglas Gregor | 8e9bebd | 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 | f9201e0 | 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 | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 576 | bool IncompatibleObjC = false; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 577 | if (Context.hasSameUnqualifiedType(FromType, ToType)) { |
Douglas Gregor | 8e9bebd | 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 | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 580 | SCS.Second = ICK_Identity; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 581 | } else if (IsIntegralPromotion(From, FromType, ToType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 582 | // Integral promotion (C++ 4.5). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 583 | SCS.Second = ICK_Integral_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 584 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 585 | } else if (IsFloatingPointPromotion(FromType, ToType)) { |
| 586 | // Floating point promotion (C++ 4.6). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 587 | SCS.Second = ICK_Floating_Promotion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 588 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 589 | } else if (IsComplexPromotion(FromType, ToType)) { |
| 590 | // Complex promotion (Clang extension) |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 591 | SCS.Second = ICK_Complex_Promotion; |
| 592 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 593 | } else if ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 594 | (ToType->isIntegralType() && !ToType->isEnumeralType())) { |
Mike Stump | ac5fc7c | 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 | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 597 | SCS.Second = ICK_Integral_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 598 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 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 | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 601 | SCS.Second = ICK_Floating_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 602 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 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 | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 605 | SCS.Second = ICK_Complex_Conversion; |
| 606 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 607 | } else if ((FromType->isFloatingType() && |
| 608 | ToType->isIntegralType() && (!ToType->isBooleanType() && |
| 609 | !ToType->isEnumeralType())) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 610 | ((FromType->isIntegralType() || FromType->isEnumeralType()) && |
Mike Stump | ac5fc7c | 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 | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 614 | SCS.Second = ICK_Floating_Integral; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 615 | FromType = ToType.getUnqualifiedType(); |
Mike Stump | ac5fc7c | 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 | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 619 | SCS.Second = ICK_Complex_Real; |
| 620 | FromType = ToType.getUnqualifiedType(); |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 621 | } else if (IsPointerConversion(From, FromType, ToType, InOverloadResolution, |
| 622 | FromType, IncompatibleObjC)) { |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 623 | // Pointer conversions (C++ 4.10). |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 624 | SCS.Second = ICK_Pointer_Conversion; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 625 | SCS.IncompatibleObjC = IncompatibleObjC; |
Mike Stump | ac5fc7c | 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 | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 628 | SCS.Second = ICK_Pointer_Member; |
Mike Stump | ac5fc7c | 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 | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 637 | SCS.Second = ICK_Boolean_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 638 | FromType = Context.BoolTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | } else if (!getLangOptions().CPlusPlus && |
Mike Stump | ac5fc7c | 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 | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 642 | SCS.Second = ICK_Compatible_Conversion; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 643 | } else { |
| 644 | // No second conversion required. |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 645 | SCS.Second = ICK_Identity; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 648 | QualType CanonFrom; |
| 649 | QualType CanonTo; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 650 | // The third conversion can be a qualification conversion (C++ 4p1). |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 651 | if (IsQualificationConversion(FromType, ToType)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 652 | SCS.Third = ICK_Qualification; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 653 | FromType = ToType; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 654 | CanonFrom = Context.getCanonicalType(FromType); |
| 655 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 656 | } else { |
| 657 | // No conversion required |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 658 | SCS.Third = ICK_Identity; |
| 659 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 660 | // C++ [over.best.ics]p6: |
Douglas Gregor | 60d62c2 | 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 | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 664 | CanonFrom = Context.getCanonicalType(FromType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 665 | CanonTo = Context.getCanonicalType(ToType); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 666 | if (CanonFrom.getUnqualifiedType() == CanonTo.getUnqualifiedType() && |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 667 | CanonFrom.getCVRQualifiers() != CanonTo.getCVRQualifiers()) { |
| 668 | FromType = ToType; |
| 669 | CanonFrom = CanonTo; |
| 670 | } |
Douglas Gregor | 8e9bebd | 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 | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 675 | if (CanonFrom != CanonTo) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 676 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 677 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 678 | SCS.ToTypePtr = FromType.getAsOpaquePtr(); |
| 679 | return true; |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 686 | bool Sema::IsIntegralPromotion(Expr *From, QualType FromType, QualType ToType) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 687 | const BuiltinType *To = ToType->getAsBuiltinType(); |
Sebastian Redl | f7be944 | 2008-11-04 15:59:10 +0000 | [diff] [blame] | 688 | // All integers are built-in. |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 689 | if (!To) { |
| 690 | return false; |
| 691 | } |
Douglas Gregor | 8e9bebd | 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 | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 698 | if (FromType->isPromotableIntegerType() && !FromType->isBooleanType()) { |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 703 | (!FromType->isSignedIntegerType() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 704 | Context.getTypeSize(FromType) < Context.getTypeSize(ToType)))) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 705 | return To->getKind() == BuiltinType::Int; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 731 | QualType PromoteTypes[6] = { |
| 732 | Context.IntTy, Context.UnsignedIntTy, |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 733 | Context.LongTy, Context.UnsignedLongTy , |
| 734 | Context.LongLongTy, Context.UnsignedLongLongTy |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 735 | }; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 736 | for (int Idx = 0; Idx < 6; ++Idx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 737 | uint64_t ToSize = Context.getTypeSize(PromoteTypes[Idx]); |
| 738 | if (FromSize < ToSize || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 | (FromSize == ToSize && |
Douglas Gregor | 8e9bebd | 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 | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 744 | return Context.getCanonicalType(ToType).getUnqualifiedType() |
Douglas Gregor | 8e9bebd | 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 | 390b4cc | 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 | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 759 | using llvm::APSInt; |
| 760 | if (From) |
| 761 | if (FieldDecl *MemberDecl = From->getBitField()) { |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 762 | APSInt BitWidth; |
Douglas Gregor | 33bbbc5 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 767 | |
Douglas Gregor | 86f1940 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 773 | |
Douglas Gregor | 86f1940 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 779 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 780 | return false; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 781 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 782 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 783 | |
Douglas Gregor | 8e9bebd | 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 | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 786 | if (FromType->isBooleanType() && To->getKind() == BuiltinType::Int) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 787 | return true; |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 788 | } |
Douglas Gregor | 8e9bebd | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 796 | bool Sema::IsFloatingPointPromotion(QualType FromType, QualType ToType) { |
Douglas Gregor | 8e9bebd | 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 | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 800 | if (const BuiltinType *ToBuiltin = ToType->getAsBuiltinType()) { |
Douglas Gregor | 8e9bebd | 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 | 5cdf821 | 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 | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 815 | return false; |
| 816 | } |
| 817 | |
Douglas Gregor | 5cdf821 | 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 | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 822 | /// floating-point or integral promotion. |
Douglas Gregor | 5cdf821 | 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 | b7b5d13 | 2009-02-12 00:26:06 +0000 | [diff] [blame] | 833 | ToComplex->getElementType()) || |
| 834 | IsIntegralPromotion(0, FromComplex->getElementType(), |
| 835 | ToComplex->getElementType()); |
Douglas Gregor | 5cdf821 | 2009-02-12 00:15:05 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Douglas Gregor | cb7de52 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | static QualType |
| 844 | BuildSimilarlyQualifiedPointerType(const PointerType *FromPtr, |
Douglas Gregor | cb7de52 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 850 | |
| 851 | // Exact qualifier match -> return the pointer type we're converting to. |
Douglas Gregor | cb7de52 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 866 | static bool isNullPointerConstantForConversion(Expr *Expr, |
Anders Carlsson | bbf306b | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 877 | |
Douglas Gregor | 8e9bebd | 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 | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 884 | /// |
Douglas Gregor | 7ca0976 | 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 | 45920e8 | 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 | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 894 | bool Sema::IsPointerConversion(Expr *From, QualType FromType, QualType ToType, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 895 | bool InOverloadResolution, |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 896 | QualType& ConvertedType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | bool &IncompatibleObjC) { |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 898 | IncompatibleObjC = false; |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 899 | if (isObjCPointerConversion(FromType, ToType, ConvertedType, IncompatibleObjC)) |
| 900 | return true; |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 901 | |
Mike Stump | 1eb4433 | 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 | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 904 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 27b09ac | 2008-12-22 20:51:52 +0000 | [diff] [blame] | 905 | ConvertedType = ToType; |
| 906 | return true; |
| 907 | } |
| 908 | |
Douglas Gregor | 071f2ae | 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 | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 911 | ToType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 071f2ae | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 917 | if (ToType->isBlockPointerType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 918 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 071f2ae | 2008-11-27 00:15:41 +0000 | [diff] [blame] | 919 | ConvertedType = ToType; |
| 920 | return true; |
| 921 | } |
| 922 | |
Sebastian Redl | 6e8ed16 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 925 | if (ToType->isNullPtrType() && |
Anders Carlsson | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 926 | isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 927 | ConvertedType = ToType; |
| 928 | return true; |
| 929 | } |
| 930 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 931 | const PointerType* ToTypePtr = ToType->getAs<PointerType>(); |
Douglas Gregor | 8e9bebd | 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 | bbf306b | 2009-08-28 15:55:56 +0000 | [diff] [blame] | 936 | if (isNullPointerConstantForConversion(From, InOverloadResolution, Context)) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 937 | ConvertedType = ToType; |
| 938 | return true; |
| 939 | } |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 940 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 941 | // Beyond this point, both types need to be pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 942 | const PointerType *FromTypePtr = FromType->getAs<PointerType>(); |
Douglas Gregor | cb7de52 | 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 | 8e9bebd | 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 | bad0e65 | 2009-03-24 20:32:41 +0000 | [diff] [blame] | 952 | if (FromPointeeType->isObjectType() && ToPointeeType->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 953 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 954 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 955 | ToType, Context); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 956 | return true; |
| 957 | } |
| 958 | |
Douglas Gregor | f9201e0 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | if (!getLangOptions().CPlusPlus && |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 962 | Context.typesAreCompatible(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 963 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 964 | ToPointeeType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 965 | ToType, Context); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 966 | return true; |
| 967 | } |
| 968 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 969 | // C++ [conv.ptr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | // |
Douglas Gregor | bc0805a | 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 | 94b1dd2 | 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 | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 982 | if (getLangOptions().CPlusPlus && |
| 983 | FromPointeeType->isRecordType() && ToPointeeType->isRecordType() && |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 984 | IsDerivedFrom(FromPointeeType, ToPointeeType)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 985 | ConvertedType = BuildSimilarlyQualifiedPointerType(FromTypePtr, |
Douglas Gregor | bf40818 | 2008-11-27 00:52:49 +0000 | [diff] [blame] | 986 | ToPointeeType, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 987 | ToType, Context); |
| 988 | return true; |
| 989 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 990 | |
Douglas Gregor | c788751 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 997 | bool Sema::isObjCPointerConversion(QualType FromType, QualType ToType, |
Douglas Gregor | c788751 | 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 | 14108da | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 | const ObjCObjectPointerType *FromObjCPtr = |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1006 | FromType->getAsObjCObjectPointerType(); |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1007 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1008 | if (ToObjCPtr && FromObjCPtr) { |
Steve Naroff | de2e22d | 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 | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1010 | // pointer to any interface (in both directions). |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1011 | if (ToObjCPtr->isObjCBuiltinType() && FromObjCPtr->isObjCBuiltinType()) { |
Steve Naroff | 14108da | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1016 | if ((FromObjCPtr->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1017 | ToObjCPtr->isObjCQualifiedIdType()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1018 | Context.ObjCQualifiedIdTypesAreCompatible(ToType, FromType, |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 1019 | /*compare=*/false)) { |
Steve Naroff | 14108da | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1038 | } |
Steve Naroff | 14108da | 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 | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1040 | QualType ToPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1041 | if (const PointerType *ToCPtr = ToType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1042 | ToPointeeType = ToCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1043 | else if (const BlockPointerType *ToBlockPtr = ToType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1044 | ToPointeeType = ToBlockPtr->getPointeeType(); |
| 1045 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1046 | return false; |
| 1047 | |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1048 | QualType FromPointeeType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1049 | if (const PointerType *FromCPtr = FromType->getAs<PointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1050 | FromPointeeType = FromCPtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1051 | else if (const BlockPointerType *FromBlockPtr = FromType->getAs<BlockPointerType>()) |
Douglas Gregor | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1052 | FromPointeeType = FromBlockPtr->getPointeeType(); |
| 1053 | else |
Douglas Gregor | c788751 | 2008-12-19 19:13:09 +0000 | [diff] [blame] | 1054 | return false; |
| 1055 | |
Douglas Gregor | c788751 | 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 | 2a7e58d | 2008-12-23 00:53:59 +0000 | [diff] [blame] | 1066 | // If we have pointers to functions or blocks, check whether the only |
Douglas Gregor | c788751 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1070 | const FunctionProtoType *FromFunctionType |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1071 | = FromPointeeType->getAsFunctionProtoType(); |
| 1072 | const FunctionProtoType *ToFunctionType |
| 1073 | = ToPointeeType->getAsFunctionProtoType(); |
Douglas Gregor | c788751 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | |
Douglas Gregor | c788751 | 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 | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1129 | return false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Douglas Gregor | 94b1dd2 | 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 | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 1134 | /// ambiguous or inaccessible derived-to-base pointer |
Douglas Gregor | 94b1dd2 | 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. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1138 | bool Sema::CheckPointerConversion(Expr *From, QualType ToType, |
| 1139 | CastExpr::CastKind &Kind) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1140 | QualType FromType = From->getType(); |
| 1141 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1142 | if (const PointerType *FromPtrType = FromType->getAs<PointerType>()) |
| 1143 | if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) { |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1144 | QualType FromPointeeType = FromPtrType->getPointeeType(), |
| 1145 | ToPointeeType = ToPtrType->getPointeeType(); |
Douglas Gregor | dda7889 | 2008-12-18 23:43:31 +0000 | [diff] [blame] | 1146 | |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1147 | if (FromPointeeType->isRecordType() && |
| 1148 | ToPointeeType->isRecordType()) { |
| 1149 | // We must have a derived-to-base conversion. Check an |
| 1150 | // ambiguous or inaccessible conversion. |
Anders Carlsson | 61faec1 | 2009-09-12 04:46:44 +0000 | [diff] [blame] | 1151 | if (CheckDerivedToBaseConversion(FromPointeeType, ToPointeeType, |
| 1152 | From->getExprLoc(), |
| 1153 | From->getSourceRange())) |
| 1154 | return true; |
| 1155 | |
| 1156 | // The conversion was successful. |
| 1157 | Kind = CastExpr::CK_DerivedToBase; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1158 | } |
| 1159 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1160 | if (const ObjCObjectPointerType *FromPtrType = |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1161 | FromType->getAsObjCObjectPointerType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1162 | if (const ObjCObjectPointerType *ToPtrType = |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1163 | ToType->getAsObjCObjectPointerType()) { |
| 1164 | // Objective-C++ conversions are always okay. |
| 1165 | // FIXME: We should have a different class of conversions for the |
| 1166 | // Objective-C++ implicit conversions. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1167 | if (FromPtrType->isObjCBuiltinType() || ToPtrType->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1168 | return false; |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1169 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1170 | } |
Douglas Gregor | 94b1dd2 | 2008-10-24 04:54:22 +0000 | [diff] [blame] | 1171 | return false; |
| 1172 | } |
| 1173 | |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1174 | /// IsMemberPointerConversion - Determines whether the conversion of the |
| 1175 | /// expression From, which has the (possibly adjusted) type FromType, can be |
| 1176 | /// converted to the type ToType via a member pointer conversion (C++ 4.11). |
| 1177 | /// If so, returns true and places the converted type (that might differ from |
| 1178 | /// ToType in its cv-qualifiers at some level) into ConvertedType. |
| 1179 | bool Sema::IsMemberPointerConversion(Expr *From, QualType FromType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | QualType ToType, QualType &ConvertedType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1181 | const MemberPointerType *ToTypePtr = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1182 | if (!ToTypePtr) |
| 1183 | return false; |
| 1184 | |
| 1185 | // A null pointer constant can be converted to a member pointer (C++ 4.11p1) |
| 1186 | if (From->isNullPointerConstant(Context)) { |
| 1187 | ConvertedType = ToType; |
| 1188 | return true; |
| 1189 | } |
| 1190 | |
| 1191 | // Otherwise, both types have to be member pointers. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1192 | const MemberPointerType *FromTypePtr = FromType->getAs<MemberPointerType>(); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1193 | if (!FromTypePtr) |
| 1194 | return false; |
| 1195 | |
| 1196 | // A pointer to member of B can be converted to a pointer to member of D, |
| 1197 | // where D is derived from B (C++ 4.11p2). |
| 1198 | QualType FromClass(FromTypePtr->getClass(), 0); |
| 1199 | QualType ToClass(ToTypePtr->getClass(), 0); |
| 1200 | // FIXME: What happens when these are dependent? Is this function even called? |
| 1201 | |
| 1202 | if (IsDerivedFrom(ToClass, FromClass)) { |
| 1203 | ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(), |
| 1204 | ToClass.getTypePtr()); |
| 1205 | return true; |
| 1206 | } |
| 1207 | |
| 1208 | return false; |
| 1209 | } |
| 1210 | |
| 1211 | /// CheckMemberPointerConversion - Check the member pointer conversion from the |
| 1212 | /// expression From to the type ToType. This routine checks for ambiguous or |
| 1213 | /// virtual (FIXME: or inaccessible) base-to-derived member pointer conversions |
| 1214 | /// for which IsMemberPointerConversion has already returned true. It returns |
| 1215 | /// true and produces a diagnostic if there was an error, or returns false |
| 1216 | /// otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1217 | bool Sema::CheckMemberPointerConversion(Expr *From, QualType ToType, |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1218 | CastExpr::CastKind &Kind) { |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1219 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1220 | const MemberPointerType *FromPtrType = FromType->getAs<MemberPointerType>(); |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1221 | if (!FromPtrType) { |
| 1222 | // This must be a null pointer to member pointer conversion |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1223 | assert(From->isNullPointerConstant(Context) && |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1224 | "Expr must be null pointer constant!"); |
| 1225 | Kind = CastExpr::CK_NullToMemberPointer; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1226 | return false; |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1227 | } |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1228 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1229 | const MemberPointerType *ToPtrType = ToType->getAs<MemberPointerType>(); |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1230 | assert(ToPtrType && "No member pointer cast has a target type " |
| 1231 | "that is not a member pointer."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1232 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1233 | QualType FromClass = QualType(FromPtrType->getClass(), 0); |
| 1234 | QualType ToClass = QualType(ToPtrType->getClass(), 0); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1235 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1236 | // FIXME: What about dependent types? |
| 1237 | assert(FromClass->isRecordType() && "Pointer into non-class."); |
| 1238 | assert(ToClass->isRecordType() && "Pointer into non-class."); |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1239 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1240 | BasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, |
| 1241 | /*DetectVirtual=*/true); |
| 1242 | bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1243 | assert(DerivationOkay && |
| 1244 | "Should not have been called if derivation isn't OK."); |
| 1245 | (void)DerivationOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1246 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1247 | if (Paths.isAmbiguous(Context.getCanonicalType(FromClass). |
| 1248 | getUnqualifiedType())) { |
| 1249 | // Derivation is ambiguous. Redo the check to find the exact paths. |
| 1250 | Paths.clear(); |
| 1251 | Paths.setRecordingPaths(true); |
| 1252 | bool StillOkay = IsDerivedFrom(ToClass, FromClass, Paths); |
| 1253 | assert(StillOkay && "Derivation changed due to quantum fluctuation."); |
| 1254 | (void)StillOkay; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1255 | |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1256 | std::string PathDisplayStr = getAmbiguousPathsDisplayString(Paths); |
| 1257 | Diag(From->getExprLoc(), diag::err_ambiguous_memptr_conv) |
| 1258 | << 0 << FromClass << ToClass << PathDisplayStr << From->getSourceRange(); |
| 1259 | return true; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1260 | } |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1261 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1262 | if (const RecordType *VBase = Paths.getDetectedVirtual()) { |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1263 | Diag(From->getExprLoc(), diag::err_memptr_conv_via_virtual) |
| 1264 | << FromClass << ToClass << QualType(VBase, 0) |
| 1265 | << From->getSourceRange(); |
| 1266 | return true; |
| 1267 | } |
| 1268 | |
Anders Carlsson | 27a5b9b | 2009-08-22 23:33:40 +0000 | [diff] [blame] | 1269 | // Must be a base to derived member conversion. |
| 1270 | Kind = CastExpr::CK_BaseToDerivedMemberPointer; |
Sebastian Redl | 4433aaf | 2009-01-25 19:43:20 +0000 | [diff] [blame] | 1271 | return false; |
| 1272 | } |
| 1273 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1274 | /// IsQualificationConversion - Determines whether the conversion from |
| 1275 | /// an rvalue of type FromType to ToType is a qualification conversion |
| 1276 | /// (C++ 4.4). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1277 | bool |
| 1278 | Sema::IsQualificationConversion(QualType FromType, QualType ToType) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1279 | FromType = Context.getCanonicalType(FromType); |
| 1280 | ToType = Context.getCanonicalType(ToType); |
| 1281 | |
| 1282 | // If FromType and ToType are the same type, this is not a |
| 1283 | // qualification conversion. |
| 1284 | if (FromType == ToType) |
| 1285 | return false; |
Sebastian Redl | 21593ac | 2009-01-28 18:33:18 +0000 | [diff] [blame] | 1286 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1287 | // (C++ 4.4p4): |
| 1288 | // A conversion can add cv-qualifiers at levels other than the first |
| 1289 | // in multi-level pointers, subject to the following rules: [...] |
| 1290 | bool PreviousToQualsIncludeConst = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1291 | bool UnwrappedAnyPointer = false; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1292 | while (UnwrapSimilarPointerTypes(FromType, ToType)) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1293 | // Within each iteration of the loop, we check the qualifiers to |
| 1294 | // determine if this still looks like a qualification |
| 1295 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1296 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1297 | // until there are no more pointers or pointers-to-members left to |
| 1298 | // unwrap. |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1299 | UnwrappedAnyPointer = true; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1300 | |
| 1301 | // -- for every j > 0, if const is in cv 1,j then const is in cv |
| 1302 | // 2,j, and similarly for volatile. |
Douglas Gregor | 9b6e2d2 | 2008-10-22 00:38:21 +0000 | [diff] [blame] | 1303 | if (!ToType.isAtLeastAsQualifiedAs(FromType)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1304 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1305 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1306 | // -- if the cv 1,j and cv 2,j are different, then const is in |
| 1307 | // every cv for 0 < k < j. |
| 1308 | if (FromType.getCVRQualifiers() != ToType.getCVRQualifiers() |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1309 | && !PreviousToQualsIncludeConst) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1310 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1311 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1312 | // Keep track of whether all prior cv-qualifiers in the "to" type |
| 1313 | // include const. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1314 | PreviousToQualsIncludeConst |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1315 | = PreviousToQualsIncludeConst && ToType.isConstQualified(); |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1316 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 1317 | |
| 1318 | // We are left with FromType and ToType being the pointee types |
| 1319 | // after unwrapping the original FromType and ToType the same number |
| 1320 | // of types. If we unwrapped any pointers, and if FromType and |
| 1321 | // ToType have the same unqualified type (since we checked |
| 1322 | // qualifiers above), then this is a qualification conversion. |
| 1323 | return UnwrappedAnyPointer && |
| 1324 | FromType.getUnqualifiedType() == ToType.getUnqualifiedType(); |
| 1325 | } |
| 1326 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1327 | /// \brief Given a function template or function, extract the function template |
| 1328 | /// declaration (if any) and the underlying function declaration. |
| 1329 | template<typename T> |
| 1330 | static void GetFunctionAndTemplate(AnyFunctionDecl Orig, T *&Function, |
| 1331 | FunctionTemplateDecl *&FunctionTemplate) { |
| 1332 | FunctionTemplate = dyn_cast<FunctionTemplateDecl>(Orig); |
| 1333 | if (FunctionTemplate) |
| 1334 | Function = cast<T>(FunctionTemplate->getTemplatedDecl()); |
| 1335 | else |
| 1336 | Function = cast<T>(Orig); |
| 1337 | } |
| 1338 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1339 | /// Determines whether there is a user-defined conversion sequence |
| 1340 | /// (C++ [over.ics.user]) that converts expression From to the type |
| 1341 | /// ToType. If such a conversion exists, User will contain the |
| 1342 | /// user-defined conversion sequence that performs such a conversion |
| 1343 | /// and this routine will return true. Otherwise, this routine returns |
| 1344 | /// false and User is unspecified. |
| 1345 | /// |
| 1346 | /// \param AllowConversionFunctions true if the conversion should |
| 1347 | /// consider conversion functions at all. If false, only constructors |
| 1348 | /// will be considered. |
| 1349 | /// |
| 1350 | /// \param AllowExplicit true if the conversion should consider C++0x |
| 1351 | /// "explicit" conversion functions as well as non-explicit conversion |
| 1352 | /// functions (C++0x [class.conv.fct]p2). |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1353 | /// |
| 1354 | /// \param ForceRValue true if the expression should be treated as an rvalue |
| 1355 | /// for overload resolution. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1356 | bool Sema::IsUserDefinedConversion(Expr *From, QualType ToType, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 1357 | UserDefinedConversionSequence& User, |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1358 | bool AllowConversionFunctions, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1359 | bool AllowExplicit, bool ForceRValue) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1360 | OverloadCandidateSet CandidateSet; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1361 | if (const RecordType *ToRecordType = ToType->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1362 | if (CXXRecordDecl *ToRecordDecl |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1363 | = dyn_cast<CXXRecordDecl>(ToRecordType->getDecl())) { |
| 1364 | // C++ [over.match.ctor]p1: |
| 1365 | // When objects of class type are direct-initialized (8.5), or |
| 1366 | // copy-initialized from an expression of the same or a |
| 1367 | // derived class type (8.5), overload resolution selects the |
| 1368 | // constructor. [...] For copy-initialization, the candidate |
| 1369 | // functions are all the converting constructors (12.3.1) of |
| 1370 | // that class. The argument list is the expression-list within |
| 1371 | // the parentheses of the initializer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1372 | DeclarationName ConstructorName |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1373 | = Context.DeclarationNames.getCXXConstructorName( |
| 1374 | Context.getCanonicalType(ToType).getUnqualifiedType()); |
| 1375 | DeclContext::lookup_iterator Con, ConEnd; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1376 | for (llvm::tie(Con, ConEnd) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1377 | = ToRecordDecl->lookup(ConstructorName); |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1378 | Con != ConEnd; ++Con) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1379 | // Find the constructor (which may be a template). |
| 1380 | CXXConstructorDecl *Constructor = 0; |
| 1381 | FunctionTemplateDecl *ConstructorTmpl |
| 1382 | = dyn_cast<FunctionTemplateDecl>(*Con); |
| 1383 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1384 | Constructor |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1385 | = cast<CXXConstructorDecl>(ConstructorTmpl->getTemplatedDecl()); |
| 1386 | else |
| 1387 | Constructor = cast<CXXConstructorDecl>(*Con); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1388 | |
Fariborz Jahanian | 52ab92b | 2009-08-06 17:22:51 +0000 | [diff] [blame] | 1389 | if (!Constructor->isInvalidDecl() && |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1390 | Constructor->isConvertingConstructor(AllowExplicit)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1391 | if (ConstructorTmpl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1392 | AddTemplateOverloadCandidate(ConstructorTmpl, false, 0, 0, &From, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1393 | 1, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1394 | /*SuppressUserConversions=*/true, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1395 | ForceRValue); |
| 1396 | else |
| 1397 | AddOverloadCandidate(Constructor, &From, 1, CandidateSet, |
| 1398 | /*SuppressUserConversions=*/true, ForceRValue); |
| 1399 | } |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1400 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1401 | } |
| 1402 | } |
| 1403 | |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 1404 | if (!AllowConversionFunctions) { |
| 1405 | // Don't allow any conversion functions to enter the overload set. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1406 | } else if (RequireCompleteType(From->getLocStart(), From->getType(), |
| 1407 | PDiag(0) |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1408 | << From->getSourceRange())) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 1409 | // No conversion functions from incomplete types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | } else if (const RecordType *FromRecordType |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1411 | = From->getType()->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | if (CXXRecordDecl *FromRecordDecl |
Fariborz Jahanian | 8664ad5 | 2009-09-11 18:46:22 +0000 | [diff] [blame] | 1413 | = dyn_cast<CXXRecordDecl>(FromRecordType->getDecl())) { |
| 1414 | // Add all of the conversion functions as candidates. |
| 1415 | // FIXME: Look for conversions in base classes! |
| 1416 | OverloadedFunctionDecl *Conversions |
| 1417 | = FromRecordDecl->getConversionFunctions(); |
| 1418 | for (OverloadedFunctionDecl::function_iterator Func |
| 1419 | = Conversions->function_begin(); |
| 1420 | Func != Conversions->function_end(); ++Func) { |
| 1421 | CXXConversionDecl *Conv; |
| 1422 | FunctionTemplateDecl *ConvTemplate; |
| 1423 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
| 1424 | if (ConvTemplate) |
| 1425 | Conv = dyn_cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 1426 | else |
| 1427 | Conv = dyn_cast<CXXConversionDecl>(*Func); |
| 1428 | |
| 1429 | if (AllowExplicit || !Conv->isExplicit()) { |
| 1430 | if (ConvTemplate) |
| 1431 | AddTemplateConversionCandidate(ConvTemplate, From, ToType, |
| 1432 | CandidateSet); |
| 1433 | else |
| 1434 | AddConversionCandidate(Conv, From, ToType, CandidateSet); |
| 1435 | } |
| 1436 | } |
| 1437 | } |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1438 | } |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1439 | |
| 1440 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1441 | switch (BestViableFunction(CandidateSet, From->getLocStart(), Best)) { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1442 | case OR_Success: |
| 1443 | // Record the standard conversion we used and the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1444 | if (CXXConstructorDecl *Constructor |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1445 | = dyn_cast<CXXConstructorDecl>(Best->Function)) { |
| 1446 | // C++ [over.ics.user]p1: |
| 1447 | // If the user-defined conversion is specified by a |
| 1448 | // constructor (12.3.1), the initial standard conversion |
| 1449 | // sequence converts the source type to the type required by |
| 1450 | // the argument of the constructor. |
| 1451 | // |
| 1452 | // FIXME: What about ellipsis conversions? |
| 1453 | QualType ThisType = Constructor->getThisType(Context); |
| 1454 | User.Before = Best->Conversions[0].Standard; |
| 1455 | User.ConversionFunction = Constructor; |
| 1456 | User.After.setAsIdentityConversion(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 | User.After.FromTypePtr |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1458 | = ThisType->getAs<PointerType>()->getPointeeType().getAsOpaquePtr(); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1459 | User.After.ToTypePtr = ToType.getAsOpaquePtr(); |
| 1460 | return true; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1461 | } else if (CXXConversionDecl *Conversion |
| 1462 | = dyn_cast<CXXConversionDecl>(Best->Function)) { |
| 1463 | // C++ [over.ics.user]p1: |
| 1464 | // |
| 1465 | // [...] If the user-defined conversion is specified by a |
| 1466 | // conversion function (12.3.2), the initial standard |
| 1467 | // conversion sequence converts the source type to the |
| 1468 | // implicit object parameter of the conversion function. |
| 1469 | User.Before = Best->Conversions[0].Standard; |
| 1470 | User.ConversionFunction = Conversion; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | |
| 1472 | // C++ [over.ics.user]p2: |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1473 | // The second standard conversion sequence converts the |
| 1474 | // result of the user-defined conversion to the target type |
| 1475 | // for the sequence. Since an implicit conversion sequence |
| 1476 | // is an initialization, the special rules for |
| 1477 | // initialization by user-defined conversion apply when |
| 1478 | // selecting the best user-defined conversion for a |
| 1479 | // user-defined conversion sequence (see 13.3.3 and |
| 1480 | // 13.3.3.1). |
| 1481 | User.After = Best->FinalConversion; |
| 1482 | return true; |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1483 | } else { |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 1484 | assert(false && "Not a constructor or conversion function?"); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1485 | return false; |
| 1486 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1488 | case OR_No_Viable_Function: |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1489 | case OR_Deleted: |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1490 | // No conversion here! We're done. |
| 1491 | return false; |
| 1492 | |
| 1493 | case OR_Ambiguous: |
| 1494 | // FIXME: See C++ [over.best.ics]p10 for the handling of |
| 1495 | // ambiguous conversion sequences. |
| 1496 | return false; |
| 1497 | } |
| 1498 | |
| 1499 | return false; |
| 1500 | } |
| 1501 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1502 | /// CompareImplicitConversionSequences - Compare two implicit |
| 1503 | /// conversion sequences to determine whether one is better than the |
| 1504 | /// other or if they are indistinguishable (C++ 13.3.3.2). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1505 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1506 | Sema::CompareImplicitConversionSequences(const ImplicitConversionSequence& ICS1, |
| 1507 | const ImplicitConversionSequence& ICS2) |
| 1508 | { |
| 1509 | // (C++ 13.3.3.2p2): When comparing the basic forms of implicit |
| 1510 | // conversion sequences (as defined in 13.3.3.1) |
| 1511 | // -- a standard conversion sequence (13.3.3.1.1) is a better |
| 1512 | // conversion sequence than a user-defined conversion sequence or |
| 1513 | // an ellipsis conversion sequence, and |
| 1514 | // -- a user-defined conversion sequence (13.3.3.1.2) is a better |
| 1515 | // conversion sequence than an ellipsis conversion sequence |
| 1516 | // (13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1517 | // |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1518 | if (ICS1.ConversionKind < ICS2.ConversionKind) |
| 1519 | return ImplicitConversionSequence::Better; |
| 1520 | else if (ICS2.ConversionKind < ICS1.ConversionKind) |
| 1521 | return ImplicitConversionSequence::Worse; |
| 1522 | |
| 1523 | // Two implicit conversion sequences of the same form are |
| 1524 | // indistinguishable conversion sequences unless one of the |
| 1525 | // following rules apply: (C++ 13.3.3.2p3): |
| 1526 | if (ICS1.ConversionKind == ImplicitConversionSequence::StandardConversion) |
| 1527 | return CompareStandardConversionSequences(ICS1.Standard, ICS2.Standard); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1528 | else if (ICS1.ConversionKind == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1529 | ImplicitConversionSequence::UserDefinedConversion) { |
| 1530 | // User-defined conversion sequence U1 is a better conversion |
| 1531 | // sequence than another user-defined conversion sequence U2 if |
| 1532 | // they contain the same user-defined conversion function or |
| 1533 | // constructor and if the second standard conversion sequence of |
| 1534 | // U1 is better than the second standard conversion sequence of |
| 1535 | // U2 (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | if (ICS1.UserDefined.ConversionFunction == |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1537 | ICS2.UserDefined.ConversionFunction) |
| 1538 | return CompareStandardConversionSequences(ICS1.UserDefined.After, |
| 1539 | ICS2.UserDefined.After); |
| 1540 | } |
| 1541 | |
| 1542 | return ImplicitConversionSequence::Indistinguishable; |
| 1543 | } |
| 1544 | |
| 1545 | /// CompareStandardConversionSequences - Compare two standard |
| 1546 | /// conversion sequences to determine whether one is better than the |
| 1547 | /// other or if they are indistinguishable (C++ 13.3.3.2p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1548 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1549 | Sema::CompareStandardConversionSequences(const StandardConversionSequence& SCS1, |
| 1550 | const StandardConversionSequence& SCS2) |
| 1551 | { |
| 1552 | // Standard conversion sequence S1 is a better conversion sequence |
| 1553 | // than standard conversion sequence S2 if (C++ 13.3.3.2p3): |
| 1554 | |
| 1555 | // -- S1 is a proper subsequence of S2 (comparing the conversion |
| 1556 | // sequences in the canonical form defined by 13.3.3.1.1, |
| 1557 | // excluding any Lvalue Transformation; the identity conversion |
| 1558 | // sequence is considered to be a subsequence of any |
| 1559 | // non-identity conversion sequence) or, if not that, |
| 1560 | if (SCS1.Second == SCS2.Second && SCS1.Third == SCS2.Third) |
| 1561 | // Neither is a proper subsequence of the other. Do nothing. |
| 1562 | ; |
| 1563 | else if ((SCS1.Second == ICK_Identity && SCS1.Third == SCS2.Third) || |
| 1564 | (SCS1.Third == ICK_Identity && SCS1.Second == SCS2.Second) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1565 | (SCS1.Second == ICK_Identity && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1566 | SCS1.Third == ICK_Identity)) |
| 1567 | // SCS1 is a proper subsequence of SCS2. |
| 1568 | return ImplicitConversionSequence::Better; |
| 1569 | else if ((SCS2.Second == ICK_Identity && SCS2.Third == SCS1.Third) || |
| 1570 | (SCS2.Third == ICK_Identity && SCS2.Second == SCS1.Second) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1571 | (SCS2.Second == ICK_Identity && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1572 | SCS2.Third == ICK_Identity)) |
| 1573 | // SCS2 is a proper subsequence of SCS1. |
| 1574 | return ImplicitConversionSequence::Worse; |
| 1575 | |
| 1576 | // -- the rank of S1 is better than the rank of S2 (by the rules |
| 1577 | // defined below), or, if not that, |
| 1578 | ImplicitConversionRank Rank1 = SCS1.getRank(); |
| 1579 | ImplicitConversionRank Rank2 = SCS2.getRank(); |
| 1580 | if (Rank1 < Rank2) |
| 1581 | return ImplicitConversionSequence::Better; |
| 1582 | else if (Rank2 < Rank1) |
| 1583 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1584 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1585 | // (C++ 13.3.3.2p4): Two conversion sequences with the same rank |
| 1586 | // are indistinguishable unless one of the following rules |
| 1587 | // applies: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1589 | // A conversion that is not a conversion of a pointer, or |
| 1590 | // pointer to member, to bool is better than another conversion |
| 1591 | // that is such a conversion. |
| 1592 | if (SCS1.isPointerConversionToBool() != SCS2.isPointerConversionToBool()) |
| 1593 | return SCS2.isPointerConversionToBool() |
| 1594 | ? ImplicitConversionSequence::Better |
| 1595 | : ImplicitConversionSequence::Worse; |
| 1596 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1597 | // C++ [over.ics.rank]p4b2: |
| 1598 | // |
| 1599 | // If class B is derived directly or indirectly from class A, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1600 | // conversion of B* to A* is better than conversion of B* to |
| 1601 | // void*, and conversion of A* to void* is better than conversion |
| 1602 | // of B* to void*. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1603 | bool SCS1ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1604 | = SCS1.isPointerConversionToVoidPointer(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1605 | bool SCS2ConvertsToVoid |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1606 | = SCS2.isPointerConversionToVoidPointer(Context); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1607 | if (SCS1ConvertsToVoid != SCS2ConvertsToVoid) { |
| 1608 | // Exactly one of the conversion sequences is a conversion to |
| 1609 | // a void pointer; it's the worse conversion. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1610 | return SCS2ConvertsToVoid ? ImplicitConversionSequence::Better |
| 1611 | : ImplicitConversionSequence::Worse; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1612 | } else if (!SCS1ConvertsToVoid && !SCS2ConvertsToVoid) { |
| 1613 | // Neither conversion sequence converts to a void pointer; compare |
| 1614 | // their derived-to-base conversions. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1615 | if (ImplicitConversionSequence::CompareKind DerivedCK |
| 1616 | = CompareDerivedToBaseConversions(SCS1, SCS2)) |
| 1617 | return DerivedCK; |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1618 | } else if (SCS1ConvertsToVoid && SCS2ConvertsToVoid) { |
| 1619 | // Both conversion sequences are conversions to void |
| 1620 | // pointers. Compare the source types to determine if there's an |
| 1621 | // inheritance relationship in their sources. |
| 1622 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1623 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1624 | |
| 1625 | // Adjust the types we're converting from via the array-to-pointer |
| 1626 | // conversion, if we need to. |
| 1627 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1628 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1629 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1630 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1631 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1632 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1633 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1634 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1635 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1636 | |
| 1637 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1638 | return ImplicitConversionSequence::Better; |
| 1639 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1640 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1641 | |
| 1642 | // Objective-C++: If one interface is more specific than the |
| 1643 | // other, it is the better one. |
| 1644 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAsObjCInterfaceType(); |
| 1645 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAsObjCInterfaceType(); |
| 1646 | if (FromIface1 && FromIface1) { |
| 1647 | if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1648 | return ImplicitConversionSequence::Better; |
| 1649 | else if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1650 | return ImplicitConversionSequence::Worse; |
| 1651 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1652 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1653 | |
| 1654 | // Compare based on qualification conversions (C++ 13.3.3.2p3, |
| 1655 | // bullet 3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1656 | if (ImplicitConversionSequence::CompareKind QualCK |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1657 | = CompareQualificationConversions(SCS1, SCS2)) |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1658 | return QualCK; |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1659 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1660 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding) { |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1661 | // C++0x [over.ics.rank]p3b4: |
| 1662 | // -- S1 and S2 are reference bindings (8.5.3) and neither refers to an |
| 1663 | // implicit object parameter of a non-static member function declared |
| 1664 | // without a ref-qualifier, and S1 binds an rvalue reference to an |
| 1665 | // rvalue and S2 binds an lvalue reference. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1666 | // FIXME: We don't know if we're dealing with the implicit object parameter, |
| 1667 | // or if the member function in this case has a ref qualifier. |
| 1668 | // (Of course, we don't have ref qualifiers yet.) |
| 1669 | if (SCS1.RRefBinding != SCS2.RRefBinding) |
| 1670 | return SCS1.RRefBinding ? ImplicitConversionSequence::Better |
| 1671 | : ImplicitConversionSequence::Worse; |
Sebastian Redl | f2e21e5 | 2009-03-22 23:49:27 +0000 | [diff] [blame] | 1672 | |
| 1673 | // C++ [over.ics.rank]p3b4: |
| 1674 | // -- S1 and S2 are reference bindings (8.5.3), and the types to |
| 1675 | // which the references refer are the same type except for |
| 1676 | // top-level cv-qualifiers, and the type to which the reference |
| 1677 | // initialized by S2 refers is more cv-qualified than the type |
| 1678 | // to which the reference initialized by S1 refers. |
Sebastian Redl | a984580 | 2009-03-29 15:27:50 +0000 | [diff] [blame] | 1679 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1680 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1681 | T1 = Context.getCanonicalType(T1); |
| 1682 | T2 = Context.getCanonicalType(T2); |
| 1683 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) { |
| 1684 | if (T2.isMoreQualifiedThan(T1)) |
| 1685 | return ImplicitConversionSequence::Better; |
| 1686 | else if (T1.isMoreQualifiedThan(T2)) |
| 1687 | return ImplicitConversionSequence::Worse; |
| 1688 | } |
| 1689 | } |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1690 | |
| 1691 | return ImplicitConversionSequence::Indistinguishable; |
| 1692 | } |
| 1693 | |
| 1694 | /// CompareQualificationConversions - Compares two standard conversion |
| 1695 | /// sequences to determine whether they can be ranked based on their |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1696 | /// qualification conversions (C++ 13.3.3.2p3 bullet 3). |
| 1697 | ImplicitConversionSequence::CompareKind |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1698 | Sema::CompareQualificationConversions(const StandardConversionSequence& SCS1, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1699 | const StandardConversionSequence& SCS2) { |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 1700 | // C++ 13.3.3.2p3: |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1701 | // -- S1 and S2 differ only in their qualification conversion and |
| 1702 | // yield similar types T1 and T2 (C++ 4.4), respectively, and the |
| 1703 | // cv-qualification signature of type T1 is a proper subset of |
| 1704 | // the cv-qualification signature of type T2, and S1 is not the |
| 1705 | // deprecated string literal array-to-pointer conversion (4.2). |
| 1706 | if (SCS1.First != SCS2.First || SCS1.Second != SCS2.Second || |
| 1707 | SCS1.Third != SCS2.Third || SCS1.Third != ICK_Qualification) |
| 1708 | return ImplicitConversionSequence::Indistinguishable; |
| 1709 | |
| 1710 | // FIXME: the example in the standard doesn't use a qualification |
| 1711 | // conversion (!) |
| 1712 | QualType T1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1713 | QualType T2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1714 | T1 = Context.getCanonicalType(T1); |
| 1715 | T2 = Context.getCanonicalType(T2); |
| 1716 | |
| 1717 | // If the types are the same, we won't learn anything by unwrapped |
| 1718 | // them. |
| 1719 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1720 | return ImplicitConversionSequence::Indistinguishable; |
| 1721 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1722 | ImplicitConversionSequence::CompareKind Result |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1723 | = ImplicitConversionSequence::Indistinguishable; |
| 1724 | while (UnwrapSimilarPointerTypes(T1, T2)) { |
| 1725 | // Within each iteration of the loop, we check the qualifiers to |
| 1726 | // determine if this still looks like a qualification |
| 1727 | // conversion. Then, if all is well, we unwrap one more level of |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 1728 | // pointers or pointers-to-members and do it all again |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1729 | // until there are no more pointers or pointers-to-members left |
| 1730 | // to unwrap. This essentially mimics what |
| 1731 | // IsQualificationConversion does, but here we're checking for a |
| 1732 | // strict subset of qualifiers. |
| 1733 | if (T1.getCVRQualifiers() == T2.getCVRQualifiers()) |
| 1734 | // The qualifiers are the same, so this doesn't tell us anything |
| 1735 | // about how the sequences rank. |
| 1736 | ; |
| 1737 | else if (T2.isMoreQualifiedThan(T1)) { |
| 1738 | // T1 has fewer qualifiers, so it could be the better sequence. |
| 1739 | if (Result == ImplicitConversionSequence::Worse) |
| 1740 | // Neither has qualifiers that are a subset of the other's |
| 1741 | // qualifiers. |
| 1742 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1743 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1744 | Result = ImplicitConversionSequence::Better; |
| 1745 | } else if (T1.isMoreQualifiedThan(T2)) { |
| 1746 | // T2 has fewer qualifiers, so it could be the better sequence. |
| 1747 | if (Result == ImplicitConversionSequence::Better) |
| 1748 | // Neither has qualifiers that are a subset of the other's |
| 1749 | // qualifiers. |
| 1750 | return ImplicitConversionSequence::Indistinguishable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1751 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1752 | Result = ImplicitConversionSequence::Worse; |
| 1753 | } else { |
| 1754 | // Qualifiers are disjoint. |
| 1755 | return ImplicitConversionSequence::Indistinguishable; |
| 1756 | } |
| 1757 | |
| 1758 | // If the types after this point are equivalent, we're done. |
| 1759 | if (T1.getUnqualifiedType() == T2.getUnqualifiedType()) |
| 1760 | break; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Douglas Gregor | 5737326 | 2008-10-22 14:17:15 +0000 | [diff] [blame] | 1763 | // Check that the winning standard conversion sequence isn't using |
| 1764 | // the deprecated string literal array to pointer conversion. |
| 1765 | switch (Result) { |
| 1766 | case ImplicitConversionSequence::Better: |
| 1767 | if (SCS1.Deprecated) |
| 1768 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1769 | break; |
| 1770 | |
| 1771 | case ImplicitConversionSequence::Indistinguishable: |
| 1772 | break; |
| 1773 | |
| 1774 | case ImplicitConversionSequence::Worse: |
| 1775 | if (SCS2.Deprecated) |
| 1776 | Result = ImplicitConversionSequence::Indistinguishable; |
| 1777 | break; |
| 1778 | } |
| 1779 | |
| 1780 | return Result; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1783 | /// CompareDerivedToBaseConversions - Compares two standard conversion |
| 1784 | /// sequences to determine whether they can be ranked based on their |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1785 | /// various kinds of derived-to-base conversions (C++ |
| 1786 | /// [over.ics.rank]p4b3). As part of these checks, we also look at |
| 1787 | /// conversions between Objective-C interface types. |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1788 | ImplicitConversionSequence::CompareKind |
| 1789 | Sema::CompareDerivedToBaseConversions(const StandardConversionSequence& SCS1, |
| 1790 | const StandardConversionSequence& SCS2) { |
| 1791 | QualType FromType1 = QualType::getFromOpaquePtr(SCS1.FromTypePtr); |
| 1792 | QualType ToType1 = QualType::getFromOpaquePtr(SCS1.ToTypePtr); |
| 1793 | QualType FromType2 = QualType::getFromOpaquePtr(SCS2.FromTypePtr); |
| 1794 | QualType ToType2 = QualType::getFromOpaquePtr(SCS2.ToTypePtr); |
| 1795 | |
| 1796 | // Adjust the types we're converting from via the array-to-pointer |
| 1797 | // conversion, if we need to. |
| 1798 | if (SCS1.First == ICK_Array_To_Pointer) |
| 1799 | FromType1 = Context.getArrayDecayedType(FromType1); |
| 1800 | if (SCS2.First == ICK_Array_To_Pointer) |
| 1801 | FromType2 = Context.getArrayDecayedType(FromType2); |
| 1802 | |
| 1803 | // Canonicalize all of the types. |
| 1804 | FromType1 = Context.getCanonicalType(FromType1); |
| 1805 | ToType1 = Context.getCanonicalType(ToType1); |
| 1806 | FromType2 = Context.getCanonicalType(FromType2); |
| 1807 | ToType2 = Context.getCanonicalType(ToType2); |
| 1808 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1809 | // C++ [over.ics.rank]p4b3: |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1810 | // |
| 1811 | // If class B is derived directly or indirectly from class A and |
| 1812 | // class C is derived directly or indirectly from B, |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1813 | // |
| 1814 | // For Objective-C, we let A, B, and C also be Objective-C |
| 1815 | // interfaces. |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1816 | |
| 1817 | // Compare based on pointer conversions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | if (SCS1.Second == ICK_Pointer_Conversion && |
Douglas Gregor | 7ca0976 | 2008-11-27 01:19:21 +0000 | [diff] [blame] | 1819 | SCS2.Second == ICK_Pointer_Conversion && |
| 1820 | /*FIXME: Remove if Objective-C id conversions get their own rank*/ |
| 1821 | FromType1->isPointerType() && FromType2->isPointerType() && |
| 1822 | ToType1->isPointerType() && ToType2->isPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | QualType FromPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1824 | = FromType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1825 | QualType ToPointee1 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1826 | = ToType1->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1827 | QualType FromPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1828 | = FromType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1829 | QualType ToPointee2 |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1830 | = ToType2->getAs<PointerType>()->getPointeeType().getUnqualifiedType(); |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1831 | |
| 1832 | const ObjCInterfaceType* FromIface1 = FromPointee1->getAsObjCInterfaceType(); |
| 1833 | const ObjCInterfaceType* FromIface2 = FromPointee2->getAsObjCInterfaceType(); |
| 1834 | const ObjCInterfaceType* ToIface1 = ToPointee1->getAsObjCInterfaceType(); |
| 1835 | const ObjCInterfaceType* ToIface2 = ToPointee2->getAsObjCInterfaceType(); |
| 1836 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1837 | // -- conversion of C* to B* is better than conversion of C* to A*, |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1838 | if (FromPointee1 == FromPointee2 && ToPointee1 != ToPointee2) { |
| 1839 | if (IsDerivedFrom(ToPointee1, ToPointee2)) |
| 1840 | return ImplicitConversionSequence::Better; |
| 1841 | else if (IsDerivedFrom(ToPointee2, ToPointee1)) |
| 1842 | return ImplicitConversionSequence::Worse; |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1843 | |
| 1844 | if (ToIface1 && ToIface2) { |
| 1845 | if (Context.canAssignObjCInterfaces(ToIface2, ToIface1)) |
| 1846 | return ImplicitConversionSequence::Better; |
| 1847 | else if (Context.canAssignObjCInterfaces(ToIface1, ToIface2)) |
| 1848 | return ImplicitConversionSequence::Worse; |
| 1849 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1850 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1851 | |
| 1852 | // -- conversion of B* to A* is better than conversion of C* to A*, |
| 1853 | if (FromPointee1 != FromPointee2 && ToPointee1 == ToPointee2) { |
| 1854 | if (IsDerivedFrom(FromPointee2, FromPointee1)) |
| 1855 | return ImplicitConversionSequence::Better; |
| 1856 | else if (IsDerivedFrom(FromPointee1, FromPointee2)) |
| 1857 | return ImplicitConversionSequence::Worse; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1858 | |
Douglas Gregor | cb7de52 | 2008-11-26 23:31:11 +0000 | [diff] [blame] | 1859 | if (FromIface1 && FromIface2) { |
| 1860 | if (Context.canAssignObjCInterfaces(FromIface1, FromIface2)) |
| 1861 | return ImplicitConversionSequence::Better; |
| 1862 | else if (Context.canAssignObjCInterfaces(FromIface2, FromIface1)) |
| 1863 | return ImplicitConversionSequence::Worse; |
| 1864 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1865 | } |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1868 | // Compare based on reference bindings. |
| 1869 | if (SCS1.ReferenceBinding && SCS2.ReferenceBinding && |
| 1870 | SCS1.Second == ICK_Derived_To_Base) { |
| 1871 | // -- binding of an expression of type C to a reference of type |
| 1872 | // B& is better than binding an expression of type C to a |
| 1873 | // reference of type A&, |
| 1874 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1875 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1876 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1877 | return ImplicitConversionSequence::Better; |
| 1878 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1879 | return ImplicitConversionSequence::Worse; |
| 1880 | } |
| 1881 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1882 | // -- binding of an expression of type B to a reference of type |
| 1883 | // A& is better than binding an expression of type C to a |
| 1884 | // reference of type A&, |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1885 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1886 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1887 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1888 | return ImplicitConversionSequence::Better; |
| 1889 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1890 | return ImplicitConversionSequence::Worse; |
| 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | |
| 1895 | // FIXME: conversion of A::* to B::* is better than conversion of |
| 1896 | // A::* to C::*, |
| 1897 | |
| 1898 | // FIXME: conversion of B::* to C::* is better than conversion of |
| 1899 | // A::* to C::*, and |
| 1900 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1901 | if (SCS1.CopyConstructor && SCS2.CopyConstructor && |
| 1902 | SCS1.Second == ICK_Derived_To_Base) { |
| 1903 | // -- conversion of C to B is better than conversion of C to A, |
| 1904 | if (FromType1.getUnqualifiedType() == FromType2.getUnqualifiedType() && |
| 1905 | ToType1.getUnqualifiedType() != ToType2.getUnqualifiedType()) { |
| 1906 | if (IsDerivedFrom(ToType1, ToType2)) |
| 1907 | return ImplicitConversionSequence::Better; |
| 1908 | else if (IsDerivedFrom(ToType2, ToType1)) |
| 1909 | return ImplicitConversionSequence::Worse; |
| 1910 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1911 | |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1912 | // -- conversion of B to A is better than conversion of C to A. |
| 1913 | if (FromType1.getUnqualifiedType() != FromType2.getUnqualifiedType() && |
| 1914 | ToType1.getUnqualifiedType() == ToType2.getUnqualifiedType()) { |
| 1915 | if (IsDerivedFrom(FromType2, FromType1)) |
| 1916 | return ImplicitConversionSequence::Better; |
| 1917 | else if (IsDerivedFrom(FromType1, FromType2)) |
| 1918 | return ImplicitConversionSequence::Worse; |
| 1919 | } |
| 1920 | } |
Douglas Gregor | f70bdb9 | 2008-10-29 14:50:44 +0000 | [diff] [blame] | 1921 | |
Douglas Gregor | bc0805a | 2008-10-23 00:40:37 +0000 | [diff] [blame] | 1922 | return ImplicitConversionSequence::Indistinguishable; |
| 1923 | } |
| 1924 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1925 | /// TryCopyInitialization - Try to copy-initialize a value of type |
| 1926 | /// ToType from the expression From. Return the implicit conversion |
| 1927 | /// sequence required to pass this argument, which may be a bad |
| 1928 | /// conversion sequence (meaning that the argument cannot be passed to |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 1929 | /// a parameter of this type). If @p SuppressUserConversions, then we |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1930 | /// do not permit any user-defined conversion sequences. If @p ForceRValue, |
| 1931 | /// then we treat @p From as an rvalue, even if it is an lvalue. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1932 | ImplicitConversionSequence |
| 1933 | Sema::TryCopyInitialization(Expr *From, QualType ToType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 1934 | bool SuppressUserConversions, bool ForceRValue, |
| 1935 | bool InOverloadResolution) { |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1936 | if (ToType->isReferenceType()) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1937 | ImplicitConversionSequence ICS; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1938 | CheckReferenceInit(From, ToType, |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 1939 | SuppressUserConversions, |
| 1940 | /*AllowExplicit=*/false, |
| 1941 | ForceRValue, |
| 1942 | &ICS); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1943 | return ICS; |
| 1944 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1945 | return TryImplicitConversion(From, ToType, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 1946 | SuppressUserConversions, |
| 1947 | /*AllowExplicit=*/false, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 1948 | ForceRValue, |
| 1949 | InOverloadResolution); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1950 | } |
| 1951 | } |
| 1952 | |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1953 | /// PerformCopyInitialization - Copy-initialize an object of type @p ToType with |
| 1954 | /// the expression @p From. Returns true (and emits a diagnostic) if there was |
| 1955 | /// an error, returns false if the initialization succeeded. Elidable should |
| 1956 | /// be true when the copy may be elided (C++ 12.8p15). Overload resolution works |
| 1957 | /// differently in C++0x for this case. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1958 | bool Sema::PerformCopyInitialization(Expr *&From, QualType ToType, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1959 | const char* Flavor, bool Elidable) { |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1960 | if (!getLangOptions().CPlusPlus) { |
| 1961 | // In C, argument passing is the same as performing an assignment. |
| 1962 | QualType FromType = From->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1963 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1964 | AssignConvertType ConvTy = |
| 1965 | CheckSingleAssignmentConstraints(ToType, From); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1966 | if (ConvTy != Compatible && |
| 1967 | CheckTransparentUnionArgumentConstraints(ToType, From) == Compatible) |
| 1968 | ConvTy = Compatible; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1969 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1970 | return DiagnoseAssignmentResult(ConvTy, From->getLocStart(), ToType, |
| 1971 | FromType, From, Flavor); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1972 | } |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1973 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1974 | if (ToType->isReferenceType()) |
Anders Carlsson | 2de3ace | 2009-08-27 17:30:43 +0000 | [diff] [blame] | 1975 | return CheckReferenceInit(From, ToType, |
| 1976 | /*SuppressUserConversions=*/false, |
| 1977 | /*AllowExplicit=*/false, |
| 1978 | /*ForceRValue=*/false); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1979 | |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1980 | if (!PerformImplicitConversion(From, ToType, Flavor, |
| 1981 | /*AllowExplicit=*/false, Elidable)) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1982 | return false; |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 1983 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1984 | return Diag(From->getSourceRange().getBegin(), |
| 1985 | diag::err_typecheck_convert_incompatible) |
| 1986 | << ToType << From->getType() << Flavor << From->getSourceRange(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 1989 | /// TryObjectArgumentInitialization - Try to initialize the object |
| 1990 | /// parameter of the given member function (@c Method) from the |
| 1991 | /// expression @p From. |
| 1992 | ImplicitConversionSequence |
| 1993 | Sema::TryObjectArgumentInitialization(Expr *From, CXXMethodDecl *Method) { |
| 1994 | QualType ClassType = Context.getTypeDeclType(Method->getParent()); |
| 1995 | unsigned MethodQuals = Method->getTypeQualifiers(); |
| 1996 | QualType ImplicitParamType = ClassType.getQualifiedType(MethodQuals); |
| 1997 | |
| 1998 | // Set up the conversion sequence as a "bad" conversion, to allow us |
| 1999 | // to exit early. |
| 2000 | ImplicitConversionSequence ICS; |
| 2001 | ICS.Standard.setAsIdentityConversion(); |
| 2002 | ICS.ConversionKind = ImplicitConversionSequence::BadConversion; |
| 2003 | |
| 2004 | // We need to have an object of class type. |
| 2005 | QualType FromType = From->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2006 | if (const PointerType *PT = FromType->getAs<PointerType>()) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2007 | FromType = PT->getPointeeType(); |
| 2008 | |
| 2009 | assert(FromType->isRecordType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2010 | |
| 2011 | // The implicit object parmeter is has the type "reference to cv X", |
| 2012 | // where X is the class of which the function is a member |
| 2013 | // (C++ [over.match.funcs]p4). However, when finding an implicit |
| 2014 | // conversion sequence for the argument, we are not allowed to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2015 | // create temporaries or perform user-defined conversions |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2016 | // (C++ [over.match.funcs]p5). We perform a simplified version of |
| 2017 | // reference binding here, that allows class rvalues to bind to |
| 2018 | // non-constant references. |
| 2019 | |
| 2020 | // First check the qualifiers. We don't care about lvalue-vs-rvalue |
| 2021 | // with the implicit object parameter (C++ [over.match.funcs]p5). |
| 2022 | QualType FromTypeCanon = Context.getCanonicalType(FromType); |
| 2023 | if (ImplicitParamType.getCVRQualifiers() != FromType.getCVRQualifiers() && |
| 2024 | !ImplicitParamType.isAtLeastAsQualifiedAs(FromType)) |
| 2025 | return ICS; |
| 2026 | |
| 2027 | // Check that we have either the same type or a derived type. It |
| 2028 | // affects the conversion rank. |
| 2029 | QualType ClassTypeCanon = Context.getCanonicalType(ClassType); |
| 2030 | if (ClassTypeCanon == FromTypeCanon.getUnqualifiedType()) |
| 2031 | ICS.Standard.Second = ICK_Identity; |
| 2032 | else if (IsDerivedFrom(FromType, ClassType)) |
| 2033 | ICS.Standard.Second = ICK_Derived_To_Base; |
| 2034 | else |
| 2035 | return ICS; |
| 2036 | |
| 2037 | // Success. Mark this as a reference binding. |
| 2038 | ICS.ConversionKind = ImplicitConversionSequence::StandardConversion; |
| 2039 | ICS.Standard.FromTypePtr = FromType.getAsOpaquePtr(); |
| 2040 | ICS.Standard.ToTypePtr = ImplicitParamType.getAsOpaquePtr(); |
| 2041 | ICS.Standard.ReferenceBinding = true; |
| 2042 | ICS.Standard.DirectBinding = true; |
Sebastian Redl | 8500239 | 2009-03-29 22:46:24 +0000 | [diff] [blame] | 2043 | ICS.Standard.RRefBinding = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2044 | return ICS; |
| 2045 | } |
| 2046 | |
| 2047 | /// PerformObjectArgumentInitialization - Perform initialization of |
| 2048 | /// the implicit object parameter for the given Method with the given |
| 2049 | /// expression. |
| 2050 | bool |
| 2051 | Sema::PerformObjectArgumentInitialization(Expr *&From, CXXMethodDecl *Method) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2052 | QualType FromRecordType, DestType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2053 | QualType ImplicitParamRecordType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2054 | Method->getThisType(Context)->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2055 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2056 | if (const PointerType *PT = From->getType()->getAs<PointerType>()) { |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2057 | FromRecordType = PT->getPointeeType(); |
| 2058 | DestType = Method->getThisType(Context); |
| 2059 | } else { |
| 2060 | FromRecordType = From->getType(); |
| 2061 | DestType = ImplicitParamRecordType; |
| 2062 | } |
| 2063 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2064 | ImplicitConversionSequence ICS |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2065 | = TryObjectArgumentInitialization(From, Method); |
| 2066 | if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) |
| 2067 | return Diag(From->getSourceRange().getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2068 | diag::err_implicit_object_parameter_init) |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2069 | << ImplicitParamRecordType << FromRecordType << From->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2070 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2071 | if (ICS.Standard.Second == ICK_Derived_To_Base && |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 2072 | CheckDerivedToBaseConversion(FromRecordType, |
| 2073 | ImplicitParamRecordType, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2074 | From->getSourceRange().getBegin(), |
| 2075 | From->getSourceRange())) |
| 2076 | return true; |
| 2077 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2078 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
Anders Carlsson | 116b7d9 | 2009-08-07 18:45:49 +0000 | [diff] [blame] | 2079 | /*isLvalue=*/true); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2080 | return false; |
| 2081 | } |
| 2082 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2083 | /// TryContextuallyConvertToBool - Attempt to contextually convert the |
| 2084 | /// expression From to bool (C++0x [conv]p3). |
| 2085 | ImplicitConversionSequence Sema::TryContextuallyConvertToBool(Expr *From) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2086 | return TryImplicitConversion(From, Context.BoolTy, |
Anders Carlsson | da7a18b | 2009-08-27 17:24:15 +0000 | [diff] [blame] | 2087 | // FIXME: Are these flags correct? |
| 2088 | /*SuppressUserConversions=*/false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2089 | /*AllowExplicit=*/true, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2090 | /*ForceRValue=*/false, |
| 2091 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | /// PerformContextuallyConvertToBool - Perform a contextual conversion |
| 2095 | /// of the expression From to bool (C++0x [conv]p3). |
| 2096 | bool Sema::PerformContextuallyConvertToBool(Expr *&From) { |
| 2097 | ImplicitConversionSequence ICS = TryContextuallyConvertToBool(From); |
| 2098 | if (!PerformImplicitConversion(From, Context.BoolTy, ICS, "converting")) |
| 2099 | return false; |
| 2100 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2101 | return Diag(From->getSourceRange().getBegin(), |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2102 | diag::err_typecheck_bool_condition) |
| 2103 | << From->getType() << From->getSourceRange(); |
| 2104 | } |
| 2105 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2106 | /// AddOverloadCandidate - Adds the given function to the set of |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2107 | /// candidate functions, using the given function call arguments. If |
| 2108 | /// @p SuppressUserConversions, then don't allow user-defined |
| 2109 | /// conversions via constructors or conversion operators. |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2110 | /// If @p ForceRValue, treat all arguments as rvalues. This is a slightly |
| 2111 | /// hacky way to implement the overloading rules for elidable copy |
| 2112 | /// initialization in C++0x (C++0x 12.8p15). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2113 | void |
| 2114 | Sema::AddOverloadCandidate(FunctionDecl *Function, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2115 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 225c41e | 2008-11-03 19:09:14 +0000 | [diff] [blame] | 2116 | OverloadCandidateSet& CandidateSet, |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2117 | bool SuppressUserConversions, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2118 | bool ForceRValue) { |
| 2119 | const FunctionProtoType* Proto |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2120 | = dyn_cast<FunctionProtoType>(Function->getType()->getAsFunctionType()); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2121 | assert(Proto && "Functions without a prototype cannot be overloaded"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2122 | assert(!isa<CXXConversionDecl>(Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2123 | "Use AddConversionCandidate for conversion functions"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2124 | assert(!Function->getDescribedFunctionTemplate() && |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2125 | "Use AddTemplateOverloadCandidate for function templates"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2126 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2127 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2128 | if (!isa<CXXConstructorDecl>(Method)) { |
| 2129 | // If we get here, it's because we're calling a member function |
| 2130 | // that is named without a member access expression (e.g., |
| 2131 | // "this->f") that was either written explicitly or created |
| 2132 | // implicitly. This can happen with a qualified call to a member |
| 2133 | // function, e.g., X::f(). We use a NULL object as the implied |
| 2134 | // object argument (C++ [over.call.func]p3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2135 | AddMethodCandidate(Method, 0, Args, NumArgs, CandidateSet, |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2136 | SuppressUserConversions, ForceRValue); |
| 2137 | return; |
| 2138 | } |
| 2139 | // We treat a constructor like a non-member function, since its object |
| 2140 | // argument doesn't participate in overload resolution. |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2141 | } |
| 2142 | |
| 2143 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2144 | // Add this candidate |
| 2145 | CandidateSet.push_back(OverloadCandidate()); |
| 2146 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2147 | Candidate.Function = Function; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2148 | Candidate.Viable = true; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2149 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2150 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2151 | |
| 2152 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2153 | |
| 2154 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2155 | // parameters is viable only if it has an ellipsis in its parameter |
| 2156 | // list (8.3.5). |
| 2157 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2158 | Candidate.Viable = false; |
| 2159 | return; |
| 2160 | } |
| 2161 | |
| 2162 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2163 | // is viable only if the (m+1)st parameter has a default argument |
| 2164 | // (8.3.6). For the purposes of overload resolution, the |
| 2165 | // parameter list is truncated on the right, so that there are |
| 2166 | // exactly m parameters. |
| 2167 | unsigned MinRequiredArgs = Function->getMinRequiredArguments(); |
| 2168 | if (NumArgs < MinRequiredArgs) { |
| 2169 | // Not enough arguments. |
| 2170 | Candidate.Viable = false; |
| 2171 | return; |
| 2172 | } |
| 2173 | |
| 2174 | // Determine the implicit conversion sequences for each of the |
| 2175 | // arguments. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2176 | Candidate.Conversions.resize(NumArgs); |
| 2177 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2178 | if (ArgIdx < NumArgsInProto) { |
| 2179 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2180 | // exist for each argument an implicit conversion sequence |
| 2181 | // (13.3.3.1) that converts that argument to the corresponding |
| 2182 | // parameter of F. |
| 2183 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2184 | Candidate.Conversions[ArgIdx] |
| 2185 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2186 | SuppressUserConversions, ForceRValue, |
| 2187 | /*InOverloadResolution=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2188 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2189 | == ImplicitConversionSequence::BadConversion) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2190 | Candidate.Viable = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2191 | break; |
| 2192 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2193 | } else { |
| 2194 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2195 | // argument for which there is no corresponding parameter is |
| 2196 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2197 | Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2198 | = ImplicitConversionSequence::EllipsisConversion; |
| 2199 | } |
| 2200 | } |
| 2201 | } |
| 2202 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2203 | /// \brief Add all of the function declarations in the given function set to |
| 2204 | /// the overload canddiate set. |
| 2205 | void Sema::AddFunctionCandidates(const FunctionSet &Functions, |
| 2206 | Expr **Args, unsigned NumArgs, |
| 2207 | OverloadCandidateSet& CandidateSet, |
| 2208 | bool SuppressUserConversions) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2209 | for (FunctionSet::const_iterator F = Functions.begin(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2210 | FEnd = Functions.end(); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2211 | F != FEnd; ++F) { |
| 2212 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*F)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2213 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2214 | SuppressUserConversions); |
| 2215 | else |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2216 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*F), |
| 2217 | /*FIXME: explicit args */false, 0, 0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2218 | Args, NumArgs, CandidateSet, |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 2219 | SuppressUserConversions); |
| 2220 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2223 | /// AddMethodCandidate - Adds the given C++ member function to the set |
| 2224 | /// of candidate functions, using the given function call arguments |
| 2225 | /// and the object argument (@c Object). For example, in a call |
| 2226 | /// @c o.f(a1,a2), @c Object will contain @c o and @c Args will contain |
| 2227 | /// both @c a1 and @c a2. If @p SuppressUserConversions, then don't |
| 2228 | /// allow user-defined conversions via constructors or conversion |
Sebastian Redl | e2b6833 | 2009-04-12 17:16:29 +0000 | [diff] [blame] | 2229 | /// operators. If @p ForceRValue, treat all arguments as rvalues. This is |
| 2230 | /// a slightly hacky way to implement the overloading rules for elidable copy |
| 2231 | /// initialization in C++0x (C++0x 12.8p15). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2232 | void |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2233 | Sema::AddMethodCandidate(CXXMethodDecl *Method, Expr *Object, |
| 2234 | Expr **Args, unsigned NumArgs, |
| 2235 | OverloadCandidateSet& CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2236 | bool SuppressUserConversions, bool ForceRValue) { |
| 2237 | const FunctionProtoType* Proto |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2238 | = dyn_cast<FunctionProtoType>(Method->getType()->getAsFunctionType()); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2239 | assert(Proto && "Methods without a prototype cannot be overloaded"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2240 | assert(!isa<CXXConversionDecl>(Method) && |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2241 | "Use AddConversionCandidate for conversion functions"); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 2242 | assert(!isa<CXXConstructorDecl>(Method) && |
| 2243 | "Use AddOverloadCandidate for constructors"); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2244 | |
| 2245 | // Add this candidate |
| 2246 | CandidateSet.push_back(OverloadCandidate()); |
| 2247 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2248 | Candidate.Function = Method; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2249 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2250 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2251 | |
| 2252 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2253 | |
| 2254 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2255 | // parameters is viable only if it has an ellipsis in its parameter |
| 2256 | // list (8.3.5). |
| 2257 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2258 | Candidate.Viable = false; |
| 2259 | return; |
| 2260 | } |
| 2261 | |
| 2262 | // (C++ 13.3.2p2): A candidate function having more than m parameters |
| 2263 | // is viable only if the (m+1)st parameter has a default argument |
| 2264 | // (8.3.6). For the purposes of overload resolution, the |
| 2265 | // parameter list is truncated on the right, so that there are |
| 2266 | // exactly m parameters. |
| 2267 | unsigned MinRequiredArgs = Method->getMinRequiredArguments(); |
| 2268 | if (NumArgs < MinRequiredArgs) { |
| 2269 | // Not enough arguments. |
| 2270 | Candidate.Viable = false; |
| 2271 | return; |
| 2272 | } |
| 2273 | |
| 2274 | Candidate.Viable = true; |
| 2275 | Candidate.Conversions.resize(NumArgs + 1); |
| 2276 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2277 | if (Method->isStatic() || !Object) |
| 2278 | // The implicit object argument is ignored. |
| 2279 | Candidate.IgnoreObjectArgument = true; |
| 2280 | else { |
| 2281 | // Determine the implicit conversion sequence for the object |
| 2282 | // parameter. |
| 2283 | Candidate.Conversions[0] = TryObjectArgumentInitialization(Object, Method); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2284 | if (Candidate.Conversions[0].ConversionKind |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2285 | == ImplicitConversionSequence::BadConversion) { |
| 2286 | Candidate.Viable = false; |
| 2287 | return; |
| 2288 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2289 | } |
| 2290 | |
| 2291 | // Determine the implicit conversion sequences for each of the |
| 2292 | // arguments. |
| 2293 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2294 | if (ArgIdx < NumArgsInProto) { |
| 2295 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2296 | // exist for each argument an implicit conversion sequence |
| 2297 | // (13.3.3.1) that converts that argument to the corresponding |
| 2298 | // parameter of F. |
| 2299 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2300 | Candidate.Conversions[ArgIdx + 1] |
| 2301 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2302 | SuppressUserConversions, ForceRValue, |
Anders Carlsson | 0897292 | 2009-08-28 15:33:32 +0000 | [diff] [blame] | 2303 | /*InOverloadResolution=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2304 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2305 | == ImplicitConversionSequence::BadConversion) { |
| 2306 | Candidate.Viable = false; |
| 2307 | break; |
| 2308 | } |
| 2309 | } else { |
| 2310 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2311 | // argument for which there is no corresponding parameter is |
| 2312 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2313 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2314 | = ImplicitConversionSequence::EllipsisConversion; |
| 2315 | } |
| 2316 | } |
| 2317 | } |
| 2318 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2319 | /// \brief Add a C++ member function template as a candidate to the candidate |
| 2320 | /// set, using template argument deduction to produce an appropriate member |
| 2321 | /// function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2322 | void |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2323 | Sema::AddMethodTemplateCandidate(FunctionTemplateDecl *MethodTmpl, |
| 2324 | bool HasExplicitTemplateArgs, |
| 2325 | const TemplateArgument *ExplicitTemplateArgs, |
| 2326 | unsigned NumExplicitTemplateArgs, |
| 2327 | Expr *Object, Expr **Args, unsigned NumArgs, |
| 2328 | OverloadCandidateSet& CandidateSet, |
| 2329 | bool SuppressUserConversions, |
| 2330 | bool ForceRValue) { |
| 2331 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2332 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2333 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2334 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2335 | // candidate functions in the usual way.113) A given name can refer to one |
| 2336 | // or more function templates and also to a set of overloaded non-template |
| 2337 | // functions. In such a case, the candidate functions generated from each |
| 2338 | // function template are combined with the set of non-template candidate |
| 2339 | // functions. |
| 2340 | TemplateDeductionInfo Info(Context); |
| 2341 | FunctionDecl *Specialization = 0; |
| 2342 | if (TemplateDeductionResult Result |
| 2343 | = DeduceTemplateArguments(MethodTmpl, HasExplicitTemplateArgs, |
| 2344 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 2345 | Args, NumArgs, Specialization, Info)) { |
| 2346 | // FIXME: Record what happened with template argument deduction, so |
| 2347 | // that we can give the user a beautiful diagnostic. |
| 2348 | (void)Result; |
| 2349 | return; |
| 2350 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2351 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2352 | // Add the function template specialization produced by template argument |
| 2353 | // deduction as a candidate. |
| 2354 | assert(Specialization && "Missing member function template specialization?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2355 | assert(isa<CXXMethodDecl>(Specialization) && |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2356 | "Specialization is not a member function?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2357 | AddMethodCandidate(cast<CXXMethodDecl>(Specialization), Object, Args, NumArgs, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2358 | CandidateSet, SuppressUserConversions, ForceRValue); |
| 2359 | } |
| 2360 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2361 | /// \brief Add a C++ function template specialization as a candidate |
| 2362 | /// in the candidate set, using template argument deduction to produce |
| 2363 | /// an appropriate function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2364 | void |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2365 | Sema::AddTemplateOverloadCandidate(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2366 | bool HasExplicitTemplateArgs, |
| 2367 | const TemplateArgument *ExplicitTemplateArgs, |
| 2368 | unsigned NumExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2369 | Expr **Args, unsigned NumArgs, |
| 2370 | OverloadCandidateSet& CandidateSet, |
| 2371 | bool SuppressUserConversions, |
| 2372 | bool ForceRValue) { |
| 2373 | // C++ [over.match.funcs]p7: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2374 | // In each case where a candidate is a function template, candidate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2375 | // function template specializations are generated using template argument |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2376 | // deduction (14.8.3, 14.8.2). Those candidates are then handled as |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2377 | // candidate functions in the usual way.113) A given name can refer to one |
| 2378 | // or more function templates and also to a set of overloaded non-template |
| 2379 | // functions. In such a case, the candidate functions generated from each |
| 2380 | // function template are combined with the set of non-template candidate |
| 2381 | // functions. |
| 2382 | TemplateDeductionInfo Info(Context); |
| 2383 | FunctionDecl *Specialization = 0; |
| 2384 | if (TemplateDeductionResult Result |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2385 | = DeduceTemplateArguments(FunctionTemplate, HasExplicitTemplateArgs, |
| 2386 | ExplicitTemplateArgs, NumExplicitTemplateArgs, |
| 2387 | Args, NumArgs, Specialization, Info)) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2388 | // FIXME: Record what happened with template argument deduction, so |
| 2389 | // that we can give the user a beautiful diagnostic. |
| 2390 | (void)Result; |
| 2391 | return; |
| 2392 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2393 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2394 | // Add the function template specialization produced by template argument |
| 2395 | // deduction as a candidate. |
| 2396 | assert(Specialization && "Missing function template specialization?"); |
| 2397 | AddOverloadCandidate(Specialization, Args, NumArgs, CandidateSet, |
| 2398 | SuppressUserConversions, ForceRValue); |
| 2399 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2400 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2401 | /// AddConversionCandidate - Add a C++ conversion function as a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2402 | /// candidate in the candidate set (C++ [over.match.conv], |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2403 | /// C++ [over.match.copy]). From is the expression we're converting from, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2404 | /// and ToType is the type that we're eventually trying to convert to |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2405 | /// (which may or may not be the same type as the type that the |
| 2406 | /// conversion function produces). |
| 2407 | void |
| 2408 | Sema::AddConversionCandidate(CXXConversionDecl *Conversion, |
| 2409 | Expr *From, QualType ToType, |
| 2410 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2411 | assert(!Conversion->getDescribedFunctionTemplate() && |
| 2412 | "Conversion function templates use AddTemplateConversionCandidate"); |
| 2413 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2414 | // Add this candidate |
| 2415 | CandidateSet.push_back(OverloadCandidate()); |
| 2416 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2417 | Candidate.Function = Conversion; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2418 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2419 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2420 | Candidate.FinalConversion.setAsIdentityConversion(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2421 | Candidate.FinalConversion.FromTypePtr |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2422 | = Conversion->getConversionType().getAsOpaquePtr(); |
| 2423 | Candidate.FinalConversion.ToTypePtr = ToType.getAsOpaquePtr(); |
| 2424 | |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2425 | // Determine the implicit conversion sequence for the implicit |
| 2426 | // object parameter. |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2427 | Candidate.Viable = true; |
| 2428 | Candidate.Conversions.resize(1); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2429 | Candidate.Conversions[0] = TryObjectArgumentInitialization(From, Conversion); |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2430 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2431 | if (Candidate.Conversions[0].ConversionKind |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2432 | == ImplicitConversionSequence::BadConversion) { |
| 2433 | Candidate.Viable = false; |
| 2434 | return; |
| 2435 | } |
| 2436 | |
| 2437 | // To determine what the conversion from the result of calling the |
| 2438 | // conversion function to the type we're eventually trying to |
| 2439 | // convert to (ToType), we need to synthesize a call to the |
| 2440 | // conversion function and attempt copy initialization from it. This |
| 2441 | // makes sure that we get the right semantics with respect to |
| 2442 | // lvalues/rvalues and the type. Fortunately, we can allocate this |
| 2443 | // call on the stack and we don't need its arguments to be |
| 2444 | // well-formed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2445 | DeclRefExpr ConversionRef(Conversion, Conversion->getType(), |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2446 | SourceLocation()); |
| 2447 | ImplicitCastExpr ConversionFn(Context.getPointerType(Conversion->getType()), |
Anders Carlsson | cdef2b7 | 2009-07-31 00:48:10 +0000 | [diff] [blame] | 2448 | CastExpr::CK_Unknown, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2449 | &ConversionRef, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2450 | |
| 2451 | // Note that it is safe to allocate CallExpr on the stack here because |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2452 | // there are 0 arguments (i.e., nothing is allocated using ASTContext's |
| 2453 | // allocator). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2454 | CallExpr Call(Context, &ConversionFn, 0, 0, |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2455 | Conversion->getConversionType().getNonReferenceType(), |
| 2456 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2457 | ImplicitConversionSequence ICS = |
| 2458 | TryCopyInitialization(&Call, ToType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2459 | /*SuppressUserConversions=*/true, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2460 | /*ForceRValue=*/false, |
| 2461 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2462 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2463 | switch (ICS.ConversionKind) { |
| 2464 | case ImplicitConversionSequence::StandardConversion: |
| 2465 | Candidate.FinalConversion = ICS.Standard; |
| 2466 | break; |
| 2467 | |
| 2468 | case ImplicitConversionSequence::BadConversion: |
| 2469 | Candidate.Viable = false; |
| 2470 | break; |
| 2471 | |
| 2472 | default: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2473 | assert(false && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 2474 | "Can only end up with a standard conversion sequence or failure"); |
| 2475 | } |
| 2476 | } |
| 2477 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2478 | /// \brief Adds a conversion function template specialization |
| 2479 | /// candidate to the overload set, using template argument deduction |
| 2480 | /// to deduce the template arguments of the conversion function |
| 2481 | /// template from the type that we are converting to (C++ |
| 2482 | /// [temp.deduct.conv]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2483 | void |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2484 | Sema::AddTemplateConversionCandidate(FunctionTemplateDecl *FunctionTemplate, |
| 2485 | Expr *From, QualType ToType, |
| 2486 | OverloadCandidateSet &CandidateSet) { |
| 2487 | assert(isa<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()) && |
| 2488 | "Only conversion function templates permitted here"); |
| 2489 | |
| 2490 | TemplateDeductionInfo Info(Context); |
| 2491 | CXXConversionDecl *Specialization = 0; |
| 2492 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2493 | = DeduceTemplateArguments(FunctionTemplate, ToType, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2494 | Specialization, Info)) { |
| 2495 | // FIXME: Record what happened with template argument deduction, so |
| 2496 | // that we can give the user a beautiful diagnostic. |
| 2497 | (void)Result; |
| 2498 | return; |
| 2499 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2500 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2501 | // Add the conversion function template specialization produced by |
| 2502 | // template argument deduction as a candidate. |
| 2503 | assert(Specialization && "Missing function template specialization?"); |
| 2504 | AddConversionCandidate(Specialization, From, ToType, CandidateSet); |
| 2505 | } |
| 2506 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2507 | /// AddSurrogateCandidate - Adds a "surrogate" candidate function that |
| 2508 | /// converts the given @c Object to a function pointer via the |
| 2509 | /// conversion function @c Conversion, and then attempts to call it |
| 2510 | /// with the given arguments (C++ [over.call.object]p2-4). Proto is |
| 2511 | /// the type of function that we'll eventually be calling. |
| 2512 | void Sema::AddSurrogateCandidate(CXXConversionDecl *Conversion, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2513 | const FunctionProtoType *Proto, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2514 | Expr *Object, Expr **Args, unsigned NumArgs, |
| 2515 | OverloadCandidateSet& CandidateSet) { |
| 2516 | CandidateSet.push_back(OverloadCandidate()); |
| 2517 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2518 | Candidate.Function = 0; |
| 2519 | Candidate.Surrogate = Conversion; |
| 2520 | Candidate.Viable = true; |
| 2521 | Candidate.IsSurrogate = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2522 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2523 | Candidate.Conversions.resize(NumArgs + 1); |
| 2524 | |
| 2525 | // Determine the implicit conversion sequence for the implicit |
| 2526 | // object parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2527 | ImplicitConversionSequence ObjectInit |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2528 | = TryObjectArgumentInitialization(Object, Conversion); |
| 2529 | if (ObjectInit.ConversionKind == ImplicitConversionSequence::BadConversion) { |
| 2530 | Candidate.Viable = false; |
| 2531 | return; |
| 2532 | } |
| 2533 | |
| 2534 | // The first conversion is actually a user-defined conversion whose |
| 2535 | // first conversion is ObjectInit's standard conversion (which is |
| 2536 | // effectively a reference binding). Record it as such. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2537 | Candidate.Conversions[0].ConversionKind |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2538 | = ImplicitConversionSequence::UserDefinedConversion; |
| 2539 | Candidate.Conversions[0].UserDefined.Before = ObjectInit.Standard; |
| 2540 | Candidate.Conversions[0].UserDefined.ConversionFunction = Conversion; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2541 | Candidate.Conversions[0].UserDefined.After |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2542 | = Candidate.Conversions[0].UserDefined.Before; |
| 2543 | Candidate.Conversions[0].UserDefined.After.setAsIdentityConversion(); |
| 2544 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2545 | // Find the |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2546 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2547 | |
| 2548 | // (C++ 13.3.2p2): A candidate function having fewer than m |
| 2549 | // parameters is viable only if it has an ellipsis in its parameter |
| 2550 | // list (8.3.5). |
| 2551 | if (NumArgs > NumArgsInProto && !Proto->isVariadic()) { |
| 2552 | Candidate.Viable = false; |
| 2553 | return; |
| 2554 | } |
| 2555 | |
| 2556 | // Function types don't have any default arguments, so just check if |
| 2557 | // we have enough arguments. |
| 2558 | if (NumArgs < NumArgsInProto) { |
| 2559 | // Not enough arguments. |
| 2560 | Candidate.Viable = false; |
| 2561 | return; |
| 2562 | } |
| 2563 | |
| 2564 | // Determine the implicit conversion sequences for each of the |
| 2565 | // arguments. |
| 2566 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
| 2567 | if (ArgIdx < NumArgsInProto) { |
| 2568 | // (C++ 13.3.2p3): for F to be a viable function, there shall |
| 2569 | // exist for each argument an implicit conversion sequence |
| 2570 | // (13.3.3.1) that converts that argument to the corresponding |
| 2571 | // parameter of F. |
| 2572 | QualType ParamType = Proto->getArgType(ArgIdx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2573 | Candidate.Conversions[ArgIdx + 1] |
| 2574 | = TryCopyInitialization(Args[ArgIdx], ParamType, |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2575 | /*SuppressUserConversions=*/false, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2576 | /*ForceRValue=*/false, |
| 2577 | /*InOverloadResolution=*/false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2578 | if (Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2579 | == ImplicitConversionSequence::BadConversion) { |
| 2580 | Candidate.Viable = false; |
| 2581 | break; |
| 2582 | } |
| 2583 | } else { |
| 2584 | // (C++ 13.3.2p2): For the purposes of overload resolution, any |
| 2585 | // argument for which there is no corresponding parameter is |
| 2586 | // considered to ""match the ellipsis" (C+ 13.3.3.1.3). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2587 | Candidate.Conversions[ArgIdx + 1].ConversionKind |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 2588 | = ImplicitConversionSequence::EllipsisConversion; |
| 2589 | } |
| 2590 | } |
| 2591 | } |
| 2592 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2593 | // FIXME: This will eventually be removed, once we've migrated all of the |
| 2594 | // operator overloading logic over to the scheme used by binary operators, which |
| 2595 | // works for template instantiation. |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2596 | void Sema::AddOperatorCandidates(OverloadedOperatorKind Op, Scope *S, |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2597 | SourceLocation OpLoc, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2598 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | f680a0f | 2009-02-04 16:44:47 +0000 | [diff] [blame] | 2599 | OverloadCandidateSet& CandidateSet, |
| 2600 | SourceRange OpRange) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2601 | |
| 2602 | FunctionSet Functions; |
| 2603 | |
| 2604 | QualType T1 = Args[0]->getType(); |
| 2605 | QualType T2; |
| 2606 | if (NumArgs > 1) |
| 2607 | T2 = Args[1]->getType(); |
| 2608 | |
| 2609 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2610 | if (S) |
| 2611 | LookupOverloadedOperatorName(Op, S, T1, T2, Functions); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 2612 | ArgumentDependentLookup(OpName, Args, NumArgs, Functions); |
| 2613 | AddFunctionCandidates(Functions, Args, NumArgs, CandidateSet); |
| 2614 | AddMemberOperatorCandidates(Op, OpLoc, Args, NumArgs, CandidateSet, OpRange); |
| 2615 | AddBuiltinOperatorCandidates(Op, Args, NumArgs, CandidateSet); |
| 2616 | } |
| 2617 | |
| 2618 | /// \brief Add overload candidates for overloaded operators that are |
| 2619 | /// member functions. |
| 2620 | /// |
| 2621 | /// Add the overloaded operator candidates that are member functions |
| 2622 | /// for the operator Op that was used in an operator expression such |
| 2623 | /// as "x Op y". , Args/NumArgs provides the operator arguments, and |
| 2624 | /// CandidateSet will store the added overload candidates. (C++ |
| 2625 | /// [over.match.oper]). |
| 2626 | void Sema::AddMemberOperatorCandidates(OverloadedOperatorKind Op, |
| 2627 | SourceLocation OpLoc, |
| 2628 | Expr **Args, unsigned NumArgs, |
| 2629 | OverloadCandidateSet& CandidateSet, |
| 2630 | SourceRange OpRange) { |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2631 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 2632 | |
| 2633 | // C++ [over.match.oper]p3: |
| 2634 | // For a unary operator @ with an operand of a type whose |
| 2635 | // cv-unqualified version is T1, and for a binary operator @ with |
| 2636 | // a left operand of a type whose cv-unqualified version is T1 and |
| 2637 | // a right operand of a type whose cv-unqualified version is T2, |
| 2638 | // three sets of candidate functions, designated member |
| 2639 | // candidates, non-member candidates and built-in candidates, are |
| 2640 | // constructed as follows: |
| 2641 | QualType T1 = Args[0]->getType(); |
| 2642 | QualType T2; |
| 2643 | if (NumArgs > 1) |
| 2644 | T2 = Args[1]->getType(); |
| 2645 | |
| 2646 | // -- If T1 is a class type, the set of member candidates is the |
| 2647 | // result of the qualified lookup of T1::operator@ |
| 2648 | // (13.3.1.1.1); otherwise, the set of member candidates is |
| 2649 | // empty. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2650 | if (const RecordType *T1Rec = T1->getAs<RecordType>()) { |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2651 | // Complete the type if it can be completed. Otherwise, we're done. |
| 2652 | if (RequireCompleteType(OpLoc, T1, PartialDiagnostic(0))) |
| 2653 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2654 | |
| 2655 | LookupResult Operators = LookupQualifiedName(T1Rec->getDecl(), OpName, |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2656 | LookupOrdinaryName, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2657 | for (LookupResult::iterator Oper = Operators.begin(), |
Douglas Gregor | 8a5ae24 | 2009-08-27 23:35:55 +0000 | [diff] [blame] | 2658 | OperEnd = Operators.end(); |
| 2659 | Oper != OperEnd; |
| 2660 | ++Oper) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2661 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Args[0], |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2662 | Args+1, NumArgs - 1, CandidateSet, |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2663 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2664 | } |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2667 | /// AddBuiltinCandidate - Add a candidate for a built-in |
| 2668 | /// operator. ResultTy and ParamTys are the result and parameter types |
| 2669 | /// of the built-in candidate, respectively. Args and NumArgs are the |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2670 | /// arguments being passed to the candidate. IsAssignmentOperator |
| 2671 | /// should be true when this built-in candidate is an assignment |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2672 | /// operator. NumContextualBoolArguments is the number of arguments |
| 2673 | /// (at the beginning of the argument list) that will be contextually |
| 2674 | /// converted to bool. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2675 | void Sema::AddBuiltinCandidate(QualType ResultTy, QualType *ParamTys, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2676 | Expr **Args, unsigned NumArgs, |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2677 | OverloadCandidateSet& CandidateSet, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2678 | bool IsAssignmentOperator, |
| 2679 | unsigned NumContextualBoolArguments) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2680 | // Add this candidate |
| 2681 | CandidateSet.push_back(OverloadCandidate()); |
| 2682 | OverloadCandidate& Candidate = CandidateSet.back(); |
| 2683 | Candidate.Function = 0; |
Douglas Gregor | c9467cf | 2008-12-12 02:00:36 +0000 | [diff] [blame] | 2684 | Candidate.IsSurrogate = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2685 | Candidate.IgnoreObjectArgument = false; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2686 | Candidate.BuiltinTypes.ResultTy = ResultTy; |
| 2687 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 2688 | Candidate.BuiltinTypes.ParamTypes[ArgIdx] = ParamTys[ArgIdx]; |
| 2689 | |
| 2690 | // Determine the implicit conversion sequences for each of the |
| 2691 | // arguments. |
| 2692 | Candidate.Viable = true; |
| 2693 | Candidate.Conversions.resize(NumArgs); |
| 2694 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2695 | // C++ [over.match.oper]p4: |
| 2696 | // For the built-in assignment operators, conversions of the |
| 2697 | // left operand are restricted as follows: |
| 2698 | // -- no temporaries are introduced to hold the left operand, and |
| 2699 | // -- no user-defined conversions are applied to the left |
| 2700 | // operand to achieve a type match with the left-most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2701 | // parameter of a built-in candidate. |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 2702 | // |
| 2703 | // We block these conversions by turning off user-defined |
| 2704 | // conversions, since that is the only way that initialization of |
| 2705 | // a reference to a non-class type can occur from something that |
| 2706 | // is not of the same type. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2707 | if (ArgIdx < NumContextualBoolArguments) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2708 | assert(ParamTys[ArgIdx] == Context.BoolTy && |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2709 | "Contextual conversion to bool requires bool type"); |
| 2710 | Candidate.Conversions[ArgIdx] = TryContextuallyConvertToBool(Args[ArgIdx]); |
| 2711 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2712 | Candidate.Conversions[ArgIdx] |
| 2713 | = TryCopyInitialization(Args[ArgIdx], ParamTys[ArgIdx], |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 2714 | ArgIdx == 0 && IsAssignmentOperator, |
Anders Carlsson | 7b361b5 | 2009-08-27 17:37:39 +0000 | [diff] [blame] | 2715 | /*ForceRValue=*/false, |
| 2716 | /*InOverloadResolution=*/false); |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2717 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2718 | if (Candidate.Conversions[ArgIdx].ConversionKind |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2719 | == ImplicitConversionSequence::BadConversion) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2720 | Candidate.Viable = false; |
Douglas Gregor | 96176b3 | 2008-11-18 23:14:02 +0000 | [diff] [blame] | 2721 | break; |
| 2722 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2723 | } |
| 2724 | } |
| 2725 | |
| 2726 | /// BuiltinCandidateTypeSet - A set of types that will be used for the |
| 2727 | /// candidate operator functions for built-in operators (C++ |
| 2728 | /// [over.built]). The types are separated into pointer types and |
| 2729 | /// enumeration types. |
| 2730 | class BuiltinCandidateTypeSet { |
| 2731 | /// TypeSet - A set of types. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2732 | typedef llvm::SmallPtrSet<QualType, 8> TypeSet; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2733 | |
| 2734 | /// PointerTypes - The set of pointer types that will be used in the |
| 2735 | /// built-in candidates. |
| 2736 | TypeSet PointerTypes; |
| 2737 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2738 | /// MemberPointerTypes - The set of member pointer types that will be |
| 2739 | /// used in the built-in candidates. |
| 2740 | TypeSet MemberPointerTypes; |
| 2741 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2742 | /// EnumerationTypes - The set of enumeration types that will be |
| 2743 | /// used in the built-in candidates. |
| 2744 | TypeSet EnumerationTypes; |
| 2745 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2746 | /// Sema - The semantic analysis instance where we are building the |
| 2747 | /// candidate type set. |
| 2748 | Sema &SemaRef; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2749 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2750 | /// Context - The AST context in which we will build the type sets. |
| 2751 | ASTContext &Context; |
| 2752 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2753 | bool AddPointerWithMoreQualifiedTypeVariants(QualType Ty); |
| 2754 | bool AddMemberPointerWithMoreQualifiedTypeVariants(QualType Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2755 | |
| 2756 | public: |
| 2757 | /// iterator - Iterates through the types that are part of the set. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2758 | typedef TypeSet::iterator iterator; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2759 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2760 | BuiltinCandidateTypeSet(Sema &SemaRef) |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2761 | : SemaRef(SemaRef), Context(SemaRef.Context) { } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2762 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2763 | void AddTypesConvertedFrom(QualType Ty, bool AllowUserConversions, |
| 2764 | bool AllowExplicitConversions); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2765 | |
| 2766 | /// pointer_begin - First pointer type found; |
| 2767 | iterator pointer_begin() { return PointerTypes.begin(); } |
| 2768 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2769 | /// pointer_end - Past the last pointer type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2770 | iterator pointer_end() { return PointerTypes.end(); } |
| 2771 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2772 | /// member_pointer_begin - First member pointer type found; |
| 2773 | iterator member_pointer_begin() { return MemberPointerTypes.begin(); } |
| 2774 | |
| 2775 | /// member_pointer_end - Past the last member pointer type found; |
| 2776 | iterator member_pointer_end() { return MemberPointerTypes.end(); } |
| 2777 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2778 | /// enumeration_begin - First enumeration type found; |
| 2779 | iterator enumeration_begin() { return EnumerationTypes.begin(); } |
| 2780 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2781 | /// enumeration_end - Past the last enumeration type found; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2782 | iterator enumeration_end() { return EnumerationTypes.end(); } |
| 2783 | }; |
| 2784 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2785 | /// AddPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty to |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2786 | /// the set of pointer types along with any more-qualified variants of |
| 2787 | /// that type. For example, if @p Ty is "int const *", this routine |
| 2788 | /// will add "int const *", "int const volatile *", "int const |
| 2789 | /// restrict *", and "int const volatile restrict *" to the set of |
| 2790 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 2791 | /// false otherwise. |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2792 | bool |
| 2793 | BuiltinCandidateTypeSet::AddPointerWithMoreQualifiedTypeVariants(QualType Ty) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2794 | // Insert this type. |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2795 | if (!PointerTypes.insert(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2796 | return false; |
| 2797 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2798 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2799 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2800 | // FIXME: Optimize this so that we don't keep trying to add the same types. |
| 2801 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2802 | // FIXME: Do we have to add CVR qualifiers at *all* levels to deal with all |
| 2803 | // pointer conversions that don't cast away constness? |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2804 | if (!PointeeTy.isConstQualified()) |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2805 | AddPointerWithMoreQualifiedTypeVariants |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2806 | (Context.getPointerType(PointeeTy.withConst())); |
| 2807 | if (!PointeeTy.isVolatileQualified()) |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2808 | AddPointerWithMoreQualifiedTypeVariants |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2809 | (Context.getPointerType(PointeeTy.withVolatile())); |
| 2810 | if (!PointeeTy.isRestrictQualified()) |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2811 | AddPointerWithMoreQualifiedTypeVariants |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2812 | (Context.getPointerType(PointeeTy.withRestrict())); |
| 2813 | } |
| 2814 | |
| 2815 | return true; |
| 2816 | } |
| 2817 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2818 | /// AddMemberPointerWithMoreQualifiedTypeVariants - Add the pointer type @p Ty |
| 2819 | /// to the set of pointer types along with any more-qualified variants of |
| 2820 | /// that type. For example, if @p Ty is "int const *", this routine |
| 2821 | /// will add "int const *", "int const volatile *", "int const |
| 2822 | /// restrict *", and "int const volatile restrict *" to the set of |
| 2823 | /// pointer types. Returns true if the add of @p Ty itself succeeded, |
| 2824 | /// false otherwise. |
| 2825 | bool |
| 2826 | BuiltinCandidateTypeSet::AddMemberPointerWithMoreQualifiedTypeVariants( |
| 2827 | QualType Ty) { |
| 2828 | // Insert this type. |
| 2829 | if (!MemberPointerTypes.insert(Ty)) |
| 2830 | return false; |
| 2831 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2832 | if (const MemberPointerType *PointerTy = Ty->getAs<MemberPointerType>()) { |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2833 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2834 | const Type *ClassTy = PointerTy->getClass(); |
| 2835 | // FIXME: Optimize this so that we don't keep trying to add the same types. |
| 2836 | |
| 2837 | if (!PointeeTy.isConstQualified()) |
| 2838 | AddMemberPointerWithMoreQualifiedTypeVariants |
| 2839 | (Context.getMemberPointerType(PointeeTy.withConst(), ClassTy)); |
| 2840 | if (!PointeeTy.isVolatileQualified()) |
| 2841 | AddMemberPointerWithMoreQualifiedTypeVariants |
| 2842 | (Context.getMemberPointerType(PointeeTy.withVolatile(), ClassTy)); |
| 2843 | if (!PointeeTy.isRestrictQualified()) |
| 2844 | AddMemberPointerWithMoreQualifiedTypeVariants |
| 2845 | (Context.getMemberPointerType(PointeeTy.withRestrict(), ClassTy)); |
| 2846 | } |
| 2847 | |
| 2848 | return true; |
| 2849 | } |
| 2850 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2851 | /// AddTypesConvertedFrom - Add each of the types to which the type @p |
| 2852 | /// Ty can be implicit converted to the given set of @p Types. We're |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2853 | /// primarily interested in pointer types and enumeration types. We also |
| 2854 | /// take member pointer types, for the conditional operator. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2855 | /// AllowUserConversions is true if we should look at the conversion |
| 2856 | /// functions of a class type, and AllowExplicitConversions if we |
| 2857 | /// should also include the explicit conversion functions of a class |
| 2858 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2859 | void |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2860 | BuiltinCandidateTypeSet::AddTypesConvertedFrom(QualType Ty, |
| 2861 | bool AllowUserConversions, |
| 2862 | bool AllowExplicitConversions) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2863 | // Only deal with canonical types. |
| 2864 | Ty = Context.getCanonicalType(Ty); |
| 2865 | |
| 2866 | // Look through reference types; they aren't part of the type of an |
| 2867 | // expression for the purposes of conversions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2868 | if (const ReferenceType *RefTy = Ty->getAs<ReferenceType>()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2869 | Ty = RefTy->getPointeeType(); |
| 2870 | |
| 2871 | // We don't care about qualifiers on the type. |
| 2872 | Ty = Ty.getUnqualifiedType(); |
| 2873 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2874 | if (const PointerType *PointerTy = Ty->getAs<PointerType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2875 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 2876 | |
| 2877 | // Insert our type, and its more-qualified variants, into the set |
| 2878 | // of types. |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2879 | if (!AddPointerWithMoreQualifiedTypeVariants(Ty)) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2880 | return; |
| 2881 | |
| 2882 | // Add 'cv void*' to our set of types. |
| 2883 | if (!Ty->isVoidType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2884 | QualType QualVoid |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2885 | = Context.VoidTy.getQualifiedType(PointeeTy.getCVRQualifiers()); |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2886 | AddPointerWithMoreQualifiedTypeVariants(Context.getPointerType(QualVoid)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
| 2889 | // If this is a pointer to a class type, add pointers to its bases |
| 2890 | // (with the same level of cv-qualification as the original |
| 2891 | // derived class, of course). |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2892 | if (const RecordType *PointeeRec = PointeeTy->getAs<RecordType>()) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2893 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(PointeeRec->getDecl()); |
| 2894 | for (CXXRecordDecl::base_class_iterator Base = ClassDecl->bases_begin(); |
| 2895 | Base != ClassDecl->bases_end(); ++Base) { |
| 2896 | QualType BaseTy = Context.getCanonicalType(Base->getType()); |
| 2897 | BaseTy = BaseTy.getQualifiedType(PointeeTy.getCVRQualifiers()); |
| 2898 | |
| 2899 | // Add the pointer type, recursively, so that we get all of |
| 2900 | // the indirect base classes, too. |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2901 | AddTypesConvertedFrom(Context.getPointerType(BaseTy), false, false); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2902 | } |
| 2903 | } |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 2904 | } else if (Ty->isMemberPointerType()) { |
| 2905 | // Member pointers are far easier, since the pointee can't be converted. |
| 2906 | if (!AddMemberPointerWithMoreQualifiedTypeVariants(Ty)) |
| 2907 | return; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2908 | } else if (Ty->isEnumeralType()) { |
Chris Lattner | e37b94c | 2009-03-29 00:04:01 +0000 | [diff] [blame] | 2909 | EnumerationTypes.insert(Ty); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2910 | } else if (AllowUserConversions) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2911 | if (const RecordType *TyRec = Ty->getAs<RecordType>()) { |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2912 | if (SemaRef.RequireCompleteType(SourceLocation(), Ty, 0)) { |
| 2913 | // No conversion functions in incomplete types. |
| 2914 | return; |
| 2915 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2916 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2917 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(TyRec->getDecl()); |
| 2918 | // FIXME: Visit conversion functions in the base classes, too. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2919 | OverloadedFunctionDecl *Conversions |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2920 | = ClassDecl->getConversionFunctions(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2921 | for (OverloadedFunctionDecl::function_iterator Func |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2922 | = Conversions->function_begin(); |
| 2923 | Func != Conversions->function_end(); ++Func) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2924 | CXXConversionDecl *Conv; |
| 2925 | FunctionTemplateDecl *ConvTemplate; |
| 2926 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
| 2927 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2928 | // Skip conversion function templates; they don't tell us anything |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2929 | // about which builtin types we can convert to. |
| 2930 | if (ConvTemplate) |
| 2931 | continue; |
| 2932 | |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 2933 | if (AllowExplicitConversions || !Conv->isExplicit()) |
| 2934 | AddTypesConvertedFrom(Conv->getConversionType(), false, false); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2935 | } |
| 2936 | } |
| 2937 | } |
| 2938 | } |
| 2939 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 2940 | /// \brief Helper function for AddBuiltinOperatorCandidates() that adds |
| 2941 | /// the volatile- and non-volatile-qualified assignment operators for the |
| 2942 | /// given type to the candidate set. |
| 2943 | static void AddBuiltinAssignmentOperatorCandidates(Sema &S, |
| 2944 | QualType T, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2945 | Expr **Args, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 2946 | unsigned NumArgs, |
| 2947 | OverloadCandidateSet &CandidateSet) { |
| 2948 | QualType ParamTypes[2]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2949 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 2950 | // T& operator=(T&, T) |
| 2951 | ParamTypes[0] = S.Context.getLValueReferenceType(T); |
| 2952 | ParamTypes[1] = T; |
| 2953 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 2954 | /*IsAssignmentOperator=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2955 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 2956 | if (!S.Context.getCanonicalType(T).isVolatileQualified()) { |
| 2957 | // volatile T& operator=(volatile T&, T) |
| 2958 | ParamTypes[0] = S.Context.getLValueReferenceType(T.withVolatile()); |
| 2959 | ParamTypes[1] = T; |
| 2960 | S.AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2961 | /*IsAssignmentOperator=*/true); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 2962 | } |
| 2963 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2964 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2965 | /// AddBuiltinOperatorCandidates - Add the appropriate built-in |
| 2966 | /// operator overloads to the candidate set (C++ [over.built]), based |
| 2967 | /// on the operator @p Op and the arguments given. For example, if the |
| 2968 | /// operator is a binary '+', this routine might add "int |
| 2969 | /// operator+(int, int)" to cover integer addition. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2970 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2971 | Sema::AddBuiltinOperatorCandidates(OverloadedOperatorKind Op, |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 2972 | Expr **Args, unsigned NumArgs, |
| 2973 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2974 | // The set of "promoted arithmetic types", which are the arithmetic |
| 2975 | // types are that preserved by promotion (C++ [over.built]p2). Note |
| 2976 | // that the first few of these types are the promoted integral |
| 2977 | // types; these types need to be first. |
| 2978 | // FIXME: What about complex? |
| 2979 | const unsigned FirstIntegralType = 0; |
| 2980 | const unsigned LastIntegralType = 13; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2981 | const unsigned FirstPromotedIntegralType = 7, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2982 | LastPromotedIntegralType = 13; |
| 2983 | const unsigned FirstPromotedArithmeticType = 7, |
| 2984 | LastPromotedArithmeticType = 16; |
| 2985 | const unsigned NumArithmeticTypes = 16; |
| 2986 | QualType ArithmeticTypes[NumArithmeticTypes] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2987 | Context.BoolTy, Context.CharTy, Context.WCharTy, |
| 2988 | // FIXME: Context.Char16Ty, Context.Char32Ty, |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 2989 | Context.SignedCharTy, Context.ShortTy, |
| 2990 | Context.UnsignedCharTy, Context.UnsignedShortTy, |
| 2991 | Context.IntTy, Context.LongTy, Context.LongLongTy, |
| 2992 | Context.UnsignedIntTy, Context.UnsignedLongTy, Context.UnsignedLongLongTy, |
| 2993 | Context.FloatTy, Context.DoubleTy, Context.LongDoubleTy |
| 2994 | }; |
| 2995 | |
| 2996 | // Find all of the types that the arguments can convert to, but only |
| 2997 | // if the operator we're looking at has built-in operator candidates |
| 2998 | // that make use of these types. |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 2999 | BuiltinCandidateTypeSet CandidateTypes(*this); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3000 | if (Op == OO_Less || Op == OO_Greater || Op == OO_LessEqual || |
| 3001 | Op == OO_GreaterEqual || Op == OO_EqualEqual || Op == OO_ExclaimEqual || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3002 | Op == OO_Plus || (Op == OO_Minus && NumArgs == 2) || Op == OO_Equal || |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3003 | Op == OO_PlusEqual || Op == OO_MinusEqual || Op == OO_Subscript || |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3004 | Op == OO_ArrowStar || Op == OO_PlusPlus || Op == OO_MinusMinus || |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3005 | (Op == OO_Star && NumArgs == 1) || Op == OO_Conditional) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3006 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3007 | CandidateTypes.AddTypesConvertedFrom(Args[ArgIdx]->getType(), |
| 3008 | true, |
| 3009 | (Op == OO_Exclaim || |
| 3010 | Op == OO_AmpAmp || |
| 3011 | Op == OO_PipePipe)); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
| 3014 | bool isComparison = false; |
| 3015 | switch (Op) { |
| 3016 | case OO_None: |
| 3017 | case NUM_OVERLOADED_OPERATORS: |
| 3018 | assert(false && "Expected an overloaded operator"); |
| 3019 | break; |
| 3020 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3021 | case OO_Star: // '*' is either unary or binary |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3022 | if (NumArgs == 1) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3023 | goto UnaryStar; |
| 3024 | else |
| 3025 | goto BinaryStar; |
| 3026 | break; |
| 3027 | |
| 3028 | case OO_Plus: // '+' is either unary or binary |
| 3029 | if (NumArgs == 1) |
| 3030 | goto UnaryPlus; |
| 3031 | else |
| 3032 | goto BinaryPlus; |
| 3033 | break; |
| 3034 | |
| 3035 | case OO_Minus: // '-' is either unary or binary |
| 3036 | if (NumArgs == 1) |
| 3037 | goto UnaryMinus; |
| 3038 | else |
| 3039 | goto BinaryMinus; |
| 3040 | break; |
| 3041 | |
| 3042 | case OO_Amp: // '&' is either unary or binary |
| 3043 | if (NumArgs == 1) |
| 3044 | goto UnaryAmp; |
| 3045 | else |
| 3046 | goto BinaryAmp; |
| 3047 | |
| 3048 | case OO_PlusPlus: |
| 3049 | case OO_MinusMinus: |
| 3050 | // C++ [over.built]p3: |
| 3051 | // |
| 3052 | // For every pair (T, VQ), where T is an arithmetic type, and VQ |
| 3053 | // is either volatile or empty, there exist candidate operator |
| 3054 | // functions of the form |
| 3055 | // |
| 3056 | // VQ T& operator++(VQ T&); |
| 3057 | // T operator++(VQ T&, int); |
| 3058 | // |
| 3059 | // C++ [over.built]p4: |
| 3060 | // |
| 3061 | // For every pair (T, VQ), where T is an arithmetic type other |
| 3062 | // than bool, and VQ is either volatile or empty, there exist |
| 3063 | // candidate operator functions of the form |
| 3064 | // |
| 3065 | // VQ T& operator--(VQ T&); |
| 3066 | // T operator--(VQ T&, int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3067 | for (unsigned Arith = (Op == OO_PlusPlus? 0 : 1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3068 | Arith < NumArithmeticTypes; ++Arith) { |
| 3069 | QualType ArithTy = ArithmeticTypes[Arith]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3070 | QualType ParamTypes[2] |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3071 | = { Context.getLValueReferenceType(ArithTy), Context.IntTy }; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3072 | |
| 3073 | // Non-volatile version. |
| 3074 | if (NumArgs == 1) |
| 3075 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3076 | else |
| 3077 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3078 | |
| 3079 | // Volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3080 | ParamTypes[0] = Context.getLValueReferenceType(ArithTy.withVolatile()); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3081 | if (NumArgs == 1) |
| 3082 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3083 | else |
| 3084 | AddBuiltinCandidate(ArithTy, ParamTypes, Args, 2, CandidateSet); |
| 3085 | } |
| 3086 | |
| 3087 | // C++ [over.built]p5: |
| 3088 | // |
| 3089 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3090 | // cv-unqualified object type, and VQ is either volatile or |
| 3091 | // empty, there exist candidate operator functions of the form |
| 3092 | // |
| 3093 | // T*VQ& operator++(T*VQ&); |
| 3094 | // T*VQ& operator--(T*VQ&); |
| 3095 | // T* operator++(T*VQ&, int); |
| 3096 | // T* operator--(T*VQ&, int); |
| 3097 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3098 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3099 | // Skip pointer types that aren't pointers to object types. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3100 | if (!(*Ptr)->getAs<PointerType>()->getPointeeType()->isObjectType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3101 | continue; |
| 3102 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3103 | QualType ParamTypes[2] = { |
| 3104 | Context.getLValueReferenceType(*Ptr), Context.IntTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3105 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3106 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3107 | // Without volatile |
| 3108 | if (NumArgs == 1) |
| 3109 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3110 | else |
| 3111 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3112 | |
| 3113 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified()) { |
| 3114 | // With volatile |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3115 | ParamTypes[0] = Context.getLValueReferenceType((*Ptr).withVolatile()); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3116 | if (NumArgs == 1) |
| 3117 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 1, CandidateSet); |
| 3118 | else |
| 3119 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3120 | } |
| 3121 | } |
| 3122 | break; |
| 3123 | |
| 3124 | UnaryStar: |
| 3125 | // C++ [over.built]p6: |
| 3126 | // For every cv-qualified or cv-unqualified object type T, there |
| 3127 | // exist candidate operator functions of the form |
| 3128 | // |
| 3129 | // T& operator*(T*); |
| 3130 | // |
| 3131 | // C++ [over.built]p7: |
| 3132 | // For every function type T, there exist candidate operator |
| 3133 | // functions of the form |
| 3134 | // T& operator*(T*); |
| 3135 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3136 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3137 | QualType ParamTy = *Ptr; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3138 | QualType PointeeTy = ParamTy->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3139 | AddBuiltinCandidate(Context.getLValueReferenceType(PointeeTy), |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3140 | &ParamTy, Args, 1, CandidateSet); |
| 3141 | } |
| 3142 | break; |
| 3143 | |
| 3144 | UnaryPlus: |
| 3145 | // C++ [over.built]p8: |
| 3146 | // For every type T, there exist candidate operator functions of |
| 3147 | // the form |
| 3148 | // |
| 3149 | // T* operator+(T*); |
| 3150 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3151 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3152 | QualType ParamTy = *Ptr; |
| 3153 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet); |
| 3154 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3155 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3156 | // Fall through |
| 3157 | |
| 3158 | UnaryMinus: |
| 3159 | // C++ [over.built]p9: |
| 3160 | // For every promoted arithmetic type T, there exist candidate |
| 3161 | // operator functions of the form |
| 3162 | // |
| 3163 | // T operator+(T); |
| 3164 | // T operator-(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3165 | for (unsigned Arith = FirstPromotedArithmeticType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3166 | Arith < LastPromotedArithmeticType; ++Arith) { |
| 3167 | QualType ArithTy = ArithmeticTypes[Arith]; |
| 3168 | AddBuiltinCandidate(ArithTy, &ArithTy, Args, 1, CandidateSet); |
| 3169 | } |
| 3170 | break; |
| 3171 | |
| 3172 | case OO_Tilde: |
| 3173 | // C++ [over.built]p10: |
| 3174 | // For every promoted integral type T, there exist candidate |
| 3175 | // operator functions of the form |
| 3176 | // |
| 3177 | // T operator~(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3178 | for (unsigned Int = FirstPromotedIntegralType; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3179 | Int < LastPromotedIntegralType; ++Int) { |
| 3180 | QualType IntTy = ArithmeticTypes[Int]; |
| 3181 | AddBuiltinCandidate(IntTy, &IntTy, Args, 1, CandidateSet); |
| 3182 | } |
| 3183 | break; |
| 3184 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3185 | case OO_New: |
| 3186 | case OO_Delete: |
| 3187 | case OO_Array_New: |
| 3188 | case OO_Array_Delete: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3189 | case OO_Call: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3190 | assert(false && "Special operators don't use AddBuiltinOperatorCandidates"); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3191 | break; |
| 3192 | |
| 3193 | case OO_Comma: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3194 | UnaryAmp: |
| 3195 | case OO_Arrow: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3196 | // C++ [over.match.oper]p3: |
| 3197 | // -- For the operator ',', the unary operator '&', or the |
| 3198 | // operator '->', the built-in candidates set is empty. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3199 | break; |
| 3200 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3201 | case OO_EqualEqual: |
| 3202 | case OO_ExclaimEqual: |
| 3203 | // C++ [over.match.oper]p16: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3204 | // For every pointer to member type T, there exist candidate operator |
| 3205 | // functions of the form |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3206 | // |
| 3207 | // bool operator==(T,T); |
| 3208 | // bool operator!=(T,T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3209 | for (BuiltinCandidateTypeSet::iterator |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3210 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3211 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3212 | MemPtr != MemPtrEnd; |
| 3213 | ++MemPtr) { |
| 3214 | QualType ParamTypes[2] = { *MemPtr, *MemPtr }; |
| 3215 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3216 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3217 | |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3218 | // Fall through |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3219 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3220 | case OO_Less: |
| 3221 | case OO_Greater: |
| 3222 | case OO_LessEqual: |
| 3223 | case OO_GreaterEqual: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3224 | // C++ [over.built]p15: |
| 3225 | // |
| 3226 | // For every pointer or enumeration type T, there exist |
| 3227 | // candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3228 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3229 | // bool operator<(T, T); |
| 3230 | // bool operator>(T, T); |
| 3231 | // bool operator<=(T, T); |
| 3232 | // bool operator>=(T, T); |
| 3233 | // bool operator==(T, T); |
| 3234 | // bool operator!=(T, T); |
| 3235 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3236 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3237 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3238 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3239 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3240 | for (BuiltinCandidateTypeSet::iterator Enum |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3241 | = CandidateTypes.enumeration_begin(); |
| 3242 | Enum != CandidateTypes.enumeration_end(); ++Enum) { |
| 3243 | QualType ParamTypes[2] = { *Enum, *Enum }; |
| 3244 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet); |
| 3245 | } |
| 3246 | |
| 3247 | // Fall through. |
| 3248 | isComparison = true; |
| 3249 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3250 | BinaryPlus: |
| 3251 | BinaryMinus: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3252 | if (!isComparison) { |
| 3253 | // We didn't fall through, so we must have OO_Plus or OO_Minus. |
| 3254 | |
| 3255 | // C++ [over.built]p13: |
| 3256 | // |
| 3257 | // For every cv-qualified or cv-unqualified object type T |
| 3258 | // there exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3259 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3260 | // T* operator+(T*, ptrdiff_t); |
| 3261 | // T& operator[](T*, ptrdiff_t); [BELOW] |
| 3262 | // T* operator-(T*, ptrdiff_t); |
| 3263 | // T* operator+(ptrdiff_t, T*); |
| 3264 | // T& operator[](ptrdiff_t, T*); [BELOW] |
| 3265 | // |
| 3266 | // C++ [over.built]p14: |
| 3267 | // |
| 3268 | // For every T, where T is a pointer to object type, there |
| 3269 | // exist candidate operator functions of the form |
| 3270 | // |
| 3271 | // ptrdiff_t operator-(T, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3272 | for (BuiltinCandidateTypeSet::iterator Ptr |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3273 | = CandidateTypes.pointer_begin(); |
| 3274 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3275 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
| 3276 | |
| 3277 | // operator+(T*, ptrdiff_t) or operator-(T*, ptrdiff_t) |
| 3278 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3279 | |
| 3280 | if (Op == OO_Plus) { |
| 3281 | // T* operator+(ptrdiff_t, T*); |
| 3282 | ParamTypes[0] = ParamTypes[1]; |
| 3283 | ParamTypes[1] = *Ptr; |
| 3284 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3285 | } else { |
| 3286 | // ptrdiff_t operator-(T, T); |
| 3287 | ParamTypes[1] = *Ptr; |
| 3288 | AddBuiltinCandidate(Context.getPointerDiffType(), ParamTypes, |
| 3289 | Args, 2, CandidateSet); |
| 3290 | } |
| 3291 | } |
| 3292 | } |
| 3293 | // Fall through |
| 3294 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3295 | case OO_Slash: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3296 | BinaryStar: |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3297 | Conditional: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3298 | // C++ [over.built]p12: |
| 3299 | // |
| 3300 | // For every pair of promoted arithmetic types L and R, there |
| 3301 | // exist candidate operator functions of the form |
| 3302 | // |
| 3303 | // LR operator*(L, R); |
| 3304 | // LR operator/(L, R); |
| 3305 | // LR operator+(L, R); |
| 3306 | // LR operator-(L, R); |
| 3307 | // bool operator<(L, R); |
| 3308 | // bool operator>(L, R); |
| 3309 | // bool operator<=(L, R); |
| 3310 | // bool operator>=(L, R); |
| 3311 | // bool operator==(L, R); |
| 3312 | // bool operator!=(L, R); |
| 3313 | // |
| 3314 | // where LR is the result of the usual arithmetic conversions |
| 3315 | // between types L and R. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3316 | // |
| 3317 | // C++ [over.built]p24: |
| 3318 | // |
| 3319 | // For every pair of promoted arithmetic types L and R, there exist |
| 3320 | // candidate operator functions of the form |
| 3321 | // |
| 3322 | // LR operator?(bool, L, R); |
| 3323 | // |
| 3324 | // where LR is the result of the usual arithmetic conversions |
| 3325 | // between types L and R. |
| 3326 | // Our candidates ignore the first parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3327 | for (unsigned Left = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3328 | Left < LastPromotedArithmeticType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3329 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3330 | Right < LastPromotedArithmeticType; ++Right) { |
| 3331 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3332 | QualType Result |
| 3333 | = isComparison |
| 3334 | ? Context.BoolTy |
| 3335 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3336 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3337 | } |
| 3338 | } |
| 3339 | break; |
| 3340 | |
| 3341 | case OO_Percent: |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3342 | BinaryAmp: |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3343 | case OO_Caret: |
| 3344 | case OO_Pipe: |
| 3345 | case OO_LessLess: |
| 3346 | case OO_GreaterGreater: |
| 3347 | // C++ [over.built]p17: |
| 3348 | // |
| 3349 | // For every pair of promoted integral types L and R, there |
| 3350 | // exist candidate operator functions of the form |
| 3351 | // |
| 3352 | // LR operator%(L, R); |
| 3353 | // LR operator&(L, R); |
| 3354 | // LR operator^(L, R); |
| 3355 | // LR operator|(L, R); |
| 3356 | // L operator<<(L, R); |
| 3357 | // L operator>>(L, R); |
| 3358 | // |
| 3359 | // where LR is the result of the usual arithmetic conversions |
| 3360 | // between types L and R. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3361 | for (unsigned Left = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3362 | Left < LastPromotedIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3363 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3364 | Right < LastPromotedIntegralType; ++Right) { |
| 3365 | QualType LandR[2] = { ArithmeticTypes[Left], ArithmeticTypes[Right] }; |
| 3366 | QualType Result = (Op == OO_LessLess || Op == OO_GreaterGreater) |
| 3367 | ? LandR[0] |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3368 | : Context.UsualArithmeticConversionsType(LandR[0], LandR[1]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3369 | AddBuiltinCandidate(Result, LandR, Args, 2, CandidateSet); |
| 3370 | } |
| 3371 | } |
| 3372 | break; |
| 3373 | |
| 3374 | case OO_Equal: |
| 3375 | // C++ [over.built]p20: |
| 3376 | // |
| 3377 | // For every pair (T, VQ), where T is an enumeration or |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3378 | // pointer to member type and VQ is either volatile or |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3379 | // empty, there exist candidate operator functions of the form |
| 3380 | // |
| 3381 | // VQ T& operator=(VQ T&, T); |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3382 | for (BuiltinCandidateTypeSet::iterator |
| 3383 | Enum = CandidateTypes.enumeration_begin(), |
| 3384 | EnumEnd = CandidateTypes.enumeration_end(); |
| 3385 | Enum != EnumEnd; ++Enum) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3386 | AddBuiltinAssignmentOperatorCandidates(*this, *Enum, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3387 | CandidateSet); |
| 3388 | for (BuiltinCandidateTypeSet::iterator |
| 3389 | MemPtr = CandidateTypes.member_pointer_begin(), |
| 3390 | MemPtrEnd = CandidateTypes.member_pointer_end(); |
| 3391 | MemPtr != MemPtrEnd; ++MemPtr) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3392 | AddBuiltinAssignmentOperatorCandidates(*this, *MemPtr, Args, 2, |
Douglas Gregor | 19b7b15 | 2009-08-24 13:43:27 +0000 | [diff] [blame] | 3393 | CandidateSet); |
| 3394 | // Fall through. |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3395 | |
| 3396 | case OO_PlusEqual: |
| 3397 | case OO_MinusEqual: |
| 3398 | // C++ [over.built]p19: |
| 3399 | // |
| 3400 | // For every pair (T, VQ), where T is any type and VQ is either |
| 3401 | // volatile or empty, there exist candidate operator functions |
| 3402 | // of the form |
| 3403 | // |
| 3404 | // T*VQ& operator=(T*VQ&, T*); |
| 3405 | // |
| 3406 | // C++ [over.built]p21: |
| 3407 | // |
| 3408 | // For every pair (T, VQ), where T is a cv-qualified or |
| 3409 | // cv-unqualified object type and VQ is either volatile or |
| 3410 | // empty, there exist candidate operator functions of the form |
| 3411 | // |
| 3412 | // T*VQ& operator+=(T*VQ&, ptrdiff_t); |
| 3413 | // T*VQ& operator-=(T*VQ&, ptrdiff_t); |
| 3414 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3415 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3416 | QualType ParamTypes[2]; |
| 3417 | ParamTypes[1] = (Op == OO_Equal)? *Ptr : Context.getPointerDiffType(); |
| 3418 | |
| 3419 | // non-volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3420 | ParamTypes[0] = Context.getLValueReferenceType(*Ptr); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3421 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3422 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3423 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3424 | if (!Context.getCanonicalType(*Ptr).isVolatileQualified()) { |
| 3425 | // volatile version |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3426 | ParamTypes[0] = Context.getLValueReferenceType((*Ptr).withVolatile()); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3427 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3428 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3429 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3430 | } |
| 3431 | // Fall through. |
| 3432 | |
| 3433 | case OO_StarEqual: |
| 3434 | case OO_SlashEqual: |
| 3435 | // C++ [over.built]p18: |
| 3436 | // |
| 3437 | // For every triple (L, VQ, R), where L is an arithmetic type, |
| 3438 | // VQ is either volatile or empty, and R is a promoted |
| 3439 | // arithmetic type, there exist candidate operator functions of |
| 3440 | // the form |
| 3441 | // |
| 3442 | // VQ L& operator=(VQ L&, R); |
| 3443 | // VQ L& operator*=(VQ L&, R); |
| 3444 | // VQ L& operator/=(VQ L&, R); |
| 3445 | // VQ L& operator+=(VQ L&, R); |
| 3446 | // VQ L& operator-=(VQ L&, R); |
| 3447 | for (unsigned Left = 0; Left < NumArithmeticTypes; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3448 | for (unsigned Right = FirstPromotedArithmeticType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3449 | Right < LastPromotedArithmeticType; ++Right) { |
| 3450 | QualType ParamTypes[2]; |
| 3451 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3452 | |
| 3453 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3454 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3455 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3456 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3457 | |
| 3458 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 3459 | ParamTypes[0] = ArithmeticTypes[Left].withVolatile(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3460 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
Douglas Gregor | 88b4bf2 | 2009-01-13 00:52:54 +0000 | [diff] [blame] | 3461 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet, |
| 3462 | /*IsAssigmentOperator=*/Op == OO_Equal); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3463 | } |
| 3464 | } |
| 3465 | break; |
| 3466 | |
| 3467 | case OO_PercentEqual: |
| 3468 | case OO_LessLessEqual: |
| 3469 | case OO_GreaterGreaterEqual: |
| 3470 | case OO_AmpEqual: |
| 3471 | case OO_CaretEqual: |
| 3472 | case OO_PipeEqual: |
| 3473 | // C++ [over.built]p22: |
| 3474 | // |
| 3475 | // For every triple (L, VQ, R), where L is an integral type, VQ |
| 3476 | // is either volatile or empty, and R is a promoted integral |
| 3477 | // type, there exist candidate operator functions of the form |
| 3478 | // |
| 3479 | // VQ L& operator%=(VQ L&, R); |
| 3480 | // VQ L& operator<<=(VQ L&, R); |
| 3481 | // VQ L& operator>>=(VQ L&, R); |
| 3482 | // VQ L& operator&=(VQ L&, R); |
| 3483 | // VQ L& operator^=(VQ L&, R); |
| 3484 | // VQ L& operator|=(VQ L&, R); |
| 3485 | for (unsigned Left = FirstIntegralType; Left < LastIntegralType; ++Left) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3486 | for (unsigned Right = FirstPromotedIntegralType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3487 | Right < LastPromotedIntegralType; ++Right) { |
| 3488 | QualType ParamTypes[2]; |
| 3489 | ParamTypes[1] = ArithmeticTypes[Right]; |
| 3490 | |
| 3491 | // Add this built-in operator as a candidate (VQ is empty). |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3492 | ParamTypes[0] = Context.getLValueReferenceType(ArithmeticTypes[Left]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3493 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3494 | |
| 3495 | // Add this built-in operator as a candidate (VQ is 'volatile'). |
| 3496 | ParamTypes[0] = ArithmeticTypes[Left]; |
| 3497 | ParamTypes[0].addVolatile(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3498 | ParamTypes[0] = Context.getLValueReferenceType(ParamTypes[0]); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3499 | AddBuiltinCandidate(ParamTypes[0], ParamTypes, Args, 2, CandidateSet); |
| 3500 | } |
| 3501 | } |
| 3502 | break; |
| 3503 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3504 | case OO_Exclaim: { |
| 3505 | // C++ [over.operator]p23: |
| 3506 | // |
| 3507 | // There also exist candidate operator functions of the form |
| 3508 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3509 | // bool operator!(bool); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3510 | // bool operator&&(bool, bool); [BELOW] |
| 3511 | // bool operator||(bool, bool); [BELOW] |
| 3512 | QualType ParamTy = Context.BoolTy; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3513 | AddBuiltinCandidate(ParamTy, &ParamTy, Args, 1, CandidateSet, |
| 3514 | /*IsAssignmentOperator=*/false, |
| 3515 | /*NumContextualBoolArguments=*/1); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3516 | break; |
| 3517 | } |
| 3518 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3519 | case OO_AmpAmp: |
| 3520 | case OO_PipePipe: { |
| 3521 | // C++ [over.operator]p23: |
| 3522 | // |
| 3523 | // There also exist candidate operator functions of the form |
| 3524 | // |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 3525 | // bool operator!(bool); [ABOVE] |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3526 | // bool operator&&(bool, bool); |
| 3527 | // bool operator||(bool, bool); |
| 3528 | QualType ParamTypes[2] = { Context.BoolTy, Context.BoolTy }; |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3529 | AddBuiltinCandidate(Context.BoolTy, ParamTypes, Args, 2, CandidateSet, |
| 3530 | /*IsAssignmentOperator=*/false, |
| 3531 | /*NumContextualBoolArguments=*/2); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3532 | break; |
| 3533 | } |
| 3534 | |
| 3535 | case OO_Subscript: |
| 3536 | // C++ [over.built]p13: |
| 3537 | // |
| 3538 | // For every cv-qualified or cv-unqualified object type T there |
| 3539 | // exist candidate operator functions of the form |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3540 | // |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3541 | // T* operator+(T*, ptrdiff_t); [ABOVE] |
| 3542 | // T& operator[](T*, ptrdiff_t); |
| 3543 | // T* operator-(T*, ptrdiff_t); [ABOVE] |
| 3544 | // T* operator+(ptrdiff_t, T*); [ABOVE] |
| 3545 | // T& operator[](ptrdiff_t, T*); |
| 3546 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(); |
| 3547 | Ptr != CandidateTypes.pointer_end(); ++Ptr) { |
| 3548 | QualType ParamTypes[2] = { *Ptr, Context.getPointerDiffType() }; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3549 | QualType PointeeType = (*Ptr)->getAs<PointerType>()->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3550 | QualType ResultTy = Context.getLValueReferenceType(PointeeType); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3551 | |
| 3552 | // T& operator[](T*, ptrdiff_t) |
| 3553 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3554 | |
| 3555 | // T& operator[](ptrdiff_t, T*); |
| 3556 | ParamTypes[0] = ParamTypes[1]; |
| 3557 | ParamTypes[1] = *Ptr; |
| 3558 | AddBuiltinCandidate(ResultTy, ParamTypes, Args, 2, CandidateSet); |
| 3559 | } |
| 3560 | break; |
| 3561 | |
| 3562 | case OO_ArrowStar: |
| 3563 | // FIXME: No support for pointer-to-members yet. |
| 3564 | break; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3565 | |
| 3566 | case OO_Conditional: |
| 3567 | // Note that we don't consider the first argument, since it has been |
| 3568 | // contextually converted to bool long ago. The candidates below are |
| 3569 | // therefore added as binary. |
| 3570 | // |
| 3571 | // C++ [over.built]p24: |
| 3572 | // For every type T, where T is a pointer or pointer-to-member type, |
| 3573 | // there exist candidate operator functions of the form |
| 3574 | // |
| 3575 | // T operator?(bool, T, T); |
| 3576 | // |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3577 | for (BuiltinCandidateTypeSet::iterator Ptr = CandidateTypes.pointer_begin(), |
| 3578 | E = CandidateTypes.pointer_end(); Ptr != E; ++Ptr) { |
| 3579 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3580 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3581 | } |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 3582 | for (BuiltinCandidateTypeSet::iterator Ptr = |
| 3583 | CandidateTypes.member_pointer_begin(), |
| 3584 | E = CandidateTypes.member_pointer_end(); Ptr != E; ++Ptr) { |
| 3585 | QualType ParamTypes[2] = { *Ptr, *Ptr }; |
| 3586 | AddBuiltinCandidate(*Ptr, ParamTypes, Args, 2, CandidateSet); |
| 3587 | } |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3588 | goto Conditional; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3589 | } |
| 3590 | } |
| 3591 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3592 | /// \brief Add function candidates found via argument-dependent lookup |
| 3593 | /// to the set of overloading candidates. |
| 3594 | /// |
| 3595 | /// This routine performs argument-dependent name lookup based on the |
| 3596 | /// given function name (which may also be an operator name) and adds |
| 3597 | /// all of the overload candidates found by ADL to the overload |
| 3598 | /// candidate set (C++ [basic.lookup.argdep]). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3599 | void |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3600 | Sema::AddArgumentDependentLookupCandidates(DeclarationName Name, |
| 3601 | Expr **Args, unsigned NumArgs, |
| 3602 | OverloadCandidateSet& CandidateSet) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3603 | FunctionSet Functions; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3604 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3605 | // Record all of the function candidates that we've already |
| 3606 | // added to the overload set, so that we don't add those same |
| 3607 | // candidates a second time. |
| 3608 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3609 | CandEnd = CandidateSet.end(); |
| 3610 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3611 | if (Cand->Function) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3612 | Functions.insert(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3613 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 3614 | Functions.insert(FunTmpl); |
| 3615 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3616 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3617 | ArgumentDependentLookup(Name, Args, NumArgs, Functions); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3618 | |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3619 | // Erase all of the candidates we already knew about. |
| 3620 | // FIXME: This is suboptimal. Is there a better way? |
| 3621 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3622 | CandEnd = CandidateSet.end(); |
| 3623 | Cand != CandEnd; ++Cand) |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3624 | if (Cand->Function) { |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3625 | Functions.erase(Cand->Function); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3626 | if (FunctionTemplateDecl *FunTmpl = Cand->Function->getPrimaryTemplate()) |
| 3627 | Functions.erase(FunTmpl); |
| 3628 | } |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 3629 | |
| 3630 | // For each of the ADL candidates we found, add it to the overload |
| 3631 | // set. |
| 3632 | for (FunctionSet::iterator Func = Functions.begin(), |
| 3633 | FuncEnd = Functions.end(); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3634 | Func != FuncEnd; ++Func) { |
| 3635 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*Func)) |
| 3636 | AddOverloadCandidate(FD, Args, NumArgs, CandidateSet); |
| 3637 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3638 | AddTemplateOverloadCandidate(cast<FunctionTemplateDecl>(*Func), |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3639 | /*FIXME: explicit args */false, 0, 0, |
| 3640 | Args, NumArgs, CandidateSet); |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 3641 | } |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3642 | } |
| 3643 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3644 | /// isBetterOverloadCandidate - Determines whether the first overload |
| 3645 | /// candidate is a better candidate than the second (C++ 13.3.3p1). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3646 | bool |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3647 | Sema::isBetterOverloadCandidate(const OverloadCandidate& Cand1, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3648 | const OverloadCandidate& Cand2) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3649 | // Define viable functions to be better candidates than non-viable |
| 3650 | // functions. |
| 3651 | if (!Cand2.Viable) |
| 3652 | return Cand1.Viable; |
| 3653 | else if (!Cand1.Viable) |
| 3654 | return false; |
| 3655 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3656 | // C++ [over.match.best]p1: |
| 3657 | // |
| 3658 | // -- if F is a static member function, ICS1(F) is defined such |
| 3659 | // that ICS1(F) is neither better nor worse than ICS1(G) for |
| 3660 | // any function G, and, symmetrically, ICS1(G) is neither |
| 3661 | // better nor worse than ICS1(F). |
| 3662 | unsigned StartArg = 0; |
| 3663 | if (Cand1.IgnoreObjectArgument || Cand2.IgnoreObjectArgument) |
| 3664 | StartArg = 1; |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3665 | |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3666 | // C++ [over.match.best]p1: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3667 | // A viable function F1 is defined to be a better function than another |
| 3668 | // viable function F2 if for all arguments i, ICSi(F1) is not a worse |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3669 | // conversion sequence than ICSi(F2), and then... |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3670 | unsigned NumArgs = Cand1.Conversions.size(); |
| 3671 | assert(Cand2.Conversions.size() == NumArgs && "Overload candidate mismatch"); |
| 3672 | bool HasBetterConversion = false; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3673 | for (unsigned ArgIdx = StartArg; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3674 | switch (CompareImplicitConversionSequences(Cand1.Conversions[ArgIdx], |
| 3675 | Cand2.Conversions[ArgIdx])) { |
| 3676 | case ImplicitConversionSequence::Better: |
| 3677 | // Cand1 has a better conversion sequence. |
| 3678 | HasBetterConversion = true; |
| 3679 | break; |
| 3680 | |
| 3681 | case ImplicitConversionSequence::Worse: |
| 3682 | // Cand1 can't be better than Cand2. |
| 3683 | return false; |
| 3684 | |
| 3685 | case ImplicitConversionSequence::Indistinguishable: |
| 3686 | // Do nothing. |
| 3687 | break; |
| 3688 | } |
| 3689 | } |
| 3690 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3691 | // -- for some argument j, ICSj(F1) is a better conversion sequence than |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3692 | // ICSj(F2), or, if not that, |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3693 | if (HasBetterConversion) |
| 3694 | return true; |
| 3695 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3696 | // - F1 is a non-template function and F2 is a function template |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3697 | // specialization, or, if not that, |
| 3698 | if (Cand1.Function && !Cand1.Function->getPrimaryTemplate() && |
| 3699 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
| 3700 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3701 | |
| 3702 | // -- F1 and F2 are function template specializations, and the function |
| 3703 | // template for F1 is more specialized than the template for F2 |
| 3704 | // according to the partial ordering rules described in 14.5.5.2, or, |
Douglas Gregor | 3e15cc3 | 2009-07-07 23:38:56 +0000 | [diff] [blame] | 3705 | // if not that, |
Douglas Gregor | 1f561c1 | 2009-08-02 23:46:29 +0000 | [diff] [blame] | 3706 | if (Cand1.Function && Cand1.Function->getPrimaryTemplate() && |
| 3707 | Cand2.Function && Cand2.Function->getPrimaryTemplate()) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3708 | if (FunctionTemplateDecl *BetterTemplate |
| 3709 | = getMoreSpecializedTemplate(Cand1.Function->getPrimaryTemplate(), |
| 3710 | Cand2.Function->getPrimaryTemplate(), |
| 3711 | true)) |
| 3712 | return BetterTemplate == Cand1.Function->getPrimaryTemplate(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3713 | |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3714 | // -- the context is an initialization by user-defined conversion |
| 3715 | // (see 8.5, 13.3.1.5) and the standard conversion sequence |
| 3716 | // from the return type of F1 to the destination type (i.e., |
| 3717 | // the type of the entity being initialized) is a better |
| 3718 | // conversion sequence than the standard conversion sequence |
| 3719 | // from the return type of F2 to the destination type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3720 | if (Cand1.Function && Cand2.Function && |
| 3721 | isa<CXXConversionDecl>(Cand1.Function) && |
Douglas Gregor | f1991ea | 2008-11-07 22:36:19 +0000 | [diff] [blame] | 3722 | isa<CXXConversionDecl>(Cand2.Function)) { |
| 3723 | switch (CompareStandardConversionSequences(Cand1.FinalConversion, |
| 3724 | Cand2.FinalConversion)) { |
| 3725 | case ImplicitConversionSequence::Better: |
| 3726 | // Cand1 has a better conversion sequence. |
| 3727 | return true; |
| 3728 | |
| 3729 | case ImplicitConversionSequence::Worse: |
| 3730 | // Cand1 can't be better than Cand2. |
| 3731 | return false; |
| 3732 | |
| 3733 | case ImplicitConversionSequence::Indistinguishable: |
| 3734 | // Do nothing |
| 3735 | break; |
| 3736 | } |
| 3737 | } |
| 3738 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3739 | return false; |
| 3740 | } |
| 3741 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3742 | /// \brief Computes the best viable function (C++ 13.3.3) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3743 | /// within an overload candidate set. |
| 3744 | /// |
| 3745 | /// \param CandidateSet the set of candidate functions. |
| 3746 | /// |
| 3747 | /// \param Loc the location of the function name (or operator symbol) for |
| 3748 | /// which overload resolution occurs. |
| 3749 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3750 | /// \param Best f overload resolution was successful or found a deleted |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3751 | /// function, Best points to the candidate function found. |
| 3752 | /// |
| 3753 | /// \returns The result of overload resolution. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3754 | Sema::OverloadingResult |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3755 | Sema::BestViableFunction(OverloadCandidateSet& CandidateSet, |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3756 | SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3757 | OverloadCandidateSet::iterator& Best) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3758 | // Find the best viable function. |
| 3759 | Best = CandidateSet.end(); |
| 3760 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3761 | Cand != CandidateSet.end(); ++Cand) { |
| 3762 | if (Cand->Viable) { |
| 3763 | if (Best == CandidateSet.end() || isBetterOverloadCandidate(*Cand, *Best)) |
| 3764 | Best = Cand; |
| 3765 | } |
| 3766 | } |
| 3767 | |
| 3768 | // If we didn't find any viable functions, abort. |
| 3769 | if (Best == CandidateSet.end()) |
| 3770 | return OR_No_Viable_Function; |
| 3771 | |
| 3772 | // Make sure that this function is better than every other viable |
| 3773 | // function. If not, we have an ambiguity. |
| 3774 | for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(); |
| 3775 | Cand != CandidateSet.end(); ++Cand) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3776 | if (Cand->Viable && |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3777 | Cand != Best && |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3778 | !isBetterOverloadCandidate(*Best, *Cand)) { |
| 3779 | Best = CandidateSet.end(); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3780 | return OR_Ambiguous; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3781 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3782 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3783 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3784 | // Best is the best viable function. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3785 | if (Best->Function && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3786 | (Best->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3787 | Best->Function->getAttr<UnavailableAttr>())) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3788 | return OR_Deleted; |
| 3789 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3790 | // C++ [basic.def.odr]p2: |
| 3791 | // An overloaded function is used if it is selected by overload resolution |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3792 | // when referred to from a potentially-evaluated expression. [Note: this |
| 3793 | // covers calls to named functions (5.2.2), operator overloading |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 3794 | // (clause 13), user-defined conversions (12.3.2), allocation function for |
| 3795 | // placement new (5.3.4), as well as non-default initialization (8.5). |
| 3796 | if (Best->Function) |
| 3797 | MarkDeclarationReferenced(Loc, Best->Function); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3798 | return OR_Success; |
| 3799 | } |
| 3800 | |
| 3801 | /// PrintOverloadCandidates - When overload resolution fails, prints |
| 3802 | /// diagnostic messages containing the candidates in the candidate |
| 3803 | /// set. If OnlyViable is true, only viable candidates will be printed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3804 | void |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3805 | Sema::PrintOverloadCandidates(OverloadCandidateSet& CandidateSet, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3806 | bool OnlyViable) { |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3807 | OverloadCandidateSet::iterator Cand = CandidateSet.begin(), |
| 3808 | LastCand = CandidateSet.end(); |
| 3809 | for (; Cand != LastCand; ++Cand) { |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3810 | if (Cand->Viable || !OnlyViable) { |
| 3811 | if (Cand->Function) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3812 | if (Cand->Function->isDeleted() || |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3813 | Cand->Function->getAttr<UnavailableAttr>()) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3814 | // Deleted or "unavailable" function. |
| 3815 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate_deleted) |
| 3816 | << Cand->Function->isDeleted(); |
| 3817 | } else { |
| 3818 | // Normal function |
| 3819 | // FIXME: Give a better reason! |
| 3820 | Diag(Cand->Function->getLocation(), diag::err_ovl_candidate); |
| 3821 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3822 | } else if (Cand->IsSurrogate) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3823 | // Desugar the type of the surrogate down to a function type, |
| 3824 | // retaining as many typedefs as possible while still showing |
| 3825 | // the function type (and, therefore, its parameter types). |
| 3826 | QualType FnType = Cand->Surrogate->getConversionType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3827 | bool isLValueReference = false; |
| 3828 | bool isRValueReference = false; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3829 | bool isPointer = false; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3830 | if (const LValueReferenceType *FnTypeRef = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3831 | FnType->getAs<LValueReferenceType>()) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3832 | FnType = FnTypeRef->getPointeeType(); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3833 | isLValueReference = true; |
| 3834 | } else if (const RValueReferenceType *FnTypeRef = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3835 | FnType->getAs<RValueReferenceType>()) { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3836 | FnType = FnTypeRef->getPointeeType(); |
| 3837 | isRValueReference = true; |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3838 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3839 | if (const PointerType *FnTypePtr = FnType->getAs<PointerType>()) { |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3840 | FnType = FnTypePtr->getPointeeType(); |
| 3841 | isPointer = true; |
| 3842 | } |
| 3843 | // Desugar down to a function type. |
| 3844 | FnType = QualType(FnType->getAsFunctionType(), 0); |
| 3845 | // Reconstruct the pointer/reference as appropriate. |
| 3846 | if (isPointer) FnType = Context.getPointerType(FnType); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3847 | if (isRValueReference) FnType = Context.getRValueReferenceType(FnType); |
| 3848 | if (isLValueReference) FnType = Context.getLValueReferenceType(FnType); |
Douglas Gregor | 621b393 | 2008-11-21 02:54:28 +0000 | [diff] [blame] | 3849 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 3850 | Diag(Cand->Surrogate->getLocation(), diag::err_ovl_surrogate_cand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3851 | << FnType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3852 | } else { |
| 3853 | // FIXME: We need to get the identifier in here |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3854 | // FIXME: Do we want the error message to point at the operator? |
| 3855 | // (built-ins won't have a location) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3856 | QualType FnType |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3857 | = Context.getFunctionType(Cand->BuiltinTypes.ResultTy, |
| 3858 | Cand->BuiltinTypes.ParamTypes, |
| 3859 | Cand->Conversions.size(), |
| 3860 | false, 0); |
| 3861 | |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3862 | Diag(SourceLocation(), diag::err_ovl_builtin_candidate) << FnType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 3863 | } |
| 3864 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3865 | } |
| 3866 | } |
| 3867 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3868 | /// ResolveAddressOfOverloadedFunction - Try to resolve the address of |
| 3869 | /// an overloaded function (C++ [over.over]), where @p From is an |
| 3870 | /// expression with overloaded function type and @p ToType is the type |
| 3871 | /// we're trying to resolve to. For example: |
| 3872 | /// |
| 3873 | /// @code |
| 3874 | /// int f(double); |
| 3875 | /// int f(int); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3876 | /// |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3877 | /// int (*pfd)(double) = f; // selects f(double) |
| 3878 | /// @endcode |
| 3879 | /// |
| 3880 | /// This routine returns the resulting FunctionDecl if it could be |
| 3881 | /// resolved, and NULL otherwise. When @p Complain is true, this |
| 3882 | /// routine will emit diagnostics if there is an error. |
| 3883 | FunctionDecl * |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3884 | Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3885 | bool Complain) { |
| 3886 | QualType FunctionType = ToType; |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3887 | bool IsMember = false; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3888 | if (const PointerType *ToTypePtr = ToType->getAs<PointerType>()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3889 | FunctionType = ToTypePtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3890 | else if (const ReferenceType *ToTypeRef = ToType->getAs<ReferenceType>()) |
Daniel Dunbar | bb71001 | 2009-02-26 19:13:44 +0000 | [diff] [blame] | 3891 | FunctionType = ToTypeRef->getPointeeType(); |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3892 | else if (const MemberPointerType *MemTypePtr = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3893 | ToType->getAs<MemberPointerType>()) { |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3894 | FunctionType = MemTypePtr->getPointeeType(); |
| 3895 | IsMember = true; |
| 3896 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3897 | |
| 3898 | // We only look at pointers or references to functions. |
Douglas Gregor | 72e771f | 2009-07-09 17:16:51 +0000 | [diff] [blame] | 3899 | FunctionType = Context.getCanonicalType(FunctionType).getUnqualifiedType(); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3900 | if (!FunctionType->isFunctionType()) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3901 | return 0; |
| 3902 | |
| 3903 | // Find the actual overloaded function declaration. |
| 3904 | OverloadedFunctionDecl *Ovl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3905 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3906 | // C++ [over.over]p1: |
| 3907 | // [...] [Note: any redundant set of parentheses surrounding the |
| 3908 | // overloaded function name is ignored (5.1). ] |
| 3909 | Expr *OvlExpr = From->IgnoreParens(); |
| 3910 | |
| 3911 | // C++ [over.over]p1: |
| 3912 | // [...] The overloaded function name can be preceded by the & |
| 3913 | // operator. |
| 3914 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(OvlExpr)) { |
| 3915 | if (UnOp->getOpcode() == UnaryOperator::AddrOf) |
| 3916 | OvlExpr = UnOp->getSubExpr()->IgnoreParens(); |
| 3917 | } |
| 3918 | |
| 3919 | // Try to dig out the overloaded function. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3920 | FunctionTemplateDecl *FunctionTemplate = 0; |
| 3921 | if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(OvlExpr)) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3922 | Ovl = dyn_cast<OverloadedFunctionDecl>(DR->getDecl()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3923 | FunctionTemplate = dyn_cast<FunctionTemplateDecl>(DR->getDecl()); |
| 3924 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3925 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3926 | // If there's no overloaded function declaration or function template, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3927 | // we're done. |
| 3928 | if (!Ovl && !FunctionTemplate) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3929 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3930 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3931 | OverloadIterator Fun; |
| 3932 | if (Ovl) |
| 3933 | Fun = Ovl; |
| 3934 | else |
| 3935 | Fun = FunctionTemplate; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3936 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3937 | // Look through all of the overloaded functions, searching for one |
| 3938 | // whose type matches exactly. |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3939 | llvm::SmallPtrSet<FunctionDecl *, 4> Matches; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3940 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3941 | bool FoundNonTemplateFunction = false; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3942 | for (OverloadIterator FunEnd; Fun != FunEnd; ++Fun) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3943 | // C++ [over.over]p3: |
| 3944 | // Non-member functions and static member functions match |
Sebastian Redl | 0defd76 | 2009-02-05 12:33:33 +0000 | [diff] [blame] | 3945 | // targets of type "pointer-to-function" or "reference-to-function." |
| 3946 | // Nonstatic member functions match targets of |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3947 | // type "pointer-to-member-function." |
| 3948 | // Note that according to DR 247, the containing class does not matter. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3949 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3950 | if (FunctionTemplateDecl *FunctionTemplate |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3951 | = dyn_cast<FunctionTemplateDecl>(*Fun)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3952 | if (CXXMethodDecl *Method |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3953 | = dyn_cast<CXXMethodDecl>(FunctionTemplate->getTemplatedDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3954 | // Skip non-static function templates when converting to pointer, and |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3955 | // static when converting to member pointer. |
| 3956 | if (Method->isStatic() == IsMember) |
| 3957 | continue; |
| 3958 | } else if (IsMember) |
| 3959 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3960 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3961 | // C++ [over.over]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3962 | // If the name is a function template, template argument deduction is |
| 3963 | // done (14.8.2.2), and if the argument deduction succeeds, the |
| 3964 | // resulting template argument list is used to generate a single |
| 3965 | // function template specialization, which is added to the set of |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3966 | // overloaded functions considered. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3967 | FunctionDecl *Specialization = 0; |
| 3968 | TemplateDeductionInfo Info(Context); |
| 3969 | if (TemplateDeductionResult Result |
| 3970 | = DeduceTemplateArguments(FunctionTemplate, /*FIXME*/false, |
| 3971 | /*FIXME:*/0, /*FIXME:*/0, |
| 3972 | FunctionType, Specialization, Info)) { |
| 3973 | // FIXME: make a note of the failed deduction for diagnostics. |
| 3974 | (void)Result; |
| 3975 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3976 | assert(FunctionType |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3977 | == Context.getCanonicalType(Specialization->getType())); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3978 | Matches.insert( |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 3979 | cast<FunctionDecl>(Specialization->getCanonicalDecl())); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3980 | } |
| 3981 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3982 | |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3983 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*Fun)) { |
| 3984 | // Skip non-static functions when converting to pointer, and static |
| 3985 | // when converting to member pointer. |
| 3986 | if (Method->isStatic() == IsMember) |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3987 | continue; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3988 | } else if (IsMember) |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 3989 | continue; |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3990 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3991 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(*Fun)) { |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3992 | if (FunctionType == Context.getCanonicalType(FunDecl->getType())) { |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 3993 | Matches.insert(cast<FunctionDecl>(Fun->getCanonicalDecl())); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3994 | FoundNonTemplateFunction = true; |
| 3995 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3996 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 3997 | } |
| 3998 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 3999 | // If there were 0 or 1 matches, we're done. |
| 4000 | if (Matches.empty()) |
| 4001 | return 0; |
| 4002 | else if (Matches.size() == 1) |
| 4003 | return *Matches.begin(); |
| 4004 | |
| 4005 | // C++ [over.over]p4: |
| 4006 | // If more than one function is selected, [...] |
| 4007 | llvm::SmallVector<FunctionDecl *, 4> RemainingMatches; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4008 | typedef llvm::SmallPtrSet<FunctionDecl *, 4>::iterator MatchIter; |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4009 | if (FoundNonTemplateFunction) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4010 | // [...] any function template specializations in the set are |
| 4011 | // eliminated if the set also contains a non-template function, [...] |
| 4012 | for (MatchIter M = Matches.begin(), MEnd = Matches.end(); M != MEnd; ++M) |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4013 | if ((*M)->getPrimaryTemplate() == 0) |
| 4014 | RemainingMatches.push_back(*M); |
| 4015 | } else { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4016 | // [...] and any given function template specialization F1 is |
| 4017 | // eliminated if the set contains a second function template |
| 4018 | // specialization whose function template is more specialized |
| 4019 | // than the function template of F1 according to the partial |
| 4020 | // ordering rules of 14.5.5.2. |
| 4021 | |
| 4022 | // The algorithm specified above is quadratic. We instead use a |
| 4023 | // two-pass algorithm (similar to the one used to identify the |
| 4024 | // best viable function in an overload set) that identifies the |
| 4025 | // best function template (if it exists). |
| 4026 | MatchIter Best = Matches.begin(); |
| 4027 | MatchIter M = Best, MEnd = Matches.end(); |
| 4028 | // Find the most specialized function. |
| 4029 | for (++M; M != MEnd; ++M) |
| 4030 | if (getMoreSpecializedTemplate((*M)->getPrimaryTemplate(), |
| 4031 | (*Best)->getPrimaryTemplate(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4032 | false) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4033 | == (*M)->getPrimaryTemplate()) |
| 4034 | Best = M; |
| 4035 | |
| 4036 | // Determine whether this function template is more specialized |
| 4037 | // that all of the others. |
| 4038 | bool Ambiguous = false; |
| 4039 | for (M = Matches.begin(); M != MEnd; ++M) { |
| 4040 | if (M != Best && |
| 4041 | getMoreSpecializedTemplate((*M)->getPrimaryTemplate(), |
| 4042 | (*Best)->getPrimaryTemplate(), |
| 4043 | false) |
| 4044 | != (*Best)->getPrimaryTemplate()) { |
| 4045 | Ambiguous = true; |
| 4046 | break; |
| 4047 | } |
| 4048 | } |
| 4049 | |
| 4050 | // If one function template was more specialized than all of the |
| 4051 | // others, return it. |
| 4052 | if (!Ambiguous) |
| 4053 | return *Best; |
| 4054 | |
| 4055 | // We could not find a most-specialized function template, which |
| 4056 | // is equivalent to having a set of function templates with more |
| 4057 | // than one such template. So, we place all of the function |
| 4058 | // templates into the set of remaining matches and produce a |
| 4059 | // diagnostic below. FIXME: we could perform the quadratic |
| 4060 | // algorithm here, pruning the result set to limit the number of |
| 4061 | // candidates output later. |
| 4062 | RemainingMatches.append(Matches.begin(), Matches.end()); |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4063 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4064 | |
| 4065 | // [...] After such eliminations, if any, there shall remain exactly one |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4066 | // selected function. |
| 4067 | if (RemainingMatches.size() == 1) |
| 4068 | return RemainingMatches.front(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4069 | |
Douglas Gregor | 00aeb52 | 2009-07-08 23:33:52 +0000 | [diff] [blame] | 4070 | // FIXME: We should probably return the same thing that BestViableFunction |
| 4071 | // returns (even if we issue the diagnostics here). |
| 4072 | Diag(From->getLocStart(), diag::err_addr_ovl_ambiguous) |
| 4073 | << RemainingMatches[0]->getDeclName(); |
| 4074 | for (unsigned I = 0, N = RemainingMatches.size(); I != N; ++I) |
| 4075 | Diag(RemainingMatches[I]->getLocation(), diag::err_ovl_candidate); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4076 | return 0; |
| 4077 | } |
| 4078 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4079 | /// ResolveOverloadedCallFn - Given the call expression that calls Fn |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4080 | /// (which eventually refers to the declaration Func) and the call |
| 4081 | /// arguments Args/NumArgs, attempt to resolve the function call down |
| 4082 | /// to a specific function. If overload resolution succeeds, returns |
| 4083 | /// the function declaration produced by overload |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4084 | /// resolution. Otherwise, emits diagnostics, deletes all of the |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4085 | /// arguments and Fn, and returns NULL. |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4086 | FunctionDecl *Sema::ResolveOverloadedCallFn(Expr *Fn, NamedDecl *Callee, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4087 | DeclarationName UnqualifiedName, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4088 | bool HasExplicitTemplateArgs, |
| 4089 | const TemplateArgument *ExplicitTemplateArgs, |
| 4090 | unsigned NumExplicitTemplateArgs, |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4091 | SourceLocation LParenLoc, |
| 4092 | Expr **Args, unsigned NumArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4093 | SourceLocation *CommaLocs, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4094 | SourceLocation RParenLoc, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4095 | bool &ArgumentDependentLookup) { |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4096 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4097 | |
| 4098 | // Add the functions denoted by Callee to the set of candidate |
| 4099 | // functions. While we're doing so, track whether argument-dependent |
| 4100 | // lookup still applies, per: |
| 4101 | // |
| 4102 | // C++0x [basic.lookup.argdep]p3: |
| 4103 | // Let X be the lookup set produced by unqualified lookup (3.4.1) |
| 4104 | // and let Y be the lookup set produced by argument dependent |
| 4105 | // lookup (defined as follows). If X contains |
| 4106 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4107 | // -- a declaration of a class member, or |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4108 | // |
| 4109 | // -- a block-scope function declaration that is not a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4110 | // using-declaration, or |
| 4111 | // |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4112 | // -- a declaration that is neither a function or a function |
| 4113 | // template |
| 4114 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4115 | // then Y is empty. |
| 4116 | if (OverloadedFunctionDecl *Ovl |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4117 | = dyn_cast_or_null<OverloadedFunctionDecl>(Callee)) { |
| 4118 | for (OverloadedFunctionDecl::function_iterator Func = Ovl->function_begin(), |
| 4119 | FuncEnd = Ovl->function_end(); |
| 4120 | Func != FuncEnd; ++Func) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4121 | DeclContext *Ctx = 0; |
| 4122 | if (FunctionDecl *FunDecl = dyn_cast<FunctionDecl>(*Func)) { |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4123 | if (HasExplicitTemplateArgs) |
| 4124 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4125 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4126 | AddOverloadCandidate(FunDecl, Args, NumArgs, CandidateSet); |
| 4127 | Ctx = FunDecl->getDeclContext(); |
| 4128 | } else { |
| 4129 | FunctionTemplateDecl *FunTmpl = cast<FunctionTemplateDecl>(*Func); |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4130 | AddTemplateOverloadCandidate(FunTmpl, HasExplicitTemplateArgs, |
| 4131 | ExplicitTemplateArgs, |
| 4132 | NumExplicitTemplateArgs, |
| 4133 | Args, NumArgs, CandidateSet); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4134 | Ctx = FunTmpl->getDeclContext(); |
| 4135 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4136 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4137 | |
| 4138 | if (Ctx->isRecord() || Ctx->isFunctionOrMethod()) |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4139 | ArgumentDependentLookup = false; |
| 4140 | } |
| 4141 | } else if (FunctionDecl *Func = dyn_cast_or_null<FunctionDecl>(Callee)) { |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4142 | assert(!HasExplicitTemplateArgs && "Explicit template arguments?"); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4143 | AddOverloadCandidate(Func, Args, NumArgs, CandidateSet); |
| 4144 | |
| 4145 | if (Func->getDeclContext()->isRecord() || |
| 4146 | Func->getDeclContext()->isFunctionOrMethod()) |
| 4147 | ArgumentDependentLookup = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4148 | } else if (FunctionTemplateDecl *FuncTemplate |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4149 | = dyn_cast_or_null<FunctionTemplateDecl>(Callee)) { |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4150 | AddTemplateOverloadCandidate(FuncTemplate, HasExplicitTemplateArgs, |
| 4151 | ExplicitTemplateArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4152 | NumExplicitTemplateArgs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4153 | Args, NumArgs, CandidateSet); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4154 | |
| 4155 | if (FuncTemplate->getDeclContext()->isRecord()) |
| 4156 | ArgumentDependentLookup = false; |
| 4157 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4158 | |
| 4159 | if (Callee) |
| 4160 | UnqualifiedName = Callee->getDeclName(); |
| 4161 | |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4162 | // FIXME: Pass explicit template arguments through for ADL |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4163 | if (ArgumentDependentLookup) |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4164 | AddArgumentDependentLookupCandidates(UnqualifiedName, Args, NumArgs, |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4165 | CandidateSet); |
| 4166 | |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4167 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4168 | switch (BestViableFunction(CandidateSet, Fn->getLocStart(), Best)) { |
Douglas Gregor | 0a39668 | 2008-11-26 06:01:48 +0000 | [diff] [blame] | 4169 | case OR_Success: |
| 4170 | return Best->Function; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4171 | |
| 4172 | case OR_No_Viable_Function: |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4173 | Diag(Fn->getSourceRange().getBegin(), |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4174 | diag::err_ovl_no_viable_function_in_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4175 | << UnqualifiedName << Fn->getSourceRange(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4176 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4177 | break; |
| 4178 | |
| 4179 | case OR_Ambiguous: |
| 4180 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_ambiguous_call) |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4181 | << UnqualifiedName << Fn->getSourceRange(); |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4182 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4183 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4184 | |
| 4185 | case OR_Deleted: |
| 4186 | Diag(Fn->getSourceRange().getBegin(), diag::err_ovl_deleted_call) |
| 4187 | << Best->Function->isDeleted() |
| 4188 | << UnqualifiedName |
| 4189 | << Fn->getSourceRange(); |
| 4190 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4191 | break; |
Douglas Gregor | f6b8969 | 2008-11-26 05:54:23 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
| 4194 | // Overload resolution failed. Destroy all of the subexpressions and |
| 4195 | // return NULL. |
| 4196 | Fn->Destroy(Context); |
| 4197 | for (unsigned Arg = 0; Arg < NumArgs; ++Arg) |
| 4198 | Args[Arg]->Destroy(Context); |
| 4199 | return 0; |
| 4200 | } |
| 4201 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4202 | /// \brief Create a unary operation that may resolve to an overloaded |
| 4203 | /// operator. |
| 4204 | /// |
| 4205 | /// \param OpLoc The location of the operator itself (e.g., '*'). |
| 4206 | /// |
| 4207 | /// \param OpcIn The UnaryOperator::Opcode that describes this |
| 4208 | /// operator. |
| 4209 | /// |
| 4210 | /// \param Functions The set of non-member functions that will be |
| 4211 | /// considered by overload resolution. The caller needs to build this |
| 4212 | /// set based on the context using, e.g., |
| 4213 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 4214 | /// set should not contain any member functions; those will be added |
| 4215 | /// by CreateOverloadedUnaryOp(). |
| 4216 | /// |
| 4217 | /// \param input The input argument. |
| 4218 | Sema::OwningExprResult Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, |
| 4219 | unsigned OpcIn, |
| 4220 | FunctionSet &Functions, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4221 | ExprArg input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4222 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
| 4223 | Expr *Input = (Expr *)input.get(); |
| 4224 | |
| 4225 | OverloadedOperatorKind Op = UnaryOperator::getOverloadedOperator(Opc); |
| 4226 | assert(Op != OO_None && "Invalid opcode for overloaded unary operator"); |
| 4227 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4228 | |
| 4229 | Expr *Args[2] = { Input, 0 }; |
| 4230 | unsigned NumArgs = 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4231 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4232 | // For post-increment and post-decrement, add the implicit '0' as |
| 4233 | // the second argument, so that we know this is a post-increment or |
| 4234 | // post-decrement. |
| 4235 | if (Opc == UnaryOperator::PostInc || Opc == UnaryOperator::PostDec) { |
| 4236 | llvm::APSInt Zero(Context.getTypeSize(Context.IntTy), false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4237 | Args[1] = new (Context) IntegerLiteral(Zero, Context.IntTy, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4238 | SourceLocation()); |
| 4239 | NumArgs = 2; |
| 4240 | } |
| 4241 | |
| 4242 | if (Input->isTypeDependent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4243 | OverloadedFunctionDecl *Overloads |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4244 | = OverloadedFunctionDecl::Create(Context, CurContext, OpName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4245 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4246 | FuncEnd = Functions.end(); |
| 4247 | Func != FuncEnd; ++Func) |
| 4248 | Overloads->addOverload(*Func); |
| 4249 | |
| 4250 | DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy, |
| 4251 | OpLoc, false, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4252 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4253 | input.release(); |
| 4254 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
| 4255 | &Args[0], NumArgs, |
| 4256 | Context.DependentTy, |
| 4257 | OpLoc)); |
| 4258 | } |
| 4259 | |
| 4260 | // Build an empty overload set. |
| 4261 | OverloadCandidateSet CandidateSet; |
| 4262 | |
| 4263 | // Add the candidates from the given function set. |
| 4264 | AddFunctionCandidates(Functions, &Args[0], NumArgs, CandidateSet, false); |
| 4265 | |
| 4266 | // Add operator candidates that are member functions. |
| 4267 | AddMemberOperatorCandidates(Op, OpLoc, &Args[0], NumArgs, CandidateSet); |
| 4268 | |
| 4269 | // Add builtin operator candidates. |
| 4270 | AddBuiltinOperatorCandidates(Op, &Args[0], NumArgs, CandidateSet); |
| 4271 | |
| 4272 | // Perform overload resolution. |
| 4273 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4274 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4275 | case OR_Success: { |
| 4276 | // We found a built-in operator or an overloaded operator. |
| 4277 | FunctionDecl *FnDecl = Best->Function; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4278 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4279 | if (FnDecl) { |
| 4280 | // We matched an overloaded operator. Build a call to that |
| 4281 | // operator. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4282 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4283 | // Convert the arguments. |
| 4284 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 4285 | if (PerformObjectArgumentInitialization(Input, Method)) |
| 4286 | return ExprError(); |
| 4287 | } else { |
| 4288 | // Convert the arguments. |
| 4289 | if (PerformCopyInitialization(Input, |
| 4290 | FnDecl->getParamDecl(0)->getType(), |
| 4291 | "passing")) |
| 4292 | return ExprError(); |
| 4293 | } |
| 4294 | |
| 4295 | // Determine the result type |
| 4296 | QualType ResultTy |
| 4297 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 4298 | ResultTy = ResultTy.getNonReferenceType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4299 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4300 | // Build the actual expression node. |
| 4301 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 4302 | SourceLocation()); |
| 4303 | UsualUnaryConversions(FnExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4304 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4305 | input.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4306 | |
| 4307 | Expr *CE = new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Anders Carlsson | 2d46eb2 | 2009-08-16 04:11:06 +0000 | [diff] [blame] | 4308 | &Input, 1, ResultTy, OpLoc); |
| 4309 | return MaybeBindToTemporary(CE); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4310 | } else { |
| 4311 | // We matched a built-in operator. Convert the arguments, then |
| 4312 | // break out so that we will build the appropriate built-in |
| 4313 | // operator node. |
| 4314 | if (PerformImplicitConversion(Input, Best->BuiltinTypes.ParamTypes[0], |
| 4315 | Best->Conversions[0], "passing")) |
| 4316 | return ExprError(); |
| 4317 | |
| 4318 | break; |
| 4319 | } |
| 4320 | } |
| 4321 | |
| 4322 | case OR_No_Viable_Function: |
| 4323 | // No viable function; fall through to handling this as a |
| 4324 | // built-in operator, which will produce an error message for us. |
| 4325 | break; |
| 4326 | |
| 4327 | case OR_Ambiguous: |
| 4328 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 4329 | << UnaryOperator::getOpcodeStr(Opc) |
| 4330 | << Input->getSourceRange(); |
| 4331 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4332 | return ExprError(); |
| 4333 | |
| 4334 | case OR_Deleted: |
| 4335 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4336 | << Best->Function->isDeleted() |
| 4337 | << UnaryOperator::getOpcodeStr(Opc) |
| 4338 | << Input->getSourceRange(); |
| 4339 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4340 | return ExprError(); |
| 4341 | } |
| 4342 | |
| 4343 | // Either we found no viable overloaded operator or we matched a |
| 4344 | // built-in operator. In either case, fall through to trying to |
| 4345 | // build a built-in operation. |
| 4346 | input.release(); |
| 4347 | return CreateBuiltinUnaryOp(OpLoc, Opc, Owned(Input)); |
| 4348 | } |
| 4349 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4350 | /// \brief Create a binary operation that may resolve to an overloaded |
| 4351 | /// operator. |
| 4352 | /// |
| 4353 | /// \param OpLoc The location of the operator itself (e.g., '+'). |
| 4354 | /// |
| 4355 | /// \param OpcIn The BinaryOperator::Opcode that describes this |
| 4356 | /// operator. |
| 4357 | /// |
| 4358 | /// \param Functions The set of non-member functions that will be |
| 4359 | /// considered by overload resolution. The caller needs to build this |
| 4360 | /// set based on the context using, e.g., |
| 4361 | /// LookupOverloadedOperatorName() and ArgumentDependentLookup(). This |
| 4362 | /// set should not contain any member functions; those will be added |
| 4363 | /// by CreateOverloadedBinOp(). |
| 4364 | /// |
| 4365 | /// \param LHS Left-hand argument. |
| 4366 | /// \param RHS Right-hand argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4367 | Sema::OwningExprResult |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4368 | Sema::CreateOverloadedBinOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4369 | unsigned OpcIn, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4370 | FunctionSet &Functions, |
| 4371 | Expr *LHS, Expr *RHS) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4372 | Expr *Args[2] = { LHS, RHS }; |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4373 | LHS=RHS=0; //Please use only Args instead of LHS/RHS couple |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4374 | |
| 4375 | BinaryOperator::Opcode Opc = static_cast<BinaryOperator::Opcode>(OpcIn); |
| 4376 | OverloadedOperatorKind Op = BinaryOperator::getOverloadedOperator(Opc); |
| 4377 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); |
| 4378 | |
| 4379 | // If either side is type-dependent, create an appropriate dependent |
| 4380 | // expression. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4381 | if (Args[0]->isTypeDependent() || Args[1]->isTypeDependent()) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4382 | // .* cannot be overloaded. |
| 4383 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4384 | return Owned(new (Context) BinaryOperator(Args[0], Args[1], Opc, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4385 | Context.DependentTy, OpLoc)); |
| 4386 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4387 | OverloadedFunctionDecl *Overloads |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4388 | = OverloadedFunctionDecl::Create(Context, CurContext, OpName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4389 | for (FunctionSet::iterator Func = Functions.begin(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4390 | FuncEnd = Functions.end(); |
| 4391 | Func != FuncEnd; ++Func) |
| 4392 | Overloads->addOverload(*Func); |
| 4393 | |
| 4394 | DeclRefExpr *Fn = new (Context) DeclRefExpr(Overloads, Context.OverloadTy, |
| 4395 | OpLoc, false, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4396 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4397 | return Owned(new (Context) CXXOperatorCallExpr(Context, Op, Fn, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4398 | Args, 2, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4399 | Context.DependentTy, |
| 4400 | OpLoc)); |
| 4401 | } |
| 4402 | |
| 4403 | // If this is the .* operator, which is not overloadable, just |
| 4404 | // create a built-in binary operator. |
| 4405 | if (Opc == BinaryOperator::PtrMemD) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4406 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4407 | |
| 4408 | // If this is one of the assignment operators, we only perform |
| 4409 | // overload resolution if the left-hand side is a class or |
| 4410 | // enumeration type (C++ [expr.ass]p3). |
| 4411 | if (Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign && |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4412 | !Args[0]->getType()->isOverloadableType()) |
| 4413 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4414 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 4415 | // Build an empty overload set. |
| 4416 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4417 | |
| 4418 | // Add the candidates from the given function set. |
| 4419 | AddFunctionCandidates(Functions, Args, 2, CandidateSet, false); |
| 4420 | |
| 4421 | // Add operator candidates that are member functions. |
| 4422 | AddMemberOperatorCandidates(Op, OpLoc, Args, 2, CandidateSet); |
| 4423 | |
| 4424 | // Add builtin operator candidates. |
| 4425 | AddBuiltinOperatorCandidates(Op, Args, 2, CandidateSet); |
| 4426 | |
| 4427 | // Perform overload resolution. |
| 4428 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4429 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4430 | case OR_Success: { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4431 | // We found a built-in operator or an overloaded operator. |
| 4432 | FunctionDecl *FnDecl = Best->Function; |
| 4433 | |
| 4434 | if (FnDecl) { |
| 4435 | // We matched an overloaded operator. Build a call to that |
| 4436 | // operator. |
| 4437 | |
| 4438 | // Convert the arguments. |
| 4439 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4440 | if (PerformObjectArgumentInitialization(Args[0], Method) || |
| 4441 | PerformCopyInitialization(Args[1], FnDecl->getParamDecl(0)->getType(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4442 | "passing")) |
| 4443 | return ExprError(); |
| 4444 | } else { |
| 4445 | // Convert the arguments. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4446 | if (PerformCopyInitialization(Args[0], FnDecl->getParamDecl(0)->getType(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4447 | "passing") || |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4448 | PerformCopyInitialization(Args[1], FnDecl->getParamDecl(1)->getType(), |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4449 | "passing")) |
| 4450 | return ExprError(); |
| 4451 | } |
| 4452 | |
| 4453 | // Determine the result type |
| 4454 | QualType ResultTy |
| 4455 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 4456 | ResultTy = ResultTy.getNonReferenceType(); |
| 4457 | |
| 4458 | // Build the actual expression node. |
| 4459 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Argyrios Kyrtzidis | 8127309 | 2009-07-14 03:19:38 +0000 | [diff] [blame] | 4460 | OpLoc); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4461 | UsualUnaryConversions(FnExpr); |
| 4462 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4463 | Expr *CE = new (Context) CXXOperatorCallExpr(Context, Op, FnExpr, |
Anders Carlsson | 2d46eb2 | 2009-08-16 04:11:06 +0000 | [diff] [blame] | 4464 | Args, 2, ResultTy, OpLoc); |
| 4465 | return MaybeBindToTemporary(CE); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4466 | } else { |
| 4467 | // We matched a built-in operator. Convert the arguments, then |
| 4468 | // break out so that we will build the appropriate built-in |
| 4469 | // operator node. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4470 | if (PerformImplicitConversion(Args[0], Best->BuiltinTypes.ParamTypes[0], |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4471 | Best->Conversions[0], "passing") || |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4472 | PerformImplicitConversion(Args[1], Best->BuiltinTypes.ParamTypes[1], |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4473 | Best->Conversions[1], "passing")) |
| 4474 | return ExprError(); |
| 4475 | |
| 4476 | break; |
| 4477 | } |
| 4478 | } |
| 4479 | |
| 4480 | case OR_No_Viable_Function: |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4481 | // For class as left operand for assignment or compound assigment operator |
| 4482 | // do not fall through to handling in built-in, but report that no overloaded |
| 4483 | // assignment operator found |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4484 | if (Args[0]->getType()->isRecordType() && Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) { |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4485 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
| 4486 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4487 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Sebastian Redl | 8593c78 | 2009-05-21 11:50:50 +0000 | [diff] [blame] | 4488 | return ExprError(); |
| 4489 | } |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4490 | // No viable function; fall through to handling this as a |
| 4491 | // built-in operator, which will produce an error message for us. |
| 4492 | break; |
| 4493 | |
| 4494 | case OR_Ambiguous: |
| 4495 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 4496 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4497 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4498 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4499 | return ExprError(); |
| 4500 | |
| 4501 | case OR_Deleted: |
| 4502 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4503 | << Best->Function->isDeleted() |
| 4504 | << BinaryOperator::getOpcodeStr(Opc) |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4505 | << Args[0]->getSourceRange() << Args[1]->getSourceRange(); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4506 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4507 | return ExprError(); |
| 4508 | } |
| 4509 | |
| 4510 | // Either we found no viable overloaded operator or we matched a |
| 4511 | // built-in operator. In either case, try to build a built-in |
| 4512 | // operation. |
Douglas Gregor | c3384cb | 2009-08-26 17:08:25 +0000 | [diff] [blame] | 4513 | return CreateBuiltinBinOp(OpLoc, Opc, Args[0], Args[1]); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4514 | } |
| 4515 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4516 | /// BuildCallToMemberFunction - Build a call to a member |
| 4517 | /// function. MemExpr is the expression that refers to the member |
| 4518 | /// function (and includes the object parameter), Args/NumArgs are the |
| 4519 | /// arguments to the function call (not including the object |
| 4520 | /// parameter). The caller needs to validate that the member |
| 4521 | /// expression refers to a member function or an overloaded member |
| 4522 | /// function. |
| 4523 | Sema::ExprResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4524 | Sema::BuildCallToMemberFunction(Scope *S, Expr *MemExprE, |
| 4525 | SourceLocation LParenLoc, Expr **Args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4526 | unsigned NumArgs, SourceLocation *CommaLocs, |
| 4527 | SourceLocation RParenLoc) { |
| 4528 | // Dig out the member expression. This holds both the object |
| 4529 | // argument and the member function we're referring to. |
| 4530 | MemberExpr *MemExpr = 0; |
| 4531 | if (ParenExpr *ParenE = dyn_cast<ParenExpr>(MemExprE)) |
| 4532 | MemExpr = dyn_cast<MemberExpr>(ParenE->getSubExpr()); |
| 4533 | else |
| 4534 | MemExpr = dyn_cast<MemberExpr>(MemExprE); |
| 4535 | assert(MemExpr && "Building member call without member expression"); |
| 4536 | |
| 4537 | // Extract the object argument. |
| 4538 | Expr *ObjectArg = MemExpr->getBase(); |
Anders Carlsson | a552f7c | 2009-05-01 18:34:30 +0000 | [diff] [blame] | 4539 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4540 | CXXMethodDecl *Method = 0; |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4541 | if (isa<OverloadedFunctionDecl>(MemExpr->getMemberDecl()) || |
| 4542 | isa<FunctionTemplateDecl>(MemExpr->getMemberDecl())) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4543 | // Add overload candidates |
| 4544 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4545 | DeclarationName DeclName = MemExpr->getMemberDecl()->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4546 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 4547 | for (OverloadIterator Func(MemExpr->getMemberDecl()), FuncEnd; |
| 4548 | Func != FuncEnd; ++Func) { |
| 4549 | if ((Method = dyn_cast<CXXMethodDecl>(*Func))) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4550 | AddMethodCandidate(Method, ObjectArg, Args, NumArgs, CandidateSet, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 4551 | /*SuppressUserConversions=*/false); |
| 4552 | else |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 4553 | AddMethodTemplateCandidate(cast<FunctionTemplateDecl>(*Func), |
| 4554 | MemExpr->hasExplicitTemplateArgumentList(), |
| 4555 | MemExpr->getTemplateArgs(), |
| 4556 | MemExpr->getNumTemplateArgs(), |
| 4557 | ObjectArg, Args, NumArgs, |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 4558 | CandidateSet, |
| 4559 | /*SuppressUsedConversions=*/false); |
| 4560 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4561 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4562 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4563 | switch (BestViableFunction(CandidateSet, MemExpr->getLocStart(), Best)) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4564 | case OR_Success: |
| 4565 | Method = cast<CXXMethodDecl>(Best->Function); |
| 4566 | break; |
| 4567 | |
| 4568 | case OR_No_Viable_Function: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4569 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4570 | diag::err_ovl_no_viable_member_function_in_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4571 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4572 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4573 | // FIXME: Leaking incoming expressions! |
| 4574 | return true; |
| 4575 | |
| 4576 | case OR_Ambiguous: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4577 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4578 | diag::err_ovl_ambiguous_member_call) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4579 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4580 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4581 | // FIXME: Leaking incoming expressions! |
| 4582 | return true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4583 | |
| 4584 | case OR_Deleted: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4585 | Diag(MemExpr->getSourceRange().getBegin(), |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4586 | diag::err_ovl_deleted_member_call) |
| 4587 | << Best->Function->isDeleted() |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 4588 | << DeclName << MemExprE->getSourceRange(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4589 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 4590 | // FIXME: Leaking incoming expressions! |
| 4591 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4592 | } |
| 4593 | |
| 4594 | FixOverloadedFunctionReference(MemExpr, Method); |
| 4595 | } else { |
| 4596 | Method = dyn_cast<CXXMethodDecl>(MemExpr->getMemberDecl()); |
| 4597 | } |
| 4598 | |
| 4599 | assert(Method && "Member call to something that isn't a method?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4600 | ExprOwningPtr<CXXMemberCallExpr> |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4601 | TheCall(this, new (Context) CXXMemberCallExpr(Context, MemExpr, Args, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4602 | NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4603 | Method->getResultType().getNonReferenceType(), |
| 4604 | RParenLoc)); |
| 4605 | |
| 4606 | // Convert the object argument (for a non-static member function call). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4607 | if (!Method->isStatic() && |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4608 | PerformObjectArgumentInitialization(ObjectArg, Method)) |
| 4609 | return true; |
| 4610 | MemExpr->setBase(ObjectArg); |
| 4611 | |
| 4612 | // Convert the rest of the arguments |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4613 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Method->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4614 | if (ConvertArgumentsForCall(&*TheCall, MemExpr, Method, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4615 | RParenLoc)) |
| 4616 | return true; |
| 4617 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4618 | if (CheckFunctionCall(Method, TheCall.get())) |
| 4619 | return true; |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 4620 | |
| 4621 | return MaybeBindToTemporary(TheCall.release()).release(); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4622 | } |
| 4623 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4624 | /// BuildCallToObjectOfClassType - Build a call to an object of class |
| 4625 | /// type (C++ [over.call.object]), which can end up invoking an |
| 4626 | /// overloaded function call operator (@c operator()) or performing a |
| 4627 | /// user-defined conversion on the object argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4628 | Sema::ExprResult |
| 4629 | Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 4630 | SourceLocation LParenLoc, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4631 | Expr **Args, unsigned NumArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4632 | SourceLocation *CommaLocs, |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4633 | SourceLocation RParenLoc) { |
| 4634 | assert(Object->getType()->isRecordType() && "Requires object type argument"); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4635 | const RecordType *Record = Object->getType()->getAs<RecordType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4636 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4637 | // C++ [over.call.object]p1: |
| 4638 | // If the primary-expression E in the function call syntax |
Eli Friedman | 33a3138 | 2009-08-05 19:21:58 +0000 | [diff] [blame] | 4639 | // evaluates to a class object of type "cv T", then the set of |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4640 | // candidate functions includes at least the function call |
| 4641 | // operators of T. The function call operators of T are obtained by |
| 4642 | // ordinary lookup of the name operator() in the context of |
| 4643 | // (E).operator(). |
| 4644 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4645 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4646 | DeclContext::lookup_const_iterator Oper, OperEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4647 | for (llvm::tie(Oper, OperEnd) = Record->getDecl()->lookup(OpName); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4648 | Oper != OperEnd; ++Oper) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4649 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Object, Args, NumArgs, |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4650 | CandidateSet, /*SuppressUserConversions=*/false); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4651 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4652 | // C++ [over.call.object]p2: |
| 4653 | // In addition, for each conversion function declared in T of the |
| 4654 | // form |
| 4655 | // |
| 4656 | // operator conversion-type-id () cv-qualifier; |
| 4657 | // |
| 4658 | // where cv-qualifier is the same cv-qualification as, or a |
| 4659 | // greater cv-qualification than, cv, and where conversion-type-id |
Douglas Gregor | a967a6f | 2008-11-20 13:33:37 +0000 | [diff] [blame] | 4660 | // denotes the type "pointer to function of (P1,...,Pn) returning |
| 4661 | // R", or the type "reference to pointer to function of |
| 4662 | // (P1,...,Pn) returning R", or the type "reference to function |
| 4663 | // of (P1,...,Pn) returning R", a surrogate call function [...] |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4664 | // is also considered as a candidate function. Similarly, |
| 4665 | // surrogate call functions are added to the set of candidate |
| 4666 | // functions for each conversion function declared in an |
| 4667 | // accessible base class provided the function is not hidden |
| 4668 | // within T by another intervening declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4669 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4670 | if (!RequireCompleteType(SourceLocation(), Object->getType(), 0)) { |
| 4671 | // FIXME: Look in base classes for more conversion operators! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4672 | OverloadedFunctionDecl *Conversions |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4673 | = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4674 | for (OverloadedFunctionDecl::function_iterator |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4675 | Func = Conversions->function_begin(), |
| 4676 | FuncEnd = Conversions->function_end(); |
| 4677 | Func != FuncEnd; ++Func) { |
| 4678 | CXXConversionDecl *Conv; |
| 4679 | FunctionTemplateDecl *ConvTemplate; |
| 4680 | GetFunctionAndTemplate(*Func, Conv, ConvTemplate); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4681 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4682 | // Skip over templated conversion functions; they aren't |
| 4683 | // surrogates. |
| 4684 | if (ConvTemplate) |
| 4685 | continue; |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4686 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4687 | // Strip the reference type (if any) and then the pointer type (if |
| 4688 | // any) to get down to what might be a function type. |
| 4689 | QualType ConvType = Conv->getConversionType().getNonReferenceType(); |
| 4690 | if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) |
| 4691 | ConvType = ConvPtrType->getPointeeType(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4692 | |
Douglas Gregor | 5842ba9 | 2009-08-24 15:23:48 +0000 | [diff] [blame] | 4693 | if (const FunctionProtoType *Proto = ConvType->getAsFunctionProtoType()) |
| 4694 | AddSurrogateCandidate(Conv, Proto, Object, Args, NumArgs, CandidateSet); |
| 4695 | } |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4696 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4697 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4698 | // Perform overload resolution. |
| 4699 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4700 | switch (BestViableFunction(CandidateSet, Object->getLocStart(), Best)) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4701 | case OR_Success: |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4702 | // Overload resolution succeeded; we'll build the appropriate call |
| 4703 | // below. |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4704 | break; |
| 4705 | |
| 4706 | case OR_No_Viable_Function: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4707 | Diag(Object->getSourceRange().getBegin(), |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 4708 | diag::err_ovl_no_viable_object_call) |
Chris Lattner | 4330d65 | 2009-02-17 07:29:20 +0000 | [diff] [blame] | 4709 | << Object->getType() << Object->getSourceRange(); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 4710 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4711 | break; |
| 4712 | |
| 4713 | case OR_Ambiguous: |
| 4714 | Diag(Object->getSourceRange().getBegin(), |
| 4715 | diag::err_ovl_ambiguous_object_call) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4716 | << Object->getType() << Object->getSourceRange(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4717 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4718 | break; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4719 | |
| 4720 | case OR_Deleted: |
| 4721 | Diag(Object->getSourceRange().getBegin(), |
| 4722 | diag::err_ovl_deleted_object_call) |
| 4723 | << Best->Function->isDeleted() |
| 4724 | << Object->getType() << Object->getSourceRange(); |
| 4725 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 4726 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4727 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4728 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4729 | if (Best == CandidateSet.end()) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4730 | // We had an error; delete all of the subexpressions and return |
| 4731 | // the error. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4732 | Object->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4733 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4734 | Args[ArgIdx]->Destroy(Context); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4735 | return true; |
| 4736 | } |
| 4737 | |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4738 | if (Best->Function == 0) { |
| 4739 | // Since there is no function declaration, this is one of the |
| 4740 | // surrogate candidates. Dig out the conversion function. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4741 | CXXConversionDecl *Conv |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4742 | = cast<CXXConversionDecl>( |
| 4743 | Best->Conversions[0].UserDefined.ConversionFunction); |
| 4744 | |
| 4745 | // We selected one of the surrogate functions that converts the |
| 4746 | // object parameter to a function pointer. Perform the conversion |
| 4747 | // on the object argument, then let ActOnCallExpr finish the job. |
| 4748 | // FIXME: Represent the user-defined conversion in the AST! |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4749 | ImpCastExprToType(Object, |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4750 | Conv->getConversionType().getNonReferenceType(), |
Anders Carlsson | 3503d04 | 2009-07-31 01:23:52 +0000 | [diff] [blame] | 4751 | CastExpr::CK_Unknown, |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4752 | Conv->getConversionType()->isLValueReferenceType()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4753 | return ActOnCallExpr(S, ExprArg(*this, Object), LParenLoc, |
| 4754 | MultiExprArg(*this, (ExprTy**)Args, NumArgs), |
| 4755 | CommaLocs, RParenLoc).release(); |
Douglas Gregor | 106c6eb | 2008-11-19 22:57:39 +0000 | [diff] [blame] | 4756 | } |
| 4757 | |
| 4758 | // We found an overloaded operator(). Build a CXXOperatorCallExpr |
| 4759 | // that calls this method, using Object for the implicit object |
| 4760 | // parameter and passing along the remaining arguments. |
| 4761 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4762 | const FunctionProtoType *Proto = Method->getType()->getAsFunctionProtoType(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4763 | |
| 4764 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4765 | unsigned NumArgsToCheck = NumArgs; |
| 4766 | |
| 4767 | // Build the full argument list for the method call (the |
| 4768 | // implicit object parameter is placed at the beginning of the |
| 4769 | // list). |
| 4770 | Expr **MethodArgs; |
| 4771 | if (NumArgs < NumArgsInProto) { |
| 4772 | NumArgsToCheck = NumArgsInProto; |
| 4773 | MethodArgs = new Expr*[NumArgsInProto + 1]; |
| 4774 | } else { |
| 4775 | MethodArgs = new Expr*[NumArgs + 1]; |
| 4776 | } |
| 4777 | MethodArgs[0] = Object; |
| 4778 | for (unsigned ArgIdx = 0; ArgIdx < NumArgs; ++ArgIdx) |
| 4779 | MethodArgs[ArgIdx + 1] = Args[ArgIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4780 | |
| 4781 | Expr *NewFn = new (Context) DeclRefExpr(Method, Method->getType(), |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4782 | SourceLocation()); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4783 | UsualUnaryConversions(NewFn); |
| 4784 | |
| 4785 | // Once we've built TheCall, all of the expressions are properly |
| 4786 | // owned. |
| 4787 | QualType ResultTy = Method->getResultType().getNonReferenceType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4788 | ExprOwningPtr<CXXOperatorCallExpr> |
| 4789 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OO_Call, NewFn, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 4790 | MethodArgs, NumArgs + 1, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4791 | ResultTy, RParenLoc)); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4792 | delete [] MethodArgs; |
| 4793 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4794 | // We may have default arguments. If so, we need to allocate more |
| 4795 | // slots in the call for them. |
| 4796 | if (NumArgs < NumArgsInProto) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4797 | TheCall->setNumArgs(Context, NumArgsInProto + 1); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4798 | else if (NumArgs > NumArgsInProto) |
| 4799 | NumArgsToCheck = NumArgsInProto; |
| 4800 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4801 | bool IsError = false; |
| 4802 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4803 | // Initialize the implicit object parameter. |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4804 | IsError |= PerformObjectArgumentInitialization(Object, Method); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4805 | TheCall->setArg(0, Object); |
| 4806 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4807 | |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4808 | // Check the argument types. |
| 4809 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4810 | Expr *Arg; |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4811 | if (i < NumArgs) { |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4812 | Arg = Args[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4813 | |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4814 | // Pass the argument. |
| 4815 | QualType ProtoArgType = Proto->getArgType(i); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4816 | IsError |= PerformCopyInitialization(Arg, ProtoArgType, "passing"); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4817 | } else { |
Anders Carlsson | f1480ee | 2009-08-14 18:30:22 +0000 | [diff] [blame] | 4818 | Arg = CXXDefaultArgExpr::Create(Context, Method->getParamDecl(i)); |
Douglas Gregor | 518fda1 | 2009-01-13 05:10:00 +0000 | [diff] [blame] | 4819 | } |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4820 | |
| 4821 | TheCall->setArg(i + 1, Arg); |
| 4822 | } |
| 4823 | |
| 4824 | // If this is a variadic call, handle args passed through "...". |
| 4825 | if (Proto->isVariadic()) { |
| 4826 | // Promote the arguments (C99 6.5.2.2p7). |
| 4827 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 4828 | Expr *Arg = Args[i]; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4829 | IsError |= DefaultVariadicArgumentPromotion(Arg, VariadicMethod); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4830 | TheCall->setArg(i + 1, Arg); |
| 4831 | } |
| 4832 | } |
| 4833 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 4834 | if (IsError) return true; |
| 4835 | |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4836 | if (CheckFunctionCall(Method, TheCall.get())) |
| 4837 | return true; |
| 4838 | |
Anders Carlsson | a303f9e | 2009-08-16 03:53:54 +0000 | [diff] [blame] | 4839 | return MaybeBindToTemporary(TheCall.release()).release(); |
Douglas Gregor | f9eb905 | 2008-11-19 21:05:33 +0000 | [diff] [blame] | 4840 | } |
| 4841 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4842 | /// BuildOverloadedArrowExpr - Build a call to an overloaded @c operator-> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4843 | /// (if one exists), where @c Base is an expression of class type and |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4844 | /// @c Member is the name of the member we're trying to find. |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 4845 | Sema::OwningExprResult |
| 4846 | Sema::BuildOverloadedArrowExpr(Scope *S, ExprArg BaseIn, SourceLocation OpLoc) { |
| 4847 | Expr *Base = static_cast<Expr *>(BaseIn.get()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4848 | assert(Base->getType()->isRecordType() && "left-hand side must have class type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4849 | |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4850 | // C++ [over.ref]p1: |
| 4851 | // |
| 4852 | // [...] An expression x->m is interpreted as (x.operator->())->m |
| 4853 | // for a class object x of type T if T::operator->() exists and if |
| 4854 | // the operator is selected as the best match function by the |
| 4855 | // overload resolution mechanism (13.3). |
| 4856 | // FIXME: look in base classes. |
| 4857 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(OO_Arrow); |
| 4858 | OverloadCandidateSet CandidateSet; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4859 | const RecordType *BaseRecord = Base->getType()->getAs<RecordType>(); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 4860 | |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 4861 | LookupResult R = LookupQualifiedName(BaseRecord->getDecl(), OpName, |
| 4862 | LookupOrdinaryName); |
| 4863 | |
| 4864 | for (LookupResult::iterator Oper = R.begin(), OperEnd = R.end(); |
| 4865 | Oper != OperEnd; ++Oper) |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 4866 | AddMethodCandidate(cast<CXXMethodDecl>(*Oper), Base, 0, 0, CandidateSet, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4867 | /*SuppressUserConversions=*/false); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4868 | |
| 4869 | // Perform overload resolution. |
| 4870 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4871 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4872 | case OR_Success: |
| 4873 | // Overload resolution succeeded; we'll build the call below. |
| 4874 | break; |
| 4875 | |
| 4876 | case OR_No_Viable_Function: |
| 4877 | if (CandidateSet.empty()) |
| 4878 | Diag(OpLoc, diag::err_typecheck_member_reference_arrow) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 4879 | << Base->getType() << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4880 | else |
| 4881 | Diag(OpLoc, diag::err_ovl_no_viable_oper) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 4882 | << "operator->" << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4883 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 4884 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4885 | |
| 4886 | case OR_Ambiguous: |
| 4887 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 4888 | << "->" << Base->getSourceRange(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4889 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 4890 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4891 | |
| 4892 | case OR_Deleted: |
| 4893 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 4894 | << Best->Function->isDeleted() |
Anders Carlsson | e30572a | 2009-09-10 23:18:36 +0000 | [diff] [blame] | 4895 | << "->" << Base->getSourceRange(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 4896 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 4897 | return ExprError(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4898 | } |
| 4899 | |
| 4900 | // Convert the object parameter. |
| 4901 | CXXMethodDecl *Method = cast<CXXMethodDecl>(Best->Function); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 4902 | if (PerformObjectArgumentInitialization(Base, Method)) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 4903 | return ExprError(); |
Douglas Gregor | fc195ef | 2008-11-21 03:04:22 +0000 | [diff] [blame] | 4904 | |
| 4905 | // No concerns about early exits now. |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 4906 | BaseIn.release(); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4907 | |
| 4908 | // Build the operator call. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4909 | Expr *FnExpr = new (Context) DeclRefExpr(Method, Method->getType(), |
| 4910 | SourceLocation()); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4911 | UsualUnaryConversions(FnExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4912 | Base = new (Context) CXXOperatorCallExpr(Context, OO_Arrow, FnExpr, &Base, 1, |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4913 | Method->getResultType().getNonReferenceType(), |
| 4914 | OpLoc); |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 4915 | return Owned(Base); |
Douglas Gregor | 8ba1074 | 2008-11-20 16:27:02 +0000 | [diff] [blame] | 4916 | } |
| 4917 | |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4918 | /// FixOverloadedFunctionReference - E is an expression that refers to |
| 4919 | /// a C++ overloaded function (possibly with some parentheses and |
| 4920 | /// perhaps a '&' around it). We have resolved the overloaded function |
| 4921 | /// to the function declaration Fn, so patch up the expression E to |
| 4922 | /// refer (possibly indirectly) to Fn. |
| 4923 | void Sema::FixOverloadedFunctionReference(Expr *E, FunctionDecl *Fn) { |
| 4924 | if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 4925 | FixOverloadedFunctionReference(PE->getSubExpr(), Fn); |
| 4926 | E->setType(PE->getSubExpr()->getType()); |
| 4927 | } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4928 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf && |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4929 | "Can only take the address of an overloaded function"); |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4930 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) { |
| 4931 | if (Method->isStatic()) { |
| 4932 | // Do nothing: static member functions aren't any different |
| 4933 | // from non-member functions. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4934 | } else if (QualifiedDeclRefExpr *DRE |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4935 | = dyn_cast<QualifiedDeclRefExpr>(UnOp->getSubExpr())) { |
| 4936 | // We have taken the address of a pointer to member |
| 4937 | // function. Perform the computation here so that we get the |
| 4938 | // appropriate pointer to member type. |
| 4939 | DRE->setDecl(Fn); |
| 4940 | DRE->setType(Fn->getType()); |
| 4941 | QualType ClassType |
| 4942 | = Context.getTypeDeclType(cast<RecordDecl>(Method->getDeclContext())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4943 | E->setType(Context.getMemberPointerType(Fn->getType(), |
Douglas Gregor | b86b057 | 2009-02-11 01:18:59 +0000 | [diff] [blame] | 4944 | ClassType.getTypePtr())); |
| 4945 | return; |
| 4946 | } |
| 4947 | } |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4948 | FixOverloadedFunctionReference(UnOp->getSubExpr(), Fn); |
Douglas Gregor | a35284b | 2009-02-11 00:19:33 +0000 | [diff] [blame] | 4949 | E->setType(Context.getPointerType(UnOp->getSubExpr()->getType())); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4950 | } else if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E)) { |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4951 | assert((isa<OverloadedFunctionDecl>(DR->getDecl()) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4952 | isa<FunctionTemplateDecl>(DR->getDecl())) && |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4953 | "Expected overloaded function or function template"); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4954 | DR->setDecl(Fn); |
| 4955 | E->setType(Fn->getType()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4956 | } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(E)) { |
| 4957 | MemExpr->setMemberDecl(Fn); |
| 4958 | E->setType(Fn->getType()); |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4959 | } else { |
| 4960 | assert(false && "Invalid reference to overloaded function"); |
| 4961 | } |
| 4962 | } |
| 4963 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4964 | } // end namespace clang |