blob: 108f20751032aab96be8ce01eae12b6ef78b7d13 [file] [log] [blame]
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001//===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template argument deduction.
10//
11//===----------------------------------------------------------------------===/
12
Douglas Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Sema.h"
John McCall19510852010-08-20 18:27:03 +000014#include "clang/Sema/DeclSpec.h"
Douglas Gregor20a55e22010-12-22 18:17:10 +000015#include "clang/Sema/SemaDiagnostic.h" // FIXME: temporary!
John McCall7cd088e2010-08-24 07:21:54 +000016#include "clang/Sema/Template.h"
John McCall2a7fb272010-08-25 05:32:35 +000017#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor0b9247f2009-06-04 00:03:07 +000018#include "clang/AST/ASTContext.h"
John McCall7cd088e2010-08-24 07:21:54 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor0b9247f2009-06-04 00:03:07 +000020#include "clang/AST/DeclTemplate.h"
21#include "clang/AST/StmtVisitor.h"
22#include "clang/AST/Expr.h"
23#include "clang/AST/ExprCXX.h"
Douglas Gregor8a514912009-09-14 18:39:43 +000024#include <algorithm>
Douglas Gregor508f1c82009-06-26 23:10:12 +000025
26namespace clang {
John McCall2a7fb272010-08-25 05:32:35 +000027 using namespace sema;
28
Douglas Gregor508f1c82009-06-26 23:10:12 +000029 /// \brief Various flags that control template argument deduction.
30 ///
31 /// These flags can be bitwise-OR'd together.
32 enum TemplateDeductionFlags {
33 /// \brief No template argument deduction flags, which indicates the
34 /// strictest results for template argument deduction (as used for, e.g.,
35 /// matching class template partial specializations).
36 TDF_None = 0,
37 /// \brief Within template argument deduction from a function call, we are
38 /// matching with a parameter type for which the original parameter was
39 /// a reference.
40 TDF_ParamWithReferenceType = 0x1,
41 /// \brief Within template argument deduction from a function call, we
42 /// are matching in a case where we ignore cv-qualifiers.
43 TDF_IgnoreQualifiers = 0x02,
44 /// \brief Within template argument deduction from a function call,
45 /// we are matching in a case where we can perform template argument
Douglas Gregor41128772009-06-26 23:27:24 +000046 /// deduction from a template-id of a derived class of the argument type.
Douglas Gregor12820292009-09-14 20:00:47 +000047 TDF_DerivedClass = 0x04,
48 /// \brief Allow non-dependent types to differ, e.g., when performing
49 /// template argument deduction from a function call where conversions
50 /// may apply.
51 TDF_SkipNonDependent = 0x08
Douglas Gregor508f1c82009-06-26 23:10:12 +000052 };
53}
54
Douglas Gregor0b9247f2009-06-04 00:03:07 +000055using namespace clang;
56
Douglas Gregor9d0e4412010-03-26 05:50:28 +000057/// \brief Compare two APSInts, extending and switching the sign as
58/// necessary to compare their values regardless of underlying type.
59static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) {
60 if (Y.getBitWidth() > X.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +000061 X = X.extend(Y.getBitWidth());
Douglas Gregor9d0e4412010-03-26 05:50:28 +000062 else if (Y.getBitWidth() < X.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +000063 Y = Y.extend(X.getBitWidth());
Douglas Gregor9d0e4412010-03-26 05:50:28 +000064
65 // If there is a signedness mismatch, correct it.
66 if (X.isSigned() != Y.isSigned()) {
67 // If the signed value is negative, then the values cannot be the same.
68 if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative()))
69 return false;
70
71 Y.setIsSigned(true);
72 X.setIsSigned(true);
73 }
74
75 return X == Y;
76}
77
Douglas Gregorf67875d2009-06-12 18:26:56 +000078static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +000079DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +000080 TemplateParameterList *TemplateParams,
81 const TemplateArgument &Param,
Douglas Gregord708c722009-06-09 16:35:58 +000082 const TemplateArgument &Arg,
John McCall2a7fb272010-08-25 05:32:35 +000083 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +000084 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced);
Douglas Gregord708c722009-06-09 16:35:58 +000085
Douglas Gregor20a55e22010-12-22 18:17:10 +000086static Sema::TemplateDeductionResult
87DeduceTemplateArguments(Sema &S,
88 TemplateParameterList *TemplateParams,
89 const TemplateArgument *Params, unsigned NumParams,
90 const TemplateArgument *Args, unsigned NumArgs,
91 TemplateDeductionInfo &Info,
92 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced);
93
Douglas Gregor199d9912009-06-05 00:53:49 +000094/// \brief If the given expression is of a form that permits the deduction
95/// of a non-type template parameter, return the declaration of that
96/// non-type template parameter.
97static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
98 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
99 E = IC->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Douglas Gregor199d9912009-06-05 00:53:49 +0000101 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
102 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Douglas Gregor199d9912009-06-05 00:53:49 +0000104 return 0;
105}
106
Mike Stump1eb44332009-09-09 15:08:12 +0000107/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +0000108/// from the given constant.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000109static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000110DeduceNonTypeTemplateArgument(Sema &S,
Mike Stump1eb44332009-09-09 15:08:12 +0000111 NonTypeTemplateParmDecl *NTTP,
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000112 llvm::APSInt Value, QualType ValueType,
Douglas Gregor02024a92010-03-28 02:42:43 +0000113 bool DeducedFromArrayBound,
John McCall2a7fb272010-08-25 05:32:35 +0000114 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000115 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +0000116 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000117 "Cannot deduce non-type template argument with depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Douglas Gregor199d9912009-06-05 00:53:49 +0000119 if (Deduced[NTTP->getIndex()].isNull()) {
Douglas Gregor02024a92010-03-28 02:42:43 +0000120 Deduced[NTTP->getIndex()] = DeducedTemplateArgument(Value, ValueType,
121 DeducedFromArrayBound);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000122 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000123 }
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000125 if (Deduced[NTTP->getIndex()].getKind() != TemplateArgument::Integral) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000126 Info.Param = NTTP;
127 Info.FirstArg = Deduced[NTTP->getIndex()];
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000128 Info.SecondArg = TemplateArgument(Value, ValueType);
129 return Sema::TDK_Inconsistent;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000130 }
131
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000132 // Extent the smaller of the two values.
133 llvm::APSInt PrevValue = *Deduced[NTTP->getIndex()].getAsIntegral();
134 if (!hasSameExtendedValue(PrevValue, Value)) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000135 Info.Param = NTTP;
136 Info.FirstArg = Deduced[NTTP->getIndex()];
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000137 Info.SecondArg = TemplateArgument(Value, ValueType);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000138 return Sema::TDK_Inconsistent;
139 }
140
Douglas Gregor02024a92010-03-28 02:42:43 +0000141 if (!DeducedFromArrayBound)
142 Deduced[NTTP->getIndex()].setDeducedFromArrayBound(false);
143
Douglas Gregorf67875d2009-06-12 18:26:56 +0000144 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000145}
146
Mike Stump1eb44332009-09-09 15:08:12 +0000147/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +0000148/// from the given type- or value-dependent expression.
149///
150/// \returns true if deduction succeeded, false otherwise.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000151static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000152DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000153 NonTypeTemplateParmDecl *NTTP,
154 Expr *Value,
John McCall2a7fb272010-08-25 05:32:35 +0000155 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000156 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +0000157 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000158 "Cannot deduce non-type template argument with depth > 0");
159 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
160 "Expression template argument must be type- or value-dependent.");
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Douglas Gregor199d9912009-06-05 00:53:49 +0000162 if (Deduced[NTTP->getIndex()].isNull()) {
John McCall3fa5cae2010-10-26 07:05:15 +0000163 Deduced[NTTP->getIndex()] = TemplateArgument(Value);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000164 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000165 }
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Douglas Gregor199d9912009-06-05 00:53:49 +0000167 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
Mike Stump1eb44332009-09-09 15:08:12 +0000168 // Okay, we deduced a constant in one case and a dependent expression
169 // in another case. FIXME: Later, we will check that instantiating the
Douglas Gregor199d9912009-06-05 00:53:49 +0000170 // dependent expression gives us the constant value.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000171 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000172 }
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Douglas Gregor9eea08b2009-09-15 16:51:42 +0000174 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
175 // Compare the expressions for equality
176 llvm::FoldingSetNodeID ID1, ID2;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000177 Deduced[NTTP->getIndex()].getAsExpr()->Profile(ID1, S.Context, true);
178 Value->Profile(ID2, S.Context, true);
Douglas Gregor9eea08b2009-09-15 16:51:42 +0000179 if (ID1 == ID2)
180 return Sema::TDK_Success;
181
182 // FIXME: Fill in argument mismatch information
183 return Sema::TDK_NonDeducedMismatch;
184 }
185
Douglas Gregorf67875d2009-06-12 18:26:56 +0000186 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000187}
188
Douglas Gregor15755cb2009-11-13 23:45:44 +0000189/// \brief Deduce the value of the given non-type template parameter
190/// from the given declaration.
191///
192/// \returns true if deduction succeeded, false otherwise.
193static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000194DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregor15755cb2009-11-13 23:45:44 +0000195 NonTypeTemplateParmDecl *NTTP,
196 Decl *D,
John McCall2a7fb272010-08-25 05:32:35 +0000197 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000198 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor15755cb2009-11-13 23:45:44 +0000199 assert(NTTP->getDepth() == 0 &&
200 "Cannot deduce non-type template argument with depth > 0");
201
202 if (Deduced[NTTP->getIndex()].isNull()) {
203 Deduced[NTTP->getIndex()] = TemplateArgument(D->getCanonicalDecl());
204 return Sema::TDK_Success;
205 }
206
207 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
208 // Okay, we deduced a declaration in one case and a dependent expression
209 // in another case.
210 return Sema::TDK_Success;
211 }
212
213 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Declaration) {
214 // Compare the declarations for equality
215 if (Deduced[NTTP->getIndex()].getAsDecl()->getCanonicalDecl() ==
216 D->getCanonicalDecl())
217 return Sema::TDK_Success;
218
219 // FIXME: Fill in argument mismatch information
220 return Sema::TDK_NonDeducedMismatch;
221 }
222
223 return Sema::TDK_Success;
224}
225
Douglas Gregorf67875d2009-06-12 18:26:56 +0000226static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000227DeduceTemplateArguments(Sema &S,
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000228 TemplateParameterList *TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000229 TemplateName Param,
230 TemplateName Arg,
John McCall2a7fb272010-08-25 05:32:35 +0000231 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000232 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregord708c722009-06-09 16:35:58 +0000233 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000234 if (!ParamDecl) {
235 // The parameter type is dependent and is not a template template parameter,
236 // so there is nothing that we can deduce.
237 return Sema::TDK_Success;
238 }
239
240 if (TemplateTemplateParmDecl *TempParam
241 = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) {
242 // Bind the template template parameter to the given template name.
243 TemplateArgument &ExistingArg = Deduced[TempParam->getIndex()];
244 if (ExistingArg.isNull()) {
245 // This is the first deduction for this template template parameter.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000246 ExistingArg = TemplateArgument(S.Context.getCanonicalTemplateName(Arg));
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000247 return Sema::TDK_Success;
248 }
249
250 // Verify that the previous binding matches this deduction.
251 assert(ExistingArg.getKind() == TemplateArgument::Template);
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000252 if (S.Context.hasSameTemplateName(ExistingArg.getAsTemplate(), Arg))
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000253 return Sema::TDK_Success;
254
255 // Inconsistent deduction.
256 Info.Param = TempParam;
257 Info.FirstArg = ExistingArg;
258 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000259 return Sema::TDK_Inconsistent;
260 }
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000261
262 // Verify that the two template names are equivalent.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000263 if (S.Context.hasSameTemplateName(Param, Arg))
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000264 return Sema::TDK_Success;
265
266 // Mismatch of non-dependent template parameter to argument.
267 Info.FirstArg = TemplateArgument(Param);
268 Info.SecondArg = TemplateArgument(Arg);
269 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000270}
271
Mike Stump1eb44332009-09-09 15:08:12 +0000272/// \brief Deduce the template arguments by comparing the template parameter
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000273/// type (which is a template-id) with the template argument type.
274///
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000275/// \param S the Sema
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000276///
277/// \param TemplateParams the template parameters that we are deducing
278///
279/// \param Param the parameter type
280///
281/// \param Arg the argument type
282///
283/// \param Info information about the template argument deduction itself
284///
285/// \param Deduced the deduced template arguments
286///
287/// \returns the result of template argument deduction so far. Note that a
288/// "success" result means that template argument deduction has not yet failed,
289/// but it may still fail, later, for other reasons.
290static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000291DeduceTemplateArguments(Sema &S,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000292 TemplateParameterList *TemplateParams,
293 const TemplateSpecializationType *Param,
294 QualType Arg,
John McCall2a7fb272010-08-25 05:32:35 +0000295 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000296 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
John McCall467b27b2009-10-22 20:10:53 +0000297 assert(Arg.isCanonical() && "Argument type must be canonical");
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000299 // Check whether the template argument is a dependent template-id.
Mike Stump1eb44332009-09-09 15:08:12 +0000300 if (const TemplateSpecializationType *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000301 = dyn_cast<TemplateSpecializationType>(Arg)) {
302 // Perform template argument deduction for the template name.
303 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000304 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000305 Param->getTemplateName(),
306 SpecArg->getTemplateName(),
307 Info, Deduced))
308 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000311 // Perform template argument deduction on each template
312 // argument.
Douglas Gregor20a55e22010-12-22 18:17:10 +0000313 // FIXME: This "min" function isn't going to work in general. We should
314 // probably pass down a flag that allows too few/too many arguments.
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000315 unsigned NumArgs = std::min(SpecArg->getNumArgs(), Param->getNumArgs());
Douglas Gregor20a55e22010-12-22 18:17:10 +0000316 return DeduceTemplateArguments(S, TemplateParams,
317 Param->getArgs(), NumArgs,
318 SpecArg->getArgs(), NumArgs,
319 Info, Deduced);
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000320 }
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000322 // If the argument type is a class template specialization, we
323 // perform template argument deduction using its template
324 // arguments.
325 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
326 if (!RecordArg)
327 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000328
329 ClassTemplateSpecializationDecl *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000330 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
331 if (!SpecArg)
332 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000334 // Perform template argument deduction for the template name.
335 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000336 = DeduceTemplateArguments(S,
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000337 TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000338 Param->getTemplateName(),
339 TemplateName(SpecArg->getSpecializedTemplate()),
340 Info, Deduced))
341 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Douglas Gregor20a55e22010-12-22 18:17:10 +0000343 // Perform template argument deduction for the template arguments.
344 return DeduceTemplateArguments(S, TemplateParams,
345 Param->getArgs(), Param->getNumArgs(),
346 SpecArg->getTemplateArgs().data(),
347 SpecArg->getTemplateArgs().size(),
348 Info, Deduced);
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000349}
350
John McCallcd05e812010-08-28 22:14:41 +0000351/// \brief Determines whether the given type is an opaque type that
352/// might be more qualified when instantiated.
353static bool IsPossiblyOpaquelyQualifiedType(QualType T) {
354 switch (T->getTypeClass()) {
355 case Type::TypeOfExpr:
356 case Type::TypeOf:
357 case Type::DependentName:
358 case Type::Decltype:
359 case Type::UnresolvedUsing:
360 return true;
361
362 case Type::ConstantArray:
363 case Type::IncompleteArray:
364 case Type::VariableArray:
365 case Type::DependentSizedArray:
366 return IsPossiblyOpaquelyQualifiedType(
367 cast<ArrayType>(T)->getElementType());
368
369 default:
370 return false;
371 }
372}
373
Douglas Gregor500d3312009-06-26 18:27:22 +0000374/// \brief Deduce the template arguments by comparing the parameter type and
375/// the argument type (C++ [temp.deduct.type]).
376///
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000377/// \param S the semantic analysis object within which we are deducing
Douglas Gregor500d3312009-06-26 18:27:22 +0000378///
379/// \param TemplateParams the template parameters that we are deducing
380///
381/// \param ParamIn the parameter type
382///
383/// \param ArgIn the argument type
384///
385/// \param Info information about the template argument deduction itself
386///
387/// \param Deduced the deduced template arguments
388///
Douglas Gregor508f1c82009-06-26 23:10:12 +0000389/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
Mike Stump1eb44332009-09-09 15:08:12 +0000390/// how template argument deduction is performed.
Douglas Gregor500d3312009-06-26 18:27:22 +0000391///
392/// \returns the result of template argument deduction so far. Note that a
393/// "success" result means that template argument deduction has not yet failed,
394/// but it may still fail, later, for other reasons.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000395static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000396DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000397 TemplateParameterList *TemplateParams,
398 QualType ParamIn, QualType ArgIn,
John McCall2a7fb272010-08-25 05:32:35 +0000399 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000400 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000401 unsigned TDF) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000402 // We only want to look at the canonical types, since typedefs and
403 // sugar are not part of template argument deduction.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000404 QualType Param = S.Context.getCanonicalType(ParamIn);
405 QualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000406
Douglas Gregor500d3312009-06-26 18:27:22 +0000407 // C++0x [temp.deduct.call]p4 bullet 1:
408 // - If the original P is a reference type, the deduced A (i.e., the type
Mike Stump1eb44332009-09-09 15:08:12 +0000409 // referred to by the reference) can be more cv-qualified than the
Douglas Gregor500d3312009-06-26 18:27:22 +0000410 // transformed A.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000411 if (TDF & TDF_ParamWithReferenceType) {
Chandler Carruthe7242462009-12-30 04:10:01 +0000412 Qualifiers Quals;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000413 QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals);
Chandler Carruthe7242462009-12-30 04:10:01 +0000414 Quals.setCVRQualifiers(Quals.getCVRQualifiers() &
415 Arg.getCVRQualifiersThroughArrayTypes());
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000416 Param = S.Context.getQualifiedType(UnqualParam, Quals);
Douglas Gregor500d3312009-06-26 18:27:22 +0000417 }
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000419 // If the parameter type is not dependent, there is nothing to deduce.
Douglas Gregor12820292009-09-14 20:00:47 +0000420 if (!Param->isDependentType()) {
421 if (!(TDF & TDF_SkipNonDependent) && Param != Arg) {
422
423 return Sema::TDK_NonDeducedMismatch;
424 }
425
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000426 return Sema::TDK_Success;
Douglas Gregor12820292009-09-14 20:00:47 +0000427 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000428
Douglas Gregor199d9912009-06-05 00:53:49 +0000429 // C++ [temp.deduct.type]p9:
Mike Stump1eb44332009-09-09 15:08:12 +0000430 // A template type argument T, a template template argument TT or a
431 // template non-type argument i can be deduced if P and A have one of
Douglas Gregor199d9912009-06-05 00:53:49 +0000432 // the following forms:
433 //
434 // T
435 // cv-list T
Mike Stump1eb44332009-09-09 15:08:12 +0000436 if (const TemplateTypeParmType *TemplateTypeParm
John McCall183700f2009-09-21 23:43:11 +0000437 = Param->getAs<TemplateTypeParmType>()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000438 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000439 bool RecanonicalizeArg = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Douglas Gregor9e9fae42009-07-22 20:02:25 +0000441 // If the argument type is an array type, move the qualifiers up to the
442 // top level, so they can be matched with the qualifiers on the parameter.
443 // FIXME: address spaces, ObjC GC qualifiers
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000444 if (isa<ArrayType>(Arg)) {
John McCall0953e762009-09-24 19:53:00 +0000445 Qualifiers Quals;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000446 Arg = S.Context.getUnqualifiedArrayType(Arg, Quals);
John McCall0953e762009-09-24 19:53:00 +0000447 if (Quals) {
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000448 Arg = S.Context.getQualifiedType(Arg, Quals);
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000449 RecanonicalizeArg = true;
450 }
451 }
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000453 // The argument type can not be less qualified than the parameter
454 // type.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000455 if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000456 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
John McCall57e97782010-08-05 09:05:08 +0000457 Info.FirstArg = TemplateArgument(Param);
John McCall833ca992009-10-29 08:12:44 +0000458 Info.SecondArg = TemplateArgument(Arg);
John McCall57e97782010-08-05 09:05:08 +0000459 return Sema::TDK_Underqualified;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000460 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000461
462 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000463 assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function");
John McCall0953e762009-09-24 19:53:00 +0000464 QualType DeducedType = Arg;
John McCall49f4e1c2010-12-10 11:01:00 +0000465
466 // local manipulation is okay because it's canonical
467 DeducedType.removeLocalCVRQualifiers(Param.getCVRQualifiers());
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000468 if (RecanonicalizeArg)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000469 DeducedType = S.Context.getCanonicalType(DeducedType);
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000471 if (Deduced[Index].isNull())
John McCall833ca992009-10-29 08:12:44 +0000472 Deduced[Index] = TemplateArgument(DeducedType);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000473 else {
Mike Stump1eb44332009-09-09 15:08:12 +0000474 // C++ [temp.deduct.type]p2:
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000475 // [...] If type deduction cannot be done for any P/A pair, or if for
Mike Stump1eb44332009-09-09 15:08:12 +0000476 // any pair the deduction leads to more than one possible set of
477 // deduced values, or if different pairs yield different deduced
478 // values, or if any template argument remains neither deduced nor
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000479 // explicitly specified, template argument deduction fails.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000480 if (Deduced[Index].getAsType() != DeducedType) {
Mike Stump1eb44332009-09-09 15:08:12 +0000481 Info.Param
Douglas Gregorf67875d2009-06-12 18:26:56 +0000482 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
483 Info.FirstArg = Deduced[Index];
John McCall833ca992009-10-29 08:12:44 +0000484 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000485 return Sema::TDK_Inconsistent;
486 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000487 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000488 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000489 }
490
Douglas Gregorf67875d2009-06-12 18:26:56 +0000491 // Set up the template argument deduction information for a failure.
John McCall833ca992009-10-29 08:12:44 +0000492 Info.FirstArg = TemplateArgument(ParamIn);
493 Info.SecondArg = TemplateArgument(ArgIn);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000494
Douglas Gregor508f1c82009-06-26 23:10:12 +0000495 // Check the cv-qualifiers on the parameter and argument types.
496 if (!(TDF & TDF_IgnoreQualifiers)) {
497 if (TDF & TDF_ParamWithReferenceType) {
498 if (Param.isMoreQualifiedThan(Arg))
499 return Sema::TDK_NonDeducedMismatch;
John McCallcd05e812010-08-28 22:14:41 +0000500 } else if (!IsPossiblyOpaquelyQualifiedType(Param)) {
Douglas Gregor508f1c82009-06-26 23:10:12 +0000501 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Mike Stump1eb44332009-09-09 15:08:12 +0000502 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor508f1c82009-06-26 23:10:12 +0000503 }
504 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000505
Douglas Gregord560d502009-06-04 00:21:18 +0000506 switch (Param->getTypeClass()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000507 // No deduction possible for these types
508 case Type::Builtin:
Douglas Gregorf67875d2009-06-12 18:26:56 +0000509 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Douglas Gregor199d9912009-06-05 00:53:49 +0000511 // T *
Douglas Gregord560d502009-06-04 00:21:18 +0000512 case Type::Pointer: {
John McCallc0008342010-05-13 07:48:05 +0000513 QualType PointeeType;
514 if (const PointerType *PointerArg = Arg->getAs<PointerType>()) {
515 PointeeType = PointerArg->getPointeeType();
516 } else if (const ObjCObjectPointerType *PointerArg
517 = Arg->getAs<ObjCObjectPointerType>()) {
518 PointeeType = PointerArg->getPointeeType();
519 } else {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000520 return Sema::TDK_NonDeducedMismatch;
John McCallc0008342010-05-13 07:48:05 +0000521 }
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Douglas Gregor41128772009-06-26 23:27:24 +0000523 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000524 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000525 cast<PointerType>(Param)->getPointeeType(),
John McCallc0008342010-05-13 07:48:05 +0000526 PointeeType,
Douglas Gregor41128772009-06-26 23:27:24 +0000527 Info, Deduced, SubTDF);
Douglas Gregord560d502009-06-04 00:21:18 +0000528 }
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Douglas Gregor199d9912009-06-05 00:53:49 +0000530 // T &
Douglas Gregord560d502009-06-04 00:21:18 +0000531 case Type::LValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000532 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000533 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000534 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000536 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000537 cast<LValueReferenceType>(Param)->getPointeeType(),
538 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000539 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000540 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000541
Douglas Gregor199d9912009-06-05 00:53:49 +0000542 // T && [C++0x]
Douglas Gregord560d502009-06-04 00:21:18 +0000543 case Type::RValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000544 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000545 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000546 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000548 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000549 cast<RValueReferenceType>(Param)->getPointeeType(),
550 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000551 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000552 }
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Douglas Gregor199d9912009-06-05 00:53:49 +0000554 // T [] (implied, but not stated explicitly)
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000555 case Type::IncompleteArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000556 const IncompleteArrayType *IncompleteArrayArg =
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000557 S.Context.getAsIncompleteArrayType(Arg);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000558 if (!IncompleteArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000559 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000560
John McCalle4f26e52010-08-19 00:20:19 +0000561 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000562 return DeduceTemplateArguments(S, TemplateParams,
563 S.Context.getAsIncompleteArrayType(Param)->getElementType(),
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000564 IncompleteArrayArg->getElementType(),
John McCalle4f26e52010-08-19 00:20:19 +0000565 Info, Deduced, SubTDF);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000566 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000567
568 // T [integer-constant]
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000569 case Type::ConstantArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000570 const ConstantArrayType *ConstantArrayArg =
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000571 S.Context.getAsConstantArrayType(Arg);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000572 if (!ConstantArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000573 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000574
575 const ConstantArrayType *ConstantArrayParm =
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000576 S.Context.getAsConstantArrayType(Param);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000577 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000578 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000579
John McCalle4f26e52010-08-19 00:20:19 +0000580 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000581 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000582 ConstantArrayParm->getElementType(),
583 ConstantArrayArg->getElementType(),
John McCalle4f26e52010-08-19 00:20:19 +0000584 Info, Deduced, SubTDF);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000585 }
586
Douglas Gregor199d9912009-06-05 00:53:49 +0000587 // type [i]
588 case Type::DependentSizedArray: {
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000589 const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
Douglas Gregor199d9912009-06-05 00:53:49 +0000590 if (!ArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000591 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000592
John McCalle4f26e52010-08-19 00:20:19 +0000593 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
594
Douglas Gregor199d9912009-06-05 00:53:49 +0000595 // Check the element type of the arrays
596 const DependentSizedArrayType *DependentArrayParm
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000597 = S.Context.getAsDependentSizedArrayType(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000598 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000599 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000600 DependentArrayParm->getElementType(),
601 ArrayArg->getElementType(),
John McCalle4f26e52010-08-19 00:20:19 +0000602 Info, Deduced, SubTDF))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000603 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Douglas Gregor199d9912009-06-05 00:53:49 +0000605 // Determine the array bound is something we can deduce.
Mike Stump1eb44332009-09-09 15:08:12 +0000606 NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000607 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
608 if (!NTTP)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000609 return Sema::TDK_Success;
Mike Stump1eb44332009-09-09 15:08:12 +0000610
611 // We can perform template argument deduction for the given non-type
Douglas Gregor199d9912009-06-05 00:53:49 +0000612 // template parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000613 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000614 "Cannot deduce non-type template argument at depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000615 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson335e24a2009-06-16 22:44:31 +0000616 = dyn_cast<ConstantArrayType>(ArrayArg)) {
617 llvm::APSInt Size(ConstantArrayArg->getSize());
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000618 return DeduceNonTypeTemplateArgument(S, NTTP, Size,
619 S.Context.getSizeType(),
Douglas Gregor02024a92010-03-28 02:42:43 +0000620 /*ArrayBound=*/true,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000621 Info, Deduced);
Anders Carlsson335e24a2009-06-16 22:44:31 +0000622 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000623 if (const DependentSizedArrayType *DependentArrayArg
624 = dyn_cast<DependentSizedArrayType>(ArrayArg))
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000625 return DeduceNonTypeTemplateArgument(S, NTTP,
Douglas Gregor199d9912009-06-05 00:53:49 +0000626 DependentArrayArg->getSizeExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000627 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Douglas Gregor199d9912009-06-05 00:53:49 +0000629 // Incomplete type does not match a dependently-sized array type
Douglas Gregorf67875d2009-06-12 18:26:56 +0000630 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000631 }
Mike Stump1eb44332009-09-09 15:08:12 +0000632
633 // type(*)(T)
634 // T(*)()
635 // T(*)(T)
Anders Carlssona27fad52009-06-08 15:19:08 +0000636 case Type::FunctionProto: {
Mike Stump1eb44332009-09-09 15:08:12 +0000637 const FunctionProtoType *FunctionProtoArg =
Anders Carlssona27fad52009-06-08 15:19:08 +0000638 dyn_cast<FunctionProtoType>(Arg);
639 if (!FunctionProtoArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000640 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000641
642 const FunctionProtoType *FunctionProtoParam =
Anders Carlssona27fad52009-06-08 15:19:08 +0000643 cast<FunctionProtoType>(Param);
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000644
Mike Stump1eb44332009-09-09 15:08:12 +0000645 if (FunctionProtoParam->getTypeQuals() !=
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000646 FunctionProtoArg->getTypeQuals())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000647 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000649 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000650 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000652 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000653 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000654
Anders Carlssona27fad52009-06-08 15:19:08 +0000655 // Check return types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000656 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000657 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000658 FunctionProtoParam->getResultType(),
659 FunctionProtoArg->getResultType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000660 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000661 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Anders Carlssona27fad52009-06-08 15:19:08 +0000663 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
664 // Check argument types.
Douglas Gregor20a55e22010-12-22 18:17:10 +0000665 // FIXME: Variadic templates.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000666 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000667 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000668 FunctionProtoParam->getArgType(I),
669 FunctionProtoArg->getArgType(I),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000670 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000671 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000672 }
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Douglas Gregorf67875d2009-06-12 18:26:56 +0000674 return Sema::TDK_Success;
Anders Carlssona27fad52009-06-08 15:19:08 +0000675 }
Mike Stump1eb44332009-09-09 15:08:12 +0000676
John McCall3cb0ebd2010-03-10 03:28:59 +0000677 case Type::InjectedClassName: {
678 // Treat a template's injected-class-name as if the template
679 // specialization type had been used.
John McCall31f17ec2010-04-27 00:57:59 +0000680 Param = cast<InjectedClassNameType>(Param)
681 ->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +0000682 assert(isa<TemplateSpecializationType>(Param) &&
683 "injected class name is not a template specialization type");
684 // fall through
685 }
686
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000687 // template-name<T> (where template-name refers to a class template)
Douglas Gregord708c722009-06-09 16:35:58 +0000688 // template-name<i>
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000689 // TT<T>
690 // TT<i>
691 // TT<>
Douglas Gregord708c722009-06-09 16:35:58 +0000692 case Type::TemplateSpecialization: {
693 const TemplateSpecializationType *SpecParam
694 = cast<TemplateSpecializationType>(Param);
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000696 // Try to deduce template arguments from the template-id.
697 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000698 = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000699 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Douglas Gregor4a5c15f2009-09-30 22:13:51 +0000701 if (Result && (TDF & TDF_DerivedClass)) {
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000702 // C++ [temp.deduct.call]p3b3:
703 // If P is a class, and P has the form template-id, then A can be a
704 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump1eb44332009-09-09 15:08:12 +0000705 // class of the form template-id, A can be a pointer to a derived
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000706 // class pointed to by the deduced A.
707 //
708 // More importantly:
Mike Stump1eb44332009-09-09 15:08:12 +0000709 // These alternatives are considered only if type deduction would
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000710 // otherwise fail.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000711 if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
712 // We cannot inspect base classes as part of deduction when the type
713 // is incomplete, so either instantiate any templates necessary to
714 // complete the type, or skip over it if it cannot be completed.
John McCall5769d612010-02-08 23:07:23 +0000715 if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000716 return Result;
717
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000718 // Use data recursion to crawl through the list of base classes.
Mike Stump1eb44332009-09-09 15:08:12 +0000719 // Visited contains the set of nodes we have already visited, while
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000720 // ToVisit is our stack of records that we still need to visit.
721 llvm::SmallPtrSet<const RecordType *, 8> Visited;
722 llvm::SmallVector<const RecordType *, 8> ToVisit;
723 ToVisit.push_back(RecordT);
724 bool Successful = false;
Douglas Gregor053105d2010-11-02 00:02:34 +0000725 llvm::SmallVectorImpl<DeducedTemplateArgument> DeducedOrig(0);
726 DeducedOrig = Deduced;
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000727 while (!ToVisit.empty()) {
728 // Retrieve the next class in the inheritance hierarchy.
729 const RecordType *NextT = ToVisit.back();
730 ToVisit.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000732 // If we have already seen this type, skip it.
733 if (!Visited.insert(NextT))
734 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000736 // If this is a base class, try to perform template argument
737 // deduction from it.
738 if (NextT != RecordT) {
739 Sema::TemplateDeductionResult BaseResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000740 = DeduceTemplateArguments(S, TemplateParams, SpecParam,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000741 QualType(NextT, 0), Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000743 // If template argument deduction for this base was successful,
Douglas Gregor053105d2010-11-02 00:02:34 +0000744 // note that we had some success. Otherwise, ignore any deductions
745 // from this base class.
746 if (BaseResult == Sema::TDK_Success) {
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000747 Successful = true;
Douglas Gregor053105d2010-11-02 00:02:34 +0000748 DeducedOrig = Deduced;
749 }
750 else
751 Deduced = DeducedOrig;
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000752 }
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000754 // Visit base classes
755 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
756 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
757 BaseEnd = Next->bases_end();
Sebastian Redl9994a342009-10-25 17:03:50 +0000758 Base != BaseEnd; ++Base) {
Mike Stump1eb44332009-09-09 15:08:12 +0000759 assert(Base->getType()->isRecordType() &&
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000760 "Base class that isn't a record?");
Ted Kremenek6217b802009-07-29 21:53:49 +0000761 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000762 }
763 }
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000765 if (Successful)
766 return Sema::TDK_Success;
767 }
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000769 }
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000771 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000772 }
773
Douglas Gregor637a4092009-06-10 23:47:09 +0000774 // T type::*
775 // T T::*
776 // T (type::*)()
777 // type (T::*)()
778 // type (type::*)(T)
779 // type (T::*)(T)
780 // T (type::*)(T)
781 // T (T::*)()
782 // T (T::*)(T)
783 case Type::MemberPointer: {
784 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
785 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
786 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000787 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +0000788
Douglas Gregorf67875d2009-06-12 18:26:56 +0000789 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000790 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000791 MemPtrParam->getPointeeType(),
792 MemPtrArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000793 Info, Deduced,
794 TDF & TDF_IgnoreQualifiers))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000795 return Result;
796
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000797 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000798 QualType(MemPtrParam->getClass(), 0),
799 QualType(MemPtrArg->getClass(), 0),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000800 Info, Deduced, 0);
Douglas Gregor637a4092009-06-10 23:47:09 +0000801 }
802
Anders Carlsson9a917e42009-06-12 22:56:54 +0000803 // (clang extension)
804 //
Mike Stump1eb44332009-09-09 15:08:12 +0000805 // type(^)(T)
806 // T(^)()
807 // T(^)(T)
Anders Carlsson859ba502009-06-12 16:23:10 +0000808 case Type::BlockPointer: {
809 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
810 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump1eb44332009-09-09 15:08:12 +0000811
Anders Carlsson859ba502009-06-12 16:23:10 +0000812 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000813 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000815 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +0000816 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000817 BlockPtrArg->getPointeeType(), Info,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000818 Deduced, 0);
Anders Carlsson859ba502009-06-12 16:23:10 +0000819 }
820
Douglas Gregor637a4092009-06-10 23:47:09 +0000821 case Type::TypeOfExpr:
822 case Type::TypeOf:
Douglas Gregor4714c122010-03-31 17:34:00 +0000823 case Type::DependentName:
Douglas Gregor637a4092009-06-10 23:47:09 +0000824 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +0000825 return Sema::TDK_Success;
Douglas Gregor637a4092009-06-10 23:47:09 +0000826
Douglas Gregord560d502009-06-04 00:21:18 +0000827 default:
828 break;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000829 }
830
831 // FIXME: Many more cases to go (to go).
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000832 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000833}
834
Douglas Gregorf67875d2009-06-12 18:26:56 +0000835static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000836DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000837 TemplateParameterList *TemplateParams,
838 const TemplateArgument &Param,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000839 const TemplateArgument &Arg,
John McCall2a7fb272010-08-25 05:32:35 +0000840 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000841 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000842 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000843 case TemplateArgument::Null:
844 assert(false && "Null template argument in parameter list");
845 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000846
847 case TemplateArgument::Type:
Douglas Gregor788cd062009-11-11 01:00:40 +0000848 if (Arg.getKind() == TemplateArgument::Type)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000849 return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
Douglas Gregor788cd062009-11-11 01:00:40 +0000850 Arg.getAsType(), Info, Deduced, 0);
851 Info.FirstArg = Param;
852 Info.SecondArg = Arg;
853 return Sema::TDK_NonDeducedMismatch;
854
855 case TemplateArgument::Template:
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000856 if (Arg.getKind() == TemplateArgument::Template)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000857 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor788cd062009-11-11 01:00:40 +0000858 Param.getAsTemplate(),
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000859 Arg.getAsTemplate(), Info, Deduced);
Douglas Gregor788cd062009-11-11 01:00:40 +0000860 Info.FirstArg = Param;
861 Info.SecondArg = Arg;
862 return Sema::TDK_NonDeducedMismatch;
863
Douglas Gregor199d9912009-06-05 00:53:49 +0000864 case TemplateArgument::Declaration:
Douglas Gregor788cd062009-11-11 01:00:40 +0000865 if (Arg.getKind() == TemplateArgument::Declaration &&
866 Param.getAsDecl()->getCanonicalDecl() ==
867 Arg.getAsDecl()->getCanonicalDecl())
868 return Sema::TDK_Success;
869
Douglas Gregorf67875d2009-06-12 18:26:56 +0000870 Info.FirstArg = Param;
871 Info.SecondArg = Arg;
872 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Douglas Gregor199d9912009-06-05 00:53:49 +0000874 case TemplateArgument::Integral:
875 if (Arg.getKind() == TemplateArgument::Integral) {
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000876 if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral()))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000877 return Sema::TDK_Success;
878
879 Info.FirstArg = Param;
880 Info.SecondArg = Arg;
881 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000882 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000883
884 if (Arg.getKind() == TemplateArgument::Expression) {
885 Info.FirstArg = Param;
886 Info.SecondArg = Arg;
887 return Sema::TDK_NonDeducedMismatch;
888 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000889
Douglas Gregorf67875d2009-06-12 18:26:56 +0000890 Info.FirstArg = Param;
891 Info.SecondArg = Arg;
892 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Douglas Gregor199d9912009-06-05 00:53:49 +0000894 case TemplateArgument::Expression: {
Mike Stump1eb44332009-09-09 15:08:12 +0000895 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000896 = getDeducedParameterFromExpr(Param.getAsExpr())) {
897 if (Arg.getKind() == TemplateArgument::Integral)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000898 return DeduceNonTypeTemplateArgument(S, NTTP,
Mike Stump1eb44332009-09-09 15:08:12 +0000899 *Arg.getAsIntegral(),
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000900 Arg.getIntegralType(),
Douglas Gregor02024a92010-03-28 02:42:43 +0000901 /*ArrayBound=*/false,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000902 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000903 if (Arg.getKind() == TemplateArgument::Expression)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000904 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000905 Info, Deduced);
Douglas Gregor15755cb2009-11-13 23:45:44 +0000906 if (Arg.getKind() == TemplateArgument::Declaration)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000907 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
Douglas Gregor15755cb2009-11-13 23:45:44 +0000908 Info, Deduced);
909
Douglas Gregorf67875d2009-06-12 18:26:56 +0000910 Info.FirstArg = Param;
911 Info.SecondArg = Arg;
912 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000913 }
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Douglas Gregor199d9912009-06-05 00:53:49 +0000915 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000916 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000917 }
Anders Carlssond01b1da2009-06-15 17:04:53 +0000918 case TemplateArgument::Pack:
Douglas Gregor20a55e22010-12-22 18:17:10 +0000919 llvm_unreachable("Argument packs should be expanded by the caller!");
Douglas Gregor199d9912009-06-05 00:53:49 +0000920 }
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Douglas Gregorf67875d2009-06-12 18:26:56 +0000922 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000923}
924
Douglas Gregor20a55e22010-12-22 18:17:10 +0000925/// \brief Determine whether there is a template argument to be used for
926/// deduction.
927///
928/// This routine "expands" argument packs in-place, overriding its input
929/// parameters so that \c Args[ArgIdx] will be the available template argument.
930///
931/// \returns true if there is another template argument (which will be at
932/// \c Args[ArgIdx]), false otherwise.
933static bool hasTemplateArgumentForDeduction(const TemplateArgument *&Args,
934 unsigned &ArgIdx,
935 unsigned &NumArgs) {
936 if (ArgIdx == NumArgs)
937 return false;
938
939 const TemplateArgument &Arg = Args[ArgIdx];
940 if (Arg.getKind() != TemplateArgument::Pack)
941 return true;
942
943 assert(ArgIdx == NumArgs - 1 && "Pack not at the end of argument list?");
944 Args = Arg.pack_begin();
945 NumArgs = Arg.pack_size();
946 ArgIdx = 0;
947 return ArgIdx < NumArgs;
948}
949
950static Sema::TemplateDeductionResult
951DeduceTemplateArguments(Sema &S,
952 TemplateParameterList *TemplateParams,
953 const TemplateArgument *Params, unsigned NumParams,
954 const TemplateArgument *Args, unsigned NumArgs,
955 TemplateDeductionInfo &Info,
956 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
957 unsigned ArgIdx = 0, ParamIdx = 0;
958 for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams);
959 ++ParamIdx) {
960 if (!Params[ParamIdx].isPackExpansion()) {
961 // The simple case: deduce template arguments by matching P and A.
962
963 // Check whether we have enough arguments.
964 if (!hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
965 return Sema::TDK_TooFewArguments;
966
967 // Perform deduction for this P/A pair.
968 if (Sema::TemplateDeductionResult Result
969 = DeduceTemplateArguments(S, TemplateParams,
970 Params[ParamIdx], Args[ArgIdx],
971 Info, Deduced))
972 return Result;
973
974 // Move to the next argument.
975 ++ArgIdx;
976 continue;
977 }
978
979 // FIXME: Variadic templates.
980 // The parameter is a pack expansion, so we'll
981 // need to repeatedly unify arguments against the parameter, capturing
982 // the bindings for each expanded parameter pack.
983 S.Diag(Info.getLocation(), diag::err_pack_expansion_deduction);
984 return Sema::TDK_TooManyArguments;
985 }
986
987 // If there is an argument remaining, then we had too many arguments.
988 if (hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
989 return Sema::TDK_TooManyArguments;
990
991 return Sema::TDK_Success;
992}
993
Mike Stump1eb44332009-09-09 15:08:12 +0000994static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000995DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000996 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000997 const TemplateArgumentList &ParamList,
998 const TemplateArgumentList &ArgList,
John McCall2a7fb272010-08-25 05:32:35 +0000999 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +00001000 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor20a55e22010-12-22 18:17:10 +00001001 return DeduceTemplateArguments(S, TemplateParams,
1002 ParamList.data(), ParamList.size(),
1003 ArgList.data(), ArgList.size(),
1004 Info, Deduced);
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001005}
1006
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001007/// \brief Determine whether two template arguments are the same.
Mike Stump1eb44332009-09-09 15:08:12 +00001008static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001009 const TemplateArgument &X,
1010 const TemplateArgument &Y) {
1011 if (X.getKind() != Y.getKind())
1012 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001014 switch (X.getKind()) {
1015 case TemplateArgument::Null:
1016 assert(false && "Comparing NULL template argument");
1017 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001019 case TemplateArgument::Type:
1020 return Context.getCanonicalType(X.getAsType()) ==
1021 Context.getCanonicalType(Y.getAsType());
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001023 case TemplateArgument::Declaration:
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001024 return X.getAsDecl()->getCanonicalDecl() ==
1025 Y.getAsDecl()->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Douglas Gregor788cd062009-11-11 01:00:40 +00001027 case TemplateArgument::Template:
1028 return Context.getCanonicalTemplateName(X.getAsTemplate())
1029 .getAsVoidPointer() ==
1030 Context.getCanonicalTemplateName(Y.getAsTemplate())
1031 .getAsVoidPointer();
1032
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001033 case TemplateArgument::Integral:
1034 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Douglas Gregor788cd062009-11-11 01:00:40 +00001036 case TemplateArgument::Expression: {
1037 llvm::FoldingSetNodeID XID, YID;
1038 X.getAsExpr()->Profile(XID, Context, true);
1039 Y.getAsExpr()->Profile(YID, Context, true);
1040 return XID == YID;
1041 }
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001043 case TemplateArgument::Pack:
1044 if (X.pack_size() != Y.pack_size())
1045 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001046
1047 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
1048 XPEnd = X.pack_end(),
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001049 YP = Y.pack_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001050 XP != XPEnd; ++XP, ++YP)
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001051 if (!isSameTemplateArg(Context, *XP, *YP))
1052 return false;
1053
1054 return true;
1055 }
1056
1057 return false;
1058}
1059
1060/// \brief Helper function to build a TemplateParameter when we don't
1061/// know its type statically.
1062static TemplateParameter makeTemplateParameter(Decl *D) {
1063 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
1064 return TemplateParameter(TTP);
1065 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
1066 return TemplateParameter(NTTP);
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001068 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
1069}
1070
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001071/// Complete template argument deduction for a class template partial
1072/// specialization.
1073static Sema::TemplateDeductionResult
1074FinishTemplateArgumentDeduction(Sema &S,
1075 ClassTemplatePartialSpecializationDecl *Partial,
1076 const TemplateArgumentList &TemplateArgs,
1077 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
John McCall2a7fb272010-08-25 05:32:35 +00001078 TemplateDeductionInfo &Info) {
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001079 // Trap errors.
1080 Sema::SFINAETrap Trap(S);
1081
1082 Sema::ContextRAII SavedContext(S, Partial);
1083
1084 // C++ [temp.deduct.type]p2:
1085 // [...] or if any template argument remains neither deduced nor
1086 // explicitly specified, template argument deduction fails.
Douglas Gregor910f8002010-11-07 23:05:16 +00001087 llvm::SmallVector<TemplateArgument, 4> Builder;
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001088 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
1089 if (Deduced[I].isNull()) {
1090 Decl *Param
Douglas Gregor3273b0c2010-10-12 18:51:08 +00001091 = const_cast<NamedDecl *>(
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001092 Partial->getTemplateParameters()->getParam(I));
1093 Info.Param = makeTemplateParameter(Param);
1094 return Sema::TDK_Incomplete;
1095 }
1096
Douglas Gregor910f8002010-11-07 23:05:16 +00001097 Builder.push_back(Deduced[I]);
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001098 }
1099
1100 // Form the template argument list from the deduced template arguments.
1101 TemplateArgumentList *DeducedArgumentList
Douglas Gregor910f8002010-11-07 23:05:16 +00001102 = TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
1103 Builder.size());
1104
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001105 Info.reset(DeducedArgumentList);
1106
1107 // Substitute the deduced template arguments into the template
1108 // arguments of the class template partial specialization, and
1109 // verify that the instantiated template arguments are both valid
1110 // and are equivalent to the template arguments originally provided
1111 // to the class template.
1112 // FIXME: Do we have to correct the types of deduced non-type template
1113 // arguments (in particular, integral non-type template arguments?).
John McCall2a7fb272010-08-25 05:32:35 +00001114 LocalInstantiationScope InstScope(S);
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001115 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
1116 const TemplateArgumentLoc *PartialTemplateArgs
1117 = Partial->getTemplateArgsAsWritten();
1118 unsigned N = Partial->getNumTemplateArgsAsWritten();
1119
1120 // Note that we don't provide the langle and rangle locations.
1121 TemplateArgumentListInfo InstArgs;
1122
1123 for (unsigned I = 0; I != N; ++I) {
1124 Decl *Param = const_cast<NamedDecl *>(
1125 ClassTemplate->getTemplateParameters()->getParam(I));
1126 TemplateArgumentLoc InstArg;
1127 if (S.Subst(PartialTemplateArgs[I], InstArg,
1128 MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
1129 Info.Param = makeTemplateParameter(Param);
1130 Info.FirstArg = PartialTemplateArgs[I].getArgument();
1131 return Sema::TDK_SubstitutionFailure;
1132 }
1133 InstArgs.addArgument(InstArg);
1134 }
1135
Douglas Gregor910f8002010-11-07 23:05:16 +00001136 llvm::SmallVector<TemplateArgument, 4> ConvertedInstArgs;
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001137 if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
Douglas Gregorec20f462010-05-08 20:07:26 +00001138 InstArgs, false, ConvertedInstArgs))
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001139 return Sema::TDK_SubstitutionFailure;
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001140
Douglas Gregor910f8002010-11-07 23:05:16 +00001141 for (unsigned I = 0, E = ConvertedInstArgs.size(); I != E; ++I) {
1142 TemplateArgument InstArg = ConvertedInstArgs.data()[I];
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001143
1144 Decl *Param = const_cast<NamedDecl *>(
1145 ClassTemplate->getTemplateParameters()->getParam(I));
1146
1147 if (InstArg.getKind() == TemplateArgument::Expression) {
1148 // When the argument is an expression, check the expression result
1149 // against the actual template parameter to get down to the canonical
1150 // template argument.
Douglas Gregor20a55e22010-12-22 18:17:10 +00001151 // FIXME: Variadic templates.
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001152 Expr *InstExpr = InstArg.getAsExpr();
1153 if (NonTypeTemplateParmDecl *NTTP
1154 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1155 if (S.CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
1156 Info.Param = makeTemplateParameter(Param);
1157 Info.FirstArg = Partial->getTemplateArgs()[I];
1158 return Sema::TDK_SubstitutionFailure;
1159 }
1160 }
1161 }
1162
1163 if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
1164 Info.Param = makeTemplateParameter(Param);
1165 Info.FirstArg = TemplateArgs[I];
1166 Info.SecondArg = InstArg;
1167 return Sema::TDK_NonDeducedMismatch;
1168 }
1169 }
1170
1171 if (Trap.hasErrorOccurred())
1172 return Sema::TDK_SubstitutionFailure;
1173
1174 return Sema::TDK_Success;
1175}
1176
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001177/// \brief Perform template argument deduction to determine whether
1178/// the given template arguments match the given class template
1179/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +00001180Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001181Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001182 const TemplateArgumentList &TemplateArgs,
1183 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001184 // C++ [temp.class.spec.match]p2:
1185 // A partial specialization matches a given actual template
1186 // argument list if the template arguments of the partial
1187 // specialization can be deduced from the actual template argument
1188 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +00001189 SFINAETrap Trap(*this);
Douglas Gregor02024a92010-03-28 02:42:43 +00001190 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001191 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +00001192 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001193 = ::DeduceTemplateArguments(*this,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001194 Partial->getTemplateParameters(),
Mike Stump1eb44332009-09-09 15:08:12 +00001195 Partial->getTemplateArgs(),
Douglas Gregorf67875d2009-06-12 18:26:56 +00001196 TemplateArgs, Info, Deduced))
1197 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +00001198
Douglas Gregor637a4092009-06-10 23:47:09 +00001199 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
Douglas Gregor9b623632010-10-12 23:32:35 +00001200 Deduced.data(), Deduced.size(), Info);
Douglas Gregor637a4092009-06-10 23:47:09 +00001201 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001202 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +00001203
Douglas Gregorbb260412009-06-14 08:02:22 +00001204 if (Trap.hasErrorOccurred())
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001205 return Sema::TDK_SubstitutionFailure;
1206
1207 return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
1208 Deduced, Info);
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001209}
Douglas Gregor031a5882009-06-13 00:26:55 +00001210
Douglas Gregor41128772009-06-26 23:27:24 +00001211/// \brief Determine whether the given type T is a simple-template-id type.
1212static bool isSimpleTemplateIdType(QualType T) {
Mike Stump1eb44332009-09-09 15:08:12 +00001213 if (const TemplateSpecializationType *Spec
John McCall183700f2009-09-21 23:43:11 +00001214 = T->getAs<TemplateSpecializationType>())
Douglas Gregor41128772009-06-26 23:27:24 +00001215 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001216
Douglas Gregor41128772009-06-26 23:27:24 +00001217 return false;
1218}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001219
1220/// \brief Substitute the explicitly-provided template arguments into the
1221/// given function template according to C++ [temp.arg.explicit].
1222///
1223/// \param FunctionTemplate the function template into which the explicit
1224/// template arguments will be substituted.
1225///
Mike Stump1eb44332009-09-09 15:08:12 +00001226/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001227/// arguments.
1228///
Mike Stump1eb44332009-09-09 15:08:12 +00001229/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor83314aa2009-07-08 20:55:45 +00001230/// with the converted and checked explicit template arguments.
1231///
Mike Stump1eb44332009-09-09 15:08:12 +00001232/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor83314aa2009-07-08 20:55:45 +00001233/// parameters.
1234///
1235/// \param FunctionType if non-NULL, the result type of the function template
1236/// will also be instantiated and the pointed-to value will be updated with
1237/// the instantiated function type.
1238///
1239/// \param Info if substitution fails for any reason, this object will be
1240/// populated with more information about the failure.
1241///
1242/// \returns TDK_Success if substitution was successful, or some failure
1243/// condition.
1244Sema::TemplateDeductionResult
1245Sema::SubstituteExplicitTemplateArguments(
1246 FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001247 const TemplateArgumentListInfo &ExplicitTemplateArgs,
Douglas Gregor02024a92010-03-28 02:42:43 +00001248 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001249 llvm::SmallVectorImpl<QualType> &ParamTypes,
1250 QualType *FunctionType,
1251 TemplateDeductionInfo &Info) {
1252 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1253 TemplateParameterList *TemplateParams
1254 = FunctionTemplate->getTemplateParameters();
1255
John McCalld5532b62009-11-23 01:53:49 +00001256 if (ExplicitTemplateArgs.size() == 0) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001257 // No arguments to substitute; just copy over the parameter types and
1258 // fill in the function type.
1259 for (FunctionDecl::param_iterator P = Function->param_begin(),
1260 PEnd = Function->param_end();
1261 P != PEnd;
1262 ++P)
1263 ParamTypes.push_back((*P)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001264
Douglas Gregor83314aa2009-07-08 20:55:45 +00001265 if (FunctionType)
1266 *FunctionType = Function->getType();
1267 return TDK_Success;
1268 }
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Douglas Gregor83314aa2009-07-08 20:55:45 +00001270 // Substitution of the explicit template arguments into a function template
1271 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001272 SFINAETrap Trap(*this);
1273
Douglas Gregor83314aa2009-07-08 20:55:45 +00001274 // C++ [temp.arg.explicit]p3:
Mike Stump1eb44332009-09-09 15:08:12 +00001275 // Template arguments that are present shall be specified in the
1276 // declaration order of their corresponding template-parameters. The
Douglas Gregor83314aa2009-07-08 20:55:45 +00001277 // template argument list shall not specify more template-arguments than
Mike Stump1eb44332009-09-09 15:08:12 +00001278 // there are corresponding template-parameters.
Douglas Gregor910f8002010-11-07 23:05:16 +00001279 llvm::SmallVector<TemplateArgument, 4> Builder;
Mike Stump1eb44332009-09-09 15:08:12 +00001280
1281 // Enter a new template instantiation context where we check the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001282 // explicitly-specified template arguments against this function template,
1283 // and then substitute them into the function parameter types.
Mike Stump1eb44332009-09-09 15:08:12 +00001284 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001285 FunctionTemplate, Deduced.data(), Deduced.size(),
Douglas Gregor9b623632010-10-12 23:32:35 +00001286 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution,
1287 Info);
Douglas Gregor83314aa2009-07-08 20:55:45 +00001288 if (Inst)
1289 return TDK_InstantiationDepth;
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Douglas Gregor83314aa2009-07-08 20:55:45 +00001291 if (CheckTemplateArgumentList(FunctionTemplate,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001292 SourceLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001293 ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001294 true,
Douglas Gregorf1a84452010-05-08 19:15:54 +00001295 Builder) || Trap.hasErrorOccurred()) {
Douglas Gregor910f8002010-11-07 23:05:16 +00001296 unsigned Index = Builder.size();
Douglas Gregorfe52c912010-05-09 01:26:06 +00001297 if (Index >= TemplateParams->size())
1298 Index = TemplateParams->size() - 1;
1299 Info.Param = makeTemplateParameter(TemplateParams->getParam(Index));
Douglas Gregor83314aa2009-07-08 20:55:45 +00001300 return TDK_InvalidExplicitArguments;
Douglas Gregorf1a84452010-05-08 19:15:54 +00001301 }
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Douglas Gregor83314aa2009-07-08 20:55:45 +00001303 // Form the template argument list from the explicitly-specified
1304 // template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001305 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor910f8002010-11-07 23:05:16 +00001306 = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001307 Info.reset(ExplicitArgumentList);
Mike Stump1eb44332009-09-09 15:08:12 +00001308
John McCalldf41f182010-10-12 19:40:14 +00001309 // Template argument deduction and the final substitution should be
1310 // done in the context of the templated declaration. Explicit
1311 // argument substitution, on the other hand, needs to happen in the
1312 // calling context.
1313 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
1314
Douglas Gregor83314aa2009-07-08 20:55:45 +00001315 // Instantiate the types of each of the function parameters given the
1316 // explicitly-specified template arguments.
1317 for (FunctionDecl::param_iterator P = Function->param_begin(),
1318 PEnd = Function->param_end();
1319 P != PEnd;
1320 ++P) {
Mike Stump1eb44332009-09-09 15:08:12 +00001321 QualType ParamType
1322 = SubstType((*P)->getType(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001323 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1324 (*P)->getLocation(), (*P)->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001325 if (ParamType.isNull() || Trap.hasErrorOccurred())
1326 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Douglas Gregor83314aa2009-07-08 20:55:45 +00001328 ParamTypes.push_back(ParamType);
1329 }
1330
1331 // If the caller wants a full function type back, instantiate the return
1332 // type and form that function type.
1333 if (FunctionType) {
1334 // FIXME: exception-specifications?
Mike Stump1eb44332009-09-09 15:08:12 +00001335 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001336 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001337 assert(Proto && "Function template does not have a prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001338
1339 QualType ResultType
Douglas Gregor357bbd02009-08-28 20:50:45 +00001340 = SubstType(Proto->getResultType(),
1341 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1342 Function->getTypeSpecStartLoc(),
1343 Function->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001344 if (ResultType.isNull() || Trap.hasErrorOccurred())
1345 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001346
1347 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001348 ParamTypes.data(), ParamTypes.size(),
1349 Proto->isVariadic(),
1350 Proto->getTypeQuals(),
1351 Function->getLocation(),
Eli Friedmanfa869542010-08-05 02:54:05 +00001352 Function->getDeclName(),
1353 Proto->getExtInfo());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001354 if (FunctionType->isNull() || Trap.hasErrorOccurred())
1355 return TDK_SubstitutionFailure;
1356 }
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Douglas Gregor83314aa2009-07-08 20:55:45 +00001358 // C++ [temp.arg.explicit]p2:
Mike Stump1eb44332009-09-09 15:08:12 +00001359 // Trailing template arguments that can be deduced (14.8.2) may be
1360 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001361 // template arguments can be deduced, they may all be omitted; in this
1362 // case, the empty template argument list <> itself may also be omitted.
1363 //
1364 // Take all of the explicitly-specified arguments and put them into the
Mike Stump1eb44332009-09-09 15:08:12 +00001365 // set of deduced template arguments.
Douglas Gregor20a55e22010-12-22 18:17:10 +00001366 //
1367 // FIXME: Variadic templates?
Douglas Gregor83314aa2009-07-08 20:55:45 +00001368 Deduced.reserve(TemplateParams->size());
1369 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001370 Deduced.push_back(ExplicitArgumentList->get(I));
1371
Douglas Gregor83314aa2009-07-08 20:55:45 +00001372 return TDK_Success;
1373}
1374
Douglas Gregor02024a92010-03-28 02:42:43 +00001375/// \brief Allocate a TemplateArgumentLoc where all locations have
1376/// been initialized to the given location.
1377///
1378/// \param S The semantic analysis object.
1379///
1380/// \param The template argument we are producing template argument
1381/// location information for.
1382///
1383/// \param NTTPType For a declaration template argument, the type of
1384/// the non-type template parameter that corresponds to this template
1385/// argument.
1386///
1387/// \param Loc The source location to use for the resulting template
1388/// argument.
1389static TemplateArgumentLoc
1390getTrivialTemplateArgumentLoc(Sema &S,
1391 const TemplateArgument &Arg,
1392 QualType NTTPType,
1393 SourceLocation Loc) {
1394 switch (Arg.getKind()) {
1395 case TemplateArgument::Null:
1396 llvm_unreachable("Can't get a NULL template argument here");
1397 break;
1398
1399 case TemplateArgument::Type:
1400 return TemplateArgumentLoc(Arg,
1401 S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
1402
1403 case TemplateArgument::Declaration: {
1404 Expr *E
1405 = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
1406 .takeAs<Expr>();
1407 return TemplateArgumentLoc(TemplateArgument(E), E);
1408 }
1409
1410 case TemplateArgument::Integral: {
1411 Expr *E
1412 = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>();
1413 return TemplateArgumentLoc(TemplateArgument(E), E);
1414 }
1415
1416 case TemplateArgument::Template:
1417 return TemplateArgumentLoc(Arg, SourceRange(), Loc);
1418
1419 case TemplateArgument::Expression:
1420 return TemplateArgumentLoc(Arg, Arg.getAsExpr());
1421
1422 case TemplateArgument::Pack:
Douglas Gregor87dd6972010-12-20 16:52:59 +00001423 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo());
Douglas Gregor02024a92010-03-28 02:42:43 +00001424 }
1425
1426 return TemplateArgumentLoc();
1427}
1428
Mike Stump1eb44332009-09-09 15:08:12 +00001429/// \brief Finish template argument deduction for a function template,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001430/// checking the deduced template arguments for completeness and forming
1431/// the function template specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00001432Sema::TemplateDeductionResult
Douglas Gregor83314aa2009-07-08 20:55:45 +00001433Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor02024a92010-03-28 02:42:43 +00001434 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1435 unsigned NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001436 FunctionDecl *&Specialization,
1437 TemplateDeductionInfo &Info) {
1438 TemplateParameterList *TemplateParams
1439 = FunctionTemplate->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Douglas Gregor83314aa2009-07-08 20:55:45 +00001441 // Template argument deduction for function templates in a SFINAE context.
1442 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001443 SFINAETrap Trap(*this);
1444
Douglas Gregor83314aa2009-07-08 20:55:45 +00001445 // Enter a new template instantiation context while we instantiate the
1446 // actual function declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001447 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001448 FunctionTemplate, Deduced.data(), Deduced.size(),
Douglas Gregor9b623632010-10-12 23:32:35 +00001449 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution,
1450 Info);
Douglas Gregor83314aa2009-07-08 20:55:45 +00001451 if (Inst)
Mike Stump1eb44332009-09-09 15:08:12 +00001452 return TDK_InstantiationDepth;
1453
John McCall96db3102010-04-29 01:18:58 +00001454 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
John McCallf5813822010-04-29 00:35:03 +00001455
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001456 // C++ [temp.deduct.type]p2:
1457 // [...] or if any template argument remains neither deduced nor
1458 // explicitly specified, template argument deduction fails.
Douglas Gregor910f8002010-11-07 23:05:16 +00001459 llvm::SmallVector<TemplateArgument, 4> Builder;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001460 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregor20a55e22010-12-22 18:17:10 +00001461 // FIXME: Variadic templates. Unwrap argument packs?
Douglas Gregor02024a92010-03-28 02:42:43 +00001462 NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001463 if (!Deduced[I].isNull()) {
Douglas Gregor3273b0c2010-10-12 18:51:08 +00001464 if (I < NumExplicitlySpecified) {
Douglas Gregor02024a92010-03-28 02:42:43 +00001465 // We have already fully type-checked and converted this
Douglas Gregor3273b0c2010-10-12 18:51:08 +00001466 // argument, because it was explicitly-specified. Just record the
1467 // presence of this argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00001468 Builder.push_back(Deduced[I]);
Douglas Gregor02024a92010-03-28 02:42:43 +00001469 continue;
1470 }
1471
1472 // We have deduced this argument, so it still needs to be
1473 // checked and converted.
1474
1475 // First, for a non-type template parameter type that is
1476 // initialized by a declaration, we need the type of the
1477 // corresponding non-type template parameter.
1478 QualType NTTPType;
1479 if (NonTypeTemplateParmDecl *NTTP
1480 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1481 if (Deduced[I].getKind() == TemplateArgument::Declaration) {
1482 NTTPType = NTTP->getType();
1483 if (NTTPType->isDependentType()) {
Douglas Gregor910f8002010-11-07 23:05:16 +00001484 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1485 Builder.data(), Builder.size());
Douglas Gregor02024a92010-03-28 02:42:43 +00001486 NTTPType = SubstType(NTTPType,
1487 MultiLevelTemplateArgumentList(TemplateArgs),
1488 NTTP->getLocation(),
1489 NTTP->getDeclName());
1490 if (NTTPType.isNull()) {
1491 Info.Param = makeTemplateParameter(Param);
Douglas Gregor910f8002010-11-07 23:05:16 +00001492 // FIXME: These template arguments are temporary. Free them!
1493 Info.reset(TemplateArgumentList::CreateCopy(Context,
1494 Builder.data(),
1495 Builder.size()));
Douglas Gregor02024a92010-03-28 02:42:43 +00001496 return TDK_SubstitutionFailure;
1497 }
1498 }
1499 }
1500 }
1501
1502 // Convert the deduced template argument into a template
1503 // argument that we can check, almost as if the user had written
1504 // the template argument explicitly.
1505 TemplateArgumentLoc Arg = getTrivialTemplateArgumentLoc(*this,
1506 Deduced[I],
1507 NTTPType,
Douglas Gregor9b623632010-10-12 23:32:35 +00001508 Info.getLocation());
Douglas Gregor02024a92010-03-28 02:42:43 +00001509
1510 // Check the template argument, converting it as necessary.
1511 if (CheckTemplateArgument(Param, Arg,
1512 FunctionTemplate,
1513 FunctionTemplate->getLocation(),
1514 FunctionTemplate->getSourceRange().getEnd(),
1515 Builder,
1516 Deduced[I].wasDeducedFromArrayBound()
1517 ? CTAK_DeducedFromArrayBound
1518 : CTAK_Deduced)) {
1519 Info.Param = makeTemplateParameter(
1520 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregor910f8002010-11-07 23:05:16 +00001521 // FIXME: These template arguments are temporary. Free them!
1522 Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
1523 Builder.size()));
Douglas Gregor02024a92010-03-28 02:42:43 +00001524 return TDK_SubstitutionFailure;
1525 }
1526
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001527 continue;
1528 }
1529
1530 // Substitute into the default template argument, if available.
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001531 TemplateArgumentLoc DefArg
1532 = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
1533 FunctionTemplate->getLocation(),
1534 FunctionTemplate->getSourceRange().getEnd(),
1535 Param,
1536 Builder);
1537
1538 // If there was no default argument, deduction is incomplete.
1539 if (DefArg.getArgument().isNull()) {
1540 Info.Param = makeTemplateParameter(
1541 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1542 return TDK_Incomplete;
1543 }
1544
1545 // Check whether we can actually use the default argument.
1546 if (CheckTemplateArgument(Param, DefArg,
1547 FunctionTemplate,
1548 FunctionTemplate->getLocation(),
1549 FunctionTemplate->getSourceRange().getEnd(),
Douglas Gregor02024a92010-03-28 02:42:43 +00001550 Builder,
1551 CTAK_Deduced)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001552 Info.Param = makeTemplateParameter(
1553 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregor910f8002010-11-07 23:05:16 +00001554 // FIXME: These template arguments are temporary. Free them!
1555 Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
1556 Builder.size()));
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001557 return TDK_SubstitutionFailure;
1558 }
1559
1560 // If we get here, we successfully used the default template argument.
1561 }
1562
1563 // Form the template argument list from the deduced template arguments.
1564 TemplateArgumentList *DeducedArgumentList
Douglas Gregor910f8002010-11-07 23:05:16 +00001565 = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001566 Info.reset(DeducedArgumentList);
1567
Mike Stump1eb44332009-09-09 15:08:12 +00001568 // Substitute the deduced template arguments into the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001569 // declaration to produce the function template specialization.
Douglas Gregord4598a22010-04-28 04:52:24 +00001570 DeclContext *Owner = FunctionTemplate->getDeclContext();
1571 if (FunctionTemplate->getFriendObjectKind())
1572 Owner = FunctionTemplate->getLexicalDeclContext();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001573 Specialization = cast_or_null<FunctionDecl>(
Douglas Gregord4598a22010-04-28 04:52:24 +00001574 SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner,
Douglas Gregor357bbd02009-08-28 20:50:45 +00001575 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor83314aa2009-07-08 20:55:45 +00001576 if (!Specialization)
1577 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Douglas Gregorf8825742009-09-15 18:26:13 +00001579 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
1580 FunctionTemplate->getCanonicalDecl());
1581
Mike Stump1eb44332009-09-09 15:08:12 +00001582 // If the template argument list is owned by the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001583 // specialization, release it.
Douglas Gregorec20f462010-05-08 20:07:26 +00001584 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList &&
1585 !Trap.hasErrorOccurred())
Douglas Gregor83314aa2009-07-08 20:55:45 +00001586 Info.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001587
Douglas Gregor83314aa2009-07-08 20:55:45 +00001588 // There may have been an error that did not prevent us from constructing a
1589 // declaration. Mark the declaration invalid and return with a substitution
1590 // failure.
1591 if (Trap.hasErrorOccurred()) {
1592 Specialization->setInvalidDecl(true);
1593 return TDK_SubstitutionFailure;
1594 }
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Douglas Gregor9b623632010-10-12 23:32:35 +00001596 // If we suppressed any diagnostics while performing template argument
1597 // deduction, and if we haven't already instantiated this declaration,
1598 // keep track of these diagnostics. They'll be emitted if this specialization
1599 // is actually used.
1600 if (Info.diag_begin() != Info.diag_end()) {
1601 llvm::DenseMap<Decl *, llvm::SmallVector<PartialDiagnosticAt, 1> >::iterator
1602 Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl());
1603 if (Pos == SuppressedDiagnostics.end())
1604 SuppressedDiagnostics[Specialization->getCanonicalDecl()]
1605 .append(Info.diag_begin(), Info.diag_end());
1606 }
1607
Mike Stump1eb44332009-09-09 15:08:12 +00001608 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001609}
1610
John McCall9c72c602010-08-27 09:08:28 +00001611/// Gets the type of a function for template-argument-deducton
1612/// purposes when it's considered as part of an overload set.
John McCalleff92132010-02-02 02:21:27 +00001613static QualType GetTypeOfFunction(ASTContext &Context,
John McCall9c72c602010-08-27 09:08:28 +00001614 const OverloadExpr::FindResult &R,
John McCalleff92132010-02-02 02:21:27 +00001615 FunctionDecl *Fn) {
John McCalleff92132010-02-02 02:21:27 +00001616 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
John McCall9c72c602010-08-27 09:08:28 +00001617 if (Method->isInstance()) {
1618 // An instance method that's referenced in a form that doesn't
1619 // look like a member pointer is just invalid.
1620 if (!R.HasFormOfMemberPointer) return QualType();
1621
John McCalleff92132010-02-02 02:21:27 +00001622 return Context.getMemberPointerType(Fn->getType(),
1623 Context.getTypeDeclType(Method->getParent()).getTypePtr());
John McCall9c72c602010-08-27 09:08:28 +00001624 }
1625
1626 if (!R.IsAddressOfOperand) return Fn->getType();
John McCalleff92132010-02-02 02:21:27 +00001627 return Context.getPointerType(Fn->getType());
1628}
1629
1630/// Apply the deduction rules for overload sets.
1631///
1632/// \return the null type if this argument should be treated as an
1633/// undeduced context
1634static QualType
1635ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
Douglas Gregor75f21af2010-08-30 21:04:23 +00001636 Expr *Arg, QualType ParamType,
1637 bool ParamWasReference) {
John McCall9c72c602010-08-27 09:08:28 +00001638
1639 OverloadExpr::FindResult R = OverloadExpr::find(Arg);
John McCalleff92132010-02-02 02:21:27 +00001640
John McCall9c72c602010-08-27 09:08:28 +00001641 OverloadExpr *Ovl = R.Expression;
John McCalleff92132010-02-02 02:21:27 +00001642
Douglas Gregor75f21af2010-08-30 21:04:23 +00001643 // C++0x [temp.deduct.call]p4
1644 unsigned TDF = 0;
1645 if (ParamWasReference)
1646 TDF |= TDF_ParamWithReferenceType;
1647 if (R.IsAddressOfOperand)
1648 TDF |= TDF_IgnoreQualifiers;
1649
John McCalleff92132010-02-02 02:21:27 +00001650 // If there were explicit template arguments, we can only find
1651 // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
1652 // unambiguously name a full specialization.
John McCall7bb12da2010-02-02 06:20:04 +00001653 if (Ovl->hasExplicitTemplateArgs()) {
John McCalleff92132010-02-02 02:21:27 +00001654 // But we can still look for an explicit specialization.
1655 if (FunctionDecl *ExplicitSpec
John McCall7bb12da2010-02-02 06:20:04 +00001656 = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
John McCall9c72c602010-08-27 09:08:28 +00001657 return GetTypeOfFunction(S.Context, R, ExplicitSpec);
John McCalleff92132010-02-02 02:21:27 +00001658 return QualType();
1659 }
1660
1661 // C++0x [temp.deduct.call]p6:
1662 // When P is a function type, pointer to function type, or pointer
1663 // to member function type:
1664
1665 if (!ParamType->isFunctionType() &&
1666 !ParamType->isFunctionPointerType() &&
1667 !ParamType->isMemberFunctionPointerType())
1668 return QualType();
1669
1670 QualType Match;
John McCall7bb12da2010-02-02 06:20:04 +00001671 for (UnresolvedSetIterator I = Ovl->decls_begin(),
1672 E = Ovl->decls_end(); I != E; ++I) {
John McCalleff92132010-02-02 02:21:27 +00001673 NamedDecl *D = (*I)->getUnderlyingDecl();
1674
1675 // - If the argument is an overload set containing one or more
1676 // function templates, the parameter is treated as a
1677 // non-deduced context.
1678 if (isa<FunctionTemplateDecl>(D))
1679 return QualType();
1680
1681 FunctionDecl *Fn = cast<FunctionDecl>(D);
John McCall9c72c602010-08-27 09:08:28 +00001682 QualType ArgType = GetTypeOfFunction(S.Context, R, Fn);
1683 if (ArgType.isNull()) continue;
John McCalleff92132010-02-02 02:21:27 +00001684
Douglas Gregor75f21af2010-08-30 21:04:23 +00001685 // Function-to-pointer conversion.
1686 if (!ParamWasReference && ParamType->isPointerType() &&
1687 ArgType->isFunctionType())
1688 ArgType = S.Context.getPointerType(ArgType);
1689
John McCalleff92132010-02-02 02:21:27 +00001690 // - If the argument is an overload set (not containing function
1691 // templates), trial argument deduction is attempted using each
1692 // of the members of the set. If deduction succeeds for only one
1693 // of the overload set members, that member is used as the
1694 // argument value for the deduction. If deduction succeeds for
1695 // more than one member of the overload set the parameter is
1696 // treated as a non-deduced context.
1697
1698 // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
1699 // Type deduction is done independently for each P/A pair, and
1700 // the deduced template argument values are then combined.
1701 // So we do not reject deductions which were made elsewhere.
Douglas Gregor02024a92010-03-28 02:42:43 +00001702 llvm::SmallVector<DeducedTemplateArgument, 8>
1703 Deduced(TemplateParams->size());
John McCall2a7fb272010-08-25 05:32:35 +00001704 TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
John McCalleff92132010-02-02 02:21:27 +00001705 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001706 = DeduceTemplateArguments(S, TemplateParams,
John McCalleff92132010-02-02 02:21:27 +00001707 ParamType, ArgType,
1708 Info, Deduced, TDF);
1709 if (Result) continue;
1710 if (!Match.isNull()) return QualType();
1711 Match = ArgType;
1712 }
1713
1714 return Match;
1715}
1716
Douglas Gregore53060f2009-06-25 22:08:12 +00001717/// \brief Perform template argument deduction from a function call
1718/// (C++ [temp.deduct.call]).
1719///
1720/// \param FunctionTemplate the function template for which we are performing
1721/// template argument deduction.
1722///
Douglas Gregor48026d22010-01-11 18:40:55 +00001723/// \param ExplicitTemplateArguments the explicit template arguments provided
1724/// for this call.
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001725///
Douglas Gregore53060f2009-06-25 22:08:12 +00001726/// \param Args the function call arguments
1727///
1728/// \param NumArgs the number of arguments in Args
1729///
Douglas Gregor48026d22010-01-11 18:40:55 +00001730/// \param Name the name of the function being called. This is only significant
1731/// when the function template is a conversion function template, in which
1732/// case this routine will also perform template argument deduction based on
1733/// the function to which
1734///
Douglas Gregore53060f2009-06-25 22:08:12 +00001735/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001736/// this will be set to the function template specialization produced by
Douglas Gregore53060f2009-06-25 22:08:12 +00001737/// template argument deduction.
1738///
1739/// \param Info the argument will be updated to provide additional information
1740/// about template argument deduction.
1741///
1742/// \returns the result of template argument deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001743Sema::TemplateDeductionResult
1744Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor48026d22010-01-11 18:40:55 +00001745 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregore53060f2009-06-25 22:08:12 +00001746 Expr **Args, unsigned NumArgs,
1747 FunctionDecl *&Specialization,
1748 TemplateDeductionInfo &Info) {
1749 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001750
Douglas Gregore53060f2009-06-25 22:08:12 +00001751 // C++ [temp.deduct.call]p1:
1752 // Template argument deduction is done by comparing each function template
1753 // parameter type (call it P) with the type of the corresponding argument
1754 // of the call (call it A) as described below.
1755 unsigned CheckArgs = NumArgs;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001756 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregore53060f2009-06-25 22:08:12 +00001757 return TDK_TooFewArguments;
1758 else if (NumArgs > Function->getNumParams()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001759 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001760 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregore53060f2009-06-25 22:08:12 +00001761 if (!Proto->isVariadic())
1762 return TDK_TooManyArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001763
Douglas Gregore53060f2009-06-25 22:08:12 +00001764 CheckArgs = Function->getNumParams();
1765 }
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001767 // The types of the parameters from which we will perform template argument
1768 // deduction.
John McCall2a7fb272010-08-25 05:32:35 +00001769 LocalInstantiationScope InstScope(*this);
Douglas Gregore53060f2009-06-25 22:08:12 +00001770 TemplateParameterList *TemplateParams
1771 = FunctionTemplate->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00001772 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001773 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregor02024a92010-03-28 02:42:43 +00001774 unsigned NumExplicitlySpecified = 0;
John McCalld5532b62009-11-23 01:53:49 +00001775 if (ExplicitTemplateArgs) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001776 TemplateDeductionResult Result =
1777 SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001778 *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001779 Deduced,
1780 ParamTypes,
1781 0,
1782 Info);
1783 if (Result)
1784 return Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00001785
1786 NumExplicitlySpecified = Deduced.size();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001787 } else {
1788 // Just fill in the parameter types from the function declaration.
1789 for (unsigned I = 0; I != CheckArgs; ++I)
1790 ParamTypes.push_back(Function->getParamDecl(I)->getType());
1791 }
Mike Stump1eb44332009-09-09 15:08:12 +00001792
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001793 // Deduce template arguments from the function parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001794 Deduced.resize(TemplateParams->size());
Douglas Gregore53060f2009-06-25 22:08:12 +00001795 for (unsigned I = 0; I != CheckArgs; ++I) {
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001796 QualType ParamType = ParamTypes[I];
Douglas Gregore53060f2009-06-25 22:08:12 +00001797 QualType ArgType = Args[I]->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001798
Douglas Gregor75f21af2010-08-30 21:04:23 +00001799 // C++0x [temp.deduct.call]p3:
1800 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
1801 // are ignored for type deduction.
1802 if (ParamType.getCVRQualifiers())
1803 ParamType = ParamType.getLocalUnqualifiedType();
1804 const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>();
1805 if (ParamRefType) {
1806 // [...] If P is a reference type, the type referred to by P is used
1807 // for type deduction.
1808 ParamType = ParamRefType->getPointeeType();
1809 }
1810
John McCalleff92132010-02-02 02:21:27 +00001811 // Overload sets usually make this parameter an undeduced
1812 // context, but there are sometimes special circumstances.
1813 if (ArgType == Context.OverloadTy) {
1814 ArgType = ResolveOverloadForDeduction(*this, TemplateParams,
Douglas Gregor75f21af2010-08-30 21:04:23 +00001815 Args[I], ParamType,
1816 ParamRefType != 0);
John McCalleff92132010-02-02 02:21:27 +00001817 if (ArgType.isNull())
1818 continue;
1819 }
1820
Douglas Gregor75f21af2010-08-30 21:04:23 +00001821 if (ParamRefType) {
1822 // C++0x [temp.deduct.call]p3:
1823 // [...] If P is of the form T&&, where T is a template parameter, and
1824 // the argument is an lvalue, the type A& is used in place of A for
1825 // type deduction.
1826 if (ParamRefType->isRValueReferenceType() &&
1827 ParamRefType->getAs<TemplateTypeParmType>() &&
John McCall7eb0a9e2010-11-24 05:12:34 +00001828 Args[I]->isLValue())
Douglas Gregor75f21af2010-08-30 21:04:23 +00001829 ArgType = Context.getLValueReferenceType(ArgType);
1830 } else {
1831 // C++ [temp.deduct.call]p2:
1832 // If P is not a reference type:
Mike Stump1eb44332009-09-09 15:08:12 +00001833 // - If A is an array type, the pointer type produced by the
1834 // array-to-pointer standard conversion (4.2) is used in place of
Douglas Gregore53060f2009-06-25 22:08:12 +00001835 // A for type deduction; otherwise,
1836 if (ArgType->isArrayType())
1837 ArgType = Context.getArrayDecayedType(ArgType);
Mike Stump1eb44332009-09-09 15:08:12 +00001838 // - If A is a function type, the pointer type produced by the
1839 // function-to-pointer standard conversion (4.3) is used in place
Douglas Gregore53060f2009-06-25 22:08:12 +00001840 // of A for type deduction; otherwise,
1841 else if (ArgType->isFunctionType())
1842 ArgType = Context.getPointerType(ArgType);
1843 else {
1844 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1845 // type are ignored for type deduction.
1846 QualType CanonArgType = Context.getCanonicalType(ArgType);
Douglas Gregor75f21af2010-08-30 21:04:23 +00001847 if (ArgType.getCVRQualifiers())
1848 ArgType = ArgType.getUnqualifiedType();
Douglas Gregore53060f2009-06-25 22:08:12 +00001849 }
1850 }
Mike Stump1eb44332009-09-09 15:08:12 +00001851
Douglas Gregore53060f2009-06-25 22:08:12 +00001852 // C++0x [temp.deduct.call]p4:
1853 // In general, the deduction process attempts to find template argument
1854 // values that will make the deduced A identical to A (after the type A
1855 // is transformed as described above). [...]
Douglas Gregor12820292009-09-14 20:00:47 +00001856 unsigned TDF = TDF_SkipNonDependent;
Mike Stump1eb44332009-09-09 15:08:12 +00001857
Douglas Gregor508f1c82009-06-26 23:10:12 +00001858 // - If the original P is a reference type, the deduced A (i.e., the
1859 // type referred to by the reference) can be more cv-qualified than
1860 // the transformed A.
Douglas Gregor75f21af2010-08-30 21:04:23 +00001861 if (ParamRefType)
Douglas Gregor508f1c82009-06-26 23:10:12 +00001862 TDF |= TDF_ParamWithReferenceType;
Mike Stump1eb44332009-09-09 15:08:12 +00001863 // - The transformed A can be another pointer or pointer to member
1864 // type that can be converted to the deduced A via a qualification
Douglas Gregor508f1c82009-06-26 23:10:12 +00001865 // conversion (4.4).
John McCalldb0bc472010-08-05 05:30:45 +00001866 if (ArgType->isPointerType() || ArgType->isMemberPointerType() ||
1867 ArgType->isObjCObjectPointerType())
Douglas Gregor508f1c82009-06-26 23:10:12 +00001868 TDF |= TDF_IgnoreQualifiers;
Mike Stump1eb44332009-09-09 15:08:12 +00001869 // - If P is a class and P has the form simple-template-id, then the
Douglas Gregor41128772009-06-26 23:27:24 +00001870 // transformed A can be a derived class of the deduced A. Likewise,
1871 // if P is a pointer to a class of the form simple-template-id, the
1872 // transformed A can be a pointer to a derived class pointed to by
1873 // the deduced A.
1874 if (isSimpleTemplateIdType(ParamType) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001875 (isa<PointerType>(ParamType) &&
Douglas Gregor41128772009-06-26 23:27:24 +00001876 isSimpleTemplateIdType(
Ted Kremenek6217b802009-07-29 21:53:49 +00001877 ParamType->getAs<PointerType>()->getPointeeType())))
Douglas Gregor41128772009-06-26 23:27:24 +00001878 TDF |= TDF_DerivedClass;
Mike Stump1eb44332009-09-09 15:08:12 +00001879
Douglas Gregore53060f2009-06-25 22:08:12 +00001880 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001881 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor500d3312009-06-26 18:27:22 +00001882 ParamType, ArgType, Info, Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +00001883 TDF))
Douglas Gregore53060f2009-06-25 22:08:12 +00001884 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001885
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001886 // FIXME: we need to check that the deduced A is the same as A,
1887 // modulo the various allowed differences.
Douglas Gregore53060f2009-06-25 22:08:12 +00001888 }
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001889
Mike Stump1eb44332009-09-09 15:08:12 +00001890 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor02024a92010-03-28 02:42:43 +00001891 NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001892 Specialization, Info);
Douglas Gregore53060f2009-06-25 22:08:12 +00001893}
1894
Douglas Gregor83314aa2009-07-08 20:55:45 +00001895/// \brief Deduce template arguments when taking the address of a function
Douglas Gregor4b52e252009-12-21 23:17:24 +00001896/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
1897/// a template.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001898///
1899/// \param FunctionTemplate the function template for which we are performing
1900/// template argument deduction.
1901///
Douglas Gregor4b52e252009-12-21 23:17:24 +00001902/// \param ExplicitTemplateArguments the explicitly-specified template
1903/// arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001904///
1905/// \param ArgFunctionType the function type that will be used as the
1906/// "argument" type (A) when performing template argument deduction from the
Douglas Gregor4b52e252009-12-21 23:17:24 +00001907/// function template's function type. This type may be NULL, if there is no
1908/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001909///
1910/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001911/// this will be set to the function template specialization produced by
Douglas Gregor83314aa2009-07-08 20:55:45 +00001912/// template argument deduction.
1913///
1914/// \param Info the argument will be updated to provide additional information
1915/// about template argument deduction.
1916///
1917/// \returns the result of template argument deduction.
1918Sema::TemplateDeductionResult
1919Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001920 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001921 QualType ArgFunctionType,
1922 FunctionDecl *&Specialization,
1923 TemplateDeductionInfo &Info) {
1924 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1925 TemplateParameterList *TemplateParams
1926 = FunctionTemplate->getTemplateParameters();
1927 QualType FunctionType = Function->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001928
Douglas Gregor83314aa2009-07-08 20:55:45 +00001929 // Substitute any explicit template arguments.
John McCall2a7fb272010-08-25 05:32:35 +00001930 LocalInstantiationScope InstScope(*this);
Douglas Gregor02024a92010-03-28 02:42:43 +00001931 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
1932 unsigned NumExplicitlySpecified = 0;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001933 llvm::SmallVector<QualType, 4> ParamTypes;
John McCalld5532b62009-11-23 01:53:49 +00001934 if (ExplicitTemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001935 if (TemplateDeductionResult Result
1936 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001937 *ExplicitTemplateArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00001938 Deduced, ParamTypes,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001939 &FunctionType, Info))
1940 return Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00001941
1942 NumExplicitlySpecified = Deduced.size();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001943 }
1944
1945 // Template argument deduction for function templates in a SFINAE context.
1946 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001947 SFINAETrap Trap(*this);
1948
John McCalleff92132010-02-02 02:21:27 +00001949 Deduced.resize(TemplateParams->size());
1950
Douglas Gregor4b52e252009-12-21 23:17:24 +00001951 if (!ArgFunctionType.isNull()) {
1952 // Deduce template arguments from the function type.
Douglas Gregor4b52e252009-12-21 23:17:24 +00001953 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001954 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor4b52e252009-12-21 23:17:24 +00001955 FunctionType, ArgFunctionType, Info,
1956 Deduced, 0))
1957 return Result;
1958 }
Douglas Gregorfbb6fad2010-09-29 21:14:36 +00001959
1960 if (TemplateDeductionResult Result
1961 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
1962 NumExplicitlySpecified,
1963 Specialization, Info))
1964 return Result;
1965
1966 // If the requested function type does not match the actual type of the
1967 // specialization, template argument deduction fails.
1968 if (!ArgFunctionType.isNull() &&
1969 !Context.hasSameType(ArgFunctionType, Specialization->getType()))
1970 return TDK_NonDeducedMismatch;
1971
1972 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001973}
1974
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001975/// \brief Deduce template arguments for a templated conversion
1976/// function (C++ [temp.deduct.conv]) and, if successful, produce a
1977/// conversion function template specialization.
1978Sema::TemplateDeductionResult
1979Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1980 QualType ToType,
1981 CXXConversionDecl *&Specialization,
1982 TemplateDeductionInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00001983 CXXConversionDecl *Conv
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001984 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
1985 QualType FromType = Conv->getConversionType();
1986
1987 // Canonicalize the types for deduction.
1988 QualType P = Context.getCanonicalType(FromType);
1989 QualType A = Context.getCanonicalType(ToType);
1990
1991 // C++0x [temp.deduct.conv]p3:
1992 // If P is a reference type, the type referred to by P is used for
1993 // type deduction.
1994 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
1995 P = PRef->getPointeeType();
1996
1997 // C++0x [temp.deduct.conv]p3:
1998 // If A is a reference type, the type referred to by A is used
1999 // for type deduction.
2000 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
2001 A = ARef->getPointeeType();
2002 // C++ [temp.deduct.conv]p2:
2003 //
Mike Stump1eb44332009-09-09 15:08:12 +00002004 // If A is not a reference type:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002005 else {
2006 assert(!A->isReferenceType() && "Reference types were handled above");
2007
2008 // - If P is an array type, the pointer type produced by the
Mike Stump1eb44332009-09-09 15:08:12 +00002009 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002010 // of P for type deduction; otherwise,
2011 if (P->isArrayType())
2012 P = Context.getArrayDecayedType(P);
2013 // - If P is a function type, the pointer type produced by the
2014 // function-to-pointer standard conversion (4.3) is used in
2015 // place of P for type deduction; otherwise,
2016 else if (P->isFunctionType())
2017 P = Context.getPointerType(P);
2018 // - If P is a cv-qualified type, the top level cv-qualifiers of
2019 // P’s type are ignored for type deduction.
2020 else
2021 P = P.getUnqualifiedType();
2022
2023 // C++0x [temp.deduct.conv]p3:
2024 // If A is a cv-qualified type, the top level cv-qualifiers of A’s
2025 // type are ignored for type deduction.
2026 A = A.getUnqualifiedType();
2027 }
2028
2029 // Template argument deduction for function templates in a SFINAE context.
2030 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002031 SFINAETrap Trap(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002032
2033 // C++ [temp.deduct.conv]p1:
2034 // Template argument deduction is done by comparing the return
2035 // type of the template conversion function (call it P) with the
2036 // type that is required as the result of the conversion (call it
2037 // A) as described in 14.8.2.4.
2038 TemplateParameterList *TemplateParams
2039 = FunctionTemplate->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00002040 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Mike Stump1eb44332009-09-09 15:08:12 +00002041 Deduced.resize(TemplateParams->size());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002042
2043 // C++0x [temp.deduct.conv]p4:
2044 // In general, the deduction process attempts to find template
2045 // argument values that will make the deduced A identical to
2046 // A. However, there are two cases that allow a difference:
2047 unsigned TDF = 0;
2048 // - If the original A is a reference type, A can be more
2049 // cv-qualified than the deduced A (i.e., the type referred to
2050 // by the reference)
2051 if (ToType->isReferenceType())
2052 TDF |= TDF_ParamWithReferenceType;
2053 // - The deduced A can be another pointer or pointer to member
2054 // type that can be converted to A via a qualification
2055 // conversion.
2056 //
2057 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
2058 // both P and A are pointers or member pointers. In this case, we
2059 // just ignore cv-qualifiers completely).
2060 if ((P->isPointerType() && A->isPointerType()) ||
2061 (P->isMemberPointerType() && P->isMemberPointerType()))
2062 TDF |= TDF_IgnoreQualifiers;
2063 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002064 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002065 P, A, Info, Deduced, TDF))
2066 return Result;
2067
2068 // FIXME: we need to check that the deduced A is the same as A,
2069 // modulo the various allowed differences.
Mike Stump1eb44332009-09-09 15:08:12 +00002070
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002071 // Finish template argument deduction.
John McCall2a7fb272010-08-25 05:32:35 +00002072 LocalInstantiationScope InstScope(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002073 FunctionDecl *Spec = 0;
2074 TemplateDeductionResult Result
Douglas Gregor02024a92010-03-28 02:42:43 +00002075 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec,
2076 Info);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002077 Specialization = cast_or_null<CXXConversionDecl>(Spec);
2078 return Result;
2079}
2080
Douglas Gregor4b52e252009-12-21 23:17:24 +00002081/// \brief Deduce template arguments for a function template when there is
2082/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
2083///
2084/// \param FunctionTemplate the function template for which we are performing
2085/// template argument deduction.
2086///
2087/// \param ExplicitTemplateArguments the explicitly-specified template
2088/// arguments.
2089///
2090/// \param Specialization if template argument deduction was successful,
2091/// this will be set to the function template specialization produced by
2092/// template argument deduction.
2093///
2094/// \param Info the argument will be updated to provide additional information
2095/// about template argument deduction.
2096///
2097/// \returns the result of template argument deduction.
2098Sema::TemplateDeductionResult
2099Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
2100 const TemplateArgumentListInfo *ExplicitTemplateArgs,
2101 FunctionDecl *&Specialization,
2102 TemplateDeductionInfo &Info) {
2103 return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
2104 QualType(), Specialization, Info);
2105}
2106
Douglas Gregor8a514912009-09-14 18:39:43 +00002107/// \brief Stores the result of comparing the qualifiers of two types.
2108enum DeductionQualifierComparison {
2109 NeitherMoreQualified = 0,
2110 ParamMoreQualified,
2111 ArgMoreQualified
2112};
2113
2114/// \brief Deduce the template arguments during partial ordering by comparing
2115/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
2116///
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002117/// \param S the semantic analysis object within which we are deducing
Douglas Gregor8a514912009-09-14 18:39:43 +00002118///
2119/// \param TemplateParams the template parameters that we are deducing
2120///
2121/// \param ParamIn the parameter type
2122///
2123/// \param ArgIn the argument type
2124///
2125/// \param Info information about the template argument deduction itself
2126///
2127/// \param Deduced the deduced template arguments
2128///
2129/// \returns the result of template argument deduction so far. Note that a
2130/// "success" result means that template argument deduction has not yet failed,
2131/// but it may still fail, later, for other reasons.
2132static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002133DeduceTemplateArgumentsDuringPartialOrdering(Sema &S,
Douglas Gregor02024a92010-03-28 02:42:43 +00002134 TemplateParameterList *TemplateParams,
Douglas Gregor8a514912009-09-14 18:39:43 +00002135 QualType ParamIn, QualType ArgIn,
John McCall2a7fb272010-08-25 05:32:35 +00002136 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +00002137 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
2138 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002139 CanQualType Param = S.Context.getCanonicalType(ParamIn);
2140 CanQualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor8a514912009-09-14 18:39:43 +00002141
2142 // C++0x [temp.deduct.partial]p5:
2143 // Before the partial ordering is done, certain transformations are
2144 // performed on the types used for partial ordering:
2145 // - If P is a reference type, P is replaced by the type referred to.
2146 CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00002147 if (!ParamRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00002148 Param = ParamRef->getPointeeType();
2149
2150 // - If A is a reference type, A is replaced by the type referred to.
2151 CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00002152 if (!ArgRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00002153 Arg = ArgRef->getPointeeType();
2154
John McCalle27ec8a2009-10-23 23:03:21 +00002155 if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
Douglas Gregor8a514912009-09-14 18:39:43 +00002156 // C++0x [temp.deduct.partial]p6:
2157 // If both P and A were reference types (before being replaced with the
2158 // type referred to above), determine which of the two types (if any) is
2159 // more cv-qualified than the other; otherwise the types are considered to
2160 // be equally cv-qualified for partial ordering purposes. The result of this
2161 // determination will be used below.
2162 //
2163 // We save this information for later, using it only when deduction
2164 // succeeds in both directions.
2165 DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
2166 if (Param.isMoreQualifiedThan(Arg))
2167 QualifierResult = ParamMoreQualified;
2168 else if (Arg.isMoreQualifiedThan(Param))
2169 QualifierResult = ArgMoreQualified;
2170 QualifierComparisons->push_back(QualifierResult);
2171 }
2172
2173 // C++0x [temp.deduct.partial]p7:
2174 // Remove any top-level cv-qualifiers:
2175 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
2176 // version of P.
2177 Param = Param.getUnqualifiedType();
2178 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
2179 // version of A.
2180 Arg = Arg.getUnqualifiedType();
2181
2182 // C++0x [temp.deduct.partial]p8:
2183 // Using the resulting types P and A the deduction is then done as
2184 // described in 14.9.2.5. If deduction succeeds for a given type, the type
2185 // from the argument template is considered to be at least as specialized
2186 // as the type from the parameter template.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002187 return DeduceTemplateArguments(S, TemplateParams, Param, Arg, Info,
Douglas Gregor8a514912009-09-14 18:39:43 +00002188 Deduced, TDF_None);
2189}
2190
2191static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002192MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2193 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002194 unsigned Level,
Douglas Gregore73bb602009-09-14 21:25:05 +00002195 llvm::SmallVectorImpl<bool> &Deduced);
Douglas Gregor77bc5722010-11-12 23:44:13 +00002196
2197/// \brief If this is a non-static member function,
2198static void MaybeAddImplicitObjectParameterType(ASTContext &Context,
2199 CXXMethodDecl *Method,
2200 llvm::SmallVectorImpl<QualType> &ArgTypes) {
2201 if (Method->isStatic())
2202 return;
2203
2204 // C++ [over.match.funcs]p4:
2205 //
2206 // For non-static member functions, the type of the implicit
2207 // object parameter is
2208 // — "lvalue reference to cv X" for functions declared without a
2209 // ref-qualifier or with the & ref-qualifier
2210 // - "rvalue reference to cv X" for functions declared with the
2211 // && ref-qualifier
2212 //
2213 // FIXME: We don't have ref-qualifiers yet, so we don't do that part.
2214 QualType ArgTy = Context.getTypeDeclType(Method->getParent());
2215 ArgTy = Context.getQualifiedType(ArgTy,
2216 Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
2217 ArgTy = Context.getLValueReferenceType(ArgTy);
2218 ArgTypes.push_back(ArgTy);
2219}
2220
Douglas Gregor8a514912009-09-14 18:39:43 +00002221/// \brief Determine whether the function template \p FT1 is at least as
2222/// specialized as \p FT2.
2223static bool isAtLeastAsSpecializedAs(Sema &S,
John McCall5769d612010-02-08 23:07:23 +00002224 SourceLocation Loc,
Douglas Gregor8a514912009-09-14 18:39:43 +00002225 FunctionTemplateDecl *FT1,
2226 FunctionTemplateDecl *FT2,
2227 TemplatePartialOrderingContext TPOC,
2228 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
2229 FunctionDecl *FD1 = FT1->getTemplatedDecl();
2230 FunctionDecl *FD2 = FT2->getTemplatedDecl();
2231 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
2232 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
2233
2234 assert(Proto1 && Proto2 && "Function templates must have prototypes");
2235 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00002236 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor8a514912009-09-14 18:39:43 +00002237 Deduced.resize(TemplateParams->size());
2238
2239 // C++0x [temp.deduct.partial]p3:
2240 // The types used to determine the ordering depend on the context in which
2241 // the partial ordering is done:
John McCall2a7fb272010-08-25 05:32:35 +00002242 TemplateDeductionInfo Info(S.Context, Loc);
Douglas Gregor8d706ec2010-11-15 15:41:16 +00002243 CXXMethodDecl *Method1 = 0;
2244 CXXMethodDecl *Method2 = 0;
2245 bool IsNonStatic2 = false;
2246 bool IsNonStatic1 = false;
2247 unsigned Skip2 = 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00002248 switch (TPOC) {
2249 case TPOC_Call: {
2250 // - In the context of a function call, the function parameter types are
2251 // used.
Douglas Gregor8d706ec2010-11-15 15:41:16 +00002252 Method1 = dyn_cast<CXXMethodDecl>(FD1);
2253 Method2 = dyn_cast<CXXMethodDecl>(FD2);
2254 IsNonStatic1 = Method1 && !Method1->isStatic();
2255 IsNonStatic2 = Method2 && !Method2->isStatic();
2256
2257 // C++0x [temp.func.order]p3:
2258 // [...] If only one of the function templates is a non-static
2259 // member, that function template is considered to have a new
2260 // first parameter inserted in its function parameter list. The
2261 // new parameter is of type "reference to cv A," where cv are
2262 // the cv-qualifiers of the function template (if any) and A is
2263 // the class of which the function template is a member.
2264 //
2265 // C++98/03 doesn't have this provision, so instead we drop the
2266 // first argument of the free function or static member, which
2267 // seems to match existing practice.
Douglas Gregor77bc5722010-11-12 23:44:13 +00002268 llvm::SmallVector<QualType, 4> Args1;
Douglas Gregor8d706ec2010-11-15 15:41:16 +00002269 unsigned Skip1 = !S.getLangOptions().CPlusPlus0x &&
2270 IsNonStatic2 && !IsNonStatic1;
2271 if (S.getLangOptions().CPlusPlus0x && IsNonStatic1 && !IsNonStatic2)
Douglas Gregor77bc5722010-11-12 23:44:13 +00002272 MaybeAddImplicitObjectParameterType(S.Context, Method1, Args1);
2273 Args1.insert(Args1.end(),
Douglas Gregor8d706ec2010-11-15 15:41:16 +00002274 Proto1->arg_type_begin() + Skip1, Proto1->arg_type_end());
Douglas Gregor77bc5722010-11-12 23:44:13 +00002275
2276 llvm::SmallVector<QualType, 4> Args2;
Douglas Gregor8d706ec2010-11-15 15:41:16 +00002277 Skip2 = !S.getLangOptions().CPlusPlus0x &&
2278 IsNonStatic1 && !IsNonStatic2;
2279 if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1)
Douglas Gregor77bc5722010-11-12 23:44:13 +00002280 MaybeAddImplicitObjectParameterType(S.Context, Method2, Args2);
2281 Args2.insert(Args2.end(),
Douglas Gregor8d706ec2010-11-15 15:41:16 +00002282 Proto2->arg_type_begin() + Skip2, Proto2->arg_type_end());
Douglas Gregor77bc5722010-11-12 23:44:13 +00002283
2284 unsigned NumParams = std::min(Args1.size(), Args2.size());
Douglas Gregor8a514912009-09-14 18:39:43 +00002285 for (unsigned I = 0; I != NumParams; ++I)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002286 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00002287 TemplateParams,
Douglas Gregor77bc5722010-11-12 23:44:13 +00002288 Args2[I],
2289 Args1[I],
Douglas Gregor8a514912009-09-14 18:39:43 +00002290 Info,
2291 Deduced,
2292 QualifierComparisons))
2293 return false;
2294
2295 break;
2296 }
2297
2298 case TPOC_Conversion:
2299 // - In the context of a call to a conversion operator, the return types
2300 // of the conversion function templates are used.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002301 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00002302 TemplateParams,
2303 Proto2->getResultType(),
2304 Proto1->getResultType(),
2305 Info,
2306 Deduced,
2307 QualifierComparisons))
2308 return false;
2309 break;
2310
2311 case TPOC_Other:
2312 // - In other contexts (14.6.6.2) the function template’s function type
2313 // is used.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002314 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00002315 TemplateParams,
2316 FD2->getType(),
2317 FD1->getType(),
2318 Info,
2319 Deduced,
2320 QualifierComparisons))
2321 return false;
2322 break;
2323 }
2324
2325 // C++0x [temp.deduct.partial]p11:
2326 // In most cases, all template parameters must have values in order for
2327 // deduction to succeed, but for partial ordering purposes a template
2328 // parameter may remain without a value provided it is not used in the
2329 // types being used for partial ordering. [ Note: a template parameter used
2330 // in a non-deduced context is considered used. -end note]
2331 unsigned ArgIdx = 0, NumArgs = Deduced.size();
2332 for (; ArgIdx != NumArgs; ++ArgIdx)
2333 if (Deduced[ArgIdx].isNull())
2334 break;
2335
2336 if (ArgIdx == NumArgs) {
2337 // All template arguments were deduced. FT1 is at least as specialized
2338 // as FT2.
2339 return true;
2340 }
2341
Douglas Gregore73bb602009-09-14 21:25:05 +00002342 // Figure out which template parameters were used.
Douglas Gregor8a514912009-09-14 18:39:43 +00002343 llvm::SmallVector<bool, 4> UsedParameters;
2344 UsedParameters.resize(TemplateParams->size());
2345 switch (TPOC) {
2346 case TPOC_Call: {
2347 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
Douglas Gregor8d706ec2010-11-15 15:41:16 +00002348 if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1)
2349 ::MarkUsedTemplateParameters(S, Method2->getThisType(S.Context), false,
2350 TemplateParams->getDepth(), UsedParameters);
2351 for (unsigned I = Skip2; I < NumParams; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002352 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
2353 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00002354 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00002355 break;
2356 }
2357
2358 case TPOC_Conversion:
Douglas Gregored9c0f92009-10-29 00:04:11 +00002359 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
2360 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00002361 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00002362 break;
2363
2364 case TPOC_Other:
Douglas Gregored9c0f92009-10-29 00:04:11 +00002365 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
2366 TemplateParams->getDepth(),
2367 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00002368 break;
2369 }
2370
2371 for (; ArgIdx != NumArgs; ++ArgIdx)
2372 // If this argument had no value deduced but was used in one of the types
2373 // used for partial ordering, then deduction fails.
2374 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
2375 return false;
2376
2377 return true;
2378}
2379
2380
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002381/// \brief Returns the more specialized function template according
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002382/// to the rules of function template partial ordering (C++ [temp.func.order]).
2383///
2384/// \param FT1 the first function template
2385///
2386/// \param FT2 the second function template
2387///
Douglas Gregor8a514912009-09-14 18:39:43 +00002388/// \param TPOC the context in which we are performing partial ordering of
2389/// function templates.
Mike Stump1eb44332009-09-09 15:08:12 +00002390///
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002391/// \returns the more specialized function template. If neither
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002392/// template is more specialized, returns NULL.
2393FunctionTemplateDecl *
2394Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
2395 FunctionTemplateDecl *FT2,
John McCall5769d612010-02-08 23:07:23 +00002396 SourceLocation Loc,
Douglas Gregor8a514912009-09-14 18:39:43 +00002397 TemplatePartialOrderingContext TPOC) {
Douglas Gregor8a514912009-09-14 18:39:43 +00002398 llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
John McCall5769d612010-02-08 23:07:23 +00002399 bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, 0);
2400 bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
Douglas Gregor8a514912009-09-14 18:39:43 +00002401 &QualifierComparisons);
2402
2403 if (Better1 != Better2) // We have a clear winner
2404 return Better1? FT1 : FT2;
2405
2406 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002407 return 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00002408
2409
2410 // C++0x [temp.deduct.partial]p10:
2411 // If for each type being considered a given template is at least as
2412 // specialized for all types and more specialized for some set of types and
2413 // the other template is not more specialized for any types or is not at
2414 // least as specialized for any types, then the given template is more
2415 // specialized than the other template. Otherwise, neither template is more
2416 // specialized than the other.
2417 Better1 = false;
2418 Better2 = false;
2419 for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
2420 // C++0x [temp.deduct.partial]p9:
2421 // If, for a given type, deduction succeeds in both directions (i.e., the
2422 // types are identical after the transformations above) and if the type
2423 // from the argument template is more cv-qualified than the type from the
2424 // parameter template (as described above) that type is considered to be
2425 // more specialized than the other. If neither type is more cv-qualified
2426 // than the other then neither type is more specialized than the other.
2427 switch (QualifierComparisons[I]) {
2428 case NeitherMoreQualified:
2429 break;
2430
2431 case ParamMoreQualified:
2432 Better1 = true;
2433 if (Better2)
2434 return 0;
2435 break;
2436
2437 case ArgMoreQualified:
2438 Better2 = true;
2439 if (Better1)
2440 return 0;
2441 break;
2442 }
2443 }
2444
2445 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002446 if (Better1)
2447 return FT1;
Douglas Gregor8a514912009-09-14 18:39:43 +00002448 else if (Better2)
2449 return FT2;
2450 else
2451 return 0;
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002452}
Douglas Gregor83314aa2009-07-08 20:55:45 +00002453
Douglas Gregord5a423b2009-09-25 18:43:00 +00002454/// \brief Determine if the two templates are equivalent.
2455static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
2456 if (T1 == T2)
2457 return true;
2458
2459 if (!T1 || !T2)
2460 return false;
2461
2462 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
2463}
2464
2465/// \brief Retrieve the most specialized of the given function template
2466/// specializations.
2467///
John McCallc373d482010-01-27 01:50:18 +00002468/// \param SpecBegin the start iterator of the function template
2469/// specializations that we will be comparing.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002470///
John McCallc373d482010-01-27 01:50:18 +00002471/// \param SpecEnd the end iterator of the function template
2472/// specializations, paired with \p SpecBegin.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002473///
2474/// \param TPOC the partial ordering context to use to compare the function
2475/// template specializations.
2476///
2477/// \param Loc the location where the ambiguity or no-specializations
2478/// diagnostic should occur.
2479///
2480/// \param NoneDiag partial diagnostic used to diagnose cases where there are
2481/// no matching candidates.
2482///
2483/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
2484/// occurs.
2485///
2486/// \param CandidateDiag partial diagnostic used for each function template
2487/// specialization that is a candidate in the ambiguous ordering. One parameter
2488/// in this diagnostic should be unbound, which will correspond to the string
2489/// describing the template arguments for the function template specialization.
2490///
2491/// \param Index if non-NULL and the result of this function is non-nULL,
2492/// receives the index corresponding to the resulting function template
2493/// specialization.
2494///
2495/// \returns the most specialized function template specialization, if
John McCallc373d482010-01-27 01:50:18 +00002496/// found. Otherwise, returns SpecEnd.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002497///
2498/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
2499/// template argument deduction.
John McCallc373d482010-01-27 01:50:18 +00002500UnresolvedSetIterator
2501Sema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
2502 UnresolvedSetIterator SpecEnd,
2503 TemplatePartialOrderingContext TPOC,
2504 SourceLocation Loc,
2505 const PartialDiagnostic &NoneDiag,
2506 const PartialDiagnostic &AmbigDiag,
2507 const PartialDiagnostic &CandidateDiag) {
2508 if (SpecBegin == SpecEnd) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00002509 Diag(Loc, NoneDiag);
John McCallc373d482010-01-27 01:50:18 +00002510 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002511 }
2512
John McCallc373d482010-01-27 01:50:18 +00002513 if (SpecBegin + 1 == SpecEnd)
2514 return SpecBegin;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002515
2516 // Find the function template that is better than all of the templates it
2517 // has been compared to.
John McCallc373d482010-01-27 01:50:18 +00002518 UnresolvedSetIterator Best = SpecBegin;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002519 FunctionTemplateDecl *BestTemplate
John McCallc373d482010-01-27 01:50:18 +00002520 = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002521 assert(BestTemplate && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00002522 for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
2523 FunctionTemplateDecl *Challenger
2524 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002525 assert(Challenger && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00002526 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCall5769d612010-02-08 23:07:23 +00002527 Loc, TPOC),
Douglas Gregord5a423b2009-09-25 18:43:00 +00002528 Challenger)) {
2529 Best = I;
2530 BestTemplate = Challenger;
2531 }
2532 }
2533
2534 // Make sure that the "best" function template is more specialized than all
2535 // of the others.
2536 bool Ambiguous = false;
John McCallc373d482010-01-27 01:50:18 +00002537 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
2538 FunctionTemplateDecl *Challenger
2539 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002540 if (I != Best &&
2541 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCall5769d612010-02-08 23:07:23 +00002542 Loc, TPOC),
Douglas Gregord5a423b2009-09-25 18:43:00 +00002543 BestTemplate)) {
2544 Ambiguous = true;
2545 break;
2546 }
2547 }
2548
2549 if (!Ambiguous) {
2550 // We found an answer. Return it.
John McCallc373d482010-01-27 01:50:18 +00002551 return Best;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002552 }
2553
2554 // Diagnose the ambiguity.
2555 Diag(Loc, AmbigDiag);
2556
2557 // FIXME: Can we order the candidates in some sane way?
John McCallc373d482010-01-27 01:50:18 +00002558 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
2559 Diag((*I)->getLocation(), CandidateDiag)
Douglas Gregord5a423b2009-09-25 18:43:00 +00002560 << getTemplateArgumentBindingsText(
John McCallc373d482010-01-27 01:50:18 +00002561 cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
2562 *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
Douglas Gregord5a423b2009-09-25 18:43:00 +00002563
John McCallc373d482010-01-27 01:50:18 +00002564 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002565}
2566
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002567/// \brief Returns the more specialized class template partial specialization
2568/// according to the rules of partial ordering of class template partial
2569/// specializations (C++ [temp.class.order]).
2570///
2571/// \param PS1 the first class template partial specialization
2572///
2573/// \param PS2 the second class template partial specialization
2574///
2575/// \returns the more specialized class template partial specialization. If
2576/// neither partial specialization is more specialized, returns NULL.
2577ClassTemplatePartialSpecializationDecl *
2578Sema::getMoreSpecializedPartialSpecialization(
2579 ClassTemplatePartialSpecializationDecl *PS1,
John McCall5769d612010-02-08 23:07:23 +00002580 ClassTemplatePartialSpecializationDecl *PS2,
2581 SourceLocation Loc) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002582 // C++ [temp.class.order]p1:
2583 // For two class template partial specializations, the first is at least as
2584 // specialized as the second if, given the following rewrite to two
2585 // function templates, the first function template is at least as
2586 // specialized as the second according to the ordering rules for function
2587 // templates (14.6.6.2):
2588 // - the first function template has the same template parameters as the
2589 // first partial specialization and has a single function parameter
2590 // whose type is a class template specialization with the template
2591 // arguments of the first partial specialization, and
2592 // - the second function template has the same template parameters as the
2593 // second partial specialization and has a single function parameter
2594 // whose type is a class template specialization with the template
2595 // arguments of the second partial specialization.
2596 //
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002597 // Rather than synthesize function templates, we merely perform the
2598 // equivalent partial ordering by performing deduction directly on
2599 // the template arguments of the class template partial
2600 // specializations. This computation is slightly simpler than the
2601 // general problem of function template partial ordering, because
2602 // class template partial specializations are more constrained. We
2603 // know that every template parameter is deducible from the class
2604 // template partial specialization's template arguments, for
2605 // example.
Douglas Gregor02024a92010-03-28 02:42:43 +00002606 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
John McCall2a7fb272010-08-25 05:32:35 +00002607 TemplateDeductionInfo Info(Context, Loc);
John McCall31f17ec2010-04-27 00:57:59 +00002608
2609 QualType PT1 = PS1->getInjectedSpecializationType();
2610 QualType PT2 = PS2->getInjectedSpecializationType();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002611
2612 // Determine whether PS1 is at least as specialized as PS2
2613 Deduced.resize(PS2->getTemplateParameters()->size());
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002614 bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002615 PS2->getTemplateParameters(),
John McCall31f17ec2010-04-27 00:57:59 +00002616 PT2,
2617 PT1,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002618 Info,
2619 Deduced,
2620 0);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00002621 if (Better1) {
2622 InstantiatingTemplate Inst(*this, PS2->getLocation(), PS2,
2623 Deduced.data(), Deduced.size(), Info);
Douglas Gregor516e6e02010-04-29 06:31:36 +00002624 Better1 = !::FinishTemplateArgumentDeduction(*this, PS2,
2625 PS1->getTemplateArgs(),
2626 Deduced, Info);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00002627 }
Douglas Gregor516e6e02010-04-29 06:31:36 +00002628
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002629 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregordb0d4b72009-11-11 23:06:43 +00002630 Deduced.clear();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002631 Deduced.resize(PS1->getTemplateParameters()->size());
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002632 bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002633 PS1->getTemplateParameters(),
John McCall31f17ec2010-04-27 00:57:59 +00002634 PT1,
2635 PT2,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002636 Info,
2637 Deduced,
2638 0);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00002639 if (Better2) {
2640 InstantiatingTemplate Inst(*this, PS1->getLocation(), PS1,
2641 Deduced.data(), Deduced.size(), Info);
Douglas Gregor516e6e02010-04-29 06:31:36 +00002642 Better2 = !::FinishTemplateArgumentDeduction(*this, PS1,
2643 PS2->getTemplateArgs(),
2644 Deduced, Info);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00002645 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002646
2647 if (Better1 == Better2)
2648 return 0;
2649
2650 return Better1? PS1 : PS2;
2651}
2652
Mike Stump1eb44332009-09-09 15:08:12 +00002653static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002654MarkUsedTemplateParameters(Sema &SemaRef,
2655 const TemplateArgument &TemplateArg,
2656 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002657 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002658 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002659
Douglas Gregore73bb602009-09-14 21:25:05 +00002660/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002661/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00002662static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002663MarkUsedTemplateParameters(Sema &SemaRef,
2664 const Expr *E,
2665 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002666 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002667 llvm::SmallVectorImpl<bool> &Used) {
2668 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2669 // find other occurrences of template parameters.
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002670 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregorc781f9c2010-01-14 18:13:22 +00002671 if (!DRE)
Douglas Gregor031a5882009-06-13 00:26:55 +00002672 return;
2673
Mike Stump1eb44332009-09-09 15:08:12 +00002674 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00002675 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2676 if (!NTTP)
2677 return;
2678
Douglas Gregored9c0f92009-10-29 00:04:11 +00002679 if (NTTP->getDepth() == Depth)
2680 Used[NTTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002681}
2682
Douglas Gregore73bb602009-09-14 21:25:05 +00002683/// \brief Mark the template parameters that are used by the given
2684/// nested name specifier.
2685static void
2686MarkUsedTemplateParameters(Sema &SemaRef,
2687 NestedNameSpecifier *NNS,
2688 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002689 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002690 llvm::SmallVectorImpl<bool> &Used) {
2691 if (!NNS)
2692 return;
2693
Douglas Gregored9c0f92009-10-29 00:04:11 +00002694 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2695 Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002696 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002697 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002698}
2699
2700/// \brief Mark the template parameters that are used by the given
2701/// template name.
2702static void
2703MarkUsedTemplateParameters(Sema &SemaRef,
2704 TemplateName Name,
2705 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002706 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002707 llvm::SmallVectorImpl<bool> &Used) {
2708 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2709 if (TemplateTemplateParmDecl *TTP
Douglas Gregored9c0f92009-10-29 00:04:11 +00002710 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2711 if (TTP->getDepth() == Depth)
2712 Used[TTP->getIndex()] = true;
2713 }
Douglas Gregore73bb602009-09-14 21:25:05 +00002714 return;
2715 }
2716
Douglas Gregor788cd062009-11-11 01:00:40 +00002717 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2718 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2719 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002720 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
Douglas Gregored9c0f92009-10-29 00:04:11 +00002721 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2722 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002723}
2724
2725/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002726/// type.
Mike Stump1eb44332009-09-09 15:08:12 +00002727static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002728MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2729 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002730 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002731 llvm::SmallVectorImpl<bool> &Used) {
2732 if (T.isNull())
2733 return;
2734
Douglas Gregor031a5882009-06-13 00:26:55 +00002735 // Non-dependent types have nothing deducible
2736 if (!T->isDependentType())
2737 return;
2738
2739 T = SemaRef.Context.getCanonicalType(T);
2740 switch (T->getTypeClass()) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002741 case Type::Pointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002742 MarkUsedTemplateParameters(SemaRef,
2743 cast<PointerType>(T)->getPointeeType(),
2744 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002745 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002746 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002747 break;
2748
2749 case Type::BlockPointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002750 MarkUsedTemplateParameters(SemaRef,
2751 cast<BlockPointerType>(T)->getPointeeType(),
2752 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002753 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002754 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002755 break;
2756
2757 case Type::LValueReference:
2758 case Type::RValueReference:
Douglas Gregore73bb602009-09-14 21:25:05 +00002759 MarkUsedTemplateParameters(SemaRef,
2760 cast<ReferenceType>(T)->getPointeeType(),
2761 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002762 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002763 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002764 break;
2765
2766 case Type::MemberPointer: {
2767 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore73bb602009-09-14 21:25:05 +00002768 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002769 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002770 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002771 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002772 break;
2773 }
2774
2775 case Type::DependentSizedArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002776 MarkUsedTemplateParameters(SemaRef,
2777 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002778 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002779 // Fall through to check the element type
2780
2781 case Type::ConstantArray:
2782 case Type::IncompleteArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002783 MarkUsedTemplateParameters(SemaRef,
2784 cast<ArrayType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002785 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002786 break;
2787
2788 case Type::Vector:
2789 case Type::ExtVector:
Douglas Gregore73bb602009-09-14 21:25:05 +00002790 MarkUsedTemplateParameters(SemaRef,
2791 cast<VectorType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002792 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002793 break;
2794
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002795 case Type::DependentSizedExtVector: {
2796 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002797 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002798 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002799 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002800 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002801 Depth, Used);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002802 break;
2803 }
2804
Douglas Gregor031a5882009-06-13 00:26:55 +00002805 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002806 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002807 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002808 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002809 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore73bb602009-09-14 21:25:05 +00002810 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002811 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002812 break;
2813 }
2814
Douglas Gregored9c0f92009-10-29 00:04:11 +00002815 case Type::TemplateTypeParm: {
2816 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2817 if (TTP->getDepth() == Depth)
2818 Used[TTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002819 break;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002820 }
Douglas Gregor031a5882009-06-13 00:26:55 +00002821
John McCall31f17ec2010-04-27 00:57:59 +00002822 case Type::InjectedClassName:
2823 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
2824 // fall through
2825
Douglas Gregor031a5882009-06-13 00:26:55 +00002826 case Type::TemplateSpecialization: {
Mike Stump1eb44332009-09-09 15:08:12 +00002827 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002828 = cast<TemplateSpecializationType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002829 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002830 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002831 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002832 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2833 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002834 break;
2835 }
2836
Douglas Gregore73bb602009-09-14 21:25:05 +00002837 case Type::Complex:
2838 if (!OnlyDeduced)
2839 MarkUsedTemplateParameters(SemaRef,
2840 cast<ComplexType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002841 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002842 break;
2843
Douglas Gregor4714c122010-03-31 17:34:00 +00002844 case Type::DependentName:
Douglas Gregore73bb602009-09-14 21:25:05 +00002845 if (!OnlyDeduced)
2846 MarkUsedTemplateParameters(SemaRef,
Douglas Gregor4714c122010-03-31 17:34:00 +00002847 cast<DependentNameType>(T)->getQualifier(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002848 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002849 break;
2850
John McCall33500952010-06-11 00:33:02 +00002851 case Type::DependentTemplateSpecialization: {
2852 const DependentTemplateSpecializationType *Spec
2853 = cast<DependentTemplateSpecializationType>(T);
2854 if (!OnlyDeduced)
2855 MarkUsedTemplateParameters(SemaRef, Spec->getQualifier(),
2856 OnlyDeduced, Depth, Used);
2857 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
2858 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2859 Used);
2860 break;
2861 }
2862
John McCallad5e7382010-03-01 23:49:17 +00002863 case Type::TypeOf:
2864 if (!OnlyDeduced)
2865 MarkUsedTemplateParameters(SemaRef,
2866 cast<TypeOfType>(T)->getUnderlyingType(),
2867 OnlyDeduced, Depth, Used);
2868 break;
2869
2870 case Type::TypeOfExpr:
2871 if (!OnlyDeduced)
2872 MarkUsedTemplateParameters(SemaRef,
2873 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
2874 OnlyDeduced, Depth, Used);
2875 break;
2876
2877 case Type::Decltype:
2878 if (!OnlyDeduced)
2879 MarkUsedTemplateParameters(SemaRef,
2880 cast<DecltypeType>(T)->getUnderlyingExpr(),
2881 OnlyDeduced, Depth, Used);
2882 break;
2883
Douglas Gregor7536dd52010-12-20 02:24:11 +00002884 case Type::PackExpansion:
2885 MarkUsedTemplateParameters(SemaRef,
2886 cast<PackExpansionType>(T)->getPattern(),
2887 OnlyDeduced, Depth, Used);
2888 break;
2889
Douglas Gregore73bb602009-09-14 21:25:05 +00002890 // None of these types have any template parameters in them.
Douglas Gregor031a5882009-06-13 00:26:55 +00002891 case Type::Builtin:
Douglas Gregor031a5882009-06-13 00:26:55 +00002892 case Type::VariableArray:
2893 case Type::FunctionNoProto:
2894 case Type::Record:
2895 case Type::Enum:
Douglas Gregor031a5882009-06-13 00:26:55 +00002896 case Type::ObjCInterface:
John McCallc12c5bb2010-05-15 11:32:37 +00002897 case Type::ObjCObject:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002898 case Type::ObjCObjectPointer:
John McCalled976492009-12-04 22:46:56 +00002899 case Type::UnresolvedUsing:
Douglas Gregor031a5882009-06-13 00:26:55 +00002900#define TYPE(Class, Base)
2901#define ABSTRACT_TYPE(Class, Base)
2902#define DEPENDENT_TYPE(Class, Base)
2903#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2904#include "clang/AST/TypeNodes.def"
2905 break;
2906 }
2907}
2908
Douglas Gregore73bb602009-09-14 21:25:05 +00002909/// \brief Mark the template parameters that are used by this
Douglas Gregor031a5882009-06-13 00:26:55 +00002910/// template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00002911static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002912MarkUsedTemplateParameters(Sema &SemaRef,
2913 const TemplateArgument &TemplateArg,
2914 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002915 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002916 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002917 switch (TemplateArg.getKind()) {
2918 case TemplateArgument::Null:
2919 case TemplateArgument::Integral:
Douglas Gregor788cd062009-11-11 01:00:40 +00002920 case TemplateArgument::Declaration:
Douglas Gregor031a5882009-06-13 00:26:55 +00002921 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002922
Douglas Gregor031a5882009-06-13 00:26:55 +00002923 case TemplateArgument::Type:
Douglas Gregore73bb602009-09-14 21:25:05 +00002924 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002925 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002926 break;
2927
Douglas Gregor788cd062009-11-11 01:00:40 +00002928 case TemplateArgument::Template:
2929 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2930 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002931 break;
2932
2933 case TemplateArgument::Expression:
Douglas Gregore73bb602009-09-14 21:25:05 +00002934 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002935 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002936 break;
Douglas Gregore73bb602009-09-14 21:25:05 +00002937
Anders Carlssond01b1da2009-06-15 17:04:53 +00002938 case TemplateArgument::Pack:
Douglas Gregore73bb602009-09-14 21:25:05 +00002939 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2940 PEnd = TemplateArg.pack_end();
2941 P != PEnd; ++P)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002942 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssond01b1da2009-06-15 17:04:53 +00002943 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00002944 }
2945}
2946
2947/// \brief Mark the template parameters can be deduced by the given
2948/// template argument list.
2949///
2950/// \param TemplateArgs the template argument list from which template
2951/// parameters will be deduced.
2952///
2953/// \param Deduced a bit vector whose elements will be set to \c true
2954/// to indicate when the corresponding template parameter will be
2955/// deduced.
Mike Stump1eb44332009-09-09 15:08:12 +00002956void
Douglas Gregore73bb602009-09-14 21:25:05 +00002957Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002958 bool OnlyDeduced, unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002959 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002960 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002961 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2962 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002963}
Douglas Gregor63f07c52009-09-18 23:21:38 +00002964
2965/// \brief Marks all of the template parameters that will be deduced by a
2966/// call to the given function template.
Douglas Gregor02024a92010-03-28 02:42:43 +00002967void
2968Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
2969 llvm::SmallVectorImpl<bool> &Deduced) {
Douglas Gregor63f07c52009-09-18 23:21:38 +00002970 TemplateParameterList *TemplateParams
2971 = FunctionTemplate->getTemplateParameters();
2972 Deduced.clear();
2973 Deduced.resize(TemplateParams->size());
2974
2975 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2976 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
2977 ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002978 true, TemplateParams->getDepth(), Deduced);
Douglas Gregor63f07c52009-09-18 23:21:38 +00002979}