blob: 9011cdf7536ffe50f6c94fbe76add422a0edcb3a [file] [log] [blame]
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001//===------- 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
Douglas Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Sema.h"
John McCall19510852010-08-20 18:27:03 +000014#include "clang/Sema/DeclSpec.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
John McCall2a7fb272010-08-25 05:32:35 +000016#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor0b9247f2009-06-04 00:03:07 +000017#include "clang/AST/ASTContext.h"
John McCall7cd088e2010-08-24 07:21:54 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor0b9247f2009-06-04 00:03:07 +000019#include "clang/AST/DeclTemplate.h"
20#include "clang/AST/StmtVisitor.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/ExprCXX.h"
Douglas Gregore02e2622010-12-22 21:19:48 +000023#include "llvm/ADT/BitVector.h"
Richard Smith34b41d92011-02-20 03:19:35 +000024#include "TreeTransform.h"
Douglas Gregor8a514912009-09-14 18:39:43 +000025#include <algorithm>
Douglas Gregor508f1c82009-06-26 23:10:12 +000026
27namespace clang {
John McCall2a7fb272010-08-25 05:32:35 +000028 using namespace sema;
29
Douglas Gregor508f1c82009-06-26 23:10:12 +000030 /// \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 Gregor41128772009-06-26 23:27:24 +000047 /// deduction from a template-id of a derived class of the argument type.
Douglas Gregor12820292009-09-14 20:00:47 +000048 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 Gregor73b3cf62011-01-25 17:19:08 +000052 TDF_SkipNonDependent = 0x08,
53 /// \brief Whether we are performing template argument deduction for
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000054 /// parameters and arguments in a top-level template argument
Douglas Gregor73b3cf62011-01-25 17:19:08 +000055 TDF_TopLevelParameterTypeList = 0x10
Douglas Gregor508f1c82009-06-26 23:10:12 +000056 };
57}
58
Douglas Gregor0b9247f2009-06-04 00:03:07 +000059using namespace clang;
60
Douglas Gregor9d0e4412010-03-26 05:50:28 +000061/// \brief Compare two APSInts, extending and switching the sign as
62/// necessary to compare their values regardless of underlying type.
63static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) {
64 if (Y.getBitWidth() > X.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +000065 X = X.extend(Y.getBitWidth());
Douglas Gregor9d0e4412010-03-26 05:50:28 +000066 else if (Y.getBitWidth() < X.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +000067 Y = Y.extend(X.getBitWidth());
Douglas Gregor9d0e4412010-03-26 05:50:28 +000068
69 // If there is a signedness mismatch, correct it.
70 if (X.isSigned() != Y.isSigned()) {
71 // If the signed value is negative, then the values cannot be the same.
72 if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative()))
73 return false;
74
75 Y.setIsSigned(true);
76 X.setIsSigned(true);
77 }
78
79 return X == Y;
80}
81
Douglas Gregorf67875d2009-06-12 18:26:56 +000082static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +000083DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +000084 TemplateParameterList *TemplateParams,
85 const TemplateArgument &Param,
Douglas Gregor77d6bb92011-01-11 22:21:24 +000086 TemplateArgument Arg,
John McCall2a7fb272010-08-25 05:32:35 +000087 TemplateDeductionInfo &Info,
Douglas Gregor0972c862010-12-22 18:55:49 +000088 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced);
Douglas Gregord708c722009-06-09 16:35:58 +000089
Douglas Gregorb939a192011-01-21 17:29:42 +000090/// \brief Whether template argument deduction for two reference parameters
91/// resulted in the argument type, parameter type, or neither type being more
92/// qualified than the other.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000093enum DeductionQualifierComparison {
94 NeitherMoreQualified = 0,
95 ParamMoreQualified,
96 ArgMoreQualified
Douglas Gregor5c7bf422011-01-11 17:34:58 +000097};
98
Douglas Gregorb939a192011-01-21 17:29:42 +000099/// \brief Stores the result of comparing two reference parameters while
100/// performing template argument deduction for partial ordering of function
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000101/// templates.
Douglas Gregorb939a192011-01-21 17:29:42 +0000102struct RefParamPartialOrderingComparison {
103 /// \brief Whether the parameter type is an rvalue reference type.
104 bool ParamIsRvalueRef;
105 /// \brief Whether the argument type is an rvalue reference type.
106 bool ArgIsRvalueRef;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000107
Douglas Gregorb939a192011-01-21 17:29:42 +0000108 /// \brief Whether the parameter or argument (or neither) is more qualified.
109 DeductionQualifierComparison Qualifiers;
110};
111
112
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000113
Douglas Gregor20a55e22010-12-22 18:17:10 +0000114static Sema::TemplateDeductionResult
115DeduceTemplateArguments(Sema &S,
116 TemplateParameterList *TemplateParams,
Douglas Gregor603cfb42011-01-05 23:12:31 +0000117 QualType Param,
118 QualType Arg,
119 TemplateDeductionInfo &Info,
120 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000121 unsigned TDF,
122 bool PartialOrdering = false,
Douglas Gregorb939a192011-01-21 17:29:42 +0000123 llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *
124 RefParamComparisons = 0);
Douglas Gregor603cfb42011-01-05 23:12:31 +0000125
126static Sema::TemplateDeductionResult
127DeduceTemplateArguments(Sema &S,
128 TemplateParameterList *TemplateParams,
Douglas Gregor20a55e22010-12-22 18:17:10 +0000129 const TemplateArgument *Params, unsigned NumParams,
130 const TemplateArgument *Args, unsigned NumArgs,
131 TemplateDeductionInfo &Info,
Douglas Gregor0972c862010-12-22 18:55:49 +0000132 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
133 bool NumberOfArgumentsMustMatch = true);
Douglas Gregor20a55e22010-12-22 18:17:10 +0000134
Douglas Gregor199d9912009-06-05 00:53:49 +0000135/// \brief If the given expression is of a form that permits the deduction
136/// of a non-type template parameter, return the declaration of that
137/// non-type template parameter.
138static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
139 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
140 E = IC->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Douglas Gregor199d9912009-06-05 00:53:49 +0000142 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
143 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Douglas Gregor199d9912009-06-05 00:53:49 +0000145 return 0;
146}
147
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000148/// \brief Determine whether two declaration pointers refer to the same
149/// declaration.
150static bool isSameDeclaration(Decl *X, Decl *Y) {
151 if (!X || !Y)
152 return !X && !Y;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000153
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000154 if (NamedDecl *NX = dyn_cast<NamedDecl>(X))
155 X = NX->getUnderlyingDecl();
156 if (NamedDecl *NY = dyn_cast<NamedDecl>(Y))
157 Y = NY->getUnderlyingDecl();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000158
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000159 return X->getCanonicalDecl() == Y->getCanonicalDecl();
160}
161
162/// \brief Verify that the given, deduced template arguments are compatible.
163///
164/// \returns The deduced template argument, or a NULL template argument if
165/// the deduced template arguments were incompatible.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000166static DeducedTemplateArgument
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000167checkDeducedTemplateArguments(ASTContext &Context,
168 const DeducedTemplateArgument &X,
169 const DeducedTemplateArgument &Y) {
170 // We have no deduction for one or both of the arguments; they're compatible.
171 if (X.isNull())
172 return Y;
173 if (Y.isNull())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000174 return X;
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000175
176 switch (X.getKind()) {
177 case TemplateArgument::Null:
178 llvm_unreachable("Non-deduced template arguments handled above");
179
180 case TemplateArgument::Type:
181 // If two template type arguments have the same type, they're compatible.
182 if (Y.getKind() == TemplateArgument::Type &&
183 Context.hasSameType(X.getAsType(), Y.getAsType()))
184 return X;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000185
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000186 return DeducedTemplateArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000187
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000188 case TemplateArgument::Integral:
189 // If we deduced a constant in one case and either a dependent expression or
190 // declaration in another case, keep the integral constant.
191 // If both are integral constants with the same value, keep that value.
192 if (Y.getKind() == TemplateArgument::Expression ||
193 Y.getKind() == TemplateArgument::Declaration ||
194 (Y.getKind() == TemplateArgument::Integral &&
195 hasSameExtendedValue(*X.getAsIntegral(), *Y.getAsIntegral())))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000196 return DeducedTemplateArgument(X,
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000197 X.wasDeducedFromArrayBound() &&
198 Y.wasDeducedFromArrayBound());
199
200 // All other combinations are incompatible.
201 return DeducedTemplateArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000202
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000203 case TemplateArgument::Template:
204 if (Y.getKind() == TemplateArgument::Template &&
205 Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate()))
206 return X;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000207
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000208 // All other combinations are incompatible.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000209 return DeducedTemplateArgument();
Douglas Gregora7fc9012011-01-05 18:58:31 +0000210
211 case TemplateArgument::TemplateExpansion:
212 if (Y.getKind() == TemplateArgument::TemplateExpansion &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000213 Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(),
Douglas Gregora7fc9012011-01-05 18:58:31 +0000214 Y.getAsTemplateOrTemplatePattern()))
215 return X;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000216
Douglas Gregora7fc9012011-01-05 18:58:31 +0000217 // All other combinations are incompatible.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000218 return DeducedTemplateArgument();
Douglas Gregora7fc9012011-01-05 18:58:31 +0000219
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000220 case TemplateArgument::Expression:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000221 // If we deduced a dependent expression in one case and either an integral
222 // constant or a declaration in another case, keep the integral constant
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000223 // or declaration.
224 if (Y.getKind() == TemplateArgument::Integral ||
225 Y.getKind() == TemplateArgument::Declaration)
226 return DeducedTemplateArgument(Y, X.wasDeducedFromArrayBound() &&
227 Y.wasDeducedFromArrayBound());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000228
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000229 if (Y.getKind() == TemplateArgument::Expression) {
230 // Compare the expressions for equality
231 llvm::FoldingSetNodeID ID1, ID2;
232 X.getAsExpr()->Profile(ID1, Context, true);
233 Y.getAsExpr()->Profile(ID2, Context, true);
234 if (ID1 == ID2)
235 return X;
236 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000237
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000238 // All other combinations are incompatible.
239 return DeducedTemplateArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000240
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000241 case TemplateArgument::Declaration:
242 // If we deduced a declaration and a dependent expression, keep the
243 // declaration.
244 if (Y.getKind() == TemplateArgument::Expression)
245 return X;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000246
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000247 // If we deduced a declaration and an integral constant, keep the
248 // integral constant.
249 if (Y.getKind() == TemplateArgument::Integral)
250 return Y;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000251
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000252 // If we deduced two declarations, make sure they they refer to the
253 // same declaration.
254 if (Y.getKind() == TemplateArgument::Declaration &&
255 isSameDeclaration(X.getAsDecl(), Y.getAsDecl()))
256 return X;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000257
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000258 // All other combinations are incompatible.
259 return DeducedTemplateArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000260
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000261 case TemplateArgument::Pack:
262 if (Y.getKind() != TemplateArgument::Pack ||
263 X.pack_size() != Y.pack_size())
264 return DeducedTemplateArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000265
266 for (TemplateArgument::pack_iterator XA = X.pack_begin(),
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000267 XAEnd = X.pack_end(),
268 YA = Y.pack_begin();
269 XA != XAEnd; ++XA, ++YA) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000270 if (checkDeducedTemplateArguments(Context,
271 DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()),
Douglas Gregor135ffa72011-01-05 21:00:53 +0000272 DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound()))
273 .isNull())
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000274 return DeducedTemplateArgument();
275 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000276
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000277 return X;
278 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000279
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000280 return DeducedTemplateArgument();
281}
282
Mike Stump1eb44332009-09-09 15:08:12 +0000283/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +0000284/// from the given constant.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000285static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000286DeduceNonTypeTemplateArgument(Sema &S,
Mike Stump1eb44332009-09-09 15:08:12 +0000287 NonTypeTemplateParmDecl *NTTP,
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000288 llvm::APSInt Value, QualType ValueType,
Douglas Gregor02024a92010-03-28 02:42:43 +0000289 bool DeducedFromArrayBound,
John McCall2a7fb272010-08-25 05:32:35 +0000290 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000291 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +0000292 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000293 "Cannot deduce non-type template argument with depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000295 DeducedTemplateArgument NewDeduced(Value, ValueType, DeducedFromArrayBound);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000296 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000297 Deduced[NTTP->getIndex()],
298 NewDeduced);
299 if (Result.isNull()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000300 Info.Param = NTTP;
301 Info.FirstArg = Deduced[NTTP->getIndex()];
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000302 Info.SecondArg = NewDeduced;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000303 return Sema::TDK_Inconsistent;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000304 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000305
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000306 Deduced[NTTP->getIndex()] = Result;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000307 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000308}
309
Mike Stump1eb44332009-09-09 15:08:12 +0000310/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +0000311/// from the given type- or value-dependent expression.
312///
313/// \returns true if deduction succeeded, false otherwise.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000314static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000315DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000316 NonTypeTemplateParmDecl *NTTP,
317 Expr *Value,
John McCall2a7fb272010-08-25 05:32:35 +0000318 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000319 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +0000320 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000321 "Cannot deduce non-type template argument with depth > 0");
322 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
323 "Expression template argument must be type- or value-dependent.");
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000325 DeducedTemplateArgument NewDeduced(Value);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000326 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
327 Deduced[NTTP->getIndex()],
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000328 NewDeduced);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000329
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000330 if (Result.isNull()) {
331 Info.Param = NTTP;
332 Info.FirstArg = Deduced[NTTP->getIndex()];
333 Info.SecondArg = NewDeduced;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000334 return Sema::TDK_Inconsistent;
Douglas Gregor199d9912009-06-05 00:53:49 +0000335 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000336
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000337 Deduced[NTTP->getIndex()] = Result;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000338 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000339}
340
Douglas Gregor15755cb2009-11-13 23:45:44 +0000341/// \brief Deduce the value of the given non-type template parameter
342/// from the given declaration.
343///
344/// \returns true if deduction succeeded, false otherwise.
345static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000346DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregor15755cb2009-11-13 23:45:44 +0000347 NonTypeTemplateParmDecl *NTTP,
348 Decl *D,
John McCall2a7fb272010-08-25 05:32:35 +0000349 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000350 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor15755cb2009-11-13 23:45:44 +0000351 assert(NTTP->getDepth() == 0 &&
352 "Cannot deduce non-type template argument with depth > 0");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000353
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000354 DeducedTemplateArgument NewDeduced(D? D->getCanonicalDecl() : 0);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000355 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000356 Deduced[NTTP->getIndex()],
357 NewDeduced);
358 if (Result.isNull()) {
359 Info.Param = NTTP;
360 Info.FirstArg = Deduced[NTTP->getIndex()];
361 Info.SecondArg = NewDeduced;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000362 return Sema::TDK_Inconsistent;
Douglas Gregor15755cb2009-11-13 23:45:44 +0000363 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000364
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000365 Deduced[NTTP->getIndex()] = Result;
Douglas Gregor15755cb2009-11-13 23:45:44 +0000366 return Sema::TDK_Success;
367}
368
Douglas Gregorf67875d2009-06-12 18:26:56 +0000369static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000370DeduceTemplateArguments(Sema &S,
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000371 TemplateParameterList *TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000372 TemplateName Param,
373 TemplateName Arg,
John McCall2a7fb272010-08-25 05:32:35 +0000374 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000375 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregord708c722009-06-09 16:35:58 +0000376 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000377 if (!ParamDecl) {
378 // The parameter type is dependent and is not a template template parameter,
379 // so there is nothing that we can deduce.
380 return Sema::TDK_Success;
381 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000382
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000383 if (TemplateTemplateParmDecl *TempParam
384 = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) {
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000385 DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000386 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000387 Deduced[TempParam->getIndex()],
388 NewDeduced);
389 if (Result.isNull()) {
390 Info.Param = TempParam;
391 Info.FirstArg = Deduced[TempParam->getIndex()];
392 Info.SecondArg = NewDeduced;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000393 return Sema::TDK_Inconsistent;
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000394 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000395
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000396 Deduced[TempParam->getIndex()] = Result;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000397 return Sema::TDK_Success;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000398 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000399
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000400 // Verify that the two template names are equivalent.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000401 if (S.Context.hasSameTemplateName(Param, Arg))
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000402 return Sema::TDK_Success;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000403
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000404 // Mismatch of non-dependent template parameter to argument.
405 Info.FirstArg = TemplateArgument(Param);
406 Info.SecondArg = TemplateArgument(Arg);
407 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000408}
409
Mike Stump1eb44332009-09-09 15:08:12 +0000410/// \brief Deduce the template arguments by comparing the template parameter
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000411/// type (which is a template-id) with the template argument type.
412///
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000413/// \param S the Sema
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000414///
415/// \param TemplateParams the template parameters that we are deducing
416///
417/// \param Param the parameter type
418///
419/// \param Arg the argument type
420///
421/// \param Info information about the template argument deduction itself
422///
423/// \param Deduced the deduced template arguments
424///
425/// \returns the result of template argument deduction so far. Note that a
426/// "success" result means that template argument deduction has not yet failed,
427/// but it may still fail, later, for other reasons.
428static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000429DeduceTemplateArguments(Sema &S,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000430 TemplateParameterList *TemplateParams,
431 const TemplateSpecializationType *Param,
432 QualType Arg,
John McCall2a7fb272010-08-25 05:32:35 +0000433 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000434 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
John McCall467b27b2009-10-22 20:10:53 +0000435 assert(Arg.isCanonical() && "Argument type must be canonical");
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000437 // Check whether the template argument is a dependent template-id.
Mike Stump1eb44332009-09-09 15:08:12 +0000438 if (const TemplateSpecializationType *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000439 = dyn_cast<TemplateSpecializationType>(Arg)) {
440 // Perform template argument deduction for the template name.
441 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000442 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000443 Param->getTemplateName(),
444 SpecArg->getTemplateName(),
445 Info, Deduced))
446 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000449 // Perform template argument deduction on each template
Douglas Gregor0972c862010-12-22 18:55:49 +0000450 // argument. Ignore any missing/extra arguments, since they could be
451 // filled in by default arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000452 return DeduceTemplateArguments(S, TemplateParams,
453 Param->getArgs(), Param->getNumArgs(),
Douglas Gregor0972c862010-12-22 18:55:49 +0000454 SpecArg->getArgs(), SpecArg->getNumArgs(),
455 Info, Deduced,
456 /*NumberOfArgumentsMustMatch=*/false);
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000457 }
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000459 // If the argument type is a class template specialization, we
460 // perform template argument deduction using its template
461 // arguments.
462 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
463 if (!RecordArg)
464 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000465
466 ClassTemplateSpecializationDecl *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000467 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
468 if (!SpecArg)
469 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000471 // Perform template argument deduction for the template name.
472 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000473 = DeduceTemplateArguments(S,
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000474 TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000475 Param->getTemplateName(),
476 TemplateName(SpecArg->getSpecializedTemplate()),
477 Info, Deduced))
478 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Douglas Gregor20a55e22010-12-22 18:17:10 +0000480 // Perform template argument deduction for the template arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000481 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor20a55e22010-12-22 18:17:10 +0000482 Param->getArgs(), Param->getNumArgs(),
483 SpecArg->getTemplateArgs().data(),
484 SpecArg->getTemplateArgs().size(),
485 Info, Deduced);
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000486}
487
John McCallcd05e812010-08-28 22:14:41 +0000488/// \brief Determines whether the given type is an opaque type that
489/// might be more qualified when instantiated.
490static bool IsPossiblyOpaquelyQualifiedType(QualType T) {
491 switch (T->getTypeClass()) {
492 case Type::TypeOfExpr:
493 case Type::TypeOf:
494 case Type::DependentName:
495 case Type::Decltype:
496 case Type::UnresolvedUsing:
John McCall62c28c82011-01-18 07:41:22 +0000497 case Type::TemplateTypeParm:
John McCallcd05e812010-08-28 22:14:41 +0000498 return true;
499
500 case Type::ConstantArray:
501 case Type::IncompleteArray:
502 case Type::VariableArray:
503 case Type::DependentSizedArray:
504 return IsPossiblyOpaquelyQualifiedType(
505 cast<ArrayType>(T)->getElementType());
506
507 default:
508 return false;
509 }
510}
511
Douglas Gregord3731192011-01-10 07:32:04 +0000512/// \brief Retrieve the depth and index of a template parameter.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000513static std::pair<unsigned, unsigned>
Douglas Gregord3731192011-01-10 07:32:04 +0000514getDepthAndIndex(NamedDecl *ND) {
Douglas Gregor603cfb42011-01-05 23:12:31 +0000515 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
516 return std::make_pair(TTP->getDepth(), TTP->getIndex());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000517
Douglas Gregor603cfb42011-01-05 23:12:31 +0000518 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
519 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000520
Douglas Gregor603cfb42011-01-05 23:12:31 +0000521 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
522 return std::make_pair(TTP->getDepth(), TTP->getIndex());
523}
524
Douglas Gregord3731192011-01-10 07:32:04 +0000525/// \brief Retrieve the depth and index of an unexpanded parameter pack.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000526static std::pair<unsigned, unsigned>
Douglas Gregord3731192011-01-10 07:32:04 +0000527getDepthAndIndex(UnexpandedParameterPack UPP) {
528 if (const TemplateTypeParmType *TTP
529 = UPP.first.dyn_cast<const TemplateTypeParmType *>())
530 return std::make_pair(TTP->getDepth(), TTP->getIndex());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000531
Douglas Gregord3731192011-01-10 07:32:04 +0000532 return getDepthAndIndex(UPP.first.get<NamedDecl *>());
533}
534
Douglas Gregor603cfb42011-01-05 23:12:31 +0000535/// \brief Helper function to build a TemplateParameter when we don't
536/// know its type statically.
537static TemplateParameter makeTemplateParameter(Decl *D) {
538 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
539 return TemplateParameter(TTP);
540 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
541 return TemplateParameter(NTTP);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000542
Douglas Gregor603cfb42011-01-05 23:12:31 +0000543 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
544}
545
Douglas Gregor54293852011-01-10 17:35:05 +0000546/// \brief Prepare to perform template argument deduction for all of the
547/// arguments in a set of argument packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000548static void PrepareArgumentPackDeduction(Sema &S,
549 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor54293852011-01-10 17:35:05 +0000550 const llvm::SmallVectorImpl<unsigned> &PackIndices,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000551 llvm::SmallVectorImpl<DeducedTemplateArgument> &SavedPacks,
Douglas Gregor54293852011-01-10 17:35:05 +0000552 llvm::SmallVectorImpl<
553 llvm::SmallVector<DeducedTemplateArgument, 4> > &NewlyDeducedPacks) {
554 // Save the deduced template arguments for each parameter pack expanded
555 // by this pack expansion, then clear out the deduction.
556 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
557 // Save the previously-deduced argument pack, then clear it out so that we
558 // can deduce a new argument pack.
559 SavedPacks[I] = Deduced[PackIndices[I]];
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000560 Deduced[PackIndices[I]] = TemplateArgument();
561
Douglas Gregor54293852011-01-10 17:35:05 +0000562 // If the template arugment pack was explicitly specified, add that to
563 // the set of deduced arguments.
564 const TemplateArgument *ExplicitArgs;
565 unsigned NumExplicitArgs;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000566 if (NamedDecl *PartiallySubstitutedPack
Douglas Gregor54293852011-01-10 17:35:05 +0000567 = S.CurrentInstantiationScope->getPartiallySubstitutedPack(
568 &ExplicitArgs,
569 &NumExplicitArgs)) {
570 if (getDepthAndIndex(PartiallySubstitutedPack).second == PackIndices[I])
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000571 NewlyDeducedPacks[I].append(ExplicitArgs,
Douglas Gregor54293852011-01-10 17:35:05 +0000572 ExplicitArgs + NumExplicitArgs);
573 }
574 }
575}
576
Douglas Gregor0216f812011-01-10 17:53:52 +0000577/// \brief Finish template argument deduction for a set of argument packs,
578/// producing the argument packs and checking for consistency with prior
579/// deductions.
580static Sema::TemplateDeductionResult
581FinishArgumentPackDeduction(Sema &S,
582 TemplateParameterList *TemplateParams,
583 bool HasAnyArguments,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000584 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor0216f812011-01-10 17:53:52 +0000585 const llvm::SmallVectorImpl<unsigned> &PackIndices,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000586 llvm::SmallVectorImpl<DeducedTemplateArgument> &SavedPacks,
Douglas Gregor0216f812011-01-10 17:53:52 +0000587 llvm::SmallVectorImpl<
588 llvm::SmallVector<DeducedTemplateArgument, 4> > &NewlyDeducedPacks,
589 TemplateDeductionInfo &Info) {
590 // Build argument packs for each of the parameter packs expanded by this
591 // pack expansion.
592 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
593 if (HasAnyArguments && NewlyDeducedPacks[I].empty()) {
594 // We were not able to deduce anything for this parameter pack,
595 // so just restore the saved argument pack.
596 Deduced[PackIndices[I]] = SavedPacks[I];
597 continue;
598 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000599
Douglas Gregor0216f812011-01-10 17:53:52 +0000600 DeducedTemplateArgument NewPack;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000601
Douglas Gregor0216f812011-01-10 17:53:52 +0000602 if (NewlyDeducedPacks[I].empty()) {
603 // If we deduced an empty argument pack, create it now.
604 NewPack = DeducedTemplateArgument(TemplateArgument(0, 0));
605 } else {
606 TemplateArgument *ArgumentPack
Douglas Gregor203e6a32011-01-11 23:09:57 +0000607 = new (S.Context) TemplateArgument [NewlyDeducedPacks[I].size()];
Douglas Gregor0216f812011-01-10 17:53:52 +0000608 std::copy(NewlyDeducedPacks[I].begin(), NewlyDeducedPacks[I].end(),
609 ArgumentPack);
610 NewPack
Douglas Gregor203e6a32011-01-11 23:09:57 +0000611 = DeducedTemplateArgument(TemplateArgument(ArgumentPack,
612 NewlyDeducedPacks[I].size()),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000613 NewlyDeducedPacks[I][0].wasDeducedFromArrayBound());
Douglas Gregor0216f812011-01-10 17:53:52 +0000614 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000615
Douglas Gregor0216f812011-01-10 17:53:52 +0000616 DeducedTemplateArgument Result
617 = checkDeducedTemplateArguments(S.Context, SavedPacks[I], NewPack);
618 if (Result.isNull()) {
619 Info.Param
620 = makeTemplateParameter(TemplateParams->getParam(PackIndices[I]));
621 Info.FirstArg = SavedPacks[I];
622 Info.SecondArg = NewPack;
623 return Sema::TDK_Inconsistent;
624 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000625
Douglas Gregor0216f812011-01-10 17:53:52 +0000626 Deduced[PackIndices[I]] = Result;
627 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000628
Douglas Gregor0216f812011-01-10 17:53:52 +0000629 return Sema::TDK_Success;
630}
631
Douglas Gregor603cfb42011-01-05 23:12:31 +0000632/// \brief Deduce the template arguments by comparing the list of parameter
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000633/// types to the list of argument types, as in the parameter-type-lists of
634/// function types (C++ [temp.deduct.type]p10).
Douglas Gregor603cfb42011-01-05 23:12:31 +0000635///
636/// \param S The semantic analysis object within which we are deducing
637///
638/// \param TemplateParams The template parameters that we are deducing
639///
640/// \param Params The list of parameter types
641///
642/// \param NumParams The number of types in \c Params
643///
644/// \param Args The list of argument types
645///
646/// \param NumArgs The number of types in \c Args
647///
648/// \param Info information about the template argument deduction itself
649///
650/// \param Deduced the deduced template arguments
651///
652/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
653/// how template argument deduction is performed.
654///
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000655/// \param PartialOrdering If true, we are performing template argument
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000656/// deduction for during partial ordering for a call
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000657/// (C++0x [temp.deduct.partial]).
658///
Douglas Gregorb939a192011-01-21 17:29:42 +0000659/// \param RefParamComparisons If we're performing template argument deduction
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000660/// in the context of partial ordering, the set of qualifier comparisons.
661///
Douglas Gregor603cfb42011-01-05 23:12:31 +0000662/// \returns the result of template argument deduction so far. Note that a
663/// "success" result means that template argument deduction has not yet failed,
664/// but it may still fail, later, for other reasons.
665static Sema::TemplateDeductionResult
666DeduceTemplateArguments(Sema &S,
667 TemplateParameterList *TemplateParams,
668 const QualType *Params, unsigned NumParams,
669 const QualType *Args, unsigned NumArgs,
670 TemplateDeductionInfo &Info,
671 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000672 unsigned TDF,
673 bool PartialOrdering = false,
Douglas Gregorb939a192011-01-21 17:29:42 +0000674 llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *
675 RefParamComparisons = 0) {
Douglas Gregor0bbacf82011-01-05 23:23:17 +0000676 // Fast-path check to see if we have too many/too few arguments.
677 if (NumParams != NumArgs &&
678 !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) &&
679 !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1])))
Douglas Gregor3cae5c92011-01-10 20:53:55 +0000680 return Sema::TDK_NonDeducedMismatch;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000681
Douglas Gregor603cfb42011-01-05 23:12:31 +0000682 // C++0x [temp.deduct.type]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000683 // Similarly, if P has a form that contains (T), then each parameter type
684 // Pi of the respective parameter-type- list of P is compared with the
685 // corresponding parameter type Ai of the corresponding parameter-type-list
686 // of A. [...]
Douglas Gregor603cfb42011-01-05 23:12:31 +0000687 unsigned ArgIdx = 0, ParamIdx = 0;
688 for (; ParamIdx != NumParams; ++ParamIdx) {
689 // Check argument types.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000690 const PackExpansionType *Expansion
Douglas Gregor603cfb42011-01-05 23:12:31 +0000691 = dyn_cast<PackExpansionType>(Params[ParamIdx]);
692 if (!Expansion) {
693 // Simple case: compare the parameter and argument types at this point.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000694
Douglas Gregor603cfb42011-01-05 23:12:31 +0000695 // Make sure we have an argument.
696 if (ArgIdx >= NumArgs)
Douglas Gregor3cae5c92011-01-10 20:53:55 +0000697 return Sema::TDK_NonDeducedMismatch;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000698
Douglas Gregor77d6bb92011-01-11 22:21:24 +0000699 if (isa<PackExpansionType>(Args[ArgIdx])) {
700 // C++0x [temp.deduct.type]p22:
701 // If the original function parameter associated with A is a function
702 // parameter pack and the function parameter associated with P is not
703 // a function parameter pack, then template argument deduction fails.
704 return Sema::TDK_NonDeducedMismatch;
705 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000706
Douglas Gregor603cfb42011-01-05 23:12:31 +0000707 if (Sema::TemplateDeductionResult Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000708 = DeduceTemplateArguments(S, TemplateParams,
709 Params[ParamIdx],
710 Args[ArgIdx],
711 Info, Deduced, TDF,
712 PartialOrdering,
Douglas Gregorb939a192011-01-21 17:29:42 +0000713 RefParamComparisons))
Douglas Gregor603cfb42011-01-05 23:12:31 +0000714 return Result;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000715
Douglas Gregor603cfb42011-01-05 23:12:31 +0000716 ++ArgIdx;
717 continue;
718 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000719
Douglas Gregor7d5c0c12011-01-11 01:52:23 +0000720 // C++0x [temp.deduct.type]p5:
721 // The non-deduced contexts are:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000722 // - A function parameter pack that does not occur at the end of the
Douglas Gregor7d5c0c12011-01-11 01:52:23 +0000723 // parameter-declaration-clause.
724 if (ParamIdx + 1 < NumParams)
725 return Sema::TDK_Success;
726
Douglas Gregor603cfb42011-01-05 23:12:31 +0000727 // C++0x [temp.deduct.type]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000728 // If the parameter-declaration corresponding to Pi is a function
Douglas Gregor603cfb42011-01-05 23:12:31 +0000729 // parameter pack, then the type of its declarator- id is compared with
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000730 // each remaining parameter type in the parameter-type-list of A. Each
Douglas Gregor603cfb42011-01-05 23:12:31 +0000731 // comparison deduces template arguments for subsequent positions in the
732 // template parameter packs expanded by the function parameter pack.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000733
Douglas Gregor603cfb42011-01-05 23:12:31 +0000734 // Compute the set of template parameter indices that correspond to
735 // parameter packs expanded by the pack expansion.
736 llvm::SmallVector<unsigned, 2> PackIndices;
737 QualType Pattern = Expansion->getPattern();
738 {
739 llvm::BitVector SawIndices(TemplateParams->size());
740 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
741 S.collectUnexpandedParameterPacks(Pattern, Unexpanded);
742 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
743 unsigned Depth, Index;
744 llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
745 if (Depth == 0 && !SawIndices[Index]) {
746 SawIndices[Index] = true;
747 PackIndices.push_back(Index);
748 }
749 }
750 }
751 assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
752
Douglas Gregord3731192011-01-10 07:32:04 +0000753 // Keep track of the deduced template arguments for each parameter pack
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000754 // expanded by this pack expansion (the outer index) and for each
Douglas Gregord3731192011-01-10 07:32:04 +0000755 // template argument (the inner SmallVectors).
756 llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2>
757 NewlyDeducedPacks(PackIndices.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000758 llvm::SmallVector<DeducedTemplateArgument, 2>
Douglas Gregor54293852011-01-10 17:35:05 +0000759 SavedPacks(PackIndices.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000760 PrepareArgumentPackDeduction(S, Deduced, PackIndices, SavedPacks,
Douglas Gregor54293852011-01-10 17:35:05 +0000761 NewlyDeducedPacks);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000762
Douglas Gregor603cfb42011-01-05 23:12:31 +0000763 bool HasAnyArguments = false;
764 for (; ArgIdx < NumArgs; ++ArgIdx) {
765 HasAnyArguments = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000766
Douglas Gregor603cfb42011-01-05 23:12:31 +0000767 // Deduce template arguments from the pattern.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000768 if (Sema::TemplateDeductionResult Result
Douglas Gregor73b3cf62011-01-25 17:19:08 +0000769 = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx],
770 Info, Deduced, TDF, PartialOrdering,
771 RefParamComparisons))
Douglas Gregor603cfb42011-01-05 23:12:31 +0000772 return Result;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000773
Douglas Gregor603cfb42011-01-05 23:12:31 +0000774 // Capture the deduced template arguments for each parameter pack expanded
775 // by this pack expansion, add them to the list of arguments we've deduced
776 // for that pack, then clear out the deduced argument.
777 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
778 DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
779 if (!DeducedArg.isNull()) {
780 NewlyDeducedPacks[I].push_back(DeducedArg);
781 DeducedArg = DeducedTemplateArgument();
782 }
783 }
784 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000785
Douglas Gregor603cfb42011-01-05 23:12:31 +0000786 // Build argument packs for each of the parameter packs expanded by this
787 // pack expansion.
Douglas Gregor0216f812011-01-10 17:53:52 +0000788 if (Sema::TemplateDeductionResult Result
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000789 = FinishArgumentPackDeduction(S, TemplateParams, HasAnyArguments,
Douglas Gregor0216f812011-01-10 17:53:52 +0000790 Deduced, PackIndices, SavedPacks,
791 NewlyDeducedPacks, Info))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000792 return Result;
Douglas Gregor603cfb42011-01-05 23:12:31 +0000793 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000794
Douglas Gregor603cfb42011-01-05 23:12:31 +0000795 // Make sure we don't have any extra arguments.
796 if (ArgIdx < NumArgs)
Douglas Gregor3cae5c92011-01-10 20:53:55 +0000797 return Sema::TDK_NonDeducedMismatch;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000798
Douglas Gregor603cfb42011-01-05 23:12:31 +0000799 return Sema::TDK_Success;
800}
801
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000802/// \brief Determine whether the parameter has qualifiers that are either
803/// inconsistent with or a superset of the argument's qualifiers.
804static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType,
805 QualType ArgType) {
806 Qualifiers ParamQs = ParamType.getQualifiers();
807 Qualifiers ArgQs = ArgType.getQualifiers();
808
809 if (ParamQs == ArgQs)
810 return false;
811
812 // Mismatched (but not missing) Objective-C GC attributes.
813 if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() &&
814 ParamQs.hasObjCGCAttr())
815 return true;
816
817 // Mismatched (but not missing) address spaces.
818 if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() &&
819 ParamQs.hasAddressSpace())
820 return true;
821
822 // CVR qualifier superset.
823 return (ParamQs.getCVRQualifiers() != ArgQs.getCVRQualifiers()) &&
824 ((ParamQs.getCVRQualifiers() | ArgQs.getCVRQualifiers())
825 == ParamQs.getCVRQualifiers());
826}
827
Douglas Gregor500d3312009-06-26 18:27:22 +0000828/// \brief Deduce the template arguments by comparing the parameter type and
829/// the argument type (C++ [temp.deduct.type]).
830///
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000831/// \param S the semantic analysis object within which we are deducing
Douglas Gregor500d3312009-06-26 18:27:22 +0000832///
833/// \param TemplateParams the template parameters that we are deducing
834///
835/// \param ParamIn the parameter type
836///
837/// \param ArgIn the argument type
838///
839/// \param Info information about the template argument deduction itself
840///
841/// \param Deduced the deduced template arguments
842///
Douglas Gregor508f1c82009-06-26 23:10:12 +0000843/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
Mike Stump1eb44332009-09-09 15:08:12 +0000844/// how template argument deduction is performed.
Douglas Gregor500d3312009-06-26 18:27:22 +0000845///
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000846/// \param PartialOrdering Whether we're performing template argument deduction
847/// in the context of partial ordering (C++0x [temp.deduct.partial]).
848///
Douglas Gregorb939a192011-01-21 17:29:42 +0000849/// \param RefParamComparisons If we're performing template argument deduction
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000850/// in the context of partial ordering, the set of qualifier comparisons.
851///
Douglas Gregor500d3312009-06-26 18:27:22 +0000852/// \returns the result of template argument deduction so far. Note that a
853/// "success" result means that template argument deduction has not yet failed,
854/// but it may still fail, later, for other reasons.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000855static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000856DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000857 TemplateParameterList *TemplateParams,
858 QualType ParamIn, QualType ArgIn,
John McCall2a7fb272010-08-25 05:32:35 +0000859 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000860 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000861 unsigned TDF,
862 bool PartialOrdering,
Douglas Gregorb939a192011-01-21 17:29:42 +0000863 llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *RefParamComparisons) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000864 // We only want to look at the canonical types, since typedefs and
865 // sugar are not part of template argument deduction.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000866 QualType Param = S.Context.getCanonicalType(ParamIn);
867 QualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000868
Douglas Gregor77d6bb92011-01-11 22:21:24 +0000869 // If the argument type is a pack expansion, look at its pattern.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000870 // This isn't explicitly called out
Douglas Gregor77d6bb92011-01-11 22:21:24 +0000871 if (const PackExpansionType *ArgExpansion
872 = dyn_cast<PackExpansionType>(Arg))
873 Arg = ArgExpansion->getPattern();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000874
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000875 if (PartialOrdering) {
876 // C++0x [temp.deduct.partial]p5:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000877 // Before the partial ordering is done, certain transformations are
878 // performed on the types used for partial ordering:
879 // - If P is a reference type, P is replaced by the type referred to.
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000880 const ReferenceType *ParamRef = Param->getAs<ReferenceType>();
881 if (ParamRef)
882 Param = ParamRef->getPointeeType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000883
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000884 // - If A is a reference type, A is replaced by the type referred to.
885 const ReferenceType *ArgRef = Arg->getAs<ReferenceType>();
886 if (ArgRef)
887 Arg = ArgRef->getPointeeType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000888
Douglas Gregorb939a192011-01-21 17:29:42 +0000889 if (RefParamComparisons && ParamRef && ArgRef) {
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000890 // C++0x [temp.deduct.partial]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000891 // If both P and A were reference types (before being replaced with the
892 // type referred to above), determine which of the two types (if any) is
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000893 // more cv-qualified than the other; otherwise the types are considered
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000894 // to be equally cv-qualified for partial ordering purposes. The result
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000895 // of this determination will be used below.
896 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000897 // We save this information for later, using it only when deduction
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000898 // succeeds in both directions.
Douglas Gregorb939a192011-01-21 17:29:42 +0000899 RefParamPartialOrderingComparison Comparison;
900 Comparison.ParamIsRvalueRef = ParamRef->getAs<RValueReferenceType>();
901 Comparison.ArgIsRvalueRef = ArgRef->getAs<RValueReferenceType>();
902 Comparison.Qualifiers = NeitherMoreQualified;
Douglas Gregor769d0cc2011-04-30 17:07:52 +0000903
904 Qualifiers ParamQuals = Param.getQualifiers();
905 Qualifiers ArgQuals = Arg.getQualifiers();
906 if (ParamQuals.isStrictSupersetOf(ArgQuals))
Douglas Gregorb939a192011-01-21 17:29:42 +0000907 Comparison.Qualifiers = ParamMoreQualified;
Douglas Gregor769d0cc2011-04-30 17:07:52 +0000908 else if (ArgQuals.isStrictSupersetOf(ParamQuals))
Douglas Gregorb939a192011-01-21 17:29:42 +0000909 Comparison.Qualifiers = ArgMoreQualified;
910 RefParamComparisons->push_back(Comparison);
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000911 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000912
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000913 // C++0x [temp.deduct.partial]p7:
914 // Remove any top-level cv-qualifiers:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000915 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000916 // version of P.
917 Param = Param.getUnqualifiedType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000918 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000919 // version of A.
920 Arg = Arg.getUnqualifiedType();
921 } else {
922 // C++0x [temp.deduct.call]p4 bullet 1:
923 // - If the original P is a reference type, the deduced A (i.e., the type
924 // referred to by the reference) can be more cv-qualified than the
925 // transformed A.
926 if (TDF & TDF_ParamWithReferenceType) {
927 Qualifiers Quals;
928 QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals);
929 Quals.setCVRQualifiers(Quals.getCVRQualifiers() &
John McCall62c28c82011-01-18 07:41:22 +0000930 Arg.getCVRQualifiers());
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000931 Param = S.Context.getQualifiedType(UnqualParam, Quals);
932 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000933
Douglas Gregor73b3cf62011-01-25 17:19:08 +0000934 if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) {
935 // C++0x [temp.deduct.type]p10:
936 // If P and A are function types that originated from deduction when
937 // taking the address of a function template (14.8.2.2) or when deducing
938 // template arguments from a function declaration (14.8.2.6) and Pi and
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000939 // Ai are parameters of the top-level parameter-type-list of P and A,
940 // respectively, Pi is adjusted if it is an rvalue reference to a
941 // cv-unqualified template parameter and Ai is an lvalue reference, in
942 // which case the type of Pi is changed to be the template parameter
Douglas Gregor73b3cf62011-01-25 17:19:08 +0000943 // type (i.e., T&& is changed to simply T). [ Note: As a result, when
944 // Pi is T&& and Ai is X&, the adjusted Pi will be T, causing T to be
NAKAMURA Takumi00995302011-01-27 07:09:49 +0000945 // deduced as X&. - end note ]
Douglas Gregor73b3cf62011-01-25 17:19:08 +0000946 TDF &= ~TDF_TopLevelParameterTypeList;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000947
Douglas Gregor73b3cf62011-01-25 17:19:08 +0000948 if (const RValueReferenceType *ParamRef
949 = Param->getAs<RValueReferenceType>()) {
950 if (isa<TemplateTypeParmType>(ParamRef->getPointeeType()) &&
951 !ParamRef->getPointeeType().getQualifiers())
952 if (Arg->isLValueReferenceType())
953 Param = ParamRef->getPointeeType();
954 }
955 }
Douglas Gregor500d3312009-06-26 18:27:22 +0000956 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000957
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000958 // If the parameter type is not dependent, there is nothing to deduce.
Douglas Gregor12820292009-09-14 20:00:47 +0000959 if (!Param->isDependentType()) {
Douglas Gregor3cae5c92011-01-10 20:53:55 +0000960 if (!(TDF & TDF_SkipNonDependent) && Param != Arg)
Douglas Gregor12820292009-09-14 20:00:47 +0000961 return Sema::TDK_NonDeducedMismatch;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000962
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000963 return Sema::TDK_Success;
Douglas Gregor12820292009-09-14 20:00:47 +0000964 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000965
Douglas Gregor199d9912009-06-05 00:53:49 +0000966 // C++ [temp.deduct.type]p9:
Mike Stump1eb44332009-09-09 15:08:12 +0000967 // A template type argument T, a template template argument TT or a
968 // template non-type argument i can be deduced if P and A have one of
Douglas Gregor199d9912009-06-05 00:53:49 +0000969 // the following forms:
970 //
971 // T
972 // cv-list T
Mike Stump1eb44332009-09-09 15:08:12 +0000973 if (const TemplateTypeParmType *TemplateTypeParm
John McCall183700f2009-09-21 23:43:11 +0000974 = Param->getAs<TemplateTypeParmType>()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000975 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000976 bool RecanonicalizeArg = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Douglas Gregor9e9fae42009-07-22 20:02:25 +0000978 // If the argument type is an array type, move the qualifiers up to the
979 // top level, so they can be matched with the qualifiers on the parameter.
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000980 if (isa<ArrayType>(Arg)) {
John McCall0953e762009-09-24 19:53:00 +0000981 Qualifiers Quals;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000982 Arg = S.Context.getUnqualifiedArrayType(Arg, Quals);
John McCall0953e762009-09-24 19:53:00 +0000983 if (Quals) {
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000984 Arg = S.Context.getQualifiedType(Arg, Quals);
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000985 RecanonicalizeArg = true;
986 }
987 }
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000989 // The argument type can not be less qualified than the parameter
990 // type.
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000991 if (!(TDF & TDF_IgnoreQualifiers) &&
992 hasInconsistentOrSupersetQualifiersOf(Param, Arg)) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000993 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
John McCall57e97782010-08-05 09:05:08 +0000994 Info.FirstArg = TemplateArgument(Param);
John McCall833ca992009-10-29 08:12:44 +0000995 Info.SecondArg = TemplateArgument(Arg);
John McCall57e97782010-08-05 09:05:08 +0000996 return Sema::TDK_Underqualified;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000997 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000998
999 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001000 assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function");
John McCall0953e762009-09-24 19:53:00 +00001001 QualType DeducedType = Arg;
John McCall49f4e1c2010-12-10 11:01:00 +00001002
Douglas Gregor61d0b6b2011-04-28 00:56:09 +00001003 // Remove any qualifiers on the parameter from the deduced type.
1004 // We checked the qualifiers for consistency above.
1005 Qualifiers DeducedQs = DeducedType.getQualifiers();
1006 Qualifiers ParamQs = Param.getQualifiers();
1007 DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers());
1008 if (ParamQs.hasObjCGCAttr())
1009 DeducedQs.removeObjCGCAttr();
1010 if (ParamQs.hasAddressSpace())
1011 DeducedQs.removeAddressSpace();
1012 DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(),
1013 DeducedQs);
1014
Douglas Gregorf290e0d2009-07-22 21:30:48 +00001015 if (RecanonicalizeArg)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001016 DeducedType = S.Context.getCanonicalType(DeducedType);
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Douglas Gregor0d80abc2010-12-22 23:09:49 +00001018 DeducedTemplateArgument NewDeduced(DeducedType);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001019 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
Douglas Gregor0d80abc2010-12-22 23:09:49 +00001020 Deduced[Index],
1021 NewDeduced);
1022 if (Result.isNull()) {
1023 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
1024 Info.FirstArg = Deduced[Index];
1025 Info.SecondArg = NewDeduced;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001026 return Sema::TDK_Inconsistent;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001027 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001028
Douglas Gregor0d80abc2010-12-22 23:09:49 +00001029 Deduced[Index] = Result;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001030 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001031 }
1032
Douglas Gregorf67875d2009-06-12 18:26:56 +00001033 // Set up the template argument deduction information for a failure.
John McCall833ca992009-10-29 08:12:44 +00001034 Info.FirstArg = TemplateArgument(ParamIn);
1035 Info.SecondArg = TemplateArgument(ArgIn);
Douglas Gregorf67875d2009-06-12 18:26:56 +00001036
Douglas Gregor0bc15d92011-01-14 05:11:40 +00001037 // If the parameter is an already-substituted template parameter
1038 // pack, do nothing: we don't know which of its arguments to look
1039 // at, so we have to wait until all of the parameter packs in this
1040 // expansion have arguments.
1041 if (isa<SubstTemplateTypeParmPackType>(Param))
1042 return Sema::TDK_Success;
1043
Douglas Gregor508f1c82009-06-26 23:10:12 +00001044 // Check the cv-qualifiers on the parameter and argument types.
1045 if (!(TDF & TDF_IgnoreQualifiers)) {
1046 if (TDF & TDF_ParamWithReferenceType) {
Douglas Gregor61d0b6b2011-04-28 00:56:09 +00001047 if (hasInconsistentOrSupersetQualifiersOf(Param, Arg))
Douglas Gregor508f1c82009-06-26 23:10:12 +00001048 return Sema::TDK_NonDeducedMismatch;
John McCallcd05e812010-08-28 22:14:41 +00001049 } else if (!IsPossiblyOpaquelyQualifiedType(Param)) {
Douglas Gregor508f1c82009-06-26 23:10:12 +00001050 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Mike Stump1eb44332009-09-09 15:08:12 +00001051 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor508f1c82009-06-26 23:10:12 +00001052 }
1053 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001054
Douglas Gregord560d502009-06-04 00:21:18 +00001055 switch (Param->getTypeClass()) {
Douglas Gregor4ac01402011-06-15 16:02:29 +00001056 // Non-canonical types cannot appear here.
1057#define NON_CANONICAL_TYPE(Class, Base) \
1058 case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class);
1059#define TYPE(Class, Base)
1060#include "clang/AST/TypeNodes.def"
1061
1062 case Type::TemplateTypeParm:
1063 case Type::SubstTemplateTypeParmPack:
1064 llvm_unreachable("Type nodes handled above");
1065
1066 // These types cannot be used in templates or cannot be dependent, so
1067 // deduction always fails.
Douglas Gregor199d9912009-06-05 00:53:49 +00001068 case Type::Builtin:
Douglas Gregor4ac01402011-06-15 16:02:29 +00001069 case Type::VariableArray:
1070 case Type::Vector:
1071 case Type::FunctionNoProto:
1072 case Type::Record:
1073 case Type::Enum:
1074 case Type::ObjCObject:
1075 case Type::ObjCInterface:
1076 case Type::ObjCObjectPointer:
Douglas Gregorf67875d2009-06-12 18:26:56 +00001077 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Douglas Gregor4ac01402011-06-15 16:02:29 +00001079 // _Complex T [placeholder extension]
1080 case Type::Complex:
1081 if (const ComplexType *ComplexArg = Arg->getAs<ComplexType>())
1082 return DeduceTemplateArguments(S, TemplateParams,
1083 cast<ComplexType>(Param)->getElementType(),
1084 ComplexArg->getElementType(),
1085 Info, Deduced, TDF);
1086
1087 return Sema::TDK_NonDeducedMismatch;
1088
Douglas Gregor199d9912009-06-05 00:53:49 +00001089 // T *
Douglas Gregord560d502009-06-04 00:21:18 +00001090 case Type::Pointer: {
John McCallc0008342010-05-13 07:48:05 +00001091 QualType PointeeType;
1092 if (const PointerType *PointerArg = Arg->getAs<PointerType>()) {
1093 PointeeType = PointerArg->getPointeeType();
1094 } else if (const ObjCObjectPointerType *PointerArg
1095 = Arg->getAs<ObjCObjectPointerType>()) {
1096 PointeeType = PointerArg->getPointeeType();
1097 } else {
Douglas Gregorf67875d2009-06-12 18:26:56 +00001098 return Sema::TDK_NonDeducedMismatch;
John McCallc0008342010-05-13 07:48:05 +00001099 }
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Douglas Gregor41128772009-06-26 23:27:24 +00001101 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001102 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +00001103 cast<PointerType>(Param)->getPointeeType(),
John McCallc0008342010-05-13 07:48:05 +00001104 PointeeType,
Douglas Gregor41128772009-06-26 23:27:24 +00001105 Info, Deduced, SubTDF);
Douglas Gregord560d502009-06-04 00:21:18 +00001106 }
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Douglas Gregor199d9912009-06-05 00:53:49 +00001108 // T &
Douglas Gregord560d502009-06-04 00:21:18 +00001109 case Type::LValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +00001110 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +00001111 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001112 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001114 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +00001115 cast<LValueReferenceType>(Param)->getPointeeType(),
1116 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +00001117 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +00001118 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001119
Douglas Gregor199d9912009-06-05 00:53:49 +00001120 // T && [C++0x]
Douglas Gregord560d502009-06-04 00:21:18 +00001121 case Type::RValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +00001122 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +00001123 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001124 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001126 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +00001127 cast<RValueReferenceType>(Param)->getPointeeType(),
1128 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +00001129 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +00001130 }
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Douglas Gregor199d9912009-06-05 00:53:49 +00001132 // T [] (implied, but not stated explicitly)
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001133 case Type::IncompleteArray: {
Mike Stump1eb44332009-09-09 15:08:12 +00001134 const IncompleteArrayType *IncompleteArrayArg =
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001135 S.Context.getAsIncompleteArrayType(Arg);
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001136 if (!IncompleteArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001137 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001138
John McCalle4f26e52010-08-19 00:20:19 +00001139 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001140 return DeduceTemplateArguments(S, TemplateParams,
1141 S.Context.getAsIncompleteArrayType(Param)->getElementType(),
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001142 IncompleteArrayArg->getElementType(),
John McCalle4f26e52010-08-19 00:20:19 +00001143 Info, Deduced, SubTDF);
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001144 }
Douglas Gregor199d9912009-06-05 00:53:49 +00001145
1146 // T [integer-constant]
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001147 case Type::ConstantArray: {
Mike Stump1eb44332009-09-09 15:08:12 +00001148 const ConstantArrayType *ConstantArrayArg =
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001149 S.Context.getAsConstantArrayType(Arg);
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001150 if (!ConstantArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001151 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001152
1153 const ConstantArrayType *ConstantArrayParm =
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001154 S.Context.getAsConstantArrayType(Param);
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001155 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregorf67875d2009-06-12 18:26:56 +00001156 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001157
John McCalle4f26e52010-08-19 00:20:19 +00001158 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001159 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001160 ConstantArrayParm->getElementType(),
1161 ConstantArrayArg->getElementType(),
John McCalle4f26e52010-08-19 00:20:19 +00001162 Info, Deduced, SubTDF);
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001163 }
1164
Douglas Gregor199d9912009-06-05 00:53:49 +00001165 // type [i]
1166 case Type::DependentSizedArray: {
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001167 const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
Douglas Gregor199d9912009-06-05 00:53:49 +00001168 if (!ArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001169 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001170
John McCalle4f26e52010-08-19 00:20:19 +00001171 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
1172
Douglas Gregor199d9912009-06-05 00:53:49 +00001173 // Check the element type of the arrays
1174 const DependentSizedArrayType *DependentArrayParm
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001175 = S.Context.getAsDependentSizedArrayType(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +00001176 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001177 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001178 DependentArrayParm->getElementType(),
1179 ArrayArg->getElementType(),
John McCalle4f26e52010-08-19 00:20:19 +00001180 Info, Deduced, SubTDF))
Douglas Gregorf67875d2009-06-12 18:26:56 +00001181 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Douglas Gregor199d9912009-06-05 00:53:49 +00001183 // Determine the array bound is something we can deduce.
Mike Stump1eb44332009-09-09 15:08:12 +00001184 NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +00001185 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
1186 if (!NTTP)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001187 return Sema::TDK_Success;
Mike Stump1eb44332009-09-09 15:08:12 +00001188
1189 // We can perform template argument deduction for the given non-type
Douglas Gregor199d9912009-06-05 00:53:49 +00001190 // template parameter.
Mike Stump1eb44332009-09-09 15:08:12 +00001191 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +00001192 "Cannot deduce non-type template argument at depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +00001193 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson335e24a2009-06-16 22:44:31 +00001194 = dyn_cast<ConstantArrayType>(ArrayArg)) {
1195 llvm::APSInt Size(ConstantArrayArg->getSize());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001196 return DeduceNonTypeTemplateArgument(S, NTTP, Size,
Douglas Gregor9d0e4412010-03-26 05:50:28 +00001197 S.Context.getSizeType(),
Douglas Gregor02024a92010-03-28 02:42:43 +00001198 /*ArrayBound=*/true,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001199 Info, Deduced);
Anders Carlsson335e24a2009-06-16 22:44:31 +00001200 }
Douglas Gregor199d9912009-06-05 00:53:49 +00001201 if (const DependentSizedArrayType *DependentArrayArg
1202 = dyn_cast<DependentSizedArrayType>(ArrayArg))
Douglas Gregor34c2f8c2010-12-22 23:15:38 +00001203 if (DependentArrayArg->getSizeExpr())
1204 return DeduceNonTypeTemplateArgument(S, NTTP,
1205 DependentArrayArg->getSizeExpr(),
1206 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Douglas Gregor199d9912009-06-05 00:53:49 +00001208 // Incomplete type does not match a dependently-sized array type
Douglas Gregorf67875d2009-06-12 18:26:56 +00001209 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +00001210 }
Mike Stump1eb44332009-09-09 15:08:12 +00001211
1212 // type(*)(T)
1213 // T(*)()
1214 // T(*)(T)
Anders Carlssona27fad52009-06-08 15:19:08 +00001215 case Type::FunctionProto: {
Douglas Gregor73b3cf62011-01-25 17:19:08 +00001216 unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList;
Mike Stump1eb44332009-09-09 15:08:12 +00001217 const FunctionProtoType *FunctionProtoArg =
Anders Carlssona27fad52009-06-08 15:19:08 +00001218 dyn_cast<FunctionProtoType>(Arg);
1219 if (!FunctionProtoArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001220 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001221
1222 const FunctionProtoType *FunctionProtoParam =
Anders Carlssona27fad52009-06-08 15:19:08 +00001223 cast<FunctionProtoType>(Param);
Anders Carlsson994b6cb2009-06-08 19:22:23 +00001224
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001225 if (FunctionProtoParam->getTypeQuals()
Douglas Gregore3c7a7c2011-01-26 16:50:54 +00001226 != FunctionProtoArg->getTypeQuals() ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001227 FunctionProtoParam->getRefQualifier()
Douglas Gregore3c7a7c2011-01-26 16:50:54 +00001228 != FunctionProtoArg->getRefQualifier() ||
1229 FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregorf67875d2009-06-12 18:26:56 +00001230 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +00001231
Anders Carlssona27fad52009-06-08 15:19:08 +00001232 // Check return types.
Douglas Gregorf67875d2009-06-12 18:26:56 +00001233 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001234 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001235 FunctionProtoParam->getResultType(),
1236 FunctionProtoArg->getResultType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +00001237 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +00001238 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Douglas Gregor603cfb42011-01-05 23:12:31 +00001240 return DeduceTemplateArguments(S, TemplateParams,
1241 FunctionProtoParam->arg_type_begin(),
1242 FunctionProtoParam->getNumArgs(),
1243 FunctionProtoArg->arg_type_begin(),
1244 FunctionProtoArg->getNumArgs(),
Douglas Gregor73b3cf62011-01-25 17:19:08 +00001245 Info, Deduced, SubTDF);
Anders Carlssona27fad52009-06-08 15:19:08 +00001246 }
Mike Stump1eb44332009-09-09 15:08:12 +00001247
John McCall3cb0ebd2010-03-10 03:28:59 +00001248 case Type::InjectedClassName: {
1249 // Treat a template's injected-class-name as if the template
1250 // specialization type had been used.
John McCall31f17ec2010-04-27 00:57:59 +00001251 Param = cast<InjectedClassNameType>(Param)
1252 ->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00001253 assert(isa<TemplateSpecializationType>(Param) &&
1254 "injected class name is not a template specialization type");
1255 // fall through
1256 }
1257
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001258 // template-name<T> (where template-name refers to a class template)
Douglas Gregord708c722009-06-09 16:35:58 +00001259 // template-name<i>
Douglas Gregordb0d4b72009-11-11 23:06:43 +00001260 // TT<T>
1261 // TT<i>
1262 // TT<>
Douglas Gregord708c722009-06-09 16:35:58 +00001263 case Type::TemplateSpecialization: {
1264 const TemplateSpecializationType *SpecParam
1265 = cast<TemplateSpecializationType>(Param);
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001267 // Try to deduce template arguments from the template-id.
1268 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001269 = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001270 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +00001271
Douglas Gregor4a5c15f2009-09-30 22:13:51 +00001272 if (Result && (TDF & TDF_DerivedClass)) {
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001273 // C++ [temp.deduct.call]p3b3:
1274 // If P is a class, and P has the form template-id, then A can be a
1275 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump1eb44332009-09-09 15:08:12 +00001276 // class of the form template-id, A can be a pointer to a derived
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001277 // class pointed to by the deduced A.
1278 //
1279 // More importantly:
Mike Stump1eb44332009-09-09 15:08:12 +00001280 // These alternatives are considered only if type deduction would
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001281 // otherwise fail.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001282 if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
1283 // We cannot inspect base classes as part of deduction when the type
1284 // is incomplete, so either instantiate any templates necessary to
1285 // complete the type, or skip over it if it cannot be completed.
John McCall5769d612010-02-08 23:07:23 +00001286 if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001287 return Result;
1288
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001289 // Use data recursion to crawl through the list of base classes.
Mike Stump1eb44332009-09-09 15:08:12 +00001290 // Visited contains the set of nodes we have already visited, while
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001291 // ToVisit is our stack of records that we still need to visit.
1292 llvm::SmallPtrSet<const RecordType *, 8> Visited;
1293 llvm::SmallVector<const RecordType *, 8> ToVisit;
1294 ToVisit.push_back(RecordT);
1295 bool Successful = false;
Douglas Gregor053105d2010-11-02 00:02:34 +00001296 llvm::SmallVectorImpl<DeducedTemplateArgument> DeducedOrig(0);
1297 DeducedOrig = Deduced;
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001298 while (!ToVisit.empty()) {
1299 // Retrieve the next class in the inheritance hierarchy.
1300 const RecordType *NextT = ToVisit.back();
1301 ToVisit.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001303 // If we have already seen this type, skip it.
1304 if (!Visited.insert(NextT))
1305 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001306
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001307 // If this is a base class, try to perform template argument
1308 // deduction from it.
1309 if (NextT != RecordT) {
1310 Sema::TemplateDeductionResult BaseResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001311 = DeduceTemplateArguments(S, TemplateParams, SpecParam,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001312 QualType(NextT, 0), Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001314 // If template argument deduction for this base was successful,
Douglas Gregor053105d2010-11-02 00:02:34 +00001315 // note that we had some success. Otherwise, ignore any deductions
1316 // from this base class.
1317 if (BaseResult == Sema::TDK_Success) {
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001318 Successful = true;
Douglas Gregor053105d2010-11-02 00:02:34 +00001319 DeducedOrig = Deduced;
1320 }
1321 else
1322 Deduced = DeducedOrig;
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001323 }
Mike Stump1eb44332009-09-09 15:08:12 +00001324
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001325 // Visit base classes
1326 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
1327 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
1328 BaseEnd = Next->bases_end();
Sebastian Redl9994a342009-10-25 17:03:50 +00001329 Base != BaseEnd; ++Base) {
Mike Stump1eb44332009-09-09 15:08:12 +00001330 assert(Base->getType()->isRecordType() &&
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001331 "Base class that isn't a record?");
Ted Kremenek6217b802009-07-29 21:53:49 +00001332 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001333 }
1334 }
Mike Stump1eb44332009-09-09 15:08:12 +00001335
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001336 if (Successful)
1337 return Sema::TDK_Success;
1338 }
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001340 }
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001342 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +00001343 }
1344
Douglas Gregor637a4092009-06-10 23:47:09 +00001345 // T type::*
1346 // T T::*
1347 // T (type::*)()
1348 // type (T::*)()
1349 // type (type::*)(T)
1350 // type (T::*)(T)
1351 // T (type::*)(T)
1352 // T (T::*)()
1353 // T (T::*)(T)
1354 case Type::MemberPointer: {
1355 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
1356 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
1357 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001358 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +00001359
Douglas Gregorf67875d2009-06-12 18:26:56 +00001360 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001361 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001362 MemPtrParam->getPointeeType(),
1363 MemPtrArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +00001364 Info, Deduced,
1365 TDF & TDF_IgnoreQualifiers))
Douglas Gregorf67875d2009-06-12 18:26:56 +00001366 return Result;
1367
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001368 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001369 QualType(MemPtrParam->getClass(), 0),
1370 QualType(MemPtrArg->getClass(), 0),
Douglas Gregor508f1c82009-06-26 23:10:12 +00001371 Info, Deduced, 0);
Douglas Gregor637a4092009-06-10 23:47:09 +00001372 }
1373
Anders Carlsson9a917e42009-06-12 22:56:54 +00001374 // (clang extension)
1375 //
Mike Stump1eb44332009-09-09 15:08:12 +00001376 // type(^)(T)
1377 // T(^)()
1378 // T(^)(T)
Anders Carlsson859ba502009-06-12 16:23:10 +00001379 case Type::BlockPointer: {
1380 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
1381 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Anders Carlsson859ba502009-06-12 16:23:10 +00001383 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001384 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001386 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +00001387 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +00001388 BlockPtrArg->getPointeeType(), Info,
Douglas Gregor508f1c82009-06-26 23:10:12 +00001389 Deduced, 0);
Anders Carlsson859ba502009-06-12 16:23:10 +00001390 }
1391
Douglas Gregor4ac01402011-06-15 16:02:29 +00001392 // (clang extension)
1393 //
1394 // T __attribute__(((ext_vector_type(<integral constant>))))
1395 case Type::ExtVector: {
1396 const ExtVectorType *VectorParam = cast<ExtVectorType>(Param);
1397 if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) {
1398 // Make sure that the vectors have the same number of elements.
1399 if (VectorParam->getNumElements() != VectorArg->getNumElements())
1400 return Sema::TDK_NonDeducedMismatch;
1401
1402 // Perform deduction on the element types.
1403 return DeduceTemplateArguments(S, TemplateParams,
1404 VectorParam->getElementType(),
1405 VectorArg->getElementType(),
1406 Info, Deduced,
1407 TDF);
1408 }
1409
1410 if (const DependentSizedExtVectorType *VectorArg
1411 = dyn_cast<DependentSizedExtVectorType>(Arg)) {
1412 // We can't check the number of elements, since the argument has a
1413 // dependent number of elements. This can only occur during partial
1414 // ordering.
1415
1416 // Perform deduction on the element types.
1417 return DeduceTemplateArguments(S, TemplateParams,
1418 VectorParam->getElementType(),
1419 VectorArg->getElementType(),
1420 Info, Deduced,
1421 TDF);
1422 }
1423
1424 return Sema::TDK_NonDeducedMismatch;
1425 }
1426
1427 // (clang extension)
1428 //
1429 // T __attribute__(((ext_vector_type(N))))
1430 case Type::DependentSizedExtVector: {
1431 const DependentSizedExtVectorType *VectorParam
1432 = cast<DependentSizedExtVectorType>(Param);
1433
1434 if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) {
1435 // Perform deduction on the element types.
1436 if (Sema::TemplateDeductionResult Result
1437 = DeduceTemplateArguments(S, TemplateParams,
1438 VectorParam->getElementType(),
1439 VectorArg->getElementType(),
1440 Info, Deduced,
1441 TDF))
1442 return Result;
1443
1444 // Perform deduction on the vector size, if we can.
1445 NonTypeTemplateParmDecl *NTTP
1446 = getDeducedParameterFromExpr(VectorParam->getSizeExpr());
1447 if (!NTTP)
1448 return Sema::TDK_Success;
1449
1450 llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false);
1451 ArgSize = VectorArg->getNumElements();
1452 return DeduceNonTypeTemplateArgument(S, NTTP, ArgSize, S.Context.IntTy,
1453 false, Info, Deduced);
1454 }
1455
1456 if (const DependentSizedExtVectorType *VectorArg
1457 = dyn_cast<DependentSizedExtVectorType>(Arg)) {
1458 // Perform deduction on the element types.
1459 if (Sema::TemplateDeductionResult Result
1460 = DeduceTemplateArguments(S, TemplateParams,
1461 VectorParam->getElementType(),
1462 VectorArg->getElementType(),
1463 Info, Deduced,
1464 TDF))
1465 return Result;
1466
1467 // Perform deduction on the vector size, if we can.
1468 NonTypeTemplateParmDecl *NTTP
1469 = getDeducedParameterFromExpr(VectorParam->getSizeExpr());
1470 if (!NTTP)
1471 return Sema::TDK_Success;
1472
1473 return DeduceNonTypeTemplateArgument(S, NTTP, VectorArg->getSizeExpr(),
1474 Info, Deduced);
1475 }
1476
1477 return Sema::TDK_NonDeducedMismatch;
1478 }
1479
Douglas Gregor637a4092009-06-10 23:47:09 +00001480 case Type::TypeOfExpr:
1481 case Type::TypeOf:
Douglas Gregor4714c122010-03-31 17:34:00 +00001482 case Type::DependentName:
Douglas Gregor4ac01402011-06-15 16:02:29 +00001483 case Type::UnresolvedUsing:
1484 case Type::Decltype:
1485 case Type::UnaryTransform:
1486 case Type::Auto:
1487 case Type::DependentTemplateSpecialization:
1488 case Type::PackExpansion:
Douglas Gregor637a4092009-06-10 23:47:09 +00001489 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +00001490 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001491 }
1492
1493 // FIXME: Many more cases to go (to go).
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001494 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001495}
1496
Douglas Gregorf67875d2009-06-12 18:26:56 +00001497static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001498DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001499 TemplateParameterList *TemplateParams,
1500 const TemplateArgument &Param,
Douglas Gregor77d6bb92011-01-11 22:21:24 +00001501 TemplateArgument Arg,
John McCall2a7fb272010-08-25 05:32:35 +00001502 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +00001503 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor77d6bb92011-01-11 22:21:24 +00001504 // If the template argument is a pack expansion, perform template argument
1505 // deduction against the pattern of that expansion. This only occurs during
1506 // partial ordering.
1507 if (Arg.isPackExpansion())
1508 Arg = Arg.getPackExpansionPattern();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001509
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001510 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +00001511 case TemplateArgument::Null:
1512 assert(false && "Null template argument in parameter list");
1513 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001514
1515 case TemplateArgument::Type:
Douglas Gregor788cd062009-11-11 01:00:40 +00001516 if (Arg.getKind() == TemplateArgument::Type)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001517 return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
Douglas Gregor788cd062009-11-11 01:00:40 +00001518 Arg.getAsType(), Info, Deduced, 0);
1519 Info.FirstArg = Param;
1520 Info.SecondArg = Arg;
1521 return Sema::TDK_NonDeducedMismatch;
Douglas Gregora7fc9012011-01-05 18:58:31 +00001522
Douglas Gregor788cd062009-11-11 01:00:40 +00001523 case TemplateArgument::Template:
Douglas Gregordb0d4b72009-11-11 23:06:43 +00001524 if (Arg.getKind() == TemplateArgument::Template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001525 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor788cd062009-11-11 01:00:40 +00001526 Param.getAsTemplate(),
Douglas Gregordb0d4b72009-11-11 23:06:43 +00001527 Arg.getAsTemplate(), Info, Deduced);
Douglas Gregor788cd062009-11-11 01:00:40 +00001528 Info.FirstArg = Param;
1529 Info.SecondArg = Arg;
1530 return Sema::TDK_NonDeducedMismatch;
Douglas Gregora7fc9012011-01-05 18:58:31 +00001531
1532 case TemplateArgument::TemplateExpansion:
1533 llvm_unreachable("caller should handle pack expansions");
1534 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001535
Douglas Gregor199d9912009-06-05 00:53:49 +00001536 case TemplateArgument::Declaration:
Douglas Gregor788cd062009-11-11 01:00:40 +00001537 if (Arg.getKind() == TemplateArgument::Declaration &&
1538 Param.getAsDecl()->getCanonicalDecl() ==
1539 Arg.getAsDecl()->getCanonicalDecl())
1540 return Sema::TDK_Success;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001541
Douglas Gregorf67875d2009-06-12 18:26:56 +00001542 Info.FirstArg = Param;
1543 Info.SecondArg = Arg;
1544 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Douglas Gregor199d9912009-06-05 00:53:49 +00001546 case TemplateArgument::Integral:
1547 if (Arg.getKind() == TemplateArgument::Integral) {
Douglas Gregor9d0e4412010-03-26 05:50:28 +00001548 if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral()))
Douglas Gregorf67875d2009-06-12 18:26:56 +00001549 return Sema::TDK_Success;
1550
1551 Info.FirstArg = Param;
1552 Info.SecondArg = Arg;
1553 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +00001554 }
Douglas Gregorf67875d2009-06-12 18:26:56 +00001555
1556 if (Arg.getKind() == TemplateArgument::Expression) {
1557 Info.FirstArg = Param;
1558 Info.SecondArg = Arg;
1559 return Sema::TDK_NonDeducedMismatch;
1560 }
Douglas Gregor199d9912009-06-05 00:53:49 +00001561
Douglas Gregorf67875d2009-06-12 18:26:56 +00001562 Info.FirstArg = Param;
1563 Info.SecondArg = Arg;
1564 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Douglas Gregor199d9912009-06-05 00:53:49 +00001566 case TemplateArgument::Expression: {
Mike Stump1eb44332009-09-09 15:08:12 +00001567 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +00001568 = getDeducedParameterFromExpr(Param.getAsExpr())) {
1569 if (Arg.getKind() == TemplateArgument::Integral)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001570 return DeduceNonTypeTemplateArgument(S, NTTP,
Mike Stump1eb44332009-09-09 15:08:12 +00001571 *Arg.getAsIntegral(),
Douglas Gregor9d0e4412010-03-26 05:50:28 +00001572 Arg.getIntegralType(),
Douglas Gregor02024a92010-03-28 02:42:43 +00001573 /*ArrayBound=*/false,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001574 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +00001575 if (Arg.getKind() == TemplateArgument::Expression)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001576 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +00001577 Info, Deduced);
Douglas Gregor15755cb2009-11-13 23:45:44 +00001578 if (Arg.getKind() == TemplateArgument::Declaration)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001579 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
Douglas Gregor15755cb2009-11-13 23:45:44 +00001580 Info, Deduced);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001581
Douglas Gregorf67875d2009-06-12 18:26:56 +00001582 Info.FirstArg = Param;
1583 Info.SecondArg = Arg;
1584 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +00001585 }
Mike Stump1eb44332009-09-09 15:08:12 +00001586
Douglas Gregor199d9912009-06-05 00:53:49 +00001587 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +00001588 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001589 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001590 case TemplateArgument::Pack:
Douglas Gregor20a55e22010-12-22 18:17:10 +00001591 llvm_unreachable("Argument packs should be expanded by the caller!");
Douglas Gregor199d9912009-06-05 00:53:49 +00001592 }
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Douglas Gregorf67875d2009-06-12 18:26:56 +00001594 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001595}
1596
Douglas Gregor20a55e22010-12-22 18:17:10 +00001597/// \brief Determine whether there is a template argument to be used for
1598/// deduction.
1599///
1600/// This routine "expands" argument packs in-place, overriding its input
1601/// parameters so that \c Args[ArgIdx] will be the available template argument.
1602///
1603/// \returns true if there is another template argument (which will be at
1604/// \c Args[ArgIdx]), false otherwise.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001605static bool hasTemplateArgumentForDeduction(const TemplateArgument *&Args,
Douglas Gregor20a55e22010-12-22 18:17:10 +00001606 unsigned &ArgIdx,
1607 unsigned &NumArgs) {
1608 if (ArgIdx == NumArgs)
1609 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001610
Douglas Gregor20a55e22010-12-22 18:17:10 +00001611 const TemplateArgument &Arg = Args[ArgIdx];
1612 if (Arg.getKind() != TemplateArgument::Pack)
1613 return true;
1614
1615 assert(ArgIdx == NumArgs - 1 && "Pack not at the end of argument list?");
1616 Args = Arg.pack_begin();
1617 NumArgs = Arg.pack_size();
1618 ArgIdx = 0;
1619 return ArgIdx < NumArgs;
1620}
1621
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001622/// \brief Determine whether the given set of template arguments has a pack
1623/// expansion that is not the last template argument.
1624static bool hasPackExpansionBeforeEnd(const TemplateArgument *Args,
1625 unsigned NumArgs) {
1626 unsigned ArgIdx = 0;
1627 while (ArgIdx < NumArgs) {
1628 const TemplateArgument &Arg = Args[ArgIdx];
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001629
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001630 // Unwrap argument packs.
1631 if (Args[ArgIdx].getKind() == TemplateArgument::Pack) {
1632 Args = Arg.pack_begin();
1633 NumArgs = Arg.pack_size();
1634 ArgIdx = 0;
1635 continue;
1636 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001637
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001638 ++ArgIdx;
1639 if (ArgIdx == NumArgs)
1640 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001641
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001642 if (Arg.isPackExpansion())
1643 return true;
1644 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001645
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001646 return false;
1647}
1648
Douglas Gregor20a55e22010-12-22 18:17:10 +00001649static Sema::TemplateDeductionResult
1650DeduceTemplateArguments(Sema &S,
1651 TemplateParameterList *TemplateParams,
1652 const TemplateArgument *Params, unsigned NumParams,
1653 const TemplateArgument *Args, unsigned NumArgs,
1654 TemplateDeductionInfo &Info,
Douglas Gregor0972c862010-12-22 18:55:49 +00001655 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1656 bool NumberOfArgumentsMustMatch) {
Douglas Gregore02e2622010-12-22 21:19:48 +00001657 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001658 // If the template argument list of P contains a pack expansion that is not
1659 // the last template argument, the entire template argument list is a
Douglas Gregore02e2622010-12-22 21:19:48 +00001660 // non-deduced context.
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001661 if (hasPackExpansionBeforeEnd(Params, NumParams))
1662 return Sema::TDK_Success;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001663
Douglas Gregore02e2622010-12-22 21:19:48 +00001664 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001665 // If P has a form that contains <T> or <i>, then each argument Pi of the
1666 // respective template argument list P is compared with the corresponding
Douglas Gregore02e2622010-12-22 21:19:48 +00001667 // argument Ai of the corresponding template argument list of A.
Douglas Gregor20a55e22010-12-22 18:17:10 +00001668 unsigned ArgIdx = 0, ParamIdx = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001669 for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams);
Douglas Gregor20a55e22010-12-22 18:17:10 +00001670 ++ParamIdx) {
1671 if (!Params[ParamIdx].isPackExpansion()) {
Douglas Gregore02e2622010-12-22 21:19:48 +00001672 // The simple case: deduce template arguments by matching Pi and Ai.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001673
Douglas Gregor20a55e22010-12-22 18:17:10 +00001674 // Check whether we have enough arguments.
1675 if (!hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
Douglas Gregor3cae5c92011-01-10 20:53:55 +00001676 return NumberOfArgumentsMustMatch? Sema::TDK_NonDeducedMismatch
Douglas Gregor0972c862010-12-22 18:55:49 +00001677 : Sema::TDK_Success;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001678
Douglas Gregor77d6bb92011-01-11 22:21:24 +00001679 if (Args[ArgIdx].isPackExpansion()) {
1680 // FIXME: We follow the logic of C++0x [temp.deduct.type]p22 here,
1681 // but applied to pack expansions that are template arguments.
1682 return Sema::TDK_NonDeducedMismatch;
1683 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001684
Douglas Gregore02e2622010-12-22 21:19:48 +00001685 // Perform deduction for this Pi/Ai pair.
Douglas Gregor20a55e22010-12-22 18:17:10 +00001686 if (Sema::TemplateDeductionResult Result
Douglas Gregor77d6bb92011-01-11 22:21:24 +00001687 = DeduceTemplateArguments(S, TemplateParams,
1688 Params[ParamIdx], Args[ArgIdx],
1689 Info, Deduced))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001690 return Result;
1691
Douglas Gregor20a55e22010-12-22 18:17:10 +00001692 // Move to the next argument.
1693 ++ArgIdx;
1694 continue;
1695 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001696
Douglas Gregore02e2622010-12-22 21:19:48 +00001697 // The parameter is a pack expansion.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001698
Douglas Gregore02e2622010-12-22 21:19:48 +00001699 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001700 // If Pi is a pack expansion, then the pattern of Pi is compared with
1701 // each remaining argument in the template argument list of A. Each
1702 // comparison deduces template arguments for subsequent positions in the
Douglas Gregore02e2622010-12-22 21:19:48 +00001703 // template parameter packs expanded by Pi.
1704 TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001705
Douglas Gregore02e2622010-12-22 21:19:48 +00001706 // Compute the set of template parameter indices that correspond to
1707 // parameter packs expanded by the pack expansion.
1708 llvm::SmallVector<unsigned, 2> PackIndices;
1709 {
1710 llvm::BitVector SawIndices(TemplateParams->size());
1711 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1712 S.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1713 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
1714 unsigned Depth, Index;
1715 llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
1716 if (Depth == 0 && !SawIndices[Index]) {
1717 SawIndices[Index] = true;
1718 PackIndices.push_back(Index);
1719 }
1720 }
1721 }
1722 assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001723
Douglas Gregore02e2622010-12-22 21:19:48 +00001724 // FIXME: If there are no remaining arguments, we can bail out early
1725 // and set any deduced parameter packs to an empty argument pack.
1726 // The latter part of this is a (minor) correctness issue.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001727
Douglas Gregore02e2622010-12-22 21:19:48 +00001728 // Save the deduced template arguments for each parameter pack expanded
1729 // by this pack expansion, then clear out the deduction.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001730 llvm::SmallVector<DeducedTemplateArgument, 2>
Douglas Gregore02e2622010-12-22 21:19:48 +00001731 SavedPacks(PackIndices.size());
Douglas Gregor54293852011-01-10 17:35:05 +00001732 llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2>
1733 NewlyDeducedPacks(PackIndices.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001734 PrepareArgumentPackDeduction(S, Deduced, PackIndices, SavedPacks,
Douglas Gregor54293852011-01-10 17:35:05 +00001735 NewlyDeducedPacks);
Douglas Gregore02e2622010-12-22 21:19:48 +00001736
1737 // Keep track of the deduced template arguments for each parameter pack
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001738 // expanded by this pack expansion (the outer index) and for each
Douglas Gregore02e2622010-12-22 21:19:48 +00001739 // template argument (the inner SmallVectors).
Douglas Gregore02e2622010-12-22 21:19:48 +00001740 bool HasAnyArguments = false;
1741 while (hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) {
1742 HasAnyArguments = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001743
Douglas Gregore02e2622010-12-22 21:19:48 +00001744 // Deduce template arguments from the pattern.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001745 if (Sema::TemplateDeductionResult Result
Douglas Gregore02e2622010-12-22 21:19:48 +00001746 = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx],
1747 Info, Deduced))
1748 return Result;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001749
Douglas Gregore02e2622010-12-22 21:19:48 +00001750 // Capture the deduced template arguments for each parameter pack expanded
1751 // by this pack expansion, add them to the list of arguments we've deduced
1752 // for that pack, then clear out the deduced argument.
1753 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
1754 DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
1755 if (!DeducedArg.isNull()) {
1756 NewlyDeducedPacks[I].push_back(DeducedArg);
1757 DeducedArg = DeducedTemplateArgument();
1758 }
1759 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001760
Douglas Gregore02e2622010-12-22 21:19:48 +00001761 ++ArgIdx;
1762 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001763
Douglas Gregore02e2622010-12-22 21:19:48 +00001764 // Build argument packs for each of the parameter packs expanded by this
1765 // pack expansion.
Douglas Gregor0216f812011-01-10 17:53:52 +00001766 if (Sema::TemplateDeductionResult Result
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001767 = FinishArgumentPackDeduction(S, TemplateParams, HasAnyArguments,
Douglas Gregor0216f812011-01-10 17:53:52 +00001768 Deduced, PackIndices, SavedPacks,
1769 NewlyDeducedPacks, Info))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001770 return Result;
Douglas Gregor20a55e22010-12-22 18:17:10 +00001771 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001772
Douglas Gregor20a55e22010-12-22 18:17:10 +00001773 // If there is an argument remaining, then we had too many arguments.
Douglas Gregor0972c862010-12-22 18:55:49 +00001774 if (NumberOfArgumentsMustMatch &&
1775 hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
Douglas Gregor3cae5c92011-01-10 20:53:55 +00001776 return Sema::TDK_NonDeducedMismatch;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001777
Douglas Gregor20a55e22010-12-22 18:17:10 +00001778 return Sema::TDK_Success;
1779}
1780
Mike Stump1eb44332009-09-09 15:08:12 +00001781static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001782DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001783 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001784 const TemplateArgumentList &ParamList,
1785 const TemplateArgumentList &ArgList,
John McCall2a7fb272010-08-25 05:32:35 +00001786 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +00001787 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001788 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor20a55e22010-12-22 18:17:10 +00001789 ParamList.data(), ParamList.size(),
1790 ArgList.data(), ArgList.size(),
1791 Info, Deduced);
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001792}
1793
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001794/// \brief Determine whether two template arguments are the same.
Mike Stump1eb44332009-09-09 15:08:12 +00001795static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001796 const TemplateArgument &X,
1797 const TemplateArgument &Y) {
1798 if (X.getKind() != Y.getKind())
1799 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001800
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001801 switch (X.getKind()) {
1802 case TemplateArgument::Null:
1803 assert(false && "Comparing NULL template argument");
1804 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001805
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001806 case TemplateArgument::Type:
1807 return Context.getCanonicalType(X.getAsType()) ==
1808 Context.getCanonicalType(Y.getAsType());
Mike Stump1eb44332009-09-09 15:08:12 +00001809
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001810 case TemplateArgument::Declaration:
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001811 return X.getAsDecl()->getCanonicalDecl() ==
1812 Y.getAsDecl()->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001813
Douglas Gregor788cd062009-11-11 01:00:40 +00001814 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00001815 case TemplateArgument::TemplateExpansion:
1816 return Context.getCanonicalTemplateName(
1817 X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() ==
1818 Context.getCanonicalTemplateName(
1819 Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001820
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001821 case TemplateArgument::Integral:
1822 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Douglas Gregor788cd062009-11-11 01:00:40 +00001824 case TemplateArgument::Expression: {
1825 llvm::FoldingSetNodeID XID, YID;
1826 X.getAsExpr()->Profile(XID, Context, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001827 Y.getAsExpr()->Profile(YID, Context, true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001828 return XID == YID;
1829 }
Mike Stump1eb44332009-09-09 15:08:12 +00001830
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001831 case TemplateArgument::Pack:
1832 if (X.pack_size() != Y.pack_size())
1833 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001834
1835 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
1836 XPEnd = X.pack_end(),
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001837 YP = Y.pack_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001838 XP != XPEnd; ++XP, ++YP)
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001839 if (!isSameTemplateArg(Context, *XP, *YP))
1840 return false;
1841
1842 return true;
1843 }
1844
1845 return false;
1846}
1847
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001848/// \brief Allocate a TemplateArgumentLoc where all locations have
1849/// been initialized to the given location.
1850///
1851/// \param S The semantic analysis object.
1852///
1853/// \param The template argument we are producing template argument
1854/// location information for.
1855///
1856/// \param NTTPType For a declaration template argument, the type of
1857/// the non-type template parameter that corresponds to this template
1858/// argument.
1859///
1860/// \param Loc The source location to use for the resulting template
1861/// argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001862static TemplateArgumentLoc
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001863getTrivialTemplateArgumentLoc(Sema &S,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001864 const TemplateArgument &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001865 QualType NTTPType,
1866 SourceLocation Loc) {
1867 switch (Arg.getKind()) {
1868 case TemplateArgument::Null:
1869 llvm_unreachable("Can't get a NULL template argument here");
1870 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001871
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001872 case TemplateArgument::Type:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001873 return TemplateArgumentLoc(Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001874 S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001875
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001876 case TemplateArgument::Declaration: {
1877 Expr *E
Douglas Gregorba68eca2011-01-05 17:40:24 +00001878 = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001879 .takeAs<Expr>();
1880 return TemplateArgumentLoc(TemplateArgument(E), E);
1881 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001882
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001883 case TemplateArgument::Integral: {
1884 Expr *E
Douglas Gregorba68eca2011-01-05 17:40:24 +00001885 = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001886 return TemplateArgumentLoc(TemplateArgument(E), E);
1887 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001888
Douglas Gregorb6744ef2011-03-02 17:09:35 +00001889 case TemplateArgument::Template:
1890 case TemplateArgument::TemplateExpansion: {
1891 NestedNameSpecifierLocBuilder Builder;
1892 TemplateName Template = Arg.getAsTemplate();
1893 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
1894 Builder.MakeTrivial(S.Context, DTN->getQualifier(), Loc);
1895 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
1896 Builder.MakeTrivial(S.Context, QTN->getQualifier(), Loc);
1897
1898 if (Arg.getKind() == TemplateArgument::Template)
1899 return TemplateArgumentLoc(Arg,
1900 Builder.getWithLocInContext(S.Context),
1901 Loc);
1902
1903
1904 return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(S.Context),
1905 Loc, Loc);
1906 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001907
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001908 case TemplateArgument::Expression:
1909 return TemplateArgumentLoc(Arg, Arg.getAsExpr());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001910
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001911 case TemplateArgument::Pack:
1912 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo());
1913 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001914
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001915 return TemplateArgumentLoc();
1916}
1917
1918
1919/// \brief Convert the given deduced template argument and add it to the set of
1920/// fully-converted template arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001921static bool ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001922 DeducedTemplateArgument Arg,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001923 NamedDecl *Template,
1924 QualType NTTPType,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001925 unsigned ArgumentPackIndex,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001926 TemplateDeductionInfo &Info,
1927 bool InFunctionTemplate,
1928 llvm::SmallVectorImpl<TemplateArgument> &Output) {
1929 if (Arg.getKind() == TemplateArgument::Pack) {
1930 // This is a template argument pack, so check each of its arguments against
1931 // the template parameter.
1932 llvm::SmallVector<TemplateArgument, 2> PackedArgsBuilder;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001933 for (TemplateArgument::pack_iterator PA = Arg.pack_begin(),
Douglas Gregor135ffa72011-01-05 21:00:53 +00001934 PAEnd = Arg.pack_end();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001935 PA != PAEnd; ++PA) {
Douglas Gregord53e16a2011-01-05 20:52:18 +00001936 // When converting the deduced template argument, append it to the
1937 // general output list. We need to do this so that the template argument
1938 // checking logic has all of the prior template arguments available.
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001939 DeducedTemplateArgument InnerArg(*PA);
1940 InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001941 if (ConvertDeducedTemplateArgument(S, Param, InnerArg, Template,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001942 NTTPType, PackedArgsBuilder.size(),
1943 Info, InFunctionTemplate, Output))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001944 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001945
Douglas Gregord53e16a2011-01-05 20:52:18 +00001946 // Move the converted template argument into our argument pack.
1947 PackedArgsBuilder.push_back(Output.back());
1948 Output.pop_back();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001949 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001950
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001951 // Create the resulting argument pack.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001952 Output.push_back(TemplateArgument::CreatePackCopy(S.Context,
Douglas Gregor203e6a32011-01-11 23:09:57 +00001953 PackedArgsBuilder.data(),
1954 PackedArgsBuilder.size()));
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001955 return false;
1956 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001957
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001958 // Convert the deduced template argument into a template
1959 // argument that we can check, almost as if the user had written
1960 // the template argument explicitly.
1961 TemplateArgumentLoc ArgLoc = getTrivialTemplateArgumentLoc(S, Arg, NTTPType,
1962 Info.getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001963
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001964 // Check the template argument, converting it as necessary.
1965 return S.CheckTemplateArgument(Param, ArgLoc,
1966 Template,
1967 Template->getLocation(),
1968 Template->getSourceRange().getEnd(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001969 ArgumentPackIndex,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001970 Output,
1971 InFunctionTemplate
1972 ? (Arg.wasDeducedFromArrayBound()
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001973 ? Sema::CTAK_DeducedFromArrayBound
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001974 : Sema::CTAK_Deduced)
1975 : Sema::CTAK_Specified);
1976}
1977
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001978/// Complete template argument deduction for a class template partial
1979/// specialization.
1980static Sema::TemplateDeductionResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001981FinishTemplateArgumentDeduction(Sema &S,
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001982 ClassTemplatePartialSpecializationDecl *Partial,
1983 const TemplateArgumentList &TemplateArgs,
1984 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
John McCall2a7fb272010-08-25 05:32:35 +00001985 TemplateDeductionInfo &Info) {
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001986 // Trap errors.
1987 Sema::SFINAETrap Trap(S);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001988
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001989 Sema::ContextRAII SavedContext(S, Partial);
1990
1991 // C++ [temp.deduct.type]p2:
1992 // [...] or if any template argument remains neither deduced nor
1993 // explicitly specified, template argument deduction fails.
Douglas Gregor910f8002010-11-07 23:05:16 +00001994 llvm::SmallVector<TemplateArgument, 4> Builder;
Douglas Gregor033a3ca2011-01-04 22:23:38 +00001995 TemplateParameterList *PartialParams = Partial->getTemplateParameters();
1996 for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) {
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001997 NamedDecl *Param = PartialParams->getParam(I);
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001998 if (Deduced[I].isNull()) {
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001999 Info.Param = makeTemplateParameter(Param);
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002000 return Sema::TDK_Incomplete;
2001 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002002
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002003 // We have deduced this argument, so it still needs to be
2004 // checked and converted.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002005
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002006 // First, for a non-type template parameter type that is
2007 // initialized by a declaration, we need the type of the
2008 // corresponding non-type template parameter.
2009 QualType NTTPType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002010 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregord53e16a2011-01-05 20:52:18 +00002011 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002012 NTTPType = NTTP->getType();
Douglas Gregord53e16a2011-01-05 20:52:18 +00002013 if (NTTPType->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002014 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregord53e16a2011-01-05 20:52:18 +00002015 Builder.data(), Builder.size());
2016 NTTPType = S.SubstType(NTTPType,
2017 MultiLevelTemplateArgumentList(TemplateArgs),
2018 NTTP->getLocation(),
2019 NTTP->getDeclName());
2020 if (NTTPType.isNull()) {
2021 Info.Param = makeTemplateParameter(Param);
2022 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002023 Info.reset(TemplateArgumentList::CreateCopy(S.Context,
2024 Builder.data(),
Douglas Gregord53e16a2011-01-05 20:52:18 +00002025 Builder.size()));
2026 return Sema::TDK_SubstitutionFailure;
2027 }
2028 }
2029 }
2030
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002031 if (ConvertDeducedTemplateArgument(S, Param, Deduced[I],
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002032 Partial, NTTPType, 0, Info, false,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002033 Builder)) {
2034 Info.Param = makeTemplateParameter(Param);
2035 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002036 Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
2037 Builder.size()));
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002038 return Sema::TDK_SubstitutionFailure;
2039 }
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002040 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002041
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002042 // Form the template argument list from the deduced template arguments.
2043 TemplateArgumentList *DeducedArgumentList
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002044 = TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002045 Builder.size());
2046
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002047 Info.reset(DeducedArgumentList);
2048
2049 // Substitute the deduced template arguments into the template
2050 // arguments of the class template partial specialization, and
2051 // verify that the instantiated template arguments are both valid
2052 // and are equivalent to the template arguments originally provided
2053 // to the class template.
John McCall2a7fb272010-08-25 05:32:35 +00002054 LocalInstantiationScope InstScope(S);
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002055 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
2056 const TemplateArgumentLoc *PartialTemplateArgs
2057 = Partial->getTemplateArgsAsWritten();
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002058
2059 // Note that we don't provide the langle and rangle locations.
2060 TemplateArgumentListInfo InstArgs;
2061
Douglas Gregore02e2622010-12-22 21:19:48 +00002062 if (S.Subst(PartialTemplateArgs,
2063 Partial->getNumTemplateArgsAsWritten(),
2064 InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
2065 unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx;
2066 if (ParamIdx >= Partial->getTemplateParameters()->size())
2067 ParamIdx = Partial->getTemplateParameters()->size() - 1;
2068
2069 Decl *Param
2070 = const_cast<NamedDecl *>(
2071 Partial->getTemplateParameters()->getParam(ParamIdx));
2072 Info.Param = makeTemplateParameter(Param);
2073 Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument();
2074 return Sema::TDK_SubstitutionFailure;
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002075 }
2076
Douglas Gregor910f8002010-11-07 23:05:16 +00002077 llvm::SmallVector<TemplateArgument, 4> ConvertedInstArgs;
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002078 if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002079 InstArgs, false, ConvertedInstArgs))
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002080 return Sema::TDK_SubstitutionFailure;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002081
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002082 TemplateParameterList *TemplateParams
2083 = ClassTemplate->getTemplateParameters();
2084 for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002085 TemplateArgument InstArg = ConvertedInstArgs.data()[I];
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002086 if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
Douglas Gregor2fdc5e82011-01-05 00:13:17 +00002087 Info.Param = makeTemplateParameter(TemplateParams->getParam(I));
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002088 Info.FirstArg = TemplateArgs[I];
2089 Info.SecondArg = InstArg;
2090 return Sema::TDK_NonDeducedMismatch;
2091 }
2092 }
2093
2094 if (Trap.hasErrorOccurred())
2095 return Sema::TDK_SubstitutionFailure;
2096
2097 return Sema::TDK_Success;
2098}
2099
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00002100/// \brief Perform template argument deduction to determine whether
2101/// the given template arguments match the given class template
2102/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +00002103Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +00002104Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +00002105 const TemplateArgumentList &TemplateArgs,
2106 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00002107 // C++ [temp.class.spec.match]p2:
2108 // A partial specialization matches a given actual template
2109 // argument list if the template arguments of the partial
2110 // specialization can be deduced from the actual template argument
2111 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +00002112 SFINAETrap Trap(*this);
Douglas Gregor02024a92010-03-28 02:42:43 +00002113 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00002114 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +00002115 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002116 = ::DeduceTemplateArguments(*this,
Douglas Gregorf67875d2009-06-12 18:26:56 +00002117 Partial->getTemplateParameters(),
Mike Stump1eb44332009-09-09 15:08:12 +00002118 Partial->getTemplateArgs(),
Douglas Gregorf67875d2009-06-12 18:26:56 +00002119 TemplateArgs, Info, Deduced))
2120 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +00002121
Douglas Gregor637a4092009-06-10 23:47:09 +00002122 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
Douglas Gregor9b623632010-10-12 23:32:35 +00002123 Deduced.data(), Deduced.size(), Info);
Douglas Gregor637a4092009-06-10 23:47:09 +00002124 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +00002125 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +00002126
Douglas Gregorbb260412009-06-14 08:02:22 +00002127 if (Trap.hasErrorOccurred())
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002128 return Sema::TDK_SubstitutionFailure;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002129
2130 return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002131 Deduced, Info);
Douglas Gregor0b9247f2009-06-04 00:03:07 +00002132}
Douglas Gregor031a5882009-06-13 00:26:55 +00002133
Douglas Gregor41128772009-06-26 23:27:24 +00002134/// \brief Determine whether the given type T is a simple-template-id type.
2135static bool isSimpleTemplateIdType(QualType T) {
Mike Stump1eb44332009-09-09 15:08:12 +00002136 if (const TemplateSpecializationType *Spec
John McCall183700f2009-09-21 23:43:11 +00002137 = T->getAs<TemplateSpecializationType>())
Douglas Gregor41128772009-06-26 23:27:24 +00002138 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002139
Douglas Gregor41128772009-06-26 23:27:24 +00002140 return false;
2141}
Douglas Gregor83314aa2009-07-08 20:55:45 +00002142
2143/// \brief Substitute the explicitly-provided template arguments into the
2144/// given function template according to C++ [temp.arg.explicit].
2145///
2146/// \param FunctionTemplate the function template into which the explicit
2147/// template arguments will be substituted.
2148///
Mike Stump1eb44332009-09-09 15:08:12 +00002149/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor83314aa2009-07-08 20:55:45 +00002150/// arguments.
2151///
Mike Stump1eb44332009-09-09 15:08:12 +00002152/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor83314aa2009-07-08 20:55:45 +00002153/// with the converted and checked explicit template arguments.
2154///
Mike Stump1eb44332009-09-09 15:08:12 +00002155/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor83314aa2009-07-08 20:55:45 +00002156/// parameters.
2157///
2158/// \param FunctionType if non-NULL, the result type of the function template
2159/// will also be instantiated and the pointed-to value will be updated with
2160/// the instantiated function type.
2161///
2162/// \param Info if substitution fails for any reason, this object will be
2163/// populated with more information about the failure.
2164///
2165/// \returns TDK_Success if substitution was successful, or some failure
2166/// condition.
2167Sema::TemplateDeductionResult
2168Sema::SubstituteExplicitTemplateArguments(
2169 FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor67714232011-03-03 02:41:12 +00002170 TemplateArgumentListInfo &ExplicitTemplateArgs,
Douglas Gregor02024a92010-03-28 02:42:43 +00002171 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002172 llvm::SmallVectorImpl<QualType> &ParamTypes,
2173 QualType *FunctionType,
2174 TemplateDeductionInfo &Info) {
2175 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2176 TemplateParameterList *TemplateParams
2177 = FunctionTemplate->getTemplateParameters();
2178
John McCalld5532b62009-11-23 01:53:49 +00002179 if (ExplicitTemplateArgs.size() == 0) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00002180 // No arguments to substitute; just copy over the parameter types and
2181 // fill in the function type.
2182 for (FunctionDecl::param_iterator P = Function->param_begin(),
2183 PEnd = Function->param_end();
2184 P != PEnd;
2185 ++P)
2186 ParamTypes.push_back((*P)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002187
Douglas Gregor83314aa2009-07-08 20:55:45 +00002188 if (FunctionType)
2189 *FunctionType = Function->getType();
2190 return TDK_Success;
2191 }
Mike Stump1eb44332009-09-09 15:08:12 +00002192
Douglas Gregor83314aa2009-07-08 20:55:45 +00002193 // Substitution of the explicit template arguments into a function template
2194 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002195 SFINAETrap Trap(*this);
2196
Douglas Gregor83314aa2009-07-08 20:55:45 +00002197 // C++ [temp.arg.explicit]p3:
Mike Stump1eb44332009-09-09 15:08:12 +00002198 // Template arguments that are present shall be specified in the
2199 // declaration order of their corresponding template-parameters. The
Douglas Gregor83314aa2009-07-08 20:55:45 +00002200 // template argument list shall not specify more template-arguments than
Mike Stump1eb44332009-09-09 15:08:12 +00002201 // there are corresponding template-parameters.
Douglas Gregor910f8002010-11-07 23:05:16 +00002202 llvm::SmallVector<TemplateArgument, 4> Builder;
Mike Stump1eb44332009-09-09 15:08:12 +00002203
2204 // Enter a new template instantiation context where we check the
Douglas Gregor83314aa2009-07-08 20:55:45 +00002205 // explicitly-specified template arguments against this function template,
2206 // and then substitute them into the function parameter types.
Mike Stump1eb44332009-09-09 15:08:12 +00002207 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00002208 FunctionTemplate, Deduced.data(), Deduced.size(),
Douglas Gregor9b623632010-10-12 23:32:35 +00002209 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution,
2210 Info);
Douglas Gregor83314aa2009-07-08 20:55:45 +00002211 if (Inst)
2212 return TDK_InstantiationDepth;
Mike Stump1eb44332009-09-09 15:08:12 +00002213
Douglas Gregor83314aa2009-07-08 20:55:45 +00002214 if (CheckTemplateArgumentList(FunctionTemplate,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002215 SourceLocation(),
John McCalld5532b62009-11-23 01:53:49 +00002216 ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002217 true,
Douglas Gregorf1a84452010-05-08 19:15:54 +00002218 Builder) || Trap.hasErrorOccurred()) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002219 unsigned Index = Builder.size();
Douglas Gregorfe52c912010-05-09 01:26:06 +00002220 if (Index >= TemplateParams->size())
2221 Index = TemplateParams->size() - 1;
2222 Info.Param = makeTemplateParameter(TemplateParams->getParam(Index));
Douglas Gregor83314aa2009-07-08 20:55:45 +00002223 return TDK_InvalidExplicitArguments;
Douglas Gregorf1a84452010-05-08 19:15:54 +00002224 }
Mike Stump1eb44332009-09-09 15:08:12 +00002225
Douglas Gregor83314aa2009-07-08 20:55:45 +00002226 // Form the template argument list from the explicitly-specified
2227 // template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00002228 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor910f8002010-11-07 23:05:16 +00002229 = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
Douglas Gregor83314aa2009-07-08 20:55:45 +00002230 Info.reset(ExplicitArgumentList);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002231
John McCalldf41f182010-10-12 19:40:14 +00002232 // Template argument deduction and the final substitution should be
2233 // done in the context of the templated declaration. Explicit
2234 // argument substitution, on the other hand, needs to happen in the
2235 // calling context.
2236 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
2237
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002238 // If we deduced template arguments for a template parameter pack,
Douglas Gregord3731192011-01-10 07:32:04 +00002239 // note that the template argument pack is partially substituted and record
2240 // the explicit template arguments. They'll be used as part of deduction
2241 // for this template parameter pack.
Douglas Gregord3731192011-01-10 07:32:04 +00002242 for (unsigned I = 0, N = Builder.size(); I != N; ++I) {
2243 const TemplateArgument &Arg = Builder[I];
2244 if (Arg.getKind() == TemplateArgument::Pack) {
Douglas Gregord3731192011-01-10 07:32:04 +00002245 CurrentInstantiationScope->SetPartiallySubstitutedPack(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002246 TemplateParams->getParam(I),
Douglas Gregord3731192011-01-10 07:32:04 +00002247 Arg.pack_begin(),
2248 Arg.pack_size());
2249 break;
2250 }
2251 }
2252
Douglas Gregor83314aa2009-07-08 20:55:45 +00002253 // Instantiate the types of each of the function parameters given the
2254 // explicitly-specified template arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002255 if (SubstParmTypes(Function->getLocation(),
Douglas Gregora009b592011-01-07 00:20:55 +00002256 Function->param_begin(), Function->getNumParams(),
2257 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
2258 ParamTypes))
2259 return TDK_SubstitutionFailure;
Douglas Gregor83314aa2009-07-08 20:55:45 +00002260
2261 // If the caller wants a full function type back, instantiate the return
2262 // type and form that function type.
2263 if (FunctionType) {
2264 // FIXME: exception-specifications?
Mike Stump1eb44332009-09-09 15:08:12 +00002265 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00002266 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor83314aa2009-07-08 20:55:45 +00002267 assert(Proto && "Function template does not have a prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00002268
2269 QualType ResultType
Douglas Gregor357bbd02009-08-28 20:50:45 +00002270 = SubstType(Proto->getResultType(),
2271 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
2272 Function->getTypeSpecStartLoc(),
2273 Function->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00002274 if (ResultType.isNull() || Trap.hasErrorOccurred())
2275 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00002276
2277 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002278 ParamTypes.data(), ParamTypes.size(),
2279 Proto->isVariadic(),
2280 Proto->getTypeQuals(),
Douglas Gregorc938c162011-01-26 05:01:58 +00002281 Proto->getRefQualifier(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00002282 Function->getLocation(),
Eli Friedmanfa869542010-08-05 02:54:05 +00002283 Function->getDeclName(),
2284 Proto->getExtInfo());
Douglas Gregor83314aa2009-07-08 20:55:45 +00002285 if (FunctionType->isNull() || Trap.hasErrorOccurred())
2286 return TDK_SubstitutionFailure;
2287 }
Mike Stump1eb44332009-09-09 15:08:12 +00002288
Douglas Gregor83314aa2009-07-08 20:55:45 +00002289 // C++ [temp.arg.explicit]p2:
Mike Stump1eb44332009-09-09 15:08:12 +00002290 // Trailing template arguments that can be deduced (14.8.2) may be
2291 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor83314aa2009-07-08 20:55:45 +00002292 // template arguments can be deduced, they may all be omitted; in this
2293 // case, the empty template argument list <> itself may also be omitted.
2294 //
Douglas Gregord3731192011-01-10 07:32:04 +00002295 // Take all of the explicitly-specified arguments and put them into
2296 // the set of deduced template arguments. Explicitly-specified
2297 // parameter packs, however, will be set to NULL since the deduction
2298 // mechanisms handle explicitly-specified argument packs directly.
Douglas Gregor83314aa2009-07-08 20:55:45 +00002299 Deduced.reserve(TemplateParams->size());
Douglas Gregord3731192011-01-10 07:32:04 +00002300 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) {
2301 const TemplateArgument &Arg = ExplicitArgumentList->get(I);
2302 if (Arg.getKind() == TemplateArgument::Pack)
2303 Deduced.push_back(DeducedTemplateArgument());
2304 else
2305 Deduced.push_back(Arg);
2306 }
Mike Stump1eb44332009-09-09 15:08:12 +00002307
Douglas Gregor83314aa2009-07-08 20:55:45 +00002308 return TDK_Success;
2309}
2310
Mike Stump1eb44332009-09-09 15:08:12 +00002311/// \brief Finish template argument deduction for a function template,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002312/// checking the deduced template arguments for completeness and forming
2313/// the function template specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00002314Sema::TemplateDeductionResult
Douglas Gregor83314aa2009-07-08 20:55:45 +00002315Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor02024a92010-03-28 02:42:43 +00002316 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
2317 unsigned NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002318 FunctionDecl *&Specialization,
2319 TemplateDeductionInfo &Info) {
2320 TemplateParameterList *TemplateParams
2321 = FunctionTemplate->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00002322
Douglas Gregor83314aa2009-07-08 20:55:45 +00002323 // Template argument deduction for function templates in a SFINAE context.
2324 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002325 SFINAETrap Trap(*this);
2326
Douglas Gregor83314aa2009-07-08 20:55:45 +00002327 // Enter a new template instantiation context while we instantiate the
2328 // actual function declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002329 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00002330 FunctionTemplate, Deduced.data(), Deduced.size(),
Douglas Gregor9b623632010-10-12 23:32:35 +00002331 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution,
2332 Info);
Douglas Gregor83314aa2009-07-08 20:55:45 +00002333 if (Inst)
Mike Stump1eb44332009-09-09 15:08:12 +00002334 return TDK_InstantiationDepth;
2335
John McCall96db3102010-04-29 01:18:58 +00002336 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
John McCallf5813822010-04-29 00:35:03 +00002337
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002338 // C++ [temp.deduct.type]p2:
2339 // [...] or if any template argument remains neither deduced nor
2340 // explicitly specified, template argument deduction fails.
Douglas Gregor910f8002010-11-07 23:05:16 +00002341 llvm::SmallVector<TemplateArgument, 4> Builder;
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002342 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
2343 NamedDecl *Param = TemplateParams->getParam(I);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002344
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002345 if (!Deduced[I].isNull()) {
Douglas Gregor3273b0c2010-10-12 18:51:08 +00002346 if (I < NumExplicitlySpecified) {
Douglas Gregor02024a92010-03-28 02:42:43 +00002347 // We have already fully type-checked and converted this
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002348 // argument, because it was explicitly-specified. Just record the
Douglas Gregor3273b0c2010-10-12 18:51:08 +00002349 // presence of this argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002350 Builder.push_back(Deduced[I]);
Douglas Gregor02024a92010-03-28 02:42:43 +00002351 continue;
2352 }
2353
2354 // We have deduced this argument, so it still needs to be
2355 // checked and converted.
2356
2357 // First, for a non-type template parameter type that is
2358 // initialized by a declaration, we need the type of the
2359 // corresponding non-type template parameter.
2360 QualType NTTPType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002361 if (NonTypeTemplateParmDecl *NTTP
2362 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002363 NTTPType = NTTP->getType();
2364 if (NTTPType->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002365 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002366 Builder.data(), Builder.size());
2367 NTTPType = SubstType(NTTPType,
2368 MultiLevelTemplateArgumentList(TemplateArgs),
2369 NTTP->getLocation(),
2370 NTTP->getDeclName());
2371 if (NTTPType.isNull()) {
2372 Info.Param = makeTemplateParameter(Param);
2373 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002374 Info.reset(TemplateArgumentList::CreateCopy(Context,
2375 Builder.data(),
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002376 Builder.size()));
2377 return TDK_SubstitutionFailure;
Douglas Gregor02024a92010-03-28 02:42:43 +00002378 }
2379 }
2380 }
2381
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002382 if (ConvertDeducedTemplateArgument(*this, Param, Deduced[I],
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002383 FunctionTemplate, NTTPType, 0, Info,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002384 true, Builder)) {
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002385 Info.Param = makeTemplateParameter(Param);
Douglas Gregor910f8002010-11-07 23:05:16 +00002386 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002387 Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
2388 Builder.size()));
Douglas Gregor02024a92010-03-28 02:42:43 +00002389 return TDK_SubstitutionFailure;
2390 }
2391
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002392 continue;
2393 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002394
Douglas Gregorea6c96f2010-12-23 01:52:01 +00002395 // C++0x [temp.arg.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002396 // A trailing template parameter pack (14.5.3) not otherwise deduced will
Douglas Gregorea6c96f2010-12-23 01:52:01 +00002397 // be deduced to an empty sequence of template arguments.
2398 // FIXME: Where did the word "trailing" come from?
2399 if (Param->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002400 // We may have had explicitly-specified template arguments for this
2401 // template parameter pack. If so, our empty deduction extends the
2402 // explicitly-specified set (C++0x [temp.arg.explicit]p9).
2403 const TemplateArgument *ExplicitArgs;
2404 unsigned NumExplicitArgs;
2405 if (CurrentInstantiationScope->getPartiallySubstitutedPack(&ExplicitArgs,
2406 &NumExplicitArgs)
2407 == Param)
2408 Builder.push_back(TemplateArgument(ExplicitArgs, NumExplicitArgs));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002409 else
Douglas Gregord3731192011-01-10 07:32:04 +00002410 Builder.push_back(TemplateArgument(0, 0));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002411
Douglas Gregorea6c96f2010-12-23 01:52:01 +00002412 continue;
2413 }
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002414
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002415 // Substitute into the default template argument, if available.
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002416 TemplateArgumentLoc DefArg
2417 = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
2418 FunctionTemplate->getLocation(),
2419 FunctionTemplate->getSourceRange().getEnd(),
2420 Param,
2421 Builder);
2422
2423 // If there was no default argument, deduction is incomplete.
2424 if (DefArg.getArgument().isNull()) {
2425 Info.Param = makeTemplateParameter(
2426 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
2427 return TDK_Incomplete;
2428 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002429
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002430 // Check whether we can actually use the default argument.
2431 if (CheckTemplateArgument(Param, DefArg,
2432 FunctionTemplate,
2433 FunctionTemplate->getLocation(),
2434 FunctionTemplate->getSourceRange().getEnd(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002435 0, Builder,
Douglas Gregor8735b292011-06-03 02:59:40 +00002436 CTAK_Specified)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002437 Info.Param = makeTemplateParameter(
2438 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregor910f8002010-11-07 23:05:16 +00002439 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002440 Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002441 Builder.size()));
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002442 return TDK_SubstitutionFailure;
2443 }
2444
2445 // If we get here, we successfully used the default template argument.
2446 }
2447
2448 // Form the template argument list from the deduced template arguments.
2449 TemplateArgumentList *DeducedArgumentList
Douglas Gregor910f8002010-11-07 23:05:16 +00002450 = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002451 Info.reset(DeducedArgumentList);
2452
Mike Stump1eb44332009-09-09 15:08:12 +00002453 // Substitute the deduced template arguments into the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00002454 // declaration to produce the function template specialization.
Douglas Gregord4598a22010-04-28 04:52:24 +00002455 DeclContext *Owner = FunctionTemplate->getDeclContext();
2456 if (FunctionTemplate->getFriendObjectKind())
2457 Owner = FunctionTemplate->getLexicalDeclContext();
Douglas Gregor83314aa2009-07-08 20:55:45 +00002458 Specialization = cast_or_null<FunctionDecl>(
Douglas Gregord4598a22010-04-28 04:52:24 +00002459 SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner,
Douglas Gregor357bbd02009-08-28 20:50:45 +00002460 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor83314aa2009-07-08 20:55:45 +00002461 if (!Specialization)
2462 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00002463
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002464 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
Douglas Gregorf8825742009-09-15 18:26:13 +00002465 FunctionTemplate->getCanonicalDecl());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002466
Mike Stump1eb44332009-09-09 15:08:12 +00002467 // If the template argument list is owned by the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00002468 // specialization, release it.
Douglas Gregorec20f462010-05-08 20:07:26 +00002469 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList &&
2470 !Trap.hasErrorOccurred())
Douglas Gregor83314aa2009-07-08 20:55:45 +00002471 Info.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002472
Douglas Gregor83314aa2009-07-08 20:55:45 +00002473 // There may have been an error that did not prevent us from constructing a
2474 // declaration. Mark the declaration invalid and return with a substitution
2475 // failure.
2476 if (Trap.hasErrorOccurred()) {
2477 Specialization->setInvalidDecl(true);
2478 return TDK_SubstitutionFailure;
2479 }
Mike Stump1eb44332009-09-09 15:08:12 +00002480
Douglas Gregor9b623632010-10-12 23:32:35 +00002481 // If we suppressed any diagnostics while performing template argument
2482 // deduction, and if we haven't already instantiated this declaration,
2483 // keep track of these diagnostics. They'll be emitted if this specialization
2484 // is actually used.
2485 if (Info.diag_begin() != Info.diag_end()) {
2486 llvm::DenseMap<Decl *, llvm::SmallVector<PartialDiagnosticAt, 1> >::iterator
2487 Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl());
2488 if (Pos == SuppressedDiagnostics.end())
2489 SuppressedDiagnostics[Specialization->getCanonicalDecl()]
2490 .append(Info.diag_begin(), Info.diag_end());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002491 }
Douglas Gregor9b623632010-10-12 23:32:35 +00002492
Mike Stump1eb44332009-09-09 15:08:12 +00002493 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00002494}
2495
John McCall9c72c602010-08-27 09:08:28 +00002496/// Gets the type of a function for template-argument-deducton
2497/// purposes when it's considered as part of an overload set.
John McCalleff92132010-02-02 02:21:27 +00002498static QualType GetTypeOfFunction(ASTContext &Context,
John McCall9c72c602010-08-27 09:08:28 +00002499 const OverloadExpr::FindResult &R,
John McCalleff92132010-02-02 02:21:27 +00002500 FunctionDecl *Fn) {
John McCalleff92132010-02-02 02:21:27 +00002501 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
John McCall9c72c602010-08-27 09:08:28 +00002502 if (Method->isInstance()) {
2503 // An instance method that's referenced in a form that doesn't
2504 // look like a member pointer is just invalid.
2505 if (!R.HasFormOfMemberPointer) return QualType();
2506
John McCalleff92132010-02-02 02:21:27 +00002507 return Context.getMemberPointerType(Fn->getType(),
2508 Context.getTypeDeclType(Method->getParent()).getTypePtr());
John McCall9c72c602010-08-27 09:08:28 +00002509 }
2510
2511 if (!R.IsAddressOfOperand) return Fn->getType();
John McCalleff92132010-02-02 02:21:27 +00002512 return Context.getPointerType(Fn->getType());
2513}
2514
2515/// Apply the deduction rules for overload sets.
2516///
2517/// \return the null type if this argument should be treated as an
2518/// undeduced context
2519static QualType
2520ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
Douglas Gregor75f21af2010-08-30 21:04:23 +00002521 Expr *Arg, QualType ParamType,
2522 bool ParamWasReference) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002523
John McCall9c72c602010-08-27 09:08:28 +00002524 OverloadExpr::FindResult R = OverloadExpr::find(Arg);
John McCalleff92132010-02-02 02:21:27 +00002525
John McCall9c72c602010-08-27 09:08:28 +00002526 OverloadExpr *Ovl = R.Expression;
John McCalleff92132010-02-02 02:21:27 +00002527
Douglas Gregor75f21af2010-08-30 21:04:23 +00002528 // C++0x [temp.deduct.call]p4
2529 unsigned TDF = 0;
2530 if (ParamWasReference)
2531 TDF |= TDF_ParamWithReferenceType;
2532 if (R.IsAddressOfOperand)
2533 TDF |= TDF_IgnoreQualifiers;
2534
John McCalleff92132010-02-02 02:21:27 +00002535 // If there were explicit template arguments, we can only find
2536 // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
2537 // unambiguously name a full specialization.
John McCall7bb12da2010-02-02 06:20:04 +00002538 if (Ovl->hasExplicitTemplateArgs()) {
John McCalleff92132010-02-02 02:21:27 +00002539 // But we can still look for an explicit specialization.
2540 if (FunctionDecl *ExplicitSpec
John McCall7bb12da2010-02-02 06:20:04 +00002541 = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
John McCall9c72c602010-08-27 09:08:28 +00002542 return GetTypeOfFunction(S.Context, R, ExplicitSpec);
John McCalleff92132010-02-02 02:21:27 +00002543 return QualType();
2544 }
2545
2546 // C++0x [temp.deduct.call]p6:
2547 // When P is a function type, pointer to function type, or pointer
2548 // to member function type:
2549
2550 if (!ParamType->isFunctionType() &&
2551 !ParamType->isFunctionPointerType() &&
2552 !ParamType->isMemberFunctionPointerType())
2553 return QualType();
2554
2555 QualType Match;
John McCall7bb12da2010-02-02 06:20:04 +00002556 for (UnresolvedSetIterator I = Ovl->decls_begin(),
2557 E = Ovl->decls_end(); I != E; ++I) {
John McCalleff92132010-02-02 02:21:27 +00002558 NamedDecl *D = (*I)->getUnderlyingDecl();
2559
2560 // - If the argument is an overload set containing one or more
2561 // function templates, the parameter is treated as a
2562 // non-deduced context.
2563 if (isa<FunctionTemplateDecl>(D))
2564 return QualType();
2565
2566 FunctionDecl *Fn = cast<FunctionDecl>(D);
John McCall9c72c602010-08-27 09:08:28 +00002567 QualType ArgType = GetTypeOfFunction(S.Context, R, Fn);
2568 if (ArgType.isNull()) continue;
John McCalleff92132010-02-02 02:21:27 +00002569
Douglas Gregor75f21af2010-08-30 21:04:23 +00002570 // Function-to-pointer conversion.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002571 if (!ParamWasReference && ParamType->isPointerType() &&
Douglas Gregor75f21af2010-08-30 21:04:23 +00002572 ArgType->isFunctionType())
2573 ArgType = S.Context.getPointerType(ArgType);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002574
John McCalleff92132010-02-02 02:21:27 +00002575 // - If the argument is an overload set (not containing function
2576 // templates), trial argument deduction is attempted using each
2577 // of the members of the set. If deduction succeeds for only one
2578 // of the overload set members, that member is used as the
2579 // argument value for the deduction. If deduction succeeds for
2580 // more than one member of the overload set the parameter is
2581 // treated as a non-deduced context.
2582
2583 // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
2584 // Type deduction is done independently for each P/A pair, and
2585 // the deduced template argument values are then combined.
2586 // So we do not reject deductions which were made elsewhere.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002587 llvm::SmallVector<DeducedTemplateArgument, 8>
Douglas Gregor02024a92010-03-28 02:42:43 +00002588 Deduced(TemplateParams->size());
John McCall2a7fb272010-08-25 05:32:35 +00002589 TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
John McCalleff92132010-02-02 02:21:27 +00002590 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002591 = DeduceTemplateArguments(S, TemplateParams,
John McCalleff92132010-02-02 02:21:27 +00002592 ParamType, ArgType,
2593 Info, Deduced, TDF);
2594 if (Result) continue;
2595 if (!Match.isNull()) return QualType();
2596 Match = ArgType;
2597 }
2598
2599 return Match;
2600}
2601
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002602/// \brief Perform the adjustments to the parameter and argument types
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002603/// described in C++ [temp.deduct.call].
2604///
2605/// \returns true if the caller should not attempt to perform any template
2606/// argument deduction based on this P/A pair.
2607static bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S,
2608 TemplateParameterList *TemplateParams,
2609 QualType &ParamType,
2610 QualType &ArgType,
2611 Expr *Arg,
2612 unsigned &TDF) {
2613 // C++0x [temp.deduct.call]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00002614 // If P is a cv-qualified type, the top level cv-qualifiers of P's type
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002615 // are ignored for type deduction.
Douglas Gregora459cc22011-04-27 23:34:22 +00002616 if (ParamType.hasQualifiers())
2617 ParamType = ParamType.getUnqualifiedType();
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002618 const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>();
2619 if (ParamRefType) {
Richard Smith34b41d92011-02-20 03:19:35 +00002620 QualType PointeeType = ParamRefType->getPointeeType();
2621
Douglas Gregorf15748a2011-06-03 03:35:07 +00002622 // If the argument has incomplete array type, try to complete it's type.
2623 if (ArgType->isIncompleteArrayType() &&
2624 !S.RequireCompleteExprType(Arg, S.PDiag(),
2625 std::make_pair(SourceLocation(), S.PDiag())))
2626 ArgType = Arg->getType();
2627
Douglas Gregor2ad746a2011-01-21 05:18:22 +00002628 // [C++0x] If P is an rvalue reference to a cv-unqualified
2629 // template parameter and the argument is an lvalue, the type
2630 // "lvalue reference to A" is used in place of A for type
2631 // deduction.
Richard Smith34b41d92011-02-20 03:19:35 +00002632 if (isa<RValueReferenceType>(ParamType)) {
2633 if (!PointeeType.getQualifiers() &&
2634 isa<TemplateTypeParmType>(PointeeType) &&
Douglas Gregor9625e442011-05-21 22:16:50 +00002635 Arg->Classify(S.Context).isLValue() &&
2636 Arg->getType() != S.Context.OverloadTy &&
2637 Arg->getType() != S.Context.BoundMemberTy)
Douglas Gregor2ad746a2011-01-21 05:18:22 +00002638 ArgType = S.Context.getLValueReferenceType(ArgType);
2639 }
2640
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002641 // [...] If P is a reference type, the type referred to by P is used
2642 // for type deduction.
Richard Smith34b41d92011-02-20 03:19:35 +00002643 ParamType = PointeeType;
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002644 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002645
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002646 // Overload sets usually make this parameter an undeduced
2647 // context, but there are sometimes special circumstances.
2648 if (ArgType == S.Context.OverloadTy) {
2649 ArgType = ResolveOverloadForDeduction(S, TemplateParams,
2650 Arg, ParamType,
2651 ParamRefType != 0);
2652 if (ArgType.isNull())
2653 return true;
2654 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002655
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002656 if (ParamRefType) {
2657 // C++0x [temp.deduct.call]p3:
2658 // [...] If P is of the form T&&, where T is a template parameter, and
2659 // the argument is an lvalue, the type A& is used in place of A for
2660 // type deduction.
2661 if (ParamRefType->isRValueReferenceType() &&
2662 ParamRefType->getAs<TemplateTypeParmType>() &&
2663 Arg->isLValue())
2664 ArgType = S.Context.getLValueReferenceType(ArgType);
2665 } else {
2666 // C++ [temp.deduct.call]p2:
2667 // If P is not a reference type:
2668 // - If A is an array type, the pointer type produced by the
2669 // array-to-pointer standard conversion (4.2) is used in place of
2670 // A for type deduction; otherwise,
2671 if (ArgType->isArrayType())
2672 ArgType = S.Context.getArrayDecayedType(ArgType);
2673 // - If A is a function type, the pointer type produced by the
2674 // function-to-pointer standard conversion (4.3) is used in place
2675 // of A for type deduction; otherwise,
2676 else if (ArgType->isFunctionType())
2677 ArgType = S.Context.getPointerType(ArgType);
2678 else {
NAKAMURA Takumi00995302011-01-27 07:09:49 +00002679 // - If A is a cv-qualified type, the top level cv-qualifiers of A's
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002680 // type are ignored for type deduction.
Douglas Gregora459cc22011-04-27 23:34:22 +00002681 ArgType = ArgType.getUnqualifiedType();
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002682 }
2683 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002684
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002685 // C++0x [temp.deduct.call]p4:
2686 // In general, the deduction process attempts to find template argument
2687 // values that will make the deduced A identical to A (after the type A
2688 // is transformed as described above). [...]
2689 TDF = TDF_SkipNonDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002690
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002691 // - If the original P is a reference type, the deduced A (i.e., the
2692 // type referred to by the reference) can be more cv-qualified than
2693 // the transformed A.
2694 if (ParamRefType)
2695 TDF |= TDF_ParamWithReferenceType;
2696 // - The transformed A can be another pointer or pointer to member
2697 // type that can be converted to the deduced A via a qualification
2698 // conversion (4.4).
2699 if (ArgType->isPointerType() || ArgType->isMemberPointerType() ||
2700 ArgType->isObjCObjectPointerType())
2701 TDF |= TDF_IgnoreQualifiers;
2702 // - If P is a class and P has the form simple-template-id, then the
2703 // transformed A can be a derived class of the deduced A. Likewise,
2704 // if P is a pointer to a class of the form simple-template-id, the
2705 // transformed A can be a pointer to a derived class pointed to by
2706 // the deduced A.
2707 if (isSimpleTemplateIdType(ParamType) ||
2708 (isa<PointerType>(ParamType) &&
2709 isSimpleTemplateIdType(
2710 ParamType->getAs<PointerType>()->getPointeeType())))
2711 TDF |= TDF_DerivedClass;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002712
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002713 return false;
2714}
2715
Douglas Gregore53060f2009-06-25 22:08:12 +00002716/// \brief Perform template argument deduction from a function call
2717/// (C++ [temp.deduct.call]).
2718///
2719/// \param FunctionTemplate the function template for which we are performing
2720/// template argument deduction.
2721///
Douglas Gregor48026d22010-01-11 18:40:55 +00002722/// \param ExplicitTemplateArguments the explicit template arguments provided
2723/// for this call.
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002724///
Douglas Gregore53060f2009-06-25 22:08:12 +00002725/// \param Args the function call arguments
2726///
2727/// \param NumArgs the number of arguments in Args
2728///
Douglas Gregor48026d22010-01-11 18:40:55 +00002729/// \param Name the name of the function being called. This is only significant
2730/// when the function template is a conversion function template, in which
2731/// case this routine will also perform template argument deduction based on
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002732/// the function to which
Douglas Gregor48026d22010-01-11 18:40:55 +00002733///
Douglas Gregore53060f2009-06-25 22:08:12 +00002734/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00002735/// this will be set to the function template specialization produced by
Douglas Gregore53060f2009-06-25 22:08:12 +00002736/// template argument deduction.
2737///
2738/// \param Info the argument will be updated to provide additional information
2739/// about template argument deduction.
2740///
2741/// \returns the result of template argument deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00002742Sema::TemplateDeductionResult
2743Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor67714232011-03-03 02:41:12 +00002744 TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregore53060f2009-06-25 22:08:12 +00002745 Expr **Args, unsigned NumArgs,
2746 FunctionDecl *&Specialization,
2747 TemplateDeductionInfo &Info) {
2748 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002749
Douglas Gregore53060f2009-06-25 22:08:12 +00002750 // C++ [temp.deduct.call]p1:
2751 // Template argument deduction is done by comparing each function template
2752 // parameter type (call it P) with the type of the corresponding argument
2753 // of the call (call it A) as described below.
2754 unsigned CheckArgs = NumArgs;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002755 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregore53060f2009-06-25 22:08:12 +00002756 return TDK_TooFewArguments;
2757 else if (NumArgs > Function->getNumParams()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002758 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00002759 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002760 if (Proto->isTemplateVariadic())
2761 /* Do nothing */;
2762 else if (Proto->isVariadic())
2763 CheckArgs = Function->getNumParams();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002764 else
Douglas Gregore53060f2009-06-25 22:08:12 +00002765 return TDK_TooManyArguments;
Douglas Gregore53060f2009-06-25 22:08:12 +00002766 }
Mike Stump1eb44332009-09-09 15:08:12 +00002767
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002768 // The types of the parameters from which we will perform template argument
2769 // deduction.
John McCall2a7fb272010-08-25 05:32:35 +00002770 LocalInstantiationScope InstScope(*this);
Douglas Gregore53060f2009-06-25 22:08:12 +00002771 TemplateParameterList *TemplateParams
2772 = FunctionTemplate->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00002773 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002774 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregor02024a92010-03-28 02:42:43 +00002775 unsigned NumExplicitlySpecified = 0;
John McCalld5532b62009-11-23 01:53:49 +00002776 if (ExplicitTemplateArgs) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00002777 TemplateDeductionResult Result =
2778 SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00002779 *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002780 Deduced,
2781 ParamTypes,
2782 0,
2783 Info);
2784 if (Result)
2785 return Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002786
2787 NumExplicitlySpecified = Deduced.size();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002788 } else {
2789 // Just fill in the parameter types from the function declaration.
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002790 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002791 ParamTypes.push_back(Function->getParamDecl(I)->getType());
2792 }
Mike Stump1eb44332009-09-09 15:08:12 +00002793
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002794 // Deduce template arguments from the function parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00002795 Deduced.resize(TemplateParams->size());
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002796 unsigned ArgIdx = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002797 for (unsigned ParamIdx = 0, NumParams = ParamTypes.size();
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002798 ParamIdx != NumParams; ++ParamIdx) {
2799 QualType ParamType = ParamTypes[ParamIdx];
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002800
2801 const PackExpansionType *ParamExpansion
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002802 = dyn_cast<PackExpansionType>(ParamType);
2803 if (!ParamExpansion) {
2804 // Simple case: matching a function parameter to a function argument.
2805 if (ArgIdx >= CheckArgs)
2806 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002807
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002808 Expr *Arg = Args[ArgIdx++];
2809 QualType ArgType = Arg->getType();
2810 unsigned TDF = 0;
2811 if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams,
2812 ParamType, ArgType, Arg,
2813 TDF))
2814 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002815
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002816 if (TemplateDeductionResult Result
2817 = ::DeduceTemplateArguments(*this, TemplateParams,
2818 ParamType, ArgType, Info, Deduced,
2819 TDF))
2820 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002821
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002822 // FIXME: we need to check that the deduced A is the same as A,
2823 // modulo the various allowed differences.
2824 continue;
Douglas Gregor75f21af2010-08-30 21:04:23 +00002825 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002826
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002827 // C++0x [temp.deduct.call]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002828 // For a function parameter pack that occurs at the end of the
2829 // parameter-declaration-list, the type A of each remaining argument of
2830 // the call is compared with the type P of the declarator-id of the
2831 // function parameter pack. Each comparison deduces template arguments
2832 // for subsequent positions in the template parameter packs expanded by
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00002833 // the function parameter pack. For a function parameter pack that does
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002834 // not occur at the end of the parameter-declaration-list, the type of
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00002835 // the parameter pack is a non-deduced context.
2836 if (ParamIdx + 1 < NumParams)
2837 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002838
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002839 QualType ParamPattern = ParamExpansion->getPattern();
2840 llvm::SmallVector<unsigned, 2> PackIndices;
2841 {
2842 llvm::BitVector SawIndices(TemplateParams->size());
2843 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2844 collectUnexpandedParameterPacks(ParamPattern, Unexpanded);
2845 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
2846 unsigned Depth, Index;
2847 llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
2848 if (Depth == 0 && !SawIndices[Index]) {
2849 SawIndices[Index] = true;
2850 PackIndices.push_back(Index);
2851 }
Douglas Gregore53060f2009-06-25 22:08:12 +00002852 }
2853 }
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002854 assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002855
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002856 // Keep track of the deduced template arguments for each parameter pack
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002857 // expanded by this pack expansion (the outer index) and for each
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002858 // template argument (the inner SmallVectors).
2859 llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2>
Douglas Gregord3731192011-01-10 07:32:04 +00002860 NewlyDeducedPacks(PackIndices.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002861 llvm::SmallVector<DeducedTemplateArgument, 2>
Douglas Gregord3731192011-01-10 07:32:04 +00002862 SavedPacks(PackIndices.size());
Douglas Gregor54293852011-01-10 17:35:05 +00002863 PrepareArgumentPackDeduction(*this, Deduced, PackIndices, SavedPacks,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002864 NewlyDeducedPacks);
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002865 bool HasAnyArguments = false;
2866 for (; ArgIdx < NumArgs; ++ArgIdx) {
2867 HasAnyArguments = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002868
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002869 ParamType = ParamPattern;
2870 Expr *Arg = Args[ArgIdx];
2871 QualType ArgType = Arg->getType();
2872 unsigned TDF = 0;
2873 if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams,
2874 ParamType, ArgType, Arg,
2875 TDF)) {
2876 // We can't actually perform any deduction for this argument, so stop
2877 // deduction at this point.
2878 ++ArgIdx;
2879 break;
2880 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002881
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002882 if (TemplateDeductionResult Result
2883 = ::DeduceTemplateArguments(*this, TemplateParams,
2884 ParamType, ArgType, Info, Deduced,
2885 TDF))
2886 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002887
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002888 // Capture the deduced template arguments for each parameter pack expanded
2889 // by this pack expansion, add them to the list of arguments we've deduced
2890 // for that pack, then clear out the deduced argument.
2891 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
2892 DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
2893 if (!DeducedArg.isNull()) {
2894 NewlyDeducedPacks[I].push_back(DeducedArg);
2895 DeducedArg = DeducedTemplateArgument();
2896 }
2897 }
2898 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002899
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002900 // Build argument packs for each of the parameter packs expanded by this
2901 // pack expansion.
Douglas Gregor0216f812011-01-10 17:53:52 +00002902 if (Sema::TemplateDeductionResult Result
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002903 = FinishArgumentPackDeduction(*this, TemplateParams, HasAnyArguments,
Douglas Gregor0216f812011-01-10 17:53:52 +00002904 Deduced, PackIndices, SavedPacks,
2905 NewlyDeducedPacks, Info))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002906 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002907
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002908 // After we've matching against a parameter pack, we're done.
2909 break;
Douglas Gregore53060f2009-06-25 22:08:12 +00002910 }
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002911
Mike Stump1eb44332009-09-09 15:08:12 +00002912 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor02024a92010-03-28 02:42:43 +00002913 NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002914 Specialization, Info);
Douglas Gregore53060f2009-06-25 22:08:12 +00002915}
2916
Douglas Gregor83314aa2009-07-08 20:55:45 +00002917/// \brief Deduce template arguments when taking the address of a function
Douglas Gregor4b52e252009-12-21 23:17:24 +00002918/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
2919/// a template.
Douglas Gregor83314aa2009-07-08 20:55:45 +00002920///
2921/// \param FunctionTemplate the function template for which we are performing
2922/// template argument deduction.
2923///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002924/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor4b52e252009-12-21 23:17:24 +00002925/// arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00002926///
2927/// \param ArgFunctionType the function type that will be used as the
2928/// "argument" type (A) when performing template argument deduction from the
Douglas Gregor4b52e252009-12-21 23:17:24 +00002929/// function template's function type. This type may be NULL, if there is no
2930/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
Douglas Gregor83314aa2009-07-08 20:55:45 +00002931///
2932/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00002933/// this will be set to the function template specialization produced by
Douglas Gregor83314aa2009-07-08 20:55:45 +00002934/// template argument deduction.
2935///
2936/// \param Info the argument will be updated to provide additional information
2937/// about template argument deduction.
2938///
2939/// \returns the result of template argument deduction.
2940Sema::TemplateDeductionResult
2941Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor67714232011-03-03 02:41:12 +00002942 TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002943 QualType ArgFunctionType,
2944 FunctionDecl *&Specialization,
2945 TemplateDeductionInfo &Info) {
2946 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2947 TemplateParameterList *TemplateParams
2948 = FunctionTemplate->getTemplateParameters();
2949 QualType FunctionType = Function->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002950
Douglas Gregor83314aa2009-07-08 20:55:45 +00002951 // Substitute any explicit template arguments.
John McCall2a7fb272010-08-25 05:32:35 +00002952 LocalInstantiationScope InstScope(*this);
Douglas Gregor02024a92010-03-28 02:42:43 +00002953 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
2954 unsigned NumExplicitlySpecified = 0;
Douglas Gregor83314aa2009-07-08 20:55:45 +00002955 llvm::SmallVector<QualType, 4> ParamTypes;
John McCalld5532b62009-11-23 01:53:49 +00002956 if (ExplicitTemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002957 if (TemplateDeductionResult Result
2958 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00002959 *ExplicitTemplateArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00002960 Deduced, ParamTypes,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002961 &FunctionType, Info))
2962 return Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002963
2964 NumExplicitlySpecified = Deduced.size();
Douglas Gregor83314aa2009-07-08 20:55:45 +00002965 }
2966
2967 // Template argument deduction for function templates in a SFINAE context.
2968 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002969 SFINAETrap Trap(*this);
2970
John McCalleff92132010-02-02 02:21:27 +00002971 Deduced.resize(TemplateParams->size());
2972
Douglas Gregor4b52e252009-12-21 23:17:24 +00002973 if (!ArgFunctionType.isNull()) {
2974 // Deduce template arguments from the function type.
Douglas Gregor4b52e252009-12-21 23:17:24 +00002975 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002976 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor4b52e252009-12-21 23:17:24 +00002977 FunctionType, ArgFunctionType, Info,
Douglas Gregor73b3cf62011-01-25 17:19:08 +00002978 Deduced, TDF_TopLevelParameterTypeList))
Douglas Gregor4b52e252009-12-21 23:17:24 +00002979 return Result;
2980 }
Douglas Gregorfbb6fad2010-09-29 21:14:36 +00002981
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002982 if (TemplateDeductionResult Result
Douglas Gregorfbb6fad2010-09-29 21:14:36 +00002983 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
2984 NumExplicitlySpecified,
2985 Specialization, Info))
2986 return Result;
2987
2988 // If the requested function type does not match the actual type of the
2989 // specialization, template argument deduction fails.
2990 if (!ArgFunctionType.isNull() &&
2991 !Context.hasSameType(ArgFunctionType, Specialization->getType()))
2992 return TDK_NonDeducedMismatch;
2993
2994 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00002995}
2996
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002997/// \brief Deduce template arguments for a templated conversion
2998/// function (C++ [temp.deduct.conv]) and, if successful, produce a
2999/// conversion function template specialization.
3000Sema::TemplateDeductionResult
3001Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
3002 QualType ToType,
3003 CXXConversionDecl *&Specialization,
3004 TemplateDeductionInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00003005 CXXConversionDecl *Conv
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003006 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
3007 QualType FromType = Conv->getConversionType();
3008
3009 // Canonicalize the types for deduction.
3010 QualType P = Context.getCanonicalType(FromType);
3011 QualType A = Context.getCanonicalType(ToType);
3012
Douglas Gregor5453d932011-03-06 09:03:20 +00003013 // C++0x [temp.deduct.conv]p2:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003014 // If P is a reference type, the type referred to by P is used for
3015 // type deduction.
3016 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
3017 P = PRef->getPointeeType();
3018
Douglas Gregor5453d932011-03-06 09:03:20 +00003019 // C++0x [temp.deduct.conv]p4:
3020 // [...] If A is a reference type, the type referred to by A is used
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003021 // for type deduction.
3022 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
Douglas Gregor5453d932011-03-06 09:03:20 +00003023 A = ARef->getPointeeType().getUnqualifiedType();
3024 // C++ [temp.deduct.conv]p3:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003025 //
Mike Stump1eb44332009-09-09 15:08:12 +00003026 // If A is not a reference type:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003027 else {
3028 assert(!A->isReferenceType() && "Reference types were handled above");
3029
3030 // - If P is an array type, the pointer type produced by the
Mike Stump1eb44332009-09-09 15:08:12 +00003031 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003032 // of P for type deduction; otherwise,
3033 if (P->isArrayType())
3034 P = Context.getArrayDecayedType(P);
3035 // - If P is a function type, the pointer type produced by the
3036 // function-to-pointer standard conversion (4.3) is used in
3037 // place of P for type deduction; otherwise,
3038 else if (P->isFunctionType())
3039 P = Context.getPointerType(P);
3040 // - If P is a cv-qualified type, the top level cv-qualifiers of
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003041 // P's type are ignored for type deduction.
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003042 else
3043 P = P.getUnqualifiedType();
3044
Douglas Gregor5453d932011-03-06 09:03:20 +00003045 // C++0x [temp.deduct.conv]p4:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003046 // If A is a cv-qualified type, the top level cv-qualifiers of A's
Douglas Gregor5453d932011-03-06 09:03:20 +00003047 // type are ignored for type deduction. If A is a reference type, the type
3048 // referred to by A is used for type deduction.
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003049 A = A.getUnqualifiedType();
3050 }
3051
3052 // Template argument deduction for function templates in a SFINAE context.
3053 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00003054 SFINAETrap Trap(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003055
3056 // C++ [temp.deduct.conv]p1:
3057 // Template argument deduction is done by comparing the return
3058 // type of the template conversion function (call it P) with the
3059 // type that is required as the result of the conversion (call it
3060 // A) as described in 14.8.2.4.
3061 TemplateParameterList *TemplateParams
3062 = FunctionTemplate->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00003063 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Mike Stump1eb44332009-09-09 15:08:12 +00003064 Deduced.resize(TemplateParams->size());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003065
3066 // C++0x [temp.deduct.conv]p4:
3067 // In general, the deduction process attempts to find template
3068 // argument values that will make the deduced A identical to
3069 // A. However, there are two cases that allow a difference:
3070 unsigned TDF = 0;
3071 // - If the original A is a reference type, A can be more
3072 // cv-qualified than the deduced A (i.e., the type referred to
3073 // by the reference)
3074 if (ToType->isReferenceType())
3075 TDF |= TDF_ParamWithReferenceType;
3076 // - The deduced A can be another pointer or pointer to member
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003077 // type that can be converted to A via a qualification
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003078 // conversion.
3079 //
3080 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
3081 // both P and A are pointers or member pointers. In this case, we
3082 // just ignore cv-qualifiers completely).
3083 if ((P->isPointerType() && A->isPointerType()) ||
3084 (P->isMemberPointerType() && P->isMemberPointerType()))
3085 TDF |= TDF_IgnoreQualifiers;
3086 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00003087 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003088 P, A, Info, Deduced, TDF))
3089 return Result;
3090
3091 // FIXME: we need to check that the deduced A is the same as A,
3092 // modulo the various allowed differences.
Mike Stump1eb44332009-09-09 15:08:12 +00003093
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003094 // Finish template argument deduction.
John McCall2a7fb272010-08-25 05:32:35 +00003095 LocalInstantiationScope InstScope(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003096 FunctionDecl *Spec = 0;
3097 TemplateDeductionResult Result
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003098 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec,
Douglas Gregor02024a92010-03-28 02:42:43 +00003099 Info);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003100 Specialization = cast_or_null<CXXConversionDecl>(Spec);
3101 return Result;
3102}
3103
Douglas Gregor4b52e252009-12-21 23:17:24 +00003104/// \brief Deduce template arguments for a function template when there is
3105/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
3106///
3107/// \param FunctionTemplate the function template for which we are performing
3108/// template argument deduction.
3109///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003110/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor4b52e252009-12-21 23:17:24 +00003111/// arguments.
3112///
3113/// \param Specialization if template argument deduction was successful,
3114/// this will be set to the function template specialization produced by
3115/// template argument deduction.
3116///
3117/// \param Info the argument will be updated to provide additional information
3118/// about template argument deduction.
3119///
3120/// \returns the result of template argument deduction.
3121Sema::TemplateDeductionResult
3122Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor67714232011-03-03 02:41:12 +00003123 TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor4b52e252009-12-21 23:17:24 +00003124 FunctionDecl *&Specialization,
3125 TemplateDeductionInfo &Info) {
3126 return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
3127 QualType(), Specialization, Info);
3128}
3129
Richard Smith34b41d92011-02-20 03:19:35 +00003130namespace {
3131 /// Substitute the 'auto' type specifier within a type for a given replacement
3132 /// type.
3133 class SubstituteAutoTransform :
3134 public TreeTransform<SubstituteAutoTransform> {
3135 QualType Replacement;
3136 public:
3137 SubstituteAutoTransform(Sema &SemaRef, QualType Replacement) :
3138 TreeTransform<SubstituteAutoTransform>(SemaRef), Replacement(Replacement) {
3139 }
3140 QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) {
3141 // If we're building the type pattern to deduce against, don't wrap the
3142 // substituted type in an AutoType. Certain template deduction rules
3143 // apply only when a template type parameter appears directly (and not if
3144 // the parameter is found through desugaring). For instance:
3145 // auto &&lref = lvalue;
3146 // must transform into "rvalue reference to T" not "rvalue reference to
3147 // auto type deduced as T" in order for [temp.deduct.call]p3 to apply.
3148 if (isa<TemplateTypeParmType>(Replacement)) {
3149 QualType Result = Replacement;
3150 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
3151 NewTL.setNameLoc(TL.getNameLoc());
3152 return Result;
3153 } else {
3154 QualType Result = RebuildAutoType(Replacement);
3155 AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result);
3156 NewTL.setNameLoc(TL.getNameLoc());
3157 return Result;
3158 }
3159 }
3160 };
3161}
3162
3163/// \brief Deduce the type for an auto type-specifier (C++0x [dcl.spec.auto]p6)
3164///
3165/// \param Type the type pattern using the auto type-specifier.
3166///
3167/// \param Init the initializer for the variable whose type is to be deduced.
3168///
3169/// \param Result if type deduction was successful, this will be set to the
3170/// deduced type. This may still contain undeduced autos if the type is
Richard Smitha085da82011-03-17 16:11:59 +00003171/// dependent. This will be set to null if deduction succeeded, but auto
3172/// substitution failed; the appropriate diagnostic will already have been
3173/// produced in that case.
Richard Smith34b41d92011-02-20 03:19:35 +00003174///
3175/// \returns true if deduction succeeded, false if it failed.
3176bool
Richard Smitha085da82011-03-17 16:11:59 +00003177Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *Init,
3178 TypeSourceInfo *&Result) {
Richard Smith34b41d92011-02-20 03:19:35 +00003179 if (Init->isTypeDependent()) {
3180 Result = Type;
3181 return true;
3182 }
3183
3184 SourceLocation Loc = Init->getExprLoc();
3185
3186 LocalInstantiationScope InstScope(*this);
3187
3188 // Build template<class TemplParam> void Func(FuncParam);
Chandler Carruth4fb86f82011-05-01 00:51:33 +00003189 TemplateTypeParmDecl *TemplParam =
3190 TemplateTypeParmDecl::Create(Context, 0, SourceLocation(), Loc, 0, 0, 0,
3191 false, false);
3192 QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0);
3193 NamedDecl *TemplParamPtr = TemplParam;
Richard Smith483b9f32011-02-21 20:05:19 +00003194 FixedSizeTemplateParameterList<1> TemplateParams(Loc, Loc, &TemplParamPtr,
3195 Loc);
3196
Richard Smitha085da82011-03-17 16:11:59 +00003197 TypeSourceInfo *FuncParamInfo =
Richard Smith34b41d92011-02-20 03:19:35 +00003198 SubstituteAutoTransform(*this, TemplArg).TransformType(Type);
Richard Smitha085da82011-03-17 16:11:59 +00003199 assert(FuncParamInfo && "substituting template parameter for 'auto' failed");
3200 QualType FuncParam = FuncParamInfo->getType();
Richard Smith34b41d92011-02-20 03:19:35 +00003201
3202 // Deduce type of TemplParam in Func(Init)
3203 llvm::SmallVector<DeducedTemplateArgument, 1> Deduced;
3204 Deduced.resize(1);
3205 QualType InitType = Init->getType();
3206 unsigned TDF = 0;
Richard Smith483b9f32011-02-21 20:05:19 +00003207 if (AdjustFunctionParmAndArgTypesForDeduction(*this, &TemplateParams,
Richard Smith34b41d92011-02-20 03:19:35 +00003208 FuncParam, InitType, Init,
3209 TDF))
3210 return false;
3211
3212 TemplateDeductionInfo Info(Context, Loc);
Richard Smith483b9f32011-02-21 20:05:19 +00003213 if (::DeduceTemplateArguments(*this, &TemplateParams,
Richard Smith34b41d92011-02-20 03:19:35 +00003214 FuncParam, InitType, Info, Deduced,
3215 TDF))
3216 return false;
3217
3218 QualType DeducedType = Deduced[0].getAsType();
3219 if (DeducedType.isNull())
3220 return false;
3221
3222 Result = SubstituteAutoTransform(*this, DeducedType).TransformType(Type);
3223 return true;
3224}
3225
Douglas Gregor8a514912009-09-14 18:39:43 +00003226static void
Douglas Gregore73bb602009-09-14 21:25:05 +00003227MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
3228 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003229 unsigned Level,
Douglas Gregore73bb602009-09-14 21:25:05 +00003230 llvm::SmallVectorImpl<bool> &Deduced);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003231
3232/// \brief If this is a non-static member function,
Douglas Gregor77bc5722010-11-12 23:44:13 +00003233static void MaybeAddImplicitObjectParameterType(ASTContext &Context,
3234 CXXMethodDecl *Method,
3235 llvm::SmallVectorImpl<QualType> &ArgTypes) {
3236 if (Method->isStatic())
3237 return;
3238
3239 // C++ [over.match.funcs]p4:
3240 //
3241 // For non-static member functions, the type of the implicit
3242 // object parameter is
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003243 // - "lvalue reference to cv X" for functions declared without a
Douglas Gregor77bc5722010-11-12 23:44:13 +00003244 // ref-qualifier or with the & ref-qualifier
3245 // - "rvalue reference to cv X" for functions declared with the
3246 // && ref-qualifier
3247 //
3248 // FIXME: We don't have ref-qualifiers yet, so we don't do that part.
3249 QualType ArgTy = Context.getTypeDeclType(Method->getParent());
3250 ArgTy = Context.getQualifiedType(ArgTy,
3251 Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
3252 ArgTy = Context.getLValueReferenceType(ArgTy);
3253 ArgTypes.push_back(ArgTy);
3254}
3255
Douglas Gregor8a514912009-09-14 18:39:43 +00003256/// \brief Determine whether the function template \p FT1 is at least as
3257/// specialized as \p FT2.
3258static bool isAtLeastAsSpecializedAs(Sema &S,
John McCall5769d612010-02-08 23:07:23 +00003259 SourceLocation Loc,
Douglas Gregor8a514912009-09-14 18:39:43 +00003260 FunctionTemplateDecl *FT1,
3261 FunctionTemplateDecl *FT2,
3262 TemplatePartialOrderingContext TPOC,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003263 unsigned NumCallArguments,
Douglas Gregorb939a192011-01-21 17:29:42 +00003264 llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *RefParamComparisons) {
Douglas Gregor8a514912009-09-14 18:39:43 +00003265 FunctionDecl *FD1 = FT1->getTemplatedDecl();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003266 FunctionDecl *FD2 = FT2->getTemplatedDecl();
Douglas Gregor8a514912009-09-14 18:39:43 +00003267 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
3268 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003269
Douglas Gregor8a514912009-09-14 18:39:43 +00003270 assert(Proto1 && Proto2 && "Function templates must have prototypes");
3271 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00003272 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor8a514912009-09-14 18:39:43 +00003273 Deduced.resize(TemplateParams->size());
3274
3275 // C++0x [temp.deduct.partial]p3:
3276 // The types used to determine the ordering depend on the context in which
3277 // the partial ordering is done:
John McCall2a7fb272010-08-25 05:32:35 +00003278 TemplateDeductionInfo Info(S.Context, Loc);
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003279 CXXMethodDecl *Method1 = 0;
3280 CXXMethodDecl *Method2 = 0;
3281 bool IsNonStatic2 = false;
3282 bool IsNonStatic1 = false;
3283 unsigned Skip2 = 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00003284 switch (TPOC) {
3285 case TPOC_Call: {
3286 // - In the context of a function call, the function parameter types are
3287 // used.
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003288 Method1 = dyn_cast<CXXMethodDecl>(FD1);
3289 Method2 = dyn_cast<CXXMethodDecl>(FD2);
3290 IsNonStatic1 = Method1 && !Method1->isStatic();
3291 IsNonStatic2 = Method2 && !Method2->isStatic();
3292
3293 // C++0x [temp.func.order]p3:
3294 // [...] If only one of the function templates is a non-static
3295 // member, that function template is considered to have a new
3296 // first parameter inserted in its function parameter list. The
3297 // new parameter is of type "reference to cv A," where cv are
3298 // the cv-qualifiers of the function template (if any) and A is
3299 // the class of which the function template is a member.
3300 //
3301 // C++98/03 doesn't have this provision, so instead we drop the
3302 // first argument of the free function or static member, which
3303 // seems to match existing practice.
Douglas Gregor77bc5722010-11-12 23:44:13 +00003304 llvm::SmallVector<QualType, 4> Args1;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003305 unsigned Skip1 = !S.getLangOptions().CPlusPlus0x &&
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003306 IsNonStatic2 && !IsNonStatic1;
3307 if (S.getLangOptions().CPlusPlus0x && IsNonStatic1 && !IsNonStatic2)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003308 MaybeAddImplicitObjectParameterType(S.Context, Method1, Args1);
3309 Args1.insert(Args1.end(),
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003310 Proto1->arg_type_begin() + Skip1, Proto1->arg_type_end());
Douglas Gregor77bc5722010-11-12 23:44:13 +00003311
3312 llvm::SmallVector<QualType, 4> Args2;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003313 Skip2 = !S.getLangOptions().CPlusPlus0x &&
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003314 IsNonStatic1 && !IsNonStatic2;
3315 if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1)
Douglas Gregor77bc5722010-11-12 23:44:13 +00003316 MaybeAddImplicitObjectParameterType(S.Context, Method2, Args2);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003317 Args2.insert(Args2.end(),
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003318 Proto2->arg_type_begin() + Skip2, Proto2->arg_type_end());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003319
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003320 // C++ [temp.func.order]p5:
3321 // The presence of unused ellipsis and default arguments has no effect on
3322 // the partial ordering of function templates.
3323 if (Args1.size() > NumCallArguments)
3324 Args1.resize(NumCallArguments);
3325 if (Args2.size() > NumCallArguments)
3326 Args2.resize(NumCallArguments);
3327 if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(),
3328 Args1.data(), Args1.size(), Info, Deduced,
3329 TDF_None, /*PartialOrdering=*/true,
Douglas Gregorb939a192011-01-21 17:29:42 +00003330 RefParamComparisons))
Douglas Gregor8a514912009-09-14 18:39:43 +00003331 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003332
Douglas Gregor8a514912009-09-14 18:39:43 +00003333 break;
3334 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003335
Douglas Gregor8a514912009-09-14 18:39:43 +00003336 case TPOC_Conversion:
3337 // - In the context of a call to a conversion operator, the return types
3338 // of the conversion function templates are used.
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003339 if (DeduceTemplateArguments(S, TemplateParams, Proto2->getResultType(),
3340 Proto1->getResultType(), Info, Deduced,
3341 TDF_None, /*PartialOrdering=*/true,
Douglas Gregorb939a192011-01-21 17:29:42 +00003342 RefParamComparisons))
Douglas Gregor8a514912009-09-14 18:39:43 +00003343 return false;
3344 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003345
Douglas Gregor8a514912009-09-14 18:39:43 +00003346 case TPOC_Other:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003347 // - In other contexts (14.6.6.2) the function template's function type
Douglas Gregor8a514912009-09-14 18:39:43 +00003348 // is used.
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003349 // FIXME: Don't we actually want to perform the adjustments on the parameter
3350 // types?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003351 if (DeduceTemplateArguments(S, TemplateParams, FD2->getType(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003352 FD1->getType(), Info, Deduced, TDF_None,
Douglas Gregorb939a192011-01-21 17:29:42 +00003353 /*PartialOrdering=*/true, RefParamComparisons))
Douglas Gregor8a514912009-09-14 18:39:43 +00003354 return false;
3355 break;
3356 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003357
Douglas Gregor8a514912009-09-14 18:39:43 +00003358 // C++0x [temp.deduct.partial]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003359 // In most cases, all template parameters must have values in order for
3360 // deduction to succeed, but for partial ordering purposes a template
3361 // parameter may remain without a value provided it is not used in the
Douglas Gregor8a514912009-09-14 18:39:43 +00003362 // types being used for partial ordering. [ Note: a template parameter used
3363 // in a non-deduced context is considered used. -end note]
3364 unsigned ArgIdx = 0, NumArgs = Deduced.size();
3365 for (; ArgIdx != NumArgs; ++ArgIdx)
3366 if (Deduced[ArgIdx].isNull())
3367 break;
3368
3369 if (ArgIdx == NumArgs) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003370 // All template arguments were deduced. FT1 is at least as specialized
Douglas Gregor8a514912009-09-14 18:39:43 +00003371 // as FT2.
3372 return true;
3373 }
3374
Douglas Gregore73bb602009-09-14 21:25:05 +00003375 // Figure out which template parameters were used.
Douglas Gregor8a514912009-09-14 18:39:43 +00003376 llvm::SmallVector<bool, 4> UsedParameters;
3377 UsedParameters.resize(TemplateParams->size());
3378 switch (TPOC) {
3379 case TPOC_Call: {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003380 unsigned NumParams = std::min(NumCallArguments,
3381 std::min(Proto1->getNumArgs(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003382 Proto2->getNumArgs()));
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003383 if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003384 ::MarkUsedTemplateParameters(S, Method2->getThisType(S.Context), false,
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003385 TemplateParams->getDepth(), UsedParameters);
3386 for (unsigned I = Skip2; I < NumParams; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003387 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003388 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00003389 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00003390 break;
3391 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003392
Douglas Gregor8a514912009-09-14 18:39:43 +00003393 case TPOC_Conversion:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003394 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003395 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00003396 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00003397 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003398
Douglas Gregor8a514912009-09-14 18:39:43 +00003399 case TPOC_Other:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003400 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003401 TemplateParams->getDepth(),
3402 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00003403 break;
3404 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003405
Douglas Gregor8a514912009-09-14 18:39:43 +00003406 for (; ArgIdx != NumArgs; ++ArgIdx)
3407 // If this argument had no value deduced but was used in one of the types
3408 // used for partial ordering, then deduction fails.
3409 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
3410 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003411
Douglas Gregor8a514912009-09-14 18:39:43 +00003412 return true;
3413}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003414
Douglas Gregor9da95e62011-01-16 16:03:23 +00003415/// \brief Determine whether this a function template whose parameter-type-list
3416/// ends with a function parameter pack.
3417static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) {
3418 FunctionDecl *Function = FunTmpl->getTemplatedDecl();
3419 unsigned NumParams = Function->getNumParams();
3420 if (NumParams == 0)
3421 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003422
Douglas Gregor9da95e62011-01-16 16:03:23 +00003423 ParmVarDecl *Last = Function->getParamDecl(NumParams - 1);
3424 if (!Last->isParameterPack())
3425 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003426
Douglas Gregor9da95e62011-01-16 16:03:23 +00003427 // Make sure that no previous parameter is a parameter pack.
3428 while (--NumParams > 0) {
3429 if (Function->getParamDecl(NumParams - 1)->isParameterPack())
3430 return false;
3431 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003432
Douglas Gregor9da95e62011-01-16 16:03:23 +00003433 return true;
3434}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003435
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003436/// \brief Returns the more specialized function template according
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003437/// to the rules of function template partial ordering (C++ [temp.func.order]).
3438///
3439/// \param FT1 the first function template
3440///
3441/// \param FT2 the second function template
3442///
Douglas Gregor8a514912009-09-14 18:39:43 +00003443/// \param TPOC the context in which we are performing partial ordering of
3444/// function templates.
Mike Stump1eb44332009-09-09 15:08:12 +00003445///
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003446/// \param NumCallArguments The number of arguments in a call, used only
3447/// when \c TPOC is \c TPOC_Call.
3448///
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003449/// \returns the more specialized function template. If neither
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003450/// template is more specialized, returns NULL.
3451FunctionTemplateDecl *
3452Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
3453 FunctionTemplateDecl *FT2,
John McCall5769d612010-02-08 23:07:23 +00003454 SourceLocation Loc,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003455 TemplatePartialOrderingContext TPOC,
3456 unsigned NumCallArguments) {
Douglas Gregorb939a192011-01-21 17:29:42 +00003457 llvm::SmallVector<RefParamPartialOrderingComparison, 4> RefParamComparisons;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003458 bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003459 NumCallArguments, 0);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003460 bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003461 NumCallArguments,
Douglas Gregorb939a192011-01-21 17:29:42 +00003462 &RefParamComparisons);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003463
Douglas Gregor8a514912009-09-14 18:39:43 +00003464 if (Better1 != Better2) // We have a clear winner
3465 return Better1? FT1 : FT2;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003466
Douglas Gregor8a514912009-09-14 18:39:43 +00003467 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003468 return 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00003469
Douglas Gregor8a514912009-09-14 18:39:43 +00003470 // C++0x [temp.deduct.partial]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003471 // If for each type being considered a given template is at least as
Douglas Gregor8a514912009-09-14 18:39:43 +00003472 // specialized for all types and more specialized for some set of types and
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003473 // the other template is not more specialized for any types or is not at
Douglas Gregor8a514912009-09-14 18:39:43 +00003474 // least as specialized for any types, then the given template is more
3475 // specialized than the other template. Otherwise, neither template is more
3476 // specialized than the other.
3477 Better1 = false;
3478 Better2 = false;
Douglas Gregorb939a192011-01-21 17:29:42 +00003479 for (unsigned I = 0, N = RefParamComparisons.size(); I != N; ++I) {
Douglas Gregor8a514912009-09-14 18:39:43 +00003480 // C++0x [temp.deduct.partial]p9:
3481 // If, for a given type, deduction succeeds in both directions (i.e., the
Douglas Gregorb939a192011-01-21 17:29:42 +00003482 // types are identical after the transformations above) and both P and A
3483 // were reference types (before being replaced with the type referred to
3484 // above):
3485
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003486 // -- if the type from the argument template was an lvalue reference
Douglas Gregorb939a192011-01-21 17:29:42 +00003487 // and the type from the parameter template was not, the argument
3488 // type is considered to be more specialized than the other;
3489 // otherwise,
3490 if (!RefParamComparisons[I].ArgIsRvalueRef &&
3491 RefParamComparisons[I].ParamIsRvalueRef) {
3492 Better2 = true;
3493 if (Better1)
3494 return 0;
3495 continue;
3496 } else if (!RefParamComparisons[I].ParamIsRvalueRef &&
3497 RefParamComparisons[I].ArgIsRvalueRef) {
3498 Better1 = true;
3499 if (Better2)
3500 return 0;
3501 continue;
Douglas Gregor8a514912009-09-14 18:39:43 +00003502 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003503
Douglas Gregorb939a192011-01-21 17:29:42 +00003504 // -- if the type from the argument template is more cv-qualified than
3505 // the type from the parameter template (as described above), the
3506 // argument type is considered to be more specialized than the
3507 // other; otherwise,
3508 switch (RefParamComparisons[I].Qualifiers) {
3509 case NeitherMoreQualified:
3510 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003511
Douglas Gregorb939a192011-01-21 17:29:42 +00003512 case ParamMoreQualified:
3513 Better1 = true;
3514 if (Better2)
3515 return 0;
3516 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003517
Douglas Gregorb939a192011-01-21 17:29:42 +00003518 case ArgMoreQualified:
3519 Better2 = true;
3520 if (Better1)
3521 return 0;
3522 continue;
3523 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003524
Douglas Gregorb939a192011-01-21 17:29:42 +00003525 // -- neither type is more specialized than the other.
Douglas Gregor8a514912009-09-14 18:39:43 +00003526 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003527
Douglas Gregor8a514912009-09-14 18:39:43 +00003528 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003529 if (Better1)
3530 return FT1;
Douglas Gregor8a514912009-09-14 18:39:43 +00003531 else if (Better2)
3532 return FT2;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003533
Douglas Gregor9da95e62011-01-16 16:03:23 +00003534 // FIXME: This mimics what GCC implements, but doesn't match up with the
3535 // proposed resolution for core issue 692. This area needs to be sorted out,
3536 // but for now we attempt to maintain compatibility.
3537 bool Variadic1 = isVariadicFunctionTemplate(FT1);
3538 bool Variadic2 = isVariadicFunctionTemplate(FT2);
3539 if (Variadic1 != Variadic2)
3540 return Variadic1? FT2 : FT1;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003541
Douglas Gregor9da95e62011-01-16 16:03:23 +00003542 return 0;
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003543}
Douglas Gregor83314aa2009-07-08 20:55:45 +00003544
Douglas Gregord5a423b2009-09-25 18:43:00 +00003545/// \brief Determine if the two templates are equivalent.
3546static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
3547 if (T1 == T2)
3548 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003549
Douglas Gregord5a423b2009-09-25 18:43:00 +00003550 if (!T1 || !T2)
3551 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003552
Douglas Gregord5a423b2009-09-25 18:43:00 +00003553 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
3554}
3555
3556/// \brief Retrieve the most specialized of the given function template
3557/// specializations.
3558///
John McCallc373d482010-01-27 01:50:18 +00003559/// \param SpecBegin the start iterator of the function template
3560/// specializations that we will be comparing.
Douglas Gregord5a423b2009-09-25 18:43:00 +00003561///
John McCallc373d482010-01-27 01:50:18 +00003562/// \param SpecEnd the end iterator of the function template
3563/// specializations, paired with \p SpecBegin.
Douglas Gregord5a423b2009-09-25 18:43:00 +00003564///
3565/// \param TPOC the partial ordering context to use to compare the function
3566/// template specializations.
3567///
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003568/// \param NumCallArguments The number of arguments in a call, used only
3569/// when \c TPOC is \c TPOC_Call.
3570///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003571/// \param Loc the location where the ambiguity or no-specializations
Douglas Gregord5a423b2009-09-25 18:43:00 +00003572/// diagnostic should occur.
3573///
3574/// \param NoneDiag partial diagnostic used to diagnose cases where there are
3575/// no matching candidates.
3576///
3577/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
3578/// occurs.
3579///
3580/// \param CandidateDiag partial diagnostic used for each function template
3581/// specialization that is a candidate in the ambiguous ordering. One parameter
3582/// in this diagnostic should be unbound, which will correspond to the string
3583/// describing the template arguments for the function template specialization.
3584///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003585/// \param Index if non-NULL and the result of this function is non-nULL,
Douglas Gregord5a423b2009-09-25 18:43:00 +00003586/// receives the index corresponding to the resulting function template
3587/// specialization.
3588///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003589/// \returns the most specialized function template specialization, if
John McCallc373d482010-01-27 01:50:18 +00003590/// found. Otherwise, returns SpecEnd.
Douglas Gregord5a423b2009-09-25 18:43:00 +00003591///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003592/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
Douglas Gregord5a423b2009-09-25 18:43:00 +00003593/// template argument deduction.
John McCallc373d482010-01-27 01:50:18 +00003594UnresolvedSetIterator
3595Sema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003596 UnresolvedSetIterator SpecEnd,
John McCallc373d482010-01-27 01:50:18 +00003597 TemplatePartialOrderingContext TPOC,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003598 unsigned NumCallArguments,
John McCallc373d482010-01-27 01:50:18 +00003599 SourceLocation Loc,
3600 const PartialDiagnostic &NoneDiag,
3601 const PartialDiagnostic &AmbigDiag,
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003602 const PartialDiagnostic &CandidateDiag,
3603 bool Complain) {
John McCallc373d482010-01-27 01:50:18 +00003604 if (SpecBegin == SpecEnd) {
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003605 if (Complain)
3606 Diag(Loc, NoneDiag);
John McCallc373d482010-01-27 01:50:18 +00003607 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00003608 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003609
3610 if (SpecBegin + 1 == SpecEnd)
John McCallc373d482010-01-27 01:50:18 +00003611 return SpecBegin;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003612
Douglas Gregord5a423b2009-09-25 18:43:00 +00003613 // Find the function template that is better than all of the templates it
3614 // has been compared to.
John McCallc373d482010-01-27 01:50:18 +00003615 UnresolvedSetIterator Best = SpecBegin;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003616 FunctionTemplateDecl *BestTemplate
John McCallc373d482010-01-27 01:50:18 +00003617 = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00003618 assert(BestTemplate && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00003619 for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
3620 FunctionTemplateDecl *Challenger
3621 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00003622 assert(Challenger && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00003623 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003624 Loc, TPOC, NumCallArguments),
Douglas Gregord5a423b2009-09-25 18:43:00 +00003625 Challenger)) {
3626 Best = I;
3627 BestTemplate = Challenger;
3628 }
3629 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003630
Douglas Gregord5a423b2009-09-25 18:43:00 +00003631 // Make sure that the "best" function template is more specialized than all
3632 // of the others.
3633 bool Ambiguous = false;
John McCallc373d482010-01-27 01:50:18 +00003634 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
3635 FunctionTemplateDecl *Challenger
3636 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00003637 if (I != Best &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003638 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003639 Loc, TPOC, NumCallArguments),
Douglas Gregord5a423b2009-09-25 18:43:00 +00003640 BestTemplate)) {
3641 Ambiguous = true;
3642 break;
3643 }
3644 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003645
Douglas Gregord5a423b2009-09-25 18:43:00 +00003646 if (!Ambiguous) {
3647 // We found an answer. Return it.
John McCallc373d482010-01-27 01:50:18 +00003648 return Best;
Douglas Gregord5a423b2009-09-25 18:43:00 +00003649 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003650
Douglas Gregord5a423b2009-09-25 18:43:00 +00003651 // Diagnose the ambiguity.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003652 if (Complain)
3653 Diag(Loc, AmbigDiag);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003654
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003655 if (Complain)
Douglas Gregord5a423b2009-09-25 18:43:00 +00003656 // FIXME: Can we order the candidates in some sane way?
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003657 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
3658 Diag((*I)->getLocation(), CandidateDiag)
3659 << getTemplateArgumentBindingsText(
3660 cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
John McCallc373d482010-01-27 01:50:18 +00003661 *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003662
John McCallc373d482010-01-27 01:50:18 +00003663 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00003664}
3665
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003666/// \brief Returns the more specialized class template partial specialization
3667/// according to the rules of partial ordering of class template partial
3668/// specializations (C++ [temp.class.order]).
3669///
3670/// \param PS1 the first class template partial specialization
3671///
3672/// \param PS2 the second class template partial specialization
3673///
3674/// \returns the more specialized class template partial specialization. If
3675/// neither partial specialization is more specialized, returns NULL.
3676ClassTemplatePartialSpecializationDecl *
3677Sema::getMoreSpecializedPartialSpecialization(
3678 ClassTemplatePartialSpecializationDecl *PS1,
John McCall5769d612010-02-08 23:07:23 +00003679 ClassTemplatePartialSpecializationDecl *PS2,
3680 SourceLocation Loc) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003681 // C++ [temp.class.order]p1:
3682 // For two class template partial specializations, the first is at least as
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003683 // specialized as the second if, given the following rewrite to two
3684 // function templates, the first function template is at least as
3685 // specialized as the second according to the ordering rules for function
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003686 // templates (14.6.6.2):
3687 // - the first function template has the same template parameters as the
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003688 // first partial specialization and has a single function parameter
3689 // whose type is a class template specialization with the template
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003690 // arguments of the first partial specialization, and
3691 // - the second function template has the same template parameters as the
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003692 // second partial specialization and has a single function parameter
3693 // whose type is a class template specialization with the template
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003694 // arguments of the second partial specialization.
3695 //
Douglas Gregor31dce8f2010-04-29 06:21:43 +00003696 // Rather than synthesize function templates, we merely perform the
3697 // equivalent partial ordering by performing deduction directly on
3698 // the template arguments of the class template partial
3699 // specializations. This computation is slightly simpler than the
3700 // general problem of function template partial ordering, because
3701 // class template partial specializations are more constrained. We
3702 // know that every template parameter is deducible from the class
3703 // template partial specialization's template arguments, for
3704 // example.
Douglas Gregor02024a92010-03-28 02:42:43 +00003705 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
John McCall2a7fb272010-08-25 05:32:35 +00003706 TemplateDeductionInfo Info(Context, Loc);
John McCall31f17ec2010-04-27 00:57:59 +00003707
3708 QualType PT1 = PS1->getInjectedSpecializationType();
3709 QualType PT2 = PS2->getInjectedSpecializationType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003710
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003711 // Determine whether PS1 is at least as specialized as PS2
3712 Deduced.resize(PS2->getTemplateParameters()->size());
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003713 bool Better1 = !::DeduceTemplateArguments(*this, PS2->getTemplateParameters(),
3714 PT2, PT1, Info, Deduced, TDF_None,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003715 /*PartialOrdering=*/true,
Douglas Gregorb939a192011-01-21 17:29:42 +00003716 /*RefParamComparisons=*/0);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00003717 if (Better1) {
3718 InstantiatingTemplate Inst(*this, PS2->getLocation(), PS2,
3719 Deduced.data(), Deduced.size(), Info);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003720 Better1 = !::FinishTemplateArgumentDeduction(*this, PS2,
3721 PS1->getTemplateArgs(),
Douglas Gregor516e6e02010-04-29 06:31:36 +00003722 Deduced, Info);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00003723 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003724
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003725 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregordb0d4b72009-11-11 23:06:43 +00003726 Deduced.clear();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003727 Deduced.resize(PS1->getTemplateParameters()->size());
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003728 bool Better2 = !::DeduceTemplateArguments(*this, PS1->getTemplateParameters(),
3729 PT1, PT2, Info, Deduced, TDF_None,
3730 /*PartialOrdering=*/true,
Douglas Gregorb939a192011-01-21 17:29:42 +00003731 /*RefParamComparisons=*/0);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00003732 if (Better2) {
3733 InstantiatingTemplate Inst(*this, PS1->getLocation(), PS1,
3734 Deduced.data(), Deduced.size(), Info);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003735 Better2 = !::FinishTemplateArgumentDeduction(*this, PS1,
3736 PS2->getTemplateArgs(),
Douglas Gregor516e6e02010-04-29 06:31:36 +00003737 Deduced, Info);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00003738 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003739
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003740 if (Better1 == Better2)
3741 return 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003742
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003743 return Better1? PS1 : PS2;
3744}
3745
Mike Stump1eb44332009-09-09 15:08:12 +00003746static void
Douglas Gregore73bb602009-09-14 21:25:05 +00003747MarkUsedTemplateParameters(Sema &SemaRef,
3748 const TemplateArgument &TemplateArg,
3749 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003750 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003751 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003752
Douglas Gregore73bb602009-09-14 21:25:05 +00003753/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00003754/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00003755static void
Douglas Gregore73bb602009-09-14 21:25:05 +00003756MarkUsedTemplateParameters(Sema &SemaRef,
3757 const Expr *E,
3758 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003759 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003760 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregorbe230c32011-01-03 17:17:50 +00003761 // We can deduce from a pack expansion.
3762 if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E))
3763 E = Expansion->getPattern();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003764
Douglas Gregor54c53cc2011-01-04 23:35:54 +00003765 // Skip through any implicit casts we added while type-checking.
3766 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
3767 E = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003768
3769 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
Douglas Gregore73bb602009-09-14 21:25:05 +00003770 // find other occurrences of template parameters.
Douglas Gregorf6ddb732009-06-18 18:45:36 +00003771 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregorc781f9c2010-01-14 18:13:22 +00003772 if (!DRE)
Douglas Gregor031a5882009-06-13 00:26:55 +00003773 return;
3774
Mike Stump1eb44332009-09-09 15:08:12 +00003775 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00003776 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
3777 if (!NTTP)
3778 return;
3779
Douglas Gregored9c0f92009-10-29 00:04:11 +00003780 if (NTTP->getDepth() == Depth)
3781 Used[NTTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00003782}
3783
Douglas Gregore73bb602009-09-14 21:25:05 +00003784/// \brief Mark the template parameters that are used by the given
3785/// nested name specifier.
3786static void
3787MarkUsedTemplateParameters(Sema &SemaRef,
3788 NestedNameSpecifier *NNS,
3789 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003790 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003791 llvm::SmallVectorImpl<bool> &Used) {
3792 if (!NNS)
3793 return;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003794
Douglas Gregored9c0f92009-10-29 00:04:11 +00003795 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
3796 Used);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003797 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003798 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003799}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003800
Douglas Gregore73bb602009-09-14 21:25:05 +00003801/// \brief Mark the template parameters that are used by the given
3802/// template name.
3803static void
3804MarkUsedTemplateParameters(Sema &SemaRef,
3805 TemplateName Name,
3806 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003807 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003808 llvm::SmallVectorImpl<bool> &Used) {
3809 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
3810 if (TemplateTemplateParmDecl *TTP
Douglas Gregored9c0f92009-10-29 00:04:11 +00003811 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
3812 if (TTP->getDepth() == Depth)
3813 Used[TTP->getIndex()] = true;
3814 }
Douglas Gregore73bb602009-09-14 21:25:05 +00003815 return;
3816 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003817
Douglas Gregor788cd062009-11-11 01:00:40 +00003818 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003819 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
Douglas Gregor788cd062009-11-11 01:00:40 +00003820 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003821 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003822 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003823 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003824}
3825
3826/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00003827/// type.
Mike Stump1eb44332009-09-09 15:08:12 +00003828static void
Douglas Gregore73bb602009-09-14 21:25:05 +00003829MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
3830 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003831 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003832 llvm::SmallVectorImpl<bool> &Used) {
3833 if (T.isNull())
3834 return;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003835
Douglas Gregor031a5882009-06-13 00:26:55 +00003836 // Non-dependent types have nothing deducible
3837 if (!T->isDependentType())
3838 return;
3839
3840 T = SemaRef.Context.getCanonicalType(T);
3841 switch (T->getTypeClass()) {
Douglas Gregor031a5882009-06-13 00:26:55 +00003842 case Type::Pointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00003843 MarkUsedTemplateParameters(SemaRef,
3844 cast<PointerType>(T)->getPointeeType(),
3845 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003846 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003847 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003848 break;
3849
3850 case Type::BlockPointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00003851 MarkUsedTemplateParameters(SemaRef,
3852 cast<BlockPointerType>(T)->getPointeeType(),
3853 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003854 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003855 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003856 break;
3857
3858 case Type::LValueReference:
3859 case Type::RValueReference:
Douglas Gregore73bb602009-09-14 21:25:05 +00003860 MarkUsedTemplateParameters(SemaRef,
3861 cast<ReferenceType>(T)->getPointeeType(),
3862 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003863 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003864 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003865 break;
3866
3867 case Type::MemberPointer: {
3868 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore73bb602009-09-14 21:25:05 +00003869 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003870 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003871 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003872 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003873 break;
3874 }
3875
3876 case Type::DependentSizedArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00003877 MarkUsedTemplateParameters(SemaRef,
3878 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003879 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003880 // Fall through to check the element type
3881
3882 case Type::ConstantArray:
3883 case Type::IncompleteArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00003884 MarkUsedTemplateParameters(SemaRef,
3885 cast<ArrayType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003886 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003887 break;
3888
3889 case Type::Vector:
3890 case Type::ExtVector:
Douglas Gregore73bb602009-09-14 21:25:05 +00003891 MarkUsedTemplateParameters(SemaRef,
3892 cast<VectorType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003893 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003894 break;
3895
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00003896 case Type::DependentSizedExtVector: {
3897 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00003898 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00003899 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003900 Depth, Used);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003901 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003902 Depth, Used);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00003903 break;
3904 }
3905
Douglas Gregor031a5882009-06-13 00:26:55 +00003906 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00003907 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00003908 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003909 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003910 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore73bb602009-09-14 21:25:05 +00003911 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003912 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003913 break;
3914 }
3915
Douglas Gregored9c0f92009-10-29 00:04:11 +00003916 case Type::TemplateTypeParm: {
3917 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
3918 if (TTP->getDepth() == Depth)
3919 Used[TTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00003920 break;
Douglas Gregored9c0f92009-10-29 00:04:11 +00003921 }
Douglas Gregor031a5882009-06-13 00:26:55 +00003922
Douglas Gregor0bc15d92011-01-14 05:11:40 +00003923 case Type::SubstTemplateTypeParmPack: {
3924 const SubstTemplateTypeParmPackType *Subst
3925 = cast<SubstTemplateTypeParmPackType>(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003926 MarkUsedTemplateParameters(SemaRef,
Douglas Gregor0bc15d92011-01-14 05:11:40 +00003927 QualType(Subst->getReplacedParameter(), 0),
3928 OnlyDeduced, Depth, Used);
3929 MarkUsedTemplateParameters(SemaRef, Subst->getArgumentPack(),
3930 OnlyDeduced, Depth, Used);
3931 break;
3932 }
3933
John McCall31f17ec2010-04-27 00:57:59 +00003934 case Type::InjectedClassName:
3935 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
3936 // fall through
3937
Douglas Gregor031a5882009-06-13 00:26:55 +00003938 case Type::TemplateSpecialization: {
Mike Stump1eb44332009-09-09 15:08:12 +00003939 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00003940 = cast<TemplateSpecializationType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00003941 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003942 Depth, Used);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003943
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003944 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003945 // If the template argument list of P contains a pack expansion that is not
3946 // the last template argument, the entire template argument list is a
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003947 // non-deduced context.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003948 if (OnlyDeduced &&
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003949 hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs()))
3950 break;
3951
Douglas Gregore73bb602009-09-14 21:25:05 +00003952 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00003953 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
3954 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003955 break;
3956 }
3957
Douglas Gregore73bb602009-09-14 21:25:05 +00003958 case Type::Complex:
3959 if (!OnlyDeduced)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003960 MarkUsedTemplateParameters(SemaRef,
Douglas Gregore73bb602009-09-14 21:25:05 +00003961 cast<ComplexType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003962 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003963 break;
3964
Douglas Gregor4714c122010-03-31 17:34:00 +00003965 case Type::DependentName:
Douglas Gregore73bb602009-09-14 21:25:05 +00003966 if (!OnlyDeduced)
3967 MarkUsedTemplateParameters(SemaRef,
Douglas Gregor4714c122010-03-31 17:34:00 +00003968 cast<DependentNameType>(T)->getQualifier(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003969 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003970 break;
3971
John McCall33500952010-06-11 00:33:02 +00003972 case Type::DependentTemplateSpecialization: {
3973 const DependentTemplateSpecializationType *Spec
3974 = cast<DependentTemplateSpecializationType>(T);
3975 if (!OnlyDeduced)
3976 MarkUsedTemplateParameters(SemaRef, Spec->getQualifier(),
3977 OnlyDeduced, Depth, Used);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003978
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003979 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003980 // If the template argument list of P contains a pack expansion that is not
3981 // the last template argument, the entire template argument list is a
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003982 // non-deduced context.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003983 if (OnlyDeduced &&
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003984 hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs()))
3985 break;
3986
John McCall33500952010-06-11 00:33:02 +00003987 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
3988 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
3989 Used);
3990 break;
3991 }
3992
John McCallad5e7382010-03-01 23:49:17 +00003993 case Type::TypeOf:
3994 if (!OnlyDeduced)
3995 MarkUsedTemplateParameters(SemaRef,
3996 cast<TypeOfType>(T)->getUnderlyingType(),
3997 OnlyDeduced, Depth, Used);
3998 break;
3999
4000 case Type::TypeOfExpr:
4001 if (!OnlyDeduced)
4002 MarkUsedTemplateParameters(SemaRef,
4003 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
4004 OnlyDeduced, Depth, Used);
4005 break;
4006
4007 case Type::Decltype:
4008 if (!OnlyDeduced)
4009 MarkUsedTemplateParameters(SemaRef,
4010 cast<DecltypeType>(T)->getUnderlyingExpr(),
4011 OnlyDeduced, Depth, Used);
4012 break;
4013
Sean Huntca63c202011-05-24 22:41:36 +00004014 case Type::UnaryTransform:
4015 if (!OnlyDeduced)
4016 MarkUsedTemplateParameters(SemaRef,
4017 cast<UnaryTransformType>(T)->getUnderlyingType(),
4018 OnlyDeduced, Depth, Used);
4019 break;
4020
Douglas Gregor7536dd52010-12-20 02:24:11 +00004021 case Type::PackExpansion:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004022 MarkUsedTemplateParameters(SemaRef,
Douglas Gregor7536dd52010-12-20 02:24:11 +00004023 cast<PackExpansionType>(T)->getPattern(),
4024 OnlyDeduced, Depth, Used);
4025 break;
4026
Richard Smith34b41d92011-02-20 03:19:35 +00004027 case Type::Auto:
4028 MarkUsedTemplateParameters(SemaRef,
4029 cast<AutoType>(T)->getDeducedType(),
4030 OnlyDeduced, Depth, Used);
4031
Douglas Gregore73bb602009-09-14 21:25:05 +00004032 // None of these types have any template parameters in them.
Douglas Gregor031a5882009-06-13 00:26:55 +00004033 case Type::Builtin:
Douglas Gregor031a5882009-06-13 00:26:55 +00004034 case Type::VariableArray:
4035 case Type::FunctionNoProto:
4036 case Type::Record:
4037 case Type::Enum:
Douglas Gregor031a5882009-06-13 00:26:55 +00004038 case Type::ObjCInterface:
John McCallc12c5bb2010-05-15 11:32:37 +00004039 case Type::ObjCObject:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00004040 case Type::ObjCObjectPointer:
John McCalled976492009-12-04 22:46:56 +00004041 case Type::UnresolvedUsing:
Douglas Gregor031a5882009-06-13 00:26:55 +00004042#define TYPE(Class, Base)
4043#define ABSTRACT_TYPE(Class, Base)
4044#define DEPENDENT_TYPE(Class, Base)
4045#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
4046#include "clang/AST/TypeNodes.def"
4047 break;
4048 }
4049}
4050
Douglas Gregore73bb602009-09-14 21:25:05 +00004051/// \brief Mark the template parameters that are used by this
Douglas Gregor031a5882009-06-13 00:26:55 +00004052/// template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00004053static void
Douglas Gregore73bb602009-09-14 21:25:05 +00004054MarkUsedTemplateParameters(Sema &SemaRef,
4055 const TemplateArgument &TemplateArg,
4056 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004057 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00004058 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00004059 switch (TemplateArg.getKind()) {
4060 case TemplateArgument::Null:
4061 case TemplateArgument::Integral:
Douglas Gregor788cd062009-11-11 01:00:40 +00004062 case TemplateArgument::Declaration:
Douglas Gregor031a5882009-06-13 00:26:55 +00004063 break;
Mike Stump1eb44332009-09-09 15:08:12 +00004064
Douglas Gregor031a5882009-06-13 00:26:55 +00004065 case TemplateArgument::Type:
Douglas Gregore73bb602009-09-14 21:25:05 +00004066 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004067 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00004068 break;
4069
Douglas Gregor788cd062009-11-11 01:00:40 +00004070 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00004071 case TemplateArgument::TemplateExpansion:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004072 MarkUsedTemplateParameters(SemaRef,
4073 TemplateArg.getAsTemplateOrTemplatePattern(),
Douglas Gregor788cd062009-11-11 01:00:40 +00004074 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00004075 break;
4076
4077 case TemplateArgument::Expression:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004078 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004079 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00004080 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004081
Anders Carlssond01b1da2009-06-15 17:04:53 +00004082 case TemplateArgument::Pack:
Douglas Gregore73bb602009-09-14 21:25:05 +00004083 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
4084 PEnd = TemplateArg.pack_end();
4085 P != PEnd; ++P)
Douglas Gregored9c0f92009-10-29 00:04:11 +00004086 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssond01b1da2009-06-15 17:04:53 +00004087 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00004088 }
4089}
4090
4091/// \brief Mark the template parameters can be deduced by the given
4092/// template argument list.
4093///
4094/// \param TemplateArgs the template argument list from which template
4095/// parameters will be deduced.
4096///
4097/// \param Deduced a bit vector whose elements will be set to \c true
4098/// to indicate when the corresponding template parameter will be
4099/// deduced.
Mike Stump1eb44332009-09-09 15:08:12 +00004100void
Douglas Gregore73bb602009-09-14 21:25:05 +00004101Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004102 bool OnlyDeduced, unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00004103 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor7b976ec2010-12-23 01:24:45 +00004104 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004105 // If the template argument list of P contains a pack expansion that is not
4106 // the last template argument, the entire template argument list is a
Douglas Gregor7b976ec2010-12-23 01:24:45 +00004107 // non-deduced context.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004108 if (OnlyDeduced &&
Douglas Gregor7b976ec2010-12-23 01:24:45 +00004109 hasPackExpansionBeforeEnd(TemplateArgs.data(), TemplateArgs.size()))
4110 return;
4111
Douglas Gregor031a5882009-06-13 00:26:55 +00004112 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004113 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004114 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00004115}
Douglas Gregor63f07c52009-09-18 23:21:38 +00004116
4117/// \brief Marks all of the template parameters that will be deduced by a
4118/// call to the given function template.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004119void
Douglas Gregor02024a92010-03-28 02:42:43 +00004120Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
4121 llvm::SmallVectorImpl<bool> &Deduced) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004122 TemplateParameterList *TemplateParams
Douglas Gregor63f07c52009-09-18 23:21:38 +00004123 = FunctionTemplate->getTemplateParameters();
4124 Deduced.clear();
4125 Deduced.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004126
Douglas Gregor63f07c52009-09-18 23:21:38 +00004127 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
4128 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
4129 ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00004130 true, TemplateParams->getDepth(), Deduced);
Douglas Gregor63f07c52009-09-18 23:21:38 +00004131}