blob: 5c16b85eab52d736d05c715d3d66ef15a7935d53 [file] [log] [blame]
Douglas Gregor55ca8f62009-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 Gregorc3a6ade2010-08-12 20:07:10 +000013#include "clang/Sema/Sema.h"
John McCall8b0666c2010-08-20 18:27:03 +000014#include "clang/Sema/DeclSpec.h"
John McCallde6836a2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
John McCall19c1bfd2010-08-25 05:32:35 +000016#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor55ca8f62009-06-04 00:03:07 +000017#include "clang/AST/ASTContext.h"
John McCallde6836a2010-08-24 07:21:54 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor55ca8f62009-06-04 00:03:07 +000019#include "clang/AST/DeclTemplate.h"
20#include "clang/AST/StmtVisitor.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/ExprCXX.h"
Douglas Gregor0ff7d922009-09-14 18:39:43 +000023#include <algorithm>
Douglas Gregorcf0b47d2009-06-26 23:10:12 +000024
25namespace clang {
John McCall19c1bfd2010-08-25 05:32:35 +000026 using namespace sema;
27
Douglas Gregorcf0b47d2009-06-26 23:10:12 +000028 /// \brief Various flags that control template argument deduction.
29 ///
30 /// These flags can be bitwise-OR'd together.
31 enum TemplateDeductionFlags {
32 /// \brief No template argument deduction flags, which indicates the
33 /// strictest results for template argument deduction (as used for, e.g.,
34 /// matching class template partial specializations).
35 TDF_None = 0,
36 /// \brief Within template argument deduction from a function call, we are
37 /// matching with a parameter type for which the original parameter was
38 /// a reference.
39 TDF_ParamWithReferenceType = 0x1,
40 /// \brief Within template argument deduction from a function call, we
41 /// are matching in a case where we ignore cv-qualifiers.
42 TDF_IgnoreQualifiers = 0x02,
43 /// \brief Within template argument deduction from a function call,
44 /// we are matching in a case where we can perform template argument
Douglas Gregorfc516c92009-06-26 23:27:24 +000045 /// deduction from a template-id of a derived class of the argument type.
Douglas Gregor406f6342009-09-14 20:00:47 +000046 TDF_DerivedClass = 0x04,
47 /// \brief Allow non-dependent types to differ, e.g., when performing
48 /// template argument deduction from a function call where conversions
49 /// may apply.
50 TDF_SkipNonDependent = 0x08
Douglas Gregorcf0b47d2009-06-26 23:10:12 +000051 };
52}
53
Douglas Gregor55ca8f62009-06-04 00:03:07 +000054using namespace clang;
55
Douglas Gregor0a29a052010-03-26 05:50:28 +000056/// \brief Compare two APSInts, extending and switching the sign as
57/// necessary to compare their values regardless of underlying type.
58static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) {
59 if (Y.getBitWidth() > X.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +000060 X = X.extend(Y.getBitWidth());
Douglas Gregor0a29a052010-03-26 05:50:28 +000061 else if (Y.getBitWidth() < X.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +000062 Y = Y.extend(X.getBitWidth());
Douglas Gregor0a29a052010-03-26 05:50:28 +000063
64 // If there is a signedness mismatch, correct it.
65 if (X.isSigned() != Y.isSigned()) {
66 // If the signed value is negative, then the values cannot be the same.
67 if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative()))
68 return false;
69
70 Y.setIsSigned(true);
71 X.setIsSigned(true);
72 }
73
74 return X == Y;
75}
76
Douglas Gregor181aa4a2009-06-12 18:26:56 +000077static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +000078DeduceTemplateArguments(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +000079 TemplateParameterList *TemplateParams,
80 const TemplateArgument &Param,
Douglas Gregor4fbe3e32009-06-09 16:35:58 +000081 const TemplateArgument &Arg,
John McCall19c1bfd2010-08-25 05:32:35 +000082 TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +000083 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced);
Douglas Gregor4fbe3e32009-06-09 16:35:58 +000084
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000085/// \brief If the given expression is of a form that permits the deduction
86/// of a non-type template parameter, return the declaration of that
87/// non-type template parameter.
88static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
89 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
90 E = IC->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +000091
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000092 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
93 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
Mike Stump11289f42009-09-09 15:08:12 +000094
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000095 return 0;
96}
97
Mike Stump11289f42009-09-09 15:08:12 +000098/// \brief Deduce the value of the given non-type template parameter
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000099/// from the given constant.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000100static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000101DeduceNonTypeTemplateArgument(Sema &S,
Mike Stump11289f42009-09-09 15:08:12 +0000102 NonTypeTemplateParmDecl *NTTP,
Douglas Gregor0a29a052010-03-26 05:50:28 +0000103 llvm::APSInt Value, QualType ValueType,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000104 bool DeducedFromArrayBound,
John McCall19c1bfd2010-08-25 05:32:35 +0000105 TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000106 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump11289f42009-09-09 15:08:12 +0000107 assert(NTTP->getDepth() == 0 &&
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000108 "Cannot deduce non-type template argument with depth > 0");
Mike Stump11289f42009-09-09 15:08:12 +0000109
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000110 if (Deduced[NTTP->getIndex()].isNull()) {
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000111 Deduced[NTTP->getIndex()] = DeducedTemplateArgument(Value, ValueType,
112 DeducedFromArrayBound);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000113 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000114 }
Mike Stump11289f42009-09-09 15:08:12 +0000115
Douglas Gregor0a29a052010-03-26 05:50:28 +0000116 if (Deduced[NTTP->getIndex()].getKind() != TemplateArgument::Integral) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000117 Info.Param = NTTP;
118 Info.FirstArg = Deduced[NTTP->getIndex()];
Douglas Gregor0a29a052010-03-26 05:50:28 +0000119 Info.SecondArg = TemplateArgument(Value, ValueType);
120 return Sema::TDK_Inconsistent;
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000121 }
122
Douglas Gregor0a29a052010-03-26 05:50:28 +0000123 // Extent the smaller of the two values.
124 llvm::APSInt PrevValue = *Deduced[NTTP->getIndex()].getAsIntegral();
125 if (!hasSameExtendedValue(PrevValue, Value)) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000126 Info.Param = NTTP;
127 Info.FirstArg = Deduced[NTTP->getIndex()];
Douglas Gregor0a29a052010-03-26 05:50:28 +0000128 Info.SecondArg = TemplateArgument(Value, ValueType);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000129 return Sema::TDK_Inconsistent;
130 }
131
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000132 if (!DeducedFromArrayBound)
133 Deduced[NTTP->getIndex()].setDeducedFromArrayBound(false);
134
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000135 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000136}
137
Mike Stump11289f42009-09-09 15:08:12 +0000138/// \brief Deduce the value of the given non-type template parameter
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000139/// from the given type- or value-dependent expression.
140///
141/// \returns true if deduction succeeded, false otherwise.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000142static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000143DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000144 NonTypeTemplateParmDecl *NTTP,
145 Expr *Value,
John McCall19c1bfd2010-08-25 05:32:35 +0000146 TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000147 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump11289f42009-09-09 15:08:12 +0000148 assert(NTTP->getDepth() == 0 &&
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000149 "Cannot deduce non-type template argument with depth > 0");
150 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
151 "Expression template argument must be type- or value-dependent.");
Mike Stump11289f42009-09-09 15:08:12 +0000152
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000153 if (Deduced[NTTP->getIndex()].isNull()) {
John McCallc3007a22010-10-26 07:05:15 +0000154 Deduced[NTTP->getIndex()] = TemplateArgument(Value);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000155 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000156 }
Mike Stump11289f42009-09-09 15:08:12 +0000157
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000158 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
Mike Stump11289f42009-09-09 15:08:12 +0000159 // Okay, we deduced a constant in one case and a dependent expression
160 // in another case. FIXME: Later, we will check that instantiating the
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000161 // dependent expression gives us the constant value.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000162 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000163 }
Mike Stump11289f42009-09-09 15:08:12 +0000164
Douglas Gregor00a511f2009-09-15 16:51:42 +0000165 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
166 // Compare the expressions for equality
167 llvm::FoldingSetNodeID ID1, ID2;
Chandler Carruthc1263112010-02-07 21:33:28 +0000168 Deduced[NTTP->getIndex()].getAsExpr()->Profile(ID1, S.Context, true);
169 Value->Profile(ID2, S.Context, true);
Douglas Gregor00a511f2009-09-15 16:51:42 +0000170 if (ID1 == ID2)
171 return Sema::TDK_Success;
172
173 // FIXME: Fill in argument mismatch information
174 return Sema::TDK_NonDeducedMismatch;
175 }
176
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000177 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000178}
179
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000180/// \brief Deduce the value of the given non-type template parameter
181/// from the given declaration.
182///
183/// \returns true if deduction succeeded, false otherwise.
184static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000185DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000186 NonTypeTemplateParmDecl *NTTP,
187 Decl *D,
John McCall19c1bfd2010-08-25 05:32:35 +0000188 TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000189 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000190 assert(NTTP->getDepth() == 0 &&
191 "Cannot deduce non-type template argument with depth > 0");
192
193 if (Deduced[NTTP->getIndex()].isNull()) {
194 Deduced[NTTP->getIndex()] = TemplateArgument(D->getCanonicalDecl());
195 return Sema::TDK_Success;
196 }
197
198 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
199 // Okay, we deduced a declaration in one case and a dependent expression
200 // in another case.
201 return Sema::TDK_Success;
202 }
203
204 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Declaration) {
205 // Compare the declarations for equality
206 if (Deduced[NTTP->getIndex()].getAsDecl()->getCanonicalDecl() ==
207 D->getCanonicalDecl())
208 return Sema::TDK_Success;
209
210 // FIXME: Fill in argument mismatch information
211 return Sema::TDK_NonDeducedMismatch;
212 }
213
214 return Sema::TDK_Success;
215}
216
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000217static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000218DeduceTemplateArguments(Sema &S,
Douglas Gregoradee3e32009-11-11 23:06:43 +0000219 TemplateParameterList *TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000220 TemplateName Param,
221 TemplateName Arg,
John McCall19c1bfd2010-08-25 05:32:35 +0000222 TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000223 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000224 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
Douglas Gregoradee3e32009-11-11 23:06:43 +0000225 if (!ParamDecl) {
226 // The parameter type is dependent and is not a template template parameter,
227 // so there is nothing that we can deduce.
228 return Sema::TDK_Success;
229 }
230
231 if (TemplateTemplateParmDecl *TempParam
232 = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) {
233 // Bind the template template parameter to the given template name.
234 TemplateArgument &ExistingArg = Deduced[TempParam->getIndex()];
235 if (ExistingArg.isNull()) {
236 // This is the first deduction for this template template parameter.
Chandler Carruthc1263112010-02-07 21:33:28 +0000237 ExistingArg = TemplateArgument(S.Context.getCanonicalTemplateName(Arg));
Douglas Gregoradee3e32009-11-11 23:06:43 +0000238 return Sema::TDK_Success;
239 }
240
241 // Verify that the previous binding matches this deduction.
242 assert(ExistingArg.getKind() == TemplateArgument::Template);
Chandler Carruthc1263112010-02-07 21:33:28 +0000243 if (S.Context.hasSameTemplateName(ExistingArg.getAsTemplate(), Arg))
Douglas Gregoradee3e32009-11-11 23:06:43 +0000244 return Sema::TDK_Success;
245
246 // Inconsistent deduction.
247 Info.Param = TempParam;
248 Info.FirstArg = ExistingArg;
249 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000250 return Sema::TDK_Inconsistent;
251 }
Douglas Gregoradee3e32009-11-11 23:06:43 +0000252
253 // Verify that the two template names are equivalent.
Chandler Carruthc1263112010-02-07 21:33:28 +0000254 if (S.Context.hasSameTemplateName(Param, Arg))
Douglas Gregoradee3e32009-11-11 23:06:43 +0000255 return Sema::TDK_Success;
256
257 // Mismatch of non-dependent template parameter to argument.
258 Info.FirstArg = TemplateArgument(Param);
259 Info.SecondArg = TemplateArgument(Arg);
260 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000261}
262
Mike Stump11289f42009-09-09 15:08:12 +0000263/// \brief Deduce the template arguments by comparing the template parameter
Douglas Gregore81f3e72009-07-07 23:09:34 +0000264/// type (which is a template-id) with the template argument type.
265///
Chandler Carruthc1263112010-02-07 21:33:28 +0000266/// \param S the Sema
Douglas Gregore81f3e72009-07-07 23:09:34 +0000267///
268/// \param TemplateParams the template parameters that we are deducing
269///
270/// \param Param the parameter type
271///
272/// \param Arg the argument type
273///
274/// \param Info information about the template argument deduction itself
275///
276/// \param Deduced the deduced template arguments
277///
278/// \returns the result of template argument deduction so far. Note that a
279/// "success" result means that template argument deduction has not yet failed,
280/// but it may still fail, later, for other reasons.
281static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000282DeduceTemplateArguments(Sema &S,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000283 TemplateParameterList *TemplateParams,
284 const TemplateSpecializationType *Param,
285 QualType Arg,
John McCall19c1bfd2010-08-25 05:32:35 +0000286 TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000287 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
John McCallb692a092009-10-22 20:10:53 +0000288 assert(Arg.isCanonical() && "Argument type must be canonical");
Mike Stump11289f42009-09-09 15:08:12 +0000289
Douglas Gregore81f3e72009-07-07 23:09:34 +0000290 // Check whether the template argument is a dependent template-id.
Mike Stump11289f42009-09-09 15:08:12 +0000291 if (const TemplateSpecializationType *SpecArg
Douglas Gregore81f3e72009-07-07 23:09:34 +0000292 = dyn_cast<TemplateSpecializationType>(Arg)) {
293 // Perform template argument deduction for the template name.
294 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000295 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000296 Param->getTemplateName(),
297 SpecArg->getTemplateName(),
298 Info, Deduced))
299 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000300
Mike Stump11289f42009-09-09 15:08:12 +0000301
Douglas Gregore81f3e72009-07-07 23:09:34 +0000302 // Perform template argument deduction on each template
303 // argument.
Douglas Gregoradee3e32009-11-11 23:06:43 +0000304 unsigned NumArgs = std::min(SpecArg->getNumArgs(), Param->getNumArgs());
Douglas Gregore81f3e72009-07-07 23:09:34 +0000305 for (unsigned I = 0; I != NumArgs; ++I)
306 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000307 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000308 Param->getArg(I),
309 SpecArg->getArg(I),
310 Info, Deduced))
311 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000312
Douglas Gregore81f3e72009-07-07 23:09:34 +0000313 return Sema::TDK_Success;
314 }
Mike Stump11289f42009-09-09 15:08:12 +0000315
Douglas Gregore81f3e72009-07-07 23:09:34 +0000316 // If the argument type is a class template specialization, we
317 // perform template argument deduction using its template
318 // arguments.
319 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
320 if (!RecordArg)
321 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000322
323 ClassTemplateSpecializationDecl *SpecArg
Douglas Gregore81f3e72009-07-07 23:09:34 +0000324 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
325 if (!SpecArg)
326 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000327
Douglas Gregore81f3e72009-07-07 23:09:34 +0000328 // Perform template argument deduction for the template name.
329 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000330 = DeduceTemplateArguments(S,
Douglas Gregoradee3e32009-11-11 23:06:43 +0000331 TemplateParams,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000332 Param->getTemplateName(),
333 TemplateName(SpecArg->getSpecializedTemplate()),
334 Info, Deduced))
335 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000336
Douglas Gregore81f3e72009-07-07 23:09:34 +0000337 unsigned NumArgs = Param->getNumArgs();
338 const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
339 if (NumArgs != ArgArgs.size())
340 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000341
Douglas Gregore81f3e72009-07-07 23:09:34 +0000342 for (unsigned I = 0; I != NumArgs; ++I)
Mike Stump11289f42009-09-09 15:08:12 +0000343 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000344 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000345 Param->getArg(I),
346 ArgArgs.get(I),
347 Info, Deduced))
348 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000349
Douglas Gregore81f3e72009-07-07 23:09:34 +0000350 return Sema::TDK_Success;
351}
352
John McCall08569062010-08-28 22:14:41 +0000353/// \brief Determines whether the given type is an opaque type that
354/// might be more qualified when instantiated.
355static bool IsPossiblyOpaquelyQualifiedType(QualType T) {
356 switch (T->getTypeClass()) {
357 case Type::TypeOfExpr:
358 case Type::TypeOf:
359 case Type::DependentName:
360 case Type::Decltype:
361 case Type::UnresolvedUsing:
362 return true;
363
364 case Type::ConstantArray:
365 case Type::IncompleteArray:
366 case Type::VariableArray:
367 case Type::DependentSizedArray:
368 return IsPossiblyOpaquelyQualifiedType(
369 cast<ArrayType>(T)->getElementType());
370
371 default:
372 return false;
373 }
374}
375
Douglas Gregorcceb9752009-06-26 18:27:22 +0000376/// \brief Deduce the template arguments by comparing the parameter type and
377/// the argument type (C++ [temp.deduct.type]).
378///
Chandler Carruthc1263112010-02-07 21:33:28 +0000379/// \param S the semantic analysis object within which we are deducing
Douglas Gregorcceb9752009-06-26 18:27:22 +0000380///
381/// \param TemplateParams the template parameters that we are deducing
382///
383/// \param ParamIn the parameter type
384///
385/// \param ArgIn the argument type
386///
387/// \param Info information about the template argument deduction itself
388///
389/// \param Deduced the deduced template arguments
390///
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000391/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
Mike Stump11289f42009-09-09 15:08:12 +0000392/// how template argument deduction is performed.
Douglas Gregorcceb9752009-06-26 18:27:22 +0000393///
394/// \returns the result of template argument deduction so far. Note that a
395/// "success" result means that template argument deduction has not yet failed,
396/// but it may still fail, later, for other reasons.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000397static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000398DeduceTemplateArguments(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000399 TemplateParameterList *TemplateParams,
400 QualType ParamIn, QualType ArgIn,
John McCall19c1bfd2010-08-25 05:32:35 +0000401 TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000402 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000403 unsigned TDF) {
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000404 // We only want to look at the canonical types, since typedefs and
405 // sugar are not part of template argument deduction.
Chandler Carruthc1263112010-02-07 21:33:28 +0000406 QualType Param = S.Context.getCanonicalType(ParamIn);
407 QualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000408
Douglas Gregorcceb9752009-06-26 18:27:22 +0000409 // C++0x [temp.deduct.call]p4 bullet 1:
410 // - If the original P is a reference type, the deduced A (i.e., the type
Mike Stump11289f42009-09-09 15:08:12 +0000411 // referred to by the reference) can be more cv-qualified than the
Douglas Gregorcceb9752009-06-26 18:27:22 +0000412 // transformed A.
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000413 if (TDF & TDF_ParamWithReferenceType) {
Chandler Carruthc712ce12009-12-30 04:10:01 +0000414 Qualifiers Quals;
Chandler Carruthc1263112010-02-07 21:33:28 +0000415 QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals);
Chandler Carruthc712ce12009-12-30 04:10:01 +0000416 Quals.setCVRQualifiers(Quals.getCVRQualifiers() &
417 Arg.getCVRQualifiersThroughArrayTypes());
Chandler Carruthc1263112010-02-07 21:33:28 +0000418 Param = S.Context.getQualifiedType(UnqualParam, Quals);
Douglas Gregorcceb9752009-06-26 18:27:22 +0000419 }
Mike Stump11289f42009-09-09 15:08:12 +0000420
Douglas Gregor705c9002009-06-26 20:57:09 +0000421 // If the parameter type is not dependent, there is nothing to deduce.
Douglas Gregor406f6342009-09-14 20:00:47 +0000422 if (!Param->isDependentType()) {
423 if (!(TDF & TDF_SkipNonDependent) && Param != Arg) {
424
425 return Sema::TDK_NonDeducedMismatch;
426 }
427
Douglas Gregor705c9002009-06-26 20:57:09 +0000428 return Sema::TDK_Success;
Douglas Gregor406f6342009-09-14 20:00:47 +0000429 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000430
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000431 // C++ [temp.deduct.type]p9:
Mike Stump11289f42009-09-09 15:08:12 +0000432 // A template type argument T, a template template argument TT or a
433 // template non-type argument i can be deduced if P and A have one of
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000434 // the following forms:
435 //
436 // T
437 // cv-list T
Mike Stump11289f42009-09-09 15:08:12 +0000438 if (const TemplateTypeParmType *TemplateTypeParm
John McCall9dd450b2009-09-21 23:43:11 +0000439 = Param->getAs<TemplateTypeParmType>()) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000440 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregord6605db2009-07-22 21:30:48 +0000441 bool RecanonicalizeArg = false;
Mike Stump11289f42009-09-09 15:08:12 +0000442
Douglas Gregor60454822009-07-22 20:02:25 +0000443 // If the argument type is an array type, move the qualifiers up to the
444 // top level, so they can be matched with the qualifiers on the parameter.
445 // FIXME: address spaces, ObjC GC qualifiers
Douglas Gregord6605db2009-07-22 21:30:48 +0000446 if (isa<ArrayType>(Arg)) {
John McCall8ccfcb52009-09-24 19:53:00 +0000447 Qualifiers Quals;
Chandler Carruthc1263112010-02-07 21:33:28 +0000448 Arg = S.Context.getUnqualifiedArrayType(Arg, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +0000449 if (Quals) {
Chandler Carruthc1263112010-02-07 21:33:28 +0000450 Arg = S.Context.getQualifiedType(Arg, Quals);
Douglas Gregord6605db2009-07-22 21:30:48 +0000451 RecanonicalizeArg = true;
452 }
453 }
Mike Stump11289f42009-09-09 15:08:12 +0000454
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000455 // The argument type can not be less qualified than the parameter
456 // type.
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000457 if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000458 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
John McCall42d7d192010-08-05 09:05:08 +0000459 Info.FirstArg = TemplateArgument(Param);
John McCall0ad16662009-10-29 08:12:44 +0000460 Info.SecondArg = TemplateArgument(Arg);
John McCall42d7d192010-08-05 09:05:08 +0000461 return Sema::TDK_Underqualified;
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000462 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000463
464 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
Chandler Carruthc1263112010-02-07 21:33:28 +0000465 assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function");
John McCall8ccfcb52009-09-24 19:53:00 +0000466 QualType DeducedType = Arg;
John McCall717d9b02010-12-10 11:01:00 +0000467
468 // local manipulation is okay because it's canonical
469 DeducedType.removeLocalCVRQualifiers(Param.getCVRQualifiers());
Douglas Gregord6605db2009-07-22 21:30:48 +0000470 if (RecanonicalizeArg)
Chandler Carruthc1263112010-02-07 21:33:28 +0000471 DeducedType = S.Context.getCanonicalType(DeducedType);
Mike Stump11289f42009-09-09 15:08:12 +0000472
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000473 if (Deduced[Index].isNull())
John McCall0ad16662009-10-29 08:12:44 +0000474 Deduced[Index] = TemplateArgument(DeducedType);
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000475 else {
Mike Stump11289f42009-09-09 15:08:12 +0000476 // C++ [temp.deduct.type]p2:
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000477 // [...] If type deduction cannot be done for any P/A pair, or if for
Mike Stump11289f42009-09-09 15:08:12 +0000478 // any pair the deduction leads to more than one possible set of
479 // deduced values, or if different pairs yield different deduced
480 // values, or if any template argument remains neither deduced nor
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000481 // explicitly specified, template argument deduction fails.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000482 if (Deduced[Index].getAsType() != DeducedType) {
Mike Stump11289f42009-09-09 15:08:12 +0000483 Info.Param
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000484 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
485 Info.FirstArg = Deduced[Index];
John McCall0ad16662009-10-29 08:12:44 +0000486 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000487 return Sema::TDK_Inconsistent;
488 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000489 }
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000490 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000491 }
492
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000493 // Set up the template argument deduction information for a failure.
John McCall0ad16662009-10-29 08:12:44 +0000494 Info.FirstArg = TemplateArgument(ParamIn);
495 Info.SecondArg = TemplateArgument(ArgIn);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000496
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000497 // Check the cv-qualifiers on the parameter and argument types.
498 if (!(TDF & TDF_IgnoreQualifiers)) {
499 if (TDF & TDF_ParamWithReferenceType) {
500 if (Param.isMoreQualifiedThan(Arg))
501 return Sema::TDK_NonDeducedMismatch;
John McCall08569062010-08-28 22:14:41 +0000502 } else if (!IsPossiblyOpaquelyQualifiedType(Param)) {
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000503 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Mike Stump11289f42009-09-09 15:08:12 +0000504 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000505 }
506 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000507
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000508 switch (Param->getTypeClass()) {
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000509 // No deduction possible for these types
510 case Type::Builtin:
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000511 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000512
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000513 // T *
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000514 case Type::Pointer: {
John McCallbb4ea812010-05-13 07:48:05 +0000515 QualType PointeeType;
516 if (const PointerType *PointerArg = Arg->getAs<PointerType>()) {
517 PointeeType = PointerArg->getPointeeType();
518 } else if (const ObjCObjectPointerType *PointerArg
519 = Arg->getAs<ObjCObjectPointerType>()) {
520 PointeeType = PointerArg->getPointeeType();
521 } else {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000522 return Sema::TDK_NonDeducedMismatch;
John McCallbb4ea812010-05-13 07:48:05 +0000523 }
Mike Stump11289f42009-09-09 15:08:12 +0000524
Douglas Gregorfc516c92009-06-26 23:27:24 +0000525 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Chandler Carruthc1263112010-02-07 21:33:28 +0000526 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000527 cast<PointerType>(Param)->getPointeeType(),
John McCallbb4ea812010-05-13 07:48:05 +0000528 PointeeType,
Douglas Gregorfc516c92009-06-26 23:27:24 +0000529 Info, Deduced, SubTDF);
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000530 }
Mike Stump11289f42009-09-09 15:08:12 +0000531
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000532 // T &
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000533 case Type::LValueReference: {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000534 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000535 if (!ReferenceArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000536 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000537
Chandler Carruthc1263112010-02-07 21:33:28 +0000538 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000539 cast<LValueReferenceType>(Param)->getPointeeType(),
540 ReferenceArg->getPointeeType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000541 Info, Deduced, 0);
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000542 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000543
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000544 // T && [C++0x]
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000545 case Type::RValueReference: {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000546 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000547 if (!ReferenceArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000548 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000549
Chandler Carruthc1263112010-02-07 21:33:28 +0000550 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000551 cast<RValueReferenceType>(Param)->getPointeeType(),
552 ReferenceArg->getPointeeType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000553 Info, Deduced, 0);
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000554 }
Mike Stump11289f42009-09-09 15:08:12 +0000555
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000556 // T [] (implied, but not stated explicitly)
Anders Carlsson35533d12009-06-04 04:11:30 +0000557 case Type::IncompleteArray: {
Mike Stump11289f42009-09-09 15:08:12 +0000558 const IncompleteArrayType *IncompleteArrayArg =
Chandler Carruthc1263112010-02-07 21:33:28 +0000559 S.Context.getAsIncompleteArrayType(Arg);
Anders Carlsson35533d12009-06-04 04:11:30 +0000560 if (!IncompleteArrayArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000561 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000562
John McCallf7332682010-08-19 00:20:19 +0000563 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
Chandler Carruthc1263112010-02-07 21:33:28 +0000564 return DeduceTemplateArguments(S, TemplateParams,
565 S.Context.getAsIncompleteArrayType(Param)->getElementType(),
Anders Carlsson35533d12009-06-04 04:11:30 +0000566 IncompleteArrayArg->getElementType(),
John McCallf7332682010-08-19 00:20:19 +0000567 Info, Deduced, SubTDF);
Anders Carlsson35533d12009-06-04 04:11:30 +0000568 }
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000569
570 // T [integer-constant]
Anders Carlsson35533d12009-06-04 04:11:30 +0000571 case Type::ConstantArray: {
Mike Stump11289f42009-09-09 15:08:12 +0000572 const ConstantArrayType *ConstantArrayArg =
Chandler Carruthc1263112010-02-07 21:33:28 +0000573 S.Context.getAsConstantArrayType(Arg);
Anders Carlsson35533d12009-06-04 04:11:30 +0000574 if (!ConstantArrayArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000575 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000576
577 const ConstantArrayType *ConstantArrayParm =
Chandler Carruthc1263112010-02-07 21:33:28 +0000578 S.Context.getAsConstantArrayType(Param);
Anders Carlsson35533d12009-06-04 04:11:30 +0000579 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000580 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000581
John McCallf7332682010-08-19 00:20:19 +0000582 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
Chandler Carruthc1263112010-02-07 21:33:28 +0000583 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson35533d12009-06-04 04:11:30 +0000584 ConstantArrayParm->getElementType(),
585 ConstantArrayArg->getElementType(),
John McCallf7332682010-08-19 00:20:19 +0000586 Info, Deduced, SubTDF);
Anders Carlsson35533d12009-06-04 04:11:30 +0000587 }
588
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000589 // type [i]
590 case Type::DependentSizedArray: {
Chandler Carruthc1263112010-02-07 21:33:28 +0000591 const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000592 if (!ArrayArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000593 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000594
John McCallf7332682010-08-19 00:20:19 +0000595 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
596
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000597 // Check the element type of the arrays
598 const DependentSizedArrayType *DependentArrayParm
Chandler Carruthc1263112010-02-07 21:33:28 +0000599 = S.Context.getAsDependentSizedArrayType(Param);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000600 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000601 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000602 DependentArrayParm->getElementType(),
603 ArrayArg->getElementType(),
John McCallf7332682010-08-19 00:20:19 +0000604 Info, Deduced, SubTDF))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000605 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000606
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000607 // Determine the array bound is something we can deduce.
Mike Stump11289f42009-09-09 15:08:12 +0000608 NonTypeTemplateParmDecl *NTTP
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000609 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
610 if (!NTTP)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000611 return Sema::TDK_Success;
Mike Stump11289f42009-09-09 15:08:12 +0000612
613 // We can perform template argument deduction for the given non-type
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000614 // template parameter.
Mike Stump11289f42009-09-09 15:08:12 +0000615 assert(NTTP->getDepth() == 0 &&
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000616 "Cannot deduce non-type template argument at depth > 0");
Mike Stump11289f42009-09-09 15:08:12 +0000617 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson3a106e02009-06-16 22:44:31 +0000618 = dyn_cast<ConstantArrayType>(ArrayArg)) {
619 llvm::APSInt Size(ConstantArrayArg->getSize());
Douglas Gregor0a29a052010-03-26 05:50:28 +0000620 return DeduceNonTypeTemplateArgument(S, NTTP, Size,
621 S.Context.getSizeType(),
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000622 /*ArrayBound=*/true,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000623 Info, Deduced);
Anders Carlsson3a106e02009-06-16 22:44:31 +0000624 }
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000625 if (const DependentSizedArrayType *DependentArrayArg
626 = dyn_cast<DependentSizedArrayType>(ArrayArg))
Chandler Carruthc1263112010-02-07 21:33:28 +0000627 return DeduceNonTypeTemplateArgument(S, NTTP,
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000628 DependentArrayArg->getSizeExpr(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000629 Info, Deduced);
Mike Stump11289f42009-09-09 15:08:12 +0000630
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000631 // Incomplete type does not match a dependently-sized array type
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000632 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000633 }
Mike Stump11289f42009-09-09 15:08:12 +0000634
635 // type(*)(T)
636 // T(*)()
637 // T(*)(T)
Anders Carlsson2128ec72009-06-08 15:19:08 +0000638 case Type::FunctionProto: {
Mike Stump11289f42009-09-09 15:08:12 +0000639 const FunctionProtoType *FunctionProtoArg =
Anders Carlsson2128ec72009-06-08 15:19:08 +0000640 dyn_cast<FunctionProtoType>(Arg);
641 if (!FunctionProtoArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000642 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000643
644 const FunctionProtoType *FunctionProtoParam =
Anders Carlsson2128ec72009-06-08 15:19:08 +0000645 cast<FunctionProtoType>(Param);
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000646
Mike Stump11289f42009-09-09 15:08:12 +0000647 if (FunctionProtoParam->getTypeQuals() !=
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000648 FunctionProtoArg->getTypeQuals())
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000649 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000650
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000651 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000652 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000653
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000654 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000655 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000656
Anders Carlsson2128ec72009-06-08 15:19:08 +0000657 // Check return types.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000658 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000659 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000660 FunctionProtoParam->getResultType(),
661 FunctionProtoArg->getResultType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000662 Info, Deduced, 0))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000663 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000664
Anders Carlsson2128ec72009-06-08 15:19:08 +0000665 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
666 // Check argument types.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000667 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000668 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000669 FunctionProtoParam->getArgType(I),
670 FunctionProtoArg->getArgType(I),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000671 Info, Deduced, 0))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000672 return Result;
Anders Carlsson2128ec72009-06-08 15:19:08 +0000673 }
Mike Stump11289f42009-09-09 15:08:12 +0000674
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000675 return Sema::TDK_Success;
Anders Carlsson2128ec72009-06-08 15:19:08 +0000676 }
Mike Stump11289f42009-09-09 15:08:12 +0000677
John McCalle78aac42010-03-10 03:28:59 +0000678 case Type::InjectedClassName: {
679 // Treat a template's injected-class-name as if the template
680 // specialization type had been used.
John McCall2408e322010-04-27 00:57:59 +0000681 Param = cast<InjectedClassNameType>(Param)
682 ->getInjectedSpecializationType();
John McCalle78aac42010-03-10 03:28:59 +0000683 assert(isa<TemplateSpecializationType>(Param) &&
684 "injected class name is not a template specialization type");
685 // fall through
686 }
687
Douglas Gregor705c9002009-06-26 20:57:09 +0000688 // template-name<T> (where template-name refers to a class template)
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000689 // template-name<i>
Douglas Gregoradee3e32009-11-11 23:06:43 +0000690 // TT<T>
691 // TT<i>
692 // TT<>
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000693 case Type::TemplateSpecialization: {
694 const TemplateSpecializationType *SpecParam
695 = cast<TemplateSpecializationType>(Param);
Mike Stump11289f42009-09-09 15:08:12 +0000696
Douglas Gregore81f3e72009-07-07 23:09:34 +0000697 // Try to deduce template arguments from the template-id.
698 Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000699 = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000700 Info, Deduced);
Mike Stump11289f42009-09-09 15:08:12 +0000701
Douglas Gregor42909752009-09-30 22:13:51 +0000702 if (Result && (TDF & TDF_DerivedClass)) {
Douglas Gregore81f3e72009-07-07 23:09:34 +0000703 // C++ [temp.deduct.call]p3b3:
704 // If P is a class, and P has the form template-id, then A can be a
705 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump11289f42009-09-09 15:08:12 +0000706 // class of the form template-id, A can be a pointer to a derived
Douglas Gregore81f3e72009-07-07 23:09:34 +0000707 // class pointed to by the deduced A.
708 //
709 // More importantly:
Mike Stump11289f42009-09-09 15:08:12 +0000710 // These alternatives are considered only if type deduction would
Douglas Gregore81f3e72009-07-07 23:09:34 +0000711 // otherwise fail.
Chandler Carruthc1263112010-02-07 21:33:28 +0000712 if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
713 // We cannot inspect base classes as part of deduction when the type
714 // is incomplete, so either instantiate any templates necessary to
715 // complete the type, or skip over it if it cannot be completed.
John McCallbc077cf2010-02-08 23:07:23 +0000716 if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
Chandler Carruthc1263112010-02-07 21:33:28 +0000717 return Result;
718
Douglas Gregore81f3e72009-07-07 23:09:34 +0000719 // Use data recursion to crawl through the list of base classes.
Mike Stump11289f42009-09-09 15:08:12 +0000720 // Visited contains the set of nodes we have already visited, while
Douglas Gregore81f3e72009-07-07 23:09:34 +0000721 // ToVisit is our stack of records that we still need to visit.
722 llvm::SmallPtrSet<const RecordType *, 8> Visited;
723 llvm::SmallVector<const RecordType *, 8> ToVisit;
724 ToVisit.push_back(RecordT);
725 bool Successful = false;
Douglas Gregore0f7a8a2010-11-02 00:02:34 +0000726 llvm::SmallVectorImpl<DeducedTemplateArgument> DeducedOrig(0);
727 DeducedOrig = Deduced;
Douglas Gregore81f3e72009-07-07 23:09:34 +0000728 while (!ToVisit.empty()) {
729 // Retrieve the next class in the inheritance hierarchy.
730 const RecordType *NextT = ToVisit.back();
731 ToVisit.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000732
Douglas Gregore81f3e72009-07-07 23:09:34 +0000733 // If we have already seen this type, skip it.
734 if (!Visited.insert(NextT))
735 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000736
Douglas Gregore81f3e72009-07-07 23:09:34 +0000737 // If this is a base class, try to perform template argument
738 // deduction from it.
739 if (NextT != RecordT) {
740 Sema::TemplateDeductionResult BaseResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000741 = DeduceTemplateArguments(S, TemplateParams, SpecParam,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000742 QualType(NextT, 0), Info, Deduced);
Mike Stump11289f42009-09-09 15:08:12 +0000743
Douglas Gregore81f3e72009-07-07 23:09:34 +0000744 // If template argument deduction for this base was successful,
Douglas Gregore0f7a8a2010-11-02 00:02:34 +0000745 // note that we had some success. Otherwise, ignore any deductions
746 // from this base class.
747 if (BaseResult == Sema::TDK_Success) {
Douglas Gregore81f3e72009-07-07 23:09:34 +0000748 Successful = true;
Douglas Gregore0f7a8a2010-11-02 00:02:34 +0000749 DeducedOrig = Deduced;
750 }
751 else
752 Deduced = DeducedOrig;
Douglas Gregore81f3e72009-07-07 23:09:34 +0000753 }
Mike Stump11289f42009-09-09 15:08:12 +0000754
Douglas Gregore81f3e72009-07-07 23:09:34 +0000755 // Visit base classes
756 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
757 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
758 BaseEnd = Next->bases_end();
Sebastian Redl1054fae2009-10-25 17:03:50 +0000759 Base != BaseEnd; ++Base) {
Mike Stump11289f42009-09-09 15:08:12 +0000760 assert(Base->getType()->isRecordType() &&
Douglas Gregore81f3e72009-07-07 23:09:34 +0000761 "Base class that isn't a record?");
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000762 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregore81f3e72009-07-07 23:09:34 +0000763 }
764 }
Mike Stump11289f42009-09-09 15:08:12 +0000765
Douglas Gregore81f3e72009-07-07 23:09:34 +0000766 if (Successful)
767 return Sema::TDK_Success;
768 }
Mike Stump11289f42009-09-09 15:08:12 +0000769
Douglas Gregore81f3e72009-07-07 23:09:34 +0000770 }
Mike Stump11289f42009-09-09 15:08:12 +0000771
Douglas Gregore81f3e72009-07-07 23:09:34 +0000772 return Result;
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000773 }
774
Douglas Gregor637d9982009-06-10 23:47:09 +0000775 // T type::*
776 // T T::*
777 // T (type::*)()
778 // type (T::*)()
779 // type (type::*)(T)
780 // type (T::*)(T)
781 // T (type::*)(T)
782 // T (T::*)()
783 // T (T::*)(T)
784 case Type::MemberPointer: {
785 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
786 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
787 if (!MemPtrArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000788 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637d9982009-06-10 23:47:09 +0000789
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000790 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000791 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000792 MemPtrParam->getPointeeType(),
793 MemPtrArg->getPointeeType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000794 Info, Deduced,
795 TDF & TDF_IgnoreQualifiers))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000796 return Result;
797
Chandler Carruthc1263112010-02-07 21:33:28 +0000798 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000799 QualType(MemPtrParam->getClass(), 0),
800 QualType(MemPtrArg->getClass(), 0),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000801 Info, Deduced, 0);
Douglas Gregor637d9982009-06-10 23:47:09 +0000802 }
803
Anders Carlsson15f1dd12009-06-12 22:56:54 +0000804 // (clang extension)
805 //
Mike Stump11289f42009-09-09 15:08:12 +0000806 // type(^)(T)
807 // T(^)()
808 // T(^)(T)
Anders Carlssona767eee2009-06-12 16:23:10 +0000809 case Type::BlockPointer: {
810 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
811 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump11289f42009-09-09 15:08:12 +0000812
Anders Carlssona767eee2009-06-12 16:23:10 +0000813 if (!BlockPtrArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000814 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000815
Chandler Carruthc1263112010-02-07 21:33:28 +0000816 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlssona767eee2009-06-12 16:23:10 +0000817 BlockPtrParam->getPointeeType(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000818 BlockPtrArg->getPointeeType(), Info,
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000819 Deduced, 0);
Anders Carlssona767eee2009-06-12 16:23:10 +0000820 }
821
Douglas Gregor637d9982009-06-10 23:47:09 +0000822 case Type::TypeOfExpr:
823 case Type::TypeOf:
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000824 case Type::DependentName:
Douglas Gregor637d9982009-06-10 23:47:09 +0000825 // No template argument deduction for these types
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000826 return Sema::TDK_Success;
Douglas Gregor637d9982009-06-10 23:47:09 +0000827
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000828 default:
829 break;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000830 }
831
832 // FIXME: Many more cases to go (to go).
Douglas Gregor705c9002009-06-26 20:57:09 +0000833 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000834}
835
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000836static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000837DeduceTemplateArguments(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000838 TemplateParameterList *TemplateParams,
839 const TemplateArgument &Param,
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000840 const TemplateArgument &Arg,
John McCall19c1bfd2010-08-25 05:32:35 +0000841 TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000842 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000843 switch (Param.getKind()) {
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000844 case TemplateArgument::Null:
845 assert(false && "Null template argument in parameter list");
846 break;
Mike Stump11289f42009-09-09 15:08:12 +0000847
848 case TemplateArgument::Type:
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000849 if (Arg.getKind() == TemplateArgument::Type)
Chandler Carruthc1263112010-02-07 21:33:28 +0000850 return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000851 Arg.getAsType(), Info, Deduced, 0);
852 Info.FirstArg = Param;
853 Info.SecondArg = Arg;
854 return Sema::TDK_NonDeducedMismatch;
855
856 case TemplateArgument::Template:
Douglas Gregoradee3e32009-11-11 23:06:43 +0000857 if (Arg.getKind() == TemplateArgument::Template)
Chandler Carruthc1263112010-02-07 21:33:28 +0000858 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000859 Param.getAsTemplate(),
Douglas Gregoradee3e32009-11-11 23:06:43 +0000860 Arg.getAsTemplate(), Info, Deduced);
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000861 Info.FirstArg = Param;
862 Info.SecondArg = Arg;
863 return Sema::TDK_NonDeducedMismatch;
864
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000865 case TemplateArgument::Declaration:
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000866 if (Arg.getKind() == TemplateArgument::Declaration &&
867 Param.getAsDecl()->getCanonicalDecl() ==
868 Arg.getAsDecl()->getCanonicalDecl())
869 return Sema::TDK_Success;
870
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000871 Info.FirstArg = Param;
872 Info.SecondArg = Arg;
873 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000874
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000875 case TemplateArgument::Integral:
876 if (Arg.getKind() == TemplateArgument::Integral) {
Douglas Gregor0a29a052010-03-26 05:50:28 +0000877 if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral()))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000878 return Sema::TDK_Success;
879
880 Info.FirstArg = Param;
881 Info.SecondArg = Arg;
882 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000883 }
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000884
885 if (Arg.getKind() == TemplateArgument::Expression) {
886 Info.FirstArg = Param;
887 Info.SecondArg = Arg;
888 return Sema::TDK_NonDeducedMismatch;
889 }
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000890
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000891 Info.FirstArg = Param;
892 Info.SecondArg = Arg;
893 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000894
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000895 case TemplateArgument::Expression: {
Mike Stump11289f42009-09-09 15:08:12 +0000896 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000897 = getDeducedParameterFromExpr(Param.getAsExpr())) {
898 if (Arg.getKind() == TemplateArgument::Integral)
Chandler Carruthc1263112010-02-07 21:33:28 +0000899 return DeduceNonTypeTemplateArgument(S, NTTP,
Mike Stump11289f42009-09-09 15:08:12 +0000900 *Arg.getAsIntegral(),
Douglas Gregor0a29a052010-03-26 05:50:28 +0000901 Arg.getIntegralType(),
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000902 /*ArrayBound=*/false,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000903 Info, Deduced);
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000904 if (Arg.getKind() == TemplateArgument::Expression)
Chandler Carruthc1263112010-02-07 21:33:28 +0000905 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000906 Info, Deduced);
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000907 if (Arg.getKind() == TemplateArgument::Declaration)
Chandler Carruthc1263112010-02-07 21:33:28 +0000908 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000909 Info, Deduced);
910
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000911 Info.FirstArg = Param;
912 Info.SecondArg = Arg;
913 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000914 }
Mike Stump11289f42009-09-09 15:08:12 +0000915
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000916 // Can't deduce anything, but that's okay.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000917 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000918 }
Anders Carlssonbc343912009-06-15 17:04:53 +0000919 case TemplateArgument::Pack:
Douglas Gregor0192c232010-12-20 16:52:59 +0000920 // FIXME: Variadic templates
Anders Carlssonbc343912009-06-15 17:04:53 +0000921 assert(0 && "FIXME: Implement!");
922 break;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000923 }
Mike Stump11289f42009-09-09 15:08:12 +0000924
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000925 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000926}
927
Mike Stump11289f42009-09-09 15:08:12 +0000928static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000929DeduceTemplateArguments(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000930 TemplateParameterList *TemplateParams,
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000931 const TemplateArgumentList &ParamList,
932 const TemplateArgumentList &ArgList,
John McCall19c1bfd2010-08-25 05:32:35 +0000933 TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000934 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000935 assert(ParamList.size() == ArgList.size());
936 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000937 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000938 = DeduceTemplateArguments(S, TemplateParams,
Mike Stump11289f42009-09-09 15:08:12 +0000939 ParamList[I], ArgList[I],
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000940 Info, Deduced))
941 return Result;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000942 }
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000943 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000944}
945
Douglas Gregor705c9002009-06-26 20:57:09 +0000946/// \brief Determine whether two template arguments are the same.
Mike Stump11289f42009-09-09 15:08:12 +0000947static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregor705c9002009-06-26 20:57:09 +0000948 const TemplateArgument &X,
949 const TemplateArgument &Y) {
950 if (X.getKind() != Y.getKind())
951 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000952
Douglas Gregor705c9002009-06-26 20:57:09 +0000953 switch (X.getKind()) {
954 case TemplateArgument::Null:
955 assert(false && "Comparing NULL template argument");
956 break;
Mike Stump11289f42009-09-09 15:08:12 +0000957
Douglas Gregor705c9002009-06-26 20:57:09 +0000958 case TemplateArgument::Type:
959 return Context.getCanonicalType(X.getAsType()) ==
960 Context.getCanonicalType(Y.getAsType());
Mike Stump11289f42009-09-09 15:08:12 +0000961
Douglas Gregor705c9002009-06-26 20:57:09 +0000962 case TemplateArgument::Declaration:
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +0000963 return X.getAsDecl()->getCanonicalDecl() ==
964 Y.getAsDecl()->getCanonicalDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000965
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000966 case TemplateArgument::Template:
967 return Context.getCanonicalTemplateName(X.getAsTemplate())
968 .getAsVoidPointer() ==
969 Context.getCanonicalTemplateName(Y.getAsTemplate())
970 .getAsVoidPointer();
971
Douglas Gregor705c9002009-06-26 20:57:09 +0000972 case TemplateArgument::Integral:
973 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump11289f42009-09-09 15:08:12 +0000974
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000975 case TemplateArgument::Expression: {
976 llvm::FoldingSetNodeID XID, YID;
977 X.getAsExpr()->Profile(XID, Context, true);
978 Y.getAsExpr()->Profile(YID, Context, true);
979 return XID == YID;
980 }
Mike Stump11289f42009-09-09 15:08:12 +0000981
Douglas Gregor705c9002009-06-26 20:57:09 +0000982 case TemplateArgument::Pack:
983 if (X.pack_size() != Y.pack_size())
984 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000985
986 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
987 XPEnd = X.pack_end(),
Douglas Gregor705c9002009-06-26 20:57:09 +0000988 YP = Y.pack_begin();
Mike Stump11289f42009-09-09 15:08:12 +0000989 XP != XPEnd; ++XP, ++YP)
Douglas Gregor705c9002009-06-26 20:57:09 +0000990 if (!isSameTemplateArg(Context, *XP, *YP))
991 return false;
992
993 return true;
994 }
995
996 return false;
997}
998
999/// \brief Helper function to build a TemplateParameter when we don't
1000/// know its type statically.
1001static TemplateParameter makeTemplateParameter(Decl *D) {
1002 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
1003 return TemplateParameter(TTP);
1004 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
1005 return TemplateParameter(NTTP);
Mike Stump11289f42009-09-09 15:08:12 +00001006
Douglas Gregor705c9002009-06-26 20:57:09 +00001007 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
1008}
1009
Douglas Gregor684268d2010-04-29 06:21:43 +00001010/// Complete template argument deduction for a class template partial
1011/// specialization.
1012static Sema::TemplateDeductionResult
1013FinishTemplateArgumentDeduction(Sema &S,
1014 ClassTemplatePartialSpecializationDecl *Partial,
1015 const TemplateArgumentList &TemplateArgs,
1016 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
John McCall19c1bfd2010-08-25 05:32:35 +00001017 TemplateDeductionInfo &Info) {
Douglas Gregor684268d2010-04-29 06:21:43 +00001018 // Trap errors.
1019 Sema::SFINAETrap Trap(S);
1020
1021 Sema::ContextRAII SavedContext(S, Partial);
1022
1023 // C++ [temp.deduct.type]p2:
1024 // [...] or if any template argument remains neither deduced nor
1025 // explicitly specified, template argument deduction fails.
Douglas Gregor1ccc8412010-11-07 23:05:16 +00001026 llvm::SmallVector<TemplateArgument, 4> Builder;
Douglas Gregor684268d2010-04-29 06:21:43 +00001027 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
1028 if (Deduced[I].isNull()) {
1029 Decl *Param
Douglas Gregor6e9cf632010-10-12 18:51:08 +00001030 = const_cast<NamedDecl *>(
Douglas Gregor684268d2010-04-29 06:21:43 +00001031 Partial->getTemplateParameters()->getParam(I));
1032 Info.Param = makeTemplateParameter(Param);
1033 return Sema::TDK_Incomplete;
1034 }
1035
Douglas Gregor1ccc8412010-11-07 23:05:16 +00001036 Builder.push_back(Deduced[I]);
Douglas Gregor684268d2010-04-29 06:21:43 +00001037 }
1038
1039 // Form the template argument list from the deduced template arguments.
1040 TemplateArgumentList *DeducedArgumentList
Douglas Gregor1ccc8412010-11-07 23:05:16 +00001041 = TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
1042 Builder.size());
1043
Douglas Gregor684268d2010-04-29 06:21:43 +00001044 Info.reset(DeducedArgumentList);
1045
1046 // Substitute the deduced template arguments into the template
1047 // arguments of the class template partial specialization, and
1048 // verify that the instantiated template arguments are both valid
1049 // and are equivalent to the template arguments originally provided
1050 // to the class template.
1051 // FIXME: Do we have to correct the types of deduced non-type template
1052 // arguments (in particular, integral non-type template arguments?).
John McCall19c1bfd2010-08-25 05:32:35 +00001053 LocalInstantiationScope InstScope(S);
Douglas Gregor684268d2010-04-29 06:21:43 +00001054 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
1055 const TemplateArgumentLoc *PartialTemplateArgs
1056 = Partial->getTemplateArgsAsWritten();
1057 unsigned N = Partial->getNumTemplateArgsAsWritten();
1058
1059 // Note that we don't provide the langle and rangle locations.
1060 TemplateArgumentListInfo InstArgs;
1061
1062 for (unsigned I = 0; I != N; ++I) {
1063 Decl *Param = const_cast<NamedDecl *>(
1064 ClassTemplate->getTemplateParameters()->getParam(I));
1065 TemplateArgumentLoc InstArg;
1066 if (S.Subst(PartialTemplateArgs[I], InstArg,
1067 MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
1068 Info.Param = makeTemplateParameter(Param);
1069 Info.FirstArg = PartialTemplateArgs[I].getArgument();
1070 return Sema::TDK_SubstitutionFailure;
1071 }
1072 InstArgs.addArgument(InstArg);
1073 }
1074
Douglas Gregor1ccc8412010-11-07 23:05:16 +00001075 llvm::SmallVector<TemplateArgument, 4> ConvertedInstArgs;
Douglas Gregor684268d2010-04-29 06:21:43 +00001076 if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
Douglas Gregord09efd42010-05-08 20:07:26 +00001077 InstArgs, false, ConvertedInstArgs))
Douglas Gregor684268d2010-04-29 06:21:43 +00001078 return Sema::TDK_SubstitutionFailure;
Douglas Gregor684268d2010-04-29 06:21:43 +00001079
Douglas Gregor1ccc8412010-11-07 23:05:16 +00001080 for (unsigned I = 0, E = ConvertedInstArgs.size(); I != E; ++I) {
1081 TemplateArgument InstArg = ConvertedInstArgs.data()[I];
Douglas Gregor684268d2010-04-29 06:21:43 +00001082
1083 Decl *Param = const_cast<NamedDecl *>(
1084 ClassTemplate->getTemplateParameters()->getParam(I));
1085
1086 if (InstArg.getKind() == TemplateArgument::Expression) {
1087 // When the argument is an expression, check the expression result
1088 // against the actual template parameter to get down to the canonical
1089 // template argument.
1090 Expr *InstExpr = InstArg.getAsExpr();
1091 if (NonTypeTemplateParmDecl *NTTP
1092 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1093 if (S.CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
1094 Info.Param = makeTemplateParameter(Param);
1095 Info.FirstArg = Partial->getTemplateArgs()[I];
1096 return Sema::TDK_SubstitutionFailure;
1097 }
1098 }
1099 }
1100
1101 if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
1102 Info.Param = makeTemplateParameter(Param);
1103 Info.FirstArg = TemplateArgs[I];
1104 Info.SecondArg = InstArg;
1105 return Sema::TDK_NonDeducedMismatch;
1106 }
1107 }
1108
1109 if (Trap.hasErrorOccurred())
1110 return Sema::TDK_SubstitutionFailure;
1111
1112 return Sema::TDK_Success;
1113}
1114
Douglas Gregor170bc422009-06-12 22:31:52 +00001115/// \brief Perform template argument deduction to determine whether
1116/// the given template arguments match the given class template
1117/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001118Sema::TemplateDeductionResult
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001119Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001120 const TemplateArgumentList &TemplateArgs,
1121 TemplateDeductionInfo &Info) {
Douglas Gregor170bc422009-06-12 22:31:52 +00001122 // C++ [temp.class.spec.match]p2:
1123 // A partial specialization matches a given actual template
1124 // argument list if the template arguments of the partial
1125 // specialization can be deduced from the actual template argument
1126 // list (14.8.2).
Douglas Gregore1416332009-06-14 08:02:22 +00001127 SFINAETrap Trap(*this);
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001128 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001129 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001130 if (TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +00001131 = ::DeduceTemplateArguments(*this,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001132 Partial->getTemplateParameters(),
Mike Stump11289f42009-09-09 15:08:12 +00001133 Partial->getTemplateArgs(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001134 TemplateArgs, Info, Deduced))
1135 return Result;
Douglas Gregor637d9982009-06-10 23:47:09 +00001136
Douglas Gregor637d9982009-06-10 23:47:09 +00001137 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001138 Deduced.data(), Deduced.size(), Info);
Douglas Gregor637d9982009-06-10 23:47:09 +00001139 if (Inst)
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001140 return TDK_InstantiationDepth;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001141
Douglas Gregore1416332009-06-14 08:02:22 +00001142 if (Trap.hasErrorOccurred())
Douglas Gregor684268d2010-04-29 06:21:43 +00001143 return Sema::TDK_SubstitutionFailure;
1144
1145 return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
1146 Deduced, Info);
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001147}
Douglas Gregor91772d12009-06-13 00:26:55 +00001148
Douglas Gregorfc516c92009-06-26 23:27:24 +00001149/// \brief Determine whether the given type T is a simple-template-id type.
1150static bool isSimpleTemplateIdType(QualType T) {
Mike Stump11289f42009-09-09 15:08:12 +00001151 if (const TemplateSpecializationType *Spec
John McCall9dd450b2009-09-21 23:43:11 +00001152 = T->getAs<TemplateSpecializationType>())
Douglas Gregorfc516c92009-06-26 23:27:24 +00001153 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump11289f42009-09-09 15:08:12 +00001154
Douglas Gregorfc516c92009-06-26 23:27:24 +00001155 return false;
1156}
Douglas Gregor9b146582009-07-08 20:55:45 +00001157
1158/// \brief Substitute the explicitly-provided template arguments into the
1159/// given function template according to C++ [temp.arg.explicit].
1160///
1161/// \param FunctionTemplate the function template into which the explicit
1162/// template arguments will be substituted.
1163///
Mike Stump11289f42009-09-09 15:08:12 +00001164/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor9b146582009-07-08 20:55:45 +00001165/// arguments.
1166///
Mike Stump11289f42009-09-09 15:08:12 +00001167/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor9b146582009-07-08 20:55:45 +00001168/// with the converted and checked explicit template arguments.
1169///
Mike Stump11289f42009-09-09 15:08:12 +00001170/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor9b146582009-07-08 20:55:45 +00001171/// parameters.
1172///
1173/// \param FunctionType if non-NULL, the result type of the function template
1174/// will also be instantiated and the pointed-to value will be updated with
1175/// the instantiated function type.
1176///
1177/// \param Info if substitution fails for any reason, this object will be
1178/// populated with more information about the failure.
1179///
1180/// \returns TDK_Success if substitution was successful, or some failure
1181/// condition.
1182Sema::TemplateDeductionResult
1183Sema::SubstituteExplicitTemplateArguments(
1184 FunctionTemplateDecl *FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001185 const TemplateArgumentListInfo &ExplicitTemplateArgs,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001186 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor9b146582009-07-08 20:55:45 +00001187 llvm::SmallVectorImpl<QualType> &ParamTypes,
1188 QualType *FunctionType,
1189 TemplateDeductionInfo &Info) {
1190 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1191 TemplateParameterList *TemplateParams
1192 = FunctionTemplate->getTemplateParameters();
1193
John McCall6b51f282009-11-23 01:53:49 +00001194 if (ExplicitTemplateArgs.size() == 0) {
Douglas Gregor9b146582009-07-08 20:55:45 +00001195 // No arguments to substitute; just copy over the parameter types and
1196 // fill in the function type.
1197 for (FunctionDecl::param_iterator P = Function->param_begin(),
1198 PEnd = Function->param_end();
1199 P != PEnd;
1200 ++P)
1201 ParamTypes.push_back((*P)->getType());
Mike Stump11289f42009-09-09 15:08:12 +00001202
Douglas Gregor9b146582009-07-08 20:55:45 +00001203 if (FunctionType)
1204 *FunctionType = Function->getType();
1205 return TDK_Success;
1206 }
Mike Stump11289f42009-09-09 15:08:12 +00001207
Douglas Gregor9b146582009-07-08 20:55:45 +00001208 // Substitution of the explicit template arguments into a function template
1209 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump11289f42009-09-09 15:08:12 +00001210 SFINAETrap Trap(*this);
1211
Douglas Gregor9b146582009-07-08 20:55:45 +00001212 // C++ [temp.arg.explicit]p3:
Mike Stump11289f42009-09-09 15:08:12 +00001213 // Template arguments that are present shall be specified in the
1214 // declaration order of their corresponding template-parameters. The
Douglas Gregor9b146582009-07-08 20:55:45 +00001215 // template argument list shall not specify more template-arguments than
Mike Stump11289f42009-09-09 15:08:12 +00001216 // there are corresponding template-parameters.
Douglas Gregor1ccc8412010-11-07 23:05:16 +00001217 llvm::SmallVector<TemplateArgument, 4> Builder;
Mike Stump11289f42009-09-09 15:08:12 +00001218
1219 // Enter a new template instantiation context where we check the
Douglas Gregor9b146582009-07-08 20:55:45 +00001220 // explicitly-specified template arguments against this function template,
1221 // and then substitute them into the function parameter types.
Mike Stump11289f42009-09-09 15:08:12 +00001222 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor9b146582009-07-08 20:55:45 +00001223 FunctionTemplate, Deduced.data(), Deduced.size(),
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001224 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution,
1225 Info);
Douglas Gregor9b146582009-07-08 20:55:45 +00001226 if (Inst)
1227 return TDK_InstantiationDepth;
Mike Stump11289f42009-09-09 15:08:12 +00001228
Douglas Gregor9b146582009-07-08 20:55:45 +00001229 if (CheckTemplateArgumentList(FunctionTemplate,
Douglas Gregor9b146582009-07-08 20:55:45 +00001230 SourceLocation(),
John McCall6b51f282009-11-23 01:53:49 +00001231 ExplicitTemplateArgs,
Douglas Gregor9b146582009-07-08 20:55:45 +00001232 true,
Douglas Gregor1d72edd2010-05-08 19:15:54 +00001233 Builder) || Trap.hasErrorOccurred()) {
Douglas Gregor1ccc8412010-11-07 23:05:16 +00001234 unsigned Index = Builder.size();
Douglas Gregor62c281a2010-05-09 01:26:06 +00001235 if (Index >= TemplateParams->size())
1236 Index = TemplateParams->size() - 1;
1237 Info.Param = makeTemplateParameter(TemplateParams->getParam(Index));
Douglas Gregor9b146582009-07-08 20:55:45 +00001238 return TDK_InvalidExplicitArguments;
Douglas Gregor1d72edd2010-05-08 19:15:54 +00001239 }
Mike Stump11289f42009-09-09 15:08:12 +00001240
Douglas Gregor9b146582009-07-08 20:55:45 +00001241 // Form the template argument list from the explicitly-specified
1242 // template arguments.
Mike Stump11289f42009-09-09 15:08:12 +00001243 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor1ccc8412010-11-07 23:05:16 +00001244 = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
Douglas Gregor9b146582009-07-08 20:55:45 +00001245 Info.reset(ExplicitArgumentList);
Mike Stump11289f42009-09-09 15:08:12 +00001246
John McCall036855a2010-10-12 19:40:14 +00001247 // Template argument deduction and the final substitution should be
1248 // done in the context of the templated declaration. Explicit
1249 // argument substitution, on the other hand, needs to happen in the
1250 // calling context.
1251 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
1252
Douglas Gregor9b146582009-07-08 20:55:45 +00001253 // Instantiate the types of each of the function parameters given the
1254 // explicitly-specified template arguments.
1255 for (FunctionDecl::param_iterator P = Function->param_begin(),
1256 PEnd = Function->param_end();
1257 P != PEnd;
1258 ++P) {
Mike Stump11289f42009-09-09 15:08:12 +00001259 QualType ParamType
1260 = SubstType((*P)->getType(),
Douglas Gregor39cacdb2009-08-28 20:50:45 +00001261 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1262 (*P)->getLocation(), (*P)->getDeclName());
Douglas Gregor9b146582009-07-08 20:55:45 +00001263 if (ParamType.isNull() || Trap.hasErrorOccurred())
1264 return TDK_SubstitutionFailure;
Mike Stump11289f42009-09-09 15:08:12 +00001265
Douglas Gregor9b146582009-07-08 20:55:45 +00001266 ParamTypes.push_back(ParamType);
1267 }
1268
1269 // If the caller wants a full function type back, instantiate the return
1270 // type and form that function type.
1271 if (FunctionType) {
1272 // FIXME: exception-specifications?
Mike Stump11289f42009-09-09 15:08:12 +00001273 const FunctionProtoType *Proto
John McCall9dd450b2009-09-21 23:43:11 +00001274 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor9b146582009-07-08 20:55:45 +00001275 assert(Proto && "Function template does not have a prototype?");
Mike Stump11289f42009-09-09 15:08:12 +00001276
1277 QualType ResultType
Douglas Gregor39cacdb2009-08-28 20:50:45 +00001278 = SubstType(Proto->getResultType(),
1279 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1280 Function->getTypeSpecStartLoc(),
1281 Function->getDeclName());
Douglas Gregor9b146582009-07-08 20:55:45 +00001282 if (ResultType.isNull() || Trap.hasErrorOccurred())
1283 return TDK_SubstitutionFailure;
Mike Stump11289f42009-09-09 15:08:12 +00001284
1285 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor9b146582009-07-08 20:55:45 +00001286 ParamTypes.data(), ParamTypes.size(),
1287 Proto->isVariadic(),
1288 Proto->getTypeQuals(),
1289 Function->getLocation(),
Eli Friedmand8725a92010-08-05 02:54:05 +00001290 Function->getDeclName(),
1291 Proto->getExtInfo());
Douglas Gregor9b146582009-07-08 20:55:45 +00001292 if (FunctionType->isNull() || Trap.hasErrorOccurred())
1293 return TDK_SubstitutionFailure;
1294 }
Mike Stump11289f42009-09-09 15:08:12 +00001295
Douglas Gregor9b146582009-07-08 20:55:45 +00001296 // C++ [temp.arg.explicit]p2:
Mike Stump11289f42009-09-09 15:08:12 +00001297 // Trailing template arguments that can be deduced (14.8.2) may be
1298 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor9b146582009-07-08 20:55:45 +00001299 // template arguments can be deduced, they may all be omitted; in this
1300 // case, the empty template argument list <> itself may also be omitted.
1301 //
1302 // Take all of the explicitly-specified arguments and put them into the
Mike Stump11289f42009-09-09 15:08:12 +00001303 // set of deduced template arguments.
Douglas Gregor9b146582009-07-08 20:55:45 +00001304 Deduced.reserve(TemplateParams->size());
1305 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001306 Deduced.push_back(ExplicitArgumentList->get(I));
1307
Douglas Gregor9b146582009-07-08 20:55:45 +00001308 return TDK_Success;
1309}
1310
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001311/// \brief Allocate a TemplateArgumentLoc where all locations have
1312/// been initialized to the given location.
1313///
1314/// \param S The semantic analysis object.
1315///
1316/// \param The template argument we are producing template argument
1317/// location information for.
1318///
1319/// \param NTTPType For a declaration template argument, the type of
1320/// the non-type template parameter that corresponds to this template
1321/// argument.
1322///
1323/// \param Loc The source location to use for the resulting template
1324/// argument.
1325static TemplateArgumentLoc
1326getTrivialTemplateArgumentLoc(Sema &S,
1327 const TemplateArgument &Arg,
1328 QualType NTTPType,
1329 SourceLocation Loc) {
1330 switch (Arg.getKind()) {
1331 case TemplateArgument::Null:
1332 llvm_unreachable("Can't get a NULL template argument here");
1333 break;
1334
1335 case TemplateArgument::Type:
1336 return TemplateArgumentLoc(Arg,
1337 S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
1338
1339 case TemplateArgument::Declaration: {
1340 Expr *E
1341 = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
1342 .takeAs<Expr>();
1343 return TemplateArgumentLoc(TemplateArgument(E), E);
1344 }
1345
1346 case TemplateArgument::Integral: {
1347 Expr *E
1348 = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>();
1349 return TemplateArgumentLoc(TemplateArgument(E), E);
1350 }
1351
1352 case TemplateArgument::Template:
1353 return TemplateArgumentLoc(Arg, SourceRange(), Loc);
1354
1355 case TemplateArgument::Expression:
1356 return TemplateArgumentLoc(Arg, Arg.getAsExpr());
1357
1358 case TemplateArgument::Pack:
Douglas Gregor0192c232010-12-20 16:52:59 +00001359 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo());
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001360 }
1361
1362 return TemplateArgumentLoc();
1363}
1364
Mike Stump11289f42009-09-09 15:08:12 +00001365/// \brief Finish template argument deduction for a function template,
Douglas Gregor9b146582009-07-08 20:55:45 +00001366/// checking the deduced template arguments for completeness and forming
1367/// the function template specialization.
Mike Stump11289f42009-09-09 15:08:12 +00001368Sema::TemplateDeductionResult
Douglas Gregor9b146582009-07-08 20:55:45 +00001369Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001370 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1371 unsigned NumExplicitlySpecified,
Douglas Gregor9b146582009-07-08 20:55:45 +00001372 FunctionDecl *&Specialization,
1373 TemplateDeductionInfo &Info) {
1374 TemplateParameterList *TemplateParams
1375 = FunctionTemplate->getTemplateParameters();
Mike Stump11289f42009-09-09 15:08:12 +00001376
Douglas Gregor9b146582009-07-08 20:55:45 +00001377 // Template argument deduction for function templates in a SFINAE context.
1378 // Trap any errors that might occur.
Mike Stump11289f42009-09-09 15:08:12 +00001379 SFINAETrap Trap(*this);
1380
Douglas Gregor9b146582009-07-08 20:55:45 +00001381 // Enter a new template instantiation context while we instantiate the
1382 // actual function declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001383 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor9b146582009-07-08 20:55:45 +00001384 FunctionTemplate, Deduced.data(), Deduced.size(),
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001385 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution,
1386 Info);
Douglas Gregor9b146582009-07-08 20:55:45 +00001387 if (Inst)
Mike Stump11289f42009-09-09 15:08:12 +00001388 return TDK_InstantiationDepth;
1389
John McCalle23b8712010-04-29 01:18:58 +00001390 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
John McCall80e58cd2010-04-29 00:35:03 +00001391
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001392 // C++ [temp.deduct.type]p2:
1393 // [...] or if any template argument remains neither deduced nor
1394 // explicitly specified, template argument deduction fails.
Douglas Gregor1ccc8412010-11-07 23:05:16 +00001395 llvm::SmallVector<TemplateArgument, 4> Builder;
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001396 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001397 NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I);
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001398 if (!Deduced[I].isNull()) {
Douglas Gregor6e9cf632010-10-12 18:51:08 +00001399 if (I < NumExplicitlySpecified) {
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001400 // We have already fully type-checked and converted this
Douglas Gregor6e9cf632010-10-12 18:51:08 +00001401 // argument, because it was explicitly-specified. Just record the
1402 // presence of this argument.
Douglas Gregor1ccc8412010-11-07 23:05:16 +00001403 Builder.push_back(Deduced[I]);
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001404 continue;
1405 }
1406
1407 // We have deduced this argument, so it still needs to be
1408 // checked and converted.
1409
1410 // First, for a non-type template parameter type that is
1411 // initialized by a declaration, we need the type of the
1412 // corresponding non-type template parameter.
1413 QualType NTTPType;
1414 if (NonTypeTemplateParmDecl *NTTP
1415 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1416 if (Deduced[I].getKind() == TemplateArgument::Declaration) {
1417 NTTPType = NTTP->getType();
1418 if (NTTPType->isDependentType()) {
Douglas Gregor1ccc8412010-11-07 23:05:16 +00001419 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1420 Builder.data(), Builder.size());
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001421 NTTPType = SubstType(NTTPType,
1422 MultiLevelTemplateArgumentList(TemplateArgs),
1423 NTTP->getLocation(),
1424 NTTP->getDeclName());
1425 if (NTTPType.isNull()) {
1426 Info.Param = makeTemplateParameter(Param);
Douglas Gregor1ccc8412010-11-07 23:05:16 +00001427 // FIXME: These template arguments are temporary. Free them!
1428 Info.reset(TemplateArgumentList::CreateCopy(Context,
1429 Builder.data(),
1430 Builder.size()));
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001431 return TDK_SubstitutionFailure;
1432 }
1433 }
1434 }
1435 }
1436
1437 // Convert the deduced template argument into a template
1438 // argument that we can check, almost as if the user had written
1439 // the template argument explicitly.
1440 TemplateArgumentLoc Arg = getTrivialTemplateArgumentLoc(*this,
1441 Deduced[I],
1442 NTTPType,
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001443 Info.getLocation());
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001444
1445 // Check the template argument, converting it as necessary.
1446 if (CheckTemplateArgument(Param, Arg,
1447 FunctionTemplate,
1448 FunctionTemplate->getLocation(),
1449 FunctionTemplate->getSourceRange().getEnd(),
1450 Builder,
1451 Deduced[I].wasDeducedFromArrayBound()
1452 ? CTAK_DeducedFromArrayBound
1453 : CTAK_Deduced)) {
1454 Info.Param = makeTemplateParameter(
1455 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregor1ccc8412010-11-07 23:05:16 +00001456 // FIXME: These template arguments are temporary. Free them!
1457 Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
1458 Builder.size()));
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001459 return TDK_SubstitutionFailure;
1460 }
1461
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001462 continue;
1463 }
1464
1465 // Substitute into the default template argument, if available.
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001466 TemplateArgumentLoc DefArg
1467 = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
1468 FunctionTemplate->getLocation(),
1469 FunctionTemplate->getSourceRange().getEnd(),
1470 Param,
1471 Builder);
1472
1473 // If there was no default argument, deduction is incomplete.
1474 if (DefArg.getArgument().isNull()) {
1475 Info.Param = makeTemplateParameter(
1476 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1477 return TDK_Incomplete;
1478 }
1479
1480 // Check whether we can actually use the default argument.
1481 if (CheckTemplateArgument(Param, DefArg,
1482 FunctionTemplate,
1483 FunctionTemplate->getLocation(),
1484 FunctionTemplate->getSourceRange().getEnd(),
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001485 Builder,
1486 CTAK_Deduced)) {
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001487 Info.Param = makeTemplateParameter(
1488 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregor1ccc8412010-11-07 23:05:16 +00001489 // FIXME: These template arguments are temporary. Free them!
1490 Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
1491 Builder.size()));
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001492 return TDK_SubstitutionFailure;
1493 }
1494
1495 // If we get here, we successfully used the default template argument.
1496 }
1497
1498 // Form the template argument list from the deduced template arguments.
1499 TemplateArgumentList *DeducedArgumentList
Douglas Gregor1ccc8412010-11-07 23:05:16 +00001500 = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001501 Info.reset(DeducedArgumentList);
1502
Mike Stump11289f42009-09-09 15:08:12 +00001503 // Substitute the deduced template arguments into the function template
Douglas Gregor9b146582009-07-08 20:55:45 +00001504 // declaration to produce the function template specialization.
Douglas Gregor16142372010-04-28 04:52:24 +00001505 DeclContext *Owner = FunctionTemplate->getDeclContext();
1506 if (FunctionTemplate->getFriendObjectKind())
1507 Owner = FunctionTemplate->getLexicalDeclContext();
Douglas Gregor9b146582009-07-08 20:55:45 +00001508 Specialization = cast_or_null<FunctionDecl>(
Douglas Gregor16142372010-04-28 04:52:24 +00001509 SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner,
Douglas Gregor39cacdb2009-08-28 20:50:45 +00001510 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor9b146582009-07-08 20:55:45 +00001511 if (!Specialization)
1512 return TDK_SubstitutionFailure;
Mike Stump11289f42009-09-09 15:08:12 +00001513
Douglas Gregor31fae892009-09-15 18:26:13 +00001514 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
1515 FunctionTemplate->getCanonicalDecl());
1516
Mike Stump11289f42009-09-09 15:08:12 +00001517 // If the template argument list is owned by the function template
Douglas Gregor9b146582009-07-08 20:55:45 +00001518 // specialization, release it.
Douglas Gregord09efd42010-05-08 20:07:26 +00001519 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList &&
1520 !Trap.hasErrorOccurred())
Douglas Gregor9b146582009-07-08 20:55:45 +00001521 Info.take();
Mike Stump11289f42009-09-09 15:08:12 +00001522
Douglas Gregor9b146582009-07-08 20:55:45 +00001523 // There may have been an error that did not prevent us from constructing a
1524 // declaration. Mark the declaration invalid and return with a substitution
1525 // failure.
1526 if (Trap.hasErrorOccurred()) {
1527 Specialization->setInvalidDecl(true);
1528 return TDK_SubstitutionFailure;
1529 }
Mike Stump11289f42009-09-09 15:08:12 +00001530
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00001531 // If we suppressed any diagnostics while performing template argument
1532 // deduction, and if we haven't already instantiated this declaration,
1533 // keep track of these diagnostics. They'll be emitted if this specialization
1534 // is actually used.
1535 if (Info.diag_begin() != Info.diag_end()) {
1536 llvm::DenseMap<Decl *, llvm::SmallVector<PartialDiagnosticAt, 1> >::iterator
1537 Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl());
1538 if (Pos == SuppressedDiagnostics.end())
1539 SuppressedDiagnostics[Specialization->getCanonicalDecl()]
1540 .append(Info.diag_begin(), Info.diag_end());
1541 }
1542
Mike Stump11289f42009-09-09 15:08:12 +00001543 return TDK_Success;
Douglas Gregor9b146582009-07-08 20:55:45 +00001544}
1545
John McCall8d08b9b2010-08-27 09:08:28 +00001546/// Gets the type of a function for template-argument-deducton
1547/// purposes when it's considered as part of an overload set.
John McCallc1f69982010-02-02 02:21:27 +00001548static QualType GetTypeOfFunction(ASTContext &Context,
John McCall8d08b9b2010-08-27 09:08:28 +00001549 const OverloadExpr::FindResult &R,
John McCallc1f69982010-02-02 02:21:27 +00001550 FunctionDecl *Fn) {
John McCallc1f69982010-02-02 02:21:27 +00001551 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
John McCall8d08b9b2010-08-27 09:08:28 +00001552 if (Method->isInstance()) {
1553 // An instance method that's referenced in a form that doesn't
1554 // look like a member pointer is just invalid.
1555 if (!R.HasFormOfMemberPointer) return QualType();
1556
John McCallc1f69982010-02-02 02:21:27 +00001557 return Context.getMemberPointerType(Fn->getType(),
1558 Context.getTypeDeclType(Method->getParent()).getTypePtr());
John McCall8d08b9b2010-08-27 09:08:28 +00001559 }
1560
1561 if (!R.IsAddressOfOperand) return Fn->getType();
John McCallc1f69982010-02-02 02:21:27 +00001562 return Context.getPointerType(Fn->getType());
1563}
1564
1565/// Apply the deduction rules for overload sets.
1566///
1567/// \return the null type if this argument should be treated as an
1568/// undeduced context
1569static QualType
1570ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
Douglas Gregor66d2c8e2010-08-30 21:04:23 +00001571 Expr *Arg, QualType ParamType,
1572 bool ParamWasReference) {
John McCall8d08b9b2010-08-27 09:08:28 +00001573
1574 OverloadExpr::FindResult R = OverloadExpr::find(Arg);
John McCallc1f69982010-02-02 02:21:27 +00001575
John McCall8d08b9b2010-08-27 09:08:28 +00001576 OverloadExpr *Ovl = R.Expression;
John McCallc1f69982010-02-02 02:21:27 +00001577
Douglas Gregor66d2c8e2010-08-30 21:04:23 +00001578 // C++0x [temp.deduct.call]p4
1579 unsigned TDF = 0;
1580 if (ParamWasReference)
1581 TDF |= TDF_ParamWithReferenceType;
1582 if (R.IsAddressOfOperand)
1583 TDF |= TDF_IgnoreQualifiers;
1584
John McCallc1f69982010-02-02 02:21:27 +00001585 // If there were explicit template arguments, we can only find
1586 // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
1587 // unambiguously name a full specialization.
John McCall1acbbb52010-02-02 06:20:04 +00001588 if (Ovl->hasExplicitTemplateArgs()) {
John McCallc1f69982010-02-02 02:21:27 +00001589 // But we can still look for an explicit specialization.
1590 if (FunctionDecl *ExplicitSpec
John McCall1acbbb52010-02-02 06:20:04 +00001591 = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
John McCall8d08b9b2010-08-27 09:08:28 +00001592 return GetTypeOfFunction(S.Context, R, ExplicitSpec);
John McCallc1f69982010-02-02 02:21:27 +00001593 return QualType();
1594 }
1595
1596 // C++0x [temp.deduct.call]p6:
1597 // When P is a function type, pointer to function type, or pointer
1598 // to member function type:
1599
1600 if (!ParamType->isFunctionType() &&
1601 !ParamType->isFunctionPointerType() &&
1602 !ParamType->isMemberFunctionPointerType())
1603 return QualType();
1604
1605 QualType Match;
John McCall1acbbb52010-02-02 06:20:04 +00001606 for (UnresolvedSetIterator I = Ovl->decls_begin(),
1607 E = Ovl->decls_end(); I != E; ++I) {
John McCallc1f69982010-02-02 02:21:27 +00001608 NamedDecl *D = (*I)->getUnderlyingDecl();
1609
1610 // - If the argument is an overload set containing one or more
1611 // function templates, the parameter is treated as a
1612 // non-deduced context.
1613 if (isa<FunctionTemplateDecl>(D))
1614 return QualType();
1615
1616 FunctionDecl *Fn = cast<FunctionDecl>(D);
John McCall8d08b9b2010-08-27 09:08:28 +00001617 QualType ArgType = GetTypeOfFunction(S.Context, R, Fn);
1618 if (ArgType.isNull()) continue;
John McCallc1f69982010-02-02 02:21:27 +00001619
Douglas Gregor66d2c8e2010-08-30 21:04:23 +00001620 // Function-to-pointer conversion.
1621 if (!ParamWasReference && ParamType->isPointerType() &&
1622 ArgType->isFunctionType())
1623 ArgType = S.Context.getPointerType(ArgType);
1624
John McCallc1f69982010-02-02 02:21:27 +00001625 // - If the argument is an overload set (not containing function
1626 // templates), trial argument deduction is attempted using each
1627 // of the members of the set. If deduction succeeds for only one
1628 // of the overload set members, that member is used as the
1629 // argument value for the deduction. If deduction succeeds for
1630 // more than one member of the overload set the parameter is
1631 // treated as a non-deduced context.
1632
1633 // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
1634 // Type deduction is done independently for each P/A pair, and
1635 // the deduced template argument values are then combined.
1636 // So we do not reject deductions which were made elsewhere.
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001637 llvm::SmallVector<DeducedTemplateArgument, 8>
1638 Deduced(TemplateParams->size());
John McCall19c1bfd2010-08-25 05:32:35 +00001639 TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
John McCallc1f69982010-02-02 02:21:27 +00001640 Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +00001641 = DeduceTemplateArguments(S, TemplateParams,
John McCallc1f69982010-02-02 02:21:27 +00001642 ParamType, ArgType,
1643 Info, Deduced, TDF);
1644 if (Result) continue;
1645 if (!Match.isNull()) return QualType();
1646 Match = ArgType;
1647 }
1648
1649 return Match;
1650}
1651
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001652/// \brief Perform template argument deduction from a function call
1653/// (C++ [temp.deduct.call]).
1654///
1655/// \param FunctionTemplate the function template for which we are performing
1656/// template argument deduction.
1657///
Douglas Gregorea0a0a92010-01-11 18:40:55 +00001658/// \param ExplicitTemplateArguments the explicit template arguments provided
1659/// for this call.
Douglas Gregor89026b52009-06-30 23:57:56 +00001660///
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001661/// \param Args the function call arguments
1662///
1663/// \param NumArgs the number of arguments in Args
1664///
Douglas Gregorea0a0a92010-01-11 18:40:55 +00001665/// \param Name the name of the function being called. This is only significant
1666/// when the function template is a conversion function template, in which
1667/// case this routine will also perform template argument deduction based on
1668/// the function to which
1669///
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001670/// \param Specialization if template argument deduction was successful,
Mike Stump11289f42009-09-09 15:08:12 +00001671/// this will be set to the function template specialization produced by
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001672/// template argument deduction.
1673///
1674/// \param Info the argument will be updated to provide additional information
1675/// about template argument deduction.
1676///
1677/// \returns the result of template argument deduction.
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001678Sema::TemplateDeductionResult
1679Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregorea0a0a92010-01-11 18:40:55 +00001680 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001681 Expr **Args, unsigned NumArgs,
1682 FunctionDecl *&Specialization,
1683 TemplateDeductionInfo &Info) {
1684 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor89026b52009-06-30 23:57:56 +00001685
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001686 // C++ [temp.deduct.call]p1:
1687 // Template argument deduction is done by comparing each function template
1688 // parameter type (call it P) with the type of the corresponding argument
1689 // of the call (call it A) as described below.
1690 unsigned CheckArgs = NumArgs;
Douglas Gregor89026b52009-06-30 23:57:56 +00001691 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001692 return TDK_TooFewArguments;
1693 else if (NumArgs > Function->getNumParams()) {
Mike Stump11289f42009-09-09 15:08:12 +00001694 const FunctionProtoType *Proto
John McCall9dd450b2009-09-21 23:43:11 +00001695 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001696 if (!Proto->isVariadic())
1697 return TDK_TooManyArguments;
Mike Stump11289f42009-09-09 15:08:12 +00001698
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001699 CheckArgs = Function->getNumParams();
1700 }
Mike Stump11289f42009-09-09 15:08:12 +00001701
Douglas Gregor89026b52009-06-30 23:57:56 +00001702 // The types of the parameters from which we will perform template argument
1703 // deduction.
John McCall19c1bfd2010-08-25 05:32:35 +00001704 LocalInstantiationScope InstScope(*this);
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001705 TemplateParameterList *TemplateParams
1706 = FunctionTemplate->getTemplateParameters();
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001707 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor89026b52009-06-30 23:57:56 +00001708 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001709 unsigned NumExplicitlySpecified = 0;
John McCall6b51f282009-11-23 01:53:49 +00001710 if (ExplicitTemplateArgs) {
Douglas Gregor9b146582009-07-08 20:55:45 +00001711 TemplateDeductionResult Result =
1712 SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001713 *ExplicitTemplateArgs,
Douglas Gregor9b146582009-07-08 20:55:45 +00001714 Deduced,
1715 ParamTypes,
1716 0,
1717 Info);
1718 if (Result)
1719 return Result;
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001720
1721 NumExplicitlySpecified = Deduced.size();
Douglas Gregor89026b52009-06-30 23:57:56 +00001722 } else {
1723 // Just fill in the parameter types from the function declaration.
1724 for (unsigned I = 0; I != CheckArgs; ++I)
1725 ParamTypes.push_back(Function->getParamDecl(I)->getType());
1726 }
Mike Stump11289f42009-09-09 15:08:12 +00001727
Douglas Gregor89026b52009-06-30 23:57:56 +00001728 // Deduce template arguments from the function parameters.
Mike Stump11289f42009-09-09 15:08:12 +00001729 Deduced.resize(TemplateParams->size());
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001730 for (unsigned I = 0; I != CheckArgs; ++I) {
Douglas Gregor89026b52009-06-30 23:57:56 +00001731 QualType ParamType = ParamTypes[I];
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001732 QualType ArgType = Args[I]->getType();
Mike Stump11289f42009-09-09 15:08:12 +00001733
Douglas Gregor66d2c8e2010-08-30 21:04:23 +00001734 // C++0x [temp.deduct.call]p3:
1735 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
1736 // are ignored for type deduction.
1737 if (ParamType.getCVRQualifiers())
1738 ParamType = ParamType.getLocalUnqualifiedType();
1739 const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>();
1740 if (ParamRefType) {
1741 // [...] If P is a reference type, the type referred to by P is used
1742 // for type deduction.
1743 ParamType = ParamRefType->getPointeeType();
1744 }
1745
John McCallc1f69982010-02-02 02:21:27 +00001746 // Overload sets usually make this parameter an undeduced
1747 // context, but there are sometimes special circumstances.
1748 if (ArgType == Context.OverloadTy) {
1749 ArgType = ResolveOverloadForDeduction(*this, TemplateParams,
Douglas Gregor66d2c8e2010-08-30 21:04:23 +00001750 Args[I], ParamType,
1751 ParamRefType != 0);
John McCallc1f69982010-02-02 02:21:27 +00001752 if (ArgType.isNull())
1753 continue;
1754 }
1755
Douglas Gregor66d2c8e2010-08-30 21:04:23 +00001756 if (ParamRefType) {
1757 // C++0x [temp.deduct.call]p3:
1758 // [...] If P is of the form T&&, where T is a template parameter, and
1759 // the argument is an lvalue, the type A& is used in place of A for
1760 // type deduction.
1761 if (ParamRefType->isRValueReferenceType() &&
1762 ParamRefType->getAs<TemplateTypeParmType>() &&
John McCall086a4642010-11-24 05:12:34 +00001763 Args[I]->isLValue())
Douglas Gregor66d2c8e2010-08-30 21:04:23 +00001764 ArgType = Context.getLValueReferenceType(ArgType);
1765 } else {
1766 // C++ [temp.deduct.call]p2:
1767 // If P is not a reference type:
Mike Stump11289f42009-09-09 15:08:12 +00001768 // - If A is an array type, the pointer type produced by the
1769 // array-to-pointer standard conversion (4.2) is used in place of
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001770 // A for type deduction; otherwise,
1771 if (ArgType->isArrayType())
1772 ArgType = Context.getArrayDecayedType(ArgType);
Mike Stump11289f42009-09-09 15:08:12 +00001773 // - If A is a function type, the pointer type produced by the
1774 // function-to-pointer standard conversion (4.3) is used in place
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001775 // of A for type deduction; otherwise,
1776 else if (ArgType->isFunctionType())
1777 ArgType = Context.getPointerType(ArgType);
1778 else {
1779 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1780 // type are ignored for type deduction.
1781 QualType CanonArgType = Context.getCanonicalType(ArgType);
Douglas Gregor66d2c8e2010-08-30 21:04:23 +00001782 if (ArgType.getCVRQualifiers())
1783 ArgType = ArgType.getUnqualifiedType();
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001784 }
1785 }
Mike Stump11289f42009-09-09 15:08:12 +00001786
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001787 // C++0x [temp.deduct.call]p4:
1788 // In general, the deduction process attempts to find template argument
1789 // values that will make the deduced A identical to A (after the type A
1790 // is transformed as described above). [...]
Douglas Gregor406f6342009-09-14 20:00:47 +00001791 unsigned TDF = TDF_SkipNonDependent;
Mike Stump11289f42009-09-09 15:08:12 +00001792
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001793 // - If the original P is a reference type, the deduced A (i.e., the
1794 // type referred to by the reference) can be more cv-qualified than
1795 // the transformed A.
Douglas Gregor66d2c8e2010-08-30 21:04:23 +00001796 if (ParamRefType)
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001797 TDF |= TDF_ParamWithReferenceType;
Mike Stump11289f42009-09-09 15:08:12 +00001798 // - The transformed A can be another pointer or pointer to member
1799 // type that can be converted to the deduced A via a qualification
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001800 // conversion (4.4).
John McCallda518412010-08-05 05:30:45 +00001801 if (ArgType->isPointerType() || ArgType->isMemberPointerType() ||
1802 ArgType->isObjCObjectPointerType())
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001803 TDF |= TDF_IgnoreQualifiers;
Mike Stump11289f42009-09-09 15:08:12 +00001804 // - If P is a class and P has the form simple-template-id, then the
Douglas Gregorfc516c92009-06-26 23:27:24 +00001805 // transformed A can be a derived class of the deduced A. Likewise,
1806 // if P is a pointer to a class of the form simple-template-id, the
1807 // transformed A can be a pointer to a derived class pointed to by
1808 // the deduced A.
1809 if (isSimpleTemplateIdType(ParamType) ||
Mike Stump11289f42009-09-09 15:08:12 +00001810 (isa<PointerType>(ParamType) &&
Douglas Gregorfc516c92009-06-26 23:27:24 +00001811 isSimpleTemplateIdType(
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001812 ParamType->getAs<PointerType>()->getPointeeType())))
Douglas Gregorfc516c92009-06-26 23:27:24 +00001813 TDF |= TDF_DerivedClass;
Mike Stump11289f42009-09-09 15:08:12 +00001814
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001815 if (TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +00001816 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregorcceb9752009-06-26 18:27:22 +00001817 ParamType, ArgType, Info, Deduced,
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001818 TDF))
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001819 return Result;
Mike Stump11289f42009-09-09 15:08:12 +00001820
Douglas Gregor05155d82009-08-21 23:19:43 +00001821 // FIXME: we need to check that the deduced A is the same as A,
1822 // modulo the various allowed differences.
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001823 }
Douglas Gregor05155d82009-08-21 23:19:43 +00001824
Mike Stump11289f42009-09-09 15:08:12 +00001825 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001826 NumExplicitlySpecified,
Douglas Gregor9b146582009-07-08 20:55:45 +00001827 Specialization, Info);
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001828}
1829
Douglas Gregor9b146582009-07-08 20:55:45 +00001830/// \brief Deduce template arguments when taking the address of a function
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001831/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
1832/// a template.
Douglas Gregor9b146582009-07-08 20:55:45 +00001833///
1834/// \param FunctionTemplate the function template for which we are performing
1835/// template argument deduction.
1836///
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001837/// \param ExplicitTemplateArguments the explicitly-specified template
1838/// arguments.
Douglas Gregor9b146582009-07-08 20:55:45 +00001839///
1840/// \param ArgFunctionType the function type that will be used as the
1841/// "argument" type (A) when performing template argument deduction from the
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001842/// function template's function type. This type may be NULL, if there is no
1843/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
Douglas Gregor9b146582009-07-08 20:55:45 +00001844///
1845/// \param Specialization if template argument deduction was successful,
Mike Stump11289f42009-09-09 15:08:12 +00001846/// this will be set to the function template specialization produced by
Douglas Gregor9b146582009-07-08 20:55:45 +00001847/// template argument deduction.
1848///
1849/// \param Info the argument will be updated to provide additional information
1850/// about template argument deduction.
1851///
1852/// \returns the result of template argument deduction.
1853Sema::TemplateDeductionResult
1854Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001855 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor9b146582009-07-08 20:55:45 +00001856 QualType ArgFunctionType,
1857 FunctionDecl *&Specialization,
1858 TemplateDeductionInfo &Info) {
1859 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1860 TemplateParameterList *TemplateParams
1861 = FunctionTemplate->getTemplateParameters();
1862 QualType FunctionType = Function->getType();
Mike Stump11289f42009-09-09 15:08:12 +00001863
Douglas Gregor9b146582009-07-08 20:55:45 +00001864 // Substitute any explicit template arguments.
John McCall19c1bfd2010-08-25 05:32:35 +00001865 LocalInstantiationScope InstScope(*this);
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001866 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
1867 unsigned NumExplicitlySpecified = 0;
Douglas Gregor9b146582009-07-08 20:55:45 +00001868 llvm::SmallVector<QualType, 4> ParamTypes;
John McCall6b51f282009-11-23 01:53:49 +00001869 if (ExplicitTemplateArgs) {
Mike Stump11289f42009-09-09 15:08:12 +00001870 if (TemplateDeductionResult Result
1871 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001872 *ExplicitTemplateArgs,
Mike Stump11289f42009-09-09 15:08:12 +00001873 Deduced, ParamTypes,
Douglas Gregor9b146582009-07-08 20:55:45 +00001874 &FunctionType, Info))
1875 return Result;
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001876
1877 NumExplicitlySpecified = Deduced.size();
Douglas Gregor9b146582009-07-08 20:55:45 +00001878 }
1879
1880 // Template argument deduction for function templates in a SFINAE context.
1881 // Trap any errors that might occur.
Mike Stump11289f42009-09-09 15:08:12 +00001882 SFINAETrap Trap(*this);
1883
John McCallc1f69982010-02-02 02:21:27 +00001884 Deduced.resize(TemplateParams->size());
1885
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001886 if (!ArgFunctionType.isNull()) {
1887 // Deduce template arguments from the function type.
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001888 if (TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +00001889 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001890 FunctionType, ArgFunctionType, Info,
1891 Deduced, 0))
1892 return Result;
1893 }
Douglas Gregor4ed49f32010-09-29 21:14:36 +00001894
1895 if (TemplateDeductionResult Result
1896 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
1897 NumExplicitlySpecified,
1898 Specialization, Info))
1899 return Result;
1900
1901 // If the requested function type does not match the actual type of the
1902 // specialization, template argument deduction fails.
1903 if (!ArgFunctionType.isNull() &&
1904 !Context.hasSameType(ArgFunctionType, Specialization->getType()))
1905 return TDK_NonDeducedMismatch;
1906
1907 return TDK_Success;
Douglas Gregor9b146582009-07-08 20:55:45 +00001908}
1909
Douglas Gregor05155d82009-08-21 23:19:43 +00001910/// \brief Deduce template arguments for a templated conversion
1911/// function (C++ [temp.deduct.conv]) and, if successful, produce a
1912/// conversion function template specialization.
1913Sema::TemplateDeductionResult
1914Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1915 QualType ToType,
1916 CXXConversionDecl *&Specialization,
1917 TemplateDeductionInfo &Info) {
Mike Stump11289f42009-09-09 15:08:12 +00001918 CXXConversionDecl *Conv
Douglas Gregor05155d82009-08-21 23:19:43 +00001919 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
1920 QualType FromType = Conv->getConversionType();
1921
1922 // Canonicalize the types for deduction.
1923 QualType P = Context.getCanonicalType(FromType);
1924 QualType A = Context.getCanonicalType(ToType);
1925
1926 // C++0x [temp.deduct.conv]p3:
1927 // If P is a reference type, the type referred to by P is used for
1928 // type deduction.
1929 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
1930 P = PRef->getPointeeType();
1931
1932 // C++0x [temp.deduct.conv]p3:
1933 // If A is a reference type, the type referred to by A is used
1934 // for type deduction.
1935 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
1936 A = ARef->getPointeeType();
1937 // C++ [temp.deduct.conv]p2:
1938 //
Mike Stump11289f42009-09-09 15:08:12 +00001939 // If A is not a reference type:
Douglas Gregor05155d82009-08-21 23:19:43 +00001940 else {
1941 assert(!A->isReferenceType() && "Reference types were handled above");
1942
1943 // - If P is an array type, the pointer type produced by the
Mike Stump11289f42009-09-09 15:08:12 +00001944 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor05155d82009-08-21 23:19:43 +00001945 // of P for type deduction; otherwise,
1946 if (P->isArrayType())
1947 P = Context.getArrayDecayedType(P);
1948 // - If P is a function type, the pointer type produced by the
1949 // function-to-pointer standard conversion (4.3) is used in
1950 // place of P for type deduction; otherwise,
1951 else if (P->isFunctionType())
1952 P = Context.getPointerType(P);
1953 // - If P is a cv-qualified type, the top level cv-qualifiers of
1954 // P’s type are ignored for type deduction.
1955 else
1956 P = P.getUnqualifiedType();
1957
1958 // C++0x [temp.deduct.conv]p3:
1959 // If A is a cv-qualified type, the top level cv-qualifiers of A’s
1960 // type are ignored for type deduction.
1961 A = A.getUnqualifiedType();
1962 }
1963
1964 // Template argument deduction for function templates in a SFINAE context.
1965 // Trap any errors that might occur.
Mike Stump11289f42009-09-09 15:08:12 +00001966 SFINAETrap Trap(*this);
Douglas Gregor05155d82009-08-21 23:19:43 +00001967
1968 // C++ [temp.deduct.conv]p1:
1969 // Template argument deduction is done by comparing the return
1970 // type of the template conversion function (call it P) with the
1971 // type that is required as the result of the conversion (call it
1972 // A) as described in 14.8.2.4.
1973 TemplateParameterList *TemplateParams
1974 = FunctionTemplate->getTemplateParameters();
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001975 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Mike Stump11289f42009-09-09 15:08:12 +00001976 Deduced.resize(TemplateParams->size());
Douglas Gregor05155d82009-08-21 23:19:43 +00001977
1978 // C++0x [temp.deduct.conv]p4:
1979 // In general, the deduction process attempts to find template
1980 // argument values that will make the deduced A identical to
1981 // A. However, there are two cases that allow a difference:
1982 unsigned TDF = 0;
1983 // - If the original A is a reference type, A can be more
1984 // cv-qualified than the deduced A (i.e., the type referred to
1985 // by the reference)
1986 if (ToType->isReferenceType())
1987 TDF |= TDF_ParamWithReferenceType;
1988 // - The deduced A can be another pointer or pointer to member
1989 // type that can be converted to A via a qualification
1990 // conversion.
1991 //
1992 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
1993 // both P and A are pointers or member pointers. In this case, we
1994 // just ignore cv-qualifiers completely).
1995 if ((P->isPointerType() && A->isPointerType()) ||
1996 (P->isMemberPointerType() && P->isMemberPointerType()))
1997 TDF |= TDF_IgnoreQualifiers;
1998 if (TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +00001999 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor05155d82009-08-21 23:19:43 +00002000 P, A, Info, Deduced, TDF))
2001 return Result;
2002
2003 // FIXME: we need to check that the deduced A is the same as A,
2004 // modulo the various allowed differences.
Mike Stump11289f42009-09-09 15:08:12 +00002005
Douglas Gregor05155d82009-08-21 23:19:43 +00002006 // Finish template argument deduction.
John McCall19c1bfd2010-08-25 05:32:35 +00002007 LocalInstantiationScope InstScope(*this);
Douglas Gregor05155d82009-08-21 23:19:43 +00002008 FunctionDecl *Spec = 0;
2009 TemplateDeductionResult Result
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00002010 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec,
2011 Info);
Douglas Gregor05155d82009-08-21 23:19:43 +00002012 Specialization = cast_or_null<CXXConversionDecl>(Spec);
2013 return Result;
2014}
2015
Douglas Gregor8364e6b2009-12-21 23:17:24 +00002016/// \brief Deduce template arguments for a function template when there is
2017/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
2018///
2019/// \param FunctionTemplate the function template for which we are performing
2020/// template argument deduction.
2021///
2022/// \param ExplicitTemplateArguments the explicitly-specified template
2023/// arguments.
2024///
2025/// \param Specialization if template argument deduction was successful,
2026/// this will be set to the function template specialization produced by
2027/// template argument deduction.
2028///
2029/// \param Info the argument will be updated to provide additional information
2030/// about template argument deduction.
2031///
2032/// \returns the result of template argument deduction.
2033Sema::TemplateDeductionResult
2034Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
2035 const TemplateArgumentListInfo *ExplicitTemplateArgs,
2036 FunctionDecl *&Specialization,
2037 TemplateDeductionInfo &Info) {
2038 return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
2039 QualType(), Specialization, Info);
2040}
2041
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002042/// \brief Stores the result of comparing the qualifiers of two types.
2043enum DeductionQualifierComparison {
2044 NeitherMoreQualified = 0,
2045 ParamMoreQualified,
2046 ArgMoreQualified
2047};
2048
2049/// \brief Deduce the template arguments during partial ordering by comparing
2050/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
2051///
Chandler Carruthc1263112010-02-07 21:33:28 +00002052/// \param S the semantic analysis object within which we are deducing
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002053///
2054/// \param TemplateParams the template parameters that we are deducing
2055///
2056/// \param ParamIn the parameter type
2057///
2058/// \param ArgIn the argument type
2059///
2060/// \param Info information about the template argument deduction itself
2061///
2062/// \param Deduced the deduced template arguments
2063///
2064/// \returns the result of template argument deduction so far. Note that a
2065/// "success" result means that template argument deduction has not yet failed,
2066/// but it may still fail, later, for other reasons.
2067static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +00002068DeduceTemplateArgumentsDuringPartialOrdering(Sema &S,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00002069 TemplateParameterList *TemplateParams,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002070 QualType ParamIn, QualType ArgIn,
John McCall19c1bfd2010-08-25 05:32:35 +00002071 TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00002072 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
2073 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
Chandler Carruthc1263112010-02-07 21:33:28 +00002074 CanQualType Param = S.Context.getCanonicalType(ParamIn);
2075 CanQualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002076
2077 // C++0x [temp.deduct.partial]p5:
2078 // Before the partial ordering is done, certain transformations are
2079 // performed on the types used for partial ordering:
2080 // - If P is a reference type, P is replaced by the type referred to.
2081 CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
John McCall48f2d582009-10-23 23:03:21 +00002082 if (!ParamRef.isNull())
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002083 Param = ParamRef->getPointeeType();
2084
2085 // - If A is a reference type, A is replaced by the type referred to.
2086 CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
John McCall48f2d582009-10-23 23:03:21 +00002087 if (!ArgRef.isNull())
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002088 Arg = ArgRef->getPointeeType();
2089
John McCall48f2d582009-10-23 23:03:21 +00002090 if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002091 // C++0x [temp.deduct.partial]p6:
2092 // If both P and A were reference types (before being replaced with the
2093 // type referred to above), determine which of the two types (if any) is
2094 // more cv-qualified than the other; otherwise the types are considered to
2095 // be equally cv-qualified for partial ordering purposes. The result of this
2096 // determination will be used below.
2097 //
2098 // We save this information for later, using it only when deduction
2099 // succeeds in both directions.
2100 DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
2101 if (Param.isMoreQualifiedThan(Arg))
2102 QualifierResult = ParamMoreQualified;
2103 else if (Arg.isMoreQualifiedThan(Param))
2104 QualifierResult = ArgMoreQualified;
2105 QualifierComparisons->push_back(QualifierResult);
2106 }
2107
2108 // C++0x [temp.deduct.partial]p7:
2109 // Remove any top-level cv-qualifiers:
2110 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
2111 // version of P.
2112 Param = Param.getUnqualifiedType();
2113 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
2114 // version of A.
2115 Arg = Arg.getUnqualifiedType();
2116
2117 // C++0x [temp.deduct.partial]p8:
2118 // Using the resulting types P and A the deduction is then done as
2119 // described in 14.9.2.5. If deduction succeeds for a given type, the type
2120 // from the argument template is considered to be at least as specialized
2121 // as the type from the parameter template.
Chandler Carruthc1263112010-02-07 21:33:28 +00002122 return DeduceTemplateArguments(S, TemplateParams, Param, Arg, Info,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002123 Deduced, TDF_None);
2124}
2125
2126static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002127MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2128 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002129 unsigned Level,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002130 llvm::SmallVectorImpl<bool> &Deduced);
Douglas Gregor52773dc2010-11-12 23:44:13 +00002131
2132/// \brief If this is a non-static member function,
2133static void MaybeAddImplicitObjectParameterType(ASTContext &Context,
2134 CXXMethodDecl *Method,
2135 llvm::SmallVectorImpl<QualType> &ArgTypes) {
2136 if (Method->isStatic())
2137 return;
2138
2139 // C++ [over.match.funcs]p4:
2140 //
2141 // For non-static member functions, the type of the implicit
2142 // object parameter is
2143 // — "lvalue reference to cv X" for functions declared without a
2144 // ref-qualifier or with the & ref-qualifier
2145 // - "rvalue reference to cv X" for functions declared with the
2146 // && ref-qualifier
2147 //
2148 // FIXME: We don't have ref-qualifiers yet, so we don't do that part.
2149 QualType ArgTy = Context.getTypeDeclType(Method->getParent());
2150 ArgTy = Context.getQualifiedType(ArgTy,
2151 Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
2152 ArgTy = Context.getLValueReferenceType(ArgTy);
2153 ArgTypes.push_back(ArgTy);
2154}
2155
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002156/// \brief Determine whether the function template \p FT1 is at least as
2157/// specialized as \p FT2.
2158static bool isAtLeastAsSpecializedAs(Sema &S,
John McCallbc077cf2010-02-08 23:07:23 +00002159 SourceLocation Loc,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002160 FunctionTemplateDecl *FT1,
2161 FunctionTemplateDecl *FT2,
2162 TemplatePartialOrderingContext TPOC,
2163 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
2164 FunctionDecl *FD1 = FT1->getTemplatedDecl();
2165 FunctionDecl *FD2 = FT2->getTemplatedDecl();
2166 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
2167 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
2168
2169 assert(Proto1 && Proto2 && "Function templates must have prototypes");
2170 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00002171 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002172 Deduced.resize(TemplateParams->size());
2173
2174 // C++0x [temp.deduct.partial]p3:
2175 // The types used to determine the ordering depend on the context in which
2176 // the partial ordering is done:
John McCall19c1bfd2010-08-25 05:32:35 +00002177 TemplateDeductionInfo Info(S.Context, Loc);
Douglas Gregoree430a32010-11-15 15:41:16 +00002178 CXXMethodDecl *Method1 = 0;
2179 CXXMethodDecl *Method2 = 0;
2180 bool IsNonStatic2 = false;
2181 bool IsNonStatic1 = false;
2182 unsigned Skip2 = 0;
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002183 switch (TPOC) {
2184 case TPOC_Call: {
2185 // - In the context of a function call, the function parameter types are
2186 // used.
Douglas Gregoree430a32010-11-15 15:41:16 +00002187 Method1 = dyn_cast<CXXMethodDecl>(FD1);
2188 Method2 = dyn_cast<CXXMethodDecl>(FD2);
2189 IsNonStatic1 = Method1 && !Method1->isStatic();
2190 IsNonStatic2 = Method2 && !Method2->isStatic();
2191
2192 // C++0x [temp.func.order]p3:
2193 // [...] If only one of the function templates is a non-static
2194 // member, that function template is considered to have a new
2195 // first parameter inserted in its function parameter list. The
2196 // new parameter is of type "reference to cv A," where cv are
2197 // the cv-qualifiers of the function template (if any) and A is
2198 // the class of which the function template is a member.
2199 //
2200 // C++98/03 doesn't have this provision, so instead we drop the
2201 // first argument of the free function or static member, which
2202 // seems to match existing practice.
Douglas Gregor52773dc2010-11-12 23:44:13 +00002203 llvm::SmallVector<QualType, 4> Args1;
Douglas Gregoree430a32010-11-15 15:41:16 +00002204 unsigned Skip1 = !S.getLangOptions().CPlusPlus0x &&
2205 IsNonStatic2 && !IsNonStatic1;
2206 if (S.getLangOptions().CPlusPlus0x && IsNonStatic1 && !IsNonStatic2)
Douglas Gregor52773dc2010-11-12 23:44:13 +00002207 MaybeAddImplicitObjectParameterType(S.Context, Method1, Args1);
2208 Args1.insert(Args1.end(),
Douglas Gregoree430a32010-11-15 15:41:16 +00002209 Proto1->arg_type_begin() + Skip1, Proto1->arg_type_end());
Douglas Gregor52773dc2010-11-12 23:44:13 +00002210
2211 llvm::SmallVector<QualType, 4> Args2;
Douglas Gregoree430a32010-11-15 15:41:16 +00002212 Skip2 = !S.getLangOptions().CPlusPlus0x &&
2213 IsNonStatic1 && !IsNonStatic2;
2214 if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1)
Douglas Gregor52773dc2010-11-12 23:44:13 +00002215 MaybeAddImplicitObjectParameterType(S.Context, Method2, Args2);
2216 Args2.insert(Args2.end(),
Douglas Gregoree430a32010-11-15 15:41:16 +00002217 Proto2->arg_type_begin() + Skip2, Proto2->arg_type_end());
Douglas Gregor52773dc2010-11-12 23:44:13 +00002218
2219 unsigned NumParams = std::min(Args1.size(), Args2.size());
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002220 for (unsigned I = 0; I != NumParams; ++I)
Chandler Carruthc1263112010-02-07 21:33:28 +00002221 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002222 TemplateParams,
Douglas Gregor52773dc2010-11-12 23:44:13 +00002223 Args2[I],
2224 Args1[I],
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002225 Info,
2226 Deduced,
2227 QualifierComparisons))
2228 return false;
2229
2230 break;
2231 }
2232
2233 case TPOC_Conversion:
2234 // - In the context of a call to a conversion operator, the return types
2235 // of the conversion function templates are used.
Chandler Carruthc1263112010-02-07 21:33:28 +00002236 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002237 TemplateParams,
2238 Proto2->getResultType(),
2239 Proto1->getResultType(),
2240 Info,
2241 Deduced,
2242 QualifierComparisons))
2243 return false;
2244 break;
2245
2246 case TPOC_Other:
2247 // - In other contexts (14.6.6.2) the function template’s function type
2248 // is used.
Chandler Carruthc1263112010-02-07 21:33:28 +00002249 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002250 TemplateParams,
2251 FD2->getType(),
2252 FD1->getType(),
2253 Info,
2254 Deduced,
2255 QualifierComparisons))
2256 return false;
2257 break;
2258 }
2259
2260 // C++0x [temp.deduct.partial]p11:
2261 // In most cases, all template parameters must have values in order for
2262 // deduction to succeed, but for partial ordering purposes a template
2263 // parameter may remain without a value provided it is not used in the
2264 // types being used for partial ordering. [ Note: a template parameter used
2265 // in a non-deduced context is considered used. -end note]
2266 unsigned ArgIdx = 0, NumArgs = Deduced.size();
2267 for (; ArgIdx != NumArgs; ++ArgIdx)
2268 if (Deduced[ArgIdx].isNull())
2269 break;
2270
2271 if (ArgIdx == NumArgs) {
2272 // All template arguments were deduced. FT1 is at least as specialized
2273 // as FT2.
2274 return true;
2275 }
2276
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002277 // Figure out which template parameters were used.
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002278 llvm::SmallVector<bool, 4> UsedParameters;
2279 UsedParameters.resize(TemplateParams->size());
2280 switch (TPOC) {
2281 case TPOC_Call: {
2282 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
Douglas Gregoree430a32010-11-15 15:41:16 +00002283 if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1)
2284 ::MarkUsedTemplateParameters(S, Method2->getThisType(S.Context), false,
2285 TemplateParams->getDepth(), UsedParameters);
2286 for (unsigned I = Skip2; I < NumParams; ++I)
Douglas Gregor21610382009-10-29 00:04:11 +00002287 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
2288 TemplateParams->getDepth(),
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002289 UsedParameters);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002290 break;
2291 }
2292
2293 case TPOC_Conversion:
Douglas Gregor21610382009-10-29 00:04:11 +00002294 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
2295 TemplateParams->getDepth(),
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002296 UsedParameters);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002297 break;
2298
2299 case TPOC_Other:
Douglas Gregor21610382009-10-29 00:04:11 +00002300 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
2301 TemplateParams->getDepth(),
2302 UsedParameters);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002303 break;
2304 }
2305
2306 for (; ArgIdx != NumArgs; ++ArgIdx)
2307 // If this argument had no value deduced but was used in one of the types
2308 // used for partial ordering, then deduction fails.
2309 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
2310 return false;
2311
2312 return true;
2313}
2314
2315
Douglas Gregorbe999392009-09-15 16:23:51 +00002316/// \brief Returns the more specialized function template according
Douglas Gregor05155d82009-08-21 23:19:43 +00002317/// to the rules of function template partial ordering (C++ [temp.func.order]).
2318///
2319/// \param FT1 the first function template
2320///
2321/// \param FT2 the second function template
2322///
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002323/// \param TPOC the context in which we are performing partial ordering of
2324/// function templates.
Mike Stump11289f42009-09-09 15:08:12 +00002325///
Douglas Gregorbe999392009-09-15 16:23:51 +00002326/// \returns the more specialized function template. If neither
Douglas Gregor05155d82009-08-21 23:19:43 +00002327/// template is more specialized, returns NULL.
2328FunctionTemplateDecl *
2329Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
2330 FunctionTemplateDecl *FT2,
John McCallbc077cf2010-02-08 23:07:23 +00002331 SourceLocation Loc,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002332 TemplatePartialOrderingContext TPOC) {
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002333 llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
John McCallbc077cf2010-02-08 23:07:23 +00002334 bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, 0);
2335 bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002336 &QualifierComparisons);
2337
2338 if (Better1 != Better2) // We have a clear winner
2339 return Better1? FT1 : FT2;
2340
2341 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor05155d82009-08-21 23:19:43 +00002342 return 0;
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002343
2344
2345 // C++0x [temp.deduct.partial]p10:
2346 // If for each type being considered a given template is at least as
2347 // specialized for all types and more specialized for some set of types and
2348 // the other template is not more specialized for any types or is not at
2349 // least as specialized for any types, then the given template is more
2350 // specialized than the other template. Otherwise, neither template is more
2351 // specialized than the other.
2352 Better1 = false;
2353 Better2 = false;
2354 for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
2355 // C++0x [temp.deduct.partial]p9:
2356 // If, for a given type, deduction succeeds in both directions (i.e., the
2357 // types are identical after the transformations above) and if the type
2358 // from the argument template is more cv-qualified than the type from the
2359 // parameter template (as described above) that type is considered to be
2360 // more specialized than the other. If neither type is more cv-qualified
2361 // than the other then neither type is more specialized than the other.
2362 switch (QualifierComparisons[I]) {
2363 case NeitherMoreQualified:
2364 break;
2365
2366 case ParamMoreQualified:
2367 Better1 = true;
2368 if (Better2)
2369 return 0;
2370 break;
2371
2372 case ArgMoreQualified:
2373 Better2 = true;
2374 if (Better1)
2375 return 0;
2376 break;
2377 }
2378 }
2379
2380 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor05155d82009-08-21 23:19:43 +00002381 if (Better1)
2382 return FT1;
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002383 else if (Better2)
2384 return FT2;
2385 else
2386 return 0;
Douglas Gregor05155d82009-08-21 23:19:43 +00002387}
Douglas Gregor9b146582009-07-08 20:55:45 +00002388
Douglas Gregor450f00842009-09-25 18:43:00 +00002389/// \brief Determine if the two templates are equivalent.
2390static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
2391 if (T1 == T2)
2392 return true;
2393
2394 if (!T1 || !T2)
2395 return false;
2396
2397 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
2398}
2399
2400/// \brief Retrieve the most specialized of the given function template
2401/// specializations.
2402///
John McCall58cc69d2010-01-27 01:50:18 +00002403/// \param SpecBegin the start iterator of the function template
2404/// specializations that we will be comparing.
Douglas Gregor450f00842009-09-25 18:43:00 +00002405///
John McCall58cc69d2010-01-27 01:50:18 +00002406/// \param SpecEnd the end iterator of the function template
2407/// specializations, paired with \p SpecBegin.
Douglas Gregor450f00842009-09-25 18:43:00 +00002408///
2409/// \param TPOC the partial ordering context to use to compare the function
2410/// template specializations.
2411///
2412/// \param Loc the location where the ambiguity or no-specializations
2413/// diagnostic should occur.
2414///
2415/// \param NoneDiag partial diagnostic used to diagnose cases where there are
2416/// no matching candidates.
2417///
2418/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
2419/// occurs.
2420///
2421/// \param CandidateDiag partial diagnostic used for each function template
2422/// specialization that is a candidate in the ambiguous ordering. One parameter
2423/// in this diagnostic should be unbound, which will correspond to the string
2424/// describing the template arguments for the function template specialization.
2425///
2426/// \param Index if non-NULL and the result of this function is non-nULL,
2427/// receives the index corresponding to the resulting function template
2428/// specialization.
2429///
2430/// \returns the most specialized function template specialization, if
John McCall58cc69d2010-01-27 01:50:18 +00002431/// found. Otherwise, returns SpecEnd.
Douglas Gregor450f00842009-09-25 18:43:00 +00002432///
2433/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
2434/// template argument deduction.
John McCall58cc69d2010-01-27 01:50:18 +00002435UnresolvedSetIterator
2436Sema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
2437 UnresolvedSetIterator SpecEnd,
2438 TemplatePartialOrderingContext TPOC,
2439 SourceLocation Loc,
2440 const PartialDiagnostic &NoneDiag,
2441 const PartialDiagnostic &AmbigDiag,
2442 const PartialDiagnostic &CandidateDiag) {
2443 if (SpecBegin == SpecEnd) {
Douglas Gregor450f00842009-09-25 18:43:00 +00002444 Diag(Loc, NoneDiag);
John McCall58cc69d2010-01-27 01:50:18 +00002445 return SpecEnd;
Douglas Gregor450f00842009-09-25 18:43:00 +00002446 }
2447
John McCall58cc69d2010-01-27 01:50:18 +00002448 if (SpecBegin + 1 == SpecEnd)
2449 return SpecBegin;
Douglas Gregor450f00842009-09-25 18:43:00 +00002450
2451 // Find the function template that is better than all of the templates it
2452 // has been compared to.
John McCall58cc69d2010-01-27 01:50:18 +00002453 UnresolvedSetIterator Best = SpecBegin;
Douglas Gregor450f00842009-09-25 18:43:00 +00002454 FunctionTemplateDecl *BestTemplate
John McCall58cc69d2010-01-27 01:50:18 +00002455 = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
Douglas Gregor450f00842009-09-25 18:43:00 +00002456 assert(BestTemplate && "Not a function template specialization?");
John McCall58cc69d2010-01-27 01:50:18 +00002457 for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
2458 FunctionTemplateDecl *Challenger
2459 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregor450f00842009-09-25 18:43:00 +00002460 assert(Challenger && "Not a function template specialization?");
John McCall58cc69d2010-01-27 01:50:18 +00002461 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCallbc077cf2010-02-08 23:07:23 +00002462 Loc, TPOC),
Douglas Gregor450f00842009-09-25 18:43:00 +00002463 Challenger)) {
2464 Best = I;
2465 BestTemplate = Challenger;
2466 }
2467 }
2468
2469 // Make sure that the "best" function template is more specialized than all
2470 // of the others.
2471 bool Ambiguous = false;
John McCall58cc69d2010-01-27 01:50:18 +00002472 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
2473 FunctionTemplateDecl *Challenger
2474 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregor450f00842009-09-25 18:43:00 +00002475 if (I != Best &&
2476 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCallbc077cf2010-02-08 23:07:23 +00002477 Loc, TPOC),
Douglas Gregor450f00842009-09-25 18:43:00 +00002478 BestTemplate)) {
2479 Ambiguous = true;
2480 break;
2481 }
2482 }
2483
2484 if (!Ambiguous) {
2485 // We found an answer. Return it.
John McCall58cc69d2010-01-27 01:50:18 +00002486 return Best;
Douglas Gregor450f00842009-09-25 18:43:00 +00002487 }
2488
2489 // Diagnose the ambiguity.
2490 Diag(Loc, AmbigDiag);
2491
2492 // FIXME: Can we order the candidates in some sane way?
John McCall58cc69d2010-01-27 01:50:18 +00002493 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
2494 Diag((*I)->getLocation(), CandidateDiag)
Douglas Gregor450f00842009-09-25 18:43:00 +00002495 << getTemplateArgumentBindingsText(
John McCall58cc69d2010-01-27 01:50:18 +00002496 cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
2497 *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
Douglas Gregor450f00842009-09-25 18:43:00 +00002498
John McCall58cc69d2010-01-27 01:50:18 +00002499 return SpecEnd;
Douglas Gregor450f00842009-09-25 18:43:00 +00002500}
2501
Douglas Gregorbe999392009-09-15 16:23:51 +00002502/// \brief Returns the more specialized class template partial specialization
2503/// according to the rules of partial ordering of class template partial
2504/// specializations (C++ [temp.class.order]).
2505///
2506/// \param PS1 the first class template partial specialization
2507///
2508/// \param PS2 the second class template partial specialization
2509///
2510/// \returns the more specialized class template partial specialization. If
2511/// neither partial specialization is more specialized, returns NULL.
2512ClassTemplatePartialSpecializationDecl *
2513Sema::getMoreSpecializedPartialSpecialization(
2514 ClassTemplatePartialSpecializationDecl *PS1,
John McCallbc077cf2010-02-08 23:07:23 +00002515 ClassTemplatePartialSpecializationDecl *PS2,
2516 SourceLocation Loc) {
Douglas Gregorbe999392009-09-15 16:23:51 +00002517 // C++ [temp.class.order]p1:
2518 // For two class template partial specializations, the first is at least as
2519 // specialized as the second if, given the following rewrite to two
2520 // function templates, the first function template is at least as
2521 // specialized as the second according to the ordering rules for function
2522 // templates (14.6.6.2):
2523 // - the first function template has the same template parameters as the
2524 // first partial specialization and has a single function parameter
2525 // whose type is a class template specialization with the template
2526 // arguments of the first partial specialization, and
2527 // - the second function template has the same template parameters as the
2528 // second partial specialization and has a single function parameter
2529 // whose type is a class template specialization with the template
2530 // arguments of the second partial specialization.
2531 //
Douglas Gregor684268d2010-04-29 06:21:43 +00002532 // Rather than synthesize function templates, we merely perform the
2533 // equivalent partial ordering by performing deduction directly on
2534 // the template arguments of the class template partial
2535 // specializations. This computation is slightly simpler than the
2536 // general problem of function template partial ordering, because
2537 // class template partial specializations are more constrained. We
2538 // know that every template parameter is deducible from the class
2539 // template partial specialization's template arguments, for
2540 // example.
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00002541 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
John McCall19c1bfd2010-08-25 05:32:35 +00002542 TemplateDeductionInfo Info(Context, Loc);
John McCall2408e322010-04-27 00:57:59 +00002543
2544 QualType PT1 = PS1->getInjectedSpecializationType();
2545 QualType PT2 = PS2->getInjectedSpecializationType();
Douglas Gregorbe999392009-09-15 16:23:51 +00002546
2547 // Determine whether PS1 is at least as specialized as PS2
2548 Deduced.resize(PS2->getTemplateParameters()->size());
Chandler Carruthc1263112010-02-07 21:33:28 +00002549 bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbe999392009-09-15 16:23:51 +00002550 PS2->getTemplateParameters(),
John McCall2408e322010-04-27 00:57:59 +00002551 PT2,
2552 PT1,
Douglas Gregorbe999392009-09-15 16:23:51 +00002553 Info,
2554 Deduced,
2555 0);
Argyrios Kyrtzidis7d391552010-11-05 23:25:18 +00002556 if (Better1) {
2557 InstantiatingTemplate Inst(*this, PS2->getLocation(), PS2,
2558 Deduced.data(), Deduced.size(), Info);
Douglas Gregor9225b022010-04-29 06:31:36 +00002559 Better1 = !::FinishTemplateArgumentDeduction(*this, PS2,
2560 PS1->getTemplateArgs(),
2561 Deduced, Info);
Argyrios Kyrtzidis7d391552010-11-05 23:25:18 +00002562 }
Douglas Gregor9225b022010-04-29 06:31:36 +00002563
Douglas Gregorbe999392009-09-15 16:23:51 +00002564 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregoradee3e32009-11-11 23:06:43 +00002565 Deduced.clear();
Douglas Gregorbe999392009-09-15 16:23:51 +00002566 Deduced.resize(PS1->getTemplateParameters()->size());
Chandler Carruthc1263112010-02-07 21:33:28 +00002567 bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbe999392009-09-15 16:23:51 +00002568 PS1->getTemplateParameters(),
John McCall2408e322010-04-27 00:57:59 +00002569 PT1,
2570 PT2,
Douglas Gregorbe999392009-09-15 16:23:51 +00002571 Info,
2572 Deduced,
2573 0);
Argyrios Kyrtzidis7d391552010-11-05 23:25:18 +00002574 if (Better2) {
2575 InstantiatingTemplate Inst(*this, PS1->getLocation(), PS1,
2576 Deduced.data(), Deduced.size(), Info);
Douglas Gregor9225b022010-04-29 06:31:36 +00002577 Better2 = !::FinishTemplateArgumentDeduction(*this, PS1,
2578 PS2->getTemplateArgs(),
2579 Deduced, Info);
Argyrios Kyrtzidis7d391552010-11-05 23:25:18 +00002580 }
Douglas Gregorbe999392009-09-15 16:23:51 +00002581
2582 if (Better1 == Better2)
2583 return 0;
2584
2585 return Better1? PS1 : PS2;
2586}
2587
Mike Stump11289f42009-09-09 15:08:12 +00002588static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002589MarkUsedTemplateParameters(Sema &SemaRef,
2590 const TemplateArgument &TemplateArg,
2591 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002592 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002593 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002594
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002595/// \brief Mark the template parameters that are used by the given
Douglas Gregor91772d12009-06-13 00:26:55 +00002596/// expression.
Mike Stump11289f42009-09-09 15:08:12 +00002597static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002598MarkUsedTemplateParameters(Sema &SemaRef,
2599 const Expr *E,
2600 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002601 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002602 llvm::SmallVectorImpl<bool> &Used) {
2603 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2604 // find other occurrences of template parameters.
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00002605 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregor04b11522010-01-14 18:13:22 +00002606 if (!DRE)
Douglas Gregor91772d12009-06-13 00:26:55 +00002607 return;
2608
Mike Stump11289f42009-09-09 15:08:12 +00002609 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor91772d12009-06-13 00:26:55 +00002610 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2611 if (!NTTP)
2612 return;
2613
Douglas Gregor21610382009-10-29 00:04:11 +00002614 if (NTTP->getDepth() == Depth)
2615 Used[NTTP->getIndex()] = true;
Douglas Gregor91772d12009-06-13 00:26:55 +00002616}
2617
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002618/// \brief Mark the template parameters that are used by the given
2619/// nested name specifier.
2620static void
2621MarkUsedTemplateParameters(Sema &SemaRef,
2622 NestedNameSpecifier *NNS,
2623 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002624 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002625 llvm::SmallVectorImpl<bool> &Used) {
2626 if (!NNS)
2627 return;
2628
Douglas Gregor21610382009-10-29 00:04:11 +00002629 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2630 Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002631 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregor21610382009-10-29 00:04:11 +00002632 OnlyDeduced, Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002633}
2634
2635/// \brief Mark the template parameters that are used by the given
2636/// template name.
2637static void
2638MarkUsedTemplateParameters(Sema &SemaRef,
2639 TemplateName Name,
2640 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002641 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002642 llvm::SmallVectorImpl<bool> &Used) {
2643 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2644 if (TemplateTemplateParmDecl *TTP
Douglas Gregor21610382009-10-29 00:04:11 +00002645 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2646 if (TTP->getDepth() == Depth)
2647 Used[TTP->getIndex()] = true;
2648 }
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002649 return;
2650 }
2651
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002652 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2653 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2654 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002655 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
Douglas Gregor21610382009-10-29 00:04:11 +00002656 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2657 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002658}
2659
2660/// \brief Mark the template parameters that are used by the given
Douglas Gregor91772d12009-06-13 00:26:55 +00002661/// type.
Mike Stump11289f42009-09-09 15:08:12 +00002662static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002663MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2664 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002665 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002666 llvm::SmallVectorImpl<bool> &Used) {
2667 if (T.isNull())
2668 return;
2669
Douglas Gregor91772d12009-06-13 00:26:55 +00002670 // Non-dependent types have nothing deducible
2671 if (!T->isDependentType())
2672 return;
2673
2674 T = SemaRef.Context.getCanonicalType(T);
2675 switch (T->getTypeClass()) {
Douglas Gregor91772d12009-06-13 00:26:55 +00002676 case Type::Pointer:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002677 MarkUsedTemplateParameters(SemaRef,
2678 cast<PointerType>(T)->getPointeeType(),
2679 OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002680 Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002681 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002682 break;
2683
2684 case Type::BlockPointer:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002685 MarkUsedTemplateParameters(SemaRef,
2686 cast<BlockPointerType>(T)->getPointeeType(),
2687 OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002688 Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002689 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002690 break;
2691
2692 case Type::LValueReference:
2693 case Type::RValueReference:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002694 MarkUsedTemplateParameters(SemaRef,
2695 cast<ReferenceType>(T)->getPointeeType(),
2696 OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002697 Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002698 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002699 break;
2700
2701 case Type::MemberPointer: {
2702 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002703 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002704 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002705 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregor21610382009-10-29 00:04:11 +00002706 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002707 break;
2708 }
2709
2710 case Type::DependentSizedArray:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002711 MarkUsedTemplateParameters(SemaRef,
2712 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregor21610382009-10-29 00:04:11 +00002713 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002714 // Fall through to check the element type
2715
2716 case Type::ConstantArray:
2717 case Type::IncompleteArray:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002718 MarkUsedTemplateParameters(SemaRef,
2719 cast<ArrayType>(T)->getElementType(),
Douglas Gregor21610382009-10-29 00:04:11 +00002720 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002721 break;
2722
2723 case Type::Vector:
2724 case Type::ExtVector:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002725 MarkUsedTemplateParameters(SemaRef,
2726 cast<VectorType>(T)->getElementType(),
Douglas Gregor21610382009-10-29 00:04:11 +00002727 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002728 break;
2729
Douglas Gregor758a8692009-06-17 21:51:59 +00002730 case Type::DependentSizedExtVector: {
2731 const DependentSizedExtVectorType *VecType
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00002732 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002733 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002734 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002735 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002736 Depth, Used);
Douglas Gregor758a8692009-06-17 21:51:59 +00002737 break;
2738 }
2739
Douglas Gregor91772d12009-06-13 00:26:55 +00002740 case Type::FunctionProto: {
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00002741 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002742 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002743 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002744 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002745 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002746 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002747 break;
2748 }
2749
Douglas Gregor21610382009-10-29 00:04:11 +00002750 case Type::TemplateTypeParm: {
2751 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2752 if (TTP->getDepth() == Depth)
2753 Used[TTP->getIndex()] = true;
Douglas Gregor91772d12009-06-13 00:26:55 +00002754 break;
Douglas Gregor21610382009-10-29 00:04:11 +00002755 }
Douglas Gregor91772d12009-06-13 00:26:55 +00002756
John McCall2408e322010-04-27 00:57:59 +00002757 case Type::InjectedClassName:
2758 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
2759 // fall through
2760
Douglas Gregor91772d12009-06-13 00:26:55 +00002761 case Type::TemplateSpecialization: {
Mike Stump11289f42009-09-09 15:08:12 +00002762 const TemplateSpecializationType *Spec
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00002763 = cast<TemplateSpecializationType>(T);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002764 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002765 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002766 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregor21610382009-10-29 00:04:11 +00002767 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2768 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002769 break;
2770 }
2771
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002772 case Type::Complex:
2773 if (!OnlyDeduced)
2774 MarkUsedTemplateParameters(SemaRef,
2775 cast<ComplexType>(T)->getElementType(),
Douglas Gregor21610382009-10-29 00:04:11 +00002776 OnlyDeduced, Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002777 break;
2778
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002779 case Type::DependentName:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002780 if (!OnlyDeduced)
2781 MarkUsedTemplateParameters(SemaRef,
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002782 cast<DependentNameType>(T)->getQualifier(),
Douglas Gregor21610382009-10-29 00:04:11 +00002783 OnlyDeduced, Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002784 break;
2785
John McCallc392f372010-06-11 00:33:02 +00002786 case Type::DependentTemplateSpecialization: {
2787 const DependentTemplateSpecializationType *Spec
2788 = cast<DependentTemplateSpecializationType>(T);
2789 if (!OnlyDeduced)
2790 MarkUsedTemplateParameters(SemaRef, Spec->getQualifier(),
2791 OnlyDeduced, Depth, Used);
2792 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
2793 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2794 Used);
2795 break;
2796 }
2797
John McCallbd8d9bd2010-03-01 23:49:17 +00002798 case Type::TypeOf:
2799 if (!OnlyDeduced)
2800 MarkUsedTemplateParameters(SemaRef,
2801 cast<TypeOfType>(T)->getUnderlyingType(),
2802 OnlyDeduced, Depth, Used);
2803 break;
2804
2805 case Type::TypeOfExpr:
2806 if (!OnlyDeduced)
2807 MarkUsedTemplateParameters(SemaRef,
2808 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
2809 OnlyDeduced, Depth, Used);
2810 break;
2811
2812 case Type::Decltype:
2813 if (!OnlyDeduced)
2814 MarkUsedTemplateParameters(SemaRef,
2815 cast<DecltypeType>(T)->getUnderlyingExpr(),
2816 OnlyDeduced, Depth, Used);
2817 break;
2818
Douglas Gregord2fa7662010-12-20 02:24:11 +00002819 case Type::PackExpansion:
2820 MarkUsedTemplateParameters(SemaRef,
2821 cast<PackExpansionType>(T)->getPattern(),
2822 OnlyDeduced, Depth, Used);
2823 break;
2824
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002825 // None of these types have any template parameters in them.
Douglas Gregor91772d12009-06-13 00:26:55 +00002826 case Type::Builtin:
Douglas Gregor91772d12009-06-13 00:26:55 +00002827 case Type::VariableArray:
2828 case Type::FunctionNoProto:
2829 case Type::Record:
2830 case Type::Enum:
Douglas Gregor91772d12009-06-13 00:26:55 +00002831 case Type::ObjCInterface:
John McCall8b07ec22010-05-15 11:32:37 +00002832 case Type::ObjCObject:
Steve Narofffb4330f2009-06-17 22:40:22 +00002833 case Type::ObjCObjectPointer:
John McCallb96ec562009-12-04 22:46:56 +00002834 case Type::UnresolvedUsing:
Douglas Gregor91772d12009-06-13 00:26:55 +00002835#define TYPE(Class, Base)
2836#define ABSTRACT_TYPE(Class, Base)
2837#define DEPENDENT_TYPE(Class, Base)
2838#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2839#include "clang/AST/TypeNodes.def"
2840 break;
2841 }
2842}
2843
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002844/// \brief Mark the template parameters that are used by this
Douglas Gregor91772d12009-06-13 00:26:55 +00002845/// template argument.
Mike Stump11289f42009-09-09 15:08:12 +00002846static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002847MarkUsedTemplateParameters(Sema &SemaRef,
2848 const TemplateArgument &TemplateArg,
2849 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002850 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002851 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor91772d12009-06-13 00:26:55 +00002852 switch (TemplateArg.getKind()) {
2853 case TemplateArgument::Null:
2854 case TemplateArgument::Integral:
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002855 case TemplateArgument::Declaration:
Douglas Gregor91772d12009-06-13 00:26:55 +00002856 break;
Mike Stump11289f42009-09-09 15:08:12 +00002857
Douglas Gregor91772d12009-06-13 00:26:55 +00002858 case TemplateArgument::Type:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002859 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002860 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002861 break;
2862
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002863 case TemplateArgument::Template:
2864 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2865 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002866 break;
2867
2868 case TemplateArgument::Expression:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002869 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002870 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002871 break;
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002872
Anders Carlssonbc343912009-06-15 17:04:53 +00002873 case TemplateArgument::Pack:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002874 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2875 PEnd = TemplateArg.pack_end();
2876 P != PEnd; ++P)
Douglas Gregor21610382009-10-29 00:04:11 +00002877 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssonbc343912009-06-15 17:04:53 +00002878 break;
Douglas Gregor91772d12009-06-13 00:26:55 +00002879 }
2880}
2881
2882/// \brief Mark the template parameters can be deduced by the given
2883/// template argument list.
2884///
2885/// \param TemplateArgs the template argument list from which template
2886/// parameters will be deduced.
2887///
2888/// \param Deduced a bit vector whose elements will be set to \c true
2889/// to indicate when the corresponding template parameter will be
2890/// deduced.
Mike Stump11289f42009-09-09 15:08:12 +00002891void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002892Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregor21610382009-10-29 00:04:11 +00002893 bool OnlyDeduced, unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002894 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor91772d12009-06-13 00:26:55 +00002895 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
Douglas Gregor21610382009-10-29 00:04:11 +00002896 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2897 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002898}
Douglas Gregorce23bae2009-09-18 23:21:38 +00002899
2900/// \brief Marks all of the template parameters that will be deduced by a
2901/// call to the given function template.
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00002902void
2903Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
2904 llvm::SmallVectorImpl<bool> &Deduced) {
Douglas Gregorce23bae2009-09-18 23:21:38 +00002905 TemplateParameterList *TemplateParams
2906 = FunctionTemplate->getTemplateParameters();
2907 Deduced.clear();
2908 Deduced.resize(TemplateParams->size());
2909
2910 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2911 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
2912 ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
Douglas Gregor21610382009-10-29 00:04:11 +00002913 true, TemplateParams->getDepth(), Deduced);
Douglas Gregorce23bae2009-09-18 23:21:38 +00002914}