blob: 842c5fb17d9159f8e9e6e267491360ffceface8c [file] [log] [blame]
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001//===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template argument deduction.
10//
11//===----------------------------------------------------------------------===/
12
Douglas Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Sema.h"
John McCall19510852010-08-20 18:27:03 +000014#include "clang/Sema/DeclSpec.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
John McCall2a7fb272010-08-25 05:32:35 +000016#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor0b9247f2009-06-04 00:03:07 +000017#include "clang/AST/ASTContext.h"
John McCall7cd088e2010-08-24 07:21:54 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor0b9247f2009-06-04 00:03:07 +000019#include "clang/AST/DeclTemplate.h"
20#include "clang/AST/StmtVisitor.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/ExprCXX.h"
Douglas Gregore02e2622010-12-22 21:19:48 +000023#include "llvm/ADT/BitVector.h"
Richard Smith34b41d92011-02-20 03:19:35 +000024#include "TreeTransform.h"
Douglas Gregor8a514912009-09-14 18:39:43 +000025#include <algorithm>
Douglas Gregor508f1c82009-06-26 23:10:12 +000026
27namespace clang {
John McCall2a7fb272010-08-25 05:32:35 +000028 using namespace sema;
29
Douglas Gregor508f1c82009-06-26 23:10:12 +000030 /// \brief Various flags that control template argument deduction.
31 ///
32 /// These flags can be bitwise-OR'd together.
33 enum TemplateDeductionFlags {
34 /// \brief No template argument deduction flags, which indicates the
35 /// strictest results for template argument deduction (as used for, e.g.,
36 /// matching class template partial specializations).
37 TDF_None = 0,
38 /// \brief Within template argument deduction from a function call, we are
39 /// matching with a parameter type for which the original parameter was
40 /// a reference.
41 TDF_ParamWithReferenceType = 0x1,
42 /// \brief Within template argument deduction from a function call, we
43 /// are matching in a case where we ignore cv-qualifiers.
44 TDF_IgnoreQualifiers = 0x02,
45 /// \brief Within template argument deduction from a function call,
46 /// we are matching in a case where we can perform template argument
Douglas Gregor41128772009-06-26 23:27:24 +000047 /// deduction from a template-id of a derived class of the argument type.
Douglas Gregor12820292009-09-14 20:00:47 +000048 TDF_DerivedClass = 0x04,
49 /// \brief Allow non-dependent types to differ, e.g., when performing
50 /// template argument deduction from a function call where conversions
51 /// may apply.
Douglas Gregor73b3cf62011-01-25 17:19:08 +000052 TDF_SkipNonDependent = 0x08,
53 /// \brief Whether we are performing template argument deduction for
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000054 /// parameters and arguments in a top-level template argument
Douglas Gregor73b3cf62011-01-25 17:19:08 +000055 TDF_TopLevelParameterTypeList = 0x10
Douglas Gregor508f1c82009-06-26 23:10:12 +000056 };
57}
58
Douglas Gregor0b9247f2009-06-04 00:03:07 +000059using namespace clang;
60
Douglas Gregor9d0e4412010-03-26 05:50:28 +000061/// \brief Compare two APSInts, extending and switching the sign as
62/// necessary to compare their values regardless of underlying type.
63static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) {
64 if (Y.getBitWidth() > X.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +000065 X = X.extend(Y.getBitWidth());
Douglas Gregor9d0e4412010-03-26 05:50:28 +000066 else if (Y.getBitWidth() < X.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +000067 Y = Y.extend(X.getBitWidth());
Douglas Gregor9d0e4412010-03-26 05:50:28 +000068
69 // If there is a signedness mismatch, correct it.
70 if (X.isSigned() != Y.isSigned()) {
71 // If the signed value is negative, then the values cannot be the same.
72 if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative()))
73 return false;
74
75 Y.setIsSigned(true);
76 X.setIsSigned(true);
77 }
78
79 return X == Y;
80}
81
Douglas Gregorf67875d2009-06-12 18:26:56 +000082static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +000083DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +000084 TemplateParameterList *TemplateParams,
85 const TemplateArgument &Param,
Douglas Gregor77d6bb92011-01-11 22:21:24 +000086 TemplateArgument Arg,
John McCall2a7fb272010-08-25 05:32:35 +000087 TemplateDeductionInfo &Info,
Douglas Gregor0972c862010-12-22 18:55:49 +000088 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced);
Douglas Gregord708c722009-06-09 16:35:58 +000089
Douglas Gregorb939a192011-01-21 17:29:42 +000090/// \brief Whether template argument deduction for two reference parameters
91/// resulted in the argument type, parameter type, or neither type being more
92/// qualified than the other.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000093enum DeductionQualifierComparison {
94 NeitherMoreQualified = 0,
95 ParamMoreQualified,
96 ArgMoreQualified
Douglas Gregor5c7bf422011-01-11 17:34:58 +000097};
98
Douglas Gregorb939a192011-01-21 17:29:42 +000099/// \brief Stores the result of comparing two reference parameters while
100/// performing template argument deduction for partial ordering of function
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000101/// templates.
Douglas Gregorb939a192011-01-21 17:29:42 +0000102struct RefParamPartialOrderingComparison {
103 /// \brief Whether the parameter type is an rvalue reference type.
104 bool ParamIsRvalueRef;
105 /// \brief Whether the argument type is an rvalue reference type.
106 bool ArgIsRvalueRef;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000107
Douglas Gregorb939a192011-01-21 17:29:42 +0000108 /// \brief Whether the parameter or argument (or neither) is more qualified.
109 DeductionQualifierComparison Qualifiers;
110};
111
112
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000113
Douglas Gregor20a55e22010-12-22 18:17:10 +0000114static Sema::TemplateDeductionResult
115DeduceTemplateArguments(Sema &S,
116 TemplateParameterList *TemplateParams,
Douglas Gregor603cfb42011-01-05 23:12:31 +0000117 QualType Param,
118 QualType Arg,
119 TemplateDeductionInfo &Info,
120 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000121 unsigned TDF,
122 bool PartialOrdering = false,
Douglas Gregorb939a192011-01-21 17:29:42 +0000123 llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *
124 RefParamComparisons = 0);
Douglas Gregor603cfb42011-01-05 23:12:31 +0000125
126static Sema::TemplateDeductionResult
127DeduceTemplateArguments(Sema &S,
128 TemplateParameterList *TemplateParams,
Douglas Gregor20a55e22010-12-22 18:17:10 +0000129 const TemplateArgument *Params, unsigned NumParams,
130 const TemplateArgument *Args, unsigned NumArgs,
131 TemplateDeductionInfo &Info,
Douglas Gregor0972c862010-12-22 18:55:49 +0000132 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
133 bool NumberOfArgumentsMustMatch = true);
Douglas Gregor20a55e22010-12-22 18:17:10 +0000134
Douglas Gregor199d9912009-06-05 00:53:49 +0000135/// \brief If the given expression is of a form that permits the deduction
136/// of a non-type template parameter, return the declaration of that
137/// non-type template parameter.
138static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
139 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
140 E = IC->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Douglas Gregor199d9912009-06-05 00:53:49 +0000142 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
143 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Douglas Gregor199d9912009-06-05 00:53:49 +0000145 return 0;
146}
147
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000148/// \brief Determine whether two declaration pointers refer to the same
149/// declaration.
150static bool isSameDeclaration(Decl *X, Decl *Y) {
151 if (!X || !Y)
152 return !X && !Y;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000153
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000154 if (NamedDecl *NX = dyn_cast<NamedDecl>(X))
155 X = NX->getUnderlyingDecl();
156 if (NamedDecl *NY = dyn_cast<NamedDecl>(Y))
157 Y = NY->getUnderlyingDecl();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000158
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000159 return X->getCanonicalDecl() == Y->getCanonicalDecl();
160}
161
162/// \brief Verify that the given, deduced template arguments are compatible.
163///
164/// \returns The deduced template argument, or a NULL template argument if
165/// the deduced template arguments were incompatible.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000166static DeducedTemplateArgument
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000167checkDeducedTemplateArguments(ASTContext &Context,
168 const DeducedTemplateArgument &X,
169 const DeducedTemplateArgument &Y) {
170 // We have no deduction for one or both of the arguments; they're compatible.
171 if (X.isNull())
172 return Y;
173 if (Y.isNull())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000174 return X;
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000175
176 switch (X.getKind()) {
177 case TemplateArgument::Null:
178 llvm_unreachable("Non-deduced template arguments handled above");
179
180 case TemplateArgument::Type:
181 // If two template type arguments have the same type, they're compatible.
182 if (Y.getKind() == TemplateArgument::Type &&
183 Context.hasSameType(X.getAsType(), Y.getAsType()))
184 return X;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000185
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000186 return DeducedTemplateArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000187
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000188 case TemplateArgument::Integral:
189 // If we deduced a constant in one case and either a dependent expression or
190 // declaration in another case, keep the integral constant.
191 // If both are integral constants with the same value, keep that value.
192 if (Y.getKind() == TemplateArgument::Expression ||
193 Y.getKind() == TemplateArgument::Declaration ||
194 (Y.getKind() == TemplateArgument::Integral &&
195 hasSameExtendedValue(*X.getAsIntegral(), *Y.getAsIntegral())))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000196 return DeducedTemplateArgument(X,
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000197 X.wasDeducedFromArrayBound() &&
198 Y.wasDeducedFromArrayBound());
199
200 // All other combinations are incompatible.
201 return DeducedTemplateArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000202
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000203 case TemplateArgument::Template:
204 if (Y.getKind() == TemplateArgument::Template &&
205 Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate()))
206 return X;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000207
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000208 // All other combinations are incompatible.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000209 return DeducedTemplateArgument();
Douglas Gregora7fc9012011-01-05 18:58:31 +0000210
211 case TemplateArgument::TemplateExpansion:
212 if (Y.getKind() == TemplateArgument::TemplateExpansion &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000213 Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(),
Douglas Gregora7fc9012011-01-05 18:58:31 +0000214 Y.getAsTemplateOrTemplatePattern()))
215 return X;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000216
Douglas Gregora7fc9012011-01-05 18:58:31 +0000217 // All other combinations are incompatible.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000218 return DeducedTemplateArgument();
Douglas Gregora7fc9012011-01-05 18:58:31 +0000219
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000220 case TemplateArgument::Expression:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000221 // If we deduced a dependent expression in one case and either an integral
222 // constant or a declaration in another case, keep the integral constant
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000223 // or declaration.
224 if (Y.getKind() == TemplateArgument::Integral ||
225 Y.getKind() == TemplateArgument::Declaration)
226 return DeducedTemplateArgument(Y, X.wasDeducedFromArrayBound() &&
227 Y.wasDeducedFromArrayBound());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000228
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000229 if (Y.getKind() == TemplateArgument::Expression) {
230 // Compare the expressions for equality
231 llvm::FoldingSetNodeID ID1, ID2;
232 X.getAsExpr()->Profile(ID1, Context, true);
233 Y.getAsExpr()->Profile(ID2, Context, true);
234 if (ID1 == ID2)
235 return X;
236 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000237
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000238 // All other combinations are incompatible.
239 return DeducedTemplateArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000240
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000241 case TemplateArgument::Declaration:
242 // If we deduced a declaration and a dependent expression, keep the
243 // declaration.
244 if (Y.getKind() == TemplateArgument::Expression)
245 return X;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000246
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000247 // If we deduced a declaration and an integral constant, keep the
248 // integral constant.
249 if (Y.getKind() == TemplateArgument::Integral)
250 return Y;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000251
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000252 // If we deduced two declarations, make sure they they refer to the
253 // same declaration.
254 if (Y.getKind() == TemplateArgument::Declaration &&
255 isSameDeclaration(X.getAsDecl(), Y.getAsDecl()))
256 return X;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000257
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000258 // All other combinations are incompatible.
259 return DeducedTemplateArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000260
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000261 case TemplateArgument::Pack:
262 if (Y.getKind() != TemplateArgument::Pack ||
263 X.pack_size() != Y.pack_size())
264 return DeducedTemplateArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000265
266 for (TemplateArgument::pack_iterator XA = X.pack_begin(),
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000267 XAEnd = X.pack_end(),
268 YA = Y.pack_begin();
269 XA != XAEnd; ++XA, ++YA) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000270 if (checkDeducedTemplateArguments(Context,
271 DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()),
Douglas Gregor135ffa72011-01-05 21:00:53 +0000272 DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound()))
273 .isNull())
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000274 return DeducedTemplateArgument();
275 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000276
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000277 return X;
278 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000279
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000280 return DeducedTemplateArgument();
281}
282
Mike Stump1eb44332009-09-09 15:08:12 +0000283/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +0000284/// from the given constant.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000285static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000286DeduceNonTypeTemplateArgument(Sema &S,
Mike Stump1eb44332009-09-09 15:08:12 +0000287 NonTypeTemplateParmDecl *NTTP,
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000288 llvm::APSInt Value, QualType ValueType,
Douglas Gregor02024a92010-03-28 02:42:43 +0000289 bool DeducedFromArrayBound,
John McCall2a7fb272010-08-25 05:32:35 +0000290 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000291 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +0000292 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000293 "Cannot deduce non-type template argument with depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000295 DeducedTemplateArgument NewDeduced(Value, ValueType, DeducedFromArrayBound);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000296 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000297 Deduced[NTTP->getIndex()],
298 NewDeduced);
299 if (Result.isNull()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000300 Info.Param = NTTP;
301 Info.FirstArg = Deduced[NTTP->getIndex()];
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000302 Info.SecondArg = NewDeduced;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000303 return Sema::TDK_Inconsistent;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000304 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000305
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000306 Deduced[NTTP->getIndex()] = Result;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000307 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000308}
309
Mike Stump1eb44332009-09-09 15:08:12 +0000310/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +0000311/// from the given type- or value-dependent expression.
312///
313/// \returns true if deduction succeeded, false otherwise.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000314static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000315DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000316 NonTypeTemplateParmDecl *NTTP,
317 Expr *Value,
John McCall2a7fb272010-08-25 05:32:35 +0000318 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000319 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +0000320 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000321 "Cannot deduce non-type template argument with depth > 0");
322 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
323 "Expression template argument must be type- or value-dependent.");
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000325 DeducedTemplateArgument NewDeduced(Value);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000326 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
327 Deduced[NTTP->getIndex()],
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000328 NewDeduced);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000329
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000330 if (Result.isNull()) {
331 Info.Param = NTTP;
332 Info.FirstArg = Deduced[NTTP->getIndex()];
333 Info.SecondArg = NewDeduced;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000334 return Sema::TDK_Inconsistent;
Douglas Gregor199d9912009-06-05 00:53:49 +0000335 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000336
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000337 Deduced[NTTP->getIndex()] = Result;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000338 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000339}
340
Douglas Gregor15755cb2009-11-13 23:45:44 +0000341/// \brief Deduce the value of the given non-type template parameter
342/// from the given declaration.
343///
344/// \returns true if deduction succeeded, false otherwise.
345static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000346DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregor15755cb2009-11-13 23:45:44 +0000347 NonTypeTemplateParmDecl *NTTP,
348 Decl *D,
John McCall2a7fb272010-08-25 05:32:35 +0000349 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000350 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor15755cb2009-11-13 23:45:44 +0000351 assert(NTTP->getDepth() == 0 &&
352 "Cannot deduce non-type template argument with depth > 0");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000353
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000354 DeducedTemplateArgument NewDeduced(D? D->getCanonicalDecl() : 0);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000355 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000356 Deduced[NTTP->getIndex()],
357 NewDeduced);
358 if (Result.isNull()) {
359 Info.Param = NTTP;
360 Info.FirstArg = Deduced[NTTP->getIndex()];
361 Info.SecondArg = NewDeduced;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000362 return Sema::TDK_Inconsistent;
Douglas Gregor15755cb2009-11-13 23:45:44 +0000363 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000364
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000365 Deduced[NTTP->getIndex()] = Result;
Douglas Gregor15755cb2009-11-13 23:45:44 +0000366 return Sema::TDK_Success;
367}
368
Douglas Gregorf67875d2009-06-12 18:26:56 +0000369static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000370DeduceTemplateArguments(Sema &S,
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000371 TemplateParameterList *TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000372 TemplateName Param,
373 TemplateName Arg,
John McCall2a7fb272010-08-25 05:32:35 +0000374 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000375 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregord708c722009-06-09 16:35:58 +0000376 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000377 if (!ParamDecl) {
378 // The parameter type is dependent and is not a template template parameter,
379 // so there is nothing that we can deduce.
380 return Sema::TDK_Success;
381 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000382
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000383 if (TemplateTemplateParmDecl *TempParam
384 = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) {
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000385 DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000386 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000387 Deduced[TempParam->getIndex()],
388 NewDeduced);
389 if (Result.isNull()) {
390 Info.Param = TempParam;
391 Info.FirstArg = Deduced[TempParam->getIndex()];
392 Info.SecondArg = NewDeduced;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000393 return Sema::TDK_Inconsistent;
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000394 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000395
Douglas Gregor0d80abc2010-12-22 23:09:49 +0000396 Deduced[TempParam->getIndex()] = Result;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000397 return Sema::TDK_Success;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000398 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000399
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000400 // Verify that the two template names are equivalent.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000401 if (S.Context.hasSameTemplateName(Param, Arg))
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000402 return Sema::TDK_Success;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000403
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000404 // Mismatch of non-dependent template parameter to argument.
405 Info.FirstArg = TemplateArgument(Param);
406 Info.SecondArg = TemplateArgument(Arg);
407 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000408}
409
Mike Stump1eb44332009-09-09 15:08:12 +0000410/// \brief Deduce the template arguments by comparing the template parameter
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000411/// type (which is a template-id) with the template argument type.
412///
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000413/// \param S the Sema
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000414///
415/// \param TemplateParams the template parameters that we are deducing
416///
417/// \param Param the parameter type
418///
419/// \param Arg the argument type
420///
421/// \param Info information about the template argument deduction itself
422///
423/// \param Deduced the deduced template arguments
424///
425/// \returns the result of template argument deduction so far. Note that a
426/// "success" result means that template argument deduction has not yet failed,
427/// but it may still fail, later, for other reasons.
428static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000429DeduceTemplateArguments(Sema &S,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000430 TemplateParameterList *TemplateParams,
431 const TemplateSpecializationType *Param,
432 QualType Arg,
John McCall2a7fb272010-08-25 05:32:35 +0000433 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000434 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
John McCall467b27b2009-10-22 20:10:53 +0000435 assert(Arg.isCanonical() && "Argument type must be canonical");
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000437 // Check whether the template argument is a dependent template-id.
Mike Stump1eb44332009-09-09 15:08:12 +0000438 if (const TemplateSpecializationType *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000439 = dyn_cast<TemplateSpecializationType>(Arg)) {
440 // Perform template argument deduction for the template name.
441 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000442 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000443 Param->getTemplateName(),
444 SpecArg->getTemplateName(),
445 Info, Deduced))
446 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000449 // Perform template argument deduction on each template
Douglas Gregor0972c862010-12-22 18:55:49 +0000450 // argument. Ignore any missing/extra arguments, since they could be
451 // filled in by default arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000452 return DeduceTemplateArguments(S, TemplateParams,
453 Param->getArgs(), Param->getNumArgs(),
Douglas Gregor0972c862010-12-22 18:55:49 +0000454 SpecArg->getArgs(), SpecArg->getNumArgs(),
455 Info, Deduced,
456 /*NumberOfArgumentsMustMatch=*/false);
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000457 }
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000459 // If the argument type is a class template specialization, we
460 // perform template argument deduction using its template
461 // arguments.
462 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
463 if (!RecordArg)
464 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000465
466 ClassTemplateSpecializationDecl *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000467 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
468 if (!SpecArg)
469 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000471 // Perform template argument deduction for the template name.
472 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000473 = DeduceTemplateArguments(S,
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000474 TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000475 Param->getTemplateName(),
476 TemplateName(SpecArg->getSpecializedTemplate()),
477 Info, Deduced))
478 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Douglas Gregor20a55e22010-12-22 18:17:10 +0000480 // Perform template argument deduction for the template arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000481 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor20a55e22010-12-22 18:17:10 +0000482 Param->getArgs(), Param->getNumArgs(),
483 SpecArg->getTemplateArgs().data(),
484 SpecArg->getTemplateArgs().size(),
485 Info, Deduced);
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000486}
487
John McCallcd05e812010-08-28 22:14:41 +0000488/// \brief Determines whether the given type is an opaque type that
489/// might be more qualified when instantiated.
490static bool IsPossiblyOpaquelyQualifiedType(QualType T) {
491 switch (T->getTypeClass()) {
492 case Type::TypeOfExpr:
493 case Type::TypeOf:
494 case Type::DependentName:
495 case Type::Decltype:
496 case Type::UnresolvedUsing:
John McCall62c28c82011-01-18 07:41:22 +0000497 case Type::TemplateTypeParm:
John McCallcd05e812010-08-28 22:14:41 +0000498 return true;
499
500 case Type::ConstantArray:
501 case Type::IncompleteArray:
502 case Type::VariableArray:
503 case Type::DependentSizedArray:
504 return IsPossiblyOpaquelyQualifiedType(
505 cast<ArrayType>(T)->getElementType());
506
507 default:
508 return false;
509 }
510}
511
Douglas Gregord3731192011-01-10 07:32:04 +0000512/// \brief Retrieve the depth and index of a template parameter.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000513static std::pair<unsigned, unsigned>
Douglas Gregord3731192011-01-10 07:32:04 +0000514getDepthAndIndex(NamedDecl *ND) {
Douglas Gregor603cfb42011-01-05 23:12:31 +0000515 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
516 return std::make_pair(TTP->getDepth(), TTP->getIndex());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000517
Douglas Gregor603cfb42011-01-05 23:12:31 +0000518 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
519 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000520
Douglas Gregor603cfb42011-01-05 23:12:31 +0000521 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
522 return std::make_pair(TTP->getDepth(), TTP->getIndex());
523}
524
Douglas Gregord3731192011-01-10 07:32:04 +0000525/// \brief Retrieve the depth and index of an unexpanded parameter pack.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000526static std::pair<unsigned, unsigned>
Douglas Gregord3731192011-01-10 07:32:04 +0000527getDepthAndIndex(UnexpandedParameterPack UPP) {
528 if (const TemplateTypeParmType *TTP
529 = UPP.first.dyn_cast<const TemplateTypeParmType *>())
530 return std::make_pair(TTP->getDepth(), TTP->getIndex());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000531
Douglas Gregord3731192011-01-10 07:32:04 +0000532 return getDepthAndIndex(UPP.first.get<NamedDecl *>());
533}
534
Douglas Gregor603cfb42011-01-05 23:12:31 +0000535/// \brief Helper function to build a TemplateParameter when we don't
536/// know its type statically.
537static TemplateParameter makeTemplateParameter(Decl *D) {
538 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
539 return TemplateParameter(TTP);
540 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
541 return TemplateParameter(NTTP);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000542
Douglas Gregor603cfb42011-01-05 23:12:31 +0000543 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
544}
545
Douglas Gregor54293852011-01-10 17:35:05 +0000546/// \brief Prepare to perform template argument deduction for all of the
547/// arguments in a set of argument packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000548static void PrepareArgumentPackDeduction(Sema &S,
549 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor54293852011-01-10 17:35:05 +0000550 const llvm::SmallVectorImpl<unsigned> &PackIndices,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000551 llvm::SmallVectorImpl<DeducedTemplateArgument> &SavedPacks,
Douglas Gregor54293852011-01-10 17:35:05 +0000552 llvm::SmallVectorImpl<
553 llvm::SmallVector<DeducedTemplateArgument, 4> > &NewlyDeducedPacks) {
554 // Save the deduced template arguments for each parameter pack expanded
555 // by this pack expansion, then clear out the deduction.
556 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
557 // Save the previously-deduced argument pack, then clear it out so that we
558 // can deduce a new argument pack.
559 SavedPacks[I] = Deduced[PackIndices[I]];
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000560 Deduced[PackIndices[I]] = TemplateArgument();
561
Douglas Gregor54293852011-01-10 17:35:05 +0000562 // If the template arugment pack was explicitly specified, add that to
563 // the set of deduced arguments.
564 const TemplateArgument *ExplicitArgs;
565 unsigned NumExplicitArgs;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000566 if (NamedDecl *PartiallySubstitutedPack
Douglas Gregor54293852011-01-10 17:35:05 +0000567 = S.CurrentInstantiationScope->getPartiallySubstitutedPack(
568 &ExplicitArgs,
569 &NumExplicitArgs)) {
570 if (getDepthAndIndex(PartiallySubstitutedPack).second == PackIndices[I])
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000571 NewlyDeducedPacks[I].append(ExplicitArgs,
Douglas Gregor54293852011-01-10 17:35:05 +0000572 ExplicitArgs + NumExplicitArgs);
573 }
574 }
575}
576
Douglas Gregor0216f812011-01-10 17:53:52 +0000577/// \brief Finish template argument deduction for a set of argument packs,
578/// producing the argument packs and checking for consistency with prior
579/// deductions.
580static Sema::TemplateDeductionResult
581FinishArgumentPackDeduction(Sema &S,
582 TemplateParameterList *TemplateParams,
583 bool HasAnyArguments,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000584 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor0216f812011-01-10 17:53:52 +0000585 const llvm::SmallVectorImpl<unsigned> &PackIndices,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000586 llvm::SmallVectorImpl<DeducedTemplateArgument> &SavedPacks,
Douglas Gregor0216f812011-01-10 17:53:52 +0000587 llvm::SmallVectorImpl<
588 llvm::SmallVector<DeducedTemplateArgument, 4> > &NewlyDeducedPacks,
589 TemplateDeductionInfo &Info) {
590 // Build argument packs for each of the parameter packs expanded by this
591 // pack expansion.
592 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
593 if (HasAnyArguments && NewlyDeducedPacks[I].empty()) {
594 // We were not able to deduce anything for this parameter pack,
595 // so just restore the saved argument pack.
596 Deduced[PackIndices[I]] = SavedPacks[I];
597 continue;
598 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000599
Douglas Gregor0216f812011-01-10 17:53:52 +0000600 DeducedTemplateArgument NewPack;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000601
Douglas Gregor0216f812011-01-10 17:53:52 +0000602 if (NewlyDeducedPacks[I].empty()) {
603 // If we deduced an empty argument pack, create it now.
604 NewPack = DeducedTemplateArgument(TemplateArgument(0, 0));
605 } else {
606 TemplateArgument *ArgumentPack
Douglas Gregor203e6a32011-01-11 23:09:57 +0000607 = new (S.Context) TemplateArgument [NewlyDeducedPacks[I].size()];
Douglas Gregor0216f812011-01-10 17:53:52 +0000608 std::copy(NewlyDeducedPacks[I].begin(), NewlyDeducedPacks[I].end(),
609 ArgumentPack);
610 NewPack
Douglas Gregor203e6a32011-01-11 23:09:57 +0000611 = DeducedTemplateArgument(TemplateArgument(ArgumentPack,
612 NewlyDeducedPacks[I].size()),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000613 NewlyDeducedPacks[I][0].wasDeducedFromArrayBound());
Douglas Gregor0216f812011-01-10 17:53:52 +0000614 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000615
Douglas Gregor0216f812011-01-10 17:53:52 +0000616 DeducedTemplateArgument Result
617 = checkDeducedTemplateArguments(S.Context, SavedPacks[I], NewPack);
618 if (Result.isNull()) {
619 Info.Param
620 = makeTemplateParameter(TemplateParams->getParam(PackIndices[I]));
621 Info.FirstArg = SavedPacks[I];
622 Info.SecondArg = NewPack;
623 return Sema::TDK_Inconsistent;
624 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000625
Douglas Gregor0216f812011-01-10 17:53:52 +0000626 Deduced[PackIndices[I]] = Result;
627 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000628
Douglas Gregor0216f812011-01-10 17:53:52 +0000629 return Sema::TDK_Success;
630}
631
Douglas Gregor603cfb42011-01-05 23:12:31 +0000632/// \brief Deduce the template arguments by comparing the list of parameter
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000633/// types to the list of argument types, as in the parameter-type-lists of
634/// function types (C++ [temp.deduct.type]p10).
Douglas Gregor603cfb42011-01-05 23:12:31 +0000635///
636/// \param S The semantic analysis object within which we are deducing
637///
638/// \param TemplateParams The template parameters that we are deducing
639///
640/// \param Params The list of parameter types
641///
642/// \param NumParams The number of types in \c Params
643///
644/// \param Args The list of argument types
645///
646/// \param NumArgs The number of types in \c Args
647///
648/// \param Info information about the template argument deduction itself
649///
650/// \param Deduced the deduced template arguments
651///
652/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
653/// how template argument deduction is performed.
654///
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000655/// \param PartialOrdering If true, we are performing template argument
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000656/// deduction for during partial ordering for a call
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000657/// (C++0x [temp.deduct.partial]).
658///
Douglas Gregorb939a192011-01-21 17:29:42 +0000659/// \param RefParamComparisons If we're performing template argument deduction
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000660/// in the context of partial ordering, the set of qualifier comparisons.
661///
Douglas Gregor603cfb42011-01-05 23:12:31 +0000662/// \returns the result of template argument deduction so far. Note that a
663/// "success" result means that template argument deduction has not yet failed,
664/// but it may still fail, later, for other reasons.
665static Sema::TemplateDeductionResult
666DeduceTemplateArguments(Sema &S,
667 TemplateParameterList *TemplateParams,
668 const QualType *Params, unsigned NumParams,
669 const QualType *Args, unsigned NumArgs,
670 TemplateDeductionInfo &Info,
671 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000672 unsigned TDF,
673 bool PartialOrdering = false,
Douglas Gregorb939a192011-01-21 17:29:42 +0000674 llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *
675 RefParamComparisons = 0) {
Douglas Gregor0bbacf82011-01-05 23:23:17 +0000676 // Fast-path check to see if we have too many/too few arguments.
677 if (NumParams != NumArgs &&
678 !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) &&
679 !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1])))
Douglas Gregor3cae5c92011-01-10 20:53:55 +0000680 return Sema::TDK_NonDeducedMismatch;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000681
Douglas Gregor603cfb42011-01-05 23:12:31 +0000682 // C++0x [temp.deduct.type]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000683 // Similarly, if P has a form that contains (T), then each parameter type
684 // Pi of the respective parameter-type- list of P is compared with the
685 // corresponding parameter type Ai of the corresponding parameter-type-list
686 // of A. [...]
Douglas Gregor603cfb42011-01-05 23:12:31 +0000687 unsigned ArgIdx = 0, ParamIdx = 0;
688 for (; ParamIdx != NumParams; ++ParamIdx) {
689 // Check argument types.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000690 const PackExpansionType *Expansion
Douglas Gregor603cfb42011-01-05 23:12:31 +0000691 = dyn_cast<PackExpansionType>(Params[ParamIdx]);
692 if (!Expansion) {
693 // Simple case: compare the parameter and argument types at this point.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000694
Douglas Gregor603cfb42011-01-05 23:12:31 +0000695 // Make sure we have an argument.
696 if (ArgIdx >= NumArgs)
Douglas Gregor3cae5c92011-01-10 20:53:55 +0000697 return Sema::TDK_NonDeducedMismatch;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000698
Douglas Gregor77d6bb92011-01-11 22:21:24 +0000699 if (isa<PackExpansionType>(Args[ArgIdx])) {
700 // C++0x [temp.deduct.type]p22:
701 // If the original function parameter associated with A is a function
702 // parameter pack and the function parameter associated with P is not
703 // a function parameter pack, then template argument deduction fails.
704 return Sema::TDK_NonDeducedMismatch;
705 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000706
Douglas Gregor603cfb42011-01-05 23:12:31 +0000707 if (Sema::TemplateDeductionResult Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000708 = DeduceTemplateArguments(S, TemplateParams,
709 Params[ParamIdx],
710 Args[ArgIdx],
711 Info, Deduced, TDF,
712 PartialOrdering,
Douglas Gregorb939a192011-01-21 17:29:42 +0000713 RefParamComparisons))
Douglas Gregor603cfb42011-01-05 23:12:31 +0000714 return Result;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000715
Douglas Gregor603cfb42011-01-05 23:12:31 +0000716 ++ArgIdx;
717 continue;
718 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000719
Douglas Gregor7d5c0c12011-01-11 01:52:23 +0000720 // C++0x [temp.deduct.type]p5:
721 // The non-deduced contexts are:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000722 // - A function parameter pack that does not occur at the end of the
Douglas Gregor7d5c0c12011-01-11 01:52:23 +0000723 // parameter-declaration-clause.
724 if (ParamIdx + 1 < NumParams)
725 return Sema::TDK_Success;
726
Douglas Gregor603cfb42011-01-05 23:12:31 +0000727 // C++0x [temp.deduct.type]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000728 // If the parameter-declaration corresponding to Pi is a function
Douglas Gregor603cfb42011-01-05 23:12:31 +0000729 // parameter pack, then the type of its declarator- id is compared with
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000730 // each remaining parameter type in the parameter-type-list of A. Each
Douglas Gregor603cfb42011-01-05 23:12:31 +0000731 // comparison deduces template arguments for subsequent positions in the
732 // template parameter packs expanded by the function parameter pack.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000733
Douglas Gregor603cfb42011-01-05 23:12:31 +0000734 // Compute the set of template parameter indices that correspond to
735 // parameter packs expanded by the pack expansion.
736 llvm::SmallVector<unsigned, 2> PackIndices;
737 QualType Pattern = Expansion->getPattern();
738 {
739 llvm::BitVector SawIndices(TemplateParams->size());
740 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
741 S.collectUnexpandedParameterPacks(Pattern, Unexpanded);
742 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
743 unsigned Depth, Index;
744 llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
745 if (Depth == 0 && !SawIndices[Index]) {
746 SawIndices[Index] = true;
747 PackIndices.push_back(Index);
748 }
749 }
750 }
751 assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
752
Douglas Gregord3731192011-01-10 07:32:04 +0000753 // Keep track of the deduced template arguments for each parameter pack
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000754 // expanded by this pack expansion (the outer index) and for each
Douglas Gregord3731192011-01-10 07:32:04 +0000755 // template argument (the inner SmallVectors).
756 llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2>
757 NewlyDeducedPacks(PackIndices.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000758 llvm::SmallVector<DeducedTemplateArgument, 2>
Douglas Gregor54293852011-01-10 17:35:05 +0000759 SavedPacks(PackIndices.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000760 PrepareArgumentPackDeduction(S, Deduced, PackIndices, SavedPacks,
Douglas Gregor54293852011-01-10 17:35:05 +0000761 NewlyDeducedPacks);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000762
Douglas Gregor603cfb42011-01-05 23:12:31 +0000763 bool HasAnyArguments = false;
764 for (; ArgIdx < NumArgs; ++ArgIdx) {
765 HasAnyArguments = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000766
Douglas Gregor603cfb42011-01-05 23:12:31 +0000767 // Deduce template arguments from the pattern.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000768 if (Sema::TemplateDeductionResult Result
Douglas Gregor73b3cf62011-01-25 17:19:08 +0000769 = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx],
770 Info, Deduced, TDF, PartialOrdering,
771 RefParamComparisons))
Douglas Gregor603cfb42011-01-05 23:12:31 +0000772 return Result;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000773
Douglas Gregor603cfb42011-01-05 23:12:31 +0000774 // Capture the deduced template arguments for each parameter pack expanded
775 // by this pack expansion, add them to the list of arguments we've deduced
776 // for that pack, then clear out the deduced argument.
777 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
778 DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
779 if (!DeducedArg.isNull()) {
780 NewlyDeducedPacks[I].push_back(DeducedArg);
781 DeducedArg = DeducedTemplateArgument();
782 }
783 }
784 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000785
Douglas Gregor603cfb42011-01-05 23:12:31 +0000786 // Build argument packs for each of the parameter packs expanded by this
787 // pack expansion.
Douglas Gregor0216f812011-01-10 17:53:52 +0000788 if (Sema::TemplateDeductionResult Result
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000789 = FinishArgumentPackDeduction(S, TemplateParams, HasAnyArguments,
Douglas Gregor0216f812011-01-10 17:53:52 +0000790 Deduced, PackIndices, SavedPacks,
791 NewlyDeducedPacks, Info))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000792 return Result;
Douglas Gregor603cfb42011-01-05 23:12:31 +0000793 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000794
Douglas Gregor603cfb42011-01-05 23:12:31 +0000795 // Make sure we don't have any extra arguments.
796 if (ArgIdx < NumArgs)
Douglas Gregor3cae5c92011-01-10 20:53:55 +0000797 return Sema::TDK_NonDeducedMismatch;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000798
Douglas Gregor603cfb42011-01-05 23:12:31 +0000799 return Sema::TDK_Success;
800}
801
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000802/// \brief Determine whether the parameter has qualifiers that are either
803/// inconsistent with or a superset of the argument's qualifiers.
804static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType,
805 QualType ArgType) {
806 Qualifiers ParamQs = ParamType.getQualifiers();
807 Qualifiers ArgQs = ArgType.getQualifiers();
808
809 if (ParamQs == ArgQs)
810 return false;
811
812 // Mismatched (but not missing) Objective-C GC attributes.
813 if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() &&
814 ParamQs.hasObjCGCAttr())
815 return true;
816
817 // Mismatched (but not missing) address spaces.
818 if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() &&
819 ParamQs.hasAddressSpace())
820 return true;
821
822 // CVR qualifier superset.
823 return (ParamQs.getCVRQualifiers() != ArgQs.getCVRQualifiers()) &&
824 ((ParamQs.getCVRQualifiers() | ArgQs.getCVRQualifiers())
825 == ParamQs.getCVRQualifiers());
826}
827
Douglas Gregor500d3312009-06-26 18:27:22 +0000828/// \brief Deduce the template arguments by comparing the parameter type and
829/// the argument type (C++ [temp.deduct.type]).
830///
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000831/// \param S the semantic analysis object within which we are deducing
Douglas Gregor500d3312009-06-26 18:27:22 +0000832///
833/// \param TemplateParams the template parameters that we are deducing
834///
835/// \param ParamIn the parameter type
836///
837/// \param ArgIn the argument type
838///
839/// \param Info information about the template argument deduction itself
840///
841/// \param Deduced the deduced template arguments
842///
Douglas Gregor508f1c82009-06-26 23:10:12 +0000843/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
Mike Stump1eb44332009-09-09 15:08:12 +0000844/// how template argument deduction is performed.
Douglas Gregor500d3312009-06-26 18:27:22 +0000845///
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000846/// \param PartialOrdering Whether we're performing template argument deduction
847/// in the context of partial ordering (C++0x [temp.deduct.partial]).
848///
Douglas Gregorb939a192011-01-21 17:29:42 +0000849/// \param RefParamComparisons If we're performing template argument deduction
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000850/// in the context of partial ordering, the set of qualifier comparisons.
851///
Douglas Gregor500d3312009-06-26 18:27:22 +0000852/// \returns the result of template argument deduction so far. Note that a
853/// "success" result means that template argument deduction has not yet failed,
854/// but it may still fail, later, for other reasons.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000855static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000856DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000857 TemplateParameterList *TemplateParams,
858 QualType ParamIn, QualType ArgIn,
John McCall2a7fb272010-08-25 05:32:35 +0000859 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000860 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000861 unsigned TDF,
862 bool PartialOrdering,
Douglas Gregorb939a192011-01-21 17:29:42 +0000863 llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *RefParamComparisons) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000864 // We only want to look at the canonical types, since typedefs and
865 // sugar are not part of template argument deduction.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000866 QualType Param = S.Context.getCanonicalType(ParamIn);
867 QualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000868
Douglas Gregor77d6bb92011-01-11 22:21:24 +0000869 // If the argument type is a pack expansion, look at its pattern.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000870 // This isn't explicitly called out
Douglas Gregor77d6bb92011-01-11 22:21:24 +0000871 if (const PackExpansionType *ArgExpansion
872 = dyn_cast<PackExpansionType>(Arg))
873 Arg = ArgExpansion->getPattern();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000874
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000875 if (PartialOrdering) {
876 // C++0x [temp.deduct.partial]p5:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000877 // Before the partial ordering is done, certain transformations are
878 // performed on the types used for partial ordering:
879 // - If P is a reference type, P is replaced by the type referred to.
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000880 const ReferenceType *ParamRef = Param->getAs<ReferenceType>();
881 if (ParamRef)
882 Param = ParamRef->getPointeeType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000883
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000884 // - If A is a reference type, A is replaced by the type referred to.
885 const ReferenceType *ArgRef = Arg->getAs<ReferenceType>();
886 if (ArgRef)
887 Arg = ArgRef->getPointeeType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000888
Douglas Gregorb939a192011-01-21 17:29:42 +0000889 if (RefParamComparisons && ParamRef && ArgRef) {
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000890 // C++0x [temp.deduct.partial]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000891 // If both P and A were reference types (before being replaced with the
892 // type referred to above), determine which of the two types (if any) is
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000893 // more cv-qualified than the other; otherwise the types are considered
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000894 // to be equally cv-qualified for partial ordering purposes. The result
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000895 // of this determination will be used below.
896 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000897 // We save this information for later, using it only when deduction
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000898 // succeeds in both directions.
Douglas Gregorb939a192011-01-21 17:29:42 +0000899 RefParamPartialOrderingComparison Comparison;
900 Comparison.ParamIsRvalueRef = ParamRef->getAs<RValueReferenceType>();
901 Comparison.ArgIsRvalueRef = ArgRef->getAs<RValueReferenceType>();
902 Comparison.Qualifiers = NeitherMoreQualified;
Douglas Gregor769d0cc2011-04-30 17:07:52 +0000903
904 Qualifiers ParamQuals = Param.getQualifiers();
905 Qualifiers ArgQuals = Arg.getQualifiers();
906 if (ParamQuals.isStrictSupersetOf(ArgQuals))
Douglas Gregorb939a192011-01-21 17:29:42 +0000907 Comparison.Qualifiers = ParamMoreQualified;
Douglas Gregor769d0cc2011-04-30 17:07:52 +0000908 else if (ArgQuals.isStrictSupersetOf(ParamQuals))
Douglas Gregorb939a192011-01-21 17:29:42 +0000909 Comparison.Qualifiers = ArgMoreQualified;
910 RefParamComparisons->push_back(Comparison);
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000911 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000912
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000913 // C++0x [temp.deduct.partial]p7:
914 // Remove any top-level cv-qualifiers:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000915 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000916 // version of P.
917 Param = Param.getUnqualifiedType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000918 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000919 // version of A.
920 Arg = Arg.getUnqualifiedType();
921 } else {
922 // C++0x [temp.deduct.call]p4 bullet 1:
923 // - If the original P is a reference type, the deduced A (i.e., the type
924 // referred to by the reference) can be more cv-qualified than the
925 // transformed A.
926 if (TDF & TDF_ParamWithReferenceType) {
927 Qualifiers Quals;
928 QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals);
929 Quals.setCVRQualifiers(Quals.getCVRQualifiers() &
John McCall62c28c82011-01-18 07:41:22 +0000930 Arg.getCVRQualifiers());
Douglas Gregor5c7bf422011-01-11 17:34:58 +0000931 Param = S.Context.getQualifiedType(UnqualParam, Quals);
932 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000933
Douglas Gregor73b3cf62011-01-25 17:19:08 +0000934 if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) {
935 // C++0x [temp.deduct.type]p10:
936 // If P and A are function types that originated from deduction when
937 // taking the address of a function template (14.8.2.2) or when deducing
938 // template arguments from a function declaration (14.8.2.6) and Pi and
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000939 // Ai are parameters of the top-level parameter-type-list of P and A,
940 // respectively, Pi is adjusted if it is an rvalue reference to a
941 // cv-unqualified template parameter and Ai is an lvalue reference, in
942 // which case the type of Pi is changed to be the template parameter
Douglas Gregor73b3cf62011-01-25 17:19:08 +0000943 // type (i.e., T&& is changed to simply T). [ Note: As a result, when
944 // Pi is T&& and Ai is X&, the adjusted Pi will be T, causing T to be
NAKAMURA Takumi00995302011-01-27 07:09:49 +0000945 // deduced as X&. - end note ]
Douglas Gregor73b3cf62011-01-25 17:19:08 +0000946 TDF &= ~TDF_TopLevelParameterTypeList;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000947
Douglas Gregor73b3cf62011-01-25 17:19:08 +0000948 if (const RValueReferenceType *ParamRef
949 = Param->getAs<RValueReferenceType>()) {
950 if (isa<TemplateTypeParmType>(ParamRef->getPointeeType()) &&
951 !ParamRef->getPointeeType().getQualifiers())
952 if (Arg->isLValueReferenceType())
953 Param = ParamRef->getPointeeType();
954 }
955 }
Douglas Gregor500d3312009-06-26 18:27:22 +0000956 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000957
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000958 // If the parameter type is not dependent, there is nothing to deduce.
Douglas Gregor12820292009-09-14 20:00:47 +0000959 if (!Param->isDependentType()) {
Douglas Gregor3cae5c92011-01-10 20:53:55 +0000960 if (!(TDF & TDF_SkipNonDependent) && Param != Arg)
Douglas Gregor12820292009-09-14 20:00:47 +0000961 return Sema::TDK_NonDeducedMismatch;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000962
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000963 return Sema::TDK_Success;
Douglas Gregor12820292009-09-14 20:00:47 +0000964 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000965
Douglas Gregor199d9912009-06-05 00:53:49 +0000966 // C++ [temp.deduct.type]p9:
Mike Stump1eb44332009-09-09 15:08:12 +0000967 // A template type argument T, a template template argument TT or a
968 // template non-type argument i can be deduced if P and A have one of
Douglas Gregor199d9912009-06-05 00:53:49 +0000969 // the following forms:
970 //
971 // T
972 // cv-list T
Mike Stump1eb44332009-09-09 15:08:12 +0000973 if (const TemplateTypeParmType *TemplateTypeParm
John McCall183700f2009-09-21 23:43:11 +0000974 = Param->getAs<TemplateTypeParmType>()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000975 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000976 bool RecanonicalizeArg = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Douglas Gregor9e9fae42009-07-22 20:02:25 +0000978 // If the argument type is an array type, move the qualifiers up to the
979 // top level, so they can be matched with the qualifiers on the parameter.
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000980 if (isa<ArrayType>(Arg)) {
John McCall0953e762009-09-24 19:53:00 +0000981 Qualifiers Quals;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000982 Arg = S.Context.getUnqualifiedArrayType(Arg, Quals);
John McCall0953e762009-09-24 19:53:00 +0000983 if (Quals) {
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000984 Arg = S.Context.getQualifiedType(Arg, Quals);
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000985 RecanonicalizeArg = true;
986 }
987 }
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000989 // The argument type can not be less qualified than the parameter
990 // type.
Douglas Gregor61d0b6b2011-04-28 00:56:09 +0000991 if (!(TDF & TDF_IgnoreQualifiers) &&
992 hasInconsistentOrSupersetQualifiersOf(Param, Arg)) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000993 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
John McCall57e97782010-08-05 09:05:08 +0000994 Info.FirstArg = TemplateArgument(Param);
John McCall833ca992009-10-29 08:12:44 +0000995 Info.SecondArg = TemplateArgument(Arg);
John McCall57e97782010-08-05 09:05:08 +0000996 return Sema::TDK_Underqualified;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000997 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000998
999 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001000 assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function");
John McCall0953e762009-09-24 19:53:00 +00001001 QualType DeducedType = Arg;
John McCall49f4e1c2010-12-10 11:01:00 +00001002
Douglas Gregor61d0b6b2011-04-28 00:56:09 +00001003 // Remove any qualifiers on the parameter from the deduced type.
1004 // We checked the qualifiers for consistency above.
1005 Qualifiers DeducedQs = DeducedType.getQualifiers();
1006 Qualifiers ParamQs = Param.getQualifiers();
1007 DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers());
1008 if (ParamQs.hasObjCGCAttr())
1009 DeducedQs.removeObjCGCAttr();
1010 if (ParamQs.hasAddressSpace())
1011 DeducedQs.removeAddressSpace();
1012 DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(),
1013 DeducedQs);
1014
Douglas Gregorf290e0d2009-07-22 21:30:48 +00001015 if (RecanonicalizeArg)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001016 DeducedType = S.Context.getCanonicalType(DeducedType);
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Douglas Gregor0d80abc2010-12-22 23:09:49 +00001018 DeducedTemplateArgument NewDeduced(DeducedType);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001019 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
Douglas Gregor0d80abc2010-12-22 23:09:49 +00001020 Deduced[Index],
1021 NewDeduced);
1022 if (Result.isNull()) {
1023 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
1024 Info.FirstArg = Deduced[Index];
1025 Info.SecondArg = NewDeduced;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001026 return Sema::TDK_Inconsistent;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001027 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001028
Douglas Gregor0d80abc2010-12-22 23:09:49 +00001029 Deduced[Index] = Result;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001030 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001031 }
1032
Douglas Gregorf67875d2009-06-12 18:26:56 +00001033 // Set up the template argument deduction information for a failure.
John McCall833ca992009-10-29 08:12:44 +00001034 Info.FirstArg = TemplateArgument(ParamIn);
1035 Info.SecondArg = TemplateArgument(ArgIn);
Douglas Gregorf67875d2009-06-12 18:26:56 +00001036
Douglas Gregor0bc15d92011-01-14 05:11:40 +00001037 // If the parameter is an already-substituted template parameter
1038 // pack, do nothing: we don't know which of its arguments to look
1039 // at, so we have to wait until all of the parameter packs in this
1040 // expansion have arguments.
1041 if (isa<SubstTemplateTypeParmPackType>(Param))
1042 return Sema::TDK_Success;
1043
Douglas Gregor508f1c82009-06-26 23:10:12 +00001044 // Check the cv-qualifiers on the parameter and argument types.
1045 if (!(TDF & TDF_IgnoreQualifiers)) {
1046 if (TDF & TDF_ParamWithReferenceType) {
Douglas Gregor61d0b6b2011-04-28 00:56:09 +00001047 if (hasInconsistentOrSupersetQualifiersOf(Param, Arg))
Douglas Gregor508f1c82009-06-26 23:10:12 +00001048 return Sema::TDK_NonDeducedMismatch;
John McCallcd05e812010-08-28 22:14:41 +00001049 } else if (!IsPossiblyOpaquelyQualifiedType(Param)) {
Douglas Gregor508f1c82009-06-26 23:10:12 +00001050 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Mike Stump1eb44332009-09-09 15:08:12 +00001051 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor508f1c82009-06-26 23:10:12 +00001052 }
1053 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001054
Douglas Gregord560d502009-06-04 00:21:18 +00001055 switch (Param->getTypeClass()) {
Douglas Gregor199d9912009-06-05 00:53:49 +00001056 // No deduction possible for these types
1057 case Type::Builtin:
Douglas Gregorf67875d2009-06-12 18:26:56 +00001058 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Douglas Gregor199d9912009-06-05 00:53:49 +00001060 // T *
Douglas Gregord560d502009-06-04 00:21:18 +00001061 case Type::Pointer: {
John McCallc0008342010-05-13 07:48:05 +00001062 QualType PointeeType;
1063 if (const PointerType *PointerArg = Arg->getAs<PointerType>()) {
1064 PointeeType = PointerArg->getPointeeType();
1065 } else if (const ObjCObjectPointerType *PointerArg
1066 = Arg->getAs<ObjCObjectPointerType>()) {
1067 PointeeType = PointerArg->getPointeeType();
1068 } else {
Douglas Gregorf67875d2009-06-12 18:26:56 +00001069 return Sema::TDK_NonDeducedMismatch;
John McCallc0008342010-05-13 07:48:05 +00001070 }
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Douglas Gregor41128772009-06-26 23:27:24 +00001072 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001073 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +00001074 cast<PointerType>(Param)->getPointeeType(),
John McCallc0008342010-05-13 07:48:05 +00001075 PointeeType,
Douglas Gregor41128772009-06-26 23:27:24 +00001076 Info, Deduced, SubTDF);
Douglas Gregord560d502009-06-04 00:21:18 +00001077 }
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Douglas Gregor199d9912009-06-05 00:53:49 +00001079 // T &
Douglas Gregord560d502009-06-04 00:21:18 +00001080 case Type::LValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +00001081 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +00001082 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001083 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001085 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +00001086 cast<LValueReferenceType>(Param)->getPointeeType(),
1087 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +00001088 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +00001089 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001090
Douglas Gregor199d9912009-06-05 00:53:49 +00001091 // T && [C++0x]
Douglas Gregord560d502009-06-04 00:21:18 +00001092 case Type::RValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +00001093 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +00001094 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001095 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001097 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +00001098 cast<RValueReferenceType>(Param)->getPointeeType(),
1099 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +00001100 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +00001101 }
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Douglas Gregor199d9912009-06-05 00:53:49 +00001103 // T [] (implied, but not stated explicitly)
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001104 case Type::IncompleteArray: {
Mike Stump1eb44332009-09-09 15:08:12 +00001105 const IncompleteArrayType *IncompleteArrayArg =
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001106 S.Context.getAsIncompleteArrayType(Arg);
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001107 if (!IncompleteArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001108 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001109
John McCalle4f26e52010-08-19 00:20:19 +00001110 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001111 return DeduceTemplateArguments(S, TemplateParams,
1112 S.Context.getAsIncompleteArrayType(Param)->getElementType(),
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001113 IncompleteArrayArg->getElementType(),
John McCalle4f26e52010-08-19 00:20:19 +00001114 Info, Deduced, SubTDF);
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001115 }
Douglas Gregor199d9912009-06-05 00:53:49 +00001116
1117 // T [integer-constant]
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001118 case Type::ConstantArray: {
Mike Stump1eb44332009-09-09 15:08:12 +00001119 const ConstantArrayType *ConstantArrayArg =
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001120 S.Context.getAsConstantArrayType(Arg);
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001121 if (!ConstantArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001122 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001123
1124 const ConstantArrayType *ConstantArrayParm =
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001125 S.Context.getAsConstantArrayType(Param);
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001126 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregorf67875d2009-06-12 18:26:56 +00001127 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001128
John McCalle4f26e52010-08-19 00:20:19 +00001129 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001130 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001131 ConstantArrayParm->getElementType(),
1132 ConstantArrayArg->getElementType(),
John McCalle4f26e52010-08-19 00:20:19 +00001133 Info, Deduced, SubTDF);
Anders Carlsson4d6fb502009-06-04 04:11:30 +00001134 }
1135
Douglas Gregor199d9912009-06-05 00:53:49 +00001136 // type [i]
1137 case Type::DependentSizedArray: {
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001138 const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
Douglas Gregor199d9912009-06-05 00:53:49 +00001139 if (!ArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001140 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001141
John McCalle4f26e52010-08-19 00:20:19 +00001142 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
1143
Douglas Gregor199d9912009-06-05 00:53:49 +00001144 // Check the element type of the arrays
1145 const DependentSizedArrayType *DependentArrayParm
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001146 = S.Context.getAsDependentSizedArrayType(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +00001147 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001148 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001149 DependentArrayParm->getElementType(),
1150 ArrayArg->getElementType(),
John McCalle4f26e52010-08-19 00:20:19 +00001151 Info, Deduced, SubTDF))
Douglas Gregorf67875d2009-06-12 18:26:56 +00001152 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Douglas Gregor199d9912009-06-05 00:53:49 +00001154 // Determine the array bound is something we can deduce.
Mike Stump1eb44332009-09-09 15:08:12 +00001155 NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +00001156 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
1157 if (!NTTP)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001158 return Sema::TDK_Success;
Mike Stump1eb44332009-09-09 15:08:12 +00001159
1160 // We can perform template argument deduction for the given non-type
Douglas Gregor199d9912009-06-05 00:53:49 +00001161 // template parameter.
Mike Stump1eb44332009-09-09 15:08:12 +00001162 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +00001163 "Cannot deduce non-type template argument at depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +00001164 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson335e24a2009-06-16 22:44:31 +00001165 = dyn_cast<ConstantArrayType>(ArrayArg)) {
1166 llvm::APSInt Size(ConstantArrayArg->getSize());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001167 return DeduceNonTypeTemplateArgument(S, NTTP, Size,
Douglas Gregor9d0e4412010-03-26 05:50:28 +00001168 S.Context.getSizeType(),
Douglas Gregor02024a92010-03-28 02:42:43 +00001169 /*ArrayBound=*/true,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001170 Info, Deduced);
Anders Carlsson335e24a2009-06-16 22:44:31 +00001171 }
Douglas Gregor199d9912009-06-05 00:53:49 +00001172 if (const DependentSizedArrayType *DependentArrayArg
1173 = dyn_cast<DependentSizedArrayType>(ArrayArg))
Douglas Gregor34c2f8c2010-12-22 23:15:38 +00001174 if (DependentArrayArg->getSizeExpr())
1175 return DeduceNonTypeTemplateArgument(S, NTTP,
1176 DependentArrayArg->getSizeExpr(),
1177 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Douglas Gregor199d9912009-06-05 00:53:49 +00001179 // Incomplete type does not match a dependently-sized array type
Douglas Gregorf67875d2009-06-12 18:26:56 +00001180 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +00001181 }
Mike Stump1eb44332009-09-09 15:08:12 +00001182
1183 // type(*)(T)
1184 // T(*)()
1185 // T(*)(T)
Anders Carlssona27fad52009-06-08 15:19:08 +00001186 case Type::FunctionProto: {
Douglas Gregor73b3cf62011-01-25 17:19:08 +00001187 unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList;
Mike Stump1eb44332009-09-09 15:08:12 +00001188 const FunctionProtoType *FunctionProtoArg =
Anders Carlssona27fad52009-06-08 15:19:08 +00001189 dyn_cast<FunctionProtoType>(Arg);
1190 if (!FunctionProtoArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001191 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001192
1193 const FunctionProtoType *FunctionProtoParam =
Anders Carlssona27fad52009-06-08 15:19:08 +00001194 cast<FunctionProtoType>(Param);
Anders Carlsson994b6cb2009-06-08 19:22:23 +00001195
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001196 if (FunctionProtoParam->getTypeQuals()
Douglas Gregore3c7a7c2011-01-26 16:50:54 +00001197 != FunctionProtoArg->getTypeQuals() ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001198 FunctionProtoParam->getRefQualifier()
Douglas Gregore3c7a7c2011-01-26 16:50:54 +00001199 != FunctionProtoArg->getRefQualifier() ||
1200 FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregorf67875d2009-06-12 18:26:56 +00001201 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +00001202
Anders Carlssona27fad52009-06-08 15:19:08 +00001203 // Check return types.
Douglas Gregorf67875d2009-06-12 18:26:56 +00001204 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001205 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001206 FunctionProtoParam->getResultType(),
1207 FunctionProtoArg->getResultType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +00001208 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +00001209 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Douglas Gregor603cfb42011-01-05 23:12:31 +00001211 return DeduceTemplateArguments(S, TemplateParams,
1212 FunctionProtoParam->arg_type_begin(),
1213 FunctionProtoParam->getNumArgs(),
1214 FunctionProtoArg->arg_type_begin(),
1215 FunctionProtoArg->getNumArgs(),
Douglas Gregor73b3cf62011-01-25 17:19:08 +00001216 Info, Deduced, SubTDF);
Anders Carlssona27fad52009-06-08 15:19:08 +00001217 }
Mike Stump1eb44332009-09-09 15:08:12 +00001218
John McCall3cb0ebd2010-03-10 03:28:59 +00001219 case Type::InjectedClassName: {
1220 // Treat a template's injected-class-name as if the template
1221 // specialization type had been used.
John McCall31f17ec2010-04-27 00:57:59 +00001222 Param = cast<InjectedClassNameType>(Param)
1223 ->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +00001224 assert(isa<TemplateSpecializationType>(Param) &&
1225 "injected class name is not a template specialization type");
1226 // fall through
1227 }
1228
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001229 // template-name<T> (where template-name refers to a class template)
Douglas Gregord708c722009-06-09 16:35:58 +00001230 // template-name<i>
Douglas Gregordb0d4b72009-11-11 23:06:43 +00001231 // TT<T>
1232 // TT<i>
1233 // TT<>
Douglas Gregord708c722009-06-09 16:35:58 +00001234 case Type::TemplateSpecialization: {
1235 const TemplateSpecializationType *SpecParam
1236 = cast<TemplateSpecializationType>(Param);
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001238 // Try to deduce template arguments from the template-id.
1239 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001240 = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001241 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Douglas Gregor4a5c15f2009-09-30 22:13:51 +00001243 if (Result && (TDF & TDF_DerivedClass)) {
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001244 // C++ [temp.deduct.call]p3b3:
1245 // If P is a class, and P has the form template-id, then A can be a
1246 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump1eb44332009-09-09 15:08:12 +00001247 // class of the form template-id, A can be a pointer to a derived
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001248 // class pointed to by the deduced A.
1249 //
1250 // More importantly:
Mike Stump1eb44332009-09-09 15:08:12 +00001251 // These alternatives are considered only if type deduction would
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001252 // otherwise fail.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001253 if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
1254 // We cannot inspect base classes as part of deduction when the type
1255 // is incomplete, so either instantiate any templates necessary to
1256 // complete the type, or skip over it if it cannot be completed.
John McCall5769d612010-02-08 23:07:23 +00001257 if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001258 return Result;
1259
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001260 // Use data recursion to crawl through the list of base classes.
Mike Stump1eb44332009-09-09 15:08:12 +00001261 // Visited contains the set of nodes we have already visited, while
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001262 // ToVisit is our stack of records that we still need to visit.
1263 llvm::SmallPtrSet<const RecordType *, 8> Visited;
1264 llvm::SmallVector<const RecordType *, 8> ToVisit;
1265 ToVisit.push_back(RecordT);
1266 bool Successful = false;
Douglas Gregor053105d2010-11-02 00:02:34 +00001267 llvm::SmallVectorImpl<DeducedTemplateArgument> DeducedOrig(0);
1268 DeducedOrig = Deduced;
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001269 while (!ToVisit.empty()) {
1270 // Retrieve the next class in the inheritance hierarchy.
1271 const RecordType *NextT = ToVisit.back();
1272 ToVisit.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001274 // If we have already seen this type, skip it.
1275 if (!Visited.insert(NextT))
1276 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001278 // If this is a base class, try to perform template argument
1279 // deduction from it.
1280 if (NextT != RecordT) {
1281 Sema::TemplateDeductionResult BaseResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001282 = DeduceTemplateArguments(S, TemplateParams, SpecParam,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001283 QualType(NextT, 0), Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001285 // If template argument deduction for this base was successful,
Douglas Gregor053105d2010-11-02 00:02:34 +00001286 // note that we had some success. Otherwise, ignore any deductions
1287 // from this base class.
1288 if (BaseResult == Sema::TDK_Success) {
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001289 Successful = true;
Douglas Gregor053105d2010-11-02 00:02:34 +00001290 DeducedOrig = Deduced;
1291 }
1292 else
1293 Deduced = DeducedOrig;
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001294 }
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001296 // Visit base classes
1297 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
1298 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
1299 BaseEnd = Next->bases_end();
Sebastian Redl9994a342009-10-25 17:03:50 +00001300 Base != BaseEnd; ++Base) {
Mike Stump1eb44332009-09-09 15:08:12 +00001301 assert(Base->getType()->isRecordType() &&
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001302 "Base class that isn't a record?");
Ted Kremenek6217b802009-07-29 21:53:49 +00001303 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001304 }
1305 }
Mike Stump1eb44332009-09-09 15:08:12 +00001306
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001307 if (Successful)
1308 return Sema::TDK_Success;
1309 }
Mike Stump1eb44332009-09-09 15:08:12 +00001310
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001311 }
Mike Stump1eb44332009-09-09 15:08:12 +00001312
Douglas Gregorde0cb8b2009-07-07 23:09:34 +00001313 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +00001314 }
1315
Douglas Gregor637a4092009-06-10 23:47:09 +00001316 // T type::*
1317 // T T::*
1318 // T (type::*)()
1319 // type (T::*)()
1320 // type (type::*)(T)
1321 // type (T::*)(T)
1322 // T (type::*)(T)
1323 // T (T::*)()
1324 // T (T::*)(T)
1325 case Type::MemberPointer: {
1326 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
1327 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
1328 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001329 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +00001330
Douglas Gregorf67875d2009-06-12 18:26:56 +00001331 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001332 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001333 MemPtrParam->getPointeeType(),
1334 MemPtrArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +00001335 Info, Deduced,
1336 TDF & TDF_IgnoreQualifiers))
Douglas Gregorf67875d2009-06-12 18:26:56 +00001337 return Result;
1338
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001339 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001340 QualType(MemPtrParam->getClass(), 0),
1341 QualType(MemPtrArg->getClass(), 0),
Douglas Gregor508f1c82009-06-26 23:10:12 +00001342 Info, Deduced, 0);
Douglas Gregor637a4092009-06-10 23:47:09 +00001343 }
1344
Anders Carlsson9a917e42009-06-12 22:56:54 +00001345 // (clang extension)
1346 //
Mike Stump1eb44332009-09-09 15:08:12 +00001347 // type(^)(T)
1348 // T(^)()
1349 // T(^)(T)
Anders Carlsson859ba502009-06-12 16:23:10 +00001350 case Type::BlockPointer: {
1351 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
1352 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Anders Carlsson859ba502009-06-12 16:23:10 +00001354 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001355 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001356
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001357 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +00001358 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +00001359 BlockPtrArg->getPointeeType(), Info,
Douglas Gregor508f1c82009-06-26 23:10:12 +00001360 Deduced, 0);
Anders Carlsson859ba502009-06-12 16:23:10 +00001361 }
1362
Douglas Gregor637a4092009-06-10 23:47:09 +00001363 case Type::TypeOfExpr:
1364 case Type::TypeOf:
Douglas Gregor4714c122010-03-31 17:34:00 +00001365 case Type::DependentName:
Douglas Gregor637a4092009-06-10 23:47:09 +00001366 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +00001367 return Sema::TDK_Success;
Douglas Gregor637a4092009-06-10 23:47:09 +00001368
Douglas Gregord560d502009-06-04 00:21:18 +00001369 default:
1370 break;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001371 }
1372
1373 // FIXME: Many more cases to go (to go).
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001374 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001375}
1376
Douglas Gregorf67875d2009-06-12 18:26:56 +00001377static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001378DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001379 TemplateParameterList *TemplateParams,
1380 const TemplateArgument &Param,
Douglas Gregor77d6bb92011-01-11 22:21:24 +00001381 TemplateArgument Arg,
John McCall2a7fb272010-08-25 05:32:35 +00001382 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +00001383 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor77d6bb92011-01-11 22:21:24 +00001384 // If the template argument is a pack expansion, perform template argument
1385 // deduction against the pattern of that expansion. This only occurs during
1386 // partial ordering.
1387 if (Arg.isPackExpansion())
1388 Arg = Arg.getPackExpansionPattern();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001389
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001390 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +00001391 case TemplateArgument::Null:
1392 assert(false && "Null template argument in parameter list");
1393 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001394
1395 case TemplateArgument::Type:
Douglas Gregor788cd062009-11-11 01:00:40 +00001396 if (Arg.getKind() == TemplateArgument::Type)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001397 return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
Douglas Gregor788cd062009-11-11 01:00:40 +00001398 Arg.getAsType(), Info, Deduced, 0);
1399 Info.FirstArg = Param;
1400 Info.SecondArg = Arg;
1401 return Sema::TDK_NonDeducedMismatch;
Douglas Gregora7fc9012011-01-05 18:58:31 +00001402
Douglas Gregor788cd062009-11-11 01:00:40 +00001403 case TemplateArgument::Template:
Douglas Gregordb0d4b72009-11-11 23:06:43 +00001404 if (Arg.getKind() == TemplateArgument::Template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001405 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor788cd062009-11-11 01:00:40 +00001406 Param.getAsTemplate(),
Douglas Gregordb0d4b72009-11-11 23:06:43 +00001407 Arg.getAsTemplate(), Info, Deduced);
Douglas Gregor788cd062009-11-11 01:00:40 +00001408 Info.FirstArg = Param;
1409 Info.SecondArg = Arg;
1410 return Sema::TDK_NonDeducedMismatch;
Douglas Gregora7fc9012011-01-05 18:58:31 +00001411
1412 case TemplateArgument::TemplateExpansion:
1413 llvm_unreachable("caller should handle pack expansions");
1414 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001415
Douglas Gregor199d9912009-06-05 00:53:49 +00001416 case TemplateArgument::Declaration:
Douglas Gregor788cd062009-11-11 01:00:40 +00001417 if (Arg.getKind() == TemplateArgument::Declaration &&
1418 Param.getAsDecl()->getCanonicalDecl() ==
1419 Arg.getAsDecl()->getCanonicalDecl())
1420 return Sema::TDK_Success;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001421
Douglas Gregorf67875d2009-06-12 18:26:56 +00001422 Info.FirstArg = Param;
1423 Info.SecondArg = Arg;
1424 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001425
Douglas Gregor199d9912009-06-05 00:53:49 +00001426 case TemplateArgument::Integral:
1427 if (Arg.getKind() == TemplateArgument::Integral) {
Douglas Gregor9d0e4412010-03-26 05:50:28 +00001428 if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral()))
Douglas Gregorf67875d2009-06-12 18:26:56 +00001429 return Sema::TDK_Success;
1430
1431 Info.FirstArg = Param;
1432 Info.SecondArg = Arg;
1433 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +00001434 }
Douglas Gregorf67875d2009-06-12 18:26:56 +00001435
1436 if (Arg.getKind() == TemplateArgument::Expression) {
1437 Info.FirstArg = Param;
1438 Info.SecondArg = Arg;
1439 return Sema::TDK_NonDeducedMismatch;
1440 }
Douglas Gregor199d9912009-06-05 00:53:49 +00001441
Douglas Gregorf67875d2009-06-12 18:26:56 +00001442 Info.FirstArg = Param;
1443 Info.SecondArg = Arg;
1444 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Douglas Gregor199d9912009-06-05 00:53:49 +00001446 case TemplateArgument::Expression: {
Mike Stump1eb44332009-09-09 15:08:12 +00001447 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +00001448 = getDeducedParameterFromExpr(Param.getAsExpr())) {
1449 if (Arg.getKind() == TemplateArgument::Integral)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001450 return DeduceNonTypeTemplateArgument(S, NTTP,
Mike Stump1eb44332009-09-09 15:08:12 +00001451 *Arg.getAsIntegral(),
Douglas Gregor9d0e4412010-03-26 05:50:28 +00001452 Arg.getIntegralType(),
Douglas Gregor02024a92010-03-28 02:42:43 +00001453 /*ArrayBound=*/false,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001454 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +00001455 if (Arg.getKind() == TemplateArgument::Expression)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001456 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +00001457 Info, Deduced);
Douglas Gregor15755cb2009-11-13 23:45:44 +00001458 if (Arg.getKind() == TemplateArgument::Declaration)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001459 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
Douglas Gregor15755cb2009-11-13 23:45:44 +00001460 Info, Deduced);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001461
Douglas Gregorf67875d2009-06-12 18:26:56 +00001462 Info.FirstArg = Param;
1463 Info.SecondArg = Arg;
1464 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +00001465 }
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Douglas Gregor199d9912009-06-05 00:53:49 +00001467 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +00001468 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001469 }
Anders Carlssond01b1da2009-06-15 17:04:53 +00001470 case TemplateArgument::Pack:
Douglas Gregor20a55e22010-12-22 18:17:10 +00001471 llvm_unreachable("Argument packs should be expanded by the caller!");
Douglas Gregor199d9912009-06-05 00:53:49 +00001472 }
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Douglas Gregorf67875d2009-06-12 18:26:56 +00001474 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001475}
1476
Douglas Gregor20a55e22010-12-22 18:17:10 +00001477/// \brief Determine whether there is a template argument to be used for
1478/// deduction.
1479///
1480/// This routine "expands" argument packs in-place, overriding its input
1481/// parameters so that \c Args[ArgIdx] will be the available template argument.
1482///
1483/// \returns true if there is another template argument (which will be at
1484/// \c Args[ArgIdx]), false otherwise.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001485static bool hasTemplateArgumentForDeduction(const TemplateArgument *&Args,
Douglas Gregor20a55e22010-12-22 18:17:10 +00001486 unsigned &ArgIdx,
1487 unsigned &NumArgs) {
1488 if (ArgIdx == NumArgs)
1489 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001490
Douglas Gregor20a55e22010-12-22 18:17:10 +00001491 const TemplateArgument &Arg = Args[ArgIdx];
1492 if (Arg.getKind() != TemplateArgument::Pack)
1493 return true;
1494
1495 assert(ArgIdx == NumArgs - 1 && "Pack not at the end of argument list?");
1496 Args = Arg.pack_begin();
1497 NumArgs = Arg.pack_size();
1498 ArgIdx = 0;
1499 return ArgIdx < NumArgs;
1500}
1501
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001502/// \brief Determine whether the given set of template arguments has a pack
1503/// expansion that is not the last template argument.
1504static bool hasPackExpansionBeforeEnd(const TemplateArgument *Args,
1505 unsigned NumArgs) {
1506 unsigned ArgIdx = 0;
1507 while (ArgIdx < NumArgs) {
1508 const TemplateArgument &Arg = Args[ArgIdx];
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001509
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001510 // Unwrap argument packs.
1511 if (Args[ArgIdx].getKind() == TemplateArgument::Pack) {
1512 Args = Arg.pack_begin();
1513 NumArgs = Arg.pack_size();
1514 ArgIdx = 0;
1515 continue;
1516 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001517
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001518 ++ArgIdx;
1519 if (ArgIdx == NumArgs)
1520 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001521
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001522 if (Arg.isPackExpansion())
1523 return true;
1524 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001525
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001526 return false;
1527}
1528
Douglas Gregor20a55e22010-12-22 18:17:10 +00001529static Sema::TemplateDeductionResult
1530DeduceTemplateArguments(Sema &S,
1531 TemplateParameterList *TemplateParams,
1532 const TemplateArgument *Params, unsigned NumParams,
1533 const TemplateArgument *Args, unsigned NumArgs,
1534 TemplateDeductionInfo &Info,
Douglas Gregor0972c862010-12-22 18:55:49 +00001535 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1536 bool NumberOfArgumentsMustMatch) {
Douglas Gregore02e2622010-12-22 21:19:48 +00001537 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001538 // If the template argument list of P contains a pack expansion that is not
1539 // the last template argument, the entire template argument list is a
Douglas Gregore02e2622010-12-22 21:19:48 +00001540 // non-deduced context.
Douglas Gregor7b976ec2010-12-23 01:24:45 +00001541 if (hasPackExpansionBeforeEnd(Params, NumParams))
1542 return Sema::TDK_Success;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001543
Douglas Gregore02e2622010-12-22 21:19:48 +00001544 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001545 // If P has a form that contains <T> or <i>, then each argument Pi of the
1546 // respective template argument list P is compared with the corresponding
Douglas Gregore02e2622010-12-22 21:19:48 +00001547 // argument Ai of the corresponding template argument list of A.
Douglas Gregor20a55e22010-12-22 18:17:10 +00001548 unsigned ArgIdx = 0, ParamIdx = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001549 for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams);
Douglas Gregor20a55e22010-12-22 18:17:10 +00001550 ++ParamIdx) {
1551 if (!Params[ParamIdx].isPackExpansion()) {
Douglas Gregore02e2622010-12-22 21:19:48 +00001552 // The simple case: deduce template arguments by matching Pi and Ai.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001553
Douglas Gregor20a55e22010-12-22 18:17:10 +00001554 // Check whether we have enough arguments.
1555 if (!hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
Douglas Gregor3cae5c92011-01-10 20:53:55 +00001556 return NumberOfArgumentsMustMatch? Sema::TDK_NonDeducedMismatch
Douglas Gregor0972c862010-12-22 18:55:49 +00001557 : Sema::TDK_Success;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001558
Douglas Gregor77d6bb92011-01-11 22:21:24 +00001559 if (Args[ArgIdx].isPackExpansion()) {
1560 // FIXME: We follow the logic of C++0x [temp.deduct.type]p22 here,
1561 // but applied to pack expansions that are template arguments.
1562 return Sema::TDK_NonDeducedMismatch;
1563 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001564
Douglas Gregore02e2622010-12-22 21:19:48 +00001565 // Perform deduction for this Pi/Ai pair.
Douglas Gregor20a55e22010-12-22 18:17:10 +00001566 if (Sema::TemplateDeductionResult Result
Douglas Gregor77d6bb92011-01-11 22:21:24 +00001567 = DeduceTemplateArguments(S, TemplateParams,
1568 Params[ParamIdx], Args[ArgIdx],
1569 Info, Deduced))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001570 return Result;
1571
Douglas Gregor20a55e22010-12-22 18:17:10 +00001572 // Move to the next argument.
1573 ++ArgIdx;
1574 continue;
1575 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001576
Douglas Gregore02e2622010-12-22 21:19:48 +00001577 // The parameter is a pack expansion.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001578
Douglas Gregore02e2622010-12-22 21:19:48 +00001579 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001580 // If Pi is a pack expansion, then the pattern of Pi is compared with
1581 // each remaining argument in the template argument list of A. Each
1582 // comparison deduces template arguments for subsequent positions in the
Douglas Gregore02e2622010-12-22 21:19:48 +00001583 // template parameter packs expanded by Pi.
1584 TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001585
Douglas Gregore02e2622010-12-22 21:19:48 +00001586 // Compute the set of template parameter indices that correspond to
1587 // parameter packs expanded by the pack expansion.
1588 llvm::SmallVector<unsigned, 2> PackIndices;
1589 {
1590 llvm::BitVector SawIndices(TemplateParams->size());
1591 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1592 S.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1593 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
1594 unsigned Depth, Index;
1595 llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
1596 if (Depth == 0 && !SawIndices[Index]) {
1597 SawIndices[Index] = true;
1598 PackIndices.push_back(Index);
1599 }
1600 }
1601 }
1602 assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001603
Douglas Gregore02e2622010-12-22 21:19:48 +00001604 // FIXME: If there are no remaining arguments, we can bail out early
1605 // and set any deduced parameter packs to an empty argument pack.
1606 // The latter part of this is a (minor) correctness issue.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001607
Douglas Gregore02e2622010-12-22 21:19:48 +00001608 // Save the deduced template arguments for each parameter pack expanded
1609 // by this pack expansion, then clear out the deduction.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001610 llvm::SmallVector<DeducedTemplateArgument, 2>
Douglas Gregore02e2622010-12-22 21:19:48 +00001611 SavedPacks(PackIndices.size());
Douglas Gregor54293852011-01-10 17:35:05 +00001612 llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2>
1613 NewlyDeducedPacks(PackIndices.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001614 PrepareArgumentPackDeduction(S, Deduced, PackIndices, SavedPacks,
Douglas Gregor54293852011-01-10 17:35:05 +00001615 NewlyDeducedPacks);
Douglas Gregore02e2622010-12-22 21:19:48 +00001616
1617 // Keep track of the deduced template arguments for each parameter pack
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001618 // expanded by this pack expansion (the outer index) and for each
Douglas Gregore02e2622010-12-22 21:19:48 +00001619 // template argument (the inner SmallVectors).
Douglas Gregore02e2622010-12-22 21:19:48 +00001620 bool HasAnyArguments = false;
1621 while (hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) {
1622 HasAnyArguments = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001623
Douglas Gregore02e2622010-12-22 21:19:48 +00001624 // Deduce template arguments from the pattern.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001625 if (Sema::TemplateDeductionResult Result
Douglas Gregore02e2622010-12-22 21:19:48 +00001626 = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx],
1627 Info, Deduced))
1628 return Result;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001629
Douglas Gregore02e2622010-12-22 21:19:48 +00001630 // Capture the deduced template arguments for each parameter pack expanded
1631 // by this pack expansion, add them to the list of arguments we've deduced
1632 // for that pack, then clear out the deduced argument.
1633 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
1634 DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
1635 if (!DeducedArg.isNull()) {
1636 NewlyDeducedPacks[I].push_back(DeducedArg);
1637 DeducedArg = DeducedTemplateArgument();
1638 }
1639 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001640
Douglas Gregore02e2622010-12-22 21:19:48 +00001641 ++ArgIdx;
1642 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001643
Douglas Gregore02e2622010-12-22 21:19:48 +00001644 // Build argument packs for each of the parameter packs expanded by this
1645 // pack expansion.
Douglas Gregor0216f812011-01-10 17:53:52 +00001646 if (Sema::TemplateDeductionResult Result
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001647 = FinishArgumentPackDeduction(S, TemplateParams, HasAnyArguments,
Douglas Gregor0216f812011-01-10 17:53:52 +00001648 Deduced, PackIndices, SavedPacks,
1649 NewlyDeducedPacks, Info))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001650 return Result;
Douglas Gregor20a55e22010-12-22 18:17:10 +00001651 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001652
Douglas Gregor20a55e22010-12-22 18:17:10 +00001653 // If there is an argument remaining, then we had too many arguments.
Douglas Gregor0972c862010-12-22 18:55:49 +00001654 if (NumberOfArgumentsMustMatch &&
1655 hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
Douglas Gregor3cae5c92011-01-10 20:53:55 +00001656 return Sema::TDK_NonDeducedMismatch;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001657
Douglas Gregor20a55e22010-12-22 18:17:10 +00001658 return Sema::TDK_Success;
1659}
1660
Mike Stump1eb44332009-09-09 15:08:12 +00001661static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001662DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001663 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001664 const TemplateArgumentList &ParamList,
1665 const TemplateArgumentList &ArgList,
John McCall2a7fb272010-08-25 05:32:35 +00001666 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +00001667 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001668 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor20a55e22010-12-22 18:17:10 +00001669 ParamList.data(), ParamList.size(),
1670 ArgList.data(), ArgList.size(),
1671 Info, Deduced);
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001672}
1673
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001674/// \brief Determine whether two template arguments are the same.
Mike Stump1eb44332009-09-09 15:08:12 +00001675static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001676 const TemplateArgument &X,
1677 const TemplateArgument &Y) {
1678 if (X.getKind() != Y.getKind())
1679 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001680
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001681 switch (X.getKind()) {
1682 case TemplateArgument::Null:
1683 assert(false && "Comparing NULL template argument");
1684 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001685
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001686 case TemplateArgument::Type:
1687 return Context.getCanonicalType(X.getAsType()) ==
1688 Context.getCanonicalType(Y.getAsType());
Mike Stump1eb44332009-09-09 15:08:12 +00001689
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001690 case TemplateArgument::Declaration:
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001691 return X.getAsDecl()->getCanonicalDecl() ==
1692 Y.getAsDecl()->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Douglas Gregor788cd062009-11-11 01:00:40 +00001694 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00001695 case TemplateArgument::TemplateExpansion:
1696 return Context.getCanonicalTemplateName(
1697 X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() ==
1698 Context.getCanonicalTemplateName(
1699 Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001700
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001701 case TemplateArgument::Integral:
1702 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump1eb44332009-09-09 15:08:12 +00001703
Douglas Gregor788cd062009-11-11 01:00:40 +00001704 case TemplateArgument::Expression: {
1705 llvm::FoldingSetNodeID XID, YID;
1706 X.getAsExpr()->Profile(XID, Context, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001707 Y.getAsExpr()->Profile(YID, Context, true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001708 return XID == YID;
1709 }
Mike Stump1eb44332009-09-09 15:08:12 +00001710
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001711 case TemplateArgument::Pack:
1712 if (X.pack_size() != Y.pack_size())
1713 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001714
1715 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
1716 XPEnd = X.pack_end(),
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001717 YP = Y.pack_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001718 XP != XPEnd; ++XP, ++YP)
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001719 if (!isSameTemplateArg(Context, *XP, *YP))
1720 return false;
1721
1722 return true;
1723 }
1724
1725 return false;
1726}
1727
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001728/// \brief Allocate a TemplateArgumentLoc where all locations have
1729/// been initialized to the given location.
1730///
1731/// \param S The semantic analysis object.
1732///
1733/// \param The template argument we are producing template argument
1734/// location information for.
1735///
1736/// \param NTTPType For a declaration template argument, the type of
1737/// the non-type template parameter that corresponds to this template
1738/// argument.
1739///
1740/// \param Loc The source location to use for the resulting template
1741/// argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001742static TemplateArgumentLoc
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001743getTrivialTemplateArgumentLoc(Sema &S,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001744 const TemplateArgument &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001745 QualType NTTPType,
1746 SourceLocation Loc) {
1747 switch (Arg.getKind()) {
1748 case TemplateArgument::Null:
1749 llvm_unreachable("Can't get a NULL template argument here");
1750 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001751
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001752 case TemplateArgument::Type:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001753 return TemplateArgumentLoc(Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001754 S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001755
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001756 case TemplateArgument::Declaration: {
1757 Expr *E
Douglas Gregorba68eca2011-01-05 17:40:24 +00001758 = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001759 .takeAs<Expr>();
1760 return TemplateArgumentLoc(TemplateArgument(E), E);
1761 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001762
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001763 case TemplateArgument::Integral: {
1764 Expr *E
Douglas Gregorba68eca2011-01-05 17:40:24 +00001765 = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001766 return TemplateArgumentLoc(TemplateArgument(E), E);
1767 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001768
Douglas Gregorb6744ef2011-03-02 17:09:35 +00001769 case TemplateArgument::Template:
1770 case TemplateArgument::TemplateExpansion: {
1771 NestedNameSpecifierLocBuilder Builder;
1772 TemplateName Template = Arg.getAsTemplate();
1773 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
1774 Builder.MakeTrivial(S.Context, DTN->getQualifier(), Loc);
1775 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
1776 Builder.MakeTrivial(S.Context, QTN->getQualifier(), Loc);
1777
1778 if (Arg.getKind() == TemplateArgument::Template)
1779 return TemplateArgumentLoc(Arg,
1780 Builder.getWithLocInContext(S.Context),
1781 Loc);
1782
1783
1784 return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(S.Context),
1785 Loc, Loc);
1786 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001787
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001788 case TemplateArgument::Expression:
1789 return TemplateArgumentLoc(Arg, Arg.getAsExpr());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001790
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001791 case TemplateArgument::Pack:
1792 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo());
1793 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001794
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001795 return TemplateArgumentLoc();
1796}
1797
1798
1799/// \brief Convert the given deduced template argument and add it to the set of
1800/// fully-converted template arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001801static bool ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001802 DeducedTemplateArgument Arg,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001803 NamedDecl *Template,
1804 QualType NTTPType,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001805 unsigned ArgumentPackIndex,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001806 TemplateDeductionInfo &Info,
1807 bool InFunctionTemplate,
1808 llvm::SmallVectorImpl<TemplateArgument> &Output) {
1809 if (Arg.getKind() == TemplateArgument::Pack) {
1810 // This is a template argument pack, so check each of its arguments against
1811 // the template parameter.
1812 llvm::SmallVector<TemplateArgument, 2> PackedArgsBuilder;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001813 for (TemplateArgument::pack_iterator PA = Arg.pack_begin(),
Douglas Gregor135ffa72011-01-05 21:00:53 +00001814 PAEnd = Arg.pack_end();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001815 PA != PAEnd; ++PA) {
Douglas Gregord53e16a2011-01-05 20:52:18 +00001816 // When converting the deduced template argument, append it to the
1817 // general output list. We need to do this so that the template argument
1818 // checking logic has all of the prior template arguments available.
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001819 DeducedTemplateArgument InnerArg(*PA);
1820 InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001821 if (ConvertDeducedTemplateArgument(S, Param, InnerArg, Template,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001822 NTTPType, PackedArgsBuilder.size(),
1823 Info, InFunctionTemplate, Output))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001824 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001825
Douglas Gregord53e16a2011-01-05 20:52:18 +00001826 // Move the converted template argument into our argument pack.
1827 PackedArgsBuilder.push_back(Output.back());
1828 Output.pop_back();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001829 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001830
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001831 // Create the resulting argument pack.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001832 Output.push_back(TemplateArgument::CreatePackCopy(S.Context,
Douglas Gregor203e6a32011-01-11 23:09:57 +00001833 PackedArgsBuilder.data(),
1834 PackedArgsBuilder.size()));
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001835 return false;
1836 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001837
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001838 // Convert the deduced template argument into a template
1839 // argument that we can check, almost as if the user had written
1840 // the template argument explicitly.
1841 TemplateArgumentLoc ArgLoc = getTrivialTemplateArgumentLoc(S, Arg, NTTPType,
1842 Info.getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001843
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001844 // Check the template argument, converting it as necessary.
1845 return S.CheckTemplateArgument(Param, ArgLoc,
1846 Template,
1847 Template->getLocation(),
1848 Template->getSourceRange().getEnd(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001849 ArgumentPackIndex,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001850 Output,
1851 InFunctionTemplate
1852 ? (Arg.wasDeducedFromArrayBound()
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001853 ? Sema::CTAK_DeducedFromArrayBound
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001854 : Sema::CTAK_Deduced)
1855 : Sema::CTAK_Specified);
1856}
1857
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001858/// Complete template argument deduction for a class template partial
1859/// specialization.
1860static Sema::TemplateDeductionResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001861FinishTemplateArgumentDeduction(Sema &S,
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001862 ClassTemplatePartialSpecializationDecl *Partial,
1863 const TemplateArgumentList &TemplateArgs,
1864 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
John McCall2a7fb272010-08-25 05:32:35 +00001865 TemplateDeductionInfo &Info) {
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001866 // Trap errors.
1867 Sema::SFINAETrap Trap(S);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001868
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001869 Sema::ContextRAII SavedContext(S, Partial);
1870
1871 // C++ [temp.deduct.type]p2:
1872 // [...] or if any template argument remains neither deduced nor
1873 // explicitly specified, template argument deduction fails.
Douglas Gregor910f8002010-11-07 23:05:16 +00001874 llvm::SmallVector<TemplateArgument, 4> Builder;
Douglas Gregor033a3ca2011-01-04 22:23:38 +00001875 TemplateParameterList *PartialParams = Partial->getTemplateParameters();
1876 for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) {
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001877 NamedDecl *Param = PartialParams->getParam(I);
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001878 if (Deduced[I].isNull()) {
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001879 Info.Param = makeTemplateParameter(Param);
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001880 return Sema::TDK_Incomplete;
1881 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001882
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001883 // We have deduced this argument, so it still needs to be
1884 // checked and converted.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001885
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001886 // First, for a non-type template parameter type that is
1887 // initialized by a declaration, we need the type of the
1888 // corresponding non-type template parameter.
1889 QualType NTTPType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001890 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregord53e16a2011-01-05 20:52:18 +00001891 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001892 NTTPType = NTTP->getType();
Douglas Gregord53e16a2011-01-05 20:52:18 +00001893 if (NTTPType->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001894 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregord53e16a2011-01-05 20:52:18 +00001895 Builder.data(), Builder.size());
1896 NTTPType = S.SubstType(NTTPType,
1897 MultiLevelTemplateArgumentList(TemplateArgs),
1898 NTTP->getLocation(),
1899 NTTP->getDeclName());
1900 if (NTTPType.isNull()) {
1901 Info.Param = makeTemplateParameter(Param);
1902 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001903 Info.reset(TemplateArgumentList::CreateCopy(S.Context,
1904 Builder.data(),
Douglas Gregord53e16a2011-01-05 20:52:18 +00001905 Builder.size()));
1906 return Sema::TDK_SubstitutionFailure;
1907 }
1908 }
1909 }
1910
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001911 if (ConvertDeducedTemplateArgument(S, Param, Deduced[I],
Douglas Gregor6952f1e2011-01-19 20:10:05 +00001912 Partial, NTTPType, 0, Info, false,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001913 Builder)) {
1914 Info.Param = makeTemplateParameter(Param);
1915 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001916 Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
1917 Builder.size()));
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001918 return Sema::TDK_SubstitutionFailure;
1919 }
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001920 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001921
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001922 // Form the template argument list from the deduced template arguments.
1923 TemplateArgumentList *DeducedArgumentList
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001924 = TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001925 Builder.size());
1926
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001927 Info.reset(DeducedArgumentList);
1928
1929 // Substitute the deduced template arguments into the template
1930 // arguments of the class template partial specialization, and
1931 // verify that the instantiated template arguments are both valid
1932 // and are equivalent to the template arguments originally provided
1933 // to the class template.
John McCall2a7fb272010-08-25 05:32:35 +00001934 LocalInstantiationScope InstScope(S);
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001935 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
1936 const TemplateArgumentLoc *PartialTemplateArgs
1937 = Partial->getTemplateArgsAsWritten();
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001938
1939 // Note that we don't provide the langle and rangle locations.
1940 TemplateArgumentListInfo InstArgs;
1941
Douglas Gregore02e2622010-12-22 21:19:48 +00001942 if (S.Subst(PartialTemplateArgs,
1943 Partial->getNumTemplateArgsAsWritten(),
1944 InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
1945 unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx;
1946 if (ParamIdx >= Partial->getTemplateParameters()->size())
1947 ParamIdx = Partial->getTemplateParameters()->size() - 1;
1948
1949 Decl *Param
1950 = const_cast<NamedDecl *>(
1951 Partial->getTemplateParameters()->getParam(ParamIdx));
1952 Info.Param = makeTemplateParameter(Param);
1953 Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument();
1954 return Sema::TDK_SubstitutionFailure;
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001955 }
1956
Douglas Gregor910f8002010-11-07 23:05:16 +00001957 llvm::SmallVector<TemplateArgument, 4> ConvertedInstArgs;
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001958 if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001959 InstArgs, false, ConvertedInstArgs))
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001960 return Sema::TDK_SubstitutionFailure;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001961
Douglas Gregor54c53cc2011-01-04 23:35:54 +00001962 TemplateParameterList *TemplateParams
1963 = ClassTemplate->getTemplateParameters();
1964 for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) {
Douglas Gregor910f8002010-11-07 23:05:16 +00001965 TemplateArgument InstArg = ConvertedInstArgs.data()[I];
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001966 if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
Douglas Gregor2fdc5e82011-01-05 00:13:17 +00001967 Info.Param = makeTemplateParameter(TemplateParams->getParam(I));
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001968 Info.FirstArg = TemplateArgs[I];
1969 Info.SecondArg = InstArg;
1970 return Sema::TDK_NonDeducedMismatch;
1971 }
1972 }
1973
1974 if (Trap.hasErrorOccurred())
1975 return Sema::TDK_SubstitutionFailure;
1976
1977 return Sema::TDK_Success;
1978}
1979
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001980/// \brief Perform template argument deduction to determine whether
1981/// the given template arguments match the given class template
1982/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +00001983Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001984Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001985 const TemplateArgumentList &TemplateArgs,
1986 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001987 // C++ [temp.class.spec.match]p2:
1988 // A partial specialization matches a given actual template
1989 // argument list if the template arguments of the partial
1990 // specialization can be deduced from the actual template argument
1991 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +00001992 SFINAETrap Trap(*this);
Douglas Gregor02024a92010-03-28 02:42:43 +00001993 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001994 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +00001995 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001996 = ::DeduceTemplateArguments(*this,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001997 Partial->getTemplateParameters(),
Mike Stump1eb44332009-09-09 15:08:12 +00001998 Partial->getTemplateArgs(),
Douglas Gregorf67875d2009-06-12 18:26:56 +00001999 TemplateArgs, Info, Deduced))
2000 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +00002001
Douglas Gregor637a4092009-06-10 23:47:09 +00002002 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
Douglas Gregor9b623632010-10-12 23:32:35 +00002003 Deduced.data(), Deduced.size(), Info);
Douglas Gregor637a4092009-06-10 23:47:09 +00002004 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +00002005 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +00002006
Douglas Gregorbb260412009-06-14 08:02:22 +00002007 if (Trap.hasErrorOccurred())
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002008 return Sema::TDK_SubstitutionFailure;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002009
2010 return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002011 Deduced, Info);
Douglas Gregor0b9247f2009-06-04 00:03:07 +00002012}
Douglas Gregor031a5882009-06-13 00:26:55 +00002013
Douglas Gregor41128772009-06-26 23:27:24 +00002014/// \brief Determine whether the given type T is a simple-template-id type.
2015static bool isSimpleTemplateIdType(QualType T) {
Mike Stump1eb44332009-09-09 15:08:12 +00002016 if (const TemplateSpecializationType *Spec
John McCall183700f2009-09-21 23:43:11 +00002017 = T->getAs<TemplateSpecializationType>())
Douglas Gregor41128772009-06-26 23:27:24 +00002018 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002019
Douglas Gregor41128772009-06-26 23:27:24 +00002020 return false;
2021}
Douglas Gregor83314aa2009-07-08 20:55:45 +00002022
2023/// \brief Substitute the explicitly-provided template arguments into the
2024/// given function template according to C++ [temp.arg.explicit].
2025///
2026/// \param FunctionTemplate the function template into which the explicit
2027/// template arguments will be substituted.
2028///
Mike Stump1eb44332009-09-09 15:08:12 +00002029/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor83314aa2009-07-08 20:55:45 +00002030/// arguments.
2031///
Mike Stump1eb44332009-09-09 15:08:12 +00002032/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor83314aa2009-07-08 20:55:45 +00002033/// with the converted and checked explicit template arguments.
2034///
Mike Stump1eb44332009-09-09 15:08:12 +00002035/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor83314aa2009-07-08 20:55:45 +00002036/// parameters.
2037///
2038/// \param FunctionType if non-NULL, the result type of the function template
2039/// will also be instantiated and the pointed-to value will be updated with
2040/// the instantiated function type.
2041///
2042/// \param Info if substitution fails for any reason, this object will be
2043/// populated with more information about the failure.
2044///
2045/// \returns TDK_Success if substitution was successful, or some failure
2046/// condition.
2047Sema::TemplateDeductionResult
2048Sema::SubstituteExplicitTemplateArguments(
2049 FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor67714232011-03-03 02:41:12 +00002050 TemplateArgumentListInfo &ExplicitTemplateArgs,
Douglas Gregor02024a92010-03-28 02:42:43 +00002051 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002052 llvm::SmallVectorImpl<QualType> &ParamTypes,
2053 QualType *FunctionType,
2054 TemplateDeductionInfo &Info) {
2055 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2056 TemplateParameterList *TemplateParams
2057 = FunctionTemplate->getTemplateParameters();
2058
John McCalld5532b62009-11-23 01:53:49 +00002059 if (ExplicitTemplateArgs.size() == 0) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00002060 // No arguments to substitute; just copy over the parameter types and
2061 // fill in the function type.
2062 for (FunctionDecl::param_iterator P = Function->param_begin(),
2063 PEnd = Function->param_end();
2064 P != PEnd;
2065 ++P)
2066 ParamTypes.push_back((*P)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00002067
Douglas Gregor83314aa2009-07-08 20:55:45 +00002068 if (FunctionType)
2069 *FunctionType = Function->getType();
2070 return TDK_Success;
2071 }
Mike Stump1eb44332009-09-09 15:08:12 +00002072
Douglas Gregor83314aa2009-07-08 20:55:45 +00002073 // Substitution of the explicit template arguments into a function template
2074 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002075 SFINAETrap Trap(*this);
2076
Douglas Gregor83314aa2009-07-08 20:55:45 +00002077 // C++ [temp.arg.explicit]p3:
Mike Stump1eb44332009-09-09 15:08:12 +00002078 // Template arguments that are present shall be specified in the
2079 // declaration order of their corresponding template-parameters. The
Douglas Gregor83314aa2009-07-08 20:55:45 +00002080 // template argument list shall not specify more template-arguments than
Mike Stump1eb44332009-09-09 15:08:12 +00002081 // there are corresponding template-parameters.
Douglas Gregor910f8002010-11-07 23:05:16 +00002082 llvm::SmallVector<TemplateArgument, 4> Builder;
Mike Stump1eb44332009-09-09 15:08:12 +00002083
2084 // Enter a new template instantiation context where we check the
Douglas Gregor83314aa2009-07-08 20:55:45 +00002085 // explicitly-specified template arguments against this function template,
2086 // and then substitute them into the function parameter types.
Mike Stump1eb44332009-09-09 15:08:12 +00002087 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00002088 FunctionTemplate, Deduced.data(), Deduced.size(),
Douglas Gregor9b623632010-10-12 23:32:35 +00002089 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution,
2090 Info);
Douglas Gregor83314aa2009-07-08 20:55:45 +00002091 if (Inst)
2092 return TDK_InstantiationDepth;
Mike Stump1eb44332009-09-09 15:08:12 +00002093
Douglas Gregor83314aa2009-07-08 20:55:45 +00002094 if (CheckTemplateArgumentList(FunctionTemplate,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002095 SourceLocation(),
John McCalld5532b62009-11-23 01:53:49 +00002096 ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002097 true,
Douglas Gregorf1a84452010-05-08 19:15:54 +00002098 Builder) || Trap.hasErrorOccurred()) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002099 unsigned Index = Builder.size();
Douglas Gregorfe52c912010-05-09 01:26:06 +00002100 if (Index >= TemplateParams->size())
2101 Index = TemplateParams->size() - 1;
2102 Info.Param = makeTemplateParameter(TemplateParams->getParam(Index));
Douglas Gregor83314aa2009-07-08 20:55:45 +00002103 return TDK_InvalidExplicitArguments;
Douglas Gregorf1a84452010-05-08 19:15:54 +00002104 }
Mike Stump1eb44332009-09-09 15:08:12 +00002105
Douglas Gregor83314aa2009-07-08 20:55:45 +00002106 // Form the template argument list from the explicitly-specified
2107 // template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00002108 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor910f8002010-11-07 23:05:16 +00002109 = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
Douglas Gregor83314aa2009-07-08 20:55:45 +00002110 Info.reset(ExplicitArgumentList);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002111
John McCalldf41f182010-10-12 19:40:14 +00002112 // Template argument deduction and the final substitution should be
2113 // done in the context of the templated declaration. Explicit
2114 // argument substitution, on the other hand, needs to happen in the
2115 // calling context.
2116 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
2117
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002118 // If we deduced template arguments for a template parameter pack,
Douglas Gregord3731192011-01-10 07:32:04 +00002119 // note that the template argument pack is partially substituted and record
2120 // the explicit template arguments. They'll be used as part of deduction
2121 // for this template parameter pack.
Douglas Gregord3731192011-01-10 07:32:04 +00002122 for (unsigned I = 0, N = Builder.size(); I != N; ++I) {
2123 const TemplateArgument &Arg = Builder[I];
2124 if (Arg.getKind() == TemplateArgument::Pack) {
Douglas Gregord3731192011-01-10 07:32:04 +00002125 CurrentInstantiationScope->SetPartiallySubstitutedPack(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002126 TemplateParams->getParam(I),
Douglas Gregord3731192011-01-10 07:32:04 +00002127 Arg.pack_begin(),
2128 Arg.pack_size());
2129 break;
2130 }
2131 }
2132
Douglas Gregor83314aa2009-07-08 20:55:45 +00002133 // Instantiate the types of each of the function parameters given the
2134 // explicitly-specified template arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002135 if (SubstParmTypes(Function->getLocation(),
Douglas Gregora009b592011-01-07 00:20:55 +00002136 Function->param_begin(), Function->getNumParams(),
2137 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
2138 ParamTypes))
2139 return TDK_SubstitutionFailure;
Douglas Gregor83314aa2009-07-08 20:55:45 +00002140
2141 // If the caller wants a full function type back, instantiate the return
2142 // type and form that function type.
2143 if (FunctionType) {
2144 // FIXME: exception-specifications?
Mike Stump1eb44332009-09-09 15:08:12 +00002145 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00002146 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor83314aa2009-07-08 20:55:45 +00002147 assert(Proto && "Function template does not have a prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00002148
2149 QualType ResultType
Douglas Gregor357bbd02009-08-28 20:50:45 +00002150 = SubstType(Proto->getResultType(),
2151 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
2152 Function->getTypeSpecStartLoc(),
2153 Function->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00002154 if (ResultType.isNull() || Trap.hasErrorOccurred())
2155 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00002156
2157 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002158 ParamTypes.data(), ParamTypes.size(),
2159 Proto->isVariadic(),
2160 Proto->getTypeQuals(),
Douglas Gregorc938c162011-01-26 05:01:58 +00002161 Proto->getRefQualifier(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00002162 Function->getLocation(),
Eli Friedmanfa869542010-08-05 02:54:05 +00002163 Function->getDeclName(),
2164 Proto->getExtInfo());
Douglas Gregor83314aa2009-07-08 20:55:45 +00002165 if (FunctionType->isNull() || Trap.hasErrorOccurred())
2166 return TDK_SubstitutionFailure;
2167 }
Mike Stump1eb44332009-09-09 15:08:12 +00002168
Douglas Gregor83314aa2009-07-08 20:55:45 +00002169 // C++ [temp.arg.explicit]p2:
Mike Stump1eb44332009-09-09 15:08:12 +00002170 // Trailing template arguments that can be deduced (14.8.2) may be
2171 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor83314aa2009-07-08 20:55:45 +00002172 // template arguments can be deduced, they may all be omitted; in this
2173 // case, the empty template argument list <> itself may also be omitted.
2174 //
Douglas Gregord3731192011-01-10 07:32:04 +00002175 // Take all of the explicitly-specified arguments and put them into
2176 // the set of deduced template arguments. Explicitly-specified
2177 // parameter packs, however, will be set to NULL since the deduction
2178 // mechanisms handle explicitly-specified argument packs directly.
Douglas Gregor83314aa2009-07-08 20:55:45 +00002179 Deduced.reserve(TemplateParams->size());
Douglas Gregord3731192011-01-10 07:32:04 +00002180 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) {
2181 const TemplateArgument &Arg = ExplicitArgumentList->get(I);
2182 if (Arg.getKind() == TemplateArgument::Pack)
2183 Deduced.push_back(DeducedTemplateArgument());
2184 else
2185 Deduced.push_back(Arg);
2186 }
Mike Stump1eb44332009-09-09 15:08:12 +00002187
Douglas Gregor83314aa2009-07-08 20:55:45 +00002188 return TDK_Success;
2189}
2190
Mike Stump1eb44332009-09-09 15:08:12 +00002191/// \brief Finish template argument deduction for a function template,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002192/// checking the deduced template arguments for completeness and forming
2193/// the function template specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00002194Sema::TemplateDeductionResult
Douglas Gregor83314aa2009-07-08 20:55:45 +00002195Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor02024a92010-03-28 02:42:43 +00002196 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
2197 unsigned NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002198 FunctionDecl *&Specialization,
2199 TemplateDeductionInfo &Info) {
2200 TemplateParameterList *TemplateParams
2201 = FunctionTemplate->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00002202
Douglas Gregor83314aa2009-07-08 20:55:45 +00002203 // Template argument deduction for function templates in a SFINAE context.
2204 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002205 SFINAETrap Trap(*this);
2206
Douglas Gregor83314aa2009-07-08 20:55:45 +00002207 // Enter a new template instantiation context while we instantiate the
2208 // actual function declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002209 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00002210 FunctionTemplate, Deduced.data(), Deduced.size(),
Douglas Gregor9b623632010-10-12 23:32:35 +00002211 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution,
2212 Info);
Douglas Gregor83314aa2009-07-08 20:55:45 +00002213 if (Inst)
Mike Stump1eb44332009-09-09 15:08:12 +00002214 return TDK_InstantiationDepth;
2215
John McCall96db3102010-04-29 01:18:58 +00002216 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
John McCallf5813822010-04-29 00:35:03 +00002217
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002218 // C++ [temp.deduct.type]p2:
2219 // [...] or if any template argument remains neither deduced nor
2220 // explicitly specified, template argument deduction fails.
Douglas Gregor910f8002010-11-07 23:05:16 +00002221 llvm::SmallVector<TemplateArgument, 4> Builder;
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002222 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
2223 NamedDecl *Param = TemplateParams->getParam(I);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002224
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002225 if (!Deduced[I].isNull()) {
Douglas Gregor3273b0c2010-10-12 18:51:08 +00002226 if (I < NumExplicitlySpecified) {
Douglas Gregor02024a92010-03-28 02:42:43 +00002227 // We have already fully type-checked and converted this
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002228 // argument, because it was explicitly-specified. Just record the
Douglas Gregor3273b0c2010-10-12 18:51:08 +00002229 // presence of this argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002230 Builder.push_back(Deduced[I]);
Douglas Gregor02024a92010-03-28 02:42:43 +00002231 continue;
2232 }
2233
2234 // We have deduced this argument, so it still needs to be
2235 // checked and converted.
2236
2237 // First, for a non-type template parameter type that is
2238 // initialized by a declaration, we need the type of the
2239 // corresponding non-type template parameter.
2240 QualType NTTPType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002241 if (NonTypeTemplateParmDecl *NTTP
2242 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002243 NTTPType = NTTP->getType();
2244 if (NTTPType->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002245 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002246 Builder.data(), Builder.size());
2247 NTTPType = SubstType(NTTPType,
2248 MultiLevelTemplateArgumentList(TemplateArgs),
2249 NTTP->getLocation(),
2250 NTTP->getDeclName());
2251 if (NTTPType.isNull()) {
2252 Info.Param = makeTemplateParameter(Param);
2253 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002254 Info.reset(TemplateArgumentList::CreateCopy(Context,
2255 Builder.data(),
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002256 Builder.size()));
2257 return TDK_SubstitutionFailure;
Douglas Gregor02024a92010-03-28 02:42:43 +00002258 }
2259 }
2260 }
2261
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002262 if (ConvertDeducedTemplateArgument(*this, Param, Deduced[I],
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002263 FunctionTemplate, NTTPType, 0, Info,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002264 true, Builder)) {
Douglas Gregorb9a7d6f2011-01-04 22:13:36 +00002265 Info.Param = makeTemplateParameter(Param);
Douglas Gregor910f8002010-11-07 23:05:16 +00002266 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002267 Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
2268 Builder.size()));
Douglas Gregor02024a92010-03-28 02:42:43 +00002269 return TDK_SubstitutionFailure;
2270 }
2271
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002272 continue;
2273 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002274
Douglas Gregorea6c96f2010-12-23 01:52:01 +00002275 // C++0x [temp.arg.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002276 // A trailing template parameter pack (14.5.3) not otherwise deduced will
Douglas Gregorea6c96f2010-12-23 01:52:01 +00002277 // be deduced to an empty sequence of template arguments.
2278 // FIXME: Where did the word "trailing" come from?
2279 if (Param->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002280 // We may have had explicitly-specified template arguments for this
2281 // template parameter pack. If so, our empty deduction extends the
2282 // explicitly-specified set (C++0x [temp.arg.explicit]p9).
2283 const TemplateArgument *ExplicitArgs;
2284 unsigned NumExplicitArgs;
2285 if (CurrentInstantiationScope->getPartiallySubstitutedPack(&ExplicitArgs,
2286 &NumExplicitArgs)
2287 == Param)
2288 Builder.push_back(TemplateArgument(ExplicitArgs, NumExplicitArgs));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002289 else
Douglas Gregord3731192011-01-10 07:32:04 +00002290 Builder.push_back(TemplateArgument(0, 0));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002291
Douglas Gregorea6c96f2010-12-23 01:52:01 +00002292 continue;
2293 }
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002294
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002295 // Substitute into the default template argument, if available.
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002296 TemplateArgumentLoc DefArg
2297 = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
2298 FunctionTemplate->getLocation(),
2299 FunctionTemplate->getSourceRange().getEnd(),
2300 Param,
2301 Builder);
2302
2303 // If there was no default argument, deduction is incomplete.
2304 if (DefArg.getArgument().isNull()) {
2305 Info.Param = makeTemplateParameter(
2306 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
2307 return TDK_Incomplete;
2308 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002309
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002310 // Check whether we can actually use the default argument.
2311 if (CheckTemplateArgument(Param, DefArg,
2312 FunctionTemplate,
2313 FunctionTemplate->getLocation(),
2314 FunctionTemplate->getSourceRange().getEnd(),
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002315 0, Builder,
Douglas Gregor8735b292011-06-03 02:59:40 +00002316 CTAK_Specified)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002317 Info.Param = makeTemplateParameter(
2318 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregor910f8002010-11-07 23:05:16 +00002319 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002320 Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002321 Builder.size()));
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002322 return TDK_SubstitutionFailure;
2323 }
2324
2325 // If we get here, we successfully used the default template argument.
2326 }
2327
2328 // Form the template argument list from the deduced template arguments.
2329 TemplateArgumentList *DeducedArgumentList
Douglas Gregor910f8002010-11-07 23:05:16 +00002330 = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002331 Info.reset(DeducedArgumentList);
2332
Mike Stump1eb44332009-09-09 15:08:12 +00002333 // Substitute the deduced template arguments into the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00002334 // declaration to produce the function template specialization.
Douglas Gregord4598a22010-04-28 04:52:24 +00002335 DeclContext *Owner = FunctionTemplate->getDeclContext();
2336 if (FunctionTemplate->getFriendObjectKind())
2337 Owner = FunctionTemplate->getLexicalDeclContext();
Douglas Gregor83314aa2009-07-08 20:55:45 +00002338 Specialization = cast_or_null<FunctionDecl>(
Douglas Gregord4598a22010-04-28 04:52:24 +00002339 SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner,
Douglas Gregor357bbd02009-08-28 20:50:45 +00002340 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor83314aa2009-07-08 20:55:45 +00002341 if (!Specialization)
2342 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00002343
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002344 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
Douglas Gregorf8825742009-09-15 18:26:13 +00002345 FunctionTemplate->getCanonicalDecl());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002346
Mike Stump1eb44332009-09-09 15:08:12 +00002347 // If the template argument list is owned by the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00002348 // specialization, release it.
Douglas Gregorec20f462010-05-08 20:07:26 +00002349 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList &&
2350 !Trap.hasErrorOccurred())
Douglas Gregor83314aa2009-07-08 20:55:45 +00002351 Info.take();
Mike Stump1eb44332009-09-09 15:08:12 +00002352
Douglas Gregor83314aa2009-07-08 20:55:45 +00002353 // There may have been an error that did not prevent us from constructing a
2354 // declaration. Mark the declaration invalid and return with a substitution
2355 // failure.
2356 if (Trap.hasErrorOccurred()) {
2357 Specialization->setInvalidDecl(true);
2358 return TDK_SubstitutionFailure;
2359 }
Mike Stump1eb44332009-09-09 15:08:12 +00002360
Douglas Gregor9b623632010-10-12 23:32:35 +00002361 // If we suppressed any diagnostics while performing template argument
2362 // deduction, and if we haven't already instantiated this declaration,
2363 // keep track of these diagnostics. They'll be emitted if this specialization
2364 // is actually used.
2365 if (Info.diag_begin() != Info.diag_end()) {
2366 llvm::DenseMap<Decl *, llvm::SmallVector<PartialDiagnosticAt, 1> >::iterator
2367 Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl());
2368 if (Pos == SuppressedDiagnostics.end())
2369 SuppressedDiagnostics[Specialization->getCanonicalDecl()]
2370 .append(Info.diag_begin(), Info.diag_end());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002371 }
Douglas Gregor9b623632010-10-12 23:32:35 +00002372
Mike Stump1eb44332009-09-09 15:08:12 +00002373 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00002374}
2375
John McCall9c72c602010-08-27 09:08:28 +00002376/// Gets the type of a function for template-argument-deducton
2377/// purposes when it's considered as part of an overload set.
John McCalleff92132010-02-02 02:21:27 +00002378static QualType GetTypeOfFunction(ASTContext &Context,
John McCall9c72c602010-08-27 09:08:28 +00002379 const OverloadExpr::FindResult &R,
John McCalleff92132010-02-02 02:21:27 +00002380 FunctionDecl *Fn) {
John McCalleff92132010-02-02 02:21:27 +00002381 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
John McCall9c72c602010-08-27 09:08:28 +00002382 if (Method->isInstance()) {
2383 // An instance method that's referenced in a form that doesn't
2384 // look like a member pointer is just invalid.
2385 if (!R.HasFormOfMemberPointer) return QualType();
2386
John McCalleff92132010-02-02 02:21:27 +00002387 return Context.getMemberPointerType(Fn->getType(),
2388 Context.getTypeDeclType(Method->getParent()).getTypePtr());
John McCall9c72c602010-08-27 09:08:28 +00002389 }
2390
2391 if (!R.IsAddressOfOperand) return Fn->getType();
John McCalleff92132010-02-02 02:21:27 +00002392 return Context.getPointerType(Fn->getType());
2393}
2394
2395/// Apply the deduction rules for overload sets.
2396///
2397/// \return the null type if this argument should be treated as an
2398/// undeduced context
2399static QualType
2400ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
Douglas Gregor75f21af2010-08-30 21:04:23 +00002401 Expr *Arg, QualType ParamType,
2402 bool ParamWasReference) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002403
John McCall9c72c602010-08-27 09:08:28 +00002404 OverloadExpr::FindResult R = OverloadExpr::find(Arg);
John McCalleff92132010-02-02 02:21:27 +00002405
John McCall9c72c602010-08-27 09:08:28 +00002406 OverloadExpr *Ovl = R.Expression;
John McCalleff92132010-02-02 02:21:27 +00002407
Douglas Gregor75f21af2010-08-30 21:04:23 +00002408 // C++0x [temp.deduct.call]p4
2409 unsigned TDF = 0;
2410 if (ParamWasReference)
2411 TDF |= TDF_ParamWithReferenceType;
2412 if (R.IsAddressOfOperand)
2413 TDF |= TDF_IgnoreQualifiers;
2414
John McCalleff92132010-02-02 02:21:27 +00002415 // If there were explicit template arguments, we can only find
2416 // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
2417 // unambiguously name a full specialization.
John McCall7bb12da2010-02-02 06:20:04 +00002418 if (Ovl->hasExplicitTemplateArgs()) {
John McCalleff92132010-02-02 02:21:27 +00002419 // But we can still look for an explicit specialization.
2420 if (FunctionDecl *ExplicitSpec
John McCall7bb12da2010-02-02 06:20:04 +00002421 = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
John McCall9c72c602010-08-27 09:08:28 +00002422 return GetTypeOfFunction(S.Context, R, ExplicitSpec);
John McCalleff92132010-02-02 02:21:27 +00002423 return QualType();
2424 }
2425
2426 // C++0x [temp.deduct.call]p6:
2427 // When P is a function type, pointer to function type, or pointer
2428 // to member function type:
2429
2430 if (!ParamType->isFunctionType() &&
2431 !ParamType->isFunctionPointerType() &&
2432 !ParamType->isMemberFunctionPointerType())
2433 return QualType();
2434
2435 QualType Match;
John McCall7bb12da2010-02-02 06:20:04 +00002436 for (UnresolvedSetIterator I = Ovl->decls_begin(),
2437 E = Ovl->decls_end(); I != E; ++I) {
John McCalleff92132010-02-02 02:21:27 +00002438 NamedDecl *D = (*I)->getUnderlyingDecl();
2439
2440 // - If the argument is an overload set containing one or more
2441 // function templates, the parameter is treated as a
2442 // non-deduced context.
2443 if (isa<FunctionTemplateDecl>(D))
2444 return QualType();
2445
2446 FunctionDecl *Fn = cast<FunctionDecl>(D);
John McCall9c72c602010-08-27 09:08:28 +00002447 QualType ArgType = GetTypeOfFunction(S.Context, R, Fn);
2448 if (ArgType.isNull()) continue;
John McCalleff92132010-02-02 02:21:27 +00002449
Douglas Gregor75f21af2010-08-30 21:04:23 +00002450 // Function-to-pointer conversion.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002451 if (!ParamWasReference && ParamType->isPointerType() &&
Douglas Gregor75f21af2010-08-30 21:04:23 +00002452 ArgType->isFunctionType())
2453 ArgType = S.Context.getPointerType(ArgType);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002454
John McCalleff92132010-02-02 02:21:27 +00002455 // - If the argument is an overload set (not containing function
2456 // templates), trial argument deduction is attempted using each
2457 // of the members of the set. If deduction succeeds for only one
2458 // of the overload set members, that member is used as the
2459 // argument value for the deduction. If deduction succeeds for
2460 // more than one member of the overload set the parameter is
2461 // treated as a non-deduced context.
2462
2463 // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
2464 // Type deduction is done independently for each P/A pair, and
2465 // the deduced template argument values are then combined.
2466 // So we do not reject deductions which were made elsewhere.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002467 llvm::SmallVector<DeducedTemplateArgument, 8>
Douglas Gregor02024a92010-03-28 02:42:43 +00002468 Deduced(TemplateParams->size());
John McCall2a7fb272010-08-25 05:32:35 +00002469 TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
John McCalleff92132010-02-02 02:21:27 +00002470 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002471 = DeduceTemplateArguments(S, TemplateParams,
John McCalleff92132010-02-02 02:21:27 +00002472 ParamType, ArgType,
2473 Info, Deduced, TDF);
2474 if (Result) continue;
2475 if (!Match.isNull()) return QualType();
2476 Match = ArgType;
2477 }
2478
2479 return Match;
2480}
2481
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002482/// \brief Perform the adjustments to the parameter and argument types
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002483/// described in C++ [temp.deduct.call].
2484///
2485/// \returns true if the caller should not attempt to perform any template
2486/// argument deduction based on this P/A pair.
2487static bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S,
2488 TemplateParameterList *TemplateParams,
2489 QualType &ParamType,
2490 QualType &ArgType,
2491 Expr *Arg,
2492 unsigned &TDF) {
2493 // C++0x [temp.deduct.call]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00002494 // If P is a cv-qualified type, the top level cv-qualifiers of P's type
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002495 // are ignored for type deduction.
Douglas Gregora459cc22011-04-27 23:34:22 +00002496 if (ParamType.hasQualifiers())
2497 ParamType = ParamType.getUnqualifiedType();
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002498 const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>();
2499 if (ParamRefType) {
Richard Smith34b41d92011-02-20 03:19:35 +00002500 QualType PointeeType = ParamRefType->getPointeeType();
2501
Douglas Gregorf15748a2011-06-03 03:35:07 +00002502 // If the argument has incomplete array type, try to complete it's type.
2503 if (ArgType->isIncompleteArrayType() &&
2504 !S.RequireCompleteExprType(Arg, S.PDiag(),
2505 std::make_pair(SourceLocation(), S.PDiag())))
2506 ArgType = Arg->getType();
2507
Douglas Gregor2ad746a2011-01-21 05:18:22 +00002508 // [C++0x] If P is an rvalue reference to a cv-unqualified
2509 // template parameter and the argument is an lvalue, the type
2510 // "lvalue reference to A" is used in place of A for type
2511 // deduction.
Richard Smith34b41d92011-02-20 03:19:35 +00002512 if (isa<RValueReferenceType>(ParamType)) {
2513 if (!PointeeType.getQualifiers() &&
2514 isa<TemplateTypeParmType>(PointeeType) &&
Douglas Gregor9625e442011-05-21 22:16:50 +00002515 Arg->Classify(S.Context).isLValue() &&
2516 Arg->getType() != S.Context.OverloadTy &&
2517 Arg->getType() != S.Context.BoundMemberTy)
Douglas Gregor2ad746a2011-01-21 05:18:22 +00002518 ArgType = S.Context.getLValueReferenceType(ArgType);
2519 }
2520
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002521 // [...] If P is a reference type, the type referred to by P is used
2522 // for type deduction.
Richard Smith34b41d92011-02-20 03:19:35 +00002523 ParamType = PointeeType;
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002524 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002525
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002526 // Overload sets usually make this parameter an undeduced
2527 // context, but there are sometimes special circumstances.
2528 if (ArgType == S.Context.OverloadTy) {
2529 ArgType = ResolveOverloadForDeduction(S, TemplateParams,
2530 Arg, ParamType,
2531 ParamRefType != 0);
2532 if (ArgType.isNull())
2533 return true;
2534 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002535
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002536 if (ParamRefType) {
2537 // C++0x [temp.deduct.call]p3:
2538 // [...] If P is of the form T&&, where T is a template parameter, and
2539 // the argument is an lvalue, the type A& is used in place of A for
2540 // type deduction.
2541 if (ParamRefType->isRValueReferenceType() &&
2542 ParamRefType->getAs<TemplateTypeParmType>() &&
2543 Arg->isLValue())
2544 ArgType = S.Context.getLValueReferenceType(ArgType);
2545 } else {
2546 // C++ [temp.deduct.call]p2:
2547 // If P is not a reference type:
2548 // - If A is an array type, the pointer type produced by the
2549 // array-to-pointer standard conversion (4.2) is used in place of
2550 // A for type deduction; otherwise,
2551 if (ArgType->isArrayType())
2552 ArgType = S.Context.getArrayDecayedType(ArgType);
2553 // - If A is a function type, the pointer type produced by the
2554 // function-to-pointer standard conversion (4.3) is used in place
2555 // of A for type deduction; otherwise,
2556 else if (ArgType->isFunctionType())
2557 ArgType = S.Context.getPointerType(ArgType);
2558 else {
NAKAMURA Takumi00995302011-01-27 07:09:49 +00002559 // - If A is a cv-qualified type, the top level cv-qualifiers of A's
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002560 // type are ignored for type deduction.
Douglas Gregora459cc22011-04-27 23:34:22 +00002561 ArgType = ArgType.getUnqualifiedType();
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002562 }
2563 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002564
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002565 // C++0x [temp.deduct.call]p4:
2566 // In general, the deduction process attempts to find template argument
2567 // values that will make the deduced A identical to A (after the type A
2568 // is transformed as described above). [...]
2569 TDF = TDF_SkipNonDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002570
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002571 // - If the original P is a reference type, the deduced A (i.e., the
2572 // type referred to by the reference) can be more cv-qualified than
2573 // the transformed A.
2574 if (ParamRefType)
2575 TDF |= TDF_ParamWithReferenceType;
2576 // - The transformed A can be another pointer or pointer to member
2577 // type that can be converted to the deduced A via a qualification
2578 // conversion (4.4).
2579 if (ArgType->isPointerType() || ArgType->isMemberPointerType() ||
2580 ArgType->isObjCObjectPointerType())
2581 TDF |= TDF_IgnoreQualifiers;
2582 // - If P is a class and P has the form simple-template-id, then the
2583 // transformed A can be a derived class of the deduced A. Likewise,
2584 // if P is a pointer to a class of the form simple-template-id, the
2585 // transformed A can be a pointer to a derived class pointed to by
2586 // the deduced A.
2587 if (isSimpleTemplateIdType(ParamType) ||
2588 (isa<PointerType>(ParamType) &&
2589 isSimpleTemplateIdType(
2590 ParamType->getAs<PointerType>()->getPointeeType())))
2591 TDF |= TDF_DerivedClass;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002592
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002593 return false;
2594}
2595
Douglas Gregore53060f2009-06-25 22:08:12 +00002596/// \brief Perform template argument deduction from a function call
2597/// (C++ [temp.deduct.call]).
2598///
2599/// \param FunctionTemplate the function template for which we are performing
2600/// template argument deduction.
2601///
Douglas Gregor48026d22010-01-11 18:40:55 +00002602/// \param ExplicitTemplateArguments the explicit template arguments provided
2603/// for this call.
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002604///
Douglas Gregore53060f2009-06-25 22:08:12 +00002605/// \param Args the function call arguments
2606///
2607/// \param NumArgs the number of arguments in Args
2608///
Douglas Gregor48026d22010-01-11 18:40:55 +00002609/// \param Name the name of the function being called. This is only significant
2610/// when the function template is a conversion function template, in which
2611/// case this routine will also perform template argument deduction based on
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002612/// the function to which
Douglas Gregor48026d22010-01-11 18:40:55 +00002613///
Douglas Gregore53060f2009-06-25 22:08:12 +00002614/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00002615/// this will be set to the function template specialization produced by
Douglas Gregore53060f2009-06-25 22:08:12 +00002616/// template argument deduction.
2617///
2618/// \param Info the argument will be updated to provide additional information
2619/// about template argument deduction.
2620///
2621/// \returns the result of template argument deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00002622Sema::TemplateDeductionResult
2623Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor67714232011-03-03 02:41:12 +00002624 TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregore53060f2009-06-25 22:08:12 +00002625 Expr **Args, unsigned NumArgs,
2626 FunctionDecl *&Specialization,
2627 TemplateDeductionInfo &Info) {
2628 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002629
Douglas Gregore53060f2009-06-25 22:08:12 +00002630 // C++ [temp.deduct.call]p1:
2631 // Template argument deduction is done by comparing each function template
2632 // parameter type (call it P) with the type of the corresponding argument
2633 // of the call (call it A) as described below.
2634 unsigned CheckArgs = NumArgs;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002635 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregore53060f2009-06-25 22:08:12 +00002636 return TDK_TooFewArguments;
2637 else if (NumArgs > Function->getNumParams()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002638 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00002639 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002640 if (Proto->isTemplateVariadic())
2641 /* Do nothing */;
2642 else if (Proto->isVariadic())
2643 CheckArgs = Function->getNumParams();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002644 else
Douglas Gregore53060f2009-06-25 22:08:12 +00002645 return TDK_TooManyArguments;
Douglas Gregore53060f2009-06-25 22:08:12 +00002646 }
Mike Stump1eb44332009-09-09 15:08:12 +00002647
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002648 // The types of the parameters from which we will perform template argument
2649 // deduction.
John McCall2a7fb272010-08-25 05:32:35 +00002650 LocalInstantiationScope InstScope(*this);
Douglas Gregore53060f2009-06-25 22:08:12 +00002651 TemplateParameterList *TemplateParams
2652 = FunctionTemplate->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00002653 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002654 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregor02024a92010-03-28 02:42:43 +00002655 unsigned NumExplicitlySpecified = 0;
John McCalld5532b62009-11-23 01:53:49 +00002656 if (ExplicitTemplateArgs) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00002657 TemplateDeductionResult Result =
2658 SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00002659 *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002660 Deduced,
2661 ParamTypes,
2662 0,
2663 Info);
2664 if (Result)
2665 return Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002666
2667 NumExplicitlySpecified = Deduced.size();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002668 } else {
2669 // Just fill in the parameter types from the function declaration.
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002670 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002671 ParamTypes.push_back(Function->getParamDecl(I)->getType());
2672 }
Mike Stump1eb44332009-09-09 15:08:12 +00002673
Douglas Gregor6db8ed42009-06-30 23:57:56 +00002674 // Deduce template arguments from the function parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00002675 Deduced.resize(TemplateParams->size());
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002676 unsigned ArgIdx = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002677 for (unsigned ParamIdx = 0, NumParams = ParamTypes.size();
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002678 ParamIdx != NumParams; ++ParamIdx) {
2679 QualType ParamType = ParamTypes[ParamIdx];
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002680
2681 const PackExpansionType *ParamExpansion
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002682 = dyn_cast<PackExpansionType>(ParamType);
2683 if (!ParamExpansion) {
2684 // Simple case: matching a function parameter to a function argument.
2685 if (ArgIdx >= CheckArgs)
2686 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002687
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002688 Expr *Arg = Args[ArgIdx++];
2689 QualType ArgType = Arg->getType();
2690 unsigned TDF = 0;
2691 if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams,
2692 ParamType, ArgType, Arg,
2693 TDF))
2694 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002695
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002696 if (TemplateDeductionResult Result
2697 = ::DeduceTemplateArguments(*this, TemplateParams,
2698 ParamType, ArgType, Info, Deduced,
2699 TDF))
2700 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002701
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002702 // FIXME: we need to check that the deduced A is the same as A,
2703 // modulo the various allowed differences.
2704 continue;
Douglas Gregor75f21af2010-08-30 21:04:23 +00002705 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002706
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002707 // C++0x [temp.deduct.call]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002708 // For a function parameter pack that occurs at the end of the
2709 // parameter-declaration-list, the type A of each remaining argument of
2710 // the call is compared with the type P of the declarator-id of the
2711 // function parameter pack. Each comparison deduces template arguments
2712 // for subsequent positions in the template parameter packs expanded by
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00002713 // the function parameter pack. For a function parameter pack that does
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002714 // not occur at the end of the parameter-declaration-list, the type of
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00002715 // the parameter pack is a non-deduced context.
2716 if (ParamIdx + 1 < NumParams)
2717 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002718
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002719 QualType ParamPattern = ParamExpansion->getPattern();
2720 llvm::SmallVector<unsigned, 2> PackIndices;
2721 {
2722 llvm::BitVector SawIndices(TemplateParams->size());
2723 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2724 collectUnexpandedParameterPacks(ParamPattern, Unexpanded);
2725 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
2726 unsigned Depth, Index;
2727 llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
2728 if (Depth == 0 && !SawIndices[Index]) {
2729 SawIndices[Index] = true;
2730 PackIndices.push_back(Index);
2731 }
Douglas Gregore53060f2009-06-25 22:08:12 +00002732 }
2733 }
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002734 assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002735
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002736 // Keep track of the deduced template arguments for each parameter pack
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002737 // expanded by this pack expansion (the outer index) and for each
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002738 // template argument (the inner SmallVectors).
2739 llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2>
Douglas Gregord3731192011-01-10 07:32:04 +00002740 NewlyDeducedPacks(PackIndices.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002741 llvm::SmallVector<DeducedTemplateArgument, 2>
Douglas Gregord3731192011-01-10 07:32:04 +00002742 SavedPacks(PackIndices.size());
Douglas Gregor54293852011-01-10 17:35:05 +00002743 PrepareArgumentPackDeduction(*this, Deduced, PackIndices, SavedPacks,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002744 NewlyDeducedPacks);
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002745 bool HasAnyArguments = false;
2746 for (; ArgIdx < NumArgs; ++ArgIdx) {
2747 HasAnyArguments = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002748
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002749 ParamType = ParamPattern;
2750 Expr *Arg = Args[ArgIdx];
2751 QualType ArgType = Arg->getType();
2752 unsigned TDF = 0;
2753 if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams,
2754 ParamType, ArgType, Arg,
2755 TDF)) {
2756 // We can't actually perform any deduction for this argument, so stop
2757 // deduction at this point.
2758 ++ArgIdx;
2759 break;
2760 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002761
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002762 if (TemplateDeductionResult Result
2763 = ::DeduceTemplateArguments(*this, TemplateParams,
2764 ParamType, ArgType, Info, Deduced,
2765 TDF))
2766 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002767
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002768 // Capture the deduced template arguments for each parameter pack expanded
2769 // by this pack expansion, add them to the list of arguments we've deduced
2770 // for that pack, then clear out the deduced argument.
2771 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
2772 DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
2773 if (!DeducedArg.isNull()) {
2774 NewlyDeducedPacks[I].push_back(DeducedArg);
2775 DeducedArg = DeducedTemplateArgument();
2776 }
2777 }
2778 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002779
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002780 // Build argument packs for each of the parameter packs expanded by this
2781 // pack expansion.
Douglas Gregor0216f812011-01-10 17:53:52 +00002782 if (Sema::TemplateDeductionResult Result
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002783 = FinishArgumentPackDeduction(*this, TemplateParams, HasAnyArguments,
Douglas Gregor0216f812011-01-10 17:53:52 +00002784 Deduced, PackIndices, SavedPacks,
2785 NewlyDeducedPacks, Info))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002786 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00002787
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00002788 // After we've matching against a parameter pack, we're done.
2789 break;
Douglas Gregore53060f2009-06-25 22:08:12 +00002790 }
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002791
Mike Stump1eb44332009-09-09 15:08:12 +00002792 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor02024a92010-03-28 02:42:43 +00002793 NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002794 Specialization, Info);
Douglas Gregore53060f2009-06-25 22:08:12 +00002795}
2796
Douglas Gregor83314aa2009-07-08 20:55:45 +00002797/// \brief Deduce template arguments when taking the address of a function
Douglas Gregor4b52e252009-12-21 23:17:24 +00002798/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
2799/// a template.
Douglas Gregor83314aa2009-07-08 20:55:45 +00002800///
2801/// \param FunctionTemplate the function template for which we are performing
2802/// template argument deduction.
2803///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002804/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor4b52e252009-12-21 23:17:24 +00002805/// arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00002806///
2807/// \param ArgFunctionType the function type that will be used as the
2808/// "argument" type (A) when performing template argument deduction from the
Douglas Gregor4b52e252009-12-21 23:17:24 +00002809/// function template's function type. This type may be NULL, if there is no
2810/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
Douglas Gregor83314aa2009-07-08 20:55:45 +00002811///
2812/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00002813/// this will be set to the function template specialization produced by
Douglas Gregor83314aa2009-07-08 20:55:45 +00002814/// template argument deduction.
2815///
2816/// \param Info the argument will be updated to provide additional information
2817/// about template argument deduction.
2818///
2819/// \returns the result of template argument deduction.
2820Sema::TemplateDeductionResult
2821Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor67714232011-03-03 02:41:12 +00002822 TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002823 QualType ArgFunctionType,
2824 FunctionDecl *&Specialization,
2825 TemplateDeductionInfo &Info) {
2826 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2827 TemplateParameterList *TemplateParams
2828 = FunctionTemplate->getTemplateParameters();
2829 QualType FunctionType = Function->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00002830
Douglas Gregor83314aa2009-07-08 20:55:45 +00002831 // Substitute any explicit template arguments.
John McCall2a7fb272010-08-25 05:32:35 +00002832 LocalInstantiationScope InstScope(*this);
Douglas Gregor02024a92010-03-28 02:42:43 +00002833 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
2834 unsigned NumExplicitlySpecified = 0;
Douglas Gregor83314aa2009-07-08 20:55:45 +00002835 llvm::SmallVector<QualType, 4> ParamTypes;
John McCalld5532b62009-11-23 01:53:49 +00002836 if (ExplicitTemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00002837 if (TemplateDeductionResult Result
2838 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00002839 *ExplicitTemplateArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00002840 Deduced, ParamTypes,
Douglas Gregor83314aa2009-07-08 20:55:45 +00002841 &FunctionType, Info))
2842 return Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002843
2844 NumExplicitlySpecified = Deduced.size();
Douglas Gregor83314aa2009-07-08 20:55:45 +00002845 }
2846
2847 // Template argument deduction for function templates in a SFINAE context.
2848 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002849 SFINAETrap Trap(*this);
2850
John McCalleff92132010-02-02 02:21:27 +00002851 Deduced.resize(TemplateParams->size());
2852
Douglas Gregor4b52e252009-12-21 23:17:24 +00002853 if (!ArgFunctionType.isNull()) {
2854 // Deduce template arguments from the function type.
Douglas Gregor4b52e252009-12-21 23:17:24 +00002855 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002856 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor4b52e252009-12-21 23:17:24 +00002857 FunctionType, ArgFunctionType, Info,
Douglas Gregor73b3cf62011-01-25 17:19:08 +00002858 Deduced, TDF_TopLevelParameterTypeList))
Douglas Gregor4b52e252009-12-21 23:17:24 +00002859 return Result;
2860 }
Douglas Gregorfbb6fad2010-09-29 21:14:36 +00002861
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002862 if (TemplateDeductionResult Result
Douglas Gregorfbb6fad2010-09-29 21:14:36 +00002863 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
2864 NumExplicitlySpecified,
2865 Specialization, Info))
2866 return Result;
2867
2868 // If the requested function type does not match the actual type of the
2869 // specialization, template argument deduction fails.
2870 if (!ArgFunctionType.isNull() &&
2871 !Context.hasSameType(ArgFunctionType, Specialization->getType()))
2872 return TDK_NonDeducedMismatch;
2873
2874 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00002875}
2876
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002877/// \brief Deduce template arguments for a templated conversion
2878/// function (C++ [temp.deduct.conv]) and, if successful, produce a
2879/// conversion function template specialization.
2880Sema::TemplateDeductionResult
2881Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
2882 QualType ToType,
2883 CXXConversionDecl *&Specialization,
2884 TemplateDeductionInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00002885 CXXConversionDecl *Conv
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002886 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
2887 QualType FromType = Conv->getConversionType();
2888
2889 // Canonicalize the types for deduction.
2890 QualType P = Context.getCanonicalType(FromType);
2891 QualType A = Context.getCanonicalType(ToType);
2892
Douglas Gregor5453d932011-03-06 09:03:20 +00002893 // C++0x [temp.deduct.conv]p2:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002894 // If P is a reference type, the type referred to by P is used for
2895 // type deduction.
2896 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
2897 P = PRef->getPointeeType();
2898
Douglas Gregor5453d932011-03-06 09:03:20 +00002899 // C++0x [temp.deduct.conv]p4:
2900 // [...] If A is a reference type, the type referred to by A is used
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002901 // for type deduction.
2902 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
Douglas Gregor5453d932011-03-06 09:03:20 +00002903 A = ARef->getPointeeType().getUnqualifiedType();
2904 // C++ [temp.deduct.conv]p3:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002905 //
Mike Stump1eb44332009-09-09 15:08:12 +00002906 // If A is not a reference type:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002907 else {
2908 assert(!A->isReferenceType() && "Reference types were handled above");
2909
2910 // - If P is an array type, the pointer type produced by the
Mike Stump1eb44332009-09-09 15:08:12 +00002911 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002912 // of P for type deduction; otherwise,
2913 if (P->isArrayType())
2914 P = Context.getArrayDecayedType(P);
2915 // - If P is a function type, the pointer type produced by the
2916 // function-to-pointer standard conversion (4.3) is used in
2917 // place of P for type deduction; otherwise,
2918 else if (P->isFunctionType())
2919 P = Context.getPointerType(P);
2920 // - If P is a cv-qualified type, the top level cv-qualifiers of
NAKAMURA Takumi00995302011-01-27 07:09:49 +00002921 // P's type are ignored for type deduction.
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002922 else
2923 P = P.getUnqualifiedType();
2924
Douglas Gregor5453d932011-03-06 09:03:20 +00002925 // C++0x [temp.deduct.conv]p4:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00002926 // If A is a cv-qualified type, the top level cv-qualifiers of A's
Douglas Gregor5453d932011-03-06 09:03:20 +00002927 // type are ignored for type deduction. If A is a reference type, the type
2928 // referred to by A is used for type deduction.
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002929 A = A.getUnqualifiedType();
2930 }
2931
2932 // Template argument deduction for function templates in a SFINAE context.
2933 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002934 SFINAETrap Trap(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002935
2936 // C++ [temp.deduct.conv]p1:
2937 // Template argument deduction is done by comparing the return
2938 // type of the template conversion function (call it P) with the
2939 // type that is required as the result of the conversion (call it
2940 // A) as described in 14.8.2.4.
2941 TemplateParameterList *TemplateParams
2942 = FunctionTemplate->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00002943 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Mike Stump1eb44332009-09-09 15:08:12 +00002944 Deduced.resize(TemplateParams->size());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002945
2946 // C++0x [temp.deduct.conv]p4:
2947 // In general, the deduction process attempts to find template
2948 // argument values that will make the deduced A identical to
2949 // A. However, there are two cases that allow a difference:
2950 unsigned TDF = 0;
2951 // - If the original A is a reference type, A can be more
2952 // cv-qualified than the deduced A (i.e., the type referred to
2953 // by the reference)
2954 if (ToType->isReferenceType())
2955 TDF |= TDF_ParamWithReferenceType;
2956 // - The deduced A can be another pointer or pointer to member
NAKAMURA Takumi00995302011-01-27 07:09:49 +00002957 // type that can be converted to A via a qualification
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002958 // conversion.
2959 //
2960 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
2961 // both P and A are pointers or member pointers. In this case, we
2962 // just ignore cv-qualifiers completely).
2963 if ((P->isPointerType() && A->isPointerType()) ||
2964 (P->isMemberPointerType() && P->isMemberPointerType()))
2965 TDF |= TDF_IgnoreQualifiers;
2966 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002967 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002968 P, A, Info, Deduced, TDF))
2969 return Result;
2970
2971 // FIXME: we need to check that the deduced A is the same as A,
2972 // modulo the various allowed differences.
Mike Stump1eb44332009-09-09 15:08:12 +00002973
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002974 // Finish template argument deduction.
John McCall2a7fb272010-08-25 05:32:35 +00002975 LocalInstantiationScope InstScope(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002976 FunctionDecl *Spec = 0;
2977 TemplateDeductionResult Result
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002978 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec,
Douglas Gregor02024a92010-03-28 02:42:43 +00002979 Info);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002980 Specialization = cast_or_null<CXXConversionDecl>(Spec);
2981 return Result;
2982}
2983
Douglas Gregor4b52e252009-12-21 23:17:24 +00002984/// \brief Deduce template arguments for a function template when there is
2985/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
2986///
2987/// \param FunctionTemplate the function template for which we are performing
2988/// template argument deduction.
2989///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002990/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor4b52e252009-12-21 23:17:24 +00002991/// arguments.
2992///
2993/// \param Specialization if template argument deduction was successful,
2994/// this will be set to the function template specialization produced by
2995/// template argument deduction.
2996///
2997/// \param Info the argument will be updated to provide additional information
2998/// about template argument deduction.
2999///
3000/// \returns the result of template argument deduction.
3001Sema::TemplateDeductionResult
3002Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor67714232011-03-03 02:41:12 +00003003 TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor4b52e252009-12-21 23:17:24 +00003004 FunctionDecl *&Specialization,
3005 TemplateDeductionInfo &Info) {
3006 return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
3007 QualType(), Specialization, Info);
3008}
3009
Richard Smith34b41d92011-02-20 03:19:35 +00003010namespace {
3011 /// Substitute the 'auto' type specifier within a type for a given replacement
3012 /// type.
3013 class SubstituteAutoTransform :
3014 public TreeTransform<SubstituteAutoTransform> {
3015 QualType Replacement;
3016 public:
3017 SubstituteAutoTransform(Sema &SemaRef, QualType Replacement) :
3018 TreeTransform<SubstituteAutoTransform>(SemaRef), Replacement(Replacement) {
3019 }
3020 QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) {
3021 // If we're building the type pattern to deduce against, don't wrap the
3022 // substituted type in an AutoType. Certain template deduction rules
3023 // apply only when a template type parameter appears directly (and not if
3024 // the parameter is found through desugaring). For instance:
3025 // auto &&lref = lvalue;
3026 // must transform into "rvalue reference to T" not "rvalue reference to
3027 // auto type deduced as T" in order for [temp.deduct.call]p3 to apply.
3028 if (isa<TemplateTypeParmType>(Replacement)) {
3029 QualType Result = Replacement;
3030 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
3031 NewTL.setNameLoc(TL.getNameLoc());
3032 return Result;
3033 } else {
3034 QualType Result = RebuildAutoType(Replacement);
3035 AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result);
3036 NewTL.setNameLoc(TL.getNameLoc());
3037 return Result;
3038 }
3039 }
3040 };
3041}
3042
3043/// \brief Deduce the type for an auto type-specifier (C++0x [dcl.spec.auto]p6)
3044///
3045/// \param Type the type pattern using the auto type-specifier.
3046///
3047/// \param Init the initializer for the variable whose type is to be deduced.
3048///
3049/// \param Result if type deduction was successful, this will be set to the
3050/// deduced type. This may still contain undeduced autos if the type is
Richard Smitha085da82011-03-17 16:11:59 +00003051/// dependent. This will be set to null if deduction succeeded, but auto
3052/// substitution failed; the appropriate diagnostic will already have been
3053/// produced in that case.
Richard Smith34b41d92011-02-20 03:19:35 +00003054///
3055/// \returns true if deduction succeeded, false if it failed.
3056bool
Richard Smitha085da82011-03-17 16:11:59 +00003057Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *Init,
3058 TypeSourceInfo *&Result) {
Richard Smith34b41d92011-02-20 03:19:35 +00003059 if (Init->isTypeDependent()) {
3060 Result = Type;
3061 return true;
3062 }
3063
3064 SourceLocation Loc = Init->getExprLoc();
3065
3066 LocalInstantiationScope InstScope(*this);
3067
3068 // Build template<class TemplParam> void Func(FuncParam);
Chandler Carruth4fb86f82011-05-01 00:51:33 +00003069 TemplateTypeParmDecl *TemplParam =
3070 TemplateTypeParmDecl::Create(Context, 0, SourceLocation(), Loc, 0, 0, 0,
3071 false, false);
3072 QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0);
3073 NamedDecl *TemplParamPtr = TemplParam;
Richard Smith483b9f32011-02-21 20:05:19 +00003074 FixedSizeTemplateParameterList<1> TemplateParams(Loc, Loc, &TemplParamPtr,
3075 Loc);
3076
Richard Smitha085da82011-03-17 16:11:59 +00003077 TypeSourceInfo *FuncParamInfo =
Richard Smith34b41d92011-02-20 03:19:35 +00003078 SubstituteAutoTransform(*this, TemplArg).TransformType(Type);
Richard Smitha085da82011-03-17 16:11:59 +00003079 assert(FuncParamInfo && "substituting template parameter for 'auto' failed");
3080 QualType FuncParam = FuncParamInfo->getType();
Richard Smith34b41d92011-02-20 03:19:35 +00003081
3082 // Deduce type of TemplParam in Func(Init)
3083 llvm::SmallVector<DeducedTemplateArgument, 1> Deduced;
3084 Deduced.resize(1);
3085 QualType InitType = Init->getType();
3086 unsigned TDF = 0;
Richard Smith483b9f32011-02-21 20:05:19 +00003087 if (AdjustFunctionParmAndArgTypesForDeduction(*this, &TemplateParams,
Richard Smith34b41d92011-02-20 03:19:35 +00003088 FuncParam, InitType, Init,
3089 TDF))
3090 return false;
3091
3092 TemplateDeductionInfo Info(Context, Loc);
Richard Smith483b9f32011-02-21 20:05:19 +00003093 if (::DeduceTemplateArguments(*this, &TemplateParams,
Richard Smith34b41d92011-02-20 03:19:35 +00003094 FuncParam, InitType, Info, Deduced,
3095 TDF))
3096 return false;
3097
3098 QualType DeducedType = Deduced[0].getAsType();
3099 if (DeducedType.isNull())
3100 return false;
3101
3102 Result = SubstituteAutoTransform(*this, DeducedType).TransformType(Type);
3103 return true;
3104}
3105
Douglas Gregor8a514912009-09-14 18:39:43 +00003106static void
Douglas Gregore73bb602009-09-14 21:25:05 +00003107MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
3108 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003109 unsigned Level,
Douglas Gregore73bb602009-09-14 21:25:05 +00003110 llvm::SmallVectorImpl<bool> &Deduced);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003111
3112/// \brief If this is a non-static member function,
Douglas Gregor77bc5722010-11-12 23:44:13 +00003113static void MaybeAddImplicitObjectParameterType(ASTContext &Context,
3114 CXXMethodDecl *Method,
3115 llvm::SmallVectorImpl<QualType> &ArgTypes) {
3116 if (Method->isStatic())
3117 return;
3118
3119 // C++ [over.match.funcs]p4:
3120 //
3121 // For non-static member functions, the type of the implicit
3122 // object parameter is
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003123 // - "lvalue reference to cv X" for functions declared without a
Douglas Gregor77bc5722010-11-12 23:44:13 +00003124 // ref-qualifier or with the & ref-qualifier
3125 // - "rvalue reference to cv X" for functions declared with the
3126 // && ref-qualifier
3127 //
3128 // FIXME: We don't have ref-qualifiers yet, so we don't do that part.
3129 QualType ArgTy = Context.getTypeDeclType(Method->getParent());
3130 ArgTy = Context.getQualifiedType(ArgTy,
3131 Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
3132 ArgTy = Context.getLValueReferenceType(ArgTy);
3133 ArgTypes.push_back(ArgTy);
3134}
3135
Douglas Gregor8a514912009-09-14 18:39:43 +00003136/// \brief Determine whether the function template \p FT1 is at least as
3137/// specialized as \p FT2.
3138static bool isAtLeastAsSpecializedAs(Sema &S,
John McCall5769d612010-02-08 23:07:23 +00003139 SourceLocation Loc,
Douglas Gregor8a514912009-09-14 18:39:43 +00003140 FunctionTemplateDecl *FT1,
3141 FunctionTemplateDecl *FT2,
3142 TemplatePartialOrderingContext TPOC,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003143 unsigned NumCallArguments,
Douglas Gregorb939a192011-01-21 17:29:42 +00003144 llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *RefParamComparisons) {
Douglas Gregor8a514912009-09-14 18:39:43 +00003145 FunctionDecl *FD1 = FT1->getTemplatedDecl();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003146 FunctionDecl *FD2 = FT2->getTemplatedDecl();
Douglas Gregor8a514912009-09-14 18:39:43 +00003147 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
3148 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003149
Douglas Gregor8a514912009-09-14 18:39:43 +00003150 assert(Proto1 && Proto2 && "Function templates must have prototypes");
3151 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00003152 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor8a514912009-09-14 18:39:43 +00003153 Deduced.resize(TemplateParams->size());
3154
3155 // C++0x [temp.deduct.partial]p3:
3156 // The types used to determine the ordering depend on the context in which
3157 // the partial ordering is done:
John McCall2a7fb272010-08-25 05:32:35 +00003158 TemplateDeductionInfo Info(S.Context, Loc);
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003159 CXXMethodDecl *Method1 = 0;
3160 CXXMethodDecl *Method2 = 0;
3161 bool IsNonStatic2 = false;
3162 bool IsNonStatic1 = false;
3163 unsigned Skip2 = 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00003164 switch (TPOC) {
3165 case TPOC_Call: {
3166 // - In the context of a function call, the function parameter types are
3167 // used.
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003168 Method1 = dyn_cast<CXXMethodDecl>(FD1);
3169 Method2 = dyn_cast<CXXMethodDecl>(FD2);
3170 IsNonStatic1 = Method1 && !Method1->isStatic();
3171 IsNonStatic2 = Method2 && !Method2->isStatic();
3172
3173 // C++0x [temp.func.order]p3:
3174 // [...] If only one of the function templates is a non-static
3175 // member, that function template is considered to have a new
3176 // first parameter inserted in its function parameter list. The
3177 // new parameter is of type "reference to cv A," where cv are
3178 // the cv-qualifiers of the function template (if any) and A is
3179 // the class of which the function template is a member.
3180 //
3181 // C++98/03 doesn't have this provision, so instead we drop the
3182 // first argument of the free function or static member, which
3183 // seems to match existing practice.
Douglas Gregor77bc5722010-11-12 23:44:13 +00003184 llvm::SmallVector<QualType, 4> Args1;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003185 unsigned Skip1 = !S.getLangOptions().CPlusPlus0x &&
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003186 IsNonStatic2 && !IsNonStatic1;
3187 if (S.getLangOptions().CPlusPlus0x && IsNonStatic1 && !IsNonStatic2)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003188 MaybeAddImplicitObjectParameterType(S.Context, Method1, Args1);
3189 Args1.insert(Args1.end(),
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003190 Proto1->arg_type_begin() + Skip1, Proto1->arg_type_end());
Douglas Gregor77bc5722010-11-12 23:44:13 +00003191
3192 llvm::SmallVector<QualType, 4> Args2;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003193 Skip2 = !S.getLangOptions().CPlusPlus0x &&
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003194 IsNonStatic1 && !IsNonStatic2;
3195 if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1)
Douglas Gregor77bc5722010-11-12 23:44:13 +00003196 MaybeAddImplicitObjectParameterType(S.Context, Method2, Args2);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003197 Args2.insert(Args2.end(),
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003198 Proto2->arg_type_begin() + Skip2, Proto2->arg_type_end());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003199
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003200 // C++ [temp.func.order]p5:
3201 // The presence of unused ellipsis and default arguments has no effect on
3202 // the partial ordering of function templates.
3203 if (Args1.size() > NumCallArguments)
3204 Args1.resize(NumCallArguments);
3205 if (Args2.size() > NumCallArguments)
3206 Args2.resize(NumCallArguments);
3207 if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(),
3208 Args1.data(), Args1.size(), Info, Deduced,
3209 TDF_None, /*PartialOrdering=*/true,
Douglas Gregorb939a192011-01-21 17:29:42 +00003210 RefParamComparisons))
Douglas Gregor8a514912009-09-14 18:39:43 +00003211 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003212
Douglas Gregor8a514912009-09-14 18:39:43 +00003213 break;
3214 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003215
Douglas Gregor8a514912009-09-14 18:39:43 +00003216 case TPOC_Conversion:
3217 // - In the context of a call to a conversion operator, the return types
3218 // of the conversion function templates are used.
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003219 if (DeduceTemplateArguments(S, TemplateParams, Proto2->getResultType(),
3220 Proto1->getResultType(), Info, Deduced,
3221 TDF_None, /*PartialOrdering=*/true,
Douglas Gregorb939a192011-01-21 17:29:42 +00003222 RefParamComparisons))
Douglas Gregor8a514912009-09-14 18:39:43 +00003223 return false;
3224 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003225
Douglas Gregor8a514912009-09-14 18:39:43 +00003226 case TPOC_Other:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003227 // - In other contexts (14.6.6.2) the function template's function type
Douglas Gregor8a514912009-09-14 18:39:43 +00003228 // is used.
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003229 // FIXME: Don't we actually want to perform the adjustments on the parameter
3230 // types?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003231 if (DeduceTemplateArguments(S, TemplateParams, FD2->getType(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003232 FD1->getType(), Info, Deduced, TDF_None,
Douglas Gregorb939a192011-01-21 17:29:42 +00003233 /*PartialOrdering=*/true, RefParamComparisons))
Douglas Gregor8a514912009-09-14 18:39:43 +00003234 return false;
3235 break;
3236 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003237
Douglas Gregor8a514912009-09-14 18:39:43 +00003238 // C++0x [temp.deduct.partial]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003239 // In most cases, all template parameters must have values in order for
3240 // deduction to succeed, but for partial ordering purposes a template
3241 // parameter may remain without a value provided it is not used in the
Douglas Gregor8a514912009-09-14 18:39:43 +00003242 // types being used for partial ordering. [ Note: a template parameter used
3243 // in a non-deduced context is considered used. -end note]
3244 unsigned ArgIdx = 0, NumArgs = Deduced.size();
3245 for (; ArgIdx != NumArgs; ++ArgIdx)
3246 if (Deduced[ArgIdx].isNull())
3247 break;
3248
3249 if (ArgIdx == NumArgs) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003250 // All template arguments were deduced. FT1 is at least as specialized
Douglas Gregor8a514912009-09-14 18:39:43 +00003251 // as FT2.
3252 return true;
3253 }
3254
Douglas Gregore73bb602009-09-14 21:25:05 +00003255 // Figure out which template parameters were used.
Douglas Gregor8a514912009-09-14 18:39:43 +00003256 llvm::SmallVector<bool, 4> UsedParameters;
3257 UsedParameters.resize(TemplateParams->size());
3258 switch (TPOC) {
3259 case TPOC_Call: {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003260 unsigned NumParams = std::min(NumCallArguments,
3261 std::min(Proto1->getNumArgs(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003262 Proto2->getNumArgs()));
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003263 if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003264 ::MarkUsedTemplateParameters(S, Method2->getThisType(S.Context), false,
Douglas Gregor8d706ec2010-11-15 15:41:16 +00003265 TemplateParams->getDepth(), UsedParameters);
3266 for (unsigned I = Skip2; I < NumParams; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003267 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003268 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00003269 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00003270 break;
3271 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003272
Douglas Gregor8a514912009-09-14 18:39:43 +00003273 case TPOC_Conversion:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003274 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003275 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00003276 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00003277 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003278
Douglas Gregor8a514912009-09-14 18:39:43 +00003279 case TPOC_Other:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003280 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003281 TemplateParams->getDepth(),
3282 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00003283 break;
3284 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003285
Douglas Gregor8a514912009-09-14 18:39:43 +00003286 for (; ArgIdx != NumArgs; ++ArgIdx)
3287 // If this argument had no value deduced but was used in one of the types
3288 // used for partial ordering, then deduction fails.
3289 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
3290 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003291
Douglas Gregor8a514912009-09-14 18:39:43 +00003292 return true;
3293}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003294
Douglas Gregor9da95e62011-01-16 16:03:23 +00003295/// \brief Determine whether this a function template whose parameter-type-list
3296/// ends with a function parameter pack.
3297static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) {
3298 FunctionDecl *Function = FunTmpl->getTemplatedDecl();
3299 unsigned NumParams = Function->getNumParams();
3300 if (NumParams == 0)
3301 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003302
Douglas Gregor9da95e62011-01-16 16:03:23 +00003303 ParmVarDecl *Last = Function->getParamDecl(NumParams - 1);
3304 if (!Last->isParameterPack())
3305 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003306
Douglas Gregor9da95e62011-01-16 16:03:23 +00003307 // Make sure that no previous parameter is a parameter pack.
3308 while (--NumParams > 0) {
3309 if (Function->getParamDecl(NumParams - 1)->isParameterPack())
3310 return false;
3311 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003312
Douglas Gregor9da95e62011-01-16 16:03:23 +00003313 return true;
3314}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003315
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003316/// \brief Returns the more specialized function template according
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003317/// to the rules of function template partial ordering (C++ [temp.func.order]).
3318///
3319/// \param FT1 the first function template
3320///
3321/// \param FT2 the second function template
3322///
Douglas Gregor8a514912009-09-14 18:39:43 +00003323/// \param TPOC the context in which we are performing partial ordering of
3324/// function templates.
Mike Stump1eb44332009-09-09 15:08:12 +00003325///
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003326/// \param NumCallArguments The number of arguments in a call, used only
3327/// when \c TPOC is \c TPOC_Call.
3328///
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003329/// \returns the more specialized function template. If neither
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003330/// template is more specialized, returns NULL.
3331FunctionTemplateDecl *
3332Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
3333 FunctionTemplateDecl *FT2,
John McCall5769d612010-02-08 23:07:23 +00003334 SourceLocation Loc,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003335 TemplatePartialOrderingContext TPOC,
3336 unsigned NumCallArguments) {
Douglas Gregorb939a192011-01-21 17:29:42 +00003337 llvm::SmallVector<RefParamPartialOrderingComparison, 4> RefParamComparisons;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003338 bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003339 NumCallArguments, 0);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003340 bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003341 NumCallArguments,
Douglas Gregorb939a192011-01-21 17:29:42 +00003342 &RefParamComparisons);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003343
Douglas Gregor8a514912009-09-14 18:39:43 +00003344 if (Better1 != Better2) // We have a clear winner
3345 return Better1? FT1 : FT2;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003346
Douglas Gregor8a514912009-09-14 18:39:43 +00003347 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003348 return 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00003349
Douglas Gregor8a514912009-09-14 18:39:43 +00003350 // C++0x [temp.deduct.partial]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003351 // If for each type being considered a given template is at least as
Douglas Gregor8a514912009-09-14 18:39:43 +00003352 // specialized for all types and more specialized for some set of types and
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003353 // the other template is not more specialized for any types or is not at
Douglas Gregor8a514912009-09-14 18:39:43 +00003354 // least as specialized for any types, then the given template is more
3355 // specialized than the other template. Otherwise, neither template is more
3356 // specialized than the other.
3357 Better1 = false;
3358 Better2 = false;
Douglas Gregorb939a192011-01-21 17:29:42 +00003359 for (unsigned I = 0, N = RefParamComparisons.size(); I != N; ++I) {
Douglas Gregor8a514912009-09-14 18:39:43 +00003360 // C++0x [temp.deduct.partial]p9:
3361 // If, for a given type, deduction succeeds in both directions (i.e., the
Douglas Gregorb939a192011-01-21 17:29:42 +00003362 // types are identical after the transformations above) and both P and A
3363 // were reference types (before being replaced with the type referred to
3364 // above):
3365
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003366 // -- if the type from the argument template was an lvalue reference
Douglas Gregorb939a192011-01-21 17:29:42 +00003367 // and the type from the parameter template was not, the argument
3368 // type is considered to be more specialized than the other;
3369 // otherwise,
3370 if (!RefParamComparisons[I].ArgIsRvalueRef &&
3371 RefParamComparisons[I].ParamIsRvalueRef) {
3372 Better2 = true;
3373 if (Better1)
3374 return 0;
3375 continue;
3376 } else if (!RefParamComparisons[I].ParamIsRvalueRef &&
3377 RefParamComparisons[I].ArgIsRvalueRef) {
3378 Better1 = true;
3379 if (Better2)
3380 return 0;
3381 continue;
Douglas Gregor8a514912009-09-14 18:39:43 +00003382 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003383
Douglas Gregorb939a192011-01-21 17:29:42 +00003384 // -- if the type from the argument template is more cv-qualified than
3385 // the type from the parameter template (as described above), the
3386 // argument type is considered to be more specialized than the
3387 // other; otherwise,
3388 switch (RefParamComparisons[I].Qualifiers) {
3389 case NeitherMoreQualified:
3390 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003391
Douglas Gregorb939a192011-01-21 17:29:42 +00003392 case ParamMoreQualified:
3393 Better1 = true;
3394 if (Better2)
3395 return 0;
3396 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003397
Douglas Gregorb939a192011-01-21 17:29:42 +00003398 case ArgMoreQualified:
3399 Better2 = true;
3400 if (Better1)
3401 return 0;
3402 continue;
3403 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003404
Douglas Gregorb939a192011-01-21 17:29:42 +00003405 // -- neither type is more specialized than the other.
Douglas Gregor8a514912009-09-14 18:39:43 +00003406 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003407
Douglas Gregor8a514912009-09-14 18:39:43 +00003408 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003409 if (Better1)
3410 return FT1;
Douglas Gregor8a514912009-09-14 18:39:43 +00003411 else if (Better2)
3412 return FT2;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003413
Douglas Gregor9da95e62011-01-16 16:03:23 +00003414 // FIXME: This mimics what GCC implements, but doesn't match up with the
3415 // proposed resolution for core issue 692. This area needs to be sorted out,
3416 // but for now we attempt to maintain compatibility.
3417 bool Variadic1 = isVariadicFunctionTemplate(FT1);
3418 bool Variadic2 = isVariadicFunctionTemplate(FT2);
3419 if (Variadic1 != Variadic2)
3420 return Variadic1? FT2 : FT1;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003421
Douglas Gregor9da95e62011-01-16 16:03:23 +00003422 return 0;
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00003423}
Douglas Gregor83314aa2009-07-08 20:55:45 +00003424
Douglas Gregord5a423b2009-09-25 18:43:00 +00003425/// \brief Determine if the two templates are equivalent.
3426static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
3427 if (T1 == T2)
3428 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003429
Douglas Gregord5a423b2009-09-25 18:43:00 +00003430 if (!T1 || !T2)
3431 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003432
Douglas Gregord5a423b2009-09-25 18:43:00 +00003433 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
3434}
3435
3436/// \brief Retrieve the most specialized of the given function template
3437/// specializations.
3438///
John McCallc373d482010-01-27 01:50:18 +00003439/// \param SpecBegin the start iterator of the function template
3440/// specializations that we will be comparing.
Douglas Gregord5a423b2009-09-25 18:43:00 +00003441///
John McCallc373d482010-01-27 01:50:18 +00003442/// \param SpecEnd the end iterator of the function template
3443/// specializations, paired with \p SpecBegin.
Douglas Gregord5a423b2009-09-25 18:43:00 +00003444///
3445/// \param TPOC the partial ordering context to use to compare the function
3446/// template specializations.
3447///
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003448/// \param NumCallArguments The number of arguments in a call, used only
3449/// when \c TPOC is \c TPOC_Call.
3450///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003451/// \param Loc the location where the ambiguity or no-specializations
Douglas Gregord5a423b2009-09-25 18:43:00 +00003452/// diagnostic should occur.
3453///
3454/// \param NoneDiag partial diagnostic used to diagnose cases where there are
3455/// no matching candidates.
3456///
3457/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
3458/// occurs.
3459///
3460/// \param CandidateDiag partial diagnostic used for each function template
3461/// specialization that is a candidate in the ambiguous ordering. One parameter
3462/// in this diagnostic should be unbound, which will correspond to the string
3463/// describing the template arguments for the function template specialization.
3464///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003465/// \param Index if non-NULL and the result of this function is non-nULL,
Douglas Gregord5a423b2009-09-25 18:43:00 +00003466/// receives the index corresponding to the resulting function template
3467/// specialization.
3468///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003469/// \returns the most specialized function template specialization, if
John McCallc373d482010-01-27 01:50:18 +00003470/// found. Otherwise, returns SpecEnd.
Douglas Gregord5a423b2009-09-25 18:43:00 +00003471///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003472/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
Douglas Gregord5a423b2009-09-25 18:43:00 +00003473/// template argument deduction.
John McCallc373d482010-01-27 01:50:18 +00003474UnresolvedSetIterator
3475Sema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003476 UnresolvedSetIterator SpecEnd,
John McCallc373d482010-01-27 01:50:18 +00003477 TemplatePartialOrderingContext TPOC,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003478 unsigned NumCallArguments,
John McCallc373d482010-01-27 01:50:18 +00003479 SourceLocation Loc,
3480 const PartialDiagnostic &NoneDiag,
3481 const PartialDiagnostic &AmbigDiag,
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003482 const PartialDiagnostic &CandidateDiag,
3483 bool Complain) {
John McCallc373d482010-01-27 01:50:18 +00003484 if (SpecBegin == SpecEnd) {
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003485 if (Complain)
3486 Diag(Loc, NoneDiag);
John McCallc373d482010-01-27 01:50:18 +00003487 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00003488 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003489
3490 if (SpecBegin + 1 == SpecEnd)
John McCallc373d482010-01-27 01:50:18 +00003491 return SpecBegin;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003492
Douglas Gregord5a423b2009-09-25 18:43:00 +00003493 // Find the function template that is better than all of the templates it
3494 // has been compared to.
John McCallc373d482010-01-27 01:50:18 +00003495 UnresolvedSetIterator Best = SpecBegin;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003496 FunctionTemplateDecl *BestTemplate
John McCallc373d482010-01-27 01:50:18 +00003497 = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00003498 assert(BestTemplate && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00003499 for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
3500 FunctionTemplateDecl *Challenger
3501 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00003502 assert(Challenger && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00003503 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003504 Loc, TPOC, NumCallArguments),
Douglas Gregord5a423b2009-09-25 18:43:00 +00003505 Challenger)) {
3506 Best = I;
3507 BestTemplate = Challenger;
3508 }
3509 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003510
Douglas Gregord5a423b2009-09-25 18:43:00 +00003511 // Make sure that the "best" function template is more specialized than all
3512 // of the others.
3513 bool Ambiguous = false;
John McCallc373d482010-01-27 01:50:18 +00003514 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
3515 FunctionTemplateDecl *Challenger
3516 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00003517 if (I != Best &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003518 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003519 Loc, TPOC, NumCallArguments),
Douglas Gregord5a423b2009-09-25 18:43:00 +00003520 BestTemplate)) {
3521 Ambiguous = true;
3522 break;
3523 }
3524 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003525
Douglas Gregord5a423b2009-09-25 18:43:00 +00003526 if (!Ambiguous) {
3527 // We found an answer. Return it.
John McCallc373d482010-01-27 01:50:18 +00003528 return Best;
Douglas Gregord5a423b2009-09-25 18:43:00 +00003529 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003530
Douglas Gregord5a423b2009-09-25 18:43:00 +00003531 // Diagnose the ambiguity.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003532 if (Complain)
3533 Diag(Loc, AmbigDiag);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003534
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003535 if (Complain)
Douglas Gregord5a423b2009-09-25 18:43:00 +00003536 // FIXME: Can we order the candidates in some sane way?
Douglas Gregor1be8eec2011-02-19 21:32:49 +00003537 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
3538 Diag((*I)->getLocation(), CandidateDiag)
3539 << getTemplateArgumentBindingsText(
3540 cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
John McCallc373d482010-01-27 01:50:18 +00003541 *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003542
John McCallc373d482010-01-27 01:50:18 +00003543 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00003544}
3545
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003546/// \brief Returns the more specialized class template partial specialization
3547/// according to the rules of partial ordering of class template partial
3548/// specializations (C++ [temp.class.order]).
3549///
3550/// \param PS1 the first class template partial specialization
3551///
3552/// \param PS2 the second class template partial specialization
3553///
3554/// \returns the more specialized class template partial specialization. If
3555/// neither partial specialization is more specialized, returns NULL.
3556ClassTemplatePartialSpecializationDecl *
3557Sema::getMoreSpecializedPartialSpecialization(
3558 ClassTemplatePartialSpecializationDecl *PS1,
John McCall5769d612010-02-08 23:07:23 +00003559 ClassTemplatePartialSpecializationDecl *PS2,
3560 SourceLocation Loc) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003561 // C++ [temp.class.order]p1:
3562 // For two class template partial specializations, the first is at least as
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003563 // specialized as the second if, given the following rewrite to two
3564 // function templates, the first function template is at least as
3565 // specialized as the second according to the ordering rules for function
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003566 // templates (14.6.6.2):
3567 // - the first function template has the same template parameters as the
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003568 // first partial specialization and has a single function parameter
3569 // whose type is a class template specialization with the template
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003570 // arguments of the first partial specialization, and
3571 // - the second function template has the same template parameters as the
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003572 // second partial specialization and has a single function parameter
3573 // whose type is a class template specialization with the template
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003574 // arguments of the second partial specialization.
3575 //
Douglas Gregor31dce8f2010-04-29 06:21:43 +00003576 // Rather than synthesize function templates, we merely perform the
3577 // equivalent partial ordering by performing deduction directly on
3578 // the template arguments of the class template partial
3579 // specializations. This computation is slightly simpler than the
3580 // general problem of function template partial ordering, because
3581 // class template partial specializations are more constrained. We
3582 // know that every template parameter is deducible from the class
3583 // template partial specialization's template arguments, for
3584 // example.
Douglas Gregor02024a92010-03-28 02:42:43 +00003585 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
John McCall2a7fb272010-08-25 05:32:35 +00003586 TemplateDeductionInfo Info(Context, Loc);
John McCall31f17ec2010-04-27 00:57:59 +00003587
3588 QualType PT1 = PS1->getInjectedSpecializationType();
3589 QualType PT2 = PS2->getInjectedSpecializationType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003590
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003591 // Determine whether PS1 is at least as specialized as PS2
3592 Deduced.resize(PS2->getTemplateParameters()->size());
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003593 bool Better1 = !::DeduceTemplateArguments(*this, PS2->getTemplateParameters(),
3594 PT2, PT1, Info, Deduced, TDF_None,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003595 /*PartialOrdering=*/true,
Douglas Gregorb939a192011-01-21 17:29:42 +00003596 /*RefParamComparisons=*/0);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00003597 if (Better1) {
3598 InstantiatingTemplate Inst(*this, PS2->getLocation(), PS2,
3599 Deduced.data(), Deduced.size(), Info);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003600 Better1 = !::FinishTemplateArgumentDeduction(*this, PS2,
3601 PS1->getTemplateArgs(),
Douglas Gregor516e6e02010-04-29 06:31:36 +00003602 Deduced, Info);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00003603 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003604
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003605 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregordb0d4b72009-11-11 23:06:43 +00003606 Deduced.clear();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003607 Deduced.resize(PS1->getTemplateParameters()->size());
Douglas Gregor5c7bf422011-01-11 17:34:58 +00003608 bool Better2 = !::DeduceTemplateArguments(*this, PS1->getTemplateParameters(),
3609 PT1, PT2, Info, Deduced, TDF_None,
3610 /*PartialOrdering=*/true,
Douglas Gregorb939a192011-01-21 17:29:42 +00003611 /*RefParamComparisons=*/0);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00003612 if (Better2) {
3613 InstantiatingTemplate Inst(*this, PS1->getLocation(), PS1,
3614 Deduced.data(), Deduced.size(), Info);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003615 Better2 = !::FinishTemplateArgumentDeduction(*this, PS1,
3616 PS2->getTemplateArgs(),
Douglas Gregor516e6e02010-04-29 06:31:36 +00003617 Deduced, Info);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00003618 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003619
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003620 if (Better1 == Better2)
3621 return 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003622
Douglas Gregorbf4ea562009-09-15 16:23:51 +00003623 return Better1? PS1 : PS2;
3624}
3625
Mike Stump1eb44332009-09-09 15:08:12 +00003626static void
Douglas Gregore73bb602009-09-14 21:25:05 +00003627MarkUsedTemplateParameters(Sema &SemaRef,
3628 const TemplateArgument &TemplateArg,
3629 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003630 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003631 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003632
Douglas Gregore73bb602009-09-14 21:25:05 +00003633/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00003634/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00003635static void
Douglas Gregore73bb602009-09-14 21:25:05 +00003636MarkUsedTemplateParameters(Sema &SemaRef,
3637 const Expr *E,
3638 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003639 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003640 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregorbe230c32011-01-03 17:17:50 +00003641 // We can deduce from a pack expansion.
3642 if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E))
3643 E = Expansion->getPattern();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003644
Douglas Gregor54c53cc2011-01-04 23:35:54 +00003645 // Skip through any implicit casts we added while type-checking.
3646 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
3647 E = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003648
3649 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
Douglas Gregore73bb602009-09-14 21:25:05 +00003650 // find other occurrences of template parameters.
Douglas Gregorf6ddb732009-06-18 18:45:36 +00003651 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregorc781f9c2010-01-14 18:13:22 +00003652 if (!DRE)
Douglas Gregor031a5882009-06-13 00:26:55 +00003653 return;
3654
Mike Stump1eb44332009-09-09 15:08:12 +00003655 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00003656 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
3657 if (!NTTP)
3658 return;
3659
Douglas Gregored9c0f92009-10-29 00:04:11 +00003660 if (NTTP->getDepth() == Depth)
3661 Used[NTTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00003662}
3663
Douglas Gregore73bb602009-09-14 21:25:05 +00003664/// \brief Mark the template parameters that are used by the given
3665/// nested name specifier.
3666static void
3667MarkUsedTemplateParameters(Sema &SemaRef,
3668 NestedNameSpecifier *NNS,
3669 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003670 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003671 llvm::SmallVectorImpl<bool> &Used) {
3672 if (!NNS)
3673 return;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003674
Douglas Gregored9c0f92009-10-29 00:04:11 +00003675 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
3676 Used);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003677 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003678 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003679}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003680
Douglas Gregore73bb602009-09-14 21:25:05 +00003681/// \brief Mark the template parameters that are used by the given
3682/// template name.
3683static void
3684MarkUsedTemplateParameters(Sema &SemaRef,
3685 TemplateName Name,
3686 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003687 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003688 llvm::SmallVectorImpl<bool> &Used) {
3689 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
3690 if (TemplateTemplateParmDecl *TTP
Douglas Gregored9c0f92009-10-29 00:04:11 +00003691 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
3692 if (TTP->getDepth() == Depth)
3693 Used[TTP->getIndex()] = true;
3694 }
Douglas Gregore73bb602009-09-14 21:25:05 +00003695 return;
3696 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003697
Douglas Gregor788cd062009-11-11 01:00:40 +00003698 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003699 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
Douglas Gregor788cd062009-11-11 01:00:40 +00003700 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003701 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003702 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003703 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003704}
3705
3706/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00003707/// type.
Mike Stump1eb44332009-09-09 15:08:12 +00003708static void
Douglas Gregore73bb602009-09-14 21:25:05 +00003709MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
3710 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003711 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003712 llvm::SmallVectorImpl<bool> &Used) {
3713 if (T.isNull())
3714 return;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003715
Douglas Gregor031a5882009-06-13 00:26:55 +00003716 // Non-dependent types have nothing deducible
3717 if (!T->isDependentType())
3718 return;
3719
3720 T = SemaRef.Context.getCanonicalType(T);
3721 switch (T->getTypeClass()) {
Douglas Gregor031a5882009-06-13 00:26:55 +00003722 case Type::Pointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00003723 MarkUsedTemplateParameters(SemaRef,
3724 cast<PointerType>(T)->getPointeeType(),
3725 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003726 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003727 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003728 break;
3729
3730 case Type::BlockPointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00003731 MarkUsedTemplateParameters(SemaRef,
3732 cast<BlockPointerType>(T)->getPointeeType(),
3733 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003734 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003735 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003736 break;
3737
3738 case Type::LValueReference:
3739 case Type::RValueReference:
Douglas Gregore73bb602009-09-14 21:25:05 +00003740 MarkUsedTemplateParameters(SemaRef,
3741 cast<ReferenceType>(T)->getPointeeType(),
3742 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003743 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003744 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003745 break;
3746
3747 case Type::MemberPointer: {
3748 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore73bb602009-09-14 21:25:05 +00003749 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003750 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003751 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003752 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003753 break;
3754 }
3755
3756 case Type::DependentSizedArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00003757 MarkUsedTemplateParameters(SemaRef,
3758 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003759 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003760 // Fall through to check the element type
3761
3762 case Type::ConstantArray:
3763 case Type::IncompleteArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00003764 MarkUsedTemplateParameters(SemaRef,
3765 cast<ArrayType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003766 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003767 break;
3768
3769 case Type::Vector:
3770 case Type::ExtVector:
Douglas Gregore73bb602009-09-14 21:25:05 +00003771 MarkUsedTemplateParameters(SemaRef,
3772 cast<VectorType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003773 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003774 break;
3775
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00003776 case Type::DependentSizedExtVector: {
3777 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00003778 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00003779 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003780 Depth, Used);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003781 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003782 Depth, Used);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00003783 break;
3784 }
3785
Douglas Gregor031a5882009-06-13 00:26:55 +00003786 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00003787 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00003788 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003789 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003790 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore73bb602009-09-14 21:25:05 +00003791 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003792 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003793 break;
3794 }
3795
Douglas Gregored9c0f92009-10-29 00:04:11 +00003796 case Type::TemplateTypeParm: {
3797 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
3798 if (TTP->getDepth() == Depth)
3799 Used[TTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00003800 break;
Douglas Gregored9c0f92009-10-29 00:04:11 +00003801 }
Douglas Gregor031a5882009-06-13 00:26:55 +00003802
Douglas Gregor0bc15d92011-01-14 05:11:40 +00003803 case Type::SubstTemplateTypeParmPack: {
3804 const SubstTemplateTypeParmPackType *Subst
3805 = cast<SubstTemplateTypeParmPackType>(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003806 MarkUsedTemplateParameters(SemaRef,
Douglas Gregor0bc15d92011-01-14 05:11:40 +00003807 QualType(Subst->getReplacedParameter(), 0),
3808 OnlyDeduced, Depth, Used);
3809 MarkUsedTemplateParameters(SemaRef, Subst->getArgumentPack(),
3810 OnlyDeduced, Depth, Used);
3811 break;
3812 }
3813
John McCall31f17ec2010-04-27 00:57:59 +00003814 case Type::InjectedClassName:
3815 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
3816 // fall through
3817
Douglas Gregor031a5882009-06-13 00:26:55 +00003818 case Type::TemplateSpecialization: {
Mike Stump1eb44332009-09-09 15:08:12 +00003819 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00003820 = cast<TemplateSpecializationType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00003821 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003822 Depth, Used);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003823
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003824 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003825 // If the template argument list of P contains a pack expansion that is not
3826 // the last template argument, the entire template argument list is a
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003827 // non-deduced context.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003828 if (OnlyDeduced &&
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003829 hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs()))
3830 break;
3831
Douglas Gregore73bb602009-09-14 21:25:05 +00003832 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00003833 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
3834 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003835 break;
3836 }
3837
Douglas Gregore73bb602009-09-14 21:25:05 +00003838 case Type::Complex:
3839 if (!OnlyDeduced)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003840 MarkUsedTemplateParameters(SemaRef,
Douglas Gregore73bb602009-09-14 21:25:05 +00003841 cast<ComplexType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003842 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003843 break;
3844
Douglas Gregor4714c122010-03-31 17:34:00 +00003845 case Type::DependentName:
Douglas Gregore73bb602009-09-14 21:25:05 +00003846 if (!OnlyDeduced)
3847 MarkUsedTemplateParameters(SemaRef,
Douglas Gregor4714c122010-03-31 17:34:00 +00003848 cast<DependentNameType>(T)->getQualifier(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00003849 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00003850 break;
3851
John McCall33500952010-06-11 00:33:02 +00003852 case Type::DependentTemplateSpecialization: {
3853 const DependentTemplateSpecializationType *Spec
3854 = cast<DependentTemplateSpecializationType>(T);
3855 if (!OnlyDeduced)
3856 MarkUsedTemplateParameters(SemaRef, Spec->getQualifier(),
3857 OnlyDeduced, Depth, Used);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003858
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003859 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003860 // If the template argument list of P contains a pack expansion that is not
3861 // the last template argument, the entire template argument list is a
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003862 // non-deduced context.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003863 if (OnlyDeduced &&
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003864 hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs()))
3865 break;
3866
John McCall33500952010-06-11 00:33:02 +00003867 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
3868 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
3869 Used);
3870 break;
3871 }
3872
John McCallad5e7382010-03-01 23:49:17 +00003873 case Type::TypeOf:
3874 if (!OnlyDeduced)
3875 MarkUsedTemplateParameters(SemaRef,
3876 cast<TypeOfType>(T)->getUnderlyingType(),
3877 OnlyDeduced, Depth, Used);
3878 break;
3879
3880 case Type::TypeOfExpr:
3881 if (!OnlyDeduced)
3882 MarkUsedTemplateParameters(SemaRef,
3883 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
3884 OnlyDeduced, Depth, Used);
3885 break;
3886
3887 case Type::Decltype:
3888 if (!OnlyDeduced)
3889 MarkUsedTemplateParameters(SemaRef,
3890 cast<DecltypeType>(T)->getUnderlyingExpr(),
3891 OnlyDeduced, Depth, Used);
3892 break;
3893
Sean Huntca63c202011-05-24 22:41:36 +00003894 case Type::UnaryTransform:
3895 if (!OnlyDeduced)
3896 MarkUsedTemplateParameters(SemaRef,
3897 cast<UnaryTransformType>(T)->getUnderlyingType(),
3898 OnlyDeduced, Depth, Used);
3899 break;
3900
Douglas Gregor7536dd52010-12-20 02:24:11 +00003901 case Type::PackExpansion:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003902 MarkUsedTemplateParameters(SemaRef,
Douglas Gregor7536dd52010-12-20 02:24:11 +00003903 cast<PackExpansionType>(T)->getPattern(),
3904 OnlyDeduced, Depth, Used);
3905 break;
3906
Richard Smith34b41d92011-02-20 03:19:35 +00003907 case Type::Auto:
3908 MarkUsedTemplateParameters(SemaRef,
3909 cast<AutoType>(T)->getDeducedType(),
3910 OnlyDeduced, Depth, Used);
3911
Douglas Gregore73bb602009-09-14 21:25:05 +00003912 // None of these types have any template parameters in them.
Douglas Gregor031a5882009-06-13 00:26:55 +00003913 case Type::Builtin:
Douglas Gregor031a5882009-06-13 00:26:55 +00003914 case Type::VariableArray:
3915 case Type::FunctionNoProto:
3916 case Type::Record:
3917 case Type::Enum:
Douglas Gregor031a5882009-06-13 00:26:55 +00003918 case Type::ObjCInterface:
John McCallc12c5bb2010-05-15 11:32:37 +00003919 case Type::ObjCObject:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00003920 case Type::ObjCObjectPointer:
John McCalled976492009-12-04 22:46:56 +00003921 case Type::UnresolvedUsing:
Douglas Gregor031a5882009-06-13 00:26:55 +00003922#define TYPE(Class, Base)
3923#define ABSTRACT_TYPE(Class, Base)
3924#define DEPENDENT_TYPE(Class, Base)
3925#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3926#include "clang/AST/TypeNodes.def"
3927 break;
3928 }
3929}
3930
Douglas Gregore73bb602009-09-14 21:25:05 +00003931/// \brief Mark the template parameters that are used by this
Douglas Gregor031a5882009-06-13 00:26:55 +00003932/// template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00003933static void
Douglas Gregore73bb602009-09-14 21:25:05 +00003934MarkUsedTemplateParameters(Sema &SemaRef,
3935 const TemplateArgument &TemplateArg,
3936 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003937 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003938 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00003939 switch (TemplateArg.getKind()) {
3940 case TemplateArgument::Null:
3941 case TemplateArgument::Integral:
Douglas Gregor788cd062009-11-11 01:00:40 +00003942 case TemplateArgument::Declaration:
Douglas Gregor031a5882009-06-13 00:26:55 +00003943 break;
Mike Stump1eb44332009-09-09 15:08:12 +00003944
Douglas Gregor031a5882009-06-13 00:26:55 +00003945 case TemplateArgument::Type:
Douglas Gregore73bb602009-09-14 21:25:05 +00003946 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003947 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003948 break;
3949
Douglas Gregor788cd062009-11-11 01:00:40 +00003950 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00003951 case TemplateArgument::TemplateExpansion:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003952 MarkUsedTemplateParameters(SemaRef,
3953 TemplateArg.getAsTemplateOrTemplatePattern(),
Douglas Gregor788cd062009-11-11 01:00:40 +00003954 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003955 break;
3956
3957 case TemplateArgument::Expression:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003958 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003959 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003960 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003961
Anders Carlssond01b1da2009-06-15 17:04:53 +00003962 case TemplateArgument::Pack:
Douglas Gregore73bb602009-09-14 21:25:05 +00003963 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
3964 PEnd = TemplateArg.pack_end();
3965 P != PEnd; ++P)
Douglas Gregored9c0f92009-10-29 00:04:11 +00003966 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssond01b1da2009-06-15 17:04:53 +00003967 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00003968 }
3969}
3970
3971/// \brief Mark the template parameters can be deduced by the given
3972/// template argument list.
3973///
3974/// \param TemplateArgs the template argument list from which template
3975/// parameters will be deduced.
3976///
3977/// \param Deduced a bit vector whose elements will be set to \c true
3978/// to indicate when the corresponding template parameter will be
3979/// deduced.
Mike Stump1eb44332009-09-09 15:08:12 +00003980void
Douglas Gregore73bb602009-09-14 21:25:05 +00003981Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003982 bool OnlyDeduced, unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00003983 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003984 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003985 // If the template argument list of P contains a pack expansion that is not
3986 // the last template argument, the entire template argument list is a
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003987 // non-deduced context.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003988 if (OnlyDeduced &&
Douglas Gregor7b976ec2010-12-23 01:24:45 +00003989 hasPackExpansionBeforeEnd(TemplateArgs.data(), TemplateArgs.size()))
3990 return;
3991
Douglas Gregor031a5882009-06-13 00:26:55 +00003992 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003993 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00003994 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00003995}
Douglas Gregor63f07c52009-09-18 23:21:38 +00003996
3997/// \brief Marks all of the template parameters that will be deduced by a
3998/// call to the given function template.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003999void
Douglas Gregor02024a92010-03-28 02:42:43 +00004000Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
4001 llvm::SmallVectorImpl<bool> &Deduced) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004002 TemplateParameterList *TemplateParams
Douglas Gregor63f07c52009-09-18 23:21:38 +00004003 = FunctionTemplate->getTemplateParameters();
4004 Deduced.clear();
4005 Deduced.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004006
Douglas Gregor63f07c52009-09-18 23:21:38 +00004007 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
4008 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
4009 ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00004010 true, TemplateParams->getDepth(), Deduced);
Douglas Gregor63f07c52009-09-18 23:21:38 +00004011}