blob: 202a736f3fdefa5c7ad53928e5950ebc765af428 [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"
Douglas Gregor20a55e22010-12-22 18:17:10 +000015#include "clang/Sema/SemaDiagnostic.h" // FIXME: temporary!
John McCall7cd088e2010-08-24 07:21:54 +000016#include "clang/Sema/Template.h"
John McCall2a7fb272010-08-25 05:32:35 +000017#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor0b9247f2009-06-04 00:03:07 +000018#include "clang/AST/ASTContext.h"
John McCall7cd088e2010-08-24 07:21:54 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor0b9247f2009-06-04 00:03:07 +000020#include "clang/AST/DeclTemplate.h"
21#include "clang/AST/StmtVisitor.h"
22#include "clang/AST/Expr.h"
23#include "clang/AST/ExprCXX.h"
Douglas Gregore02e2622010-12-22 21:19:48 +000024#include "llvm/ADT/BitVector.h"
Richard Smith34b41d92011-02-20 03:19:35 +000025#include "TreeTransform.h"
Douglas Gregor8a514912009-09-14 18:39:43 +000026#include <algorithm>
Douglas Gregor508f1c82009-06-26 23:10:12 +000027
28namespace clang {
John McCall2a7fb272010-08-25 05:32:35 +000029 using namespace sema;
30
Douglas Gregor508f1c82009-06-26 23:10:12 +000031 /// \brief Various flags that control template argument deduction.
32 ///
33 /// These flags can be bitwise-OR'd together.
34 enum TemplateDeductionFlags {
35 /// \brief No template argument deduction flags, which indicates the
36 /// strictest results for template argument deduction (as used for, e.g.,
37 /// matching class template partial specializations).
38 TDF_None = 0,
39 /// \brief Within template argument deduction from a function call, we are
40 /// matching with a parameter type for which the original parameter was
41 /// a reference.
42 TDF_ParamWithReferenceType = 0x1,
43 /// \brief Within template argument deduction from a function call, we
44 /// are matching in a case where we ignore cv-qualifiers.
45 TDF_IgnoreQualifiers = 0x02,
46 /// \brief Within template argument deduction from a function call,
47 /// we are matching in a case where we can perform template argument
Douglas Gregor41128772009-06-26 23:27:24 +000048 /// deduction from a template-id of a derived class of the argument type.
Douglas Gregor12820292009-09-14 20:00:47 +000049 TDF_DerivedClass = 0x04,
50 /// \brief Allow non-dependent types to differ, e.g., when performing
51 /// template argument deduction from a function call where conversions
52 /// may apply.
Douglas Gregor73b3cf62011-01-25 17:19:08 +000053 TDF_SkipNonDependent = 0x08,
54 /// \brief Whether we are performing template argument deduction for
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000055 /// parameters and arguments in a top-level template argument
Douglas Gregor73b3cf62011-01-25 17:19:08 +000056 TDF_TopLevelParameterTypeList = 0x10
Douglas Gregor508f1c82009-06-26 23:10:12 +000057 };
58}
59
Douglas Gregor0b9247f2009-06-04 00:03:07 +000060using namespace clang;
61
Douglas Gregor9d0e4412010-03-26 05:50:28 +000062/// \brief Compare two APSInts, extending and switching the sign as
63/// necessary to compare their values regardless of underlying type.
64static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) {
65 if (Y.getBitWidth() > X.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +000066 X = X.extend(Y.getBitWidth());
Douglas Gregor9d0e4412010-03-26 05:50:28 +000067 else if (Y.getBitWidth() < X.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +000068 Y = Y.extend(X.getBitWidth());
Douglas Gregor9d0e4412010-03-26 05:50:28 +000069
70 // If there is a signedness mismatch, correct it.
71 if (X.isSigned() != Y.isSigned()) {
72 // If the signed value is negative, then the values cannot be the same.
73 if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative()))
74 return false;
75
76 Y.setIsSigned(true);
77 X.setIsSigned(true);
78 }
79
80 return X == Y;
81}
82
Douglas Gregorf67875d2009-06-12 18:26:56 +000083static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +000084DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +000085 TemplateParameterList *TemplateParams,
86 const TemplateArgument &Param,
Douglas Gregor77d6bb92011-01-11 22:21:24 +000087 TemplateArgument Arg,
John McCall2a7fb272010-08-25 05:32:35 +000088 TemplateDeductionInfo &Info,
Douglas Gregor0972c862010-12-22 18:55:49 +000089 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced);
Douglas Gregord708c722009-06-09 16:35:58 +000090
Douglas Gregorb939a192011-01-21 17:29:42 +000091/// \brief Whether template argument deduction for two reference parameters
92/// resulted in the argument type, parameter type, or neither type being more
93/// qualified than the other.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000094enum DeductionQualifierComparison {
95 NeitherMoreQualified = 0,
96 ParamMoreQualified,
97 ArgMoreQualified
Douglas Gregor5c7bf422011-01-11 17:34:58 +000098};
99
Douglas Gregorb939a192011-01-21 17:29:42 +0000100/// \brief Stores the result of comparing two reference parameters while
101/// performing template argument deduction for partial ordering of function
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000102/// templates.
Douglas Gregorb939a192011-01-21 17:29:42 +0000103struct RefParamPartialOrderingComparison {
104 /// \brief Whether the parameter type is an rvalue reference type.
105 bool ParamIsRvalueRef;
106 /// \brief Whether the argument type is an rvalue reference type.
107 bool ArgIsRvalueRef;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000108
Douglas Gregorb939a192011-01-21 17:29:42 +0000109 /// \brief Whether the parameter or argument (or neither) is more qualified.
110 DeductionQualifierComparison Qualifiers;
111};
112
113
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000114
Douglas Gregor20a55e22010-12-22 18:17:10 +0000115static Sema::TemplateDeductionResult
116DeduceTemplateArguments(Sema &S,
117 TemplateParameterList *TemplateParams,
Douglas Gregor603cfb42011-01-05 23:12:31 +0000118 QualType Param,
119 QualType Arg,
120 TemplateDeductionInfo &Info,
121 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000122 unsigned TDF,
123 bool PartialOrdering = false,
Douglas Gregorb939a192011-01-21 17:29:42 +0000124 llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *
125 RefParamComparisons = 0);
Douglas Gregor603cfb42011-01-05 23:12:31 +0000126
127static Sema::TemplateDeductionResult
128DeduceTemplateArguments(Sema &S,
129 TemplateParameterList *TemplateParams,
Douglas Gregor20a55e22010-12-22 18:17:10 +0000130 const TemplateArgument *Params, unsigned NumParams,
131 const TemplateArgument *Args, unsigned NumArgs,
132 TemplateDeductionInfo &Info,
Douglas Gregor0972c862010-12-22 18:55:49 +0000133 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
134 bool NumberOfArgumentsMustMatch = true);
Douglas Gregor20a55e22010-12-22 18:17:10 +0000135
Douglas Gregor199d9912009-06-05 00:53:49 +0000136/// \brief If the given expression is of a form that permits the deduction
137/// of a non-type template parameter, return the declaration of that
138/// non-type template parameter.
139static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
140 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
141 E = IC->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Douglas Gregor199d9912009-06-05 00:53:49 +0000143 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
144 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Douglas Gregor199d9912009-06-05 00:53:49 +0000146 return 0;
147}
148
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000149/// \brief Determine whether two declaration pointers refer to the same
150/// declaration.
151static bool isSameDeclaration(Decl *X, Decl *Y) {
152 if (!X || !Y)
153 return !X && !Y;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000154
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000155 if (NamedDecl *NX = dyn_cast<NamedDecl>(X))
156 X = NX->getUnderlyingDecl();
157 if (NamedDecl *NY = dyn_cast<NamedDecl>(Y))
158 Y = NY->getUnderlyingDecl();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000159
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000160 return X->getCanonicalDecl() == Y->getCanonicalDecl();
161}
162
163/// \brief Verify that the given, deduced template arguments are compatible.
164///
165/// \returns The deduced template argument, or a NULL template argument if
166/// the deduced template arguments were incompatible.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000167static DeducedTemplateArgument
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000168checkDeducedTemplateArguments(ASTContext &Context,
169 const DeducedTemplateArgument &X,
170 const DeducedTemplateArgument &Y) {
171 // We have no deduction for one or both of the arguments; they're compatible.
172 if (X.isNull())
173 return Y;
174 if (Y.isNull())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000175 return X;
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000176
177 switch (X.getKind()) {
178 case TemplateArgument::Null:
179 llvm_unreachable("Non-deduced template arguments handled above");
180
181 case TemplateArgument::Type:
182 // If two template type arguments have the same type, they're compatible.
183 if (Y.getKind() == TemplateArgument::Type &&
184 Context.hasSameType(X.getAsType(), Y.getAsType()))
185 return X;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000186
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000187 return DeducedTemplateArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000188
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000189 case TemplateArgument::Integral:
190 // If we deduced a constant in one case and either a dependent expression or
191 // declaration in another case, keep the integral constant.
192 // If both are integral constants with the same value, keep that value.
193 if (Y.getKind() == TemplateArgument::Expression ||
194 Y.getKind() == TemplateArgument::Declaration ||
195 (Y.getKind() == TemplateArgument::Integral &&
196 hasSameExtendedValue(*X.getAsIntegral(), *Y.getAsIntegral())))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000197 return DeducedTemplateArgument(X,
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000198 X.wasDeducedFromArrayBound() &&
199 Y.wasDeducedFromArrayBound());
200
201 // All other combinations are incompatible.
202 return DeducedTemplateArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000203
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000204 case TemplateArgument::Template:
205 if (Y.getKind() == TemplateArgument::Template &&
206 Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate()))
207 return X;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000208
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000209 // All other combinations are incompatible.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000210 return DeducedTemplateArgument();
Douglas Gregora7fc9012011-01-05 18:58:31 +0000211
212 case TemplateArgument::TemplateExpansion:
213 if (Y.getKind() == TemplateArgument::TemplateExpansion &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000214 Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(),
Douglas Gregora7fc9012011-01-05 18:58:31 +0000215 Y.getAsTemplateOrTemplatePattern()))
216 return X;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000217
Douglas Gregora7fc9012011-01-05 18:58:31 +0000218 // All other combinations are incompatible.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000219 return DeducedTemplateArgument();
Douglas Gregora7fc9012011-01-05 18:58:31 +0000220
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000221 case TemplateArgument::Expression:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000222 // If we deduced a dependent expression in one case and either an integral
223 // constant or a declaration in another case, keep the integral constant
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000224 // or declaration.
225 if (Y.getKind() == TemplateArgument::Integral ||
226 Y.getKind() == TemplateArgument::Declaration)
227 return DeducedTemplateArgument(Y, X.wasDeducedFromArrayBound() &&
228 Y.wasDeducedFromArrayBound());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000229
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000230 if (Y.getKind() == TemplateArgument::Expression) {
231 // Compare the expressions for equality
232 llvm::FoldingSetNodeID ID1, ID2;
233 X.getAsExpr()->Profile(ID1, Context, true);
234 Y.getAsExpr()->Profile(ID2, Context, true);
235 if (ID1 == ID2)
236 return X;
237 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000238
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000239 // All other combinations are incompatible.
240 return DeducedTemplateArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000241
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000242 case TemplateArgument::Declaration:
243 // If we deduced a declaration and a dependent expression, keep the
244 // declaration.
245 if (Y.getKind() == TemplateArgument::Expression)
246 return X;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000247
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000248 // If we deduced a declaration and an integral constant, keep the
249 // integral constant.
250 if (Y.getKind() == TemplateArgument::Integral)
251 return Y;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000252
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000253 // If we deduced two declarations, make sure they they refer to the
254 // same declaration.
255 if (Y.getKind() == TemplateArgument::Declaration &&
256 isSameDeclaration(X.getAsDecl(), Y.getAsDecl()))
257 return X;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000258
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000259 // All other combinations are incompatible.
260 return DeducedTemplateArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000261
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000262 case TemplateArgument::Pack:
263 if (Y.getKind() != TemplateArgument::Pack ||
264 X.pack_size() != Y.pack_size())
265 return DeducedTemplateArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000266
267 for (TemplateArgument::pack_iterator XA = X.pack_begin(),
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000268 XAEnd = X.pack_end(),
269 YA = Y.pack_begin();
270 XA != XAEnd; ++XA, ++YA) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000271 if (checkDeducedTemplateArguments(Context,
272 DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()),
Douglas Gregor135ffa72011-01-05 21:00:53 +0000273 DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound()))
274 .isNull())
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000275 return DeducedTemplateArgument();
276 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000277
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000278 return X;
279 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000280
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000281 return DeducedTemplateArgument();
282}
283
Mike Stump1eb44332009-09-09 15:08:12 +0000284/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +0000285/// from the given constant.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000286static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000287DeduceNonTypeTemplateArgument(Sema &S,
Mike Stump1eb44332009-09-09 15:08:12 +0000288 NonTypeTemplateParmDecl *NTTP,
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000289 llvm::APSInt Value, QualType ValueType,
Douglas Gregor02024a92010-03-28 02:42:43 +0000290 bool DeducedFromArrayBound,
John McCall2a7fb272010-08-25 05:32:35 +0000291 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000292 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +0000293 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000294 "Cannot deduce non-type template argument with depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000296 DeducedTemplateArgument NewDeduced(Value, ValueType, DeducedFromArrayBound);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000297 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000298 Deduced[NTTP->getIndex()],
299 NewDeduced);
300 if (Result.isNull()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000301 Info.Param = NTTP;
302 Info.FirstArg = Deduced[NTTP->getIndex()];
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000303 Info.SecondArg = NewDeduced;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000304 return Sema::TDK_Inconsistent;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000305 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000306
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000307 Deduced[NTTP->getIndex()] = Result;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000308 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000309}
310
Mike Stump1eb44332009-09-09 15:08:12 +0000311/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +0000312/// from the given type- or value-dependent expression.
313///
314/// \returns true if deduction succeeded, false otherwise.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000315static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000316DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000317 NonTypeTemplateParmDecl *NTTP,
318 Expr *Value,
John McCall2a7fb272010-08-25 05:32:35 +0000319 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000320 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +0000321 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000322 "Cannot deduce non-type template argument with depth > 0");
323 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
324 "Expression template argument must be type- or value-dependent.");
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000326 DeducedTemplateArgument NewDeduced(Value);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000327 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
328 Deduced[NTTP->getIndex()],
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000329 NewDeduced);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000330
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000331 if (Result.isNull()) {
332 Info.Param = NTTP;
333 Info.FirstArg = Deduced[NTTP->getIndex()];
334 Info.SecondArg = NewDeduced;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000335 return Sema::TDK_Inconsistent;
Douglas Gregor199d9912009-06-05 00:53:49 +0000336 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000337
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000338 Deduced[NTTP->getIndex()] = Result;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000339 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000340}
341
Douglas Gregor15755cb2009-11-13 23:45:44 +0000342/// \brief Deduce the value of the given non-type template parameter
343/// from the given declaration.
344///
345/// \returns true if deduction succeeded, false otherwise.
346static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000347DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregor15755cb2009-11-13 23:45:44 +0000348 NonTypeTemplateParmDecl *NTTP,
349 Decl *D,
John McCall2a7fb272010-08-25 05:32:35 +0000350 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000351 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor15755cb2009-11-13 23:45:44 +0000352 assert(NTTP->getDepth() == 0 &&
353 "Cannot deduce non-type template argument with depth > 0");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000354
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000355 DeducedTemplateArgument NewDeduced(D? D->getCanonicalDecl() : 0);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000356 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000357 Deduced[NTTP->getIndex()],
358 NewDeduced);
359 if (Result.isNull()) {
360 Info.Param = NTTP;
361 Info.FirstArg = Deduced[NTTP->getIndex()];
362 Info.SecondArg = NewDeduced;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000363 return Sema::TDK_Inconsistent;
Douglas Gregor15755cb2009-11-13 23:45:44 +0000364 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000365
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000366 Deduced[NTTP->getIndex()] = Result;
Douglas Gregor15755cb2009-11-13 23:45:44 +0000367 return Sema::TDK_Success;
368}
369
Douglas Gregorf67875d2009-06-12 18:26:56 +0000370static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000371DeduceTemplateArguments(Sema &S,
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000372 TemplateParameterList *TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000373 TemplateName Param,
374 TemplateName Arg,
John McCall2a7fb272010-08-25 05:32:35 +0000375 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000376 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregord708c722009-06-09 16:35:58 +0000377 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000378 if (!ParamDecl) {
379 // The parameter type is dependent and is not a template template parameter,
380 // so there is nothing that we can deduce.
381 return Sema::TDK_Success;
382 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000383
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000384 if (TemplateTemplateParmDecl *TempParam
385 = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) {
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000386 DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000387 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000388 Deduced[TempParam->getIndex()],
389 NewDeduced);
390 if (Result.isNull()) {
391 Info.Param = TempParam;
392 Info.FirstArg = Deduced[TempParam->getIndex()];
393 Info.SecondArg = NewDeduced;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000394 return Sema::TDK_Inconsistent;
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000395 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000396
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000397 Deduced[TempParam->getIndex()] = Result;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000398 return Sema::TDK_Success;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000399 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000400
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000401 // Verify that the two template names are equivalent.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000402 if (S.Context.hasSameTemplateName(Param, Arg))
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000403 return Sema::TDK_Success;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000404
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000405 // Mismatch of non-dependent template parameter to argument.
406 Info.FirstArg = TemplateArgument(Param);
407 Info.SecondArg = TemplateArgument(Arg);
408 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000409}
410
Mike Stump1eb44332009-09-09 15:08:12 +0000411/// \brief Deduce the template arguments by comparing the template parameter
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000412/// type (which is a template-id) with the template argument type.
413///
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000414/// \param S the Sema
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000415///
416/// \param TemplateParams the template parameters that we are deducing
417///
418/// \param Param the parameter type
419///
420/// \param Arg the argument type
421///
422/// \param Info information about the template argument deduction itself
423///
424/// \param Deduced the deduced template arguments
425///
426/// \returns the result of template argument deduction so far. Note that a
427/// "success" result means that template argument deduction has not yet failed,
428/// but it may still fail, later, for other reasons.
429static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000430DeduceTemplateArguments(Sema &S,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000431 TemplateParameterList *TemplateParams,
432 const TemplateSpecializationType *Param,
433 QualType Arg,
John McCall2a7fb272010-08-25 05:32:35 +0000434 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000435 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
John McCall467b27b2009-10-22 20:10:53 +0000436 assert(Arg.isCanonical() && "Argument type must be canonical");
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000438 // Check whether the template argument is a dependent template-id.
Mike Stump1eb44332009-09-09 15:08:12 +0000439 if (const TemplateSpecializationType *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000440 = dyn_cast<TemplateSpecializationType>(Arg)) {
441 // Perform template argument deduction for the template name.
442 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000443 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000444 Param->getTemplateName(),
445 SpecArg->getTemplateName(),
446 Info, Deduced))
447 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000450 // Perform template argument deduction on each template
Douglas Gregor0972c862010-12-22 18:55:49 +0000451 // argument. Ignore any missing/extra arguments, since they could be
452 // filled in by default arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000453 return DeduceTemplateArguments(S, TemplateParams,
454 Param->getArgs(), Param->getNumArgs(),
Douglas Gregor0972c862010-12-22 18:55:49 +0000455 SpecArg->getArgs(), SpecArg->getNumArgs(),
456 Info, Deduced,
457 /*NumberOfArgumentsMustMatch=*/false);
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000458 }
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000460 // If the argument type is a class template specialization, we
461 // perform template argument deduction using its template
462 // arguments.
463 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
464 if (!RecordArg)
465 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000466
467 ClassTemplateSpecializationDecl *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000468 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
469 if (!SpecArg)
470 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000472 // Perform template argument deduction for the template name.
473 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000474 = DeduceTemplateArguments(S,
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000475 TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000476 Param->getTemplateName(),
477 TemplateName(SpecArg->getSpecializedTemplate()),
478 Info, Deduced))
479 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Douglas Gregor20a55e22010-12-22 18:17:10 +0000481 // Perform template argument deduction for the template arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000482 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor20a55e22010-12-22 18:17:10 +0000483 Param->getArgs(), Param->getNumArgs(),
484 SpecArg->getTemplateArgs().data(),
485 SpecArg->getTemplateArgs().size(),
486 Info, Deduced);
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000487}
488
John McCallcd05e812010-08-28 22:14:41 +0000489/// \brief Determines whether the given type is an opaque type that
490/// might be more qualified when instantiated.
491static bool IsPossiblyOpaquelyQualifiedType(QualType T) {
492 switch (T->getTypeClass()) {
493 case Type::TypeOfExpr:
494 case Type::TypeOf:
495 case Type::DependentName:
496 case Type::Decltype:
497 case Type::UnresolvedUsing:
John McCall62c28c82011-01-18 07:41:22 +0000498 case Type::TemplateTypeParm:
John McCallcd05e812010-08-28 22:14:41 +0000499 return true;
500
501 case Type::ConstantArray:
502 case Type::IncompleteArray:
503 case Type::VariableArray:
504 case Type::DependentSizedArray:
505 return IsPossiblyOpaquelyQualifiedType(
506 cast<ArrayType>(T)->getElementType());
507
508 default:
509 return false;
510 }
511}
512
Douglas Gregord3731192011-01-10 07:32:04 +0000513/// \brief Retrieve the depth and index of a template parameter.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000514static std::pair<unsigned, unsigned>
Douglas Gregord3731192011-01-10 07:32:04 +0000515getDepthAndIndex(NamedDecl *ND) {
Douglas Gregor603cfb42011-01-05 23:12:31 +0000516 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
517 return std::make_pair(TTP->getDepth(), TTP->getIndex());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000518
Douglas Gregor603cfb42011-01-05 23:12:31 +0000519 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
520 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000521
Douglas Gregor603cfb42011-01-05 23:12:31 +0000522 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
523 return std::make_pair(TTP->getDepth(), TTP->getIndex());
524}
525
Douglas Gregord3731192011-01-10 07:32:04 +0000526/// \brief Retrieve the depth and index of an unexpanded parameter pack.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000527static std::pair<unsigned, unsigned>
Douglas Gregord3731192011-01-10 07:32:04 +0000528getDepthAndIndex(UnexpandedParameterPack UPP) {
529 if (const TemplateTypeParmType *TTP
530 = UPP.first.dyn_cast<const TemplateTypeParmType *>())
531 return std::make_pair(TTP->getDepth(), TTP->getIndex());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000532
Douglas Gregord3731192011-01-10 07:32:04 +0000533 return getDepthAndIndex(UPP.first.get<NamedDecl *>());
534}
535
Douglas Gregor603cfb42011-01-05 23:12:31 +0000536/// \brief Helper function to build a TemplateParameter when we don't
537/// know its type statically.
538static TemplateParameter makeTemplateParameter(Decl *D) {
539 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
540 return TemplateParameter(TTP);
541 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
542 return TemplateParameter(NTTP);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000543
Douglas Gregor603cfb42011-01-05 23:12:31 +0000544 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
545}
546
Douglas Gregor54293852011-01-10 17:35:05 +0000547/// \brief Prepare to perform template argument deduction for all of the
548/// arguments in a set of argument packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000549static void PrepareArgumentPackDeduction(Sema &S,
550 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor54293852011-01-10 17:35:05 +0000551 const llvm::SmallVectorImpl<unsigned> &PackIndices,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000552 llvm::SmallVectorImpl<DeducedTemplateArgument> &SavedPacks,
Douglas Gregor54293852011-01-10 17:35:05 +0000553 llvm::SmallVectorImpl<
554 llvm::SmallVector<DeducedTemplateArgument, 4> > &NewlyDeducedPacks) {
555 // Save the deduced template arguments for each parameter pack expanded
556 // by this pack expansion, then clear out the deduction.
557 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
558 // Save the previously-deduced argument pack, then clear it out so that we
559 // can deduce a new argument pack.
560 SavedPacks[I] = Deduced[PackIndices[I]];
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000561 Deduced[PackIndices[I]] = TemplateArgument();
562
Douglas Gregor54293852011-01-10 17:35:05 +0000563 // If the template arugment pack was explicitly specified, add that to
564 // the set of deduced arguments.
565 const TemplateArgument *ExplicitArgs;
566 unsigned NumExplicitArgs;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000567 if (NamedDecl *PartiallySubstitutedPack
Douglas Gregor54293852011-01-10 17:35:05 +0000568 = S.CurrentInstantiationScope->getPartiallySubstitutedPack(
569 &ExplicitArgs,
570 &NumExplicitArgs)) {
571 if (getDepthAndIndex(PartiallySubstitutedPack).second == PackIndices[I])
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000572 NewlyDeducedPacks[I].append(ExplicitArgs,
Douglas Gregor54293852011-01-10 17:35:05 +0000573 ExplicitArgs + NumExplicitArgs);
574 }
575 }
576}
577
Douglas Gregor0216f812011-01-10 17:53:52 +0000578/// \brief Finish template argument deduction for a set of argument packs,
579/// producing the argument packs and checking for consistency with prior
580/// deductions.
581static Sema::TemplateDeductionResult
582FinishArgumentPackDeduction(Sema &S,
583 TemplateParameterList *TemplateParams,
584 bool HasAnyArguments,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000585 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor0216f812011-01-10 17:53:52 +0000586 const llvm::SmallVectorImpl<unsigned> &PackIndices,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000587 llvm::SmallVectorImpl<DeducedTemplateArgument> &SavedPacks,
Douglas Gregor0216f812011-01-10 17:53:52 +0000588 llvm::SmallVectorImpl<
589 llvm::SmallVector<DeducedTemplateArgument, 4> > &NewlyDeducedPacks,
590 TemplateDeductionInfo &Info) {
591 // Build argument packs for each of the parameter packs expanded by this
592 // pack expansion.
593 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
594 if (HasAnyArguments && NewlyDeducedPacks[I].empty()) {
595 // We were not able to deduce anything for this parameter pack,
596 // so just restore the saved argument pack.
597 Deduced[PackIndices[I]] = SavedPacks[I];
598 continue;
599 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000600
Douglas Gregor0216f812011-01-10 17:53:52 +0000601 DeducedTemplateArgument NewPack;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000602
Douglas Gregor0216f812011-01-10 17:53:52 +0000603 if (NewlyDeducedPacks[I].empty()) {
604 // If we deduced an empty argument pack, create it now.
605 NewPack = DeducedTemplateArgument(TemplateArgument(0, 0));
606 } else {
607 TemplateArgument *ArgumentPack
Douglas Gregor203e6a32011-01-11 23:09:57 +0000608 = new (S.Context) TemplateArgument [NewlyDeducedPacks[I].size()];
Douglas Gregor0216f812011-01-10 17:53:52 +0000609 std::copy(NewlyDeducedPacks[I].begin(), NewlyDeducedPacks[I].end(),
610 ArgumentPack);
611 NewPack
Douglas Gregor203e6a32011-01-11 23:09:57 +0000612 = DeducedTemplateArgument(TemplateArgument(ArgumentPack,
613 NewlyDeducedPacks[I].size()),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000614 NewlyDeducedPacks[I][0].wasDeducedFromArrayBound());
Douglas Gregor0216f812011-01-10 17:53:52 +0000615 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000616
Douglas Gregor0216f812011-01-10 17:53:52 +0000617 DeducedTemplateArgument Result
618 = checkDeducedTemplateArguments(S.Context, SavedPacks[I], NewPack);
619 if (Result.isNull()) {
620 Info.Param
621 = makeTemplateParameter(TemplateParams->getParam(PackIndices[I]));
622 Info.FirstArg = SavedPacks[I];
623 Info.SecondArg = NewPack;
624 return Sema::TDK_Inconsistent;
625 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000626
Douglas Gregor0216f812011-01-10 17:53:52 +0000627 Deduced[PackIndices[I]] = Result;
628 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000629
Douglas Gregor0216f812011-01-10 17:53:52 +0000630 return Sema::TDK_Success;
631}
632
Douglas Gregor603cfb42011-01-05 23:12:31 +0000633/// \brief Deduce the template arguments by comparing the list of parameter
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000634/// types to the list of argument types, as in the parameter-type-lists of
635/// function types (C++ [temp.deduct.type]p10).
Douglas Gregor603cfb42011-01-05 23:12:31 +0000636///
637/// \param S The semantic analysis object within which we are deducing
638///
639/// \param TemplateParams The template parameters that we are deducing
640///
641/// \param Params The list of parameter types
642///
643/// \param NumParams The number of types in \c Params
644///
645/// \param Args The list of argument types
646///
647/// \param NumArgs The number of types in \c Args
648///
649/// \param Info information about the template argument deduction itself
650///
651/// \param Deduced the deduced template arguments
652///
653/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
654/// how template argument deduction is performed.
655///
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000656/// \param PartialOrdering If true, we are performing template argument
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000657/// deduction for during partial ordering for a call
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000658/// (C++0x [temp.deduct.partial]).
659///
Douglas Gregorb939a192011-01-21 17:29:42 +0000660/// \param RefParamComparisons If we're performing template argument deduction
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000661/// in the context of partial ordering, the set of qualifier comparisons.
662///
Douglas Gregor603cfb42011-01-05 23:12:31 +0000663/// \returns the result of template argument deduction so far. Note that a
664/// "success" result means that template argument deduction has not yet failed,
665/// but it may still fail, later, for other reasons.
666static Sema::TemplateDeductionResult
667DeduceTemplateArguments(Sema &S,
668 TemplateParameterList *TemplateParams,
669 const QualType *Params, unsigned NumParams,
670 const QualType *Args, unsigned NumArgs,
671 TemplateDeductionInfo &Info,
672 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000673 unsigned TDF,
674 bool PartialOrdering = false,
Douglas Gregorb939a192011-01-21 17:29:42 +0000675 llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *
676 RefParamComparisons = 0) {
Douglas Gregor0bbacf82011-01-05 23:23:17 +0000677 // Fast-path check to see if we have too many/too few arguments.
678 if (NumParams != NumArgs &&
679 !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) &&
680 !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1])))
Douglas Gregor3cae5c92011-01-10 20:53:55 +0000681 return Sema::TDK_NonDeducedMismatch;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000682
Douglas Gregor603cfb42011-01-05 23:12:31 +0000683 // C++0x [temp.deduct.type]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000684 // Similarly, if P has a form that contains (T), then each parameter type
685 // Pi of the respective parameter-type- list of P is compared with the
686 // corresponding parameter type Ai of the corresponding parameter-type-list
687 // of A. [...]
Douglas Gregor603cfb42011-01-05 23:12:31 +0000688 unsigned ArgIdx = 0, ParamIdx = 0;
689 for (; ParamIdx != NumParams; ++ParamIdx) {
690 // Check argument types.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000691 const PackExpansionType *Expansion
Douglas Gregor603cfb42011-01-05 23:12:31 +0000692 = dyn_cast<PackExpansionType>(Params[ParamIdx]);
693 if (!Expansion) {
694 // Simple case: compare the parameter and argument types at this point.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000695
Douglas Gregor603cfb42011-01-05 23:12:31 +0000696 // Make sure we have an argument.
697 if (ArgIdx >= NumArgs)
Douglas Gregor3cae5c92011-01-10 20:53:55 +0000698 return Sema::TDK_NonDeducedMismatch;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000699
Douglas Gregor77d6bb92011-01-11 22:21:24 +0000700 if (isa<PackExpansionType>(Args[ArgIdx])) {
701 // C++0x [temp.deduct.type]p22:
702 // If the original function parameter associated with A is a function
703 // parameter pack and the function parameter associated with P is not
704 // a function parameter pack, then template argument deduction fails.
705 return Sema::TDK_NonDeducedMismatch;
706 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000707
Douglas Gregor603cfb42011-01-05 23:12:31 +0000708 if (Sema::TemplateDeductionResult Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000709 = DeduceTemplateArguments(S, TemplateParams,
710 Params[ParamIdx],
711 Args[ArgIdx],
712 Info, Deduced, TDF,
713 PartialOrdering,
Douglas Gregorb939a192011-01-21 17:29:42 +0000714 RefParamComparisons))
Douglas Gregor603cfb42011-01-05 23:12:31 +0000715 return Result;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000716
Douglas Gregor603cfb42011-01-05 23:12:31 +0000717 ++ArgIdx;
718 continue;
719 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000720
Douglas Gregor7d5c0c12011-01-11 01:52:23 +0000721 // C++0x [temp.deduct.type]p5:
722 // The non-deduced contexts are:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000723 // - A function parameter pack that does not occur at the end of the
Douglas Gregor7d5c0c12011-01-11 01:52:23 +0000724 // parameter-declaration-clause.
725 if (ParamIdx + 1 < NumParams)
726 return Sema::TDK_Success;
727
Douglas Gregor603cfb42011-01-05 23:12:31 +0000728 // C++0x [temp.deduct.type]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000729 // If the parameter-declaration corresponding to Pi is a function
Douglas Gregor603cfb42011-01-05 23:12:31 +0000730 // parameter pack, then the type of its declarator- id is compared with
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000731 // each remaining parameter type in the parameter-type-list of A. Each
Douglas Gregor603cfb42011-01-05 23:12:31 +0000732 // comparison deduces template arguments for subsequent positions in the
733 // template parameter packs expanded by the function parameter pack.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000734
Douglas Gregor603cfb42011-01-05 23:12:31 +0000735 // Compute the set of template parameter indices that correspond to
736 // parameter packs expanded by the pack expansion.
737 llvm::SmallVector<unsigned, 2> PackIndices;
738 QualType Pattern = Expansion->getPattern();
739 {
740 llvm::BitVector SawIndices(TemplateParams->size());
741 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
742 S.collectUnexpandedParameterPacks(Pattern, Unexpanded);
743 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
744 unsigned Depth, Index;
745 llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
746 if (Depth == 0 && !SawIndices[Index]) {
747 SawIndices[Index] = true;
748 PackIndices.push_back(Index);
749 }
750 }
751 }
752 assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
753
Douglas Gregord3731192011-01-10 07:32:04 +0000754 // Keep track of the deduced template arguments for each parameter pack
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000755 // expanded by this pack expansion (the outer index) and for each
Douglas Gregord3731192011-01-10 07:32:04 +0000756 // template argument (the inner SmallVectors).
757 llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2>
758 NewlyDeducedPacks(PackIndices.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000759 llvm::SmallVector<DeducedTemplateArgument, 2>
Douglas Gregor54293852011-01-10 17:35:05 +0000760 SavedPacks(PackIndices.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000761 PrepareArgumentPackDeduction(S, Deduced, PackIndices, SavedPacks,
Douglas Gregor54293852011-01-10 17:35:05 +0000762 NewlyDeducedPacks);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000763
Douglas Gregor603cfb42011-01-05 23:12:31 +0000764 bool HasAnyArguments = false;
765 for (; ArgIdx < NumArgs; ++ArgIdx) {
766 HasAnyArguments = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000767
Douglas Gregor603cfb42011-01-05 23:12:31 +0000768 // Deduce template arguments from the pattern.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000769 if (Sema::TemplateDeductionResult Result
Douglas Gregor73b3cf62011-01-25 17:19:08 +0000770 = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx],
771 Info, Deduced, TDF, PartialOrdering,
772 RefParamComparisons))
Douglas Gregor603cfb42011-01-05 23:12:31 +0000773 return Result;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000774
Douglas Gregor603cfb42011-01-05 23:12:31 +0000775 // Capture the deduced template arguments for each parameter pack expanded
776 // by this pack expansion, add them to the list of arguments we've deduced
777 // for that pack, then clear out the deduced argument.
778 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
779 DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
780 if (!DeducedArg.isNull()) {
781 NewlyDeducedPacks[I].push_back(DeducedArg);
782 DeducedArg = DeducedTemplateArgument();
783 }
784 }
785 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000786
Douglas Gregor603cfb42011-01-05 23:12:31 +0000787 // Build argument packs for each of the parameter packs expanded by this
788 // pack expansion.
Douglas Gregor0216f812011-01-10 17:53:52 +0000789 if (Sema::TemplateDeductionResult Result
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000790 = FinishArgumentPackDeduction(S, TemplateParams, HasAnyArguments,
Douglas Gregor0216f812011-01-10 17:53:52 +0000791 Deduced, PackIndices, SavedPacks,
792 NewlyDeducedPacks, Info))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000793 return Result;
Douglas Gregor603cfb42011-01-05 23:12:31 +0000794 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000795
Douglas Gregor603cfb42011-01-05 23:12:31 +0000796 // Make sure we don't have any extra arguments.
797 if (ArgIdx < NumArgs)
Douglas Gregor3cae5c92011-01-10 20:53:55 +0000798 return Sema::TDK_NonDeducedMismatch;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000799
Douglas Gregor603cfb42011-01-05 23:12:31 +0000800 return Sema::TDK_Success;
801}
802
Douglas Gregor500d3312009-06-26 18:27:22 +0000803/// \brief Deduce the template arguments by comparing the parameter type and
804/// the argument type (C++ [temp.deduct.type]).
805///
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000806/// \param S the semantic analysis object within which we are deducing
Douglas Gregor500d3312009-06-26 18:27:22 +0000807///
808/// \param TemplateParams the template parameters that we are deducing
809///
810/// \param ParamIn the parameter type
811///
812/// \param ArgIn the argument type
813///
814/// \param Info information about the template argument deduction itself
815///
816/// \param Deduced the deduced template arguments
817///
Douglas Gregor508f1c82009-06-26 23:10:12 +0000818/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
Mike Stump1eb44332009-09-09 15:08:12 +0000819/// how template argument deduction is performed.
Douglas Gregor500d3312009-06-26 18:27:22 +0000820///
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000821/// \param PartialOrdering Whether we're performing template argument deduction
822/// in the context of partial ordering (C++0x [temp.deduct.partial]).
823///
Douglas Gregorb939a192011-01-21 17:29:42 +0000824/// \param RefParamComparisons If we're performing template argument deduction
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000825/// in the context of partial ordering, the set of qualifier comparisons.
826///
Douglas Gregor500d3312009-06-26 18:27:22 +0000827/// \returns the result of template argument deduction so far. Note that a
828/// "success" result means that template argument deduction has not yet failed,
829/// but it may still fail, later, for other reasons.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000830static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000831DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000832 TemplateParameterList *TemplateParams,
833 QualType ParamIn, QualType ArgIn,
John McCall2a7fb272010-08-25 05:32:35 +0000834 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000835 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000836 unsigned TDF,
837 bool PartialOrdering,
Douglas Gregorb939a192011-01-21 17:29:42 +0000838 llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *RefParamComparisons) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000839 // We only want to look at the canonical types, since typedefs and
840 // sugar are not part of template argument deduction.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000841 QualType Param = S.Context.getCanonicalType(ParamIn);
842 QualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000843
Douglas Gregor77d6bb92011-01-11 22:21:24 +0000844 // If the argument type is a pack expansion, look at its pattern.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000845 // This isn't explicitly called out
Douglas Gregor77d6bb92011-01-11 22:21:24 +0000846 if (const PackExpansionType *ArgExpansion
847 = dyn_cast<PackExpansionType>(Arg))
848 Arg = ArgExpansion->getPattern();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000849
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000850 if (PartialOrdering) {
851 // C++0x [temp.deduct.partial]p5:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000852 // Before the partial ordering is done, certain transformations are
853 // performed on the types used for partial ordering:
854 // - If P is a reference type, P is replaced by the type referred to.
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000855 const ReferenceType *ParamRef = Param->getAs<ReferenceType>();
856 if (ParamRef)
857 Param = ParamRef->getPointeeType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000858
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000859 // - If A is a reference type, A is replaced by the type referred to.
860 const ReferenceType *ArgRef = Arg->getAs<ReferenceType>();
861 if (ArgRef)
862 Arg = ArgRef->getPointeeType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000863
Douglas Gregorb939a192011-01-21 17:29:42 +0000864 if (RefParamComparisons && ParamRef && ArgRef) {
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000865 // C++0x [temp.deduct.partial]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000866 // If both P and A were reference types (before being replaced with the
867 // type referred to above), determine which of the two types (if any) is
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000868 // more cv-qualified than the other; otherwise the types are considered
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000869 // to be equally cv-qualified for partial ordering purposes. The result
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000870 // of this determination will be used below.
871 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000872 // We save this information for later, using it only when deduction
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000873 // succeeds in both directions.
Douglas Gregorb939a192011-01-21 17:29:42 +0000874 RefParamPartialOrderingComparison Comparison;
875 Comparison.ParamIsRvalueRef = ParamRef->getAs<RValueReferenceType>();
876 Comparison.ArgIsRvalueRef = ArgRef->getAs<RValueReferenceType>();
877 Comparison.Qualifiers = NeitherMoreQualified;
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000878 if (Param.isMoreQualifiedThan(Arg))
Douglas Gregorb939a192011-01-21 17:29:42 +0000879 Comparison.Qualifiers = ParamMoreQualified;
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000880 else if (Arg.isMoreQualifiedThan(Param))
Douglas Gregorb939a192011-01-21 17:29:42 +0000881 Comparison.Qualifiers = ArgMoreQualified;
882 RefParamComparisons->push_back(Comparison);
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000883 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000884
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000885 // C++0x [temp.deduct.partial]p7:
886 // Remove any top-level cv-qualifiers:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000887 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000888 // version of P.
889 Param = Param.getUnqualifiedType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000890 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000891 // version of A.
892 Arg = Arg.getUnqualifiedType();
893 } else {
894 // C++0x [temp.deduct.call]p4 bullet 1:
895 // - If the original P is a reference type, the deduced A (i.e., the type
896 // referred to by the reference) can be more cv-qualified than the
897 // transformed A.
898 if (TDF & TDF_ParamWithReferenceType) {
899 Qualifiers Quals;
900 QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals);
901 Quals.setCVRQualifiers(Quals.getCVRQualifiers() &
John McCall62c28c82011-01-18 07:41:22 +0000902 Arg.getCVRQualifiers());
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000903 Param = S.Context.getQualifiedType(UnqualParam, Quals);
904 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000905
Douglas Gregor73b3cf62011-01-25 17:19:08 +0000906 if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) {
907 // C++0x [temp.deduct.type]p10:
908 // If P and A are function types that originated from deduction when
909 // taking the address of a function template (14.8.2.2) or when deducing
910 // template arguments from a function declaration (14.8.2.6) and Pi and
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000911 // Ai are parameters of the top-level parameter-type-list of P and A,
912 // respectively, Pi is adjusted if it is an rvalue reference to a
913 // cv-unqualified template parameter and Ai is an lvalue reference, in
914 // which case the type of Pi is changed to be the template parameter
Douglas Gregor73b3cf62011-01-25 17:19:08 +0000915 // type (i.e., T&& is changed to simply T). [ Note: As a result, when
916 // Pi is T&& and Ai is X&, the adjusted Pi will be T, causing T to be
NAKAMURA Takumi00995302011-01-27 07:09:49 +0000917 // deduced as X&. - end note ]
Douglas Gregor73b3cf62011-01-25 17:19:08 +0000918 TDF &= ~TDF_TopLevelParameterTypeList;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000919
Douglas Gregor73b3cf62011-01-25 17:19:08 +0000920 if (const RValueReferenceType *ParamRef
921 = Param->getAs<RValueReferenceType>()) {
922 if (isa<TemplateTypeParmType>(ParamRef->getPointeeType()) &&
923 !ParamRef->getPointeeType().getQualifiers())
924 if (Arg->isLValueReferenceType())
925 Param = ParamRef->getPointeeType();
926 }
927 }
Douglas Gregor500d3312009-06-26 18:27:22 +0000928 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000929
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000930 // If the parameter type is not dependent, there is nothing to deduce.
Douglas Gregor12820292009-09-14 20:00:47 +0000931 if (!Param->isDependentType()) {
Douglas Gregor3cae5c92011-01-10 20:53:55 +0000932 if (!(TDF & TDF_SkipNonDependent) && Param != Arg)
Douglas Gregor12820292009-09-14 20:00:47 +0000933 return Sema::TDK_NonDeducedMismatch;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000934
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000935 return Sema::TDK_Success;
Douglas Gregor12820292009-09-14 20:00:47 +0000936 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000937
Douglas Gregor199d9912009-06-05 00:53:49 +0000938 // C++ [temp.deduct.type]p9:
Mike Stump1eb44332009-09-09 15:08:12 +0000939 // A template type argument T, a template template argument TT or a
940 // template non-type argument i can be deduced if P and A have one of
Douglas Gregor199d9912009-06-05 00:53:49 +0000941 // the following forms:
942 //
943 // T
944 // cv-list T
Mike Stump1eb44332009-09-09 15:08:12 +0000945 if (const TemplateTypeParmType *TemplateTypeParm
John McCall183700f2009-09-21 23:43:11 +0000946 = Param->getAs<TemplateTypeParmType>()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000947 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000948 bool RecanonicalizeArg = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Douglas Gregor9e9fae42009-07-22 20:02:25 +0000950 // If the argument type is an array type, move the qualifiers up to the
951 // top level, so they can be matched with the qualifiers on the parameter.
952 // FIXME: address spaces, ObjC GC qualifiers
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000953 if (isa<ArrayType>(Arg)) {
John McCall0953e762009-09-24 19:53:00 +0000954 Qualifiers Quals;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000955 Arg = S.Context.getUnqualifiedArrayType(Arg, Quals);
John McCall0953e762009-09-24 19:53:00 +0000956 if (Quals) {
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000957 Arg = S.Context.getQualifiedType(Arg, Quals);
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000958 RecanonicalizeArg = true;
959 }
960 }
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000962 // The argument type can not be less qualified than the parameter
963 // type.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000964 if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000965 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
John McCall57e97782010-08-05 09:05:08 +0000966 Info.FirstArg = TemplateArgument(Param);
John McCall833ca992009-10-29 08:12:44 +0000967 Info.SecondArg = TemplateArgument(Arg);
John McCall57e97782010-08-05 09:05:08 +0000968 return Sema::TDK_Underqualified;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000969 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000970
971 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000972 assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function");
John McCall0953e762009-09-24 19:53:00 +0000973 QualType DeducedType = Arg;
John McCall49f4e1c2010-12-10 11:01:00 +0000974
975 // local manipulation is okay because it's canonical
976 DeducedType.removeLocalCVRQualifiers(Param.getCVRQualifiers());
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000977 if (RecanonicalizeArg)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000978 DeducedType = S.Context.getCanonicalType(DeducedType);
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000980 DeducedTemplateArgument NewDeduced(DeducedType);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000981 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000982 Deduced[Index],
983 NewDeduced);
984 if (Result.isNull()) {
985 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
986 Info.FirstArg = Deduced[Index];
987 Info.SecondArg = NewDeduced;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000988 return Sema::TDK_Inconsistent;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000989 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000990
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000991 Deduced[Index] = Result;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000992 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000993 }
994
Douglas Gregorf67875d2009-06-12 18:26:56 +0000995 // Set up the template argument deduction information for a failure.
John McCall833ca992009-10-29 08:12:44 +0000996 Info.FirstArg = TemplateArgument(ParamIn);
997 Info.SecondArg = TemplateArgument(ArgIn);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000998
Douglas Gregor0bc15d92011-01-14 05:11:40 +0000999 // If the parameter is an already-substituted template parameter
1000 // pack, do nothing: we don't know which of its arguments to look
1001 // at, so we have to wait until all of the parameter packs in this
1002 // expansion have arguments.
1003 if (isa<SubstTemplateTypeParmPackType>(Param))
1004 return Sema::TDK_Success;
1005
Douglas Gregor508f1c82009-06-26 23:10:12 +00001006 // Check the cv-qualifiers on the parameter and argument types.
1007 if (!(TDF & TDF_IgnoreQualifiers)) {
1008 if (TDF & TDF_ParamWithReferenceType) {
1009 if (Param.isMoreQualifiedThan(Arg))
1010 return Sema::TDK_NonDeducedMismatch;
John McCallcd05e812010-08-28 22:14:41 +00001011 } else if (!IsPossiblyOpaquelyQualifiedType(Param)) {
Douglas Gregor508f1c82009-06-26 23:10:12 +00001012 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Mike Stump1eb44332009-09-09 15:08:12 +00001013 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor508f1c82009-06-26 23:10:12 +00001014 }
1015 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001016
Douglas Gregord560d502009-06-04 00:21:18 +00001017 switch (Param->getTypeClass()) {
Douglas Gregor199d9912009-06-05 00:53:49 +00001018 // No deduction possible for these types
1019 case Type::Builtin:
Douglas Gregorf67875d2009-06-12 18:26:56 +00001020 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Douglas Gregor199d9912009-06-05 00:53:49 +00001022 // T *
Douglas Gregord560d502009-06-04 00:21:18 +00001023 case Type::Pointer: {
John McCallc0008342010-05-13 07:48:05 +00001024 QualType PointeeType;
1025 if (const PointerType *PointerArg = Arg->getAs<PointerType>()) {
1026 PointeeType = PointerArg->getPointeeType();
1027 } else if (const ObjCObjectPointerType *PointerArg
1028 = Arg->getAs<ObjCObjectPointerType>()) {
1029 PointeeType = PointerArg->getPointeeType();
1030 } else {
Douglas Gregorf67875d2009-06-12 18:26:56 +00001031 return Sema::TDK_NonDeducedMismatch;
John McCallc0008342010-05-13 07:48:05 +00001032 }
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Douglas Gregor41128772009-06-26 23:27:24 +00001034 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001035 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +00001036 cast<PointerType>(Param)->getPointeeType(),
John McCallc0008342010-05-13 07:48:05 +00001037 PointeeType,
Douglas Gregor41128772009-06-26 23:27:24 +00001038 Info, Deduced, SubTDF);
Douglas Gregord560d502009-06-04 00:21:18 +00001039 }
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Douglas Gregor199d9912009-06-05 00:53:49 +00001041 // T &
Douglas Gregord560d502009-06-04 00:21:18 +00001042 case Type::LValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +00001043 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +00001044 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001045 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001047 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +00001048 cast<LValueReferenceType>(Param)->getPointeeType(),
1049 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +00001050 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +00001051 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001052
Douglas Gregor199d9912009-06-05 00:53:49 +00001053 // T && [C++0x]
Douglas Gregord560d502009-06-04 00:21:18 +00001054 case Type::RValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +00001055 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +00001056 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001057 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001058
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001059 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +00001060 cast<RValueReferenceType>(Param)->getPointeeType(),
1061 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +00001062 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +00001063 }
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Douglas Gregor199d9912009-06-05 00:53:49 +00001065 // T [] (implied, but not stated explicitly)
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001066 case Type::IncompleteArray: {
Mike Stump1eb44332009-09-09 15:08:12 +00001067 const IncompleteArrayType *IncompleteArrayArg =
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001068 S.Context.getAsIncompleteArrayType(Arg);
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001069 if (!IncompleteArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001070 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001071
John McCalle4f26e52010-08-19 00:20:19 +00001072 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001073 return DeduceTemplateArguments(S, TemplateParams,
1074 S.Context.getAsIncompleteArrayType(Param)->getElementType(),
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001075 IncompleteArrayArg->getElementType(),
John McCalle4f26e52010-08-19 00:20:19 +00001076 Info, Deduced, SubTDF);
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001077 }
Douglas Gregor199d9912009-06-05 00:53:49 +00001078
1079 // T [integer-constant]
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001080 case Type::ConstantArray: {
Mike Stump1eb44332009-09-09 15:08:12 +00001081 const ConstantArrayType *ConstantArrayArg =
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001082 S.Context.getAsConstantArrayType(Arg);
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001083 if (!ConstantArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001084 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001085
1086 const ConstantArrayType *ConstantArrayParm =
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001087 S.Context.getAsConstantArrayType(Param);
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001088 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregorf67875d2009-06-12 18:26:56 +00001089 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001090
John McCalle4f26e52010-08-19 00:20:19 +00001091 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001092 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001093 ConstantArrayParm->getElementType(),
1094 ConstantArrayArg->getElementType(),
John McCalle4f26e52010-08-19 00:20:19 +00001095 Info, Deduced, SubTDF);
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001096 }
1097
Douglas Gregor199d9912009-06-05 00:53:49 +00001098 // type [i]
1099 case Type::DependentSizedArray: {
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001100 const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
Douglas Gregor199d9912009-06-05 00:53:49 +00001101 if (!ArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001102 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001103
John McCalle4f26e52010-08-19 00:20:19 +00001104 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
1105
Douglas Gregor199d9912009-06-05 00:53:49 +00001106 // Check the element type of the arrays
1107 const DependentSizedArrayType *DependentArrayParm
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001108 = S.Context.getAsDependentSizedArrayType(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +00001109 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001110 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001111 DependentArrayParm->getElementType(),
1112 ArrayArg->getElementType(),
John McCalle4f26e52010-08-19 00:20:19 +00001113 Info, Deduced, SubTDF))
Douglas Gregorf67875d2009-06-12 18:26:56 +00001114 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Douglas Gregor199d9912009-06-05 00:53:49 +00001116 // Determine the array bound is something we can deduce.
Mike Stump1eb44332009-09-09 15:08:12 +00001117 NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +00001118 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
1119 if (!NTTP)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001120 return Sema::TDK_Success;
Mike Stump1eb44332009-09-09 15:08:12 +00001121
1122 // We can perform template argument deduction for the given non-type
Douglas Gregor199d9912009-06-05 00:53:49 +00001123 // template parameter.
Mike Stump1eb44332009-09-09 15:08:12 +00001124 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +00001125 "Cannot deduce non-type template argument at depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +00001126 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson335e24a2009-06-16 22:44:31 +00001127 = dyn_cast<ConstantArrayType>(ArrayArg)) {
1128 llvm::APSInt Size(ConstantArrayArg->getSize());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001129 return DeduceNonTypeTemplateArgument(S, NTTP, Size,
Douglas Gregor9d0e4412010-03-26 05:50:28 +00001130 S.Context.getSizeType(),
Douglas Gregor02024a92010-03-28 02:42:43 +00001131 /*ArrayBound=*/true,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001132 Info, Deduced);
Anders Carlsson335e24a2009-06-16 22:44:31 +00001133 }
Douglas Gregor199d9912009-06-05 00:53:49 +00001134 if (const DependentSizedArrayType *DependentArrayArg
1135 = dyn_cast<DependentSizedArrayType>(ArrayArg))
Douglas Gregor34c2f8c2010-12-22 23:15:38 +00001136 if (DependentArrayArg->getSizeExpr())
1137 return DeduceNonTypeTemplateArgument(S, NTTP,
1138 DependentArrayArg->getSizeExpr(),
1139 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Douglas Gregor199d9912009-06-05 00:53:49 +00001141 // Incomplete type does not match a dependently-sized array type
Douglas Gregorf67875d2009-06-12 18:26:56 +00001142 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +00001143 }
Mike Stump1eb44332009-09-09 15:08:12 +00001144
1145 // type(*)(T)
1146 // T(*)()
1147 // T(*)(T)
Anders Carlssona27fad52009-06-08 15:19:08 +00001148 case Type::FunctionProto: {
Douglas Gregor73b3cf62011-01-25 17:19:08 +00001149 unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList;
Mike Stump1eb44332009-09-09 15:08:12 +00001150 const FunctionProtoType *FunctionProtoArg =
Anders Carlssona27fad52009-06-08 15:19:08 +00001151 dyn_cast<FunctionProtoType>(Arg);
1152 if (!FunctionProtoArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001153 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001154
1155 const FunctionProtoType *FunctionProtoParam =
Anders Carlssona27fad52009-06-08 15:19:08 +00001156 cast<FunctionProtoType>(Param);
Anders Carlsson994b6cb2009-06-08 19:22:23 +00001157
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001158 if (FunctionProtoParam->getTypeQuals()
Douglas Gregore3c7a7c2011-01-26 16:50:54 +00001159 != FunctionProtoArg->getTypeQuals() ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001160 FunctionProtoParam->getRefQualifier()
Douglas Gregore3c7a7c2011-01-26 16:50:54 +00001161 != FunctionProtoArg->getRefQualifier() ||
1162 FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregorf67875d2009-06-12 18:26:56 +00001163 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +00001164
Anders Carlssona27fad52009-06-08 15:19:08 +00001165 // Check return types.
Douglas Gregorf67875d2009-06-12 18:26:56 +00001166 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001167 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001168 FunctionProtoParam->getResultType(),
1169 FunctionProtoArg->getResultType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +00001170 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +00001171 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Douglas Gregor603cfb42011-01-05 23:12:31 +00001173 return DeduceTemplateArguments(S, TemplateParams,
1174 FunctionProtoParam->arg_type_begin(),
1175 FunctionProtoParam->getNumArgs(),
1176 FunctionProtoArg->arg_type_begin(),
1177 FunctionProtoArg->getNumArgs(),
Douglas Gregor73b3cf62011-01-25 17:19:08 +00001178 Info, Deduced, SubTDF);
Anders Carlssona27fad52009-06-08 15:19:08 +00001179 }
Mike Stump1eb44332009-09-09 15:08:12 +00001180
John McCall3cb0ebd2010-03-10 03:28:59 +00001181 case Type::InjectedClassName: {
1182 // Treat a template's injected-class-name as if the template
1183 // specialization type had been used.
John McCall31f17ec2010-04-27 00:57:59 +00001184 Param = cast<InjectedClassNameType>(Param)
1185 ->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00001186 assert(isa<TemplateSpecializationType>(Param) &&
1187 "injected class name is not a template specialization type");
1188 // fall through
1189 }
1190
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001191 // template-name<T> (where template-name refers to a class template)
Douglas Gregord708c722009-06-09 16:35:58 +00001192 // template-name<i>
Douglas Gregordb0d4b72009-11-11 23:06:43 +00001193 // TT<T>
1194 // TT<i>
1195 // TT<>
Douglas Gregord708c722009-06-09 16:35:58 +00001196 case Type::TemplateSpecialization: {
1197 const TemplateSpecializationType *SpecParam
1198 = cast<TemplateSpecializationType>(Param);
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001200 // Try to deduce template arguments from the template-id.
1201 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001202 = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001203 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +00001204
Douglas Gregor4a5c15f2009-09-30 22:13:51 +00001205 if (Result && (TDF & TDF_DerivedClass)) {
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001206 // C++ [temp.deduct.call]p3b3:
1207 // If P is a class, and P has the form template-id, then A can be a
1208 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump1eb44332009-09-09 15:08:12 +00001209 // class of the form template-id, A can be a pointer to a derived
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001210 // class pointed to by the deduced A.
1211 //
1212 // More importantly:
Mike Stump1eb44332009-09-09 15:08:12 +00001213 // These alternatives are considered only if type deduction would
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001214 // otherwise fail.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001215 if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
1216 // We cannot inspect base classes as part of deduction when the type
1217 // is incomplete, so either instantiate any templates necessary to
1218 // complete the type, or skip over it if it cannot be completed.
John McCall5769d612010-02-08 23:07:23 +00001219 if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001220 return Result;
1221
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001222 // Use data recursion to crawl through the list of base classes.
Mike Stump1eb44332009-09-09 15:08:12 +00001223 // Visited contains the set of nodes we have already visited, while
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001224 // ToVisit is our stack of records that we still need to visit.
1225 llvm::SmallPtrSet<const RecordType *, 8> Visited;
1226 llvm::SmallVector<const RecordType *, 8> ToVisit;
1227 ToVisit.push_back(RecordT);
1228 bool Successful = false;
Douglas Gregor053105d2010-11-02 00:02:34 +00001229 llvm::SmallVectorImpl<DeducedTemplateArgument> DeducedOrig(0);
1230 DeducedOrig = Deduced;
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001231 while (!ToVisit.empty()) {
1232 // Retrieve the next class in the inheritance hierarchy.
1233 const RecordType *NextT = ToVisit.back();
1234 ToVisit.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +00001235
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001236 // If we have already seen this type, skip it.
1237 if (!Visited.insert(NextT))
1238 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001240 // If this is a base class, try to perform template argument
1241 // deduction from it.
1242 if (NextT != RecordT) {
1243 Sema::TemplateDeductionResult BaseResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001244 = DeduceTemplateArguments(S, TemplateParams, SpecParam,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001245 QualType(NextT, 0), Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001247 // If template argument deduction for this base was successful,
Douglas Gregor053105d2010-11-02 00:02:34 +00001248 // note that we had some success. Otherwise, ignore any deductions
1249 // from this base class.
1250 if (BaseResult == Sema::TDK_Success) {
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001251 Successful = true;
Douglas Gregor053105d2010-11-02 00:02:34 +00001252 DeducedOrig = Deduced;
1253 }
1254 else
1255 Deduced = DeducedOrig;
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001256 }
Mike Stump1eb44332009-09-09 15:08:12 +00001257
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001258 // Visit base classes
1259 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
1260 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
1261 BaseEnd = Next->bases_end();
Sebastian Redl9994a342009-10-25 17:03:50 +00001262 Base != BaseEnd; ++Base) {
Mike Stump1eb44332009-09-09 15:08:12 +00001263 assert(Base->getType()->isRecordType() &&
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001264 "Base class that isn't a record?");
Ted Kremenek6217b802009-07-29 21:53:49 +00001265 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001266 }
1267 }
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001269 if (Successful)
1270 return Sema::TDK_Success;
1271 }
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001273 }
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001275 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +00001276 }
1277
Douglas Gregor637a4092009-06-10 23:47:09 +00001278 // T type::*
1279 // T T::*
1280 // T (type::*)()
1281 // type (T::*)()
1282 // type (type::*)(T)
1283 // type (T::*)(T)
1284 // T (type::*)(T)
1285 // T (T::*)()
1286 // T (T::*)(T)
1287 case Type::MemberPointer: {
1288 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
1289 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
1290 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001291 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +00001292
Douglas Gregorf67875d2009-06-12 18:26:56 +00001293 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001294 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001295 MemPtrParam->getPointeeType(),
1296 MemPtrArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +00001297 Info, Deduced,
1298 TDF & TDF_IgnoreQualifiers))
Douglas Gregorf67875d2009-06-12 18:26:56 +00001299 return Result;
1300
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001301 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001302 QualType(MemPtrParam->getClass(), 0),
1303 QualType(MemPtrArg->getClass(), 0),
Douglas Gregor508f1c82009-06-26 23:10:12 +00001304 Info, Deduced, 0);
Douglas Gregor637a4092009-06-10 23:47:09 +00001305 }
1306
Anders Carlsson9a917e42009-06-12 22:56:54 +00001307 // (clang extension)
1308 //
Mike Stump1eb44332009-09-09 15:08:12 +00001309 // type(^)(T)
1310 // T(^)()
1311 // T(^)(T)
Anders Carlsson859ba502009-06-12 16:23:10 +00001312 case Type::BlockPointer: {
1313 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
1314 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump1eb44332009-09-09 15:08:12 +00001315
Anders Carlsson859ba502009-06-12 16:23:10 +00001316 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001317 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001319 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +00001320 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +00001321 BlockPtrArg->getPointeeType(), Info,
Douglas Gregor508f1c82009-06-26 23:10:12 +00001322 Deduced, 0);
Anders Carlsson859ba502009-06-12 16:23:10 +00001323 }
1324
Douglas Gregor637a4092009-06-10 23:47:09 +00001325 case Type::TypeOfExpr:
1326 case Type::TypeOf:
Douglas Gregor4714c122010-03-31 17:34:00 +00001327 case Type::DependentName:
Douglas Gregor637a4092009-06-10 23:47:09 +00001328 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +00001329 return Sema::TDK_Success;
Douglas Gregor637a4092009-06-10 23:47:09 +00001330
Douglas Gregord560d502009-06-04 00:21:18 +00001331 default:
1332 break;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001333 }
1334
1335 // FIXME: Many more cases to go (to go).
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001336 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001337}
1338
Douglas Gregorf67875d2009-06-12 18:26:56 +00001339static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001340DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001341 TemplateParameterList *TemplateParams,
1342 const TemplateArgument &Param,
Douglas Gregor77d6bb92011-01-11 22:21:24 +00001343 TemplateArgument Arg,
John McCall2a7fb272010-08-25 05:32:35 +00001344 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +00001345 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor77d6bb92011-01-11 22:21:24 +00001346 // If the template argument is a pack expansion, perform template argument
1347 // deduction against the pattern of that expansion. This only occurs during
1348 // partial ordering.
1349 if (Arg.isPackExpansion())
1350 Arg = Arg.getPackExpansionPattern();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001351
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001352 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +00001353 case TemplateArgument::Null:
1354 assert(false && "Null template argument in parameter list");
1355 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001356
1357 case TemplateArgument::Type:
Douglas Gregor788cd062009-11-11 01:00:40 +00001358 if (Arg.getKind() == TemplateArgument::Type)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001359 return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
Douglas Gregor788cd062009-11-11 01:00:40 +00001360 Arg.getAsType(), Info, Deduced, 0);
1361 Info.FirstArg = Param;
1362 Info.SecondArg = Arg;
1363 return Sema::TDK_NonDeducedMismatch;
Douglas Gregora7fc9012011-01-05 18:58:31 +00001364
Douglas Gregor788cd062009-11-11 01:00:40 +00001365 case TemplateArgument::Template:
Douglas Gregordb0d4b72009-11-11 23:06:43 +00001366 if (Arg.getKind() == TemplateArgument::Template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001367 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor788cd062009-11-11 01:00:40 +00001368 Param.getAsTemplate(),
Douglas Gregordb0d4b72009-11-11 23:06:43 +00001369 Arg.getAsTemplate(), Info, Deduced);
Douglas Gregor788cd062009-11-11 01:00:40 +00001370 Info.FirstArg = Param;
1371 Info.SecondArg = Arg;
1372 return Sema::TDK_NonDeducedMismatch;
Douglas Gregora7fc9012011-01-05 18:58:31 +00001373
1374 case TemplateArgument::TemplateExpansion:
1375 llvm_unreachable("caller should handle pack expansions");
1376 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001377
Douglas Gregor199d9912009-06-05 00:53:49 +00001378 case TemplateArgument::Declaration:
Douglas Gregor788cd062009-11-11 01:00:40 +00001379 if (Arg.getKind() == TemplateArgument::Declaration &&
1380 Param.getAsDecl()->getCanonicalDecl() ==
1381 Arg.getAsDecl()->getCanonicalDecl())
1382 return Sema::TDK_Success;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001383
Douglas Gregorf67875d2009-06-12 18:26:56 +00001384 Info.FirstArg = Param;
1385 Info.SecondArg = Arg;
1386 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001387
Douglas Gregor199d9912009-06-05 00:53:49 +00001388 case TemplateArgument::Integral:
1389 if (Arg.getKind() == TemplateArgument::Integral) {
Douglas Gregor9d0e4412010-03-26 05:50:28 +00001390 if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral()))
Douglas Gregorf67875d2009-06-12 18:26:56 +00001391 return Sema::TDK_Success;
1392
1393 Info.FirstArg = Param;
1394 Info.SecondArg = Arg;
1395 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +00001396 }
Douglas Gregorf67875d2009-06-12 18:26:56 +00001397
1398 if (Arg.getKind() == TemplateArgument::Expression) {
1399 Info.FirstArg = Param;
1400 Info.SecondArg = Arg;
1401 return Sema::TDK_NonDeducedMismatch;
1402 }
Douglas Gregor199d9912009-06-05 00:53:49 +00001403
Douglas Gregorf67875d2009-06-12 18:26:56 +00001404 Info.FirstArg = Param;
1405 Info.SecondArg = Arg;
1406 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Douglas Gregor199d9912009-06-05 00:53:49 +00001408 case TemplateArgument::Expression: {
Mike Stump1eb44332009-09-09 15:08:12 +00001409 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +00001410 = getDeducedParameterFromExpr(Param.getAsExpr())) {
1411 if (Arg.getKind() == TemplateArgument::Integral)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001412 return DeduceNonTypeTemplateArgument(S, NTTP,
Mike Stump1eb44332009-09-09 15:08:12 +00001413 *Arg.getAsIntegral(),
Douglas Gregor9d0e4412010-03-26 05:50:28 +00001414 Arg.getIntegralType(),
Douglas Gregor02024a92010-03-28 02:42:43 +00001415 /*ArrayBound=*/false,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001416 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +00001417 if (Arg.getKind() == TemplateArgument::Expression)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001418 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +00001419 Info, Deduced);
Douglas Gregor15755cb2009-11-13 23:45:44 +00001420 if (Arg.getKind() == TemplateArgument::Declaration)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001421 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
Douglas Gregor15755cb2009-11-13 23:45:44 +00001422 Info, Deduced);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001423
Douglas Gregorf67875d2009-06-12 18:26:56 +00001424 Info.FirstArg = Param;
1425 Info.SecondArg = Arg;
1426 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +00001427 }
Mike Stump1eb44332009-09-09 15:08:12 +00001428
Douglas Gregor199d9912009-06-05 00:53:49 +00001429 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +00001430 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001431 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001432 case TemplateArgument::Pack:
Douglas Gregor20a55e22010-12-22 18:17:10 +00001433 llvm_unreachable("Argument packs should be expanded by the caller!");
Douglas Gregor199d9912009-06-05 00:53:49 +00001434 }
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Douglas Gregorf67875d2009-06-12 18:26:56 +00001436 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001437}
1438
Douglas Gregor20a55e22010-12-22 18:17:10 +00001439/// \brief Determine whether there is a template argument to be used for
1440/// deduction.
1441///
1442/// This routine "expands" argument packs in-place, overriding its input
1443/// parameters so that \c Args[ArgIdx] will be the available template argument.
1444///
1445/// \returns true if there is another template argument (which will be at
1446/// \c Args[ArgIdx]), false otherwise.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001447static bool hasTemplateArgumentForDeduction(const TemplateArgument *&Args,
Douglas Gregor20a55e22010-12-22 18:17:10 +00001448 unsigned &ArgIdx,
1449 unsigned &NumArgs) {
1450 if (ArgIdx == NumArgs)
1451 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001452
Douglas Gregor20a55e22010-12-22 18:17:10 +00001453 const TemplateArgument &Arg = Args[ArgIdx];
1454 if (Arg.getKind() != TemplateArgument::Pack)
1455 return true;
1456
1457 assert(ArgIdx == NumArgs - 1 && "Pack not at the end of argument list?");
1458 Args = Arg.pack_begin();
1459 NumArgs = Arg.pack_size();
1460 ArgIdx = 0;
1461 return ArgIdx < NumArgs;
1462}
1463
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001464/// \brief Determine whether the given set of template arguments has a pack
1465/// expansion that is not the last template argument.
1466static bool hasPackExpansionBeforeEnd(const TemplateArgument *Args,
1467 unsigned NumArgs) {
1468 unsigned ArgIdx = 0;
1469 while (ArgIdx < NumArgs) {
1470 const TemplateArgument &Arg = Args[ArgIdx];
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001471
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001472 // Unwrap argument packs.
1473 if (Args[ArgIdx].getKind() == TemplateArgument::Pack) {
1474 Args = Arg.pack_begin();
1475 NumArgs = Arg.pack_size();
1476 ArgIdx = 0;
1477 continue;
1478 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001479
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001480 ++ArgIdx;
1481 if (ArgIdx == NumArgs)
1482 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001483
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001484 if (Arg.isPackExpansion())
1485 return true;
1486 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001487
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001488 return false;
1489}
1490
Douglas Gregor20a55e22010-12-22 18:17:10 +00001491static Sema::TemplateDeductionResult
1492DeduceTemplateArguments(Sema &S,
1493 TemplateParameterList *TemplateParams,
1494 const TemplateArgument *Params, unsigned NumParams,
1495 const TemplateArgument *Args, unsigned NumArgs,
1496 TemplateDeductionInfo &Info,
Douglas Gregor0972c862010-12-22 18:55:49 +00001497 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1498 bool NumberOfArgumentsMustMatch) {
Douglas Gregore02e2622010-12-22 21:19:48 +00001499 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001500 // If the template argument list of P contains a pack expansion that is not
1501 // the last template argument, the entire template argument list is a
Douglas Gregore02e2622010-12-22 21:19:48 +00001502 // non-deduced context.
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001503 if (hasPackExpansionBeforeEnd(Params, NumParams))
1504 return Sema::TDK_Success;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001505
Douglas Gregore02e2622010-12-22 21:19:48 +00001506 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001507 // If P has a form that contains <T> or <i>, then each argument Pi of the
1508 // respective template argument list P is compared with the corresponding
Douglas Gregore02e2622010-12-22 21:19:48 +00001509 // argument Ai of the corresponding template argument list of A.
Douglas Gregor20a55e22010-12-22 18:17:10 +00001510 unsigned ArgIdx = 0, ParamIdx = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001511 for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams);
Douglas Gregor20a55e22010-12-22 18:17:10 +00001512 ++ParamIdx) {
1513 if (!Params[ParamIdx].isPackExpansion()) {
Douglas Gregore02e2622010-12-22 21:19:48 +00001514 // The simple case: deduce template arguments by matching Pi and Ai.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001515
Douglas Gregor20a55e22010-12-22 18:17:10 +00001516 // Check whether we have enough arguments.
1517 if (!hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
Douglas Gregor3cae5c92011-01-10 20:53:55 +00001518 return NumberOfArgumentsMustMatch? Sema::TDK_NonDeducedMismatch
Douglas Gregor0972c862010-12-22 18:55:49 +00001519 : Sema::TDK_Success;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001520
Douglas Gregor77d6bb92011-01-11 22:21:24 +00001521 if (Args[ArgIdx].isPackExpansion()) {
1522 // FIXME: We follow the logic of C++0x [temp.deduct.type]p22 here,
1523 // but applied to pack expansions that are template arguments.
1524 return Sema::TDK_NonDeducedMismatch;
1525 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001526
Douglas Gregore02e2622010-12-22 21:19:48 +00001527 // Perform deduction for this Pi/Ai pair.
Douglas Gregor20a55e22010-12-22 18:17:10 +00001528 if (Sema::TemplateDeductionResult Result
Douglas Gregor77d6bb92011-01-11 22:21:24 +00001529 = DeduceTemplateArguments(S, TemplateParams,
1530 Params[ParamIdx], Args[ArgIdx],
1531 Info, Deduced))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001532 return Result;
1533
Douglas Gregor20a55e22010-12-22 18:17:10 +00001534 // Move to the next argument.
1535 ++ArgIdx;
1536 continue;
1537 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001538
Douglas Gregore02e2622010-12-22 21:19:48 +00001539 // The parameter is a pack expansion.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001540
Douglas Gregore02e2622010-12-22 21:19:48 +00001541 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001542 // If Pi is a pack expansion, then the pattern of Pi is compared with
1543 // each remaining argument in the template argument list of A. Each
1544 // comparison deduces template arguments for subsequent positions in the
Douglas Gregore02e2622010-12-22 21:19:48 +00001545 // template parameter packs expanded by Pi.
1546 TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001547
Douglas Gregore02e2622010-12-22 21:19:48 +00001548 // Compute the set of template parameter indices that correspond to
1549 // parameter packs expanded by the pack expansion.
1550 llvm::SmallVector<unsigned, 2> PackIndices;
1551 {
1552 llvm::BitVector SawIndices(TemplateParams->size());
1553 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1554 S.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1555 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
1556 unsigned Depth, Index;
1557 llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
1558 if (Depth == 0 && !SawIndices[Index]) {
1559 SawIndices[Index] = true;
1560 PackIndices.push_back(Index);
1561 }
1562 }
1563 }
1564 assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001565
Douglas Gregore02e2622010-12-22 21:19:48 +00001566 // FIXME: If there are no remaining arguments, we can bail out early
1567 // and set any deduced parameter packs to an empty argument pack.
1568 // The latter part of this is a (minor) correctness issue.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001569
Douglas Gregore02e2622010-12-22 21:19:48 +00001570 // Save the deduced template arguments for each parameter pack expanded
1571 // by this pack expansion, then clear out the deduction.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001572 llvm::SmallVector<DeducedTemplateArgument, 2>
Douglas Gregore02e2622010-12-22 21:19:48 +00001573 SavedPacks(PackIndices.size());
Douglas Gregor54293852011-01-10 17:35:05 +00001574 llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2>
1575 NewlyDeducedPacks(PackIndices.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001576 PrepareArgumentPackDeduction(S, Deduced, PackIndices, SavedPacks,
Douglas Gregor54293852011-01-10 17:35:05 +00001577 NewlyDeducedPacks);
Douglas Gregore02e2622010-12-22 21:19:48 +00001578
1579 // Keep track of the deduced template arguments for each parameter pack
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001580 // expanded by this pack expansion (the outer index) and for each
Douglas Gregore02e2622010-12-22 21:19:48 +00001581 // template argument (the inner SmallVectors).
Douglas Gregore02e2622010-12-22 21:19:48 +00001582 bool HasAnyArguments = false;
1583 while (hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) {
1584 HasAnyArguments = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001585
Douglas Gregore02e2622010-12-22 21:19:48 +00001586 // Deduce template arguments from the pattern.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001587 if (Sema::TemplateDeductionResult Result
Douglas Gregore02e2622010-12-22 21:19:48 +00001588 = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx],
1589 Info, Deduced))
1590 return Result;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001591
Douglas Gregore02e2622010-12-22 21:19:48 +00001592 // Capture the deduced template arguments for each parameter pack expanded
1593 // by this pack expansion, add them to the list of arguments we've deduced
1594 // for that pack, then clear out the deduced argument.
1595 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
1596 DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
1597 if (!DeducedArg.isNull()) {
1598 NewlyDeducedPacks[I].push_back(DeducedArg);
1599 DeducedArg = DeducedTemplateArgument();
1600 }
1601 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001602
Douglas Gregore02e2622010-12-22 21:19:48 +00001603 ++ArgIdx;
1604 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001605
Douglas Gregore02e2622010-12-22 21:19:48 +00001606 // Build argument packs for each of the parameter packs expanded by this
1607 // pack expansion.
Douglas Gregor0216f812011-01-10 17:53:52 +00001608 if (Sema::TemplateDeductionResult Result
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001609 = FinishArgumentPackDeduction(S, TemplateParams, HasAnyArguments,
Douglas Gregor0216f812011-01-10 17:53:52 +00001610 Deduced, PackIndices, SavedPacks,
1611 NewlyDeducedPacks, Info))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001612 return Result;
Douglas Gregor20a55e22010-12-22 18:17:10 +00001613 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001614
Douglas Gregor20a55e22010-12-22 18:17:10 +00001615 // If there is an argument remaining, then we had too many arguments.
Douglas Gregor0972c862010-12-22 18:55:49 +00001616 if (NumberOfArgumentsMustMatch &&
1617 hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
Douglas Gregor3cae5c92011-01-10 20:53:55 +00001618 return Sema::TDK_NonDeducedMismatch;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001619
Douglas Gregor20a55e22010-12-22 18:17:10 +00001620 return Sema::TDK_Success;
1621}
1622
Mike Stump1eb44332009-09-09 15:08:12 +00001623static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001624DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001625 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001626 const TemplateArgumentList &ParamList,
1627 const TemplateArgumentList &ArgList,
John McCall2a7fb272010-08-25 05:32:35 +00001628 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +00001629 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001630 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor20a55e22010-12-22 18:17:10 +00001631 ParamList.data(), ParamList.size(),
1632 ArgList.data(), ArgList.size(),
1633 Info, Deduced);
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001634}
1635
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001636/// \brief Determine whether two template arguments are the same.
Mike Stump1eb44332009-09-09 15:08:12 +00001637static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001638 const TemplateArgument &X,
1639 const TemplateArgument &Y) {
1640 if (X.getKind() != Y.getKind())
1641 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001642
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001643 switch (X.getKind()) {
1644 case TemplateArgument::Null:
1645 assert(false && "Comparing NULL template argument");
1646 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001648 case TemplateArgument::Type:
1649 return Context.getCanonicalType(X.getAsType()) ==
1650 Context.getCanonicalType(Y.getAsType());
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001652 case TemplateArgument::Declaration:
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001653 return X.getAsDecl()->getCanonicalDecl() ==
1654 Y.getAsDecl()->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001655
Douglas Gregor788cd062009-11-11 01:00:40 +00001656 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00001657 case TemplateArgument::TemplateExpansion:
1658 return Context.getCanonicalTemplateName(
1659 X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() ==
1660 Context.getCanonicalTemplateName(
1661 Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001662
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001663 case TemplateArgument::Integral:
1664 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump1eb44332009-09-09 15:08:12 +00001665
Douglas Gregor788cd062009-11-11 01:00:40 +00001666 case TemplateArgument::Expression: {
1667 llvm::FoldingSetNodeID XID, YID;
1668 X.getAsExpr()->Profile(XID, Context, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001669 Y.getAsExpr()->Profile(YID, Context, true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001670 return XID == YID;
1671 }
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001673 case TemplateArgument::Pack:
1674 if (X.pack_size() != Y.pack_size())
1675 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001676
1677 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
1678 XPEnd = X.pack_end(),
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001679 YP = Y.pack_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001680 XP != XPEnd; ++XP, ++YP)
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001681 if (!isSameTemplateArg(Context, *XP, *YP))
1682 return false;
1683
1684 return true;
1685 }
1686
1687 return false;
1688}
1689
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001690/// \brief Allocate a TemplateArgumentLoc where all locations have
1691/// been initialized to the given location.
1692///
1693/// \param S The semantic analysis object.
1694///
1695/// \param The template argument we are producing template argument
1696/// location information for.
1697///
1698/// \param NTTPType For a declaration template argument, the type of
1699/// the non-type template parameter that corresponds to this template
1700/// argument.
1701///
1702/// \param Loc The source location to use for the resulting template
1703/// argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001704static TemplateArgumentLoc
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001705getTrivialTemplateArgumentLoc(Sema &S,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001706 const TemplateArgument &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001707 QualType NTTPType,
1708 SourceLocation Loc) {
1709 switch (Arg.getKind()) {
1710 case TemplateArgument::Null:
1711 llvm_unreachable("Can't get a NULL template argument here");
1712 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001713
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001714 case TemplateArgument::Type:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001715 return TemplateArgumentLoc(Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001716 S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001717
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001718 case TemplateArgument::Declaration: {
1719 Expr *E
Douglas Gregorba68eca2011-01-05 17:40:24 +00001720 = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001721 .takeAs<Expr>();
1722 return TemplateArgumentLoc(TemplateArgument(E), E);
1723 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001724
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001725 case TemplateArgument::Integral: {
1726 Expr *E
Douglas Gregorba68eca2011-01-05 17:40:24 +00001727 = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001728 return TemplateArgumentLoc(TemplateArgument(E), E);
1729 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001730
Douglas Gregorb6744ef2011-03-02 17:09:35 +00001731 case TemplateArgument::Template:
1732 case TemplateArgument::TemplateExpansion: {
1733 NestedNameSpecifierLocBuilder Builder;
1734 TemplateName Template = Arg.getAsTemplate();
1735 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
1736 Builder.MakeTrivial(S.Context, DTN->getQualifier(), Loc);
1737 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
1738 Builder.MakeTrivial(S.Context, QTN->getQualifier(), Loc);
1739
1740 if (Arg.getKind() == TemplateArgument::Template)
1741 return TemplateArgumentLoc(Arg,
1742 Builder.getWithLocInContext(S.Context),
1743 Loc);
1744
1745
1746 return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(S.Context),
1747 Loc, Loc);
1748 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001749
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001750 case TemplateArgument::Expression:
1751 return TemplateArgumentLoc(Arg, Arg.getAsExpr());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001752
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001753 case TemplateArgument::Pack:
1754 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo());
1755 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001756
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001757 return TemplateArgumentLoc();
1758}
1759
1760
1761/// \brief Convert the given deduced template argument and add it to the set of
1762/// fully-converted template arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001763static bool ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001764 DeducedTemplateArgument Arg,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001765 NamedDecl *Template,
1766 QualType NTTPType,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001767 unsigned ArgumentPackIndex,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001768 TemplateDeductionInfo &Info,
1769 bool InFunctionTemplate,
1770 llvm::SmallVectorImpl<TemplateArgument> &Output) {
1771 if (Arg.getKind() == TemplateArgument::Pack) {
1772 // This is a template argument pack, so check each of its arguments against
1773 // the template parameter.
1774 llvm::SmallVector<TemplateArgument, 2> PackedArgsBuilder;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001775 for (TemplateArgument::pack_iterator PA = Arg.pack_begin(),
Douglas Gregor135ffa72011-01-05 21:00:53 +00001776 PAEnd = Arg.pack_end();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001777 PA != PAEnd; ++PA) {
Douglas Gregord53e16a2011-01-05 20:52:18 +00001778 // When converting the deduced template argument, append it to the
1779 // general output list. We need to do this so that the template argument
1780 // checking logic has all of the prior template arguments available.
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001781 DeducedTemplateArgument InnerArg(*PA);
1782 InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001783 if (ConvertDeducedTemplateArgument(S, Param, InnerArg, Template,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001784 NTTPType, PackedArgsBuilder.size(),
1785 Info, InFunctionTemplate, Output))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001786 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001787
Douglas Gregord53e16a2011-01-05 20:52:18 +00001788 // Move the converted template argument into our argument pack.
1789 PackedArgsBuilder.push_back(Output.back());
1790 Output.pop_back();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001791 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001792
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001793 // Create the resulting argument pack.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001794 Output.push_back(TemplateArgument::CreatePackCopy(S.Context,
Douglas Gregor203e6a32011-01-11 23:09:57 +00001795 PackedArgsBuilder.data(),
1796 PackedArgsBuilder.size()));
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001797 return false;
1798 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001799
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001800 // Convert the deduced template argument into a template
1801 // argument that we can check, almost as if the user had written
1802 // the template argument explicitly.
1803 TemplateArgumentLoc ArgLoc = getTrivialTemplateArgumentLoc(S, Arg, NTTPType,
1804 Info.getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001805
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001806 // Check the template argument, converting it as necessary.
1807 return S.CheckTemplateArgument(Param, ArgLoc,
1808 Template,
1809 Template->getLocation(),
1810 Template->getSourceRange().getEnd(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001811 ArgumentPackIndex,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001812 Output,
1813 InFunctionTemplate
1814 ? (Arg.wasDeducedFromArrayBound()
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001815 ? Sema::CTAK_DeducedFromArrayBound
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001816 : Sema::CTAK_Deduced)
1817 : Sema::CTAK_Specified);
1818}
1819
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001820/// Complete template argument deduction for a class template partial
1821/// specialization.
1822static Sema::TemplateDeductionResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001823FinishTemplateArgumentDeduction(Sema &S,
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001824 ClassTemplatePartialSpecializationDecl *Partial,
1825 const TemplateArgumentList &TemplateArgs,
1826 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
John McCall2a7fb272010-08-25 05:32:35 +00001827 TemplateDeductionInfo &Info) {
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001828 // Trap errors.
1829 Sema::SFINAETrap Trap(S);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001830
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001831 Sema::ContextRAII SavedContext(S, Partial);
1832
1833 // C++ [temp.deduct.type]p2:
1834 // [...] or if any template argument remains neither deduced nor
1835 // explicitly specified, template argument deduction fails.
Douglas Gregor910f8002010-11-07 23:05:16 +00001836 llvm::SmallVector<TemplateArgument, 4> Builder;
Douglas Gregor033a3ca2011-01-04 22:23:38 +00001837 TemplateParameterList *PartialParams = Partial->getTemplateParameters();
1838 for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) {
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001839 NamedDecl *Param = PartialParams->getParam(I);
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001840 if (Deduced[I].isNull()) {
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001841 Info.Param = makeTemplateParameter(Param);
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001842 return Sema::TDK_Incomplete;
1843 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001844
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001845 // We have deduced this argument, so it still needs to be
1846 // checked and converted.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001847
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001848 // First, for a non-type template parameter type that is
1849 // initialized by a declaration, we need the type of the
1850 // corresponding non-type template parameter.
1851 QualType NTTPType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001852 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregord53e16a2011-01-05 20:52:18 +00001853 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001854 NTTPType = NTTP->getType();
Douglas Gregord53e16a2011-01-05 20:52:18 +00001855 if (NTTPType->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001856 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregord53e16a2011-01-05 20:52:18 +00001857 Builder.data(), Builder.size());
1858 NTTPType = S.SubstType(NTTPType,
1859 MultiLevelTemplateArgumentList(TemplateArgs),
1860 NTTP->getLocation(),
1861 NTTP->getDeclName());
1862 if (NTTPType.isNull()) {
1863 Info.Param = makeTemplateParameter(Param);
1864 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001865 Info.reset(TemplateArgumentList::CreateCopy(S.Context,
1866 Builder.data(),
Douglas Gregord53e16a2011-01-05 20:52:18 +00001867 Builder.size()));
1868 return Sema::TDK_SubstitutionFailure;
1869 }
1870 }
1871 }
1872
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001873 if (ConvertDeducedTemplateArgument(S, Param, Deduced[I],
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001874 Partial, NTTPType, 0, Info, false,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001875 Builder)) {
1876 Info.Param = makeTemplateParameter(Param);
1877 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001878 Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
1879 Builder.size()));
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001880 return Sema::TDK_SubstitutionFailure;
1881 }
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001882 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001883
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001884 // Form the template argument list from the deduced template arguments.
1885 TemplateArgumentList *DeducedArgumentList
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001886 = TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001887 Builder.size());
1888
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001889 Info.reset(DeducedArgumentList);
1890
1891 // Substitute the deduced template arguments into the template
1892 // arguments of the class template partial specialization, and
1893 // verify that the instantiated template arguments are both valid
1894 // and are equivalent to the template arguments originally provided
1895 // to the class template.
John McCall2a7fb272010-08-25 05:32:35 +00001896 LocalInstantiationScope InstScope(S);
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001897 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
1898 const TemplateArgumentLoc *PartialTemplateArgs
1899 = Partial->getTemplateArgsAsWritten();
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001900
1901 // Note that we don't provide the langle and rangle locations.
1902 TemplateArgumentListInfo InstArgs;
1903
Douglas Gregore02e2622010-12-22 21:19:48 +00001904 if (S.Subst(PartialTemplateArgs,
1905 Partial->getNumTemplateArgsAsWritten(),
1906 InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
1907 unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx;
1908 if (ParamIdx >= Partial->getTemplateParameters()->size())
1909 ParamIdx = Partial->getTemplateParameters()->size() - 1;
1910
1911 Decl *Param
1912 = const_cast<NamedDecl *>(
1913 Partial->getTemplateParameters()->getParam(ParamIdx));
1914 Info.Param = makeTemplateParameter(Param);
1915 Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument();
1916 return Sema::TDK_SubstitutionFailure;
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001917 }
1918
Douglas Gregor910f8002010-11-07 23:05:16 +00001919 llvm::SmallVector<TemplateArgument, 4> ConvertedInstArgs;
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001920 if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001921 InstArgs, false, ConvertedInstArgs))
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001922 return Sema::TDK_SubstitutionFailure;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001923
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001924 TemplateParameterList *TemplateParams
1925 = ClassTemplate->getTemplateParameters();
1926 for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) {
Douglas Gregor910f8002010-11-07 23:05:16 +00001927 TemplateArgument InstArg = ConvertedInstArgs.data()[I];
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001928 if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
Douglas Gregor2fdc5e82011-01-05 00:13:17 +00001929 Info.Param = makeTemplateParameter(TemplateParams->getParam(I));
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001930 Info.FirstArg = TemplateArgs[I];
1931 Info.SecondArg = InstArg;
1932 return Sema::TDK_NonDeducedMismatch;
1933 }
1934 }
1935
1936 if (Trap.hasErrorOccurred())
1937 return Sema::TDK_SubstitutionFailure;
1938
1939 return Sema::TDK_Success;
1940}
1941
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001942/// \brief Perform template argument deduction to determine whether
1943/// the given template arguments match the given class template
1944/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +00001945Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001946Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001947 const TemplateArgumentList &TemplateArgs,
1948 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001949 // C++ [temp.class.spec.match]p2:
1950 // A partial specialization matches a given actual template
1951 // argument list if the template arguments of the partial
1952 // specialization can be deduced from the actual template argument
1953 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +00001954 SFINAETrap Trap(*this);
Douglas Gregor02024a92010-03-28 02:42:43 +00001955 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001956 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +00001957 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001958 = ::DeduceTemplateArguments(*this,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001959 Partial->getTemplateParameters(),
Mike Stump1eb44332009-09-09 15:08:12 +00001960 Partial->getTemplateArgs(),
Douglas Gregorf67875d2009-06-12 18:26:56 +00001961 TemplateArgs, Info, Deduced))
1962 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +00001963
Douglas Gregor637a4092009-06-10 23:47:09 +00001964 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
Douglas Gregor9b623632010-10-12 23:32:35 +00001965 Deduced.data(), Deduced.size(), Info);
Douglas Gregor637a4092009-06-10 23:47:09 +00001966 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001967 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +00001968
Douglas Gregorbb260412009-06-14 08:02:22 +00001969 if (Trap.hasErrorOccurred())
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001970 return Sema::TDK_SubstitutionFailure;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001971
1972 return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001973 Deduced, Info);
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001974}
Douglas Gregor031a5882009-06-13 00:26:55 +00001975
Douglas Gregor41128772009-06-26 23:27:24 +00001976/// \brief Determine whether the given type T is a simple-template-id type.
1977static bool isSimpleTemplateIdType(QualType T) {
Mike Stump1eb44332009-09-09 15:08:12 +00001978 if (const TemplateSpecializationType *Spec
John McCall183700f2009-09-21 23:43:11 +00001979 = T->getAs<TemplateSpecializationType>())
Douglas Gregor41128772009-06-26 23:27:24 +00001980 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Douglas Gregor41128772009-06-26 23:27:24 +00001982 return false;
1983}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001984
1985/// \brief Substitute the explicitly-provided template arguments into the
1986/// given function template according to C++ [temp.arg.explicit].
1987///
1988/// \param FunctionTemplate the function template into which the explicit
1989/// template arguments will be substituted.
1990///
Mike Stump1eb44332009-09-09 15:08:12 +00001991/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001992/// arguments.
1993///
Mike Stump1eb44332009-09-09 15:08:12 +00001994/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor83314aa2009-07-08 20:55:45 +00001995/// with the converted and checked explicit template arguments.
1996///
Mike Stump1eb44332009-09-09 15:08:12 +00001997/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor83314aa2009-07-08 20:55:45 +00001998/// parameters.
1999///
2000/// \param FunctionType if non-NULL, the result type of the function template
2001/// will also be instantiated and the pointed-to value will be updated with
2002/// the instantiated function type.
2003///
2004/// \param Info if substitution fails for any reason, this object will be
2005/// populated with more information about the failure.
2006///
2007/// \returns TDK_Success if substitution was successful, or some failure
2008/// condition.
2009Sema::TemplateDeductionResult
2010Sema::SubstituteExplicitTemplateArguments(
2011 FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor67714232011-03-03 02:41:12 +00002012 TemplateArgumentListInfo &ExplicitTemplateArgs,
Douglas Gregor02024a92010-03-28 02:42:43 +00002013 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002014 llvm::SmallVectorImpl<QualType> &ParamTypes,
2015 QualType *FunctionType,
2016 TemplateDeductionInfo &Info) {
2017 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2018 TemplateParameterList *TemplateParams
2019 = FunctionTemplate->getTemplateParameters();
2020
John McCalld5532b62009-11-23 01:53:49 +00002021 if (ExplicitTemplateArgs.size() == 0) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00002022 // No arguments to substitute; just copy over the parameter types and
2023 // fill in the function type.
2024 for (FunctionDecl::param_iterator P = Function->param_begin(),
2025 PEnd = Function->param_end();
2026 P != PEnd;
2027 ++P)
2028 ParamTypes.push_back((*P)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002029
Douglas Gregor83314aa2009-07-08 20:55:45 +00002030 if (FunctionType)
2031 *FunctionType = Function->getType();
2032 return TDK_Success;
2033 }
Mike Stump1eb44332009-09-09 15:08:12 +00002034
Douglas Gregor83314aa2009-07-08 20:55:45 +00002035 // Substitution of the explicit template arguments into a function template
2036 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002037 SFINAETrap Trap(*this);
2038
Douglas Gregor83314aa2009-07-08 20:55:45 +00002039 // C++ [temp.arg.explicit]p3:
Mike Stump1eb44332009-09-09 15:08:12 +00002040 // Template arguments that are present shall be specified in the
2041 // declaration order of their corresponding template-parameters. The
Douglas Gregor83314aa2009-07-08 20:55:45 +00002042 // template argument list shall not specify more template-arguments than
Mike Stump1eb44332009-09-09 15:08:12 +00002043 // there are corresponding template-parameters.
Douglas Gregor910f8002010-11-07 23:05:16 +00002044 llvm::SmallVector<TemplateArgument, 4> Builder;
Mike Stump1eb44332009-09-09 15:08:12 +00002045
2046 // Enter a new template instantiation context where we check the
Douglas Gregor83314aa2009-07-08 20:55:45 +00002047 // explicitly-specified template arguments against this function template,
2048 // and then substitute them into the function parameter types.
Mike Stump1eb44332009-09-09 15:08:12 +00002049 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00002050 FunctionTemplate, Deduced.data(), Deduced.size(),
Douglas Gregor9b623632010-10-12 23:32:35 +00002051 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution,
2052 Info);
Douglas Gregor83314aa2009-07-08 20:55:45 +00002053 if (Inst)
2054 return TDK_InstantiationDepth;
Mike Stump1eb44332009-09-09 15:08:12 +00002055
Douglas Gregor83314aa2009-07-08 20:55:45 +00002056 if (CheckTemplateArgumentList(FunctionTemplate,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002057 SourceLocation(),
John McCalld5532b62009-11-23 01:53:49 +00002058 ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002059 true,
Douglas Gregorf1a84452010-05-08 19:15:54 +00002060 Builder) || Trap.hasErrorOccurred()) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002061 unsigned Index = Builder.size();
Douglas Gregorfe52c912010-05-09 01:26:06 +00002062 if (Index >= TemplateParams->size())
2063 Index = TemplateParams->size() - 1;
2064 Info.Param = makeTemplateParameter(TemplateParams->getParam(Index));
Douglas Gregor83314aa2009-07-08 20:55:45 +00002065 return TDK_InvalidExplicitArguments;
Douglas Gregorf1a84452010-05-08 19:15:54 +00002066 }
Mike Stump1eb44332009-09-09 15:08:12 +00002067
Douglas Gregor83314aa2009-07-08 20:55:45 +00002068 // Form the template argument list from the explicitly-specified
2069 // template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00002070 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor910f8002010-11-07 23:05:16 +00002071 = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
Douglas Gregor83314aa2009-07-08 20:55:45 +00002072 Info.reset(ExplicitArgumentList);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002073
John McCalldf41f182010-10-12 19:40:14 +00002074 // Template argument deduction and the final substitution should be
2075 // done in the context of the templated declaration. Explicit
2076 // argument substitution, on the other hand, needs to happen in the
2077 // calling context.
2078 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
2079
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002080 // If we deduced template arguments for a template parameter pack,
Douglas Gregord3731192011-01-10 07:32:04 +00002081 // note that the template argument pack is partially substituted and record
2082 // the explicit template arguments. They'll be used as part of deduction
2083 // for this template parameter pack.
Douglas Gregord3731192011-01-10 07:32:04 +00002084 for (unsigned I = 0, N = Builder.size(); I != N; ++I) {
2085 const TemplateArgument &Arg = Builder[I];
2086 if (Arg.getKind() == TemplateArgument::Pack) {
Douglas Gregord3731192011-01-10 07:32:04 +00002087 CurrentInstantiationScope->SetPartiallySubstitutedPack(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002088 TemplateParams->getParam(I),
Douglas Gregord3731192011-01-10 07:32:04 +00002089 Arg.pack_begin(),
2090 Arg.pack_size());
2091 break;
2092 }
2093 }
2094
Douglas Gregor83314aa2009-07-08 20:55:45 +00002095 // Instantiate the types of each of the function parameters given the
2096 // explicitly-specified template arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002097 if (SubstParmTypes(Function->getLocation(),
Douglas Gregora009b592011-01-07 00:20:55 +00002098 Function->param_begin(), Function->getNumParams(),
2099 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
2100 ParamTypes))
2101 return TDK_SubstitutionFailure;
Douglas Gregor83314aa2009-07-08 20:55:45 +00002102
2103 // If the caller wants a full function type back, instantiate the return
2104 // type and form that function type.
2105 if (FunctionType) {
2106 // FIXME: exception-specifications?
Mike Stump1eb44332009-09-09 15:08:12 +00002107 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00002108 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor83314aa2009-07-08 20:55:45 +00002109 assert(Proto && "Function template does not have a prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00002110
2111 QualType ResultType
Douglas Gregor357bbd02009-08-28 20:50:45 +00002112 = SubstType(Proto->getResultType(),
2113 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
2114 Function->getTypeSpecStartLoc(),
2115 Function->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00002116 if (ResultType.isNull() || Trap.hasErrorOccurred())
2117 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00002118
2119 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002120 ParamTypes.data(), ParamTypes.size(),
2121 Proto->isVariadic(),
2122 Proto->getTypeQuals(),
Douglas Gregorc938c162011-01-26 05:01:58 +00002123 Proto->getRefQualifier(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00002124 Function->getLocation(),
Eli Friedmanfa869542010-08-05 02:54:05 +00002125 Function->getDeclName(),
2126 Proto->getExtInfo());
Douglas Gregor83314aa2009-07-08 20:55:45 +00002127 if (FunctionType->isNull() || Trap.hasErrorOccurred())
2128 return TDK_SubstitutionFailure;
2129 }
Mike Stump1eb44332009-09-09 15:08:12 +00002130
Douglas Gregor83314aa2009-07-08 20:55:45 +00002131 // C++ [temp.arg.explicit]p2:
Mike Stump1eb44332009-09-09 15:08:12 +00002132 // Trailing template arguments that can be deduced (14.8.2) may be
2133 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor83314aa2009-07-08 20:55:45 +00002134 // template arguments can be deduced, they may all be omitted; in this
2135 // case, the empty template argument list <> itself may also be omitted.
2136 //
Douglas Gregord3731192011-01-10 07:32:04 +00002137 // Take all of the explicitly-specified arguments and put them into
2138 // the set of deduced template arguments. Explicitly-specified
2139 // parameter packs, however, will be set to NULL since the deduction
2140 // mechanisms handle explicitly-specified argument packs directly.
Douglas Gregor83314aa2009-07-08 20:55:45 +00002141 Deduced.reserve(TemplateParams->size());
Douglas Gregord3731192011-01-10 07:32:04 +00002142 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) {
2143 const TemplateArgument &Arg = ExplicitArgumentList->get(I);
2144 if (Arg.getKind() == TemplateArgument::Pack)
2145 Deduced.push_back(DeducedTemplateArgument());
2146 else
2147 Deduced.push_back(Arg);
2148 }
Mike Stump1eb44332009-09-09 15:08:12 +00002149
Douglas Gregor83314aa2009-07-08 20:55:45 +00002150 return TDK_Success;
2151}
2152
Mike Stump1eb44332009-09-09 15:08:12 +00002153/// \brief Finish template argument deduction for a function template,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002154/// checking the deduced template arguments for completeness and forming
2155/// the function template specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00002156Sema::TemplateDeductionResult
Douglas Gregor83314aa2009-07-08 20:55:45 +00002157Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor02024a92010-03-28 02:42:43 +00002158 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
2159 unsigned NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002160 FunctionDecl *&Specialization,
2161 TemplateDeductionInfo &Info) {
2162 TemplateParameterList *TemplateParams
2163 = FunctionTemplate->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00002164
Douglas Gregor83314aa2009-07-08 20:55:45 +00002165 // Template argument deduction for function templates in a SFINAE context.
2166 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002167 SFINAETrap Trap(*this);
2168
Douglas Gregor83314aa2009-07-08 20:55:45 +00002169 // Enter a new template instantiation context while we instantiate the
2170 // actual function declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002171 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00002172 FunctionTemplate, Deduced.data(), Deduced.size(),
Douglas Gregor9b623632010-10-12 23:32:35 +00002173 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution,
2174 Info);
Douglas Gregor83314aa2009-07-08 20:55:45 +00002175 if (Inst)
Mike Stump1eb44332009-09-09 15:08:12 +00002176 return TDK_InstantiationDepth;
2177
John McCall96db3102010-04-29 01:18:58 +00002178 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
John McCallf5813822010-04-29 00:35:03 +00002179
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002180 // C++ [temp.deduct.type]p2:
2181 // [...] or if any template argument remains neither deduced nor
2182 // explicitly specified, template argument deduction fails.
Douglas Gregor910f8002010-11-07 23:05:16 +00002183 llvm::SmallVector<TemplateArgument, 4> Builder;
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002184 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
2185 NamedDecl *Param = TemplateParams->getParam(I);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002186
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002187 if (!Deduced[I].isNull()) {
Douglas Gregor3273b0c2010-10-12 18:51:08 +00002188 if (I < NumExplicitlySpecified) {
Douglas Gregor02024a92010-03-28 02:42:43 +00002189 // We have already fully type-checked and converted this
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002190 // argument, because it was explicitly-specified. Just record the
Douglas Gregor3273b0c2010-10-12 18:51:08 +00002191 // presence of this argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002192 Builder.push_back(Deduced[I]);
Douglas Gregor02024a92010-03-28 02:42:43 +00002193 continue;
2194 }
2195
2196 // We have deduced this argument, so it still needs to be
2197 // checked and converted.
2198
2199 // First, for a non-type template parameter type that is
2200 // initialized by a declaration, we need the type of the
2201 // corresponding non-type template parameter.
2202 QualType NTTPType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002203 if (NonTypeTemplateParmDecl *NTTP
2204 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002205 NTTPType = NTTP->getType();
2206 if (NTTPType->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002207 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002208 Builder.data(), Builder.size());
2209 NTTPType = SubstType(NTTPType,
2210 MultiLevelTemplateArgumentList(TemplateArgs),
2211 NTTP->getLocation(),
2212 NTTP->getDeclName());
2213 if (NTTPType.isNull()) {
2214 Info.Param = makeTemplateParameter(Param);
2215 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002216 Info.reset(TemplateArgumentList::CreateCopy(Context,
2217 Builder.data(),
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002218 Builder.size()));
2219 return TDK_SubstitutionFailure;
Douglas Gregor02024a92010-03-28 02:42:43 +00002220 }
2221 }
2222 }
2223
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002224 if (ConvertDeducedTemplateArgument(*this, Param, Deduced[I],
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002225 FunctionTemplate, NTTPType, 0, Info,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002226 true, Builder)) {
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002227 Info.Param = makeTemplateParameter(Param);
Douglas Gregor910f8002010-11-07 23:05:16 +00002228 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002229 Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
2230 Builder.size()));
Douglas Gregor02024a92010-03-28 02:42:43 +00002231 return TDK_SubstitutionFailure;
2232 }
2233
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002234 continue;
2235 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002236
Douglas Gregorea6c96f2010-12-23 01:52:01 +00002237 // C++0x [temp.arg.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002238 // A trailing template parameter pack (14.5.3) not otherwise deduced will
Douglas Gregorea6c96f2010-12-23 01:52:01 +00002239 // be deduced to an empty sequence of template arguments.
2240 // FIXME: Where did the word "trailing" come from?
2241 if (Param->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002242 // We may have had explicitly-specified template arguments for this
2243 // template parameter pack. If so, our empty deduction extends the
2244 // explicitly-specified set (C++0x [temp.arg.explicit]p9).
2245 const TemplateArgument *ExplicitArgs;
2246 unsigned NumExplicitArgs;
2247 if (CurrentInstantiationScope->getPartiallySubstitutedPack(&ExplicitArgs,
2248 &NumExplicitArgs)
2249 == Param)
2250 Builder.push_back(TemplateArgument(ExplicitArgs, NumExplicitArgs));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002251 else
Douglas Gregord3731192011-01-10 07:32:04 +00002252 Builder.push_back(TemplateArgument(0, 0));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002253
Douglas Gregorea6c96f2010-12-23 01:52:01 +00002254 continue;
2255 }
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002256
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002257 // Substitute into the default template argument, if available.
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002258 TemplateArgumentLoc DefArg
2259 = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
2260 FunctionTemplate->getLocation(),
2261 FunctionTemplate->getSourceRange().getEnd(),
2262 Param,
2263 Builder);
2264
2265 // If there was no default argument, deduction is incomplete.
2266 if (DefArg.getArgument().isNull()) {
2267 Info.Param = makeTemplateParameter(
2268 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
2269 return TDK_Incomplete;
2270 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002271
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002272 // Check whether we can actually use the default argument.
2273 if (CheckTemplateArgument(Param, DefArg,
2274 FunctionTemplate,
2275 FunctionTemplate->getLocation(),
2276 FunctionTemplate->getSourceRange().getEnd(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002277 0, Builder,
Douglas Gregor02024a92010-03-28 02:42:43 +00002278 CTAK_Deduced)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002279 Info.Param = makeTemplateParameter(
2280 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregor910f8002010-11-07 23:05:16 +00002281 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002282 Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002283 Builder.size()));
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002284 return TDK_SubstitutionFailure;
2285 }
2286
2287 // If we get here, we successfully used the default template argument.
2288 }
2289
2290 // Form the template argument list from the deduced template arguments.
2291 TemplateArgumentList *DeducedArgumentList
Douglas Gregor910f8002010-11-07 23:05:16 +00002292 = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002293 Info.reset(DeducedArgumentList);
2294
Mike Stump1eb44332009-09-09 15:08:12 +00002295 // Substitute the deduced template arguments into the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00002296 // declaration to produce the function template specialization.
Douglas Gregord4598a22010-04-28 04:52:24 +00002297 DeclContext *Owner = FunctionTemplate->getDeclContext();
2298 if (FunctionTemplate->getFriendObjectKind())
2299 Owner = FunctionTemplate->getLexicalDeclContext();
Douglas Gregor83314aa2009-07-08 20:55:45 +00002300 Specialization = cast_or_null<FunctionDecl>(
Douglas Gregord4598a22010-04-28 04:52:24 +00002301 SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner,
Douglas Gregor357bbd02009-08-28 20:50:45 +00002302 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor83314aa2009-07-08 20:55:45 +00002303 if (!Specialization)
2304 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00002305
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002306 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
Douglas Gregorf8825742009-09-15 18:26:13 +00002307 FunctionTemplate->getCanonicalDecl());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002308
Mike Stump1eb44332009-09-09 15:08:12 +00002309 // If the template argument list is owned by the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00002310 // specialization, release it.
Douglas Gregorec20f462010-05-08 20:07:26 +00002311 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList &&
2312 !Trap.hasErrorOccurred())
Douglas Gregor83314aa2009-07-08 20:55:45 +00002313 Info.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002314
Douglas Gregor83314aa2009-07-08 20:55:45 +00002315 // There may have been an error that did not prevent us from constructing a
2316 // declaration. Mark the declaration invalid and return with a substitution
2317 // failure.
2318 if (Trap.hasErrorOccurred()) {
2319 Specialization->setInvalidDecl(true);
2320 return TDK_SubstitutionFailure;
2321 }
Mike Stump1eb44332009-09-09 15:08:12 +00002322
Douglas Gregor9b623632010-10-12 23:32:35 +00002323 // If we suppressed any diagnostics while performing template argument
2324 // deduction, and if we haven't already instantiated this declaration,
2325 // keep track of these diagnostics. They'll be emitted if this specialization
2326 // is actually used.
2327 if (Info.diag_begin() != Info.diag_end()) {
2328 llvm::DenseMap<Decl *, llvm::SmallVector<PartialDiagnosticAt, 1> >::iterator
2329 Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl());
2330 if (Pos == SuppressedDiagnostics.end())
2331 SuppressedDiagnostics[Specialization->getCanonicalDecl()]
2332 .append(Info.diag_begin(), Info.diag_end());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002333 }
Douglas Gregor9b623632010-10-12 23:32:35 +00002334
Mike Stump1eb44332009-09-09 15:08:12 +00002335 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00002336}
2337
John McCall9c72c602010-08-27 09:08:28 +00002338/// Gets the type of a function for template-argument-deducton
2339/// purposes when it's considered as part of an overload set.
John McCalleff92132010-02-02 02:21:27 +00002340static QualType GetTypeOfFunction(ASTContext &Context,
John McCall9c72c602010-08-27 09:08:28 +00002341 const OverloadExpr::FindResult &R,
John McCalleff92132010-02-02 02:21:27 +00002342 FunctionDecl *Fn) {
John McCalleff92132010-02-02 02:21:27 +00002343 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
John McCall9c72c602010-08-27 09:08:28 +00002344 if (Method->isInstance()) {
2345 // An instance method that's referenced in a form that doesn't
2346 // look like a member pointer is just invalid.
2347 if (!R.HasFormOfMemberPointer) return QualType();
2348
John McCalleff92132010-02-02 02:21:27 +00002349 return Context.getMemberPointerType(Fn->getType(),
2350 Context.getTypeDeclType(Method->getParent()).getTypePtr());
John McCall9c72c602010-08-27 09:08:28 +00002351 }
2352
2353 if (!R.IsAddressOfOperand) return Fn->getType();
John McCalleff92132010-02-02 02:21:27 +00002354 return Context.getPointerType(Fn->getType());
2355}
2356
2357/// Apply the deduction rules for overload sets.
2358///
2359/// \return the null type if this argument should be treated as an
2360/// undeduced context
2361static QualType
2362ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
Douglas Gregor75f21af2010-08-30 21:04:23 +00002363 Expr *Arg, QualType ParamType,
2364 bool ParamWasReference) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002365
John McCall9c72c602010-08-27 09:08:28 +00002366 OverloadExpr::FindResult R = OverloadExpr::find(Arg);
John McCalleff92132010-02-02 02:21:27 +00002367
John McCall9c72c602010-08-27 09:08:28 +00002368 OverloadExpr *Ovl = R.Expression;
John McCalleff92132010-02-02 02:21:27 +00002369
Douglas Gregor75f21af2010-08-30 21:04:23 +00002370 // C++0x [temp.deduct.call]p4
2371 unsigned TDF = 0;
2372 if (ParamWasReference)
2373 TDF |= TDF_ParamWithReferenceType;
2374 if (R.IsAddressOfOperand)
2375 TDF |= TDF_IgnoreQualifiers;
2376
John McCalleff92132010-02-02 02:21:27 +00002377 // If there were explicit template arguments, we can only find
2378 // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
2379 // unambiguously name a full specialization.
John McCall7bb12da2010-02-02 06:20:04 +00002380 if (Ovl->hasExplicitTemplateArgs()) {
John McCalleff92132010-02-02 02:21:27 +00002381 // But we can still look for an explicit specialization.
2382 if (FunctionDecl *ExplicitSpec
John McCall7bb12da2010-02-02 06:20:04 +00002383 = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
John McCall9c72c602010-08-27 09:08:28 +00002384 return GetTypeOfFunction(S.Context, R, ExplicitSpec);
John McCalleff92132010-02-02 02:21:27 +00002385 return QualType();
2386 }
2387
2388 // C++0x [temp.deduct.call]p6:
2389 // When P is a function type, pointer to function type, or pointer
2390 // to member function type:
2391
2392 if (!ParamType->isFunctionType() &&
2393 !ParamType->isFunctionPointerType() &&
2394 !ParamType->isMemberFunctionPointerType())
2395 return QualType();
2396
2397 QualType Match;
John McCall7bb12da2010-02-02 06:20:04 +00002398 for (UnresolvedSetIterator I = Ovl->decls_begin(),
2399 E = Ovl->decls_end(); I != E; ++I) {
John McCalleff92132010-02-02 02:21:27 +00002400 NamedDecl *D = (*I)->getUnderlyingDecl();
2401
2402 // - If the argument is an overload set containing one or more
2403 // function templates, the parameter is treated as a
2404 // non-deduced context.
2405 if (isa<FunctionTemplateDecl>(D))
2406 return QualType();
2407
2408 FunctionDecl *Fn = cast<FunctionDecl>(D);
John McCall9c72c602010-08-27 09:08:28 +00002409 QualType ArgType = GetTypeOfFunction(S.Context, R, Fn);
2410 if (ArgType.isNull()) continue;
John McCalleff92132010-02-02 02:21:27 +00002411
Douglas Gregor75f21af2010-08-30 21:04:23 +00002412 // Function-to-pointer conversion.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002413 if (!ParamWasReference && ParamType->isPointerType() &&
Douglas Gregor75f21af2010-08-30 21:04:23 +00002414 ArgType->isFunctionType())
2415 ArgType = S.Context.getPointerType(ArgType);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002416
John McCalleff92132010-02-02 02:21:27 +00002417 // - If the argument is an overload set (not containing function
2418 // templates), trial argument deduction is attempted using each
2419 // of the members of the set. If deduction succeeds for only one
2420 // of the overload set members, that member is used as the
2421 // argument value for the deduction. If deduction succeeds for
2422 // more than one member of the overload set the parameter is
2423 // treated as a non-deduced context.
2424
2425 // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
2426 // Type deduction is done independently for each P/A pair, and
2427 // the deduced template argument values are then combined.
2428 // So we do not reject deductions which were made elsewhere.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002429 llvm::SmallVector<DeducedTemplateArgument, 8>
Douglas Gregor02024a92010-03-28 02:42:43 +00002430 Deduced(TemplateParams->size());
John McCall2a7fb272010-08-25 05:32:35 +00002431 TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
John McCalleff92132010-02-02 02:21:27 +00002432 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002433 = DeduceTemplateArguments(S, TemplateParams,
John McCalleff92132010-02-02 02:21:27 +00002434 ParamType, ArgType,
2435 Info, Deduced, TDF);
2436 if (Result) continue;
2437 if (!Match.isNull()) return QualType();
2438 Match = ArgType;
2439 }
2440
2441 return Match;
2442}
2443
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002444/// \brief Perform the adjustments to the parameter and argument types
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002445/// described in C++ [temp.deduct.call].
2446///
2447/// \returns true if the caller should not attempt to perform any template
2448/// argument deduction based on this P/A pair.
2449static bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S,
2450 TemplateParameterList *TemplateParams,
2451 QualType &ParamType,
2452 QualType &ArgType,
2453 Expr *Arg,
2454 unsigned &TDF) {
2455 // C++0x [temp.deduct.call]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00002456 // If P is a cv-qualified type, the top level cv-qualifiers of P's type
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002457 // are ignored for type deduction.
2458 if (ParamType.getCVRQualifiers())
2459 ParamType = ParamType.getLocalUnqualifiedType();
2460 const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>();
2461 if (ParamRefType) {
Richard Smith34b41d92011-02-20 03:19:35 +00002462 QualType PointeeType = ParamRefType->getPointeeType();
2463
Douglas Gregor2ad746a2011-01-21 05:18:22 +00002464 // [C++0x] If P is an rvalue reference to a cv-unqualified
2465 // template parameter and the argument is an lvalue, the type
2466 // "lvalue reference to A" is used in place of A for type
2467 // deduction.
Richard Smith34b41d92011-02-20 03:19:35 +00002468 if (isa<RValueReferenceType>(ParamType)) {
2469 if (!PointeeType.getQualifiers() &&
2470 isa<TemplateTypeParmType>(PointeeType) &&
Douglas Gregor2ad746a2011-01-21 05:18:22 +00002471 Arg->Classify(S.Context).isLValue())
2472 ArgType = S.Context.getLValueReferenceType(ArgType);
2473 }
2474
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002475 // [...] If P is a reference type, the type referred to by P is used
2476 // for type deduction.
Richard Smith34b41d92011-02-20 03:19:35 +00002477 ParamType = PointeeType;
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002478 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002479
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002480 // Overload sets usually make this parameter an undeduced
2481 // context, but there are sometimes special circumstances.
2482 if (ArgType == S.Context.OverloadTy) {
2483 ArgType = ResolveOverloadForDeduction(S, TemplateParams,
2484 Arg, ParamType,
2485 ParamRefType != 0);
2486 if (ArgType.isNull())
2487 return true;
2488 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002489
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002490 if (ParamRefType) {
2491 // C++0x [temp.deduct.call]p3:
2492 // [...] If P is of the form T&&, where T is a template parameter, and
2493 // the argument is an lvalue, the type A& is used in place of A for
2494 // type deduction.
2495 if (ParamRefType->isRValueReferenceType() &&
2496 ParamRefType->getAs<TemplateTypeParmType>() &&
2497 Arg->isLValue())
2498 ArgType = S.Context.getLValueReferenceType(ArgType);
2499 } else {
2500 // C++ [temp.deduct.call]p2:
2501 // If P is not a reference type:
2502 // - If A is an array type, the pointer type produced by the
2503 // array-to-pointer standard conversion (4.2) is used in place of
2504 // A for type deduction; otherwise,
2505 if (ArgType->isArrayType())
2506 ArgType = S.Context.getArrayDecayedType(ArgType);
2507 // - If A is a function type, the pointer type produced by the
2508 // function-to-pointer standard conversion (4.3) is used in place
2509 // of A for type deduction; otherwise,
2510 else if (ArgType->isFunctionType())
2511 ArgType = S.Context.getPointerType(ArgType);
2512 else {
NAKAMURA Takumi00995302011-01-27 07:09:49 +00002513 // - If A is a cv-qualified type, the top level cv-qualifiers of A's
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002514 // type are ignored for type deduction.
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002515 if (ArgType.getCVRQualifiers())
2516 ArgType = ArgType.getUnqualifiedType();
2517 }
2518 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002519
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002520 // C++0x [temp.deduct.call]p4:
2521 // In general, the deduction process attempts to find template argument
2522 // values that will make the deduced A identical to A (after the type A
2523 // is transformed as described above). [...]
2524 TDF = TDF_SkipNonDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002525
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002526 // - If the original P is a reference type, the deduced A (i.e., the
2527 // type referred to by the reference) can be more cv-qualified than
2528 // the transformed A.
2529 if (ParamRefType)
2530 TDF |= TDF_ParamWithReferenceType;
2531 // - The transformed A can be another pointer or pointer to member
2532 // type that can be converted to the deduced A via a qualification
2533 // conversion (4.4).
2534 if (ArgType->isPointerType() || ArgType->isMemberPointerType() ||
2535 ArgType->isObjCObjectPointerType())
2536 TDF |= TDF_IgnoreQualifiers;
2537 // - If P is a class and P has the form simple-template-id, then the
2538 // transformed A can be a derived class of the deduced A. Likewise,
2539 // if P is a pointer to a class of the form simple-template-id, the
2540 // transformed A can be a pointer to a derived class pointed to by
2541 // the deduced A.
2542 if (isSimpleTemplateIdType(ParamType) ||
2543 (isa<PointerType>(ParamType) &&
2544 isSimpleTemplateIdType(
2545 ParamType->getAs<PointerType>()->getPointeeType())))
2546 TDF |= TDF_DerivedClass;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002547
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002548 return false;
2549}
2550
Douglas Gregore53060f2009-06-25 22:08:12 +00002551/// \brief Perform template argument deduction from a function call
2552/// (C++ [temp.deduct.call]).
2553///
2554/// \param FunctionTemplate the function template for which we are performing
2555/// template argument deduction.
2556///
Douglas Gregor48026d22010-01-11 18:40:55 +00002557/// \param ExplicitTemplateArguments the explicit template arguments provided
2558/// for this call.
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002559///
Douglas Gregore53060f2009-06-25 22:08:12 +00002560/// \param Args the function call arguments
2561///
2562/// \param NumArgs the number of arguments in Args
2563///
Douglas Gregor48026d22010-01-11 18:40:55 +00002564/// \param Name the name of the function being called. This is only significant
2565/// when the function template is a conversion function template, in which
2566/// case this routine will also perform template argument deduction based on
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002567/// the function to which
Douglas Gregor48026d22010-01-11 18:40:55 +00002568///
Douglas Gregore53060f2009-06-25 22:08:12 +00002569/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00002570/// this will be set to the function template specialization produced by
Douglas Gregore53060f2009-06-25 22:08:12 +00002571/// template argument deduction.
2572///
2573/// \param Info the argument will be updated to provide additional information
2574/// about template argument deduction.
2575///
2576/// \returns the result of template argument deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00002577Sema::TemplateDeductionResult
2578Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor67714232011-03-03 02:41:12 +00002579 TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregore53060f2009-06-25 22:08:12 +00002580 Expr **Args, unsigned NumArgs,
2581 FunctionDecl *&Specialization,
2582 TemplateDeductionInfo &Info) {
2583 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002584
Douglas Gregore53060f2009-06-25 22:08:12 +00002585 // C++ [temp.deduct.call]p1:
2586 // Template argument deduction is done by comparing each function template
2587 // parameter type (call it P) with the type of the corresponding argument
2588 // of the call (call it A) as described below.
2589 unsigned CheckArgs = NumArgs;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002590 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregore53060f2009-06-25 22:08:12 +00002591 return TDK_TooFewArguments;
2592 else if (NumArgs > Function->getNumParams()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002593 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00002594 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002595 if (Proto->isTemplateVariadic())
2596 /* Do nothing */;
2597 else if (Proto->isVariadic())
2598 CheckArgs = Function->getNumParams();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002599 else
Douglas Gregore53060f2009-06-25 22:08:12 +00002600 return TDK_TooManyArguments;
Douglas Gregore53060f2009-06-25 22:08:12 +00002601 }
Mike Stump1eb44332009-09-09 15:08:12 +00002602
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002603 // The types of the parameters from which we will perform template argument
2604 // deduction.
John McCall2a7fb272010-08-25 05:32:35 +00002605 LocalInstantiationScope InstScope(*this);
Douglas Gregore53060f2009-06-25 22:08:12 +00002606 TemplateParameterList *TemplateParams
2607 = FunctionTemplate->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00002608 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002609 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregor02024a92010-03-28 02:42:43 +00002610 unsigned NumExplicitlySpecified = 0;
John McCalld5532b62009-11-23 01:53:49 +00002611 if (ExplicitTemplateArgs) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00002612 TemplateDeductionResult Result =
2613 SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00002614 *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002615 Deduced,
2616 ParamTypes,
2617 0,
2618 Info);
2619 if (Result)
2620 return Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002621
2622 NumExplicitlySpecified = Deduced.size();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002623 } else {
2624 // Just fill in the parameter types from the function declaration.
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002625 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002626 ParamTypes.push_back(Function->getParamDecl(I)->getType());
2627 }
Mike Stump1eb44332009-09-09 15:08:12 +00002628
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002629 // Deduce template arguments from the function parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00002630 Deduced.resize(TemplateParams->size());
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002631 unsigned ArgIdx = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002632 for (unsigned ParamIdx = 0, NumParams = ParamTypes.size();
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002633 ParamIdx != NumParams; ++ParamIdx) {
2634 QualType ParamType = ParamTypes[ParamIdx];
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002635
2636 const PackExpansionType *ParamExpansion
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002637 = dyn_cast<PackExpansionType>(ParamType);
2638 if (!ParamExpansion) {
2639 // Simple case: matching a function parameter to a function argument.
2640 if (ArgIdx >= CheckArgs)
2641 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002642
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002643 Expr *Arg = Args[ArgIdx++];
2644 QualType ArgType = Arg->getType();
2645 unsigned TDF = 0;
2646 if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams,
2647 ParamType, ArgType, Arg,
2648 TDF))
2649 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002650
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002651 if (TemplateDeductionResult Result
2652 = ::DeduceTemplateArguments(*this, TemplateParams,
2653 ParamType, ArgType, Info, Deduced,
2654 TDF))
2655 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002656
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002657 // FIXME: we need to check that the deduced A is the same as A,
2658 // modulo the various allowed differences.
2659 continue;
Douglas Gregor75f21af2010-08-30 21:04:23 +00002660 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002661
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002662 // C++0x [temp.deduct.call]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002663 // For a function parameter pack that occurs at the end of the
2664 // parameter-declaration-list, the type A of each remaining argument of
2665 // the call is compared with the type P of the declarator-id of the
2666 // function parameter pack. Each comparison deduces template arguments
2667 // for subsequent positions in the template parameter packs expanded by
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00002668 // the function parameter pack. For a function parameter pack that does
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002669 // not occur at the end of the parameter-declaration-list, the type of
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00002670 // the parameter pack is a non-deduced context.
2671 if (ParamIdx + 1 < NumParams)
2672 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002673
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002674 QualType ParamPattern = ParamExpansion->getPattern();
2675 llvm::SmallVector<unsigned, 2> PackIndices;
2676 {
2677 llvm::BitVector SawIndices(TemplateParams->size());
2678 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2679 collectUnexpandedParameterPacks(ParamPattern, Unexpanded);
2680 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
2681 unsigned Depth, Index;
2682 llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
2683 if (Depth == 0 && !SawIndices[Index]) {
2684 SawIndices[Index] = true;
2685 PackIndices.push_back(Index);
2686 }
Douglas Gregore53060f2009-06-25 22:08:12 +00002687 }
2688 }
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002689 assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002690
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002691 // Keep track of the deduced template arguments for each parameter pack
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002692 // expanded by this pack expansion (the outer index) and for each
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002693 // template argument (the inner SmallVectors).
2694 llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2>
Douglas Gregord3731192011-01-10 07:32:04 +00002695 NewlyDeducedPacks(PackIndices.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002696 llvm::SmallVector<DeducedTemplateArgument, 2>
Douglas Gregord3731192011-01-10 07:32:04 +00002697 SavedPacks(PackIndices.size());
Douglas Gregor54293852011-01-10 17:35:05 +00002698 PrepareArgumentPackDeduction(*this, Deduced, PackIndices, SavedPacks,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002699 NewlyDeducedPacks);
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002700 bool HasAnyArguments = false;
2701 for (; ArgIdx < NumArgs; ++ArgIdx) {
2702 HasAnyArguments = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002703
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002704 ParamType = ParamPattern;
2705 Expr *Arg = Args[ArgIdx];
2706 QualType ArgType = Arg->getType();
2707 unsigned TDF = 0;
2708 if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams,
2709 ParamType, ArgType, Arg,
2710 TDF)) {
2711 // We can't actually perform any deduction for this argument, so stop
2712 // deduction at this point.
2713 ++ArgIdx;
2714 break;
2715 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002716
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002717 if (TemplateDeductionResult Result
2718 = ::DeduceTemplateArguments(*this, TemplateParams,
2719 ParamType, ArgType, Info, Deduced,
2720 TDF))
2721 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002722
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002723 // Capture the deduced template arguments for each parameter pack expanded
2724 // by this pack expansion, add them to the list of arguments we've deduced
2725 // for that pack, then clear out the deduced argument.
2726 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
2727 DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
2728 if (!DeducedArg.isNull()) {
2729 NewlyDeducedPacks[I].push_back(DeducedArg);
2730 DeducedArg = DeducedTemplateArgument();
2731 }
2732 }
2733 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002734
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002735 // Build argument packs for each of the parameter packs expanded by this
2736 // pack expansion.
Douglas Gregor0216f812011-01-10 17:53:52 +00002737 if (Sema::TemplateDeductionResult Result
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002738 = FinishArgumentPackDeduction(*this, TemplateParams, HasAnyArguments,
Douglas Gregor0216f812011-01-10 17:53:52 +00002739 Deduced, PackIndices, SavedPacks,
2740 NewlyDeducedPacks, Info))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002741 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002742
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002743 // After we've matching against a parameter pack, we're done.
2744 break;
Douglas Gregore53060f2009-06-25 22:08:12 +00002745 }
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002746
Mike Stump1eb44332009-09-09 15:08:12 +00002747 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor02024a92010-03-28 02:42:43 +00002748 NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002749 Specialization, Info);
Douglas Gregore53060f2009-06-25 22:08:12 +00002750}
2751
Douglas Gregor83314aa2009-07-08 20:55:45 +00002752/// \brief Deduce template arguments when taking the address of a function
Douglas Gregor4b52e252009-12-21 23:17:24 +00002753/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
2754/// a template.
Douglas Gregor83314aa2009-07-08 20:55:45 +00002755///
2756/// \param FunctionTemplate the function template for which we are performing
2757/// template argument deduction.
2758///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002759/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor4b52e252009-12-21 23:17:24 +00002760/// arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00002761///
2762/// \param ArgFunctionType the function type that will be used as the
2763/// "argument" type (A) when performing template argument deduction from the
Douglas Gregor4b52e252009-12-21 23:17:24 +00002764/// function template's function type. This type may be NULL, if there is no
2765/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
Douglas Gregor83314aa2009-07-08 20:55:45 +00002766///
2767/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00002768/// this will be set to the function template specialization produced by
Douglas Gregor83314aa2009-07-08 20:55:45 +00002769/// template argument deduction.
2770///
2771/// \param Info the argument will be updated to provide additional information
2772/// about template argument deduction.
2773///
2774/// \returns the result of template argument deduction.
2775Sema::TemplateDeductionResult
2776Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor67714232011-03-03 02:41:12 +00002777 TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002778 QualType ArgFunctionType,
2779 FunctionDecl *&Specialization,
2780 TemplateDeductionInfo &Info) {
2781 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2782 TemplateParameterList *TemplateParams
2783 = FunctionTemplate->getTemplateParameters();
2784 QualType FunctionType = Function->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002785
Douglas Gregor83314aa2009-07-08 20:55:45 +00002786 // Substitute any explicit template arguments.
John McCall2a7fb272010-08-25 05:32:35 +00002787 LocalInstantiationScope InstScope(*this);
Douglas Gregor02024a92010-03-28 02:42:43 +00002788 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
2789 unsigned NumExplicitlySpecified = 0;
Douglas Gregor83314aa2009-07-08 20:55:45 +00002790 llvm::SmallVector<QualType, 4> ParamTypes;
John McCalld5532b62009-11-23 01:53:49 +00002791 if (ExplicitTemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002792 if (TemplateDeductionResult Result
2793 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00002794 *ExplicitTemplateArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00002795 Deduced, ParamTypes,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002796 &FunctionType, Info))
2797 return Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002798
2799 NumExplicitlySpecified = Deduced.size();
Douglas Gregor83314aa2009-07-08 20:55:45 +00002800 }
2801
2802 // Template argument deduction for function templates in a SFINAE context.
2803 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002804 SFINAETrap Trap(*this);
2805
John McCalleff92132010-02-02 02:21:27 +00002806 Deduced.resize(TemplateParams->size());
2807
Douglas Gregor4b52e252009-12-21 23:17:24 +00002808 if (!ArgFunctionType.isNull()) {
2809 // Deduce template arguments from the function type.
Douglas Gregor4b52e252009-12-21 23:17:24 +00002810 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002811 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor4b52e252009-12-21 23:17:24 +00002812 FunctionType, ArgFunctionType, Info,
Douglas Gregor73b3cf62011-01-25 17:19:08 +00002813 Deduced, TDF_TopLevelParameterTypeList))
Douglas Gregor4b52e252009-12-21 23:17:24 +00002814 return Result;
2815 }
Douglas Gregorfbb6fad2010-09-29 21:14:36 +00002816
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002817 if (TemplateDeductionResult Result
Douglas Gregorfbb6fad2010-09-29 21:14:36 +00002818 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
2819 NumExplicitlySpecified,
2820 Specialization, Info))
2821 return Result;
2822
2823 // If the requested function type does not match the actual type of the
2824 // specialization, template argument deduction fails.
2825 if (!ArgFunctionType.isNull() &&
2826 !Context.hasSameType(ArgFunctionType, Specialization->getType()))
2827 return TDK_NonDeducedMismatch;
2828
2829 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00002830}
2831
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002832/// \brief Deduce template arguments for a templated conversion
2833/// function (C++ [temp.deduct.conv]) and, if successful, produce a
2834/// conversion function template specialization.
2835Sema::TemplateDeductionResult
2836Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
2837 QualType ToType,
2838 CXXConversionDecl *&Specialization,
2839 TemplateDeductionInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00002840 CXXConversionDecl *Conv
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002841 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
2842 QualType FromType = Conv->getConversionType();
2843
2844 // Canonicalize the types for deduction.
2845 QualType P = Context.getCanonicalType(FromType);
2846 QualType A = Context.getCanonicalType(ToType);
2847
Douglas Gregor5453d932011-03-06 09:03:20 +00002848 // C++0x [temp.deduct.conv]p2:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002849 // If P is a reference type, the type referred to by P is used for
2850 // type deduction.
2851 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
2852 P = PRef->getPointeeType();
2853
Douglas Gregor5453d932011-03-06 09:03:20 +00002854 // C++0x [temp.deduct.conv]p4:
2855 // [...] If A is a reference type, the type referred to by A is used
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002856 // for type deduction.
2857 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
Douglas Gregor5453d932011-03-06 09:03:20 +00002858 A = ARef->getPointeeType().getUnqualifiedType();
2859 // C++ [temp.deduct.conv]p3:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002860 //
Mike Stump1eb44332009-09-09 15:08:12 +00002861 // If A is not a reference type:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002862 else {
2863 assert(!A->isReferenceType() && "Reference types were handled above");
2864
2865 // - If P is an array type, the pointer type produced by the
Mike Stump1eb44332009-09-09 15:08:12 +00002866 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002867 // of P for type deduction; otherwise,
2868 if (P->isArrayType())
2869 P = Context.getArrayDecayedType(P);
2870 // - If P is a function type, the pointer type produced by the
2871 // function-to-pointer standard conversion (4.3) is used in
2872 // place of P for type deduction; otherwise,
2873 else if (P->isFunctionType())
2874 P = Context.getPointerType(P);
2875 // - If P is a cv-qualified type, the top level cv-qualifiers of
NAKAMURA Takumi00995302011-01-27 07:09:49 +00002876 // P's type are ignored for type deduction.
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002877 else
2878 P = P.getUnqualifiedType();
2879
Douglas Gregor5453d932011-03-06 09:03:20 +00002880 // C++0x [temp.deduct.conv]p4:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00002881 // If A is a cv-qualified type, the top level cv-qualifiers of A's
Douglas Gregor5453d932011-03-06 09:03:20 +00002882 // type are ignored for type deduction. If A is a reference type, the type
2883 // referred to by A is used for type deduction.
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002884 A = A.getUnqualifiedType();
2885 }
2886
2887 // Template argument deduction for function templates in a SFINAE context.
2888 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002889 SFINAETrap Trap(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002890
2891 // C++ [temp.deduct.conv]p1:
2892 // Template argument deduction is done by comparing the return
2893 // type of the template conversion function (call it P) with the
2894 // type that is required as the result of the conversion (call it
2895 // A) as described in 14.8.2.4.
2896 TemplateParameterList *TemplateParams
2897 = FunctionTemplate->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00002898 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Mike Stump1eb44332009-09-09 15:08:12 +00002899 Deduced.resize(TemplateParams->size());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002900
2901 // C++0x [temp.deduct.conv]p4:
2902 // In general, the deduction process attempts to find template
2903 // argument values that will make the deduced A identical to
2904 // A. However, there are two cases that allow a difference:
2905 unsigned TDF = 0;
2906 // - If the original A is a reference type, A can be more
2907 // cv-qualified than the deduced A (i.e., the type referred to
2908 // by the reference)
2909 if (ToType->isReferenceType())
2910 TDF |= TDF_ParamWithReferenceType;
2911 // - The deduced A can be another pointer or pointer to member
NAKAMURA Takumi00995302011-01-27 07:09:49 +00002912 // type that can be converted to A via a qualification
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002913 // conversion.
2914 //
2915 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
2916 // both P and A are pointers or member pointers. In this case, we
2917 // just ignore cv-qualifiers completely).
2918 if ((P->isPointerType() && A->isPointerType()) ||
2919 (P->isMemberPointerType() && P->isMemberPointerType()))
2920 TDF |= TDF_IgnoreQualifiers;
2921 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002922 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002923 P, A, Info, Deduced, TDF))
2924 return Result;
2925
2926 // FIXME: we need to check that the deduced A is the same as A,
2927 // modulo the various allowed differences.
Mike Stump1eb44332009-09-09 15:08:12 +00002928
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002929 // Finish template argument deduction.
John McCall2a7fb272010-08-25 05:32:35 +00002930 LocalInstantiationScope InstScope(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002931 FunctionDecl *Spec = 0;
2932 TemplateDeductionResult Result
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002933 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec,
Douglas Gregor02024a92010-03-28 02:42:43 +00002934 Info);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002935 Specialization = cast_or_null<CXXConversionDecl>(Spec);
2936 return Result;
2937}
2938
Douglas Gregor4b52e252009-12-21 23:17:24 +00002939/// \brief Deduce template arguments for a function template when there is
2940/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
2941///
2942/// \param FunctionTemplate the function template for which we are performing
2943/// template argument deduction.
2944///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002945/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor4b52e252009-12-21 23:17:24 +00002946/// arguments.
2947///
2948/// \param Specialization if template argument deduction was successful,
2949/// this will be set to the function template specialization produced by
2950/// template argument deduction.
2951///
2952/// \param Info the argument will be updated to provide additional information
2953/// about template argument deduction.
2954///
2955/// \returns the result of template argument deduction.
2956Sema::TemplateDeductionResult
2957Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor67714232011-03-03 02:41:12 +00002958 TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor4b52e252009-12-21 23:17:24 +00002959 FunctionDecl *&Specialization,
2960 TemplateDeductionInfo &Info) {
2961 return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
2962 QualType(), Specialization, Info);
2963}
2964
Richard Smith34b41d92011-02-20 03:19:35 +00002965namespace {
2966 /// Substitute the 'auto' type specifier within a type for a given replacement
2967 /// type.
2968 class SubstituteAutoTransform :
2969 public TreeTransform<SubstituteAutoTransform> {
2970 QualType Replacement;
2971 public:
2972 SubstituteAutoTransform(Sema &SemaRef, QualType Replacement) :
2973 TreeTransform<SubstituteAutoTransform>(SemaRef), Replacement(Replacement) {
2974 }
2975 QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) {
2976 // If we're building the type pattern to deduce against, don't wrap the
2977 // substituted type in an AutoType. Certain template deduction rules
2978 // apply only when a template type parameter appears directly (and not if
2979 // the parameter is found through desugaring). For instance:
2980 // auto &&lref = lvalue;
2981 // must transform into "rvalue reference to T" not "rvalue reference to
2982 // auto type deduced as T" in order for [temp.deduct.call]p3 to apply.
2983 if (isa<TemplateTypeParmType>(Replacement)) {
2984 QualType Result = Replacement;
2985 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
2986 NewTL.setNameLoc(TL.getNameLoc());
2987 return Result;
2988 } else {
2989 QualType Result = RebuildAutoType(Replacement);
2990 AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result);
2991 NewTL.setNameLoc(TL.getNameLoc());
2992 return Result;
2993 }
2994 }
2995 };
2996}
2997
2998/// \brief Deduce the type for an auto type-specifier (C++0x [dcl.spec.auto]p6)
2999///
3000/// \param Type the type pattern using the auto type-specifier.
3001///
3002/// \param Init the initializer for the variable whose type is to be deduced.
3003///
3004/// \param Result if type deduction was successful, this will be set to the
3005/// deduced type. This may still contain undeduced autos if the type is
Richard Smitha085da82011-03-17 16:11:59 +00003006/// dependent. This will be set to null if deduction succeeded, but auto
3007/// substitution failed; the appropriate diagnostic will already have been
3008/// produced in that case.
Richard Smith34b41d92011-02-20 03:19:35 +00003009///
3010/// \returns true if deduction succeeded, false if it failed.
3011bool
Richard Smitha085da82011-03-17 16:11:59 +00003012Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *Init,
3013 TypeSourceInfo *&Result) {
Richard Smith34b41d92011-02-20 03:19:35 +00003014 if (Init->isTypeDependent()) {
3015 Result = Type;
3016 return true;
3017 }
3018
3019 SourceLocation Loc = Init->getExprLoc();
3020
3021 LocalInstantiationScope InstScope(*this);
3022
3023 // Build template<class TemplParam> void Func(FuncParam);
Richard Smith34b41d92011-02-20 03:19:35 +00003024 QualType TemplArg = Context.getTemplateTypeParmType(0, 0, false);
Abramo Bagnara344577e2011-03-06 15:48:19 +00003025 TemplateTypeParmDecl TemplParam(0, SourceLocation(), Loc, 0, false,
3026 TemplArg, false);
Richard Smith483b9f32011-02-21 20:05:19 +00003027 NamedDecl *TemplParamPtr = &TemplParam;
3028 FixedSizeTemplateParameterList<1> TemplateParams(Loc, Loc, &TemplParamPtr,
3029 Loc);
3030
Richard Smitha085da82011-03-17 16:11:59 +00003031 TypeSourceInfo *FuncParamInfo =
Richard Smith34b41d92011-02-20 03:19:35 +00003032 SubstituteAutoTransform(*this, TemplArg).TransformType(Type);
Richard Smitha085da82011-03-17 16:11:59 +00003033 assert(FuncParamInfo && "substituting template parameter for 'auto' failed");
3034 QualType FuncParam = FuncParamInfo->getType();
Richard Smith34b41d92011-02-20 03:19:35 +00003035
3036 // Deduce type of TemplParam in Func(Init)
3037 llvm::SmallVector<DeducedTemplateArgument, 1> Deduced;
3038 Deduced.resize(1);
3039 QualType InitType = Init->getType();
3040 unsigned TDF = 0;
Richard Smith483b9f32011-02-21 20:05:19 +00003041 if (AdjustFunctionParmAndArgTypesForDeduction(*this, &TemplateParams,
Richard Smith34b41d92011-02-20 03:19:35 +00003042 FuncParam, InitType, Init,
3043 TDF))
3044 return false;
3045
3046 TemplateDeductionInfo Info(Context, Loc);
Richard Smith483b9f32011-02-21 20:05:19 +00003047 if (::DeduceTemplateArguments(*this, &TemplateParams,
Richard Smith34b41d92011-02-20 03:19:35 +00003048 FuncParam, InitType, Info, Deduced,
3049 TDF))
3050 return false;
3051
3052 QualType DeducedType = Deduced[0].getAsType();
3053 if (DeducedType.isNull())
3054 return false;
3055
3056 Result = SubstituteAutoTransform(*this, DeducedType).TransformType(Type);
3057 return true;
3058}
3059
Douglas Gregor8a514912009-09-14 18:39:43 +00003060static void
Douglas Gregore73bb602009-09-14 21:25:05 +00003061MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
3062 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003063 unsigned Level,
Douglas Gregore73bb602009-09-14 21:25:05 +00003064 llvm::SmallVectorImpl<bool> &Deduced);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003065
3066/// \brief If this is a non-static member function,
Douglas Gregor77bc5722010-11-12 23:44:13 +00003067static void MaybeAddImplicitObjectParameterType(ASTContext &Context,
3068 CXXMethodDecl *Method,
3069 llvm::SmallVectorImpl<QualType> &ArgTypes) {
3070 if (Method->isStatic())
3071 return;
3072
3073 // C++ [over.match.funcs]p4:
3074 //
3075 // For non-static member functions, the type of the implicit
3076 // object parameter is
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003077 // - "lvalue reference to cv X" for functions declared without a
Douglas Gregor77bc5722010-11-12 23:44:13 +00003078 // ref-qualifier or with the & ref-qualifier
3079 // - "rvalue reference to cv X" for functions declared with the
3080 // && ref-qualifier
3081 //
3082 // FIXME: We don't have ref-qualifiers yet, so we don't do that part.
3083 QualType ArgTy = Context.getTypeDeclType(Method->getParent());
3084 ArgTy = Context.getQualifiedType(ArgTy,
3085 Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
3086 ArgTy = Context.getLValueReferenceType(ArgTy);
3087 ArgTypes.push_back(ArgTy);
3088}
3089
Douglas Gregor8a514912009-09-14 18:39:43 +00003090/// \brief Determine whether the function template \p FT1 is at least as
3091/// specialized as \p FT2.
3092static bool isAtLeastAsSpecializedAs(Sema &S,
John McCall5769d612010-02-08 23:07:23 +00003093 SourceLocation Loc,
Douglas Gregor8a514912009-09-14 18:39:43 +00003094 FunctionTemplateDecl *FT1,
3095 FunctionTemplateDecl *FT2,
3096 TemplatePartialOrderingContext TPOC,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003097 unsigned NumCallArguments,
Douglas Gregorb939a192011-01-21 17:29:42 +00003098 llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *RefParamComparisons) {
Douglas Gregor8a514912009-09-14 18:39:43 +00003099 FunctionDecl *FD1 = FT1->getTemplatedDecl();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003100 FunctionDecl *FD2 = FT2->getTemplatedDecl();
Douglas Gregor8a514912009-09-14 18:39:43 +00003101 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
3102 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003103
Douglas Gregor8a514912009-09-14 18:39:43 +00003104 assert(Proto1 && Proto2 && "Function templates must have prototypes");
3105 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00003106 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor8a514912009-09-14 18:39:43 +00003107 Deduced.resize(TemplateParams->size());
3108
3109 // C++0x [temp.deduct.partial]p3:
3110 // The types used to determine the ordering depend on the context in which
3111 // the partial ordering is done:
John McCall2a7fb272010-08-25 05:32:35 +00003112 TemplateDeductionInfo Info(S.Context, Loc);
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003113 CXXMethodDecl *Method1 = 0;
3114 CXXMethodDecl *Method2 = 0;
3115 bool IsNonStatic2 = false;
3116 bool IsNonStatic1 = false;
3117 unsigned Skip2 = 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00003118 switch (TPOC) {
3119 case TPOC_Call: {
3120 // - In the context of a function call, the function parameter types are
3121 // used.
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003122 Method1 = dyn_cast<CXXMethodDecl>(FD1);
3123 Method2 = dyn_cast<CXXMethodDecl>(FD2);
3124 IsNonStatic1 = Method1 && !Method1->isStatic();
3125 IsNonStatic2 = Method2 && !Method2->isStatic();
3126
3127 // C++0x [temp.func.order]p3:
3128 // [...] If only one of the function templates is a non-static
3129 // member, that function template is considered to have a new
3130 // first parameter inserted in its function parameter list. The
3131 // new parameter is of type "reference to cv A," where cv are
3132 // the cv-qualifiers of the function template (if any) and A is
3133 // the class of which the function template is a member.
3134 //
3135 // C++98/03 doesn't have this provision, so instead we drop the
3136 // first argument of the free function or static member, which
3137 // seems to match existing practice.
Douglas Gregor77bc5722010-11-12 23:44:13 +00003138 llvm::SmallVector<QualType, 4> Args1;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003139 unsigned Skip1 = !S.getLangOptions().CPlusPlus0x &&
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003140 IsNonStatic2 && !IsNonStatic1;
3141 if (S.getLangOptions().CPlusPlus0x && IsNonStatic1 && !IsNonStatic2)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003142 MaybeAddImplicitObjectParameterType(S.Context, Method1, Args1);
3143 Args1.insert(Args1.end(),
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003144 Proto1->arg_type_begin() + Skip1, Proto1->arg_type_end());
Douglas Gregor77bc5722010-11-12 23:44:13 +00003145
3146 llvm::SmallVector<QualType, 4> Args2;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003147 Skip2 = !S.getLangOptions().CPlusPlus0x &&
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003148 IsNonStatic1 && !IsNonStatic2;
3149 if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1)
Douglas Gregor77bc5722010-11-12 23:44:13 +00003150 MaybeAddImplicitObjectParameterType(S.Context, Method2, Args2);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003151 Args2.insert(Args2.end(),
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003152 Proto2->arg_type_begin() + Skip2, Proto2->arg_type_end());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003153
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003154 // C++ [temp.func.order]p5:
3155 // The presence of unused ellipsis and default arguments has no effect on
3156 // the partial ordering of function templates.
3157 if (Args1.size() > NumCallArguments)
3158 Args1.resize(NumCallArguments);
3159 if (Args2.size() > NumCallArguments)
3160 Args2.resize(NumCallArguments);
3161 if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(),
3162 Args1.data(), Args1.size(), Info, Deduced,
3163 TDF_None, /*PartialOrdering=*/true,
Douglas Gregorb939a192011-01-21 17:29:42 +00003164 RefParamComparisons))
Douglas Gregor8a514912009-09-14 18:39:43 +00003165 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003166
Douglas Gregor8a514912009-09-14 18:39:43 +00003167 break;
3168 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003169
Douglas Gregor8a514912009-09-14 18:39:43 +00003170 case TPOC_Conversion:
3171 // - In the context of a call to a conversion operator, the return types
3172 // of the conversion function templates are used.
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003173 if (DeduceTemplateArguments(S, TemplateParams, Proto2->getResultType(),
3174 Proto1->getResultType(), Info, Deduced,
3175 TDF_None, /*PartialOrdering=*/true,
Douglas Gregorb939a192011-01-21 17:29:42 +00003176 RefParamComparisons))
Douglas Gregor8a514912009-09-14 18:39:43 +00003177 return false;
3178 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003179
Douglas Gregor8a514912009-09-14 18:39:43 +00003180 case TPOC_Other:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003181 // - In other contexts (14.6.6.2) the function template's function type
Douglas Gregor8a514912009-09-14 18:39:43 +00003182 // is used.
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003183 // FIXME: Don't we actually want to perform the adjustments on the parameter
3184 // types?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003185 if (DeduceTemplateArguments(S, TemplateParams, FD2->getType(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003186 FD1->getType(), Info, Deduced, TDF_None,
Douglas Gregorb939a192011-01-21 17:29:42 +00003187 /*PartialOrdering=*/true, RefParamComparisons))
Douglas Gregor8a514912009-09-14 18:39:43 +00003188 return false;
3189 break;
3190 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003191
Douglas Gregor8a514912009-09-14 18:39:43 +00003192 // C++0x [temp.deduct.partial]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003193 // In most cases, all template parameters must have values in order for
3194 // deduction to succeed, but for partial ordering purposes a template
3195 // parameter may remain without a value provided it is not used in the
Douglas Gregor8a514912009-09-14 18:39:43 +00003196 // types being used for partial ordering. [ Note: a template parameter used
3197 // in a non-deduced context is considered used. -end note]
3198 unsigned ArgIdx = 0, NumArgs = Deduced.size();
3199 for (; ArgIdx != NumArgs; ++ArgIdx)
3200 if (Deduced[ArgIdx].isNull())
3201 break;
3202
3203 if (ArgIdx == NumArgs) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003204 // All template arguments were deduced. FT1 is at least as specialized
Douglas Gregor8a514912009-09-14 18:39:43 +00003205 // as FT2.
3206 return true;
3207 }
3208
Douglas Gregore73bb602009-09-14 21:25:05 +00003209 // Figure out which template parameters were used.
Douglas Gregor8a514912009-09-14 18:39:43 +00003210 llvm::SmallVector<bool, 4> UsedParameters;
3211 UsedParameters.resize(TemplateParams->size());
3212 switch (TPOC) {
3213 case TPOC_Call: {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003214 unsigned NumParams = std::min(NumCallArguments,
3215 std::min(Proto1->getNumArgs(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003216 Proto2->getNumArgs()));
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003217 if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003218 ::MarkUsedTemplateParameters(S, Method2->getThisType(S.Context), false,
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003219 TemplateParams->getDepth(), UsedParameters);
3220 for (unsigned I = Skip2; I < NumParams; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003221 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003222 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00003223 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00003224 break;
3225 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003226
Douglas Gregor8a514912009-09-14 18:39:43 +00003227 case TPOC_Conversion:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003228 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003229 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00003230 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00003231 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003232
Douglas Gregor8a514912009-09-14 18:39:43 +00003233 case TPOC_Other:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003234 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003235 TemplateParams->getDepth(),
3236 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00003237 break;
3238 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003239
Douglas Gregor8a514912009-09-14 18:39:43 +00003240 for (; ArgIdx != NumArgs; ++ArgIdx)
3241 // If this argument had no value deduced but was used in one of the types
3242 // used for partial ordering, then deduction fails.
3243 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
3244 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003245
Douglas Gregor8a514912009-09-14 18:39:43 +00003246 return true;
3247}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003248
Douglas Gregor9da95e62011-01-16 16:03:23 +00003249/// \brief Determine whether this a function template whose parameter-type-list
3250/// ends with a function parameter pack.
3251static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) {
3252 FunctionDecl *Function = FunTmpl->getTemplatedDecl();
3253 unsigned NumParams = Function->getNumParams();
3254 if (NumParams == 0)
3255 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003256
Douglas Gregor9da95e62011-01-16 16:03:23 +00003257 ParmVarDecl *Last = Function->getParamDecl(NumParams - 1);
3258 if (!Last->isParameterPack())
3259 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003260
Douglas Gregor9da95e62011-01-16 16:03:23 +00003261 // Make sure that no previous parameter is a parameter pack.
3262 while (--NumParams > 0) {
3263 if (Function->getParamDecl(NumParams - 1)->isParameterPack())
3264 return false;
3265 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003266
Douglas Gregor9da95e62011-01-16 16:03:23 +00003267 return true;
3268}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003269
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003270/// \brief Returns the more specialized function template according
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003271/// to the rules of function template partial ordering (C++ [temp.func.order]).
3272///
3273/// \param FT1 the first function template
3274///
3275/// \param FT2 the second function template
3276///
Douglas Gregor8a514912009-09-14 18:39:43 +00003277/// \param TPOC the context in which we are performing partial ordering of
3278/// function templates.
Mike Stump1eb44332009-09-09 15:08:12 +00003279///
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003280/// \param NumCallArguments The number of arguments in a call, used only
3281/// when \c TPOC is \c TPOC_Call.
3282///
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003283/// \returns the more specialized function template. If neither
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003284/// template is more specialized, returns NULL.
3285FunctionTemplateDecl *
3286Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
3287 FunctionTemplateDecl *FT2,
John McCall5769d612010-02-08 23:07:23 +00003288 SourceLocation Loc,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003289 TemplatePartialOrderingContext TPOC,
3290 unsigned NumCallArguments) {
Douglas Gregorb939a192011-01-21 17:29:42 +00003291 llvm::SmallVector<RefParamPartialOrderingComparison, 4> RefParamComparisons;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003292 bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003293 NumCallArguments, 0);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003294 bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003295 NumCallArguments,
Douglas Gregorb939a192011-01-21 17:29:42 +00003296 &RefParamComparisons);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003297
Douglas Gregor8a514912009-09-14 18:39:43 +00003298 if (Better1 != Better2) // We have a clear winner
3299 return Better1? FT1 : FT2;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003300
Douglas Gregor8a514912009-09-14 18:39:43 +00003301 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003302 return 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00003303
Douglas Gregor8a514912009-09-14 18:39:43 +00003304 // C++0x [temp.deduct.partial]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003305 // If for each type being considered a given template is at least as
Douglas Gregor8a514912009-09-14 18:39:43 +00003306 // specialized for all types and more specialized for some set of types and
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003307 // the other template is not more specialized for any types or is not at
Douglas Gregor8a514912009-09-14 18:39:43 +00003308 // least as specialized for any types, then the given template is more
3309 // specialized than the other template. Otherwise, neither template is more
3310 // specialized than the other.
3311 Better1 = false;
3312 Better2 = false;
Douglas Gregorb939a192011-01-21 17:29:42 +00003313 for (unsigned I = 0, N = RefParamComparisons.size(); I != N; ++I) {
Douglas Gregor8a514912009-09-14 18:39:43 +00003314 // C++0x [temp.deduct.partial]p9:
3315 // If, for a given type, deduction succeeds in both directions (i.e., the
Douglas Gregorb939a192011-01-21 17:29:42 +00003316 // types are identical after the transformations above) and both P and A
3317 // were reference types (before being replaced with the type referred to
3318 // above):
3319
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003320 // -- if the type from the argument template was an lvalue reference
Douglas Gregorb939a192011-01-21 17:29:42 +00003321 // and the type from the parameter template was not, the argument
3322 // type is considered to be more specialized than the other;
3323 // otherwise,
3324 if (!RefParamComparisons[I].ArgIsRvalueRef &&
3325 RefParamComparisons[I].ParamIsRvalueRef) {
3326 Better2 = true;
3327 if (Better1)
3328 return 0;
3329 continue;
3330 } else if (!RefParamComparisons[I].ParamIsRvalueRef &&
3331 RefParamComparisons[I].ArgIsRvalueRef) {
3332 Better1 = true;
3333 if (Better2)
3334 return 0;
3335 continue;
Douglas Gregor8a514912009-09-14 18:39:43 +00003336 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003337
Douglas Gregorb939a192011-01-21 17:29:42 +00003338 // -- if the type from the argument template is more cv-qualified than
3339 // the type from the parameter template (as described above), the
3340 // argument type is considered to be more specialized than the
3341 // other; otherwise,
3342 switch (RefParamComparisons[I].Qualifiers) {
3343 case NeitherMoreQualified:
3344 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003345
Douglas Gregorb939a192011-01-21 17:29:42 +00003346 case ParamMoreQualified:
3347 Better1 = true;
3348 if (Better2)
3349 return 0;
3350 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003351
Douglas Gregorb939a192011-01-21 17:29:42 +00003352 case ArgMoreQualified:
3353 Better2 = true;
3354 if (Better1)
3355 return 0;
3356 continue;
3357 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003358
Douglas Gregorb939a192011-01-21 17:29:42 +00003359 // -- neither type is more specialized than the other.
Douglas Gregor8a514912009-09-14 18:39:43 +00003360 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003361
Douglas Gregor8a514912009-09-14 18:39:43 +00003362 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003363 if (Better1)
3364 return FT1;
Douglas Gregor8a514912009-09-14 18:39:43 +00003365 else if (Better2)
3366 return FT2;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003367
Douglas Gregor9da95e62011-01-16 16:03:23 +00003368 // FIXME: This mimics what GCC implements, but doesn't match up with the
3369 // proposed resolution for core issue 692. This area needs to be sorted out,
3370 // but for now we attempt to maintain compatibility.
3371 bool Variadic1 = isVariadicFunctionTemplate(FT1);
3372 bool Variadic2 = isVariadicFunctionTemplate(FT2);
3373 if (Variadic1 != Variadic2)
3374 return Variadic1? FT2 : FT1;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003375
Douglas Gregor9da95e62011-01-16 16:03:23 +00003376 return 0;
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003377}
Douglas Gregor83314aa2009-07-08 20:55:45 +00003378
Douglas Gregord5a423b2009-09-25 18:43:00 +00003379/// \brief Determine if the two templates are equivalent.
3380static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
3381 if (T1 == T2)
3382 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003383
Douglas Gregord5a423b2009-09-25 18:43:00 +00003384 if (!T1 || !T2)
3385 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003386
Douglas Gregord5a423b2009-09-25 18:43:00 +00003387 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
3388}
3389
3390/// \brief Retrieve the most specialized of the given function template
3391/// specializations.
3392///
John McCallc373d482010-01-27 01:50:18 +00003393/// \param SpecBegin the start iterator of the function template
3394/// specializations that we will be comparing.
Douglas Gregord5a423b2009-09-25 18:43:00 +00003395///
John McCallc373d482010-01-27 01:50:18 +00003396/// \param SpecEnd the end iterator of the function template
3397/// specializations, paired with \p SpecBegin.
Douglas Gregord5a423b2009-09-25 18:43:00 +00003398///
3399/// \param TPOC the partial ordering context to use to compare the function
3400/// template specializations.
3401///
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003402/// \param NumCallArguments The number of arguments in a call, used only
3403/// when \c TPOC is \c TPOC_Call.
3404///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003405/// \param Loc the location where the ambiguity or no-specializations
Douglas Gregord5a423b2009-09-25 18:43:00 +00003406/// diagnostic should occur.
3407///
3408/// \param NoneDiag partial diagnostic used to diagnose cases where there are
3409/// no matching candidates.
3410///
3411/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
3412/// occurs.
3413///
3414/// \param CandidateDiag partial diagnostic used for each function template
3415/// specialization that is a candidate in the ambiguous ordering. One parameter
3416/// in this diagnostic should be unbound, which will correspond to the string
3417/// describing the template arguments for the function template specialization.
3418///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003419/// \param Index if non-NULL and the result of this function is non-nULL,
Douglas Gregord5a423b2009-09-25 18:43:00 +00003420/// receives the index corresponding to the resulting function template
3421/// specialization.
3422///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003423/// \returns the most specialized function template specialization, if
John McCallc373d482010-01-27 01:50:18 +00003424/// found. Otherwise, returns SpecEnd.
Douglas Gregord5a423b2009-09-25 18:43:00 +00003425///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003426/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
Douglas Gregord5a423b2009-09-25 18:43:00 +00003427/// template argument deduction.
John McCallc373d482010-01-27 01:50:18 +00003428UnresolvedSetIterator
3429Sema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003430 UnresolvedSetIterator SpecEnd,
John McCallc373d482010-01-27 01:50:18 +00003431 TemplatePartialOrderingContext TPOC,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003432 unsigned NumCallArguments,
John McCallc373d482010-01-27 01:50:18 +00003433 SourceLocation Loc,
3434 const PartialDiagnostic &NoneDiag,
3435 const PartialDiagnostic &AmbigDiag,
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003436 const PartialDiagnostic &CandidateDiag,
3437 bool Complain) {
John McCallc373d482010-01-27 01:50:18 +00003438 if (SpecBegin == SpecEnd) {
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003439 if (Complain)
3440 Diag(Loc, NoneDiag);
John McCallc373d482010-01-27 01:50:18 +00003441 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00003442 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003443
3444 if (SpecBegin + 1 == SpecEnd)
John McCallc373d482010-01-27 01:50:18 +00003445 return SpecBegin;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003446
Douglas Gregord5a423b2009-09-25 18:43:00 +00003447 // Find the function template that is better than all of the templates it
3448 // has been compared to.
John McCallc373d482010-01-27 01:50:18 +00003449 UnresolvedSetIterator Best = SpecBegin;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003450 FunctionTemplateDecl *BestTemplate
John McCallc373d482010-01-27 01:50:18 +00003451 = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00003452 assert(BestTemplate && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00003453 for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
3454 FunctionTemplateDecl *Challenger
3455 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00003456 assert(Challenger && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00003457 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003458 Loc, TPOC, NumCallArguments),
Douglas Gregord5a423b2009-09-25 18:43:00 +00003459 Challenger)) {
3460 Best = I;
3461 BestTemplate = Challenger;
3462 }
3463 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003464
Douglas Gregord5a423b2009-09-25 18:43:00 +00003465 // Make sure that the "best" function template is more specialized than all
3466 // of the others.
3467 bool Ambiguous = false;
John McCallc373d482010-01-27 01:50:18 +00003468 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
3469 FunctionTemplateDecl *Challenger
3470 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00003471 if (I != Best &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003472 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003473 Loc, TPOC, NumCallArguments),
Douglas Gregord5a423b2009-09-25 18:43:00 +00003474 BestTemplate)) {
3475 Ambiguous = true;
3476 break;
3477 }
3478 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003479
Douglas Gregord5a423b2009-09-25 18:43:00 +00003480 if (!Ambiguous) {
3481 // We found an answer. Return it.
John McCallc373d482010-01-27 01:50:18 +00003482 return Best;
Douglas Gregord5a423b2009-09-25 18:43:00 +00003483 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003484
Douglas Gregord5a423b2009-09-25 18:43:00 +00003485 // Diagnose the ambiguity.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003486 if (Complain)
3487 Diag(Loc, AmbigDiag);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003488
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003489 if (Complain)
Douglas Gregord5a423b2009-09-25 18:43:00 +00003490 // FIXME: Can we order the candidates in some sane way?
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003491 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
3492 Diag((*I)->getLocation(), CandidateDiag)
3493 << getTemplateArgumentBindingsText(
3494 cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
John McCallc373d482010-01-27 01:50:18 +00003495 *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003496
John McCallc373d482010-01-27 01:50:18 +00003497 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00003498}
3499
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003500/// \brief Returns the more specialized class template partial specialization
3501/// according to the rules of partial ordering of class template partial
3502/// specializations (C++ [temp.class.order]).
3503///
3504/// \param PS1 the first class template partial specialization
3505///
3506/// \param PS2 the second class template partial specialization
3507///
3508/// \returns the more specialized class template partial specialization. If
3509/// neither partial specialization is more specialized, returns NULL.
3510ClassTemplatePartialSpecializationDecl *
3511Sema::getMoreSpecializedPartialSpecialization(
3512 ClassTemplatePartialSpecializationDecl *PS1,
John McCall5769d612010-02-08 23:07:23 +00003513 ClassTemplatePartialSpecializationDecl *PS2,
3514 SourceLocation Loc) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003515 // C++ [temp.class.order]p1:
3516 // For two class template partial specializations, the first is at least as
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003517 // specialized as the second if, given the following rewrite to two
3518 // function templates, the first function template is at least as
3519 // specialized as the second according to the ordering rules for function
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003520 // templates (14.6.6.2):
3521 // - the first function template has the same template parameters as the
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003522 // first partial specialization and has a single function parameter
3523 // whose type is a class template specialization with the template
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003524 // arguments of the first partial specialization, and
3525 // - the second function template has the same template parameters as the
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003526 // second partial specialization and has a single function parameter
3527 // whose type is a class template specialization with the template
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003528 // arguments of the second partial specialization.
3529 //
Douglas Gregor31dce8f2010-04-29 06:21:43 +00003530 // Rather than synthesize function templates, we merely perform the
3531 // equivalent partial ordering by performing deduction directly on
3532 // the template arguments of the class template partial
3533 // specializations. This computation is slightly simpler than the
3534 // general problem of function template partial ordering, because
3535 // class template partial specializations are more constrained. We
3536 // know that every template parameter is deducible from the class
3537 // template partial specialization's template arguments, for
3538 // example.
Douglas Gregor02024a92010-03-28 02:42:43 +00003539 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
John McCall2a7fb272010-08-25 05:32:35 +00003540 TemplateDeductionInfo Info(Context, Loc);
John McCall31f17ec2010-04-27 00:57:59 +00003541
3542 QualType PT1 = PS1->getInjectedSpecializationType();
3543 QualType PT2 = PS2->getInjectedSpecializationType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003544
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003545 // Determine whether PS1 is at least as specialized as PS2
3546 Deduced.resize(PS2->getTemplateParameters()->size());
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003547 bool Better1 = !::DeduceTemplateArguments(*this, PS2->getTemplateParameters(),
3548 PT2, PT1, Info, Deduced, TDF_None,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003549 /*PartialOrdering=*/true,
Douglas Gregorb939a192011-01-21 17:29:42 +00003550 /*RefParamComparisons=*/0);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00003551 if (Better1) {
3552 InstantiatingTemplate Inst(*this, PS2->getLocation(), PS2,
3553 Deduced.data(), Deduced.size(), Info);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003554 Better1 = !::FinishTemplateArgumentDeduction(*this, PS2,
3555 PS1->getTemplateArgs(),
Douglas Gregor516e6e02010-04-29 06:31:36 +00003556 Deduced, Info);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00003557 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003558
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003559 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregordb0d4b72009-11-11 23:06:43 +00003560 Deduced.clear();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003561 Deduced.resize(PS1->getTemplateParameters()->size());
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003562 bool Better2 = !::DeduceTemplateArguments(*this, PS1->getTemplateParameters(),
3563 PT1, PT2, Info, Deduced, TDF_None,
3564 /*PartialOrdering=*/true,
Douglas Gregorb939a192011-01-21 17:29:42 +00003565 /*RefParamComparisons=*/0);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00003566 if (Better2) {
3567 InstantiatingTemplate Inst(*this, PS1->getLocation(), PS1,
3568 Deduced.data(), Deduced.size(), Info);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003569 Better2 = !::FinishTemplateArgumentDeduction(*this, PS1,
3570 PS2->getTemplateArgs(),
Douglas Gregor516e6e02010-04-29 06:31:36 +00003571 Deduced, Info);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00003572 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003573
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003574 if (Better1 == Better2)
3575 return 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003576
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003577 return Better1? PS1 : PS2;
3578}
3579
Mike Stump1eb44332009-09-09 15:08:12 +00003580static void
Douglas Gregore73bb602009-09-14 21:25:05 +00003581MarkUsedTemplateParameters(Sema &SemaRef,
3582 const TemplateArgument &TemplateArg,
3583 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003584 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003585 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003586
Douglas Gregore73bb602009-09-14 21:25:05 +00003587/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00003588/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00003589static void
Douglas Gregore73bb602009-09-14 21:25:05 +00003590MarkUsedTemplateParameters(Sema &SemaRef,
3591 const Expr *E,
3592 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003593 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003594 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregorbe230c32011-01-03 17:17:50 +00003595 // We can deduce from a pack expansion.
3596 if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E))
3597 E = Expansion->getPattern();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003598
Douglas Gregor54c53cc2011-01-04 23:35:54 +00003599 // Skip through any implicit casts we added while type-checking.
3600 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
3601 E = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003602
3603 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
Douglas Gregore73bb602009-09-14 21:25:05 +00003604 // find other occurrences of template parameters.
Douglas Gregorf6ddb732009-06-18 18:45:36 +00003605 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregorc781f9c2010-01-14 18:13:22 +00003606 if (!DRE)
Douglas Gregor031a5882009-06-13 00:26:55 +00003607 return;
3608
Mike Stump1eb44332009-09-09 15:08:12 +00003609 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00003610 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
3611 if (!NTTP)
3612 return;
3613
Douglas Gregored9c0f92009-10-29 00:04:11 +00003614 if (NTTP->getDepth() == Depth)
3615 Used[NTTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00003616}
3617
Douglas Gregore73bb602009-09-14 21:25:05 +00003618/// \brief Mark the template parameters that are used by the given
3619/// nested name specifier.
3620static void
3621MarkUsedTemplateParameters(Sema &SemaRef,
3622 NestedNameSpecifier *NNS,
3623 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003624 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003625 llvm::SmallVectorImpl<bool> &Used) {
3626 if (!NNS)
3627 return;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003628
Douglas Gregored9c0f92009-10-29 00:04:11 +00003629 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
3630 Used);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003631 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003632 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003633}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003634
Douglas Gregore73bb602009-09-14 21:25:05 +00003635/// \brief Mark the template parameters that are used by the given
3636/// template name.
3637static void
3638MarkUsedTemplateParameters(Sema &SemaRef,
3639 TemplateName Name,
3640 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003641 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003642 llvm::SmallVectorImpl<bool> &Used) {
3643 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
3644 if (TemplateTemplateParmDecl *TTP
Douglas Gregored9c0f92009-10-29 00:04:11 +00003645 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
3646 if (TTP->getDepth() == Depth)
3647 Used[TTP->getIndex()] = true;
3648 }
Douglas Gregore73bb602009-09-14 21:25:05 +00003649 return;
3650 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003651
Douglas Gregor788cd062009-11-11 01:00:40 +00003652 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003653 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
Douglas Gregor788cd062009-11-11 01:00:40 +00003654 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003655 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003656 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003657 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003658}
3659
3660/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00003661/// type.
Mike Stump1eb44332009-09-09 15:08:12 +00003662static void
Douglas Gregore73bb602009-09-14 21:25:05 +00003663MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
3664 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003665 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003666 llvm::SmallVectorImpl<bool> &Used) {
3667 if (T.isNull())
3668 return;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003669
Douglas Gregor031a5882009-06-13 00:26:55 +00003670 // Non-dependent types have nothing deducible
3671 if (!T->isDependentType())
3672 return;
3673
3674 T = SemaRef.Context.getCanonicalType(T);
3675 switch (T->getTypeClass()) {
Douglas Gregor031a5882009-06-13 00:26:55 +00003676 case Type::Pointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00003677 MarkUsedTemplateParameters(SemaRef,
3678 cast<PointerType>(T)->getPointeeType(),
3679 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003680 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003681 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003682 break;
3683
3684 case Type::BlockPointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00003685 MarkUsedTemplateParameters(SemaRef,
3686 cast<BlockPointerType>(T)->getPointeeType(),
3687 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003688 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003689 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003690 break;
3691
3692 case Type::LValueReference:
3693 case Type::RValueReference:
Douglas Gregore73bb602009-09-14 21:25:05 +00003694 MarkUsedTemplateParameters(SemaRef,
3695 cast<ReferenceType>(T)->getPointeeType(),
3696 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003697 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003698 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003699 break;
3700
3701 case Type::MemberPointer: {
3702 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore73bb602009-09-14 21:25:05 +00003703 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003704 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003705 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003706 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003707 break;
3708 }
3709
3710 case Type::DependentSizedArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00003711 MarkUsedTemplateParameters(SemaRef,
3712 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003713 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003714 // Fall through to check the element type
3715
3716 case Type::ConstantArray:
3717 case Type::IncompleteArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00003718 MarkUsedTemplateParameters(SemaRef,
3719 cast<ArrayType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003720 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003721 break;
3722
3723 case Type::Vector:
3724 case Type::ExtVector:
Douglas Gregore73bb602009-09-14 21:25:05 +00003725 MarkUsedTemplateParameters(SemaRef,
3726 cast<VectorType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003727 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003728 break;
3729
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00003730 case Type::DependentSizedExtVector: {
3731 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00003732 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00003733 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003734 Depth, Used);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003735 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003736 Depth, Used);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00003737 break;
3738 }
3739
Douglas Gregor031a5882009-06-13 00:26:55 +00003740 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00003741 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00003742 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003743 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003744 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore73bb602009-09-14 21:25:05 +00003745 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003746 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003747 break;
3748 }
3749
Douglas Gregored9c0f92009-10-29 00:04:11 +00003750 case Type::TemplateTypeParm: {
3751 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
3752 if (TTP->getDepth() == Depth)
3753 Used[TTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00003754 break;
Douglas Gregored9c0f92009-10-29 00:04:11 +00003755 }
Douglas Gregor031a5882009-06-13 00:26:55 +00003756
Douglas Gregor0bc15d92011-01-14 05:11:40 +00003757 case Type::SubstTemplateTypeParmPack: {
3758 const SubstTemplateTypeParmPackType *Subst
3759 = cast<SubstTemplateTypeParmPackType>(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003760 MarkUsedTemplateParameters(SemaRef,
Douglas Gregor0bc15d92011-01-14 05:11:40 +00003761 QualType(Subst->getReplacedParameter(), 0),
3762 OnlyDeduced, Depth, Used);
3763 MarkUsedTemplateParameters(SemaRef, Subst->getArgumentPack(),
3764 OnlyDeduced, Depth, Used);
3765 break;
3766 }
3767
John McCall31f17ec2010-04-27 00:57:59 +00003768 case Type::InjectedClassName:
3769 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
3770 // fall through
3771
Douglas Gregor031a5882009-06-13 00:26:55 +00003772 case Type::TemplateSpecialization: {
Mike Stump1eb44332009-09-09 15:08:12 +00003773 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00003774 = cast<TemplateSpecializationType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00003775 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003776 Depth, Used);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003777
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003778 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003779 // If the template argument list of P contains a pack expansion that is not
3780 // the last template argument, the entire template argument list is a
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003781 // non-deduced context.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003782 if (OnlyDeduced &&
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003783 hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs()))
3784 break;
3785
Douglas Gregore73bb602009-09-14 21:25:05 +00003786 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00003787 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
3788 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003789 break;
3790 }
3791
Douglas Gregore73bb602009-09-14 21:25:05 +00003792 case Type::Complex:
3793 if (!OnlyDeduced)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003794 MarkUsedTemplateParameters(SemaRef,
Douglas Gregore73bb602009-09-14 21:25:05 +00003795 cast<ComplexType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003796 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003797 break;
3798
Douglas Gregor4714c122010-03-31 17:34:00 +00003799 case Type::DependentName:
Douglas Gregore73bb602009-09-14 21:25:05 +00003800 if (!OnlyDeduced)
3801 MarkUsedTemplateParameters(SemaRef,
Douglas Gregor4714c122010-03-31 17:34:00 +00003802 cast<DependentNameType>(T)->getQualifier(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003803 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003804 break;
3805
John McCall33500952010-06-11 00:33:02 +00003806 case Type::DependentTemplateSpecialization: {
3807 const DependentTemplateSpecializationType *Spec
3808 = cast<DependentTemplateSpecializationType>(T);
3809 if (!OnlyDeduced)
3810 MarkUsedTemplateParameters(SemaRef, Spec->getQualifier(),
3811 OnlyDeduced, Depth, Used);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003812
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003813 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003814 // If the template argument list of P contains a pack expansion that is not
3815 // the last template argument, the entire template argument list is a
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003816 // non-deduced context.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003817 if (OnlyDeduced &&
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003818 hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs()))
3819 break;
3820
John McCall33500952010-06-11 00:33:02 +00003821 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
3822 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
3823 Used);
3824 break;
3825 }
3826
John McCallad5e7382010-03-01 23:49:17 +00003827 case Type::TypeOf:
3828 if (!OnlyDeduced)
3829 MarkUsedTemplateParameters(SemaRef,
3830 cast<TypeOfType>(T)->getUnderlyingType(),
3831 OnlyDeduced, Depth, Used);
3832 break;
3833
3834 case Type::TypeOfExpr:
3835 if (!OnlyDeduced)
3836 MarkUsedTemplateParameters(SemaRef,
3837 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
3838 OnlyDeduced, Depth, Used);
3839 break;
3840
3841 case Type::Decltype:
3842 if (!OnlyDeduced)
3843 MarkUsedTemplateParameters(SemaRef,
3844 cast<DecltypeType>(T)->getUnderlyingExpr(),
3845 OnlyDeduced, Depth, Used);
3846 break;
3847
Douglas Gregor7536dd52010-12-20 02:24:11 +00003848 case Type::PackExpansion:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003849 MarkUsedTemplateParameters(SemaRef,
Douglas Gregor7536dd52010-12-20 02:24:11 +00003850 cast<PackExpansionType>(T)->getPattern(),
3851 OnlyDeduced, Depth, Used);
3852 break;
3853
Richard Smith34b41d92011-02-20 03:19:35 +00003854 case Type::Auto:
3855 MarkUsedTemplateParameters(SemaRef,
3856 cast<AutoType>(T)->getDeducedType(),
3857 OnlyDeduced, Depth, Used);
3858
Douglas Gregore73bb602009-09-14 21:25:05 +00003859 // None of these types have any template parameters in them.
Douglas Gregor031a5882009-06-13 00:26:55 +00003860 case Type::Builtin:
Douglas Gregor031a5882009-06-13 00:26:55 +00003861 case Type::VariableArray:
3862 case Type::FunctionNoProto:
3863 case Type::Record:
3864 case Type::Enum:
Douglas Gregor031a5882009-06-13 00:26:55 +00003865 case Type::ObjCInterface:
John McCallc12c5bb2010-05-15 11:32:37 +00003866 case Type::ObjCObject:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00003867 case Type::ObjCObjectPointer:
John McCalled976492009-12-04 22:46:56 +00003868 case Type::UnresolvedUsing:
Douglas Gregor031a5882009-06-13 00:26:55 +00003869#define TYPE(Class, Base)
3870#define ABSTRACT_TYPE(Class, Base)
3871#define DEPENDENT_TYPE(Class, Base)
3872#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3873#include "clang/AST/TypeNodes.def"
3874 break;
3875 }
3876}
3877
Douglas Gregore73bb602009-09-14 21:25:05 +00003878/// \brief Mark the template parameters that are used by this
Douglas Gregor031a5882009-06-13 00:26:55 +00003879/// template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00003880static void
Douglas Gregore73bb602009-09-14 21:25:05 +00003881MarkUsedTemplateParameters(Sema &SemaRef,
3882 const TemplateArgument &TemplateArg,
3883 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003884 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003885 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00003886 switch (TemplateArg.getKind()) {
3887 case TemplateArgument::Null:
3888 case TemplateArgument::Integral:
Douglas Gregor788cd062009-11-11 01:00:40 +00003889 case TemplateArgument::Declaration:
Douglas Gregor031a5882009-06-13 00:26:55 +00003890 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003891
Douglas Gregor031a5882009-06-13 00:26:55 +00003892 case TemplateArgument::Type:
Douglas Gregore73bb602009-09-14 21:25:05 +00003893 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003894 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003895 break;
3896
Douglas Gregor788cd062009-11-11 01:00:40 +00003897 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00003898 case TemplateArgument::TemplateExpansion:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003899 MarkUsedTemplateParameters(SemaRef,
3900 TemplateArg.getAsTemplateOrTemplatePattern(),
Douglas Gregor788cd062009-11-11 01:00:40 +00003901 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003902 break;
3903
3904 case TemplateArgument::Expression:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003905 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003906 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003907 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003908
Anders Carlssond01b1da2009-06-15 17:04:53 +00003909 case TemplateArgument::Pack:
Douglas Gregore73bb602009-09-14 21:25:05 +00003910 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
3911 PEnd = TemplateArg.pack_end();
3912 P != PEnd; ++P)
Douglas Gregored9c0f92009-10-29 00:04:11 +00003913 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssond01b1da2009-06-15 17:04:53 +00003914 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00003915 }
3916}
3917
3918/// \brief Mark the template parameters can be deduced by the given
3919/// template argument list.
3920///
3921/// \param TemplateArgs the template argument list from which template
3922/// parameters will be deduced.
3923///
3924/// \param Deduced a bit vector whose elements will be set to \c true
3925/// to indicate when the corresponding template parameter will be
3926/// deduced.
Mike Stump1eb44332009-09-09 15:08:12 +00003927void
Douglas Gregore73bb602009-09-14 21:25:05 +00003928Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003929 bool OnlyDeduced, unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003930 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003931 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003932 // If the template argument list of P contains a pack expansion that is not
3933 // the last template argument, the entire template argument list is a
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003934 // non-deduced context.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003935 if (OnlyDeduced &&
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003936 hasPackExpansionBeforeEnd(TemplateArgs.data(), TemplateArgs.size()))
3937 return;
3938
Douglas Gregor031a5882009-06-13 00:26:55 +00003939 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003940 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003941 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003942}
Douglas Gregor63f07c52009-09-18 23:21:38 +00003943
3944/// \brief Marks all of the template parameters that will be deduced by a
3945/// call to the given function template.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003946void
Douglas Gregor02024a92010-03-28 02:42:43 +00003947Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
3948 llvm::SmallVectorImpl<bool> &Deduced) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003949 TemplateParameterList *TemplateParams
Douglas Gregor63f07c52009-09-18 23:21:38 +00003950 = FunctionTemplate->getTemplateParameters();
3951 Deduced.clear();
3952 Deduced.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003953
Douglas Gregor63f07c52009-09-18 23:21:38 +00003954 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
3955 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
3956 ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003957 true, TemplateParams->getDepth(), Deduced);
Douglas Gregor63f07c52009-09-18 23:21:38 +00003958}