Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1 | //===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/ |
| 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 | // This file implements C++ template argument deduction. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===/ |
| 12 | |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 13 | #include "clang/Sema/TemplateDeduction.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "TreeTransform.h" |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTLambda.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
| 20 | #include "clang/AST/ExprCXX.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtVisitor.h" |
| 22 | #include "clang/Sema/DeclSpec.h" |
| 23 | #include "clang/Sema/Sema.h" |
| 24 | #include "clang/Sema/Template.h" |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallBitVector.h" |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 27 | |
| 28 | namespace clang { |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 29 | using namespace sema; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 30 | /// \brief Various flags that control template argument deduction. |
| 31 | /// |
| 32 | /// These flags can be bitwise-OR'd together. |
| 33 | enum TemplateDeductionFlags { |
| 34 | /// \brief No template argument deduction flags, which indicates the |
| 35 | /// strictest results for template argument deduction (as used for, e.g., |
| 36 | /// matching class template partial specializations). |
| 37 | TDF_None = 0, |
| 38 | /// \brief Within template argument deduction from a function call, we are |
| 39 | /// matching with a parameter type for which the original parameter was |
| 40 | /// a reference. |
| 41 | TDF_ParamWithReferenceType = 0x1, |
| 42 | /// \brief Within template argument deduction from a function call, we |
| 43 | /// are matching in a case where we ignore cv-qualifiers. |
| 44 | TDF_IgnoreQualifiers = 0x02, |
| 45 | /// \brief Within template argument deduction from a function call, |
| 46 | /// we are matching in a case where we can perform template argument |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 47 | /// deduction from a template-id of a derived class of the argument type. |
Douglas Gregor | 406f634 | 2009-09-14 20:00:47 +0000 | [diff] [blame] | 48 | TDF_DerivedClass = 0x04, |
| 49 | /// \brief Allow non-dependent types to differ, e.g., when performing |
| 50 | /// template argument deduction from a function call where conversions |
| 51 | /// may apply. |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 52 | TDF_SkipNonDependent = 0x08, |
| 53 | /// \brief Whether we are performing template argument deduction for |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 54 | /// parameters and arguments in a top-level template argument |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 55 | TDF_TopLevelParameterTypeList = 0x10, |
| 56 | /// \brief Within template argument deduction from overload resolution per |
| 57 | /// C++ [over.over] allow matching function types that are compatible in |
| 58 | /// terms of noreturn and default calling convention adjustments. |
| 59 | TDF_InOverloadResolution = 0x20 |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 60 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 61 | } |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 62 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 63 | using namespace clang; |
| 64 | |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 65 | /// \brief Compare two APSInts, extending and switching the sign as |
| 66 | /// necessary to compare their values regardless of underlying type. |
| 67 | static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) { |
| 68 | if (Y.getBitWidth() > X.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 69 | X = X.extend(Y.getBitWidth()); |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 70 | else if (Y.getBitWidth() < X.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 71 | Y = Y.extend(X.getBitWidth()); |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 72 | |
| 73 | // If there is a signedness mismatch, correct it. |
| 74 | if (X.isSigned() != Y.isSigned()) { |
| 75 | // If the signed value is negative, then the values cannot be the same. |
| 76 | if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative())) |
| 77 | return false; |
| 78 | |
| 79 | Y.setIsSigned(true); |
| 80 | X.setIsSigned(true); |
| 81 | } |
| 82 | |
| 83 | return X == Y; |
| 84 | } |
| 85 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 86 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 87 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 88 | TemplateParameterList *TemplateParams, |
| 89 | const TemplateArgument &Param, |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 90 | TemplateArgument Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 91 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 92 | SmallVectorImpl<DeducedTemplateArgument> &Deduced); |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 93 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 94 | static Sema::TemplateDeductionResult |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 95 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 96 | TemplateParameterList *TemplateParams, |
| 97 | QualType Param, |
| 98 | QualType Arg, |
| 99 | TemplateDeductionInfo &Info, |
| 100 | SmallVectorImpl<DeducedTemplateArgument> & |
| 101 | Deduced, |
| 102 | unsigned TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 103 | bool PartialOrdering = false); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 104 | |
| 105 | static Sema::TemplateDeductionResult |
| 106 | DeduceTemplateArguments(Sema &S, |
| 107 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 108 | const TemplateArgument *Params, unsigned NumParams, |
| 109 | const TemplateArgument *Args, unsigned NumArgs, |
| 110 | TemplateDeductionInfo &Info, |
Richard Smith | 16b6539 | 2012-12-06 06:44:44 +0000 | [diff] [blame] | 111 | SmallVectorImpl<DeducedTemplateArgument> &Deduced); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 112 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 113 | /// \brief If the given expression is of a form that permits the deduction |
| 114 | /// of a non-type template parameter, return the declaration of that |
| 115 | /// non-type template parameter. |
| 116 | static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) { |
Richard Smith | 7ebb07c | 2012-07-08 04:37:51 +0000 | [diff] [blame] | 117 | // If we are within an alias template, the expression may have undergone |
| 118 | // any number of parameter substitutions already. |
| 119 | while (1) { |
| 120 | if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E)) |
| 121 | E = IC->getSubExpr(); |
| 122 | else if (SubstNonTypeTemplateParmExpr *Subst = |
| 123 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 124 | E = Subst->getReplacement(); |
| 125 | else |
| 126 | break; |
| 127 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 129 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 130 | return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 132 | return nullptr; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 135 | /// \brief Determine whether two declaration pointers refer to the same |
| 136 | /// declaration. |
| 137 | static bool isSameDeclaration(Decl *X, Decl *Y) { |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 138 | if (NamedDecl *NX = dyn_cast<NamedDecl>(X)) |
| 139 | X = NX->getUnderlyingDecl(); |
| 140 | if (NamedDecl *NY = dyn_cast<NamedDecl>(Y)) |
| 141 | Y = NY->getUnderlyingDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 142 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 143 | return X->getCanonicalDecl() == Y->getCanonicalDecl(); |
| 144 | } |
| 145 | |
| 146 | /// \brief Verify that the given, deduced template arguments are compatible. |
| 147 | /// |
| 148 | /// \returns The deduced template argument, or a NULL template argument if |
| 149 | /// the deduced template arguments were incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 150 | static DeducedTemplateArgument |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 151 | checkDeducedTemplateArguments(ASTContext &Context, |
| 152 | const DeducedTemplateArgument &X, |
| 153 | const DeducedTemplateArgument &Y) { |
| 154 | // We have no deduction for one or both of the arguments; they're compatible. |
| 155 | if (X.isNull()) |
| 156 | return Y; |
| 157 | if (Y.isNull()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 158 | return X; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 159 | |
| 160 | switch (X.getKind()) { |
| 161 | case TemplateArgument::Null: |
| 162 | llvm_unreachable("Non-deduced template arguments handled above"); |
| 163 | |
| 164 | case TemplateArgument::Type: |
| 165 | // If two template type arguments have the same type, they're compatible. |
| 166 | if (Y.getKind() == TemplateArgument::Type && |
| 167 | Context.hasSameType(X.getAsType(), Y.getAsType())) |
| 168 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 169 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 170 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 171 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 172 | case TemplateArgument::Integral: |
| 173 | // If we deduced a constant in one case and either a dependent expression or |
| 174 | // declaration in another case, keep the integral constant. |
| 175 | // If both are integral constants with the same value, keep that value. |
| 176 | if (Y.getKind() == TemplateArgument::Expression || |
| 177 | Y.getKind() == TemplateArgument::Declaration || |
| 178 | (Y.getKind() == TemplateArgument::Integral && |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 179 | hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 180 | return DeducedTemplateArgument(X, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 181 | X.wasDeducedFromArrayBound() && |
| 182 | Y.wasDeducedFromArrayBound()); |
| 183 | |
| 184 | // All other combinations are incompatible. |
| 185 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 186 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 187 | case TemplateArgument::Template: |
| 188 | if (Y.getKind() == TemplateArgument::Template && |
| 189 | Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate())) |
| 190 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 191 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 192 | // All other combinations are incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 193 | return DeducedTemplateArgument(); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 194 | |
| 195 | case TemplateArgument::TemplateExpansion: |
| 196 | if (Y.getKind() == TemplateArgument::TemplateExpansion && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 197 | Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 198 | Y.getAsTemplateOrTemplatePattern())) |
| 199 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 200 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 201 | // All other combinations are incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 202 | return DeducedTemplateArgument(); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 203 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 204 | case TemplateArgument::Expression: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 205 | // If we deduced a dependent expression in one case and either an integral |
| 206 | // constant or a declaration in another case, keep the integral constant |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 207 | // or declaration. |
| 208 | if (Y.getKind() == TemplateArgument::Integral || |
| 209 | Y.getKind() == TemplateArgument::Declaration) |
| 210 | return DeducedTemplateArgument(Y, X.wasDeducedFromArrayBound() && |
| 211 | Y.wasDeducedFromArrayBound()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 212 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 213 | if (Y.getKind() == TemplateArgument::Expression) { |
| 214 | // Compare the expressions for equality |
| 215 | llvm::FoldingSetNodeID ID1, ID2; |
| 216 | X.getAsExpr()->Profile(ID1, Context, true); |
| 217 | Y.getAsExpr()->Profile(ID2, Context, true); |
| 218 | if (ID1 == ID2) |
| 219 | return X; |
| 220 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 221 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 222 | // All other combinations are incompatible. |
| 223 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 224 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 225 | case TemplateArgument::Declaration: |
| 226 | // If we deduced a declaration and a dependent expression, keep the |
| 227 | // declaration. |
| 228 | if (Y.getKind() == TemplateArgument::Expression) |
| 229 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 230 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 231 | // If we deduced a declaration and an integral constant, keep the |
| 232 | // integral constant. |
| 233 | if (Y.getKind() == TemplateArgument::Integral) |
| 234 | return Y; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 235 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 236 | // If we deduced two declarations, make sure they they refer to the |
| 237 | // same declaration. |
| 238 | if (Y.getKind() == TemplateArgument::Declaration && |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 239 | isSameDeclaration(X.getAsDecl(), Y.getAsDecl())) |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 240 | return X; |
| 241 | |
| 242 | // All other combinations are incompatible. |
| 243 | return DeducedTemplateArgument(); |
| 244 | |
| 245 | case TemplateArgument::NullPtr: |
| 246 | // If we deduced a null pointer and a dependent expression, keep the |
| 247 | // null pointer. |
| 248 | if (Y.getKind() == TemplateArgument::Expression) |
| 249 | return X; |
| 250 | |
| 251 | // If we deduced a null pointer and an integral constant, keep the |
| 252 | // integral constant. |
| 253 | if (Y.getKind() == TemplateArgument::Integral) |
| 254 | return Y; |
| 255 | |
| 256 | // If we deduced two null pointers, make sure they have the same type. |
| 257 | if (Y.getKind() == TemplateArgument::NullPtr && |
| 258 | Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType())) |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 259 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 260 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 261 | // All other combinations are incompatible. |
| 262 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 263 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 264 | case TemplateArgument::Pack: |
| 265 | if (Y.getKind() != TemplateArgument::Pack || |
| 266 | X.pack_size() != Y.pack_size()) |
| 267 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 268 | |
| 269 | for (TemplateArgument::pack_iterator XA = X.pack_begin(), |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 270 | XAEnd = X.pack_end(), |
| 271 | YA = Y.pack_begin(); |
| 272 | XA != XAEnd; ++XA, ++YA) { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 273 | // FIXME: Do we need to merge the results together here? |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 274 | if (checkDeducedTemplateArguments(Context, |
| 275 | DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()), |
Douglas Gregor | f491ee2 | 2011-01-05 21:00:53 +0000 | [diff] [blame] | 276 | DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound())) |
| 277 | .isNull()) |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 278 | return DeducedTemplateArgument(); |
| 279 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 280 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 281 | return X; |
| 282 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 283 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 284 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | /// \brief Deduce the value of the given non-type template parameter |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 288 | /// from the given constant. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 289 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 290 | DeduceNonTypeTemplateArgument(Sema &S, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | NonTypeTemplateParmDecl *NTTP, |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 292 | llvm::APSInt Value, QualType ValueType, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 293 | bool DeducedFromArrayBound, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 294 | TemplateDeductionInfo &Info, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 295 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 297 | "Cannot deduce non-type template argument with depth > 0"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 299 | DeducedTemplateArgument NewDeduced(S.Context, Value, ValueType, |
| 300 | DeducedFromArrayBound); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 301 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 302 | Deduced[NTTP->getIndex()], |
| 303 | NewDeduced); |
| 304 | if (Result.isNull()) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 305 | Info.Param = NTTP; |
| 306 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 307 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 308 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 309 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 310 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 311 | Deduced[NTTP->getIndex()] = Result; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 312 | return Sema::TDK_Success; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | /// \brief Deduce the value of the given non-type template parameter |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 316 | /// from the given type- or value-dependent expression. |
| 317 | /// |
| 318 | /// \returns true if deduction succeeded, false otherwise. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 319 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 320 | DeduceNonTypeTemplateArgument(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 321 | NonTypeTemplateParmDecl *NTTP, |
| 322 | Expr *Value, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 323 | TemplateDeductionInfo &Info, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 324 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 326 | "Cannot deduce non-type template argument with depth > 0"); |
| 327 | assert((Value->isTypeDependent() || Value->isValueDependent()) && |
| 328 | "Expression template argument must be type- or value-dependent."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 329 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 330 | DeducedTemplateArgument NewDeduced(Value); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 331 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
| 332 | Deduced[NTTP->getIndex()], |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 333 | NewDeduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 334 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 335 | if (Result.isNull()) { |
| 336 | Info.Param = NTTP; |
| 337 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| 338 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 339 | return Sema::TDK_Inconsistent; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 340 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 341 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 342 | Deduced[NTTP->getIndex()] = Result; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 343 | return Sema::TDK_Success; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 346 | /// \brief Deduce the value of the given non-type template parameter |
| 347 | /// from the given declaration. |
| 348 | /// |
| 349 | /// \returns true if deduction succeeded, false otherwise. |
| 350 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 351 | DeduceNonTypeTemplateArgument(Sema &S, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 352 | NonTypeTemplateParmDecl *NTTP, |
| 353 | ValueDecl *D, |
| 354 | TemplateDeductionInfo &Info, |
| 355 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 356 | assert(NTTP->getDepth() == 0 && |
| 357 | "Cannot deduce non-type template argument with depth > 0"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 358 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 359 | D = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 360 | TemplateArgument New(D, NTTP->getType()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 361 | DeducedTemplateArgument NewDeduced(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 362 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 363 | Deduced[NTTP->getIndex()], |
| 364 | NewDeduced); |
| 365 | if (Result.isNull()) { |
| 366 | Info.Param = NTTP; |
| 367 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| 368 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 369 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 370 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 371 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 372 | Deduced[NTTP->getIndex()] = Result; |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 373 | return Sema::TDK_Success; |
| 374 | } |
| 375 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 376 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 377 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 378 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 379 | TemplateName Param, |
| 380 | TemplateName Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 381 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 382 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 383 | TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 384 | if (!ParamDecl) { |
| 385 | // The parameter type is dependent and is not a template template parameter, |
| 386 | // so there is nothing that we can deduce. |
| 387 | return Sema::TDK_Success; |
| 388 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 389 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 390 | if (TemplateTemplateParmDecl *TempParam |
| 391 | = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 392 | DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 393 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 394 | Deduced[TempParam->getIndex()], |
| 395 | NewDeduced); |
| 396 | if (Result.isNull()) { |
| 397 | Info.Param = TempParam; |
| 398 | Info.FirstArg = Deduced[TempParam->getIndex()]; |
| 399 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 400 | return Sema::TDK_Inconsistent; |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 401 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 402 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 403 | Deduced[TempParam->getIndex()] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 404 | return Sema::TDK_Success; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 405 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 406 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 407 | // Verify that the two template names are equivalent. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 408 | if (S.Context.hasSameTemplateName(Param, Arg)) |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 409 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 410 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 411 | // Mismatch of non-dependent template parameter to argument. |
| 412 | Info.FirstArg = TemplateArgument(Param); |
| 413 | Info.SecondArg = TemplateArgument(Arg); |
| 414 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | /// \brief Deduce the template arguments by comparing the template parameter |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 418 | /// type (which is a template-id) with the template argument type. |
| 419 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 420 | /// \param S the Sema |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 421 | /// |
| 422 | /// \param TemplateParams the template parameters that we are deducing |
| 423 | /// |
| 424 | /// \param Param the parameter type |
| 425 | /// |
| 426 | /// \param Arg the argument type |
| 427 | /// |
| 428 | /// \param Info information about the template argument deduction itself |
| 429 | /// |
| 430 | /// \param Deduced the deduced template arguments |
| 431 | /// |
| 432 | /// \returns the result of template argument deduction so far. Note that a |
| 433 | /// "success" result means that template argument deduction has not yet failed, |
| 434 | /// but it may still fail, later, for other reasons. |
| 435 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 436 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 437 | TemplateParameterList *TemplateParams, |
| 438 | const TemplateSpecializationType *Param, |
| 439 | QualType Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 440 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 441 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 442 | assert(Arg.isCanonical() && "Argument type must be canonical"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 444 | // Check whether the template argument is a dependent template-id. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | if (const TemplateSpecializationType *SpecArg |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 446 | = dyn_cast<TemplateSpecializationType>(Arg)) { |
| 447 | // Perform template argument deduction for the template name. |
| 448 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 449 | = DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 450 | Param->getTemplateName(), |
| 451 | SpecArg->getTemplateName(), |
| 452 | Info, Deduced)) |
| 453 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 456 | // Perform template argument deduction on each template |
Douglas Gregor | d80ea20 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 457 | // argument. Ignore any missing/extra arguments, since they could be |
| 458 | // filled in by default arguments. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 459 | return DeduceTemplateArguments(S, TemplateParams, |
| 460 | Param->getArgs(), Param->getNumArgs(), |
Douglas Gregor | d80ea20 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 461 | SpecArg->getArgs(), SpecArg->getNumArgs(), |
Richard Smith | 16b6539 | 2012-12-06 06:44:44 +0000 | [diff] [blame] | 462 | Info, Deduced); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 463 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 465 | // If the argument type is a class template specialization, we |
| 466 | // perform template argument deduction using its template |
| 467 | // arguments. |
| 468 | const RecordType *RecordArg = dyn_cast<RecordType>(Arg); |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 469 | if (!RecordArg) { |
| 470 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 471 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 472 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 473 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | |
| 475 | ClassTemplateSpecializationDecl *SpecArg |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 476 | = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 477 | if (!SpecArg) { |
| 478 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 479 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 480 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 481 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 483 | // Perform template argument deduction for the template name. |
| 484 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 485 | = DeduceTemplateArguments(S, |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 486 | TemplateParams, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 487 | Param->getTemplateName(), |
| 488 | TemplateName(SpecArg->getSpecializedTemplate()), |
| 489 | Info, Deduced)) |
| 490 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 492 | // Perform template argument deduction for the template arguments. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 493 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 494 | Param->getArgs(), Param->getNumArgs(), |
| 495 | SpecArg->getTemplateArgs().data(), |
| 496 | SpecArg->getTemplateArgs().size(), |
| 497 | Info, Deduced); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 498 | } |
| 499 | |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 500 | /// \brief Determines whether the given type is an opaque type that |
| 501 | /// might be more qualified when instantiated. |
| 502 | static bool IsPossiblyOpaquelyQualifiedType(QualType T) { |
| 503 | switch (T->getTypeClass()) { |
| 504 | case Type::TypeOfExpr: |
| 505 | case Type::TypeOf: |
| 506 | case Type::DependentName: |
| 507 | case Type::Decltype: |
| 508 | case Type::UnresolvedUsing: |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 509 | case Type::TemplateTypeParm: |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 510 | return true; |
| 511 | |
| 512 | case Type::ConstantArray: |
| 513 | case Type::IncompleteArray: |
| 514 | case Type::VariableArray: |
| 515 | case Type::DependentSizedArray: |
| 516 | return IsPossiblyOpaquelyQualifiedType( |
| 517 | cast<ArrayType>(T)->getElementType()); |
| 518 | |
| 519 | default: |
| 520 | return false; |
| 521 | } |
| 522 | } |
| 523 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 524 | /// \brief Retrieve the depth and index of a template parameter. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 525 | static std::pair<unsigned, unsigned> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 526 | getDepthAndIndex(NamedDecl *ND) { |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 527 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) |
| 528 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 529 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 530 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND)) |
| 531 | return std::make_pair(NTTP->getDepth(), NTTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 532 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 533 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND); |
| 534 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 535 | } |
| 536 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 537 | /// \brief Retrieve the depth and index of an unexpanded parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 538 | static std::pair<unsigned, unsigned> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 539 | getDepthAndIndex(UnexpandedParameterPack UPP) { |
| 540 | if (const TemplateTypeParmType *TTP |
| 541 | = UPP.first.dyn_cast<const TemplateTypeParmType *>()) |
| 542 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 543 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 544 | return getDepthAndIndex(UPP.first.get<NamedDecl *>()); |
| 545 | } |
| 546 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 547 | /// \brief Helper function to build a TemplateParameter when we don't |
| 548 | /// know its type statically. |
| 549 | static TemplateParameter makeTemplateParameter(Decl *D) { |
| 550 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
| 551 | return TemplateParameter(TTP); |
Craig Topper | 4b482ee | 2013-07-08 04:24:47 +0000 | [diff] [blame] | 552 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 553 | return TemplateParameter(NTTP); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 554 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 555 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
| 556 | } |
| 557 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 558 | /// A pack that we're currently deducing. |
| 559 | struct clang::DeducedPack { |
| 560 | DeducedPack(unsigned Index) : Index(Index), Outer(nullptr) {} |
Craig Topper | 0a4e1f5 | 2013-07-08 04:44:01 +0000 | [diff] [blame] | 561 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 562 | // The index of the pack. |
| 563 | unsigned Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 564 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 565 | // The old value of the pack before we started deducing it. |
| 566 | DeducedTemplateArgument Saved; |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 567 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 568 | // A deferred value of this pack from an inner deduction, that couldn't be |
| 569 | // deduced because this deduction hadn't happened yet. |
| 570 | DeducedTemplateArgument DeferredDeduction; |
| 571 | |
| 572 | // The new value of the pack. |
| 573 | SmallVector<DeducedTemplateArgument, 4> New; |
| 574 | |
| 575 | // The outer deduction for this pack, if any. |
| 576 | DeducedPack *Outer; |
| 577 | }; |
| 578 | |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 579 | namespace { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 580 | /// A scope in which we're performing pack deduction. |
| 581 | class PackDeductionScope { |
| 582 | public: |
| 583 | PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, |
| 584 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 585 | TemplateDeductionInfo &Info, TemplateArgument Pattern) |
| 586 | : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { |
| 587 | // Compute the set of template parameter indices that correspond to |
| 588 | // parameter packs expanded by the pack expansion. |
| 589 | { |
| 590 | llvm::SmallBitVector SawIndices(TemplateParams->size()); |
| 591 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 592 | S.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| 593 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 594 | unsigned Depth, Index; |
| 595 | std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
| 596 | if (Depth == 0 && !SawIndices[Index]) { |
| 597 | SawIndices[Index] = true; |
| 598 | |
| 599 | // Save the deduced template argument for the parameter pack expanded |
| 600 | // by this pack expansion, then clear out the deduction. |
| 601 | DeducedPack Pack(Index); |
| 602 | Pack.Saved = Deduced[Index]; |
| 603 | Deduced[Index] = TemplateArgument(); |
| 604 | |
| 605 | Packs.push_back(Pack); |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | assert(!Packs.empty() && "Pack expansion without unexpanded packs?"); |
| 610 | |
| 611 | for (auto &Pack : Packs) { |
| 612 | if (Info.PendingDeducedPacks.size() > Pack.Index) |
| 613 | Pack.Outer = Info.PendingDeducedPacks[Pack.Index]; |
| 614 | else |
| 615 | Info.PendingDeducedPacks.resize(Pack.Index + 1); |
| 616 | Info.PendingDeducedPacks[Pack.Index] = &Pack; |
| 617 | |
| 618 | if (S.CurrentInstantiationScope) { |
| 619 | // If the template argument pack was explicitly specified, add that to |
| 620 | // the set of deduced arguments. |
| 621 | const TemplateArgument *ExplicitArgs; |
| 622 | unsigned NumExplicitArgs; |
| 623 | NamedDecl *PartiallySubstitutedPack = |
| 624 | S.CurrentInstantiationScope->getPartiallySubstitutedPack( |
| 625 | &ExplicitArgs, &NumExplicitArgs); |
| 626 | if (PartiallySubstitutedPack && |
| 627 | getDepthAndIndex(PartiallySubstitutedPack).second == Pack.Index) |
| 628 | Pack.New.append(ExplicitArgs, ExplicitArgs + NumExplicitArgs); |
| 629 | } |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 630 | } |
| 631 | } |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 632 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 633 | ~PackDeductionScope() { |
| 634 | for (auto &Pack : Packs) |
| 635 | Info.PendingDeducedPacks[Pack.Index] = Pack.Outer; |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 636 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 637 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 638 | /// Move to deducing the next element in each pack that is being deduced. |
| 639 | void nextPackElement() { |
| 640 | // Capture the deduced template arguments for each parameter pack expanded |
| 641 | // by this pack expansion, add them to the list of arguments we've deduced |
| 642 | // for that pack, then clear out the deduced argument. |
| 643 | for (auto &Pack : Packs) { |
| 644 | DeducedTemplateArgument &DeducedArg = Deduced[Pack.Index]; |
| 645 | if (!DeducedArg.isNull()) { |
| 646 | Pack.New.push_back(DeducedArg); |
| 647 | DeducedArg = DeducedTemplateArgument(); |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | /// \brief Finish template argument deduction for a set of argument packs, |
| 653 | /// producing the argument packs and checking for consistency with prior |
| 654 | /// deductions. |
| 655 | Sema::TemplateDeductionResult finish(bool HasAnyArguments) { |
| 656 | // Build argument packs for each of the parameter packs expanded by this |
| 657 | // pack expansion. |
| 658 | for (auto &Pack : Packs) { |
| 659 | // Put back the old value for this pack. |
| 660 | Deduced[Pack.Index] = Pack.Saved; |
| 661 | |
| 662 | // Build or find a new value for this pack. |
| 663 | DeducedTemplateArgument NewPack; |
| 664 | if (HasAnyArguments && Pack.New.empty()) { |
| 665 | if (Pack.DeferredDeduction.isNull()) { |
| 666 | // We were not able to deduce anything for this parameter pack |
| 667 | // (because it only appeared in non-deduced contexts), so just |
| 668 | // restore the saved argument pack. |
| 669 | continue; |
| 670 | } |
| 671 | |
| 672 | NewPack = Pack.DeferredDeduction; |
| 673 | Pack.DeferredDeduction = TemplateArgument(); |
| 674 | } else if (Pack.New.empty()) { |
| 675 | // If we deduced an empty argument pack, create it now. |
| 676 | NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack()); |
| 677 | } else { |
| 678 | TemplateArgument *ArgumentPack = |
| 679 | new (S.Context) TemplateArgument[Pack.New.size()]; |
| 680 | std::copy(Pack.New.begin(), Pack.New.end(), ArgumentPack); |
| 681 | NewPack = DeducedTemplateArgument( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 682 | TemplateArgument(llvm::makeArrayRef(ArgumentPack, Pack.New.size())), |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 683 | Pack.New[0].wasDeducedFromArrayBound()); |
| 684 | } |
| 685 | |
| 686 | // Pick where we're going to put the merged pack. |
| 687 | DeducedTemplateArgument *Loc; |
| 688 | if (Pack.Outer) { |
| 689 | if (Pack.Outer->DeferredDeduction.isNull()) { |
| 690 | // Defer checking this pack until we have a complete pack to compare |
| 691 | // it against. |
| 692 | Pack.Outer->DeferredDeduction = NewPack; |
| 693 | continue; |
| 694 | } |
| 695 | Loc = &Pack.Outer->DeferredDeduction; |
| 696 | } else { |
| 697 | Loc = &Deduced[Pack.Index]; |
| 698 | } |
| 699 | |
| 700 | // Check the new pack matches any previous value. |
| 701 | DeducedTemplateArgument OldPack = *Loc; |
| 702 | DeducedTemplateArgument Result = |
| 703 | checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 704 | |
| 705 | // If we deferred a deduction of this pack, check that one now too. |
| 706 | if (!Result.isNull() && !Pack.DeferredDeduction.isNull()) { |
| 707 | OldPack = Result; |
| 708 | NewPack = Pack.DeferredDeduction; |
| 709 | Result = checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 710 | } |
| 711 | |
| 712 | if (Result.isNull()) { |
| 713 | Info.Param = |
| 714 | makeTemplateParameter(TemplateParams->getParam(Pack.Index)); |
| 715 | Info.FirstArg = OldPack; |
| 716 | Info.SecondArg = NewPack; |
| 717 | return Sema::TDK_Inconsistent; |
| 718 | } |
| 719 | |
| 720 | *Loc = Result; |
| 721 | } |
| 722 | |
| 723 | return Sema::TDK_Success; |
| 724 | } |
| 725 | |
| 726 | private: |
| 727 | Sema &S; |
| 728 | TemplateParameterList *TemplateParams; |
| 729 | SmallVectorImpl<DeducedTemplateArgument> &Deduced; |
| 730 | TemplateDeductionInfo &Info; |
| 731 | |
| 732 | SmallVector<DeducedPack, 2> Packs; |
| 733 | }; |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 734 | } // namespace |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 735 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 736 | /// \brief Deduce the template arguments by comparing the list of parameter |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 737 | /// types to the list of argument types, as in the parameter-type-lists of |
| 738 | /// function types (C++ [temp.deduct.type]p10). |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 739 | /// |
| 740 | /// \param S The semantic analysis object within which we are deducing |
| 741 | /// |
| 742 | /// \param TemplateParams The template parameters that we are deducing |
| 743 | /// |
| 744 | /// \param Params The list of parameter types |
| 745 | /// |
| 746 | /// \param NumParams The number of types in \c Params |
| 747 | /// |
| 748 | /// \param Args The list of argument types |
| 749 | /// |
| 750 | /// \param NumArgs The number of types in \c Args |
| 751 | /// |
| 752 | /// \param Info information about the template argument deduction itself |
| 753 | /// |
| 754 | /// \param Deduced the deduced template arguments |
| 755 | /// |
| 756 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
| 757 | /// how template argument deduction is performed. |
| 758 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 759 | /// \param PartialOrdering If true, we are performing template argument |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 760 | /// deduction for during partial ordering for a call |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 761 | /// (C++0x [temp.deduct.partial]). |
| 762 | /// |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 763 | /// \returns the result of template argument deduction so far. Note that a |
| 764 | /// "success" result means that template argument deduction has not yet failed, |
| 765 | /// but it may still fail, later, for other reasons. |
| 766 | static Sema::TemplateDeductionResult |
| 767 | DeduceTemplateArguments(Sema &S, |
| 768 | TemplateParameterList *TemplateParams, |
| 769 | const QualType *Params, unsigned NumParams, |
| 770 | const QualType *Args, unsigned NumArgs, |
| 771 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 772 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 773 | unsigned TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 774 | bool PartialOrdering = false) { |
Douglas Gregor | 86bea35 | 2011-01-05 23:23:17 +0000 | [diff] [blame] | 775 | // Fast-path check to see if we have too many/too few arguments. |
| 776 | if (NumParams != NumArgs && |
| 777 | !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) && |
| 778 | !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1]))) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 779 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 780 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 781 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 782 | // Similarly, if P has a form that contains (T), then each parameter type |
| 783 | // Pi of the respective parameter-type- list of P is compared with the |
| 784 | // corresponding parameter type Ai of the corresponding parameter-type-list |
| 785 | // of A. [...] |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 786 | unsigned ArgIdx = 0, ParamIdx = 0; |
| 787 | for (; ParamIdx != NumParams; ++ParamIdx) { |
| 788 | // Check argument types. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 789 | const PackExpansionType *Expansion |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 790 | = dyn_cast<PackExpansionType>(Params[ParamIdx]); |
| 791 | if (!Expansion) { |
| 792 | // Simple case: compare the parameter and argument types at this point. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 793 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 794 | // Make sure we have an argument. |
| 795 | if (ArgIdx >= NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 796 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 797 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 798 | if (isa<PackExpansionType>(Args[ArgIdx])) { |
| 799 | // C++0x [temp.deduct.type]p22: |
| 800 | // If the original function parameter associated with A is a function |
| 801 | // parameter pack and the function parameter associated with P is not |
| 802 | // a function parameter pack, then template argument deduction fails. |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 803 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 804 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 805 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 806 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 807 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 808 | Params[ParamIdx], Args[ArgIdx], |
| 809 | Info, Deduced, TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 810 | PartialOrdering)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 811 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 812 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 813 | ++ArgIdx; |
| 814 | continue; |
| 815 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 816 | |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 817 | // C++0x [temp.deduct.type]p5: |
| 818 | // The non-deduced contexts are: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 819 | // - A function parameter pack that does not occur at the end of the |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 820 | // parameter-declaration-clause. |
| 821 | if (ParamIdx + 1 < NumParams) |
| 822 | return Sema::TDK_Success; |
| 823 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 824 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 825 | // If the parameter-declaration corresponding to Pi is a function |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 826 | // parameter pack, then the type of its declarator- id is compared with |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 827 | // each remaining parameter type in the parameter-type-list of A. Each |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 828 | // comparison deduces template arguments for subsequent positions in the |
| 829 | // template parameter packs expanded by the function parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 830 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 831 | QualType Pattern = Expansion->getPattern(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 832 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 833 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 834 | bool HasAnyArguments = false; |
| 835 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| 836 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 837 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 838 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 839 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 840 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, Pattern, |
| 841 | Args[ArgIdx], Info, Deduced, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 842 | TDF, PartialOrdering)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 843 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 844 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 845 | PackScope.nextPackElement(); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 846 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 847 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 848 | // Build argument packs for each of the parameter packs expanded by this |
| 849 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 850 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 851 | return Result; |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 852 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 853 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 854 | // Make sure we don't have any extra arguments. |
| 855 | if (ArgIdx < NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 856 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 857 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 858 | return Sema::TDK_Success; |
| 859 | } |
| 860 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 861 | /// \brief Determine whether the parameter has qualifiers that are either |
| 862 | /// inconsistent with or a superset of the argument's qualifiers. |
| 863 | static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType, |
| 864 | QualType ArgType) { |
| 865 | Qualifiers ParamQs = ParamType.getQualifiers(); |
| 866 | Qualifiers ArgQs = ArgType.getQualifiers(); |
| 867 | |
| 868 | if (ParamQs == ArgQs) |
| 869 | return false; |
| 870 | |
| 871 | // Mismatched (but not missing) Objective-C GC attributes. |
| 872 | if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() && |
| 873 | ParamQs.hasObjCGCAttr()) |
| 874 | return true; |
| 875 | |
| 876 | // Mismatched (but not missing) address spaces. |
| 877 | if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() && |
| 878 | ParamQs.hasAddressSpace()) |
| 879 | return true; |
| 880 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 881 | // Mismatched (but not missing) Objective-C lifetime qualifiers. |
| 882 | if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() && |
| 883 | ParamQs.hasObjCLifetime()) |
| 884 | return true; |
| 885 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 886 | // CVR qualifier superset. |
| 887 | return (ParamQs.getCVRQualifiers() != ArgQs.getCVRQualifiers()) && |
| 888 | ((ParamQs.getCVRQualifiers() | ArgQs.getCVRQualifiers()) |
| 889 | == ParamQs.getCVRQualifiers()); |
| 890 | } |
| 891 | |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 892 | /// \brief Compare types for equality with respect to possibly compatible |
| 893 | /// function types (noreturn adjustment, implicit calling conventions). If any |
| 894 | /// of parameter and argument is not a function, just perform type comparison. |
| 895 | /// |
| 896 | /// \param Param the template parameter type. |
| 897 | /// |
| 898 | /// \param Arg the argument type. |
| 899 | bool Sema::isSameOrCompatibleFunctionType(CanQualType Param, |
| 900 | CanQualType Arg) { |
| 901 | const FunctionType *ParamFunction = Param->getAs<FunctionType>(), |
| 902 | *ArgFunction = Arg->getAs<FunctionType>(); |
| 903 | |
| 904 | // Just compare if not functions. |
| 905 | if (!ParamFunction || !ArgFunction) |
| 906 | return Param == Arg; |
| 907 | |
| 908 | // Noreturn adjustment. |
| 909 | QualType AdjustedParam; |
| 910 | if (IsNoReturnConversion(Param, Arg, AdjustedParam)) |
| 911 | return Arg == Context.getCanonicalType(AdjustedParam); |
| 912 | |
| 913 | // FIXME: Compatible calling conventions. |
| 914 | |
| 915 | return Param == Arg; |
| 916 | } |
| 917 | |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 918 | /// \brief Deduce the template arguments by comparing the parameter type and |
| 919 | /// the argument type (C++ [temp.deduct.type]). |
| 920 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 921 | /// \param S the semantic analysis object within which we are deducing |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 922 | /// |
| 923 | /// \param TemplateParams the template parameters that we are deducing |
| 924 | /// |
| 925 | /// \param ParamIn the parameter type |
| 926 | /// |
| 927 | /// \param ArgIn the argument type |
| 928 | /// |
| 929 | /// \param Info information about the template argument deduction itself |
| 930 | /// |
| 931 | /// \param Deduced the deduced template arguments |
| 932 | /// |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 933 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 934 | /// how template argument deduction is performed. |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 935 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 936 | /// \param PartialOrdering Whether we're performing template argument deduction |
| 937 | /// in the context of partial ordering (C++0x [temp.deduct.partial]). |
| 938 | /// |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 939 | /// \returns the result of template argument deduction so far. Note that a |
| 940 | /// "success" result means that template argument deduction has not yet failed, |
| 941 | /// but it may still fail, later, for other reasons. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 942 | static Sema::TemplateDeductionResult |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 943 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 944 | TemplateParameterList *TemplateParams, |
| 945 | QualType ParamIn, QualType ArgIn, |
| 946 | TemplateDeductionInfo &Info, |
| 947 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 948 | unsigned TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 949 | bool PartialOrdering) { |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 950 | // We only want to look at the canonical types, since typedefs and |
| 951 | // sugar are not part of template argument deduction. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 952 | QualType Param = S.Context.getCanonicalType(ParamIn); |
| 953 | QualType Arg = S.Context.getCanonicalType(ArgIn); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 954 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 955 | // If the argument type is a pack expansion, look at its pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 956 | // This isn't explicitly called out |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 957 | if (const PackExpansionType *ArgExpansion |
| 958 | = dyn_cast<PackExpansionType>(Arg)) |
| 959 | Arg = ArgExpansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 960 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 961 | if (PartialOrdering) { |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 962 | // C++11 [temp.deduct.partial]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 963 | // Before the partial ordering is done, certain transformations are |
| 964 | // performed on the types used for partial ordering: |
| 965 | // - If P is a reference type, P is replaced by the type referred to. |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 966 | const ReferenceType *ParamRef = Param->getAs<ReferenceType>(); |
| 967 | if (ParamRef) |
| 968 | Param = ParamRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 969 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 970 | // - If A is a reference type, A is replaced by the type referred to. |
| 971 | const ReferenceType *ArgRef = Arg->getAs<ReferenceType>(); |
| 972 | if (ArgRef) |
| 973 | Arg = ArgRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 974 | |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 975 | if (ParamRef && ArgRef && S.Context.hasSameUnqualifiedType(Param, Arg)) { |
| 976 | // C++11 [temp.deduct.partial]p9: |
| 977 | // If, for a given type, deduction succeeds in both directions (i.e., |
| 978 | // the types are identical after the transformations above) and both |
| 979 | // P and A were reference types [...]: |
| 980 | // - if [one type] was an lvalue reference and [the other type] was |
| 981 | // not, [the other type] is not considered to be at least as |
| 982 | // specialized as [the first type] |
| 983 | // - if [one type] is more cv-qualified than [the other type], |
| 984 | // [the other type] is not considered to be at least as specialized |
| 985 | // as [the first type] |
| 986 | // Objective-C ARC adds: |
| 987 | // - [one type] has non-trivial lifetime, [the other type] has |
| 988 | // __unsafe_unretained lifetime, and the types are otherwise |
| 989 | // identical |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 990 | // |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 991 | // A is "considered to be at least as specialized" as P iff deduction |
| 992 | // succeeds, so we model this as a deduction failure. Note that |
| 993 | // [the first type] is P and [the other type] is A here; the standard |
| 994 | // gets this backwards. |
Douglas Gregor | 85894a8 | 2011-04-30 17:07:52 +0000 | [diff] [blame] | 995 | Qualifiers ParamQuals = Param.getQualifiers(); |
| 996 | Qualifiers ArgQuals = Arg.getQualifiers(); |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 997 | if ((ParamRef->isLValueReferenceType() && |
| 998 | !ArgRef->isLValueReferenceType()) || |
| 999 | ParamQuals.isStrictSupersetOf(ArgQuals) || |
| 1000 | (ParamQuals.hasNonTrivialObjCLifetime() && |
| 1001 | ArgQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone && |
| 1002 | ParamQuals.withoutObjCLifetime() == |
| 1003 | ArgQuals.withoutObjCLifetime())) { |
| 1004 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1005 | Info.SecondArg = TemplateArgument(ArgIn); |
| 1006 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 6beabee | 2014-01-02 19:42:02 +0000 | [diff] [blame] | 1007 | } |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1008 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1009 | |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1010 | // C++11 [temp.deduct.partial]p7: |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1011 | // Remove any top-level cv-qualifiers: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1012 | // - If P is a cv-qualified type, P is replaced by the cv-unqualified |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1013 | // version of P. |
| 1014 | Param = Param.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1015 | // - If A is a cv-qualified type, A is replaced by the cv-unqualified |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1016 | // version of A. |
| 1017 | Arg = Arg.getUnqualifiedType(); |
| 1018 | } else { |
| 1019 | // C++0x [temp.deduct.call]p4 bullet 1: |
| 1020 | // - If the original P is a reference type, the deduced A (i.e., the type |
| 1021 | // referred to by the reference) can be more cv-qualified than the |
| 1022 | // transformed A. |
| 1023 | if (TDF & TDF_ParamWithReferenceType) { |
| 1024 | Qualifiers Quals; |
| 1025 | QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals); |
| 1026 | Quals.setCVRQualifiers(Quals.getCVRQualifiers() & |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 1027 | Arg.getCVRQualifiers()); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1028 | Param = S.Context.getQualifiedType(UnqualParam, Quals); |
| 1029 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1030 | |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1031 | if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) { |
| 1032 | // C++0x [temp.deduct.type]p10: |
| 1033 | // If P and A are function types that originated from deduction when |
| 1034 | // taking the address of a function template (14.8.2.2) or when deducing |
| 1035 | // template arguments from a function declaration (14.8.2.6) and Pi and |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1036 | // Ai are parameters of the top-level parameter-type-list of P and A, |
| 1037 | // respectively, Pi is adjusted if it is an rvalue reference to a |
| 1038 | // cv-unqualified template parameter and Ai is an lvalue reference, in |
| 1039 | // which case the type of Pi is changed to be the template parameter |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1040 | // type (i.e., T&& is changed to simply T). [ Note: As a result, when |
| 1041 | // Pi is T&& and Ai is X&, the adjusted Pi will be T, causing T to be |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1042 | // deduced as X&. - end note ] |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1043 | TDF &= ~TDF_TopLevelParameterTypeList; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1044 | |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1045 | if (const RValueReferenceType *ParamRef |
| 1046 | = Param->getAs<RValueReferenceType>()) { |
| 1047 | if (isa<TemplateTypeParmType>(ParamRef->getPointeeType()) && |
| 1048 | !ParamRef->getPointeeType().getQualifiers()) |
| 1049 | if (Arg->isLValueReferenceType()) |
| 1050 | Param = ParamRef->getPointeeType(); |
| 1051 | } |
| 1052 | } |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1053 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1054 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1055 | // C++ [temp.deduct.type]p9: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1056 | // A template type argument T, a template template argument TT or a |
| 1057 | // template non-type argument i can be deduced if P and A have one of |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1058 | // the following forms: |
| 1059 | // |
| 1060 | // T |
| 1061 | // cv-list T |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1062 | if (const TemplateTypeParmType *TemplateTypeParm |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1063 | = Param->getAs<TemplateTypeParmType>()) { |
Douglas Gregor | 4ea5dec | 2011-09-22 15:57:07 +0000 | [diff] [blame] | 1064 | // Just skip any attempts to deduce from a placeholder type. |
| 1065 | if (Arg->isPlaceholderType()) |
| 1066 | return Sema::TDK_Success; |
| 1067 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1068 | unsigned Index = TemplateTypeParm->getIndex(); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1069 | bool RecanonicalizeArg = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1070 | |
Douglas Gregor | 6045482 | 2009-07-22 20:02:25 +0000 | [diff] [blame] | 1071 | // If the argument type is an array type, move the qualifiers up to the |
| 1072 | // top level, so they can be matched with the qualifiers on the parameter. |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1073 | if (isa<ArrayType>(Arg)) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1074 | Qualifiers Quals; |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1075 | Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1076 | if (Quals) { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1077 | Arg = S.Context.getQualifiedType(Arg, Quals); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1078 | RecanonicalizeArg = true; |
| 1079 | } |
| 1080 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1081 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1082 | // The argument type can not be less qualified than the parameter |
| 1083 | // type. |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1084 | if (!(TDF & TDF_IgnoreQualifiers) && |
| 1085 | hasInconsistentOrSupersetQualifiersOf(Param, Arg)) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1086 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1087 | Info.FirstArg = TemplateArgument(Param); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1088 | Info.SecondArg = TemplateArgument(Arg); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1089 | return Sema::TDK_Underqualified; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1090 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1091 | |
| 1092 | assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0"); |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1093 | assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function"); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1094 | QualType DeducedType = Arg; |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1095 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1096 | // Remove any qualifiers on the parameter from the deduced type. |
| 1097 | // We checked the qualifiers for consistency above. |
| 1098 | Qualifiers DeducedQs = DeducedType.getQualifiers(); |
| 1099 | Qualifiers ParamQs = Param.getQualifiers(); |
| 1100 | DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers()); |
| 1101 | if (ParamQs.hasObjCGCAttr()) |
| 1102 | DeducedQs.removeObjCGCAttr(); |
| 1103 | if (ParamQs.hasAddressSpace()) |
| 1104 | DeducedQs.removeAddressSpace(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1105 | if (ParamQs.hasObjCLifetime()) |
| 1106 | DeducedQs.removeObjCLifetime(); |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1107 | |
| 1108 | // Objective-C ARC: |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1109 | // If template deduction would produce a lifetime qualifier on a type |
| 1110 | // that is not a lifetime type, template argument deduction fails. |
| 1111 | if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType() && |
| 1112 | !DeducedType->isDependentType()) { |
| 1113 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1114 | Info.FirstArg = TemplateArgument(Param); |
| 1115 | Info.SecondArg = TemplateArgument(Arg); |
| 1116 | return Sema::TDK_Underqualified; |
| 1117 | } |
| 1118 | |
| 1119 | // Objective-C ARC: |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1120 | // If template deduction would produce an argument type with lifetime type |
| 1121 | // but no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1122 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1123 | DeducedType->isObjCLifetimeType() && |
| 1124 | !DeducedQs.hasObjCLifetime()) |
| 1125 | DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 1126 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1127 | DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(), |
| 1128 | DeducedQs); |
| 1129 | |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1130 | if (RecanonicalizeArg) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1131 | DeducedType = S.Context.getCanonicalType(DeducedType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1132 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1133 | DeducedTemplateArgument NewDeduced(DeducedType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1134 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1135 | Deduced[Index], |
| 1136 | NewDeduced); |
| 1137 | if (Result.isNull()) { |
| 1138 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1139 | Info.FirstArg = Deduced[Index]; |
| 1140 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1141 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1142 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1143 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1144 | Deduced[Index] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1145 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1148 | // Set up the template argument deduction information for a failure. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1149 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1150 | Info.SecondArg = TemplateArgument(ArgIn); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1151 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 1152 | // If the parameter is an already-substituted template parameter |
| 1153 | // pack, do nothing: we don't know which of its arguments to look |
| 1154 | // at, so we have to wait until all of the parameter packs in this |
| 1155 | // expansion have arguments. |
| 1156 | if (isa<SubstTemplateTypeParmPackType>(Param)) |
| 1157 | return Sema::TDK_Success; |
| 1158 | |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1159 | // Check the cv-qualifiers on the parameter and argument types. |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1160 | CanQualType CanParam = S.Context.getCanonicalType(Param); |
| 1161 | CanQualType CanArg = S.Context.getCanonicalType(Arg); |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1162 | if (!(TDF & TDF_IgnoreQualifiers)) { |
| 1163 | if (TDF & TDF_ParamWithReferenceType) { |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1164 | if (hasInconsistentOrSupersetQualifiersOf(Param, Arg)) |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1165 | return Sema::TDK_NonDeducedMismatch; |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 1166 | } else if (!IsPossiblyOpaquelyQualifiedType(Param)) { |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1167 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1168 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1169 | } |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1170 | |
| 1171 | // If the parameter type is not dependent, there is nothing to deduce. |
| 1172 | if (!Param->isDependentType()) { |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1173 | if (!(TDF & TDF_SkipNonDependent)) { |
| 1174 | bool NonDeduced = (TDF & TDF_InOverloadResolution)? |
| 1175 | !S.isSameOrCompatibleFunctionType(CanParam, CanArg) : |
| 1176 | Param != Arg; |
| 1177 | if (NonDeduced) { |
| 1178 | return Sema::TDK_NonDeducedMismatch; |
| 1179 | } |
| 1180 | } |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1181 | return Sema::TDK_Success; |
| 1182 | } |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1183 | } else if (!Param->isDependentType()) { |
| 1184 | CanQualType ParamUnqualType = CanParam.getUnqualifiedType(), |
| 1185 | ArgUnqualType = CanArg.getUnqualifiedType(); |
| 1186 | bool Success = (TDF & TDF_InOverloadResolution)? |
| 1187 | S.isSameOrCompatibleFunctionType(ParamUnqualType, |
| 1188 | ArgUnqualType) : |
| 1189 | ParamUnqualType == ArgUnqualType; |
| 1190 | if (Success) |
| 1191 | return Sema::TDK_Success; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1192 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1193 | |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1194 | switch (Param->getTypeClass()) { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1195 | // Non-canonical types cannot appear here. |
| 1196 | #define NON_CANONICAL_TYPE(Class, Base) \ |
| 1197 | case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class); |
| 1198 | #define TYPE(Class, Base) |
| 1199 | #include "clang/AST/TypeNodes.def" |
| 1200 | |
| 1201 | case Type::TemplateTypeParm: |
| 1202 | case Type::SubstTemplateTypeParmPack: |
| 1203 | llvm_unreachable("Type nodes handled above"); |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1204 | |
| 1205 | // These types cannot be dependent, so simply check whether the types are |
| 1206 | // the same. |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1207 | case Type::Builtin: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1208 | case Type::VariableArray: |
| 1209 | case Type::Vector: |
| 1210 | case Type::FunctionNoProto: |
| 1211 | case Type::Record: |
| 1212 | case Type::Enum: |
| 1213 | case Type::ObjCObject: |
| 1214 | case Type::ObjCInterface: |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1215 | case Type::ObjCObjectPointer: { |
| 1216 | if (TDF & TDF_SkipNonDependent) |
| 1217 | return Sema::TDK_Success; |
| 1218 | |
| 1219 | if (TDF & TDF_IgnoreQualifiers) { |
| 1220 | Param = Param.getUnqualifiedType(); |
| 1221 | Arg = Arg.getUnqualifiedType(); |
| 1222 | } |
| 1223 | |
| 1224 | return Param == Arg? Sema::TDK_Success : Sema::TDK_NonDeducedMismatch; |
| 1225 | } |
| 1226 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1227 | // _Complex T [placeholder extension] |
| 1228 | case Type::Complex: |
| 1229 | if (const ComplexType *ComplexArg = Arg->getAs<ComplexType>()) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1230 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1231 | cast<ComplexType>(Param)->getElementType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1232 | ComplexArg->getElementType(), |
| 1233 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1234 | |
| 1235 | return Sema::TDK_NonDeducedMismatch; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1236 | |
| 1237 | // _Atomic T [extension] |
| 1238 | case Type::Atomic: |
| 1239 | if (const AtomicType *AtomicArg = Arg->getAs<AtomicType>()) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1240 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1241 | cast<AtomicType>(Param)->getValueType(), |
| 1242 | AtomicArg->getValueType(), |
| 1243 | Info, Deduced, TDF); |
| 1244 | |
| 1245 | return Sema::TDK_NonDeducedMismatch; |
| 1246 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1247 | // T * |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1248 | case Type::Pointer: { |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1249 | QualType PointeeType; |
| 1250 | if (const PointerType *PointerArg = Arg->getAs<PointerType>()) { |
| 1251 | PointeeType = PointerArg->getPointeeType(); |
| 1252 | } else if (const ObjCObjectPointerType *PointerArg |
| 1253 | = Arg->getAs<ObjCObjectPointerType>()) { |
| 1254 | PointeeType = PointerArg->getPointeeType(); |
| 1255 | } else { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1256 | return Sema::TDK_NonDeducedMismatch; |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1257 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1258 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1259 | unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1260 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1261 | cast<PointerType>(Param)->getPointeeType(), |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1262 | PointeeType, |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1263 | Info, Deduced, SubTDF); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1264 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1266 | // T & |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1267 | case Type::LValueReference: { |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1268 | const LValueReferenceType *ReferenceArg = |
| 1269 | Arg->getAs<LValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1270 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1271 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1272 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1273 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1274 | cast<LValueReferenceType>(Param)->getPointeeType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1275 | ReferenceArg->getPointeeType(), Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1276 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1277 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1278 | // T && [C++0x] |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1279 | case Type::RValueReference: { |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1280 | const RValueReferenceType *ReferenceArg = |
| 1281 | Arg->getAs<RValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1282 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1283 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1284 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1285 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1286 | cast<RValueReferenceType>(Param)->getPointeeType(), |
| 1287 | ReferenceArg->getPointeeType(), |
| 1288 | Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1289 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1290 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1291 | // T [] (implied, but not stated explicitly) |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1292 | case Type::IncompleteArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1293 | const IncompleteArrayType *IncompleteArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1294 | S.Context.getAsIncompleteArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1295 | if (!IncompleteArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1296 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1297 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1298 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1299 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1300 | S.Context.getAsIncompleteArrayType(Param)->getElementType(), |
| 1301 | IncompleteArrayArg->getElementType(), |
| 1302 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1303 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1304 | |
| 1305 | // T [integer-constant] |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1306 | case Type::ConstantArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1307 | const ConstantArrayType *ConstantArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1308 | S.Context.getAsConstantArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1309 | if (!ConstantArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1310 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1311 | |
| 1312 | const ConstantArrayType *ConstantArrayParm = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1313 | S.Context.getAsConstantArrayType(Param); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1314 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1315 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1316 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1317 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1318 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1319 | ConstantArrayParm->getElementType(), |
| 1320 | ConstantArrayArg->getElementType(), |
| 1321 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1324 | // type [i] |
| 1325 | case Type::DependentSizedArray: { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1326 | const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1327 | if (!ArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1328 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1329 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1330 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
| 1331 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1332 | // Check the element type of the arrays |
| 1333 | const DependentSizedArrayType *DependentArrayParm |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1334 | = S.Context.getAsDependentSizedArrayType(Param); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1335 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1336 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1337 | DependentArrayParm->getElementType(), |
| 1338 | ArrayArg->getElementType(), |
| 1339 | Info, Deduced, SubTDF)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1340 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1341 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1342 | // Determine the array bound is something we can deduce. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1343 | NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1344 | = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr()); |
| 1345 | if (!NTTP) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1346 | return Sema::TDK_Success; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1347 | |
| 1348 | // We can perform template argument deduction for the given non-type |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1349 | // template parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1351 | "Cannot deduce non-type template argument at depth > 0"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1352 | if (const ConstantArrayType *ConstantArrayArg |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1353 | = dyn_cast<ConstantArrayType>(ArrayArg)) { |
| 1354 | llvm::APSInt Size(ConstantArrayArg->getSize()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1355 | return DeduceNonTypeTemplateArgument(S, NTTP, Size, |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1356 | S.Context.getSizeType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1357 | /*ArrayBound=*/true, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1358 | Info, Deduced); |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1359 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1360 | if (const DependentSizedArrayType *DependentArrayArg |
| 1361 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1362 | if (DependentArrayArg->getSizeExpr()) |
| 1363 | return DeduceNonTypeTemplateArgument(S, NTTP, |
| 1364 | DependentArrayArg->getSizeExpr(), |
| 1365 | Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1366 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1367 | // Incomplete type does not match a dependently-sized array type |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1368 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1369 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1370 | |
| 1371 | // type(*)(T) |
| 1372 | // T(*)() |
| 1373 | // T(*)(T) |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1374 | case Type::FunctionProto: { |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1375 | unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1376 | const FunctionProtoType *FunctionProtoArg = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1377 | dyn_cast<FunctionProtoType>(Arg); |
| 1378 | if (!FunctionProtoArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1379 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1380 | |
| 1381 | const FunctionProtoType *FunctionProtoParam = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1382 | cast<FunctionProtoType>(Param); |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1383 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1384 | if (FunctionProtoParam->getTypeQuals() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1385 | != FunctionProtoArg->getTypeQuals() || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1386 | FunctionProtoParam->getRefQualifier() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1387 | != FunctionProtoArg->getRefQualifier() || |
| 1388 | FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1389 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1390 | |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1391 | // Check return types. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1392 | if (Sema::TemplateDeductionResult Result = |
| 1393 | DeduceTemplateArgumentsByTypeMatch( |
| 1394 | S, TemplateParams, FunctionProtoParam->getReturnType(), |
| 1395 | FunctionProtoArg->getReturnType(), Info, Deduced, 0)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1396 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1397 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1398 | return DeduceTemplateArguments( |
| 1399 | S, TemplateParams, FunctionProtoParam->param_type_begin(), |
| 1400 | FunctionProtoParam->getNumParams(), |
| 1401 | FunctionProtoArg->param_type_begin(), |
| 1402 | FunctionProtoArg->getNumParams(), Info, Deduced, SubTDF); |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1403 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1405 | case Type::InjectedClassName: { |
| 1406 | // Treat a template's injected-class-name as if the template |
| 1407 | // specialization type had been used. |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1408 | Param = cast<InjectedClassNameType>(Param) |
| 1409 | ->getInjectedSpecializationType(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1410 | assert(isa<TemplateSpecializationType>(Param) && |
| 1411 | "injected class name is not a template specialization type"); |
| 1412 | // fall through |
| 1413 | } |
| 1414 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1415 | // template-name<T> (where template-name refers to a class template) |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1416 | // template-name<i> |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1417 | // TT<T> |
| 1418 | // TT<i> |
| 1419 | // TT<> |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1420 | case Type::TemplateSpecialization: { |
| 1421 | const TemplateSpecializationType *SpecParam |
| 1422 | = cast<TemplateSpecializationType>(Param); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1423 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1424 | // Try to deduce template arguments from the template-id. |
| 1425 | Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1426 | = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1427 | Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | |
Douglas Gregor | 4290975 | 2009-09-30 22:13:51 +0000 | [diff] [blame] | 1429 | if (Result && (TDF & TDF_DerivedClass)) { |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1430 | // C++ [temp.deduct.call]p3b3: |
| 1431 | // If P is a class, and P has the form template-id, then A can be a |
| 1432 | // derived class of the deduced A. Likewise, if P is a pointer to a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | // class of the form template-id, A can be a pointer to a derived |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1434 | // class pointed to by the deduced A. |
| 1435 | // |
| 1436 | // More importantly: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1437 | // These alternatives are considered only if type deduction would |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1438 | // otherwise fail. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1439 | if (const RecordType *RecordT = Arg->getAs<RecordType>()) { |
| 1440 | // We cannot inspect base classes as part of deduction when the type |
| 1441 | // is incomplete, so either instantiate any templates necessary to |
| 1442 | // complete the type, or skip over it if it cannot be completed. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 1443 | if (!S.isCompleteType(Info.getLocation(), Arg)) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1444 | return Result; |
| 1445 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1446 | // Use data recursion to crawl through the list of base classes. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1447 | // Visited contains the set of nodes we have already visited, while |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1448 | // ToVisit is our stack of records that we still need to visit. |
| 1449 | llvm::SmallPtrSet<const RecordType *, 8> Visited; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1450 | SmallVector<const RecordType *, 8> ToVisit; |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1451 | ToVisit.push_back(RecordT); |
| 1452 | bool Successful = false; |
Benjamin Kramer | 4d08ccb | 2012-01-20 16:39:18 +0000 | [diff] [blame] | 1453 | SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(), |
| 1454 | Deduced.end()); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1455 | while (!ToVisit.empty()) { |
| 1456 | // Retrieve the next class in the inheritance hierarchy. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 1457 | const RecordType *NextT = ToVisit.pop_back_val(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1459 | // If we have already seen this type, skip it. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 1460 | if (!Visited.insert(NextT).second) |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1461 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1462 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1463 | // If this is a base class, try to perform template argument |
| 1464 | // deduction from it. |
| 1465 | if (NextT != RecordT) { |
Richard Trieu | 23bafad | 2012-11-07 21:17:13 +0000 | [diff] [blame] | 1466 | TemplateDeductionInfo BaseInfo(Info.getLocation()); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1467 | Sema::TemplateDeductionResult BaseResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1468 | = DeduceTemplateArguments(S, TemplateParams, SpecParam, |
Richard Trieu | 23bafad | 2012-11-07 21:17:13 +0000 | [diff] [blame] | 1469 | QualType(NextT, 0), BaseInfo, |
| 1470 | Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1472 | // If template argument deduction for this base was successful, |
Douglas Gregor | e0f7a8a | 2010-11-02 00:02:34 +0000 | [diff] [blame] | 1473 | // note that we had some success. Otherwise, ignore any deductions |
| 1474 | // from this base class. |
| 1475 | if (BaseResult == Sema::TDK_Success) { |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1476 | Successful = true; |
Benjamin Kramer | 4d08ccb | 2012-01-20 16:39:18 +0000 | [diff] [blame] | 1477 | DeducedOrig.clear(); |
| 1478 | DeducedOrig.append(Deduced.begin(), Deduced.end()); |
Richard Trieu | 23bafad | 2012-11-07 21:17:13 +0000 | [diff] [blame] | 1479 | Info.Param = BaseInfo.Param; |
| 1480 | Info.FirstArg = BaseInfo.FirstArg; |
| 1481 | Info.SecondArg = BaseInfo.SecondArg; |
Douglas Gregor | e0f7a8a | 2010-11-02 00:02:34 +0000 | [diff] [blame] | 1482 | } |
| 1483 | else |
| 1484 | Deduced = DeducedOrig; |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1485 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1486 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1487 | // Visit base classes |
| 1488 | CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1489 | for (const auto &Base : Next->bases()) { |
| 1490 | assert(Base.getType()->isRecordType() && |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1491 | "Base class that isn't a record?"); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1492 | ToVisit.push_back(Base.getType()->getAs<RecordType>()); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1493 | } |
| 1494 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1495 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1496 | if (Successful) |
| 1497 | return Sema::TDK_Success; |
| 1498 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1499 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1500 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1502 | return Result; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1505 | // T type::* |
| 1506 | // T T::* |
| 1507 | // T (type::*)() |
| 1508 | // type (T::*)() |
| 1509 | // type (type::*)(T) |
| 1510 | // type (T::*)(T) |
| 1511 | // T (type::*)(T) |
| 1512 | // T (T::*)() |
| 1513 | // T (T::*)(T) |
| 1514 | case Type::MemberPointer: { |
| 1515 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 1516 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 1517 | if (!MemPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1518 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1519 | |
David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1520 | QualType ParamPointeeType = MemPtrParam->getPointeeType(); |
| 1521 | if (ParamPointeeType->isFunctionType()) |
| 1522 | S.adjustMemberFunctionCC(ParamPointeeType, /*IsStatic=*/true, |
| 1523 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1524 | QualType ArgPointeeType = MemPtrArg->getPointeeType(); |
| 1525 | if (ArgPointeeType->isFunctionType()) |
| 1526 | S.adjustMemberFunctionCC(ArgPointeeType, /*IsStatic=*/true, |
| 1527 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1528 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1529 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1530 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1531 | ParamPointeeType, |
| 1532 | ArgPointeeType, |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1533 | Info, Deduced, |
| 1534 | TDF & TDF_IgnoreQualifiers)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1535 | return Result; |
| 1536 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1537 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1538 | QualType(MemPtrParam->getClass(), 0), |
| 1539 | QualType(MemPtrArg->getClass(), 0), |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1540 | Info, Deduced, |
| 1541 | TDF & TDF_IgnoreQualifiers); |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Anders Carlsson | 15f1dd1 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1544 | // (clang extension) |
| 1545 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1546 | // type(^)(T) |
| 1547 | // T(^)() |
| 1548 | // T(^)(T) |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1549 | case Type::BlockPointer: { |
| 1550 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 1551 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1553 | if (!BlockPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1554 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1556 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1557 | BlockPtrParam->getPointeeType(), |
| 1558 | BlockPtrArg->getPointeeType(), |
| 1559 | Info, Deduced, 0); |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1562 | // (clang extension) |
| 1563 | // |
| 1564 | // T __attribute__(((ext_vector_type(<integral constant>)))) |
| 1565 | case Type::ExtVector: { |
| 1566 | const ExtVectorType *VectorParam = cast<ExtVectorType>(Param); |
| 1567 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1568 | // Make sure that the vectors have the same number of elements. |
| 1569 | if (VectorParam->getNumElements() != VectorArg->getNumElements()) |
| 1570 | return Sema::TDK_NonDeducedMismatch; |
| 1571 | |
| 1572 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1573 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1574 | VectorParam->getElementType(), |
| 1575 | VectorArg->getElementType(), |
| 1576 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | if (const DependentSizedExtVectorType *VectorArg |
| 1580 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1581 | // We can't check the number of elements, since the argument has a |
| 1582 | // dependent number of elements. This can only occur during partial |
| 1583 | // ordering. |
| 1584 | |
| 1585 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1586 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1587 | VectorParam->getElementType(), |
| 1588 | VectorArg->getElementType(), |
| 1589 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | return Sema::TDK_NonDeducedMismatch; |
| 1593 | } |
| 1594 | |
| 1595 | // (clang extension) |
| 1596 | // |
| 1597 | // T __attribute__(((ext_vector_type(N)))) |
| 1598 | case Type::DependentSizedExtVector: { |
| 1599 | const DependentSizedExtVectorType *VectorParam |
| 1600 | = cast<DependentSizedExtVectorType>(Param); |
| 1601 | |
| 1602 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1603 | // Perform deduction on the element types. |
| 1604 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1605 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1606 | VectorParam->getElementType(), |
| 1607 | VectorArg->getElementType(), |
| 1608 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1609 | return Result; |
| 1610 | |
| 1611 | // Perform deduction on the vector size, if we can. |
| 1612 | NonTypeTemplateParmDecl *NTTP |
| 1613 | = getDeducedParameterFromExpr(VectorParam->getSizeExpr()); |
| 1614 | if (!NTTP) |
| 1615 | return Sema::TDK_Success; |
| 1616 | |
| 1617 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
| 1618 | ArgSize = VectorArg->getNumElements(); |
| 1619 | return DeduceNonTypeTemplateArgument(S, NTTP, ArgSize, S.Context.IntTy, |
| 1620 | false, Info, Deduced); |
| 1621 | } |
| 1622 | |
| 1623 | if (const DependentSizedExtVectorType *VectorArg |
| 1624 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1625 | // Perform deduction on the element types. |
| 1626 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1627 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1628 | VectorParam->getElementType(), |
| 1629 | VectorArg->getElementType(), |
| 1630 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1631 | return Result; |
| 1632 | |
| 1633 | // Perform deduction on the vector size, if we can. |
| 1634 | NonTypeTemplateParmDecl *NTTP |
| 1635 | = getDeducedParameterFromExpr(VectorParam->getSizeExpr()); |
| 1636 | if (!NTTP) |
| 1637 | return Sema::TDK_Success; |
| 1638 | |
| 1639 | return DeduceNonTypeTemplateArgument(S, NTTP, VectorArg->getSizeExpr(), |
| 1640 | Info, Deduced); |
| 1641 | } |
| 1642 | |
| 1643 | return Sema::TDK_NonDeducedMismatch; |
| 1644 | } |
| 1645 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1646 | case Type::TypeOfExpr: |
| 1647 | case Type::TypeOf: |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1648 | case Type::DependentName: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1649 | case Type::UnresolvedUsing: |
| 1650 | case Type::Decltype: |
| 1651 | case Type::UnaryTransform: |
| 1652 | case Type::Auto: |
| 1653 | case Type::DependentTemplateSpecialization: |
| 1654 | case Type::PackExpansion: |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1655 | // No template argument deduction for these types |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1656 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1659 | llvm_unreachable("Invalid Type Class!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1662 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1663 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1664 | TemplateParameterList *TemplateParams, |
| 1665 | const TemplateArgument &Param, |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1666 | TemplateArgument Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1667 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1668 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1669 | // If the template argument is a pack expansion, perform template argument |
| 1670 | // deduction against the pattern of that expansion. This only occurs during |
| 1671 | // partial ordering. |
| 1672 | if (Arg.isPackExpansion()) |
| 1673 | Arg = Arg.getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1674 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1675 | switch (Param.getKind()) { |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1676 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1677 | llvm_unreachable("Null template argument in parameter list"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | |
| 1679 | case TemplateArgument::Type: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1680 | if (Arg.getKind() == TemplateArgument::Type) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1681 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1682 | Param.getAsType(), |
| 1683 | Arg.getAsType(), |
| 1684 | Info, Deduced, 0); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1685 | Info.FirstArg = Param; |
| 1686 | Info.SecondArg = Arg; |
| 1687 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1688 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1689 | case TemplateArgument::Template: |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1690 | if (Arg.getKind() == TemplateArgument::Template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1691 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1692 | Param.getAsTemplate(), |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1693 | Arg.getAsTemplate(), Info, Deduced); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1694 | Info.FirstArg = Param; |
| 1695 | Info.SecondArg = Arg; |
| 1696 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1697 | |
| 1698 | case TemplateArgument::TemplateExpansion: |
| 1699 | llvm_unreachable("caller should handle pack expansions"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1700 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1701 | case TemplateArgument::Declaration: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1702 | if (Arg.getKind() == TemplateArgument::Declaration && |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 1703 | isSameDeclaration(Param.getAsDecl(), Arg.getAsDecl())) |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 1704 | return Sema::TDK_Success; |
| 1705 | |
| 1706 | Info.FirstArg = Param; |
| 1707 | Info.SecondArg = Arg; |
| 1708 | return Sema::TDK_NonDeducedMismatch; |
| 1709 | |
| 1710 | case TemplateArgument::NullPtr: |
| 1711 | if (Arg.getKind() == TemplateArgument::NullPtr && |
| 1712 | S.Context.hasSameType(Param.getNullPtrType(), Arg.getNullPtrType())) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1713 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1714 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1715 | Info.FirstArg = Param; |
| 1716 | Info.SecondArg = Arg; |
| 1717 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1718 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1719 | case TemplateArgument::Integral: |
| 1720 | if (Arg.getKind() == TemplateArgument::Integral) { |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1721 | if (hasSameExtendedValue(Param.getAsIntegral(), Arg.getAsIntegral())) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1722 | return Sema::TDK_Success; |
| 1723 | |
| 1724 | Info.FirstArg = Param; |
| 1725 | Info.SecondArg = Arg; |
| 1726 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1727 | } |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1728 | |
| 1729 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 1730 | Info.FirstArg = Param; |
| 1731 | Info.SecondArg = Arg; |
| 1732 | return Sema::TDK_NonDeducedMismatch; |
| 1733 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1734 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1735 | Info.FirstArg = Param; |
| 1736 | Info.SecondArg = Arg; |
| 1737 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1738 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1739 | case TemplateArgument::Expression: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1740 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1741 | = getDeducedParameterFromExpr(Param.getAsExpr())) { |
| 1742 | if (Arg.getKind() == TemplateArgument::Integral) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1743 | return DeduceNonTypeTemplateArgument(S, NTTP, |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1744 | Arg.getAsIntegral(), |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1745 | Arg.getIntegralType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1746 | /*ArrayBound=*/false, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1747 | Info, Deduced); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1748 | if (Arg.getKind() == TemplateArgument::Expression) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1749 | return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(), |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1750 | Info, Deduced); |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1751 | if (Arg.getKind() == TemplateArgument::Declaration) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1752 | return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(), |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1753 | Info, Deduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1754 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1755 | Info.FirstArg = Param; |
| 1756 | Info.SecondArg = Arg; |
| 1757 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1758 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1759 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1760 | // Can't deduce anything, but that's okay. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1761 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1762 | } |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 1763 | case TemplateArgument::Pack: |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1764 | llvm_unreachable("Argument packs should be expanded by the caller!"); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1765 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1766 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1767 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1770 | /// \brief Determine whether there is a template argument to be used for |
| 1771 | /// deduction. |
| 1772 | /// |
| 1773 | /// This routine "expands" argument packs in-place, overriding its input |
| 1774 | /// parameters so that \c Args[ArgIdx] will be the available template argument. |
| 1775 | /// |
| 1776 | /// \returns true if there is another template argument (which will be at |
| 1777 | /// \c Args[ArgIdx]), false otherwise. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1778 | static bool hasTemplateArgumentForDeduction(const TemplateArgument *&Args, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1779 | unsigned &ArgIdx, |
| 1780 | unsigned &NumArgs) { |
| 1781 | if (ArgIdx == NumArgs) |
| 1782 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1783 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1784 | const TemplateArgument &Arg = Args[ArgIdx]; |
| 1785 | if (Arg.getKind() != TemplateArgument::Pack) |
| 1786 | return true; |
| 1787 | |
| 1788 | assert(ArgIdx == NumArgs - 1 && "Pack not at the end of argument list?"); |
| 1789 | Args = Arg.pack_begin(); |
| 1790 | NumArgs = Arg.pack_size(); |
| 1791 | ArgIdx = 0; |
| 1792 | return ArgIdx < NumArgs; |
| 1793 | } |
| 1794 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1795 | /// \brief Determine whether the given set of template arguments has a pack |
| 1796 | /// expansion that is not the last template argument. |
| 1797 | static bool hasPackExpansionBeforeEnd(const TemplateArgument *Args, |
| 1798 | unsigned NumArgs) { |
| 1799 | unsigned ArgIdx = 0; |
| 1800 | while (ArgIdx < NumArgs) { |
| 1801 | const TemplateArgument &Arg = Args[ArgIdx]; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1802 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1803 | // Unwrap argument packs. |
| 1804 | if (Args[ArgIdx].getKind() == TemplateArgument::Pack) { |
| 1805 | Args = Arg.pack_begin(); |
| 1806 | NumArgs = Arg.pack_size(); |
| 1807 | ArgIdx = 0; |
| 1808 | continue; |
| 1809 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1810 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1811 | ++ArgIdx; |
| 1812 | if (ArgIdx == NumArgs) |
| 1813 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1814 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1815 | if (Arg.isPackExpansion()) |
| 1816 | return true; |
| 1817 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1818 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1819 | return false; |
| 1820 | } |
| 1821 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1822 | static Sema::TemplateDeductionResult |
| 1823 | DeduceTemplateArguments(Sema &S, |
| 1824 | TemplateParameterList *TemplateParams, |
| 1825 | const TemplateArgument *Params, unsigned NumParams, |
| 1826 | const TemplateArgument *Args, unsigned NumArgs, |
| 1827 | TemplateDeductionInfo &Info, |
Richard Smith | 16b6539 | 2012-12-06 06:44:44 +0000 | [diff] [blame] | 1828 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1829 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1830 | // If the template argument list of P contains a pack expansion that is not |
| 1831 | // the last template argument, the entire template argument list is a |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1832 | // non-deduced context. |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1833 | if (hasPackExpansionBeforeEnd(Params, NumParams)) |
| 1834 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1835 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1836 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1837 | // If P has a form that contains <T> or <i>, then each argument Pi of the |
| 1838 | // respective template argument list P is compared with the corresponding |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1839 | // argument Ai of the corresponding template argument list of A. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1840 | unsigned ArgIdx = 0, ParamIdx = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1841 | for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1842 | ++ParamIdx) { |
| 1843 | if (!Params[ParamIdx].isPackExpansion()) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1844 | // The simple case: deduce template arguments by matching Pi and Ai. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1845 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1846 | // Check whether we have enough arguments. |
| 1847 | if (!hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) |
Richard Smith | 16b6539 | 2012-12-06 06:44:44 +0000 | [diff] [blame] | 1848 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1849 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1850 | if (Args[ArgIdx].isPackExpansion()) { |
| 1851 | // FIXME: We follow the logic of C++0x [temp.deduct.type]p22 here, |
| 1852 | // but applied to pack expansions that are template arguments. |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 1853 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1854 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1855 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1856 | // Perform deduction for this Pi/Ai pair. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1857 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1858 | = DeduceTemplateArguments(S, TemplateParams, |
| 1859 | Params[ParamIdx], Args[ArgIdx], |
| 1860 | Info, Deduced)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1861 | return Result; |
| 1862 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1863 | // Move to the next argument. |
| 1864 | ++ArgIdx; |
| 1865 | continue; |
| 1866 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1867 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1868 | // The parameter is a pack expansion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1869 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1870 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1871 | // If Pi is a pack expansion, then the pattern of Pi is compared with |
| 1872 | // each remaining argument in the template argument list of A. Each |
| 1873 | // comparison deduces template arguments for subsequent positions in the |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1874 | // template parameter packs expanded by Pi. |
| 1875 | TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1876 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1877 | // FIXME: If there are no remaining arguments, we can bail out early |
| 1878 | // and set any deduced parameter packs to an empty argument pack. |
| 1879 | // The latter part of this is a (minor) correctness issue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1880 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1881 | // Prepare to deduce the packs within the pattern. |
| 1882 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1883 | |
| 1884 | // Keep track of the deduced template arguments for each parameter pack |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1885 | // expanded by this pack expansion (the outer index) and for each |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1886 | // template argument (the inner SmallVectors). |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1887 | bool HasAnyArguments = false; |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1888 | for (; hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs); ++ArgIdx) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1889 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1890 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1891 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1892 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1893 | = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx], |
| 1894 | Info, Deduced)) |
| 1895 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1896 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1897 | PackScope.nextPackElement(); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1898 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1899 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1900 | // Build argument packs for each of the parameter packs expanded by this |
| 1901 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1902 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1903 | return Result; |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1904 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1905 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1906 | return Sema::TDK_Success; |
| 1907 | } |
| 1908 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1909 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1910 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1911 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1912 | const TemplateArgumentList &ParamList, |
| 1913 | const TemplateArgumentList &ArgList, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1914 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1915 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1916 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1917 | ParamList.data(), ParamList.size(), |
| 1918 | ArgList.data(), ArgList.size(), |
| 1919 | Info, Deduced); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1922 | /// \brief Determine whether two template arguments are the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1923 | static bool isSameTemplateArg(ASTContext &Context, |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1924 | const TemplateArgument &X, |
| 1925 | const TemplateArgument &Y) { |
| 1926 | if (X.getKind() != Y.getKind()) |
| 1927 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1928 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1929 | switch (X.getKind()) { |
| 1930 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1931 | llvm_unreachable("Comparing NULL template argument"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1932 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1933 | case TemplateArgument::Type: |
| 1934 | return Context.getCanonicalType(X.getAsType()) == |
| 1935 | Context.getCanonicalType(Y.getAsType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1936 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1937 | case TemplateArgument::Declaration: |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 1938 | return isSameDeclaration(X.getAsDecl(), Y.getAsDecl()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 1939 | |
| 1940 | case TemplateArgument::NullPtr: |
| 1941 | return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1942 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1943 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1944 | case TemplateArgument::TemplateExpansion: |
| 1945 | return Context.getCanonicalTemplateName( |
| 1946 | X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == |
| 1947 | Context.getCanonicalTemplateName( |
| 1948 | Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1949 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1950 | case TemplateArgument::Integral: |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1951 | return X.getAsIntegral() == Y.getAsIntegral(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1952 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1953 | case TemplateArgument::Expression: { |
| 1954 | llvm::FoldingSetNodeID XID, YID; |
| 1955 | X.getAsExpr()->Profile(XID, Context, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1956 | Y.getAsExpr()->Profile(YID, Context, true); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1957 | return XID == YID; |
| 1958 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1959 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1960 | case TemplateArgument::Pack: |
| 1961 | if (X.pack_size() != Y.pack_size()) |
| 1962 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1963 | |
| 1964 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
| 1965 | XPEnd = X.pack_end(), |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1966 | YP = Y.pack_begin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1967 | XP != XPEnd; ++XP, ++YP) |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1968 | if (!isSameTemplateArg(Context, *XP, *YP)) |
| 1969 | return false; |
| 1970 | |
| 1971 | return true; |
| 1972 | } |
| 1973 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1974 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1977 | /// \brief Allocate a TemplateArgumentLoc where all locations have |
| 1978 | /// been initialized to the given location. |
| 1979 | /// |
| 1980 | /// \param S The semantic analysis object. |
| 1981 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 1982 | /// \param Arg The template argument we are producing template argument |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1983 | /// location information for. |
| 1984 | /// |
| 1985 | /// \param NTTPType For a declaration template argument, the type of |
| 1986 | /// the non-type template parameter that corresponds to this template |
| 1987 | /// argument. |
| 1988 | /// |
| 1989 | /// \param Loc The source location to use for the resulting template |
| 1990 | /// argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1991 | static TemplateArgumentLoc |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1992 | getTrivialTemplateArgumentLoc(Sema &S, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1993 | const TemplateArgument &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1994 | QualType NTTPType, |
| 1995 | SourceLocation Loc) { |
| 1996 | switch (Arg.getKind()) { |
| 1997 | case TemplateArgument::Null: |
| 1998 | llvm_unreachable("Can't get a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1999 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2000 | case TemplateArgument::Type: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2001 | return TemplateArgumentLoc(Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2002 | S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2003 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2004 | case TemplateArgument::Declaration: { |
| 2005 | Expr *E |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 2006 | = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2007 | .getAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2008 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2009 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2010 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2011 | case TemplateArgument::NullPtr: { |
| 2012 | Expr *E |
| 2013 | = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2014 | .getAs<Expr>(); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2015 | return TemplateArgumentLoc(TemplateArgument(NTTPType, /*isNullPtr*/true), |
| 2016 | E); |
| 2017 | } |
| 2018 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2019 | case TemplateArgument::Integral: { |
| 2020 | Expr *E |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2021 | = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).getAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2022 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2023 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2024 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2025 | case TemplateArgument::Template: |
| 2026 | case TemplateArgument::TemplateExpansion: { |
| 2027 | NestedNameSpecifierLocBuilder Builder; |
| 2028 | TemplateName Template = Arg.getAsTemplate(); |
| 2029 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) |
| 2030 | Builder.MakeTrivial(S.Context, DTN->getQualifier(), Loc); |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 2031 | else if (QualifiedTemplateName *QTN = |
| 2032 | Template.getAsQualifiedTemplateName()) |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2033 | Builder.MakeTrivial(S.Context, QTN->getQualifier(), Loc); |
| 2034 | |
| 2035 | if (Arg.getKind() == TemplateArgument::Template) |
| 2036 | return TemplateArgumentLoc(Arg, |
| 2037 | Builder.getWithLocInContext(S.Context), |
| 2038 | Loc); |
| 2039 | |
| 2040 | |
| 2041 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(S.Context), |
| 2042 | Loc, Loc); |
| 2043 | } |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2044 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2045 | case TemplateArgument::Expression: |
| 2046 | return TemplateArgumentLoc(Arg, Arg.getAsExpr()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2047 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2048 | case TemplateArgument::Pack: |
| 2049 | return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); |
| 2050 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2051 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2052 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | |
| 2056 | /// \brief Convert the given deduced template argument and add it to the set of |
| 2057 | /// fully-converted template arguments. |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2058 | static bool |
| 2059 | ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param, |
| 2060 | DeducedTemplateArgument Arg, |
| 2061 | NamedDecl *Template, |
| 2062 | QualType NTTPType, |
| 2063 | unsigned ArgumentPackIndex, |
| 2064 | TemplateDeductionInfo &Info, |
| 2065 | bool InFunctionTemplate, |
| 2066 | SmallVectorImpl<TemplateArgument> &Output) { |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2067 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 2068 | // This is a template argument pack, so check each of its arguments against |
| 2069 | // the template parameter. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2070 | SmallVector<TemplateArgument, 2> PackedArgsBuilder; |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2071 | for (const auto &P : Arg.pack_elements()) { |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2072 | // When converting the deduced template argument, append it to the |
| 2073 | // general output list. We need to do this so that the template argument |
| 2074 | // checking logic has all of the prior template arguments available. |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2075 | DeducedTemplateArgument InnerArg(P); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2076 | InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2077 | if (ConvertDeducedTemplateArgument(S, Param, InnerArg, Template, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2078 | NTTPType, PackedArgsBuilder.size(), |
| 2079 | Info, InFunctionTemplate, Output)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2080 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2081 | |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2082 | // Move the converted template argument into our argument pack. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 2083 | PackedArgsBuilder.push_back(Output.pop_back_val()); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2084 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2085 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2086 | // Create the resulting argument pack. |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 2087 | Output.push_back( |
| 2088 | TemplateArgument::CreatePackCopy(S.Context, PackedArgsBuilder)); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2089 | return false; |
| 2090 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2091 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2092 | // Convert the deduced template argument into a template |
| 2093 | // argument that we can check, almost as if the user had written |
| 2094 | // the template argument explicitly. |
| 2095 | TemplateArgumentLoc ArgLoc = getTrivialTemplateArgumentLoc(S, Arg, NTTPType, |
| 2096 | Info.getLocation()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2097 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2098 | // Check the template argument, converting it as necessary. |
| 2099 | return S.CheckTemplateArgument(Param, ArgLoc, |
| 2100 | Template, |
| 2101 | Template->getLocation(), |
| 2102 | Template->getSourceRange().getEnd(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2103 | ArgumentPackIndex, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2104 | Output, |
| 2105 | InFunctionTemplate |
| 2106 | ? (Arg.wasDeducedFromArrayBound() |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2107 | ? Sema::CTAK_DeducedFromArrayBound |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2108 | : Sema::CTAK_Deduced) |
| 2109 | : Sema::CTAK_Specified); |
| 2110 | } |
| 2111 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2112 | /// Complete template argument deduction for a class template partial |
| 2113 | /// specialization. |
| 2114 | static Sema::TemplateDeductionResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2115 | FinishTemplateArgumentDeduction(Sema &S, |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2116 | ClassTemplatePartialSpecializationDecl *Partial, |
| 2117 | const TemplateArgumentList &TemplateArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2118 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2119 | TemplateDeductionInfo &Info) { |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2120 | // Unevaluated SFINAE context. |
| 2121 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2122 | Sema::SFINAETrap Trap(S); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2123 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2124 | Sema::ContextRAII SavedContext(S, Partial); |
| 2125 | |
| 2126 | // C++ [temp.deduct.type]p2: |
| 2127 | // [...] or if any template argument remains neither deduced nor |
| 2128 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2129 | SmallVector<TemplateArgument, 4> Builder; |
Douglas Gregor | aef93f2 | 2011-01-04 22:23:38 +0000 | [diff] [blame] | 2130 | TemplateParameterList *PartialParams = Partial->getTemplateParameters(); |
| 2131 | for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) { |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2132 | NamedDecl *Param = PartialParams->getParam(I); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2133 | if (Deduced[I].isNull()) { |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2134 | Info.Param = makeTemplateParameter(Param); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2135 | return Sema::TDK_Incomplete; |
| 2136 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2137 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2138 | // We have deduced this argument, so it still needs to be |
| 2139 | // checked and converted. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2140 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2141 | // First, for a non-type template parameter type that is |
| 2142 | // initialized by a declaration, we need the type of the |
| 2143 | // corresponding non-type template parameter. |
| 2144 | QualType NTTPType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2145 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2146 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2147 | NTTPType = NTTP->getType(); |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2148 | if (NTTPType->isDependentType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2149 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2150 | Builder.data(), Builder.size()); |
| 2151 | NTTPType = S.SubstType(NTTPType, |
| 2152 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2153 | NTTP->getLocation(), |
| 2154 | NTTP->getDeclName()); |
| 2155 | if (NTTPType.isNull()) { |
| 2156 | Info.Param = makeTemplateParameter(Param); |
| 2157 | // FIXME: These template arguments are temporary. Free them! |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2158 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, |
| 2159 | Builder.data(), |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2160 | Builder.size())); |
| 2161 | return Sema::TDK_SubstitutionFailure; |
| 2162 | } |
| 2163 | } |
| 2164 | } |
| 2165 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2166 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2167 | Partial, NTTPType, 0, Info, false, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2168 | Builder)) { |
| 2169 | Info.Param = makeTemplateParameter(Param); |
| 2170 | // FIXME: These template arguments are temporary. Free them! |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2171 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(), |
| 2172 | Builder.size())); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2173 | return Sema::TDK_SubstitutionFailure; |
| 2174 | } |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2175 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2176 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2177 | // Form the template argument list from the deduced template arguments. |
| 2178 | TemplateArgumentList *DeducedArgumentList |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2179 | = TemplateArgumentList::CreateCopy(S.Context, Builder.data(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2180 | Builder.size()); |
| 2181 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2182 | Info.reset(DeducedArgumentList); |
| 2183 | |
| 2184 | // Substitute the deduced template arguments into the template |
| 2185 | // arguments of the class template partial specialization, and |
| 2186 | // verify that the instantiated template arguments are both valid |
| 2187 | // and are equivalent to the template arguments originally provided |
| 2188 | // to the class template. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2189 | LocalInstantiationScope InstScope(S); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2190 | ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate(); |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2191 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2192 | = Partial->getTemplateArgsAsWritten(); |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2193 | const TemplateArgumentLoc *PartialTemplateArgs |
| 2194 | = PartialTemplArgInfo->getTemplateArgs(); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2195 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2196 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
| 2197 | PartialTemplArgInfo->RAngleLoc); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2198 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2199 | if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2200 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 2201 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 2202 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 2203 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 2204 | |
| 2205 | Decl *Param |
| 2206 | = const_cast<NamedDecl *>( |
| 2207 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
| 2208 | Info.Param = makeTemplateParameter(Param); |
| 2209 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 2210 | return Sema::TDK_SubstitutionFailure; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2213 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2214 | if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(), |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2215 | InstArgs, false, ConvertedInstArgs)) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2216 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2217 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2218 | TemplateParameterList *TemplateParams |
| 2219 | = ClassTemplate->getTemplateParameters(); |
| 2220 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2221 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2222 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 2223 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2224 | Info.FirstArg = TemplateArgs[I]; |
| 2225 | Info.SecondArg = InstArg; |
| 2226 | return Sema::TDK_NonDeducedMismatch; |
| 2227 | } |
| 2228 | } |
| 2229 | |
| 2230 | if (Trap.hasErrorOccurred()) |
| 2231 | return Sema::TDK_SubstitutionFailure; |
| 2232 | |
| 2233 | return Sema::TDK_Success; |
| 2234 | } |
| 2235 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2236 | /// \brief Perform template argument deduction to determine whether |
Larisse Voufo | 833b05a | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 2237 | /// the given template arguments match the given class template |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2238 | /// partial specialization per C++ [temp.class.spec.match]. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2239 | Sema::TemplateDeductionResult |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2240 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2241 | const TemplateArgumentList &TemplateArgs, |
| 2242 | TemplateDeductionInfo &Info) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 2243 | if (Partial->isInvalidDecl()) |
| 2244 | return TDK_Invalid; |
| 2245 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2246 | // C++ [temp.class.spec.match]p2: |
| 2247 | // A partial specialization matches a given actual template |
| 2248 | // argument list if the template arguments of the partial |
| 2249 | // specialization can be deduced from the actual template argument |
| 2250 | // list (14.8.2). |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2251 | |
| 2252 | // Unevaluated SFINAE context. |
| 2253 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2254 | SFINAETrap Trap(*this); |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2255 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2256 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2257 | Deduced.resize(Partial->getTemplateParameters()->size()); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2258 | if (TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2259 | = ::DeduceTemplateArguments(*this, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2260 | Partial->getTemplateParameters(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2261 | Partial->getTemplateArgs(), |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2262 | TemplateArgs, Info, Deduced)) |
| 2263 | return Result; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 2264 | |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2265 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2266 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2267 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2268 | if (Inst.isInvalid()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2269 | return TDK_InstantiationDepth; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2270 | |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2271 | if (Trap.hasErrorOccurred()) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2272 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2273 | |
| 2274 | return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs, |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2275 | Deduced, Info); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2276 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2277 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2278 | /// Complete template argument deduction for a variable template partial |
| 2279 | /// specialization. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2280 | /// TODO: Unify with ClassTemplatePartialSpecializationDecl version? |
| 2281 | /// May require unifying ClassTemplate(Partial)SpecializationDecl and |
| 2282 | /// VarTemplate(Partial)SpecializationDecl with a new data |
| 2283 | /// structure Template(Partial)SpecializationDecl, and |
| 2284 | /// using Template(Partial)SpecializationDecl as input type. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2285 | static Sema::TemplateDeductionResult FinishTemplateArgumentDeduction( |
| 2286 | Sema &S, VarTemplatePartialSpecializationDecl *Partial, |
| 2287 | const TemplateArgumentList &TemplateArgs, |
| 2288 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2289 | TemplateDeductionInfo &Info) { |
| 2290 | // Unevaluated SFINAE context. |
| 2291 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
| 2292 | Sema::SFINAETrap Trap(S); |
| 2293 | |
| 2294 | // C++ [temp.deduct.type]p2: |
| 2295 | // [...] or if any template argument remains neither deduced nor |
| 2296 | // explicitly specified, template argument deduction fails. |
| 2297 | SmallVector<TemplateArgument, 4> Builder; |
| 2298 | TemplateParameterList *PartialParams = Partial->getTemplateParameters(); |
| 2299 | for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) { |
| 2300 | NamedDecl *Param = PartialParams->getParam(I); |
| 2301 | if (Deduced[I].isNull()) { |
| 2302 | Info.Param = makeTemplateParameter(Param); |
| 2303 | return Sema::TDK_Incomplete; |
| 2304 | } |
| 2305 | |
| 2306 | // We have deduced this argument, so it still needs to be |
| 2307 | // checked and converted. |
| 2308 | |
| 2309 | // First, for a non-type template parameter type that is |
| 2310 | // initialized by a declaration, we need the type of the |
| 2311 | // corresponding non-type template parameter. |
| 2312 | QualType NTTPType; |
| 2313 | if (NonTypeTemplateParmDecl *NTTP = |
| 2314 | dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2315 | NTTPType = NTTP->getType(); |
| 2316 | if (NTTPType->isDependentType()) { |
| 2317 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2318 | Builder.data(), Builder.size()); |
| 2319 | NTTPType = |
| 2320 | S.SubstType(NTTPType, MultiLevelTemplateArgumentList(TemplateArgs), |
| 2321 | NTTP->getLocation(), NTTP->getDeclName()); |
| 2322 | if (NTTPType.isNull()) { |
| 2323 | Info.Param = makeTemplateParameter(Param); |
| 2324 | // FIXME: These template arguments are temporary. Free them! |
| 2325 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(), |
| 2326 | Builder.size())); |
| 2327 | return Sema::TDK_SubstitutionFailure; |
| 2328 | } |
| 2329 | } |
| 2330 | } |
| 2331 | |
| 2332 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Partial, NTTPType, |
| 2333 | 0, Info, false, Builder)) { |
| 2334 | Info.Param = makeTemplateParameter(Param); |
| 2335 | // FIXME: These template arguments are temporary. Free them! |
| 2336 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(), |
| 2337 | Builder.size())); |
| 2338 | return Sema::TDK_SubstitutionFailure; |
| 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | // Form the template argument list from the deduced template arguments. |
| 2343 | TemplateArgumentList *DeducedArgumentList = TemplateArgumentList::CreateCopy( |
| 2344 | S.Context, Builder.data(), Builder.size()); |
| 2345 | |
| 2346 | Info.reset(DeducedArgumentList); |
| 2347 | |
| 2348 | // Substitute the deduced template arguments into the template |
| 2349 | // arguments of the class template partial specialization, and |
| 2350 | // verify that the instantiated template arguments are both valid |
| 2351 | // and are equivalent to the template arguments originally provided |
| 2352 | // to the class template. |
| 2353 | LocalInstantiationScope InstScope(S); |
| 2354 | VarTemplateDecl *VarTemplate = Partial->getSpecializedTemplate(); |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2355 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo |
| 2356 | = Partial->getTemplateArgsAsWritten(); |
| 2357 | const TemplateArgumentLoc *PartialTemplateArgs |
| 2358 | = PartialTemplArgInfo->getTemplateArgs(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2359 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2360 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
| 2361 | PartialTemplArgInfo->RAngleLoc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2362 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2363 | if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2364 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 2365 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 2366 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 2367 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 2368 | |
| 2369 | Decl *Param = const_cast<NamedDecl *>( |
| 2370 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
| 2371 | Info.Param = makeTemplateParameter(Param); |
| 2372 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 2373 | return Sema::TDK_SubstitutionFailure; |
| 2374 | } |
| 2375 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
| 2376 | if (S.CheckTemplateArgumentList(VarTemplate, Partial->getLocation(), InstArgs, |
| 2377 | false, ConvertedInstArgs)) |
| 2378 | return Sema::TDK_SubstitutionFailure; |
| 2379 | |
| 2380 | TemplateParameterList *TemplateParams = VarTemplate->getTemplateParameters(); |
| 2381 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
| 2382 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
| 2383 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
| 2384 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
| 2385 | Info.FirstArg = TemplateArgs[I]; |
| 2386 | Info.SecondArg = InstArg; |
| 2387 | return Sema::TDK_NonDeducedMismatch; |
| 2388 | } |
| 2389 | } |
| 2390 | |
| 2391 | if (Trap.hasErrorOccurred()) |
| 2392 | return Sema::TDK_SubstitutionFailure; |
| 2393 | |
| 2394 | return Sema::TDK_Success; |
| 2395 | } |
| 2396 | |
| 2397 | /// \brief Perform template argument deduction to determine whether |
| 2398 | /// the given template arguments match the given variable template |
| 2399 | /// partial specialization per C++ [temp.class.spec.match]. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2400 | /// TODO: Unify with ClassTemplatePartialSpecializationDecl version? |
| 2401 | /// May require unifying ClassTemplate(Partial)SpecializationDecl and |
| 2402 | /// VarTemplate(Partial)SpecializationDecl with a new data |
| 2403 | /// structure Template(Partial)SpecializationDecl, and |
| 2404 | /// using Template(Partial)SpecializationDecl as input type. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2405 | Sema::TemplateDeductionResult |
| 2406 | Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial, |
| 2407 | const TemplateArgumentList &TemplateArgs, |
| 2408 | TemplateDeductionInfo &Info) { |
| 2409 | if (Partial->isInvalidDecl()) |
| 2410 | return TDK_Invalid; |
| 2411 | |
| 2412 | // C++ [temp.class.spec.match]p2: |
| 2413 | // A partial specialization matches a given actual template |
| 2414 | // argument list if the template arguments of the partial |
| 2415 | // specialization can be deduced from the actual template argument |
| 2416 | // list (14.8.2). |
| 2417 | |
| 2418 | // Unevaluated SFINAE context. |
| 2419 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
| 2420 | SFINAETrap Trap(*this); |
| 2421 | |
| 2422 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 2423 | Deduced.resize(Partial->getTemplateParameters()->size()); |
| 2424 | if (TemplateDeductionResult Result = ::DeduceTemplateArguments( |
| 2425 | *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(), |
| 2426 | TemplateArgs, Info, Deduced)) |
| 2427 | return Result; |
| 2428 | |
| 2429 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2430 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2431 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2432 | if (Inst.isInvalid()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2433 | return TDK_InstantiationDepth; |
| 2434 | |
| 2435 | if (Trap.hasErrorOccurred()) |
| 2436 | return Sema::TDK_SubstitutionFailure; |
| 2437 | |
| 2438 | return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs, |
| 2439 | Deduced, Info); |
| 2440 | } |
| 2441 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2442 | /// \brief Determine whether the given type T is a simple-template-id type. |
| 2443 | static bool isSimpleTemplateIdType(QualType T) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2444 | if (const TemplateSpecializationType *Spec |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2445 | = T->getAs<TemplateSpecializationType>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2446 | return Spec->getTemplateName().getAsTemplateDecl() != nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2447 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2448 | return false; |
| 2449 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2450 | |
| 2451 | /// \brief Substitute the explicitly-provided template arguments into the |
| 2452 | /// given function template according to C++ [temp.arg.explicit]. |
| 2453 | /// |
| 2454 | /// \param FunctionTemplate the function template into which the explicit |
| 2455 | /// template arguments will be substituted. |
| 2456 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2457 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2458 | /// arguments. |
| 2459 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2460 | /// \param Deduced the deduced template arguments, which will be populated |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2461 | /// with the converted and checked explicit template arguments. |
| 2462 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2463 | /// \param ParamTypes will be populated with the instantiated function |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2464 | /// parameters. |
| 2465 | /// |
| 2466 | /// \param FunctionType if non-NULL, the result type of the function template |
| 2467 | /// will also be instantiated and the pointed-to value will be updated with |
| 2468 | /// the instantiated function type. |
| 2469 | /// |
| 2470 | /// \param Info if substitution fails for any reason, this object will be |
| 2471 | /// populated with more information about the failure. |
| 2472 | /// |
| 2473 | /// \returns TDK_Success if substitution was successful, or some failure |
| 2474 | /// condition. |
| 2475 | Sema::TemplateDeductionResult |
| 2476 | Sema::SubstituteExplicitTemplateArguments( |
| 2477 | FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2478 | TemplateArgumentListInfo &ExplicitTemplateArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2479 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2480 | SmallVectorImpl<QualType> &ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2481 | QualType *FunctionType, |
| 2482 | TemplateDeductionInfo &Info) { |
| 2483 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 2484 | TemplateParameterList *TemplateParams |
| 2485 | = FunctionTemplate->getTemplateParameters(); |
| 2486 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2487 | if (ExplicitTemplateArgs.size() == 0) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2488 | // No arguments to substitute; just copy over the parameter types and |
| 2489 | // fill in the function type. |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 2490 | for (auto P : Function->params()) |
| 2491 | ParamTypes.push_back(P->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2492 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2493 | if (FunctionType) |
| 2494 | *FunctionType = Function->getType(); |
| 2495 | return TDK_Success; |
| 2496 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2497 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2498 | // Unevaluated SFINAE context. |
| 2499 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2500 | SFINAETrap Trap(*this); |
| 2501 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2502 | // C++ [temp.arg.explicit]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2503 | // Template arguments that are present shall be specified in the |
| 2504 | // declaration order of their corresponding template-parameters. The |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2505 | // template argument list shall not specify more template-arguments than |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2506 | // there are corresponding template-parameters. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2507 | SmallVector<TemplateArgument, 4> Builder; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2508 | |
| 2509 | // Enter a new template instantiation context where we check the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2510 | // explicitly-specified template arguments against this function template, |
| 2511 | // and then substitute them into the function parameter types. |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2512 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2513 | InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate, |
| 2514 | DeducedArgs, |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2515 | ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution, |
| 2516 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2517 | if (Inst.isInvalid()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2518 | return TDK_InstantiationDepth; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2519 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2520 | if (CheckTemplateArgumentList(FunctionTemplate, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2521 | SourceLocation(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2522 | ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2523 | true, |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2524 | Builder) || Trap.hasErrorOccurred()) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2525 | unsigned Index = Builder.size(); |
Douglas Gregor | 62c281a | 2010-05-09 01:26:06 +0000 | [diff] [blame] | 2526 | if (Index >= TemplateParams->size()) |
| 2527 | Index = TemplateParams->size() - 1; |
| 2528 | Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2529 | return TDK_InvalidExplicitArguments; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2530 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2531 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2532 | // Form the template argument list from the explicitly-specified |
| 2533 | // template arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2534 | TemplateArgumentList *ExplicitArgumentList |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2535 | = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size()); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2536 | Info.reset(ExplicitArgumentList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2537 | |
John McCall | 036855a | 2010-10-12 19:40:14 +0000 | [diff] [blame] | 2538 | // Template argument deduction and the final substitution should be |
| 2539 | // done in the context of the templated declaration. Explicit |
| 2540 | // argument substitution, on the other hand, needs to happen in the |
| 2541 | // calling context. |
| 2542 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
| 2543 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2544 | // If we deduced template arguments for a template parameter pack, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2545 | // note that the template argument pack is partially substituted and record |
| 2546 | // the explicit template arguments. They'll be used as part of deduction |
| 2547 | // for this template parameter pack. |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2548 | for (unsigned I = 0, N = Builder.size(); I != N; ++I) { |
| 2549 | const TemplateArgument &Arg = Builder[I]; |
| 2550 | if (Arg.getKind() == TemplateArgument::Pack) { |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2551 | CurrentInstantiationScope->SetPartiallySubstitutedPack( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2552 | TemplateParams->getParam(I), |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2553 | Arg.pack_begin(), |
| 2554 | Arg.pack_size()); |
| 2555 | break; |
| 2556 | } |
| 2557 | } |
| 2558 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2559 | const FunctionProtoType *Proto |
| 2560 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2561 | assert(Proto && "Function template does not have a prototype?"); |
| 2562 | |
Richard Smith | 70b1304 | 2015-01-09 01:19:56 +0000 | [diff] [blame] | 2563 | // Isolate our substituted parameters from our caller. |
| 2564 | LocalInstantiationScope InstScope(*this, /*MergeWithOuterScope*/true); |
| 2565 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2566 | // Instantiate the types of each of the function parameters given the |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2567 | // explicitly-specified template arguments. If the function has a trailing |
| 2568 | // return type, substitute it after the arguments to ensure we substitute |
| 2569 | // in lexical order. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2570 | if (Proto->hasTrailingReturn()) { |
| 2571 | if (SubstParmTypes(Function->getLocation(), |
| 2572 | Function->param_begin(), Function->getNumParams(), |
| 2573 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 2574 | ParamTypes)) |
| 2575 | return TDK_SubstitutionFailure; |
| 2576 | } |
| 2577 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2578 | // Instantiate the return type. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2579 | QualType ResultType; |
| 2580 | { |
| 2581 | // C++11 [expr.prim.general]p3: |
| 2582 | // If a declaration declares a member function or member function |
| 2583 | // template of a class X, the expression this is a prvalue of type |
| 2584 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
| 2585 | // and the end of the function-definition, member-declarator, or |
| 2586 | // declarator. |
| 2587 | unsigned ThisTypeQuals = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2588 | CXXRecordDecl *ThisContext = nullptr; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2589 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
| 2590 | ThisContext = Method->getParent(); |
| 2591 | ThisTypeQuals = Method->getTypeQualifiers(); |
| 2592 | } |
| 2593 | |
| 2594 | CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2595 | getLangOpts().CPlusPlus11); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2596 | |
| 2597 | ResultType = |
| 2598 | SubstType(Proto->getReturnType(), |
| 2599 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 2600 | Function->getTypeSpecStartLoc(), Function->getDeclName()); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2601 | if (ResultType.isNull() || Trap.hasErrorOccurred()) |
| 2602 | return TDK_SubstitutionFailure; |
| 2603 | } |
| 2604 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2605 | // Instantiate the types of each of the function parameters given the |
| 2606 | // explicitly-specified template arguments if we didn't do so earlier. |
| 2607 | if (!Proto->hasTrailingReturn() && |
| 2608 | SubstParmTypes(Function->getLocation(), |
| 2609 | Function->param_begin(), Function->getNumParams(), |
| 2610 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 2611 | ParamTypes)) |
| 2612 | return TDK_SubstitutionFailure; |
| 2613 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2614 | if (FunctionType) { |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2615 | *FunctionType = BuildFunctionType(ResultType, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2616 | Function->getLocation(), |
Eli Friedman | d8725a9 | 2010-08-05 02:54:05 +0000 | [diff] [blame] | 2617 | Function->getDeclName(), |
Jordan Rose | a0a86be | 2013-03-08 22:25:36 +0000 | [diff] [blame] | 2618 | Proto->getExtProtoInfo()); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2619 | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
| 2620 | return TDK_SubstitutionFailure; |
| 2621 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2622 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2623 | // C++ [temp.arg.explicit]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2624 | // Trailing template arguments that can be deduced (14.8.2) may be |
| 2625 | // omitted from the list of explicit template-arguments. If all of the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2626 | // template arguments can be deduced, they may all be omitted; in this |
| 2627 | // case, the empty template argument list <> itself may also be omitted. |
| 2628 | // |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2629 | // Take all of the explicitly-specified arguments and put them into |
| 2630 | // the set of deduced template arguments. Explicitly-specified |
| 2631 | // parameter packs, however, will be set to NULL since the deduction |
| 2632 | // mechanisms handle explicitly-specified argument packs directly. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2633 | Deduced.reserve(TemplateParams->size()); |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2634 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) { |
| 2635 | const TemplateArgument &Arg = ExplicitArgumentList->get(I); |
| 2636 | if (Arg.getKind() == TemplateArgument::Pack) |
| 2637 | Deduced.push_back(DeducedTemplateArgument()); |
| 2638 | else |
| 2639 | Deduced.push_back(Arg); |
| 2640 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2641 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2642 | return TDK_Success; |
| 2643 | } |
| 2644 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2645 | /// \brief Check whether the deduced argument type for a call to a function |
| 2646 | /// template matches the actual argument type per C++ [temp.deduct.call]p4. |
| 2647 | static bool |
| 2648 | CheckOriginalCallArgDeduction(Sema &S, Sema::OriginalCallArg OriginalArg, |
| 2649 | QualType DeducedA) { |
| 2650 | ASTContext &Context = S.Context; |
| 2651 | |
| 2652 | QualType A = OriginalArg.OriginalArgType; |
| 2653 | QualType OriginalParamType = OriginalArg.OriginalParamType; |
| 2654 | |
| 2655 | // Check for type equality (top-level cv-qualifiers are ignored). |
| 2656 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 2657 | return false; |
| 2658 | |
| 2659 | // Strip off references on the argument types; they aren't needed for |
| 2660 | // the following checks. |
| 2661 | if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>()) |
| 2662 | DeducedA = DeducedARef->getPointeeType(); |
| 2663 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
| 2664 | A = ARef->getPointeeType(); |
| 2665 | |
| 2666 | // C++ [temp.deduct.call]p4: |
| 2667 | // [...] However, there are three cases that allow a difference: |
| 2668 | // - If the original P is a reference type, the deduced A (i.e., the |
| 2669 | // type referred to by the reference) can be more cv-qualified than |
| 2670 | // the transformed A. |
| 2671 | if (const ReferenceType *OriginalParamRef |
| 2672 | = OriginalParamType->getAs<ReferenceType>()) { |
| 2673 | // We don't want to keep the reference around any more. |
| 2674 | OriginalParamType = OriginalParamRef->getPointeeType(); |
| 2675 | |
| 2676 | Qualifiers AQuals = A.getQualifiers(); |
| 2677 | Qualifiers DeducedAQuals = DeducedA.getQualifiers(); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2678 | |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2679 | // Under Objective-C++ ARC, the deduced type may have implicitly |
| 2680 | // been given strong or (when dealing with a const reference) |
| 2681 | // unsafe_unretained lifetime. If so, update the original |
| 2682 | // qualifiers to include this lifetime. |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2683 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2684 | ((DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong && |
| 2685 | AQuals.getObjCLifetime() == Qualifiers::OCL_None) || |
| 2686 | (DeducedAQuals.hasConst() && |
| 2687 | DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone))) { |
| 2688 | AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime()); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2689 | } |
| 2690 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2691 | if (AQuals == DeducedAQuals) { |
| 2692 | // Qualifiers match; there's nothing to do. |
| 2693 | } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) { |
Douglas Gregor | ddaae52 | 2011-06-17 14:36:00 +0000 | [diff] [blame] | 2694 | return true; |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2695 | } else { |
| 2696 | // Qualifiers are compatible, so have the argument type adopt the |
| 2697 | // deduced argument type's qualifiers as if we had performed the |
| 2698 | // qualification conversion. |
| 2699 | A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals); |
| 2700 | } |
| 2701 | } |
| 2702 | |
| 2703 | // - The transformed A can be another pointer or pointer to member |
| 2704 | // type that can be converted to the deduced A via a qualification |
| 2705 | // conversion. |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2706 | // |
| 2707 | // Also allow conversions which merely strip [[noreturn]] from function types |
| 2708 | // (recursively) as an extension. |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2709 | // FIXME: Currently, this doesn't play nicely with qualification conversions. |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2710 | bool ObjCLifetimeConversion = false; |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2711 | QualType ResultTy; |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2712 | if ((A->isAnyPointerType() || A->isMemberPointerType()) && |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2713 | (S.IsQualificationConversion(A, DeducedA, false, |
| 2714 | ObjCLifetimeConversion) || |
| 2715 | S.IsNoReturnConversion(A, DeducedA, ResultTy))) |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2716 | return false; |
| 2717 | |
| 2718 | |
| 2719 | // - If P is a class and P has the form simple-template-id, then the |
| 2720 | // transformed A can be a derived class of the deduced A. [...] |
| 2721 | // [...] Likewise, if P is a pointer to a class of the form |
| 2722 | // simple-template-id, the transformed A can be a pointer to a |
| 2723 | // derived class pointed to by the deduced A. |
| 2724 | if (const PointerType *OriginalParamPtr |
| 2725 | = OriginalParamType->getAs<PointerType>()) { |
| 2726 | if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) { |
| 2727 | if (const PointerType *APtr = A->getAs<PointerType>()) { |
| 2728 | if (A->getPointeeType()->isRecordType()) { |
| 2729 | OriginalParamType = OriginalParamPtr->getPointeeType(); |
| 2730 | DeducedA = DeducedAPtr->getPointeeType(); |
| 2731 | A = APtr->getPointeeType(); |
| 2732 | } |
| 2733 | } |
| 2734 | } |
| 2735 | } |
| 2736 | |
| 2737 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 2738 | return false; |
| 2739 | |
| 2740 | if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) && |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 2741 | S.IsDerivedFrom(SourceLocation(), A, DeducedA)) |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2742 | return false; |
| 2743 | |
| 2744 | return true; |
| 2745 | } |
| 2746 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2747 | /// \brief Finish template argument deduction for a function template, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2748 | /// checking the deduced template arguments for completeness and forming |
| 2749 | /// the function template specialization. |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2750 | /// |
| 2751 | /// \param OriginalCallArgs If non-NULL, the original call arguments against |
| 2752 | /// which the deduced argument types should be compared. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2753 | Sema::TemplateDeductionResult |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2754 | Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2755 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2756 | unsigned NumExplicitlySpecified, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2757 | FunctionDecl *&Specialization, |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2758 | TemplateDeductionInfo &Info, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 2759 | SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs, |
| 2760 | bool PartialOverloading) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2761 | TemplateParameterList *TemplateParams |
| 2762 | = FunctionTemplate->getTemplateParameters(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2763 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2764 | // Unevaluated SFINAE context. |
| 2765 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2766 | SFINAETrap Trap(*this); |
| 2767 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2768 | // Enter a new template instantiation context while we instantiate the |
| 2769 | // actual function declaration. |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2770 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2771 | InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate, |
| 2772 | DeducedArgs, |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2773 | ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution, |
| 2774 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2775 | if (Inst.isInvalid()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2776 | return TDK_InstantiationDepth; |
| 2777 | |
John McCall | e23b871 | 2010-04-29 01:18:58 +0000 | [diff] [blame] | 2778 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
John McCall | 80e58cd | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 2779 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2780 | // C++ [temp.deduct.type]p2: |
| 2781 | // [...] or if any template argument remains neither deduced nor |
| 2782 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2783 | SmallVector<TemplateArgument, 4> Builder; |
Douglas Gregor | aaa6a90 | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2784 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 2785 | NamedDecl *Param = TemplateParams->getParam(I); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2786 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2787 | if (!Deduced[I].isNull()) { |
Douglas Gregor | 6e9cf63 | 2010-10-12 18:51:08 +0000 | [diff] [blame] | 2788 | if (I < NumExplicitlySpecified) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2789 | // We have already fully type-checked and converted this |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2790 | // argument, because it was explicitly-specified. Just record the |
Douglas Gregor | 6e9cf63 | 2010-10-12 18:51:08 +0000 | [diff] [blame] | 2791 | // presence of this argument. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2792 | Builder.push_back(Deduced[I]); |
Faisal Vali | 3628cb9 | 2014-06-01 16:11:54 +0000 | [diff] [blame] | 2793 | // We may have had explicitly-specified template arguments for a |
| 2794 | // template parameter pack (that may or may not have been extended |
| 2795 | // via additional deduced arguments). |
| 2796 | if (Param->isParameterPack() && CurrentInstantiationScope) { |
| 2797 | if (CurrentInstantiationScope->getPartiallySubstitutedPack() == |
| 2798 | Param) { |
| 2799 | // Forget the partially-substituted pack; its substitution is now |
| 2800 | // complete. |
| 2801 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2802 | } |
| 2803 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2804 | continue; |
| 2805 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2806 | // We have deduced this argument, so it still needs to be |
| 2807 | // checked and converted. |
| 2808 | |
| 2809 | // First, for a non-type template parameter type that is |
| 2810 | // initialized by a declaration, we need the type of the |
| 2811 | // corresponding non-type template parameter. |
| 2812 | QualType NTTPType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2813 | if (NonTypeTemplateParmDecl *NTTP |
| 2814 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | aaa6a90 | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2815 | NTTPType = NTTP->getType(); |
| 2816 | if (NTTPType->isDependentType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2817 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | aaa6a90 | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2818 | Builder.data(), Builder.size()); |
| 2819 | NTTPType = SubstType(NTTPType, |
| 2820 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2821 | NTTP->getLocation(), |
| 2822 | NTTP->getDeclName()); |
| 2823 | if (NTTPType.isNull()) { |
| 2824 | Info.Param = makeTemplateParameter(Param); |
| 2825 | // FIXME: These template arguments are temporary. Free them! |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2826 | Info.reset(TemplateArgumentList::CreateCopy(Context, |
| 2827 | Builder.data(), |
Douglas Gregor | aaa6a90 | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2828 | Builder.size())); |
| 2829 | return TDK_SubstitutionFailure; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2830 | } |
| 2831 | } |
| 2832 | } |
| 2833 | |
Douglas Gregor | aaa6a90 | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2834 | if (ConvertDeducedTemplateArgument(*this, Param, Deduced[I], |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2835 | FunctionTemplate, NTTPType, 0, Info, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2836 | true, Builder)) { |
Douglas Gregor | aaa6a90 | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2837 | Info.Param = makeTemplateParameter(Param); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2838 | // FIXME: These template arguments are temporary. Free them! |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2839 | Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(), |
| 2840 | Builder.size())); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2841 | return TDK_SubstitutionFailure; |
| 2842 | } |
| 2843 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2844 | continue; |
| 2845 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2846 | |
Douglas Gregor | ca4d91d | 2010-12-23 01:52:01 +0000 | [diff] [blame] | 2847 | // C++0x [temp.arg.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2848 | // A trailing template parameter pack (14.5.3) not otherwise deduced will |
Douglas Gregor | ca4d91d | 2010-12-23 01:52:01 +0000 | [diff] [blame] | 2849 | // be deduced to an empty sequence of template arguments. |
| 2850 | // FIXME: Where did the word "trailing" come from? |
| 2851 | if (Param->isTemplateParameterPack()) { |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2852 | // We may have had explicitly-specified template arguments for this |
| 2853 | // template parameter pack. If so, our empty deduction extends the |
| 2854 | // explicitly-specified set (C++0x [temp.arg.explicit]p9). |
| 2855 | const TemplateArgument *ExplicitArgs; |
| 2856 | unsigned NumExplicitArgs; |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2857 | if (CurrentInstantiationScope && |
| 2858 | CurrentInstantiationScope->getPartiallySubstitutedPack(&ExplicitArgs, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2859 | &NumExplicitArgs) |
Douglas Gregor | caddba9 | 2013-01-18 22:27:09 +0000 | [diff] [blame] | 2860 | == Param) { |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 2861 | Builder.push_back(TemplateArgument( |
| 2862 | llvm::makeArrayRef(ExplicitArgs, NumExplicitArgs))); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2863 | |
Douglas Gregor | caddba9 | 2013-01-18 22:27:09 +0000 | [diff] [blame] | 2864 | // Forget the partially-substituted pack; it's substitution is now |
| 2865 | // complete. |
| 2866 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2867 | } else { |
| 2868 | Builder.push_back(TemplateArgument::getEmptyPack()); |
| 2869 | } |
Douglas Gregor | ca4d91d | 2010-12-23 01:52:01 +0000 | [diff] [blame] | 2870 | continue; |
| 2871 | } |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2872 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2873 | // Substitute into the default template argument, if available. |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 2874 | bool HasDefaultArg = false; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2875 | TemplateArgumentLoc DefArg |
| 2876 | = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate, |
| 2877 | FunctionTemplate->getLocation(), |
| 2878 | FunctionTemplate->getSourceRange().getEnd(), |
| 2879 | Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 2880 | Builder, HasDefaultArg); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2881 | |
| 2882 | // If there was no default argument, deduction is incomplete. |
| 2883 | if (DefArg.getArgument().isNull()) { |
| 2884 | Info.Param = makeTemplateParameter( |
| 2885 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 2886 | Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(), |
| 2887 | Builder.size())); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 2888 | if (PartialOverloading) break; |
| 2889 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 2890 | return HasDefaultArg ? TDK_SubstitutionFailure : TDK_Incomplete; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2891 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2892 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2893 | // Check whether we can actually use the default argument. |
| 2894 | if (CheckTemplateArgument(Param, DefArg, |
| 2895 | FunctionTemplate, |
| 2896 | FunctionTemplate->getLocation(), |
| 2897 | FunctionTemplate->getSourceRange().getEnd(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2898 | 0, Builder, |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2899 | CTAK_Specified)) { |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2900 | Info.Param = makeTemplateParameter( |
| 2901 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2902 | // FIXME: These template arguments are temporary. Free them! |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2903 | Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2904 | Builder.size())); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2905 | return TDK_SubstitutionFailure; |
| 2906 | } |
| 2907 | |
| 2908 | // If we get here, we successfully used the default template argument. |
| 2909 | } |
| 2910 | |
| 2911 | // Form the template argument list from the deduced template arguments. |
| 2912 | TemplateArgumentList *DeducedArgumentList |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2913 | = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size()); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2914 | Info.reset(DeducedArgumentList); |
| 2915 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2916 | // Substitute the deduced template arguments into the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2917 | // declaration to produce the function template specialization. |
Douglas Gregor | 1614237 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2918 | DeclContext *Owner = FunctionTemplate->getDeclContext(); |
| 2919 | if (FunctionTemplate->getFriendObjectKind()) |
| 2920 | Owner = FunctionTemplate->getLexicalDeclContext(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2921 | Specialization = cast_or_null<FunctionDecl>( |
Douglas Gregor | 1614237 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2922 | SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, |
Douglas Gregor | 39cacdb | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 2923 | MultiLevelTemplateArgumentList(*DeducedArgumentList))); |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 2924 | if (!Specialization || Specialization->isInvalidDecl()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2925 | return TDK_SubstitutionFailure; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2926 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2927 | assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == |
Douglas Gregor | 31fae89 | 2009-09-15 18:26:13 +0000 | [diff] [blame] | 2928 | FunctionTemplate->getCanonicalDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2929 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2930 | // If the template argument list is owned by the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2931 | // specialization, release it. |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 2932 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList && |
| 2933 | !Trap.hasErrorOccurred()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2934 | Info.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2935 | |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 2936 | // There may have been an error that did not prevent us from constructing a |
| 2937 | // declaration. Mark the declaration invalid and return with a substitution |
| 2938 | // failure. |
| 2939 | if (Trap.hasErrorOccurred()) { |
| 2940 | Specialization->setInvalidDecl(true); |
| 2941 | return TDK_SubstitutionFailure; |
| 2942 | } |
| 2943 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2944 | if (OriginalCallArgs) { |
| 2945 | // C++ [temp.deduct.call]p4: |
| 2946 | // In general, the deduction process attempts to find template argument |
| 2947 | // values that will make the deduced A identical to A (after the type A |
| 2948 | // is transformed as described above). [...] |
| 2949 | for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) { |
| 2950 | OriginalCallArg OriginalArg = (*OriginalCallArgs)[I]; |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2951 | unsigned ParamIdx = OriginalArg.ArgIdx; |
| 2952 | |
| 2953 | if (ParamIdx >= Specialization->getNumParams()) |
| 2954 | continue; |
| 2955 | |
| 2956 | QualType DeducedA = Specialization->getParamDecl(ParamIdx)->getType(); |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2957 | if (CheckOriginalCallArgDeduction(*this, OriginalArg, DeducedA)) |
| 2958 | return Sema::TDK_SubstitutionFailure; |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2959 | } |
| 2960 | } |
| 2961 | |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2962 | // If we suppressed any diagnostics while performing template argument |
| 2963 | // deduction, and if we haven't already instantiated this declaration, |
| 2964 | // keep track of these diagnostics. They'll be emitted if this specialization |
| 2965 | // is actually used. |
| 2966 | if (Info.diag_begin() != Info.diag_end()) { |
Craig Topper | 79be4cd | 2013-07-05 04:33:53 +0000 | [diff] [blame] | 2967 | SuppressedDiagnosticsMap::iterator |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2968 | Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); |
| 2969 | if (Pos == SuppressedDiagnostics.end()) |
| 2970 | SuppressedDiagnostics[Specialization->getCanonicalDecl()] |
| 2971 | .append(Info.diag_begin(), Info.diag_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2972 | } |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2973 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2974 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2977 | /// Gets the type of a function for template-argument-deducton |
| 2978 | /// purposes when it's considered as part of an overload set. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2979 | static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R, |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2980 | FunctionDecl *Fn) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2981 | // We may need to deduce the return type of the function now. |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 2982 | if (S.getLangOpts().CPlusPlus14 && Fn->getReturnType()->isUndeducedType() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2983 | S.DeduceReturnType(Fn, R.Expression->getExprLoc(), /*Diagnose*/ false)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2984 | return QualType(); |
| 2985 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2986 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2987 | if (Method->isInstance()) { |
| 2988 | // An instance method that's referenced in a form that doesn't |
| 2989 | // look like a member pointer is just invalid. |
| 2990 | if (!R.HasFormOfMemberPointer) return QualType(); |
| 2991 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2992 | return S.Context.getMemberPointerType(Fn->getType(), |
| 2993 | S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2994 | } |
| 2995 | |
| 2996 | if (!R.IsAddressOfOperand) return Fn->getType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2997 | return S.Context.getPointerType(Fn->getType()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | /// Apply the deduction rules for overload sets. |
| 3001 | /// |
| 3002 | /// \return the null type if this argument should be treated as an |
| 3003 | /// undeduced context |
| 3004 | static QualType |
| 3005 | ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3006 | Expr *Arg, QualType ParamType, |
| 3007 | bool ParamWasReference) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3008 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3009 | OverloadExpr::FindResult R = OverloadExpr::find(Arg); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3010 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3011 | OverloadExpr *Ovl = R.Expression; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3012 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3013 | // C++0x [temp.deduct.call]p4 |
| 3014 | unsigned TDF = 0; |
| 3015 | if (ParamWasReference) |
| 3016 | TDF |= TDF_ParamWithReferenceType; |
| 3017 | if (R.IsAddressOfOperand) |
| 3018 | TDF |= TDF_IgnoreQualifiers; |
| 3019 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3020 | // C++0x [temp.deduct.call]p6: |
| 3021 | // When P is a function type, pointer to function type, or pointer |
| 3022 | // to member function type: |
| 3023 | |
| 3024 | if (!ParamType->isFunctionType() && |
| 3025 | !ParamType->isFunctionPointerType() && |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3026 | !ParamType->isMemberFunctionPointerType()) { |
| 3027 | if (Ovl->hasExplicitTemplateArgs()) { |
| 3028 | // But we can still look for an explicit specialization. |
| 3029 | if (FunctionDecl *ExplicitSpec |
| 3030 | = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3031 | return GetTypeOfFunction(S, R, ExplicitSpec); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3032 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3033 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3034 | return QualType(); |
| 3035 | } |
| 3036 | |
| 3037 | // Gather the explicit template arguments, if any. |
| 3038 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 3039 | if (Ovl->hasExplicitTemplateArgs()) |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame^] | 3040 | Ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3041 | QualType Match; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 3042 | for (UnresolvedSetIterator I = Ovl->decls_begin(), |
| 3043 | E = Ovl->decls_end(); I != E; ++I) { |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3044 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 3045 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3046 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) { |
| 3047 | // - If the argument is an overload set containing one or more |
| 3048 | // function templates, the parameter is treated as a |
| 3049 | // non-deduced context. |
| 3050 | if (!Ovl->hasExplicitTemplateArgs()) |
| 3051 | return QualType(); |
| 3052 | |
| 3053 | // Otherwise, see if we can resolve a function type |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3054 | FunctionDecl *Specialization = nullptr; |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3055 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3056 | if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs, |
| 3057 | Specialization, Info)) |
| 3058 | continue; |
| 3059 | |
| 3060 | D = Specialization; |
| 3061 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3062 | |
| 3063 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3064 | QualType ArgType = GetTypeOfFunction(S, R, Fn); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3065 | if (ArgType.isNull()) continue; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3066 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3067 | // Function-to-pointer conversion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3068 | if (!ParamWasReference && ParamType->isPointerType() && |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3069 | ArgType->isFunctionType()) |
| 3070 | ArgType = S.Context.getPointerType(ArgType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3071 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3072 | // - If the argument is an overload set (not containing function |
| 3073 | // templates), trial argument deduction is attempted using each |
| 3074 | // of the members of the set. If deduction succeeds for only one |
| 3075 | // of the overload set members, that member is used as the |
| 3076 | // argument value for the deduction. If deduction succeeds for |
| 3077 | // more than one member of the overload set the parameter is |
| 3078 | // treated as a non-deduced context. |
| 3079 | |
| 3080 | // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: |
| 3081 | // Type deduction is done independently for each P/A pair, and |
| 3082 | // the deduced template argument values are then combined. |
| 3083 | // So we do not reject deductions which were made elsewhere. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3084 | SmallVector<DeducedTemplateArgument, 8> |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3085 | Deduced(TemplateParams->size()); |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3086 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3087 | Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3088 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
| 3089 | ArgType, Info, Deduced, TDF); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3090 | if (Result) continue; |
| 3091 | if (!Match.isNull()) return QualType(); |
| 3092 | Match = ArgType; |
| 3093 | } |
| 3094 | |
| 3095 | return Match; |
| 3096 | } |
| 3097 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3098 | /// \brief Perform the adjustments to the parameter and argument types |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3099 | /// described in C++ [temp.deduct.call]. |
| 3100 | /// |
| 3101 | /// \returns true if the caller should not attempt to perform any template |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3102 | /// argument deduction based on this P/A pair because the argument is an |
| 3103 | /// overloaded function set that could not be resolved. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3104 | static bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S, |
| 3105 | TemplateParameterList *TemplateParams, |
| 3106 | QualType &ParamType, |
| 3107 | QualType &ArgType, |
| 3108 | Expr *Arg, |
| 3109 | unsigned &TDF) { |
| 3110 | // C++0x [temp.deduct.call]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3111 | // If P is a cv-qualified type, the top level cv-qualifiers of P's type |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3112 | // are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3113 | if (ParamType.hasQualifiers()) |
| 3114 | ParamType = ParamType.getUnqualifiedType(); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3115 | |
| 3116 | // [...] If P is a reference type, the type referred to by P is |
| 3117 | // used for type deduction. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3118 | const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3119 | if (ParamRefType) |
| 3120 | ParamType = ParamRefType->getPointeeType(); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3121 | |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3122 | // Overload sets usually make this parameter an undeduced context, |
| 3123 | // but there are sometimes special circumstances. Typically |
| 3124 | // involving a template-id-expr. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3125 | if (ArgType == S.Context.OverloadTy) { |
| 3126 | ArgType = ResolveOverloadForDeduction(S, TemplateParams, |
| 3127 | Arg, ParamType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3128 | ParamRefType != nullptr); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3129 | if (ArgType.isNull()) |
| 3130 | return true; |
| 3131 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3132 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3133 | if (ParamRefType) { |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3134 | // If the argument has incomplete array type, try to complete its type. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3135 | if (ArgType->isIncompleteArrayType()) { |
| 3136 | S.completeExprArrayBound(Arg); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3137 | ArgType = Arg->getType(); |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3138 | } |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3139 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3140 | // C++0x [temp.deduct.call]p3: |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3141 | // If P is an rvalue reference to a cv-unqualified template |
| 3142 | // parameter and the argument is an lvalue, the type "lvalue |
| 3143 | // reference to A" is used in place of A for type deduction. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3144 | if (ParamRefType->isRValueReferenceType() && |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3145 | !ParamType.getQualifiers() && |
| 3146 | isa<TemplateTypeParmType>(ParamType) && |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3147 | Arg->isLValue()) |
| 3148 | ArgType = S.Context.getLValueReferenceType(ArgType); |
| 3149 | } else { |
| 3150 | // C++ [temp.deduct.call]p2: |
| 3151 | // If P is not a reference type: |
| 3152 | // - If A is an array type, the pointer type produced by the |
| 3153 | // array-to-pointer standard conversion (4.2) is used in place of |
| 3154 | // A for type deduction; otherwise, |
| 3155 | if (ArgType->isArrayType()) |
| 3156 | ArgType = S.Context.getArrayDecayedType(ArgType); |
| 3157 | // - If A is a function type, the pointer type produced by the |
| 3158 | // function-to-pointer standard conversion (4.3) is used in place |
| 3159 | // of A for type deduction; otherwise, |
| 3160 | else if (ArgType->isFunctionType()) |
| 3161 | ArgType = S.Context.getPointerType(ArgType); |
| 3162 | else { |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3163 | // - If A is a cv-qualified type, the top level cv-qualifiers of A's |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3164 | // type are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3165 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3166 | } |
| 3167 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3168 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3169 | // C++0x [temp.deduct.call]p4: |
| 3170 | // In general, the deduction process attempts to find template argument |
| 3171 | // values that will make the deduced A identical to A (after the type A |
| 3172 | // is transformed as described above). [...] |
| 3173 | TDF = TDF_SkipNonDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3174 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3175 | // - If the original P is a reference type, the deduced A (i.e., the |
| 3176 | // type referred to by the reference) can be more cv-qualified than |
| 3177 | // the transformed A. |
| 3178 | if (ParamRefType) |
| 3179 | TDF |= TDF_ParamWithReferenceType; |
| 3180 | // - The transformed A can be another pointer or pointer to member |
| 3181 | // type that can be converted to the deduced A via a qualification |
| 3182 | // conversion (4.4). |
| 3183 | if (ArgType->isPointerType() || ArgType->isMemberPointerType() || |
| 3184 | ArgType->isObjCObjectPointerType()) |
| 3185 | TDF |= TDF_IgnoreQualifiers; |
| 3186 | // - If P is a class and P has the form simple-template-id, then the |
| 3187 | // transformed A can be a derived class of the deduced A. Likewise, |
| 3188 | // if P is a pointer to a class of the form simple-template-id, the |
| 3189 | // transformed A can be a pointer to a derived class pointed to by |
| 3190 | // the deduced A. |
| 3191 | if (isSimpleTemplateIdType(ParamType) || |
| 3192 | (isa<PointerType>(ParamType) && |
| 3193 | isSimpleTemplateIdType( |
| 3194 | ParamType->getAs<PointerType>()->getPointeeType()))) |
| 3195 | TDF |= TDF_DerivedClass; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3196 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3197 | return false; |
| 3198 | } |
| 3199 | |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3200 | static bool |
| 3201 | hasDeducibleTemplateParameters(Sema &S, FunctionTemplateDecl *FunctionTemplate, |
| 3202 | QualType T); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3203 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3204 | static Sema::TemplateDeductionResult DeduceTemplateArgumentByListElement( |
| 3205 | Sema &S, TemplateParameterList *TemplateParams, QualType ParamType, |
| 3206 | Expr *Arg, TemplateDeductionInfo &Info, |
| 3207 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, unsigned TDF); |
| 3208 | |
| 3209 | /// \brief Attempt template argument deduction from an initializer list |
| 3210 | /// deemed to be an argument in a function call. |
| 3211 | static bool |
| 3212 | DeduceFromInitializerList(Sema &S, TemplateParameterList *TemplateParams, |
| 3213 | QualType AdjustedParamType, InitListExpr *ILE, |
| 3214 | TemplateDeductionInfo &Info, |
| 3215 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3216 | unsigned TDF, Sema::TemplateDeductionResult &Result) { |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3217 | |
| 3218 | // [temp.deduct.call] p1 (post CWG-1591) |
| 3219 | // If removing references and cv-qualifiers from P gives |
| 3220 | // std::initializer_list<P0> or P0[N] for some P0 and N and the argument is a |
| 3221 | // non-empty initializer list (8.5.4), then deduction is performed instead for |
| 3222 | // each element of the initializer list, taking P0 as a function template |
| 3223 | // parameter type and the initializer element as its argument, and in the |
| 3224 | // P0[N] case, if N is a non-type template parameter, N is deduced from the |
| 3225 | // length of the initializer list. Otherwise, an initializer list argument |
| 3226 | // causes the parameter to be considered a non-deduced context |
| 3227 | |
| 3228 | const bool IsConstSizedArray = AdjustedParamType->isConstantArrayType(); |
| 3229 | |
| 3230 | const bool IsDependentSizedArray = |
| 3231 | !IsConstSizedArray && AdjustedParamType->isDependentSizedArrayType(); |
| 3232 | |
Faisal Vali | dd76cc1 | 2015-12-10 12:29:11 +0000 | [diff] [blame] | 3233 | QualType ElTy; // The element type of the std::initializer_list or the array. |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3234 | |
| 3235 | const bool IsSTDList = !IsConstSizedArray && !IsDependentSizedArray && |
| 3236 | S.isStdInitializerList(AdjustedParamType, &ElTy); |
| 3237 | |
| 3238 | if (!IsConstSizedArray && !IsDependentSizedArray && !IsSTDList) |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3239 | return false; |
| 3240 | |
| 3241 | Result = Sema::TDK_Success; |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3242 | // If we are not deducing against the 'T' in a std::initializer_list<T> then |
| 3243 | // deduce against the 'T' in T[N]. |
| 3244 | if (ElTy.isNull()) { |
| 3245 | assert(!IsSTDList); |
| 3246 | ElTy = S.Context.getAsArrayType(AdjustedParamType)->getElementType(); |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3247 | } |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3248 | // Deduction only needs to be done for dependent types. |
| 3249 | if (ElTy->isDependentType()) { |
| 3250 | for (Expr *E : ILE->inits()) { |
Craig Topper | 0852953 | 2015-12-10 08:49:55 +0000 | [diff] [blame] | 3251 | if ((Result = DeduceTemplateArgumentByListElement(S, TemplateParams, ElTy, |
| 3252 | E, Info, Deduced, TDF))) |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3253 | return true; |
| 3254 | } |
| 3255 | } |
| 3256 | if (IsDependentSizedArray) { |
| 3257 | const DependentSizedArrayType *ArrTy = |
| 3258 | S.Context.getAsDependentSizedArrayType(AdjustedParamType); |
| 3259 | // Determine the array bound is something we can deduce. |
| 3260 | if (NonTypeTemplateParmDecl *NTTP = |
| 3261 | getDeducedParameterFromExpr(ArrTy->getSizeExpr())) { |
| 3262 | // We can perform template argument deduction for the given non-type |
| 3263 | // template parameter. |
| 3264 | assert(NTTP->getDepth() == 0 && |
| 3265 | "Cannot deduce non-type template argument at depth > 0"); |
| 3266 | llvm::APInt Size(S.Context.getIntWidth(NTTP->getType()), |
| 3267 | ILE->getNumInits()); |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3268 | |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3269 | Result = DeduceNonTypeTemplateArgument( |
| 3270 | S, NTTP, llvm::APSInt(Size), NTTP->getType(), |
| 3271 | /*ArrayBound=*/true, Info, Deduced); |
| 3272 | } |
| 3273 | } |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3274 | return true; |
| 3275 | } |
| 3276 | |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3277 | /// \brief Perform template argument deduction by matching a parameter type |
| 3278 | /// against a single expression, where the expression is an element of |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3279 | /// an initializer list that was originally matched against a parameter |
| 3280 | /// of type \c initializer_list\<ParamType\>. |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3281 | static Sema::TemplateDeductionResult |
| 3282 | DeduceTemplateArgumentByListElement(Sema &S, |
| 3283 | TemplateParameterList *TemplateParams, |
| 3284 | QualType ParamType, Expr *Arg, |
| 3285 | TemplateDeductionInfo &Info, |
| 3286 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3287 | unsigned TDF) { |
| 3288 | // Handle the case where an init list contains another init list as the |
| 3289 | // element. |
| 3290 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3291 | Sema::TemplateDeductionResult Result; |
| 3292 | if (!DeduceFromInitializerList(S, TemplateParams, |
| 3293 | ParamType.getNonReferenceType(), ILE, Info, |
| 3294 | Deduced, TDF, Result)) |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3295 | return Sema::TDK_Success; // Just ignore this expression. |
| 3296 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3297 | return Result; |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | // For all other cases, just match by type. |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3301 | QualType ArgType = Arg->getType(); |
| 3302 | if (AdjustFunctionParmAndArgTypesForDeduction(S, TemplateParams, ParamType, |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3303 | ArgType, Arg, TDF)) { |
| 3304 | Info.Expression = Arg; |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3305 | return Sema::TDK_FailedOverloadResolution; |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3306 | } |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3307 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3308 | ArgType, Info, Deduced, TDF); |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3311 | /// \brief Perform template argument deduction from a function call |
| 3312 | /// (C++ [temp.deduct.call]). |
| 3313 | /// |
| 3314 | /// \param FunctionTemplate the function template for which we are performing |
| 3315 | /// template argument deduction. |
| 3316 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3317 | /// \param ExplicitTemplateArgs the explicit template arguments provided |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3318 | /// for this call. |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3319 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3320 | /// \param Args the function call arguments |
| 3321 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3322 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3323 | /// this will be set to the function template specialization produced by |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3324 | /// template argument deduction. |
| 3325 | /// |
| 3326 | /// \param Info the argument will be updated to provide additional information |
| 3327 | /// about template argument deduction. |
| 3328 | /// |
| 3329 | /// \returns the result of template argument deduction. |
Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 3330 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3331 | FunctionTemplateDecl *FunctionTemplate, |
| 3332 | TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3333 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 3334 | bool PartialOverloading) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3335 | if (FunctionTemplate->isInvalidDecl()) |
| 3336 | return TDK_Invalid; |
| 3337 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3338 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3339 | unsigned NumParams = Function->getNumParams(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3340 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3341 | // C++ [temp.deduct.call]p1: |
| 3342 | // Template argument deduction is done by comparing each function template |
| 3343 | // parameter type (call it P) with the type of the corresponding argument |
| 3344 | // of the call (call it A) as described below. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3345 | unsigned CheckArgs = Args.size(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3346 | if (Args.size() < Function->getMinRequiredArguments() && !PartialOverloading) |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3347 | return TDK_TooFewArguments; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3348 | else if (TooManyArguments(NumParams, Args.size(), PartialOverloading)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3349 | const FunctionProtoType *Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3350 | = Function->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3351 | if (Proto->isTemplateVariadic()) |
| 3352 | /* Do nothing */; |
| 3353 | else if (Proto->isVariadic()) |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3354 | CheckArgs = NumParams; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3355 | else |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3356 | return TDK_TooManyArguments; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3357 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3358 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3359 | // The types of the parameters from which we will perform template argument |
| 3360 | // deduction. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3361 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3362 | TemplateParameterList *TemplateParams |
| 3363 | = FunctionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3364 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 3365 | SmallVector<QualType, 4> ParamTypes; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3366 | unsigned NumExplicitlySpecified = 0; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3367 | if (ExplicitTemplateArgs) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3368 | TemplateDeductionResult Result = |
| 3369 | SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3370 | *ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3371 | Deduced, |
| 3372 | ParamTypes, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3373 | nullptr, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3374 | Info); |
| 3375 | if (Result) |
| 3376 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3377 | |
| 3378 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3379 | } else { |
| 3380 | // Just fill in the parameter types from the function declaration. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3381 | for (unsigned I = 0; I != NumParams; ++I) |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3382 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
| 3383 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3384 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3385 | // Deduce template arguments from the function parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3386 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3387 | unsigned ArgIdx = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3388 | SmallVector<OriginalCallArg, 4> OriginalCallArgs; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3389 | for (unsigned ParamIdx = 0, NumParamTypes = ParamTypes.size(); |
| 3390 | ParamIdx != NumParamTypes; ++ParamIdx) { |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3391 | QualType OrigParamType = ParamTypes[ParamIdx]; |
| 3392 | QualType ParamType = OrigParamType; |
| 3393 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3394 | const PackExpansionType *ParamExpansion |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3395 | = dyn_cast<PackExpansionType>(ParamType); |
| 3396 | if (!ParamExpansion) { |
| 3397 | // Simple case: matching a function parameter to a function argument. |
| 3398 | if (ArgIdx >= CheckArgs) |
| 3399 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3400 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3401 | Expr *Arg = Args[ArgIdx++]; |
| 3402 | QualType ArgType = Arg->getType(); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3403 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3404 | unsigned TDF = 0; |
| 3405 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, |
| 3406 | ParamType, ArgType, Arg, |
| 3407 | TDF)) |
| 3408 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3409 | |
Douglas Gregor | 0c83c81 | 2011-10-09 22:06:46 +0000 | [diff] [blame] | 3410 | // If we have nothing to deduce, we're done. |
| 3411 | if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
| 3412 | continue; |
| 3413 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3414 | // If the argument is an initializer list ... |
| 3415 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3416 | TemplateDeductionResult Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3417 | // Removing references was already done. |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3418 | if (!DeduceFromInitializerList(*this, TemplateParams, ParamType, ILE, |
| 3419 | Info, Deduced, TDF, Result)) |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3420 | continue; |
| 3421 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3422 | if (Result) |
| 3423 | return Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3424 | // Don't track the argument type, since an initializer list has none. |
| 3425 | continue; |
| 3426 | } |
| 3427 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3428 | // Keep track of the argument type and corresponding parameter index, |
| 3429 | // so we can check for compatibility between the deduced A and A. |
Douglas Gregor | 0c83c81 | 2011-10-09 22:06:46 +0000 | [diff] [blame] | 3430 | OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx-1, |
| 3431 | ArgType)); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3432 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3433 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3434 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3435 | ParamType, ArgType, |
| 3436 | Info, Deduced, TDF)) |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3437 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3438 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3439 | continue; |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3440 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3441 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3442 | // C++0x [temp.deduct.call]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3443 | // For a function parameter pack that occurs at the end of the |
| 3444 | // parameter-declaration-list, the type A of each remaining argument of |
| 3445 | // the call is compared with the type P of the declarator-id of the |
| 3446 | // function parameter pack. Each comparison deduces template arguments |
| 3447 | // for subsequent positions in the template parameter packs expanded by |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3448 | // the function parameter pack. For a function parameter pack that does |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3449 | // not occur at the end of the parameter-declaration-list, the type of |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3450 | // the parameter pack is a non-deduced context. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3451 | if (ParamIdx + 1 < NumParamTypes) |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3452 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3453 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3454 | QualType ParamPattern = ParamExpansion->getPattern(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3455 | PackDeductionScope PackScope(*this, TemplateParams, Deduced, Info, |
| 3456 | ParamPattern); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3457 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3458 | bool HasAnyArguments = false; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3459 | for (; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3460 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3461 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3462 | QualType OrigParamType = ParamPattern; |
| 3463 | ParamType = OrigParamType; |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3464 | Expr *Arg = Args[ArgIdx]; |
| 3465 | QualType ArgType = Arg->getType(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3466 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3467 | unsigned TDF = 0; |
| 3468 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, |
| 3469 | ParamType, ArgType, Arg, |
| 3470 | TDF)) { |
| 3471 | // We can't actually perform any deduction for this argument, so stop |
| 3472 | // deduction at this point. |
| 3473 | ++ArgIdx; |
| 3474 | break; |
| 3475 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3476 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3477 | // As above, initializer lists need special handling. |
| 3478 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3479 | TemplateDeductionResult Result; |
| 3480 | if (!DeduceFromInitializerList(*this, TemplateParams, ParamType, ILE, |
| 3481 | Info, Deduced, TDF, Result)) { |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3482 | ++ArgIdx; |
| 3483 | break; |
| 3484 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3485 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3486 | if (Result) |
| 3487 | return Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3488 | } else { |
| 3489 | |
| 3490 | // Keep track of the argument type and corresponding argument index, |
| 3491 | // so we can check for compatibility between the deduced A and A. |
| 3492 | if (hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
| 3493 | OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx, |
| 3494 | ArgType)); |
| 3495 | |
| 3496 | if (TemplateDeductionResult Result |
| 3497 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3498 | ParamType, ArgType, Info, |
| 3499 | Deduced, TDF)) |
| 3500 | return Result; |
| 3501 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3502 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3503 | PackScope.nextPackElement(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3504 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3505 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3506 | // Build argument packs for each of the parameter packs expanded by this |
| 3507 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3508 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3509 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3510 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3511 | // After we've matching against a parameter pack, we're done. |
| 3512 | break; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3513 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3514 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3515 | return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3516 | NumExplicitlySpecified, Specialization, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3517 | Info, &OriginalCallArgs, |
| 3518 | PartialOverloading); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3519 | } |
| 3520 | |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3521 | QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType, |
| 3522 | QualType FunctionType) { |
| 3523 | if (ArgFunctionType.isNull()) |
| 3524 | return ArgFunctionType; |
| 3525 | |
| 3526 | const FunctionProtoType *FunctionTypeP = |
| 3527 | FunctionType->castAs<FunctionProtoType>(); |
| 3528 | CallingConv CC = FunctionTypeP->getCallConv(); |
| 3529 | bool NoReturn = FunctionTypeP->getNoReturnAttr(); |
| 3530 | const FunctionProtoType *ArgFunctionTypeP = |
| 3531 | ArgFunctionType->getAs<FunctionProtoType>(); |
| 3532 | if (ArgFunctionTypeP->getCallConv() == CC && |
| 3533 | ArgFunctionTypeP->getNoReturnAttr() == NoReturn) |
| 3534 | return ArgFunctionType; |
| 3535 | |
| 3536 | FunctionType::ExtInfo EI = ArgFunctionTypeP->getExtInfo().withCallingConv(CC); |
| 3537 | EI = EI.withNoReturn(NoReturn); |
| 3538 | ArgFunctionTypeP = |
| 3539 | cast<FunctionProtoType>(Context.adjustFunctionType(ArgFunctionTypeP, EI)); |
| 3540 | return QualType(ArgFunctionTypeP, 0); |
| 3541 | } |
| 3542 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3543 | /// \brief Deduce template arguments when taking the address of a function |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3544 | /// template (C++ [temp.deduct.funcaddr]) or matching a specialization to |
| 3545 | /// a template. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3546 | /// |
| 3547 | /// \param FunctionTemplate the function template for which we are performing |
| 3548 | /// template argument deduction. |
| 3549 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3550 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3551 | /// arguments. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3552 | /// |
| 3553 | /// \param ArgFunctionType the function type that will be used as the |
| 3554 | /// "argument" type (A) when performing template argument deduction from the |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3555 | /// function template's function type. This type may be NULL, if there is no |
| 3556 | /// argument type to compare against, in C++0x [temp.arg.explicit]p3. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3557 | /// |
| 3558 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3559 | /// this will be set to the function template specialization produced by |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3560 | /// template argument deduction. |
| 3561 | /// |
| 3562 | /// \param Info the argument will be updated to provide additional information |
| 3563 | /// about template argument deduction. |
| 3564 | /// |
| 3565 | /// \returns the result of template argument deduction. |
| 3566 | Sema::TemplateDeductionResult |
| 3567 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3568 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3569 | QualType ArgFunctionType, |
| 3570 | FunctionDecl *&Specialization, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3571 | TemplateDeductionInfo &Info, |
| 3572 | bool InOverloadResolution) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3573 | if (FunctionTemplate->isInvalidDecl()) |
| 3574 | return TDK_Invalid; |
| 3575 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3576 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 3577 | TemplateParameterList *TemplateParams |
| 3578 | = FunctionTemplate->getTemplateParameters(); |
| 3579 | QualType FunctionType = Function->getType(); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3580 | if (!InOverloadResolution) |
| 3581 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3582 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3583 | // Substitute any explicit template arguments. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3584 | LocalInstantiationScope InstScope(*this); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3585 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3586 | unsigned NumExplicitlySpecified = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3587 | SmallVector<QualType, 4> ParamTypes; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3588 | if (ExplicitTemplateArgs) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3589 | if (TemplateDeductionResult Result |
| 3590 | = SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3591 | *ExplicitTemplateArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3592 | Deduced, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3593 | &FunctionType, Info)) |
| 3594 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3595 | |
| 3596 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3597 | } |
| 3598 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3599 | // Unevaluated SFINAE context. |
| 3600 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3601 | SFINAETrap Trap(*this); |
| 3602 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3603 | Deduced.resize(TemplateParams->size()); |
| 3604 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3605 | // If the function has a deduced return type, substitute it for a dependent |
| 3606 | // type so that we treat it as a non-deduced context in what follows. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3607 | bool HasDeducedReturnType = false; |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3608 | if (getLangOpts().CPlusPlus14 && InOverloadResolution && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3609 | Function->getReturnType()->getContainedAutoType()) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3610 | FunctionType = SubstAutoType(FunctionType, Context.DependentTy); |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3611 | HasDeducedReturnType = true; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3612 | } |
| 3613 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3614 | if (!ArgFunctionType.isNull()) { |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3615 | unsigned TDF = TDF_TopLevelParameterTypeList; |
| 3616 | if (InOverloadResolution) TDF |= TDF_InOverloadResolution; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3617 | // Deduce template arguments from the function type. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3618 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3619 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3620 | FunctionType, ArgFunctionType, |
| 3621 | Info, Deduced, TDF)) |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3622 | return Result; |
| 3623 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3624 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3625 | if (TemplateDeductionResult Result |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3626 | = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
| 3627 | NumExplicitlySpecified, |
| 3628 | Specialization, Info)) |
| 3629 | return Result; |
| 3630 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3631 | // If the function has a deduced return type, deduce it now, so we can check |
| 3632 | // that the deduced function type matches the requested type. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3633 | if (HasDeducedReturnType && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3634 | Specialization->getReturnType()->isUndeducedType() && |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3635 | DeduceReturnType(Specialization, Info.getLocation(), false)) |
| 3636 | return TDK_MiscellaneousDeductionFailure; |
| 3637 | |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3638 | // If the requested function type does not match the actual type of the |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3639 | // specialization with respect to arguments of compatible pointer to function |
| 3640 | // types, template argument deduction fails. |
| 3641 | if (!ArgFunctionType.isNull()) { |
| 3642 | if (InOverloadResolution && !isSameOrCompatibleFunctionType( |
| 3643 | Context.getCanonicalType(Specialization->getType()), |
| 3644 | Context.getCanonicalType(ArgFunctionType))) |
| 3645 | return TDK_MiscellaneousDeductionFailure; |
| 3646 | else if(!InOverloadResolution && |
| 3647 | !Context.hasSameType(Specialization->getType(), ArgFunctionType)) |
| 3648 | return TDK_MiscellaneousDeductionFailure; |
| 3649 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3650 | |
| 3651 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3654 | /// \brief Given a function declaration (e.g. a generic lambda conversion |
| 3655 | /// function) that contains an 'auto' in its result type, substitute it |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3656 | /// with TypeToReplaceAutoWith. Be careful to pass in the type you want |
| 3657 | /// to replace 'auto' with and not the actual result type you want |
| 3658 | /// to set the function to. |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3659 | static inline void |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3660 | SubstAutoWithinFunctionReturnType(FunctionDecl *F, |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3661 | QualType TypeToReplaceAutoWith, Sema &S) { |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3662 | assert(!TypeToReplaceAutoWith->getContainedAutoType()); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3663 | QualType AutoResultType = F->getReturnType(); |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3664 | assert(AutoResultType->getContainedAutoType()); |
| 3665 | QualType DeducedResultType = S.SubstAutoType(AutoResultType, |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3666 | TypeToReplaceAutoWith); |
| 3667 | S.Context.adjustDeducedFunctionResultType(F, DeducedResultType); |
| 3668 | } |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3669 | |
| 3670 | /// \brief Given a specialized conversion operator of a generic lambda |
| 3671 | /// create the corresponding specializations of the call operator and |
| 3672 | /// the static-invoker. If the return type of the call operator is auto, |
| 3673 | /// deduce its return type and check if that matches the |
| 3674 | /// return type of the destination function ptr. |
| 3675 | |
| 3676 | static inline Sema::TemplateDeductionResult |
| 3677 | SpecializeCorrespondingLambdaCallOperatorAndInvoker( |
| 3678 | CXXConversionDecl *ConversionSpecialized, |
| 3679 | SmallVectorImpl<DeducedTemplateArgument> &DeducedArguments, |
| 3680 | QualType ReturnTypeOfDestFunctionPtr, |
| 3681 | TemplateDeductionInfo &TDInfo, |
| 3682 | Sema &S) { |
| 3683 | |
| 3684 | CXXRecordDecl *LambdaClass = ConversionSpecialized->getParent(); |
| 3685 | assert(LambdaClass && LambdaClass->isGenericLambda()); |
| 3686 | |
| 3687 | CXXMethodDecl *CallOpGeneric = LambdaClass->getLambdaCallOperator(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3688 | QualType CallOpResultType = CallOpGeneric->getReturnType(); |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3689 | const bool GenericLambdaCallOperatorHasDeducedReturnType = |
| 3690 | CallOpResultType->getContainedAutoType(); |
| 3691 | |
| 3692 | FunctionTemplateDecl *CallOpTemplate = |
| 3693 | CallOpGeneric->getDescribedFunctionTemplate(); |
| 3694 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3695 | FunctionDecl *CallOpSpecialized = nullptr; |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3696 | // Use the deduced arguments of the conversion function, to specialize our |
| 3697 | // generic lambda's call operator. |
| 3698 | if (Sema::TemplateDeductionResult Result |
| 3699 | = S.FinishTemplateArgumentDeduction(CallOpTemplate, |
| 3700 | DeducedArguments, |
| 3701 | 0, CallOpSpecialized, TDInfo)) |
| 3702 | return Result; |
| 3703 | |
| 3704 | // If we need to deduce the return type, do so (instantiates the callop). |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3705 | if (GenericLambdaCallOperatorHasDeducedReturnType && |
| 3706 | CallOpSpecialized->getReturnType()->isUndeducedType()) |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3707 | S.DeduceReturnType(CallOpSpecialized, |
| 3708 | CallOpSpecialized->getPointOfInstantiation(), |
| 3709 | /*Diagnose*/ true); |
| 3710 | |
| 3711 | // Check to see if the return type of the destination ptr-to-function |
| 3712 | // matches the return type of the call operator. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3713 | if (!S.Context.hasSameType(CallOpSpecialized->getReturnType(), |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3714 | ReturnTypeOfDestFunctionPtr)) |
| 3715 | return Sema::TDK_NonDeducedMismatch; |
| 3716 | // Since we have succeeded in matching the source and destination |
| 3717 | // ptr-to-functions (now including return type), and have successfully |
| 3718 | // specialized our corresponding call operator, we are ready to |
| 3719 | // specialize the static invoker with the deduced arguments of our |
| 3720 | // ptr-to-function. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3721 | FunctionDecl *InvokerSpecialized = nullptr; |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3722 | FunctionTemplateDecl *InvokerTemplate = LambdaClass-> |
| 3723 | getLambdaStaticInvoker()->getDescribedFunctionTemplate(); |
| 3724 | |
Yaron Keren | f428fcf | 2015-05-13 17:56:46 +0000 | [diff] [blame] | 3725 | #ifndef NDEBUG |
| 3726 | Sema::TemplateDeductionResult LLVM_ATTRIBUTE_UNUSED Result = |
| 3727 | #endif |
| 3728 | S.FinishTemplateArgumentDeduction(InvokerTemplate, DeducedArguments, 0, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3729 | InvokerSpecialized, TDInfo); |
| 3730 | assert(Result == Sema::TDK_Success && |
| 3731 | "If the call operator succeeded so should the invoker!"); |
| 3732 | // Set the result type to match the corresponding call operator |
| 3733 | // specialization's result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3734 | if (GenericLambdaCallOperatorHasDeducedReturnType && |
| 3735 | InvokerSpecialized->getReturnType()->isUndeducedType()) { |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3736 | // Be sure to get the type to replace 'auto' with and not |
| 3737 | // the full result type of the call op specialization |
| 3738 | // to substitute into the 'auto' of the invoker and conversion |
| 3739 | // function. |
| 3740 | // For e.g. |
| 3741 | // int* (*fp)(int*) = [](auto* a) -> auto* { return a; }; |
| 3742 | // We don't want to subst 'int*' into 'auto' to get int**. |
| 3743 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3744 | QualType TypeToReplaceAutoWith = CallOpSpecialized->getReturnType() |
| 3745 | ->getContainedAutoType() |
| 3746 | ->getDeducedType(); |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3747 | SubstAutoWithinFunctionReturnType(InvokerSpecialized, |
| 3748 | TypeToReplaceAutoWith, S); |
| 3749 | SubstAutoWithinFunctionReturnType(ConversionSpecialized, |
| 3750 | TypeToReplaceAutoWith, S); |
| 3751 | } |
| 3752 | |
| 3753 | // Ensure that static invoker doesn't have a const qualifier. |
| 3754 | // FIXME: When creating the InvokerTemplate in SemaLambda.cpp |
| 3755 | // do not use the CallOperator's TypeSourceInfo which allows |
| 3756 | // the const qualifier to leak through. |
| 3757 | const FunctionProtoType *InvokerFPT = InvokerSpecialized-> |
| 3758 | getType().getTypePtr()->castAs<FunctionProtoType>(); |
| 3759 | FunctionProtoType::ExtProtoInfo EPI = InvokerFPT->getExtProtoInfo(); |
| 3760 | EPI.TypeQuals = 0; |
| 3761 | InvokerSpecialized->setType(S.Context.getFunctionType( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3762 | InvokerFPT->getReturnType(), InvokerFPT->getParamTypes(), EPI)); |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3763 | return Sema::TDK_Success; |
| 3764 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3765 | /// \brief Deduce template arguments for a templated conversion |
| 3766 | /// function (C++ [temp.deduct.conv]) and, if successful, produce a |
| 3767 | /// conversion function template specialization. |
| 3768 | Sema::TemplateDeductionResult |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3769 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *ConversionTemplate, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3770 | QualType ToType, |
| 3771 | CXXConversionDecl *&Specialization, |
| 3772 | TemplateDeductionInfo &Info) { |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3773 | if (ConversionTemplate->isInvalidDecl()) |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3774 | return TDK_Invalid; |
| 3775 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3776 | CXXConversionDecl *ConversionGeneric |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3777 | = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl()); |
| 3778 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3779 | QualType FromType = ConversionGeneric->getConversionType(); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3780 | |
| 3781 | // Canonicalize the types for deduction. |
| 3782 | QualType P = Context.getCanonicalType(FromType); |
| 3783 | QualType A = Context.getCanonicalType(ToType); |
| 3784 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3785 | // C++0x [temp.deduct.conv]p2: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3786 | // If P is a reference type, the type referred to by P is used for |
| 3787 | // type deduction. |
| 3788 | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
| 3789 | P = PRef->getPointeeType(); |
| 3790 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3791 | // C++0x [temp.deduct.conv]p4: |
| 3792 | // [...] If A is a reference type, the type referred to by A is used |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3793 | // for type deduction. |
| 3794 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3795 | A = ARef->getPointeeType().getUnqualifiedType(); |
| 3796 | // C++ [temp.deduct.conv]p3: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3797 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3798 | // If A is not a reference type: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3799 | else { |
| 3800 | assert(!A->isReferenceType() && "Reference types were handled above"); |
| 3801 | |
| 3802 | // - If P is an array type, the pointer type produced by the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3803 | // array-to-pointer standard conversion (4.2) is used in place |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3804 | // of P for type deduction; otherwise, |
| 3805 | if (P->isArrayType()) |
| 3806 | P = Context.getArrayDecayedType(P); |
| 3807 | // - If P is a function type, the pointer type produced by the |
| 3808 | // function-to-pointer standard conversion (4.3) is used in |
| 3809 | // place of P for type deduction; otherwise, |
| 3810 | else if (P->isFunctionType()) |
| 3811 | P = Context.getPointerType(P); |
| 3812 | // - If P is a cv-qualified type, the top level cv-qualifiers of |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3813 | // P's type are ignored for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3814 | else |
| 3815 | P = P.getUnqualifiedType(); |
| 3816 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3817 | // C++0x [temp.deduct.conv]p4: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3818 | // If A is a cv-qualified type, the top level cv-qualifiers of A's |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3819 | // type are ignored for type deduction. If A is a reference type, the type |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3820 | // referred to by A is used for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3821 | A = A.getUnqualifiedType(); |
| 3822 | } |
| 3823 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3824 | // Unevaluated SFINAE context. |
| 3825 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3826 | SFINAETrap Trap(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3827 | |
| 3828 | // C++ [temp.deduct.conv]p1: |
| 3829 | // Template argument deduction is done by comparing the return |
| 3830 | // type of the template conversion function (call it P) with the |
| 3831 | // type that is required as the result of the conversion (call it |
| 3832 | // A) as described in 14.8.2.4. |
| 3833 | TemplateParameterList *TemplateParams |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3834 | = ConversionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3835 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3836 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3837 | |
| 3838 | // C++0x [temp.deduct.conv]p4: |
| 3839 | // In general, the deduction process attempts to find template |
| 3840 | // argument values that will make the deduced A identical to |
| 3841 | // A. However, there are two cases that allow a difference: |
| 3842 | unsigned TDF = 0; |
| 3843 | // - If the original A is a reference type, A can be more |
| 3844 | // cv-qualified than the deduced A (i.e., the type referred to |
| 3845 | // by the reference) |
| 3846 | if (ToType->isReferenceType()) |
| 3847 | TDF |= TDF_ParamWithReferenceType; |
| 3848 | // - The deduced A can be another pointer or pointer to member |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3849 | // type that can be converted to A via a qualification |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3850 | // conversion. |
| 3851 | // |
| 3852 | // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when |
| 3853 | // both P and A are pointers or member pointers. In this case, we |
| 3854 | // just ignore cv-qualifiers completely). |
| 3855 | if ((P->isPointerType() && A->isPointerType()) || |
Douglas Gregor | 9f05ed5 | 2011-08-30 00:37:54 +0000 | [diff] [blame] | 3856 | (P->isMemberPointerType() && A->isMemberPointerType())) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3857 | TDF |= TDF_IgnoreQualifiers; |
| 3858 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3859 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3860 | P, A, Info, Deduced, TDF)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3861 | return Result; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3862 | |
| 3863 | // Create an Instantiation Scope for finalizing the operator. |
| 3864 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3865 | // Finish template argument deduction. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3866 | FunctionDecl *ConversionSpecialized = nullptr; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3867 | TemplateDeductionResult Result |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3868 | = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0, |
| 3869 | ConversionSpecialized, Info); |
| 3870 | Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized); |
| 3871 | |
| 3872 | // If the conversion operator is being invoked on a lambda closure to convert |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3873 | // to a ptr-to-function, use the deduced arguments from the conversion |
| 3874 | // function to specialize the corresponding call operator. |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3875 | // e.g., int (*fp)(int) = [](auto a) { return a; }; |
| 3876 | if (Result == TDK_Success && isLambdaConversionOperator(ConversionGeneric)) { |
| 3877 | |
| 3878 | // Get the return type of the destination ptr-to-function we are converting |
| 3879 | // to. This is necessary for matching the lambda call operator's return |
| 3880 | // type to that of the destination ptr-to-function's return type. |
| 3881 | assert(A->isPointerType() && |
| 3882 | "Can only convert from lambda to ptr-to-function"); |
| 3883 | const FunctionType *ToFunType = |
| 3884 | A->getPointeeType().getTypePtr()->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3885 | const QualType DestFunctionPtrReturnType = ToFunType->getReturnType(); |
| 3886 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3887 | // Create the corresponding specializations of the call operator and |
| 3888 | // the static-invoker; and if the return type is auto, |
| 3889 | // deduce the return type and check if it matches the |
| 3890 | // DestFunctionPtrReturnType. |
| 3891 | // For instance: |
| 3892 | // auto L = [](auto a) { return f(a); }; |
| 3893 | // int (*fp)(int) = L; |
| 3894 | // char (*fp2)(int) = L; <-- Not OK. |
| 3895 | |
| 3896 | Result = SpecializeCorrespondingLambdaCallOperatorAndInvoker( |
| 3897 | Specialization, Deduced, DestFunctionPtrReturnType, |
| 3898 | Info, *this); |
| 3899 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3900 | return Result; |
| 3901 | } |
| 3902 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3903 | /// \brief Deduce template arguments for a function template when there is |
| 3904 | /// nothing to deduce against (C++0x [temp.arg.explicit]p3). |
| 3905 | /// |
| 3906 | /// \param FunctionTemplate the function template for which we are performing |
| 3907 | /// template argument deduction. |
| 3908 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3909 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3910 | /// arguments. |
| 3911 | /// |
| 3912 | /// \param Specialization if template argument deduction was successful, |
| 3913 | /// this will be set to the function template specialization produced by |
| 3914 | /// template argument deduction. |
| 3915 | /// |
| 3916 | /// \param Info the argument will be updated to provide additional information |
| 3917 | /// about template argument deduction. |
| 3918 | /// |
| 3919 | /// \returns the result of template argument deduction. |
| 3920 | Sema::TemplateDeductionResult |
| 3921 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3922 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3923 | FunctionDecl *&Specialization, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3924 | TemplateDeductionInfo &Info, |
| 3925 | bool InOverloadResolution) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3926 | return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3927 | QualType(), Specialization, Info, |
| 3928 | InOverloadResolution); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3931 | namespace { |
| 3932 | /// Substitute the 'auto' type specifier within a type for a given replacement |
| 3933 | /// type. |
| 3934 | class SubstituteAutoTransform : |
| 3935 | public TreeTransform<SubstituteAutoTransform> { |
| 3936 | QualType Replacement; |
| 3937 | public: |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3938 | SubstituteAutoTransform(Sema &SemaRef, QualType Replacement) |
| 3939 | : TreeTransform<SubstituteAutoTransform>(SemaRef), |
| 3940 | Replacement(Replacement) {} |
| 3941 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3942 | QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) { |
| 3943 | // If we're building the type pattern to deduce against, don't wrap the |
| 3944 | // substituted type in an AutoType. Certain template deduction rules |
| 3945 | // apply only when a template type parameter appears directly (and not if |
| 3946 | // the parameter is found through desugaring). For instance: |
| 3947 | // auto &&lref = lvalue; |
| 3948 | // must transform into "rvalue reference to T" not "rvalue reference to |
| 3949 | // auto type deduced as T" in order for [temp.deduct.call]p3 to apply. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3950 | if (!Replacement.isNull() && isa<TemplateTypeParmType>(Replacement)) { |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3951 | QualType Result = Replacement; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 3952 | TemplateTypeParmTypeLoc NewTL = |
| 3953 | TLB.push<TemplateTypeParmTypeLoc>(Result); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3954 | NewTL.setNameLoc(TL.getNameLoc()); |
| 3955 | return Result; |
| 3956 | } else { |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 3957 | bool Dependent = |
| 3958 | !Replacement.isNull() && Replacement->isDependentType(); |
| 3959 | QualType Result = |
| 3960 | SemaRef.Context.getAutoType(Dependent ? QualType() : Replacement, |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 3961 | TL.getTypePtr()->getKeyword(), |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3962 | Dependent); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3963 | AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result); |
| 3964 | NewTL.setNameLoc(TL.getNameLoc()); |
| 3965 | return Result; |
| 3966 | } |
| 3967 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 3968 | |
| 3969 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 3970 | // Lambdas never need to be transformed. |
| 3971 | return E; |
| 3972 | } |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3973 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3974 | QualType Apply(TypeLoc TL) { |
| 3975 | // Create some scratch storage for the transformed type locations. |
| 3976 | // FIXME: We're just going to throw this information away. Don't build it. |
| 3977 | TypeLocBuilder TLB; |
| 3978 | TLB.reserve(TL.getFullDataSize()); |
| 3979 | return TransformType(TLB, TL); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3980 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3981 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3982 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3983 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3984 | Sema::DeduceAutoResult |
| 3985 | Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *&Init, QualType &Result) { |
| 3986 | return DeduceAutoType(Type->getTypeLoc(), Init, Result); |
| 3987 | } |
| 3988 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3989 | /// \brief Deduce the type for an auto type-specifier (C++11 [dcl.spec.auto]p6) |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3990 | /// |
| 3991 | /// \param Type the type pattern using the auto type-specifier. |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3992 | /// \param Init the initializer for the variable whose type is to be deduced. |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3993 | /// \param Result if type deduction was successful, this will be set to the |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3994 | /// deduced type. |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 3995 | Sema::DeduceAutoResult |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3996 | Sema::DeduceAutoType(TypeLoc Type, Expr *&Init, QualType &Result) { |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 3997 | if (Init->getType()->isNonOverloadPlaceholderType()) { |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3998 | ExprResult NonPlaceholder = CheckPlaceholderExpr(Init); |
| 3999 | if (NonPlaceholder.isInvalid()) |
| 4000 | return DAR_FailedAlreadyDiagnosed; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4001 | Init = NonPlaceholder.get(); |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4004 | if (Init->isTypeDependent() || Type.getType()->isDependentType()) { |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4005 | Result = SubstituteAutoTransform(*this, Context.DependentTy).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4006 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4007 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4008 | } |
| 4009 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4010 | // If this is a 'decltype(auto)' specifier, do the decltype dance. |
| 4011 | // Since 'decltype(auto)' can only occur at the top of the type, we |
| 4012 | // don't need to go digging for it. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4013 | if (const AutoType *AT = Type.getType()->getAs<AutoType>()) { |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4014 | if (AT->isDecltypeAuto()) { |
| 4015 | if (isa<InitListExpr>(Init)) { |
| 4016 | Diag(Init->getLocStart(), diag::err_decltype_auto_initializer_list); |
| 4017 | return DAR_FailedAlreadyDiagnosed; |
| 4018 | } |
| 4019 | |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 4020 | QualType Deduced = BuildDecltypeType(Init, Init->getLocStart(), false); |
David Majnemer | 3c20ab2 | 2015-07-01 00:29:28 +0000 | [diff] [blame] | 4021 | if (Deduced.isNull()) |
| 4022 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4023 | // FIXME: Support a non-canonical deduced type for 'auto'. |
| 4024 | Deduced = Context.getCanonicalType(Deduced); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4025 | Result = SubstituteAutoTransform(*this, Deduced).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4026 | if (Result.isNull()) |
| 4027 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4028 | return DAR_Succeeded; |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4029 | } else if (!getLangOpts().CPlusPlus) { |
| 4030 | if (isa<InitListExpr>(Init)) { |
| 4031 | Diag(Init->getLocStart(), diag::err_auto_init_list_from_c); |
| 4032 | return DAR_FailedAlreadyDiagnosed; |
| 4033 | } |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4034 | } |
| 4035 | } |
| 4036 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4037 | SourceLocation Loc = Init->getExprLoc(); |
| 4038 | |
| 4039 | LocalInstantiationScope InstScope(*this); |
| 4040 | |
| 4041 | // Build template<class TemplParam> void Func(FuncParam); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4042 | TemplateTypeParmDecl *TemplParam = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4043 | TemplateTypeParmDecl::Create(Context, nullptr, SourceLocation(), Loc, 0, 0, |
| 4044 | nullptr, false, false); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4045 | QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0); |
| 4046 | NamedDecl *TemplParamPtr = TemplParam; |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4047 | FixedSizeTemplateParameterListStorage<1> TemplateParamsSt( |
| 4048 | Loc, Loc, &TemplParamPtr, Loc); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 4049 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4050 | QualType FuncParam = SubstituteAutoTransform(*this, TemplArg).Apply(Type); |
| 4051 | assert(!FuncParam.isNull() && |
| 4052 | "substituting template parameter for 'auto' failed"); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4053 | |
| 4054 | // Deduce type of TemplParam in Func(Init) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4055 | SmallVector<DeducedTemplateArgument, 1> Deduced; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4056 | Deduced.resize(1); |
| 4057 | QualType InitType = Init->getType(); |
| 4058 | unsigned TDF = 0; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4059 | |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4060 | TemplateDeductionInfo Info(Loc); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4061 | |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4062 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4063 | if (InitList) { |
| 4064 | for (unsigned i = 0, e = InitList->getNumInits(); i < e; ++i) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4065 | if (DeduceTemplateArgumentByListElement(*this, TemplateParamsSt.get(), |
| 4066 | TemplArg, InitList->getInit(i), |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 4067 | Info, Deduced, TDF)) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4068 | return DAR_Failed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4069 | } |
| 4070 | } else { |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4071 | if (!getLangOpts().CPlusPlus && Init->refersToBitField()) { |
| 4072 | Diag(Loc, diag::err_auto_bitfield); |
| 4073 | return DAR_FailedAlreadyDiagnosed; |
| 4074 | } |
| 4075 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4076 | if (AdjustFunctionParmAndArgTypesForDeduction( |
| 4077 | *this, TemplateParamsSt.get(), FuncParam, InitType, Init, TDF)) |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 4078 | return DAR_Failed; |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4079 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4080 | if (DeduceTemplateArgumentsByTypeMatch(*this, TemplateParamsSt.get(), |
| 4081 | FuncParam, InitType, Info, Deduced, |
| 4082 | TDF)) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4083 | return DAR_Failed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4084 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4085 | |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4086 | if (Deduced[0].getKind() != TemplateArgument::Type) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4087 | return DAR_Failed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4088 | |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4089 | QualType DeducedType = Deduced[0].getAsType(); |
| 4090 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4091 | if (InitList) { |
| 4092 | DeducedType = BuildStdInitializerList(DeducedType, Loc); |
| 4093 | if (DeducedType.isNull()) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4094 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4097 | Result = SubstituteAutoTransform(*this, DeducedType).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4098 | if (Result.isNull()) |
| 4099 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4100 | |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4101 | // Check that the deduced argument type is compatible with the original |
| 4102 | // argument type per C++ [temp.deduct.call]p4. |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4103 | if (!InitList && !Result.isNull() && |
| 4104 | CheckOriginalCallArgDeduction(*this, |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4105 | Sema::OriginalCallArg(FuncParam,0,InitType), |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4106 | Result)) { |
| 4107 | Result = QualType(); |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4108 | return DAR_Failed; |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4109 | } |
| 4110 | |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4111 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4114 | QualType Sema::SubstAutoType(QualType TypeWithAuto, |
| 4115 | QualType TypeToReplaceAuto) { |
| 4116 | return SubstituteAutoTransform(*this, TypeToReplaceAuto). |
| 4117 | TransformType(TypeWithAuto); |
| 4118 | } |
| 4119 | |
| 4120 | TypeSourceInfo* Sema::SubstAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, |
| 4121 | QualType TypeToReplaceAuto) { |
| 4122 | return SubstituteAutoTransform(*this, TypeToReplaceAuto). |
| 4123 | TransformType(TypeWithAuto); |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4126 | void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) { |
| 4127 | if (isa<InitListExpr>(Init)) |
| 4128 | Diag(VDecl->getLocation(), |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4129 | VDecl->isInitCapture() |
| 4130 | ? diag::err_init_capture_deduction_failure_from_init_list |
| 4131 | : diag::err_auto_var_deduction_failure_from_init_list) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4132 | << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange(); |
| 4133 | else |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4134 | Diag(VDecl->getLocation(), |
| 4135 | VDecl->isInitCapture() ? diag::err_init_capture_deduction_failure |
| 4136 | : diag::err_auto_var_deduction_failure) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4137 | << VDecl->getDeclName() << VDecl->getType() << Init->getType() |
| 4138 | << Init->getSourceRange(); |
| 4139 | } |
| 4140 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4141 | bool Sema::DeduceReturnType(FunctionDecl *FD, SourceLocation Loc, |
| 4142 | bool Diagnose) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4143 | assert(FD->getReturnType()->isUndeducedType()); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4144 | |
| 4145 | if (FD->getTemplateInstantiationPattern()) |
| 4146 | InstantiateFunctionDefinition(Loc, FD); |
| 4147 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4148 | bool StillUndeduced = FD->getReturnType()->isUndeducedType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4149 | if (StillUndeduced && Diagnose && !FD->isInvalidDecl()) { |
| 4150 | Diag(Loc, diag::err_auto_fn_used_before_defined) << FD; |
| 4151 | Diag(FD->getLocation(), diag::note_callee_decl) << FD; |
| 4152 | } |
| 4153 | |
| 4154 | return StillUndeduced; |
| 4155 | } |
| 4156 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4157 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4158 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4159 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4160 | unsigned Level, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4161 | llvm::SmallBitVector &Deduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4162 | |
| 4163 | /// \brief If this is a non-static member function, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 4164 | static void |
| 4165 | AddImplicitObjectParameterType(ASTContext &Context, |
| 4166 | CXXMethodDecl *Method, |
| 4167 | SmallVectorImpl<QualType> &ArgTypes) { |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4168 | // C++11 [temp.func.order]p3: |
| 4169 | // [...] The new parameter is of type "reference to cv A," where cv are |
| 4170 | // the cv-qualifiers of the function template (if any) and A is |
| 4171 | // the class of which the function template is a member. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4172 | // |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4173 | // The standard doesn't say explicitly, but we pick the appropriate kind of |
| 4174 | // reference type based on [over.match.funcs]p4. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4175 | QualType ArgTy = Context.getTypeDeclType(Method->getParent()); |
| 4176 | ArgTy = Context.getQualifiedType(ArgTy, |
| 4177 | Qualifiers::fromCVRMask(Method->getTypeQualifiers())); |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4178 | if (Method->getRefQualifier() == RQ_RValue) |
| 4179 | ArgTy = Context.getRValueReferenceType(ArgTy); |
| 4180 | else |
| 4181 | ArgTy = Context.getLValueReferenceType(ArgTy); |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4182 | ArgTypes.push_back(ArgTy); |
| 4183 | } |
| 4184 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4185 | /// \brief Determine whether the function template \p FT1 is at least as |
| 4186 | /// specialized as \p FT2. |
| 4187 | static bool isAtLeastAsSpecializedAs(Sema &S, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4188 | SourceLocation Loc, |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4189 | FunctionTemplateDecl *FT1, |
| 4190 | FunctionTemplateDecl *FT2, |
| 4191 | TemplatePartialOrderingContext TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4192 | unsigned NumCallArguments1) { |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4193 | FunctionDecl *FD1 = FT1->getTemplatedDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4194 | FunctionDecl *FD2 = FT2->getTemplatedDecl(); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4195 | const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); |
| 4196 | const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4197 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4198 | assert(Proto1 && Proto2 && "Function templates must have prototypes"); |
| 4199 | TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4200 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4201 | Deduced.resize(TemplateParams->size()); |
| 4202 | |
| 4203 | // C++0x [temp.deduct.partial]p3: |
| 4204 | // The types used to determine the ordering depend on the context in which |
| 4205 | // the partial ordering is done: |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4206 | TemplateDeductionInfo Info(Loc); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4207 | SmallVector<QualType, 4> Args2; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4208 | switch (TPOC) { |
| 4209 | case TPOC_Call: { |
| 4210 | // - In the context of a function call, the function parameter types are |
| 4211 | // used. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4212 | CXXMethodDecl *Method1 = dyn_cast<CXXMethodDecl>(FD1); |
| 4213 | CXXMethodDecl *Method2 = dyn_cast<CXXMethodDecl>(FD2); |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4214 | |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4215 | // C++11 [temp.func.order]p3: |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4216 | // [...] If only one of the function templates is a non-static |
| 4217 | // member, that function template is considered to have a new |
| 4218 | // first parameter inserted in its function parameter list. The |
| 4219 | // new parameter is of type "reference to cv A," where cv are |
| 4220 | // the cv-qualifiers of the function template (if any) and A is |
| 4221 | // the class of which the function template is a member. |
| 4222 | // |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4223 | // Note that we interpret this to mean "if one of the function |
| 4224 | // templates is a non-static member and the other is a non-member"; |
| 4225 | // otherwise, the ordering rules for static functions against non-static |
| 4226 | // functions don't make any sense. |
| 4227 | // |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4228 | // C++98/03 doesn't have this provision but we've extended DR532 to cover |
| 4229 | // it as wording was broken prior to it. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4230 | SmallVector<QualType, 4> Args1; |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4231 | |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4232 | unsigned NumComparedArguments = NumCallArguments1; |
| 4233 | |
| 4234 | if (!Method2 && Method1 && !Method1->isStatic()) { |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4235 | // Compare 'this' from Method1 against first parameter from Method2. |
| 4236 | AddImplicitObjectParameterType(S.Context, Method1, Args1); |
| 4237 | ++NumComparedArguments; |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4238 | } else if (!Method1 && Method2 && !Method2->isStatic()) { |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4239 | // Compare 'this' from Method2 against first parameter from Method1. |
| 4240 | AddImplicitObjectParameterType(S.Context, Method2, Args2); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4243 | Args1.insert(Args1.end(), Proto1->param_type_begin(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4244 | Proto1->param_type_end()); |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4245 | Args2.insert(Args2.end(), Proto2->param_type_begin(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4246 | Proto2->param_type_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4247 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4248 | // C++ [temp.func.order]p5: |
| 4249 | // The presence of unused ellipsis and default arguments has no effect on |
| 4250 | // the partial ordering of function templates. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4251 | if (Args1.size() > NumComparedArguments) |
| 4252 | Args1.resize(NumComparedArguments); |
| 4253 | if (Args2.size() > NumComparedArguments) |
| 4254 | Args2.resize(NumComparedArguments); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4255 | if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(), |
| 4256 | Args1.data(), Args1.size(), Info, Deduced, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4257 | TDF_None, /*PartialOrdering=*/true)) |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 4258 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4259 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4260 | break; |
| 4261 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4262 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4263 | case TPOC_Conversion: |
| 4264 | // - In the context of a call to a conversion operator, the return types |
| 4265 | // of the conversion function templates are used. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4266 | if (DeduceTemplateArgumentsByTypeMatch( |
| 4267 | S, TemplateParams, Proto2->getReturnType(), Proto1->getReturnType(), |
| 4268 | Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4269 | /*PartialOrdering=*/true)) |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4270 | return false; |
| 4271 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4272 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4273 | case TPOC_Other: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4274 | // - In other contexts (14.6.6.2) the function template's function type |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4275 | // is used. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4276 | if (DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 4277 | FD2->getType(), FD1->getType(), |
| 4278 | Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4279 | /*PartialOrdering=*/true)) |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4280 | return false; |
| 4281 | break; |
| 4282 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4283 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4284 | // C++0x [temp.deduct.partial]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4285 | // In most cases, all template parameters must have values in order for |
| 4286 | // deduction to succeed, but for partial ordering purposes a template |
| 4287 | // parameter may remain without a value provided it is not used in the |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4288 | // types being used for partial ordering. [ Note: a template parameter used |
| 4289 | // in a non-deduced context is considered used. -end note] |
| 4290 | unsigned ArgIdx = 0, NumArgs = Deduced.size(); |
| 4291 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4292 | if (Deduced[ArgIdx].isNull()) |
| 4293 | break; |
| 4294 | |
| 4295 | if (ArgIdx == NumArgs) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4296 | // All template arguments were deduced. FT1 is at least as specialized |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4297 | // as FT2. |
| 4298 | return true; |
| 4299 | } |
| 4300 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4301 | // Figure out which template parameters were used. |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4302 | llvm::SmallBitVector UsedParameters(TemplateParams->size()); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4303 | switch (TPOC) { |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4304 | case TPOC_Call: |
| 4305 | for (unsigned I = 0, N = Args2.size(); I != N; ++I) |
| 4306 | ::MarkUsedTemplateParameters(S.Context, Args2[I], false, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4307 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4308 | UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4309 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4310 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4311 | case TPOC_Conversion: |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4312 | ::MarkUsedTemplateParameters(S.Context, Proto2->getReturnType(), false, |
| 4313 | TemplateParams->getDepth(), UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4314 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4315 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4316 | case TPOC_Other: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4317 | ::MarkUsedTemplateParameters(S.Context, FD2->getType(), false, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4318 | TemplateParams->getDepth(), |
| 4319 | UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4320 | break; |
| 4321 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4322 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4323 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4324 | // If this argument had no value deduced but was used in one of the types |
| 4325 | // used for partial ordering, then deduction fails. |
| 4326 | if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) |
| 4327 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4328 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4329 | return true; |
| 4330 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4331 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4332 | /// \brief Determine whether this a function template whose parameter-type-list |
| 4333 | /// ends with a function parameter pack. |
| 4334 | static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) { |
| 4335 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
| 4336 | unsigned NumParams = Function->getNumParams(); |
| 4337 | if (NumParams == 0) |
| 4338 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4339 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4340 | ParmVarDecl *Last = Function->getParamDecl(NumParams - 1); |
| 4341 | if (!Last->isParameterPack()) |
| 4342 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4343 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4344 | // Make sure that no previous parameter is a parameter pack. |
| 4345 | while (--NumParams > 0) { |
| 4346 | if (Function->getParamDecl(NumParams - 1)->isParameterPack()) |
| 4347 | return false; |
| 4348 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4349 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4350 | return true; |
| 4351 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4352 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4353 | /// \brief Returns the more specialized function template according |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4354 | /// to the rules of function template partial ordering (C++ [temp.func.order]). |
| 4355 | /// |
| 4356 | /// \param FT1 the first function template |
| 4357 | /// |
| 4358 | /// \param FT2 the second function template |
| 4359 | /// |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4360 | /// \param TPOC the context in which we are performing partial ordering of |
| 4361 | /// function templates. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4362 | /// |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4363 | /// \param NumCallArguments1 The number of arguments in the call to FT1, used |
| 4364 | /// only when \c TPOC is \c TPOC_Call. |
| 4365 | /// |
| 4366 | /// \param NumCallArguments2 The number of arguments in the call to FT2, used |
| 4367 | /// only when \c TPOC is \c TPOC_Call. |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4368 | /// |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4369 | /// \returns the more specialized function template. If neither |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4370 | /// template is more specialized, returns NULL. |
| 4371 | FunctionTemplateDecl * |
| 4372 | Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, |
| 4373 | FunctionTemplateDecl *FT2, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4374 | SourceLocation Loc, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4375 | TemplatePartialOrderingContext TPOC, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4376 | unsigned NumCallArguments1, |
| 4377 | unsigned NumCallArguments2) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4378 | bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4379 | NumCallArguments1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4380 | bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4381 | NumCallArguments2); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4382 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4383 | if (Better1 != Better2) // We have a clear winner |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4384 | return Better1 ? FT1 : FT2; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4385 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4386 | if (!Better1 && !Better2) // Neither is better than the other |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4387 | return nullptr; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4388 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4389 | // FIXME: This mimics what GCC implements, but doesn't match up with the |
| 4390 | // proposed resolution for core issue 692. This area needs to be sorted out, |
| 4391 | // but for now we attempt to maintain compatibility. |
| 4392 | bool Variadic1 = isVariadicFunctionTemplate(FT1); |
| 4393 | bool Variadic2 = isVariadicFunctionTemplate(FT2); |
| 4394 | if (Variadic1 != Variadic2) |
| 4395 | return Variadic1? FT2 : FT1; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4396 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4397 | return nullptr; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4398 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4399 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4400 | /// \brief Determine if the two templates are equivalent. |
| 4401 | static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { |
| 4402 | if (T1 == T2) |
| 4403 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4404 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4405 | if (!T1 || !T2) |
| 4406 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4407 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4408 | return T1->getCanonicalDecl() == T2->getCanonicalDecl(); |
| 4409 | } |
| 4410 | |
| 4411 | /// \brief Retrieve the most specialized of the given function template |
| 4412 | /// specializations. |
| 4413 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4414 | /// \param SpecBegin the start iterator of the function template |
| 4415 | /// specializations that we will be comparing. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4416 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4417 | /// \param SpecEnd the end iterator of the function template |
| 4418 | /// specializations, paired with \p SpecBegin. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4419 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4420 | /// \param Loc the location where the ambiguity or no-specializations |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4421 | /// diagnostic should occur. |
| 4422 | /// |
| 4423 | /// \param NoneDiag partial diagnostic used to diagnose cases where there are |
| 4424 | /// no matching candidates. |
| 4425 | /// |
| 4426 | /// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one |
| 4427 | /// occurs. |
| 4428 | /// |
| 4429 | /// \param CandidateDiag partial diagnostic used for each function template |
| 4430 | /// specialization that is a candidate in the ambiguous ordering. One parameter |
| 4431 | /// in this diagnostic should be unbound, which will correspond to the string |
| 4432 | /// describing the template arguments for the function template specialization. |
| 4433 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4434 | /// \returns the most specialized function template specialization, if |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4435 | /// found. Otherwise, returns SpecEnd. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4436 | UnresolvedSetIterator Sema::getMostSpecialized( |
| 4437 | UnresolvedSetIterator SpecBegin, UnresolvedSetIterator SpecEnd, |
| 4438 | TemplateSpecCandidateSet &FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4439 | SourceLocation Loc, const PartialDiagnostic &NoneDiag, |
| 4440 | const PartialDiagnostic &AmbigDiag, const PartialDiagnostic &CandidateDiag, |
| 4441 | bool Complain, QualType TargetType) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4442 | if (SpecBegin == SpecEnd) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4443 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4444 | Diag(Loc, NoneDiag); |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4445 | FailedCandidates.NoteCandidates(*this, Loc); |
| 4446 | } |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4447 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4448 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4449 | |
| 4450 | if (SpecBegin + 1 == SpecEnd) |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4451 | return SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4452 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4453 | // Find the function template that is better than all of the templates it |
| 4454 | // has been compared to. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4455 | UnresolvedSetIterator Best = SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4456 | FunctionTemplateDecl *BestTemplate |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4457 | = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4458 | assert(BestTemplate && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4459 | for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { |
| 4460 | FunctionTemplateDecl *Challenger |
| 4461 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4462 | assert(Challenger && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4463 | if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4464 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4465 | Challenger)) { |
| 4466 | Best = I; |
| 4467 | BestTemplate = Challenger; |
| 4468 | } |
| 4469 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4470 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4471 | // Make sure that the "best" function template is more specialized than all |
| 4472 | // of the others. |
| 4473 | bool Ambiguous = false; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4474 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4475 | FunctionTemplateDecl *Challenger |
| 4476 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4477 | if (I != Best && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4478 | !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4479 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4480 | BestTemplate)) { |
| 4481 | Ambiguous = true; |
| 4482 | break; |
| 4483 | } |
| 4484 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4485 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4486 | if (!Ambiguous) { |
| 4487 | // We found an answer. Return it. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4488 | return Best; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4489 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4490 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4491 | // Diagnose the ambiguity. |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4492 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4493 | Diag(Loc, AmbigDiag); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4494 | |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4495 | // FIXME: Can we order the candidates in some sane way? |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4496 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4497 | PartialDiagnostic PD = CandidateDiag; |
| 4498 | PD << getTemplateArgumentBindingsText( |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4499 | cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(), |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4500 | *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs()); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4501 | if (!TargetType.isNull()) |
| 4502 | HandleFunctionTypeMismatch(PD, cast<FunctionDecl>(*I)->getType(), |
| 4503 | TargetType); |
| 4504 | Diag((*I)->getLocation(), PD); |
| 4505 | } |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4506 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4507 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4508 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4509 | } |
| 4510 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4511 | /// \brief Returns the more specialized class template partial specialization |
| 4512 | /// according to the rules of partial ordering of class template partial |
| 4513 | /// specializations (C++ [temp.class.order]). |
| 4514 | /// |
| 4515 | /// \param PS1 the first class template partial specialization |
| 4516 | /// |
| 4517 | /// \param PS2 the second class template partial specialization |
| 4518 | /// |
| 4519 | /// \returns the more specialized class template partial specialization. If |
| 4520 | /// neither partial specialization is more specialized, returns NULL. |
| 4521 | ClassTemplatePartialSpecializationDecl * |
| 4522 | Sema::getMoreSpecializedPartialSpecialization( |
| 4523 | ClassTemplatePartialSpecializationDecl *PS1, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4524 | ClassTemplatePartialSpecializationDecl *PS2, |
| 4525 | SourceLocation Loc) { |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4526 | // C++ [temp.class.order]p1: |
| 4527 | // For two class template partial specializations, the first is at least as |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4528 | // specialized as the second if, given the following rewrite to two |
| 4529 | // function templates, the first function template is at least as |
| 4530 | // specialized as the second according to the ordering rules for function |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4531 | // templates (14.6.6.2): |
| 4532 | // - the first function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4533 | // first partial specialization and has a single function parameter |
| 4534 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4535 | // arguments of the first partial specialization, and |
| 4536 | // - the second function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4537 | // second partial specialization and has a single function parameter |
| 4538 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4539 | // arguments of the second partial specialization. |
| 4540 | // |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 4541 | // Rather than synthesize function templates, we merely perform the |
| 4542 | // equivalent partial ordering by performing deduction directly on |
| 4543 | // the template arguments of the class template partial |
| 4544 | // specializations. This computation is slightly simpler than the |
| 4545 | // general problem of function template partial ordering, because |
| 4546 | // class template partial specializations are more constrained. We |
| 4547 | // know that every template parameter is deducible from the class |
| 4548 | // template partial specialization's template arguments, for |
| 4549 | // example. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4550 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4551 | TemplateDeductionInfo Info(Loc); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4552 | |
| 4553 | QualType PT1 = PS1->getInjectedSpecializationType(); |
| 4554 | QualType PT2 = PS2->getInjectedSpecializationType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4555 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4556 | // Determine whether PS1 is at least as specialized as PS2 |
| 4557 | Deduced.resize(PS2->getTemplateParameters()->size()); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4558 | bool Better1 = !DeduceTemplateArgumentsByTypeMatch(*this, |
| 4559 | PS2->getTemplateParameters(), |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4560 | PT2, PT1, Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4561 | /*PartialOrdering=*/true); |
Argyrios Kyrtzidis | 7d39155 | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4562 | if (Better1) { |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4563 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(),Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 4564 | InstantiatingTemplate Inst(*this, Loc, PS2, DeducedArgs, Info); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4565 | Better1 = !::FinishTemplateArgumentDeduction( |
| 4566 | *this, PS2, PS1->getTemplateArgs(), Deduced, Info); |
| 4567 | } |
| 4568 | |
| 4569 | // Determine whether PS2 is at least as specialized as PS1 |
| 4570 | Deduced.clear(); |
| 4571 | Deduced.resize(PS1->getTemplateParameters()->size()); |
| 4572 | bool Better2 = !DeduceTemplateArgumentsByTypeMatch( |
| 4573 | *this, PS1->getTemplateParameters(), PT1, PT2, Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4574 | /*PartialOrdering=*/true); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4575 | if (Better2) { |
| 4576 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
| 4577 | Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 4578 | InstantiatingTemplate Inst(*this, Loc, PS1, DeducedArgs, Info); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4579 | Better2 = !::FinishTemplateArgumentDeduction( |
| 4580 | *this, PS1, PS2->getTemplateArgs(), Deduced, Info); |
| 4581 | } |
| 4582 | |
| 4583 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4584 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4585 | |
| 4586 | return Better1 ? PS1 : PS2; |
| 4587 | } |
| 4588 | |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 4589 | /// TODO: Unify with ClassTemplatePartialSpecializationDecl version? |
| 4590 | /// May require unifying ClassTemplate(Partial)SpecializationDecl and |
| 4591 | /// VarTemplate(Partial)SpecializationDecl with a new data |
| 4592 | /// structure Template(Partial)SpecializationDecl, and |
| 4593 | /// using Template(Partial)SpecializationDecl as input type. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4594 | VarTemplatePartialSpecializationDecl * |
| 4595 | Sema::getMoreSpecializedPartialSpecialization( |
| 4596 | VarTemplatePartialSpecializationDecl *PS1, |
| 4597 | VarTemplatePartialSpecializationDecl *PS2, SourceLocation Loc) { |
| 4598 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 4599 | TemplateDeductionInfo Info(Loc); |
| 4600 | |
Richard Smith | f04fd0b | 2013-12-12 23:14:16 +0000 | [diff] [blame] | 4601 | assert(PS1->getSpecializedTemplate() == PS2->getSpecializedTemplate() && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4602 | "the partial specializations being compared should specialize" |
| 4603 | " the same template."); |
| 4604 | TemplateName Name(PS1->getSpecializedTemplate()); |
| 4605 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4606 | QualType PT1 = Context.getTemplateSpecializationType( |
| 4607 | CanonTemplate, PS1->getTemplateArgs().data(), |
| 4608 | PS1->getTemplateArgs().size()); |
| 4609 | QualType PT2 = Context.getTemplateSpecializationType( |
| 4610 | CanonTemplate, PS2->getTemplateArgs().data(), |
| 4611 | PS2->getTemplateArgs().size()); |
| 4612 | |
| 4613 | // Determine whether PS1 is at least as specialized as PS2 |
| 4614 | Deduced.resize(PS2->getTemplateParameters()->size()); |
| 4615 | bool Better1 = !DeduceTemplateArgumentsByTypeMatch( |
| 4616 | *this, PS2->getTemplateParameters(), PT2, PT1, Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4617 | /*PartialOrdering=*/true); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4618 | if (Better1) { |
| 4619 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
| 4620 | Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 4621 | InstantiatingTemplate Inst(*this, Loc, PS2, DeducedArgs, Info); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4622 | Better1 = !::FinishTemplateArgumentDeduction(*this, PS2, |
| 4623 | PS1->getTemplateArgs(), |
Douglas Gregor | 9225b02 | 2010-04-29 06:31:36 +0000 | [diff] [blame] | 4624 | Deduced, Info); |
Argyrios Kyrtzidis | 7d39155 | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4625 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4626 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4627 | // Determine whether PS2 is at least as specialized as PS1 |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 4628 | Deduced.clear(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4629 | Deduced.resize(PS1->getTemplateParameters()->size()); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4630 | bool Better2 = !DeduceTemplateArgumentsByTypeMatch(*this, |
| 4631 | PS1->getTemplateParameters(), |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4632 | PT1, PT2, Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4633 | /*PartialOrdering=*/true); |
Argyrios Kyrtzidis | 7d39155 | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4634 | if (Better2) { |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4635 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(),Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 4636 | InstantiatingTemplate Inst(*this, Loc, PS1, DeducedArgs, Info); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4637 | Better2 = !::FinishTemplateArgumentDeduction(*this, PS1, |
| 4638 | PS2->getTemplateArgs(), |
Douglas Gregor | 9225b02 | 2010-04-29 06:31:36 +0000 | [diff] [blame] | 4639 | Deduced, Info); |
Argyrios Kyrtzidis | 7d39155 | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4640 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4641 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4642 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4643 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4644 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4645 | return Better1? PS1 : PS2; |
| 4646 | } |
| 4647 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4648 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4649 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4650 | const TemplateArgument &TemplateArg, |
| 4651 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4652 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4653 | llvm::SmallBitVector &Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4654 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4655 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4656 | /// expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4657 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4658 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4659 | const Expr *E, |
| 4660 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4661 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4662 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 4663 | // We can deduce from a pack expansion. |
| 4664 | if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E)) |
| 4665 | E = Expansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4666 | |
Richard Smith | 3434900 | 2012-07-09 03:07:20 +0000 | [diff] [blame] | 4667 | // Skip through any implicit casts we added while type-checking, and any |
| 4668 | // substitutions performed by template alias expansion. |
| 4669 | while (1) { |
| 4670 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 4671 | E = ICE->getSubExpr(); |
| 4672 | else if (const SubstNonTypeTemplateParmExpr *Subst = |
| 4673 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 4674 | E = Subst->getReplacement(); |
| 4675 | else |
| 4676 | break; |
| 4677 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4678 | |
| 4679 | // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4680 | // find other occurrences of template parameters. |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4681 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
Douglas Gregor | 04b1152 | 2010-01-14 18:13:22 +0000 | [diff] [blame] | 4682 | if (!DRE) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4683 | return; |
| 4684 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4685 | const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4686 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 4687 | if (!NTTP) |
| 4688 | return; |
| 4689 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4690 | if (NTTP->getDepth() == Depth) |
| 4691 | Used[NTTP->getIndex()] = true; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4692 | } |
| 4693 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4694 | /// \brief Mark the template parameters that are used by the given |
| 4695 | /// nested name specifier. |
| 4696 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4697 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4698 | NestedNameSpecifier *NNS, |
| 4699 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4700 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4701 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4702 | if (!NNS) |
| 4703 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4704 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4705 | MarkUsedTemplateParameters(Ctx, NNS->getPrefix(), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4706 | Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4707 | MarkUsedTemplateParameters(Ctx, QualType(NNS->getAsType(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4708 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4709 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4710 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4711 | /// \brief Mark the template parameters that are used by the given |
| 4712 | /// template name. |
| 4713 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4714 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4715 | TemplateName Name, |
| 4716 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4717 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4718 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4719 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 4720 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4721 | = dyn_cast<TemplateTemplateParmDecl>(Template)) { |
| 4722 | if (TTP->getDepth() == Depth) |
| 4723 | Used[TTP->getIndex()] = true; |
| 4724 | } |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4725 | return; |
| 4726 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4727 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4728 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4729 | MarkUsedTemplateParameters(Ctx, QTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4730 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4731 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4732 | MarkUsedTemplateParameters(Ctx, DTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4733 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4734 | } |
| 4735 | |
| 4736 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4737 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4738 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4739 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4740 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4741 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4742 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4743 | if (T.isNull()) |
| 4744 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4745 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4746 | // Non-dependent types have nothing deducible |
| 4747 | if (!T->isDependentType()) |
| 4748 | return; |
| 4749 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4750 | T = Ctx.getCanonicalType(T); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4751 | switch (T->getTypeClass()) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4752 | case Type::Pointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4753 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4754 | cast<PointerType>(T)->getPointeeType(), |
| 4755 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4756 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4757 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4758 | break; |
| 4759 | |
| 4760 | case Type::BlockPointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4761 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4762 | cast<BlockPointerType>(T)->getPointeeType(), |
| 4763 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4764 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4765 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4766 | break; |
| 4767 | |
| 4768 | case Type::LValueReference: |
| 4769 | case Type::RValueReference: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4770 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4771 | cast<ReferenceType>(T)->getPointeeType(), |
| 4772 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4773 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4774 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4775 | break; |
| 4776 | |
| 4777 | case Type::MemberPointer: { |
| 4778 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4779 | MarkUsedTemplateParameters(Ctx, MemPtr->getPointeeType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4780 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4781 | MarkUsedTemplateParameters(Ctx, QualType(MemPtr->getClass(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4782 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4783 | break; |
| 4784 | } |
| 4785 | |
| 4786 | case Type::DependentSizedArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4787 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4788 | cast<DependentSizedArrayType>(T)->getSizeExpr(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4789 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4790 | // Fall through to check the element type |
| 4791 | |
| 4792 | case Type::ConstantArray: |
| 4793 | case Type::IncompleteArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4794 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4795 | cast<ArrayType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4796 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4797 | break; |
| 4798 | |
| 4799 | case Type::Vector: |
| 4800 | case Type::ExtVector: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4801 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4802 | cast<VectorType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4803 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4804 | break; |
| 4805 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4806 | case Type::DependentSizedExtVector: { |
| 4807 | const DependentSizedExtVectorType *VecType |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4808 | = cast<DependentSizedExtVectorType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4809 | MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4810 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4811 | MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4812 | Depth, Used); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4813 | break; |
| 4814 | } |
| 4815 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4816 | case Type::FunctionProto: { |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4817 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4818 | MarkUsedTemplateParameters(Ctx, Proto->getReturnType(), OnlyDeduced, Depth, |
| 4819 | Used); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4820 | for (unsigned I = 0, N = Proto->getNumParams(); I != N; ++I) |
| 4821 | MarkUsedTemplateParameters(Ctx, Proto->getParamType(I), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4822 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4823 | break; |
| 4824 | } |
| 4825 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4826 | case Type::TemplateTypeParm: { |
| 4827 | const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); |
| 4828 | if (TTP->getDepth() == Depth) |
| 4829 | Used[TTP->getIndex()] = true; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4830 | break; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4831 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4832 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4833 | case Type::SubstTemplateTypeParmPack: { |
| 4834 | const SubstTemplateTypeParmPackType *Subst |
| 4835 | = cast<SubstTemplateTypeParmPackType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4836 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4837 | QualType(Subst->getReplacedParameter(), 0), |
| 4838 | OnlyDeduced, Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4839 | MarkUsedTemplateParameters(Ctx, Subst->getArgumentPack(), |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4840 | OnlyDeduced, Depth, Used); |
| 4841 | break; |
| 4842 | } |
| 4843 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4844 | case Type::InjectedClassName: |
| 4845 | T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); |
| 4846 | // fall through |
| 4847 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4848 | case Type::TemplateSpecialization: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4849 | const TemplateSpecializationType *Spec |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4850 | = cast<TemplateSpecializationType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4851 | MarkUsedTemplateParameters(Ctx, Spec->getTemplateName(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4852 | Depth, Used); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4853 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4854 | // C++0x [temp.deduct.type]p9: |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 4855 | // If the template argument list of P contains a pack expansion that is |
| 4856 | // not the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4857 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4858 | if (OnlyDeduced && |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4859 | hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs())) |
| 4860 | break; |
| 4861 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4862 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4863 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4864 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4865 | break; |
| 4866 | } |
| 4867 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4868 | case Type::Complex: |
| 4869 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4870 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4871 | cast<ComplexType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4872 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4873 | break; |
| 4874 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4875 | case Type::Atomic: |
| 4876 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4877 | MarkUsedTemplateParameters(Ctx, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4878 | cast<AtomicType>(T)->getValueType(), |
| 4879 | OnlyDeduced, Depth, Used); |
| 4880 | break; |
| 4881 | |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4882 | case Type::DependentName: |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4883 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4884 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4885 | cast<DependentNameType>(T)->getQualifier(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4886 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4887 | break; |
| 4888 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4889 | case Type::DependentTemplateSpecialization: { |
| 4890 | const DependentTemplateSpecializationType *Spec |
| 4891 | = cast<DependentTemplateSpecializationType>(T); |
| 4892 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4893 | MarkUsedTemplateParameters(Ctx, Spec->getQualifier(), |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4894 | OnlyDeduced, Depth, Used); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4895 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4896 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4897 | // If the template argument list of P contains a pack expansion that is not |
| 4898 | // the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4899 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4900 | if (OnlyDeduced && |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4901 | hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs())) |
| 4902 | break; |
| 4903 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4904 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4905 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4906 | Used); |
| 4907 | break; |
| 4908 | } |
| 4909 | |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4910 | case Type::TypeOf: |
| 4911 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4912 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4913 | cast<TypeOfType>(T)->getUnderlyingType(), |
| 4914 | OnlyDeduced, Depth, Used); |
| 4915 | break; |
| 4916 | |
| 4917 | case Type::TypeOfExpr: |
| 4918 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4919 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4920 | cast<TypeOfExprType>(T)->getUnderlyingExpr(), |
| 4921 | OnlyDeduced, Depth, Used); |
| 4922 | break; |
| 4923 | |
| 4924 | case Type::Decltype: |
| 4925 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4926 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4927 | cast<DecltypeType>(T)->getUnderlyingExpr(), |
| 4928 | OnlyDeduced, Depth, Used); |
| 4929 | break; |
| 4930 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4931 | case Type::UnaryTransform: |
| 4932 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4933 | MarkUsedTemplateParameters(Ctx, |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4934 | cast<UnaryTransformType>(T)->getUnderlyingType(), |
| 4935 | OnlyDeduced, Depth, Used); |
| 4936 | break; |
| 4937 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4938 | case Type::PackExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4939 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4940 | cast<PackExpansionType>(T)->getPattern(), |
| 4941 | OnlyDeduced, Depth, Used); |
| 4942 | break; |
| 4943 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4944 | case Type::Auto: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4945 | MarkUsedTemplateParameters(Ctx, |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4946 | cast<AutoType>(T)->getDeducedType(), |
| 4947 | OnlyDeduced, Depth, Used); |
| 4948 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4949 | // None of these types have any template parameters in them. |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4950 | case Type::Builtin: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4951 | case Type::VariableArray: |
| 4952 | case Type::FunctionNoProto: |
| 4953 | case Type::Record: |
| 4954 | case Type::Enum: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4955 | case Type::ObjCInterface: |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4956 | case Type::ObjCObject: |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4957 | case Type::ObjCObjectPointer: |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4958 | case Type::UnresolvedUsing: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4959 | #define TYPE(Class, Base) |
| 4960 | #define ABSTRACT_TYPE(Class, Base) |
| 4961 | #define DEPENDENT_TYPE(Class, Base) |
| 4962 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 4963 | #include "clang/AST/TypeNodes.def" |
| 4964 | break; |
| 4965 | } |
| 4966 | } |
| 4967 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4968 | /// \brief Mark the template parameters that are used by this |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4969 | /// template argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4970 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4971 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4972 | const TemplateArgument &TemplateArg, |
| 4973 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4974 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4975 | llvm::SmallBitVector &Used) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4976 | switch (TemplateArg.getKind()) { |
| 4977 | case TemplateArgument::Null: |
| 4978 | case TemplateArgument::Integral: |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4979 | case TemplateArgument::Declaration: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4980 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4981 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4982 | case TemplateArgument::NullPtr: |
| 4983 | MarkUsedTemplateParameters(Ctx, TemplateArg.getNullPtrType(), OnlyDeduced, |
| 4984 | Depth, Used); |
| 4985 | break; |
| 4986 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4987 | case TemplateArgument::Type: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4988 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4989 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4990 | break; |
| 4991 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4992 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4993 | case TemplateArgument::TemplateExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4994 | MarkUsedTemplateParameters(Ctx, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4995 | TemplateArg.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4996 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4997 | break; |
| 4998 | |
| 4999 | case TemplateArgument::Expression: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5000 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5001 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5002 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5003 | |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5004 | case TemplateArgument::Pack: |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 5005 | for (const auto &P : TemplateArg.pack_elements()) |
| 5006 | MarkUsedTemplateParameters(Ctx, P, OnlyDeduced, Depth, Used); |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5007 | break; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5008 | } |
| 5009 | } |
| 5010 | |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5011 | /// \brief Mark which template parameters can be deduced from a given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5012 | /// template argument list. |
| 5013 | /// |
| 5014 | /// \param TemplateArgs the template argument list from which template |
| 5015 | /// parameters will be deduced. |
| 5016 | /// |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5017 | /// \param Used a bit vector whose elements will be set to \c true |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5018 | /// to indicate when the corresponding template parameter will be |
| 5019 | /// deduced. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5020 | void |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5021 | Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5022 | bool OnlyDeduced, unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5023 | llvm::SmallBitVector &Used) { |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5024 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5025 | // If the template argument list of P contains a pack expansion that is not |
| 5026 | // the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5027 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5028 | if (OnlyDeduced && |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5029 | hasPackExpansionBeforeEnd(TemplateArgs.data(), TemplateArgs.size())) |
| 5030 | return; |
| 5031 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5032 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5033 | ::MarkUsedTemplateParameters(Context, TemplateArgs[I], OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5034 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5035 | } |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5036 | |
| 5037 | /// \brief Marks all of the template parameters that will be deduced by a |
| 5038 | /// call to the given function template. |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 5039 | void Sema::MarkDeducedTemplateParameters( |
| 5040 | ASTContext &Ctx, const FunctionTemplateDecl *FunctionTemplate, |
| 5041 | llvm::SmallBitVector &Deduced) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5042 | TemplateParameterList *TemplateParams |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5043 | = FunctionTemplate->getTemplateParameters(); |
| 5044 | Deduced.clear(); |
| 5045 | Deduced.resize(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5046 | |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5047 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 5048 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5049 | ::MarkUsedTemplateParameters(Ctx, Function->getParamDecl(I)->getType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5050 | true, TemplateParams->getDepth(), Deduced); |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5051 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5052 | |
| 5053 | bool hasDeducibleTemplateParameters(Sema &S, |
| 5054 | FunctionTemplateDecl *FunctionTemplate, |
| 5055 | QualType T) { |
| 5056 | if (!T->isDependentType()) |
| 5057 | return false; |
| 5058 | |
| 5059 | TemplateParameterList *TemplateParams |
| 5060 | = FunctionTemplate->getTemplateParameters(); |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5061 | llvm::SmallBitVector Deduced(TemplateParams->size()); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5062 | ::MarkUsedTemplateParameters(S.Context, T, true, TemplateParams->getDepth(), |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5063 | Deduced); |
| 5064 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5065 | return Deduced.any(); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5066 | } |