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 | }; |
| 61 | } |
| 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 | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 94 | /// \brief Whether template argument deduction for two reference parameters |
| 95 | /// resulted in the argument type, parameter type, or neither type being more |
| 96 | /// qualified than the other. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 97 | enum DeductionQualifierComparison { |
| 98 | NeitherMoreQualified = 0, |
| 99 | ParamMoreQualified, |
| 100 | ArgMoreQualified |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 103 | /// \brief Stores the result of comparing two reference parameters while |
| 104 | /// performing template argument deduction for partial ordering of function |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 105 | /// templates. |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 106 | struct RefParamPartialOrderingComparison { |
| 107 | /// \brief Whether the parameter type is an rvalue reference type. |
| 108 | bool ParamIsRvalueRef; |
| 109 | /// \brief Whether the argument type is an rvalue reference type. |
| 110 | bool ArgIsRvalueRef; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 111 | |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 112 | /// \brief Whether the parameter or argument (or neither) is more qualified. |
| 113 | DeductionQualifierComparison Qualifiers; |
| 114 | }; |
| 115 | |
| 116 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 117 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 118 | static Sema::TemplateDeductionResult |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 119 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 120 | TemplateParameterList *TemplateParams, |
| 121 | QualType Param, |
| 122 | QualType Arg, |
| 123 | TemplateDeductionInfo &Info, |
| 124 | SmallVectorImpl<DeducedTemplateArgument> & |
| 125 | Deduced, |
| 126 | unsigned TDF, |
| 127 | bool PartialOrdering = false, |
| 128 | SmallVectorImpl<RefParamPartialOrderingComparison> * |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 129 | RefParamComparisons = nullptr); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 130 | |
| 131 | static Sema::TemplateDeductionResult |
| 132 | DeduceTemplateArguments(Sema &S, |
| 133 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 134 | const TemplateArgument *Params, unsigned NumParams, |
| 135 | const TemplateArgument *Args, unsigned NumArgs, |
| 136 | TemplateDeductionInfo &Info, |
Richard Smith | 16b6539 | 2012-12-06 06:44:44 +0000 | [diff] [blame] | 137 | SmallVectorImpl<DeducedTemplateArgument> &Deduced); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 138 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 139 | /// \brief If the given expression is of a form that permits the deduction |
| 140 | /// of a non-type template parameter, return the declaration of that |
| 141 | /// non-type template parameter. |
| 142 | static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) { |
Richard Smith | 7ebb07c | 2012-07-08 04:37:51 +0000 | [diff] [blame] | 143 | // If we are within an alias template, the expression may have undergone |
| 144 | // any number of parameter substitutions already. |
| 145 | while (1) { |
| 146 | if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E)) |
| 147 | E = IC->getSubExpr(); |
| 148 | else if (SubstNonTypeTemplateParmExpr *Subst = |
| 149 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 150 | E = Subst->getReplacement(); |
| 151 | else |
| 152 | break; |
| 153 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 155 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 156 | return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 158 | return nullptr; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 161 | /// \brief Determine whether two declaration pointers refer to the same |
| 162 | /// declaration. |
| 163 | static bool isSameDeclaration(Decl *X, Decl *Y) { |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 164 | if (NamedDecl *NX = dyn_cast<NamedDecl>(X)) |
| 165 | X = NX->getUnderlyingDecl(); |
| 166 | if (NamedDecl *NY = dyn_cast<NamedDecl>(Y)) |
| 167 | Y = NY->getUnderlyingDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 168 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 169 | return X->getCanonicalDecl() == Y->getCanonicalDecl(); |
| 170 | } |
| 171 | |
| 172 | /// \brief Verify that the given, deduced template arguments are compatible. |
| 173 | /// |
| 174 | /// \returns The deduced template argument, or a NULL template argument if |
| 175 | /// the deduced template arguments were incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 176 | static DeducedTemplateArgument |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 177 | checkDeducedTemplateArguments(ASTContext &Context, |
| 178 | const DeducedTemplateArgument &X, |
| 179 | const DeducedTemplateArgument &Y) { |
| 180 | // We have no deduction for one or both of the arguments; they're compatible. |
| 181 | if (X.isNull()) |
| 182 | return Y; |
| 183 | if (Y.isNull()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 184 | return X; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 185 | |
| 186 | switch (X.getKind()) { |
| 187 | case TemplateArgument::Null: |
| 188 | llvm_unreachable("Non-deduced template arguments handled above"); |
| 189 | |
| 190 | case TemplateArgument::Type: |
| 191 | // If two template type arguments have the same type, they're compatible. |
| 192 | if (Y.getKind() == TemplateArgument::Type && |
| 193 | Context.hasSameType(X.getAsType(), Y.getAsType())) |
| 194 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 195 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 196 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 197 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 198 | case TemplateArgument::Integral: |
| 199 | // If we deduced a constant in one case and either a dependent expression or |
| 200 | // declaration in another case, keep the integral constant. |
| 201 | // If both are integral constants with the same value, keep that value. |
| 202 | if (Y.getKind() == TemplateArgument::Expression || |
| 203 | Y.getKind() == TemplateArgument::Declaration || |
| 204 | (Y.getKind() == TemplateArgument::Integral && |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 205 | hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 206 | return DeducedTemplateArgument(X, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 207 | X.wasDeducedFromArrayBound() && |
| 208 | Y.wasDeducedFromArrayBound()); |
| 209 | |
| 210 | // All other combinations are incompatible. |
| 211 | return DeducedTemplateArgument(); |
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 | case TemplateArgument::Template: |
| 214 | if (Y.getKind() == TemplateArgument::Template && |
| 215 | Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate())) |
| 216 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 217 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 218 | // All other combinations are incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 219 | return DeducedTemplateArgument(); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 220 | |
| 221 | case TemplateArgument::TemplateExpansion: |
| 222 | if (Y.getKind() == TemplateArgument::TemplateExpansion && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 223 | Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 224 | Y.getAsTemplateOrTemplatePattern())) |
| 225 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 226 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 227 | // All other combinations are incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 228 | return DeducedTemplateArgument(); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 229 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 230 | case TemplateArgument::Expression: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 231 | // If we deduced a dependent expression in one case and either an integral |
| 232 | // constant or a declaration in another case, keep the integral constant |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 233 | // or declaration. |
| 234 | if (Y.getKind() == TemplateArgument::Integral || |
| 235 | Y.getKind() == TemplateArgument::Declaration) |
| 236 | return DeducedTemplateArgument(Y, X.wasDeducedFromArrayBound() && |
| 237 | Y.wasDeducedFromArrayBound()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 238 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 239 | if (Y.getKind() == TemplateArgument::Expression) { |
| 240 | // Compare the expressions for equality |
| 241 | llvm::FoldingSetNodeID ID1, ID2; |
| 242 | X.getAsExpr()->Profile(ID1, Context, true); |
| 243 | Y.getAsExpr()->Profile(ID2, Context, true); |
| 244 | if (ID1 == ID2) |
| 245 | return X; |
| 246 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 247 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 248 | // All other combinations are incompatible. |
| 249 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 250 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 251 | case TemplateArgument::Declaration: |
| 252 | // If we deduced a declaration and a dependent expression, keep the |
| 253 | // declaration. |
| 254 | if (Y.getKind() == TemplateArgument::Expression) |
| 255 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 256 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 257 | // If we deduced a declaration and an integral constant, keep the |
| 258 | // integral constant. |
| 259 | if (Y.getKind() == TemplateArgument::Integral) |
| 260 | return Y; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 261 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 262 | // If we deduced two declarations, make sure they they refer to the |
| 263 | // same declaration. |
| 264 | if (Y.getKind() == TemplateArgument::Declaration && |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 265 | isSameDeclaration(X.getAsDecl(), Y.getAsDecl()) && |
| 266 | X.isDeclForReferenceParam() == Y.isDeclForReferenceParam()) |
| 267 | return X; |
| 268 | |
| 269 | // All other combinations are incompatible. |
| 270 | return DeducedTemplateArgument(); |
| 271 | |
| 272 | case TemplateArgument::NullPtr: |
| 273 | // If we deduced a null pointer and a dependent expression, keep the |
| 274 | // null pointer. |
| 275 | if (Y.getKind() == TemplateArgument::Expression) |
| 276 | return X; |
| 277 | |
| 278 | // If we deduced a null pointer and an integral constant, keep the |
| 279 | // integral constant. |
| 280 | if (Y.getKind() == TemplateArgument::Integral) |
| 281 | return Y; |
| 282 | |
| 283 | // If we deduced two null pointers, make sure they have the same type. |
| 284 | if (Y.getKind() == TemplateArgument::NullPtr && |
| 285 | Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType())) |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 286 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 287 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 288 | // All other combinations are incompatible. |
| 289 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 290 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 291 | case TemplateArgument::Pack: |
| 292 | if (Y.getKind() != TemplateArgument::Pack || |
| 293 | X.pack_size() != Y.pack_size()) |
| 294 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 295 | |
| 296 | for (TemplateArgument::pack_iterator XA = X.pack_begin(), |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 297 | XAEnd = X.pack_end(), |
| 298 | YA = Y.pack_begin(); |
| 299 | XA != XAEnd; ++XA, ++YA) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 300 | if (checkDeducedTemplateArguments(Context, |
| 301 | DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()), |
Douglas Gregor | f491ee2 | 2011-01-05 21:00:53 +0000 | [diff] [blame] | 302 | DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound())) |
| 303 | .isNull()) |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 304 | return DeducedTemplateArgument(); |
| 305 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 306 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 307 | return X; |
| 308 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 309 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 310 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | /// \brief Deduce the value of the given non-type template parameter |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 314 | /// from the given constant. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 315 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 316 | DeduceNonTypeTemplateArgument(Sema &S, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | NonTypeTemplateParmDecl *NTTP, |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 318 | llvm::APSInt Value, QualType ValueType, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 319 | bool DeducedFromArrayBound, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 320 | TemplateDeductionInfo &Info, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 321 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 323 | "Cannot deduce non-type template argument with depth > 0"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 325 | DeducedTemplateArgument NewDeduced(S.Context, Value, ValueType, |
| 326 | DeducedFromArrayBound); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 327 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 328 | Deduced[NTTP->getIndex()], |
| 329 | NewDeduced); |
| 330 | if (Result.isNull()) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 331 | Info.Param = NTTP; |
| 332 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 333 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 334 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 335 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 336 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 337 | Deduced[NTTP->getIndex()] = Result; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 338 | return Sema::TDK_Success; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | /// \brief Deduce the value of the given non-type template parameter |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 342 | /// from the given type- or value-dependent expression. |
| 343 | /// |
| 344 | /// \returns true if deduction succeeded, false otherwise. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 345 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 346 | DeduceNonTypeTemplateArgument(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 347 | NonTypeTemplateParmDecl *NTTP, |
| 348 | Expr *Value, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 349 | TemplateDeductionInfo &Info, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 350 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 352 | "Cannot deduce non-type template argument with depth > 0"); |
| 353 | assert((Value->isTypeDependent() || Value->isValueDependent()) && |
| 354 | "Expression template argument must be type- or value-dependent."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 355 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 356 | DeducedTemplateArgument NewDeduced(Value); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 357 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
| 358 | Deduced[NTTP->getIndex()], |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 359 | NewDeduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 360 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 361 | if (Result.isNull()) { |
| 362 | Info.Param = NTTP; |
| 363 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| 364 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 365 | return Sema::TDK_Inconsistent; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 366 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 367 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 368 | Deduced[NTTP->getIndex()] = Result; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 369 | return Sema::TDK_Success; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 372 | /// \brief Deduce the value of the given non-type template parameter |
| 373 | /// from the given declaration. |
| 374 | /// |
| 375 | /// \returns true if deduction succeeded, false otherwise. |
| 376 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 377 | DeduceNonTypeTemplateArgument(Sema &S, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 378 | NonTypeTemplateParmDecl *NTTP, |
| 379 | ValueDecl *D, |
| 380 | TemplateDeductionInfo &Info, |
| 381 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 382 | assert(NTTP->getDepth() == 0 && |
| 383 | "Cannot deduce non-type template argument with depth > 0"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 384 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 385 | D = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 386 | TemplateArgument New(D, NTTP->getType()->isReferenceType()); |
| 387 | DeducedTemplateArgument NewDeduced(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 388 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 389 | Deduced[NTTP->getIndex()], |
| 390 | NewDeduced); |
| 391 | if (Result.isNull()) { |
| 392 | Info.Param = NTTP; |
| 393 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| 394 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 395 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 396 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 397 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 398 | Deduced[NTTP->getIndex()] = Result; |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 399 | return Sema::TDK_Success; |
| 400 | } |
| 401 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 402 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 403 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 404 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 405 | TemplateName Param, |
| 406 | TemplateName Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 407 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 408 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 409 | TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 410 | if (!ParamDecl) { |
| 411 | // The parameter type is dependent and is not a template template parameter, |
| 412 | // so there is nothing that we can deduce. |
| 413 | return Sema::TDK_Success; |
| 414 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 415 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 416 | if (TemplateTemplateParmDecl *TempParam |
| 417 | = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 418 | DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 419 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 420 | Deduced[TempParam->getIndex()], |
| 421 | NewDeduced); |
| 422 | if (Result.isNull()) { |
| 423 | Info.Param = TempParam; |
| 424 | Info.FirstArg = Deduced[TempParam->getIndex()]; |
| 425 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 426 | return Sema::TDK_Inconsistent; |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 427 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 428 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 429 | Deduced[TempParam->getIndex()] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 430 | return Sema::TDK_Success; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 431 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 432 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 433 | // Verify that the two template names are equivalent. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 434 | if (S.Context.hasSameTemplateName(Param, Arg)) |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 435 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 436 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 437 | // Mismatch of non-dependent template parameter to argument. |
| 438 | Info.FirstArg = TemplateArgument(Param); |
| 439 | Info.SecondArg = TemplateArgument(Arg); |
| 440 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | /// \brief Deduce the template arguments by comparing the template parameter |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 444 | /// type (which is a template-id) with the template argument type. |
| 445 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 446 | /// \param S the Sema |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 447 | /// |
| 448 | /// \param TemplateParams the template parameters that we are deducing |
| 449 | /// |
| 450 | /// \param Param the parameter type |
| 451 | /// |
| 452 | /// \param Arg the argument type |
| 453 | /// |
| 454 | /// \param Info information about the template argument deduction itself |
| 455 | /// |
| 456 | /// \param Deduced the deduced template arguments |
| 457 | /// |
| 458 | /// \returns the result of template argument deduction so far. Note that a |
| 459 | /// "success" result means that template argument deduction has not yet failed, |
| 460 | /// but it may still fail, later, for other reasons. |
| 461 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 462 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 463 | TemplateParameterList *TemplateParams, |
| 464 | const TemplateSpecializationType *Param, |
| 465 | QualType Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 466 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 467 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 468 | assert(Arg.isCanonical() && "Argument type must be canonical"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 470 | // Check whether the template argument is a dependent template-id. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 471 | if (const TemplateSpecializationType *SpecArg |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 472 | = dyn_cast<TemplateSpecializationType>(Arg)) { |
| 473 | // Perform template argument deduction for the template name. |
| 474 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 475 | = DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 476 | Param->getTemplateName(), |
| 477 | SpecArg->getTemplateName(), |
| 478 | Info, Deduced)) |
| 479 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 481 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 482 | // Perform template argument deduction on each template |
Douglas Gregor | d80ea20 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 483 | // argument. Ignore any missing/extra arguments, since they could be |
| 484 | // filled in by default arguments. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 485 | return DeduceTemplateArguments(S, TemplateParams, |
| 486 | Param->getArgs(), Param->getNumArgs(), |
Douglas Gregor | d80ea20 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 487 | SpecArg->getArgs(), SpecArg->getNumArgs(), |
Richard Smith | 16b6539 | 2012-12-06 06:44:44 +0000 | [diff] [blame] | 488 | Info, Deduced); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 489 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 491 | // If the argument type is a class template specialization, we |
| 492 | // perform template argument deduction using its template |
| 493 | // arguments. |
| 494 | const RecordType *RecordArg = dyn_cast<RecordType>(Arg); |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 495 | if (!RecordArg) { |
| 496 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 497 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 498 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 499 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 500 | |
| 501 | ClassTemplateSpecializationDecl *SpecArg |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 502 | = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 503 | if (!SpecArg) { |
| 504 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 505 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 506 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 507 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 509 | // Perform template argument deduction for the template name. |
| 510 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 511 | = DeduceTemplateArguments(S, |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 512 | TemplateParams, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 513 | Param->getTemplateName(), |
| 514 | TemplateName(SpecArg->getSpecializedTemplate()), |
| 515 | Info, Deduced)) |
| 516 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 518 | // Perform template argument deduction for the template arguments. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 519 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 520 | Param->getArgs(), Param->getNumArgs(), |
| 521 | SpecArg->getTemplateArgs().data(), |
| 522 | SpecArg->getTemplateArgs().size(), |
| 523 | Info, Deduced); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 524 | } |
| 525 | |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 526 | /// \brief Determines whether the given type is an opaque type that |
| 527 | /// might be more qualified when instantiated. |
| 528 | static bool IsPossiblyOpaquelyQualifiedType(QualType T) { |
| 529 | switch (T->getTypeClass()) { |
| 530 | case Type::TypeOfExpr: |
| 531 | case Type::TypeOf: |
| 532 | case Type::DependentName: |
| 533 | case Type::Decltype: |
| 534 | case Type::UnresolvedUsing: |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 535 | case Type::TemplateTypeParm: |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 536 | return true; |
| 537 | |
| 538 | case Type::ConstantArray: |
| 539 | case Type::IncompleteArray: |
| 540 | case Type::VariableArray: |
| 541 | case Type::DependentSizedArray: |
| 542 | return IsPossiblyOpaquelyQualifiedType( |
| 543 | cast<ArrayType>(T)->getElementType()); |
| 544 | |
| 545 | default: |
| 546 | return false; |
| 547 | } |
| 548 | } |
| 549 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 550 | /// \brief Retrieve the depth and index of a template parameter. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 551 | static std::pair<unsigned, unsigned> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 552 | getDepthAndIndex(NamedDecl *ND) { |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 553 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) |
| 554 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 555 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 556 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND)) |
| 557 | return std::make_pair(NTTP->getDepth(), NTTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 558 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 559 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND); |
| 560 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 561 | } |
| 562 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 563 | /// \brief Retrieve the depth and index of an unexpanded parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 564 | static std::pair<unsigned, unsigned> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 565 | getDepthAndIndex(UnexpandedParameterPack UPP) { |
| 566 | if (const TemplateTypeParmType *TTP |
| 567 | = UPP.first.dyn_cast<const TemplateTypeParmType *>()) |
| 568 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 569 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 570 | return getDepthAndIndex(UPP.first.get<NamedDecl *>()); |
| 571 | } |
| 572 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 573 | /// \brief Helper function to build a TemplateParameter when we don't |
| 574 | /// know its type statically. |
| 575 | static TemplateParameter makeTemplateParameter(Decl *D) { |
| 576 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
| 577 | return TemplateParameter(TTP); |
Craig Topper | 4b482ee | 2013-07-08 04:24:47 +0000 | [diff] [blame] | 578 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 579 | return TemplateParameter(NTTP); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 580 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 581 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
| 582 | } |
| 583 | |
Craig Topper | 0a4e1f5 | 2013-07-08 04:44:01 +0000 | [diff] [blame] | 584 | typedef SmallVector<SmallVector<DeducedTemplateArgument, 4>, 2> |
| 585 | NewlyDeducedPacksType; |
| 586 | |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 587 | /// \brief Prepare to perform template argument deduction for all of the |
| 588 | /// arguments in a set of argument packs. |
Craig Topper | 0a4e1f5 | 2013-07-08 04:44:01 +0000 | [diff] [blame] | 589 | static void |
| 590 | PrepareArgumentPackDeduction(Sema &S, |
| 591 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 592 | ArrayRef<unsigned> PackIndices, |
| 593 | SmallVectorImpl<DeducedTemplateArgument> &SavedPacks, |
| 594 | NewlyDeducedPacksType &NewlyDeducedPacks) { |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 595 | // Save the deduced template arguments for each parameter pack expanded |
| 596 | // by this pack expansion, then clear out the deduction. |
| 597 | for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) { |
| 598 | // Save the previously-deduced argument pack, then clear it out so that we |
| 599 | // can deduce a new argument pack. |
| 600 | SavedPacks[I] = Deduced[PackIndices[I]]; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 601 | Deduced[PackIndices[I]] = TemplateArgument(); |
| 602 | |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 603 | if (!S.CurrentInstantiationScope) |
| 604 | continue; |
| 605 | |
Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 606 | // If the template argument pack was explicitly specified, add that to |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 607 | // the set of deduced arguments. |
| 608 | const TemplateArgument *ExplicitArgs; |
| 609 | unsigned NumExplicitArgs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 610 | if (NamedDecl *PartiallySubstitutedPack |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 611 | = S.CurrentInstantiationScope->getPartiallySubstitutedPack( |
| 612 | &ExplicitArgs, |
| 613 | &NumExplicitArgs)) { |
| 614 | if (getDepthAndIndex(PartiallySubstitutedPack).second == PackIndices[I]) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 615 | NewlyDeducedPacks[I].append(ExplicitArgs, |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 616 | ExplicitArgs + NumExplicitArgs); |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 621 | /// \brief Finish template argument deduction for a set of argument packs, |
| 622 | /// producing the argument packs and checking for consistency with prior |
| 623 | /// deductions. |
| 624 | static Sema::TemplateDeductionResult |
| 625 | FinishArgumentPackDeduction(Sema &S, |
Craig Topper | 0a4e1f5 | 2013-07-08 04:44:01 +0000 | [diff] [blame] | 626 | TemplateParameterList *TemplateParams, |
| 627 | bool HasAnyArguments, |
| 628 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 629 | ArrayRef<unsigned> PackIndices, |
| 630 | SmallVectorImpl<DeducedTemplateArgument> &SavedPacks, |
| 631 | NewlyDeducedPacksType &NewlyDeducedPacks, |
| 632 | TemplateDeductionInfo &Info) { |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 633 | // Build argument packs for each of the parameter packs expanded by this |
| 634 | // pack expansion. |
| 635 | for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) { |
| 636 | if (HasAnyArguments && NewlyDeducedPacks[I].empty()) { |
| 637 | // We were not able to deduce anything for this parameter pack, |
| 638 | // so just restore the saved argument pack. |
| 639 | Deduced[PackIndices[I]] = SavedPacks[I]; |
| 640 | continue; |
| 641 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 642 | |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 643 | DeducedTemplateArgument NewPack; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 644 | |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 645 | if (NewlyDeducedPacks[I].empty()) { |
| 646 | // If we deduced an empty argument pack, create it now. |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 647 | NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack()); |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 648 | } else { |
| 649 | TemplateArgument *ArgumentPack |
Douglas Gregor | 74c6d19 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 650 | = new (S.Context) TemplateArgument [NewlyDeducedPacks[I].size()]; |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 651 | std::copy(NewlyDeducedPacks[I].begin(), NewlyDeducedPacks[I].end(), |
| 652 | ArgumentPack); |
| 653 | NewPack |
Douglas Gregor | 74c6d19 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 654 | = DeducedTemplateArgument(TemplateArgument(ArgumentPack, |
| 655 | NewlyDeducedPacks[I].size()), |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 656 | NewlyDeducedPacks[I][0].wasDeducedFromArrayBound()); |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 657 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 658 | |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 659 | DeducedTemplateArgument Result |
| 660 | = checkDeducedTemplateArguments(S.Context, SavedPacks[I], NewPack); |
| 661 | if (Result.isNull()) { |
| 662 | Info.Param |
| 663 | = makeTemplateParameter(TemplateParams->getParam(PackIndices[I])); |
| 664 | Info.FirstArg = SavedPacks[I]; |
| 665 | Info.SecondArg = NewPack; |
| 666 | return Sema::TDK_Inconsistent; |
| 667 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 668 | |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 669 | Deduced[PackIndices[I]] = Result; |
| 670 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 671 | |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 672 | return Sema::TDK_Success; |
| 673 | } |
| 674 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 675 | /// \brief Deduce the template arguments by comparing the list of parameter |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 676 | /// types to the list of argument types, as in the parameter-type-lists of |
| 677 | /// function types (C++ [temp.deduct.type]p10). |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 678 | /// |
| 679 | /// \param S The semantic analysis object within which we are deducing |
| 680 | /// |
| 681 | /// \param TemplateParams The template parameters that we are deducing |
| 682 | /// |
| 683 | /// \param Params The list of parameter types |
| 684 | /// |
| 685 | /// \param NumParams The number of types in \c Params |
| 686 | /// |
| 687 | /// \param Args The list of argument types |
| 688 | /// |
| 689 | /// \param NumArgs The number of types in \c Args |
| 690 | /// |
| 691 | /// \param Info information about the template argument deduction itself |
| 692 | /// |
| 693 | /// \param Deduced the deduced template arguments |
| 694 | /// |
| 695 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
| 696 | /// how template argument deduction is performed. |
| 697 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 698 | /// \param PartialOrdering If true, we are performing template argument |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 699 | /// deduction for during partial ordering for a call |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 700 | /// (C++0x [temp.deduct.partial]). |
| 701 | /// |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 702 | /// \param RefParamComparisons If we're performing template argument deduction |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 703 | /// in the context of partial ordering, the set of qualifier comparisons. |
| 704 | /// |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 705 | /// \returns the result of template argument deduction so far. Note that a |
| 706 | /// "success" result means that template argument deduction has not yet failed, |
| 707 | /// but it may still fail, later, for other reasons. |
| 708 | static Sema::TemplateDeductionResult |
| 709 | DeduceTemplateArguments(Sema &S, |
| 710 | TemplateParameterList *TemplateParams, |
| 711 | const QualType *Params, unsigned NumParams, |
| 712 | const QualType *Args, unsigned NumArgs, |
| 713 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 714 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 715 | unsigned TDF, |
| 716 | bool PartialOrdering = false, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 717 | SmallVectorImpl<RefParamPartialOrderingComparison> * |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 718 | RefParamComparisons = nullptr) { |
Douglas Gregor | 86bea35 | 2011-01-05 23:23:17 +0000 | [diff] [blame] | 719 | // Fast-path check to see if we have too many/too few arguments. |
| 720 | if (NumParams != NumArgs && |
| 721 | !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) && |
| 722 | !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1]))) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 723 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 724 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 725 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 726 | // Similarly, if P has a form that contains (T), then each parameter type |
| 727 | // Pi of the respective parameter-type- list of P is compared with the |
| 728 | // corresponding parameter type Ai of the corresponding parameter-type-list |
| 729 | // of A. [...] |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 730 | unsigned ArgIdx = 0, ParamIdx = 0; |
| 731 | for (; ParamIdx != NumParams; ++ParamIdx) { |
| 732 | // Check argument types. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 733 | const PackExpansionType *Expansion |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 734 | = dyn_cast<PackExpansionType>(Params[ParamIdx]); |
| 735 | if (!Expansion) { |
| 736 | // Simple case: compare the parameter and argument types at this point. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 737 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 738 | // Make sure we have an argument. |
| 739 | if (ArgIdx >= NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 740 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 741 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 742 | if (isa<PackExpansionType>(Args[ArgIdx])) { |
| 743 | // C++0x [temp.deduct.type]p22: |
| 744 | // If the original function parameter associated with A is a function |
| 745 | // parameter pack and the function parameter associated with P is not |
| 746 | // a function parameter pack, then template argument deduction fails. |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 747 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 748 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 749 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 750 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 751 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 752 | Params[ParamIdx], Args[ArgIdx], |
| 753 | Info, Deduced, TDF, |
| 754 | PartialOrdering, |
| 755 | RefParamComparisons)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 756 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 757 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 758 | ++ArgIdx; |
| 759 | continue; |
| 760 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 761 | |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 762 | // C++0x [temp.deduct.type]p5: |
| 763 | // The non-deduced contexts are: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 764 | // - 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] | 765 | // parameter-declaration-clause. |
| 766 | if (ParamIdx + 1 < NumParams) |
| 767 | return Sema::TDK_Success; |
| 768 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 769 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 770 | // If the parameter-declaration corresponding to Pi is a function |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 771 | // parameter pack, then the type of its declarator- id is compared with |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 772 | // each remaining parameter type in the parameter-type-list of A. Each |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 773 | // comparison deduces template arguments for subsequent positions in the |
| 774 | // template parameter packs expanded by the function parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 775 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 776 | // Compute the set of template parameter indices that correspond to |
| 777 | // parameter packs expanded by the pack expansion. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 778 | SmallVector<unsigned, 2> PackIndices; |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 779 | QualType Pattern = Expansion->getPattern(); |
| 780 | { |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 781 | llvm::SmallBitVector SawIndices(TemplateParams->size()); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 782 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 783 | S.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| 784 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 785 | unsigned Depth, Index; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 786 | std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 787 | if (Depth == 0 && !SawIndices[Index]) { |
| 788 | SawIndices[Index] = true; |
| 789 | PackIndices.push_back(Index); |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?"); |
| 794 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 795 | // Keep track of the deduced template arguments for each parameter pack |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 796 | // expanded by this pack expansion (the outer index) and for each |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 797 | // template argument (the inner SmallVectors). |
Craig Topper | 0a4e1f5 | 2013-07-08 04:44:01 +0000 | [diff] [blame] | 798 | NewlyDeducedPacksType NewlyDeducedPacks(PackIndices.size()); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 799 | SmallVector<DeducedTemplateArgument, 2> |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 800 | SavedPacks(PackIndices.size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 801 | PrepareArgumentPackDeduction(S, Deduced, PackIndices, SavedPacks, |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 802 | NewlyDeducedPacks); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 803 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 804 | bool HasAnyArguments = false; |
| 805 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| 806 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 807 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 808 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 809 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 810 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, Pattern, |
| 811 | Args[ArgIdx], Info, Deduced, |
| 812 | TDF, PartialOrdering, |
| 813 | RefParamComparisons)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 814 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 815 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 816 | // Capture the deduced template arguments for each parameter pack expanded |
| 817 | // by this pack expansion, add them to the list of arguments we've deduced |
| 818 | // for that pack, then clear out the deduced argument. |
| 819 | for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) { |
| 820 | DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]]; |
| 821 | if (!DeducedArg.isNull()) { |
| 822 | NewlyDeducedPacks[I].push_back(DeducedArg); |
| 823 | DeducedArg = DeducedTemplateArgument(); |
| 824 | } |
| 825 | } |
| 826 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 827 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 828 | // Build argument packs for each of the parameter packs expanded by this |
| 829 | // pack expansion. |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 830 | if (Sema::TemplateDeductionResult Result |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 831 | = FinishArgumentPackDeduction(S, TemplateParams, HasAnyArguments, |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 832 | Deduced, PackIndices, SavedPacks, |
| 833 | NewlyDeducedPacks, Info)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 834 | return Result; |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 835 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 836 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 837 | // Make sure we don't have any extra arguments. |
| 838 | if (ArgIdx < NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 839 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 840 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 841 | return Sema::TDK_Success; |
| 842 | } |
| 843 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 844 | /// \brief Determine whether the parameter has qualifiers that are either |
| 845 | /// inconsistent with or a superset of the argument's qualifiers. |
| 846 | static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType, |
| 847 | QualType ArgType) { |
| 848 | Qualifiers ParamQs = ParamType.getQualifiers(); |
| 849 | Qualifiers ArgQs = ArgType.getQualifiers(); |
| 850 | |
| 851 | if (ParamQs == ArgQs) |
| 852 | return false; |
| 853 | |
| 854 | // Mismatched (but not missing) Objective-C GC attributes. |
| 855 | if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() && |
| 856 | ParamQs.hasObjCGCAttr()) |
| 857 | return true; |
| 858 | |
| 859 | // Mismatched (but not missing) address spaces. |
| 860 | if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() && |
| 861 | ParamQs.hasAddressSpace()) |
| 862 | return true; |
| 863 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 864 | // Mismatched (but not missing) Objective-C lifetime qualifiers. |
| 865 | if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() && |
| 866 | ParamQs.hasObjCLifetime()) |
| 867 | return true; |
| 868 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 869 | // CVR qualifier superset. |
| 870 | return (ParamQs.getCVRQualifiers() != ArgQs.getCVRQualifiers()) && |
| 871 | ((ParamQs.getCVRQualifiers() | ArgQs.getCVRQualifiers()) |
| 872 | == ParamQs.getCVRQualifiers()); |
| 873 | } |
| 874 | |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 875 | /// \brief Compare types for equality with respect to possibly compatible |
| 876 | /// function types (noreturn adjustment, implicit calling conventions). If any |
| 877 | /// of parameter and argument is not a function, just perform type comparison. |
| 878 | /// |
| 879 | /// \param Param the template parameter type. |
| 880 | /// |
| 881 | /// \param Arg the argument type. |
| 882 | bool Sema::isSameOrCompatibleFunctionType(CanQualType Param, |
| 883 | CanQualType Arg) { |
| 884 | const FunctionType *ParamFunction = Param->getAs<FunctionType>(), |
| 885 | *ArgFunction = Arg->getAs<FunctionType>(); |
| 886 | |
| 887 | // Just compare if not functions. |
| 888 | if (!ParamFunction || !ArgFunction) |
| 889 | return Param == Arg; |
| 890 | |
| 891 | // Noreturn adjustment. |
| 892 | QualType AdjustedParam; |
| 893 | if (IsNoReturnConversion(Param, Arg, AdjustedParam)) |
| 894 | return Arg == Context.getCanonicalType(AdjustedParam); |
| 895 | |
| 896 | // FIXME: Compatible calling conventions. |
| 897 | |
| 898 | return Param == Arg; |
| 899 | } |
| 900 | |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 901 | /// \brief Deduce the template arguments by comparing the parameter type and |
| 902 | /// the argument type (C++ [temp.deduct.type]). |
| 903 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 904 | /// \param S the semantic analysis object within which we are deducing |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 905 | /// |
| 906 | /// \param TemplateParams the template parameters that we are deducing |
| 907 | /// |
| 908 | /// \param ParamIn the parameter type |
| 909 | /// |
| 910 | /// \param ArgIn the argument type |
| 911 | /// |
| 912 | /// \param Info information about the template argument deduction itself |
| 913 | /// |
| 914 | /// \param Deduced the deduced template arguments |
| 915 | /// |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 916 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 917 | /// how template argument deduction is performed. |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 918 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 919 | /// \param PartialOrdering Whether we're performing template argument deduction |
| 920 | /// in the context of partial ordering (C++0x [temp.deduct.partial]). |
| 921 | /// |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 922 | /// \param RefParamComparisons If we're performing template argument deduction |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 923 | /// in the context of partial ordering, the set of qualifier comparisons. |
| 924 | /// |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 925 | /// \returns the result of template argument deduction so far. Note that a |
| 926 | /// "success" result means that template argument deduction has not yet failed, |
| 927 | /// but it may still fail, later, for other reasons. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 928 | static Sema::TemplateDeductionResult |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 929 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 930 | TemplateParameterList *TemplateParams, |
| 931 | QualType ParamIn, QualType ArgIn, |
| 932 | TemplateDeductionInfo &Info, |
| 933 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 934 | unsigned TDF, |
| 935 | bool PartialOrdering, |
| 936 | SmallVectorImpl<RefParamPartialOrderingComparison> * |
| 937 | RefParamComparisons) { |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 938 | // We only want to look at the canonical types, since typedefs and |
| 939 | // sugar are not part of template argument deduction. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 940 | QualType Param = S.Context.getCanonicalType(ParamIn); |
| 941 | QualType Arg = S.Context.getCanonicalType(ArgIn); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 942 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 943 | // If the argument type is a pack expansion, look at its pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 944 | // This isn't explicitly called out |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 945 | if (const PackExpansionType *ArgExpansion |
| 946 | = dyn_cast<PackExpansionType>(Arg)) |
| 947 | Arg = ArgExpansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 948 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 949 | if (PartialOrdering) { |
| 950 | // C++0x [temp.deduct.partial]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 951 | // Before the partial ordering is done, certain transformations are |
| 952 | // performed on the types used for partial ordering: |
| 953 | // - 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] | 954 | const ReferenceType *ParamRef = Param->getAs<ReferenceType>(); |
| 955 | if (ParamRef) |
| 956 | Param = ParamRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 957 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 958 | // - If A is a reference type, A is replaced by the type referred to. |
| 959 | const ReferenceType *ArgRef = Arg->getAs<ReferenceType>(); |
| 960 | if (ArgRef) |
| 961 | Arg = ArgRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 962 | |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 963 | if (RefParamComparisons && ParamRef && ArgRef) { |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 964 | // C++0x [temp.deduct.partial]p6: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 965 | // If both P and A were reference types (before being replaced with the |
| 966 | // type referred to above), determine which of the two types (if any) is |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 967 | // more cv-qualified than the other; otherwise the types are considered |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 968 | // to be equally cv-qualified for partial ordering purposes. The result |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 969 | // of this determination will be used below. |
| 970 | // |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 971 | // We save this information for later, using it only when deduction |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 972 | // succeeds in both directions. |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 973 | RefParamPartialOrderingComparison Comparison; |
| 974 | Comparison.ParamIsRvalueRef = ParamRef->getAs<RValueReferenceType>(); |
| 975 | Comparison.ArgIsRvalueRef = ArgRef->getAs<RValueReferenceType>(); |
| 976 | Comparison.Qualifiers = NeitherMoreQualified; |
Douglas Gregor | 85894a8 | 2011-04-30 17:07:52 +0000 | [diff] [blame] | 977 | |
| 978 | Qualifiers ParamQuals = Param.getQualifiers(); |
| 979 | Qualifiers ArgQuals = Arg.getQualifiers(); |
| 980 | if (ParamQuals.isStrictSupersetOf(ArgQuals)) |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 981 | Comparison.Qualifiers = ParamMoreQualified; |
Douglas Gregor | 85894a8 | 2011-04-30 17:07:52 +0000 | [diff] [blame] | 982 | else if (ArgQuals.isStrictSupersetOf(ParamQuals)) |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 983 | Comparison.Qualifiers = ArgMoreQualified; |
Douglas Gregor | 6beabee | 2014-01-02 19:42:02 +0000 | [diff] [blame] | 984 | else if (ArgQuals.getObjCLifetime() != ParamQuals.getObjCLifetime() && |
| 985 | ArgQuals.withoutObjCLifetime() |
| 986 | == ParamQuals.withoutObjCLifetime()) { |
| 987 | // Prefer binding to non-__unsafe_autoretained parameters. |
| 988 | if (ArgQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone && |
| 989 | ParamQuals.getObjCLifetime()) |
| 990 | Comparison.Qualifiers = ParamMoreQualified; |
| 991 | else if (ParamQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone && |
| 992 | ArgQuals.getObjCLifetime()) |
| 993 | Comparison.Qualifiers = ArgMoreQualified; |
| 994 | } |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 995 | RefParamComparisons->push_back(Comparison); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 996 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 997 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 998 | // C++0x [temp.deduct.partial]p7: |
| 999 | // Remove any top-level cv-qualifiers: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1000 | // - 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] | 1001 | // version of P. |
| 1002 | Param = Param.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1003 | // - 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] | 1004 | // version of A. |
| 1005 | Arg = Arg.getUnqualifiedType(); |
| 1006 | } else { |
| 1007 | // C++0x [temp.deduct.call]p4 bullet 1: |
| 1008 | // - If the original P is a reference type, the deduced A (i.e., the type |
| 1009 | // referred to by the reference) can be more cv-qualified than the |
| 1010 | // transformed A. |
| 1011 | if (TDF & TDF_ParamWithReferenceType) { |
| 1012 | Qualifiers Quals; |
| 1013 | QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals); |
| 1014 | Quals.setCVRQualifiers(Quals.getCVRQualifiers() & |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 1015 | Arg.getCVRQualifiers()); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1016 | Param = S.Context.getQualifiedType(UnqualParam, Quals); |
| 1017 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1018 | |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1019 | if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) { |
| 1020 | // C++0x [temp.deduct.type]p10: |
| 1021 | // If P and A are function types that originated from deduction when |
| 1022 | // taking the address of a function template (14.8.2.2) or when deducing |
| 1023 | // 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] | 1024 | // Ai are parameters of the top-level parameter-type-list of P and A, |
| 1025 | // respectively, Pi is adjusted if it is an rvalue reference to a |
| 1026 | // cv-unqualified template parameter and Ai is an lvalue reference, in |
| 1027 | // 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] | 1028 | // type (i.e., T&& is changed to simply T). [ Note: As a result, when |
| 1029 | // 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] | 1030 | // deduced as X&. - end note ] |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1031 | TDF &= ~TDF_TopLevelParameterTypeList; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1032 | |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1033 | if (const RValueReferenceType *ParamRef |
| 1034 | = Param->getAs<RValueReferenceType>()) { |
| 1035 | if (isa<TemplateTypeParmType>(ParamRef->getPointeeType()) && |
| 1036 | !ParamRef->getPointeeType().getQualifiers()) |
| 1037 | if (Arg->isLValueReferenceType()) |
| 1038 | Param = ParamRef->getPointeeType(); |
| 1039 | } |
| 1040 | } |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1041 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1042 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1043 | // C++ [temp.deduct.type]p9: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1044 | // A template type argument T, a template template argument TT or a |
| 1045 | // 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] | 1046 | // the following forms: |
| 1047 | // |
| 1048 | // T |
| 1049 | // cv-list T |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | if (const TemplateTypeParmType *TemplateTypeParm |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1051 | = Param->getAs<TemplateTypeParmType>()) { |
Douglas Gregor | 4ea5dec | 2011-09-22 15:57:07 +0000 | [diff] [blame] | 1052 | // Just skip any attempts to deduce from a placeholder type. |
| 1053 | if (Arg->isPlaceholderType()) |
| 1054 | return Sema::TDK_Success; |
| 1055 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1056 | unsigned Index = TemplateTypeParm->getIndex(); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1057 | bool RecanonicalizeArg = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1058 | |
Douglas Gregor | 6045482 | 2009-07-22 20:02:25 +0000 | [diff] [blame] | 1059 | // If the argument type is an array type, move the qualifiers up to the |
| 1060 | // 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] | 1061 | if (isa<ArrayType>(Arg)) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1062 | Qualifiers Quals; |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1063 | Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1064 | if (Quals) { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1065 | Arg = S.Context.getQualifiedType(Arg, Quals); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1066 | RecanonicalizeArg = true; |
| 1067 | } |
| 1068 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1069 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1070 | // The argument type can not be less qualified than the parameter |
| 1071 | // type. |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1072 | if (!(TDF & TDF_IgnoreQualifiers) && |
| 1073 | hasInconsistentOrSupersetQualifiersOf(Param, Arg)) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1074 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1075 | Info.FirstArg = TemplateArgument(Param); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1076 | Info.SecondArg = TemplateArgument(Arg); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1077 | return Sema::TDK_Underqualified; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1078 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1079 | |
| 1080 | assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0"); |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1081 | assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function"); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1082 | QualType DeducedType = Arg; |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1083 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1084 | // Remove any qualifiers on the parameter from the deduced type. |
| 1085 | // We checked the qualifiers for consistency above. |
| 1086 | Qualifiers DeducedQs = DeducedType.getQualifiers(); |
| 1087 | Qualifiers ParamQs = Param.getQualifiers(); |
| 1088 | DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers()); |
| 1089 | if (ParamQs.hasObjCGCAttr()) |
| 1090 | DeducedQs.removeObjCGCAttr(); |
| 1091 | if (ParamQs.hasAddressSpace()) |
| 1092 | DeducedQs.removeAddressSpace(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1093 | if (ParamQs.hasObjCLifetime()) |
| 1094 | DeducedQs.removeObjCLifetime(); |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1095 | |
| 1096 | // Objective-C ARC: |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1097 | // If template deduction would produce a lifetime qualifier on a type |
| 1098 | // that is not a lifetime type, template argument deduction fails. |
| 1099 | if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType() && |
| 1100 | !DeducedType->isDependentType()) { |
| 1101 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1102 | Info.FirstArg = TemplateArgument(Param); |
| 1103 | Info.SecondArg = TemplateArgument(Arg); |
| 1104 | return Sema::TDK_Underqualified; |
| 1105 | } |
| 1106 | |
| 1107 | // Objective-C ARC: |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1108 | // If template deduction would produce an argument type with lifetime type |
| 1109 | // but no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1110 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1111 | DeducedType->isObjCLifetimeType() && |
| 1112 | !DeducedQs.hasObjCLifetime()) |
| 1113 | DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 1114 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1115 | DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(), |
| 1116 | DeducedQs); |
| 1117 | |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1118 | if (RecanonicalizeArg) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1119 | DeducedType = S.Context.getCanonicalType(DeducedType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1120 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1121 | DeducedTemplateArgument NewDeduced(DeducedType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1122 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1123 | Deduced[Index], |
| 1124 | NewDeduced); |
| 1125 | if (Result.isNull()) { |
| 1126 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1127 | Info.FirstArg = Deduced[Index]; |
| 1128 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1129 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1130 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1131 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1132 | Deduced[Index] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1133 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1136 | // Set up the template argument deduction information for a failure. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1137 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1138 | Info.SecondArg = TemplateArgument(ArgIn); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1139 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 1140 | // If the parameter is an already-substituted template parameter |
| 1141 | // pack, do nothing: we don't know which of its arguments to look |
| 1142 | // at, so we have to wait until all of the parameter packs in this |
| 1143 | // expansion have arguments. |
| 1144 | if (isa<SubstTemplateTypeParmPackType>(Param)) |
| 1145 | return Sema::TDK_Success; |
| 1146 | |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1147 | // Check the cv-qualifiers on the parameter and argument types. |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1148 | CanQualType CanParam = S.Context.getCanonicalType(Param); |
| 1149 | CanQualType CanArg = S.Context.getCanonicalType(Arg); |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1150 | if (!(TDF & TDF_IgnoreQualifiers)) { |
| 1151 | if (TDF & TDF_ParamWithReferenceType) { |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1152 | if (hasInconsistentOrSupersetQualifiersOf(Param, Arg)) |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1153 | return Sema::TDK_NonDeducedMismatch; |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 1154 | } else if (!IsPossiblyOpaquelyQualifiedType(Param)) { |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1155 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1156 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1157 | } |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1158 | |
| 1159 | // If the parameter type is not dependent, there is nothing to deduce. |
| 1160 | if (!Param->isDependentType()) { |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1161 | if (!(TDF & TDF_SkipNonDependent)) { |
| 1162 | bool NonDeduced = (TDF & TDF_InOverloadResolution)? |
| 1163 | !S.isSameOrCompatibleFunctionType(CanParam, CanArg) : |
| 1164 | Param != Arg; |
| 1165 | if (NonDeduced) { |
| 1166 | return Sema::TDK_NonDeducedMismatch; |
| 1167 | } |
| 1168 | } |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1169 | return Sema::TDK_Success; |
| 1170 | } |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1171 | } else if (!Param->isDependentType()) { |
| 1172 | CanQualType ParamUnqualType = CanParam.getUnqualifiedType(), |
| 1173 | ArgUnqualType = CanArg.getUnqualifiedType(); |
| 1174 | bool Success = (TDF & TDF_InOverloadResolution)? |
| 1175 | S.isSameOrCompatibleFunctionType(ParamUnqualType, |
| 1176 | ArgUnqualType) : |
| 1177 | ParamUnqualType == ArgUnqualType; |
| 1178 | if (Success) |
| 1179 | return Sema::TDK_Success; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1180 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1181 | |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1182 | switch (Param->getTypeClass()) { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1183 | // Non-canonical types cannot appear here. |
| 1184 | #define NON_CANONICAL_TYPE(Class, Base) \ |
| 1185 | case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class); |
| 1186 | #define TYPE(Class, Base) |
| 1187 | #include "clang/AST/TypeNodes.def" |
| 1188 | |
| 1189 | case Type::TemplateTypeParm: |
| 1190 | case Type::SubstTemplateTypeParmPack: |
| 1191 | llvm_unreachable("Type nodes handled above"); |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1192 | |
| 1193 | // These types cannot be dependent, so simply check whether the types are |
| 1194 | // the same. |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1195 | case Type::Builtin: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1196 | case Type::VariableArray: |
| 1197 | case Type::Vector: |
| 1198 | case Type::FunctionNoProto: |
| 1199 | case Type::Record: |
| 1200 | case Type::Enum: |
| 1201 | case Type::ObjCObject: |
| 1202 | case Type::ObjCInterface: |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1203 | case Type::ObjCObjectPointer: { |
| 1204 | if (TDF & TDF_SkipNonDependent) |
| 1205 | return Sema::TDK_Success; |
| 1206 | |
| 1207 | if (TDF & TDF_IgnoreQualifiers) { |
| 1208 | Param = Param.getUnqualifiedType(); |
| 1209 | Arg = Arg.getUnqualifiedType(); |
| 1210 | } |
| 1211 | |
| 1212 | return Param == Arg? Sema::TDK_Success : Sema::TDK_NonDeducedMismatch; |
| 1213 | } |
| 1214 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1215 | // _Complex T [placeholder extension] |
| 1216 | case Type::Complex: |
| 1217 | if (const ComplexType *ComplexArg = Arg->getAs<ComplexType>()) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1218 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1219 | cast<ComplexType>(Param)->getElementType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1220 | ComplexArg->getElementType(), |
| 1221 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1222 | |
| 1223 | return Sema::TDK_NonDeducedMismatch; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1224 | |
| 1225 | // _Atomic T [extension] |
| 1226 | case Type::Atomic: |
| 1227 | if (const AtomicType *AtomicArg = Arg->getAs<AtomicType>()) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1228 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1229 | cast<AtomicType>(Param)->getValueType(), |
| 1230 | AtomicArg->getValueType(), |
| 1231 | Info, Deduced, TDF); |
| 1232 | |
| 1233 | return Sema::TDK_NonDeducedMismatch; |
| 1234 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1235 | // T * |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1236 | case Type::Pointer: { |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1237 | QualType PointeeType; |
| 1238 | if (const PointerType *PointerArg = Arg->getAs<PointerType>()) { |
| 1239 | PointeeType = PointerArg->getPointeeType(); |
| 1240 | } else if (const ObjCObjectPointerType *PointerArg |
| 1241 | = Arg->getAs<ObjCObjectPointerType>()) { |
| 1242 | PointeeType = PointerArg->getPointeeType(); |
| 1243 | } else { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1244 | return Sema::TDK_NonDeducedMismatch; |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1245 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1246 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1247 | unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1248 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1249 | cast<PointerType>(Param)->getPointeeType(), |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1250 | PointeeType, |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1251 | Info, Deduced, SubTDF); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1252 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1253 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1254 | // T & |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1255 | case Type::LValueReference: { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1256 | const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1257 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1258 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1259 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1260 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1261 | cast<LValueReferenceType>(Param)->getPointeeType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1262 | ReferenceArg->getPointeeType(), Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1263 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1264 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1265 | // T && [C++0x] |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1266 | case Type::RValueReference: { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1267 | const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1268 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1269 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1270 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1271 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1272 | cast<RValueReferenceType>(Param)->getPointeeType(), |
| 1273 | ReferenceArg->getPointeeType(), |
| 1274 | Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1275 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1276 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1277 | // T [] (implied, but not stated explicitly) |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1278 | case Type::IncompleteArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1279 | const IncompleteArrayType *IncompleteArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1280 | S.Context.getAsIncompleteArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1281 | if (!IncompleteArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1282 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1283 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1284 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1285 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1286 | S.Context.getAsIncompleteArrayType(Param)->getElementType(), |
| 1287 | IncompleteArrayArg->getElementType(), |
| 1288 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1289 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1290 | |
| 1291 | // T [integer-constant] |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1292 | case Type::ConstantArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1293 | const ConstantArrayType *ConstantArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1294 | S.Context.getAsConstantArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1295 | if (!ConstantArrayArg) |
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 | |
| 1298 | const ConstantArrayType *ConstantArrayParm = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1299 | S.Context.getAsConstantArrayType(Param); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1300 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1301 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1302 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1303 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1304 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1305 | ConstantArrayParm->getElementType(), |
| 1306 | ConstantArrayArg->getElementType(), |
| 1307 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1310 | // type [i] |
| 1311 | case Type::DependentSizedArray: { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1312 | const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1313 | if (!ArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1314 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1316 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
| 1317 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1318 | // Check the element type of the arrays |
| 1319 | const DependentSizedArrayType *DependentArrayParm |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1320 | = S.Context.getAsDependentSizedArrayType(Param); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1321 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1322 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1323 | DependentArrayParm->getElementType(), |
| 1324 | ArrayArg->getElementType(), |
| 1325 | Info, Deduced, SubTDF)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1326 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1327 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1328 | // Determine the array bound is something we can deduce. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1329 | NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1330 | = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr()); |
| 1331 | if (!NTTP) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1332 | return Sema::TDK_Success; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1333 | |
| 1334 | // We can perform template argument deduction for the given non-type |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1335 | // template parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1336 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1337 | "Cannot deduce non-type template argument at depth > 0"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1338 | if (const ConstantArrayType *ConstantArrayArg |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1339 | = dyn_cast<ConstantArrayType>(ArrayArg)) { |
| 1340 | llvm::APSInt Size(ConstantArrayArg->getSize()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1341 | return DeduceNonTypeTemplateArgument(S, NTTP, Size, |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1342 | S.Context.getSizeType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1343 | /*ArrayBound=*/true, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1344 | Info, Deduced); |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1345 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1346 | if (const DependentSizedArrayType *DependentArrayArg |
| 1347 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1348 | if (DependentArrayArg->getSizeExpr()) |
| 1349 | return DeduceNonTypeTemplateArgument(S, NTTP, |
| 1350 | DependentArrayArg->getSizeExpr(), |
| 1351 | Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1352 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1353 | // Incomplete type does not match a dependently-sized array type |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1354 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1355 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1356 | |
| 1357 | // type(*)(T) |
| 1358 | // T(*)() |
| 1359 | // T(*)(T) |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1360 | case Type::FunctionProto: { |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1361 | unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1362 | const FunctionProtoType *FunctionProtoArg = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1363 | dyn_cast<FunctionProtoType>(Arg); |
| 1364 | if (!FunctionProtoArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1365 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1366 | |
| 1367 | const FunctionProtoType *FunctionProtoParam = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1368 | cast<FunctionProtoType>(Param); |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1369 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1370 | if (FunctionProtoParam->getTypeQuals() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1371 | != FunctionProtoArg->getTypeQuals() || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1372 | FunctionProtoParam->getRefQualifier() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1373 | != FunctionProtoArg->getRefQualifier() || |
| 1374 | FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1375 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1376 | |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1377 | // Check return types. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1378 | if (Sema::TemplateDeductionResult Result = |
| 1379 | DeduceTemplateArgumentsByTypeMatch( |
| 1380 | S, TemplateParams, FunctionProtoParam->getReturnType(), |
| 1381 | FunctionProtoArg->getReturnType(), Info, Deduced, 0)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1382 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1383 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1384 | return DeduceTemplateArguments( |
| 1385 | S, TemplateParams, FunctionProtoParam->param_type_begin(), |
| 1386 | FunctionProtoParam->getNumParams(), |
| 1387 | FunctionProtoArg->param_type_begin(), |
| 1388 | FunctionProtoArg->getNumParams(), Info, Deduced, SubTDF); |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1389 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1390 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1391 | case Type::InjectedClassName: { |
| 1392 | // Treat a template's injected-class-name as if the template |
| 1393 | // specialization type had been used. |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1394 | Param = cast<InjectedClassNameType>(Param) |
| 1395 | ->getInjectedSpecializationType(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1396 | assert(isa<TemplateSpecializationType>(Param) && |
| 1397 | "injected class name is not a template specialization type"); |
| 1398 | // fall through |
| 1399 | } |
| 1400 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1401 | // template-name<T> (where template-name refers to a class template) |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1402 | // template-name<i> |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1403 | // TT<T> |
| 1404 | // TT<i> |
| 1405 | // TT<> |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1406 | case Type::TemplateSpecialization: { |
| 1407 | const TemplateSpecializationType *SpecParam |
| 1408 | = cast<TemplateSpecializationType>(Param); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1409 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1410 | // Try to deduce template arguments from the template-id. |
| 1411 | Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1412 | = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1413 | Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | |
Douglas Gregor | 4290975 | 2009-09-30 22:13:51 +0000 | [diff] [blame] | 1415 | if (Result && (TDF & TDF_DerivedClass)) { |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1416 | // C++ [temp.deduct.call]p3b3: |
| 1417 | // If P is a class, and P has the form template-id, then A can be a |
| 1418 | // 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] | 1419 | // 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] | 1420 | // class pointed to by the deduced A. |
| 1421 | // |
| 1422 | // More importantly: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1423 | // These alternatives are considered only if type deduction would |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1424 | // otherwise fail. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1425 | if (const RecordType *RecordT = Arg->getAs<RecordType>()) { |
| 1426 | // We cannot inspect base classes as part of deduction when the type |
| 1427 | // is incomplete, so either instantiate any templates necessary to |
| 1428 | // complete the type, or skip over it if it cannot be completed. |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1429 | if (S.RequireCompleteType(Info.getLocation(), Arg, 0)) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1430 | return Result; |
| 1431 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1432 | // Use data recursion to crawl through the list of base classes. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | // Visited contains the set of nodes we have already visited, while |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1434 | // ToVisit is our stack of records that we still need to visit. |
| 1435 | llvm::SmallPtrSet<const RecordType *, 8> Visited; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1436 | SmallVector<const RecordType *, 8> ToVisit; |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1437 | ToVisit.push_back(RecordT); |
| 1438 | bool Successful = false; |
Benjamin Kramer | 4d08ccb | 2012-01-20 16:39:18 +0000 | [diff] [blame] | 1439 | SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(), |
| 1440 | Deduced.end()); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1441 | while (!ToVisit.empty()) { |
| 1442 | // Retrieve the next class in the inheritance hierarchy. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 1443 | const RecordType *NextT = ToVisit.pop_back_val(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1444 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1445 | // If we have already seen this type, skip it. |
| 1446 | if (!Visited.insert(NextT)) |
| 1447 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1448 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1449 | // If this is a base class, try to perform template argument |
| 1450 | // deduction from it. |
| 1451 | if (NextT != RecordT) { |
Richard Trieu | 23bafad | 2012-11-07 21:17:13 +0000 | [diff] [blame] | 1452 | TemplateDeductionInfo BaseInfo(Info.getLocation()); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1453 | Sema::TemplateDeductionResult BaseResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1454 | = DeduceTemplateArguments(S, TemplateParams, SpecParam, |
Richard Trieu | 23bafad | 2012-11-07 21:17:13 +0000 | [diff] [blame] | 1455 | QualType(NextT, 0), BaseInfo, |
| 1456 | Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1458 | // If template argument deduction for this base was successful, |
Douglas Gregor | e0f7a8a | 2010-11-02 00:02:34 +0000 | [diff] [blame] | 1459 | // note that we had some success. Otherwise, ignore any deductions |
| 1460 | // from this base class. |
| 1461 | if (BaseResult == Sema::TDK_Success) { |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1462 | Successful = true; |
Benjamin Kramer | 4d08ccb | 2012-01-20 16:39:18 +0000 | [diff] [blame] | 1463 | DeducedOrig.clear(); |
| 1464 | DeducedOrig.append(Deduced.begin(), Deduced.end()); |
Richard Trieu | 23bafad | 2012-11-07 21:17:13 +0000 | [diff] [blame] | 1465 | Info.Param = BaseInfo.Param; |
| 1466 | Info.FirstArg = BaseInfo.FirstArg; |
| 1467 | Info.SecondArg = BaseInfo.SecondArg; |
Douglas Gregor | e0f7a8a | 2010-11-02 00:02:34 +0000 | [diff] [blame] | 1468 | } |
| 1469 | else |
| 1470 | Deduced = DeducedOrig; |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1471 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1472 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1473 | // Visit base classes |
| 1474 | CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1475 | for (const auto &Base : Next->bases()) { |
| 1476 | assert(Base.getType()->isRecordType() && |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1477 | "Base class that isn't a record?"); |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1478 | ToVisit.push_back(Base.getType()->getAs<RecordType>()); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1479 | } |
| 1480 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1481 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1482 | if (Successful) |
| 1483 | return Sema::TDK_Success; |
| 1484 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1485 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1486 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1488 | return Result; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1491 | // T type::* |
| 1492 | // T T::* |
| 1493 | // T (type::*)() |
| 1494 | // type (T::*)() |
| 1495 | // type (type::*)(T) |
| 1496 | // type (T::*)(T) |
| 1497 | // T (type::*)(T) |
| 1498 | // T (T::*)() |
| 1499 | // T (T::*)(T) |
| 1500 | case Type::MemberPointer: { |
| 1501 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 1502 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 1503 | if (!MemPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1504 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1505 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1506 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1507 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1508 | MemPtrParam->getPointeeType(), |
| 1509 | MemPtrArg->getPointeeType(), |
| 1510 | Info, Deduced, |
| 1511 | TDF & TDF_IgnoreQualifiers)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1512 | return Result; |
| 1513 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1514 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1515 | QualType(MemPtrParam->getClass(), 0), |
| 1516 | QualType(MemPtrArg->getClass(), 0), |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1517 | Info, Deduced, |
| 1518 | TDF & TDF_IgnoreQualifiers); |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Anders Carlsson | 15f1dd1 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1521 | // (clang extension) |
| 1522 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1523 | // type(^)(T) |
| 1524 | // T(^)() |
| 1525 | // T(^)(T) |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1526 | case Type::BlockPointer: { |
| 1527 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 1528 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1529 | |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1530 | if (!BlockPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1531 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1533 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1534 | BlockPtrParam->getPointeeType(), |
| 1535 | BlockPtrArg->getPointeeType(), |
| 1536 | Info, Deduced, 0); |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1539 | // (clang extension) |
| 1540 | // |
| 1541 | // T __attribute__(((ext_vector_type(<integral constant>)))) |
| 1542 | case Type::ExtVector: { |
| 1543 | const ExtVectorType *VectorParam = cast<ExtVectorType>(Param); |
| 1544 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1545 | // Make sure that the vectors have the same number of elements. |
| 1546 | if (VectorParam->getNumElements() != VectorArg->getNumElements()) |
| 1547 | return Sema::TDK_NonDeducedMismatch; |
| 1548 | |
| 1549 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1550 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1551 | VectorParam->getElementType(), |
| 1552 | VectorArg->getElementType(), |
| 1553 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | if (const DependentSizedExtVectorType *VectorArg |
| 1557 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1558 | // We can't check the number of elements, since the argument has a |
| 1559 | // dependent number of elements. This can only occur during partial |
| 1560 | // ordering. |
| 1561 | |
| 1562 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1563 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1564 | VectorParam->getElementType(), |
| 1565 | VectorArg->getElementType(), |
| 1566 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | return Sema::TDK_NonDeducedMismatch; |
| 1570 | } |
| 1571 | |
| 1572 | // (clang extension) |
| 1573 | // |
| 1574 | // T __attribute__(((ext_vector_type(N)))) |
| 1575 | case Type::DependentSizedExtVector: { |
| 1576 | const DependentSizedExtVectorType *VectorParam |
| 1577 | = cast<DependentSizedExtVectorType>(Param); |
| 1578 | |
| 1579 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1580 | // Perform deduction on the element types. |
| 1581 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1582 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1583 | VectorParam->getElementType(), |
| 1584 | VectorArg->getElementType(), |
| 1585 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1586 | return Result; |
| 1587 | |
| 1588 | // Perform deduction on the vector size, if we can. |
| 1589 | NonTypeTemplateParmDecl *NTTP |
| 1590 | = getDeducedParameterFromExpr(VectorParam->getSizeExpr()); |
| 1591 | if (!NTTP) |
| 1592 | return Sema::TDK_Success; |
| 1593 | |
| 1594 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
| 1595 | ArgSize = VectorArg->getNumElements(); |
| 1596 | return DeduceNonTypeTemplateArgument(S, NTTP, ArgSize, S.Context.IntTy, |
| 1597 | false, Info, Deduced); |
| 1598 | } |
| 1599 | |
| 1600 | if (const DependentSizedExtVectorType *VectorArg |
| 1601 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1602 | // Perform deduction on the element types. |
| 1603 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1604 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1605 | VectorParam->getElementType(), |
| 1606 | VectorArg->getElementType(), |
| 1607 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1608 | return Result; |
| 1609 | |
| 1610 | // Perform deduction on the vector size, if we can. |
| 1611 | NonTypeTemplateParmDecl *NTTP |
| 1612 | = getDeducedParameterFromExpr(VectorParam->getSizeExpr()); |
| 1613 | if (!NTTP) |
| 1614 | return Sema::TDK_Success; |
| 1615 | |
| 1616 | return DeduceNonTypeTemplateArgument(S, NTTP, VectorArg->getSizeExpr(), |
| 1617 | Info, Deduced); |
| 1618 | } |
| 1619 | |
| 1620 | return Sema::TDK_NonDeducedMismatch; |
| 1621 | } |
| 1622 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1623 | case Type::TypeOfExpr: |
| 1624 | case Type::TypeOf: |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1625 | case Type::DependentName: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1626 | case Type::UnresolvedUsing: |
| 1627 | case Type::Decltype: |
| 1628 | case Type::UnaryTransform: |
| 1629 | case Type::Auto: |
| 1630 | case Type::DependentTemplateSpecialization: |
| 1631 | case Type::PackExpansion: |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1632 | // No template argument deduction for these types |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1633 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1636 | llvm_unreachable("Invalid Type Class!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1639 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1640 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1641 | TemplateParameterList *TemplateParams, |
| 1642 | const TemplateArgument &Param, |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1643 | TemplateArgument Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1644 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1645 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1646 | // If the template argument is a pack expansion, perform template argument |
| 1647 | // deduction against the pattern of that expansion. This only occurs during |
| 1648 | // partial ordering. |
| 1649 | if (Arg.isPackExpansion()) |
| 1650 | Arg = Arg.getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1651 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1652 | switch (Param.getKind()) { |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1653 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1654 | llvm_unreachable("Null template argument in parameter list"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1655 | |
| 1656 | case TemplateArgument::Type: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1657 | if (Arg.getKind() == TemplateArgument::Type) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1658 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1659 | Param.getAsType(), |
| 1660 | Arg.getAsType(), |
| 1661 | Info, Deduced, 0); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1662 | Info.FirstArg = Param; |
| 1663 | Info.SecondArg = Arg; |
| 1664 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1665 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1666 | case TemplateArgument::Template: |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1667 | if (Arg.getKind() == TemplateArgument::Template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1668 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1669 | Param.getAsTemplate(), |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1670 | Arg.getAsTemplate(), Info, Deduced); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1671 | Info.FirstArg = Param; |
| 1672 | Info.SecondArg = Arg; |
| 1673 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1674 | |
| 1675 | case TemplateArgument::TemplateExpansion: |
| 1676 | llvm_unreachable("caller should handle pack expansions"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1677 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1678 | case TemplateArgument::Declaration: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1679 | if (Arg.getKind() == TemplateArgument::Declaration && |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 1680 | isSameDeclaration(Param.getAsDecl(), Arg.getAsDecl()) && |
| 1681 | Param.isDeclForReferenceParam() == Arg.isDeclForReferenceParam()) |
| 1682 | return Sema::TDK_Success; |
| 1683 | |
| 1684 | Info.FirstArg = Param; |
| 1685 | Info.SecondArg = Arg; |
| 1686 | return Sema::TDK_NonDeducedMismatch; |
| 1687 | |
| 1688 | case TemplateArgument::NullPtr: |
| 1689 | if (Arg.getKind() == TemplateArgument::NullPtr && |
| 1690 | S.Context.hasSameType(Param.getNullPtrType(), Arg.getNullPtrType())) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1691 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1692 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1693 | Info.FirstArg = Param; |
| 1694 | Info.SecondArg = Arg; |
| 1695 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1696 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1697 | case TemplateArgument::Integral: |
| 1698 | if (Arg.getKind() == TemplateArgument::Integral) { |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1699 | if (hasSameExtendedValue(Param.getAsIntegral(), Arg.getAsIntegral())) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1700 | return Sema::TDK_Success; |
| 1701 | |
| 1702 | Info.FirstArg = Param; |
| 1703 | Info.SecondArg = Arg; |
| 1704 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1705 | } |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1706 | |
| 1707 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 1708 | Info.FirstArg = Param; |
| 1709 | Info.SecondArg = Arg; |
| 1710 | return Sema::TDK_NonDeducedMismatch; |
| 1711 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1712 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1713 | Info.FirstArg = Param; |
| 1714 | Info.SecondArg = Arg; |
| 1715 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1716 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1717 | case TemplateArgument::Expression: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1718 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1719 | = getDeducedParameterFromExpr(Param.getAsExpr())) { |
| 1720 | if (Arg.getKind() == TemplateArgument::Integral) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1721 | return DeduceNonTypeTemplateArgument(S, NTTP, |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1722 | Arg.getAsIntegral(), |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1723 | Arg.getIntegralType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1724 | /*ArrayBound=*/false, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1725 | Info, Deduced); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1726 | if (Arg.getKind() == TemplateArgument::Expression) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1727 | return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(), |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1728 | Info, Deduced); |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1729 | if (Arg.getKind() == TemplateArgument::Declaration) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1730 | return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(), |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1731 | Info, Deduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1732 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1733 | Info.FirstArg = Param; |
| 1734 | Info.SecondArg = Arg; |
| 1735 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1736 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1737 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1738 | // Can't deduce anything, but that's okay. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1739 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1740 | } |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 1741 | case TemplateArgument::Pack: |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1742 | llvm_unreachable("Argument packs should be expanded by the caller!"); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1743 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1744 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1745 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1748 | /// \brief Determine whether there is a template argument to be used for |
| 1749 | /// deduction. |
| 1750 | /// |
| 1751 | /// This routine "expands" argument packs in-place, overriding its input |
| 1752 | /// parameters so that \c Args[ArgIdx] will be the available template argument. |
| 1753 | /// |
| 1754 | /// \returns true if there is another template argument (which will be at |
| 1755 | /// \c Args[ArgIdx]), false otherwise. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1756 | static bool hasTemplateArgumentForDeduction(const TemplateArgument *&Args, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1757 | unsigned &ArgIdx, |
| 1758 | unsigned &NumArgs) { |
| 1759 | if (ArgIdx == NumArgs) |
| 1760 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1761 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1762 | const TemplateArgument &Arg = Args[ArgIdx]; |
| 1763 | if (Arg.getKind() != TemplateArgument::Pack) |
| 1764 | return true; |
| 1765 | |
| 1766 | assert(ArgIdx == NumArgs - 1 && "Pack not at the end of argument list?"); |
| 1767 | Args = Arg.pack_begin(); |
| 1768 | NumArgs = Arg.pack_size(); |
| 1769 | ArgIdx = 0; |
| 1770 | return ArgIdx < NumArgs; |
| 1771 | } |
| 1772 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1773 | /// \brief Determine whether the given set of template arguments has a pack |
| 1774 | /// expansion that is not the last template argument. |
| 1775 | static bool hasPackExpansionBeforeEnd(const TemplateArgument *Args, |
| 1776 | unsigned NumArgs) { |
| 1777 | unsigned ArgIdx = 0; |
| 1778 | while (ArgIdx < NumArgs) { |
| 1779 | const TemplateArgument &Arg = Args[ArgIdx]; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1780 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1781 | // Unwrap argument packs. |
| 1782 | if (Args[ArgIdx].getKind() == TemplateArgument::Pack) { |
| 1783 | Args = Arg.pack_begin(); |
| 1784 | NumArgs = Arg.pack_size(); |
| 1785 | ArgIdx = 0; |
| 1786 | continue; |
| 1787 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1788 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1789 | ++ArgIdx; |
| 1790 | if (ArgIdx == NumArgs) |
| 1791 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1792 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1793 | if (Arg.isPackExpansion()) |
| 1794 | return true; |
| 1795 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1796 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1797 | return false; |
| 1798 | } |
| 1799 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1800 | static Sema::TemplateDeductionResult |
| 1801 | DeduceTemplateArguments(Sema &S, |
| 1802 | TemplateParameterList *TemplateParams, |
| 1803 | const TemplateArgument *Params, unsigned NumParams, |
| 1804 | const TemplateArgument *Args, unsigned NumArgs, |
| 1805 | TemplateDeductionInfo &Info, |
Richard Smith | 16b6539 | 2012-12-06 06:44:44 +0000 | [diff] [blame] | 1806 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1807 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1808 | // If the template argument list of P contains a pack expansion that is not |
| 1809 | // the last template argument, the entire template argument list is a |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1810 | // non-deduced context. |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1811 | if (hasPackExpansionBeforeEnd(Params, NumParams)) |
| 1812 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1813 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1814 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1815 | // If P has a form that contains <T> or <i>, then each argument Pi of the |
| 1816 | // respective template argument list P is compared with the corresponding |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1817 | // argument Ai of the corresponding template argument list of A. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1818 | unsigned ArgIdx = 0, ParamIdx = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1819 | for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1820 | ++ParamIdx) { |
| 1821 | if (!Params[ParamIdx].isPackExpansion()) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1822 | // The simple case: deduce template arguments by matching Pi and Ai. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1823 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1824 | // Check whether we have enough arguments. |
| 1825 | if (!hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) |
Richard Smith | 16b6539 | 2012-12-06 06:44:44 +0000 | [diff] [blame] | 1826 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1827 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1828 | if (Args[ArgIdx].isPackExpansion()) { |
| 1829 | // FIXME: We follow the logic of C++0x [temp.deduct.type]p22 here, |
| 1830 | // but applied to pack expansions that are template arguments. |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 1831 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1832 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1833 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1834 | // Perform deduction for this Pi/Ai pair. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1835 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1836 | = DeduceTemplateArguments(S, TemplateParams, |
| 1837 | Params[ParamIdx], Args[ArgIdx], |
| 1838 | Info, Deduced)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1839 | return Result; |
| 1840 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1841 | // Move to the next argument. |
| 1842 | ++ArgIdx; |
| 1843 | continue; |
| 1844 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1845 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1846 | // The parameter is a pack expansion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1847 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1848 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1849 | // If Pi is a pack expansion, then the pattern of Pi is compared with |
| 1850 | // each remaining argument in the template argument list of A. Each |
| 1851 | // comparison deduces template arguments for subsequent positions in the |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1852 | // template parameter packs expanded by Pi. |
| 1853 | TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1854 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1855 | // Compute the set of template parameter indices that correspond to |
| 1856 | // parameter packs expanded by the pack expansion. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1857 | SmallVector<unsigned, 2> PackIndices; |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1858 | { |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 1859 | llvm::SmallBitVector SawIndices(TemplateParams->size()); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1860 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1861 | S.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| 1862 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 1863 | unsigned Depth, Index; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1864 | std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1865 | if (Depth == 0 && !SawIndices[Index]) { |
| 1866 | SawIndices[Index] = true; |
| 1867 | PackIndices.push_back(Index); |
| 1868 | } |
| 1869 | } |
| 1870 | } |
| 1871 | assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1872 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1873 | // FIXME: If there are no remaining arguments, we can bail out early |
| 1874 | // and set any deduced parameter packs to an empty argument pack. |
| 1875 | // The latter part of this is a (minor) correctness issue. |
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 | // Save the deduced template arguments for each parameter pack expanded |
| 1878 | // by this pack expansion, then clear out the deduction. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1879 | SmallVector<DeducedTemplateArgument, 2> |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1880 | SavedPacks(PackIndices.size()); |
Craig Topper | 0a4e1f5 | 2013-07-08 04:44:01 +0000 | [diff] [blame] | 1881 | NewlyDeducedPacksType NewlyDeducedPacks(PackIndices.size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1882 | PrepareArgumentPackDeduction(S, Deduced, PackIndices, SavedPacks, |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 1883 | NewlyDeducedPacks); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1884 | |
| 1885 | // Keep track of the deduced template arguments for each parameter pack |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1886 | // expanded by this pack expansion (the outer index) and for each |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1887 | // template argument (the inner SmallVectors). |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1888 | bool HasAnyArguments = false; |
| 1889 | while (hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) { |
| 1890 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1891 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1892 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1893 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1894 | = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx], |
| 1895 | Info, Deduced)) |
| 1896 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1897 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1898 | // Capture the deduced template arguments for each parameter pack expanded |
| 1899 | // by this pack expansion, add them to the list of arguments we've deduced |
| 1900 | // for that pack, then clear out the deduced argument. |
| 1901 | for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) { |
| 1902 | DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]]; |
| 1903 | if (!DeducedArg.isNull()) { |
| 1904 | NewlyDeducedPacks[I].push_back(DeducedArg); |
| 1905 | DeducedArg = DeducedTemplateArgument(); |
| 1906 | } |
| 1907 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1908 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1909 | ++ArgIdx; |
| 1910 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1911 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1912 | // Build argument packs for each of the parameter packs expanded by this |
| 1913 | // pack expansion. |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 1914 | if (Sema::TemplateDeductionResult Result |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1915 | = FinishArgumentPackDeduction(S, TemplateParams, HasAnyArguments, |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 1916 | Deduced, PackIndices, SavedPacks, |
| 1917 | NewlyDeducedPacks, Info)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1918 | return Result; |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1919 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1920 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1921 | return Sema::TDK_Success; |
| 1922 | } |
| 1923 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1924 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1925 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1926 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1927 | const TemplateArgumentList &ParamList, |
| 1928 | const TemplateArgumentList &ArgList, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1929 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1930 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1931 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1932 | ParamList.data(), ParamList.size(), |
| 1933 | ArgList.data(), ArgList.size(), |
| 1934 | Info, Deduced); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1937 | /// \brief Determine whether two template arguments are the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1938 | static bool isSameTemplateArg(ASTContext &Context, |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1939 | const TemplateArgument &X, |
| 1940 | const TemplateArgument &Y) { |
| 1941 | if (X.getKind() != Y.getKind()) |
| 1942 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1943 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1944 | switch (X.getKind()) { |
| 1945 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1946 | llvm_unreachable("Comparing NULL template argument"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1947 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1948 | case TemplateArgument::Type: |
| 1949 | return Context.getCanonicalType(X.getAsType()) == |
| 1950 | Context.getCanonicalType(Y.getAsType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1951 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1952 | case TemplateArgument::Declaration: |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 1953 | return isSameDeclaration(X.getAsDecl(), Y.getAsDecl()) && |
| 1954 | X.isDeclForReferenceParam() == Y.isDeclForReferenceParam(); |
| 1955 | |
| 1956 | case TemplateArgument::NullPtr: |
| 1957 | return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1958 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1959 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1960 | case TemplateArgument::TemplateExpansion: |
| 1961 | return Context.getCanonicalTemplateName( |
| 1962 | X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == |
| 1963 | Context.getCanonicalTemplateName( |
| 1964 | Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1965 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1966 | case TemplateArgument::Integral: |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1967 | return X.getAsIntegral() == Y.getAsIntegral(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1968 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1969 | case TemplateArgument::Expression: { |
| 1970 | llvm::FoldingSetNodeID XID, YID; |
| 1971 | X.getAsExpr()->Profile(XID, Context, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1972 | Y.getAsExpr()->Profile(YID, Context, true); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1973 | return XID == YID; |
| 1974 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1975 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1976 | case TemplateArgument::Pack: |
| 1977 | if (X.pack_size() != Y.pack_size()) |
| 1978 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1979 | |
| 1980 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
| 1981 | XPEnd = X.pack_end(), |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1982 | YP = Y.pack_begin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1983 | XP != XPEnd; ++XP, ++YP) |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1984 | if (!isSameTemplateArg(Context, *XP, *YP)) |
| 1985 | return false; |
| 1986 | |
| 1987 | return true; |
| 1988 | } |
| 1989 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1990 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1993 | /// \brief Allocate a TemplateArgumentLoc where all locations have |
| 1994 | /// been initialized to the given location. |
| 1995 | /// |
| 1996 | /// \param S The semantic analysis object. |
| 1997 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 1998 | /// \param Arg The template argument we are producing template argument |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1999 | /// location information for. |
| 2000 | /// |
| 2001 | /// \param NTTPType For a declaration template argument, the type of |
| 2002 | /// the non-type template parameter that corresponds to this template |
| 2003 | /// argument. |
| 2004 | /// |
| 2005 | /// \param Loc The source location to use for the resulting template |
| 2006 | /// argument. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2007 | static TemplateArgumentLoc |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2008 | getTrivialTemplateArgumentLoc(Sema &S, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2009 | const TemplateArgument &Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2010 | QualType NTTPType, |
| 2011 | SourceLocation Loc) { |
| 2012 | switch (Arg.getKind()) { |
| 2013 | case TemplateArgument::Null: |
| 2014 | llvm_unreachable("Can't get a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2015 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2016 | case TemplateArgument::Type: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2017 | return TemplateArgumentLoc(Arg, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2018 | S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2019 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2020 | case TemplateArgument::Declaration: { |
| 2021 | Expr *E |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 2022 | = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 2023 | .takeAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2024 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2025 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2026 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2027 | case TemplateArgument::NullPtr: { |
| 2028 | Expr *E |
| 2029 | = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2030 | .takeAs<Expr>(); |
| 2031 | return TemplateArgumentLoc(TemplateArgument(NTTPType, /*isNullPtr*/true), |
| 2032 | E); |
| 2033 | } |
| 2034 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2035 | case TemplateArgument::Integral: { |
| 2036 | Expr *E |
Douglas Gregor | eb29d18 | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 2037 | = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2038 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2039 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2040 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2041 | case TemplateArgument::Template: |
| 2042 | case TemplateArgument::TemplateExpansion: { |
| 2043 | NestedNameSpecifierLocBuilder Builder; |
| 2044 | TemplateName Template = Arg.getAsTemplate(); |
| 2045 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) |
| 2046 | Builder.MakeTrivial(S.Context, DTN->getQualifier(), Loc); |
| 2047 | else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2048 | Builder.MakeTrivial(S.Context, QTN->getQualifier(), Loc); |
| 2049 | |
| 2050 | if (Arg.getKind() == TemplateArgument::Template) |
| 2051 | return TemplateArgumentLoc(Arg, |
| 2052 | Builder.getWithLocInContext(S.Context), |
| 2053 | Loc); |
| 2054 | |
| 2055 | |
| 2056 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(S.Context), |
| 2057 | Loc, Loc); |
| 2058 | } |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2059 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2060 | case TemplateArgument::Expression: |
| 2061 | return TemplateArgumentLoc(Arg, Arg.getAsExpr()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2062 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2063 | case TemplateArgument::Pack: |
| 2064 | return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); |
| 2065 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2066 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2067 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | |
| 2071 | /// \brief Convert the given deduced template argument and add it to the set of |
| 2072 | /// fully-converted template arguments. |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2073 | static bool |
| 2074 | ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param, |
| 2075 | DeducedTemplateArgument Arg, |
| 2076 | NamedDecl *Template, |
| 2077 | QualType NTTPType, |
| 2078 | unsigned ArgumentPackIndex, |
| 2079 | TemplateDeductionInfo &Info, |
| 2080 | bool InFunctionTemplate, |
| 2081 | SmallVectorImpl<TemplateArgument> &Output) { |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2082 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 2083 | // This is a template argument pack, so check each of its arguments against |
| 2084 | // the template parameter. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2085 | SmallVector<TemplateArgument, 2> PackedArgsBuilder; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2086 | for (TemplateArgument::pack_iterator PA = Arg.pack_begin(), |
Douglas Gregor | f491ee2 | 2011-01-05 21:00:53 +0000 | [diff] [blame] | 2087 | PAEnd = Arg.pack_end(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2088 | PA != PAEnd; ++PA) { |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2089 | // When converting the deduced template argument, append it to the |
| 2090 | // general output list. We need to do this so that the template argument |
| 2091 | // checking logic has all of the prior template arguments available. |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2092 | DeducedTemplateArgument InnerArg(*PA); |
| 2093 | InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2094 | if (ConvertDeducedTemplateArgument(S, Param, InnerArg, Template, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2095 | NTTPType, PackedArgsBuilder.size(), |
| 2096 | Info, InFunctionTemplate, Output)) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2097 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2098 | |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2099 | // Move the converted template argument into our argument pack. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 2100 | PackedArgsBuilder.push_back(Output.pop_back_val()); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2101 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2102 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2103 | // Create the resulting argument pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2104 | Output.push_back(TemplateArgument::CreatePackCopy(S.Context, |
Douglas Gregor | 74c6d19 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 2105 | PackedArgsBuilder.data(), |
| 2106 | PackedArgsBuilder.size())); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2107 | return false; |
| 2108 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2109 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2110 | // Convert the deduced template argument into a template |
| 2111 | // argument that we can check, almost as if the user had written |
| 2112 | // the template argument explicitly. |
| 2113 | TemplateArgumentLoc ArgLoc = getTrivialTemplateArgumentLoc(S, Arg, NTTPType, |
| 2114 | Info.getLocation()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2115 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2116 | // Check the template argument, converting it as necessary. |
| 2117 | return S.CheckTemplateArgument(Param, ArgLoc, |
| 2118 | Template, |
| 2119 | Template->getLocation(), |
| 2120 | Template->getSourceRange().getEnd(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2121 | ArgumentPackIndex, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2122 | Output, |
| 2123 | InFunctionTemplate |
| 2124 | ? (Arg.wasDeducedFromArrayBound() |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2125 | ? Sema::CTAK_DeducedFromArrayBound |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2126 | : Sema::CTAK_Deduced) |
| 2127 | : Sema::CTAK_Specified); |
| 2128 | } |
| 2129 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2130 | /// Complete template argument deduction for a class template partial |
| 2131 | /// specialization. |
| 2132 | static Sema::TemplateDeductionResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2133 | FinishTemplateArgumentDeduction(Sema &S, |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2134 | ClassTemplatePartialSpecializationDecl *Partial, |
| 2135 | const TemplateArgumentList &TemplateArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2136 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2137 | TemplateDeductionInfo &Info) { |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2138 | // Unevaluated SFINAE context. |
| 2139 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2140 | Sema::SFINAETrap Trap(S); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2141 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2142 | Sema::ContextRAII SavedContext(S, Partial); |
| 2143 | |
| 2144 | // C++ [temp.deduct.type]p2: |
| 2145 | // [...] or if any template argument remains neither deduced nor |
| 2146 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2147 | SmallVector<TemplateArgument, 4> Builder; |
Douglas Gregor | aef93f2 | 2011-01-04 22:23:38 +0000 | [diff] [blame] | 2148 | TemplateParameterList *PartialParams = Partial->getTemplateParameters(); |
| 2149 | for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) { |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2150 | NamedDecl *Param = PartialParams->getParam(I); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2151 | if (Deduced[I].isNull()) { |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2152 | Info.Param = makeTemplateParameter(Param); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2153 | return Sema::TDK_Incomplete; |
| 2154 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2155 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2156 | // We have deduced this argument, so it still needs to be |
| 2157 | // checked and converted. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2158 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2159 | // First, for a non-type template parameter type that is |
| 2160 | // initialized by a declaration, we need the type of the |
| 2161 | // corresponding non-type template parameter. |
| 2162 | QualType NTTPType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2163 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2164 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2165 | NTTPType = NTTP->getType(); |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2166 | if (NTTPType->isDependentType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2167 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2168 | Builder.data(), Builder.size()); |
| 2169 | NTTPType = S.SubstType(NTTPType, |
| 2170 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2171 | NTTP->getLocation(), |
| 2172 | NTTP->getDeclName()); |
| 2173 | if (NTTPType.isNull()) { |
| 2174 | Info.Param = makeTemplateParameter(Param); |
| 2175 | // FIXME: These template arguments are temporary. Free them! |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2176 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, |
| 2177 | Builder.data(), |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2178 | Builder.size())); |
| 2179 | return Sema::TDK_SubstitutionFailure; |
| 2180 | } |
| 2181 | } |
| 2182 | } |
| 2183 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2184 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2185 | Partial, NTTPType, 0, Info, false, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2186 | Builder)) { |
| 2187 | Info.Param = makeTemplateParameter(Param); |
| 2188 | // FIXME: These template arguments are temporary. Free them! |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2189 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(), |
| 2190 | Builder.size())); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2191 | return Sema::TDK_SubstitutionFailure; |
| 2192 | } |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2193 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2194 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2195 | // Form the template argument list from the deduced template arguments. |
| 2196 | TemplateArgumentList *DeducedArgumentList |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2197 | = TemplateArgumentList::CreateCopy(S.Context, Builder.data(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2198 | Builder.size()); |
| 2199 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2200 | Info.reset(DeducedArgumentList); |
| 2201 | |
| 2202 | // Substitute the deduced template arguments into the template |
| 2203 | // arguments of the class template partial specialization, and |
| 2204 | // verify that the instantiated template arguments are both valid |
| 2205 | // and are equivalent to the template arguments originally provided |
| 2206 | // to the class template. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2207 | LocalInstantiationScope InstScope(S); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2208 | ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate(); |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2209 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2210 | = Partial->getTemplateArgsAsWritten(); |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2211 | const TemplateArgumentLoc *PartialTemplateArgs |
| 2212 | = PartialTemplArgInfo->getTemplateArgs(); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2213 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2214 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
| 2215 | PartialTemplArgInfo->RAngleLoc); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2216 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2217 | if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2218 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 2219 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 2220 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 2221 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 2222 | |
| 2223 | Decl *Param |
| 2224 | = const_cast<NamedDecl *>( |
| 2225 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
| 2226 | Info.Param = makeTemplateParameter(Param); |
| 2227 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 2228 | return Sema::TDK_SubstitutionFailure; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2231 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2232 | if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(), |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2233 | InstArgs, false, ConvertedInstArgs)) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2234 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2235 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2236 | TemplateParameterList *TemplateParams |
| 2237 | = ClassTemplate->getTemplateParameters(); |
| 2238 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2239 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2240 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 2241 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2242 | Info.FirstArg = TemplateArgs[I]; |
| 2243 | Info.SecondArg = InstArg; |
| 2244 | return Sema::TDK_NonDeducedMismatch; |
| 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | if (Trap.hasErrorOccurred()) |
| 2249 | return Sema::TDK_SubstitutionFailure; |
| 2250 | |
| 2251 | return Sema::TDK_Success; |
| 2252 | } |
| 2253 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2254 | /// \brief Perform template argument deduction to determine whether |
Larisse Voufo | 833b05a | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 2255 | /// the given template arguments match the given class template |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2256 | /// partial specialization per C++ [temp.class.spec.match]. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2257 | Sema::TemplateDeductionResult |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2258 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2259 | const TemplateArgumentList &TemplateArgs, |
| 2260 | TemplateDeductionInfo &Info) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 2261 | if (Partial->isInvalidDecl()) |
| 2262 | return TDK_Invalid; |
| 2263 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2264 | // C++ [temp.class.spec.match]p2: |
| 2265 | // A partial specialization matches a given actual template |
| 2266 | // argument list if the template arguments of the partial |
| 2267 | // specialization can be deduced from the actual template argument |
| 2268 | // list (14.8.2). |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2269 | |
| 2270 | // Unevaluated SFINAE context. |
| 2271 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2272 | SFINAETrap Trap(*this); |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2273 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2274 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2275 | Deduced.resize(Partial->getTemplateParameters()->size()); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2276 | if (TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2277 | = ::DeduceTemplateArguments(*this, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2278 | Partial->getTemplateParameters(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2279 | Partial->getTemplateArgs(), |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2280 | TemplateArgs, Info, Deduced)) |
| 2281 | return Result; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 2282 | |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2283 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2284 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2285 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2286 | if (Inst.isInvalid()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2287 | return TDK_InstantiationDepth; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2288 | |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2289 | if (Trap.hasErrorOccurred()) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2290 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2291 | |
| 2292 | return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs, |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2293 | Deduced, Info); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2294 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2295 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2296 | /// Complete template argument deduction for a variable template partial |
| 2297 | /// specialization. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2298 | /// TODO: Unify with ClassTemplatePartialSpecializationDecl version? |
| 2299 | /// May require unifying ClassTemplate(Partial)SpecializationDecl and |
| 2300 | /// VarTemplate(Partial)SpecializationDecl with a new data |
| 2301 | /// structure Template(Partial)SpecializationDecl, and |
| 2302 | /// using Template(Partial)SpecializationDecl as input type. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2303 | static Sema::TemplateDeductionResult FinishTemplateArgumentDeduction( |
| 2304 | Sema &S, VarTemplatePartialSpecializationDecl *Partial, |
| 2305 | const TemplateArgumentList &TemplateArgs, |
| 2306 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2307 | TemplateDeductionInfo &Info) { |
| 2308 | // Unevaluated SFINAE context. |
| 2309 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
| 2310 | Sema::SFINAETrap Trap(S); |
| 2311 | |
| 2312 | // C++ [temp.deduct.type]p2: |
| 2313 | // [...] or if any template argument remains neither deduced nor |
| 2314 | // explicitly specified, template argument deduction fails. |
| 2315 | SmallVector<TemplateArgument, 4> Builder; |
| 2316 | TemplateParameterList *PartialParams = Partial->getTemplateParameters(); |
| 2317 | for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) { |
| 2318 | NamedDecl *Param = PartialParams->getParam(I); |
| 2319 | if (Deduced[I].isNull()) { |
| 2320 | Info.Param = makeTemplateParameter(Param); |
| 2321 | return Sema::TDK_Incomplete; |
| 2322 | } |
| 2323 | |
| 2324 | // We have deduced this argument, so it still needs to be |
| 2325 | // checked and converted. |
| 2326 | |
| 2327 | // First, for a non-type template parameter type that is |
| 2328 | // initialized by a declaration, we need the type of the |
| 2329 | // corresponding non-type template parameter. |
| 2330 | QualType NTTPType; |
| 2331 | if (NonTypeTemplateParmDecl *NTTP = |
| 2332 | dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2333 | NTTPType = NTTP->getType(); |
| 2334 | if (NTTPType->isDependentType()) { |
| 2335 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2336 | Builder.data(), Builder.size()); |
| 2337 | NTTPType = |
| 2338 | S.SubstType(NTTPType, MultiLevelTemplateArgumentList(TemplateArgs), |
| 2339 | NTTP->getLocation(), NTTP->getDeclName()); |
| 2340 | if (NTTPType.isNull()) { |
| 2341 | Info.Param = makeTemplateParameter(Param); |
| 2342 | // FIXME: These template arguments are temporary. Free them! |
| 2343 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(), |
| 2344 | Builder.size())); |
| 2345 | return Sema::TDK_SubstitutionFailure; |
| 2346 | } |
| 2347 | } |
| 2348 | } |
| 2349 | |
| 2350 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Partial, NTTPType, |
| 2351 | 0, Info, false, Builder)) { |
| 2352 | Info.Param = makeTemplateParameter(Param); |
| 2353 | // FIXME: These template arguments are temporary. Free them! |
| 2354 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(), |
| 2355 | Builder.size())); |
| 2356 | return Sema::TDK_SubstitutionFailure; |
| 2357 | } |
| 2358 | } |
| 2359 | |
| 2360 | // Form the template argument list from the deduced template arguments. |
| 2361 | TemplateArgumentList *DeducedArgumentList = TemplateArgumentList::CreateCopy( |
| 2362 | S.Context, Builder.data(), Builder.size()); |
| 2363 | |
| 2364 | Info.reset(DeducedArgumentList); |
| 2365 | |
| 2366 | // Substitute the deduced template arguments into the template |
| 2367 | // arguments of the class template partial specialization, and |
| 2368 | // verify that the instantiated template arguments are both valid |
| 2369 | // and are equivalent to the template arguments originally provided |
| 2370 | // to the class template. |
| 2371 | LocalInstantiationScope InstScope(S); |
| 2372 | VarTemplateDecl *VarTemplate = Partial->getSpecializedTemplate(); |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2373 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo |
| 2374 | = Partial->getTemplateArgsAsWritten(); |
| 2375 | const TemplateArgumentLoc *PartialTemplateArgs |
| 2376 | = PartialTemplArgInfo->getTemplateArgs(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2377 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2378 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
| 2379 | PartialTemplArgInfo->RAngleLoc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2380 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2381 | if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2382 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 2383 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 2384 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 2385 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 2386 | |
| 2387 | Decl *Param = const_cast<NamedDecl *>( |
| 2388 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
| 2389 | Info.Param = makeTemplateParameter(Param); |
| 2390 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 2391 | return Sema::TDK_SubstitutionFailure; |
| 2392 | } |
| 2393 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
| 2394 | if (S.CheckTemplateArgumentList(VarTemplate, Partial->getLocation(), InstArgs, |
| 2395 | false, ConvertedInstArgs)) |
| 2396 | return Sema::TDK_SubstitutionFailure; |
| 2397 | |
| 2398 | TemplateParameterList *TemplateParams = VarTemplate->getTemplateParameters(); |
| 2399 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
| 2400 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
| 2401 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
| 2402 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
| 2403 | Info.FirstArg = TemplateArgs[I]; |
| 2404 | Info.SecondArg = InstArg; |
| 2405 | return Sema::TDK_NonDeducedMismatch; |
| 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | if (Trap.hasErrorOccurred()) |
| 2410 | return Sema::TDK_SubstitutionFailure; |
| 2411 | |
| 2412 | return Sema::TDK_Success; |
| 2413 | } |
| 2414 | |
| 2415 | /// \brief Perform template argument deduction to determine whether |
| 2416 | /// the given template arguments match the given variable template |
| 2417 | /// partial specialization per C++ [temp.class.spec.match]. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2418 | /// TODO: Unify with ClassTemplatePartialSpecializationDecl version? |
| 2419 | /// May require unifying ClassTemplate(Partial)SpecializationDecl and |
| 2420 | /// VarTemplate(Partial)SpecializationDecl with a new data |
| 2421 | /// structure Template(Partial)SpecializationDecl, and |
| 2422 | /// using Template(Partial)SpecializationDecl as input type. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2423 | Sema::TemplateDeductionResult |
| 2424 | Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial, |
| 2425 | const TemplateArgumentList &TemplateArgs, |
| 2426 | TemplateDeductionInfo &Info) { |
| 2427 | if (Partial->isInvalidDecl()) |
| 2428 | return TDK_Invalid; |
| 2429 | |
| 2430 | // C++ [temp.class.spec.match]p2: |
| 2431 | // A partial specialization matches a given actual template |
| 2432 | // argument list if the template arguments of the partial |
| 2433 | // specialization can be deduced from the actual template argument |
| 2434 | // list (14.8.2). |
| 2435 | |
| 2436 | // Unevaluated SFINAE context. |
| 2437 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
| 2438 | SFINAETrap Trap(*this); |
| 2439 | |
| 2440 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 2441 | Deduced.resize(Partial->getTemplateParameters()->size()); |
| 2442 | if (TemplateDeductionResult Result = ::DeduceTemplateArguments( |
| 2443 | *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(), |
| 2444 | TemplateArgs, Info, Deduced)) |
| 2445 | return Result; |
| 2446 | |
| 2447 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2448 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2449 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2450 | if (Inst.isInvalid()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2451 | return TDK_InstantiationDepth; |
| 2452 | |
| 2453 | if (Trap.hasErrorOccurred()) |
| 2454 | return Sema::TDK_SubstitutionFailure; |
| 2455 | |
| 2456 | return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs, |
| 2457 | Deduced, Info); |
| 2458 | } |
| 2459 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2460 | /// \brief Determine whether the given type T is a simple-template-id type. |
| 2461 | static bool isSimpleTemplateIdType(QualType T) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2462 | if (const TemplateSpecializationType *Spec |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2463 | = T->getAs<TemplateSpecializationType>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 2464 | return Spec->getTemplateName().getAsTemplateDecl() != nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2465 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2466 | return false; |
| 2467 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2468 | |
| 2469 | /// \brief Substitute the explicitly-provided template arguments into the |
| 2470 | /// given function template according to C++ [temp.arg.explicit]. |
| 2471 | /// |
| 2472 | /// \param FunctionTemplate the function template into which the explicit |
| 2473 | /// template arguments will be substituted. |
| 2474 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2475 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2476 | /// arguments. |
| 2477 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2478 | /// \param Deduced the deduced template arguments, which will be populated |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2479 | /// with the converted and checked explicit template arguments. |
| 2480 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2481 | /// \param ParamTypes will be populated with the instantiated function |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2482 | /// parameters. |
| 2483 | /// |
| 2484 | /// \param FunctionType if non-NULL, the result type of the function template |
| 2485 | /// will also be instantiated and the pointed-to value will be updated with |
| 2486 | /// the instantiated function type. |
| 2487 | /// |
| 2488 | /// \param Info if substitution fails for any reason, this object will be |
| 2489 | /// populated with more information about the failure. |
| 2490 | /// |
| 2491 | /// \returns TDK_Success if substitution was successful, or some failure |
| 2492 | /// condition. |
| 2493 | Sema::TemplateDeductionResult |
| 2494 | Sema::SubstituteExplicitTemplateArguments( |
| 2495 | FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2496 | TemplateArgumentListInfo &ExplicitTemplateArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2497 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2498 | SmallVectorImpl<QualType> &ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2499 | QualType *FunctionType, |
| 2500 | TemplateDeductionInfo &Info) { |
| 2501 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 2502 | TemplateParameterList *TemplateParams |
| 2503 | = FunctionTemplate->getTemplateParameters(); |
| 2504 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2505 | if (ExplicitTemplateArgs.size() == 0) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2506 | // No arguments to substitute; just copy over the parameter types and |
| 2507 | // fill in the function type. |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 2508 | for (auto P : Function->params()) |
| 2509 | ParamTypes.push_back(P->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2510 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2511 | if (FunctionType) |
| 2512 | *FunctionType = Function->getType(); |
| 2513 | return TDK_Success; |
| 2514 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2515 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2516 | // Unevaluated SFINAE context. |
| 2517 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2518 | SFINAETrap Trap(*this); |
| 2519 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2520 | // C++ [temp.arg.explicit]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2521 | // Template arguments that are present shall be specified in the |
| 2522 | // declaration order of their corresponding template-parameters. The |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2523 | // template argument list shall not specify more template-arguments than |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2524 | // there are corresponding template-parameters. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2525 | SmallVector<TemplateArgument, 4> Builder; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2526 | |
| 2527 | // Enter a new template instantiation context where we check the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2528 | // explicitly-specified template arguments against this function template, |
| 2529 | // and then substitute them into the function parameter types. |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2530 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2531 | InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate, |
| 2532 | DeducedArgs, |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2533 | ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution, |
| 2534 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2535 | if (Inst.isInvalid()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2536 | return TDK_InstantiationDepth; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2537 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2538 | if (CheckTemplateArgumentList(FunctionTemplate, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2539 | SourceLocation(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2540 | ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2541 | true, |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2542 | Builder) || Trap.hasErrorOccurred()) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2543 | unsigned Index = Builder.size(); |
Douglas Gregor | 62c281a | 2010-05-09 01:26:06 +0000 | [diff] [blame] | 2544 | if (Index >= TemplateParams->size()) |
| 2545 | Index = TemplateParams->size() - 1; |
| 2546 | Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2547 | return TDK_InvalidExplicitArguments; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2548 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2549 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2550 | // Form the template argument list from the explicitly-specified |
| 2551 | // template arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2552 | TemplateArgumentList *ExplicitArgumentList |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2553 | = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size()); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2554 | Info.reset(ExplicitArgumentList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2555 | |
John McCall | 036855a | 2010-10-12 19:40:14 +0000 | [diff] [blame] | 2556 | // Template argument deduction and the final substitution should be |
| 2557 | // done in the context of the templated declaration. Explicit |
| 2558 | // argument substitution, on the other hand, needs to happen in the |
| 2559 | // calling context. |
| 2560 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
| 2561 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2562 | // If we deduced template arguments for a template parameter pack, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2563 | // note that the template argument pack is partially substituted and record |
| 2564 | // the explicit template arguments. They'll be used as part of deduction |
| 2565 | // for this template parameter pack. |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2566 | for (unsigned I = 0, N = Builder.size(); I != N; ++I) { |
| 2567 | const TemplateArgument &Arg = Builder[I]; |
| 2568 | if (Arg.getKind() == TemplateArgument::Pack) { |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2569 | CurrentInstantiationScope->SetPartiallySubstitutedPack( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2570 | TemplateParams->getParam(I), |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2571 | Arg.pack_begin(), |
| 2572 | Arg.pack_size()); |
| 2573 | break; |
| 2574 | } |
| 2575 | } |
| 2576 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2577 | const FunctionProtoType *Proto |
| 2578 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2579 | assert(Proto && "Function template does not have a prototype?"); |
| 2580 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2581 | // Instantiate the types of each of the function parameters given the |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2582 | // explicitly-specified template arguments. If the function has a trailing |
| 2583 | // return type, substitute it after the arguments to ensure we substitute |
| 2584 | // in lexical order. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2585 | if (Proto->hasTrailingReturn()) { |
| 2586 | if (SubstParmTypes(Function->getLocation(), |
| 2587 | Function->param_begin(), Function->getNumParams(), |
| 2588 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 2589 | ParamTypes)) |
| 2590 | return TDK_SubstitutionFailure; |
| 2591 | } |
| 2592 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2593 | // Instantiate the return type. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2594 | QualType ResultType; |
| 2595 | { |
| 2596 | // C++11 [expr.prim.general]p3: |
| 2597 | // If a declaration declares a member function or member function |
| 2598 | // template of a class X, the expression this is a prvalue of type |
| 2599 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
| 2600 | // and the end of the function-definition, member-declarator, or |
| 2601 | // declarator. |
| 2602 | unsigned ThisTypeQuals = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 2603 | CXXRecordDecl *ThisContext = nullptr; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2604 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
| 2605 | ThisContext = Method->getParent(); |
| 2606 | ThisTypeQuals = Method->getTypeQualifiers(); |
| 2607 | } |
| 2608 | |
| 2609 | CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2610 | getLangOpts().CPlusPlus11); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2611 | |
| 2612 | ResultType = |
| 2613 | SubstType(Proto->getReturnType(), |
| 2614 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 2615 | Function->getTypeSpecStartLoc(), Function->getDeclName()); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2616 | if (ResultType.isNull() || Trap.hasErrorOccurred()) |
| 2617 | return TDK_SubstitutionFailure; |
| 2618 | } |
| 2619 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2620 | // Instantiate the types of each of the function parameters given the |
| 2621 | // explicitly-specified template arguments if we didn't do so earlier. |
| 2622 | if (!Proto->hasTrailingReturn() && |
| 2623 | SubstParmTypes(Function->getLocation(), |
| 2624 | Function->param_begin(), Function->getNumParams(), |
| 2625 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 2626 | ParamTypes)) |
| 2627 | return TDK_SubstitutionFailure; |
| 2628 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2629 | if (FunctionType) { |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2630 | *FunctionType = BuildFunctionType(ResultType, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2631 | Function->getLocation(), |
Eli Friedman | d8725a9 | 2010-08-05 02:54:05 +0000 | [diff] [blame] | 2632 | Function->getDeclName(), |
Jordan Rose | a0a86be | 2013-03-08 22:25:36 +0000 | [diff] [blame] | 2633 | Proto->getExtProtoInfo()); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2634 | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
| 2635 | return TDK_SubstitutionFailure; |
| 2636 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2637 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2638 | // C++ [temp.arg.explicit]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2639 | // Trailing template arguments that can be deduced (14.8.2) may be |
| 2640 | // omitted from the list of explicit template-arguments. If all of the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2641 | // template arguments can be deduced, they may all be omitted; in this |
| 2642 | // case, the empty template argument list <> itself may also be omitted. |
| 2643 | // |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2644 | // Take all of the explicitly-specified arguments and put them into |
| 2645 | // the set of deduced template arguments. Explicitly-specified |
| 2646 | // parameter packs, however, will be set to NULL since the deduction |
| 2647 | // mechanisms handle explicitly-specified argument packs directly. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2648 | Deduced.reserve(TemplateParams->size()); |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2649 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) { |
| 2650 | const TemplateArgument &Arg = ExplicitArgumentList->get(I); |
| 2651 | if (Arg.getKind() == TemplateArgument::Pack) |
| 2652 | Deduced.push_back(DeducedTemplateArgument()); |
| 2653 | else |
| 2654 | Deduced.push_back(Arg); |
| 2655 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2656 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2657 | return TDK_Success; |
| 2658 | } |
| 2659 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2660 | /// \brief Check whether the deduced argument type for a call to a function |
| 2661 | /// template matches the actual argument type per C++ [temp.deduct.call]p4. |
| 2662 | static bool |
| 2663 | CheckOriginalCallArgDeduction(Sema &S, Sema::OriginalCallArg OriginalArg, |
| 2664 | QualType DeducedA) { |
| 2665 | ASTContext &Context = S.Context; |
| 2666 | |
| 2667 | QualType A = OriginalArg.OriginalArgType; |
| 2668 | QualType OriginalParamType = OriginalArg.OriginalParamType; |
| 2669 | |
| 2670 | // Check for type equality (top-level cv-qualifiers are ignored). |
| 2671 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 2672 | return false; |
| 2673 | |
| 2674 | // Strip off references on the argument types; they aren't needed for |
| 2675 | // the following checks. |
| 2676 | if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>()) |
| 2677 | DeducedA = DeducedARef->getPointeeType(); |
| 2678 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
| 2679 | A = ARef->getPointeeType(); |
| 2680 | |
| 2681 | // C++ [temp.deduct.call]p4: |
| 2682 | // [...] However, there are three cases that allow a difference: |
| 2683 | // - If the original P is a reference type, the deduced A (i.e., the |
| 2684 | // type referred to by the reference) can be more cv-qualified than |
| 2685 | // the transformed A. |
| 2686 | if (const ReferenceType *OriginalParamRef |
| 2687 | = OriginalParamType->getAs<ReferenceType>()) { |
| 2688 | // We don't want to keep the reference around any more. |
| 2689 | OriginalParamType = OriginalParamRef->getPointeeType(); |
| 2690 | |
| 2691 | Qualifiers AQuals = A.getQualifiers(); |
| 2692 | Qualifiers DeducedAQuals = DeducedA.getQualifiers(); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2693 | |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2694 | // Under Objective-C++ ARC, the deduced type may have implicitly |
| 2695 | // been given strong or (when dealing with a const reference) |
| 2696 | // unsafe_unretained lifetime. If so, update the original |
| 2697 | // qualifiers to include this lifetime. |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2698 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2699 | ((DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong && |
| 2700 | AQuals.getObjCLifetime() == Qualifiers::OCL_None) || |
| 2701 | (DeducedAQuals.hasConst() && |
| 2702 | DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone))) { |
| 2703 | AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime()); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2706 | if (AQuals == DeducedAQuals) { |
| 2707 | // Qualifiers match; there's nothing to do. |
| 2708 | } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) { |
Douglas Gregor | ddaae52 | 2011-06-17 14:36:00 +0000 | [diff] [blame] | 2709 | return true; |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2710 | } else { |
| 2711 | // Qualifiers are compatible, so have the argument type adopt the |
| 2712 | // deduced argument type's qualifiers as if we had performed the |
| 2713 | // qualification conversion. |
| 2714 | A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals); |
| 2715 | } |
| 2716 | } |
| 2717 | |
| 2718 | // - The transformed A can be another pointer or pointer to member |
| 2719 | // type that can be converted to the deduced A via a qualification |
| 2720 | // conversion. |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2721 | // |
| 2722 | // Also allow conversions which merely strip [[noreturn]] from function types |
| 2723 | // (recursively) as an extension. |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2724 | // FIXME: Currently, this doesn't play nicely with qualification conversions. |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2725 | bool ObjCLifetimeConversion = false; |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2726 | QualType ResultTy; |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2727 | if ((A->isAnyPointerType() || A->isMemberPointerType()) && |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2728 | (S.IsQualificationConversion(A, DeducedA, false, |
| 2729 | ObjCLifetimeConversion) || |
| 2730 | S.IsNoReturnConversion(A, DeducedA, ResultTy))) |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2731 | return false; |
| 2732 | |
| 2733 | |
| 2734 | // - If P is a class and P has the form simple-template-id, then the |
| 2735 | // transformed A can be a derived class of the deduced A. [...] |
| 2736 | // [...] Likewise, if P is a pointer to a class of the form |
| 2737 | // simple-template-id, the transformed A can be a pointer to a |
| 2738 | // derived class pointed to by the deduced A. |
| 2739 | if (const PointerType *OriginalParamPtr |
| 2740 | = OriginalParamType->getAs<PointerType>()) { |
| 2741 | if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) { |
| 2742 | if (const PointerType *APtr = A->getAs<PointerType>()) { |
| 2743 | if (A->getPointeeType()->isRecordType()) { |
| 2744 | OriginalParamType = OriginalParamPtr->getPointeeType(); |
| 2745 | DeducedA = DeducedAPtr->getPointeeType(); |
| 2746 | A = APtr->getPointeeType(); |
| 2747 | } |
| 2748 | } |
| 2749 | } |
| 2750 | } |
| 2751 | |
| 2752 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 2753 | return false; |
| 2754 | |
| 2755 | if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) && |
| 2756 | S.IsDerivedFrom(A, DeducedA)) |
| 2757 | return false; |
| 2758 | |
| 2759 | return true; |
| 2760 | } |
| 2761 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2762 | /// \brief Finish template argument deduction for a function template, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2763 | /// checking the deduced template arguments for completeness and forming |
| 2764 | /// the function template specialization. |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2765 | /// |
| 2766 | /// \param OriginalCallArgs If non-NULL, the original call arguments against |
| 2767 | /// which the deduced argument types should be compared. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2768 | Sema::TemplateDeductionResult |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2769 | Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2770 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2771 | unsigned NumExplicitlySpecified, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2772 | FunctionDecl *&Specialization, |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2773 | TemplateDeductionInfo &Info, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2774 | SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2775 | TemplateParameterList *TemplateParams |
| 2776 | = FunctionTemplate->getTemplateParameters(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2777 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2778 | // Unevaluated SFINAE context. |
| 2779 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2780 | SFINAETrap Trap(*this); |
| 2781 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2782 | // Enter a new template instantiation context while we instantiate the |
| 2783 | // actual function declaration. |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2784 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2785 | InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate, |
| 2786 | DeducedArgs, |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2787 | ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution, |
| 2788 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2789 | if (Inst.isInvalid()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2790 | return TDK_InstantiationDepth; |
| 2791 | |
John McCall | e23b871 | 2010-04-29 01:18:58 +0000 | [diff] [blame] | 2792 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
John McCall | 80e58cd | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 2793 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2794 | // C++ [temp.deduct.type]p2: |
| 2795 | // [...] or if any template argument remains neither deduced nor |
| 2796 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2797 | SmallVector<TemplateArgument, 4> Builder; |
Douglas Gregor | aaa6a90 | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2798 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 2799 | NamedDecl *Param = TemplateParams->getParam(I); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2800 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2801 | if (!Deduced[I].isNull()) { |
Douglas Gregor | 6e9cf63 | 2010-10-12 18:51:08 +0000 | [diff] [blame] | 2802 | if (I < NumExplicitlySpecified) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2803 | // We have already fully type-checked and converted this |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2804 | // argument, because it was explicitly-specified. Just record the |
Douglas Gregor | 6e9cf63 | 2010-10-12 18:51:08 +0000 | [diff] [blame] | 2805 | // presence of this argument. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2806 | Builder.push_back(Deduced[I]); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2807 | continue; |
| 2808 | } |
| 2809 | |
| 2810 | // We have deduced this argument, so it still needs to be |
| 2811 | // checked and converted. |
| 2812 | |
| 2813 | // First, for a non-type template parameter type that is |
| 2814 | // initialized by a declaration, we need the type of the |
| 2815 | // corresponding non-type template parameter. |
| 2816 | QualType NTTPType; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2817 | if (NonTypeTemplateParmDecl *NTTP |
| 2818 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | aaa6a90 | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2819 | NTTPType = NTTP->getType(); |
| 2820 | if (NTTPType->isDependentType()) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2821 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | aaa6a90 | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2822 | Builder.data(), Builder.size()); |
| 2823 | NTTPType = SubstType(NTTPType, |
| 2824 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2825 | NTTP->getLocation(), |
| 2826 | NTTP->getDeclName()); |
| 2827 | if (NTTPType.isNull()) { |
| 2828 | Info.Param = makeTemplateParameter(Param); |
| 2829 | // FIXME: These template arguments are temporary. Free them! |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2830 | Info.reset(TemplateArgumentList::CreateCopy(Context, |
| 2831 | Builder.data(), |
Douglas Gregor | aaa6a90 | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2832 | Builder.size())); |
| 2833 | return TDK_SubstitutionFailure; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2834 | } |
| 2835 | } |
| 2836 | } |
| 2837 | |
Douglas Gregor | aaa6a90 | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2838 | if (ConvertDeducedTemplateArgument(*this, Param, Deduced[I], |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2839 | FunctionTemplate, NTTPType, 0, Info, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2840 | true, Builder)) { |
Douglas Gregor | aaa6a90 | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2841 | Info.Param = makeTemplateParameter(Param); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2842 | // FIXME: These template arguments are temporary. Free them! |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2843 | Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(), |
| 2844 | Builder.size())); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2845 | return TDK_SubstitutionFailure; |
| 2846 | } |
| 2847 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2848 | continue; |
| 2849 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2850 | |
Douglas Gregor | ca4d91d | 2010-12-23 01:52:01 +0000 | [diff] [blame] | 2851 | // C++0x [temp.arg.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2852 | // A trailing template parameter pack (14.5.3) not otherwise deduced will |
Douglas Gregor | ca4d91d | 2010-12-23 01:52:01 +0000 | [diff] [blame] | 2853 | // be deduced to an empty sequence of template arguments. |
| 2854 | // FIXME: Where did the word "trailing" come from? |
| 2855 | if (Param->isTemplateParameterPack()) { |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2856 | // We may have had explicitly-specified template arguments for this |
| 2857 | // template parameter pack. If so, our empty deduction extends the |
| 2858 | // explicitly-specified set (C++0x [temp.arg.explicit]p9). |
| 2859 | const TemplateArgument *ExplicitArgs; |
| 2860 | unsigned NumExplicitArgs; |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2861 | if (CurrentInstantiationScope && |
| 2862 | CurrentInstantiationScope->getPartiallySubstitutedPack(&ExplicitArgs, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2863 | &NumExplicitArgs) |
Douglas Gregor | caddba9 | 2013-01-18 22:27:09 +0000 | [diff] [blame] | 2864 | == Param) { |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2865 | Builder.push_back(TemplateArgument(ExplicitArgs, NumExplicitArgs)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2866 | |
Douglas Gregor | caddba9 | 2013-01-18 22:27:09 +0000 | [diff] [blame] | 2867 | // Forget the partially-substituted pack; it's substitution is now |
| 2868 | // complete. |
| 2869 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2870 | } else { |
| 2871 | Builder.push_back(TemplateArgument::getEmptyPack()); |
| 2872 | } |
Douglas Gregor | ca4d91d | 2010-12-23 01:52:01 +0000 | [diff] [blame] | 2873 | continue; |
| 2874 | } |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2875 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2876 | // Substitute into the default template argument, if available. |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 2877 | bool HasDefaultArg = false; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2878 | TemplateArgumentLoc DefArg |
| 2879 | = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate, |
| 2880 | FunctionTemplate->getLocation(), |
| 2881 | FunctionTemplate->getSourceRange().getEnd(), |
| 2882 | Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 2883 | Builder, HasDefaultArg); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2884 | |
| 2885 | // If there was no default argument, deduction is incomplete. |
| 2886 | if (DefArg.getArgument().isNull()) { |
| 2887 | Info.Param = makeTemplateParameter( |
| 2888 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 2889 | Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(), |
| 2890 | Builder.size())); |
| 2891 | return HasDefaultArg ? TDK_SubstitutionFailure : TDK_Incomplete; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2892 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2893 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2894 | // Check whether we can actually use the default argument. |
| 2895 | if (CheckTemplateArgument(Param, DefArg, |
| 2896 | FunctionTemplate, |
| 2897 | FunctionTemplate->getLocation(), |
| 2898 | FunctionTemplate->getSourceRange().getEnd(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2899 | 0, Builder, |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2900 | CTAK_Specified)) { |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2901 | Info.Param = makeTemplateParameter( |
| 2902 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2903 | // FIXME: These template arguments are temporary. Free them! |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2904 | Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(), |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2905 | Builder.size())); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2906 | return TDK_SubstitutionFailure; |
| 2907 | } |
| 2908 | |
| 2909 | // If we get here, we successfully used the default template argument. |
| 2910 | } |
| 2911 | |
| 2912 | // Form the template argument list from the deduced template arguments. |
| 2913 | TemplateArgumentList *DeducedArgumentList |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2914 | = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size()); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2915 | Info.reset(DeducedArgumentList); |
| 2916 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2917 | // Substitute the deduced template arguments into the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2918 | // declaration to produce the function template specialization. |
Douglas Gregor | 1614237 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2919 | DeclContext *Owner = FunctionTemplate->getDeclContext(); |
| 2920 | if (FunctionTemplate->getFriendObjectKind()) |
| 2921 | Owner = FunctionTemplate->getLexicalDeclContext(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2922 | Specialization = cast_or_null<FunctionDecl>( |
Douglas Gregor | 1614237 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2923 | SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, |
Douglas Gregor | 39cacdb | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 2924 | MultiLevelTemplateArgumentList(*DeducedArgumentList))); |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 2925 | if (!Specialization || Specialization->isInvalidDecl()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2926 | return TDK_SubstitutionFailure; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2927 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2928 | assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == |
Douglas Gregor | 31fae89 | 2009-09-15 18:26:13 +0000 | [diff] [blame] | 2929 | FunctionTemplate->getCanonicalDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2930 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2931 | // If the template argument list is owned by the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2932 | // specialization, release it. |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 2933 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList && |
| 2934 | !Trap.hasErrorOccurred()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2935 | Info.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2936 | |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 2937 | // There may have been an error that did not prevent us from constructing a |
| 2938 | // declaration. Mark the declaration invalid and return with a substitution |
| 2939 | // failure. |
| 2940 | if (Trap.hasErrorOccurred()) { |
| 2941 | Specialization->setInvalidDecl(true); |
| 2942 | return TDK_SubstitutionFailure; |
| 2943 | } |
| 2944 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2945 | if (OriginalCallArgs) { |
| 2946 | // C++ [temp.deduct.call]p4: |
| 2947 | // In general, the deduction process attempts to find template argument |
| 2948 | // values that will make the deduced A identical to A (after the type A |
| 2949 | // is transformed as described above). [...] |
| 2950 | for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) { |
| 2951 | OriginalCallArg OriginalArg = (*OriginalCallArgs)[I]; |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2952 | unsigned ParamIdx = OriginalArg.ArgIdx; |
| 2953 | |
| 2954 | if (ParamIdx >= Specialization->getNumParams()) |
| 2955 | continue; |
| 2956 | |
| 2957 | QualType DeducedA = Specialization->getParamDecl(ParamIdx)->getType(); |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2958 | if (CheckOriginalCallArgDeduction(*this, OriginalArg, DeducedA)) |
| 2959 | return Sema::TDK_SubstitutionFailure; |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2960 | } |
| 2961 | } |
| 2962 | |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2963 | // If we suppressed any diagnostics while performing template argument |
| 2964 | // deduction, and if we haven't already instantiated this declaration, |
| 2965 | // keep track of these diagnostics. They'll be emitted if this specialization |
| 2966 | // is actually used. |
| 2967 | if (Info.diag_begin() != Info.diag_end()) { |
Craig Topper | 79be4cd | 2013-07-05 04:33:53 +0000 | [diff] [blame] | 2968 | SuppressedDiagnosticsMap::iterator |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2969 | Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); |
| 2970 | if (Pos == SuppressedDiagnostics.end()) |
| 2971 | SuppressedDiagnostics[Specialization->getCanonicalDecl()] |
| 2972 | .append(Info.diag_begin(), Info.diag_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2973 | } |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2974 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2975 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2976 | } |
| 2977 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2978 | /// Gets the type of a function for template-argument-deducton |
| 2979 | /// purposes when it's considered as part of an overload set. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2980 | static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R, |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2981 | FunctionDecl *Fn) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2982 | // We may need to deduce the return type of the function now. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2983 | if (S.getLangOpts().CPlusPlus1y && Fn->getReturnType()->isUndeducedType() && |
| 2984 | S.DeduceReturnType(Fn, R.Expression->getExprLoc(), /*Diagnose*/ false)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2985 | return QualType(); |
| 2986 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2987 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2988 | if (Method->isInstance()) { |
| 2989 | // An instance method that's referenced in a form that doesn't |
| 2990 | // look like a member pointer is just invalid. |
| 2991 | if (!R.HasFormOfMemberPointer) return QualType(); |
| 2992 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2993 | return S.Context.getMemberPointerType(Fn->getType(), |
| 2994 | S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2995 | } |
| 2996 | |
| 2997 | if (!R.IsAddressOfOperand) return Fn->getType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2998 | return S.Context.getPointerType(Fn->getType()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2999 | } |
| 3000 | |
| 3001 | /// Apply the deduction rules for overload sets. |
| 3002 | /// |
| 3003 | /// \return the null type if this argument should be treated as an |
| 3004 | /// undeduced context |
| 3005 | static QualType |
| 3006 | ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3007 | Expr *Arg, QualType ParamType, |
| 3008 | bool ParamWasReference) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3009 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3010 | OverloadExpr::FindResult R = OverloadExpr::find(Arg); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3011 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3012 | OverloadExpr *Ovl = R.Expression; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3013 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3014 | // C++0x [temp.deduct.call]p4 |
| 3015 | unsigned TDF = 0; |
| 3016 | if (ParamWasReference) |
| 3017 | TDF |= TDF_ParamWithReferenceType; |
| 3018 | if (R.IsAddressOfOperand) |
| 3019 | TDF |= TDF_IgnoreQualifiers; |
| 3020 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3021 | // C++0x [temp.deduct.call]p6: |
| 3022 | // When P is a function type, pointer to function type, or pointer |
| 3023 | // to member function type: |
| 3024 | |
| 3025 | if (!ParamType->isFunctionType() && |
| 3026 | !ParamType->isFunctionPointerType() && |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3027 | !ParamType->isMemberFunctionPointerType()) { |
| 3028 | if (Ovl->hasExplicitTemplateArgs()) { |
| 3029 | // But we can still look for an explicit specialization. |
| 3030 | if (FunctionDecl *ExplicitSpec |
| 3031 | = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3032 | return GetTypeOfFunction(S, R, ExplicitSpec); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3033 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3034 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3035 | return QualType(); |
| 3036 | } |
| 3037 | |
| 3038 | // Gather the explicit template arguments, if any. |
| 3039 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 3040 | if (Ovl->hasExplicitTemplateArgs()) |
| 3041 | Ovl->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3042 | QualType Match; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 3043 | for (UnresolvedSetIterator I = Ovl->decls_begin(), |
| 3044 | E = Ovl->decls_end(); I != E; ++I) { |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3045 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 3046 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3047 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) { |
| 3048 | // - If the argument is an overload set containing one or more |
| 3049 | // function templates, the parameter is treated as a |
| 3050 | // non-deduced context. |
| 3051 | if (!Ovl->hasExplicitTemplateArgs()) |
| 3052 | return QualType(); |
| 3053 | |
| 3054 | // Otherwise, see if we can resolve a function type |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 3055 | FunctionDecl *Specialization = nullptr; |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3056 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3057 | if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs, |
| 3058 | Specialization, Info)) |
| 3059 | continue; |
| 3060 | |
| 3061 | D = Specialization; |
| 3062 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3063 | |
| 3064 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3065 | QualType ArgType = GetTypeOfFunction(S, R, Fn); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3066 | if (ArgType.isNull()) continue; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3067 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3068 | // Function-to-pointer conversion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3069 | if (!ParamWasReference && ParamType->isPointerType() && |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3070 | ArgType->isFunctionType()) |
| 3071 | ArgType = S.Context.getPointerType(ArgType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3072 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3073 | // - If the argument is an overload set (not containing function |
| 3074 | // templates), trial argument deduction is attempted using each |
| 3075 | // of the members of the set. If deduction succeeds for only one |
| 3076 | // of the overload set members, that member is used as the |
| 3077 | // argument value for the deduction. If deduction succeeds for |
| 3078 | // more than one member of the overload set the parameter is |
| 3079 | // treated as a non-deduced context. |
| 3080 | |
| 3081 | // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: |
| 3082 | // Type deduction is done independently for each P/A pair, and |
| 3083 | // the deduced template argument values are then combined. |
| 3084 | // So we do not reject deductions which were made elsewhere. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3085 | SmallVector<DeducedTemplateArgument, 8> |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3086 | Deduced(TemplateParams->size()); |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3087 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3088 | Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3089 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
| 3090 | ArgType, Info, Deduced, TDF); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3091 | if (Result) continue; |
| 3092 | if (!Match.isNull()) return QualType(); |
| 3093 | Match = ArgType; |
| 3094 | } |
| 3095 | |
| 3096 | return Match; |
| 3097 | } |
| 3098 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3099 | /// \brief Perform the adjustments to the parameter and argument types |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3100 | /// described in C++ [temp.deduct.call]. |
| 3101 | /// |
| 3102 | /// \returns true if the caller should not attempt to perform any template |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3103 | /// argument deduction based on this P/A pair because the argument is an |
| 3104 | /// overloaded function set that could not be resolved. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3105 | static bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S, |
| 3106 | TemplateParameterList *TemplateParams, |
| 3107 | QualType &ParamType, |
| 3108 | QualType &ArgType, |
| 3109 | Expr *Arg, |
| 3110 | unsigned &TDF) { |
| 3111 | // C++0x [temp.deduct.call]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3112 | // 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] | 3113 | // are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3114 | if (ParamType.hasQualifiers()) |
| 3115 | ParamType = ParamType.getUnqualifiedType(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3116 | const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); |
| 3117 | if (ParamRefType) { |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3118 | QualType PointeeType = ParamRefType->getPointeeType(); |
| 3119 | |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3120 | // If the argument has incomplete array type, try to complete its type. |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3121 | if (ArgType->isIncompleteArrayType() && !S.RequireCompleteExprType(Arg, 0)) |
Douglas Gregor | 57d4f97 | 2011-06-03 03:35:07 +0000 | [diff] [blame] | 3122 | ArgType = Arg->getType(); |
| 3123 | |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3124 | // [C++0x] If P is an rvalue reference to a cv-unqualified |
| 3125 | // template parameter and the argument is an lvalue, the type |
| 3126 | // "lvalue reference to A" is used in place of A for type |
| 3127 | // deduction. |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3128 | if (isa<RValueReferenceType>(ParamType)) { |
| 3129 | if (!PointeeType.getQualifiers() && |
| 3130 | isa<TemplateTypeParmType>(PointeeType) && |
Douglas Gregor | 291e8ee | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 3131 | Arg->Classify(S.Context).isLValue() && |
| 3132 | Arg->getType() != S.Context.OverloadTy && |
| 3133 | Arg->getType() != S.Context.BoundMemberTy) |
Douglas Gregor | cba72b1 | 2011-01-21 05:18:22 +0000 | [diff] [blame] | 3134 | ArgType = S.Context.getLValueReferenceType(ArgType); |
| 3135 | } |
| 3136 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3137 | // [...] If P is a reference type, the type referred to by P is used |
| 3138 | // for type deduction. |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3139 | ParamType = PointeeType; |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3140 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3141 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3142 | // Overload sets usually make this parameter an undeduced |
| 3143 | // context, but there are sometimes special circumstances. |
| 3144 | if (ArgType == S.Context.OverloadTy) { |
| 3145 | ArgType = ResolveOverloadForDeduction(S, TemplateParams, |
| 3146 | Arg, ParamType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 3147 | ParamRefType != nullptr); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3148 | if (ArgType.isNull()) |
| 3149 | return true; |
| 3150 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3151 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3152 | if (ParamRefType) { |
| 3153 | // C++0x [temp.deduct.call]p3: |
| 3154 | // [...] If P is of the form T&&, where T is a template parameter, and |
| 3155 | // the argument is an lvalue, the type A& is used in place of A for |
| 3156 | // type deduction. |
| 3157 | if (ParamRefType->isRValueReferenceType() && |
| 3158 | ParamRefType->getAs<TemplateTypeParmType>() && |
| 3159 | Arg->isLValue()) |
| 3160 | ArgType = S.Context.getLValueReferenceType(ArgType); |
| 3161 | } else { |
| 3162 | // C++ [temp.deduct.call]p2: |
| 3163 | // If P is not a reference type: |
| 3164 | // - If A is an array type, the pointer type produced by the |
| 3165 | // array-to-pointer standard conversion (4.2) is used in place of |
| 3166 | // A for type deduction; otherwise, |
| 3167 | if (ArgType->isArrayType()) |
| 3168 | ArgType = S.Context.getArrayDecayedType(ArgType); |
| 3169 | // - If A is a function type, the pointer type produced by the |
| 3170 | // function-to-pointer standard conversion (4.3) is used in place |
| 3171 | // of A for type deduction; otherwise, |
| 3172 | else if (ArgType->isFunctionType()) |
| 3173 | ArgType = S.Context.getPointerType(ArgType); |
| 3174 | else { |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3175 | // - 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] | 3176 | // type are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3177 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3178 | } |
| 3179 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3180 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3181 | // C++0x [temp.deduct.call]p4: |
| 3182 | // In general, the deduction process attempts to find template argument |
| 3183 | // values that will make the deduced A identical to A (after the type A |
| 3184 | // is transformed as described above). [...] |
| 3185 | TDF = TDF_SkipNonDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3186 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3187 | // - If the original P is a reference type, the deduced A (i.e., the |
| 3188 | // type referred to by the reference) can be more cv-qualified than |
| 3189 | // the transformed A. |
| 3190 | if (ParamRefType) |
| 3191 | TDF |= TDF_ParamWithReferenceType; |
| 3192 | // - The transformed A can be another pointer or pointer to member |
| 3193 | // type that can be converted to the deduced A via a qualification |
| 3194 | // conversion (4.4). |
| 3195 | if (ArgType->isPointerType() || ArgType->isMemberPointerType() || |
| 3196 | ArgType->isObjCObjectPointerType()) |
| 3197 | TDF |= TDF_IgnoreQualifiers; |
| 3198 | // - If P is a class and P has the form simple-template-id, then the |
| 3199 | // transformed A can be a derived class of the deduced A. Likewise, |
| 3200 | // if P is a pointer to a class of the form simple-template-id, the |
| 3201 | // transformed A can be a pointer to a derived class pointed to by |
| 3202 | // the deduced A. |
| 3203 | if (isSimpleTemplateIdType(ParamType) || |
| 3204 | (isa<PointerType>(ParamType) && |
| 3205 | isSimpleTemplateIdType( |
| 3206 | ParamType->getAs<PointerType>()->getPointeeType()))) |
| 3207 | TDF |= TDF_DerivedClass; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3208 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3209 | return false; |
| 3210 | } |
| 3211 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3212 | static bool hasDeducibleTemplateParameters(Sema &S, |
| 3213 | FunctionTemplateDecl *FunctionTemplate, |
| 3214 | QualType T); |
| 3215 | |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3216 | /// \brief Perform template argument deduction by matching a parameter type |
| 3217 | /// against a single expression, where the expression is an element of |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3218 | /// an initializer list that was originally matched against a parameter |
| 3219 | /// of type \c initializer_list\<ParamType\>. |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3220 | static Sema::TemplateDeductionResult |
| 3221 | DeduceTemplateArgumentByListElement(Sema &S, |
| 3222 | TemplateParameterList *TemplateParams, |
| 3223 | QualType ParamType, Expr *Arg, |
| 3224 | TemplateDeductionInfo &Info, |
| 3225 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3226 | unsigned TDF) { |
| 3227 | // Handle the case where an init list contains another init list as the |
| 3228 | // element. |
| 3229 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
| 3230 | QualType X; |
| 3231 | if (!S.isStdInitializerList(ParamType.getNonReferenceType(), &X)) |
| 3232 | return Sema::TDK_Success; // Just ignore this expression. |
| 3233 | |
| 3234 | // Recurse down into the init list. |
| 3235 | for (unsigned i = 0, e = ILE->getNumInits(); i < e; ++i) { |
| 3236 | if (Sema::TemplateDeductionResult Result = |
| 3237 | DeduceTemplateArgumentByListElement(S, TemplateParams, X, |
| 3238 | ILE->getInit(i), |
| 3239 | Info, Deduced, TDF)) |
| 3240 | return Result; |
| 3241 | } |
| 3242 | return Sema::TDK_Success; |
| 3243 | } |
| 3244 | |
| 3245 | // For all other cases, just match by type. |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3246 | QualType ArgType = Arg->getType(); |
| 3247 | if (AdjustFunctionParmAndArgTypesForDeduction(S, TemplateParams, ParamType, |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3248 | ArgType, Arg, TDF)) { |
| 3249 | Info.Expression = Arg; |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3250 | return Sema::TDK_FailedOverloadResolution; |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3251 | } |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3252 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3253 | ArgType, Info, Deduced, TDF); |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3254 | } |
| 3255 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3256 | /// \brief Perform template argument deduction from a function call |
| 3257 | /// (C++ [temp.deduct.call]). |
| 3258 | /// |
| 3259 | /// \param FunctionTemplate the function template for which we are performing |
| 3260 | /// template argument deduction. |
| 3261 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3262 | /// \param ExplicitTemplateArgs the explicit template arguments provided |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3263 | /// for this call. |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3264 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3265 | /// \param Args the function call arguments |
| 3266 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3267 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3268 | /// this will be set to the function template specialization produced by |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3269 | /// template argument deduction. |
| 3270 | /// |
| 3271 | /// \param Info the argument will be updated to provide additional information |
| 3272 | /// about template argument deduction. |
| 3273 | /// |
| 3274 | /// \returns the result of template argument deduction. |
Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 3275 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3276 | FunctionTemplateDecl *FunctionTemplate, |
| 3277 | TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args, |
| 3278 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3279 | if (FunctionTemplate->isInvalidDecl()) |
| 3280 | return TDK_Invalid; |
| 3281 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3282 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3283 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3284 | // C++ [temp.deduct.call]p1: |
| 3285 | // Template argument deduction is done by comparing each function template |
| 3286 | // parameter type (call it P) with the type of the corresponding argument |
| 3287 | // of the call (call it A) as described below. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3288 | unsigned CheckArgs = Args.size(); |
| 3289 | if (Args.size() < Function->getMinRequiredArguments()) |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3290 | return TDK_TooFewArguments; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3291 | else if (Args.size() > Function->getNumParams()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3292 | const FunctionProtoType *Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3293 | = Function->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3294 | if (Proto->isTemplateVariadic()) |
| 3295 | /* Do nothing */; |
| 3296 | else if (Proto->isVariadic()) |
| 3297 | CheckArgs = Function->getNumParams(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3298 | else |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3299 | return TDK_TooManyArguments; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3300 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3301 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3302 | // The types of the parameters from which we will perform template argument |
| 3303 | // deduction. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3304 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3305 | TemplateParameterList *TemplateParams |
| 3306 | = FunctionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3307 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 3308 | SmallVector<QualType, 4> ParamTypes; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3309 | unsigned NumExplicitlySpecified = 0; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3310 | if (ExplicitTemplateArgs) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3311 | TemplateDeductionResult Result = |
| 3312 | SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3313 | *ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3314 | Deduced, |
| 3315 | ParamTypes, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 3316 | nullptr, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3317 | Info); |
| 3318 | if (Result) |
| 3319 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3320 | |
| 3321 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3322 | } else { |
| 3323 | // Just fill in the parameter types from the function declaration. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3324 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3325 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
| 3326 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3327 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3328 | // Deduce template arguments from the function parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3329 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3330 | unsigned ArgIdx = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3331 | SmallVector<OriginalCallArg, 4> OriginalCallArgs; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3332 | for (unsigned ParamIdx = 0, NumParams = ParamTypes.size(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3333 | ParamIdx != NumParams; ++ParamIdx) { |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3334 | QualType OrigParamType = ParamTypes[ParamIdx]; |
| 3335 | QualType ParamType = OrigParamType; |
| 3336 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3337 | const PackExpansionType *ParamExpansion |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3338 | = dyn_cast<PackExpansionType>(ParamType); |
| 3339 | if (!ParamExpansion) { |
| 3340 | // Simple case: matching a function parameter to a function argument. |
| 3341 | if (ArgIdx >= CheckArgs) |
| 3342 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3343 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3344 | Expr *Arg = Args[ArgIdx++]; |
| 3345 | QualType ArgType = Arg->getType(); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3346 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3347 | unsigned TDF = 0; |
| 3348 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, |
| 3349 | ParamType, ArgType, Arg, |
| 3350 | TDF)) |
| 3351 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3352 | |
Douglas Gregor | 0c83c81 | 2011-10-09 22:06:46 +0000 | [diff] [blame] | 3353 | // If we have nothing to deduce, we're done. |
| 3354 | if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
| 3355 | continue; |
| 3356 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3357 | // If the argument is an initializer list ... |
| 3358 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
| 3359 | // ... then the parameter is an undeduced context, unless the parameter |
| 3360 | // type is (reference to cv) std::initializer_list<P'>, in which case |
| 3361 | // deduction is done for each element of the initializer list, and the |
| 3362 | // result is the deduced type if it's the same for all elements. |
| 3363 | QualType X; |
| 3364 | // Removing references was already done. |
| 3365 | if (!isStdInitializerList(ParamType, &X)) |
| 3366 | continue; |
| 3367 | |
| 3368 | for (unsigned i = 0, e = ILE->getNumInits(); i < e; ++i) { |
| 3369 | if (TemplateDeductionResult Result = |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3370 | DeduceTemplateArgumentByListElement(*this, TemplateParams, X, |
| 3371 | ILE->getInit(i), |
| 3372 | Info, Deduced, TDF)) |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3373 | return Result; |
| 3374 | } |
| 3375 | // Don't track the argument type, since an initializer list has none. |
| 3376 | continue; |
| 3377 | } |
| 3378 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3379 | // Keep track of the argument type and corresponding parameter index, |
| 3380 | // so we can check for compatibility between the deduced A and A. |
Douglas Gregor | 0c83c81 | 2011-10-09 22:06:46 +0000 | [diff] [blame] | 3381 | OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx-1, |
| 3382 | ArgType)); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3383 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3384 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3385 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3386 | ParamType, ArgType, |
| 3387 | Info, Deduced, TDF)) |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3388 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3389 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3390 | continue; |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3391 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3392 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3393 | // C++0x [temp.deduct.call]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3394 | // For a function parameter pack that occurs at the end of the |
| 3395 | // parameter-declaration-list, the type A of each remaining argument of |
| 3396 | // the call is compared with the type P of the declarator-id of the |
| 3397 | // function parameter pack. Each comparison deduces template arguments |
| 3398 | // for subsequent positions in the template parameter packs expanded by |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3399 | // the function parameter pack. For a function parameter pack that does |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3400 | // 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] | 3401 | // the parameter pack is a non-deduced context. |
| 3402 | if (ParamIdx + 1 < NumParams) |
| 3403 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3404 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3405 | QualType ParamPattern = ParamExpansion->getPattern(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3406 | SmallVector<unsigned, 2> PackIndices; |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3407 | { |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 3408 | llvm::SmallBitVector SawIndices(TemplateParams->size()); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3409 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3410 | collectUnexpandedParameterPacks(ParamPattern, Unexpanded); |
| 3411 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 3412 | unsigned Depth, Index; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 3413 | std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3414 | if (Depth == 0 && !SawIndices[Index]) { |
| 3415 | SawIndices[Index] = true; |
| 3416 | PackIndices.push_back(Index); |
| 3417 | } |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3418 | } |
| 3419 | } |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3420 | assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3421 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3422 | // Keep track of the deduced template arguments for each parameter pack |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3423 | // expanded by this pack expansion (the outer index) and for each |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3424 | // template argument (the inner SmallVectors). |
Craig Topper | 0a4e1f5 | 2013-07-08 04:44:01 +0000 | [diff] [blame] | 3425 | NewlyDeducedPacksType NewlyDeducedPacks(PackIndices.size()); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3426 | SmallVector<DeducedTemplateArgument, 2> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3427 | SavedPacks(PackIndices.size()); |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 3428 | PrepareArgumentPackDeduction(*this, Deduced, PackIndices, SavedPacks, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3429 | NewlyDeducedPacks); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3430 | bool HasAnyArguments = false; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3431 | for (; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3432 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3433 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3434 | QualType OrigParamType = ParamPattern; |
| 3435 | ParamType = OrigParamType; |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3436 | Expr *Arg = Args[ArgIdx]; |
| 3437 | QualType ArgType = Arg->getType(); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3438 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3439 | unsigned TDF = 0; |
| 3440 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, |
| 3441 | ParamType, ArgType, Arg, |
| 3442 | TDF)) { |
| 3443 | // We can't actually perform any deduction for this argument, so stop |
| 3444 | // deduction at this point. |
| 3445 | ++ArgIdx; |
| 3446 | break; |
| 3447 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3448 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3449 | // As above, initializer lists need special handling. |
| 3450 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
| 3451 | QualType X; |
| 3452 | if (!isStdInitializerList(ParamType, &X)) { |
| 3453 | ++ArgIdx; |
| 3454 | break; |
| 3455 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3456 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3457 | for (unsigned i = 0, e = ILE->getNumInits(); i < e; ++i) { |
| 3458 | if (TemplateDeductionResult Result = |
| 3459 | DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, X, |
| 3460 | ILE->getInit(i)->getType(), |
| 3461 | Info, Deduced, TDF)) |
| 3462 | return Result; |
| 3463 | } |
| 3464 | } else { |
| 3465 | |
| 3466 | // Keep track of the argument type and corresponding argument index, |
| 3467 | // so we can check for compatibility between the deduced A and A. |
| 3468 | if (hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
| 3469 | OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx, |
| 3470 | ArgType)); |
| 3471 | |
| 3472 | if (TemplateDeductionResult Result |
| 3473 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3474 | ParamType, ArgType, Info, |
| 3475 | Deduced, TDF)) |
| 3476 | return Result; |
| 3477 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3478 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3479 | // Capture the deduced template arguments for each parameter pack expanded |
| 3480 | // by this pack expansion, add them to the list of arguments we've deduced |
| 3481 | // for that pack, then clear out the deduced argument. |
| 3482 | for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) { |
| 3483 | DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]]; |
| 3484 | if (!DeducedArg.isNull()) { |
| 3485 | NewlyDeducedPacks[I].push_back(DeducedArg); |
| 3486 | DeducedArg = DeducedTemplateArgument(); |
| 3487 | } |
| 3488 | } |
| 3489 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3490 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3491 | // Build argument packs for each of the parameter packs expanded by this |
| 3492 | // pack expansion. |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 3493 | if (Sema::TemplateDeductionResult Result |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3494 | = FinishArgumentPackDeduction(*this, TemplateParams, HasAnyArguments, |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 3495 | Deduced, PackIndices, SavedPacks, |
| 3496 | NewlyDeducedPacks, Info)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3497 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3498 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3499 | // After we've matching against a parameter pack, we're done. |
| 3500 | break; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3501 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3502 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3503 | return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3504 | NumExplicitlySpecified, |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3505 | Specialization, Info, &OriginalCallArgs); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3506 | } |
| 3507 | |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3508 | QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType, |
| 3509 | QualType FunctionType) { |
| 3510 | if (ArgFunctionType.isNull()) |
| 3511 | return ArgFunctionType; |
| 3512 | |
| 3513 | const FunctionProtoType *FunctionTypeP = |
| 3514 | FunctionType->castAs<FunctionProtoType>(); |
| 3515 | CallingConv CC = FunctionTypeP->getCallConv(); |
| 3516 | bool NoReturn = FunctionTypeP->getNoReturnAttr(); |
| 3517 | const FunctionProtoType *ArgFunctionTypeP = |
| 3518 | ArgFunctionType->getAs<FunctionProtoType>(); |
| 3519 | if (ArgFunctionTypeP->getCallConv() == CC && |
| 3520 | ArgFunctionTypeP->getNoReturnAttr() == NoReturn) |
| 3521 | return ArgFunctionType; |
| 3522 | |
| 3523 | FunctionType::ExtInfo EI = ArgFunctionTypeP->getExtInfo().withCallingConv(CC); |
| 3524 | EI = EI.withNoReturn(NoReturn); |
| 3525 | ArgFunctionTypeP = |
| 3526 | cast<FunctionProtoType>(Context.adjustFunctionType(ArgFunctionTypeP, EI)); |
| 3527 | return QualType(ArgFunctionTypeP, 0); |
| 3528 | } |
| 3529 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3530 | /// \brief Deduce template arguments when taking the address of a function |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3531 | /// template (C++ [temp.deduct.funcaddr]) or matching a specialization to |
| 3532 | /// a template. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3533 | /// |
| 3534 | /// \param FunctionTemplate the function template for which we are performing |
| 3535 | /// template argument deduction. |
| 3536 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3537 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3538 | /// arguments. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3539 | /// |
| 3540 | /// \param ArgFunctionType the function type that will be used as the |
| 3541 | /// "argument" type (A) when performing template argument deduction from the |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3542 | /// function template's function type. This type may be NULL, if there is no |
| 3543 | /// argument type to compare against, in C++0x [temp.arg.explicit]p3. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3544 | /// |
| 3545 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3546 | /// this will be set to the function template specialization produced by |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3547 | /// template argument deduction. |
| 3548 | /// |
| 3549 | /// \param Info the argument will be updated to provide additional information |
| 3550 | /// about template argument deduction. |
| 3551 | /// |
| 3552 | /// \returns the result of template argument deduction. |
| 3553 | Sema::TemplateDeductionResult |
| 3554 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3555 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3556 | QualType ArgFunctionType, |
| 3557 | FunctionDecl *&Specialization, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3558 | TemplateDeductionInfo &Info, |
| 3559 | bool InOverloadResolution) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3560 | if (FunctionTemplate->isInvalidDecl()) |
| 3561 | return TDK_Invalid; |
| 3562 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3563 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 3564 | TemplateParameterList *TemplateParams |
| 3565 | = FunctionTemplate->getTemplateParameters(); |
| 3566 | QualType FunctionType = Function->getType(); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3567 | if (!InOverloadResolution) |
| 3568 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3569 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3570 | // Substitute any explicit template arguments. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3571 | LocalInstantiationScope InstScope(*this); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3572 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3573 | unsigned NumExplicitlySpecified = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3574 | SmallVector<QualType, 4> ParamTypes; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3575 | if (ExplicitTemplateArgs) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3576 | if (TemplateDeductionResult Result |
| 3577 | = SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3578 | *ExplicitTemplateArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3579 | Deduced, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3580 | &FunctionType, Info)) |
| 3581 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3582 | |
| 3583 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3586 | // Unevaluated SFINAE context. |
| 3587 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3588 | SFINAETrap Trap(*this); |
| 3589 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3590 | Deduced.resize(TemplateParams->size()); |
| 3591 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3592 | // If the function has a deduced return type, substitute it for a dependent |
| 3593 | // 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] | 3594 | bool HasDeducedReturnType = false; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3595 | if (getLangOpts().CPlusPlus1y && InOverloadResolution && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3596 | Function->getReturnType()->getContainedAutoType()) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3597 | FunctionType = SubstAutoType(FunctionType, Context.DependentTy); |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3598 | HasDeducedReturnType = true; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3601 | if (!ArgFunctionType.isNull()) { |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3602 | unsigned TDF = TDF_TopLevelParameterTypeList; |
| 3603 | if (InOverloadResolution) TDF |= TDF_InOverloadResolution; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3604 | // Deduce template arguments from the function type. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3605 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3606 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3607 | FunctionType, ArgFunctionType, |
| 3608 | Info, Deduced, TDF)) |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3609 | return Result; |
| 3610 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3611 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3612 | if (TemplateDeductionResult Result |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3613 | = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
| 3614 | NumExplicitlySpecified, |
| 3615 | Specialization, Info)) |
| 3616 | return Result; |
| 3617 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3618 | // If the function has a deduced return type, deduce it now, so we can check |
| 3619 | // that the deduced function type matches the requested type. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3620 | if (HasDeducedReturnType && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3621 | Specialization->getReturnType()->isUndeducedType() && |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3622 | DeduceReturnType(Specialization, Info.getLocation(), false)) |
| 3623 | return TDK_MiscellaneousDeductionFailure; |
| 3624 | |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3625 | // 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] | 3626 | // specialization with respect to arguments of compatible pointer to function |
| 3627 | // types, template argument deduction fails. |
| 3628 | if (!ArgFunctionType.isNull()) { |
| 3629 | if (InOverloadResolution && !isSameOrCompatibleFunctionType( |
| 3630 | Context.getCanonicalType(Specialization->getType()), |
| 3631 | Context.getCanonicalType(ArgFunctionType))) |
| 3632 | return TDK_MiscellaneousDeductionFailure; |
| 3633 | else if(!InOverloadResolution && |
| 3634 | !Context.hasSameType(Specialization->getType(), ArgFunctionType)) |
| 3635 | return TDK_MiscellaneousDeductionFailure; |
| 3636 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3637 | |
| 3638 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3639 | } |
| 3640 | |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3641 | /// \brief Given a function declaration (e.g. a generic lambda conversion |
| 3642 | /// function) that contains an 'auto' in its result type, substitute it |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3643 | /// with TypeToReplaceAutoWith. Be careful to pass in the type you want |
| 3644 | /// to replace 'auto' with and not the actual result type you want |
| 3645 | /// to set the function to. |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3646 | static inline void |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3647 | SubstAutoWithinFunctionReturnType(FunctionDecl *F, |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3648 | QualType TypeToReplaceAutoWith, Sema &S) { |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3649 | assert(!TypeToReplaceAutoWith->getContainedAutoType()); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3650 | QualType AutoResultType = F->getReturnType(); |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3651 | assert(AutoResultType->getContainedAutoType()); |
| 3652 | QualType DeducedResultType = S.SubstAutoType(AutoResultType, |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3653 | TypeToReplaceAutoWith); |
| 3654 | S.Context.adjustDeducedFunctionResultType(F, DeducedResultType); |
| 3655 | } |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3656 | |
| 3657 | /// \brief Given a specialized conversion operator of a generic lambda |
| 3658 | /// create the corresponding specializations of the call operator and |
| 3659 | /// the static-invoker. If the return type of the call operator is auto, |
| 3660 | /// deduce its return type and check if that matches the |
| 3661 | /// return type of the destination function ptr. |
| 3662 | |
| 3663 | static inline Sema::TemplateDeductionResult |
| 3664 | SpecializeCorrespondingLambdaCallOperatorAndInvoker( |
| 3665 | CXXConversionDecl *ConversionSpecialized, |
| 3666 | SmallVectorImpl<DeducedTemplateArgument> &DeducedArguments, |
| 3667 | QualType ReturnTypeOfDestFunctionPtr, |
| 3668 | TemplateDeductionInfo &TDInfo, |
| 3669 | Sema &S) { |
| 3670 | |
| 3671 | CXXRecordDecl *LambdaClass = ConversionSpecialized->getParent(); |
| 3672 | assert(LambdaClass && LambdaClass->isGenericLambda()); |
| 3673 | |
| 3674 | CXXMethodDecl *CallOpGeneric = LambdaClass->getLambdaCallOperator(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3675 | QualType CallOpResultType = CallOpGeneric->getReturnType(); |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3676 | const bool GenericLambdaCallOperatorHasDeducedReturnType = |
| 3677 | CallOpResultType->getContainedAutoType(); |
| 3678 | |
| 3679 | FunctionTemplateDecl *CallOpTemplate = |
| 3680 | CallOpGeneric->getDescribedFunctionTemplate(); |
| 3681 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 3682 | FunctionDecl *CallOpSpecialized = nullptr; |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3683 | // Use the deduced arguments of the conversion function, to specialize our |
| 3684 | // generic lambda's call operator. |
| 3685 | if (Sema::TemplateDeductionResult Result |
| 3686 | = S.FinishTemplateArgumentDeduction(CallOpTemplate, |
| 3687 | DeducedArguments, |
| 3688 | 0, CallOpSpecialized, TDInfo)) |
| 3689 | return Result; |
| 3690 | |
| 3691 | // 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] | 3692 | if (GenericLambdaCallOperatorHasDeducedReturnType && |
| 3693 | CallOpSpecialized->getReturnType()->isUndeducedType()) |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3694 | S.DeduceReturnType(CallOpSpecialized, |
| 3695 | CallOpSpecialized->getPointOfInstantiation(), |
| 3696 | /*Diagnose*/ true); |
| 3697 | |
| 3698 | // Check to see if the return type of the destination ptr-to-function |
| 3699 | // matches the return type of the call operator. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3700 | if (!S.Context.hasSameType(CallOpSpecialized->getReturnType(), |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3701 | ReturnTypeOfDestFunctionPtr)) |
| 3702 | return Sema::TDK_NonDeducedMismatch; |
| 3703 | // Since we have succeeded in matching the source and destination |
| 3704 | // ptr-to-functions (now including return type), and have successfully |
| 3705 | // specialized our corresponding call operator, we are ready to |
| 3706 | // specialize the static invoker with the deduced arguments of our |
| 3707 | // ptr-to-function. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 3708 | FunctionDecl *InvokerSpecialized = nullptr; |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3709 | FunctionTemplateDecl *InvokerTemplate = LambdaClass-> |
| 3710 | getLambdaStaticInvoker()->getDescribedFunctionTemplate(); |
| 3711 | |
| 3712 | Sema::TemplateDeductionResult LLVM_ATTRIBUTE_UNUSED Result |
| 3713 | = S.FinishTemplateArgumentDeduction(InvokerTemplate, DeducedArguments, 0, |
| 3714 | InvokerSpecialized, TDInfo); |
| 3715 | assert(Result == Sema::TDK_Success && |
| 3716 | "If the call operator succeeded so should the invoker!"); |
| 3717 | // Set the result type to match the corresponding call operator |
| 3718 | // specialization's result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3719 | if (GenericLambdaCallOperatorHasDeducedReturnType && |
| 3720 | InvokerSpecialized->getReturnType()->isUndeducedType()) { |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3721 | // Be sure to get the type to replace 'auto' with and not |
| 3722 | // the full result type of the call op specialization |
| 3723 | // to substitute into the 'auto' of the invoker and conversion |
| 3724 | // function. |
| 3725 | // For e.g. |
| 3726 | // int* (*fp)(int*) = [](auto* a) -> auto* { return a; }; |
| 3727 | // We don't want to subst 'int*' into 'auto' to get int**. |
| 3728 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3729 | QualType TypeToReplaceAutoWith = CallOpSpecialized->getReturnType() |
| 3730 | ->getContainedAutoType() |
| 3731 | ->getDeducedType(); |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3732 | SubstAutoWithinFunctionReturnType(InvokerSpecialized, |
| 3733 | TypeToReplaceAutoWith, S); |
| 3734 | SubstAutoWithinFunctionReturnType(ConversionSpecialized, |
| 3735 | TypeToReplaceAutoWith, S); |
| 3736 | } |
| 3737 | |
| 3738 | // Ensure that static invoker doesn't have a const qualifier. |
| 3739 | // FIXME: When creating the InvokerTemplate in SemaLambda.cpp |
| 3740 | // do not use the CallOperator's TypeSourceInfo which allows |
| 3741 | // the const qualifier to leak through. |
| 3742 | const FunctionProtoType *InvokerFPT = InvokerSpecialized-> |
| 3743 | getType().getTypePtr()->castAs<FunctionProtoType>(); |
| 3744 | FunctionProtoType::ExtProtoInfo EPI = InvokerFPT->getExtProtoInfo(); |
| 3745 | EPI.TypeQuals = 0; |
| 3746 | InvokerSpecialized->setType(S.Context.getFunctionType( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3747 | InvokerFPT->getReturnType(), InvokerFPT->getParamTypes(), EPI)); |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3748 | return Sema::TDK_Success; |
| 3749 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3750 | /// \brief Deduce template arguments for a templated conversion |
| 3751 | /// function (C++ [temp.deduct.conv]) and, if successful, produce a |
| 3752 | /// conversion function template specialization. |
| 3753 | Sema::TemplateDeductionResult |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3754 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *ConversionTemplate, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3755 | QualType ToType, |
| 3756 | CXXConversionDecl *&Specialization, |
| 3757 | TemplateDeductionInfo &Info) { |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3758 | if (ConversionTemplate->isInvalidDecl()) |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3759 | return TDK_Invalid; |
| 3760 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3761 | CXXConversionDecl *ConversionGeneric |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3762 | = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl()); |
| 3763 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3764 | QualType FromType = ConversionGeneric->getConversionType(); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3765 | |
| 3766 | // Canonicalize the types for deduction. |
| 3767 | QualType P = Context.getCanonicalType(FromType); |
| 3768 | QualType A = Context.getCanonicalType(ToType); |
| 3769 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3770 | // C++0x [temp.deduct.conv]p2: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3771 | // If P is a reference type, the type referred to by P is used for |
| 3772 | // type deduction. |
| 3773 | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
| 3774 | P = PRef->getPointeeType(); |
| 3775 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3776 | // C++0x [temp.deduct.conv]p4: |
| 3777 | // [...] 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] | 3778 | // for type deduction. |
| 3779 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3780 | A = ARef->getPointeeType().getUnqualifiedType(); |
| 3781 | // C++ [temp.deduct.conv]p3: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3782 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3783 | // If A is not a reference type: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3784 | else { |
| 3785 | assert(!A->isReferenceType() && "Reference types were handled above"); |
| 3786 | |
| 3787 | // - If P is an array type, the pointer type produced by the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3788 | // array-to-pointer standard conversion (4.2) is used in place |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3789 | // of P for type deduction; otherwise, |
| 3790 | if (P->isArrayType()) |
| 3791 | P = Context.getArrayDecayedType(P); |
| 3792 | // - If P is a function type, the pointer type produced by the |
| 3793 | // function-to-pointer standard conversion (4.3) is used in |
| 3794 | // place of P for type deduction; otherwise, |
| 3795 | else if (P->isFunctionType()) |
| 3796 | P = Context.getPointerType(P); |
| 3797 | // - 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] | 3798 | // P's type are ignored for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3799 | else |
| 3800 | P = P.getUnqualifiedType(); |
| 3801 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3802 | // C++0x [temp.deduct.conv]p4: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3803 | // If A is a cv-qualified type, the top level cv-qualifiers of A's |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3804 | // type are ignored for type deduction. If A is a reference type, the type |
| 3805 | // referred to by A is used for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3806 | A = A.getUnqualifiedType(); |
| 3807 | } |
| 3808 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3809 | // Unevaluated SFINAE context. |
| 3810 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3811 | SFINAETrap Trap(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3812 | |
| 3813 | // C++ [temp.deduct.conv]p1: |
| 3814 | // Template argument deduction is done by comparing the return |
| 3815 | // type of the template conversion function (call it P) with the |
| 3816 | // type that is required as the result of the conversion (call it |
| 3817 | // A) as described in 14.8.2.4. |
| 3818 | TemplateParameterList *TemplateParams |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3819 | = ConversionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3820 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3821 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3822 | |
| 3823 | // C++0x [temp.deduct.conv]p4: |
| 3824 | // In general, the deduction process attempts to find template |
| 3825 | // argument values that will make the deduced A identical to |
| 3826 | // A. However, there are two cases that allow a difference: |
| 3827 | unsigned TDF = 0; |
| 3828 | // - If the original A is a reference type, A can be more |
| 3829 | // cv-qualified than the deduced A (i.e., the type referred to |
| 3830 | // by the reference) |
| 3831 | if (ToType->isReferenceType()) |
| 3832 | TDF |= TDF_ParamWithReferenceType; |
| 3833 | // - The deduced A can be another pointer or pointer to member |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3834 | // type that can be converted to A via a qualification |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3835 | // conversion. |
| 3836 | // |
| 3837 | // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when |
| 3838 | // both P and A are pointers or member pointers. In this case, we |
| 3839 | // just ignore cv-qualifiers completely). |
| 3840 | if ((P->isPointerType() && A->isPointerType()) || |
Douglas Gregor | 9f05ed5 | 2011-08-30 00:37:54 +0000 | [diff] [blame] | 3841 | (P->isMemberPointerType() && A->isMemberPointerType())) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3842 | TDF |= TDF_IgnoreQualifiers; |
| 3843 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3844 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3845 | P, A, Info, Deduced, TDF)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3846 | return Result; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3847 | |
| 3848 | // Create an Instantiation Scope for finalizing the operator. |
| 3849 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3850 | // Finish template argument deduction. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 3851 | FunctionDecl *ConversionSpecialized = nullptr; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3852 | TemplateDeductionResult Result |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3853 | = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0, |
| 3854 | ConversionSpecialized, Info); |
| 3855 | Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized); |
| 3856 | |
| 3857 | // If the conversion operator is being invoked on a lambda closure to convert |
| 3858 | // to a ptr-to-function, use the deduced arguments from the conversion function |
| 3859 | // to specialize the corresponding call operator. |
| 3860 | // e.g., int (*fp)(int) = [](auto a) { return a; }; |
| 3861 | if (Result == TDK_Success && isLambdaConversionOperator(ConversionGeneric)) { |
| 3862 | |
| 3863 | // Get the return type of the destination ptr-to-function we are converting |
| 3864 | // to. This is necessary for matching the lambda call operator's return |
| 3865 | // type to that of the destination ptr-to-function's return type. |
| 3866 | assert(A->isPointerType() && |
| 3867 | "Can only convert from lambda to ptr-to-function"); |
| 3868 | const FunctionType *ToFunType = |
| 3869 | A->getPointeeType().getTypePtr()->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3870 | const QualType DestFunctionPtrReturnType = ToFunType->getReturnType(); |
| 3871 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3872 | // Create the corresponding specializations of the call operator and |
| 3873 | // the static-invoker; and if the return type is auto, |
| 3874 | // deduce the return type and check if it matches the |
| 3875 | // DestFunctionPtrReturnType. |
| 3876 | // For instance: |
| 3877 | // auto L = [](auto a) { return f(a); }; |
| 3878 | // int (*fp)(int) = L; |
| 3879 | // char (*fp2)(int) = L; <-- Not OK. |
| 3880 | |
| 3881 | Result = SpecializeCorrespondingLambdaCallOperatorAndInvoker( |
| 3882 | Specialization, Deduced, DestFunctionPtrReturnType, |
| 3883 | Info, *this); |
| 3884 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3885 | return Result; |
| 3886 | } |
| 3887 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3888 | /// \brief Deduce template arguments for a function template when there is |
| 3889 | /// nothing to deduce against (C++0x [temp.arg.explicit]p3). |
| 3890 | /// |
| 3891 | /// \param FunctionTemplate the function template for which we are performing |
| 3892 | /// template argument deduction. |
| 3893 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3894 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3895 | /// arguments. |
| 3896 | /// |
| 3897 | /// \param Specialization if template argument deduction was successful, |
| 3898 | /// this will be set to the function template specialization produced by |
| 3899 | /// template argument deduction. |
| 3900 | /// |
| 3901 | /// \param Info the argument will be updated to provide additional information |
| 3902 | /// about template argument deduction. |
| 3903 | /// |
| 3904 | /// \returns the result of template argument deduction. |
| 3905 | Sema::TemplateDeductionResult |
| 3906 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3907 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3908 | FunctionDecl *&Specialization, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3909 | TemplateDeductionInfo &Info, |
| 3910 | bool InOverloadResolution) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3911 | return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3912 | QualType(), Specialization, Info, |
| 3913 | InOverloadResolution); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3916 | namespace { |
| 3917 | /// Substitute the 'auto' type specifier within a type for a given replacement |
| 3918 | /// type. |
| 3919 | class SubstituteAutoTransform : |
| 3920 | public TreeTransform<SubstituteAutoTransform> { |
| 3921 | QualType Replacement; |
| 3922 | public: |
| 3923 | SubstituteAutoTransform(Sema &SemaRef, QualType Replacement) : |
| 3924 | TreeTransform<SubstituteAutoTransform>(SemaRef), Replacement(Replacement) { |
| 3925 | } |
| 3926 | QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) { |
| 3927 | // If we're building the type pattern to deduce against, don't wrap the |
| 3928 | // substituted type in an AutoType. Certain template deduction rules |
| 3929 | // apply only when a template type parameter appears directly (and not if |
| 3930 | // the parameter is found through desugaring). For instance: |
| 3931 | // auto &&lref = lvalue; |
| 3932 | // must transform into "rvalue reference to T" not "rvalue reference to |
| 3933 | // 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] | 3934 | if (!Replacement.isNull() && isa<TemplateTypeParmType>(Replacement)) { |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3935 | QualType Result = Replacement; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 3936 | TemplateTypeParmTypeLoc NewTL = |
| 3937 | TLB.push<TemplateTypeParmTypeLoc>(Result); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3938 | NewTL.setNameLoc(TL.getNameLoc()); |
| 3939 | return Result; |
| 3940 | } else { |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 3941 | bool Dependent = |
| 3942 | !Replacement.isNull() && Replacement->isDependentType(); |
| 3943 | QualType Result = |
| 3944 | SemaRef.Context.getAutoType(Dependent ? QualType() : Replacement, |
| 3945 | TL.getTypePtr()->isDecltypeAuto(), |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3946 | Dependent); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3947 | AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result); |
| 3948 | NewTL.setNameLoc(TL.getNameLoc()); |
| 3949 | return Result; |
| 3950 | } |
| 3951 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 3952 | |
| 3953 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 3954 | // Lambdas never need to be transformed. |
| 3955 | return E; |
| 3956 | } |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3957 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3958 | QualType Apply(TypeLoc TL) { |
| 3959 | // Create some scratch storage for the transformed type locations. |
| 3960 | // FIXME: We're just going to throw this information away. Don't build it. |
| 3961 | TypeLocBuilder TLB; |
| 3962 | TLB.reserve(TL.getFullDataSize()); |
| 3963 | return TransformType(TLB, TL); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3964 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3965 | }; |
| 3966 | } |
| 3967 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3968 | Sema::DeduceAutoResult |
| 3969 | Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *&Init, QualType &Result) { |
| 3970 | return DeduceAutoType(Type->getTypeLoc(), Init, Result); |
| 3971 | } |
| 3972 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3973 | /// \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] | 3974 | /// |
| 3975 | /// \param Type the type pattern using the auto type-specifier. |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3976 | /// \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] | 3977 | /// \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] | 3978 | /// deduced type. |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 3979 | Sema::DeduceAutoResult |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3980 | Sema::DeduceAutoType(TypeLoc Type, Expr *&Init, QualType &Result) { |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 3981 | if (Init->getType()->isNonOverloadPlaceholderType()) { |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3982 | ExprResult NonPlaceholder = CheckPlaceholderExpr(Init); |
| 3983 | if (NonPlaceholder.isInvalid()) |
| 3984 | return DAR_FailedAlreadyDiagnosed; |
| 3985 | Init = NonPlaceholder.take(); |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 3986 | } |
| 3987 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3988 | if (Init->isTypeDependent() || Type.getType()->isDependentType()) { |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3989 | Result = SubstituteAutoTransform(*this, Context.DependentTy).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3990 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 3991 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3992 | } |
| 3993 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 3994 | // If this is a 'decltype(auto)' specifier, do the decltype dance. |
| 3995 | // Since 'decltype(auto)' can only occur at the top of the type, we |
| 3996 | // don't need to go digging for it. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3997 | if (const AutoType *AT = Type.getType()->getAs<AutoType>()) { |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 3998 | if (AT->isDecltypeAuto()) { |
| 3999 | if (isa<InitListExpr>(Init)) { |
| 4000 | Diag(Init->getLocStart(), diag::err_decltype_auto_initializer_list); |
| 4001 | return DAR_FailedAlreadyDiagnosed; |
| 4002 | } |
| 4003 | |
| 4004 | QualType Deduced = BuildDecltypeType(Init, Init->getLocStart()); |
| 4005 | // FIXME: Support a non-canonical deduced type for 'auto'. |
| 4006 | Deduced = Context.getCanonicalType(Deduced); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4007 | Result = SubstituteAutoTransform(*this, Deduced).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4008 | if (Result.isNull()) |
| 4009 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4010 | return DAR_Succeeded; |
| 4011 | } |
| 4012 | } |
| 4013 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4014 | SourceLocation Loc = Init->getExprLoc(); |
| 4015 | |
| 4016 | LocalInstantiationScope InstScope(*this); |
| 4017 | |
| 4018 | // Build template<class TemplParam> void Func(FuncParam); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4019 | TemplateTypeParmDecl *TemplParam = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 4020 | TemplateTypeParmDecl::Create(Context, nullptr, SourceLocation(), Loc, 0, 0, |
| 4021 | nullptr, false, false); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4022 | QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0); |
| 4023 | NamedDecl *TemplParamPtr = TemplParam; |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 4024 | FixedSizeTemplateParameterList<1> TemplateParams(Loc, Loc, &TemplParamPtr, |
| 4025 | Loc); |
| 4026 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4027 | QualType FuncParam = SubstituteAutoTransform(*this, TemplArg).Apply(Type); |
| 4028 | assert(!FuncParam.isNull() && |
| 4029 | "substituting template parameter for 'auto' failed"); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4030 | |
| 4031 | // Deduce type of TemplParam in Func(Init) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4032 | SmallVector<DeducedTemplateArgument, 1> Deduced; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4033 | Deduced.resize(1); |
| 4034 | QualType InitType = Init->getType(); |
| 4035 | unsigned TDF = 0; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4036 | |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4037 | TemplateDeductionInfo Info(Loc); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4038 | |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4039 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4040 | if (InitList) { |
| 4041 | for (unsigned i = 0, e = InitList->getNumInits(); i < e; ++i) { |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4042 | if (DeduceTemplateArgumentByListElement(*this, &TemplateParams, |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 4043 | TemplArg, |
| 4044 | InitList->getInit(i), |
| 4045 | Info, Deduced, TDF)) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4046 | return DAR_Failed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4047 | } |
| 4048 | } else { |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 4049 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, &TemplateParams, |
| 4050 | FuncParam, InitType, Init, |
| 4051 | TDF)) |
| 4052 | return DAR_Failed; |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4053 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4054 | if (DeduceTemplateArgumentsByTypeMatch(*this, &TemplateParams, FuncParam, |
| 4055 | InitType, Info, Deduced, TDF)) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4056 | return DAR_Failed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4057 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4058 | |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4059 | if (Deduced[0].getKind() != TemplateArgument::Type) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4060 | return DAR_Failed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4061 | |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4062 | QualType DeducedType = Deduced[0].getAsType(); |
| 4063 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4064 | if (InitList) { |
| 4065 | DeducedType = BuildStdInitializerList(DeducedType, Loc); |
| 4066 | if (DeducedType.isNull()) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4067 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4068 | } |
| 4069 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4070 | Result = SubstituteAutoTransform(*this, DeducedType).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4071 | if (Result.isNull()) |
| 4072 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4073 | |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4074 | // Check that the deduced argument type is compatible with the original |
| 4075 | // argument type per C++ [temp.deduct.call]p4. |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4076 | if (!InitList && !Result.isNull() && |
| 4077 | CheckOriginalCallArgDeduction(*this, |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4078 | Sema::OriginalCallArg(FuncParam,0,InitType), |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4079 | Result)) { |
| 4080 | Result = QualType(); |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4081 | return DAR_Failed; |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4082 | } |
| 4083 | |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4084 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4085 | } |
| 4086 | |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4087 | QualType Sema::SubstAutoType(QualType TypeWithAuto, |
| 4088 | QualType TypeToReplaceAuto) { |
| 4089 | return SubstituteAutoTransform(*this, TypeToReplaceAuto). |
| 4090 | TransformType(TypeWithAuto); |
| 4091 | } |
| 4092 | |
| 4093 | TypeSourceInfo* Sema::SubstAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, |
| 4094 | QualType TypeToReplaceAuto) { |
| 4095 | return SubstituteAutoTransform(*this, TypeToReplaceAuto). |
| 4096 | TransformType(TypeWithAuto); |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 4097 | } |
| 4098 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4099 | void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) { |
| 4100 | if (isa<InitListExpr>(Init)) |
| 4101 | Diag(VDecl->getLocation(), |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4102 | VDecl->isInitCapture() |
| 4103 | ? diag::err_init_capture_deduction_failure_from_init_list |
| 4104 | : diag::err_auto_var_deduction_failure_from_init_list) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4105 | << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange(); |
| 4106 | else |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4107 | Diag(VDecl->getLocation(), |
| 4108 | VDecl->isInitCapture() ? diag::err_init_capture_deduction_failure |
| 4109 | : diag::err_auto_var_deduction_failure) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4110 | << VDecl->getDeclName() << VDecl->getType() << Init->getType() |
| 4111 | << Init->getSourceRange(); |
| 4112 | } |
| 4113 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4114 | bool Sema::DeduceReturnType(FunctionDecl *FD, SourceLocation Loc, |
| 4115 | bool Diagnose) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4116 | assert(FD->getReturnType()->isUndeducedType()); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4117 | |
| 4118 | if (FD->getTemplateInstantiationPattern()) |
| 4119 | InstantiateFunctionDefinition(Loc, FD); |
| 4120 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4121 | bool StillUndeduced = FD->getReturnType()->isUndeducedType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4122 | if (StillUndeduced && Diagnose && !FD->isInvalidDecl()) { |
| 4123 | Diag(Loc, diag::err_auto_fn_used_before_defined) << FD; |
| 4124 | Diag(FD->getLocation(), diag::note_callee_decl) << FD; |
| 4125 | } |
| 4126 | |
| 4127 | return StillUndeduced; |
| 4128 | } |
| 4129 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4130 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4131 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4132 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4133 | unsigned Level, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4134 | llvm::SmallBitVector &Deduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4135 | |
| 4136 | /// \brief If this is a non-static member function, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 4137 | static void |
| 4138 | AddImplicitObjectParameterType(ASTContext &Context, |
| 4139 | CXXMethodDecl *Method, |
| 4140 | SmallVectorImpl<QualType> &ArgTypes) { |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4141 | // C++11 [temp.func.order]p3: |
| 4142 | // [...] The new parameter is of type "reference to cv A," where cv are |
| 4143 | // the cv-qualifiers of the function template (if any) and A is |
| 4144 | // the class of which the function template is a member. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4145 | // |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4146 | // The standard doesn't say explicitly, but we pick the appropriate kind of |
| 4147 | // reference type based on [over.match.funcs]p4. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4148 | QualType ArgTy = Context.getTypeDeclType(Method->getParent()); |
| 4149 | ArgTy = Context.getQualifiedType(ArgTy, |
| 4150 | Qualifiers::fromCVRMask(Method->getTypeQualifiers())); |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4151 | if (Method->getRefQualifier() == RQ_RValue) |
| 4152 | ArgTy = Context.getRValueReferenceType(ArgTy); |
| 4153 | else |
| 4154 | ArgTy = Context.getLValueReferenceType(ArgTy); |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4155 | ArgTypes.push_back(ArgTy); |
| 4156 | } |
| 4157 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4158 | /// \brief Determine whether the function template \p FT1 is at least as |
| 4159 | /// specialized as \p FT2. |
| 4160 | static bool isAtLeastAsSpecializedAs(Sema &S, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4161 | SourceLocation Loc, |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4162 | FunctionTemplateDecl *FT1, |
| 4163 | FunctionTemplateDecl *FT2, |
| 4164 | TemplatePartialOrderingContext TPOC, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4165 | unsigned NumCallArguments1, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4166 | SmallVectorImpl<RefParamPartialOrderingComparison> *RefParamComparisons) { |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4167 | FunctionDecl *FD1 = FT1->getTemplatedDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4168 | FunctionDecl *FD2 = FT2->getTemplatedDecl(); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4169 | const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); |
| 4170 | const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4171 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4172 | assert(Proto1 && Proto2 && "Function templates must have prototypes"); |
| 4173 | TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4174 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4175 | Deduced.resize(TemplateParams->size()); |
| 4176 | |
| 4177 | // C++0x [temp.deduct.partial]p3: |
| 4178 | // The types used to determine the ordering depend on the context in which |
| 4179 | // the partial ordering is done: |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4180 | TemplateDeductionInfo Info(Loc); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4181 | SmallVector<QualType, 4> Args2; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4182 | switch (TPOC) { |
| 4183 | case TPOC_Call: { |
| 4184 | // - In the context of a function call, the function parameter types are |
| 4185 | // used. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4186 | CXXMethodDecl *Method1 = dyn_cast<CXXMethodDecl>(FD1); |
| 4187 | CXXMethodDecl *Method2 = dyn_cast<CXXMethodDecl>(FD2); |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4188 | |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4189 | // C++11 [temp.func.order]p3: |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4190 | // [...] If only one of the function templates is a non-static |
| 4191 | // member, that function template is considered to have a new |
| 4192 | // first parameter inserted in its function parameter list. The |
| 4193 | // new parameter is of type "reference to cv A," where cv are |
| 4194 | // the cv-qualifiers of the function template (if any) and A is |
| 4195 | // the class of which the function template is a member. |
| 4196 | // |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4197 | // Note that we interpret this to mean "if one of the function |
| 4198 | // templates is a non-static member and the other is a non-member"; |
| 4199 | // otherwise, the ordering rules for static functions against non-static |
| 4200 | // functions don't make any sense. |
| 4201 | // |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4202 | // C++98/03 doesn't have this provision, so instead we drop the |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4203 | // first argument of the free function, which seems to match |
| 4204 | // existing practice. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4205 | SmallVector<QualType, 4> Args1; |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4206 | |
| 4207 | unsigned Skip1 = 0, Skip2 = 0; |
| 4208 | unsigned NumComparedArguments = NumCallArguments1; |
| 4209 | |
| 4210 | if (!Method2 && Method1 && !Method1->isStatic()) { |
| 4211 | if (S.getLangOpts().CPlusPlus11) { |
| 4212 | // Compare 'this' from Method1 against first parameter from Method2. |
| 4213 | AddImplicitObjectParameterType(S.Context, Method1, Args1); |
| 4214 | ++NumComparedArguments; |
| 4215 | } else |
| 4216 | // Ignore first parameter from Method2. |
| 4217 | ++Skip2; |
| 4218 | } else if (!Method1 && Method2 && !Method2->isStatic()) { |
| 4219 | if (S.getLangOpts().CPlusPlus11) |
| 4220 | // Compare 'this' from Method2 against first parameter from Method1. |
| 4221 | AddImplicitObjectParameterType(S.Context, Method2, Args2); |
| 4222 | else |
| 4223 | // Ignore first parameter from Method1. |
| 4224 | ++Skip1; |
| 4225 | } |
| 4226 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4227 | Args1.insert(Args1.end(), Proto1->param_type_begin() + Skip1, |
| 4228 | Proto1->param_type_end()); |
| 4229 | Args2.insert(Args2.end(), Proto2->param_type_begin() + Skip2, |
| 4230 | Proto2->param_type_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4231 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4232 | // C++ [temp.func.order]p5: |
| 4233 | // The presence of unused ellipsis and default arguments has no effect on |
| 4234 | // the partial ordering of function templates. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4235 | if (Args1.size() > NumComparedArguments) |
| 4236 | Args1.resize(NumComparedArguments); |
| 4237 | if (Args2.size() > NumComparedArguments) |
| 4238 | Args2.resize(NumComparedArguments); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4239 | if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(), |
| 4240 | Args1.data(), Args1.size(), Info, Deduced, |
| 4241 | TDF_None, /*PartialOrdering=*/true, |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 4242 | RefParamComparisons)) |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4243 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4244 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4245 | break; |
| 4246 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4247 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4248 | case TPOC_Conversion: |
| 4249 | // - In the context of a call to a conversion operator, the return types |
| 4250 | // of the conversion function templates are used. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4251 | if (DeduceTemplateArgumentsByTypeMatch( |
| 4252 | S, TemplateParams, Proto2->getReturnType(), Proto1->getReturnType(), |
| 4253 | Info, Deduced, TDF_None, |
| 4254 | /*PartialOrdering=*/true, RefParamComparisons)) |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4255 | return false; |
| 4256 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4257 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4258 | case TPOC_Other: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4259 | // - 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] | 4260 | // is used. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4261 | if (DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 4262 | FD2->getType(), FD1->getType(), |
| 4263 | Info, Deduced, TDF_None, |
| 4264 | /*PartialOrdering=*/true, |
| 4265 | RefParamComparisons)) |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4266 | return false; |
| 4267 | break; |
| 4268 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4269 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4270 | // C++0x [temp.deduct.partial]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4271 | // In most cases, all template parameters must have values in order for |
| 4272 | // deduction to succeed, but for partial ordering purposes a template |
| 4273 | // 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] | 4274 | // types being used for partial ordering. [ Note: a template parameter used |
| 4275 | // in a non-deduced context is considered used. -end note] |
| 4276 | unsigned ArgIdx = 0, NumArgs = Deduced.size(); |
| 4277 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4278 | if (Deduced[ArgIdx].isNull()) |
| 4279 | break; |
| 4280 | |
| 4281 | if (ArgIdx == NumArgs) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4282 | // All template arguments were deduced. FT1 is at least as specialized |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4283 | // as FT2. |
| 4284 | return true; |
| 4285 | } |
| 4286 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4287 | // Figure out which template parameters were used. |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4288 | llvm::SmallBitVector UsedParameters(TemplateParams->size()); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4289 | switch (TPOC) { |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4290 | case TPOC_Call: |
| 4291 | for (unsigned I = 0, N = Args2.size(); I != N; ++I) |
| 4292 | ::MarkUsedTemplateParameters(S.Context, Args2[I], false, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4293 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4294 | UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4295 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4296 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4297 | case TPOC_Conversion: |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4298 | ::MarkUsedTemplateParameters(S.Context, Proto2->getReturnType(), false, |
| 4299 | TemplateParams->getDepth(), UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4300 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4301 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4302 | case TPOC_Other: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4303 | ::MarkUsedTemplateParameters(S.Context, FD2->getType(), false, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4304 | TemplateParams->getDepth(), |
| 4305 | UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4306 | break; |
| 4307 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4308 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4309 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4310 | // If this argument had no value deduced but was used in one of the types |
| 4311 | // used for partial ordering, then deduction fails. |
| 4312 | if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) |
| 4313 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4314 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4315 | return true; |
| 4316 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4317 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4318 | /// \brief Determine whether this a function template whose parameter-type-list |
| 4319 | /// ends with a function parameter pack. |
| 4320 | static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) { |
| 4321 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
| 4322 | unsigned NumParams = Function->getNumParams(); |
| 4323 | if (NumParams == 0) |
| 4324 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4325 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4326 | ParmVarDecl *Last = Function->getParamDecl(NumParams - 1); |
| 4327 | if (!Last->isParameterPack()) |
| 4328 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4329 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4330 | // Make sure that no previous parameter is a parameter pack. |
| 4331 | while (--NumParams > 0) { |
| 4332 | if (Function->getParamDecl(NumParams - 1)->isParameterPack()) |
| 4333 | return false; |
| 4334 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4335 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4336 | return true; |
| 4337 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4338 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4339 | /// \brief Returns the more specialized function template according |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4340 | /// to the rules of function template partial ordering (C++ [temp.func.order]). |
| 4341 | /// |
| 4342 | /// \param FT1 the first function template |
| 4343 | /// |
| 4344 | /// \param FT2 the second function template |
| 4345 | /// |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4346 | /// \param TPOC the context in which we are performing partial ordering of |
| 4347 | /// function templates. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4348 | /// |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4349 | /// \param NumCallArguments1 The number of arguments in the call to FT1, used |
| 4350 | /// only when \c TPOC is \c TPOC_Call. |
| 4351 | /// |
| 4352 | /// \param NumCallArguments2 The number of arguments in the call to FT2, used |
| 4353 | /// only when \c TPOC is \c TPOC_Call. |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4354 | /// |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4355 | /// \returns the more specialized function template. If neither |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4356 | /// template is more specialized, returns NULL. |
| 4357 | FunctionTemplateDecl * |
| 4358 | Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, |
| 4359 | FunctionTemplateDecl *FT2, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4360 | SourceLocation Loc, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4361 | TemplatePartialOrderingContext TPOC, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4362 | unsigned NumCallArguments1, |
| 4363 | unsigned NumCallArguments2) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4364 | SmallVector<RefParamPartialOrderingComparison, 4> RefParamComparisons; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4365 | bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 4366 | NumCallArguments1, nullptr); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4367 | bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4368 | NumCallArguments2, |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 4369 | &RefParamComparisons); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4370 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4371 | if (Better1 != Better2) // We have a clear winner |
| 4372 | return Better1? FT1 : FT2; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4373 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4374 | if (!Better1 && !Better2) // Neither is better than the other |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 4375 | return nullptr; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4376 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4377 | // C++0x [temp.deduct.partial]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4378 | // If for each type being considered a given template is at least as |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4379 | // specialized for all types and more specialized for some set of types and |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4380 | // the other template is not more specialized for any types or is not at |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4381 | // least as specialized for any types, then the given template is more |
| 4382 | // specialized than the other template. Otherwise, neither template is more |
| 4383 | // specialized than the other. |
| 4384 | Better1 = false; |
| 4385 | Better2 = false; |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 4386 | for (unsigned I = 0, N = RefParamComparisons.size(); I != N; ++I) { |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4387 | // C++0x [temp.deduct.partial]p9: |
| 4388 | // If, for a given type, deduction succeeds in both directions (i.e., the |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 4389 | // types are identical after the transformations above) and both P and A |
| 4390 | // were reference types (before being replaced with the type referred to |
| 4391 | // above): |
| 4392 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4393 | // -- if the type from the argument template was an lvalue reference |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 4394 | // and the type from the parameter template was not, the argument |
| 4395 | // type is considered to be more specialized than the other; |
| 4396 | // otherwise, |
| 4397 | if (!RefParamComparisons[I].ArgIsRvalueRef && |
| 4398 | RefParamComparisons[I].ParamIsRvalueRef) { |
| 4399 | Better2 = true; |
| 4400 | if (Better1) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 4401 | return nullptr; |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 4402 | continue; |
| 4403 | } else if (!RefParamComparisons[I].ParamIsRvalueRef && |
| 4404 | RefParamComparisons[I].ArgIsRvalueRef) { |
| 4405 | Better1 = true; |
| 4406 | if (Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 4407 | return nullptr; |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 4408 | continue; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4409 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4410 | |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 4411 | // -- if the type from the argument template is more cv-qualified than |
| 4412 | // the type from the parameter template (as described above), the |
| 4413 | // argument type is considered to be more specialized than the |
| 4414 | // other; otherwise, |
| 4415 | switch (RefParamComparisons[I].Qualifiers) { |
| 4416 | case NeitherMoreQualified: |
| 4417 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4418 | |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 4419 | case ParamMoreQualified: |
| 4420 | Better1 = true; |
| 4421 | if (Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 4422 | return nullptr; |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 4423 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4424 | |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 4425 | case ArgMoreQualified: |
| 4426 | Better2 = true; |
| 4427 | if (Better1) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 4428 | return nullptr; |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 4429 | continue; |
| 4430 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4431 | |
Douglas Gregor | 6381402 | 2011-01-21 17:29:42 +0000 | [diff] [blame] | 4432 | // -- neither type is more specialized than the other. |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4433 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4434 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4435 | assert(!(Better1 && Better2) && "Should have broken out in the loop above"); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4436 | if (Better1) |
| 4437 | return FT1; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4438 | else if (Better2) |
| 4439 | return FT2; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4440 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4441 | // FIXME: This mimics what GCC implements, but doesn't match up with the |
| 4442 | // proposed resolution for core issue 692. This area needs to be sorted out, |
| 4443 | // but for now we attempt to maintain compatibility. |
| 4444 | bool Variadic1 = isVariadicFunctionTemplate(FT1); |
| 4445 | bool Variadic2 = isVariadicFunctionTemplate(FT2); |
| 4446 | if (Variadic1 != Variadic2) |
| 4447 | return Variadic1? FT2 : FT1; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4448 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 4449 | return nullptr; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4450 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4451 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4452 | /// \brief Determine if the two templates are equivalent. |
| 4453 | static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { |
| 4454 | if (T1 == T2) |
| 4455 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4456 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4457 | if (!T1 || !T2) |
| 4458 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4459 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4460 | return T1->getCanonicalDecl() == T2->getCanonicalDecl(); |
| 4461 | } |
| 4462 | |
| 4463 | /// \brief Retrieve the most specialized of the given function template |
| 4464 | /// specializations. |
| 4465 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4466 | /// \param SpecBegin the start iterator of the function template |
| 4467 | /// specializations that we will be comparing. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4468 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4469 | /// \param SpecEnd the end iterator of the function template |
| 4470 | /// specializations, paired with \p SpecBegin. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4471 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4472 | /// \param Loc the location where the ambiguity or no-specializations |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4473 | /// diagnostic should occur. |
| 4474 | /// |
| 4475 | /// \param NoneDiag partial diagnostic used to diagnose cases where there are |
| 4476 | /// no matching candidates. |
| 4477 | /// |
| 4478 | /// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one |
| 4479 | /// occurs. |
| 4480 | /// |
| 4481 | /// \param CandidateDiag partial diagnostic used for each function template |
| 4482 | /// specialization that is a candidate in the ambiguous ordering. One parameter |
| 4483 | /// in this diagnostic should be unbound, which will correspond to the string |
| 4484 | /// describing the template arguments for the function template specialization. |
| 4485 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4486 | /// \returns the most specialized function template specialization, if |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4487 | /// found. Otherwise, returns SpecEnd. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4488 | UnresolvedSetIterator Sema::getMostSpecialized( |
| 4489 | UnresolvedSetIterator SpecBegin, UnresolvedSetIterator SpecEnd, |
| 4490 | TemplateSpecCandidateSet &FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4491 | SourceLocation Loc, const PartialDiagnostic &NoneDiag, |
| 4492 | const PartialDiagnostic &AmbigDiag, const PartialDiagnostic &CandidateDiag, |
| 4493 | bool Complain, QualType TargetType) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4494 | if (SpecBegin == SpecEnd) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4495 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4496 | Diag(Loc, NoneDiag); |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4497 | FailedCandidates.NoteCandidates(*this, Loc); |
| 4498 | } |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4499 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4500 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4501 | |
| 4502 | if (SpecBegin + 1 == SpecEnd) |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4503 | return SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4504 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4505 | // Find the function template that is better than all of the templates it |
| 4506 | // has been compared to. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4507 | UnresolvedSetIterator Best = SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4508 | FunctionTemplateDecl *BestTemplate |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4509 | = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4510 | assert(BestTemplate && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4511 | for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { |
| 4512 | FunctionTemplateDecl *Challenger |
| 4513 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4514 | assert(Challenger && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4515 | if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4516 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4517 | Challenger)) { |
| 4518 | Best = I; |
| 4519 | BestTemplate = Challenger; |
| 4520 | } |
| 4521 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4522 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4523 | // Make sure that the "best" function template is more specialized than all |
| 4524 | // of the others. |
| 4525 | bool Ambiguous = false; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4526 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4527 | FunctionTemplateDecl *Challenger |
| 4528 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4529 | if (I != Best && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4530 | !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4531 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4532 | BestTemplate)) { |
| 4533 | Ambiguous = true; |
| 4534 | break; |
| 4535 | } |
| 4536 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4537 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4538 | if (!Ambiguous) { |
| 4539 | // We found an answer. Return it. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4540 | return Best; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4541 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4542 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4543 | // Diagnose the ambiguity. |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4544 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4545 | Diag(Loc, AmbigDiag); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4546 | |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4547 | // FIXME: Can we order the candidates in some sane way? |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4548 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4549 | PartialDiagnostic PD = CandidateDiag; |
| 4550 | PD << getTemplateArgumentBindingsText( |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4551 | cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(), |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4552 | *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs()); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4553 | if (!TargetType.isNull()) |
| 4554 | HandleFunctionTypeMismatch(PD, cast<FunctionDecl>(*I)->getType(), |
| 4555 | TargetType); |
| 4556 | Diag((*I)->getLocation(), PD); |
| 4557 | } |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4558 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4559 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4560 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4561 | } |
| 4562 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4563 | /// \brief Returns the more specialized class template partial specialization |
| 4564 | /// according to the rules of partial ordering of class template partial |
| 4565 | /// specializations (C++ [temp.class.order]). |
| 4566 | /// |
| 4567 | /// \param PS1 the first class template partial specialization |
| 4568 | /// |
| 4569 | /// \param PS2 the second class template partial specialization |
| 4570 | /// |
| 4571 | /// \returns the more specialized class template partial specialization. If |
| 4572 | /// neither partial specialization is more specialized, returns NULL. |
| 4573 | ClassTemplatePartialSpecializationDecl * |
| 4574 | Sema::getMoreSpecializedPartialSpecialization( |
| 4575 | ClassTemplatePartialSpecializationDecl *PS1, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4576 | ClassTemplatePartialSpecializationDecl *PS2, |
| 4577 | SourceLocation Loc) { |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4578 | // C++ [temp.class.order]p1: |
| 4579 | // For two class template partial specializations, the first is at least as |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4580 | // specialized as the second if, given the following rewrite to two |
| 4581 | // function templates, the first function template is at least as |
| 4582 | // specialized as the second according to the ordering rules for function |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4583 | // templates (14.6.6.2): |
| 4584 | // - the first function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4585 | // first partial specialization and has a single function parameter |
| 4586 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4587 | // arguments of the first partial specialization, and |
| 4588 | // - the second function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4589 | // second partial specialization and has a single function parameter |
| 4590 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4591 | // arguments of the second partial specialization. |
| 4592 | // |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 4593 | // Rather than synthesize function templates, we merely perform the |
| 4594 | // equivalent partial ordering by performing deduction directly on |
| 4595 | // the template arguments of the class template partial |
| 4596 | // specializations. This computation is slightly simpler than the |
| 4597 | // general problem of function template partial ordering, because |
| 4598 | // class template partial specializations are more constrained. We |
| 4599 | // know that every template parameter is deducible from the class |
| 4600 | // template partial specialization's template arguments, for |
| 4601 | // example. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4602 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4603 | TemplateDeductionInfo Info(Loc); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4604 | |
| 4605 | QualType PT1 = PS1->getInjectedSpecializationType(); |
| 4606 | QualType PT2 = PS2->getInjectedSpecializationType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4607 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4608 | // Determine whether PS1 is at least as specialized as PS2 |
| 4609 | Deduced.resize(PS2->getTemplateParameters()->size()); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4610 | bool Better1 = !DeduceTemplateArgumentsByTypeMatch(*this, |
| 4611 | PS2->getTemplateParameters(), |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4612 | PT2, PT1, Info, Deduced, TDF_None, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4613 | /*PartialOrdering=*/true, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 4614 | /*RefParamComparisons=*/nullptr); |
Argyrios Kyrtzidis | 7d39155 | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4615 | if (Better1) { |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4616 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(),Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 4617 | InstantiatingTemplate Inst(*this, Loc, PS2, DeducedArgs, Info); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4618 | Better1 = !::FinishTemplateArgumentDeduction( |
| 4619 | *this, PS2, PS1->getTemplateArgs(), Deduced, Info); |
| 4620 | } |
| 4621 | |
| 4622 | // Determine whether PS2 is at least as specialized as PS1 |
| 4623 | Deduced.clear(); |
| 4624 | Deduced.resize(PS1->getTemplateParameters()->size()); |
| 4625 | bool Better2 = !DeduceTemplateArgumentsByTypeMatch( |
| 4626 | *this, PS1->getTemplateParameters(), PT1, PT2, Info, Deduced, TDF_None, |
| 4627 | /*PartialOrdering=*/true, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 4628 | /*RefParamComparisons=*/nullptr); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4629 | if (Better2) { |
| 4630 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
| 4631 | Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 4632 | InstantiatingTemplate Inst(*this, Loc, PS1, DeducedArgs, Info); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4633 | Better2 = !::FinishTemplateArgumentDeduction( |
| 4634 | *this, PS1, PS2->getTemplateArgs(), Deduced, Info); |
| 4635 | } |
| 4636 | |
| 4637 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 4638 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4639 | |
| 4640 | return Better1 ? PS1 : PS2; |
| 4641 | } |
| 4642 | |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 4643 | /// TODO: Unify with ClassTemplatePartialSpecializationDecl version? |
| 4644 | /// May require unifying ClassTemplate(Partial)SpecializationDecl and |
| 4645 | /// VarTemplate(Partial)SpecializationDecl with a new data |
| 4646 | /// structure Template(Partial)SpecializationDecl, and |
| 4647 | /// using Template(Partial)SpecializationDecl as input type. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4648 | VarTemplatePartialSpecializationDecl * |
| 4649 | Sema::getMoreSpecializedPartialSpecialization( |
| 4650 | VarTemplatePartialSpecializationDecl *PS1, |
| 4651 | VarTemplatePartialSpecializationDecl *PS2, SourceLocation Loc) { |
| 4652 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 4653 | TemplateDeductionInfo Info(Loc); |
| 4654 | |
Richard Smith | f04fd0b | 2013-12-12 23:14:16 +0000 | [diff] [blame] | 4655 | assert(PS1->getSpecializedTemplate() == PS2->getSpecializedTemplate() && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4656 | "the partial specializations being compared should specialize" |
| 4657 | " the same template."); |
| 4658 | TemplateName Name(PS1->getSpecializedTemplate()); |
| 4659 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4660 | QualType PT1 = Context.getTemplateSpecializationType( |
| 4661 | CanonTemplate, PS1->getTemplateArgs().data(), |
| 4662 | PS1->getTemplateArgs().size()); |
| 4663 | QualType PT2 = Context.getTemplateSpecializationType( |
| 4664 | CanonTemplate, PS2->getTemplateArgs().data(), |
| 4665 | PS2->getTemplateArgs().size()); |
| 4666 | |
| 4667 | // Determine whether PS1 is at least as specialized as PS2 |
| 4668 | Deduced.resize(PS2->getTemplateParameters()->size()); |
| 4669 | bool Better1 = !DeduceTemplateArgumentsByTypeMatch( |
| 4670 | *this, PS2->getTemplateParameters(), PT2, PT1, Info, Deduced, TDF_None, |
| 4671 | /*PartialOrdering=*/true, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 4672 | /*RefParamComparisons=*/nullptr); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4673 | if (Better1) { |
| 4674 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
| 4675 | Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 4676 | InstantiatingTemplate Inst(*this, Loc, PS2, DeducedArgs, Info); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4677 | Better1 = !::FinishTemplateArgumentDeduction(*this, PS2, |
| 4678 | PS1->getTemplateArgs(), |
Douglas Gregor | 9225b02 | 2010-04-29 06:31:36 +0000 | [diff] [blame] | 4679 | Deduced, Info); |
Argyrios Kyrtzidis | 7d39155 | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4680 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4681 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4682 | // Determine whether PS2 is at least as specialized as PS1 |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 4683 | Deduced.clear(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4684 | Deduced.resize(PS1->getTemplateParameters()->size()); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4685 | bool Better2 = !DeduceTemplateArgumentsByTypeMatch(*this, |
| 4686 | PS1->getTemplateParameters(), |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4687 | PT1, PT2, Info, Deduced, TDF_None, |
| 4688 | /*PartialOrdering=*/true, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 4689 | /*RefParamComparisons=*/nullptr); |
Argyrios Kyrtzidis | 7d39155 | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4690 | if (Better2) { |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4691 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(),Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 4692 | InstantiatingTemplate Inst(*this, Loc, PS1, DeducedArgs, Info); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4693 | Better2 = !::FinishTemplateArgumentDeduction(*this, PS1, |
| 4694 | PS2->getTemplateArgs(), |
Douglas Gregor | 9225b02 | 2010-04-29 06:31:36 +0000 | [diff] [blame] | 4695 | Deduced, Info); |
Argyrios Kyrtzidis | 7d39155 | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4696 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4697 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4698 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame^] | 4699 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4700 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4701 | return Better1? PS1 : PS2; |
| 4702 | } |
| 4703 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4704 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4705 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4706 | const TemplateArgument &TemplateArg, |
| 4707 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4708 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4709 | llvm::SmallBitVector &Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +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 |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4712 | /// expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 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 | const Expr *E, |
| 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 | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 4719 | // We can deduce from a pack expansion. |
| 4720 | if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E)) |
| 4721 | E = Expansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4722 | |
Richard Smith | 3434900 | 2012-07-09 03:07:20 +0000 | [diff] [blame] | 4723 | // Skip through any implicit casts we added while type-checking, and any |
| 4724 | // substitutions performed by template alias expansion. |
| 4725 | while (1) { |
| 4726 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 4727 | E = ICE->getSubExpr(); |
| 4728 | else if (const SubstNonTypeTemplateParmExpr *Subst = |
| 4729 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 4730 | E = Subst->getReplacement(); |
| 4731 | else |
| 4732 | break; |
| 4733 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4734 | |
| 4735 | // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4736 | // find other occurrences of template parameters. |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4737 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
Douglas Gregor | 04b1152 | 2010-01-14 18:13:22 +0000 | [diff] [blame] | 4738 | if (!DRE) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4739 | return; |
| 4740 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4741 | const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4742 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 4743 | if (!NTTP) |
| 4744 | return; |
| 4745 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4746 | if (NTTP->getDepth() == Depth) |
| 4747 | Used[NTTP->getIndex()] = true; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4748 | } |
| 4749 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4750 | /// \brief Mark the template parameters that are used by the given |
| 4751 | /// nested name specifier. |
| 4752 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4753 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4754 | NestedNameSpecifier *NNS, |
| 4755 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4756 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4757 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4758 | if (!NNS) |
| 4759 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4760 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4761 | MarkUsedTemplateParameters(Ctx, NNS->getPrefix(), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4762 | Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4763 | MarkUsedTemplateParameters(Ctx, QualType(NNS->getAsType(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4764 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4765 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4766 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4767 | /// \brief Mark the template parameters that are used by the given |
| 4768 | /// template name. |
| 4769 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4770 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4771 | TemplateName Name, |
| 4772 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4773 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4774 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4775 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 4776 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4777 | = dyn_cast<TemplateTemplateParmDecl>(Template)) { |
| 4778 | if (TTP->getDepth() == Depth) |
| 4779 | Used[TTP->getIndex()] = true; |
| 4780 | } |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4781 | return; |
| 4782 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4783 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4784 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4785 | MarkUsedTemplateParameters(Ctx, QTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4786 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4787 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4788 | MarkUsedTemplateParameters(Ctx, DTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4789 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
| 4792 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4793 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4794 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4795 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4796 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4797 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4798 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4799 | if (T.isNull()) |
| 4800 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4801 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4802 | // Non-dependent types have nothing deducible |
| 4803 | if (!T->isDependentType()) |
| 4804 | return; |
| 4805 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4806 | T = Ctx.getCanonicalType(T); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4807 | switch (T->getTypeClass()) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4808 | case Type::Pointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4809 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4810 | cast<PointerType>(T)->getPointeeType(), |
| 4811 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4812 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4813 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4814 | break; |
| 4815 | |
| 4816 | case Type::BlockPointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4817 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4818 | cast<BlockPointerType>(T)->getPointeeType(), |
| 4819 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4820 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4821 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4822 | break; |
| 4823 | |
| 4824 | case Type::LValueReference: |
| 4825 | case Type::RValueReference: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4826 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4827 | cast<ReferenceType>(T)->getPointeeType(), |
| 4828 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4829 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4830 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4831 | break; |
| 4832 | |
| 4833 | case Type::MemberPointer: { |
| 4834 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4835 | MarkUsedTemplateParameters(Ctx, MemPtr->getPointeeType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4836 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4837 | MarkUsedTemplateParameters(Ctx, QualType(MemPtr->getClass(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4838 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4839 | break; |
| 4840 | } |
| 4841 | |
| 4842 | case Type::DependentSizedArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4843 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4844 | cast<DependentSizedArrayType>(T)->getSizeExpr(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4845 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4846 | // Fall through to check the element type |
| 4847 | |
| 4848 | case Type::ConstantArray: |
| 4849 | case Type::IncompleteArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4850 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4851 | cast<ArrayType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4852 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4853 | break; |
| 4854 | |
| 4855 | case Type::Vector: |
| 4856 | case Type::ExtVector: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4857 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4858 | cast<VectorType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4859 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4860 | break; |
| 4861 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4862 | case Type::DependentSizedExtVector: { |
| 4863 | const DependentSizedExtVectorType *VecType |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4864 | = cast<DependentSizedExtVectorType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4865 | MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4866 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4867 | MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4868 | Depth, Used); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4869 | break; |
| 4870 | } |
| 4871 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4872 | case Type::FunctionProto: { |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4873 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4874 | MarkUsedTemplateParameters(Ctx, Proto->getReturnType(), OnlyDeduced, Depth, |
| 4875 | Used); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4876 | for (unsigned I = 0, N = Proto->getNumParams(); I != N; ++I) |
| 4877 | MarkUsedTemplateParameters(Ctx, Proto->getParamType(I), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4878 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4879 | break; |
| 4880 | } |
| 4881 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4882 | case Type::TemplateTypeParm: { |
| 4883 | const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); |
| 4884 | if (TTP->getDepth() == Depth) |
| 4885 | Used[TTP->getIndex()] = true; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4886 | break; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4887 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4888 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4889 | case Type::SubstTemplateTypeParmPack: { |
| 4890 | const SubstTemplateTypeParmPackType *Subst |
| 4891 | = cast<SubstTemplateTypeParmPackType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4892 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4893 | QualType(Subst->getReplacedParameter(), 0), |
| 4894 | OnlyDeduced, Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4895 | MarkUsedTemplateParameters(Ctx, Subst->getArgumentPack(), |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4896 | OnlyDeduced, Depth, Used); |
| 4897 | break; |
| 4898 | } |
| 4899 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4900 | case Type::InjectedClassName: |
| 4901 | T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); |
| 4902 | // fall through |
| 4903 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4904 | case Type::TemplateSpecialization: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4905 | const TemplateSpecializationType *Spec |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4906 | = cast<TemplateSpecializationType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4907 | MarkUsedTemplateParameters(Ctx, Spec->getTemplateName(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4908 | Depth, Used); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4909 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4910 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4911 | // If the template argument list of P contains a pack expansion that is not |
| 4912 | // the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4913 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4914 | if (OnlyDeduced && |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4915 | hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs())) |
| 4916 | break; |
| 4917 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4918 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4919 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4920 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4921 | break; |
| 4922 | } |
| 4923 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4924 | case Type::Complex: |
| 4925 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4926 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4927 | cast<ComplexType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4928 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4929 | break; |
| 4930 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4931 | case Type::Atomic: |
| 4932 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4933 | MarkUsedTemplateParameters(Ctx, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4934 | cast<AtomicType>(T)->getValueType(), |
| 4935 | OnlyDeduced, Depth, Used); |
| 4936 | break; |
| 4937 | |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4938 | case Type::DependentName: |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4939 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4940 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4941 | cast<DependentNameType>(T)->getQualifier(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4942 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4943 | break; |
| 4944 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4945 | case Type::DependentTemplateSpecialization: { |
| 4946 | const DependentTemplateSpecializationType *Spec |
| 4947 | = cast<DependentTemplateSpecializationType>(T); |
| 4948 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4949 | MarkUsedTemplateParameters(Ctx, Spec->getQualifier(), |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4950 | OnlyDeduced, Depth, Used); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4951 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4952 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4953 | // If the template argument list of P contains a pack expansion that is not |
| 4954 | // the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4955 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4956 | if (OnlyDeduced && |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4957 | hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs())) |
| 4958 | break; |
| 4959 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4960 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4961 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4962 | Used); |
| 4963 | break; |
| 4964 | } |
| 4965 | |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4966 | case Type::TypeOf: |
| 4967 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4968 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4969 | cast<TypeOfType>(T)->getUnderlyingType(), |
| 4970 | OnlyDeduced, Depth, Used); |
| 4971 | break; |
| 4972 | |
| 4973 | case Type::TypeOfExpr: |
| 4974 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4975 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4976 | cast<TypeOfExprType>(T)->getUnderlyingExpr(), |
| 4977 | OnlyDeduced, Depth, Used); |
| 4978 | break; |
| 4979 | |
| 4980 | case Type::Decltype: |
| 4981 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4982 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4983 | cast<DecltypeType>(T)->getUnderlyingExpr(), |
| 4984 | OnlyDeduced, Depth, Used); |
| 4985 | break; |
| 4986 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4987 | case Type::UnaryTransform: |
| 4988 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4989 | MarkUsedTemplateParameters(Ctx, |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4990 | cast<UnaryTransformType>(T)->getUnderlyingType(), |
| 4991 | OnlyDeduced, Depth, Used); |
| 4992 | break; |
| 4993 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4994 | case Type::PackExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4995 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4996 | cast<PackExpansionType>(T)->getPattern(), |
| 4997 | OnlyDeduced, Depth, Used); |
| 4998 | break; |
| 4999 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5000 | case Type::Auto: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5001 | MarkUsedTemplateParameters(Ctx, |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5002 | cast<AutoType>(T)->getDeducedType(), |
| 5003 | OnlyDeduced, Depth, Used); |
| 5004 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5005 | // None of these types have any template parameters in them. |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5006 | case Type::Builtin: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5007 | case Type::VariableArray: |
| 5008 | case Type::FunctionNoProto: |
| 5009 | case Type::Record: |
| 5010 | case Type::Enum: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5011 | case Type::ObjCInterface: |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5012 | case Type::ObjCObject: |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 5013 | case Type::ObjCObjectPointer: |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5014 | case Type::UnresolvedUsing: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5015 | #define TYPE(Class, Base) |
| 5016 | #define ABSTRACT_TYPE(Class, Base) |
| 5017 | #define DEPENDENT_TYPE(Class, Base) |
| 5018 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 5019 | #include "clang/AST/TypeNodes.def" |
| 5020 | break; |
| 5021 | } |
| 5022 | } |
| 5023 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5024 | /// \brief Mark the template parameters that are used by this |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5025 | /// template argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5026 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5027 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5028 | const TemplateArgument &TemplateArg, |
| 5029 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5030 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5031 | llvm::SmallBitVector &Used) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5032 | switch (TemplateArg.getKind()) { |
| 5033 | case TemplateArgument::Null: |
| 5034 | case TemplateArgument::Integral: |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5035 | case TemplateArgument::Declaration: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5036 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5037 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5038 | case TemplateArgument::NullPtr: |
| 5039 | MarkUsedTemplateParameters(Ctx, TemplateArg.getNullPtrType(), OnlyDeduced, |
| 5040 | Depth, Used); |
| 5041 | break; |
| 5042 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5043 | case TemplateArgument::Type: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5044 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5045 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5046 | break; |
| 5047 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5048 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 5049 | case TemplateArgument::TemplateExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5050 | MarkUsedTemplateParameters(Ctx, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5051 | TemplateArg.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5052 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5053 | break; |
| 5054 | |
| 5055 | case TemplateArgument::Expression: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5056 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5057 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5058 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5059 | |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5060 | case TemplateArgument::Pack: |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5061 | for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(), |
| 5062 | PEnd = TemplateArg.pack_end(); |
| 5063 | P != PEnd; ++P) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5064 | MarkUsedTemplateParameters(Ctx, *P, OnlyDeduced, Depth, Used); |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5065 | break; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5066 | } |
| 5067 | } |
| 5068 | |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5069 | /// \brief Mark which template parameters can be deduced from a given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5070 | /// template argument list. |
| 5071 | /// |
| 5072 | /// \param TemplateArgs the template argument list from which template |
| 5073 | /// parameters will be deduced. |
| 5074 | /// |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5075 | /// \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] | 5076 | /// to indicate when the corresponding template parameter will be |
| 5077 | /// deduced. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5078 | void |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5079 | Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5080 | bool OnlyDeduced, unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5081 | llvm::SmallBitVector &Used) { |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5082 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5083 | // If the template argument list of P contains a pack expansion that is not |
| 5084 | // the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5085 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5086 | if (OnlyDeduced && |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5087 | hasPackExpansionBeforeEnd(TemplateArgs.data(), TemplateArgs.size())) |
| 5088 | return; |
| 5089 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5090 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5091 | ::MarkUsedTemplateParameters(Context, TemplateArgs[I], OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5092 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5093 | } |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5094 | |
| 5095 | /// \brief Marks all of the template parameters that will be deduced by a |
| 5096 | /// call to the given function template. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5097 | void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5098 | Sema::MarkDeducedTemplateParameters(ASTContext &Ctx, |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 5099 | const FunctionTemplateDecl *FunctionTemplate, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5100 | llvm::SmallBitVector &Deduced) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5101 | TemplateParameterList *TemplateParams |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5102 | = FunctionTemplate->getTemplateParameters(); |
| 5103 | Deduced.clear(); |
| 5104 | Deduced.resize(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5105 | |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5106 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 5107 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5108 | ::MarkUsedTemplateParameters(Ctx, Function->getParamDecl(I)->getType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5109 | true, TemplateParams->getDepth(), Deduced); |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5110 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5111 | |
| 5112 | bool hasDeducibleTemplateParameters(Sema &S, |
| 5113 | FunctionTemplateDecl *FunctionTemplate, |
| 5114 | QualType T) { |
| 5115 | if (!T->isDependentType()) |
| 5116 | return false; |
| 5117 | |
| 5118 | TemplateParameterList *TemplateParams |
| 5119 | = FunctionTemplate->getTemplateParameters(); |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5120 | llvm::SmallBitVector Deduced(TemplateParams->size()); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5121 | ::MarkUsedTemplateParameters(S.Context, T, true, TemplateParams->getDepth(), |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5122 | Deduced); |
| 5123 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5124 | return Deduced.any(); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5125 | } |