blob: 80ba1a3dd6924a6547aa43acadb4c3a0e72e7712 [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 Gregor0972c862010-12-22 18:55:49 +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,
Douglas Gregor0972c862010-12-22 18:55:49 +000092 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
93 bool NumberOfArgumentsMustMatch = true);
Douglas Gregor20a55e22010-12-22 18:17:10 +000094
Douglas Gregor199d9912009-06-05 00:53:49 +000095/// \brief If the given expression is of a form that permits the deduction
96/// of a non-type template parameter, return the declaration of that
97/// non-type template parameter.
98static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
99 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
100 E = IC->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Douglas Gregor199d9912009-06-05 00:53:49 +0000102 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
103 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Douglas Gregor199d9912009-06-05 00:53:49 +0000105 return 0;
106}
107
Mike Stump1eb44332009-09-09 15:08:12 +0000108/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +0000109/// from the given constant.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000110static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000111DeduceNonTypeTemplateArgument(Sema &S,
Mike Stump1eb44332009-09-09 15:08:12 +0000112 NonTypeTemplateParmDecl *NTTP,
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000113 llvm::APSInt Value, QualType ValueType,
Douglas Gregor02024a92010-03-28 02:42:43 +0000114 bool DeducedFromArrayBound,
John McCall2a7fb272010-08-25 05:32:35 +0000115 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000116 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +0000117 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000118 "Cannot deduce non-type template argument with depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Douglas Gregor199d9912009-06-05 00:53:49 +0000120 if (Deduced[NTTP->getIndex()].isNull()) {
Douglas Gregor02024a92010-03-28 02:42:43 +0000121 Deduced[NTTP->getIndex()] = DeducedTemplateArgument(Value, ValueType,
122 DeducedFromArrayBound);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000123 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000124 }
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000126 if (Deduced[NTTP->getIndex()].getKind() != TemplateArgument::Integral) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000127 Info.Param = NTTP;
128 Info.FirstArg = Deduced[NTTP->getIndex()];
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000129 Info.SecondArg = TemplateArgument(Value, ValueType);
130 return Sema::TDK_Inconsistent;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000131 }
132
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000133 // Extent the smaller of the two values.
134 llvm::APSInt PrevValue = *Deduced[NTTP->getIndex()].getAsIntegral();
135 if (!hasSameExtendedValue(PrevValue, Value)) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000136 Info.Param = NTTP;
137 Info.FirstArg = Deduced[NTTP->getIndex()];
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000138 Info.SecondArg = TemplateArgument(Value, ValueType);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000139 return Sema::TDK_Inconsistent;
140 }
141
Douglas Gregor02024a92010-03-28 02:42:43 +0000142 if (!DeducedFromArrayBound)
143 Deduced[NTTP->getIndex()].setDeducedFromArrayBound(false);
144
Douglas Gregorf67875d2009-06-12 18:26:56 +0000145 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000146}
147
Mike Stump1eb44332009-09-09 15:08:12 +0000148/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +0000149/// from the given type- or value-dependent expression.
150///
151/// \returns true if deduction succeeded, false otherwise.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000152static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000153DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000154 NonTypeTemplateParmDecl *NTTP,
155 Expr *Value,
John McCall2a7fb272010-08-25 05:32:35 +0000156 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000157 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +0000158 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000159 "Cannot deduce non-type template argument with depth > 0");
160 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
161 "Expression template argument must be type- or value-dependent.");
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Douglas Gregor199d9912009-06-05 00:53:49 +0000163 if (Deduced[NTTP->getIndex()].isNull()) {
John McCall3fa5cae2010-10-26 07:05:15 +0000164 Deduced[NTTP->getIndex()] = TemplateArgument(Value);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000165 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000166 }
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Douglas Gregor199d9912009-06-05 00:53:49 +0000168 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
Mike Stump1eb44332009-09-09 15:08:12 +0000169 // Okay, we deduced a constant in one case and a dependent expression
170 // in another case. FIXME: Later, we will check that instantiating the
Douglas Gregor199d9912009-06-05 00:53:49 +0000171 // dependent expression gives us the constant value.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000172 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000173 }
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Douglas Gregor9eea08b2009-09-15 16:51:42 +0000175 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
176 // Compare the expressions for equality
177 llvm::FoldingSetNodeID ID1, ID2;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000178 Deduced[NTTP->getIndex()].getAsExpr()->Profile(ID1, S.Context, true);
179 Value->Profile(ID2, S.Context, true);
Douglas Gregor9eea08b2009-09-15 16:51:42 +0000180 if (ID1 == ID2)
181 return Sema::TDK_Success;
182
183 // FIXME: Fill in argument mismatch information
184 return Sema::TDK_NonDeducedMismatch;
185 }
186
Douglas Gregorf67875d2009-06-12 18:26:56 +0000187 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000188}
189
Douglas Gregor15755cb2009-11-13 23:45:44 +0000190/// \brief Deduce the value of the given non-type template parameter
191/// from the given declaration.
192///
193/// \returns true if deduction succeeded, false otherwise.
194static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000195DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregor15755cb2009-11-13 23:45:44 +0000196 NonTypeTemplateParmDecl *NTTP,
197 Decl *D,
John McCall2a7fb272010-08-25 05:32:35 +0000198 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000199 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor15755cb2009-11-13 23:45:44 +0000200 assert(NTTP->getDepth() == 0 &&
201 "Cannot deduce non-type template argument with depth > 0");
202
203 if (Deduced[NTTP->getIndex()].isNull()) {
204 Deduced[NTTP->getIndex()] = TemplateArgument(D->getCanonicalDecl());
205 return Sema::TDK_Success;
206 }
207
208 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
209 // Okay, we deduced a declaration in one case and a dependent expression
210 // in another case.
211 return Sema::TDK_Success;
212 }
213
214 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Declaration) {
215 // Compare the declarations for equality
216 if (Deduced[NTTP->getIndex()].getAsDecl()->getCanonicalDecl() ==
217 D->getCanonicalDecl())
218 return Sema::TDK_Success;
219
220 // FIXME: Fill in argument mismatch information
221 return Sema::TDK_NonDeducedMismatch;
222 }
223
224 return Sema::TDK_Success;
225}
226
Douglas Gregorf67875d2009-06-12 18:26:56 +0000227static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000228DeduceTemplateArguments(Sema &S,
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000229 TemplateParameterList *TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000230 TemplateName Param,
231 TemplateName Arg,
John McCall2a7fb272010-08-25 05:32:35 +0000232 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000233 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregord708c722009-06-09 16:35:58 +0000234 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000235 if (!ParamDecl) {
236 // The parameter type is dependent and is not a template template parameter,
237 // so there is nothing that we can deduce.
238 return Sema::TDK_Success;
239 }
240
241 if (TemplateTemplateParmDecl *TempParam
242 = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) {
243 // Bind the template template parameter to the given template name.
244 TemplateArgument &ExistingArg = Deduced[TempParam->getIndex()];
245 if (ExistingArg.isNull()) {
246 // This is the first deduction for this template template parameter.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000247 ExistingArg = TemplateArgument(S.Context.getCanonicalTemplateName(Arg));
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000248 return Sema::TDK_Success;
249 }
250
251 // Verify that the previous binding matches this deduction.
252 assert(ExistingArg.getKind() == TemplateArgument::Template);
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000253 if (S.Context.hasSameTemplateName(ExistingArg.getAsTemplate(), Arg))
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000254 return Sema::TDK_Success;
255
256 // Inconsistent deduction.
257 Info.Param = TempParam;
258 Info.FirstArg = ExistingArg;
259 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000260 return Sema::TDK_Inconsistent;
261 }
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000262
263 // Verify that the two template names are equivalent.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000264 if (S.Context.hasSameTemplateName(Param, Arg))
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000265 return Sema::TDK_Success;
266
267 // Mismatch of non-dependent template parameter to argument.
268 Info.FirstArg = TemplateArgument(Param);
269 Info.SecondArg = TemplateArgument(Arg);
270 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000271}
272
Mike Stump1eb44332009-09-09 15:08:12 +0000273/// \brief Deduce the template arguments by comparing the template parameter
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000274/// type (which is a template-id) with the template argument type.
275///
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000276/// \param S the Sema
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000277///
278/// \param TemplateParams the template parameters that we are deducing
279///
280/// \param Param the parameter type
281///
282/// \param Arg the argument type
283///
284/// \param Info information about the template argument deduction itself
285///
286/// \param Deduced the deduced template arguments
287///
288/// \returns the result of template argument deduction so far. Note that a
289/// "success" result means that template argument deduction has not yet failed,
290/// but it may still fail, later, for other reasons.
291static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000292DeduceTemplateArguments(Sema &S,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000293 TemplateParameterList *TemplateParams,
294 const TemplateSpecializationType *Param,
295 QualType Arg,
John McCall2a7fb272010-08-25 05:32:35 +0000296 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000297 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
John McCall467b27b2009-10-22 20:10:53 +0000298 assert(Arg.isCanonical() && "Argument type must be canonical");
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000300 // Check whether the template argument is a dependent template-id.
Mike Stump1eb44332009-09-09 15:08:12 +0000301 if (const TemplateSpecializationType *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000302 = dyn_cast<TemplateSpecializationType>(Arg)) {
303 // Perform template argument deduction for the template name.
304 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000305 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000306 Param->getTemplateName(),
307 SpecArg->getTemplateName(),
308 Info, Deduced))
309 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000312 // Perform template argument deduction on each template
Douglas Gregor0972c862010-12-22 18:55:49 +0000313 // argument. Ignore any missing/extra arguments, since they could be
314 // filled in by default arguments.
Douglas Gregor20a55e22010-12-22 18:17:10 +0000315 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor0972c862010-12-22 18:55:49 +0000316 Param->getArgs(), Param->getNumArgs(),
317 SpecArg->getArgs(), SpecArg->getNumArgs(),
318 Info, Deduced,
319 /*NumberOfArgumentsMustMatch=*/false);
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,
Douglas Gregor0972c862010-12-22 18:55:49 +0000956 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
957 bool NumberOfArgumentsMustMatch) {
Douglas Gregor20a55e22010-12-22 18:17:10 +0000958 unsigned ArgIdx = 0, ParamIdx = 0;
959 for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams);
960 ++ParamIdx) {
961 if (!Params[ParamIdx].isPackExpansion()) {
962 // The simple case: deduce template arguments by matching P and A.
963
964 // Check whether we have enough arguments.
965 if (!hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
Douglas Gregor0972c862010-12-22 18:55:49 +0000966 return NumberOfArgumentsMustMatch? Sema::TDK_TooFewArguments
967 : Sema::TDK_Success;
Douglas Gregor20a55e22010-12-22 18:17:10 +0000968
969 // Perform deduction for this P/A pair.
970 if (Sema::TemplateDeductionResult Result
971 = DeduceTemplateArguments(S, TemplateParams,
972 Params[ParamIdx], Args[ArgIdx],
973 Info, Deduced))
974 return Result;
975
976 // Move to the next argument.
977 ++ArgIdx;
978 continue;
979 }
980
981 // FIXME: Variadic templates.
982 // The parameter is a pack expansion, so we'll
983 // need to repeatedly unify arguments against the parameter, capturing
984 // the bindings for each expanded parameter pack.
985 S.Diag(Info.getLocation(), diag::err_pack_expansion_deduction);
986 return Sema::TDK_TooManyArguments;
987 }
988
989 // If there is an argument remaining, then we had too many arguments.
Douglas Gregor0972c862010-12-22 18:55:49 +0000990 if (NumberOfArgumentsMustMatch &&
991 hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
Douglas Gregor20a55e22010-12-22 18:17:10 +0000992 return Sema::TDK_TooManyArguments;
993
994 return Sema::TDK_Success;
995}
996
Mike Stump1eb44332009-09-09 15:08:12 +0000997static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000998DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000999 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001000 const TemplateArgumentList &ParamList,
1001 const TemplateArgumentList &ArgList,
John McCall2a7fb272010-08-25 05:32:35 +00001002 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +00001003 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor20a55e22010-12-22 18:17:10 +00001004 return DeduceTemplateArguments(S, TemplateParams,
1005 ParamList.data(), ParamList.size(),
1006 ArgList.data(), ArgList.size(),
1007 Info, Deduced);
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001008}
1009
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001010/// \brief Determine whether two template arguments are the same.
Mike Stump1eb44332009-09-09 15:08:12 +00001011static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001012 const TemplateArgument &X,
1013 const TemplateArgument &Y) {
1014 if (X.getKind() != Y.getKind())
1015 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001017 switch (X.getKind()) {
1018 case TemplateArgument::Null:
1019 assert(false && "Comparing NULL template argument");
1020 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001022 case TemplateArgument::Type:
1023 return Context.getCanonicalType(X.getAsType()) ==
1024 Context.getCanonicalType(Y.getAsType());
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001026 case TemplateArgument::Declaration:
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00001027 return X.getAsDecl()->getCanonicalDecl() ==
1028 Y.getAsDecl()->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Douglas Gregor788cd062009-11-11 01:00:40 +00001030 case TemplateArgument::Template:
1031 return Context.getCanonicalTemplateName(X.getAsTemplate())
1032 .getAsVoidPointer() ==
1033 Context.getCanonicalTemplateName(Y.getAsTemplate())
1034 .getAsVoidPointer();
1035
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001036 case TemplateArgument::Integral:
1037 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Douglas Gregor788cd062009-11-11 01:00:40 +00001039 case TemplateArgument::Expression: {
1040 llvm::FoldingSetNodeID XID, YID;
1041 X.getAsExpr()->Profile(XID, Context, true);
1042 Y.getAsExpr()->Profile(YID, Context, true);
1043 return XID == YID;
1044 }
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001046 case TemplateArgument::Pack:
1047 if (X.pack_size() != Y.pack_size())
1048 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001049
1050 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
1051 XPEnd = X.pack_end(),
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001052 YP = Y.pack_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001053 XP != XPEnd; ++XP, ++YP)
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001054 if (!isSameTemplateArg(Context, *XP, *YP))
1055 return false;
1056
1057 return true;
1058 }
1059
1060 return false;
1061}
1062
1063/// \brief Helper function to build a TemplateParameter when we don't
1064/// know its type statically.
1065static TemplateParameter makeTemplateParameter(Decl *D) {
1066 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
1067 return TemplateParameter(TTP);
1068 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
1069 return TemplateParameter(NTTP);
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001071 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
1072}
1073
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001074/// Complete template argument deduction for a class template partial
1075/// specialization.
1076static Sema::TemplateDeductionResult
1077FinishTemplateArgumentDeduction(Sema &S,
1078 ClassTemplatePartialSpecializationDecl *Partial,
1079 const TemplateArgumentList &TemplateArgs,
1080 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
John McCall2a7fb272010-08-25 05:32:35 +00001081 TemplateDeductionInfo &Info) {
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001082 // Trap errors.
1083 Sema::SFINAETrap Trap(S);
1084
1085 Sema::ContextRAII SavedContext(S, Partial);
1086
1087 // C++ [temp.deduct.type]p2:
1088 // [...] or if any template argument remains neither deduced nor
1089 // explicitly specified, template argument deduction fails.
Douglas Gregor910f8002010-11-07 23:05:16 +00001090 llvm::SmallVector<TemplateArgument, 4> Builder;
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001091 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
1092 if (Deduced[I].isNull()) {
1093 Decl *Param
Douglas Gregor3273b0c2010-10-12 18:51:08 +00001094 = const_cast<NamedDecl *>(
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001095 Partial->getTemplateParameters()->getParam(I));
1096 Info.Param = makeTemplateParameter(Param);
1097 return Sema::TDK_Incomplete;
1098 }
1099
Douglas Gregor910f8002010-11-07 23:05:16 +00001100 Builder.push_back(Deduced[I]);
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001101 }
1102
1103 // Form the template argument list from the deduced template arguments.
1104 TemplateArgumentList *DeducedArgumentList
Douglas Gregor910f8002010-11-07 23:05:16 +00001105 = TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
1106 Builder.size());
1107
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001108 Info.reset(DeducedArgumentList);
1109
1110 // Substitute the deduced template arguments into the template
1111 // arguments of the class template partial specialization, and
1112 // verify that the instantiated template arguments are both valid
1113 // and are equivalent to the template arguments originally provided
1114 // to the class template.
1115 // FIXME: Do we have to correct the types of deduced non-type template
1116 // arguments (in particular, integral non-type template arguments?).
John McCall2a7fb272010-08-25 05:32:35 +00001117 LocalInstantiationScope InstScope(S);
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001118 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
1119 const TemplateArgumentLoc *PartialTemplateArgs
1120 = Partial->getTemplateArgsAsWritten();
1121 unsigned N = Partial->getNumTemplateArgsAsWritten();
1122
1123 // Note that we don't provide the langle and rangle locations.
1124 TemplateArgumentListInfo InstArgs;
1125
1126 for (unsigned I = 0; I != N; ++I) {
1127 Decl *Param = const_cast<NamedDecl *>(
1128 ClassTemplate->getTemplateParameters()->getParam(I));
1129 TemplateArgumentLoc InstArg;
1130 if (S.Subst(PartialTemplateArgs[I], InstArg,
1131 MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
1132 Info.Param = makeTemplateParameter(Param);
1133 Info.FirstArg = PartialTemplateArgs[I].getArgument();
1134 return Sema::TDK_SubstitutionFailure;
1135 }
1136 InstArgs.addArgument(InstArg);
1137 }
1138
Douglas Gregor910f8002010-11-07 23:05:16 +00001139 llvm::SmallVector<TemplateArgument, 4> ConvertedInstArgs;
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001140 if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
Douglas Gregorec20f462010-05-08 20:07:26 +00001141 InstArgs, false, ConvertedInstArgs))
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001142 return Sema::TDK_SubstitutionFailure;
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001143
Douglas Gregor910f8002010-11-07 23:05:16 +00001144 for (unsigned I = 0, E = ConvertedInstArgs.size(); I != E; ++I) {
1145 TemplateArgument InstArg = ConvertedInstArgs.data()[I];
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001146
1147 Decl *Param = const_cast<NamedDecl *>(
1148 ClassTemplate->getTemplateParameters()->getParam(I));
1149
1150 if (InstArg.getKind() == TemplateArgument::Expression) {
1151 // When the argument is an expression, check the expression result
1152 // against the actual template parameter to get down to the canonical
1153 // template argument.
Douglas Gregor20a55e22010-12-22 18:17:10 +00001154 // FIXME: Variadic templates.
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001155 Expr *InstExpr = InstArg.getAsExpr();
1156 if (NonTypeTemplateParmDecl *NTTP
1157 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1158 if (S.CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
1159 Info.Param = makeTemplateParameter(Param);
1160 Info.FirstArg = Partial->getTemplateArgs()[I];
1161 return Sema::TDK_SubstitutionFailure;
1162 }
1163 }
1164 }
1165
1166 if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
1167 Info.Param = makeTemplateParameter(Param);
1168 Info.FirstArg = TemplateArgs[I];
1169 Info.SecondArg = InstArg;
1170 return Sema::TDK_NonDeducedMismatch;
1171 }
1172 }
1173
1174 if (Trap.hasErrorOccurred())
1175 return Sema::TDK_SubstitutionFailure;
1176
1177 return Sema::TDK_Success;
1178}
1179
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001180/// \brief Perform template argument deduction to determine whether
1181/// the given template arguments match the given class template
1182/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +00001183Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001184Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001185 const TemplateArgumentList &TemplateArgs,
1186 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001187 // C++ [temp.class.spec.match]p2:
1188 // A partial specialization matches a given actual template
1189 // argument list if the template arguments of the partial
1190 // specialization can be deduced from the actual template argument
1191 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +00001192 SFINAETrap Trap(*this);
Douglas Gregor02024a92010-03-28 02:42:43 +00001193 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001194 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +00001195 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001196 = ::DeduceTemplateArguments(*this,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001197 Partial->getTemplateParameters(),
Mike Stump1eb44332009-09-09 15:08:12 +00001198 Partial->getTemplateArgs(),
Douglas Gregorf67875d2009-06-12 18:26:56 +00001199 TemplateArgs, Info, Deduced))
1200 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +00001201
Douglas Gregor637a4092009-06-10 23:47:09 +00001202 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
Douglas Gregor9b623632010-10-12 23:32:35 +00001203 Deduced.data(), Deduced.size(), Info);
Douglas Gregor637a4092009-06-10 23:47:09 +00001204 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001205 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +00001206
Douglas Gregorbb260412009-06-14 08:02:22 +00001207 if (Trap.hasErrorOccurred())
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001208 return Sema::TDK_SubstitutionFailure;
1209
1210 return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
1211 Deduced, Info);
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001212}
Douglas Gregor031a5882009-06-13 00:26:55 +00001213
Douglas Gregor41128772009-06-26 23:27:24 +00001214/// \brief Determine whether the given type T is a simple-template-id type.
1215static bool isSimpleTemplateIdType(QualType T) {
Mike Stump1eb44332009-09-09 15:08:12 +00001216 if (const TemplateSpecializationType *Spec
John McCall183700f2009-09-21 23:43:11 +00001217 = T->getAs<TemplateSpecializationType>())
Douglas Gregor41128772009-06-26 23:27:24 +00001218 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001219
Douglas Gregor41128772009-06-26 23:27:24 +00001220 return false;
1221}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001222
1223/// \brief Substitute the explicitly-provided template arguments into the
1224/// given function template according to C++ [temp.arg.explicit].
1225///
1226/// \param FunctionTemplate the function template into which the explicit
1227/// template arguments will be substituted.
1228///
Mike Stump1eb44332009-09-09 15:08:12 +00001229/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001230/// arguments.
1231///
Mike Stump1eb44332009-09-09 15:08:12 +00001232/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor83314aa2009-07-08 20:55:45 +00001233/// with the converted and checked explicit template arguments.
1234///
Mike Stump1eb44332009-09-09 15:08:12 +00001235/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor83314aa2009-07-08 20:55:45 +00001236/// parameters.
1237///
1238/// \param FunctionType if non-NULL, the result type of the function template
1239/// will also be instantiated and the pointed-to value will be updated with
1240/// the instantiated function type.
1241///
1242/// \param Info if substitution fails for any reason, this object will be
1243/// populated with more information about the failure.
1244///
1245/// \returns TDK_Success if substitution was successful, or some failure
1246/// condition.
1247Sema::TemplateDeductionResult
1248Sema::SubstituteExplicitTemplateArguments(
1249 FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001250 const TemplateArgumentListInfo &ExplicitTemplateArgs,
Douglas Gregor02024a92010-03-28 02:42:43 +00001251 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001252 llvm::SmallVectorImpl<QualType> &ParamTypes,
1253 QualType *FunctionType,
1254 TemplateDeductionInfo &Info) {
1255 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1256 TemplateParameterList *TemplateParams
1257 = FunctionTemplate->getTemplateParameters();
1258
John McCalld5532b62009-11-23 01:53:49 +00001259 if (ExplicitTemplateArgs.size() == 0) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001260 // No arguments to substitute; just copy over the parameter types and
1261 // fill in the function type.
1262 for (FunctionDecl::param_iterator P = Function->param_begin(),
1263 PEnd = Function->param_end();
1264 P != PEnd;
1265 ++P)
1266 ParamTypes.push_back((*P)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Douglas Gregor83314aa2009-07-08 20:55:45 +00001268 if (FunctionType)
1269 *FunctionType = Function->getType();
1270 return TDK_Success;
1271 }
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Douglas Gregor83314aa2009-07-08 20:55:45 +00001273 // Substitution of the explicit template arguments into a function template
1274 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001275 SFINAETrap Trap(*this);
1276
Douglas Gregor83314aa2009-07-08 20:55:45 +00001277 // C++ [temp.arg.explicit]p3:
Mike Stump1eb44332009-09-09 15:08:12 +00001278 // Template arguments that are present shall be specified in the
1279 // declaration order of their corresponding template-parameters. The
Douglas Gregor83314aa2009-07-08 20:55:45 +00001280 // template argument list shall not specify more template-arguments than
Mike Stump1eb44332009-09-09 15:08:12 +00001281 // there are corresponding template-parameters.
Douglas Gregor910f8002010-11-07 23:05:16 +00001282 llvm::SmallVector<TemplateArgument, 4> Builder;
Mike Stump1eb44332009-09-09 15:08:12 +00001283
1284 // Enter a new template instantiation context where we check the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001285 // explicitly-specified template arguments against this function template,
1286 // and then substitute them into the function parameter types.
Mike Stump1eb44332009-09-09 15:08:12 +00001287 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001288 FunctionTemplate, Deduced.data(), Deduced.size(),
Douglas Gregor9b623632010-10-12 23:32:35 +00001289 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution,
1290 Info);
Douglas Gregor83314aa2009-07-08 20:55:45 +00001291 if (Inst)
1292 return TDK_InstantiationDepth;
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Douglas Gregor83314aa2009-07-08 20:55:45 +00001294 if (CheckTemplateArgumentList(FunctionTemplate,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001295 SourceLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001296 ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001297 true,
Douglas Gregorf1a84452010-05-08 19:15:54 +00001298 Builder) || Trap.hasErrorOccurred()) {
Douglas Gregor910f8002010-11-07 23:05:16 +00001299 unsigned Index = Builder.size();
Douglas Gregorfe52c912010-05-09 01:26:06 +00001300 if (Index >= TemplateParams->size())
1301 Index = TemplateParams->size() - 1;
1302 Info.Param = makeTemplateParameter(TemplateParams->getParam(Index));
Douglas Gregor83314aa2009-07-08 20:55:45 +00001303 return TDK_InvalidExplicitArguments;
Douglas Gregorf1a84452010-05-08 19:15:54 +00001304 }
Mike Stump1eb44332009-09-09 15:08:12 +00001305
Douglas Gregor83314aa2009-07-08 20:55:45 +00001306 // Form the template argument list from the explicitly-specified
1307 // template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001308 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor910f8002010-11-07 23:05:16 +00001309 = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001310 Info.reset(ExplicitArgumentList);
Mike Stump1eb44332009-09-09 15:08:12 +00001311
John McCalldf41f182010-10-12 19:40:14 +00001312 // Template argument deduction and the final substitution should be
1313 // done in the context of the templated declaration. Explicit
1314 // argument substitution, on the other hand, needs to happen in the
1315 // calling context.
1316 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
1317
Douglas Gregor83314aa2009-07-08 20:55:45 +00001318 // Instantiate the types of each of the function parameters given the
1319 // explicitly-specified template arguments.
1320 for (FunctionDecl::param_iterator P = Function->param_begin(),
1321 PEnd = Function->param_end();
1322 P != PEnd;
1323 ++P) {
Mike Stump1eb44332009-09-09 15:08:12 +00001324 QualType ParamType
1325 = SubstType((*P)->getType(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001326 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1327 (*P)->getLocation(), (*P)->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001328 if (ParamType.isNull() || Trap.hasErrorOccurred())
1329 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001330
Douglas Gregor83314aa2009-07-08 20:55:45 +00001331 ParamTypes.push_back(ParamType);
1332 }
1333
1334 // If the caller wants a full function type back, instantiate the return
1335 // type and form that function type.
1336 if (FunctionType) {
1337 // FIXME: exception-specifications?
Mike Stump1eb44332009-09-09 15:08:12 +00001338 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001339 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001340 assert(Proto && "Function template does not have a prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001341
1342 QualType ResultType
Douglas Gregor357bbd02009-08-28 20:50:45 +00001343 = SubstType(Proto->getResultType(),
1344 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1345 Function->getTypeSpecStartLoc(),
1346 Function->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001347 if (ResultType.isNull() || Trap.hasErrorOccurred())
1348 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001349
1350 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001351 ParamTypes.data(), ParamTypes.size(),
1352 Proto->isVariadic(),
1353 Proto->getTypeQuals(),
1354 Function->getLocation(),
Eli Friedmanfa869542010-08-05 02:54:05 +00001355 Function->getDeclName(),
1356 Proto->getExtInfo());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001357 if (FunctionType->isNull() || Trap.hasErrorOccurred())
1358 return TDK_SubstitutionFailure;
1359 }
Mike Stump1eb44332009-09-09 15:08:12 +00001360
Douglas Gregor83314aa2009-07-08 20:55:45 +00001361 // C++ [temp.arg.explicit]p2:
Mike Stump1eb44332009-09-09 15:08:12 +00001362 // Trailing template arguments that can be deduced (14.8.2) may be
1363 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001364 // template arguments can be deduced, they may all be omitted; in this
1365 // case, the empty template argument list <> itself may also be omitted.
1366 //
1367 // Take all of the explicitly-specified arguments and put them into the
Mike Stump1eb44332009-09-09 15:08:12 +00001368 // set of deduced template arguments.
Douglas Gregor20a55e22010-12-22 18:17:10 +00001369 //
1370 // FIXME: Variadic templates?
Douglas Gregor83314aa2009-07-08 20:55:45 +00001371 Deduced.reserve(TemplateParams->size());
1372 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001373 Deduced.push_back(ExplicitArgumentList->get(I));
1374
Douglas Gregor83314aa2009-07-08 20:55:45 +00001375 return TDK_Success;
1376}
1377
Douglas Gregor02024a92010-03-28 02:42:43 +00001378/// \brief Allocate a TemplateArgumentLoc where all locations have
1379/// been initialized to the given location.
1380///
1381/// \param S The semantic analysis object.
1382///
1383/// \param The template argument we are producing template argument
1384/// location information for.
1385///
1386/// \param NTTPType For a declaration template argument, the type of
1387/// the non-type template parameter that corresponds to this template
1388/// argument.
1389///
1390/// \param Loc The source location to use for the resulting template
1391/// argument.
1392static TemplateArgumentLoc
1393getTrivialTemplateArgumentLoc(Sema &S,
1394 const TemplateArgument &Arg,
1395 QualType NTTPType,
1396 SourceLocation Loc) {
1397 switch (Arg.getKind()) {
1398 case TemplateArgument::Null:
1399 llvm_unreachable("Can't get a NULL template argument here");
1400 break;
1401
1402 case TemplateArgument::Type:
1403 return TemplateArgumentLoc(Arg,
1404 S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
1405
1406 case TemplateArgument::Declaration: {
1407 Expr *E
1408 = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
1409 .takeAs<Expr>();
1410 return TemplateArgumentLoc(TemplateArgument(E), E);
1411 }
1412
1413 case TemplateArgument::Integral: {
1414 Expr *E
1415 = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>();
1416 return TemplateArgumentLoc(TemplateArgument(E), E);
1417 }
1418
1419 case TemplateArgument::Template:
1420 return TemplateArgumentLoc(Arg, SourceRange(), Loc);
1421
1422 case TemplateArgument::Expression:
1423 return TemplateArgumentLoc(Arg, Arg.getAsExpr());
1424
1425 case TemplateArgument::Pack:
Douglas Gregor87dd6972010-12-20 16:52:59 +00001426 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo());
Douglas Gregor02024a92010-03-28 02:42:43 +00001427 }
1428
1429 return TemplateArgumentLoc();
1430}
1431
Mike Stump1eb44332009-09-09 15:08:12 +00001432/// \brief Finish template argument deduction for a function template,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001433/// checking the deduced template arguments for completeness and forming
1434/// the function template specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00001435Sema::TemplateDeductionResult
Douglas Gregor83314aa2009-07-08 20:55:45 +00001436Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor02024a92010-03-28 02:42:43 +00001437 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1438 unsigned NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001439 FunctionDecl *&Specialization,
1440 TemplateDeductionInfo &Info) {
1441 TemplateParameterList *TemplateParams
1442 = FunctionTemplate->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Douglas Gregor83314aa2009-07-08 20:55:45 +00001444 // Template argument deduction for function templates in a SFINAE context.
1445 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001446 SFINAETrap Trap(*this);
1447
Douglas Gregor83314aa2009-07-08 20:55:45 +00001448 // Enter a new template instantiation context while we instantiate the
1449 // actual function declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001450 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001451 FunctionTemplate, Deduced.data(), Deduced.size(),
Douglas Gregor9b623632010-10-12 23:32:35 +00001452 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution,
1453 Info);
Douglas Gregor83314aa2009-07-08 20:55:45 +00001454 if (Inst)
Mike Stump1eb44332009-09-09 15:08:12 +00001455 return TDK_InstantiationDepth;
1456
John McCall96db3102010-04-29 01:18:58 +00001457 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
John McCallf5813822010-04-29 00:35:03 +00001458
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001459 // C++ [temp.deduct.type]p2:
1460 // [...] or if any template argument remains neither deduced nor
1461 // explicitly specified, template argument deduction fails.
Douglas Gregor910f8002010-11-07 23:05:16 +00001462 llvm::SmallVector<TemplateArgument, 4> Builder;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001463 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregor20a55e22010-12-22 18:17:10 +00001464 // FIXME: Variadic templates. Unwrap argument packs?
Douglas Gregor02024a92010-03-28 02:42:43 +00001465 NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001466 if (!Deduced[I].isNull()) {
Douglas Gregor3273b0c2010-10-12 18:51:08 +00001467 if (I < NumExplicitlySpecified) {
Douglas Gregor02024a92010-03-28 02:42:43 +00001468 // We have already fully type-checked and converted this
Douglas Gregor3273b0c2010-10-12 18:51:08 +00001469 // argument, because it was explicitly-specified. Just record the
1470 // presence of this argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00001471 Builder.push_back(Deduced[I]);
Douglas Gregor02024a92010-03-28 02:42:43 +00001472 continue;
1473 }
1474
1475 // We have deduced this argument, so it still needs to be
1476 // checked and converted.
1477
1478 // First, for a non-type template parameter type that is
1479 // initialized by a declaration, we need the type of the
1480 // corresponding non-type template parameter.
1481 QualType NTTPType;
1482 if (NonTypeTemplateParmDecl *NTTP
1483 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1484 if (Deduced[I].getKind() == TemplateArgument::Declaration) {
1485 NTTPType = NTTP->getType();
1486 if (NTTPType->isDependentType()) {
Douglas Gregor910f8002010-11-07 23:05:16 +00001487 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1488 Builder.data(), Builder.size());
Douglas Gregor02024a92010-03-28 02:42:43 +00001489 NTTPType = SubstType(NTTPType,
1490 MultiLevelTemplateArgumentList(TemplateArgs),
1491 NTTP->getLocation(),
1492 NTTP->getDeclName());
1493 if (NTTPType.isNull()) {
1494 Info.Param = makeTemplateParameter(Param);
Douglas Gregor910f8002010-11-07 23:05:16 +00001495 // FIXME: These template arguments are temporary. Free them!
1496 Info.reset(TemplateArgumentList::CreateCopy(Context,
1497 Builder.data(),
1498 Builder.size()));
Douglas Gregor02024a92010-03-28 02:42:43 +00001499 return TDK_SubstitutionFailure;
1500 }
1501 }
1502 }
1503 }
1504
1505 // Convert the deduced template argument into a template
1506 // argument that we can check, almost as if the user had written
1507 // the template argument explicitly.
1508 TemplateArgumentLoc Arg = getTrivialTemplateArgumentLoc(*this,
1509 Deduced[I],
1510 NTTPType,
Douglas Gregor9b623632010-10-12 23:32:35 +00001511 Info.getLocation());
Douglas Gregor02024a92010-03-28 02:42:43 +00001512
1513 // Check the template argument, converting it as necessary.
1514 if (CheckTemplateArgument(Param, Arg,
1515 FunctionTemplate,
1516 FunctionTemplate->getLocation(),
1517 FunctionTemplate->getSourceRange().getEnd(),
1518 Builder,
1519 Deduced[I].wasDeducedFromArrayBound()
1520 ? CTAK_DeducedFromArrayBound
1521 : CTAK_Deduced)) {
1522 Info.Param = makeTemplateParameter(
1523 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregor910f8002010-11-07 23:05:16 +00001524 // FIXME: These template arguments are temporary. Free them!
1525 Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
1526 Builder.size()));
Douglas Gregor02024a92010-03-28 02:42:43 +00001527 return TDK_SubstitutionFailure;
1528 }
1529
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001530 continue;
1531 }
1532
1533 // Substitute into the default template argument, if available.
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001534 TemplateArgumentLoc DefArg
1535 = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
1536 FunctionTemplate->getLocation(),
1537 FunctionTemplate->getSourceRange().getEnd(),
1538 Param,
1539 Builder);
1540
1541 // If there was no default argument, deduction is incomplete.
1542 if (DefArg.getArgument().isNull()) {
1543 Info.Param = makeTemplateParameter(
1544 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1545 return TDK_Incomplete;
1546 }
1547
1548 // Check whether we can actually use the default argument.
1549 if (CheckTemplateArgument(Param, DefArg,
1550 FunctionTemplate,
1551 FunctionTemplate->getLocation(),
1552 FunctionTemplate->getSourceRange().getEnd(),
Douglas Gregor02024a92010-03-28 02:42:43 +00001553 Builder,
1554 CTAK_Deduced)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001555 Info.Param = makeTemplateParameter(
1556 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregor910f8002010-11-07 23:05:16 +00001557 // FIXME: These template arguments are temporary. Free them!
1558 Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
1559 Builder.size()));
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001560 return TDK_SubstitutionFailure;
1561 }
1562
1563 // If we get here, we successfully used the default template argument.
1564 }
1565
1566 // Form the template argument list from the deduced template arguments.
1567 TemplateArgumentList *DeducedArgumentList
Douglas Gregor910f8002010-11-07 23:05:16 +00001568 = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001569 Info.reset(DeducedArgumentList);
1570
Mike Stump1eb44332009-09-09 15:08:12 +00001571 // Substitute the deduced template arguments into the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001572 // declaration to produce the function template specialization.
Douglas Gregord4598a22010-04-28 04:52:24 +00001573 DeclContext *Owner = FunctionTemplate->getDeclContext();
1574 if (FunctionTemplate->getFriendObjectKind())
1575 Owner = FunctionTemplate->getLexicalDeclContext();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001576 Specialization = cast_or_null<FunctionDecl>(
Douglas Gregord4598a22010-04-28 04:52:24 +00001577 SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner,
Douglas Gregor357bbd02009-08-28 20:50:45 +00001578 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor83314aa2009-07-08 20:55:45 +00001579 if (!Specialization)
1580 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001581
Douglas Gregorf8825742009-09-15 18:26:13 +00001582 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
1583 FunctionTemplate->getCanonicalDecl());
1584
Mike Stump1eb44332009-09-09 15:08:12 +00001585 // If the template argument list is owned by the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001586 // specialization, release it.
Douglas Gregorec20f462010-05-08 20:07:26 +00001587 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList &&
1588 !Trap.hasErrorOccurred())
Douglas Gregor83314aa2009-07-08 20:55:45 +00001589 Info.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001590
Douglas Gregor83314aa2009-07-08 20:55:45 +00001591 // There may have been an error that did not prevent us from constructing a
1592 // declaration. Mark the declaration invalid and return with a substitution
1593 // failure.
1594 if (Trap.hasErrorOccurred()) {
1595 Specialization->setInvalidDecl(true);
1596 return TDK_SubstitutionFailure;
1597 }
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Douglas Gregor9b623632010-10-12 23:32:35 +00001599 // If we suppressed any diagnostics while performing template argument
1600 // deduction, and if we haven't already instantiated this declaration,
1601 // keep track of these diagnostics. They'll be emitted if this specialization
1602 // is actually used.
1603 if (Info.diag_begin() != Info.diag_end()) {
1604 llvm::DenseMap<Decl *, llvm::SmallVector<PartialDiagnosticAt, 1> >::iterator
1605 Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl());
1606 if (Pos == SuppressedDiagnostics.end())
1607 SuppressedDiagnostics[Specialization->getCanonicalDecl()]
1608 .append(Info.diag_begin(), Info.diag_end());
1609 }
1610
Mike Stump1eb44332009-09-09 15:08:12 +00001611 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001612}
1613
John McCall9c72c602010-08-27 09:08:28 +00001614/// Gets the type of a function for template-argument-deducton
1615/// purposes when it's considered as part of an overload set.
John McCalleff92132010-02-02 02:21:27 +00001616static QualType GetTypeOfFunction(ASTContext &Context,
John McCall9c72c602010-08-27 09:08:28 +00001617 const OverloadExpr::FindResult &R,
John McCalleff92132010-02-02 02:21:27 +00001618 FunctionDecl *Fn) {
John McCalleff92132010-02-02 02:21:27 +00001619 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
John McCall9c72c602010-08-27 09:08:28 +00001620 if (Method->isInstance()) {
1621 // An instance method that's referenced in a form that doesn't
1622 // look like a member pointer is just invalid.
1623 if (!R.HasFormOfMemberPointer) return QualType();
1624
John McCalleff92132010-02-02 02:21:27 +00001625 return Context.getMemberPointerType(Fn->getType(),
1626 Context.getTypeDeclType(Method->getParent()).getTypePtr());
John McCall9c72c602010-08-27 09:08:28 +00001627 }
1628
1629 if (!R.IsAddressOfOperand) return Fn->getType();
John McCalleff92132010-02-02 02:21:27 +00001630 return Context.getPointerType(Fn->getType());
1631}
1632
1633/// Apply the deduction rules for overload sets.
1634///
1635/// \return the null type if this argument should be treated as an
1636/// undeduced context
1637static QualType
1638ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
Douglas Gregor75f21af2010-08-30 21:04:23 +00001639 Expr *Arg, QualType ParamType,
1640 bool ParamWasReference) {
John McCall9c72c602010-08-27 09:08:28 +00001641
1642 OverloadExpr::FindResult R = OverloadExpr::find(Arg);
John McCalleff92132010-02-02 02:21:27 +00001643
John McCall9c72c602010-08-27 09:08:28 +00001644 OverloadExpr *Ovl = R.Expression;
John McCalleff92132010-02-02 02:21:27 +00001645
Douglas Gregor75f21af2010-08-30 21:04:23 +00001646 // C++0x [temp.deduct.call]p4
1647 unsigned TDF = 0;
1648 if (ParamWasReference)
1649 TDF |= TDF_ParamWithReferenceType;
1650 if (R.IsAddressOfOperand)
1651 TDF |= TDF_IgnoreQualifiers;
1652
John McCalleff92132010-02-02 02:21:27 +00001653 // If there were explicit template arguments, we can only find
1654 // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
1655 // unambiguously name a full specialization.
John McCall7bb12da2010-02-02 06:20:04 +00001656 if (Ovl->hasExplicitTemplateArgs()) {
John McCalleff92132010-02-02 02:21:27 +00001657 // But we can still look for an explicit specialization.
1658 if (FunctionDecl *ExplicitSpec
John McCall7bb12da2010-02-02 06:20:04 +00001659 = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
John McCall9c72c602010-08-27 09:08:28 +00001660 return GetTypeOfFunction(S.Context, R, ExplicitSpec);
John McCalleff92132010-02-02 02:21:27 +00001661 return QualType();
1662 }
1663
1664 // C++0x [temp.deduct.call]p6:
1665 // When P is a function type, pointer to function type, or pointer
1666 // to member function type:
1667
1668 if (!ParamType->isFunctionType() &&
1669 !ParamType->isFunctionPointerType() &&
1670 !ParamType->isMemberFunctionPointerType())
1671 return QualType();
1672
1673 QualType Match;
John McCall7bb12da2010-02-02 06:20:04 +00001674 for (UnresolvedSetIterator I = Ovl->decls_begin(),
1675 E = Ovl->decls_end(); I != E; ++I) {
John McCalleff92132010-02-02 02:21:27 +00001676 NamedDecl *D = (*I)->getUnderlyingDecl();
1677
1678 // - If the argument is an overload set containing one or more
1679 // function templates, the parameter is treated as a
1680 // non-deduced context.
1681 if (isa<FunctionTemplateDecl>(D))
1682 return QualType();
1683
1684 FunctionDecl *Fn = cast<FunctionDecl>(D);
John McCall9c72c602010-08-27 09:08:28 +00001685 QualType ArgType = GetTypeOfFunction(S.Context, R, Fn);
1686 if (ArgType.isNull()) continue;
John McCalleff92132010-02-02 02:21:27 +00001687
Douglas Gregor75f21af2010-08-30 21:04:23 +00001688 // Function-to-pointer conversion.
1689 if (!ParamWasReference && ParamType->isPointerType() &&
1690 ArgType->isFunctionType())
1691 ArgType = S.Context.getPointerType(ArgType);
1692
John McCalleff92132010-02-02 02:21:27 +00001693 // - If the argument is an overload set (not containing function
1694 // templates), trial argument deduction is attempted using each
1695 // of the members of the set. If deduction succeeds for only one
1696 // of the overload set members, that member is used as the
1697 // argument value for the deduction. If deduction succeeds for
1698 // more than one member of the overload set the parameter is
1699 // treated as a non-deduced context.
1700
1701 // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
1702 // Type deduction is done independently for each P/A pair, and
1703 // the deduced template argument values are then combined.
1704 // So we do not reject deductions which were made elsewhere.
Douglas Gregor02024a92010-03-28 02:42:43 +00001705 llvm::SmallVector<DeducedTemplateArgument, 8>
1706 Deduced(TemplateParams->size());
John McCall2a7fb272010-08-25 05:32:35 +00001707 TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
John McCalleff92132010-02-02 02:21:27 +00001708 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001709 = DeduceTemplateArguments(S, TemplateParams,
John McCalleff92132010-02-02 02:21:27 +00001710 ParamType, ArgType,
1711 Info, Deduced, TDF);
1712 if (Result) continue;
1713 if (!Match.isNull()) return QualType();
1714 Match = ArgType;
1715 }
1716
1717 return Match;
1718}
1719
Douglas Gregore53060f2009-06-25 22:08:12 +00001720/// \brief Perform template argument deduction from a function call
1721/// (C++ [temp.deduct.call]).
1722///
1723/// \param FunctionTemplate the function template for which we are performing
1724/// template argument deduction.
1725///
Douglas Gregor48026d22010-01-11 18:40:55 +00001726/// \param ExplicitTemplateArguments the explicit template arguments provided
1727/// for this call.
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001728///
Douglas Gregore53060f2009-06-25 22:08:12 +00001729/// \param Args the function call arguments
1730///
1731/// \param NumArgs the number of arguments in Args
1732///
Douglas Gregor48026d22010-01-11 18:40:55 +00001733/// \param Name the name of the function being called. This is only significant
1734/// when the function template is a conversion function template, in which
1735/// case this routine will also perform template argument deduction based on
1736/// the function to which
1737///
Douglas Gregore53060f2009-06-25 22:08:12 +00001738/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001739/// this will be set to the function template specialization produced by
Douglas Gregore53060f2009-06-25 22:08:12 +00001740/// template argument deduction.
1741///
1742/// \param Info the argument will be updated to provide additional information
1743/// about template argument deduction.
1744///
1745/// \returns the result of template argument deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001746Sema::TemplateDeductionResult
1747Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor48026d22010-01-11 18:40:55 +00001748 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregore53060f2009-06-25 22:08:12 +00001749 Expr **Args, unsigned NumArgs,
1750 FunctionDecl *&Specialization,
1751 TemplateDeductionInfo &Info) {
1752 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001753
Douglas Gregore53060f2009-06-25 22:08:12 +00001754 // C++ [temp.deduct.call]p1:
1755 // Template argument deduction is done by comparing each function template
1756 // parameter type (call it P) with the type of the corresponding argument
1757 // of the call (call it A) as described below.
1758 unsigned CheckArgs = NumArgs;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001759 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregore53060f2009-06-25 22:08:12 +00001760 return TDK_TooFewArguments;
1761 else if (NumArgs > Function->getNumParams()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001762 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001763 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregore53060f2009-06-25 22:08:12 +00001764 if (!Proto->isVariadic())
1765 return TDK_TooManyArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Douglas Gregore53060f2009-06-25 22:08:12 +00001767 CheckArgs = Function->getNumParams();
1768 }
Mike Stump1eb44332009-09-09 15:08:12 +00001769
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001770 // The types of the parameters from which we will perform template argument
1771 // deduction.
John McCall2a7fb272010-08-25 05:32:35 +00001772 LocalInstantiationScope InstScope(*this);
Douglas Gregore53060f2009-06-25 22:08:12 +00001773 TemplateParameterList *TemplateParams
1774 = FunctionTemplate->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00001775 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001776 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregor02024a92010-03-28 02:42:43 +00001777 unsigned NumExplicitlySpecified = 0;
John McCalld5532b62009-11-23 01:53:49 +00001778 if (ExplicitTemplateArgs) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001779 TemplateDeductionResult Result =
1780 SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001781 *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001782 Deduced,
1783 ParamTypes,
1784 0,
1785 Info);
1786 if (Result)
1787 return Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00001788
1789 NumExplicitlySpecified = Deduced.size();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001790 } else {
1791 // Just fill in the parameter types from the function declaration.
1792 for (unsigned I = 0; I != CheckArgs; ++I)
1793 ParamTypes.push_back(Function->getParamDecl(I)->getType());
1794 }
Mike Stump1eb44332009-09-09 15:08:12 +00001795
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001796 // Deduce template arguments from the function parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001797 Deduced.resize(TemplateParams->size());
Douglas Gregore53060f2009-06-25 22:08:12 +00001798 for (unsigned I = 0; I != CheckArgs; ++I) {
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001799 QualType ParamType = ParamTypes[I];
Douglas Gregore53060f2009-06-25 22:08:12 +00001800 QualType ArgType = Args[I]->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001801
Douglas Gregor75f21af2010-08-30 21:04:23 +00001802 // C++0x [temp.deduct.call]p3:
1803 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
1804 // are ignored for type deduction.
1805 if (ParamType.getCVRQualifiers())
1806 ParamType = ParamType.getLocalUnqualifiedType();
1807 const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>();
1808 if (ParamRefType) {
1809 // [...] If P is a reference type, the type referred to by P is used
1810 // for type deduction.
1811 ParamType = ParamRefType->getPointeeType();
1812 }
1813
John McCalleff92132010-02-02 02:21:27 +00001814 // Overload sets usually make this parameter an undeduced
1815 // context, but there are sometimes special circumstances.
1816 if (ArgType == Context.OverloadTy) {
1817 ArgType = ResolveOverloadForDeduction(*this, TemplateParams,
Douglas Gregor75f21af2010-08-30 21:04:23 +00001818 Args[I], ParamType,
1819 ParamRefType != 0);
John McCalleff92132010-02-02 02:21:27 +00001820 if (ArgType.isNull())
1821 continue;
1822 }
1823
Douglas Gregor75f21af2010-08-30 21:04:23 +00001824 if (ParamRefType) {
1825 // C++0x [temp.deduct.call]p3:
1826 // [...] If P is of the form T&&, where T is a template parameter, and
1827 // the argument is an lvalue, the type A& is used in place of A for
1828 // type deduction.
1829 if (ParamRefType->isRValueReferenceType() &&
1830 ParamRefType->getAs<TemplateTypeParmType>() &&
John McCall7eb0a9e2010-11-24 05:12:34 +00001831 Args[I]->isLValue())
Douglas Gregor75f21af2010-08-30 21:04:23 +00001832 ArgType = Context.getLValueReferenceType(ArgType);
1833 } else {
1834 // C++ [temp.deduct.call]p2:
1835 // If P is not a reference type:
Mike Stump1eb44332009-09-09 15:08:12 +00001836 // - If A is an array type, the pointer type produced by the
1837 // array-to-pointer standard conversion (4.2) is used in place of
Douglas Gregore53060f2009-06-25 22:08:12 +00001838 // A for type deduction; otherwise,
1839 if (ArgType->isArrayType())
1840 ArgType = Context.getArrayDecayedType(ArgType);
Mike Stump1eb44332009-09-09 15:08:12 +00001841 // - If A is a function type, the pointer type produced by the
1842 // function-to-pointer standard conversion (4.3) is used in place
Douglas Gregore53060f2009-06-25 22:08:12 +00001843 // of A for type deduction; otherwise,
1844 else if (ArgType->isFunctionType())
1845 ArgType = Context.getPointerType(ArgType);
1846 else {
1847 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1848 // type are ignored for type deduction.
1849 QualType CanonArgType = Context.getCanonicalType(ArgType);
Douglas Gregor75f21af2010-08-30 21:04:23 +00001850 if (ArgType.getCVRQualifiers())
1851 ArgType = ArgType.getUnqualifiedType();
Douglas Gregore53060f2009-06-25 22:08:12 +00001852 }
1853 }
Mike Stump1eb44332009-09-09 15:08:12 +00001854
Douglas Gregore53060f2009-06-25 22:08:12 +00001855 // C++0x [temp.deduct.call]p4:
1856 // In general, the deduction process attempts to find template argument
1857 // values that will make the deduced A identical to A (after the type A
1858 // is transformed as described above). [...]
Douglas Gregor12820292009-09-14 20:00:47 +00001859 unsigned TDF = TDF_SkipNonDependent;
Mike Stump1eb44332009-09-09 15:08:12 +00001860
Douglas Gregor508f1c82009-06-26 23:10:12 +00001861 // - If the original P is a reference type, the deduced A (i.e., the
1862 // type referred to by the reference) can be more cv-qualified than
1863 // the transformed A.
Douglas Gregor75f21af2010-08-30 21:04:23 +00001864 if (ParamRefType)
Douglas Gregor508f1c82009-06-26 23:10:12 +00001865 TDF |= TDF_ParamWithReferenceType;
Mike Stump1eb44332009-09-09 15:08:12 +00001866 // - The transformed A can be another pointer or pointer to member
1867 // type that can be converted to the deduced A via a qualification
Douglas Gregor508f1c82009-06-26 23:10:12 +00001868 // conversion (4.4).
John McCalldb0bc472010-08-05 05:30:45 +00001869 if (ArgType->isPointerType() || ArgType->isMemberPointerType() ||
1870 ArgType->isObjCObjectPointerType())
Douglas Gregor508f1c82009-06-26 23:10:12 +00001871 TDF |= TDF_IgnoreQualifiers;
Mike Stump1eb44332009-09-09 15:08:12 +00001872 // - If P is a class and P has the form simple-template-id, then the
Douglas Gregor41128772009-06-26 23:27:24 +00001873 // transformed A can be a derived class of the deduced A. Likewise,
1874 // if P is a pointer to a class of the form simple-template-id, the
1875 // transformed A can be a pointer to a derived class pointed to by
1876 // the deduced A.
1877 if (isSimpleTemplateIdType(ParamType) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001878 (isa<PointerType>(ParamType) &&
Douglas Gregor41128772009-06-26 23:27:24 +00001879 isSimpleTemplateIdType(
Ted Kremenek6217b802009-07-29 21:53:49 +00001880 ParamType->getAs<PointerType>()->getPointeeType())))
Douglas Gregor41128772009-06-26 23:27:24 +00001881 TDF |= TDF_DerivedClass;
Mike Stump1eb44332009-09-09 15:08:12 +00001882
Douglas Gregore53060f2009-06-25 22:08:12 +00001883 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001884 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor500d3312009-06-26 18:27:22 +00001885 ParamType, ArgType, Info, Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +00001886 TDF))
Douglas Gregore53060f2009-06-25 22:08:12 +00001887 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001888
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001889 // FIXME: we need to check that the deduced A is the same as A,
1890 // modulo the various allowed differences.
Douglas Gregore53060f2009-06-25 22:08:12 +00001891 }
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001892
Mike Stump1eb44332009-09-09 15:08:12 +00001893 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor02024a92010-03-28 02:42:43 +00001894 NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001895 Specialization, Info);
Douglas Gregore53060f2009-06-25 22:08:12 +00001896}
1897
Douglas Gregor83314aa2009-07-08 20:55:45 +00001898/// \brief Deduce template arguments when taking the address of a function
Douglas Gregor4b52e252009-12-21 23:17:24 +00001899/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
1900/// a template.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001901///
1902/// \param FunctionTemplate the function template for which we are performing
1903/// template argument deduction.
1904///
Douglas Gregor4b52e252009-12-21 23:17:24 +00001905/// \param ExplicitTemplateArguments the explicitly-specified template
1906/// arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001907///
1908/// \param ArgFunctionType the function type that will be used as the
1909/// "argument" type (A) when performing template argument deduction from the
Douglas Gregor4b52e252009-12-21 23:17:24 +00001910/// function template's function type. This type may be NULL, if there is no
1911/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001912///
1913/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001914/// this will be set to the function template specialization produced by
Douglas Gregor83314aa2009-07-08 20:55:45 +00001915/// template argument deduction.
1916///
1917/// \param Info the argument will be updated to provide additional information
1918/// about template argument deduction.
1919///
1920/// \returns the result of template argument deduction.
1921Sema::TemplateDeductionResult
1922Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001923 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001924 QualType ArgFunctionType,
1925 FunctionDecl *&Specialization,
1926 TemplateDeductionInfo &Info) {
1927 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1928 TemplateParameterList *TemplateParams
1929 = FunctionTemplate->getTemplateParameters();
1930 QualType FunctionType = Function->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001931
Douglas Gregor83314aa2009-07-08 20:55:45 +00001932 // Substitute any explicit template arguments.
John McCall2a7fb272010-08-25 05:32:35 +00001933 LocalInstantiationScope InstScope(*this);
Douglas Gregor02024a92010-03-28 02:42:43 +00001934 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
1935 unsigned NumExplicitlySpecified = 0;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001936 llvm::SmallVector<QualType, 4> ParamTypes;
John McCalld5532b62009-11-23 01:53:49 +00001937 if (ExplicitTemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001938 if (TemplateDeductionResult Result
1939 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001940 *ExplicitTemplateArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00001941 Deduced, ParamTypes,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001942 &FunctionType, Info))
1943 return Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00001944
1945 NumExplicitlySpecified = Deduced.size();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001946 }
1947
1948 // Template argument deduction for function templates in a SFINAE context.
1949 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001950 SFINAETrap Trap(*this);
1951
John McCalleff92132010-02-02 02:21:27 +00001952 Deduced.resize(TemplateParams->size());
1953
Douglas Gregor4b52e252009-12-21 23:17:24 +00001954 if (!ArgFunctionType.isNull()) {
1955 // Deduce template arguments from the function type.
Douglas Gregor4b52e252009-12-21 23:17:24 +00001956 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001957 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor4b52e252009-12-21 23:17:24 +00001958 FunctionType, ArgFunctionType, Info,
1959 Deduced, 0))
1960 return Result;
1961 }
Douglas Gregorfbb6fad2010-09-29 21:14:36 +00001962
1963 if (TemplateDeductionResult Result
1964 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
1965 NumExplicitlySpecified,
1966 Specialization, Info))
1967 return Result;
1968
1969 // If the requested function type does not match the actual type of the
1970 // specialization, template argument deduction fails.
1971 if (!ArgFunctionType.isNull() &&
1972 !Context.hasSameType(ArgFunctionType, Specialization->getType()))
1973 return TDK_NonDeducedMismatch;
1974
1975 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001976}
1977
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001978/// \brief Deduce template arguments for a templated conversion
1979/// function (C++ [temp.deduct.conv]) and, if successful, produce a
1980/// conversion function template specialization.
1981Sema::TemplateDeductionResult
1982Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1983 QualType ToType,
1984 CXXConversionDecl *&Specialization,
1985 TemplateDeductionInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00001986 CXXConversionDecl *Conv
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001987 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
1988 QualType FromType = Conv->getConversionType();
1989
1990 // Canonicalize the types for deduction.
1991 QualType P = Context.getCanonicalType(FromType);
1992 QualType A = Context.getCanonicalType(ToType);
1993
1994 // C++0x [temp.deduct.conv]p3:
1995 // If P is a reference type, the type referred to by P is used for
1996 // type deduction.
1997 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
1998 P = PRef->getPointeeType();
1999
2000 // C++0x [temp.deduct.conv]p3:
2001 // If A is a reference type, the type referred to by A is used
2002 // for type deduction.
2003 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
2004 A = ARef->getPointeeType();
2005 // C++ [temp.deduct.conv]p2:
2006 //
Mike Stump1eb44332009-09-09 15:08:12 +00002007 // If A is not a reference type:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002008 else {
2009 assert(!A->isReferenceType() && "Reference types were handled above");
2010
2011 // - If P is an array type, the pointer type produced by the
Mike Stump1eb44332009-09-09 15:08:12 +00002012 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002013 // of P for type deduction; otherwise,
2014 if (P->isArrayType())
2015 P = Context.getArrayDecayedType(P);
2016 // - If P is a function type, the pointer type produced by the
2017 // function-to-pointer standard conversion (4.3) is used in
2018 // place of P for type deduction; otherwise,
2019 else if (P->isFunctionType())
2020 P = Context.getPointerType(P);
2021 // - If P is a cv-qualified type, the top level cv-qualifiers of
2022 // P’s type are ignored for type deduction.
2023 else
2024 P = P.getUnqualifiedType();
2025
2026 // C++0x [temp.deduct.conv]p3:
2027 // If A is a cv-qualified type, the top level cv-qualifiers of A’s
2028 // type are ignored for type deduction.
2029 A = A.getUnqualifiedType();
2030 }
2031
2032 // Template argument deduction for function templates in a SFINAE context.
2033 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00002034 SFINAETrap Trap(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002035
2036 // C++ [temp.deduct.conv]p1:
2037 // Template argument deduction is done by comparing the return
2038 // type of the template conversion function (call it P) with the
2039 // type that is required as the result of the conversion (call it
2040 // A) as described in 14.8.2.4.
2041 TemplateParameterList *TemplateParams
2042 = FunctionTemplate->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00002043 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Mike Stump1eb44332009-09-09 15:08:12 +00002044 Deduced.resize(TemplateParams->size());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002045
2046 // C++0x [temp.deduct.conv]p4:
2047 // In general, the deduction process attempts to find template
2048 // argument values that will make the deduced A identical to
2049 // A. However, there are two cases that allow a difference:
2050 unsigned TDF = 0;
2051 // - If the original A is a reference type, A can be more
2052 // cv-qualified than the deduced A (i.e., the type referred to
2053 // by the reference)
2054 if (ToType->isReferenceType())
2055 TDF |= TDF_ParamWithReferenceType;
2056 // - The deduced A can be another pointer or pointer to member
2057 // type that can be converted to A via a qualification
2058 // conversion.
2059 //
2060 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
2061 // both P and A are pointers or member pointers. In this case, we
2062 // just ignore cv-qualifiers completely).
2063 if ((P->isPointerType() && A->isPointerType()) ||
2064 (P->isMemberPointerType() && P->isMemberPointerType()))
2065 TDF |= TDF_IgnoreQualifiers;
2066 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002067 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002068 P, A, Info, Deduced, TDF))
2069 return Result;
2070
2071 // FIXME: we need to check that the deduced A is the same as A,
2072 // modulo the various allowed differences.
Mike Stump1eb44332009-09-09 15:08:12 +00002073
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002074 // Finish template argument deduction.
John McCall2a7fb272010-08-25 05:32:35 +00002075 LocalInstantiationScope InstScope(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002076 FunctionDecl *Spec = 0;
2077 TemplateDeductionResult Result
Douglas Gregor02024a92010-03-28 02:42:43 +00002078 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec,
2079 Info);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002080 Specialization = cast_or_null<CXXConversionDecl>(Spec);
2081 return Result;
2082}
2083
Douglas Gregor4b52e252009-12-21 23:17:24 +00002084/// \brief Deduce template arguments for a function template when there is
2085/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
2086///
2087/// \param FunctionTemplate the function template for which we are performing
2088/// template argument deduction.
2089///
2090/// \param ExplicitTemplateArguments the explicitly-specified template
2091/// arguments.
2092///
2093/// \param Specialization if template argument deduction was successful,
2094/// this will be set to the function template specialization produced by
2095/// template argument deduction.
2096///
2097/// \param Info the argument will be updated to provide additional information
2098/// about template argument deduction.
2099///
2100/// \returns the result of template argument deduction.
2101Sema::TemplateDeductionResult
2102Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
2103 const TemplateArgumentListInfo *ExplicitTemplateArgs,
2104 FunctionDecl *&Specialization,
2105 TemplateDeductionInfo &Info) {
2106 return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
2107 QualType(), Specialization, Info);
2108}
2109
Douglas Gregor8a514912009-09-14 18:39:43 +00002110/// \brief Stores the result of comparing the qualifiers of two types.
2111enum DeductionQualifierComparison {
2112 NeitherMoreQualified = 0,
2113 ParamMoreQualified,
2114 ArgMoreQualified
2115};
2116
2117/// \brief Deduce the template arguments during partial ordering by comparing
2118/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
2119///
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002120/// \param S the semantic analysis object within which we are deducing
Douglas Gregor8a514912009-09-14 18:39:43 +00002121///
2122/// \param TemplateParams the template parameters that we are deducing
2123///
2124/// \param ParamIn the parameter type
2125///
2126/// \param ArgIn the argument type
2127///
2128/// \param Info information about the template argument deduction itself
2129///
2130/// \param Deduced the deduced template arguments
2131///
2132/// \returns the result of template argument deduction so far. Note that a
2133/// "success" result means that template argument deduction has not yet failed,
2134/// but it may still fail, later, for other reasons.
2135static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002136DeduceTemplateArgumentsDuringPartialOrdering(Sema &S,
Douglas Gregor02024a92010-03-28 02:42:43 +00002137 TemplateParameterList *TemplateParams,
Douglas Gregor8a514912009-09-14 18:39:43 +00002138 QualType ParamIn, QualType ArgIn,
John McCall2a7fb272010-08-25 05:32:35 +00002139 TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +00002140 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
2141 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002142 CanQualType Param = S.Context.getCanonicalType(ParamIn);
2143 CanQualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor8a514912009-09-14 18:39:43 +00002144
2145 // C++0x [temp.deduct.partial]p5:
2146 // Before the partial ordering is done, certain transformations are
2147 // performed on the types used for partial ordering:
2148 // - If P is a reference type, P is replaced by the type referred to.
2149 CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00002150 if (!ParamRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00002151 Param = ParamRef->getPointeeType();
2152
2153 // - If A is a reference type, A is replaced by the type referred to.
2154 CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00002155 if (!ArgRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00002156 Arg = ArgRef->getPointeeType();
2157
John McCalle27ec8a2009-10-23 23:03:21 +00002158 if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
Douglas Gregor8a514912009-09-14 18:39:43 +00002159 // C++0x [temp.deduct.partial]p6:
2160 // If both P and A were reference types (before being replaced with the
2161 // type referred to above), determine which of the two types (if any) is
2162 // more cv-qualified than the other; otherwise the types are considered to
2163 // be equally cv-qualified for partial ordering purposes. The result of this
2164 // determination will be used below.
2165 //
2166 // We save this information for later, using it only when deduction
2167 // succeeds in both directions.
2168 DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
2169 if (Param.isMoreQualifiedThan(Arg))
2170 QualifierResult = ParamMoreQualified;
2171 else if (Arg.isMoreQualifiedThan(Param))
2172 QualifierResult = ArgMoreQualified;
2173 QualifierComparisons->push_back(QualifierResult);
2174 }
2175
2176 // C++0x [temp.deduct.partial]p7:
2177 // Remove any top-level cv-qualifiers:
2178 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
2179 // version of P.
2180 Param = Param.getUnqualifiedType();
2181 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
2182 // version of A.
2183 Arg = Arg.getUnqualifiedType();
2184
2185 // C++0x [temp.deduct.partial]p8:
2186 // Using the resulting types P and A the deduction is then done as
2187 // described in 14.9.2.5. If deduction succeeds for a given type, the type
2188 // from the argument template is considered to be at least as specialized
2189 // as the type from the parameter template.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002190 return DeduceTemplateArguments(S, TemplateParams, Param, Arg, Info,
Douglas Gregor8a514912009-09-14 18:39:43 +00002191 Deduced, TDF_None);
2192}
2193
2194static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002195MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2196 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002197 unsigned Level,
Douglas Gregore73bb602009-09-14 21:25:05 +00002198 llvm::SmallVectorImpl<bool> &Deduced);
Douglas Gregor77bc5722010-11-12 23:44:13 +00002199
2200/// \brief If this is a non-static member function,
2201static void MaybeAddImplicitObjectParameterType(ASTContext &Context,
2202 CXXMethodDecl *Method,
2203 llvm::SmallVectorImpl<QualType> &ArgTypes) {
2204 if (Method->isStatic())
2205 return;
2206
2207 // C++ [over.match.funcs]p4:
2208 //
2209 // For non-static member functions, the type of the implicit
2210 // object parameter is
2211 // — "lvalue reference to cv X" for functions declared without a
2212 // ref-qualifier or with the & ref-qualifier
2213 // - "rvalue reference to cv X" for functions declared with the
2214 // && ref-qualifier
2215 //
2216 // FIXME: We don't have ref-qualifiers yet, so we don't do that part.
2217 QualType ArgTy = Context.getTypeDeclType(Method->getParent());
2218 ArgTy = Context.getQualifiedType(ArgTy,
2219 Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
2220 ArgTy = Context.getLValueReferenceType(ArgTy);
2221 ArgTypes.push_back(ArgTy);
2222}
2223
Douglas Gregor8a514912009-09-14 18:39:43 +00002224/// \brief Determine whether the function template \p FT1 is at least as
2225/// specialized as \p FT2.
2226static bool isAtLeastAsSpecializedAs(Sema &S,
John McCall5769d612010-02-08 23:07:23 +00002227 SourceLocation Loc,
Douglas Gregor8a514912009-09-14 18:39:43 +00002228 FunctionTemplateDecl *FT1,
2229 FunctionTemplateDecl *FT2,
2230 TemplatePartialOrderingContext TPOC,
2231 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
2232 FunctionDecl *FD1 = FT1->getTemplatedDecl();
2233 FunctionDecl *FD2 = FT2->getTemplatedDecl();
2234 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
2235 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
2236
2237 assert(Proto1 && Proto2 && "Function templates must have prototypes");
2238 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00002239 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor8a514912009-09-14 18:39:43 +00002240 Deduced.resize(TemplateParams->size());
2241
2242 // C++0x [temp.deduct.partial]p3:
2243 // The types used to determine the ordering depend on the context in which
2244 // the partial ordering is done:
John McCall2a7fb272010-08-25 05:32:35 +00002245 TemplateDeductionInfo Info(S.Context, Loc);
Douglas Gregor8d706ec2010-11-15 15:41:16 +00002246 CXXMethodDecl *Method1 = 0;
2247 CXXMethodDecl *Method2 = 0;
2248 bool IsNonStatic2 = false;
2249 bool IsNonStatic1 = false;
2250 unsigned Skip2 = 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00002251 switch (TPOC) {
2252 case TPOC_Call: {
2253 // - In the context of a function call, the function parameter types are
2254 // used.
Douglas Gregor8d706ec2010-11-15 15:41:16 +00002255 Method1 = dyn_cast<CXXMethodDecl>(FD1);
2256 Method2 = dyn_cast<CXXMethodDecl>(FD2);
2257 IsNonStatic1 = Method1 && !Method1->isStatic();
2258 IsNonStatic2 = Method2 && !Method2->isStatic();
2259
2260 // C++0x [temp.func.order]p3:
2261 // [...] If only one of the function templates is a non-static
2262 // member, that function template is considered to have a new
2263 // first parameter inserted in its function parameter list. The
2264 // new parameter is of type "reference to cv A," where cv are
2265 // the cv-qualifiers of the function template (if any) and A is
2266 // the class of which the function template is a member.
2267 //
2268 // C++98/03 doesn't have this provision, so instead we drop the
2269 // first argument of the free function or static member, which
2270 // seems to match existing practice.
Douglas Gregor77bc5722010-11-12 23:44:13 +00002271 llvm::SmallVector<QualType, 4> Args1;
Douglas Gregor8d706ec2010-11-15 15:41:16 +00002272 unsigned Skip1 = !S.getLangOptions().CPlusPlus0x &&
2273 IsNonStatic2 && !IsNonStatic1;
2274 if (S.getLangOptions().CPlusPlus0x && IsNonStatic1 && !IsNonStatic2)
Douglas Gregor77bc5722010-11-12 23:44:13 +00002275 MaybeAddImplicitObjectParameterType(S.Context, Method1, Args1);
2276 Args1.insert(Args1.end(),
Douglas Gregor8d706ec2010-11-15 15:41:16 +00002277 Proto1->arg_type_begin() + Skip1, Proto1->arg_type_end());
Douglas Gregor77bc5722010-11-12 23:44:13 +00002278
2279 llvm::SmallVector<QualType, 4> Args2;
Douglas Gregor8d706ec2010-11-15 15:41:16 +00002280 Skip2 = !S.getLangOptions().CPlusPlus0x &&
2281 IsNonStatic1 && !IsNonStatic2;
2282 if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1)
Douglas Gregor77bc5722010-11-12 23:44:13 +00002283 MaybeAddImplicitObjectParameterType(S.Context, Method2, Args2);
2284 Args2.insert(Args2.end(),
Douglas Gregor8d706ec2010-11-15 15:41:16 +00002285 Proto2->arg_type_begin() + Skip2, Proto2->arg_type_end());
Douglas Gregor77bc5722010-11-12 23:44:13 +00002286
2287 unsigned NumParams = std::min(Args1.size(), Args2.size());
Douglas Gregor8a514912009-09-14 18:39:43 +00002288 for (unsigned I = 0; I != NumParams; ++I)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002289 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00002290 TemplateParams,
Douglas Gregor77bc5722010-11-12 23:44:13 +00002291 Args2[I],
2292 Args1[I],
Douglas Gregor8a514912009-09-14 18:39:43 +00002293 Info,
2294 Deduced,
2295 QualifierComparisons))
2296 return false;
2297
2298 break;
2299 }
2300
2301 case TPOC_Conversion:
2302 // - In the context of a call to a conversion operator, the return types
2303 // of the conversion function templates are used.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002304 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00002305 TemplateParams,
2306 Proto2->getResultType(),
2307 Proto1->getResultType(),
2308 Info,
2309 Deduced,
2310 QualifierComparisons))
2311 return false;
2312 break;
2313
2314 case TPOC_Other:
2315 // - In other contexts (14.6.6.2) the function template’s function type
2316 // is used.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002317 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00002318 TemplateParams,
2319 FD2->getType(),
2320 FD1->getType(),
2321 Info,
2322 Deduced,
2323 QualifierComparisons))
2324 return false;
2325 break;
2326 }
2327
2328 // C++0x [temp.deduct.partial]p11:
2329 // In most cases, all template parameters must have values in order for
2330 // deduction to succeed, but for partial ordering purposes a template
2331 // parameter may remain without a value provided it is not used in the
2332 // types being used for partial ordering. [ Note: a template parameter used
2333 // in a non-deduced context is considered used. -end note]
2334 unsigned ArgIdx = 0, NumArgs = Deduced.size();
2335 for (; ArgIdx != NumArgs; ++ArgIdx)
2336 if (Deduced[ArgIdx].isNull())
2337 break;
2338
2339 if (ArgIdx == NumArgs) {
2340 // All template arguments were deduced. FT1 is at least as specialized
2341 // as FT2.
2342 return true;
2343 }
2344
Douglas Gregore73bb602009-09-14 21:25:05 +00002345 // Figure out which template parameters were used.
Douglas Gregor8a514912009-09-14 18:39:43 +00002346 llvm::SmallVector<bool, 4> UsedParameters;
2347 UsedParameters.resize(TemplateParams->size());
2348 switch (TPOC) {
2349 case TPOC_Call: {
2350 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
Douglas Gregor8d706ec2010-11-15 15:41:16 +00002351 if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1)
2352 ::MarkUsedTemplateParameters(S, Method2->getThisType(S.Context), false,
2353 TemplateParams->getDepth(), UsedParameters);
2354 for (unsigned I = Skip2; I < NumParams; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002355 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
2356 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00002357 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00002358 break;
2359 }
2360
2361 case TPOC_Conversion:
Douglas Gregored9c0f92009-10-29 00:04:11 +00002362 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
2363 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00002364 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00002365 break;
2366
2367 case TPOC_Other:
Douglas Gregored9c0f92009-10-29 00:04:11 +00002368 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
2369 TemplateParams->getDepth(),
2370 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00002371 break;
2372 }
2373
2374 for (; ArgIdx != NumArgs; ++ArgIdx)
2375 // If this argument had no value deduced but was used in one of the types
2376 // used for partial ordering, then deduction fails.
2377 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
2378 return false;
2379
2380 return true;
2381}
2382
2383
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002384/// \brief Returns the more specialized function template according
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002385/// to the rules of function template partial ordering (C++ [temp.func.order]).
2386///
2387/// \param FT1 the first function template
2388///
2389/// \param FT2 the second function template
2390///
Douglas Gregor8a514912009-09-14 18:39:43 +00002391/// \param TPOC the context in which we are performing partial ordering of
2392/// function templates.
Mike Stump1eb44332009-09-09 15:08:12 +00002393///
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002394/// \returns the more specialized function template. If neither
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002395/// template is more specialized, returns NULL.
2396FunctionTemplateDecl *
2397Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
2398 FunctionTemplateDecl *FT2,
John McCall5769d612010-02-08 23:07:23 +00002399 SourceLocation Loc,
Douglas Gregor8a514912009-09-14 18:39:43 +00002400 TemplatePartialOrderingContext TPOC) {
Douglas Gregor8a514912009-09-14 18:39:43 +00002401 llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
John McCall5769d612010-02-08 23:07:23 +00002402 bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, 0);
2403 bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
Douglas Gregor8a514912009-09-14 18:39:43 +00002404 &QualifierComparisons);
2405
2406 if (Better1 != Better2) // We have a clear winner
2407 return Better1? FT1 : FT2;
2408
2409 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002410 return 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00002411
2412
2413 // C++0x [temp.deduct.partial]p10:
2414 // If for each type being considered a given template is at least as
2415 // specialized for all types and more specialized for some set of types and
2416 // the other template is not more specialized for any types or is not at
2417 // least as specialized for any types, then the given template is more
2418 // specialized than the other template. Otherwise, neither template is more
2419 // specialized than the other.
2420 Better1 = false;
2421 Better2 = false;
2422 for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
2423 // C++0x [temp.deduct.partial]p9:
2424 // If, for a given type, deduction succeeds in both directions (i.e., the
2425 // types are identical after the transformations above) and if the type
2426 // from the argument template is more cv-qualified than the type from the
2427 // parameter template (as described above) that type is considered to be
2428 // more specialized than the other. If neither type is more cv-qualified
2429 // than the other then neither type is more specialized than the other.
2430 switch (QualifierComparisons[I]) {
2431 case NeitherMoreQualified:
2432 break;
2433
2434 case ParamMoreQualified:
2435 Better1 = true;
2436 if (Better2)
2437 return 0;
2438 break;
2439
2440 case ArgMoreQualified:
2441 Better2 = true;
2442 if (Better1)
2443 return 0;
2444 break;
2445 }
2446 }
2447
2448 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002449 if (Better1)
2450 return FT1;
Douglas Gregor8a514912009-09-14 18:39:43 +00002451 else if (Better2)
2452 return FT2;
2453 else
2454 return 0;
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002455}
Douglas Gregor83314aa2009-07-08 20:55:45 +00002456
Douglas Gregord5a423b2009-09-25 18:43:00 +00002457/// \brief Determine if the two templates are equivalent.
2458static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
2459 if (T1 == T2)
2460 return true;
2461
2462 if (!T1 || !T2)
2463 return false;
2464
2465 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
2466}
2467
2468/// \brief Retrieve the most specialized of the given function template
2469/// specializations.
2470///
John McCallc373d482010-01-27 01:50:18 +00002471/// \param SpecBegin the start iterator of the function template
2472/// specializations that we will be comparing.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002473///
John McCallc373d482010-01-27 01:50:18 +00002474/// \param SpecEnd the end iterator of the function template
2475/// specializations, paired with \p SpecBegin.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002476///
2477/// \param TPOC the partial ordering context to use to compare the function
2478/// template specializations.
2479///
2480/// \param Loc the location where the ambiguity or no-specializations
2481/// diagnostic should occur.
2482///
2483/// \param NoneDiag partial diagnostic used to diagnose cases where there are
2484/// no matching candidates.
2485///
2486/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
2487/// occurs.
2488///
2489/// \param CandidateDiag partial diagnostic used for each function template
2490/// specialization that is a candidate in the ambiguous ordering. One parameter
2491/// in this diagnostic should be unbound, which will correspond to the string
2492/// describing the template arguments for the function template specialization.
2493///
2494/// \param Index if non-NULL and the result of this function is non-nULL,
2495/// receives the index corresponding to the resulting function template
2496/// specialization.
2497///
2498/// \returns the most specialized function template specialization, if
John McCallc373d482010-01-27 01:50:18 +00002499/// found. Otherwise, returns SpecEnd.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002500///
2501/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
2502/// template argument deduction.
John McCallc373d482010-01-27 01:50:18 +00002503UnresolvedSetIterator
2504Sema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
2505 UnresolvedSetIterator SpecEnd,
2506 TemplatePartialOrderingContext TPOC,
2507 SourceLocation Loc,
2508 const PartialDiagnostic &NoneDiag,
2509 const PartialDiagnostic &AmbigDiag,
2510 const PartialDiagnostic &CandidateDiag) {
2511 if (SpecBegin == SpecEnd) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00002512 Diag(Loc, NoneDiag);
John McCallc373d482010-01-27 01:50:18 +00002513 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002514 }
2515
John McCallc373d482010-01-27 01:50:18 +00002516 if (SpecBegin + 1 == SpecEnd)
2517 return SpecBegin;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002518
2519 // Find the function template that is better than all of the templates it
2520 // has been compared to.
John McCallc373d482010-01-27 01:50:18 +00002521 UnresolvedSetIterator Best = SpecBegin;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002522 FunctionTemplateDecl *BestTemplate
John McCallc373d482010-01-27 01:50:18 +00002523 = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002524 assert(BestTemplate && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00002525 for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
2526 FunctionTemplateDecl *Challenger
2527 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002528 assert(Challenger && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00002529 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCall5769d612010-02-08 23:07:23 +00002530 Loc, TPOC),
Douglas Gregord5a423b2009-09-25 18:43:00 +00002531 Challenger)) {
2532 Best = I;
2533 BestTemplate = Challenger;
2534 }
2535 }
2536
2537 // Make sure that the "best" function template is more specialized than all
2538 // of the others.
2539 bool Ambiguous = false;
John McCallc373d482010-01-27 01:50:18 +00002540 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
2541 FunctionTemplateDecl *Challenger
2542 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002543 if (I != Best &&
2544 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCall5769d612010-02-08 23:07:23 +00002545 Loc, TPOC),
Douglas Gregord5a423b2009-09-25 18:43:00 +00002546 BestTemplate)) {
2547 Ambiguous = true;
2548 break;
2549 }
2550 }
2551
2552 if (!Ambiguous) {
2553 // We found an answer. Return it.
John McCallc373d482010-01-27 01:50:18 +00002554 return Best;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002555 }
2556
2557 // Diagnose the ambiguity.
2558 Diag(Loc, AmbigDiag);
2559
2560 // FIXME: Can we order the candidates in some sane way?
John McCallc373d482010-01-27 01:50:18 +00002561 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
2562 Diag((*I)->getLocation(), CandidateDiag)
Douglas Gregord5a423b2009-09-25 18:43:00 +00002563 << getTemplateArgumentBindingsText(
John McCallc373d482010-01-27 01:50:18 +00002564 cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
2565 *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
Douglas Gregord5a423b2009-09-25 18:43:00 +00002566
John McCallc373d482010-01-27 01:50:18 +00002567 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002568}
2569
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002570/// \brief Returns the more specialized class template partial specialization
2571/// according to the rules of partial ordering of class template partial
2572/// specializations (C++ [temp.class.order]).
2573///
2574/// \param PS1 the first class template partial specialization
2575///
2576/// \param PS2 the second class template partial specialization
2577///
2578/// \returns the more specialized class template partial specialization. If
2579/// neither partial specialization is more specialized, returns NULL.
2580ClassTemplatePartialSpecializationDecl *
2581Sema::getMoreSpecializedPartialSpecialization(
2582 ClassTemplatePartialSpecializationDecl *PS1,
John McCall5769d612010-02-08 23:07:23 +00002583 ClassTemplatePartialSpecializationDecl *PS2,
2584 SourceLocation Loc) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002585 // C++ [temp.class.order]p1:
2586 // For two class template partial specializations, the first is at least as
2587 // specialized as the second if, given the following rewrite to two
2588 // function templates, the first function template is at least as
2589 // specialized as the second according to the ordering rules for function
2590 // templates (14.6.6.2):
2591 // - the first function template has the same template parameters as the
2592 // first partial specialization and has a single function parameter
2593 // whose type is a class template specialization with the template
2594 // arguments of the first partial specialization, and
2595 // - the second function template has the same template parameters as the
2596 // second partial specialization and has a single function parameter
2597 // whose type is a class template specialization with the template
2598 // arguments of the second partial specialization.
2599 //
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002600 // Rather than synthesize function templates, we merely perform the
2601 // equivalent partial ordering by performing deduction directly on
2602 // the template arguments of the class template partial
2603 // specializations. This computation is slightly simpler than the
2604 // general problem of function template partial ordering, because
2605 // class template partial specializations are more constrained. We
2606 // know that every template parameter is deducible from the class
2607 // template partial specialization's template arguments, for
2608 // example.
Douglas Gregor02024a92010-03-28 02:42:43 +00002609 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
John McCall2a7fb272010-08-25 05:32:35 +00002610 TemplateDeductionInfo Info(Context, Loc);
John McCall31f17ec2010-04-27 00:57:59 +00002611
2612 QualType PT1 = PS1->getInjectedSpecializationType();
2613 QualType PT2 = PS2->getInjectedSpecializationType();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002614
2615 // Determine whether PS1 is at least as specialized as PS2
2616 Deduced.resize(PS2->getTemplateParameters()->size());
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002617 bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002618 PS2->getTemplateParameters(),
John McCall31f17ec2010-04-27 00:57:59 +00002619 PT2,
2620 PT1,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002621 Info,
2622 Deduced,
2623 0);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00002624 if (Better1) {
2625 InstantiatingTemplate Inst(*this, PS2->getLocation(), PS2,
2626 Deduced.data(), Deduced.size(), Info);
Douglas Gregor516e6e02010-04-29 06:31:36 +00002627 Better1 = !::FinishTemplateArgumentDeduction(*this, PS2,
2628 PS1->getTemplateArgs(),
2629 Deduced, Info);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00002630 }
Douglas Gregor516e6e02010-04-29 06:31:36 +00002631
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002632 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregordb0d4b72009-11-11 23:06:43 +00002633 Deduced.clear();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002634 Deduced.resize(PS1->getTemplateParameters()->size());
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002635 bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002636 PS1->getTemplateParameters(),
John McCall31f17ec2010-04-27 00:57:59 +00002637 PT1,
2638 PT2,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002639 Info,
2640 Deduced,
2641 0);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00002642 if (Better2) {
2643 InstantiatingTemplate Inst(*this, PS1->getLocation(), PS1,
2644 Deduced.data(), Deduced.size(), Info);
Douglas Gregor516e6e02010-04-29 06:31:36 +00002645 Better2 = !::FinishTemplateArgumentDeduction(*this, PS1,
2646 PS2->getTemplateArgs(),
2647 Deduced, Info);
Argyrios Kyrtzidis2c4792c2010-11-05 23:25:18 +00002648 }
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002649
2650 if (Better1 == Better2)
2651 return 0;
2652
2653 return Better1? PS1 : PS2;
2654}
2655
Mike Stump1eb44332009-09-09 15:08:12 +00002656static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002657MarkUsedTemplateParameters(Sema &SemaRef,
2658 const TemplateArgument &TemplateArg,
2659 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002660 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002661 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002662
Douglas Gregore73bb602009-09-14 21:25:05 +00002663/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002664/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00002665static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002666MarkUsedTemplateParameters(Sema &SemaRef,
2667 const Expr *E,
2668 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002669 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002670 llvm::SmallVectorImpl<bool> &Used) {
2671 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2672 // find other occurrences of template parameters.
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002673 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregorc781f9c2010-01-14 18:13:22 +00002674 if (!DRE)
Douglas Gregor031a5882009-06-13 00:26:55 +00002675 return;
2676
Mike Stump1eb44332009-09-09 15:08:12 +00002677 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00002678 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2679 if (!NTTP)
2680 return;
2681
Douglas Gregored9c0f92009-10-29 00:04:11 +00002682 if (NTTP->getDepth() == Depth)
2683 Used[NTTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002684}
2685
Douglas Gregore73bb602009-09-14 21:25:05 +00002686/// \brief Mark the template parameters that are used by the given
2687/// nested name specifier.
2688static void
2689MarkUsedTemplateParameters(Sema &SemaRef,
2690 NestedNameSpecifier *NNS,
2691 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002692 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002693 llvm::SmallVectorImpl<bool> &Used) {
2694 if (!NNS)
2695 return;
2696
Douglas Gregored9c0f92009-10-29 00:04:11 +00002697 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2698 Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002699 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002700 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002701}
2702
2703/// \brief Mark the template parameters that are used by the given
2704/// template name.
2705static void
2706MarkUsedTemplateParameters(Sema &SemaRef,
2707 TemplateName Name,
2708 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002709 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002710 llvm::SmallVectorImpl<bool> &Used) {
2711 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2712 if (TemplateTemplateParmDecl *TTP
Douglas Gregored9c0f92009-10-29 00:04:11 +00002713 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2714 if (TTP->getDepth() == Depth)
2715 Used[TTP->getIndex()] = true;
2716 }
Douglas Gregore73bb602009-09-14 21:25:05 +00002717 return;
2718 }
2719
Douglas Gregor788cd062009-11-11 01:00:40 +00002720 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2721 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2722 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002723 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
Douglas Gregored9c0f92009-10-29 00:04:11 +00002724 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2725 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002726}
2727
2728/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002729/// type.
Mike Stump1eb44332009-09-09 15:08:12 +00002730static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002731MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2732 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002733 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002734 llvm::SmallVectorImpl<bool> &Used) {
2735 if (T.isNull())
2736 return;
2737
Douglas Gregor031a5882009-06-13 00:26:55 +00002738 // Non-dependent types have nothing deducible
2739 if (!T->isDependentType())
2740 return;
2741
2742 T = SemaRef.Context.getCanonicalType(T);
2743 switch (T->getTypeClass()) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002744 case Type::Pointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002745 MarkUsedTemplateParameters(SemaRef,
2746 cast<PointerType>(T)->getPointeeType(),
2747 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002748 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002749 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002750 break;
2751
2752 case Type::BlockPointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002753 MarkUsedTemplateParameters(SemaRef,
2754 cast<BlockPointerType>(T)->getPointeeType(),
2755 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002756 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002757 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002758 break;
2759
2760 case Type::LValueReference:
2761 case Type::RValueReference:
Douglas Gregore73bb602009-09-14 21:25:05 +00002762 MarkUsedTemplateParameters(SemaRef,
2763 cast<ReferenceType>(T)->getPointeeType(),
2764 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002765 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002766 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002767 break;
2768
2769 case Type::MemberPointer: {
2770 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore73bb602009-09-14 21:25:05 +00002771 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002772 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002773 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002774 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002775 break;
2776 }
2777
2778 case Type::DependentSizedArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002779 MarkUsedTemplateParameters(SemaRef,
2780 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002781 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002782 // Fall through to check the element type
2783
2784 case Type::ConstantArray:
2785 case Type::IncompleteArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002786 MarkUsedTemplateParameters(SemaRef,
2787 cast<ArrayType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002788 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002789 break;
2790
2791 case Type::Vector:
2792 case Type::ExtVector:
Douglas Gregore73bb602009-09-14 21:25:05 +00002793 MarkUsedTemplateParameters(SemaRef,
2794 cast<VectorType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002795 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002796 break;
2797
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002798 case Type::DependentSizedExtVector: {
2799 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002800 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002801 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002802 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002803 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002804 Depth, Used);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002805 break;
2806 }
2807
Douglas Gregor031a5882009-06-13 00:26:55 +00002808 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002809 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002810 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002811 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002812 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore73bb602009-09-14 21:25:05 +00002813 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002814 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002815 break;
2816 }
2817
Douglas Gregored9c0f92009-10-29 00:04:11 +00002818 case Type::TemplateTypeParm: {
2819 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2820 if (TTP->getDepth() == Depth)
2821 Used[TTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002822 break;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002823 }
Douglas Gregor031a5882009-06-13 00:26:55 +00002824
John McCall31f17ec2010-04-27 00:57:59 +00002825 case Type::InjectedClassName:
2826 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
2827 // fall through
2828
Douglas Gregor031a5882009-06-13 00:26:55 +00002829 case Type::TemplateSpecialization: {
Mike Stump1eb44332009-09-09 15:08:12 +00002830 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002831 = cast<TemplateSpecializationType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002832 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002833 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002834 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002835 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2836 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002837 break;
2838 }
2839
Douglas Gregore73bb602009-09-14 21:25:05 +00002840 case Type::Complex:
2841 if (!OnlyDeduced)
2842 MarkUsedTemplateParameters(SemaRef,
2843 cast<ComplexType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002844 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002845 break;
2846
Douglas Gregor4714c122010-03-31 17:34:00 +00002847 case Type::DependentName:
Douglas Gregore73bb602009-09-14 21:25:05 +00002848 if (!OnlyDeduced)
2849 MarkUsedTemplateParameters(SemaRef,
Douglas Gregor4714c122010-03-31 17:34:00 +00002850 cast<DependentNameType>(T)->getQualifier(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002851 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002852 break;
2853
John McCall33500952010-06-11 00:33:02 +00002854 case Type::DependentTemplateSpecialization: {
2855 const DependentTemplateSpecializationType *Spec
2856 = cast<DependentTemplateSpecializationType>(T);
2857 if (!OnlyDeduced)
2858 MarkUsedTemplateParameters(SemaRef, Spec->getQualifier(),
2859 OnlyDeduced, Depth, Used);
2860 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
2861 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2862 Used);
2863 break;
2864 }
2865
John McCallad5e7382010-03-01 23:49:17 +00002866 case Type::TypeOf:
2867 if (!OnlyDeduced)
2868 MarkUsedTemplateParameters(SemaRef,
2869 cast<TypeOfType>(T)->getUnderlyingType(),
2870 OnlyDeduced, Depth, Used);
2871 break;
2872
2873 case Type::TypeOfExpr:
2874 if (!OnlyDeduced)
2875 MarkUsedTemplateParameters(SemaRef,
2876 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
2877 OnlyDeduced, Depth, Used);
2878 break;
2879
2880 case Type::Decltype:
2881 if (!OnlyDeduced)
2882 MarkUsedTemplateParameters(SemaRef,
2883 cast<DecltypeType>(T)->getUnderlyingExpr(),
2884 OnlyDeduced, Depth, Used);
2885 break;
2886
Douglas Gregor7536dd52010-12-20 02:24:11 +00002887 case Type::PackExpansion:
2888 MarkUsedTemplateParameters(SemaRef,
2889 cast<PackExpansionType>(T)->getPattern(),
2890 OnlyDeduced, Depth, Used);
2891 break;
2892
Douglas Gregore73bb602009-09-14 21:25:05 +00002893 // None of these types have any template parameters in them.
Douglas Gregor031a5882009-06-13 00:26:55 +00002894 case Type::Builtin:
Douglas Gregor031a5882009-06-13 00:26:55 +00002895 case Type::VariableArray:
2896 case Type::FunctionNoProto:
2897 case Type::Record:
2898 case Type::Enum:
Douglas Gregor031a5882009-06-13 00:26:55 +00002899 case Type::ObjCInterface:
John McCallc12c5bb2010-05-15 11:32:37 +00002900 case Type::ObjCObject:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002901 case Type::ObjCObjectPointer:
John McCalled976492009-12-04 22:46:56 +00002902 case Type::UnresolvedUsing:
Douglas Gregor031a5882009-06-13 00:26:55 +00002903#define TYPE(Class, Base)
2904#define ABSTRACT_TYPE(Class, Base)
2905#define DEPENDENT_TYPE(Class, Base)
2906#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2907#include "clang/AST/TypeNodes.def"
2908 break;
2909 }
2910}
2911
Douglas Gregore73bb602009-09-14 21:25:05 +00002912/// \brief Mark the template parameters that are used by this
Douglas Gregor031a5882009-06-13 00:26:55 +00002913/// template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00002914static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002915MarkUsedTemplateParameters(Sema &SemaRef,
2916 const TemplateArgument &TemplateArg,
2917 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002918 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002919 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002920 switch (TemplateArg.getKind()) {
2921 case TemplateArgument::Null:
2922 case TemplateArgument::Integral:
Douglas Gregor788cd062009-11-11 01:00:40 +00002923 case TemplateArgument::Declaration:
Douglas Gregor031a5882009-06-13 00:26:55 +00002924 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002925
Douglas Gregor031a5882009-06-13 00:26:55 +00002926 case TemplateArgument::Type:
Douglas Gregore73bb602009-09-14 21:25:05 +00002927 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002928 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002929 break;
2930
Douglas Gregor788cd062009-11-11 01:00:40 +00002931 case TemplateArgument::Template:
2932 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2933 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002934 break;
2935
2936 case TemplateArgument::Expression:
Douglas Gregore73bb602009-09-14 21:25:05 +00002937 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002938 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002939 break;
Douglas Gregore73bb602009-09-14 21:25:05 +00002940
Anders Carlssond01b1da2009-06-15 17:04:53 +00002941 case TemplateArgument::Pack:
Douglas Gregore73bb602009-09-14 21:25:05 +00002942 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2943 PEnd = TemplateArg.pack_end();
2944 P != PEnd; ++P)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002945 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssond01b1da2009-06-15 17:04:53 +00002946 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00002947 }
2948}
2949
2950/// \brief Mark the template parameters can be deduced by the given
2951/// template argument list.
2952///
2953/// \param TemplateArgs the template argument list from which template
2954/// parameters will be deduced.
2955///
2956/// \param Deduced a bit vector whose elements will be set to \c true
2957/// to indicate when the corresponding template parameter will be
2958/// deduced.
Mike Stump1eb44332009-09-09 15:08:12 +00002959void
Douglas Gregore73bb602009-09-14 21:25:05 +00002960Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002961 bool OnlyDeduced, unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002962 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002963 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002964 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2965 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002966}
Douglas Gregor63f07c52009-09-18 23:21:38 +00002967
2968/// \brief Marks all of the template parameters that will be deduced by a
2969/// call to the given function template.
Douglas Gregor02024a92010-03-28 02:42:43 +00002970void
2971Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
2972 llvm::SmallVectorImpl<bool> &Deduced) {
Douglas Gregor63f07c52009-09-18 23:21:38 +00002973 TemplateParameterList *TemplateParams
2974 = FunctionTemplate->getTemplateParameters();
2975 Deduced.clear();
2976 Deduced.resize(TemplateParams->size());
2977
2978 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2979 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
2980 ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002981 true, TemplateParams->getDepth(), Deduced);
Douglas Gregor63f07c52009-09-18 23:21:38 +00002982}