blob: 73d523f8bb1216119c86688c0fa3a01a6ffa9c75 [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 Gregor61d0b6b2011-04-28 00:56:09 +0000803/// \brief Determine whether the parameter has qualifiers that are either
804/// inconsistent with or a superset of the argument's qualifiers.
805static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType,
806 QualType ArgType) {
807 Qualifiers ParamQs = ParamType.getQualifiers();
808 Qualifiers ArgQs = ArgType.getQualifiers();
809
810 if (ParamQs == ArgQs)
811 return false;
812
813 // Mismatched (but not missing) Objective-C GC attributes.
814 if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() &&
815 ParamQs.hasObjCGCAttr())
816 return true;
817
818 // Mismatched (but not missing) address spaces.
819 if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() &&
820 ParamQs.hasAddressSpace())
821 return true;
822
823 // CVR qualifier superset.
824 return (ParamQs.getCVRQualifiers() != ArgQs.getCVRQualifiers()) &&
825 ((ParamQs.getCVRQualifiers() | ArgQs.getCVRQualifiers())
826 == ParamQs.getCVRQualifiers());
827}
828
Douglas Gregor500d3312009-06-26 18:27:22 +0000829/// \brief Deduce the template arguments by comparing the parameter type and
830/// the argument type (C++ [temp.deduct.type]).
831///
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000832/// \param S the semantic analysis object within which we are deducing
Douglas Gregor500d3312009-06-26 18:27:22 +0000833///
834/// \param TemplateParams the template parameters that we are deducing
835///
836/// \param ParamIn the parameter type
837///
838/// \param ArgIn the argument type
839///
840/// \param Info information about the template argument deduction itself
841///
842/// \param Deduced the deduced template arguments
843///
Douglas Gregor508f1c82009-06-26 23:10:12 +0000844/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
Mike Stump1eb44332009-09-09 15:08:12 +0000845/// how template argument deduction is performed.
Douglas Gregor500d3312009-06-26 18:27:22 +0000846///
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000847/// \param PartialOrdering Whether we're performing template argument deduction
848/// in the context of partial ordering (C++0x [temp.deduct.partial]).
849///
Douglas Gregorb939a192011-01-21 17:29:42 +0000850/// \param RefParamComparisons If we're performing template argument deduction
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000851/// in the context of partial ordering, the set of qualifier comparisons.
852///
Douglas Gregor500d3312009-06-26 18:27:22 +0000853/// \returns the result of template argument deduction so far. Note that a
854/// "success" result means that template argument deduction has not yet failed,
855/// but it may still fail, later, for other reasons.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000856static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000857DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000858 TemplateParameterList *TemplateParams,
859 QualType ParamIn, QualType ArgIn,
John McCall2a7fb272010-08-25 05:32:35 +0000860 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000861 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000862 unsigned TDF,
863 bool PartialOrdering,
Douglas Gregorb939a192011-01-21 17:29:42 +0000864 llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *RefParamComparisons) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000865 // We only want to look at the canonical types, since typedefs and
866 // sugar are not part of template argument deduction.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000867 QualType Param = S.Context.getCanonicalType(ParamIn);
868 QualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000869
Douglas Gregor77d6bb92011-01-11 22:21:24 +0000870 // If the argument type is a pack expansion, look at its pattern.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000871 // This isn't explicitly called out
Douglas Gregor77d6bb92011-01-11 22:21:24 +0000872 if (const PackExpansionType *ArgExpansion
873 = dyn_cast<PackExpansionType>(Arg))
874 Arg = ArgExpansion->getPattern();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000875
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000876 if (PartialOrdering) {
877 // C++0x [temp.deduct.partial]p5:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000878 // Before the partial ordering is done, certain transformations are
879 // performed on the types used for partial ordering:
880 // - If P is a reference type, P is replaced by the type referred to.
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000881 const ReferenceType *ParamRef = Param->getAs<ReferenceType>();
882 if (ParamRef)
883 Param = ParamRef->getPointeeType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000884
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000885 // - If A is a reference type, A is replaced by the type referred to.
886 const ReferenceType *ArgRef = Arg->getAs<ReferenceType>();
887 if (ArgRef)
888 Arg = ArgRef->getPointeeType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000889
Douglas Gregorb939a192011-01-21 17:29:42 +0000890 if (RefParamComparisons && ParamRef && ArgRef) {
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000891 // C++0x [temp.deduct.partial]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000892 // If both P and A were reference types (before being replaced with the
893 // type referred to above), determine which of the two types (if any) is
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000894 // more cv-qualified than the other; otherwise the types are considered
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000895 // to be equally cv-qualified for partial ordering purposes. The result
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000896 // of this determination will be used below.
897 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000898 // We save this information for later, using it only when deduction
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000899 // succeeds in both directions.
Douglas Gregorb939a192011-01-21 17:29:42 +0000900 RefParamPartialOrderingComparison Comparison;
901 Comparison.ParamIsRvalueRef = ParamRef->getAs<RValueReferenceType>();
902 Comparison.ArgIsRvalueRef = ArgRef->getAs<RValueReferenceType>();
903 Comparison.Qualifiers = NeitherMoreQualified;
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000904 if (Param.isMoreQualifiedThan(Arg))
Douglas Gregorb939a192011-01-21 17:29:42 +0000905 Comparison.Qualifiers = ParamMoreQualified;
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000906 else if (Arg.isMoreQualifiedThan(Param))
Douglas Gregorb939a192011-01-21 17:29:42 +0000907 Comparison.Qualifiers = ArgMoreQualified;
908 RefParamComparisons->push_back(Comparison);
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000909 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000910
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000911 // C++0x [temp.deduct.partial]p7:
912 // Remove any top-level cv-qualifiers:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000913 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000914 // version of P.
915 Param = Param.getUnqualifiedType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000916 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000917 // version of A.
918 Arg = Arg.getUnqualifiedType();
919 } else {
920 // C++0x [temp.deduct.call]p4 bullet 1:
921 // - If the original P is a reference type, the deduced A (i.e., the type
922 // referred to by the reference) can be more cv-qualified than the
923 // transformed A.
924 if (TDF & TDF_ParamWithReferenceType) {
925 Qualifiers Quals;
926 QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals);
927 Quals.setCVRQualifiers(Quals.getCVRQualifiers() &
John McCall62c28c82011-01-18 07:41:22 +0000928 Arg.getCVRQualifiers());
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000929 Param = S.Context.getQualifiedType(UnqualParam, Quals);
930 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000931
Douglas Gregor73b3cf62011-01-25 17:19:08 +0000932 if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) {
933 // C++0x [temp.deduct.type]p10:
934 // If P and A are function types that originated from deduction when
935 // taking the address of a function template (14.8.2.2) or when deducing
936 // template arguments from a function declaration (14.8.2.6) and Pi and
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000937 // Ai are parameters of the top-level parameter-type-list of P and A,
938 // respectively, Pi is adjusted if it is an rvalue reference to a
939 // cv-unqualified template parameter and Ai is an lvalue reference, in
940 // which case the type of Pi is changed to be the template parameter
Douglas Gregor73b3cf62011-01-25 17:19:08 +0000941 // type (i.e., T&& is changed to simply T). [ Note: As a result, when
942 // Pi is T&& and Ai is X&, the adjusted Pi will be T, causing T to be
NAKAMURA Takumi00995302011-01-27 07:09:49 +0000943 // deduced as X&. - end note ]
Douglas Gregor73b3cf62011-01-25 17:19:08 +0000944 TDF &= ~TDF_TopLevelParameterTypeList;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000945
Douglas Gregor73b3cf62011-01-25 17:19:08 +0000946 if (const RValueReferenceType *ParamRef
947 = Param->getAs<RValueReferenceType>()) {
948 if (isa<TemplateTypeParmType>(ParamRef->getPointeeType()) &&
949 !ParamRef->getPointeeType().getQualifiers())
950 if (Arg->isLValueReferenceType())
951 Param = ParamRef->getPointeeType();
952 }
953 }
Douglas Gregor500d3312009-06-26 18:27:22 +0000954 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000955
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000956 // If the parameter type is not dependent, there is nothing to deduce.
Douglas Gregor12820292009-09-14 20:00:47 +0000957 if (!Param->isDependentType()) {
Douglas Gregor3cae5c92011-01-10 20:53:55 +0000958 if (!(TDF & TDF_SkipNonDependent) && Param != Arg)
Douglas Gregor12820292009-09-14 20:00:47 +0000959 return Sema::TDK_NonDeducedMismatch;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000960
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000961 return Sema::TDK_Success;
Douglas Gregor12820292009-09-14 20:00:47 +0000962 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000963
Douglas Gregor199d9912009-06-05 00:53:49 +0000964 // C++ [temp.deduct.type]p9:
Mike Stump1eb44332009-09-09 15:08:12 +0000965 // A template type argument T, a template template argument TT or a
966 // template non-type argument i can be deduced if P and A have one of
Douglas Gregor199d9912009-06-05 00:53:49 +0000967 // the following forms:
968 //
969 // T
970 // cv-list T
Mike Stump1eb44332009-09-09 15:08:12 +0000971 if (const TemplateTypeParmType *TemplateTypeParm
John McCall183700f2009-09-21 23:43:11 +0000972 = Param->getAs<TemplateTypeParmType>()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000973 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000974 bool RecanonicalizeArg = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Douglas Gregor9e9fae42009-07-22 20:02:25 +0000976 // If the argument type is an array type, move the qualifiers up to the
977 // top level, so they can be matched with the qualifiers on the parameter.
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000978 if (isa<ArrayType>(Arg)) {
John McCall0953e762009-09-24 19:53:00 +0000979 Qualifiers Quals;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000980 Arg = S.Context.getUnqualifiedArrayType(Arg, Quals);
John McCall0953e762009-09-24 19:53:00 +0000981 if (Quals) {
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000982 Arg = S.Context.getQualifiedType(Arg, Quals);
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000983 RecanonicalizeArg = true;
984 }
985 }
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000987 // The argument type can not be less qualified than the parameter
988 // type.
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000989 if (!(TDF & TDF_IgnoreQualifiers) &&
990 hasInconsistentOrSupersetQualifiersOf(Param, Arg)) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000991 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
John McCall57e97782010-08-05 09:05:08 +0000992 Info.FirstArg = TemplateArgument(Param);
John McCall833ca992009-10-29 08:12:44 +0000993 Info.SecondArg = TemplateArgument(Arg);
John McCall57e97782010-08-05 09:05:08 +0000994 return Sema::TDK_Underqualified;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000995 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000996
997 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000998 assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function");
John McCall0953e762009-09-24 19:53:00 +0000999 QualType DeducedType = Arg;
John McCall49f4e1c2010-12-10 11:01:00 +00001000
Douglas Gregor61d0b6b2011-04-28 00:56:09 +00001001 // Remove any qualifiers on the parameter from the deduced type.
1002 // We checked the qualifiers for consistency above.
1003 Qualifiers DeducedQs = DeducedType.getQualifiers();
1004 Qualifiers ParamQs = Param.getQualifiers();
1005 DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers());
1006 if (ParamQs.hasObjCGCAttr())
1007 DeducedQs.removeObjCGCAttr();
1008 if (ParamQs.hasAddressSpace())
1009 DeducedQs.removeAddressSpace();
1010 DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(),
1011 DeducedQs);
1012
Douglas Gregorf290e0d2009-07-22 21:30:48 +00001013 if (RecanonicalizeArg)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001014 DeducedType = S.Context.getCanonicalType(DeducedType);
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Douglas Gregor0d80abc2010-12-22 23:09:49 +00001016 DeducedTemplateArgument NewDeduced(DeducedType);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001017 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
Douglas Gregor0d80abc2010-12-22 23:09:49 +00001018 Deduced[Index],
1019 NewDeduced);
1020 if (Result.isNull()) {
1021 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
1022 Info.FirstArg = Deduced[Index];
1023 Info.SecondArg = NewDeduced;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001024 return Sema::TDK_Inconsistent;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001025 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001026
Douglas Gregor0d80abc2010-12-22 23:09:49 +00001027 Deduced[Index] = Result;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001028 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001029 }
1030
Douglas Gregorf67875d2009-06-12 18:26:56 +00001031 // Set up the template argument deduction information for a failure.
John McCall833ca992009-10-29 08:12:44 +00001032 Info.FirstArg = TemplateArgument(ParamIn);
1033 Info.SecondArg = TemplateArgument(ArgIn);
Douglas Gregorf67875d2009-06-12 18:26:56 +00001034
Douglas Gregor0bc15d92011-01-14 05:11:40 +00001035 // If the parameter is an already-substituted template parameter
1036 // pack, do nothing: we don't know which of its arguments to look
1037 // at, so we have to wait until all of the parameter packs in this
1038 // expansion have arguments.
1039 if (isa<SubstTemplateTypeParmPackType>(Param))
1040 return Sema::TDK_Success;
1041
Douglas Gregor508f1c82009-06-26 23:10:12 +00001042 // Check the cv-qualifiers on the parameter and argument types.
1043 if (!(TDF & TDF_IgnoreQualifiers)) {
1044 if (TDF & TDF_ParamWithReferenceType) {
Douglas Gregor61d0b6b2011-04-28 00:56:09 +00001045 if (hasInconsistentOrSupersetQualifiersOf(Param, Arg))
Douglas Gregor508f1c82009-06-26 23:10:12 +00001046 return Sema::TDK_NonDeducedMismatch;
John McCallcd05e812010-08-28 22:14:41 +00001047 } else if (!IsPossiblyOpaquelyQualifiedType(Param)) {
Douglas Gregor508f1c82009-06-26 23:10:12 +00001048 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Mike Stump1eb44332009-09-09 15:08:12 +00001049 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor508f1c82009-06-26 23:10:12 +00001050 }
1051 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001052
Douglas Gregord560d502009-06-04 00:21:18 +00001053 switch (Param->getTypeClass()) {
Douglas Gregor199d9912009-06-05 00:53:49 +00001054 // No deduction possible for these types
1055 case Type::Builtin:
Douglas Gregorf67875d2009-06-12 18:26:56 +00001056 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Douglas Gregor199d9912009-06-05 00:53:49 +00001058 // T *
Douglas Gregord560d502009-06-04 00:21:18 +00001059 case Type::Pointer: {
John McCallc0008342010-05-13 07:48:05 +00001060 QualType PointeeType;
1061 if (const PointerType *PointerArg = Arg->getAs<PointerType>()) {
1062 PointeeType = PointerArg->getPointeeType();
1063 } else if (const ObjCObjectPointerType *PointerArg
1064 = Arg->getAs<ObjCObjectPointerType>()) {
1065 PointeeType = PointerArg->getPointeeType();
1066 } else {
Douglas Gregorf67875d2009-06-12 18:26:56 +00001067 return Sema::TDK_NonDeducedMismatch;
John McCallc0008342010-05-13 07:48:05 +00001068 }
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Douglas Gregor41128772009-06-26 23:27:24 +00001070 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001071 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +00001072 cast<PointerType>(Param)->getPointeeType(),
John McCallc0008342010-05-13 07:48:05 +00001073 PointeeType,
Douglas Gregor41128772009-06-26 23:27:24 +00001074 Info, Deduced, SubTDF);
Douglas Gregord560d502009-06-04 00:21:18 +00001075 }
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Douglas Gregor199d9912009-06-05 00:53:49 +00001077 // T &
Douglas Gregord560d502009-06-04 00:21:18 +00001078 case Type::LValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +00001079 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +00001080 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001081 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001083 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +00001084 cast<LValueReferenceType>(Param)->getPointeeType(),
1085 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +00001086 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +00001087 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001088
Douglas Gregor199d9912009-06-05 00:53:49 +00001089 // T && [C++0x]
Douglas Gregord560d502009-06-04 00:21:18 +00001090 case Type::RValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +00001091 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +00001092 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001093 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001095 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +00001096 cast<RValueReferenceType>(Param)->getPointeeType(),
1097 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +00001098 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +00001099 }
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Douglas Gregor199d9912009-06-05 00:53:49 +00001101 // T [] (implied, but not stated explicitly)
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001102 case Type::IncompleteArray: {
Mike Stump1eb44332009-09-09 15:08:12 +00001103 const IncompleteArrayType *IncompleteArrayArg =
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001104 S.Context.getAsIncompleteArrayType(Arg);
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001105 if (!IncompleteArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001106 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001107
John McCalle4f26e52010-08-19 00:20:19 +00001108 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001109 return DeduceTemplateArguments(S, TemplateParams,
1110 S.Context.getAsIncompleteArrayType(Param)->getElementType(),
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001111 IncompleteArrayArg->getElementType(),
John McCalle4f26e52010-08-19 00:20:19 +00001112 Info, Deduced, SubTDF);
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001113 }
Douglas Gregor199d9912009-06-05 00:53:49 +00001114
1115 // T [integer-constant]
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001116 case Type::ConstantArray: {
Mike Stump1eb44332009-09-09 15:08:12 +00001117 const ConstantArrayType *ConstantArrayArg =
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001118 S.Context.getAsConstantArrayType(Arg);
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001119 if (!ConstantArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001120 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001121
1122 const ConstantArrayType *ConstantArrayParm =
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001123 S.Context.getAsConstantArrayType(Param);
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001124 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregorf67875d2009-06-12 18:26:56 +00001125 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001126
John McCalle4f26e52010-08-19 00:20:19 +00001127 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001128 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001129 ConstantArrayParm->getElementType(),
1130 ConstantArrayArg->getElementType(),
John McCalle4f26e52010-08-19 00:20:19 +00001131 Info, Deduced, SubTDF);
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001132 }
1133
Douglas Gregor199d9912009-06-05 00:53:49 +00001134 // type [i]
1135 case Type::DependentSizedArray: {
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001136 const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
Douglas Gregor199d9912009-06-05 00:53:49 +00001137 if (!ArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001138 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001139
John McCalle4f26e52010-08-19 00:20:19 +00001140 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
1141
Douglas Gregor199d9912009-06-05 00:53:49 +00001142 // Check the element type of the arrays
1143 const DependentSizedArrayType *DependentArrayParm
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001144 = S.Context.getAsDependentSizedArrayType(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +00001145 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001146 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001147 DependentArrayParm->getElementType(),
1148 ArrayArg->getElementType(),
John McCalle4f26e52010-08-19 00:20:19 +00001149 Info, Deduced, SubTDF))
Douglas Gregorf67875d2009-06-12 18:26:56 +00001150 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Douglas Gregor199d9912009-06-05 00:53:49 +00001152 // Determine the array bound is something we can deduce.
Mike Stump1eb44332009-09-09 15:08:12 +00001153 NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +00001154 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
1155 if (!NTTP)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001156 return Sema::TDK_Success;
Mike Stump1eb44332009-09-09 15:08:12 +00001157
1158 // We can perform template argument deduction for the given non-type
Douglas Gregor199d9912009-06-05 00:53:49 +00001159 // template parameter.
Mike Stump1eb44332009-09-09 15:08:12 +00001160 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +00001161 "Cannot deduce non-type template argument at depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +00001162 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson335e24a2009-06-16 22:44:31 +00001163 = dyn_cast<ConstantArrayType>(ArrayArg)) {
1164 llvm::APSInt Size(ConstantArrayArg->getSize());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001165 return DeduceNonTypeTemplateArgument(S, NTTP, Size,
Douglas Gregor9d0e4412010-03-26 05:50:28 +00001166 S.Context.getSizeType(),
Douglas Gregor02024a92010-03-28 02:42:43 +00001167 /*ArrayBound=*/true,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001168 Info, Deduced);
Anders Carlsson335e24a2009-06-16 22:44:31 +00001169 }
Douglas Gregor199d9912009-06-05 00:53:49 +00001170 if (const DependentSizedArrayType *DependentArrayArg
1171 = dyn_cast<DependentSizedArrayType>(ArrayArg))
Douglas Gregor34c2f8c2010-12-22 23:15:38 +00001172 if (DependentArrayArg->getSizeExpr())
1173 return DeduceNonTypeTemplateArgument(S, NTTP,
1174 DependentArrayArg->getSizeExpr(),
1175 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Douglas Gregor199d9912009-06-05 00:53:49 +00001177 // Incomplete type does not match a dependently-sized array type
Douglas Gregorf67875d2009-06-12 18:26:56 +00001178 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +00001179 }
Mike Stump1eb44332009-09-09 15:08:12 +00001180
1181 // type(*)(T)
1182 // T(*)()
1183 // T(*)(T)
Anders Carlssona27fad52009-06-08 15:19:08 +00001184 case Type::FunctionProto: {
Douglas Gregor73b3cf62011-01-25 17:19:08 +00001185 unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList;
Mike Stump1eb44332009-09-09 15:08:12 +00001186 const FunctionProtoType *FunctionProtoArg =
Anders Carlssona27fad52009-06-08 15:19:08 +00001187 dyn_cast<FunctionProtoType>(Arg);
1188 if (!FunctionProtoArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001189 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001190
1191 const FunctionProtoType *FunctionProtoParam =
Anders Carlssona27fad52009-06-08 15:19:08 +00001192 cast<FunctionProtoType>(Param);
Anders Carlsson994b6cb2009-06-08 19:22:23 +00001193
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001194 if (FunctionProtoParam->getTypeQuals()
Douglas Gregore3c7a7c2011-01-26 16:50:54 +00001195 != FunctionProtoArg->getTypeQuals() ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001196 FunctionProtoParam->getRefQualifier()
Douglas Gregore3c7a7c2011-01-26 16:50:54 +00001197 != FunctionProtoArg->getRefQualifier() ||
1198 FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregorf67875d2009-06-12 18:26:56 +00001199 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +00001200
Anders Carlssona27fad52009-06-08 15:19:08 +00001201 // Check return types.
Douglas Gregorf67875d2009-06-12 18:26:56 +00001202 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001203 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001204 FunctionProtoParam->getResultType(),
1205 FunctionProtoArg->getResultType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +00001206 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +00001207 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Douglas Gregor603cfb42011-01-05 23:12:31 +00001209 return DeduceTemplateArguments(S, TemplateParams,
1210 FunctionProtoParam->arg_type_begin(),
1211 FunctionProtoParam->getNumArgs(),
1212 FunctionProtoArg->arg_type_begin(),
1213 FunctionProtoArg->getNumArgs(),
Douglas Gregor73b3cf62011-01-25 17:19:08 +00001214 Info, Deduced, SubTDF);
Anders Carlssona27fad52009-06-08 15:19:08 +00001215 }
Mike Stump1eb44332009-09-09 15:08:12 +00001216
John McCall3cb0ebd2010-03-10 03:28:59 +00001217 case Type::InjectedClassName: {
1218 // Treat a template's injected-class-name as if the template
1219 // specialization type had been used.
John McCall31f17ec2010-04-27 00:57:59 +00001220 Param = cast<InjectedClassNameType>(Param)
1221 ->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00001222 assert(isa<TemplateSpecializationType>(Param) &&
1223 "injected class name is not a template specialization type");
1224 // fall through
1225 }
1226
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001227 // template-name<T> (where template-name refers to a class template)
Douglas Gregord708c722009-06-09 16:35:58 +00001228 // template-name<i>
Douglas Gregordb0d4b72009-11-11 23:06:43 +00001229 // TT<T>
1230 // TT<i>
1231 // TT<>
Douglas Gregord708c722009-06-09 16:35:58 +00001232 case Type::TemplateSpecialization: {
1233 const TemplateSpecializationType *SpecParam
1234 = cast<TemplateSpecializationType>(Param);
Mike Stump1eb44332009-09-09 15:08:12 +00001235
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001236 // Try to deduce template arguments from the template-id.
1237 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001238 = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001239 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Douglas Gregor4a5c15f2009-09-30 22:13:51 +00001241 if (Result && (TDF & TDF_DerivedClass)) {
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001242 // C++ [temp.deduct.call]p3b3:
1243 // If P is a class, and P has the form template-id, then A can be a
1244 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump1eb44332009-09-09 15:08:12 +00001245 // class of the form template-id, A can be a pointer to a derived
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001246 // class pointed to by the deduced A.
1247 //
1248 // More importantly:
Mike Stump1eb44332009-09-09 15:08:12 +00001249 // These alternatives are considered only if type deduction would
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001250 // otherwise fail.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001251 if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
1252 // We cannot inspect base classes as part of deduction when the type
1253 // is incomplete, so either instantiate any templates necessary to
1254 // complete the type, or skip over it if it cannot be completed.
John McCall5769d612010-02-08 23:07:23 +00001255 if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001256 return Result;
1257
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001258 // Use data recursion to crawl through the list of base classes.
Mike Stump1eb44332009-09-09 15:08:12 +00001259 // Visited contains the set of nodes we have already visited, while
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001260 // ToVisit is our stack of records that we still need to visit.
1261 llvm::SmallPtrSet<const RecordType *, 8> Visited;
1262 llvm::SmallVector<const RecordType *, 8> ToVisit;
1263 ToVisit.push_back(RecordT);
1264 bool Successful = false;
Douglas Gregor053105d2010-11-02 00:02:34 +00001265 llvm::SmallVectorImpl<DeducedTemplateArgument> DeducedOrig(0);
1266 DeducedOrig = Deduced;
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001267 while (!ToVisit.empty()) {
1268 // Retrieve the next class in the inheritance hierarchy.
1269 const RecordType *NextT = ToVisit.back();
1270 ToVisit.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +00001271
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001272 // If we have already seen this type, skip it.
1273 if (!Visited.insert(NextT))
1274 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001275
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001276 // If this is a base class, try to perform template argument
1277 // deduction from it.
1278 if (NextT != RecordT) {
1279 Sema::TemplateDeductionResult BaseResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001280 = DeduceTemplateArguments(S, TemplateParams, SpecParam,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001281 QualType(NextT, 0), Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001283 // If template argument deduction for this base was successful,
Douglas Gregor053105d2010-11-02 00:02:34 +00001284 // note that we had some success. Otherwise, ignore any deductions
1285 // from this base class.
1286 if (BaseResult == Sema::TDK_Success) {
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001287 Successful = true;
Douglas Gregor053105d2010-11-02 00:02:34 +00001288 DeducedOrig = Deduced;
1289 }
1290 else
1291 Deduced = DeducedOrig;
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001292 }
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001294 // Visit base classes
1295 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
1296 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
1297 BaseEnd = Next->bases_end();
Sebastian Redl9994a342009-10-25 17:03:50 +00001298 Base != BaseEnd; ++Base) {
Mike Stump1eb44332009-09-09 15:08:12 +00001299 assert(Base->getType()->isRecordType() &&
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001300 "Base class that isn't a record?");
Ted Kremenek6217b802009-07-29 21:53:49 +00001301 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001302 }
1303 }
Mike Stump1eb44332009-09-09 15:08:12 +00001304
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001305 if (Successful)
1306 return Sema::TDK_Success;
1307 }
Mike Stump1eb44332009-09-09 15:08:12 +00001308
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001309 }
Mike Stump1eb44332009-09-09 15:08:12 +00001310
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001311 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +00001312 }
1313
Douglas Gregor637a4092009-06-10 23:47:09 +00001314 // T type::*
1315 // T T::*
1316 // T (type::*)()
1317 // type (T::*)()
1318 // type (type::*)(T)
1319 // type (T::*)(T)
1320 // T (type::*)(T)
1321 // T (T::*)()
1322 // T (T::*)(T)
1323 case Type::MemberPointer: {
1324 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
1325 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
1326 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001327 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +00001328
Douglas Gregorf67875d2009-06-12 18:26:56 +00001329 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001330 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001331 MemPtrParam->getPointeeType(),
1332 MemPtrArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +00001333 Info, Deduced,
1334 TDF & TDF_IgnoreQualifiers))
Douglas Gregorf67875d2009-06-12 18:26:56 +00001335 return Result;
1336
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001337 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001338 QualType(MemPtrParam->getClass(), 0),
1339 QualType(MemPtrArg->getClass(), 0),
Douglas Gregor508f1c82009-06-26 23:10:12 +00001340 Info, Deduced, 0);
Douglas Gregor637a4092009-06-10 23:47:09 +00001341 }
1342
Anders Carlsson9a917e42009-06-12 22:56:54 +00001343 // (clang extension)
1344 //
Mike Stump1eb44332009-09-09 15:08:12 +00001345 // type(^)(T)
1346 // T(^)()
1347 // T(^)(T)
Anders Carlsson859ba502009-06-12 16:23:10 +00001348 case Type::BlockPointer: {
1349 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
1350 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Anders Carlsson859ba502009-06-12 16:23:10 +00001352 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001353 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001354
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001355 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +00001356 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +00001357 BlockPtrArg->getPointeeType(), Info,
Douglas Gregor508f1c82009-06-26 23:10:12 +00001358 Deduced, 0);
Anders Carlsson859ba502009-06-12 16:23:10 +00001359 }
1360
Douglas Gregor637a4092009-06-10 23:47:09 +00001361 case Type::TypeOfExpr:
1362 case Type::TypeOf:
Douglas Gregor4714c122010-03-31 17:34:00 +00001363 case Type::DependentName:
Douglas Gregor637a4092009-06-10 23:47:09 +00001364 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +00001365 return Sema::TDK_Success;
Douglas Gregor637a4092009-06-10 23:47:09 +00001366
Douglas Gregord560d502009-06-04 00:21:18 +00001367 default:
1368 break;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001369 }
1370
1371 // FIXME: Many more cases to go (to go).
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001372 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001373}
1374
Douglas Gregorf67875d2009-06-12 18:26:56 +00001375static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001376DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001377 TemplateParameterList *TemplateParams,
1378 const TemplateArgument &Param,
Douglas Gregor77d6bb92011-01-11 22:21:24 +00001379 TemplateArgument Arg,
John McCall2a7fb272010-08-25 05:32:35 +00001380 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +00001381 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor77d6bb92011-01-11 22:21:24 +00001382 // If the template argument is a pack expansion, perform template argument
1383 // deduction against the pattern of that expansion. This only occurs during
1384 // partial ordering.
1385 if (Arg.isPackExpansion())
1386 Arg = Arg.getPackExpansionPattern();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001387
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001388 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +00001389 case TemplateArgument::Null:
1390 assert(false && "Null template argument in parameter list");
1391 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001392
1393 case TemplateArgument::Type:
Douglas Gregor788cd062009-11-11 01:00:40 +00001394 if (Arg.getKind() == TemplateArgument::Type)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001395 return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
Douglas Gregor788cd062009-11-11 01:00:40 +00001396 Arg.getAsType(), Info, Deduced, 0);
1397 Info.FirstArg = Param;
1398 Info.SecondArg = Arg;
1399 return Sema::TDK_NonDeducedMismatch;
Douglas Gregora7fc9012011-01-05 18:58:31 +00001400
Douglas Gregor788cd062009-11-11 01:00:40 +00001401 case TemplateArgument::Template:
Douglas Gregordb0d4b72009-11-11 23:06:43 +00001402 if (Arg.getKind() == TemplateArgument::Template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001403 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor788cd062009-11-11 01:00:40 +00001404 Param.getAsTemplate(),
Douglas Gregordb0d4b72009-11-11 23:06:43 +00001405 Arg.getAsTemplate(), Info, Deduced);
Douglas Gregor788cd062009-11-11 01:00:40 +00001406 Info.FirstArg = Param;
1407 Info.SecondArg = Arg;
1408 return Sema::TDK_NonDeducedMismatch;
Douglas Gregora7fc9012011-01-05 18:58:31 +00001409
1410 case TemplateArgument::TemplateExpansion:
1411 llvm_unreachable("caller should handle pack expansions");
1412 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001413
Douglas Gregor199d9912009-06-05 00:53:49 +00001414 case TemplateArgument::Declaration:
Douglas Gregor788cd062009-11-11 01:00:40 +00001415 if (Arg.getKind() == TemplateArgument::Declaration &&
1416 Param.getAsDecl()->getCanonicalDecl() ==
1417 Arg.getAsDecl()->getCanonicalDecl())
1418 return Sema::TDK_Success;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001419
Douglas Gregorf67875d2009-06-12 18:26:56 +00001420 Info.FirstArg = Param;
1421 Info.SecondArg = Arg;
1422 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Douglas Gregor199d9912009-06-05 00:53:49 +00001424 case TemplateArgument::Integral:
1425 if (Arg.getKind() == TemplateArgument::Integral) {
Douglas Gregor9d0e4412010-03-26 05:50:28 +00001426 if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral()))
Douglas Gregorf67875d2009-06-12 18:26:56 +00001427 return Sema::TDK_Success;
1428
1429 Info.FirstArg = Param;
1430 Info.SecondArg = Arg;
1431 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +00001432 }
Douglas Gregorf67875d2009-06-12 18:26:56 +00001433
1434 if (Arg.getKind() == TemplateArgument::Expression) {
1435 Info.FirstArg = Param;
1436 Info.SecondArg = Arg;
1437 return Sema::TDK_NonDeducedMismatch;
1438 }
Douglas Gregor199d9912009-06-05 00:53:49 +00001439
Douglas Gregorf67875d2009-06-12 18:26:56 +00001440 Info.FirstArg = Param;
1441 Info.SecondArg = Arg;
1442 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Douglas Gregor199d9912009-06-05 00:53:49 +00001444 case TemplateArgument::Expression: {
Mike Stump1eb44332009-09-09 15:08:12 +00001445 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +00001446 = getDeducedParameterFromExpr(Param.getAsExpr())) {
1447 if (Arg.getKind() == TemplateArgument::Integral)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001448 return DeduceNonTypeTemplateArgument(S, NTTP,
Mike Stump1eb44332009-09-09 15:08:12 +00001449 *Arg.getAsIntegral(),
Douglas Gregor9d0e4412010-03-26 05:50:28 +00001450 Arg.getIntegralType(),
Douglas Gregor02024a92010-03-28 02:42:43 +00001451 /*ArrayBound=*/false,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001452 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +00001453 if (Arg.getKind() == TemplateArgument::Expression)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001454 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +00001455 Info, Deduced);
Douglas Gregor15755cb2009-11-13 23:45:44 +00001456 if (Arg.getKind() == TemplateArgument::Declaration)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001457 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
Douglas Gregor15755cb2009-11-13 23:45:44 +00001458 Info, Deduced);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001459
Douglas Gregorf67875d2009-06-12 18:26:56 +00001460 Info.FirstArg = Param;
1461 Info.SecondArg = Arg;
1462 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +00001463 }
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Douglas Gregor199d9912009-06-05 00:53:49 +00001465 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +00001466 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001467 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001468 case TemplateArgument::Pack:
Douglas Gregor20a55e22010-12-22 18:17:10 +00001469 llvm_unreachable("Argument packs should be expanded by the caller!");
Douglas Gregor199d9912009-06-05 00:53:49 +00001470 }
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Douglas Gregorf67875d2009-06-12 18:26:56 +00001472 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001473}
1474
Douglas Gregor20a55e22010-12-22 18:17:10 +00001475/// \brief Determine whether there is a template argument to be used for
1476/// deduction.
1477///
1478/// This routine "expands" argument packs in-place, overriding its input
1479/// parameters so that \c Args[ArgIdx] will be the available template argument.
1480///
1481/// \returns true if there is another template argument (which will be at
1482/// \c Args[ArgIdx]), false otherwise.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001483static bool hasTemplateArgumentForDeduction(const TemplateArgument *&Args,
Douglas Gregor20a55e22010-12-22 18:17:10 +00001484 unsigned &ArgIdx,
1485 unsigned &NumArgs) {
1486 if (ArgIdx == NumArgs)
1487 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001488
Douglas Gregor20a55e22010-12-22 18:17:10 +00001489 const TemplateArgument &Arg = Args[ArgIdx];
1490 if (Arg.getKind() != TemplateArgument::Pack)
1491 return true;
1492
1493 assert(ArgIdx == NumArgs - 1 && "Pack not at the end of argument list?");
1494 Args = Arg.pack_begin();
1495 NumArgs = Arg.pack_size();
1496 ArgIdx = 0;
1497 return ArgIdx < NumArgs;
1498}
1499
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001500/// \brief Determine whether the given set of template arguments has a pack
1501/// expansion that is not the last template argument.
1502static bool hasPackExpansionBeforeEnd(const TemplateArgument *Args,
1503 unsigned NumArgs) {
1504 unsigned ArgIdx = 0;
1505 while (ArgIdx < NumArgs) {
1506 const TemplateArgument &Arg = Args[ArgIdx];
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001507
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001508 // Unwrap argument packs.
1509 if (Args[ArgIdx].getKind() == TemplateArgument::Pack) {
1510 Args = Arg.pack_begin();
1511 NumArgs = Arg.pack_size();
1512 ArgIdx = 0;
1513 continue;
1514 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001515
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001516 ++ArgIdx;
1517 if (ArgIdx == NumArgs)
1518 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001519
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001520 if (Arg.isPackExpansion())
1521 return true;
1522 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001523
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001524 return false;
1525}
1526
Douglas Gregor20a55e22010-12-22 18:17:10 +00001527static Sema::TemplateDeductionResult
1528DeduceTemplateArguments(Sema &S,
1529 TemplateParameterList *TemplateParams,
1530 const TemplateArgument *Params, unsigned NumParams,
1531 const TemplateArgument *Args, unsigned NumArgs,
1532 TemplateDeductionInfo &Info,
Douglas Gregor0972c862010-12-22 18:55:49 +00001533 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1534 bool NumberOfArgumentsMustMatch) {
Douglas Gregore02e2622010-12-22 21:19:48 +00001535 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001536 // If the template argument list of P contains a pack expansion that is not
1537 // the last template argument, the entire template argument list is a
Douglas Gregore02e2622010-12-22 21:19:48 +00001538 // non-deduced context.
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001539 if (hasPackExpansionBeforeEnd(Params, NumParams))
1540 return Sema::TDK_Success;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001541
Douglas Gregore02e2622010-12-22 21:19:48 +00001542 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001543 // If P has a form that contains <T> or <i>, then each argument Pi of the
1544 // respective template argument list P is compared with the corresponding
Douglas Gregore02e2622010-12-22 21:19:48 +00001545 // argument Ai of the corresponding template argument list of A.
Douglas Gregor20a55e22010-12-22 18:17:10 +00001546 unsigned ArgIdx = 0, ParamIdx = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001547 for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams);
Douglas Gregor20a55e22010-12-22 18:17:10 +00001548 ++ParamIdx) {
1549 if (!Params[ParamIdx].isPackExpansion()) {
Douglas Gregore02e2622010-12-22 21:19:48 +00001550 // The simple case: deduce template arguments by matching Pi and Ai.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001551
Douglas Gregor20a55e22010-12-22 18:17:10 +00001552 // Check whether we have enough arguments.
1553 if (!hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
Douglas Gregor3cae5c92011-01-10 20:53:55 +00001554 return NumberOfArgumentsMustMatch? Sema::TDK_NonDeducedMismatch
Douglas Gregor0972c862010-12-22 18:55:49 +00001555 : Sema::TDK_Success;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001556
Douglas Gregor77d6bb92011-01-11 22:21:24 +00001557 if (Args[ArgIdx].isPackExpansion()) {
1558 // FIXME: We follow the logic of C++0x [temp.deduct.type]p22 here,
1559 // but applied to pack expansions that are template arguments.
1560 return Sema::TDK_NonDeducedMismatch;
1561 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001562
Douglas Gregore02e2622010-12-22 21:19:48 +00001563 // Perform deduction for this Pi/Ai pair.
Douglas Gregor20a55e22010-12-22 18:17:10 +00001564 if (Sema::TemplateDeductionResult Result
Douglas Gregor77d6bb92011-01-11 22:21:24 +00001565 = DeduceTemplateArguments(S, TemplateParams,
1566 Params[ParamIdx], Args[ArgIdx],
1567 Info, Deduced))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001568 return Result;
1569
Douglas Gregor20a55e22010-12-22 18:17:10 +00001570 // Move to the next argument.
1571 ++ArgIdx;
1572 continue;
1573 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001574
Douglas Gregore02e2622010-12-22 21:19:48 +00001575 // The parameter is a pack expansion.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001576
Douglas Gregore02e2622010-12-22 21:19:48 +00001577 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001578 // If Pi is a pack expansion, then the pattern of Pi is compared with
1579 // each remaining argument in the template argument list of A. Each
1580 // comparison deduces template arguments for subsequent positions in the
Douglas Gregore02e2622010-12-22 21:19:48 +00001581 // template parameter packs expanded by Pi.
1582 TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001583
Douglas Gregore02e2622010-12-22 21:19:48 +00001584 // Compute the set of template parameter indices that correspond to
1585 // parameter packs expanded by the pack expansion.
1586 llvm::SmallVector<unsigned, 2> PackIndices;
1587 {
1588 llvm::BitVector SawIndices(TemplateParams->size());
1589 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1590 S.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1591 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
1592 unsigned Depth, Index;
1593 llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
1594 if (Depth == 0 && !SawIndices[Index]) {
1595 SawIndices[Index] = true;
1596 PackIndices.push_back(Index);
1597 }
1598 }
1599 }
1600 assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001601
Douglas Gregore02e2622010-12-22 21:19:48 +00001602 // FIXME: If there are no remaining arguments, we can bail out early
1603 // and set any deduced parameter packs to an empty argument pack.
1604 // The latter part of this is a (minor) correctness issue.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001605
Douglas Gregore02e2622010-12-22 21:19:48 +00001606 // Save the deduced template arguments for each parameter pack expanded
1607 // by this pack expansion, then clear out the deduction.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001608 llvm::SmallVector<DeducedTemplateArgument, 2>
Douglas Gregore02e2622010-12-22 21:19:48 +00001609 SavedPacks(PackIndices.size());
Douglas Gregor54293852011-01-10 17:35:05 +00001610 llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2>
1611 NewlyDeducedPacks(PackIndices.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001612 PrepareArgumentPackDeduction(S, Deduced, PackIndices, SavedPacks,
Douglas Gregor54293852011-01-10 17:35:05 +00001613 NewlyDeducedPacks);
Douglas Gregore02e2622010-12-22 21:19:48 +00001614
1615 // Keep track of the deduced template arguments for each parameter pack
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001616 // expanded by this pack expansion (the outer index) and for each
Douglas Gregore02e2622010-12-22 21:19:48 +00001617 // template argument (the inner SmallVectors).
Douglas Gregore02e2622010-12-22 21:19:48 +00001618 bool HasAnyArguments = false;
1619 while (hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) {
1620 HasAnyArguments = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001621
Douglas Gregore02e2622010-12-22 21:19:48 +00001622 // Deduce template arguments from the pattern.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001623 if (Sema::TemplateDeductionResult Result
Douglas Gregore02e2622010-12-22 21:19:48 +00001624 = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx],
1625 Info, Deduced))
1626 return Result;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001627
Douglas Gregore02e2622010-12-22 21:19:48 +00001628 // Capture the deduced template arguments for each parameter pack expanded
1629 // by this pack expansion, add them to the list of arguments we've deduced
1630 // for that pack, then clear out the deduced argument.
1631 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
1632 DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
1633 if (!DeducedArg.isNull()) {
1634 NewlyDeducedPacks[I].push_back(DeducedArg);
1635 DeducedArg = DeducedTemplateArgument();
1636 }
1637 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001638
Douglas Gregore02e2622010-12-22 21:19:48 +00001639 ++ArgIdx;
1640 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001641
Douglas Gregore02e2622010-12-22 21:19:48 +00001642 // Build argument packs for each of the parameter packs expanded by this
1643 // pack expansion.
Douglas Gregor0216f812011-01-10 17:53:52 +00001644 if (Sema::TemplateDeductionResult Result
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001645 = FinishArgumentPackDeduction(S, TemplateParams, HasAnyArguments,
Douglas Gregor0216f812011-01-10 17:53:52 +00001646 Deduced, PackIndices, SavedPacks,
1647 NewlyDeducedPacks, Info))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001648 return Result;
Douglas Gregor20a55e22010-12-22 18:17:10 +00001649 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001650
Douglas Gregor20a55e22010-12-22 18:17:10 +00001651 // If there is an argument remaining, then we had too many arguments.
Douglas Gregor0972c862010-12-22 18:55:49 +00001652 if (NumberOfArgumentsMustMatch &&
1653 hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
Douglas Gregor3cae5c92011-01-10 20:53:55 +00001654 return Sema::TDK_NonDeducedMismatch;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001655
Douglas Gregor20a55e22010-12-22 18:17:10 +00001656 return Sema::TDK_Success;
1657}
1658
Mike Stump1eb44332009-09-09 15:08:12 +00001659static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001660DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001661 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001662 const TemplateArgumentList &ParamList,
1663 const TemplateArgumentList &ArgList,
John McCall2a7fb272010-08-25 05:32:35 +00001664 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +00001665 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001666 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor20a55e22010-12-22 18:17:10 +00001667 ParamList.data(), ParamList.size(),
1668 ArgList.data(), ArgList.size(),
1669 Info, Deduced);
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001670}
1671
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001672/// \brief Determine whether two template arguments are the same.
Mike Stump1eb44332009-09-09 15:08:12 +00001673static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001674 const TemplateArgument &X,
1675 const TemplateArgument &Y) {
1676 if (X.getKind() != Y.getKind())
1677 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001679 switch (X.getKind()) {
1680 case TemplateArgument::Null:
1681 assert(false && "Comparing NULL template argument");
1682 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001684 case TemplateArgument::Type:
1685 return Context.getCanonicalType(X.getAsType()) ==
1686 Context.getCanonicalType(Y.getAsType());
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001688 case TemplateArgument::Declaration:
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001689 return X.getAsDecl()->getCanonicalDecl() ==
1690 Y.getAsDecl()->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Douglas Gregor788cd062009-11-11 01:00:40 +00001692 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00001693 case TemplateArgument::TemplateExpansion:
1694 return Context.getCanonicalTemplateName(
1695 X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() ==
1696 Context.getCanonicalTemplateName(
1697 Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001698
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001699 case TemplateArgument::Integral:
1700 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump1eb44332009-09-09 15:08:12 +00001701
Douglas Gregor788cd062009-11-11 01:00:40 +00001702 case TemplateArgument::Expression: {
1703 llvm::FoldingSetNodeID XID, YID;
1704 X.getAsExpr()->Profile(XID, Context, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001705 Y.getAsExpr()->Profile(YID, Context, true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001706 return XID == YID;
1707 }
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001709 case TemplateArgument::Pack:
1710 if (X.pack_size() != Y.pack_size())
1711 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001712
1713 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
1714 XPEnd = X.pack_end(),
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001715 YP = Y.pack_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001716 XP != XPEnd; ++XP, ++YP)
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001717 if (!isSameTemplateArg(Context, *XP, *YP))
1718 return false;
1719
1720 return true;
1721 }
1722
1723 return false;
1724}
1725
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001726/// \brief Allocate a TemplateArgumentLoc where all locations have
1727/// been initialized to the given location.
1728///
1729/// \param S The semantic analysis object.
1730///
1731/// \param The template argument we are producing template argument
1732/// location information for.
1733///
1734/// \param NTTPType For a declaration template argument, the type of
1735/// the non-type template parameter that corresponds to this template
1736/// argument.
1737///
1738/// \param Loc The source location to use for the resulting template
1739/// argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001740static TemplateArgumentLoc
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001741getTrivialTemplateArgumentLoc(Sema &S,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001742 const TemplateArgument &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001743 QualType NTTPType,
1744 SourceLocation Loc) {
1745 switch (Arg.getKind()) {
1746 case TemplateArgument::Null:
1747 llvm_unreachable("Can't get a NULL template argument here");
1748 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001749
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001750 case TemplateArgument::Type:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001751 return TemplateArgumentLoc(Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001752 S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001753
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001754 case TemplateArgument::Declaration: {
1755 Expr *E
Douglas Gregorba68eca2011-01-05 17:40:24 +00001756 = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001757 .takeAs<Expr>();
1758 return TemplateArgumentLoc(TemplateArgument(E), E);
1759 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001760
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001761 case TemplateArgument::Integral: {
1762 Expr *E
Douglas Gregorba68eca2011-01-05 17:40:24 +00001763 = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001764 return TemplateArgumentLoc(TemplateArgument(E), E);
1765 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001766
Douglas Gregorb6744ef2011-03-02 17:09:35 +00001767 case TemplateArgument::Template:
1768 case TemplateArgument::TemplateExpansion: {
1769 NestedNameSpecifierLocBuilder Builder;
1770 TemplateName Template = Arg.getAsTemplate();
1771 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
1772 Builder.MakeTrivial(S.Context, DTN->getQualifier(), Loc);
1773 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
1774 Builder.MakeTrivial(S.Context, QTN->getQualifier(), Loc);
1775
1776 if (Arg.getKind() == TemplateArgument::Template)
1777 return TemplateArgumentLoc(Arg,
1778 Builder.getWithLocInContext(S.Context),
1779 Loc);
1780
1781
1782 return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(S.Context),
1783 Loc, Loc);
1784 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001785
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001786 case TemplateArgument::Expression:
1787 return TemplateArgumentLoc(Arg, Arg.getAsExpr());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001788
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001789 case TemplateArgument::Pack:
1790 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo());
1791 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001792
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001793 return TemplateArgumentLoc();
1794}
1795
1796
1797/// \brief Convert the given deduced template argument and add it to the set of
1798/// fully-converted template arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001799static bool ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001800 DeducedTemplateArgument Arg,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001801 NamedDecl *Template,
1802 QualType NTTPType,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001803 unsigned ArgumentPackIndex,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001804 TemplateDeductionInfo &Info,
1805 bool InFunctionTemplate,
1806 llvm::SmallVectorImpl<TemplateArgument> &Output) {
1807 if (Arg.getKind() == TemplateArgument::Pack) {
1808 // This is a template argument pack, so check each of its arguments against
1809 // the template parameter.
1810 llvm::SmallVector<TemplateArgument, 2> PackedArgsBuilder;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001811 for (TemplateArgument::pack_iterator PA = Arg.pack_begin(),
Douglas Gregor135ffa72011-01-05 21:00:53 +00001812 PAEnd = Arg.pack_end();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001813 PA != PAEnd; ++PA) {
Douglas Gregord53e16a2011-01-05 20:52:18 +00001814 // When converting the deduced template argument, append it to the
1815 // general output list. We need to do this so that the template argument
1816 // checking logic has all of the prior template arguments available.
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001817 DeducedTemplateArgument InnerArg(*PA);
1818 InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001819 if (ConvertDeducedTemplateArgument(S, Param, InnerArg, Template,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001820 NTTPType, PackedArgsBuilder.size(),
1821 Info, InFunctionTemplate, Output))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001822 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001823
Douglas Gregord53e16a2011-01-05 20:52:18 +00001824 // Move the converted template argument into our argument pack.
1825 PackedArgsBuilder.push_back(Output.back());
1826 Output.pop_back();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001827 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001828
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001829 // Create the resulting argument pack.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001830 Output.push_back(TemplateArgument::CreatePackCopy(S.Context,
Douglas Gregor203e6a32011-01-11 23:09:57 +00001831 PackedArgsBuilder.data(),
1832 PackedArgsBuilder.size()));
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001833 return false;
1834 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001835
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001836 // Convert the deduced template argument into a template
1837 // argument that we can check, almost as if the user had written
1838 // the template argument explicitly.
1839 TemplateArgumentLoc ArgLoc = getTrivialTemplateArgumentLoc(S, Arg, NTTPType,
1840 Info.getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001841
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001842 // Check the template argument, converting it as necessary.
1843 return S.CheckTemplateArgument(Param, ArgLoc,
1844 Template,
1845 Template->getLocation(),
1846 Template->getSourceRange().getEnd(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001847 ArgumentPackIndex,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001848 Output,
1849 InFunctionTemplate
1850 ? (Arg.wasDeducedFromArrayBound()
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001851 ? Sema::CTAK_DeducedFromArrayBound
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001852 : Sema::CTAK_Deduced)
1853 : Sema::CTAK_Specified);
1854}
1855
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001856/// Complete template argument deduction for a class template partial
1857/// specialization.
1858static Sema::TemplateDeductionResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001859FinishTemplateArgumentDeduction(Sema &S,
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001860 ClassTemplatePartialSpecializationDecl *Partial,
1861 const TemplateArgumentList &TemplateArgs,
1862 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
John McCall2a7fb272010-08-25 05:32:35 +00001863 TemplateDeductionInfo &Info) {
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001864 // Trap errors.
1865 Sema::SFINAETrap Trap(S);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001866
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001867 Sema::ContextRAII SavedContext(S, Partial);
1868
1869 // C++ [temp.deduct.type]p2:
1870 // [...] or if any template argument remains neither deduced nor
1871 // explicitly specified, template argument deduction fails.
Douglas Gregor910f8002010-11-07 23:05:16 +00001872 llvm::SmallVector<TemplateArgument, 4> Builder;
Douglas Gregor033a3ca2011-01-04 22:23:38 +00001873 TemplateParameterList *PartialParams = Partial->getTemplateParameters();
1874 for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) {
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001875 NamedDecl *Param = PartialParams->getParam(I);
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001876 if (Deduced[I].isNull()) {
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001877 Info.Param = makeTemplateParameter(Param);
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001878 return Sema::TDK_Incomplete;
1879 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001880
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001881 // We have deduced this argument, so it still needs to be
1882 // checked and converted.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001883
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001884 // First, for a non-type template parameter type that is
1885 // initialized by a declaration, we need the type of the
1886 // corresponding non-type template parameter.
1887 QualType NTTPType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001888 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregord53e16a2011-01-05 20:52:18 +00001889 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001890 NTTPType = NTTP->getType();
Douglas Gregord53e16a2011-01-05 20:52:18 +00001891 if (NTTPType->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001892 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregord53e16a2011-01-05 20:52:18 +00001893 Builder.data(), Builder.size());
1894 NTTPType = S.SubstType(NTTPType,
1895 MultiLevelTemplateArgumentList(TemplateArgs),
1896 NTTP->getLocation(),
1897 NTTP->getDeclName());
1898 if (NTTPType.isNull()) {
1899 Info.Param = makeTemplateParameter(Param);
1900 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001901 Info.reset(TemplateArgumentList::CreateCopy(S.Context,
1902 Builder.data(),
Douglas Gregord53e16a2011-01-05 20:52:18 +00001903 Builder.size()));
1904 return Sema::TDK_SubstitutionFailure;
1905 }
1906 }
1907 }
1908
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001909 if (ConvertDeducedTemplateArgument(S, Param, Deduced[I],
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001910 Partial, NTTPType, 0, Info, false,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001911 Builder)) {
1912 Info.Param = makeTemplateParameter(Param);
1913 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001914 Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
1915 Builder.size()));
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001916 return Sema::TDK_SubstitutionFailure;
1917 }
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001918 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001919
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001920 // Form the template argument list from the deduced template arguments.
1921 TemplateArgumentList *DeducedArgumentList
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001922 = TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001923 Builder.size());
1924
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001925 Info.reset(DeducedArgumentList);
1926
1927 // Substitute the deduced template arguments into the template
1928 // arguments of the class template partial specialization, and
1929 // verify that the instantiated template arguments are both valid
1930 // and are equivalent to the template arguments originally provided
1931 // to the class template.
John McCall2a7fb272010-08-25 05:32:35 +00001932 LocalInstantiationScope InstScope(S);
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001933 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
1934 const TemplateArgumentLoc *PartialTemplateArgs
1935 = Partial->getTemplateArgsAsWritten();
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001936
1937 // Note that we don't provide the langle and rangle locations.
1938 TemplateArgumentListInfo InstArgs;
1939
Douglas Gregore02e2622010-12-22 21:19:48 +00001940 if (S.Subst(PartialTemplateArgs,
1941 Partial->getNumTemplateArgsAsWritten(),
1942 InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
1943 unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx;
1944 if (ParamIdx >= Partial->getTemplateParameters()->size())
1945 ParamIdx = Partial->getTemplateParameters()->size() - 1;
1946
1947 Decl *Param
1948 = const_cast<NamedDecl *>(
1949 Partial->getTemplateParameters()->getParam(ParamIdx));
1950 Info.Param = makeTemplateParameter(Param);
1951 Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument();
1952 return Sema::TDK_SubstitutionFailure;
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001953 }
1954
Douglas Gregor910f8002010-11-07 23:05:16 +00001955 llvm::SmallVector<TemplateArgument, 4> ConvertedInstArgs;
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001956 if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001957 InstArgs, false, ConvertedInstArgs))
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001958 return Sema::TDK_SubstitutionFailure;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001959
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001960 TemplateParameterList *TemplateParams
1961 = ClassTemplate->getTemplateParameters();
1962 for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) {
Douglas Gregor910f8002010-11-07 23:05:16 +00001963 TemplateArgument InstArg = ConvertedInstArgs.data()[I];
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001964 if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
Douglas Gregor2fdc5e82011-01-05 00:13:17 +00001965 Info.Param = makeTemplateParameter(TemplateParams->getParam(I));
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001966 Info.FirstArg = TemplateArgs[I];
1967 Info.SecondArg = InstArg;
1968 return Sema::TDK_NonDeducedMismatch;
1969 }
1970 }
1971
1972 if (Trap.hasErrorOccurred())
1973 return Sema::TDK_SubstitutionFailure;
1974
1975 return Sema::TDK_Success;
1976}
1977
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001978/// \brief Perform template argument deduction to determine whether
1979/// the given template arguments match the given class template
1980/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +00001981Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001982Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001983 const TemplateArgumentList &TemplateArgs,
1984 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001985 // C++ [temp.class.spec.match]p2:
1986 // A partial specialization matches a given actual template
1987 // argument list if the template arguments of the partial
1988 // specialization can be deduced from the actual template argument
1989 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +00001990 SFINAETrap Trap(*this);
Douglas Gregor02024a92010-03-28 02:42:43 +00001991 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001992 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +00001993 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001994 = ::DeduceTemplateArguments(*this,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001995 Partial->getTemplateParameters(),
Mike Stump1eb44332009-09-09 15:08:12 +00001996 Partial->getTemplateArgs(),
Douglas Gregorf67875d2009-06-12 18:26:56 +00001997 TemplateArgs, Info, Deduced))
1998 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +00001999
Douglas Gregor637a4092009-06-10 23:47:09 +00002000 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
Douglas Gregor9b623632010-10-12 23:32:35 +00002001 Deduced.data(), Deduced.size(), Info);
Douglas Gregor637a4092009-06-10 23:47:09 +00002002 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +00002003 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +00002004
Douglas Gregorbb260412009-06-14 08:02:22 +00002005 if (Trap.hasErrorOccurred())
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002006 return Sema::TDK_SubstitutionFailure;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002007
2008 return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002009 Deduced, Info);
Douglas Gregor0b9247f2009-06-04 00:03:07 +00002010}
Douglas Gregor031a5882009-06-13 00:26:55 +00002011
Douglas Gregor41128772009-06-26 23:27:24 +00002012/// \brief Determine whether the given type T is a simple-template-id type.
2013static bool isSimpleTemplateIdType(QualType T) {
Mike Stump1eb44332009-09-09 15:08:12 +00002014 if (const TemplateSpecializationType *Spec
John McCall183700f2009-09-21 23:43:11 +00002015 = T->getAs<TemplateSpecializationType>())
Douglas Gregor41128772009-06-26 23:27:24 +00002016 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Douglas Gregor41128772009-06-26 23:27:24 +00002018 return false;
2019}
Douglas Gregor83314aa2009-07-08 20:55:45 +00002020
2021/// \brief Substitute the explicitly-provided template arguments into the
2022/// given function template according to C++ [temp.arg.explicit].
2023///
2024/// \param FunctionTemplate the function template into which the explicit
2025/// template arguments will be substituted.
2026///
Mike Stump1eb44332009-09-09 15:08:12 +00002027/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor83314aa2009-07-08 20:55:45 +00002028/// arguments.
2029///
Mike Stump1eb44332009-09-09 15:08:12 +00002030/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor83314aa2009-07-08 20:55:45 +00002031/// with the converted and checked explicit template arguments.
2032///
Mike Stump1eb44332009-09-09 15:08:12 +00002033/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor83314aa2009-07-08 20:55:45 +00002034/// parameters.
2035///
2036/// \param FunctionType if non-NULL, the result type of the function template
2037/// will also be instantiated and the pointed-to value will be updated with
2038/// the instantiated function type.
2039///
2040/// \param Info if substitution fails for any reason, this object will be
2041/// populated with more information about the failure.
2042///
2043/// \returns TDK_Success if substitution was successful, or some failure
2044/// condition.
2045Sema::TemplateDeductionResult
2046Sema::SubstituteExplicitTemplateArguments(
2047 FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor67714232011-03-03 02:41:12 +00002048 TemplateArgumentListInfo &ExplicitTemplateArgs,
Douglas Gregor02024a92010-03-28 02:42:43 +00002049 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002050 llvm::SmallVectorImpl<QualType> &ParamTypes,
2051 QualType *FunctionType,
2052 TemplateDeductionInfo &Info) {
2053 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2054 TemplateParameterList *TemplateParams
2055 = FunctionTemplate->getTemplateParameters();
2056
John McCalld5532b62009-11-23 01:53:49 +00002057 if (ExplicitTemplateArgs.size() == 0) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00002058 // No arguments to substitute; just copy over the parameter types and
2059 // fill in the function type.
2060 for (FunctionDecl::param_iterator P = Function->param_begin(),
2061 PEnd = Function->param_end();
2062 P != PEnd;
2063 ++P)
2064 ParamTypes.push_back((*P)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002065
Douglas Gregor83314aa2009-07-08 20:55:45 +00002066 if (FunctionType)
2067 *FunctionType = Function->getType();
2068 return TDK_Success;
2069 }
Mike Stump1eb44332009-09-09 15:08:12 +00002070
Douglas Gregor83314aa2009-07-08 20:55:45 +00002071 // Substitution of the explicit template arguments into a function template
2072 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002073 SFINAETrap Trap(*this);
2074
Douglas Gregor83314aa2009-07-08 20:55:45 +00002075 // C++ [temp.arg.explicit]p3:
Mike Stump1eb44332009-09-09 15:08:12 +00002076 // Template arguments that are present shall be specified in the
2077 // declaration order of their corresponding template-parameters. The
Douglas Gregor83314aa2009-07-08 20:55:45 +00002078 // template argument list shall not specify more template-arguments than
Mike Stump1eb44332009-09-09 15:08:12 +00002079 // there are corresponding template-parameters.
Douglas Gregor910f8002010-11-07 23:05:16 +00002080 llvm::SmallVector<TemplateArgument, 4> Builder;
Mike Stump1eb44332009-09-09 15:08:12 +00002081
2082 // Enter a new template instantiation context where we check the
Douglas Gregor83314aa2009-07-08 20:55:45 +00002083 // explicitly-specified template arguments against this function template,
2084 // and then substitute them into the function parameter types.
Mike Stump1eb44332009-09-09 15:08:12 +00002085 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00002086 FunctionTemplate, Deduced.data(), Deduced.size(),
Douglas Gregor9b623632010-10-12 23:32:35 +00002087 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution,
2088 Info);
Douglas Gregor83314aa2009-07-08 20:55:45 +00002089 if (Inst)
2090 return TDK_InstantiationDepth;
Mike Stump1eb44332009-09-09 15:08:12 +00002091
Douglas Gregor83314aa2009-07-08 20:55:45 +00002092 if (CheckTemplateArgumentList(FunctionTemplate,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002093 SourceLocation(),
John McCalld5532b62009-11-23 01:53:49 +00002094 ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002095 true,
Douglas Gregorf1a84452010-05-08 19:15:54 +00002096 Builder) || Trap.hasErrorOccurred()) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002097 unsigned Index = Builder.size();
Douglas Gregorfe52c912010-05-09 01:26:06 +00002098 if (Index >= TemplateParams->size())
2099 Index = TemplateParams->size() - 1;
2100 Info.Param = makeTemplateParameter(TemplateParams->getParam(Index));
Douglas Gregor83314aa2009-07-08 20:55:45 +00002101 return TDK_InvalidExplicitArguments;
Douglas Gregorf1a84452010-05-08 19:15:54 +00002102 }
Mike Stump1eb44332009-09-09 15:08:12 +00002103
Douglas Gregor83314aa2009-07-08 20:55:45 +00002104 // Form the template argument list from the explicitly-specified
2105 // template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00002106 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor910f8002010-11-07 23:05:16 +00002107 = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
Douglas Gregor83314aa2009-07-08 20:55:45 +00002108 Info.reset(ExplicitArgumentList);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002109
John McCalldf41f182010-10-12 19:40:14 +00002110 // Template argument deduction and the final substitution should be
2111 // done in the context of the templated declaration. Explicit
2112 // argument substitution, on the other hand, needs to happen in the
2113 // calling context.
2114 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
2115
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002116 // If we deduced template arguments for a template parameter pack,
Douglas Gregord3731192011-01-10 07:32:04 +00002117 // note that the template argument pack is partially substituted and record
2118 // the explicit template arguments. They'll be used as part of deduction
2119 // for this template parameter pack.
Douglas Gregord3731192011-01-10 07:32:04 +00002120 for (unsigned I = 0, N = Builder.size(); I != N; ++I) {
2121 const TemplateArgument &Arg = Builder[I];
2122 if (Arg.getKind() == TemplateArgument::Pack) {
Douglas Gregord3731192011-01-10 07:32:04 +00002123 CurrentInstantiationScope->SetPartiallySubstitutedPack(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002124 TemplateParams->getParam(I),
Douglas Gregord3731192011-01-10 07:32:04 +00002125 Arg.pack_begin(),
2126 Arg.pack_size());
2127 break;
2128 }
2129 }
2130
Douglas Gregor83314aa2009-07-08 20:55:45 +00002131 // Instantiate the types of each of the function parameters given the
2132 // explicitly-specified template arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002133 if (SubstParmTypes(Function->getLocation(),
Douglas Gregora009b592011-01-07 00:20:55 +00002134 Function->param_begin(), Function->getNumParams(),
2135 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
2136 ParamTypes))
2137 return TDK_SubstitutionFailure;
Douglas Gregor83314aa2009-07-08 20:55:45 +00002138
2139 // If the caller wants a full function type back, instantiate the return
2140 // type and form that function type.
2141 if (FunctionType) {
2142 // FIXME: exception-specifications?
Mike Stump1eb44332009-09-09 15:08:12 +00002143 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00002144 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor83314aa2009-07-08 20:55:45 +00002145 assert(Proto && "Function template does not have a prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00002146
2147 QualType ResultType
Douglas Gregor357bbd02009-08-28 20:50:45 +00002148 = SubstType(Proto->getResultType(),
2149 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
2150 Function->getTypeSpecStartLoc(),
2151 Function->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00002152 if (ResultType.isNull() || Trap.hasErrorOccurred())
2153 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00002154
2155 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002156 ParamTypes.data(), ParamTypes.size(),
2157 Proto->isVariadic(),
2158 Proto->getTypeQuals(),
Douglas Gregorc938c162011-01-26 05:01:58 +00002159 Proto->getRefQualifier(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00002160 Function->getLocation(),
Eli Friedmanfa869542010-08-05 02:54:05 +00002161 Function->getDeclName(),
2162 Proto->getExtInfo());
Douglas Gregor83314aa2009-07-08 20:55:45 +00002163 if (FunctionType->isNull() || Trap.hasErrorOccurred())
2164 return TDK_SubstitutionFailure;
2165 }
Mike Stump1eb44332009-09-09 15:08:12 +00002166
Douglas Gregor83314aa2009-07-08 20:55:45 +00002167 // C++ [temp.arg.explicit]p2:
Mike Stump1eb44332009-09-09 15:08:12 +00002168 // Trailing template arguments that can be deduced (14.8.2) may be
2169 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor83314aa2009-07-08 20:55:45 +00002170 // template arguments can be deduced, they may all be omitted; in this
2171 // case, the empty template argument list <> itself may also be omitted.
2172 //
Douglas Gregord3731192011-01-10 07:32:04 +00002173 // Take all of the explicitly-specified arguments and put them into
2174 // the set of deduced template arguments. Explicitly-specified
2175 // parameter packs, however, will be set to NULL since the deduction
2176 // mechanisms handle explicitly-specified argument packs directly.
Douglas Gregor83314aa2009-07-08 20:55:45 +00002177 Deduced.reserve(TemplateParams->size());
Douglas Gregord3731192011-01-10 07:32:04 +00002178 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) {
2179 const TemplateArgument &Arg = ExplicitArgumentList->get(I);
2180 if (Arg.getKind() == TemplateArgument::Pack)
2181 Deduced.push_back(DeducedTemplateArgument());
2182 else
2183 Deduced.push_back(Arg);
2184 }
Mike Stump1eb44332009-09-09 15:08:12 +00002185
Douglas Gregor83314aa2009-07-08 20:55:45 +00002186 return TDK_Success;
2187}
2188
Mike Stump1eb44332009-09-09 15:08:12 +00002189/// \brief Finish template argument deduction for a function template,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002190/// checking the deduced template arguments for completeness and forming
2191/// the function template specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00002192Sema::TemplateDeductionResult
Douglas Gregor83314aa2009-07-08 20:55:45 +00002193Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor02024a92010-03-28 02:42:43 +00002194 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
2195 unsigned NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002196 FunctionDecl *&Specialization,
2197 TemplateDeductionInfo &Info) {
2198 TemplateParameterList *TemplateParams
2199 = FunctionTemplate->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00002200
Douglas Gregor83314aa2009-07-08 20:55:45 +00002201 // Template argument deduction for function templates in a SFINAE context.
2202 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002203 SFINAETrap Trap(*this);
2204
Douglas Gregor83314aa2009-07-08 20:55:45 +00002205 // Enter a new template instantiation context while we instantiate the
2206 // actual function declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002207 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00002208 FunctionTemplate, Deduced.data(), Deduced.size(),
Douglas Gregor9b623632010-10-12 23:32:35 +00002209 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution,
2210 Info);
Douglas Gregor83314aa2009-07-08 20:55:45 +00002211 if (Inst)
Mike Stump1eb44332009-09-09 15:08:12 +00002212 return TDK_InstantiationDepth;
2213
John McCall96db3102010-04-29 01:18:58 +00002214 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
John McCallf5813822010-04-29 00:35:03 +00002215
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002216 // C++ [temp.deduct.type]p2:
2217 // [...] or if any template argument remains neither deduced nor
2218 // explicitly specified, template argument deduction fails.
Douglas Gregor910f8002010-11-07 23:05:16 +00002219 llvm::SmallVector<TemplateArgument, 4> Builder;
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002220 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
2221 NamedDecl *Param = TemplateParams->getParam(I);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002222
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002223 if (!Deduced[I].isNull()) {
Douglas Gregor3273b0c2010-10-12 18:51:08 +00002224 if (I < NumExplicitlySpecified) {
Douglas Gregor02024a92010-03-28 02:42:43 +00002225 // We have already fully type-checked and converted this
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002226 // argument, because it was explicitly-specified. Just record the
Douglas Gregor3273b0c2010-10-12 18:51:08 +00002227 // presence of this argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002228 Builder.push_back(Deduced[I]);
Douglas Gregor02024a92010-03-28 02:42:43 +00002229 continue;
2230 }
2231
2232 // We have deduced this argument, so it still needs to be
2233 // checked and converted.
2234
2235 // First, for a non-type template parameter type that is
2236 // initialized by a declaration, we need the type of the
2237 // corresponding non-type template parameter.
2238 QualType NTTPType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002239 if (NonTypeTemplateParmDecl *NTTP
2240 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002241 NTTPType = NTTP->getType();
2242 if (NTTPType->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002243 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002244 Builder.data(), Builder.size());
2245 NTTPType = SubstType(NTTPType,
2246 MultiLevelTemplateArgumentList(TemplateArgs),
2247 NTTP->getLocation(),
2248 NTTP->getDeclName());
2249 if (NTTPType.isNull()) {
2250 Info.Param = makeTemplateParameter(Param);
2251 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002252 Info.reset(TemplateArgumentList::CreateCopy(Context,
2253 Builder.data(),
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002254 Builder.size()));
2255 return TDK_SubstitutionFailure;
Douglas Gregor02024a92010-03-28 02:42:43 +00002256 }
2257 }
2258 }
2259
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002260 if (ConvertDeducedTemplateArgument(*this, Param, Deduced[I],
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002261 FunctionTemplate, NTTPType, 0, Info,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002262 true, Builder)) {
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002263 Info.Param = makeTemplateParameter(Param);
Douglas Gregor910f8002010-11-07 23:05:16 +00002264 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002265 Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
2266 Builder.size()));
Douglas Gregor02024a92010-03-28 02:42:43 +00002267 return TDK_SubstitutionFailure;
2268 }
2269
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002270 continue;
2271 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002272
Douglas Gregorea6c96f2010-12-23 01:52:01 +00002273 // C++0x [temp.arg.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002274 // A trailing template parameter pack (14.5.3) not otherwise deduced will
Douglas Gregorea6c96f2010-12-23 01:52:01 +00002275 // be deduced to an empty sequence of template arguments.
2276 // FIXME: Where did the word "trailing" come from?
2277 if (Param->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002278 // We may have had explicitly-specified template arguments for this
2279 // template parameter pack. If so, our empty deduction extends the
2280 // explicitly-specified set (C++0x [temp.arg.explicit]p9).
2281 const TemplateArgument *ExplicitArgs;
2282 unsigned NumExplicitArgs;
2283 if (CurrentInstantiationScope->getPartiallySubstitutedPack(&ExplicitArgs,
2284 &NumExplicitArgs)
2285 == Param)
2286 Builder.push_back(TemplateArgument(ExplicitArgs, NumExplicitArgs));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002287 else
Douglas Gregord3731192011-01-10 07:32:04 +00002288 Builder.push_back(TemplateArgument(0, 0));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002289
Douglas Gregorea6c96f2010-12-23 01:52:01 +00002290 continue;
2291 }
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002292
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002293 // Substitute into the default template argument, if available.
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002294 TemplateArgumentLoc DefArg
2295 = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
2296 FunctionTemplate->getLocation(),
2297 FunctionTemplate->getSourceRange().getEnd(),
2298 Param,
2299 Builder);
2300
2301 // If there was no default argument, deduction is incomplete.
2302 if (DefArg.getArgument().isNull()) {
2303 Info.Param = makeTemplateParameter(
2304 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
2305 return TDK_Incomplete;
2306 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002307
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002308 // Check whether we can actually use the default argument.
2309 if (CheckTemplateArgument(Param, DefArg,
2310 FunctionTemplate,
2311 FunctionTemplate->getLocation(),
2312 FunctionTemplate->getSourceRange().getEnd(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002313 0, Builder,
Douglas Gregor02024a92010-03-28 02:42:43 +00002314 CTAK_Deduced)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002315 Info.Param = makeTemplateParameter(
2316 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregor910f8002010-11-07 23:05:16 +00002317 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002318 Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002319 Builder.size()));
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002320 return TDK_SubstitutionFailure;
2321 }
2322
2323 // If we get here, we successfully used the default template argument.
2324 }
2325
2326 // Form the template argument list from the deduced template arguments.
2327 TemplateArgumentList *DeducedArgumentList
Douglas Gregor910f8002010-11-07 23:05:16 +00002328 = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002329 Info.reset(DeducedArgumentList);
2330
Mike Stump1eb44332009-09-09 15:08:12 +00002331 // Substitute the deduced template arguments into the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00002332 // declaration to produce the function template specialization.
Douglas Gregord4598a22010-04-28 04:52:24 +00002333 DeclContext *Owner = FunctionTemplate->getDeclContext();
2334 if (FunctionTemplate->getFriendObjectKind())
2335 Owner = FunctionTemplate->getLexicalDeclContext();
Douglas Gregor83314aa2009-07-08 20:55:45 +00002336 Specialization = cast_or_null<FunctionDecl>(
Douglas Gregord4598a22010-04-28 04:52:24 +00002337 SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner,
Douglas Gregor357bbd02009-08-28 20:50:45 +00002338 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor83314aa2009-07-08 20:55:45 +00002339 if (!Specialization)
2340 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00002341
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002342 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
Douglas Gregorf8825742009-09-15 18:26:13 +00002343 FunctionTemplate->getCanonicalDecl());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002344
Mike Stump1eb44332009-09-09 15:08:12 +00002345 // If the template argument list is owned by the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00002346 // specialization, release it.
Douglas Gregorec20f462010-05-08 20:07:26 +00002347 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList &&
2348 !Trap.hasErrorOccurred())
Douglas Gregor83314aa2009-07-08 20:55:45 +00002349 Info.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002350
Douglas Gregor83314aa2009-07-08 20:55:45 +00002351 // There may have been an error that did not prevent us from constructing a
2352 // declaration. Mark the declaration invalid and return with a substitution
2353 // failure.
2354 if (Trap.hasErrorOccurred()) {
2355 Specialization->setInvalidDecl(true);
2356 return TDK_SubstitutionFailure;
2357 }
Mike Stump1eb44332009-09-09 15:08:12 +00002358
Douglas Gregor9b623632010-10-12 23:32:35 +00002359 // If we suppressed any diagnostics while performing template argument
2360 // deduction, and if we haven't already instantiated this declaration,
2361 // keep track of these diagnostics. They'll be emitted if this specialization
2362 // is actually used.
2363 if (Info.diag_begin() != Info.diag_end()) {
2364 llvm::DenseMap<Decl *, llvm::SmallVector<PartialDiagnosticAt, 1> >::iterator
2365 Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl());
2366 if (Pos == SuppressedDiagnostics.end())
2367 SuppressedDiagnostics[Specialization->getCanonicalDecl()]
2368 .append(Info.diag_begin(), Info.diag_end());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002369 }
Douglas Gregor9b623632010-10-12 23:32:35 +00002370
Mike Stump1eb44332009-09-09 15:08:12 +00002371 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00002372}
2373
John McCall9c72c602010-08-27 09:08:28 +00002374/// Gets the type of a function for template-argument-deducton
2375/// purposes when it's considered as part of an overload set.
John McCalleff92132010-02-02 02:21:27 +00002376static QualType GetTypeOfFunction(ASTContext &Context,
John McCall9c72c602010-08-27 09:08:28 +00002377 const OverloadExpr::FindResult &R,
John McCalleff92132010-02-02 02:21:27 +00002378 FunctionDecl *Fn) {
John McCalleff92132010-02-02 02:21:27 +00002379 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
John McCall9c72c602010-08-27 09:08:28 +00002380 if (Method->isInstance()) {
2381 // An instance method that's referenced in a form that doesn't
2382 // look like a member pointer is just invalid.
2383 if (!R.HasFormOfMemberPointer) return QualType();
2384
John McCalleff92132010-02-02 02:21:27 +00002385 return Context.getMemberPointerType(Fn->getType(),
2386 Context.getTypeDeclType(Method->getParent()).getTypePtr());
John McCall9c72c602010-08-27 09:08:28 +00002387 }
2388
2389 if (!R.IsAddressOfOperand) return Fn->getType();
John McCalleff92132010-02-02 02:21:27 +00002390 return Context.getPointerType(Fn->getType());
2391}
2392
2393/// Apply the deduction rules for overload sets.
2394///
2395/// \return the null type if this argument should be treated as an
2396/// undeduced context
2397static QualType
2398ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
Douglas Gregor75f21af2010-08-30 21:04:23 +00002399 Expr *Arg, QualType ParamType,
2400 bool ParamWasReference) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002401
John McCall9c72c602010-08-27 09:08:28 +00002402 OverloadExpr::FindResult R = OverloadExpr::find(Arg);
John McCalleff92132010-02-02 02:21:27 +00002403
John McCall9c72c602010-08-27 09:08:28 +00002404 OverloadExpr *Ovl = R.Expression;
John McCalleff92132010-02-02 02:21:27 +00002405
Douglas Gregor75f21af2010-08-30 21:04:23 +00002406 // C++0x [temp.deduct.call]p4
2407 unsigned TDF = 0;
2408 if (ParamWasReference)
2409 TDF |= TDF_ParamWithReferenceType;
2410 if (R.IsAddressOfOperand)
2411 TDF |= TDF_IgnoreQualifiers;
2412
John McCalleff92132010-02-02 02:21:27 +00002413 // If there were explicit template arguments, we can only find
2414 // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
2415 // unambiguously name a full specialization.
John McCall7bb12da2010-02-02 06:20:04 +00002416 if (Ovl->hasExplicitTemplateArgs()) {
John McCalleff92132010-02-02 02:21:27 +00002417 // But we can still look for an explicit specialization.
2418 if (FunctionDecl *ExplicitSpec
John McCall7bb12da2010-02-02 06:20:04 +00002419 = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
John McCall9c72c602010-08-27 09:08:28 +00002420 return GetTypeOfFunction(S.Context, R, ExplicitSpec);
John McCalleff92132010-02-02 02:21:27 +00002421 return QualType();
2422 }
2423
2424 // C++0x [temp.deduct.call]p6:
2425 // When P is a function type, pointer to function type, or pointer
2426 // to member function type:
2427
2428 if (!ParamType->isFunctionType() &&
2429 !ParamType->isFunctionPointerType() &&
2430 !ParamType->isMemberFunctionPointerType())
2431 return QualType();
2432
2433 QualType Match;
John McCall7bb12da2010-02-02 06:20:04 +00002434 for (UnresolvedSetIterator I = Ovl->decls_begin(),
2435 E = Ovl->decls_end(); I != E; ++I) {
John McCalleff92132010-02-02 02:21:27 +00002436 NamedDecl *D = (*I)->getUnderlyingDecl();
2437
2438 // - If the argument is an overload set containing one or more
2439 // function templates, the parameter is treated as a
2440 // non-deduced context.
2441 if (isa<FunctionTemplateDecl>(D))
2442 return QualType();
2443
2444 FunctionDecl *Fn = cast<FunctionDecl>(D);
John McCall9c72c602010-08-27 09:08:28 +00002445 QualType ArgType = GetTypeOfFunction(S.Context, R, Fn);
2446 if (ArgType.isNull()) continue;
John McCalleff92132010-02-02 02:21:27 +00002447
Douglas Gregor75f21af2010-08-30 21:04:23 +00002448 // Function-to-pointer conversion.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002449 if (!ParamWasReference && ParamType->isPointerType() &&
Douglas Gregor75f21af2010-08-30 21:04:23 +00002450 ArgType->isFunctionType())
2451 ArgType = S.Context.getPointerType(ArgType);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002452
John McCalleff92132010-02-02 02:21:27 +00002453 // - If the argument is an overload set (not containing function
2454 // templates), trial argument deduction is attempted using each
2455 // of the members of the set. If deduction succeeds for only one
2456 // of the overload set members, that member is used as the
2457 // argument value for the deduction. If deduction succeeds for
2458 // more than one member of the overload set the parameter is
2459 // treated as a non-deduced context.
2460
2461 // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
2462 // Type deduction is done independently for each P/A pair, and
2463 // the deduced template argument values are then combined.
2464 // So we do not reject deductions which were made elsewhere.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002465 llvm::SmallVector<DeducedTemplateArgument, 8>
Douglas Gregor02024a92010-03-28 02:42:43 +00002466 Deduced(TemplateParams->size());
John McCall2a7fb272010-08-25 05:32:35 +00002467 TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
John McCalleff92132010-02-02 02:21:27 +00002468 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002469 = DeduceTemplateArguments(S, TemplateParams,
John McCalleff92132010-02-02 02:21:27 +00002470 ParamType, ArgType,
2471 Info, Deduced, TDF);
2472 if (Result) continue;
2473 if (!Match.isNull()) return QualType();
2474 Match = ArgType;
2475 }
2476
2477 return Match;
2478}
2479
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002480/// \brief Perform the adjustments to the parameter and argument types
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002481/// described in C++ [temp.deduct.call].
2482///
2483/// \returns true if the caller should not attempt to perform any template
2484/// argument deduction based on this P/A pair.
2485static bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S,
2486 TemplateParameterList *TemplateParams,
2487 QualType &ParamType,
2488 QualType &ArgType,
2489 Expr *Arg,
2490 unsigned &TDF) {
2491 // C++0x [temp.deduct.call]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00002492 // If P is a cv-qualified type, the top level cv-qualifiers of P's type
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002493 // are ignored for type deduction.
Douglas Gregora459cc22011-04-27 23:34:22 +00002494 if (ParamType.hasQualifiers())
2495 ParamType = ParamType.getUnqualifiedType();
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002496 const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>();
2497 if (ParamRefType) {
Richard Smith34b41d92011-02-20 03:19:35 +00002498 QualType PointeeType = ParamRefType->getPointeeType();
2499
Douglas Gregor2ad746a2011-01-21 05:18:22 +00002500 // [C++0x] If P is an rvalue reference to a cv-unqualified
2501 // template parameter and the argument is an lvalue, the type
2502 // "lvalue reference to A" is used in place of A for type
2503 // deduction.
Richard Smith34b41d92011-02-20 03:19:35 +00002504 if (isa<RValueReferenceType>(ParamType)) {
2505 if (!PointeeType.getQualifiers() &&
2506 isa<TemplateTypeParmType>(PointeeType) &&
Douglas Gregor2ad746a2011-01-21 05:18:22 +00002507 Arg->Classify(S.Context).isLValue())
2508 ArgType = S.Context.getLValueReferenceType(ArgType);
2509 }
2510
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002511 // [...] If P is a reference type, the type referred to by P is used
2512 // for type deduction.
Richard Smith34b41d92011-02-20 03:19:35 +00002513 ParamType = PointeeType;
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002514 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002515
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002516 // Overload sets usually make this parameter an undeduced
2517 // context, but there are sometimes special circumstances.
2518 if (ArgType == S.Context.OverloadTy) {
2519 ArgType = ResolveOverloadForDeduction(S, TemplateParams,
2520 Arg, ParamType,
2521 ParamRefType != 0);
2522 if (ArgType.isNull())
2523 return true;
2524 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002525
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002526 if (ParamRefType) {
2527 // C++0x [temp.deduct.call]p3:
2528 // [...] If P is of the form T&&, where T is a template parameter, and
2529 // the argument is an lvalue, the type A& is used in place of A for
2530 // type deduction.
2531 if (ParamRefType->isRValueReferenceType() &&
2532 ParamRefType->getAs<TemplateTypeParmType>() &&
2533 Arg->isLValue())
2534 ArgType = S.Context.getLValueReferenceType(ArgType);
2535 } else {
2536 // C++ [temp.deduct.call]p2:
2537 // If P is not a reference type:
2538 // - If A is an array type, the pointer type produced by the
2539 // array-to-pointer standard conversion (4.2) is used in place of
2540 // A for type deduction; otherwise,
2541 if (ArgType->isArrayType())
2542 ArgType = S.Context.getArrayDecayedType(ArgType);
2543 // - If A is a function type, the pointer type produced by the
2544 // function-to-pointer standard conversion (4.3) is used in place
2545 // of A for type deduction; otherwise,
2546 else if (ArgType->isFunctionType())
2547 ArgType = S.Context.getPointerType(ArgType);
2548 else {
NAKAMURA Takumi00995302011-01-27 07:09:49 +00002549 // - If A is a cv-qualified type, the top level cv-qualifiers of A's
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002550 // type are ignored for type deduction.
Douglas Gregora459cc22011-04-27 23:34:22 +00002551 ArgType = ArgType.getUnqualifiedType();
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002552 }
2553 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002554
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002555 // C++0x [temp.deduct.call]p4:
2556 // In general, the deduction process attempts to find template argument
2557 // values that will make the deduced A identical to A (after the type A
2558 // is transformed as described above). [...]
2559 TDF = TDF_SkipNonDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002560
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002561 // - If the original P is a reference type, the deduced A (i.e., the
2562 // type referred to by the reference) can be more cv-qualified than
2563 // the transformed A.
2564 if (ParamRefType)
2565 TDF |= TDF_ParamWithReferenceType;
2566 // - The transformed A can be another pointer or pointer to member
2567 // type that can be converted to the deduced A via a qualification
2568 // conversion (4.4).
2569 if (ArgType->isPointerType() || ArgType->isMemberPointerType() ||
2570 ArgType->isObjCObjectPointerType())
2571 TDF |= TDF_IgnoreQualifiers;
2572 // - If P is a class and P has the form simple-template-id, then the
2573 // transformed A can be a derived class of the deduced A. Likewise,
2574 // if P is a pointer to a class of the form simple-template-id, the
2575 // transformed A can be a pointer to a derived class pointed to by
2576 // the deduced A.
2577 if (isSimpleTemplateIdType(ParamType) ||
2578 (isa<PointerType>(ParamType) &&
2579 isSimpleTemplateIdType(
2580 ParamType->getAs<PointerType>()->getPointeeType())))
2581 TDF |= TDF_DerivedClass;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002582
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002583 return false;
2584}
2585
Douglas Gregore53060f2009-06-25 22:08:12 +00002586/// \brief Perform template argument deduction from a function call
2587/// (C++ [temp.deduct.call]).
2588///
2589/// \param FunctionTemplate the function template for which we are performing
2590/// template argument deduction.
2591///
Douglas Gregor48026d22010-01-11 18:40:55 +00002592/// \param ExplicitTemplateArguments the explicit template arguments provided
2593/// for this call.
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002594///
Douglas Gregore53060f2009-06-25 22:08:12 +00002595/// \param Args the function call arguments
2596///
2597/// \param NumArgs the number of arguments in Args
2598///
Douglas Gregor48026d22010-01-11 18:40:55 +00002599/// \param Name the name of the function being called. This is only significant
2600/// when the function template is a conversion function template, in which
2601/// case this routine will also perform template argument deduction based on
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002602/// the function to which
Douglas Gregor48026d22010-01-11 18:40:55 +00002603///
Douglas Gregore53060f2009-06-25 22:08:12 +00002604/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00002605/// this will be set to the function template specialization produced by
Douglas Gregore53060f2009-06-25 22:08:12 +00002606/// template argument deduction.
2607///
2608/// \param Info the argument will be updated to provide additional information
2609/// about template argument deduction.
2610///
2611/// \returns the result of template argument deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00002612Sema::TemplateDeductionResult
2613Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor67714232011-03-03 02:41:12 +00002614 TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregore53060f2009-06-25 22:08:12 +00002615 Expr **Args, unsigned NumArgs,
2616 FunctionDecl *&Specialization,
2617 TemplateDeductionInfo &Info) {
2618 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002619
Douglas Gregore53060f2009-06-25 22:08:12 +00002620 // C++ [temp.deduct.call]p1:
2621 // Template argument deduction is done by comparing each function template
2622 // parameter type (call it P) with the type of the corresponding argument
2623 // of the call (call it A) as described below.
2624 unsigned CheckArgs = NumArgs;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002625 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregore53060f2009-06-25 22:08:12 +00002626 return TDK_TooFewArguments;
2627 else if (NumArgs > Function->getNumParams()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002628 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00002629 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002630 if (Proto->isTemplateVariadic())
2631 /* Do nothing */;
2632 else if (Proto->isVariadic())
2633 CheckArgs = Function->getNumParams();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002634 else
Douglas Gregore53060f2009-06-25 22:08:12 +00002635 return TDK_TooManyArguments;
Douglas Gregore53060f2009-06-25 22:08:12 +00002636 }
Mike Stump1eb44332009-09-09 15:08:12 +00002637
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002638 // The types of the parameters from which we will perform template argument
2639 // deduction.
John McCall2a7fb272010-08-25 05:32:35 +00002640 LocalInstantiationScope InstScope(*this);
Douglas Gregore53060f2009-06-25 22:08:12 +00002641 TemplateParameterList *TemplateParams
2642 = FunctionTemplate->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00002643 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002644 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregor02024a92010-03-28 02:42:43 +00002645 unsigned NumExplicitlySpecified = 0;
John McCalld5532b62009-11-23 01:53:49 +00002646 if (ExplicitTemplateArgs) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00002647 TemplateDeductionResult Result =
2648 SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00002649 *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002650 Deduced,
2651 ParamTypes,
2652 0,
2653 Info);
2654 if (Result)
2655 return Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002656
2657 NumExplicitlySpecified = Deduced.size();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002658 } else {
2659 // Just fill in the parameter types from the function declaration.
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002660 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002661 ParamTypes.push_back(Function->getParamDecl(I)->getType());
2662 }
Mike Stump1eb44332009-09-09 15:08:12 +00002663
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002664 // Deduce template arguments from the function parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00002665 Deduced.resize(TemplateParams->size());
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002666 unsigned ArgIdx = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002667 for (unsigned ParamIdx = 0, NumParams = ParamTypes.size();
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002668 ParamIdx != NumParams; ++ParamIdx) {
2669 QualType ParamType = ParamTypes[ParamIdx];
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002670
2671 const PackExpansionType *ParamExpansion
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002672 = dyn_cast<PackExpansionType>(ParamType);
2673 if (!ParamExpansion) {
2674 // Simple case: matching a function parameter to a function argument.
2675 if (ArgIdx >= CheckArgs)
2676 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002677
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002678 Expr *Arg = Args[ArgIdx++];
2679 QualType ArgType = Arg->getType();
2680 unsigned TDF = 0;
2681 if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams,
2682 ParamType, ArgType, Arg,
2683 TDF))
2684 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002685
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002686 if (TemplateDeductionResult Result
2687 = ::DeduceTemplateArguments(*this, TemplateParams,
2688 ParamType, ArgType, Info, Deduced,
2689 TDF))
2690 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002691
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002692 // FIXME: we need to check that the deduced A is the same as A,
2693 // modulo the various allowed differences.
2694 continue;
Douglas Gregor75f21af2010-08-30 21:04:23 +00002695 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002696
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002697 // C++0x [temp.deduct.call]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002698 // For a function parameter pack that occurs at the end of the
2699 // parameter-declaration-list, the type A of each remaining argument of
2700 // the call is compared with the type P of the declarator-id of the
2701 // function parameter pack. Each comparison deduces template arguments
2702 // for subsequent positions in the template parameter packs expanded by
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00002703 // the function parameter pack. For a function parameter pack that does
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002704 // not occur at the end of the parameter-declaration-list, the type of
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00002705 // the parameter pack is a non-deduced context.
2706 if (ParamIdx + 1 < NumParams)
2707 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002708
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002709 QualType ParamPattern = ParamExpansion->getPattern();
2710 llvm::SmallVector<unsigned, 2> PackIndices;
2711 {
2712 llvm::BitVector SawIndices(TemplateParams->size());
2713 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2714 collectUnexpandedParameterPacks(ParamPattern, Unexpanded);
2715 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
2716 unsigned Depth, Index;
2717 llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
2718 if (Depth == 0 && !SawIndices[Index]) {
2719 SawIndices[Index] = true;
2720 PackIndices.push_back(Index);
2721 }
Douglas Gregore53060f2009-06-25 22:08:12 +00002722 }
2723 }
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002724 assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002725
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002726 // Keep track of the deduced template arguments for each parameter pack
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002727 // expanded by this pack expansion (the outer index) and for each
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002728 // template argument (the inner SmallVectors).
2729 llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2>
Douglas Gregord3731192011-01-10 07:32:04 +00002730 NewlyDeducedPacks(PackIndices.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002731 llvm::SmallVector<DeducedTemplateArgument, 2>
Douglas Gregord3731192011-01-10 07:32:04 +00002732 SavedPacks(PackIndices.size());
Douglas Gregor54293852011-01-10 17:35:05 +00002733 PrepareArgumentPackDeduction(*this, Deduced, PackIndices, SavedPacks,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002734 NewlyDeducedPacks);
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002735 bool HasAnyArguments = false;
2736 for (; ArgIdx < NumArgs; ++ArgIdx) {
2737 HasAnyArguments = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002738
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002739 ParamType = ParamPattern;
2740 Expr *Arg = Args[ArgIdx];
2741 QualType ArgType = Arg->getType();
2742 unsigned TDF = 0;
2743 if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams,
2744 ParamType, ArgType, Arg,
2745 TDF)) {
2746 // We can't actually perform any deduction for this argument, so stop
2747 // deduction at this point.
2748 ++ArgIdx;
2749 break;
2750 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002751
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002752 if (TemplateDeductionResult Result
2753 = ::DeduceTemplateArguments(*this, TemplateParams,
2754 ParamType, ArgType, Info, Deduced,
2755 TDF))
2756 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002757
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002758 // Capture the deduced template arguments for each parameter pack expanded
2759 // by this pack expansion, add them to the list of arguments we've deduced
2760 // for that pack, then clear out the deduced argument.
2761 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
2762 DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
2763 if (!DeducedArg.isNull()) {
2764 NewlyDeducedPacks[I].push_back(DeducedArg);
2765 DeducedArg = DeducedTemplateArgument();
2766 }
2767 }
2768 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002769
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002770 // Build argument packs for each of the parameter packs expanded by this
2771 // pack expansion.
Douglas Gregor0216f812011-01-10 17:53:52 +00002772 if (Sema::TemplateDeductionResult Result
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002773 = FinishArgumentPackDeduction(*this, TemplateParams, HasAnyArguments,
Douglas Gregor0216f812011-01-10 17:53:52 +00002774 Deduced, PackIndices, SavedPacks,
2775 NewlyDeducedPacks, Info))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002776 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002777
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002778 // After we've matching against a parameter pack, we're done.
2779 break;
Douglas Gregore53060f2009-06-25 22:08:12 +00002780 }
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002781
Mike Stump1eb44332009-09-09 15:08:12 +00002782 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor02024a92010-03-28 02:42:43 +00002783 NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002784 Specialization, Info);
Douglas Gregore53060f2009-06-25 22:08:12 +00002785}
2786
Douglas Gregor83314aa2009-07-08 20:55:45 +00002787/// \brief Deduce template arguments when taking the address of a function
Douglas Gregor4b52e252009-12-21 23:17:24 +00002788/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
2789/// a template.
Douglas Gregor83314aa2009-07-08 20:55:45 +00002790///
2791/// \param FunctionTemplate the function template for which we are performing
2792/// template argument deduction.
2793///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002794/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor4b52e252009-12-21 23:17:24 +00002795/// arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00002796///
2797/// \param ArgFunctionType the function type that will be used as the
2798/// "argument" type (A) when performing template argument deduction from the
Douglas Gregor4b52e252009-12-21 23:17:24 +00002799/// function template's function type. This type may be NULL, if there is no
2800/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
Douglas Gregor83314aa2009-07-08 20:55:45 +00002801///
2802/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00002803/// this will be set to the function template specialization produced by
Douglas Gregor83314aa2009-07-08 20:55:45 +00002804/// template argument deduction.
2805///
2806/// \param Info the argument will be updated to provide additional information
2807/// about template argument deduction.
2808///
2809/// \returns the result of template argument deduction.
2810Sema::TemplateDeductionResult
2811Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor67714232011-03-03 02:41:12 +00002812 TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002813 QualType ArgFunctionType,
2814 FunctionDecl *&Specialization,
2815 TemplateDeductionInfo &Info) {
2816 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2817 TemplateParameterList *TemplateParams
2818 = FunctionTemplate->getTemplateParameters();
2819 QualType FunctionType = Function->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002820
Douglas Gregor83314aa2009-07-08 20:55:45 +00002821 // Substitute any explicit template arguments.
John McCall2a7fb272010-08-25 05:32:35 +00002822 LocalInstantiationScope InstScope(*this);
Douglas Gregor02024a92010-03-28 02:42:43 +00002823 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
2824 unsigned NumExplicitlySpecified = 0;
Douglas Gregor83314aa2009-07-08 20:55:45 +00002825 llvm::SmallVector<QualType, 4> ParamTypes;
John McCalld5532b62009-11-23 01:53:49 +00002826 if (ExplicitTemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002827 if (TemplateDeductionResult Result
2828 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00002829 *ExplicitTemplateArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00002830 Deduced, ParamTypes,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002831 &FunctionType, Info))
2832 return Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002833
2834 NumExplicitlySpecified = Deduced.size();
Douglas Gregor83314aa2009-07-08 20:55:45 +00002835 }
2836
2837 // Template argument deduction for function templates in a SFINAE context.
2838 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002839 SFINAETrap Trap(*this);
2840
John McCalleff92132010-02-02 02:21:27 +00002841 Deduced.resize(TemplateParams->size());
2842
Douglas Gregor4b52e252009-12-21 23:17:24 +00002843 if (!ArgFunctionType.isNull()) {
2844 // Deduce template arguments from the function type.
Douglas Gregor4b52e252009-12-21 23:17:24 +00002845 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002846 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor4b52e252009-12-21 23:17:24 +00002847 FunctionType, ArgFunctionType, Info,
Douglas Gregor73b3cf62011-01-25 17:19:08 +00002848 Deduced, TDF_TopLevelParameterTypeList))
Douglas Gregor4b52e252009-12-21 23:17:24 +00002849 return Result;
2850 }
Douglas Gregorfbb6fad2010-09-29 21:14:36 +00002851
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002852 if (TemplateDeductionResult Result
Douglas Gregorfbb6fad2010-09-29 21:14:36 +00002853 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
2854 NumExplicitlySpecified,
2855 Specialization, Info))
2856 return Result;
2857
2858 // If the requested function type does not match the actual type of the
2859 // specialization, template argument deduction fails.
2860 if (!ArgFunctionType.isNull() &&
2861 !Context.hasSameType(ArgFunctionType, Specialization->getType()))
2862 return TDK_NonDeducedMismatch;
2863
2864 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00002865}
2866
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002867/// \brief Deduce template arguments for a templated conversion
2868/// function (C++ [temp.deduct.conv]) and, if successful, produce a
2869/// conversion function template specialization.
2870Sema::TemplateDeductionResult
2871Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
2872 QualType ToType,
2873 CXXConversionDecl *&Specialization,
2874 TemplateDeductionInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00002875 CXXConversionDecl *Conv
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002876 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
2877 QualType FromType = Conv->getConversionType();
2878
2879 // Canonicalize the types for deduction.
2880 QualType P = Context.getCanonicalType(FromType);
2881 QualType A = Context.getCanonicalType(ToType);
2882
Douglas Gregor5453d932011-03-06 09:03:20 +00002883 // C++0x [temp.deduct.conv]p2:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002884 // If P is a reference type, the type referred to by P is used for
2885 // type deduction.
2886 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
2887 P = PRef->getPointeeType();
2888
Douglas Gregor5453d932011-03-06 09:03:20 +00002889 // C++0x [temp.deduct.conv]p4:
2890 // [...] If A is a reference type, the type referred to by A is used
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002891 // for type deduction.
2892 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
Douglas Gregor5453d932011-03-06 09:03:20 +00002893 A = ARef->getPointeeType().getUnqualifiedType();
2894 // C++ [temp.deduct.conv]p3:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002895 //
Mike Stump1eb44332009-09-09 15:08:12 +00002896 // If A is not a reference type:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002897 else {
2898 assert(!A->isReferenceType() && "Reference types were handled above");
2899
2900 // - If P is an array type, the pointer type produced by the
Mike Stump1eb44332009-09-09 15:08:12 +00002901 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002902 // of P for type deduction; otherwise,
2903 if (P->isArrayType())
2904 P = Context.getArrayDecayedType(P);
2905 // - If P is a function type, the pointer type produced by the
2906 // function-to-pointer standard conversion (4.3) is used in
2907 // place of P for type deduction; otherwise,
2908 else if (P->isFunctionType())
2909 P = Context.getPointerType(P);
2910 // - If P is a cv-qualified type, the top level cv-qualifiers of
NAKAMURA Takumi00995302011-01-27 07:09:49 +00002911 // P's type are ignored for type deduction.
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002912 else
2913 P = P.getUnqualifiedType();
2914
Douglas Gregor5453d932011-03-06 09:03:20 +00002915 // C++0x [temp.deduct.conv]p4:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00002916 // If A is a cv-qualified type, the top level cv-qualifiers of A's
Douglas Gregor5453d932011-03-06 09:03:20 +00002917 // type are ignored for type deduction. If A is a reference type, the type
2918 // referred to by A is used for type deduction.
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002919 A = A.getUnqualifiedType();
2920 }
2921
2922 // Template argument deduction for function templates in a SFINAE context.
2923 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002924 SFINAETrap Trap(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002925
2926 // C++ [temp.deduct.conv]p1:
2927 // Template argument deduction is done by comparing the return
2928 // type of the template conversion function (call it P) with the
2929 // type that is required as the result of the conversion (call it
2930 // A) as described in 14.8.2.4.
2931 TemplateParameterList *TemplateParams
2932 = FunctionTemplate->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00002933 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Mike Stump1eb44332009-09-09 15:08:12 +00002934 Deduced.resize(TemplateParams->size());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002935
2936 // C++0x [temp.deduct.conv]p4:
2937 // In general, the deduction process attempts to find template
2938 // argument values that will make the deduced A identical to
2939 // A. However, there are two cases that allow a difference:
2940 unsigned TDF = 0;
2941 // - If the original A is a reference type, A can be more
2942 // cv-qualified than the deduced A (i.e., the type referred to
2943 // by the reference)
2944 if (ToType->isReferenceType())
2945 TDF |= TDF_ParamWithReferenceType;
2946 // - The deduced A can be another pointer or pointer to member
NAKAMURA Takumi00995302011-01-27 07:09:49 +00002947 // type that can be converted to A via a qualification
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002948 // conversion.
2949 //
2950 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
2951 // both P and A are pointers or member pointers. In this case, we
2952 // just ignore cv-qualifiers completely).
2953 if ((P->isPointerType() && A->isPointerType()) ||
2954 (P->isMemberPointerType() && P->isMemberPointerType()))
2955 TDF |= TDF_IgnoreQualifiers;
2956 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002957 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002958 P, A, Info, Deduced, TDF))
2959 return Result;
2960
2961 // FIXME: we need to check that the deduced A is the same as A,
2962 // modulo the various allowed differences.
Mike Stump1eb44332009-09-09 15:08:12 +00002963
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002964 // Finish template argument deduction.
John McCall2a7fb272010-08-25 05:32:35 +00002965 LocalInstantiationScope InstScope(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002966 FunctionDecl *Spec = 0;
2967 TemplateDeductionResult Result
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002968 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec,
Douglas Gregor02024a92010-03-28 02:42:43 +00002969 Info);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002970 Specialization = cast_or_null<CXXConversionDecl>(Spec);
2971 return Result;
2972}
2973
Douglas Gregor4b52e252009-12-21 23:17:24 +00002974/// \brief Deduce template arguments for a function template when there is
2975/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
2976///
2977/// \param FunctionTemplate the function template for which we are performing
2978/// template argument deduction.
2979///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002980/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor4b52e252009-12-21 23:17:24 +00002981/// arguments.
2982///
2983/// \param Specialization if template argument deduction was successful,
2984/// this will be set to the function template specialization produced by
2985/// template argument deduction.
2986///
2987/// \param Info the argument will be updated to provide additional information
2988/// about template argument deduction.
2989///
2990/// \returns the result of template argument deduction.
2991Sema::TemplateDeductionResult
2992Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor67714232011-03-03 02:41:12 +00002993 TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor4b52e252009-12-21 23:17:24 +00002994 FunctionDecl *&Specialization,
2995 TemplateDeductionInfo &Info) {
2996 return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
2997 QualType(), Specialization, Info);
2998}
2999
Richard Smith34b41d92011-02-20 03:19:35 +00003000namespace {
3001 /// Substitute the 'auto' type specifier within a type for a given replacement
3002 /// type.
3003 class SubstituteAutoTransform :
3004 public TreeTransform<SubstituteAutoTransform> {
3005 QualType Replacement;
3006 public:
3007 SubstituteAutoTransform(Sema &SemaRef, QualType Replacement) :
3008 TreeTransform<SubstituteAutoTransform>(SemaRef), Replacement(Replacement) {
3009 }
3010 QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) {
3011 // If we're building the type pattern to deduce against, don't wrap the
3012 // substituted type in an AutoType. Certain template deduction rules
3013 // apply only when a template type parameter appears directly (and not if
3014 // the parameter is found through desugaring). For instance:
3015 // auto &&lref = lvalue;
3016 // must transform into "rvalue reference to T" not "rvalue reference to
3017 // auto type deduced as T" in order for [temp.deduct.call]p3 to apply.
3018 if (isa<TemplateTypeParmType>(Replacement)) {
3019 QualType Result = Replacement;
3020 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
3021 NewTL.setNameLoc(TL.getNameLoc());
3022 return Result;
3023 } else {
3024 QualType Result = RebuildAutoType(Replacement);
3025 AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result);
3026 NewTL.setNameLoc(TL.getNameLoc());
3027 return Result;
3028 }
3029 }
3030 };
3031}
3032
3033/// \brief Deduce the type for an auto type-specifier (C++0x [dcl.spec.auto]p6)
3034///
3035/// \param Type the type pattern using the auto type-specifier.
3036///
3037/// \param Init the initializer for the variable whose type is to be deduced.
3038///
3039/// \param Result if type deduction was successful, this will be set to the
3040/// deduced type. This may still contain undeduced autos if the type is
Richard Smitha085da82011-03-17 16:11:59 +00003041/// dependent. This will be set to null if deduction succeeded, but auto
3042/// substitution failed; the appropriate diagnostic will already have been
3043/// produced in that case.
Richard Smith34b41d92011-02-20 03:19:35 +00003044///
3045/// \returns true if deduction succeeded, false if it failed.
3046bool
Richard Smitha085da82011-03-17 16:11:59 +00003047Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *Init,
3048 TypeSourceInfo *&Result) {
Richard Smith34b41d92011-02-20 03:19:35 +00003049 if (Init->isTypeDependent()) {
3050 Result = Type;
3051 return true;
3052 }
3053
3054 SourceLocation Loc = Init->getExprLoc();
3055
3056 LocalInstantiationScope InstScope(*this);
3057
3058 // Build template<class TemplParam> void Func(FuncParam);
Richard Smith34b41d92011-02-20 03:19:35 +00003059 QualType TemplArg = Context.getTemplateTypeParmType(0, 0, false);
Abramo Bagnara344577e2011-03-06 15:48:19 +00003060 TemplateTypeParmDecl TemplParam(0, SourceLocation(), Loc, 0, false,
3061 TemplArg, false);
Richard Smith483b9f32011-02-21 20:05:19 +00003062 NamedDecl *TemplParamPtr = &TemplParam;
3063 FixedSizeTemplateParameterList<1> TemplateParams(Loc, Loc, &TemplParamPtr,
3064 Loc);
3065
Richard Smitha085da82011-03-17 16:11:59 +00003066 TypeSourceInfo *FuncParamInfo =
Richard Smith34b41d92011-02-20 03:19:35 +00003067 SubstituteAutoTransform(*this, TemplArg).TransformType(Type);
Richard Smitha085da82011-03-17 16:11:59 +00003068 assert(FuncParamInfo && "substituting template parameter for 'auto' failed");
3069 QualType FuncParam = FuncParamInfo->getType();
Richard Smith34b41d92011-02-20 03:19:35 +00003070
3071 // Deduce type of TemplParam in Func(Init)
3072 llvm::SmallVector<DeducedTemplateArgument, 1> Deduced;
3073 Deduced.resize(1);
3074 QualType InitType = Init->getType();
3075 unsigned TDF = 0;
Richard Smith483b9f32011-02-21 20:05:19 +00003076 if (AdjustFunctionParmAndArgTypesForDeduction(*this, &TemplateParams,
Richard Smith34b41d92011-02-20 03:19:35 +00003077 FuncParam, InitType, Init,
3078 TDF))
3079 return false;
3080
3081 TemplateDeductionInfo Info(Context, Loc);
Richard Smith483b9f32011-02-21 20:05:19 +00003082 if (::DeduceTemplateArguments(*this, &TemplateParams,
Richard Smith34b41d92011-02-20 03:19:35 +00003083 FuncParam, InitType, Info, Deduced,
3084 TDF))
3085 return false;
3086
3087 QualType DeducedType = Deduced[0].getAsType();
3088 if (DeducedType.isNull())
3089 return false;
3090
3091 Result = SubstituteAutoTransform(*this, DeducedType).TransformType(Type);
3092 return true;
3093}
3094
Douglas Gregor8a514912009-09-14 18:39:43 +00003095static void
Douglas Gregore73bb602009-09-14 21:25:05 +00003096MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
3097 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003098 unsigned Level,
Douglas Gregore73bb602009-09-14 21:25:05 +00003099 llvm::SmallVectorImpl<bool> &Deduced);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003100
3101/// \brief If this is a non-static member function,
Douglas Gregor77bc5722010-11-12 23:44:13 +00003102static void MaybeAddImplicitObjectParameterType(ASTContext &Context,
3103 CXXMethodDecl *Method,
3104 llvm::SmallVectorImpl<QualType> &ArgTypes) {
3105 if (Method->isStatic())
3106 return;
3107
3108 // C++ [over.match.funcs]p4:
3109 //
3110 // For non-static member functions, the type of the implicit
3111 // object parameter is
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003112 // - "lvalue reference to cv X" for functions declared without a
Douglas Gregor77bc5722010-11-12 23:44:13 +00003113 // ref-qualifier or with the & ref-qualifier
3114 // - "rvalue reference to cv X" for functions declared with the
3115 // && ref-qualifier
3116 //
3117 // FIXME: We don't have ref-qualifiers yet, so we don't do that part.
3118 QualType ArgTy = Context.getTypeDeclType(Method->getParent());
3119 ArgTy = Context.getQualifiedType(ArgTy,
3120 Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
3121 ArgTy = Context.getLValueReferenceType(ArgTy);
3122 ArgTypes.push_back(ArgTy);
3123}
3124
Douglas Gregor8a514912009-09-14 18:39:43 +00003125/// \brief Determine whether the function template \p FT1 is at least as
3126/// specialized as \p FT2.
3127static bool isAtLeastAsSpecializedAs(Sema &S,
John McCall5769d612010-02-08 23:07:23 +00003128 SourceLocation Loc,
Douglas Gregor8a514912009-09-14 18:39:43 +00003129 FunctionTemplateDecl *FT1,
3130 FunctionTemplateDecl *FT2,
3131 TemplatePartialOrderingContext TPOC,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003132 unsigned NumCallArguments,
Douglas Gregorb939a192011-01-21 17:29:42 +00003133 llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *RefParamComparisons) {
Douglas Gregor8a514912009-09-14 18:39:43 +00003134 FunctionDecl *FD1 = FT1->getTemplatedDecl();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003135 FunctionDecl *FD2 = FT2->getTemplatedDecl();
Douglas Gregor8a514912009-09-14 18:39:43 +00003136 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
3137 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003138
Douglas Gregor8a514912009-09-14 18:39:43 +00003139 assert(Proto1 && Proto2 && "Function templates must have prototypes");
3140 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00003141 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor8a514912009-09-14 18:39:43 +00003142 Deduced.resize(TemplateParams->size());
3143
3144 // C++0x [temp.deduct.partial]p3:
3145 // The types used to determine the ordering depend on the context in which
3146 // the partial ordering is done:
John McCall2a7fb272010-08-25 05:32:35 +00003147 TemplateDeductionInfo Info(S.Context, Loc);
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003148 CXXMethodDecl *Method1 = 0;
3149 CXXMethodDecl *Method2 = 0;
3150 bool IsNonStatic2 = false;
3151 bool IsNonStatic1 = false;
3152 unsigned Skip2 = 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00003153 switch (TPOC) {
3154 case TPOC_Call: {
3155 // - In the context of a function call, the function parameter types are
3156 // used.
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003157 Method1 = dyn_cast<CXXMethodDecl>(FD1);
3158 Method2 = dyn_cast<CXXMethodDecl>(FD2);
3159 IsNonStatic1 = Method1 && !Method1->isStatic();
3160 IsNonStatic2 = Method2 && !Method2->isStatic();
3161
3162 // C++0x [temp.func.order]p3:
3163 // [...] If only one of the function templates is a non-static
3164 // member, that function template is considered to have a new
3165 // first parameter inserted in its function parameter list. The
3166 // new parameter is of type "reference to cv A," where cv are
3167 // the cv-qualifiers of the function template (if any) and A is
3168 // the class of which the function template is a member.
3169 //
3170 // C++98/03 doesn't have this provision, so instead we drop the
3171 // first argument of the free function or static member, which
3172 // seems to match existing practice.
Douglas Gregor77bc5722010-11-12 23:44:13 +00003173 llvm::SmallVector<QualType, 4> Args1;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003174 unsigned Skip1 = !S.getLangOptions().CPlusPlus0x &&
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003175 IsNonStatic2 && !IsNonStatic1;
3176 if (S.getLangOptions().CPlusPlus0x && IsNonStatic1 && !IsNonStatic2)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003177 MaybeAddImplicitObjectParameterType(S.Context, Method1, Args1);
3178 Args1.insert(Args1.end(),
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003179 Proto1->arg_type_begin() + Skip1, Proto1->arg_type_end());
Douglas Gregor77bc5722010-11-12 23:44:13 +00003180
3181 llvm::SmallVector<QualType, 4> Args2;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003182 Skip2 = !S.getLangOptions().CPlusPlus0x &&
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003183 IsNonStatic1 && !IsNonStatic2;
3184 if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1)
Douglas Gregor77bc5722010-11-12 23:44:13 +00003185 MaybeAddImplicitObjectParameterType(S.Context, Method2, Args2);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003186 Args2.insert(Args2.end(),
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003187 Proto2->arg_type_begin() + Skip2, Proto2->arg_type_end());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003188
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003189 // C++ [temp.func.order]p5:
3190 // The presence of unused ellipsis and default arguments has no effect on
3191 // the partial ordering of function templates.
3192 if (Args1.size() > NumCallArguments)
3193 Args1.resize(NumCallArguments);
3194 if (Args2.size() > NumCallArguments)
3195 Args2.resize(NumCallArguments);
3196 if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(),
3197 Args1.data(), Args1.size(), Info, Deduced,
3198 TDF_None, /*PartialOrdering=*/true,
Douglas Gregorb939a192011-01-21 17:29:42 +00003199 RefParamComparisons))
Douglas Gregor8a514912009-09-14 18:39:43 +00003200 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003201
Douglas Gregor8a514912009-09-14 18:39:43 +00003202 break;
3203 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003204
Douglas Gregor8a514912009-09-14 18:39:43 +00003205 case TPOC_Conversion:
3206 // - In the context of a call to a conversion operator, the return types
3207 // of the conversion function templates are used.
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003208 if (DeduceTemplateArguments(S, TemplateParams, Proto2->getResultType(),
3209 Proto1->getResultType(), Info, Deduced,
3210 TDF_None, /*PartialOrdering=*/true,
Douglas Gregorb939a192011-01-21 17:29:42 +00003211 RefParamComparisons))
Douglas Gregor8a514912009-09-14 18:39:43 +00003212 return false;
3213 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003214
Douglas Gregor8a514912009-09-14 18:39:43 +00003215 case TPOC_Other:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003216 // - In other contexts (14.6.6.2) the function template's function type
Douglas Gregor8a514912009-09-14 18:39:43 +00003217 // is used.
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003218 // FIXME: Don't we actually want to perform the adjustments on the parameter
3219 // types?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003220 if (DeduceTemplateArguments(S, TemplateParams, FD2->getType(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003221 FD1->getType(), Info, Deduced, TDF_None,
Douglas Gregorb939a192011-01-21 17:29:42 +00003222 /*PartialOrdering=*/true, RefParamComparisons))
Douglas Gregor8a514912009-09-14 18:39:43 +00003223 return false;
3224 break;
3225 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003226
Douglas Gregor8a514912009-09-14 18:39:43 +00003227 // C++0x [temp.deduct.partial]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003228 // In most cases, all template parameters must have values in order for
3229 // deduction to succeed, but for partial ordering purposes a template
3230 // parameter may remain without a value provided it is not used in the
Douglas Gregor8a514912009-09-14 18:39:43 +00003231 // types being used for partial ordering. [ Note: a template parameter used
3232 // in a non-deduced context is considered used. -end note]
3233 unsigned ArgIdx = 0, NumArgs = Deduced.size();
3234 for (; ArgIdx != NumArgs; ++ArgIdx)
3235 if (Deduced[ArgIdx].isNull())
3236 break;
3237
3238 if (ArgIdx == NumArgs) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003239 // All template arguments were deduced. FT1 is at least as specialized
Douglas Gregor8a514912009-09-14 18:39:43 +00003240 // as FT2.
3241 return true;
3242 }
3243
Douglas Gregore73bb602009-09-14 21:25:05 +00003244 // Figure out which template parameters were used.
Douglas Gregor8a514912009-09-14 18:39:43 +00003245 llvm::SmallVector<bool, 4> UsedParameters;
3246 UsedParameters.resize(TemplateParams->size());
3247 switch (TPOC) {
3248 case TPOC_Call: {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003249 unsigned NumParams = std::min(NumCallArguments,
3250 std::min(Proto1->getNumArgs(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003251 Proto2->getNumArgs()));
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003252 if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003253 ::MarkUsedTemplateParameters(S, Method2->getThisType(S.Context), false,
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003254 TemplateParams->getDepth(), UsedParameters);
3255 for (unsigned I = Skip2; I < NumParams; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003256 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003257 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00003258 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00003259 break;
3260 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003261
Douglas Gregor8a514912009-09-14 18:39:43 +00003262 case TPOC_Conversion:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003263 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003264 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00003265 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00003266 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003267
Douglas Gregor8a514912009-09-14 18:39:43 +00003268 case TPOC_Other:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003269 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003270 TemplateParams->getDepth(),
3271 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00003272 break;
3273 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003274
Douglas Gregor8a514912009-09-14 18:39:43 +00003275 for (; ArgIdx != NumArgs; ++ArgIdx)
3276 // If this argument had no value deduced but was used in one of the types
3277 // used for partial ordering, then deduction fails.
3278 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
3279 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003280
Douglas Gregor8a514912009-09-14 18:39:43 +00003281 return true;
3282}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003283
Douglas Gregor9da95e62011-01-16 16:03:23 +00003284/// \brief Determine whether this a function template whose parameter-type-list
3285/// ends with a function parameter pack.
3286static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) {
3287 FunctionDecl *Function = FunTmpl->getTemplatedDecl();
3288 unsigned NumParams = Function->getNumParams();
3289 if (NumParams == 0)
3290 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003291
Douglas Gregor9da95e62011-01-16 16:03:23 +00003292 ParmVarDecl *Last = Function->getParamDecl(NumParams - 1);
3293 if (!Last->isParameterPack())
3294 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003295
Douglas Gregor9da95e62011-01-16 16:03:23 +00003296 // Make sure that no previous parameter is a parameter pack.
3297 while (--NumParams > 0) {
3298 if (Function->getParamDecl(NumParams - 1)->isParameterPack())
3299 return false;
3300 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003301
Douglas Gregor9da95e62011-01-16 16:03:23 +00003302 return true;
3303}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003304
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003305/// \brief Returns the more specialized function template according
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003306/// to the rules of function template partial ordering (C++ [temp.func.order]).
3307///
3308/// \param FT1 the first function template
3309///
3310/// \param FT2 the second function template
3311///
Douglas Gregor8a514912009-09-14 18:39:43 +00003312/// \param TPOC the context in which we are performing partial ordering of
3313/// function templates.
Mike Stump1eb44332009-09-09 15:08:12 +00003314///
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003315/// \param NumCallArguments The number of arguments in a call, used only
3316/// when \c TPOC is \c TPOC_Call.
3317///
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003318/// \returns the more specialized function template. If neither
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003319/// template is more specialized, returns NULL.
3320FunctionTemplateDecl *
3321Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
3322 FunctionTemplateDecl *FT2,
John McCall5769d612010-02-08 23:07:23 +00003323 SourceLocation Loc,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003324 TemplatePartialOrderingContext TPOC,
3325 unsigned NumCallArguments) {
Douglas Gregorb939a192011-01-21 17:29:42 +00003326 llvm::SmallVector<RefParamPartialOrderingComparison, 4> RefParamComparisons;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003327 bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003328 NumCallArguments, 0);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003329 bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003330 NumCallArguments,
Douglas Gregorb939a192011-01-21 17:29:42 +00003331 &RefParamComparisons);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003332
Douglas Gregor8a514912009-09-14 18:39:43 +00003333 if (Better1 != Better2) // We have a clear winner
3334 return Better1? FT1 : FT2;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003335
Douglas Gregor8a514912009-09-14 18:39:43 +00003336 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003337 return 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00003338
Douglas Gregor8a514912009-09-14 18:39:43 +00003339 // C++0x [temp.deduct.partial]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003340 // If for each type being considered a given template is at least as
Douglas Gregor8a514912009-09-14 18:39:43 +00003341 // specialized for all types and more specialized for some set of types and
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003342 // the other template is not more specialized for any types or is not at
Douglas Gregor8a514912009-09-14 18:39:43 +00003343 // least as specialized for any types, then the given template is more
3344 // specialized than the other template. Otherwise, neither template is more
3345 // specialized than the other.
3346 Better1 = false;
3347 Better2 = false;
Douglas Gregorb939a192011-01-21 17:29:42 +00003348 for (unsigned I = 0, N = RefParamComparisons.size(); I != N; ++I) {
Douglas Gregor8a514912009-09-14 18:39:43 +00003349 // C++0x [temp.deduct.partial]p9:
3350 // If, for a given type, deduction succeeds in both directions (i.e., the
Douglas Gregorb939a192011-01-21 17:29:42 +00003351 // types are identical after the transformations above) and both P and A
3352 // were reference types (before being replaced with the type referred to
3353 // above):
3354
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003355 // -- if the type from the argument template was an lvalue reference
Douglas Gregorb939a192011-01-21 17:29:42 +00003356 // and the type from the parameter template was not, the argument
3357 // type is considered to be more specialized than the other;
3358 // otherwise,
3359 if (!RefParamComparisons[I].ArgIsRvalueRef &&
3360 RefParamComparisons[I].ParamIsRvalueRef) {
3361 Better2 = true;
3362 if (Better1)
3363 return 0;
3364 continue;
3365 } else if (!RefParamComparisons[I].ParamIsRvalueRef &&
3366 RefParamComparisons[I].ArgIsRvalueRef) {
3367 Better1 = true;
3368 if (Better2)
3369 return 0;
3370 continue;
Douglas Gregor8a514912009-09-14 18:39:43 +00003371 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003372
Douglas Gregorb939a192011-01-21 17:29:42 +00003373 // -- if the type from the argument template is more cv-qualified than
3374 // the type from the parameter template (as described above), the
3375 // argument type is considered to be more specialized than the
3376 // other; otherwise,
3377 switch (RefParamComparisons[I].Qualifiers) {
3378 case NeitherMoreQualified:
3379 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003380
Douglas Gregorb939a192011-01-21 17:29:42 +00003381 case ParamMoreQualified:
3382 Better1 = true;
3383 if (Better2)
3384 return 0;
3385 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003386
Douglas Gregorb939a192011-01-21 17:29:42 +00003387 case ArgMoreQualified:
3388 Better2 = true;
3389 if (Better1)
3390 return 0;
3391 continue;
3392 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003393
Douglas Gregorb939a192011-01-21 17:29:42 +00003394 // -- neither type is more specialized than the other.
Douglas Gregor8a514912009-09-14 18:39:43 +00003395 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003396
Douglas Gregor8a514912009-09-14 18:39:43 +00003397 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003398 if (Better1)
3399 return FT1;
Douglas Gregor8a514912009-09-14 18:39:43 +00003400 else if (Better2)
3401 return FT2;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003402
Douglas Gregor9da95e62011-01-16 16:03:23 +00003403 // FIXME: This mimics what GCC implements, but doesn't match up with the
3404 // proposed resolution for core issue 692. This area needs to be sorted out,
3405 // but for now we attempt to maintain compatibility.
3406 bool Variadic1 = isVariadicFunctionTemplate(FT1);
3407 bool Variadic2 = isVariadicFunctionTemplate(FT2);
3408 if (Variadic1 != Variadic2)
3409 return Variadic1? FT2 : FT1;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003410
Douglas Gregor9da95e62011-01-16 16:03:23 +00003411 return 0;
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003412}
Douglas Gregor83314aa2009-07-08 20:55:45 +00003413
Douglas Gregord5a423b2009-09-25 18:43:00 +00003414/// \brief Determine if the two templates are equivalent.
3415static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
3416 if (T1 == T2)
3417 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003418
Douglas Gregord5a423b2009-09-25 18:43:00 +00003419 if (!T1 || !T2)
3420 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003421
Douglas Gregord5a423b2009-09-25 18:43:00 +00003422 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
3423}
3424
3425/// \brief Retrieve the most specialized of the given function template
3426/// specializations.
3427///
John McCallc373d482010-01-27 01:50:18 +00003428/// \param SpecBegin the start iterator of the function template
3429/// specializations that we will be comparing.
Douglas Gregord5a423b2009-09-25 18:43:00 +00003430///
John McCallc373d482010-01-27 01:50:18 +00003431/// \param SpecEnd the end iterator of the function template
3432/// specializations, paired with \p SpecBegin.
Douglas Gregord5a423b2009-09-25 18:43:00 +00003433///
3434/// \param TPOC the partial ordering context to use to compare the function
3435/// template specializations.
3436///
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003437/// \param NumCallArguments The number of arguments in a call, used only
3438/// when \c TPOC is \c TPOC_Call.
3439///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003440/// \param Loc the location where the ambiguity or no-specializations
Douglas Gregord5a423b2009-09-25 18:43:00 +00003441/// diagnostic should occur.
3442///
3443/// \param NoneDiag partial diagnostic used to diagnose cases where there are
3444/// no matching candidates.
3445///
3446/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
3447/// occurs.
3448///
3449/// \param CandidateDiag partial diagnostic used for each function template
3450/// specialization that is a candidate in the ambiguous ordering. One parameter
3451/// in this diagnostic should be unbound, which will correspond to the string
3452/// describing the template arguments for the function template specialization.
3453///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003454/// \param Index if non-NULL and the result of this function is non-nULL,
Douglas Gregord5a423b2009-09-25 18:43:00 +00003455/// receives the index corresponding to the resulting function template
3456/// specialization.
3457///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003458/// \returns the most specialized function template specialization, if
John McCallc373d482010-01-27 01:50:18 +00003459/// found. Otherwise, returns SpecEnd.
Douglas Gregord5a423b2009-09-25 18:43:00 +00003460///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003461/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
Douglas Gregord5a423b2009-09-25 18:43:00 +00003462/// template argument deduction.
John McCallc373d482010-01-27 01:50:18 +00003463UnresolvedSetIterator
3464Sema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003465 UnresolvedSetIterator SpecEnd,
John McCallc373d482010-01-27 01:50:18 +00003466 TemplatePartialOrderingContext TPOC,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003467 unsigned NumCallArguments,
John McCallc373d482010-01-27 01:50:18 +00003468 SourceLocation Loc,
3469 const PartialDiagnostic &NoneDiag,
3470 const PartialDiagnostic &AmbigDiag,
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003471 const PartialDiagnostic &CandidateDiag,
3472 bool Complain) {
John McCallc373d482010-01-27 01:50:18 +00003473 if (SpecBegin == SpecEnd) {
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003474 if (Complain)
3475 Diag(Loc, NoneDiag);
John McCallc373d482010-01-27 01:50:18 +00003476 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00003477 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003478
3479 if (SpecBegin + 1 == SpecEnd)
John McCallc373d482010-01-27 01:50:18 +00003480 return SpecBegin;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003481
Douglas Gregord5a423b2009-09-25 18:43:00 +00003482 // Find the function template that is better than all of the templates it
3483 // has been compared to.
John McCallc373d482010-01-27 01:50:18 +00003484 UnresolvedSetIterator Best = SpecBegin;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003485 FunctionTemplateDecl *BestTemplate
John McCallc373d482010-01-27 01:50:18 +00003486 = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00003487 assert(BestTemplate && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00003488 for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
3489 FunctionTemplateDecl *Challenger
3490 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00003491 assert(Challenger && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00003492 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003493 Loc, TPOC, NumCallArguments),
Douglas Gregord5a423b2009-09-25 18:43:00 +00003494 Challenger)) {
3495 Best = I;
3496 BestTemplate = Challenger;
3497 }
3498 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003499
Douglas Gregord5a423b2009-09-25 18:43:00 +00003500 // Make sure that the "best" function template is more specialized than all
3501 // of the others.
3502 bool Ambiguous = false;
John McCallc373d482010-01-27 01:50:18 +00003503 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
3504 FunctionTemplateDecl *Challenger
3505 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00003506 if (I != Best &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003507 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003508 Loc, TPOC, NumCallArguments),
Douglas Gregord5a423b2009-09-25 18:43:00 +00003509 BestTemplate)) {
3510 Ambiguous = true;
3511 break;
3512 }
3513 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003514
Douglas Gregord5a423b2009-09-25 18:43:00 +00003515 if (!Ambiguous) {
3516 // We found an answer. Return it.
John McCallc373d482010-01-27 01:50:18 +00003517 return Best;
Douglas Gregord5a423b2009-09-25 18:43:00 +00003518 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003519
Douglas Gregord5a423b2009-09-25 18:43:00 +00003520 // Diagnose the ambiguity.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003521 if (Complain)
3522 Diag(Loc, AmbigDiag);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003523
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003524 if (Complain)
Douglas Gregord5a423b2009-09-25 18:43:00 +00003525 // FIXME: Can we order the candidates in some sane way?
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003526 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
3527 Diag((*I)->getLocation(), CandidateDiag)
3528 << getTemplateArgumentBindingsText(
3529 cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
John McCallc373d482010-01-27 01:50:18 +00003530 *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003531
John McCallc373d482010-01-27 01:50:18 +00003532 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00003533}
3534
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003535/// \brief Returns the more specialized class template partial specialization
3536/// according to the rules of partial ordering of class template partial
3537/// specializations (C++ [temp.class.order]).
3538///
3539/// \param PS1 the first class template partial specialization
3540///
3541/// \param PS2 the second class template partial specialization
3542///
3543/// \returns the more specialized class template partial specialization. If
3544/// neither partial specialization is more specialized, returns NULL.
3545ClassTemplatePartialSpecializationDecl *
3546Sema::getMoreSpecializedPartialSpecialization(
3547 ClassTemplatePartialSpecializationDecl *PS1,
John McCall5769d612010-02-08 23:07:23 +00003548 ClassTemplatePartialSpecializationDecl *PS2,
3549 SourceLocation Loc) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003550 // C++ [temp.class.order]p1:
3551 // For two class template partial specializations, the first is at least as
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003552 // specialized as the second if, given the following rewrite to two
3553 // function templates, the first function template is at least as
3554 // specialized as the second according to the ordering rules for function
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003555 // templates (14.6.6.2):
3556 // - the first function template has the same template parameters as the
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003557 // first partial specialization and has a single function parameter
3558 // whose type is a class template specialization with the template
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003559 // arguments of the first partial specialization, and
3560 // - the second function template has the same template parameters as the
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003561 // second partial specialization and has a single function parameter
3562 // whose type is a class template specialization with the template
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003563 // arguments of the second partial specialization.
3564 //
Douglas Gregor31dce8f2010-04-29 06:21:43 +00003565 // Rather than synthesize function templates, we merely perform the
3566 // equivalent partial ordering by performing deduction directly on
3567 // the template arguments of the class template partial
3568 // specializations. This computation is slightly simpler than the
3569 // general problem of function template partial ordering, because
3570 // class template partial specializations are more constrained. We
3571 // know that every template parameter is deducible from the class
3572 // template partial specialization's template arguments, for
3573 // example.
Douglas Gregor02024a92010-03-28 02:42:43 +00003574 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
John McCall2a7fb272010-08-25 05:32:35 +00003575 TemplateDeductionInfo Info(Context, Loc);
John McCall31f17ec2010-04-27 00:57:59 +00003576
3577 QualType PT1 = PS1->getInjectedSpecializationType();
3578 QualType PT2 = PS2->getInjectedSpecializationType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003579
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003580 // Determine whether PS1 is at least as specialized as PS2
3581 Deduced.resize(PS2->getTemplateParameters()->size());
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003582 bool Better1 = !::DeduceTemplateArguments(*this, PS2->getTemplateParameters(),
3583 PT2, PT1, Info, Deduced, TDF_None,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003584 /*PartialOrdering=*/true,
Douglas Gregorb939a192011-01-21 17:29:42 +00003585 /*RefParamComparisons=*/0);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00003586 if (Better1) {
3587 InstantiatingTemplate Inst(*this, PS2->getLocation(), PS2,
3588 Deduced.data(), Deduced.size(), Info);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003589 Better1 = !::FinishTemplateArgumentDeduction(*this, PS2,
3590 PS1->getTemplateArgs(),
Douglas Gregor516e6e02010-04-29 06:31:36 +00003591 Deduced, Info);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00003592 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003593
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003594 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregordb0d4b72009-11-11 23:06:43 +00003595 Deduced.clear();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003596 Deduced.resize(PS1->getTemplateParameters()->size());
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003597 bool Better2 = !::DeduceTemplateArguments(*this, PS1->getTemplateParameters(),
3598 PT1, PT2, Info, Deduced, TDF_None,
3599 /*PartialOrdering=*/true,
Douglas Gregorb939a192011-01-21 17:29:42 +00003600 /*RefParamComparisons=*/0);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00003601 if (Better2) {
3602 InstantiatingTemplate Inst(*this, PS1->getLocation(), PS1,
3603 Deduced.data(), Deduced.size(), Info);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003604 Better2 = !::FinishTemplateArgumentDeduction(*this, PS1,
3605 PS2->getTemplateArgs(),
Douglas Gregor516e6e02010-04-29 06:31:36 +00003606 Deduced, Info);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00003607 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003608
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003609 if (Better1 == Better2)
3610 return 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003611
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003612 return Better1? PS1 : PS2;
3613}
3614
Mike Stump1eb44332009-09-09 15:08:12 +00003615static void
Douglas Gregore73bb602009-09-14 21:25:05 +00003616MarkUsedTemplateParameters(Sema &SemaRef,
3617 const TemplateArgument &TemplateArg,
3618 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003619 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003620 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003621
Douglas Gregore73bb602009-09-14 21:25:05 +00003622/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00003623/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00003624static void
Douglas Gregore73bb602009-09-14 21:25:05 +00003625MarkUsedTemplateParameters(Sema &SemaRef,
3626 const Expr *E,
3627 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003628 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003629 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregorbe230c32011-01-03 17:17:50 +00003630 // We can deduce from a pack expansion.
3631 if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E))
3632 E = Expansion->getPattern();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003633
Douglas Gregor54c53cc2011-01-04 23:35:54 +00003634 // Skip through any implicit casts we added while type-checking.
3635 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
3636 E = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003637
3638 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
Douglas Gregore73bb602009-09-14 21:25:05 +00003639 // find other occurrences of template parameters.
Douglas Gregorf6ddb732009-06-18 18:45:36 +00003640 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregorc781f9c2010-01-14 18:13:22 +00003641 if (!DRE)
Douglas Gregor031a5882009-06-13 00:26:55 +00003642 return;
3643
Mike Stump1eb44332009-09-09 15:08:12 +00003644 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00003645 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
3646 if (!NTTP)
3647 return;
3648
Douglas Gregored9c0f92009-10-29 00:04:11 +00003649 if (NTTP->getDepth() == Depth)
3650 Used[NTTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00003651}
3652
Douglas Gregore73bb602009-09-14 21:25:05 +00003653/// \brief Mark the template parameters that are used by the given
3654/// nested name specifier.
3655static void
3656MarkUsedTemplateParameters(Sema &SemaRef,
3657 NestedNameSpecifier *NNS,
3658 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003659 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003660 llvm::SmallVectorImpl<bool> &Used) {
3661 if (!NNS)
3662 return;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003663
Douglas Gregored9c0f92009-10-29 00:04:11 +00003664 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
3665 Used);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003666 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003667 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003668}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003669
Douglas Gregore73bb602009-09-14 21:25:05 +00003670/// \brief Mark the template parameters that are used by the given
3671/// template name.
3672static void
3673MarkUsedTemplateParameters(Sema &SemaRef,
3674 TemplateName Name,
3675 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003676 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003677 llvm::SmallVectorImpl<bool> &Used) {
3678 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
3679 if (TemplateTemplateParmDecl *TTP
Douglas Gregored9c0f92009-10-29 00:04:11 +00003680 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
3681 if (TTP->getDepth() == Depth)
3682 Used[TTP->getIndex()] = true;
3683 }
Douglas Gregore73bb602009-09-14 21:25:05 +00003684 return;
3685 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003686
Douglas Gregor788cd062009-11-11 01:00:40 +00003687 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003688 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
Douglas Gregor788cd062009-11-11 01:00:40 +00003689 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003690 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003691 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003692 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003693}
3694
3695/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00003696/// type.
Mike Stump1eb44332009-09-09 15:08:12 +00003697static void
Douglas Gregore73bb602009-09-14 21:25:05 +00003698MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
3699 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003700 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003701 llvm::SmallVectorImpl<bool> &Used) {
3702 if (T.isNull())
3703 return;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003704
Douglas Gregor031a5882009-06-13 00:26:55 +00003705 // Non-dependent types have nothing deducible
3706 if (!T->isDependentType())
3707 return;
3708
3709 T = SemaRef.Context.getCanonicalType(T);
3710 switch (T->getTypeClass()) {
Douglas Gregor031a5882009-06-13 00:26:55 +00003711 case Type::Pointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00003712 MarkUsedTemplateParameters(SemaRef,
3713 cast<PointerType>(T)->getPointeeType(),
3714 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003715 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003716 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003717 break;
3718
3719 case Type::BlockPointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00003720 MarkUsedTemplateParameters(SemaRef,
3721 cast<BlockPointerType>(T)->getPointeeType(),
3722 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003723 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003724 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003725 break;
3726
3727 case Type::LValueReference:
3728 case Type::RValueReference:
Douglas Gregore73bb602009-09-14 21:25:05 +00003729 MarkUsedTemplateParameters(SemaRef,
3730 cast<ReferenceType>(T)->getPointeeType(),
3731 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003732 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003733 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003734 break;
3735
3736 case Type::MemberPointer: {
3737 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore73bb602009-09-14 21:25:05 +00003738 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003739 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003740 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003741 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003742 break;
3743 }
3744
3745 case Type::DependentSizedArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00003746 MarkUsedTemplateParameters(SemaRef,
3747 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003748 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003749 // Fall through to check the element type
3750
3751 case Type::ConstantArray:
3752 case Type::IncompleteArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00003753 MarkUsedTemplateParameters(SemaRef,
3754 cast<ArrayType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003755 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003756 break;
3757
3758 case Type::Vector:
3759 case Type::ExtVector:
Douglas Gregore73bb602009-09-14 21:25:05 +00003760 MarkUsedTemplateParameters(SemaRef,
3761 cast<VectorType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003762 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003763 break;
3764
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00003765 case Type::DependentSizedExtVector: {
3766 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00003767 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00003768 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003769 Depth, Used);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003770 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003771 Depth, Used);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00003772 break;
3773 }
3774
Douglas Gregor031a5882009-06-13 00:26:55 +00003775 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00003776 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00003777 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003778 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003779 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore73bb602009-09-14 21:25:05 +00003780 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003781 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003782 break;
3783 }
3784
Douglas Gregored9c0f92009-10-29 00:04:11 +00003785 case Type::TemplateTypeParm: {
3786 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
3787 if (TTP->getDepth() == Depth)
3788 Used[TTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00003789 break;
Douglas Gregored9c0f92009-10-29 00:04:11 +00003790 }
Douglas Gregor031a5882009-06-13 00:26:55 +00003791
Douglas Gregor0bc15d92011-01-14 05:11:40 +00003792 case Type::SubstTemplateTypeParmPack: {
3793 const SubstTemplateTypeParmPackType *Subst
3794 = cast<SubstTemplateTypeParmPackType>(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003795 MarkUsedTemplateParameters(SemaRef,
Douglas Gregor0bc15d92011-01-14 05:11:40 +00003796 QualType(Subst->getReplacedParameter(), 0),
3797 OnlyDeduced, Depth, Used);
3798 MarkUsedTemplateParameters(SemaRef, Subst->getArgumentPack(),
3799 OnlyDeduced, Depth, Used);
3800 break;
3801 }
3802
John McCall31f17ec2010-04-27 00:57:59 +00003803 case Type::InjectedClassName:
3804 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
3805 // fall through
3806
Douglas Gregor031a5882009-06-13 00:26:55 +00003807 case Type::TemplateSpecialization: {
Mike Stump1eb44332009-09-09 15:08:12 +00003808 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00003809 = cast<TemplateSpecializationType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00003810 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003811 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
Douglas Gregore73bb602009-09-14 21:25:05 +00003821 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00003822 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
3823 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003824 break;
3825 }
3826
Douglas Gregore73bb602009-09-14 21:25:05 +00003827 case Type::Complex:
3828 if (!OnlyDeduced)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003829 MarkUsedTemplateParameters(SemaRef,
Douglas Gregore73bb602009-09-14 21:25:05 +00003830 cast<ComplexType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003831 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003832 break;
3833
Douglas Gregor4714c122010-03-31 17:34:00 +00003834 case Type::DependentName:
Douglas Gregore73bb602009-09-14 21:25:05 +00003835 if (!OnlyDeduced)
3836 MarkUsedTemplateParameters(SemaRef,
Douglas Gregor4714c122010-03-31 17:34:00 +00003837 cast<DependentNameType>(T)->getQualifier(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003838 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003839 break;
3840
John McCall33500952010-06-11 00:33:02 +00003841 case Type::DependentTemplateSpecialization: {
3842 const DependentTemplateSpecializationType *Spec
3843 = cast<DependentTemplateSpecializationType>(T);
3844 if (!OnlyDeduced)
3845 MarkUsedTemplateParameters(SemaRef, Spec->getQualifier(),
3846 OnlyDeduced, Depth, Used);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003847
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003848 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003849 // If the template argument list of P contains a pack expansion that is not
3850 // the last template argument, the entire template argument list is a
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003851 // non-deduced context.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003852 if (OnlyDeduced &&
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003853 hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs()))
3854 break;
3855
John McCall33500952010-06-11 00:33:02 +00003856 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
3857 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
3858 Used);
3859 break;
3860 }
3861
John McCallad5e7382010-03-01 23:49:17 +00003862 case Type::TypeOf:
3863 if (!OnlyDeduced)
3864 MarkUsedTemplateParameters(SemaRef,
3865 cast<TypeOfType>(T)->getUnderlyingType(),
3866 OnlyDeduced, Depth, Used);
3867 break;
3868
3869 case Type::TypeOfExpr:
3870 if (!OnlyDeduced)
3871 MarkUsedTemplateParameters(SemaRef,
3872 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
3873 OnlyDeduced, Depth, Used);
3874 break;
3875
3876 case Type::Decltype:
3877 if (!OnlyDeduced)
3878 MarkUsedTemplateParameters(SemaRef,
3879 cast<DecltypeType>(T)->getUnderlyingExpr(),
3880 OnlyDeduced, Depth, Used);
3881 break;
3882
Douglas Gregor7536dd52010-12-20 02:24:11 +00003883 case Type::PackExpansion:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003884 MarkUsedTemplateParameters(SemaRef,
Douglas Gregor7536dd52010-12-20 02:24:11 +00003885 cast<PackExpansionType>(T)->getPattern(),
3886 OnlyDeduced, Depth, Used);
3887 break;
3888
Richard Smith34b41d92011-02-20 03:19:35 +00003889 case Type::Auto:
3890 MarkUsedTemplateParameters(SemaRef,
3891 cast<AutoType>(T)->getDeducedType(),
3892 OnlyDeduced, Depth, Used);
3893
Douglas Gregore73bb602009-09-14 21:25:05 +00003894 // None of these types have any template parameters in them.
Douglas Gregor031a5882009-06-13 00:26:55 +00003895 case Type::Builtin:
Douglas Gregor031a5882009-06-13 00:26:55 +00003896 case Type::VariableArray:
3897 case Type::FunctionNoProto:
3898 case Type::Record:
3899 case Type::Enum:
Douglas Gregor031a5882009-06-13 00:26:55 +00003900 case Type::ObjCInterface:
John McCallc12c5bb2010-05-15 11:32:37 +00003901 case Type::ObjCObject:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00003902 case Type::ObjCObjectPointer:
John McCalled976492009-12-04 22:46:56 +00003903 case Type::UnresolvedUsing:
Douglas Gregor031a5882009-06-13 00:26:55 +00003904#define TYPE(Class, Base)
3905#define ABSTRACT_TYPE(Class, Base)
3906#define DEPENDENT_TYPE(Class, Base)
3907#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3908#include "clang/AST/TypeNodes.def"
3909 break;
3910 }
3911}
3912
Douglas Gregore73bb602009-09-14 21:25:05 +00003913/// \brief Mark the template parameters that are used by this
Douglas Gregor031a5882009-06-13 00:26:55 +00003914/// template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00003915static void
Douglas Gregore73bb602009-09-14 21:25:05 +00003916MarkUsedTemplateParameters(Sema &SemaRef,
3917 const TemplateArgument &TemplateArg,
3918 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003919 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003920 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00003921 switch (TemplateArg.getKind()) {
3922 case TemplateArgument::Null:
3923 case TemplateArgument::Integral:
Douglas Gregor788cd062009-11-11 01:00:40 +00003924 case TemplateArgument::Declaration:
Douglas Gregor031a5882009-06-13 00:26:55 +00003925 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003926
Douglas Gregor031a5882009-06-13 00:26:55 +00003927 case TemplateArgument::Type:
Douglas Gregore73bb602009-09-14 21:25:05 +00003928 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003929 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003930 break;
3931
Douglas Gregor788cd062009-11-11 01:00:40 +00003932 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00003933 case TemplateArgument::TemplateExpansion:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003934 MarkUsedTemplateParameters(SemaRef,
3935 TemplateArg.getAsTemplateOrTemplatePattern(),
Douglas Gregor788cd062009-11-11 01:00:40 +00003936 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003937 break;
3938
3939 case TemplateArgument::Expression:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003940 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003941 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003942 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003943
Anders Carlssond01b1da2009-06-15 17:04:53 +00003944 case TemplateArgument::Pack:
Douglas Gregore73bb602009-09-14 21:25:05 +00003945 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
3946 PEnd = TemplateArg.pack_end();
3947 P != PEnd; ++P)
Douglas Gregored9c0f92009-10-29 00:04:11 +00003948 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssond01b1da2009-06-15 17:04:53 +00003949 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00003950 }
3951}
3952
3953/// \brief Mark the template parameters can be deduced by the given
3954/// template argument list.
3955///
3956/// \param TemplateArgs the template argument list from which template
3957/// parameters will be deduced.
3958///
3959/// \param Deduced a bit vector whose elements will be set to \c true
3960/// to indicate when the corresponding template parameter will be
3961/// deduced.
Mike Stump1eb44332009-09-09 15:08:12 +00003962void
Douglas Gregore73bb602009-09-14 21:25:05 +00003963Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003964 bool OnlyDeduced, unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003965 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003966 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003967 // If the template argument list of P contains a pack expansion that is not
3968 // the last template argument, the entire template argument list is a
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003969 // non-deduced context.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003970 if (OnlyDeduced &&
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003971 hasPackExpansionBeforeEnd(TemplateArgs.data(), TemplateArgs.size()))
3972 return;
3973
Douglas Gregor031a5882009-06-13 00:26:55 +00003974 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003975 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003976 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003977}
Douglas Gregor63f07c52009-09-18 23:21:38 +00003978
3979/// \brief Marks all of the template parameters that will be deduced by a
3980/// call to the given function template.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003981void
Douglas Gregor02024a92010-03-28 02:42:43 +00003982Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
3983 llvm::SmallVectorImpl<bool> &Deduced) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003984 TemplateParameterList *TemplateParams
Douglas Gregor63f07c52009-09-18 23:21:38 +00003985 = FunctionTemplate->getTemplateParameters();
3986 Deduced.clear();
3987 Deduced.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003988
Douglas Gregor63f07c52009-09-18 23:21:38 +00003989 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
3990 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
3991 ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003992 true, TemplateParams->getDepth(), Deduced);
Douglas Gregor63f07c52009-09-18 23:21:38 +00003993}