blob: d61a767dc5fea710e7e68b36e64c2c916f2ea0e3 [file] [log] [blame]
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001//===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template argument deduction.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/StmtVisitor.h"
17#include "clang/AST/Expr.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/Parse/DeclSpec.h"
Douglas Gregor8a514912009-09-14 18:39:43 +000020#include <algorithm>
Douglas Gregor508f1c82009-06-26 23:10:12 +000021
22namespace clang {
23 /// \brief Various flags that control template argument deduction.
24 ///
25 /// These flags can be bitwise-OR'd together.
26 enum TemplateDeductionFlags {
27 /// \brief No template argument deduction flags, which indicates the
28 /// strictest results for template argument deduction (as used for, e.g.,
29 /// matching class template partial specializations).
30 TDF_None = 0,
31 /// \brief Within template argument deduction from a function call, we are
32 /// matching with a parameter type for which the original parameter was
33 /// a reference.
34 TDF_ParamWithReferenceType = 0x1,
35 /// \brief Within template argument deduction from a function call, we
36 /// are matching in a case where we ignore cv-qualifiers.
37 TDF_IgnoreQualifiers = 0x02,
38 /// \brief Within template argument deduction from a function call,
39 /// we are matching in a case where we can perform template argument
Douglas Gregor41128772009-06-26 23:27:24 +000040 /// deduction from a template-id of a derived class of the argument type.
Douglas Gregor12820292009-09-14 20:00:47 +000041 TDF_DerivedClass = 0x04,
42 /// \brief Allow non-dependent types to differ, e.g., when performing
43 /// template argument deduction from a function call where conversions
44 /// may apply.
45 TDF_SkipNonDependent = 0x08
Douglas Gregor508f1c82009-06-26 23:10:12 +000046 };
47}
48
Douglas Gregor0b9247f2009-06-04 00:03:07 +000049using namespace clang;
50
Douglas Gregor9d0e4412010-03-26 05:50:28 +000051/// \brief Compare two APSInts, extending and switching the sign as
52/// necessary to compare their values regardless of underlying type.
53static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) {
54 if (Y.getBitWidth() > X.getBitWidth())
55 X.extend(Y.getBitWidth());
56 else if (Y.getBitWidth() < X.getBitWidth())
57 Y.extend(X.getBitWidth());
58
59 // If there is a signedness mismatch, correct it.
60 if (X.isSigned() != Y.isSigned()) {
61 // If the signed value is negative, then the values cannot be the same.
62 if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative()))
63 return false;
64
65 Y.setIsSigned(true);
66 X.setIsSigned(true);
67 }
68
69 return X == Y;
70}
71
Douglas Gregorf67875d2009-06-12 18:26:56 +000072static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +000073DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +000074 TemplateParameterList *TemplateParams,
75 const TemplateArgument &Param,
Douglas Gregord708c722009-06-09 16:35:58 +000076 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +000077 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +000078 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced);
Douglas Gregord708c722009-06-09 16:35:58 +000079
Douglas Gregor199d9912009-06-05 00:53:49 +000080/// \brief If the given expression is of a form that permits the deduction
81/// of a non-type template parameter, return the declaration of that
82/// non-type template parameter.
83static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
84 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
85 E = IC->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +000086
Douglas Gregor199d9912009-06-05 00:53:49 +000087 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
88 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +000089
Douglas Gregor199d9912009-06-05 00:53:49 +000090 return 0;
91}
92
Mike Stump1eb44332009-09-09 15:08:12 +000093/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +000094/// from the given constant.
Douglas Gregorf67875d2009-06-12 18:26:56 +000095static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +000096DeduceNonTypeTemplateArgument(Sema &S,
Mike Stump1eb44332009-09-09 15:08:12 +000097 NonTypeTemplateParmDecl *NTTP,
Douglas Gregor9d0e4412010-03-26 05:50:28 +000098 llvm::APSInt Value, QualType ValueType,
Douglas Gregor02024a92010-03-28 02:42:43 +000099 bool DeducedFromArrayBound,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000100 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000101 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +0000102 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000103 "Cannot deduce non-type template argument with depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Douglas Gregor199d9912009-06-05 00:53:49 +0000105 if (Deduced[NTTP->getIndex()].isNull()) {
Douglas Gregor02024a92010-03-28 02:42:43 +0000106 Deduced[NTTP->getIndex()] = DeducedTemplateArgument(Value, ValueType,
107 DeducedFromArrayBound);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000108 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000109 }
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000111 if (Deduced[NTTP->getIndex()].getKind() != TemplateArgument::Integral) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000112 Info.Param = NTTP;
113 Info.FirstArg = Deduced[NTTP->getIndex()];
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000114 Info.SecondArg = TemplateArgument(Value, ValueType);
115 return Sema::TDK_Inconsistent;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000116 }
117
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000118 // Extent the smaller of the two values.
119 llvm::APSInt PrevValue = *Deduced[NTTP->getIndex()].getAsIntegral();
120 if (!hasSameExtendedValue(PrevValue, Value)) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000121 Info.Param = NTTP;
122 Info.FirstArg = Deduced[NTTP->getIndex()];
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000123 Info.SecondArg = TemplateArgument(Value, ValueType);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000124 return Sema::TDK_Inconsistent;
125 }
126
Douglas Gregor02024a92010-03-28 02:42:43 +0000127 if (!DeducedFromArrayBound)
128 Deduced[NTTP->getIndex()].setDeducedFromArrayBound(false);
129
Douglas Gregorf67875d2009-06-12 18:26:56 +0000130 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000131}
132
Mike Stump1eb44332009-09-09 15:08:12 +0000133/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +0000134/// from the given type- or value-dependent expression.
135///
136/// \returns true if deduction succeeded, false otherwise.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000137static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000138DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000139 NonTypeTemplateParmDecl *NTTP,
140 Expr *Value,
141 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000142 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +0000143 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000144 "Cannot deduce non-type template argument with depth > 0");
145 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
146 "Expression template argument must be type- or value-dependent.");
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Douglas Gregor199d9912009-06-05 00:53:49 +0000148 if (Deduced[NTTP->getIndex()].isNull()) {
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000149 Deduced[NTTP->getIndex()] = TemplateArgument(Value->Retain());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000150 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000151 }
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Douglas Gregor199d9912009-06-05 00:53:49 +0000153 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
Mike Stump1eb44332009-09-09 15:08:12 +0000154 // Okay, we deduced a constant in one case and a dependent expression
155 // in another case. FIXME: Later, we will check that instantiating the
Douglas Gregor199d9912009-06-05 00:53:49 +0000156 // dependent expression gives us the constant value.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000157 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000158 }
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Douglas Gregor9eea08b2009-09-15 16:51:42 +0000160 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
161 // Compare the expressions for equality
162 llvm::FoldingSetNodeID ID1, ID2;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000163 Deduced[NTTP->getIndex()].getAsExpr()->Profile(ID1, S.Context, true);
164 Value->Profile(ID2, S.Context, true);
Douglas Gregor9eea08b2009-09-15 16:51:42 +0000165 if (ID1 == ID2)
166 return Sema::TDK_Success;
167
168 // FIXME: Fill in argument mismatch information
169 return Sema::TDK_NonDeducedMismatch;
170 }
171
Douglas Gregorf67875d2009-06-12 18:26:56 +0000172 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000173}
174
Douglas Gregor15755cb2009-11-13 23:45:44 +0000175/// \brief Deduce the value of the given non-type template parameter
176/// from the given declaration.
177///
178/// \returns true if deduction succeeded, false otherwise.
179static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000180DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregor15755cb2009-11-13 23:45:44 +0000181 NonTypeTemplateParmDecl *NTTP,
182 Decl *D,
183 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000184 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor15755cb2009-11-13 23:45:44 +0000185 assert(NTTP->getDepth() == 0 &&
186 "Cannot deduce non-type template argument with depth > 0");
187
188 if (Deduced[NTTP->getIndex()].isNull()) {
189 Deduced[NTTP->getIndex()] = TemplateArgument(D->getCanonicalDecl());
190 return Sema::TDK_Success;
191 }
192
193 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
194 // Okay, we deduced a declaration in one case and a dependent expression
195 // in another case.
196 return Sema::TDK_Success;
197 }
198
199 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Declaration) {
200 // Compare the declarations for equality
201 if (Deduced[NTTP->getIndex()].getAsDecl()->getCanonicalDecl() ==
202 D->getCanonicalDecl())
203 return Sema::TDK_Success;
204
205 // FIXME: Fill in argument mismatch information
206 return Sema::TDK_NonDeducedMismatch;
207 }
208
209 return Sema::TDK_Success;
210}
211
Douglas Gregorf67875d2009-06-12 18:26:56 +0000212static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000213DeduceTemplateArguments(Sema &S,
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000214 TemplateParameterList *TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000215 TemplateName Param,
216 TemplateName Arg,
217 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000218 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregord708c722009-06-09 16:35:58 +0000219 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000220 if (!ParamDecl) {
221 // The parameter type is dependent and is not a template template parameter,
222 // so there is nothing that we can deduce.
223 return Sema::TDK_Success;
224 }
225
226 if (TemplateTemplateParmDecl *TempParam
227 = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) {
228 // Bind the template template parameter to the given template name.
229 TemplateArgument &ExistingArg = Deduced[TempParam->getIndex()];
230 if (ExistingArg.isNull()) {
231 // This is the first deduction for this template template parameter.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000232 ExistingArg = TemplateArgument(S.Context.getCanonicalTemplateName(Arg));
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000233 return Sema::TDK_Success;
234 }
235
236 // Verify that the previous binding matches this deduction.
237 assert(ExistingArg.getKind() == TemplateArgument::Template);
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000238 if (S.Context.hasSameTemplateName(ExistingArg.getAsTemplate(), Arg))
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000239 return Sema::TDK_Success;
240
241 // Inconsistent deduction.
242 Info.Param = TempParam;
243 Info.FirstArg = ExistingArg;
244 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000245 return Sema::TDK_Inconsistent;
246 }
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000247
248 // Verify that the two template names are equivalent.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000249 if (S.Context.hasSameTemplateName(Param, Arg))
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000250 return Sema::TDK_Success;
251
252 // Mismatch of non-dependent template parameter to argument.
253 Info.FirstArg = TemplateArgument(Param);
254 Info.SecondArg = TemplateArgument(Arg);
255 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000256}
257
Mike Stump1eb44332009-09-09 15:08:12 +0000258/// \brief Deduce the template arguments by comparing the template parameter
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000259/// type (which is a template-id) with the template argument type.
260///
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000261/// \param S the Sema
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000262///
263/// \param TemplateParams the template parameters that we are deducing
264///
265/// \param Param the parameter type
266///
267/// \param Arg the argument type
268///
269/// \param Info information about the template argument deduction itself
270///
271/// \param Deduced the deduced template arguments
272///
273/// \returns the result of template argument deduction so far. Note that a
274/// "success" result means that template argument deduction has not yet failed,
275/// but it may still fail, later, for other reasons.
276static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000277DeduceTemplateArguments(Sema &S,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000278 TemplateParameterList *TemplateParams,
279 const TemplateSpecializationType *Param,
280 QualType Arg,
281 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000282 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
John McCall467b27b2009-10-22 20:10:53 +0000283 assert(Arg.isCanonical() && "Argument type must be canonical");
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000285 // Check whether the template argument is a dependent template-id.
Mike Stump1eb44332009-09-09 15:08:12 +0000286 if (const TemplateSpecializationType *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000287 = dyn_cast<TemplateSpecializationType>(Arg)) {
288 // Perform template argument deduction for the template name.
289 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000290 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000291 Param->getTemplateName(),
292 SpecArg->getTemplateName(),
293 Info, Deduced))
294 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000297 // Perform template argument deduction on each template
298 // argument.
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000299 unsigned NumArgs = std::min(SpecArg->getNumArgs(), Param->getNumArgs());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000300 for (unsigned I = 0; I != NumArgs; ++I)
301 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000302 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000303 Param->getArg(I),
304 SpecArg->getArg(I),
305 Info, Deduced))
306 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000308 return Sema::TDK_Success;
309 }
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000311 // If the argument type is a class template specialization, we
312 // perform template argument deduction using its template
313 // arguments.
314 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
315 if (!RecordArg)
316 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000317
318 ClassTemplateSpecializationDecl *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000319 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
320 if (!SpecArg)
321 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000323 // Perform template argument deduction for the template name.
324 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000325 = DeduceTemplateArguments(S,
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000326 TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000327 Param->getTemplateName(),
328 TemplateName(SpecArg->getSpecializedTemplate()),
329 Info, Deduced))
330 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000332 unsigned NumArgs = Param->getNumArgs();
333 const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
334 if (NumArgs != ArgArgs.size())
335 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000337 for (unsigned I = 0; I != NumArgs; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000338 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000339 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000340 Param->getArg(I),
341 ArgArgs.get(I),
342 Info, Deduced))
343 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000345 return Sema::TDK_Success;
346}
347
Douglas Gregor500d3312009-06-26 18:27:22 +0000348/// \brief Deduce the template arguments by comparing the parameter type and
349/// the argument type (C++ [temp.deduct.type]).
350///
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000351/// \param S the semantic analysis object within which we are deducing
Douglas Gregor500d3312009-06-26 18:27:22 +0000352///
353/// \param TemplateParams the template parameters that we are deducing
354///
355/// \param ParamIn the parameter type
356///
357/// \param ArgIn the argument type
358///
359/// \param Info information about the template argument deduction itself
360///
361/// \param Deduced the deduced template arguments
362///
Douglas Gregor508f1c82009-06-26 23:10:12 +0000363/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
Mike Stump1eb44332009-09-09 15:08:12 +0000364/// how template argument deduction is performed.
Douglas Gregor500d3312009-06-26 18:27:22 +0000365///
366/// \returns the result of template argument deduction so far. Note that a
367/// "success" result means that template argument deduction has not yet failed,
368/// but it may still fail, later, for other reasons.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000369static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000370DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000371 TemplateParameterList *TemplateParams,
372 QualType ParamIn, QualType ArgIn,
373 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000374 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000375 unsigned TDF) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000376 // We only want to look at the canonical types, since typedefs and
377 // sugar are not part of template argument deduction.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000378 QualType Param = S.Context.getCanonicalType(ParamIn);
379 QualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000380
Douglas Gregor500d3312009-06-26 18:27:22 +0000381 // C++0x [temp.deduct.call]p4 bullet 1:
382 // - If the original P is a reference type, the deduced A (i.e., the type
Mike Stump1eb44332009-09-09 15:08:12 +0000383 // referred to by the reference) can be more cv-qualified than the
Douglas Gregor500d3312009-06-26 18:27:22 +0000384 // transformed A.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000385 if (TDF & TDF_ParamWithReferenceType) {
Chandler Carruthe7242462009-12-30 04:10:01 +0000386 Qualifiers Quals;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000387 QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals);
Chandler Carruthe7242462009-12-30 04:10:01 +0000388 Quals.setCVRQualifiers(Quals.getCVRQualifiers() &
389 Arg.getCVRQualifiersThroughArrayTypes());
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000390 Param = S.Context.getQualifiedType(UnqualParam, Quals);
Douglas Gregor500d3312009-06-26 18:27:22 +0000391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000393 // If the parameter type is not dependent, there is nothing to deduce.
Douglas Gregor12820292009-09-14 20:00:47 +0000394 if (!Param->isDependentType()) {
395 if (!(TDF & TDF_SkipNonDependent) && Param != Arg) {
396
397 return Sema::TDK_NonDeducedMismatch;
398 }
399
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000400 return Sema::TDK_Success;
Douglas Gregor12820292009-09-14 20:00:47 +0000401 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000402
Douglas Gregor199d9912009-06-05 00:53:49 +0000403 // C++ [temp.deduct.type]p9:
Mike Stump1eb44332009-09-09 15:08:12 +0000404 // A template type argument T, a template template argument TT or a
405 // template non-type argument i can be deduced if P and A have one of
Douglas Gregor199d9912009-06-05 00:53:49 +0000406 // the following forms:
407 //
408 // T
409 // cv-list T
Mike Stump1eb44332009-09-09 15:08:12 +0000410 if (const TemplateTypeParmType *TemplateTypeParm
John McCall183700f2009-09-21 23:43:11 +0000411 = Param->getAs<TemplateTypeParmType>()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000412 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000413 bool RecanonicalizeArg = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Douglas Gregor9e9fae42009-07-22 20:02:25 +0000415 // If the argument type is an array type, move the qualifiers up to the
416 // top level, so they can be matched with the qualifiers on the parameter.
417 // FIXME: address spaces, ObjC GC qualifiers
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000418 if (isa<ArrayType>(Arg)) {
John McCall0953e762009-09-24 19:53:00 +0000419 Qualifiers Quals;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000420 Arg = S.Context.getUnqualifiedArrayType(Arg, Quals);
John McCall0953e762009-09-24 19:53:00 +0000421 if (Quals) {
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000422 Arg = S.Context.getQualifiedType(Arg, Quals);
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000423 RecanonicalizeArg = true;
424 }
425 }
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000427 // The argument type can not be less qualified than the parameter
428 // type.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000429 if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000430 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
431 Info.FirstArg = Deduced[Index];
John McCall833ca992009-10-29 08:12:44 +0000432 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000433 return Sema::TDK_InconsistentQuals;
434 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000435
436 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000437 assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function");
John McCall0953e762009-09-24 19:53:00 +0000438 QualType DeducedType = Arg;
439 DeducedType.removeCVRQualifiers(Param.getCVRQualifiers());
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000440 if (RecanonicalizeArg)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000441 DeducedType = S.Context.getCanonicalType(DeducedType);
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000443 if (Deduced[Index].isNull())
John McCall833ca992009-10-29 08:12:44 +0000444 Deduced[Index] = TemplateArgument(DeducedType);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000445 else {
Mike Stump1eb44332009-09-09 15:08:12 +0000446 // C++ [temp.deduct.type]p2:
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000447 // [...] If type deduction cannot be done for any P/A pair, or if for
Mike Stump1eb44332009-09-09 15:08:12 +0000448 // any pair the deduction leads to more than one possible set of
449 // deduced values, or if different pairs yield different deduced
450 // values, or if any template argument remains neither deduced nor
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000451 // explicitly specified, template argument deduction fails.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000452 if (Deduced[Index].getAsType() != DeducedType) {
Mike Stump1eb44332009-09-09 15:08:12 +0000453 Info.Param
Douglas Gregorf67875d2009-06-12 18:26:56 +0000454 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
455 Info.FirstArg = Deduced[Index];
John McCall833ca992009-10-29 08:12:44 +0000456 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000457 return Sema::TDK_Inconsistent;
458 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000459 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000460 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000461 }
462
Douglas Gregorf67875d2009-06-12 18:26:56 +0000463 // Set up the template argument deduction information for a failure.
John McCall833ca992009-10-29 08:12:44 +0000464 Info.FirstArg = TemplateArgument(ParamIn);
465 Info.SecondArg = TemplateArgument(ArgIn);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000466
Douglas Gregor508f1c82009-06-26 23:10:12 +0000467 // Check the cv-qualifiers on the parameter and argument types.
468 if (!(TDF & TDF_IgnoreQualifiers)) {
469 if (TDF & TDF_ParamWithReferenceType) {
470 if (Param.isMoreQualifiedThan(Arg))
471 return Sema::TDK_NonDeducedMismatch;
472 } else {
473 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Mike Stump1eb44332009-09-09 15:08:12 +0000474 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor508f1c82009-06-26 23:10:12 +0000475 }
476 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000477
Douglas Gregord560d502009-06-04 00:21:18 +0000478 switch (Param->getTypeClass()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000479 // No deduction possible for these types
480 case Type::Builtin:
Douglas Gregorf67875d2009-06-12 18:26:56 +0000481 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Douglas Gregor199d9912009-06-05 00:53:49 +0000483 // T *
Douglas Gregord560d502009-06-04 00:21:18 +0000484 case Type::Pointer: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000485 const PointerType *PointerArg = Arg->getAs<PointerType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000486 if (!PointerArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000487 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Douglas Gregor41128772009-06-26 23:27:24 +0000489 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000490 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000491 cast<PointerType>(Param)->getPointeeType(),
492 PointerArg->getPointeeType(),
Douglas Gregor41128772009-06-26 23:27:24 +0000493 Info, Deduced, SubTDF);
Douglas Gregord560d502009-06-04 00:21:18 +0000494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Douglas Gregor199d9912009-06-05 00:53:49 +0000496 // T &
Douglas Gregord560d502009-06-04 00:21:18 +0000497 case Type::LValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000498 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000499 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000500 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000502 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000503 cast<LValueReferenceType>(Param)->getPointeeType(),
504 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000505 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000506 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000507
Douglas Gregor199d9912009-06-05 00:53:49 +0000508 // T && [C++0x]
Douglas Gregord560d502009-06-04 00:21:18 +0000509 case Type::RValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000510 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000511 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000512 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000514 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000515 cast<RValueReferenceType>(Param)->getPointeeType(),
516 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000517 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000518 }
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Douglas Gregor199d9912009-06-05 00:53:49 +0000520 // T [] (implied, but not stated explicitly)
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000521 case Type::IncompleteArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000522 const IncompleteArrayType *IncompleteArrayArg =
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000523 S.Context.getAsIncompleteArrayType(Arg);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000524 if (!IncompleteArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000525 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000527 return DeduceTemplateArguments(S, TemplateParams,
528 S.Context.getAsIncompleteArrayType(Param)->getElementType(),
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000529 IncompleteArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000530 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000531 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000532
533 // T [integer-constant]
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000534 case Type::ConstantArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000535 const ConstantArrayType *ConstantArrayArg =
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000536 S.Context.getAsConstantArrayType(Arg);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000537 if (!ConstantArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000538 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000539
540 const ConstantArrayType *ConstantArrayParm =
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000541 S.Context.getAsConstantArrayType(Param);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000542 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000543 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000545 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000546 ConstantArrayParm->getElementType(),
547 ConstantArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000548 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000549 }
550
Douglas Gregor199d9912009-06-05 00:53:49 +0000551 // type [i]
552 case Type::DependentSizedArray: {
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000553 const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
Douglas Gregor199d9912009-06-05 00:53:49 +0000554 if (!ArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000555 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Douglas Gregor199d9912009-06-05 00:53:49 +0000557 // Check the element type of the arrays
558 const DependentSizedArrayType *DependentArrayParm
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000559 = S.Context.getAsDependentSizedArrayType(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000560 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000561 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000562 DependentArrayParm->getElementType(),
563 ArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000564 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000565 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Douglas Gregor199d9912009-06-05 00:53:49 +0000567 // Determine the array bound is something we can deduce.
Mike Stump1eb44332009-09-09 15:08:12 +0000568 NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000569 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
570 if (!NTTP)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000571 return Sema::TDK_Success;
Mike Stump1eb44332009-09-09 15:08:12 +0000572
573 // We can perform template argument deduction for the given non-type
Douglas Gregor199d9912009-06-05 00:53:49 +0000574 // template parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000575 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000576 "Cannot deduce non-type template argument at depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000577 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson335e24a2009-06-16 22:44:31 +0000578 = dyn_cast<ConstantArrayType>(ArrayArg)) {
579 llvm::APSInt Size(ConstantArrayArg->getSize());
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000580 return DeduceNonTypeTemplateArgument(S, NTTP, Size,
581 S.Context.getSizeType(),
Douglas Gregor02024a92010-03-28 02:42:43 +0000582 /*ArrayBound=*/true,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000583 Info, Deduced);
Anders Carlsson335e24a2009-06-16 22:44:31 +0000584 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000585 if (const DependentSizedArrayType *DependentArrayArg
586 = dyn_cast<DependentSizedArrayType>(ArrayArg))
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000587 return DeduceNonTypeTemplateArgument(S, NTTP,
Douglas Gregor199d9912009-06-05 00:53:49 +0000588 DependentArrayArg->getSizeExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000589 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Douglas Gregor199d9912009-06-05 00:53:49 +0000591 // Incomplete type does not match a dependently-sized array type
Douglas Gregorf67875d2009-06-12 18:26:56 +0000592 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000593 }
Mike Stump1eb44332009-09-09 15:08:12 +0000594
595 // type(*)(T)
596 // T(*)()
597 // T(*)(T)
Anders Carlssona27fad52009-06-08 15:19:08 +0000598 case Type::FunctionProto: {
Mike Stump1eb44332009-09-09 15:08:12 +0000599 const FunctionProtoType *FunctionProtoArg =
Anders Carlssona27fad52009-06-08 15:19:08 +0000600 dyn_cast<FunctionProtoType>(Arg);
601 if (!FunctionProtoArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000602 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000603
604 const FunctionProtoType *FunctionProtoParam =
Anders Carlssona27fad52009-06-08 15:19:08 +0000605 cast<FunctionProtoType>(Param);
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000606
Mike Stump1eb44332009-09-09 15:08:12 +0000607 if (FunctionProtoParam->getTypeQuals() !=
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000608 FunctionProtoArg->getTypeQuals())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000609 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000611 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000612 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000614 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000615 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000616
Anders Carlssona27fad52009-06-08 15:19:08 +0000617 // Check return types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000618 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000619 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000620 FunctionProtoParam->getResultType(),
621 FunctionProtoArg->getResultType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000622 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000623 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Anders Carlssona27fad52009-06-08 15:19:08 +0000625 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
626 // Check argument types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000627 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000628 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000629 FunctionProtoParam->getArgType(I),
630 FunctionProtoArg->getArgType(I),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000631 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000632 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Douglas Gregorf67875d2009-06-12 18:26:56 +0000635 return Sema::TDK_Success;
Anders Carlssona27fad52009-06-08 15:19:08 +0000636 }
Mike Stump1eb44332009-09-09 15:08:12 +0000637
John McCall3cb0ebd2010-03-10 03:28:59 +0000638 case Type::InjectedClassName: {
639 // Treat a template's injected-class-name as if the template
640 // specialization type had been used.
641 Param = cast<InjectedClassNameType>(Param)->getUnderlyingType();
642 assert(isa<TemplateSpecializationType>(Param) &&
643 "injected class name is not a template specialization type");
644 // fall through
645 }
646
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000647 // template-name<T> (where template-name refers to a class template)
Douglas Gregord708c722009-06-09 16:35:58 +0000648 // template-name<i>
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000649 // TT<T>
650 // TT<i>
651 // TT<>
Douglas Gregord708c722009-06-09 16:35:58 +0000652 case Type::TemplateSpecialization: {
653 const TemplateSpecializationType *SpecParam
654 = cast<TemplateSpecializationType>(Param);
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000656 // Try to deduce template arguments from the template-id.
657 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000658 = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000659 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Douglas Gregor4a5c15f2009-09-30 22:13:51 +0000661 if (Result && (TDF & TDF_DerivedClass)) {
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000662 // C++ [temp.deduct.call]p3b3:
663 // If P is a class, and P has the form template-id, then A can be a
664 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump1eb44332009-09-09 15:08:12 +0000665 // class of the form template-id, A can be a pointer to a derived
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000666 // class pointed to by the deduced A.
667 //
668 // More importantly:
Mike Stump1eb44332009-09-09 15:08:12 +0000669 // These alternatives are considered only if type deduction would
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000670 // otherwise fail.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000671 if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
672 // We cannot inspect base classes as part of deduction when the type
673 // is incomplete, so either instantiate any templates necessary to
674 // complete the type, or skip over it if it cannot be completed.
John McCall5769d612010-02-08 23:07:23 +0000675 if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000676 return Result;
677
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000678 // Use data recursion to crawl through the list of base classes.
Mike Stump1eb44332009-09-09 15:08:12 +0000679 // Visited contains the set of nodes we have already visited, while
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000680 // ToVisit is our stack of records that we still need to visit.
681 llvm::SmallPtrSet<const RecordType *, 8> Visited;
682 llvm::SmallVector<const RecordType *, 8> ToVisit;
683 ToVisit.push_back(RecordT);
684 bool Successful = false;
685 while (!ToVisit.empty()) {
686 // Retrieve the next class in the inheritance hierarchy.
687 const RecordType *NextT = ToVisit.back();
688 ToVisit.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000690 // If we have already seen this type, skip it.
691 if (!Visited.insert(NextT))
692 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000694 // If this is a base class, try to perform template argument
695 // deduction from it.
696 if (NextT != RecordT) {
697 Sema::TemplateDeductionResult BaseResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000698 = DeduceTemplateArguments(S, TemplateParams, SpecParam,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000699 QualType(NextT, 0), Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000701 // If template argument deduction for this base was successful,
702 // note that we had some success.
703 if (BaseResult == Sema::TDK_Success)
704 Successful = true;
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000705 }
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000707 // Visit base classes
708 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
709 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
710 BaseEnd = Next->bases_end();
Sebastian Redl9994a342009-10-25 17:03:50 +0000711 Base != BaseEnd; ++Base) {
Mike Stump1eb44332009-09-09 15:08:12 +0000712 assert(Base->getType()->isRecordType() &&
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000713 "Base class that isn't a record?");
Ted Kremenek6217b802009-07-29 21:53:49 +0000714 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000715 }
716 }
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000718 if (Successful)
719 return Sema::TDK_Success;
720 }
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000722 }
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000724 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000725 }
726
Douglas Gregor637a4092009-06-10 23:47:09 +0000727 // T type::*
728 // T T::*
729 // T (type::*)()
730 // type (T::*)()
731 // type (type::*)(T)
732 // type (T::*)(T)
733 // T (type::*)(T)
734 // T (T::*)()
735 // T (T::*)(T)
736 case Type::MemberPointer: {
737 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
738 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
739 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000740 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +0000741
Douglas Gregorf67875d2009-06-12 18:26:56 +0000742 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000743 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000744 MemPtrParam->getPointeeType(),
745 MemPtrArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000746 Info, Deduced,
747 TDF & TDF_IgnoreQualifiers))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000748 return Result;
749
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000750 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000751 QualType(MemPtrParam->getClass(), 0),
752 QualType(MemPtrArg->getClass(), 0),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000753 Info, Deduced, 0);
Douglas Gregor637a4092009-06-10 23:47:09 +0000754 }
755
Anders Carlsson9a917e42009-06-12 22:56:54 +0000756 // (clang extension)
757 //
Mike Stump1eb44332009-09-09 15:08:12 +0000758 // type(^)(T)
759 // T(^)()
760 // T(^)(T)
Anders Carlsson859ba502009-06-12 16:23:10 +0000761 case Type::BlockPointer: {
762 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
763 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Anders Carlsson859ba502009-06-12 16:23:10 +0000765 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000766 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000768 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +0000769 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000770 BlockPtrArg->getPointeeType(), Info,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000771 Deduced, 0);
Anders Carlsson859ba502009-06-12 16:23:10 +0000772 }
773
Douglas Gregor637a4092009-06-10 23:47:09 +0000774 case Type::TypeOfExpr:
775 case Type::TypeOf:
Douglas Gregor4714c122010-03-31 17:34:00 +0000776 case Type::DependentName:
Douglas Gregor637a4092009-06-10 23:47:09 +0000777 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +0000778 return Sema::TDK_Success;
Douglas Gregor637a4092009-06-10 23:47:09 +0000779
Douglas Gregord560d502009-06-04 00:21:18 +0000780 default:
781 break;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000782 }
783
784 // FIXME: Many more cases to go (to go).
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000785 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000786}
787
Douglas Gregorf67875d2009-06-12 18:26:56 +0000788static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000789DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000790 TemplateParameterList *TemplateParams,
791 const TemplateArgument &Param,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000792 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000793 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000794 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000795 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000796 case TemplateArgument::Null:
797 assert(false && "Null template argument in parameter list");
798 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000799
800 case TemplateArgument::Type:
Douglas Gregor788cd062009-11-11 01:00:40 +0000801 if (Arg.getKind() == TemplateArgument::Type)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000802 return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
Douglas Gregor788cd062009-11-11 01:00:40 +0000803 Arg.getAsType(), Info, Deduced, 0);
804 Info.FirstArg = Param;
805 Info.SecondArg = Arg;
806 return Sema::TDK_NonDeducedMismatch;
807
808 case TemplateArgument::Template:
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000809 if (Arg.getKind() == TemplateArgument::Template)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000810 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor788cd062009-11-11 01:00:40 +0000811 Param.getAsTemplate(),
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000812 Arg.getAsTemplate(), Info, Deduced);
Douglas Gregor788cd062009-11-11 01:00:40 +0000813 Info.FirstArg = Param;
814 Info.SecondArg = Arg;
815 return Sema::TDK_NonDeducedMismatch;
816
Douglas Gregor199d9912009-06-05 00:53:49 +0000817 case TemplateArgument::Declaration:
Douglas Gregor788cd062009-11-11 01:00:40 +0000818 if (Arg.getKind() == TemplateArgument::Declaration &&
819 Param.getAsDecl()->getCanonicalDecl() ==
820 Arg.getAsDecl()->getCanonicalDecl())
821 return Sema::TDK_Success;
822
Douglas Gregorf67875d2009-06-12 18:26:56 +0000823 Info.FirstArg = Param;
824 Info.SecondArg = Arg;
825 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Douglas Gregor199d9912009-06-05 00:53:49 +0000827 case TemplateArgument::Integral:
828 if (Arg.getKind() == TemplateArgument::Integral) {
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000829 if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral()))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000830 return Sema::TDK_Success;
831
832 Info.FirstArg = Param;
833 Info.SecondArg = Arg;
834 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000835 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000836
837 if (Arg.getKind() == TemplateArgument::Expression) {
838 Info.FirstArg = Param;
839 Info.SecondArg = Arg;
840 return Sema::TDK_NonDeducedMismatch;
841 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000842
843 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000844 Info.FirstArg = Param;
845 Info.SecondArg = Arg;
846 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Douglas Gregor199d9912009-06-05 00:53:49 +0000848 case TemplateArgument::Expression: {
Mike Stump1eb44332009-09-09 15:08:12 +0000849 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000850 = getDeducedParameterFromExpr(Param.getAsExpr())) {
851 if (Arg.getKind() == TemplateArgument::Integral)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000852 return DeduceNonTypeTemplateArgument(S, NTTP,
Mike Stump1eb44332009-09-09 15:08:12 +0000853 *Arg.getAsIntegral(),
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000854 Arg.getIntegralType(),
Douglas Gregor02024a92010-03-28 02:42:43 +0000855 /*ArrayBound=*/false,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000856 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000857 if (Arg.getKind() == TemplateArgument::Expression)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000858 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000859 Info, Deduced);
Douglas Gregor15755cb2009-11-13 23:45:44 +0000860 if (Arg.getKind() == TemplateArgument::Declaration)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000861 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
Douglas Gregor15755cb2009-11-13 23:45:44 +0000862 Info, Deduced);
863
Douglas Gregor199d9912009-06-05 00:53:49 +0000864 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000865 Info.FirstArg = Param;
866 Info.SecondArg = Arg;
867 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000868 }
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Douglas Gregor199d9912009-06-05 00:53:49 +0000870 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000871 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000872 }
Anders Carlssond01b1da2009-06-15 17:04:53 +0000873 case TemplateArgument::Pack:
874 assert(0 && "FIXME: Implement!");
875 break;
Douglas Gregor199d9912009-06-05 00:53:49 +0000876 }
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Douglas Gregorf67875d2009-06-12 18:26:56 +0000878 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000879}
880
Mike Stump1eb44332009-09-09 15:08:12 +0000881static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000882DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000883 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000884 const TemplateArgumentList &ParamList,
885 const TemplateArgumentList &ArgList,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000886 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000887 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000888 assert(ParamList.size() == ArgList.size());
889 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000890 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000891 = DeduceTemplateArguments(S, TemplateParams,
Mike Stump1eb44332009-09-09 15:08:12 +0000892 ParamList[I], ArgList[I],
Douglas Gregorf67875d2009-06-12 18:26:56 +0000893 Info, Deduced))
894 return Result;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000895 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000896 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000897}
898
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000899/// \brief Determine whether two template arguments are the same.
Mike Stump1eb44332009-09-09 15:08:12 +0000900static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000901 const TemplateArgument &X,
902 const TemplateArgument &Y) {
903 if (X.getKind() != Y.getKind())
904 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000906 switch (X.getKind()) {
907 case TemplateArgument::Null:
908 assert(false && "Comparing NULL template argument");
909 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000911 case TemplateArgument::Type:
912 return Context.getCanonicalType(X.getAsType()) ==
913 Context.getCanonicalType(Y.getAsType());
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000915 case TemplateArgument::Declaration:
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000916 return X.getAsDecl()->getCanonicalDecl() ==
917 Y.getAsDecl()->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Douglas Gregor788cd062009-11-11 01:00:40 +0000919 case TemplateArgument::Template:
920 return Context.getCanonicalTemplateName(X.getAsTemplate())
921 .getAsVoidPointer() ==
922 Context.getCanonicalTemplateName(Y.getAsTemplate())
923 .getAsVoidPointer();
924
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000925 case TemplateArgument::Integral:
926 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Douglas Gregor788cd062009-11-11 01:00:40 +0000928 case TemplateArgument::Expression: {
929 llvm::FoldingSetNodeID XID, YID;
930 X.getAsExpr()->Profile(XID, Context, true);
931 Y.getAsExpr()->Profile(YID, Context, true);
932 return XID == YID;
933 }
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000935 case TemplateArgument::Pack:
936 if (X.pack_size() != Y.pack_size())
937 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000938
939 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
940 XPEnd = X.pack_end(),
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000941 YP = Y.pack_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000942 XP != XPEnd; ++XP, ++YP)
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000943 if (!isSameTemplateArg(Context, *XP, *YP))
944 return false;
945
946 return true;
947 }
948
949 return false;
950}
951
952/// \brief Helper function to build a TemplateParameter when we don't
953/// know its type statically.
954static TemplateParameter makeTemplateParameter(Decl *D) {
955 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
956 return TemplateParameter(TTP);
957 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
958 return TemplateParameter(NTTP);
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000960 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
961}
962
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000963/// \brief Perform template argument deduction to determine whether
964/// the given template arguments match the given class template
965/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +0000966Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000967Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000968 const TemplateArgumentList &TemplateArgs,
969 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000970 // C++ [temp.class.spec.match]p2:
971 // A partial specialization matches a given actual template
972 // argument list if the template arguments of the partial
973 // specialization can be deduced from the actual template argument
974 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +0000975 SFINAETrap Trap(*this);
Douglas Gregor02024a92010-03-28 02:42:43 +0000976 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000977 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000978 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000979 = ::DeduceTemplateArguments(*this,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000980 Partial->getTemplateParameters(),
Mike Stump1eb44332009-09-09 15:08:12 +0000981 Partial->getTemplateArgs(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000982 TemplateArgs, Info, Deduced))
983 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +0000984
Douglas Gregor637a4092009-06-10 23:47:09 +0000985 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
986 Deduced.data(), Deduced.size());
987 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000988 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +0000989
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000990 // C++ [temp.deduct.type]p2:
991 // [...] or if any template argument remains neither deduced nor
992 // explicitly specified, template argument deduction fails.
Anders Carlssonfb250522009-06-23 01:26:57 +0000993 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
994 Deduced.size());
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000995 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000996 if (Deduced[I].isNull()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000997 Decl *Param
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000998 = const_cast<NamedDecl *>(
999 Partial->getTemplateParameters()->getParam(I));
Douglas Gregor2b0749a42010-03-25 15:38:42 +00001000 Info.Param = makeTemplateParameter(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +00001001 return TDK_Incomplete;
1002 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001003
Anders Carlssonfb250522009-06-23 01:26:57 +00001004 Builder.Append(Deduced[I]);
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001005 }
1006
1007 // Form the template argument list from the deduced template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001008 TemplateArgumentList *DeducedArgumentList
Anders Carlssonfb250522009-06-23 01:26:57 +00001009 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
Douglas Gregorf67875d2009-06-12 18:26:56 +00001010 Info.reset(DeducedArgumentList);
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001011
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001012 // Substitute the deduced template arguments into the template
1013 // arguments of the class template partial specialization, and
1014 // verify that the instantiated template arguments are both valid
1015 // and are equivalent to the template arguments originally provided
Mike Stump1eb44332009-09-09 15:08:12 +00001016 // to the class template.
Douglas Gregor02024a92010-03-28 02:42:43 +00001017 // FIXME: Do we have to correct the types of deduced non-type template
1018 // arguments (in particular, integral non-type template arguments?).
Douglas Gregor2b0749a42010-03-25 15:38:42 +00001019 Sema::LocalInstantiationScope InstScope(*this);
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001020 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
John McCall833ca992009-10-29 08:12:44 +00001021 const TemplateArgumentLoc *PartialTemplateArgs
1022 = Partial->getTemplateArgsAsWritten();
1023 unsigned N = Partial->getNumTemplateArgsAsWritten();
John McCalld5532b62009-11-23 01:53:49 +00001024
1025 // Note that we don't provide the langle and rangle locations.
1026 TemplateArgumentListInfo InstArgs;
1027
John McCall833ca992009-10-29 08:12:44 +00001028 for (unsigned I = 0; I != N; ++I) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001029 Decl *Param = const_cast<NamedDecl *>(
Douglas Gregorc9e5d252009-06-13 00:59:32 +00001030 ClassTemplate->getTemplateParameters()->getParam(I));
John McCalld5532b62009-11-23 01:53:49 +00001031 TemplateArgumentLoc InstArg;
1032 if (Subst(PartialTemplateArgs[I], InstArg,
John McCall833ca992009-10-29 08:12:44 +00001033 MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001034 Info.Param = makeTemplateParameter(Param);
John McCall833ca992009-10-29 08:12:44 +00001035 Info.FirstArg = PartialTemplateArgs[I].getArgument();
Mike Stump1eb44332009-09-09 15:08:12 +00001036 return TDK_SubstitutionFailure;
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001037 }
John McCalld5532b62009-11-23 01:53:49 +00001038 InstArgs.addArgument(InstArg);
John McCall833ca992009-10-29 08:12:44 +00001039 }
1040
1041 TemplateArgumentListBuilder ConvertedInstArgs(
1042 ClassTemplate->getTemplateParameters(), N);
1043
1044 if (CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001045 InstArgs, false, ConvertedInstArgs)) {
John McCall833ca992009-10-29 08:12:44 +00001046 // FIXME: fail with more useful information?
1047 return TDK_SubstitutionFailure;
1048 }
1049
1050 for (unsigned I = 0, E = ConvertedInstArgs.flatSize(); I != E; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001051 TemplateArgument InstArg = ConvertedInstArgs.getFlatArguments()[I];
John McCall833ca992009-10-29 08:12:44 +00001052
1053 Decl *Param = const_cast<NamedDecl *>(
1054 ClassTemplate->getTemplateParameters()->getParam(I));
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001056 if (InstArg.getKind() == TemplateArgument::Expression) {
Mike Stump1eb44332009-09-09 15:08:12 +00001057 // When the argument is an expression, check the expression result
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001058 // against the actual template parameter to get down to the canonical
1059 // template argument.
1060 Expr *InstExpr = InstArg.getAsExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001061 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001062 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1063 if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
1064 Info.Param = makeTemplateParameter(Param);
John McCall833ca992009-10-29 08:12:44 +00001065 Info.FirstArg = Partial->getTemplateArgs()[I];
Mike Stump1eb44332009-09-09 15:08:12 +00001066 return TDK_SubstitutionFailure;
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001067 }
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001068 }
1069 }
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001071 if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) {
1072 Info.Param = makeTemplateParameter(Param);
1073 Info.FirstArg = TemplateArgs[I];
1074 Info.SecondArg = InstArg;
1075 return TDK_NonDeducedMismatch;
1076 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001077 }
1078
Douglas Gregorbb260412009-06-14 08:02:22 +00001079 if (Trap.hasErrorOccurred())
1080 return TDK_SubstitutionFailure;
1081
Douglas Gregorf67875d2009-06-12 18:26:56 +00001082 return TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001083}
Douglas Gregor031a5882009-06-13 00:26:55 +00001084
Douglas Gregor41128772009-06-26 23:27:24 +00001085/// \brief Determine whether the given type T is a simple-template-id type.
1086static bool isSimpleTemplateIdType(QualType T) {
Mike Stump1eb44332009-09-09 15:08:12 +00001087 if (const TemplateSpecializationType *Spec
John McCall183700f2009-09-21 23:43:11 +00001088 = T->getAs<TemplateSpecializationType>())
Douglas Gregor41128772009-06-26 23:27:24 +00001089 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Douglas Gregor41128772009-06-26 23:27:24 +00001091 return false;
1092}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001093
1094/// \brief Substitute the explicitly-provided template arguments into the
1095/// given function template according to C++ [temp.arg.explicit].
1096///
1097/// \param FunctionTemplate the function template into which the explicit
1098/// template arguments will be substituted.
1099///
Mike Stump1eb44332009-09-09 15:08:12 +00001100/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001101/// arguments.
1102///
Mike Stump1eb44332009-09-09 15:08:12 +00001103/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor83314aa2009-07-08 20:55:45 +00001104/// with the converted and checked explicit template arguments.
1105///
Mike Stump1eb44332009-09-09 15:08:12 +00001106/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor83314aa2009-07-08 20:55:45 +00001107/// parameters.
1108///
1109/// \param FunctionType if non-NULL, the result type of the function template
1110/// will also be instantiated and the pointed-to value will be updated with
1111/// the instantiated function type.
1112///
1113/// \param Info if substitution fails for any reason, this object will be
1114/// populated with more information about the failure.
1115///
1116/// \returns TDK_Success if substitution was successful, or some failure
1117/// condition.
1118Sema::TemplateDeductionResult
1119Sema::SubstituteExplicitTemplateArguments(
1120 FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001121 const TemplateArgumentListInfo &ExplicitTemplateArgs,
Douglas Gregor02024a92010-03-28 02:42:43 +00001122 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001123 llvm::SmallVectorImpl<QualType> &ParamTypes,
1124 QualType *FunctionType,
1125 TemplateDeductionInfo &Info) {
1126 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1127 TemplateParameterList *TemplateParams
1128 = FunctionTemplate->getTemplateParameters();
1129
John McCalld5532b62009-11-23 01:53:49 +00001130 if (ExplicitTemplateArgs.size() == 0) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001131 // No arguments to substitute; just copy over the parameter types and
1132 // fill in the function type.
1133 for (FunctionDecl::param_iterator P = Function->param_begin(),
1134 PEnd = Function->param_end();
1135 P != PEnd;
1136 ++P)
1137 ParamTypes.push_back((*P)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Douglas Gregor83314aa2009-07-08 20:55:45 +00001139 if (FunctionType)
1140 *FunctionType = Function->getType();
1141 return TDK_Success;
1142 }
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Douglas Gregor83314aa2009-07-08 20:55:45 +00001144 // Substitution of the explicit template arguments into a function template
1145 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001146 SFINAETrap Trap(*this);
1147
Douglas Gregor83314aa2009-07-08 20:55:45 +00001148 // C++ [temp.arg.explicit]p3:
Mike Stump1eb44332009-09-09 15:08:12 +00001149 // Template arguments that are present shall be specified in the
1150 // declaration order of their corresponding template-parameters. The
Douglas Gregor83314aa2009-07-08 20:55:45 +00001151 // template argument list shall not specify more template-arguments than
Mike Stump1eb44332009-09-09 15:08:12 +00001152 // there are corresponding template-parameters.
1153 TemplateArgumentListBuilder Builder(TemplateParams,
John McCalld5532b62009-11-23 01:53:49 +00001154 ExplicitTemplateArgs.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001155
1156 // Enter a new template instantiation context where we check the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001157 // explicitly-specified template arguments against this function template,
1158 // and then substitute them into the function parameter types.
Mike Stump1eb44332009-09-09 15:08:12 +00001159 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001160 FunctionTemplate, Deduced.data(), Deduced.size(),
1161 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution);
1162 if (Inst)
1163 return TDK_InstantiationDepth;
Mike Stump1eb44332009-09-09 15:08:12 +00001164
Douglas Gregor83314aa2009-07-08 20:55:45 +00001165 if (CheckTemplateArgumentList(FunctionTemplate,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001166 SourceLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001167 ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001168 true,
1169 Builder) || Trap.hasErrorOccurred())
1170 return TDK_InvalidExplicitArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001171
Douglas Gregor83314aa2009-07-08 20:55:45 +00001172 // Form the template argument list from the explicitly-specified
1173 // template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001174 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor83314aa2009-07-08 20:55:45 +00001175 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1176 Info.reset(ExplicitArgumentList);
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Douglas Gregor83314aa2009-07-08 20:55:45 +00001178 // Instantiate the types of each of the function parameters given the
1179 // explicitly-specified template arguments.
1180 for (FunctionDecl::param_iterator P = Function->param_begin(),
1181 PEnd = Function->param_end();
1182 P != PEnd;
1183 ++P) {
Mike Stump1eb44332009-09-09 15:08:12 +00001184 QualType ParamType
1185 = SubstType((*P)->getType(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001186 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1187 (*P)->getLocation(), (*P)->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001188 if (ParamType.isNull() || Trap.hasErrorOccurred())
1189 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Douglas Gregor83314aa2009-07-08 20:55:45 +00001191 ParamTypes.push_back(ParamType);
1192 }
1193
1194 // If the caller wants a full function type back, instantiate the return
1195 // type and form that function type.
1196 if (FunctionType) {
1197 // FIXME: exception-specifications?
Mike Stump1eb44332009-09-09 15:08:12 +00001198 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001199 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001200 assert(Proto && "Function template does not have a prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001201
1202 QualType ResultType
Douglas Gregor357bbd02009-08-28 20:50:45 +00001203 = SubstType(Proto->getResultType(),
1204 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1205 Function->getTypeSpecStartLoc(),
1206 Function->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001207 if (ResultType.isNull() || Trap.hasErrorOccurred())
1208 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001209
1210 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001211 ParamTypes.data(), ParamTypes.size(),
1212 Proto->isVariadic(),
1213 Proto->getTypeQuals(),
1214 Function->getLocation(),
1215 Function->getDeclName());
1216 if (FunctionType->isNull() || Trap.hasErrorOccurred())
1217 return TDK_SubstitutionFailure;
1218 }
Mike Stump1eb44332009-09-09 15:08:12 +00001219
Douglas Gregor83314aa2009-07-08 20:55:45 +00001220 // C++ [temp.arg.explicit]p2:
Mike Stump1eb44332009-09-09 15:08:12 +00001221 // Trailing template arguments that can be deduced (14.8.2) may be
1222 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001223 // template arguments can be deduced, they may all be omitted; in this
1224 // case, the empty template argument list <> itself may also be omitted.
1225 //
1226 // Take all of the explicitly-specified arguments and put them into the
Mike Stump1eb44332009-09-09 15:08:12 +00001227 // set of deduced template arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001228 Deduced.reserve(TemplateParams->size());
1229 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001230 Deduced.push_back(ExplicitArgumentList->get(I));
1231
Douglas Gregor83314aa2009-07-08 20:55:45 +00001232 return TDK_Success;
1233}
1234
Douglas Gregor02024a92010-03-28 02:42:43 +00001235/// \brief Allocate a TemplateArgumentLoc where all locations have
1236/// been initialized to the given location.
1237///
1238/// \param S The semantic analysis object.
1239///
1240/// \param The template argument we are producing template argument
1241/// location information for.
1242///
1243/// \param NTTPType For a declaration template argument, the type of
1244/// the non-type template parameter that corresponds to this template
1245/// argument.
1246///
1247/// \param Loc The source location to use for the resulting template
1248/// argument.
1249static TemplateArgumentLoc
1250getTrivialTemplateArgumentLoc(Sema &S,
1251 const TemplateArgument &Arg,
1252 QualType NTTPType,
1253 SourceLocation Loc) {
1254 switch (Arg.getKind()) {
1255 case TemplateArgument::Null:
1256 llvm_unreachable("Can't get a NULL template argument here");
1257 break;
1258
1259 case TemplateArgument::Type:
1260 return TemplateArgumentLoc(Arg,
1261 S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
1262
1263 case TemplateArgument::Declaration: {
1264 Expr *E
1265 = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
1266 .takeAs<Expr>();
1267 return TemplateArgumentLoc(TemplateArgument(E), E);
1268 }
1269
1270 case TemplateArgument::Integral: {
1271 Expr *E
1272 = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>();
1273 return TemplateArgumentLoc(TemplateArgument(E), E);
1274 }
1275
1276 case TemplateArgument::Template:
1277 return TemplateArgumentLoc(Arg, SourceRange(), Loc);
1278
1279 case TemplateArgument::Expression:
1280 return TemplateArgumentLoc(Arg, Arg.getAsExpr());
1281
1282 case TemplateArgument::Pack:
1283 llvm_unreachable("Template parameter packs are not yet supported");
1284 }
1285
1286 return TemplateArgumentLoc();
1287}
1288
Mike Stump1eb44332009-09-09 15:08:12 +00001289/// \brief Finish template argument deduction for a function template,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001290/// checking the deduced template arguments for completeness and forming
1291/// the function template specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00001292Sema::TemplateDeductionResult
Douglas Gregor83314aa2009-07-08 20:55:45 +00001293Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor02024a92010-03-28 02:42:43 +00001294 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1295 unsigned NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001296 FunctionDecl *&Specialization,
1297 TemplateDeductionInfo &Info) {
1298 TemplateParameterList *TemplateParams
1299 = FunctionTemplate->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001300
Douglas Gregor83314aa2009-07-08 20:55:45 +00001301 // Template argument deduction for function templates in a SFINAE context.
1302 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001303 SFINAETrap Trap(*this);
1304
Douglas Gregor83314aa2009-07-08 20:55:45 +00001305 // Enter a new template instantiation context while we instantiate the
1306 // actual function declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001307 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001308 FunctionTemplate, Deduced.data(), Deduced.size(),
1309 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
1310 if (Inst)
Mike Stump1eb44332009-09-09 15:08:12 +00001311 return TDK_InstantiationDepth;
1312
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001313 // C++ [temp.deduct.type]p2:
1314 // [...] or if any template argument remains neither deduced nor
1315 // explicitly specified, template argument deduction fails.
1316 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
1317 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregor02024a92010-03-28 02:42:43 +00001318 NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001319 if (!Deduced[I].isNull()) {
Douglas Gregor02024a92010-03-28 02:42:43 +00001320 if (I < NumExplicitlySpecified ||
1321 Deduced[I].getKind() == TemplateArgument::Type) {
1322 // We have already fully type-checked and converted this
1323 // argument (because it was explicitly-specified) or no
1324 // additional checking is necessary (because it's a template
1325 // type parameter). Just record the presence of this
1326 // parameter.
1327 Builder.Append(Deduced[I]);
1328 continue;
1329 }
1330
1331 // We have deduced this argument, so it still needs to be
1332 // checked and converted.
1333
1334 // First, for a non-type template parameter type that is
1335 // initialized by a declaration, we need the type of the
1336 // corresponding non-type template parameter.
1337 QualType NTTPType;
1338 if (NonTypeTemplateParmDecl *NTTP
1339 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1340 if (Deduced[I].getKind() == TemplateArgument::Declaration) {
1341 NTTPType = NTTP->getType();
1342 if (NTTPType->isDependentType()) {
1343 TemplateArgumentList TemplateArgs(Context, Builder,
1344 /*TakeArgs=*/false);
1345 NTTPType = SubstType(NTTPType,
1346 MultiLevelTemplateArgumentList(TemplateArgs),
1347 NTTP->getLocation(),
1348 NTTP->getDeclName());
1349 if (NTTPType.isNull()) {
1350 Info.Param = makeTemplateParameter(Param);
1351 return TDK_SubstitutionFailure;
1352 }
1353 }
1354 }
1355 }
1356
1357 // Convert the deduced template argument into a template
1358 // argument that we can check, almost as if the user had written
1359 // the template argument explicitly.
1360 TemplateArgumentLoc Arg = getTrivialTemplateArgumentLoc(*this,
1361 Deduced[I],
1362 NTTPType,
1363 SourceLocation());
1364
1365 // Check the template argument, converting it as necessary.
1366 if (CheckTemplateArgument(Param, Arg,
1367 FunctionTemplate,
1368 FunctionTemplate->getLocation(),
1369 FunctionTemplate->getSourceRange().getEnd(),
1370 Builder,
1371 Deduced[I].wasDeducedFromArrayBound()
1372 ? CTAK_DeducedFromArrayBound
1373 : CTAK_Deduced)) {
1374 Info.Param = makeTemplateParameter(
1375 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1376 return TDK_SubstitutionFailure;
1377 }
1378
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001379 continue;
1380 }
1381
1382 // Substitute into the default template argument, if available.
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001383 TemplateArgumentLoc DefArg
1384 = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
1385 FunctionTemplate->getLocation(),
1386 FunctionTemplate->getSourceRange().getEnd(),
1387 Param,
1388 Builder);
1389
1390 // If there was no default argument, deduction is incomplete.
1391 if (DefArg.getArgument().isNull()) {
1392 Info.Param = makeTemplateParameter(
1393 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1394 return TDK_Incomplete;
1395 }
1396
1397 // Check whether we can actually use the default argument.
1398 if (CheckTemplateArgument(Param, DefArg,
1399 FunctionTemplate,
1400 FunctionTemplate->getLocation(),
1401 FunctionTemplate->getSourceRange().getEnd(),
Douglas Gregor02024a92010-03-28 02:42:43 +00001402 Builder,
1403 CTAK_Deduced)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001404 Info.Param = makeTemplateParameter(
1405 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1406 return TDK_SubstitutionFailure;
1407 }
1408
1409 // If we get here, we successfully used the default template argument.
1410 }
1411
1412 // Form the template argument list from the deduced template arguments.
1413 TemplateArgumentList *DeducedArgumentList
1414 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1415 Info.reset(DeducedArgumentList);
1416
Mike Stump1eb44332009-09-09 15:08:12 +00001417 // Substitute the deduced template arguments into the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001418 // declaration to produce the function template specialization.
1419 Specialization = cast_or_null<FunctionDecl>(
John McCallce3ff2b2009-08-25 22:02:44 +00001420 SubstDecl(FunctionTemplate->getTemplatedDecl(),
1421 FunctionTemplate->getDeclContext(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001422 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor83314aa2009-07-08 20:55:45 +00001423 if (!Specialization)
1424 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001425
Douglas Gregorf8825742009-09-15 18:26:13 +00001426 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
1427 FunctionTemplate->getCanonicalDecl());
1428
Mike Stump1eb44332009-09-09 15:08:12 +00001429 // If the template argument list is owned by the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001430 // specialization, release it.
1431 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList)
1432 Info.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Douglas Gregor83314aa2009-07-08 20:55:45 +00001434 // There may have been an error that did not prevent us from constructing a
1435 // declaration. Mark the declaration invalid and return with a substitution
1436 // failure.
1437 if (Trap.hasErrorOccurred()) {
1438 Specialization->setInvalidDecl(true);
1439 return TDK_SubstitutionFailure;
1440 }
Mike Stump1eb44332009-09-09 15:08:12 +00001441
1442 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001443}
1444
John McCalleff92132010-02-02 02:21:27 +00001445static QualType GetTypeOfFunction(ASTContext &Context,
1446 bool isAddressOfOperand,
1447 FunctionDecl *Fn) {
1448 if (!isAddressOfOperand) return Fn->getType();
1449 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
1450 if (Method->isInstance())
1451 return Context.getMemberPointerType(Fn->getType(),
1452 Context.getTypeDeclType(Method->getParent()).getTypePtr());
1453 return Context.getPointerType(Fn->getType());
1454}
1455
1456/// Apply the deduction rules for overload sets.
1457///
1458/// \return the null type if this argument should be treated as an
1459/// undeduced context
1460static QualType
1461ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
1462 Expr *Arg, QualType ParamType) {
John McCall7bb12da2010-02-02 06:20:04 +00001463 llvm::PointerIntPair<OverloadExpr*,1> R = OverloadExpr::find(Arg);
John McCalleff92132010-02-02 02:21:27 +00001464
John McCall7bb12da2010-02-02 06:20:04 +00001465 bool isAddressOfOperand = bool(R.getInt());
1466 OverloadExpr *Ovl = R.getPointer();
John McCalleff92132010-02-02 02:21:27 +00001467
1468 // If there were explicit template arguments, we can only find
1469 // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
1470 // unambiguously name a full specialization.
John McCall7bb12da2010-02-02 06:20:04 +00001471 if (Ovl->hasExplicitTemplateArgs()) {
John McCalleff92132010-02-02 02:21:27 +00001472 // But we can still look for an explicit specialization.
1473 if (FunctionDecl *ExplicitSpec
John McCall7bb12da2010-02-02 06:20:04 +00001474 = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
1475 return GetTypeOfFunction(S.Context, isAddressOfOperand, ExplicitSpec);
John McCalleff92132010-02-02 02:21:27 +00001476 return QualType();
1477 }
1478
1479 // C++0x [temp.deduct.call]p6:
1480 // When P is a function type, pointer to function type, or pointer
1481 // to member function type:
1482
1483 if (!ParamType->isFunctionType() &&
1484 !ParamType->isFunctionPointerType() &&
1485 !ParamType->isMemberFunctionPointerType())
1486 return QualType();
1487
1488 QualType Match;
John McCall7bb12da2010-02-02 06:20:04 +00001489 for (UnresolvedSetIterator I = Ovl->decls_begin(),
1490 E = Ovl->decls_end(); I != E; ++I) {
John McCalleff92132010-02-02 02:21:27 +00001491 NamedDecl *D = (*I)->getUnderlyingDecl();
1492
1493 // - If the argument is an overload set containing one or more
1494 // function templates, the parameter is treated as a
1495 // non-deduced context.
1496 if (isa<FunctionTemplateDecl>(D))
1497 return QualType();
1498
1499 FunctionDecl *Fn = cast<FunctionDecl>(D);
1500 QualType ArgType = GetTypeOfFunction(S.Context, isAddressOfOperand, Fn);
1501
1502 // - If the argument is an overload set (not containing function
1503 // templates), trial argument deduction is attempted using each
1504 // of the members of the set. If deduction succeeds for only one
1505 // of the overload set members, that member is used as the
1506 // argument value for the deduction. If deduction succeeds for
1507 // more than one member of the overload set the parameter is
1508 // treated as a non-deduced context.
1509
1510 // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
1511 // Type deduction is done independently for each P/A pair, and
1512 // the deduced template argument values are then combined.
1513 // So we do not reject deductions which were made elsewhere.
Douglas Gregor02024a92010-03-28 02:42:43 +00001514 llvm::SmallVector<DeducedTemplateArgument, 8>
1515 Deduced(TemplateParams->size());
John McCall5769d612010-02-08 23:07:23 +00001516 Sema::TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
John McCalleff92132010-02-02 02:21:27 +00001517 unsigned TDF = 0;
1518
1519 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001520 = DeduceTemplateArguments(S, TemplateParams,
John McCalleff92132010-02-02 02:21:27 +00001521 ParamType, ArgType,
1522 Info, Deduced, TDF);
1523 if (Result) continue;
1524 if (!Match.isNull()) return QualType();
1525 Match = ArgType;
1526 }
1527
1528 return Match;
1529}
1530
Douglas Gregore53060f2009-06-25 22:08:12 +00001531/// \brief Perform template argument deduction from a function call
1532/// (C++ [temp.deduct.call]).
1533///
1534/// \param FunctionTemplate the function template for which we are performing
1535/// template argument deduction.
1536///
Douglas Gregor48026d22010-01-11 18:40:55 +00001537/// \param ExplicitTemplateArguments the explicit template arguments provided
1538/// for this call.
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001539///
Douglas Gregore53060f2009-06-25 22:08:12 +00001540/// \param Args the function call arguments
1541///
1542/// \param NumArgs the number of arguments in Args
1543///
Douglas Gregor48026d22010-01-11 18:40:55 +00001544/// \param Name the name of the function being called. This is only significant
1545/// when the function template is a conversion function template, in which
1546/// case this routine will also perform template argument deduction based on
1547/// the function to which
1548///
Douglas Gregore53060f2009-06-25 22:08:12 +00001549/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001550/// this will be set to the function template specialization produced by
Douglas Gregore53060f2009-06-25 22:08:12 +00001551/// template argument deduction.
1552///
1553/// \param Info the argument will be updated to provide additional information
1554/// about template argument deduction.
1555///
1556/// \returns the result of template argument deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001557Sema::TemplateDeductionResult
1558Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor48026d22010-01-11 18:40:55 +00001559 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregore53060f2009-06-25 22:08:12 +00001560 Expr **Args, unsigned NumArgs,
1561 FunctionDecl *&Specialization,
1562 TemplateDeductionInfo &Info) {
1563 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001564
Douglas Gregore53060f2009-06-25 22:08:12 +00001565 // C++ [temp.deduct.call]p1:
1566 // Template argument deduction is done by comparing each function template
1567 // parameter type (call it P) with the type of the corresponding argument
1568 // of the call (call it A) as described below.
1569 unsigned CheckArgs = NumArgs;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001570 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregore53060f2009-06-25 22:08:12 +00001571 return TDK_TooFewArguments;
1572 else if (NumArgs > Function->getNumParams()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001573 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001574 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregore53060f2009-06-25 22:08:12 +00001575 if (!Proto->isVariadic())
1576 return TDK_TooManyArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Douglas Gregore53060f2009-06-25 22:08:12 +00001578 CheckArgs = Function->getNumParams();
1579 }
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001581 // The types of the parameters from which we will perform template argument
1582 // deduction.
Douglas Gregor2b0749a42010-03-25 15:38:42 +00001583 Sema::LocalInstantiationScope InstScope(*this);
Douglas Gregore53060f2009-06-25 22:08:12 +00001584 TemplateParameterList *TemplateParams
1585 = FunctionTemplate->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00001586 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001587 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregor02024a92010-03-28 02:42:43 +00001588 unsigned NumExplicitlySpecified = 0;
John McCalld5532b62009-11-23 01:53:49 +00001589 if (ExplicitTemplateArgs) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001590 TemplateDeductionResult Result =
1591 SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001592 *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001593 Deduced,
1594 ParamTypes,
1595 0,
1596 Info);
1597 if (Result)
1598 return Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00001599
1600 NumExplicitlySpecified = Deduced.size();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001601 } else {
1602 // Just fill in the parameter types from the function declaration.
1603 for (unsigned I = 0; I != CheckArgs; ++I)
1604 ParamTypes.push_back(Function->getParamDecl(I)->getType());
1605 }
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001607 // Deduce template arguments from the function parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001608 Deduced.resize(TemplateParams->size());
Douglas Gregore53060f2009-06-25 22:08:12 +00001609 for (unsigned I = 0; I != CheckArgs; ++I) {
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001610 QualType ParamType = ParamTypes[I];
Douglas Gregore53060f2009-06-25 22:08:12 +00001611 QualType ArgType = Args[I]->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001612
John McCalleff92132010-02-02 02:21:27 +00001613 // Overload sets usually make this parameter an undeduced
1614 // context, but there are sometimes special circumstances.
1615 if (ArgType == Context.OverloadTy) {
1616 ArgType = ResolveOverloadForDeduction(*this, TemplateParams,
1617 Args[I], ParamType);
1618 if (ArgType.isNull())
1619 continue;
1620 }
1621
Douglas Gregore53060f2009-06-25 22:08:12 +00001622 // C++ [temp.deduct.call]p2:
1623 // If P is not a reference type:
1624 QualType CanonParamType = Context.getCanonicalType(ParamType);
Douglas Gregor500d3312009-06-26 18:27:22 +00001625 bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1626 if (!ParamWasReference) {
Mike Stump1eb44332009-09-09 15:08:12 +00001627 // - If A is an array type, the pointer type produced by the
1628 // array-to-pointer standard conversion (4.2) is used in place of
Douglas Gregore53060f2009-06-25 22:08:12 +00001629 // A for type deduction; otherwise,
1630 if (ArgType->isArrayType())
1631 ArgType = Context.getArrayDecayedType(ArgType);
Mike Stump1eb44332009-09-09 15:08:12 +00001632 // - If A is a function type, the pointer type produced by the
1633 // function-to-pointer standard conversion (4.3) is used in place
Douglas Gregore53060f2009-06-25 22:08:12 +00001634 // of A for type deduction; otherwise,
1635 else if (ArgType->isFunctionType())
1636 ArgType = Context.getPointerType(ArgType);
1637 else {
1638 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1639 // type are ignored for type deduction.
1640 QualType CanonArgType = Context.getCanonicalType(ArgType);
Douglas Gregora4923eb2009-11-16 21:35:15 +00001641 if (CanonArgType.getLocalCVRQualifiers())
1642 ArgType = CanonArgType.getLocalUnqualifiedType();
Douglas Gregore53060f2009-06-25 22:08:12 +00001643 }
1644 }
Mike Stump1eb44332009-09-09 15:08:12 +00001645
Douglas Gregore53060f2009-06-25 22:08:12 +00001646 // C++0x [temp.deduct.call]p3:
1647 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
Mike Stump1eb44332009-09-09 15:08:12 +00001648 // are ignored for type deduction.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001649 if (CanonParamType.getLocalCVRQualifiers())
1650 ParamType = CanonParamType.getLocalUnqualifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001651 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001652 // [...] If P is a reference type, the type referred to by P is used
1653 // for type deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001654 ParamType = ParamRefType->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00001655
1656 // [...] If P is of the form T&&, where T is a template parameter, and
1657 // the argument is an lvalue, the type A& is used in place of A for
Douglas Gregore53060f2009-06-25 22:08:12 +00001658 // type deduction.
1659 if (isa<RValueReferenceType>(ParamRefType) &&
John McCall183700f2009-09-21 23:43:11 +00001660 ParamRefType->getAs<TemplateTypeParmType>() &&
Douglas Gregore53060f2009-06-25 22:08:12 +00001661 Args[I]->isLvalue(Context) == Expr::LV_Valid)
1662 ArgType = Context.getLValueReferenceType(ArgType);
1663 }
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Douglas Gregore53060f2009-06-25 22:08:12 +00001665 // C++0x [temp.deduct.call]p4:
1666 // In general, the deduction process attempts to find template argument
1667 // values that will make the deduced A identical to A (after the type A
1668 // is transformed as described above). [...]
Douglas Gregor12820292009-09-14 20:00:47 +00001669 unsigned TDF = TDF_SkipNonDependent;
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Douglas Gregor508f1c82009-06-26 23:10:12 +00001671 // - If the original P is a reference type, the deduced A (i.e., the
1672 // type referred to by the reference) can be more cv-qualified than
1673 // the transformed A.
1674 if (ParamWasReference)
1675 TDF |= TDF_ParamWithReferenceType;
Mike Stump1eb44332009-09-09 15:08:12 +00001676 // - The transformed A can be another pointer or pointer to member
1677 // type that can be converted to the deduced A via a qualification
Douglas Gregor508f1c82009-06-26 23:10:12 +00001678 // conversion (4.4).
1679 if (ArgType->isPointerType() || ArgType->isMemberPointerType())
1680 TDF |= TDF_IgnoreQualifiers;
Mike Stump1eb44332009-09-09 15:08:12 +00001681 // - If P is a class and P has the form simple-template-id, then the
Douglas Gregor41128772009-06-26 23:27:24 +00001682 // transformed A can be a derived class of the deduced A. Likewise,
1683 // if P is a pointer to a class of the form simple-template-id, the
1684 // transformed A can be a pointer to a derived class pointed to by
1685 // the deduced A.
1686 if (isSimpleTemplateIdType(ParamType) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001687 (isa<PointerType>(ParamType) &&
Douglas Gregor41128772009-06-26 23:27:24 +00001688 isSimpleTemplateIdType(
Ted Kremenek6217b802009-07-29 21:53:49 +00001689 ParamType->getAs<PointerType>()->getPointeeType())))
Douglas Gregor41128772009-06-26 23:27:24 +00001690 TDF |= TDF_DerivedClass;
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Douglas Gregore53060f2009-06-25 22:08:12 +00001692 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001693 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor500d3312009-06-26 18:27:22 +00001694 ParamType, ArgType, Info, Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +00001695 TDF))
Douglas Gregore53060f2009-06-25 22:08:12 +00001696 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001697
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001698 // FIXME: we need to check that the deduced A is the same as A,
1699 // modulo the various allowed differences.
Douglas Gregore53060f2009-06-25 22:08:12 +00001700 }
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001701
Mike Stump1eb44332009-09-09 15:08:12 +00001702 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor02024a92010-03-28 02:42:43 +00001703 NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001704 Specialization, Info);
Douglas Gregore53060f2009-06-25 22:08:12 +00001705}
1706
Douglas Gregor83314aa2009-07-08 20:55:45 +00001707/// \brief Deduce template arguments when taking the address of a function
Douglas Gregor4b52e252009-12-21 23:17:24 +00001708/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
1709/// a template.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001710///
1711/// \param FunctionTemplate the function template for which we are performing
1712/// template argument deduction.
1713///
Douglas Gregor4b52e252009-12-21 23:17:24 +00001714/// \param ExplicitTemplateArguments the explicitly-specified template
1715/// arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001716///
1717/// \param ArgFunctionType the function type that will be used as the
1718/// "argument" type (A) when performing template argument deduction from the
Douglas Gregor4b52e252009-12-21 23:17:24 +00001719/// function template's function type. This type may be NULL, if there is no
1720/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001721///
1722/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001723/// this will be set to the function template specialization produced by
Douglas Gregor83314aa2009-07-08 20:55:45 +00001724/// template argument deduction.
1725///
1726/// \param Info the argument will be updated to provide additional information
1727/// about template argument deduction.
1728///
1729/// \returns the result of template argument deduction.
1730Sema::TemplateDeductionResult
1731Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001732 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001733 QualType ArgFunctionType,
1734 FunctionDecl *&Specialization,
1735 TemplateDeductionInfo &Info) {
1736 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1737 TemplateParameterList *TemplateParams
1738 = FunctionTemplate->getTemplateParameters();
1739 QualType FunctionType = Function->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001740
Douglas Gregor83314aa2009-07-08 20:55:45 +00001741 // Substitute any explicit template arguments.
Douglas Gregor2b0749a42010-03-25 15:38:42 +00001742 Sema::LocalInstantiationScope InstScope(*this);
Douglas Gregor02024a92010-03-28 02:42:43 +00001743 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
1744 unsigned NumExplicitlySpecified = 0;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001745 llvm::SmallVector<QualType, 4> ParamTypes;
John McCalld5532b62009-11-23 01:53:49 +00001746 if (ExplicitTemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001747 if (TemplateDeductionResult Result
1748 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001749 *ExplicitTemplateArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00001750 Deduced, ParamTypes,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001751 &FunctionType, Info))
1752 return Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00001753
1754 NumExplicitlySpecified = Deduced.size();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001755 }
1756
1757 // Template argument deduction for function templates in a SFINAE context.
1758 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001759 SFINAETrap Trap(*this);
1760
John McCalleff92132010-02-02 02:21:27 +00001761 Deduced.resize(TemplateParams->size());
1762
Douglas Gregor4b52e252009-12-21 23:17:24 +00001763 if (!ArgFunctionType.isNull()) {
1764 // Deduce template arguments from the function type.
Douglas Gregor4b52e252009-12-21 23:17:24 +00001765 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001766 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor4b52e252009-12-21 23:17:24 +00001767 FunctionType, ArgFunctionType, Info,
1768 Deduced, 0))
1769 return Result;
1770 }
1771
Mike Stump1eb44332009-09-09 15:08:12 +00001772 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor02024a92010-03-28 02:42:43 +00001773 NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001774 Specialization, Info);
1775}
1776
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001777/// \brief Deduce template arguments for a templated conversion
1778/// function (C++ [temp.deduct.conv]) and, if successful, produce a
1779/// conversion function template specialization.
1780Sema::TemplateDeductionResult
1781Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1782 QualType ToType,
1783 CXXConversionDecl *&Specialization,
1784 TemplateDeductionInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00001785 CXXConversionDecl *Conv
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001786 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
1787 QualType FromType = Conv->getConversionType();
1788
1789 // Canonicalize the types for deduction.
1790 QualType P = Context.getCanonicalType(FromType);
1791 QualType A = Context.getCanonicalType(ToType);
1792
1793 // C++0x [temp.deduct.conv]p3:
1794 // If P is a reference type, the type referred to by P is used for
1795 // type deduction.
1796 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
1797 P = PRef->getPointeeType();
1798
1799 // C++0x [temp.deduct.conv]p3:
1800 // If A is a reference type, the type referred to by A is used
1801 // for type deduction.
1802 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
1803 A = ARef->getPointeeType();
1804 // C++ [temp.deduct.conv]p2:
1805 //
Mike Stump1eb44332009-09-09 15:08:12 +00001806 // If A is not a reference type:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001807 else {
1808 assert(!A->isReferenceType() && "Reference types were handled above");
1809
1810 // - If P is an array type, the pointer type produced by the
Mike Stump1eb44332009-09-09 15:08:12 +00001811 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001812 // of P for type deduction; otherwise,
1813 if (P->isArrayType())
1814 P = Context.getArrayDecayedType(P);
1815 // - If P is a function type, the pointer type produced by the
1816 // function-to-pointer standard conversion (4.3) is used in
1817 // place of P for type deduction; otherwise,
1818 else if (P->isFunctionType())
1819 P = Context.getPointerType(P);
1820 // - If P is a cv-qualified type, the top level cv-qualifiers of
1821 // P’s type are ignored for type deduction.
1822 else
1823 P = P.getUnqualifiedType();
1824
1825 // C++0x [temp.deduct.conv]p3:
1826 // If A is a cv-qualified type, the top level cv-qualifiers of A’s
1827 // type are ignored for type deduction.
1828 A = A.getUnqualifiedType();
1829 }
1830
1831 // Template argument deduction for function templates in a SFINAE context.
1832 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001833 SFINAETrap Trap(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001834
1835 // C++ [temp.deduct.conv]p1:
1836 // Template argument deduction is done by comparing the return
1837 // type of the template conversion function (call it P) with the
1838 // type that is required as the result of the conversion (call it
1839 // A) as described in 14.8.2.4.
1840 TemplateParameterList *TemplateParams
1841 = FunctionTemplate->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00001842 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Mike Stump1eb44332009-09-09 15:08:12 +00001843 Deduced.resize(TemplateParams->size());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001844
1845 // C++0x [temp.deduct.conv]p4:
1846 // In general, the deduction process attempts to find template
1847 // argument values that will make the deduced A identical to
1848 // A. However, there are two cases that allow a difference:
1849 unsigned TDF = 0;
1850 // - If the original A is a reference type, A can be more
1851 // cv-qualified than the deduced A (i.e., the type referred to
1852 // by the reference)
1853 if (ToType->isReferenceType())
1854 TDF |= TDF_ParamWithReferenceType;
1855 // - The deduced A can be another pointer or pointer to member
1856 // type that can be converted to A via a qualification
1857 // conversion.
1858 //
1859 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
1860 // both P and A are pointers or member pointers. In this case, we
1861 // just ignore cv-qualifiers completely).
1862 if ((P->isPointerType() && A->isPointerType()) ||
1863 (P->isMemberPointerType() && P->isMemberPointerType()))
1864 TDF |= TDF_IgnoreQualifiers;
1865 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001866 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001867 P, A, Info, Deduced, TDF))
1868 return Result;
1869
1870 // FIXME: we need to check that the deduced A is the same as A,
1871 // modulo the various allowed differences.
Mike Stump1eb44332009-09-09 15:08:12 +00001872
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001873 // Finish template argument deduction.
Douglas Gregor2b0749a42010-03-25 15:38:42 +00001874 Sema::LocalInstantiationScope InstScope(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001875 FunctionDecl *Spec = 0;
1876 TemplateDeductionResult Result
Douglas Gregor02024a92010-03-28 02:42:43 +00001877 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec,
1878 Info);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001879 Specialization = cast_or_null<CXXConversionDecl>(Spec);
1880 return Result;
1881}
1882
Douglas Gregor4b52e252009-12-21 23:17:24 +00001883/// \brief Deduce template arguments for a function template when there is
1884/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
1885///
1886/// \param FunctionTemplate the function template for which we are performing
1887/// template argument deduction.
1888///
1889/// \param ExplicitTemplateArguments the explicitly-specified template
1890/// arguments.
1891///
1892/// \param Specialization if template argument deduction was successful,
1893/// this will be set to the function template specialization produced by
1894/// template argument deduction.
1895///
1896/// \param Info the argument will be updated to provide additional information
1897/// about template argument deduction.
1898///
1899/// \returns the result of template argument deduction.
1900Sema::TemplateDeductionResult
1901Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1902 const TemplateArgumentListInfo *ExplicitTemplateArgs,
1903 FunctionDecl *&Specialization,
1904 TemplateDeductionInfo &Info) {
1905 return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
1906 QualType(), Specialization, Info);
1907}
1908
Douglas Gregor8a514912009-09-14 18:39:43 +00001909/// \brief Stores the result of comparing the qualifiers of two types.
1910enum DeductionQualifierComparison {
1911 NeitherMoreQualified = 0,
1912 ParamMoreQualified,
1913 ArgMoreQualified
1914};
1915
1916/// \brief Deduce the template arguments during partial ordering by comparing
1917/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
1918///
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001919/// \param S the semantic analysis object within which we are deducing
Douglas Gregor8a514912009-09-14 18:39:43 +00001920///
1921/// \param TemplateParams the template parameters that we are deducing
1922///
1923/// \param ParamIn the parameter type
1924///
1925/// \param ArgIn the argument type
1926///
1927/// \param Info information about the template argument deduction itself
1928///
1929/// \param Deduced the deduced template arguments
1930///
1931/// \returns the result of template argument deduction so far. Note that a
1932/// "success" result means that template argument deduction has not yet failed,
1933/// but it may still fail, later, for other reasons.
1934static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001935DeduceTemplateArgumentsDuringPartialOrdering(Sema &S,
Douglas Gregor02024a92010-03-28 02:42:43 +00001936 TemplateParameterList *TemplateParams,
Douglas Gregor8a514912009-09-14 18:39:43 +00001937 QualType ParamIn, QualType ArgIn,
1938 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +00001939 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1940 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001941 CanQualType Param = S.Context.getCanonicalType(ParamIn);
1942 CanQualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor8a514912009-09-14 18:39:43 +00001943
1944 // C++0x [temp.deduct.partial]p5:
1945 // Before the partial ordering is done, certain transformations are
1946 // performed on the types used for partial ordering:
1947 // - If P is a reference type, P is replaced by the type referred to.
1948 CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00001949 if (!ParamRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00001950 Param = ParamRef->getPointeeType();
1951
1952 // - If A is a reference type, A is replaced by the type referred to.
1953 CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00001954 if (!ArgRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00001955 Arg = ArgRef->getPointeeType();
1956
John McCalle27ec8a2009-10-23 23:03:21 +00001957 if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
Douglas Gregor8a514912009-09-14 18:39:43 +00001958 // C++0x [temp.deduct.partial]p6:
1959 // If both P and A were reference types (before being replaced with the
1960 // type referred to above), determine which of the two types (if any) is
1961 // more cv-qualified than the other; otherwise the types are considered to
1962 // be equally cv-qualified for partial ordering purposes. The result of this
1963 // determination will be used below.
1964 //
1965 // We save this information for later, using it only when deduction
1966 // succeeds in both directions.
1967 DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
1968 if (Param.isMoreQualifiedThan(Arg))
1969 QualifierResult = ParamMoreQualified;
1970 else if (Arg.isMoreQualifiedThan(Param))
1971 QualifierResult = ArgMoreQualified;
1972 QualifierComparisons->push_back(QualifierResult);
1973 }
1974
1975 // C++0x [temp.deduct.partial]p7:
1976 // Remove any top-level cv-qualifiers:
1977 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
1978 // version of P.
1979 Param = Param.getUnqualifiedType();
1980 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
1981 // version of A.
1982 Arg = Arg.getUnqualifiedType();
1983
1984 // C++0x [temp.deduct.partial]p8:
1985 // Using the resulting types P and A the deduction is then done as
1986 // described in 14.9.2.5. If deduction succeeds for a given type, the type
1987 // from the argument template is considered to be at least as specialized
1988 // as the type from the parameter template.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001989 return DeduceTemplateArguments(S, TemplateParams, Param, Arg, Info,
Douglas Gregor8a514912009-09-14 18:39:43 +00001990 Deduced, TDF_None);
1991}
1992
1993static void
Douglas Gregore73bb602009-09-14 21:25:05 +00001994MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
1995 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001996 unsigned Level,
Douglas Gregore73bb602009-09-14 21:25:05 +00001997 llvm::SmallVectorImpl<bool> &Deduced);
Douglas Gregor8a514912009-09-14 18:39:43 +00001998
1999/// \brief Determine whether the function template \p FT1 is at least as
2000/// specialized as \p FT2.
2001static bool isAtLeastAsSpecializedAs(Sema &S,
John McCall5769d612010-02-08 23:07:23 +00002002 SourceLocation Loc,
Douglas Gregor8a514912009-09-14 18:39:43 +00002003 FunctionTemplateDecl *FT1,
2004 FunctionTemplateDecl *FT2,
2005 TemplatePartialOrderingContext TPOC,
2006 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
2007 FunctionDecl *FD1 = FT1->getTemplatedDecl();
2008 FunctionDecl *FD2 = FT2->getTemplatedDecl();
2009 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
2010 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
2011
2012 assert(Proto1 && Proto2 && "Function templates must have prototypes");
2013 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00002014 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor8a514912009-09-14 18:39:43 +00002015 Deduced.resize(TemplateParams->size());
2016
2017 // C++0x [temp.deduct.partial]p3:
2018 // The types used to determine the ordering depend on the context in which
2019 // the partial ordering is done:
John McCall5769d612010-02-08 23:07:23 +00002020 Sema::TemplateDeductionInfo Info(S.Context, Loc);
Douglas Gregor8a514912009-09-14 18:39:43 +00002021 switch (TPOC) {
2022 case TPOC_Call: {
2023 // - In the context of a function call, the function parameter types are
2024 // used.
2025 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
2026 for (unsigned I = 0; I != NumParams; ++I)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002027 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00002028 TemplateParams,
2029 Proto2->getArgType(I),
2030 Proto1->getArgType(I),
2031 Info,
2032 Deduced,
2033 QualifierComparisons))
2034 return false;
2035
2036 break;
2037 }
2038
2039 case TPOC_Conversion:
2040 // - In the context of a call to a conversion operator, the return types
2041 // of the conversion function templates are used.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002042 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00002043 TemplateParams,
2044 Proto2->getResultType(),
2045 Proto1->getResultType(),
2046 Info,
2047 Deduced,
2048 QualifierComparisons))
2049 return false;
2050 break;
2051
2052 case TPOC_Other:
2053 // - In other contexts (14.6.6.2) the function template’s function type
2054 // is used.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002055 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00002056 TemplateParams,
2057 FD2->getType(),
2058 FD1->getType(),
2059 Info,
2060 Deduced,
2061 QualifierComparisons))
2062 return false;
2063 break;
2064 }
2065
2066 // C++0x [temp.deduct.partial]p11:
2067 // In most cases, all template parameters must have values in order for
2068 // deduction to succeed, but for partial ordering purposes a template
2069 // parameter may remain without a value provided it is not used in the
2070 // types being used for partial ordering. [ Note: a template parameter used
2071 // in a non-deduced context is considered used. -end note]
2072 unsigned ArgIdx = 0, NumArgs = Deduced.size();
2073 for (; ArgIdx != NumArgs; ++ArgIdx)
2074 if (Deduced[ArgIdx].isNull())
2075 break;
2076
2077 if (ArgIdx == NumArgs) {
2078 // All template arguments were deduced. FT1 is at least as specialized
2079 // as FT2.
2080 return true;
2081 }
2082
Douglas Gregore73bb602009-09-14 21:25:05 +00002083 // Figure out which template parameters were used.
Douglas Gregor8a514912009-09-14 18:39:43 +00002084 llvm::SmallVector<bool, 4> UsedParameters;
2085 UsedParameters.resize(TemplateParams->size());
2086 switch (TPOC) {
2087 case TPOC_Call: {
2088 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
2089 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002090 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
2091 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00002092 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00002093 break;
2094 }
2095
2096 case TPOC_Conversion:
Douglas Gregored9c0f92009-10-29 00:04:11 +00002097 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
2098 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00002099 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00002100 break;
2101
2102 case TPOC_Other:
Douglas Gregored9c0f92009-10-29 00:04:11 +00002103 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
2104 TemplateParams->getDepth(),
2105 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00002106 break;
2107 }
2108
2109 for (; ArgIdx != NumArgs; ++ArgIdx)
2110 // If this argument had no value deduced but was used in one of the types
2111 // used for partial ordering, then deduction fails.
2112 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
2113 return false;
2114
2115 return true;
2116}
2117
2118
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002119/// \brief Returns the more specialized function template according
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002120/// to the rules of function template partial ordering (C++ [temp.func.order]).
2121///
2122/// \param FT1 the first function template
2123///
2124/// \param FT2 the second function template
2125///
Douglas Gregor8a514912009-09-14 18:39:43 +00002126/// \param TPOC the context in which we are performing partial ordering of
2127/// function templates.
Mike Stump1eb44332009-09-09 15:08:12 +00002128///
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002129/// \returns the more specialized function template. If neither
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002130/// template is more specialized, returns NULL.
2131FunctionTemplateDecl *
2132Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
2133 FunctionTemplateDecl *FT2,
John McCall5769d612010-02-08 23:07:23 +00002134 SourceLocation Loc,
Douglas Gregor8a514912009-09-14 18:39:43 +00002135 TemplatePartialOrderingContext TPOC) {
Douglas Gregor8a514912009-09-14 18:39:43 +00002136 llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
John McCall5769d612010-02-08 23:07:23 +00002137 bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, 0);
2138 bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
Douglas Gregor8a514912009-09-14 18:39:43 +00002139 &QualifierComparisons);
2140
2141 if (Better1 != Better2) // We have a clear winner
2142 return Better1? FT1 : FT2;
2143
2144 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002145 return 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00002146
2147
2148 // C++0x [temp.deduct.partial]p10:
2149 // If for each type being considered a given template is at least as
2150 // specialized for all types and more specialized for some set of types and
2151 // the other template is not more specialized for any types or is not at
2152 // least as specialized for any types, then the given template is more
2153 // specialized than the other template. Otherwise, neither template is more
2154 // specialized than the other.
2155 Better1 = false;
2156 Better2 = false;
2157 for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
2158 // C++0x [temp.deduct.partial]p9:
2159 // If, for a given type, deduction succeeds in both directions (i.e., the
2160 // types are identical after the transformations above) and if the type
2161 // from the argument template is more cv-qualified than the type from the
2162 // parameter template (as described above) that type is considered to be
2163 // more specialized than the other. If neither type is more cv-qualified
2164 // than the other then neither type is more specialized than the other.
2165 switch (QualifierComparisons[I]) {
2166 case NeitherMoreQualified:
2167 break;
2168
2169 case ParamMoreQualified:
2170 Better1 = true;
2171 if (Better2)
2172 return 0;
2173 break;
2174
2175 case ArgMoreQualified:
2176 Better2 = true;
2177 if (Better1)
2178 return 0;
2179 break;
2180 }
2181 }
2182
2183 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002184 if (Better1)
2185 return FT1;
Douglas Gregor8a514912009-09-14 18:39:43 +00002186 else if (Better2)
2187 return FT2;
2188 else
2189 return 0;
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002190}
Douglas Gregor83314aa2009-07-08 20:55:45 +00002191
Douglas Gregord5a423b2009-09-25 18:43:00 +00002192/// \brief Determine if the two templates are equivalent.
2193static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
2194 if (T1 == T2)
2195 return true;
2196
2197 if (!T1 || !T2)
2198 return false;
2199
2200 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
2201}
2202
2203/// \brief Retrieve the most specialized of the given function template
2204/// specializations.
2205///
John McCallc373d482010-01-27 01:50:18 +00002206/// \param SpecBegin the start iterator of the function template
2207/// specializations that we will be comparing.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002208///
John McCallc373d482010-01-27 01:50:18 +00002209/// \param SpecEnd the end iterator of the function template
2210/// specializations, paired with \p SpecBegin.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002211///
2212/// \param TPOC the partial ordering context to use to compare the function
2213/// template specializations.
2214///
2215/// \param Loc the location where the ambiguity or no-specializations
2216/// diagnostic should occur.
2217///
2218/// \param NoneDiag partial diagnostic used to diagnose cases where there are
2219/// no matching candidates.
2220///
2221/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
2222/// occurs.
2223///
2224/// \param CandidateDiag partial diagnostic used for each function template
2225/// specialization that is a candidate in the ambiguous ordering. One parameter
2226/// in this diagnostic should be unbound, which will correspond to the string
2227/// describing the template arguments for the function template specialization.
2228///
2229/// \param Index if non-NULL and the result of this function is non-nULL,
2230/// receives the index corresponding to the resulting function template
2231/// specialization.
2232///
2233/// \returns the most specialized function template specialization, if
John McCallc373d482010-01-27 01:50:18 +00002234/// found. Otherwise, returns SpecEnd.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002235///
2236/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
2237/// template argument deduction.
John McCallc373d482010-01-27 01:50:18 +00002238UnresolvedSetIterator
2239Sema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
2240 UnresolvedSetIterator SpecEnd,
2241 TemplatePartialOrderingContext TPOC,
2242 SourceLocation Loc,
2243 const PartialDiagnostic &NoneDiag,
2244 const PartialDiagnostic &AmbigDiag,
2245 const PartialDiagnostic &CandidateDiag) {
2246 if (SpecBegin == SpecEnd) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00002247 Diag(Loc, NoneDiag);
John McCallc373d482010-01-27 01:50:18 +00002248 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002249 }
2250
John McCallc373d482010-01-27 01:50:18 +00002251 if (SpecBegin + 1 == SpecEnd)
2252 return SpecBegin;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002253
2254 // Find the function template that is better than all of the templates it
2255 // has been compared to.
John McCallc373d482010-01-27 01:50:18 +00002256 UnresolvedSetIterator Best = SpecBegin;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002257 FunctionTemplateDecl *BestTemplate
John McCallc373d482010-01-27 01:50:18 +00002258 = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002259 assert(BestTemplate && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00002260 for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
2261 FunctionTemplateDecl *Challenger
2262 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002263 assert(Challenger && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00002264 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCall5769d612010-02-08 23:07:23 +00002265 Loc, TPOC),
Douglas Gregord5a423b2009-09-25 18:43:00 +00002266 Challenger)) {
2267 Best = I;
2268 BestTemplate = Challenger;
2269 }
2270 }
2271
2272 // Make sure that the "best" function template is more specialized than all
2273 // of the others.
2274 bool Ambiguous = false;
John McCallc373d482010-01-27 01:50:18 +00002275 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
2276 FunctionTemplateDecl *Challenger
2277 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002278 if (I != Best &&
2279 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCall5769d612010-02-08 23:07:23 +00002280 Loc, TPOC),
Douglas Gregord5a423b2009-09-25 18:43:00 +00002281 BestTemplate)) {
2282 Ambiguous = true;
2283 break;
2284 }
2285 }
2286
2287 if (!Ambiguous) {
2288 // We found an answer. Return it.
John McCallc373d482010-01-27 01:50:18 +00002289 return Best;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002290 }
2291
2292 // Diagnose the ambiguity.
2293 Diag(Loc, AmbigDiag);
2294
2295 // FIXME: Can we order the candidates in some sane way?
John McCallc373d482010-01-27 01:50:18 +00002296 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
2297 Diag((*I)->getLocation(), CandidateDiag)
Douglas Gregord5a423b2009-09-25 18:43:00 +00002298 << getTemplateArgumentBindingsText(
John McCallc373d482010-01-27 01:50:18 +00002299 cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
2300 *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
Douglas Gregord5a423b2009-09-25 18:43:00 +00002301
John McCallc373d482010-01-27 01:50:18 +00002302 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002303}
2304
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002305/// \brief Returns the more specialized class template partial specialization
2306/// according to the rules of partial ordering of class template partial
2307/// specializations (C++ [temp.class.order]).
2308///
2309/// \param PS1 the first class template partial specialization
2310///
2311/// \param PS2 the second class template partial specialization
2312///
2313/// \returns the more specialized class template partial specialization. If
2314/// neither partial specialization is more specialized, returns NULL.
2315ClassTemplatePartialSpecializationDecl *
2316Sema::getMoreSpecializedPartialSpecialization(
2317 ClassTemplatePartialSpecializationDecl *PS1,
John McCall5769d612010-02-08 23:07:23 +00002318 ClassTemplatePartialSpecializationDecl *PS2,
2319 SourceLocation Loc) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002320 // C++ [temp.class.order]p1:
2321 // For two class template partial specializations, the first is at least as
2322 // specialized as the second if, given the following rewrite to two
2323 // function templates, the first function template is at least as
2324 // specialized as the second according to the ordering rules for function
2325 // templates (14.6.6.2):
2326 // - the first function template has the same template parameters as the
2327 // first partial specialization and has a single function parameter
2328 // whose type is a class template specialization with the template
2329 // arguments of the first partial specialization, and
2330 // - the second function template has the same template parameters as the
2331 // second partial specialization and has a single function parameter
2332 // whose type is a class template specialization with the template
2333 // arguments of the second partial specialization.
2334 //
2335 // Rather than synthesize function templates, we merely perform the
2336 // equivalent partial ordering by performing deduction directly on the
2337 // template arguments of the class template partial specializations. This
2338 // computation is slightly simpler than the general problem of function
2339 // template partial ordering, because class template partial specializations
2340 // are more constrained. We know that every template parameter is deduc
Douglas Gregor02024a92010-03-28 02:42:43 +00002341 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
John McCall5769d612010-02-08 23:07:23 +00002342 Sema::TemplateDeductionInfo Info(Context, Loc);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002343
2344 // Determine whether PS1 is at least as specialized as PS2
2345 Deduced.resize(PS2->getTemplateParameters()->size());
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002346 bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002347 PS2->getTemplateParameters(),
2348 Context.getTypeDeclType(PS2),
2349 Context.getTypeDeclType(PS1),
2350 Info,
2351 Deduced,
2352 0);
2353
2354 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregordb0d4b72009-11-11 23:06:43 +00002355 Deduced.clear();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002356 Deduced.resize(PS1->getTemplateParameters()->size());
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002357 bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002358 PS1->getTemplateParameters(),
2359 Context.getTypeDeclType(PS1),
2360 Context.getTypeDeclType(PS2),
2361 Info,
2362 Deduced,
2363 0);
2364
2365 if (Better1 == Better2)
2366 return 0;
2367
2368 return Better1? PS1 : PS2;
2369}
2370
Mike Stump1eb44332009-09-09 15:08:12 +00002371static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002372MarkUsedTemplateParameters(Sema &SemaRef,
2373 const TemplateArgument &TemplateArg,
2374 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002375 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002376 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002377
Douglas Gregore73bb602009-09-14 21:25:05 +00002378/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002379/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00002380static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002381MarkUsedTemplateParameters(Sema &SemaRef,
2382 const Expr *E,
2383 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002384 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002385 llvm::SmallVectorImpl<bool> &Used) {
2386 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2387 // find other occurrences of template parameters.
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002388 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregorc781f9c2010-01-14 18:13:22 +00002389 if (!DRE)
Douglas Gregor031a5882009-06-13 00:26:55 +00002390 return;
2391
Mike Stump1eb44332009-09-09 15:08:12 +00002392 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00002393 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2394 if (!NTTP)
2395 return;
2396
Douglas Gregored9c0f92009-10-29 00:04:11 +00002397 if (NTTP->getDepth() == Depth)
2398 Used[NTTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002399}
2400
Douglas Gregore73bb602009-09-14 21:25:05 +00002401/// \brief Mark the template parameters that are used by the given
2402/// nested name specifier.
2403static void
2404MarkUsedTemplateParameters(Sema &SemaRef,
2405 NestedNameSpecifier *NNS,
2406 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002407 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002408 llvm::SmallVectorImpl<bool> &Used) {
2409 if (!NNS)
2410 return;
2411
Douglas Gregored9c0f92009-10-29 00:04:11 +00002412 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2413 Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002414 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002415 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002416}
2417
2418/// \brief Mark the template parameters that are used by the given
2419/// template name.
2420static void
2421MarkUsedTemplateParameters(Sema &SemaRef,
2422 TemplateName Name,
2423 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002424 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002425 llvm::SmallVectorImpl<bool> &Used) {
2426 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2427 if (TemplateTemplateParmDecl *TTP
Douglas Gregored9c0f92009-10-29 00:04:11 +00002428 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2429 if (TTP->getDepth() == Depth)
2430 Used[TTP->getIndex()] = true;
2431 }
Douglas Gregore73bb602009-09-14 21:25:05 +00002432 return;
2433 }
2434
Douglas Gregor788cd062009-11-11 01:00:40 +00002435 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2436 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2437 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002438 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
Douglas Gregored9c0f92009-10-29 00:04:11 +00002439 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2440 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002441}
2442
2443/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002444/// type.
Mike Stump1eb44332009-09-09 15:08:12 +00002445static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002446MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2447 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002448 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002449 llvm::SmallVectorImpl<bool> &Used) {
2450 if (T.isNull())
2451 return;
2452
Douglas Gregor031a5882009-06-13 00:26:55 +00002453 // Non-dependent types have nothing deducible
2454 if (!T->isDependentType())
2455 return;
2456
2457 T = SemaRef.Context.getCanonicalType(T);
2458 switch (T->getTypeClass()) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002459 case Type::Pointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002460 MarkUsedTemplateParameters(SemaRef,
2461 cast<PointerType>(T)->getPointeeType(),
2462 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002463 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002464 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002465 break;
2466
2467 case Type::BlockPointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002468 MarkUsedTemplateParameters(SemaRef,
2469 cast<BlockPointerType>(T)->getPointeeType(),
2470 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002471 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002472 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002473 break;
2474
2475 case Type::LValueReference:
2476 case Type::RValueReference:
Douglas Gregore73bb602009-09-14 21:25:05 +00002477 MarkUsedTemplateParameters(SemaRef,
2478 cast<ReferenceType>(T)->getPointeeType(),
2479 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002480 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002481 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002482 break;
2483
2484 case Type::MemberPointer: {
2485 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore73bb602009-09-14 21:25:05 +00002486 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002487 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002488 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002489 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002490 break;
2491 }
2492
2493 case Type::DependentSizedArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002494 MarkUsedTemplateParameters(SemaRef,
2495 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002496 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002497 // Fall through to check the element type
2498
2499 case Type::ConstantArray:
2500 case Type::IncompleteArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002501 MarkUsedTemplateParameters(SemaRef,
2502 cast<ArrayType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002503 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002504 break;
2505
2506 case Type::Vector:
2507 case Type::ExtVector:
Douglas Gregore73bb602009-09-14 21:25:05 +00002508 MarkUsedTemplateParameters(SemaRef,
2509 cast<VectorType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002510 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002511 break;
2512
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002513 case Type::DependentSizedExtVector: {
2514 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002515 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002516 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002517 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002518 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002519 Depth, Used);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002520 break;
2521 }
2522
Douglas Gregor031a5882009-06-13 00:26:55 +00002523 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002524 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002525 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002526 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002527 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore73bb602009-09-14 21:25:05 +00002528 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002529 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002530 break;
2531 }
2532
Douglas Gregored9c0f92009-10-29 00:04:11 +00002533 case Type::TemplateTypeParm: {
2534 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2535 if (TTP->getDepth() == Depth)
2536 Used[TTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002537 break;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002538 }
Douglas Gregor031a5882009-06-13 00:26:55 +00002539
2540 case Type::TemplateSpecialization: {
Mike Stump1eb44332009-09-09 15:08:12 +00002541 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002542 = cast<TemplateSpecializationType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002543 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002544 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002545 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002546 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2547 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002548 break;
2549 }
2550
Douglas Gregore73bb602009-09-14 21:25:05 +00002551 case Type::Complex:
2552 if (!OnlyDeduced)
2553 MarkUsedTemplateParameters(SemaRef,
2554 cast<ComplexType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002555 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002556 break;
2557
Douglas Gregor4714c122010-03-31 17:34:00 +00002558 case Type::DependentName:
Douglas Gregore73bb602009-09-14 21:25:05 +00002559 if (!OnlyDeduced)
2560 MarkUsedTemplateParameters(SemaRef,
Douglas Gregor4714c122010-03-31 17:34:00 +00002561 cast<DependentNameType>(T)->getQualifier(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002562 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002563 break;
2564
John McCallad5e7382010-03-01 23:49:17 +00002565 case Type::TypeOf:
2566 if (!OnlyDeduced)
2567 MarkUsedTemplateParameters(SemaRef,
2568 cast<TypeOfType>(T)->getUnderlyingType(),
2569 OnlyDeduced, Depth, Used);
2570 break;
2571
2572 case Type::TypeOfExpr:
2573 if (!OnlyDeduced)
2574 MarkUsedTemplateParameters(SemaRef,
2575 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
2576 OnlyDeduced, Depth, Used);
2577 break;
2578
2579 case Type::Decltype:
2580 if (!OnlyDeduced)
2581 MarkUsedTemplateParameters(SemaRef,
2582 cast<DecltypeType>(T)->getUnderlyingExpr(),
2583 OnlyDeduced, Depth, Used);
2584 break;
2585
Douglas Gregore73bb602009-09-14 21:25:05 +00002586 // None of these types have any template parameters in them.
Douglas Gregor031a5882009-06-13 00:26:55 +00002587 case Type::Builtin:
Douglas Gregor031a5882009-06-13 00:26:55 +00002588 case Type::VariableArray:
2589 case Type::FunctionNoProto:
2590 case Type::Record:
2591 case Type::Enum:
Douglas Gregor031a5882009-06-13 00:26:55 +00002592 case Type::ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002593 case Type::ObjCObjectPointer:
John McCalled976492009-12-04 22:46:56 +00002594 case Type::UnresolvedUsing:
Douglas Gregor031a5882009-06-13 00:26:55 +00002595#define TYPE(Class, Base)
2596#define ABSTRACT_TYPE(Class, Base)
2597#define DEPENDENT_TYPE(Class, Base)
2598#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2599#include "clang/AST/TypeNodes.def"
2600 break;
2601 }
2602}
2603
Douglas Gregore73bb602009-09-14 21:25:05 +00002604/// \brief Mark the template parameters that are used by this
Douglas Gregor031a5882009-06-13 00:26:55 +00002605/// template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00002606static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002607MarkUsedTemplateParameters(Sema &SemaRef,
2608 const TemplateArgument &TemplateArg,
2609 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002610 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002611 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002612 switch (TemplateArg.getKind()) {
2613 case TemplateArgument::Null:
2614 case TemplateArgument::Integral:
Douglas Gregor788cd062009-11-11 01:00:40 +00002615 case TemplateArgument::Declaration:
Douglas Gregor031a5882009-06-13 00:26:55 +00002616 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002617
Douglas Gregor031a5882009-06-13 00:26:55 +00002618 case TemplateArgument::Type:
Douglas Gregore73bb602009-09-14 21:25:05 +00002619 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002620 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002621 break;
2622
Douglas Gregor788cd062009-11-11 01:00:40 +00002623 case TemplateArgument::Template:
2624 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2625 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002626 break;
2627
2628 case TemplateArgument::Expression:
Douglas Gregore73bb602009-09-14 21:25:05 +00002629 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002630 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002631 break;
Douglas Gregore73bb602009-09-14 21:25:05 +00002632
Anders Carlssond01b1da2009-06-15 17:04:53 +00002633 case TemplateArgument::Pack:
Douglas Gregore73bb602009-09-14 21:25:05 +00002634 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2635 PEnd = TemplateArg.pack_end();
2636 P != PEnd; ++P)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002637 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssond01b1da2009-06-15 17:04:53 +00002638 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00002639 }
2640}
2641
2642/// \brief Mark the template parameters can be deduced by the given
2643/// template argument list.
2644///
2645/// \param TemplateArgs the template argument list from which template
2646/// parameters will be deduced.
2647///
2648/// \param Deduced a bit vector whose elements will be set to \c true
2649/// to indicate when the corresponding template parameter will be
2650/// deduced.
Mike Stump1eb44332009-09-09 15:08:12 +00002651void
Douglas Gregore73bb602009-09-14 21:25:05 +00002652Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002653 bool OnlyDeduced, unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002654 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002655 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002656 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2657 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002658}
Douglas Gregor63f07c52009-09-18 23:21:38 +00002659
2660/// \brief Marks all of the template parameters that will be deduced by a
2661/// call to the given function template.
Douglas Gregor02024a92010-03-28 02:42:43 +00002662void
2663Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
2664 llvm::SmallVectorImpl<bool> &Deduced) {
Douglas Gregor63f07c52009-09-18 23:21:38 +00002665 TemplateParameterList *TemplateParams
2666 = FunctionTemplate->getTemplateParameters();
2667 Deduced.clear();
2668 Deduced.resize(TemplateParams->size());
2669
2670 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2671 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
2672 ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002673 true, TemplateParams->getDepth(), Deduced);
Douglas Gregor63f07c52009-09-18 23:21:38 +00002674}