blob: b85f2bd422d621ad80f78a5425e33cf4e4442888 [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.
John McCall31f17ec2010-04-27 00:57:59 +0000641 Param = cast<InjectedClassNameType>(Param)
642 ->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +0000643 assert(isa<TemplateSpecializationType>(Param) &&
644 "injected class name is not a template specialization type");
645 // fall through
646 }
647
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000648 // template-name<T> (where template-name refers to a class template)
Douglas Gregord708c722009-06-09 16:35:58 +0000649 // template-name<i>
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000650 // TT<T>
651 // TT<i>
652 // TT<>
Douglas Gregord708c722009-06-09 16:35:58 +0000653 case Type::TemplateSpecialization: {
654 const TemplateSpecializationType *SpecParam
655 = cast<TemplateSpecializationType>(Param);
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000657 // Try to deduce template arguments from the template-id.
658 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000659 = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000660 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Douglas Gregor4a5c15f2009-09-30 22:13:51 +0000662 if (Result && (TDF & TDF_DerivedClass)) {
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000663 // C++ [temp.deduct.call]p3b3:
664 // If P is a class, and P has the form template-id, then A can be a
665 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump1eb44332009-09-09 15:08:12 +0000666 // class of the form template-id, A can be a pointer to a derived
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000667 // class pointed to by the deduced A.
668 //
669 // More importantly:
Mike Stump1eb44332009-09-09 15:08:12 +0000670 // These alternatives are considered only if type deduction would
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000671 // otherwise fail.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000672 if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
673 // We cannot inspect base classes as part of deduction when the type
674 // is incomplete, so either instantiate any templates necessary to
675 // complete the type, or skip over it if it cannot be completed.
John McCall5769d612010-02-08 23:07:23 +0000676 if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000677 return Result;
678
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000679 // Use data recursion to crawl through the list of base classes.
Mike Stump1eb44332009-09-09 15:08:12 +0000680 // Visited contains the set of nodes we have already visited, while
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000681 // ToVisit is our stack of records that we still need to visit.
682 llvm::SmallPtrSet<const RecordType *, 8> Visited;
683 llvm::SmallVector<const RecordType *, 8> ToVisit;
684 ToVisit.push_back(RecordT);
685 bool Successful = false;
686 while (!ToVisit.empty()) {
687 // Retrieve the next class in the inheritance hierarchy.
688 const RecordType *NextT = ToVisit.back();
689 ToVisit.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000691 // If we have already seen this type, skip it.
692 if (!Visited.insert(NextT))
693 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000695 // If this is a base class, try to perform template argument
696 // deduction from it.
697 if (NextT != RecordT) {
698 Sema::TemplateDeductionResult BaseResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000699 = DeduceTemplateArguments(S, TemplateParams, SpecParam,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000700 QualType(NextT, 0), Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000702 // If template argument deduction for this base was successful,
703 // note that we had some success.
704 if (BaseResult == Sema::TDK_Success)
705 Successful = true;
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000706 }
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000708 // Visit base classes
709 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
710 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
711 BaseEnd = Next->bases_end();
Sebastian Redl9994a342009-10-25 17:03:50 +0000712 Base != BaseEnd; ++Base) {
Mike Stump1eb44332009-09-09 15:08:12 +0000713 assert(Base->getType()->isRecordType() &&
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000714 "Base class that isn't a record?");
Ted Kremenek6217b802009-07-29 21:53:49 +0000715 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000716 }
717 }
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000719 if (Successful)
720 return Sema::TDK_Success;
721 }
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000723 }
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000725 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000726 }
727
Douglas Gregor637a4092009-06-10 23:47:09 +0000728 // T type::*
729 // T T::*
730 // T (type::*)()
731 // type (T::*)()
732 // type (type::*)(T)
733 // type (T::*)(T)
734 // T (type::*)(T)
735 // T (T::*)()
736 // T (T::*)(T)
737 case Type::MemberPointer: {
738 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
739 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
740 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000741 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +0000742
Douglas Gregorf67875d2009-06-12 18:26:56 +0000743 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000744 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000745 MemPtrParam->getPointeeType(),
746 MemPtrArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000747 Info, Deduced,
748 TDF & TDF_IgnoreQualifiers))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000749 return Result;
750
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000751 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000752 QualType(MemPtrParam->getClass(), 0),
753 QualType(MemPtrArg->getClass(), 0),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000754 Info, Deduced, 0);
Douglas Gregor637a4092009-06-10 23:47:09 +0000755 }
756
Anders Carlsson9a917e42009-06-12 22:56:54 +0000757 // (clang extension)
758 //
Mike Stump1eb44332009-09-09 15:08:12 +0000759 // type(^)(T)
760 // T(^)()
761 // T(^)(T)
Anders Carlsson859ba502009-06-12 16:23:10 +0000762 case Type::BlockPointer: {
763 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
764 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Anders Carlsson859ba502009-06-12 16:23:10 +0000766 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000767 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000769 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +0000770 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000771 BlockPtrArg->getPointeeType(), Info,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000772 Deduced, 0);
Anders Carlsson859ba502009-06-12 16:23:10 +0000773 }
774
Douglas Gregor637a4092009-06-10 23:47:09 +0000775 case Type::TypeOfExpr:
776 case Type::TypeOf:
Douglas Gregor4714c122010-03-31 17:34:00 +0000777 case Type::DependentName:
Douglas Gregor637a4092009-06-10 23:47:09 +0000778 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +0000779 return Sema::TDK_Success;
Douglas Gregor637a4092009-06-10 23:47:09 +0000780
Douglas Gregord560d502009-06-04 00:21:18 +0000781 default:
782 break;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000783 }
784
785 // FIXME: Many more cases to go (to go).
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000786 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000787}
788
Douglas Gregorf67875d2009-06-12 18:26:56 +0000789static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000790DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000791 TemplateParameterList *TemplateParams,
792 const TemplateArgument &Param,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000793 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000794 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000795 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000796 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000797 case TemplateArgument::Null:
798 assert(false && "Null template argument in parameter list");
799 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000800
801 case TemplateArgument::Type:
Douglas Gregor788cd062009-11-11 01:00:40 +0000802 if (Arg.getKind() == TemplateArgument::Type)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000803 return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
Douglas Gregor788cd062009-11-11 01:00:40 +0000804 Arg.getAsType(), Info, Deduced, 0);
805 Info.FirstArg = Param;
806 Info.SecondArg = Arg;
807 return Sema::TDK_NonDeducedMismatch;
808
809 case TemplateArgument::Template:
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000810 if (Arg.getKind() == TemplateArgument::Template)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000811 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor788cd062009-11-11 01:00:40 +0000812 Param.getAsTemplate(),
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000813 Arg.getAsTemplate(), Info, Deduced);
Douglas Gregor788cd062009-11-11 01:00:40 +0000814 Info.FirstArg = Param;
815 Info.SecondArg = Arg;
816 return Sema::TDK_NonDeducedMismatch;
817
Douglas Gregor199d9912009-06-05 00:53:49 +0000818 case TemplateArgument::Declaration:
Douglas Gregor788cd062009-11-11 01:00:40 +0000819 if (Arg.getKind() == TemplateArgument::Declaration &&
820 Param.getAsDecl()->getCanonicalDecl() ==
821 Arg.getAsDecl()->getCanonicalDecl())
822 return Sema::TDK_Success;
823
Douglas Gregorf67875d2009-06-12 18:26:56 +0000824 Info.FirstArg = Param;
825 Info.SecondArg = Arg;
826 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Douglas Gregor199d9912009-06-05 00:53:49 +0000828 case TemplateArgument::Integral:
829 if (Arg.getKind() == TemplateArgument::Integral) {
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000830 if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral()))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000831 return Sema::TDK_Success;
832
833 Info.FirstArg = Param;
834 Info.SecondArg = Arg;
835 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000836 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000837
838 if (Arg.getKind() == TemplateArgument::Expression) {
839 Info.FirstArg = Param;
840 Info.SecondArg = Arg;
841 return Sema::TDK_NonDeducedMismatch;
842 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000843
844 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000845 Info.FirstArg = Param;
846 Info.SecondArg = Arg;
847 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Douglas Gregor199d9912009-06-05 00:53:49 +0000849 case TemplateArgument::Expression: {
Mike Stump1eb44332009-09-09 15:08:12 +0000850 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000851 = getDeducedParameterFromExpr(Param.getAsExpr())) {
852 if (Arg.getKind() == TemplateArgument::Integral)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000853 return DeduceNonTypeTemplateArgument(S, NTTP,
Mike Stump1eb44332009-09-09 15:08:12 +0000854 *Arg.getAsIntegral(),
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000855 Arg.getIntegralType(),
Douglas Gregor02024a92010-03-28 02:42:43 +0000856 /*ArrayBound=*/false,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000857 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000858 if (Arg.getKind() == TemplateArgument::Expression)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000859 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000860 Info, Deduced);
Douglas Gregor15755cb2009-11-13 23:45:44 +0000861 if (Arg.getKind() == TemplateArgument::Declaration)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000862 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
Douglas Gregor15755cb2009-11-13 23:45:44 +0000863 Info, Deduced);
864
Douglas Gregor199d9912009-06-05 00:53:49 +0000865 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000866 Info.FirstArg = Param;
867 Info.SecondArg = Arg;
868 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000869 }
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Douglas Gregor199d9912009-06-05 00:53:49 +0000871 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000872 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000873 }
Anders Carlssond01b1da2009-06-15 17:04:53 +0000874 case TemplateArgument::Pack:
875 assert(0 && "FIXME: Implement!");
876 break;
Douglas Gregor199d9912009-06-05 00:53:49 +0000877 }
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Douglas Gregorf67875d2009-06-12 18:26:56 +0000879 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000880}
881
Mike Stump1eb44332009-09-09 15:08:12 +0000882static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000883DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000884 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000885 const TemplateArgumentList &ParamList,
886 const TemplateArgumentList &ArgList,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000887 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000888 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000889 assert(ParamList.size() == ArgList.size());
890 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000891 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000892 = DeduceTemplateArguments(S, TemplateParams,
Mike Stump1eb44332009-09-09 15:08:12 +0000893 ParamList[I], ArgList[I],
Douglas Gregorf67875d2009-06-12 18:26:56 +0000894 Info, Deduced))
895 return Result;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000896 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000897 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000898}
899
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000900/// \brief Determine whether two template arguments are the same.
Mike Stump1eb44332009-09-09 15:08:12 +0000901static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000902 const TemplateArgument &X,
903 const TemplateArgument &Y) {
904 if (X.getKind() != Y.getKind())
905 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000907 switch (X.getKind()) {
908 case TemplateArgument::Null:
909 assert(false && "Comparing NULL template argument");
910 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000912 case TemplateArgument::Type:
913 return Context.getCanonicalType(X.getAsType()) ==
914 Context.getCanonicalType(Y.getAsType());
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000916 case TemplateArgument::Declaration:
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000917 return X.getAsDecl()->getCanonicalDecl() ==
918 Y.getAsDecl()->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Douglas Gregor788cd062009-11-11 01:00:40 +0000920 case TemplateArgument::Template:
921 return Context.getCanonicalTemplateName(X.getAsTemplate())
922 .getAsVoidPointer() ==
923 Context.getCanonicalTemplateName(Y.getAsTemplate())
924 .getAsVoidPointer();
925
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000926 case TemplateArgument::Integral:
927 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Douglas Gregor788cd062009-11-11 01:00:40 +0000929 case TemplateArgument::Expression: {
930 llvm::FoldingSetNodeID XID, YID;
931 X.getAsExpr()->Profile(XID, Context, true);
932 Y.getAsExpr()->Profile(YID, Context, true);
933 return XID == YID;
934 }
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000936 case TemplateArgument::Pack:
937 if (X.pack_size() != Y.pack_size())
938 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000939
940 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
941 XPEnd = X.pack_end(),
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000942 YP = Y.pack_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000943 XP != XPEnd; ++XP, ++YP)
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000944 if (!isSameTemplateArg(Context, *XP, *YP))
945 return false;
946
947 return true;
948 }
949
950 return false;
951}
952
953/// \brief Helper function to build a TemplateParameter when we don't
954/// know its type statically.
955static TemplateParameter makeTemplateParameter(Decl *D) {
956 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
957 return TemplateParameter(TTP);
958 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
959 return TemplateParameter(NTTP);
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000961 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
962}
963
Douglas Gregor31dce8f2010-04-29 06:21:43 +0000964/// Complete template argument deduction for a class template partial
965/// specialization.
966static Sema::TemplateDeductionResult
967FinishTemplateArgumentDeduction(Sema &S,
968 ClassTemplatePartialSpecializationDecl *Partial,
969 const TemplateArgumentList &TemplateArgs,
970 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
971 Sema::TemplateDeductionInfo &Info) {
972 // Trap errors.
973 Sema::SFINAETrap Trap(S);
974
975 Sema::ContextRAII SavedContext(S, Partial);
976
977 // C++ [temp.deduct.type]p2:
978 // [...] or if any template argument remains neither deduced nor
979 // explicitly specified, template argument deduction fails.
980 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
981 Deduced.size());
982 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
983 if (Deduced[I].isNull()) {
984 Decl *Param
985 = const_cast<NamedDecl *>(
986 Partial->getTemplateParameters()->getParam(I));
987 Info.Param = makeTemplateParameter(Param);
988 return Sema::TDK_Incomplete;
989 }
990
991 Builder.Append(Deduced[I]);
992 }
993
994 // Form the template argument list from the deduced template arguments.
995 TemplateArgumentList *DeducedArgumentList
996 = new (S.Context) TemplateArgumentList(S.Context, Builder,
997 /*TakeArgs=*/true);
998 Info.reset(DeducedArgumentList);
999
1000 // Substitute the deduced template arguments into the template
1001 // arguments of the class template partial specialization, and
1002 // verify that the instantiated template arguments are both valid
1003 // and are equivalent to the template arguments originally provided
1004 // to the class template.
1005 // FIXME: Do we have to correct the types of deduced non-type template
1006 // arguments (in particular, integral non-type template arguments?).
1007 Sema::LocalInstantiationScope InstScope(S);
1008 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
1009 const TemplateArgumentLoc *PartialTemplateArgs
1010 = Partial->getTemplateArgsAsWritten();
1011 unsigned N = Partial->getNumTemplateArgsAsWritten();
1012
1013 // Note that we don't provide the langle and rangle locations.
1014 TemplateArgumentListInfo InstArgs;
1015
1016 for (unsigned I = 0; I != N; ++I) {
1017 Decl *Param = const_cast<NamedDecl *>(
1018 ClassTemplate->getTemplateParameters()->getParam(I));
1019 TemplateArgumentLoc InstArg;
1020 if (S.Subst(PartialTemplateArgs[I], InstArg,
1021 MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
1022 Info.Param = makeTemplateParameter(Param);
1023 Info.FirstArg = PartialTemplateArgs[I].getArgument();
1024 return Sema::TDK_SubstitutionFailure;
1025 }
1026 InstArgs.addArgument(InstArg);
1027 }
1028
1029 TemplateArgumentListBuilder ConvertedInstArgs(
1030 ClassTemplate->getTemplateParameters(), N);
1031
1032 if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
Douglas Gregorec20f462010-05-08 20:07:26 +00001033 InstArgs, false, ConvertedInstArgs))
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001034 return Sema::TDK_SubstitutionFailure;
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001035
1036 for (unsigned I = 0, E = ConvertedInstArgs.flatSize(); I != E; ++I) {
1037 TemplateArgument InstArg = ConvertedInstArgs.getFlatArguments()[I];
1038
1039 Decl *Param = const_cast<NamedDecl *>(
1040 ClassTemplate->getTemplateParameters()->getParam(I));
1041
1042 if (InstArg.getKind() == TemplateArgument::Expression) {
1043 // When the argument is an expression, check the expression result
1044 // against the actual template parameter to get down to the canonical
1045 // template argument.
1046 Expr *InstExpr = InstArg.getAsExpr();
1047 if (NonTypeTemplateParmDecl *NTTP
1048 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1049 if (S.CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
1050 Info.Param = makeTemplateParameter(Param);
1051 Info.FirstArg = Partial->getTemplateArgs()[I];
1052 return Sema::TDK_SubstitutionFailure;
1053 }
1054 }
1055 }
1056
1057 if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
1058 Info.Param = makeTemplateParameter(Param);
1059 Info.FirstArg = TemplateArgs[I];
1060 Info.SecondArg = InstArg;
1061 return Sema::TDK_NonDeducedMismatch;
1062 }
1063 }
1064
1065 if (Trap.hasErrorOccurred())
1066 return Sema::TDK_SubstitutionFailure;
1067
1068 return Sema::TDK_Success;
1069}
1070
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001071/// \brief Perform template argument deduction to determine whether
1072/// the given template arguments match the given class template
1073/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +00001074Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001075Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001076 const TemplateArgumentList &TemplateArgs,
1077 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001078 // C++ [temp.class.spec.match]p2:
1079 // A partial specialization matches a given actual template
1080 // argument list if the template arguments of the partial
1081 // specialization can be deduced from the actual template argument
1082 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +00001083 SFINAETrap Trap(*this);
Douglas Gregor02024a92010-03-28 02:42:43 +00001084 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001085 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +00001086 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001087 = ::DeduceTemplateArguments(*this,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001088 Partial->getTemplateParameters(),
Mike Stump1eb44332009-09-09 15:08:12 +00001089 Partial->getTemplateArgs(),
Douglas Gregorf67875d2009-06-12 18:26:56 +00001090 TemplateArgs, Info, Deduced))
1091 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +00001092
Douglas Gregor637a4092009-06-10 23:47:09 +00001093 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
1094 Deduced.data(), Deduced.size());
1095 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001096 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +00001097
Douglas Gregorbb260412009-06-14 08:02:22 +00001098 if (Trap.hasErrorOccurred())
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001099 return Sema::TDK_SubstitutionFailure;
1100
1101 return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
1102 Deduced, Info);
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001103}
Douglas Gregor031a5882009-06-13 00:26:55 +00001104
Douglas Gregor41128772009-06-26 23:27:24 +00001105/// \brief Determine whether the given type T is a simple-template-id type.
1106static bool isSimpleTemplateIdType(QualType T) {
Mike Stump1eb44332009-09-09 15:08:12 +00001107 if (const TemplateSpecializationType *Spec
John McCall183700f2009-09-21 23:43:11 +00001108 = T->getAs<TemplateSpecializationType>())
Douglas Gregor41128772009-06-26 23:27:24 +00001109 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Douglas Gregor41128772009-06-26 23:27:24 +00001111 return false;
1112}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001113
1114/// \brief Substitute the explicitly-provided template arguments into the
1115/// given function template according to C++ [temp.arg.explicit].
1116///
1117/// \param FunctionTemplate the function template into which the explicit
1118/// template arguments will be substituted.
1119///
Mike Stump1eb44332009-09-09 15:08:12 +00001120/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001121/// arguments.
1122///
Mike Stump1eb44332009-09-09 15:08:12 +00001123/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor83314aa2009-07-08 20:55:45 +00001124/// with the converted and checked explicit template arguments.
1125///
Mike Stump1eb44332009-09-09 15:08:12 +00001126/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor83314aa2009-07-08 20:55:45 +00001127/// parameters.
1128///
1129/// \param FunctionType if non-NULL, the result type of the function template
1130/// will also be instantiated and the pointed-to value will be updated with
1131/// the instantiated function type.
1132///
1133/// \param Info if substitution fails for any reason, this object will be
1134/// populated with more information about the failure.
1135///
1136/// \returns TDK_Success if substitution was successful, or some failure
1137/// condition.
1138Sema::TemplateDeductionResult
1139Sema::SubstituteExplicitTemplateArguments(
1140 FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001141 const TemplateArgumentListInfo &ExplicitTemplateArgs,
Douglas Gregor02024a92010-03-28 02:42:43 +00001142 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001143 llvm::SmallVectorImpl<QualType> &ParamTypes,
1144 QualType *FunctionType,
1145 TemplateDeductionInfo &Info) {
1146 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1147 TemplateParameterList *TemplateParams
1148 = FunctionTemplate->getTemplateParameters();
1149
John McCalld5532b62009-11-23 01:53:49 +00001150 if (ExplicitTemplateArgs.size() == 0) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001151 // No arguments to substitute; just copy over the parameter types and
1152 // fill in the function type.
1153 for (FunctionDecl::param_iterator P = Function->param_begin(),
1154 PEnd = Function->param_end();
1155 P != PEnd;
1156 ++P)
1157 ParamTypes.push_back((*P)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Douglas Gregor83314aa2009-07-08 20:55:45 +00001159 if (FunctionType)
1160 *FunctionType = Function->getType();
1161 return TDK_Success;
1162 }
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Douglas Gregor83314aa2009-07-08 20:55:45 +00001164 // Substitution of the explicit template arguments into a function template
1165 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001166 SFINAETrap Trap(*this);
1167
Douglas Gregor83314aa2009-07-08 20:55:45 +00001168 // C++ [temp.arg.explicit]p3:
Mike Stump1eb44332009-09-09 15:08:12 +00001169 // Template arguments that are present shall be specified in the
1170 // declaration order of their corresponding template-parameters. The
Douglas Gregor83314aa2009-07-08 20:55:45 +00001171 // template argument list shall not specify more template-arguments than
Mike Stump1eb44332009-09-09 15:08:12 +00001172 // there are corresponding template-parameters.
1173 TemplateArgumentListBuilder Builder(TemplateParams,
John McCalld5532b62009-11-23 01:53:49 +00001174 ExplicitTemplateArgs.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001175
1176 // Enter a new template instantiation context where we check the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001177 // explicitly-specified template arguments against this function template,
1178 // and then substitute them into the function parameter types.
Mike Stump1eb44332009-09-09 15:08:12 +00001179 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001180 FunctionTemplate, Deduced.data(), Deduced.size(),
1181 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution);
1182 if (Inst)
1183 return TDK_InstantiationDepth;
Mike Stump1eb44332009-09-09 15:08:12 +00001184
John McCall96db3102010-04-29 01:18:58 +00001185 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
John McCallf5813822010-04-29 00:35:03 +00001186
Douglas Gregor83314aa2009-07-08 20:55:45 +00001187 if (CheckTemplateArgumentList(FunctionTemplate,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001188 SourceLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001189 ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001190 true,
Douglas Gregorf1a84452010-05-08 19:15:54 +00001191 Builder) || Trap.hasErrorOccurred()) {
Douglas Gregorfe52c912010-05-09 01:26:06 +00001192 unsigned Index = Builder.structuredSize();
1193 if (Index >= TemplateParams->size())
1194 Index = TemplateParams->size() - 1;
1195 Info.Param = makeTemplateParameter(TemplateParams->getParam(Index));
Douglas Gregor83314aa2009-07-08 20:55:45 +00001196 return TDK_InvalidExplicitArguments;
Douglas Gregorf1a84452010-05-08 19:15:54 +00001197 }
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Douglas Gregor83314aa2009-07-08 20:55:45 +00001199 // Form the template argument list from the explicitly-specified
1200 // template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001201 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor83314aa2009-07-08 20:55:45 +00001202 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1203 Info.reset(ExplicitArgumentList);
Mike Stump1eb44332009-09-09 15:08:12 +00001204
Douglas Gregor83314aa2009-07-08 20:55:45 +00001205 // Instantiate the types of each of the function parameters given the
1206 // explicitly-specified template arguments.
1207 for (FunctionDecl::param_iterator P = Function->param_begin(),
1208 PEnd = Function->param_end();
1209 P != PEnd;
1210 ++P) {
Mike Stump1eb44332009-09-09 15:08:12 +00001211 QualType ParamType
1212 = SubstType((*P)->getType(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001213 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1214 (*P)->getLocation(), (*P)->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001215 if (ParamType.isNull() || Trap.hasErrorOccurred())
1216 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Douglas Gregor83314aa2009-07-08 20:55:45 +00001218 ParamTypes.push_back(ParamType);
1219 }
1220
1221 // If the caller wants a full function type back, instantiate the return
1222 // type and form that function type.
1223 if (FunctionType) {
1224 // FIXME: exception-specifications?
Mike Stump1eb44332009-09-09 15:08:12 +00001225 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001226 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001227 assert(Proto && "Function template does not have a prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001228
1229 QualType ResultType
Douglas Gregor357bbd02009-08-28 20:50:45 +00001230 = SubstType(Proto->getResultType(),
1231 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1232 Function->getTypeSpecStartLoc(),
1233 Function->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001234 if (ResultType.isNull() || Trap.hasErrorOccurred())
1235 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001236
1237 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001238 ParamTypes.data(), ParamTypes.size(),
1239 Proto->isVariadic(),
1240 Proto->getTypeQuals(),
1241 Function->getLocation(),
1242 Function->getDeclName());
1243 if (FunctionType->isNull() || Trap.hasErrorOccurred())
1244 return TDK_SubstitutionFailure;
1245 }
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Douglas Gregor83314aa2009-07-08 20:55:45 +00001247 // C++ [temp.arg.explicit]p2:
Mike Stump1eb44332009-09-09 15:08:12 +00001248 // Trailing template arguments that can be deduced (14.8.2) may be
1249 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001250 // template arguments can be deduced, they may all be omitted; in this
1251 // case, the empty template argument list <> itself may also be omitted.
1252 //
1253 // Take all of the explicitly-specified arguments and put them into the
Mike Stump1eb44332009-09-09 15:08:12 +00001254 // set of deduced template arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001255 Deduced.reserve(TemplateParams->size());
1256 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001257 Deduced.push_back(ExplicitArgumentList->get(I));
1258
Douglas Gregor83314aa2009-07-08 20:55:45 +00001259 return TDK_Success;
1260}
1261
Douglas Gregor02024a92010-03-28 02:42:43 +00001262/// \brief Allocate a TemplateArgumentLoc where all locations have
1263/// been initialized to the given location.
1264///
1265/// \param S The semantic analysis object.
1266///
1267/// \param The template argument we are producing template argument
1268/// location information for.
1269///
1270/// \param NTTPType For a declaration template argument, the type of
1271/// the non-type template parameter that corresponds to this template
1272/// argument.
1273///
1274/// \param Loc The source location to use for the resulting template
1275/// argument.
1276static TemplateArgumentLoc
1277getTrivialTemplateArgumentLoc(Sema &S,
1278 const TemplateArgument &Arg,
1279 QualType NTTPType,
1280 SourceLocation Loc) {
1281 switch (Arg.getKind()) {
1282 case TemplateArgument::Null:
1283 llvm_unreachable("Can't get a NULL template argument here");
1284 break;
1285
1286 case TemplateArgument::Type:
1287 return TemplateArgumentLoc(Arg,
1288 S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
1289
1290 case TemplateArgument::Declaration: {
1291 Expr *E
1292 = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
1293 .takeAs<Expr>();
1294 return TemplateArgumentLoc(TemplateArgument(E), E);
1295 }
1296
1297 case TemplateArgument::Integral: {
1298 Expr *E
1299 = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>();
1300 return TemplateArgumentLoc(TemplateArgument(E), E);
1301 }
1302
1303 case TemplateArgument::Template:
1304 return TemplateArgumentLoc(Arg, SourceRange(), Loc);
1305
1306 case TemplateArgument::Expression:
1307 return TemplateArgumentLoc(Arg, Arg.getAsExpr());
1308
1309 case TemplateArgument::Pack:
1310 llvm_unreachable("Template parameter packs are not yet supported");
1311 }
1312
1313 return TemplateArgumentLoc();
1314}
1315
Mike Stump1eb44332009-09-09 15:08:12 +00001316/// \brief Finish template argument deduction for a function template,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001317/// checking the deduced template arguments for completeness and forming
1318/// the function template specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00001319Sema::TemplateDeductionResult
Douglas Gregor83314aa2009-07-08 20:55:45 +00001320Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor02024a92010-03-28 02:42:43 +00001321 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1322 unsigned NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001323 FunctionDecl *&Specialization,
1324 TemplateDeductionInfo &Info) {
1325 TemplateParameterList *TemplateParams
1326 = FunctionTemplate->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Douglas Gregor83314aa2009-07-08 20:55:45 +00001328 // Template argument deduction for function templates in a SFINAE context.
1329 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001330 SFINAETrap Trap(*this);
1331
Douglas Gregor83314aa2009-07-08 20:55:45 +00001332 // Enter a new template instantiation context while we instantiate the
1333 // actual function declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001334 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001335 FunctionTemplate, Deduced.data(), Deduced.size(),
1336 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
1337 if (Inst)
Mike Stump1eb44332009-09-09 15:08:12 +00001338 return TDK_InstantiationDepth;
1339
John McCall96db3102010-04-29 01:18:58 +00001340 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
John McCallf5813822010-04-29 00:35:03 +00001341
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001342 // C++ [temp.deduct.type]p2:
1343 // [...] or if any template argument remains neither deduced nor
1344 // explicitly specified, template argument deduction fails.
1345 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
1346 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregor02024a92010-03-28 02:42:43 +00001347 NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001348 if (!Deduced[I].isNull()) {
Douglas Gregor02024a92010-03-28 02:42:43 +00001349 if (I < NumExplicitlySpecified ||
1350 Deduced[I].getKind() == TemplateArgument::Type) {
1351 // We have already fully type-checked and converted this
1352 // argument (because it was explicitly-specified) or no
1353 // additional checking is necessary (because it's a template
1354 // type parameter). Just record the presence of this
1355 // parameter.
1356 Builder.Append(Deduced[I]);
1357 continue;
1358 }
1359
1360 // We have deduced this argument, so it still needs to be
1361 // checked and converted.
1362
1363 // First, for a non-type template parameter type that is
1364 // initialized by a declaration, we need the type of the
1365 // corresponding non-type template parameter.
1366 QualType NTTPType;
1367 if (NonTypeTemplateParmDecl *NTTP
1368 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1369 if (Deduced[I].getKind() == TemplateArgument::Declaration) {
1370 NTTPType = NTTP->getType();
1371 if (NTTPType->isDependentType()) {
1372 TemplateArgumentList TemplateArgs(Context, Builder,
1373 /*TakeArgs=*/false);
1374 NTTPType = SubstType(NTTPType,
1375 MultiLevelTemplateArgumentList(TemplateArgs),
1376 NTTP->getLocation(),
1377 NTTP->getDeclName());
1378 if (NTTPType.isNull()) {
1379 Info.Param = makeTemplateParameter(Param);
Douglas Gregorec20f462010-05-08 20:07:26 +00001380 Info.reset(new (Context) TemplateArgumentList(Context, Builder,
1381 /*TakeArgs=*/true));
Douglas Gregor02024a92010-03-28 02:42:43 +00001382 return TDK_SubstitutionFailure;
1383 }
1384 }
1385 }
1386 }
1387
1388 // Convert the deduced template argument into a template
1389 // argument that we can check, almost as if the user had written
1390 // the template argument explicitly.
1391 TemplateArgumentLoc Arg = getTrivialTemplateArgumentLoc(*this,
1392 Deduced[I],
1393 NTTPType,
1394 SourceLocation());
1395
1396 // Check the template argument, converting it as necessary.
1397 if (CheckTemplateArgument(Param, Arg,
1398 FunctionTemplate,
1399 FunctionTemplate->getLocation(),
1400 FunctionTemplate->getSourceRange().getEnd(),
1401 Builder,
1402 Deduced[I].wasDeducedFromArrayBound()
1403 ? CTAK_DeducedFromArrayBound
1404 : CTAK_Deduced)) {
1405 Info.Param = makeTemplateParameter(
1406 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregorec20f462010-05-08 20:07:26 +00001407 Info.reset(new (Context) TemplateArgumentList(Context, Builder,
1408 /*TakeArgs=*/true));
Douglas Gregor02024a92010-03-28 02:42:43 +00001409 return TDK_SubstitutionFailure;
1410 }
1411
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001412 continue;
1413 }
1414
1415 // Substitute into the default template argument, if available.
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001416 TemplateArgumentLoc DefArg
1417 = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
1418 FunctionTemplate->getLocation(),
1419 FunctionTemplate->getSourceRange().getEnd(),
1420 Param,
1421 Builder);
1422
1423 // If there was no default argument, deduction is incomplete.
1424 if (DefArg.getArgument().isNull()) {
1425 Info.Param = makeTemplateParameter(
1426 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1427 return TDK_Incomplete;
1428 }
1429
1430 // Check whether we can actually use the default argument.
1431 if (CheckTemplateArgument(Param, DefArg,
1432 FunctionTemplate,
1433 FunctionTemplate->getLocation(),
1434 FunctionTemplate->getSourceRange().getEnd(),
Douglas Gregor02024a92010-03-28 02:42:43 +00001435 Builder,
1436 CTAK_Deduced)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001437 Info.Param = makeTemplateParameter(
1438 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregorec20f462010-05-08 20:07:26 +00001439 Info.reset(new (Context) TemplateArgumentList(Context, Builder,
1440 /*TakeArgs=*/true));
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001441 return TDK_SubstitutionFailure;
1442 }
1443
1444 // If we get here, we successfully used the default template argument.
1445 }
1446
1447 // Form the template argument list from the deduced template arguments.
1448 TemplateArgumentList *DeducedArgumentList
1449 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1450 Info.reset(DeducedArgumentList);
1451
Mike Stump1eb44332009-09-09 15:08:12 +00001452 // Substitute the deduced template arguments into the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001453 // declaration to produce the function template specialization.
Douglas Gregord4598a22010-04-28 04:52:24 +00001454 DeclContext *Owner = FunctionTemplate->getDeclContext();
1455 if (FunctionTemplate->getFriendObjectKind())
1456 Owner = FunctionTemplate->getLexicalDeclContext();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001457 Specialization = cast_or_null<FunctionDecl>(
Douglas Gregord4598a22010-04-28 04:52:24 +00001458 SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner,
Douglas Gregor357bbd02009-08-28 20:50:45 +00001459 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor83314aa2009-07-08 20:55:45 +00001460 if (!Specialization)
1461 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Douglas Gregorf8825742009-09-15 18:26:13 +00001463 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
1464 FunctionTemplate->getCanonicalDecl());
1465
Mike Stump1eb44332009-09-09 15:08:12 +00001466 // If the template argument list is owned by the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001467 // specialization, release it.
Douglas Gregorec20f462010-05-08 20:07:26 +00001468 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList &&
1469 !Trap.hasErrorOccurred())
Douglas Gregor83314aa2009-07-08 20:55:45 +00001470 Info.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Douglas Gregor83314aa2009-07-08 20:55:45 +00001472 // There may have been an error that did not prevent us from constructing a
1473 // declaration. Mark the declaration invalid and return with a substitution
1474 // failure.
1475 if (Trap.hasErrorOccurred()) {
1476 Specialization->setInvalidDecl(true);
1477 return TDK_SubstitutionFailure;
1478 }
Mike Stump1eb44332009-09-09 15:08:12 +00001479
1480 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001481}
1482
John McCalleff92132010-02-02 02:21:27 +00001483static QualType GetTypeOfFunction(ASTContext &Context,
1484 bool isAddressOfOperand,
1485 FunctionDecl *Fn) {
1486 if (!isAddressOfOperand) return Fn->getType();
1487 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
1488 if (Method->isInstance())
1489 return Context.getMemberPointerType(Fn->getType(),
1490 Context.getTypeDeclType(Method->getParent()).getTypePtr());
1491 return Context.getPointerType(Fn->getType());
1492}
1493
1494/// Apply the deduction rules for overload sets.
1495///
1496/// \return the null type if this argument should be treated as an
1497/// undeduced context
1498static QualType
1499ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
1500 Expr *Arg, QualType ParamType) {
John McCall7bb12da2010-02-02 06:20:04 +00001501 llvm::PointerIntPair<OverloadExpr*,1> R = OverloadExpr::find(Arg);
John McCalleff92132010-02-02 02:21:27 +00001502
John McCall7bb12da2010-02-02 06:20:04 +00001503 bool isAddressOfOperand = bool(R.getInt());
1504 OverloadExpr *Ovl = R.getPointer();
John McCalleff92132010-02-02 02:21:27 +00001505
1506 // If there were explicit template arguments, we can only find
1507 // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
1508 // unambiguously name a full specialization.
John McCall7bb12da2010-02-02 06:20:04 +00001509 if (Ovl->hasExplicitTemplateArgs()) {
John McCalleff92132010-02-02 02:21:27 +00001510 // But we can still look for an explicit specialization.
1511 if (FunctionDecl *ExplicitSpec
John McCall7bb12da2010-02-02 06:20:04 +00001512 = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
1513 return GetTypeOfFunction(S.Context, isAddressOfOperand, ExplicitSpec);
John McCalleff92132010-02-02 02:21:27 +00001514 return QualType();
1515 }
1516
1517 // C++0x [temp.deduct.call]p6:
1518 // When P is a function type, pointer to function type, or pointer
1519 // to member function type:
1520
1521 if (!ParamType->isFunctionType() &&
1522 !ParamType->isFunctionPointerType() &&
1523 !ParamType->isMemberFunctionPointerType())
1524 return QualType();
1525
1526 QualType Match;
John McCall7bb12da2010-02-02 06:20:04 +00001527 for (UnresolvedSetIterator I = Ovl->decls_begin(),
1528 E = Ovl->decls_end(); I != E; ++I) {
John McCalleff92132010-02-02 02:21:27 +00001529 NamedDecl *D = (*I)->getUnderlyingDecl();
1530
1531 // - If the argument is an overload set containing one or more
1532 // function templates, the parameter is treated as a
1533 // non-deduced context.
1534 if (isa<FunctionTemplateDecl>(D))
1535 return QualType();
1536
1537 FunctionDecl *Fn = cast<FunctionDecl>(D);
1538 QualType ArgType = GetTypeOfFunction(S.Context, isAddressOfOperand, Fn);
1539
1540 // - If the argument is an overload set (not containing function
1541 // templates), trial argument deduction is attempted using each
1542 // of the members of the set. If deduction succeeds for only one
1543 // of the overload set members, that member is used as the
1544 // argument value for the deduction. If deduction succeeds for
1545 // more than one member of the overload set the parameter is
1546 // treated as a non-deduced context.
1547
1548 // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
1549 // Type deduction is done independently for each P/A pair, and
1550 // the deduced template argument values are then combined.
1551 // So we do not reject deductions which were made elsewhere.
Douglas Gregor02024a92010-03-28 02:42:43 +00001552 llvm::SmallVector<DeducedTemplateArgument, 8>
1553 Deduced(TemplateParams->size());
John McCall5769d612010-02-08 23:07:23 +00001554 Sema::TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
John McCalleff92132010-02-02 02:21:27 +00001555 unsigned TDF = 0;
1556
1557 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001558 = DeduceTemplateArguments(S, TemplateParams,
John McCalleff92132010-02-02 02:21:27 +00001559 ParamType, ArgType,
1560 Info, Deduced, TDF);
1561 if (Result) continue;
1562 if (!Match.isNull()) return QualType();
1563 Match = ArgType;
1564 }
1565
1566 return Match;
1567}
1568
Douglas Gregore53060f2009-06-25 22:08:12 +00001569/// \brief Perform template argument deduction from a function call
1570/// (C++ [temp.deduct.call]).
1571///
1572/// \param FunctionTemplate the function template for which we are performing
1573/// template argument deduction.
1574///
Douglas Gregor48026d22010-01-11 18:40:55 +00001575/// \param ExplicitTemplateArguments the explicit template arguments provided
1576/// for this call.
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001577///
Douglas Gregore53060f2009-06-25 22:08:12 +00001578/// \param Args the function call arguments
1579///
1580/// \param NumArgs the number of arguments in Args
1581///
Douglas Gregor48026d22010-01-11 18:40:55 +00001582/// \param Name the name of the function being called. This is only significant
1583/// when the function template is a conversion function template, in which
1584/// case this routine will also perform template argument deduction based on
1585/// the function to which
1586///
Douglas Gregore53060f2009-06-25 22:08:12 +00001587/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001588/// this will be set to the function template specialization produced by
Douglas Gregore53060f2009-06-25 22:08:12 +00001589/// template argument deduction.
1590///
1591/// \param Info the argument will be updated to provide additional information
1592/// about template argument deduction.
1593///
1594/// \returns the result of template argument deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001595Sema::TemplateDeductionResult
1596Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor48026d22010-01-11 18:40:55 +00001597 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregore53060f2009-06-25 22:08:12 +00001598 Expr **Args, unsigned NumArgs,
1599 FunctionDecl *&Specialization,
1600 TemplateDeductionInfo &Info) {
1601 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001602
Douglas Gregore53060f2009-06-25 22:08:12 +00001603 // C++ [temp.deduct.call]p1:
1604 // Template argument deduction is done by comparing each function template
1605 // parameter type (call it P) with the type of the corresponding argument
1606 // of the call (call it A) as described below.
1607 unsigned CheckArgs = NumArgs;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001608 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregore53060f2009-06-25 22:08:12 +00001609 return TDK_TooFewArguments;
1610 else if (NumArgs > Function->getNumParams()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001611 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001612 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregore53060f2009-06-25 22:08:12 +00001613 if (!Proto->isVariadic())
1614 return TDK_TooManyArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Douglas Gregore53060f2009-06-25 22:08:12 +00001616 CheckArgs = Function->getNumParams();
1617 }
Mike Stump1eb44332009-09-09 15:08:12 +00001618
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001619 // The types of the parameters from which we will perform template argument
1620 // deduction.
Douglas Gregor2b0749a42010-03-25 15:38:42 +00001621 Sema::LocalInstantiationScope InstScope(*this);
Douglas Gregore53060f2009-06-25 22:08:12 +00001622 TemplateParameterList *TemplateParams
1623 = FunctionTemplate->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00001624 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001625 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregor02024a92010-03-28 02:42:43 +00001626 unsigned NumExplicitlySpecified = 0;
John McCalld5532b62009-11-23 01:53:49 +00001627 if (ExplicitTemplateArgs) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001628 TemplateDeductionResult Result =
1629 SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001630 *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001631 Deduced,
1632 ParamTypes,
1633 0,
1634 Info);
1635 if (Result)
1636 return Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00001637
1638 NumExplicitlySpecified = Deduced.size();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001639 } else {
1640 // Just fill in the parameter types from the function declaration.
1641 for (unsigned I = 0; I != CheckArgs; ++I)
1642 ParamTypes.push_back(Function->getParamDecl(I)->getType());
1643 }
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001645 // Deduce template arguments from the function parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001646 Deduced.resize(TemplateParams->size());
Douglas Gregore53060f2009-06-25 22:08:12 +00001647 for (unsigned I = 0; I != CheckArgs; ++I) {
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001648 QualType ParamType = ParamTypes[I];
Douglas Gregore53060f2009-06-25 22:08:12 +00001649 QualType ArgType = Args[I]->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001650
John McCalleff92132010-02-02 02:21:27 +00001651 // Overload sets usually make this parameter an undeduced
1652 // context, but there are sometimes special circumstances.
1653 if (ArgType == Context.OverloadTy) {
1654 ArgType = ResolveOverloadForDeduction(*this, TemplateParams,
1655 Args[I], ParamType);
1656 if (ArgType.isNull())
1657 continue;
1658 }
1659
Douglas Gregore53060f2009-06-25 22:08:12 +00001660 // C++ [temp.deduct.call]p2:
1661 // If P is not a reference type:
1662 QualType CanonParamType = Context.getCanonicalType(ParamType);
Douglas Gregor500d3312009-06-26 18:27:22 +00001663 bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1664 if (!ParamWasReference) {
Mike Stump1eb44332009-09-09 15:08:12 +00001665 // - If A is an array type, the pointer type produced by the
1666 // array-to-pointer standard conversion (4.2) is used in place of
Douglas Gregore53060f2009-06-25 22:08:12 +00001667 // A for type deduction; otherwise,
1668 if (ArgType->isArrayType())
1669 ArgType = Context.getArrayDecayedType(ArgType);
Mike Stump1eb44332009-09-09 15:08:12 +00001670 // - If A is a function type, the pointer type produced by the
1671 // function-to-pointer standard conversion (4.3) is used in place
Douglas Gregore53060f2009-06-25 22:08:12 +00001672 // of A for type deduction; otherwise,
1673 else if (ArgType->isFunctionType())
1674 ArgType = Context.getPointerType(ArgType);
1675 else {
1676 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1677 // type are ignored for type deduction.
1678 QualType CanonArgType = Context.getCanonicalType(ArgType);
Douglas Gregora4923eb2009-11-16 21:35:15 +00001679 if (CanonArgType.getLocalCVRQualifiers())
1680 ArgType = CanonArgType.getLocalUnqualifiedType();
Douglas Gregore53060f2009-06-25 22:08:12 +00001681 }
1682 }
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Douglas Gregore53060f2009-06-25 22:08:12 +00001684 // C++0x [temp.deduct.call]p3:
1685 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
Mike Stump1eb44332009-09-09 15:08:12 +00001686 // are ignored for type deduction.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001687 if (CanonParamType.getLocalCVRQualifiers())
1688 ParamType = CanonParamType.getLocalUnqualifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001689 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001690 // [...] If P is a reference type, the type referred to by P is used
1691 // for type deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001692 ParamType = ParamRefType->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00001693
1694 // [...] If P is of the form T&&, where T is a template parameter, and
1695 // the argument is an lvalue, the type A& is used in place of A for
Douglas Gregore53060f2009-06-25 22:08:12 +00001696 // type deduction.
1697 if (isa<RValueReferenceType>(ParamRefType) &&
John McCall183700f2009-09-21 23:43:11 +00001698 ParamRefType->getAs<TemplateTypeParmType>() &&
Douglas Gregore53060f2009-06-25 22:08:12 +00001699 Args[I]->isLvalue(Context) == Expr::LV_Valid)
1700 ArgType = Context.getLValueReferenceType(ArgType);
1701 }
Mike Stump1eb44332009-09-09 15:08:12 +00001702
Douglas Gregore53060f2009-06-25 22:08:12 +00001703 // C++0x [temp.deduct.call]p4:
1704 // In general, the deduction process attempts to find template argument
1705 // values that will make the deduced A identical to A (after the type A
1706 // is transformed as described above). [...]
Douglas Gregor12820292009-09-14 20:00:47 +00001707 unsigned TDF = TDF_SkipNonDependent;
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Douglas Gregor508f1c82009-06-26 23:10:12 +00001709 // - If the original P is a reference type, the deduced A (i.e., the
1710 // type referred to by the reference) can be more cv-qualified than
1711 // the transformed A.
1712 if (ParamWasReference)
1713 TDF |= TDF_ParamWithReferenceType;
Mike Stump1eb44332009-09-09 15:08:12 +00001714 // - The transformed A can be another pointer or pointer to member
1715 // type that can be converted to the deduced A via a qualification
Douglas Gregor508f1c82009-06-26 23:10:12 +00001716 // conversion (4.4).
1717 if (ArgType->isPointerType() || ArgType->isMemberPointerType())
1718 TDF |= TDF_IgnoreQualifiers;
Mike Stump1eb44332009-09-09 15:08:12 +00001719 // - If P is a class and P has the form simple-template-id, then the
Douglas Gregor41128772009-06-26 23:27:24 +00001720 // transformed A can be a derived class of the deduced A. Likewise,
1721 // if P is a pointer to a class of the form simple-template-id, the
1722 // transformed A can be a pointer to a derived class pointed to by
1723 // the deduced A.
1724 if (isSimpleTemplateIdType(ParamType) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001725 (isa<PointerType>(ParamType) &&
Douglas Gregor41128772009-06-26 23:27:24 +00001726 isSimpleTemplateIdType(
Ted Kremenek6217b802009-07-29 21:53:49 +00001727 ParamType->getAs<PointerType>()->getPointeeType())))
Douglas Gregor41128772009-06-26 23:27:24 +00001728 TDF |= TDF_DerivedClass;
Mike Stump1eb44332009-09-09 15:08:12 +00001729
Douglas Gregore53060f2009-06-25 22:08:12 +00001730 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001731 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor500d3312009-06-26 18:27:22 +00001732 ParamType, ArgType, Info, Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +00001733 TDF))
Douglas Gregore53060f2009-06-25 22:08:12 +00001734 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001735
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001736 // FIXME: we need to check that the deduced A is the same as A,
1737 // modulo the various allowed differences.
Douglas Gregore53060f2009-06-25 22:08:12 +00001738 }
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001739
Mike Stump1eb44332009-09-09 15:08:12 +00001740 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor02024a92010-03-28 02:42:43 +00001741 NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001742 Specialization, Info);
Douglas Gregore53060f2009-06-25 22:08:12 +00001743}
1744
Douglas Gregor83314aa2009-07-08 20:55:45 +00001745/// \brief Deduce template arguments when taking the address of a function
Douglas Gregor4b52e252009-12-21 23:17:24 +00001746/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
1747/// a template.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001748///
1749/// \param FunctionTemplate the function template for which we are performing
1750/// template argument deduction.
1751///
Douglas Gregor4b52e252009-12-21 23:17:24 +00001752/// \param ExplicitTemplateArguments the explicitly-specified template
1753/// arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001754///
1755/// \param ArgFunctionType the function type that will be used as the
1756/// "argument" type (A) when performing template argument deduction from the
Douglas Gregor4b52e252009-12-21 23:17:24 +00001757/// function template's function type. This type may be NULL, if there is no
1758/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001759///
1760/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001761/// this will be set to the function template specialization produced by
Douglas Gregor83314aa2009-07-08 20:55:45 +00001762/// template argument deduction.
1763///
1764/// \param Info the argument will be updated to provide additional information
1765/// about template argument deduction.
1766///
1767/// \returns the result of template argument deduction.
1768Sema::TemplateDeductionResult
1769Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001770 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001771 QualType ArgFunctionType,
1772 FunctionDecl *&Specialization,
1773 TemplateDeductionInfo &Info) {
1774 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1775 TemplateParameterList *TemplateParams
1776 = FunctionTemplate->getTemplateParameters();
1777 QualType FunctionType = Function->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Douglas Gregor83314aa2009-07-08 20:55:45 +00001779 // Substitute any explicit template arguments.
Douglas Gregor2b0749a42010-03-25 15:38:42 +00001780 Sema::LocalInstantiationScope InstScope(*this);
Douglas Gregor02024a92010-03-28 02:42:43 +00001781 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
1782 unsigned NumExplicitlySpecified = 0;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001783 llvm::SmallVector<QualType, 4> ParamTypes;
John McCalld5532b62009-11-23 01:53:49 +00001784 if (ExplicitTemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001785 if (TemplateDeductionResult Result
1786 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001787 *ExplicitTemplateArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00001788 Deduced, ParamTypes,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001789 &FunctionType, Info))
1790 return Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00001791
1792 NumExplicitlySpecified = Deduced.size();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001793 }
1794
1795 // Template argument deduction for function templates in a SFINAE context.
1796 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001797 SFINAETrap Trap(*this);
1798
John McCalleff92132010-02-02 02:21:27 +00001799 Deduced.resize(TemplateParams->size());
1800
Douglas Gregor4b52e252009-12-21 23:17:24 +00001801 if (!ArgFunctionType.isNull()) {
1802 // Deduce template arguments from the function type.
Douglas Gregor4b52e252009-12-21 23:17:24 +00001803 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001804 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor4b52e252009-12-21 23:17:24 +00001805 FunctionType, ArgFunctionType, Info,
1806 Deduced, 0))
1807 return Result;
1808 }
1809
Mike Stump1eb44332009-09-09 15:08:12 +00001810 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor02024a92010-03-28 02:42:43 +00001811 NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001812 Specialization, Info);
1813}
1814
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001815/// \brief Deduce template arguments for a templated conversion
1816/// function (C++ [temp.deduct.conv]) and, if successful, produce a
1817/// conversion function template specialization.
1818Sema::TemplateDeductionResult
1819Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1820 QualType ToType,
1821 CXXConversionDecl *&Specialization,
1822 TemplateDeductionInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00001823 CXXConversionDecl *Conv
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001824 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
1825 QualType FromType = Conv->getConversionType();
1826
1827 // Canonicalize the types for deduction.
1828 QualType P = Context.getCanonicalType(FromType);
1829 QualType A = Context.getCanonicalType(ToType);
1830
1831 // C++0x [temp.deduct.conv]p3:
1832 // If P is a reference type, the type referred to by P is used for
1833 // type deduction.
1834 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
1835 P = PRef->getPointeeType();
1836
1837 // C++0x [temp.deduct.conv]p3:
1838 // If A is a reference type, the type referred to by A is used
1839 // for type deduction.
1840 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
1841 A = ARef->getPointeeType();
1842 // C++ [temp.deduct.conv]p2:
1843 //
Mike Stump1eb44332009-09-09 15:08:12 +00001844 // If A is not a reference type:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001845 else {
1846 assert(!A->isReferenceType() && "Reference types were handled above");
1847
1848 // - If P is an array type, the pointer type produced by the
Mike Stump1eb44332009-09-09 15:08:12 +00001849 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001850 // of P for type deduction; otherwise,
1851 if (P->isArrayType())
1852 P = Context.getArrayDecayedType(P);
1853 // - If P is a function type, the pointer type produced by the
1854 // function-to-pointer standard conversion (4.3) is used in
1855 // place of P for type deduction; otherwise,
1856 else if (P->isFunctionType())
1857 P = Context.getPointerType(P);
1858 // - If P is a cv-qualified type, the top level cv-qualifiers of
1859 // P’s type are ignored for type deduction.
1860 else
1861 P = P.getUnqualifiedType();
1862
1863 // C++0x [temp.deduct.conv]p3:
1864 // If A is a cv-qualified type, the top level cv-qualifiers of A’s
1865 // type are ignored for type deduction.
1866 A = A.getUnqualifiedType();
1867 }
1868
1869 // Template argument deduction for function templates in a SFINAE context.
1870 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001871 SFINAETrap Trap(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001872
1873 // C++ [temp.deduct.conv]p1:
1874 // Template argument deduction is done by comparing the return
1875 // type of the template conversion function (call it P) with the
1876 // type that is required as the result of the conversion (call it
1877 // A) as described in 14.8.2.4.
1878 TemplateParameterList *TemplateParams
1879 = FunctionTemplate->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00001880 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Mike Stump1eb44332009-09-09 15:08:12 +00001881 Deduced.resize(TemplateParams->size());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001882
1883 // C++0x [temp.deduct.conv]p4:
1884 // In general, the deduction process attempts to find template
1885 // argument values that will make the deduced A identical to
1886 // A. However, there are two cases that allow a difference:
1887 unsigned TDF = 0;
1888 // - If the original A is a reference type, A can be more
1889 // cv-qualified than the deduced A (i.e., the type referred to
1890 // by the reference)
1891 if (ToType->isReferenceType())
1892 TDF |= TDF_ParamWithReferenceType;
1893 // - The deduced A can be another pointer or pointer to member
1894 // type that can be converted to A via a qualification
1895 // conversion.
1896 //
1897 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
1898 // both P and A are pointers or member pointers. In this case, we
1899 // just ignore cv-qualifiers completely).
1900 if ((P->isPointerType() && A->isPointerType()) ||
1901 (P->isMemberPointerType() && P->isMemberPointerType()))
1902 TDF |= TDF_IgnoreQualifiers;
1903 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001904 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001905 P, A, Info, Deduced, TDF))
1906 return Result;
1907
1908 // FIXME: we need to check that the deduced A is the same as A,
1909 // modulo the various allowed differences.
Mike Stump1eb44332009-09-09 15:08:12 +00001910
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001911 // Finish template argument deduction.
Douglas Gregor2b0749a42010-03-25 15:38:42 +00001912 Sema::LocalInstantiationScope InstScope(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001913 FunctionDecl *Spec = 0;
1914 TemplateDeductionResult Result
Douglas Gregor02024a92010-03-28 02:42:43 +00001915 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec,
1916 Info);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001917 Specialization = cast_or_null<CXXConversionDecl>(Spec);
1918 return Result;
1919}
1920
Douglas Gregor4b52e252009-12-21 23:17:24 +00001921/// \brief Deduce template arguments for a function template when there is
1922/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
1923///
1924/// \param FunctionTemplate the function template for which we are performing
1925/// template argument deduction.
1926///
1927/// \param ExplicitTemplateArguments the explicitly-specified template
1928/// arguments.
1929///
1930/// \param Specialization if template argument deduction was successful,
1931/// this will be set to the function template specialization produced by
1932/// template argument deduction.
1933///
1934/// \param Info the argument will be updated to provide additional information
1935/// about template argument deduction.
1936///
1937/// \returns the result of template argument deduction.
1938Sema::TemplateDeductionResult
1939Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1940 const TemplateArgumentListInfo *ExplicitTemplateArgs,
1941 FunctionDecl *&Specialization,
1942 TemplateDeductionInfo &Info) {
1943 return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
1944 QualType(), Specialization, Info);
1945}
1946
Douglas Gregor8a514912009-09-14 18:39:43 +00001947/// \brief Stores the result of comparing the qualifiers of two types.
1948enum DeductionQualifierComparison {
1949 NeitherMoreQualified = 0,
1950 ParamMoreQualified,
1951 ArgMoreQualified
1952};
1953
1954/// \brief Deduce the template arguments during partial ordering by comparing
1955/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
1956///
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001957/// \param S the semantic analysis object within which we are deducing
Douglas Gregor8a514912009-09-14 18:39:43 +00001958///
1959/// \param TemplateParams the template parameters that we are deducing
1960///
1961/// \param ParamIn the parameter type
1962///
1963/// \param ArgIn the argument type
1964///
1965/// \param Info information about the template argument deduction itself
1966///
1967/// \param Deduced the deduced template arguments
1968///
1969/// \returns the result of template argument deduction so far. Note that a
1970/// "success" result means that template argument deduction has not yet failed,
1971/// but it may still fail, later, for other reasons.
1972static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001973DeduceTemplateArgumentsDuringPartialOrdering(Sema &S,
Douglas Gregor02024a92010-03-28 02:42:43 +00001974 TemplateParameterList *TemplateParams,
Douglas Gregor8a514912009-09-14 18:39:43 +00001975 QualType ParamIn, QualType ArgIn,
1976 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +00001977 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1978 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001979 CanQualType Param = S.Context.getCanonicalType(ParamIn);
1980 CanQualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor8a514912009-09-14 18:39:43 +00001981
1982 // C++0x [temp.deduct.partial]p5:
1983 // Before the partial ordering is done, certain transformations are
1984 // performed on the types used for partial ordering:
1985 // - If P is a reference type, P is replaced by the type referred to.
1986 CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00001987 if (!ParamRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00001988 Param = ParamRef->getPointeeType();
1989
1990 // - If A is a reference type, A is replaced by the type referred to.
1991 CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00001992 if (!ArgRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00001993 Arg = ArgRef->getPointeeType();
1994
John McCalle27ec8a2009-10-23 23:03:21 +00001995 if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
Douglas Gregor8a514912009-09-14 18:39:43 +00001996 // C++0x [temp.deduct.partial]p6:
1997 // If both P and A were reference types (before being replaced with the
1998 // type referred to above), determine which of the two types (if any) is
1999 // more cv-qualified than the other; otherwise the types are considered to
2000 // be equally cv-qualified for partial ordering purposes. The result of this
2001 // determination will be used below.
2002 //
2003 // We save this information for later, using it only when deduction
2004 // succeeds in both directions.
2005 DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
2006 if (Param.isMoreQualifiedThan(Arg))
2007 QualifierResult = ParamMoreQualified;
2008 else if (Arg.isMoreQualifiedThan(Param))
2009 QualifierResult = ArgMoreQualified;
2010 QualifierComparisons->push_back(QualifierResult);
2011 }
2012
2013 // C++0x [temp.deduct.partial]p7:
2014 // Remove any top-level cv-qualifiers:
2015 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
2016 // version of P.
2017 Param = Param.getUnqualifiedType();
2018 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
2019 // version of A.
2020 Arg = Arg.getUnqualifiedType();
2021
2022 // C++0x [temp.deduct.partial]p8:
2023 // Using the resulting types P and A the deduction is then done as
2024 // described in 14.9.2.5. If deduction succeeds for a given type, the type
2025 // from the argument template is considered to be at least as specialized
2026 // as the type from the parameter template.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002027 return DeduceTemplateArguments(S, TemplateParams, Param, Arg, Info,
Douglas Gregor8a514912009-09-14 18:39:43 +00002028 Deduced, TDF_None);
2029}
2030
2031static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002032MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2033 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002034 unsigned Level,
Douglas Gregore73bb602009-09-14 21:25:05 +00002035 llvm::SmallVectorImpl<bool> &Deduced);
Douglas Gregor8a514912009-09-14 18:39:43 +00002036
2037/// \brief Determine whether the function template \p FT1 is at least as
2038/// specialized as \p FT2.
2039static bool isAtLeastAsSpecializedAs(Sema &S,
John McCall5769d612010-02-08 23:07:23 +00002040 SourceLocation Loc,
Douglas Gregor8a514912009-09-14 18:39:43 +00002041 FunctionTemplateDecl *FT1,
2042 FunctionTemplateDecl *FT2,
2043 TemplatePartialOrderingContext TPOC,
2044 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
2045 FunctionDecl *FD1 = FT1->getTemplatedDecl();
2046 FunctionDecl *FD2 = FT2->getTemplatedDecl();
2047 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
2048 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
2049
2050 assert(Proto1 && Proto2 && "Function templates must have prototypes");
2051 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00002052 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor8a514912009-09-14 18:39:43 +00002053 Deduced.resize(TemplateParams->size());
2054
2055 // C++0x [temp.deduct.partial]p3:
2056 // The types used to determine the ordering depend on the context in which
2057 // the partial ordering is done:
John McCall5769d612010-02-08 23:07:23 +00002058 Sema::TemplateDeductionInfo Info(S.Context, Loc);
Douglas Gregor8a514912009-09-14 18:39:43 +00002059 switch (TPOC) {
2060 case TPOC_Call: {
2061 // - In the context of a function call, the function parameter types are
2062 // used.
2063 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
2064 for (unsigned I = 0; I != NumParams; ++I)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002065 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00002066 TemplateParams,
2067 Proto2->getArgType(I),
2068 Proto1->getArgType(I),
2069 Info,
2070 Deduced,
2071 QualifierComparisons))
2072 return false;
2073
2074 break;
2075 }
2076
2077 case TPOC_Conversion:
2078 // - In the context of a call to a conversion operator, the return types
2079 // of the conversion function templates are used.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002080 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00002081 TemplateParams,
2082 Proto2->getResultType(),
2083 Proto1->getResultType(),
2084 Info,
2085 Deduced,
2086 QualifierComparisons))
2087 return false;
2088 break;
2089
2090 case TPOC_Other:
2091 // - In other contexts (14.6.6.2) the function template’s function type
2092 // is used.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002093 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00002094 TemplateParams,
2095 FD2->getType(),
2096 FD1->getType(),
2097 Info,
2098 Deduced,
2099 QualifierComparisons))
2100 return false;
2101 break;
2102 }
2103
2104 // C++0x [temp.deduct.partial]p11:
2105 // In most cases, all template parameters must have values in order for
2106 // deduction to succeed, but for partial ordering purposes a template
2107 // parameter may remain without a value provided it is not used in the
2108 // types being used for partial ordering. [ Note: a template parameter used
2109 // in a non-deduced context is considered used. -end note]
2110 unsigned ArgIdx = 0, NumArgs = Deduced.size();
2111 for (; ArgIdx != NumArgs; ++ArgIdx)
2112 if (Deduced[ArgIdx].isNull())
2113 break;
2114
2115 if (ArgIdx == NumArgs) {
2116 // All template arguments were deduced. FT1 is at least as specialized
2117 // as FT2.
2118 return true;
2119 }
2120
Douglas Gregore73bb602009-09-14 21:25:05 +00002121 // Figure out which template parameters were used.
Douglas Gregor8a514912009-09-14 18:39:43 +00002122 llvm::SmallVector<bool, 4> UsedParameters;
2123 UsedParameters.resize(TemplateParams->size());
2124 switch (TPOC) {
2125 case TPOC_Call: {
2126 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
2127 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002128 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
2129 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00002130 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00002131 break;
2132 }
2133
2134 case TPOC_Conversion:
Douglas Gregored9c0f92009-10-29 00:04:11 +00002135 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
2136 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00002137 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00002138 break;
2139
2140 case TPOC_Other:
Douglas Gregored9c0f92009-10-29 00:04:11 +00002141 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
2142 TemplateParams->getDepth(),
2143 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00002144 break;
2145 }
2146
2147 for (; ArgIdx != NumArgs; ++ArgIdx)
2148 // If this argument had no value deduced but was used in one of the types
2149 // used for partial ordering, then deduction fails.
2150 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
2151 return false;
2152
2153 return true;
2154}
2155
2156
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002157/// \brief Returns the more specialized function template according
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002158/// to the rules of function template partial ordering (C++ [temp.func.order]).
2159///
2160/// \param FT1 the first function template
2161///
2162/// \param FT2 the second function template
2163///
Douglas Gregor8a514912009-09-14 18:39:43 +00002164/// \param TPOC the context in which we are performing partial ordering of
2165/// function templates.
Mike Stump1eb44332009-09-09 15:08:12 +00002166///
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002167/// \returns the more specialized function template. If neither
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002168/// template is more specialized, returns NULL.
2169FunctionTemplateDecl *
2170Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
2171 FunctionTemplateDecl *FT2,
John McCall5769d612010-02-08 23:07:23 +00002172 SourceLocation Loc,
Douglas Gregor8a514912009-09-14 18:39:43 +00002173 TemplatePartialOrderingContext TPOC) {
Douglas Gregor8a514912009-09-14 18:39:43 +00002174 llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
John McCall5769d612010-02-08 23:07:23 +00002175 bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, 0);
2176 bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
Douglas Gregor8a514912009-09-14 18:39:43 +00002177 &QualifierComparisons);
2178
2179 if (Better1 != Better2) // We have a clear winner
2180 return Better1? FT1 : FT2;
2181
2182 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002183 return 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00002184
2185
2186 // C++0x [temp.deduct.partial]p10:
2187 // If for each type being considered a given template is at least as
2188 // specialized for all types and more specialized for some set of types and
2189 // the other template is not more specialized for any types or is not at
2190 // least as specialized for any types, then the given template is more
2191 // specialized than the other template. Otherwise, neither template is more
2192 // specialized than the other.
2193 Better1 = false;
2194 Better2 = false;
2195 for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
2196 // C++0x [temp.deduct.partial]p9:
2197 // If, for a given type, deduction succeeds in both directions (i.e., the
2198 // types are identical after the transformations above) and if the type
2199 // from the argument template is more cv-qualified than the type from the
2200 // parameter template (as described above) that type is considered to be
2201 // more specialized than the other. If neither type is more cv-qualified
2202 // than the other then neither type is more specialized than the other.
2203 switch (QualifierComparisons[I]) {
2204 case NeitherMoreQualified:
2205 break;
2206
2207 case ParamMoreQualified:
2208 Better1 = true;
2209 if (Better2)
2210 return 0;
2211 break;
2212
2213 case ArgMoreQualified:
2214 Better2 = true;
2215 if (Better1)
2216 return 0;
2217 break;
2218 }
2219 }
2220
2221 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002222 if (Better1)
2223 return FT1;
Douglas Gregor8a514912009-09-14 18:39:43 +00002224 else if (Better2)
2225 return FT2;
2226 else
2227 return 0;
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002228}
Douglas Gregor83314aa2009-07-08 20:55:45 +00002229
Douglas Gregord5a423b2009-09-25 18:43:00 +00002230/// \brief Determine if the two templates are equivalent.
2231static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
2232 if (T1 == T2)
2233 return true;
2234
2235 if (!T1 || !T2)
2236 return false;
2237
2238 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
2239}
2240
2241/// \brief Retrieve the most specialized of the given function template
2242/// specializations.
2243///
John McCallc373d482010-01-27 01:50:18 +00002244/// \param SpecBegin the start iterator of the function template
2245/// specializations that we will be comparing.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002246///
John McCallc373d482010-01-27 01:50:18 +00002247/// \param SpecEnd the end iterator of the function template
2248/// specializations, paired with \p SpecBegin.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002249///
2250/// \param TPOC the partial ordering context to use to compare the function
2251/// template specializations.
2252///
2253/// \param Loc the location where the ambiguity or no-specializations
2254/// diagnostic should occur.
2255///
2256/// \param NoneDiag partial diagnostic used to diagnose cases where there are
2257/// no matching candidates.
2258///
2259/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
2260/// occurs.
2261///
2262/// \param CandidateDiag partial diagnostic used for each function template
2263/// specialization that is a candidate in the ambiguous ordering. One parameter
2264/// in this diagnostic should be unbound, which will correspond to the string
2265/// describing the template arguments for the function template specialization.
2266///
2267/// \param Index if non-NULL and the result of this function is non-nULL,
2268/// receives the index corresponding to the resulting function template
2269/// specialization.
2270///
2271/// \returns the most specialized function template specialization, if
John McCallc373d482010-01-27 01:50:18 +00002272/// found. Otherwise, returns SpecEnd.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002273///
2274/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
2275/// template argument deduction.
John McCallc373d482010-01-27 01:50:18 +00002276UnresolvedSetIterator
2277Sema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
2278 UnresolvedSetIterator SpecEnd,
2279 TemplatePartialOrderingContext TPOC,
2280 SourceLocation Loc,
2281 const PartialDiagnostic &NoneDiag,
2282 const PartialDiagnostic &AmbigDiag,
2283 const PartialDiagnostic &CandidateDiag) {
2284 if (SpecBegin == SpecEnd) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00002285 Diag(Loc, NoneDiag);
John McCallc373d482010-01-27 01:50:18 +00002286 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002287 }
2288
John McCallc373d482010-01-27 01:50:18 +00002289 if (SpecBegin + 1 == SpecEnd)
2290 return SpecBegin;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002291
2292 // Find the function template that is better than all of the templates it
2293 // has been compared to.
John McCallc373d482010-01-27 01:50:18 +00002294 UnresolvedSetIterator Best = SpecBegin;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002295 FunctionTemplateDecl *BestTemplate
John McCallc373d482010-01-27 01:50:18 +00002296 = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002297 assert(BestTemplate && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00002298 for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
2299 FunctionTemplateDecl *Challenger
2300 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002301 assert(Challenger && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00002302 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCall5769d612010-02-08 23:07:23 +00002303 Loc, TPOC),
Douglas Gregord5a423b2009-09-25 18:43:00 +00002304 Challenger)) {
2305 Best = I;
2306 BestTemplate = Challenger;
2307 }
2308 }
2309
2310 // Make sure that the "best" function template is more specialized than all
2311 // of the others.
2312 bool Ambiguous = false;
John McCallc373d482010-01-27 01:50:18 +00002313 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
2314 FunctionTemplateDecl *Challenger
2315 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002316 if (I != Best &&
2317 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCall5769d612010-02-08 23:07:23 +00002318 Loc, TPOC),
Douglas Gregord5a423b2009-09-25 18:43:00 +00002319 BestTemplate)) {
2320 Ambiguous = true;
2321 break;
2322 }
2323 }
2324
2325 if (!Ambiguous) {
2326 // We found an answer. Return it.
John McCallc373d482010-01-27 01:50:18 +00002327 return Best;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002328 }
2329
2330 // Diagnose the ambiguity.
2331 Diag(Loc, AmbigDiag);
2332
2333 // FIXME: Can we order the candidates in some sane way?
John McCallc373d482010-01-27 01:50:18 +00002334 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
2335 Diag((*I)->getLocation(), CandidateDiag)
Douglas Gregord5a423b2009-09-25 18:43:00 +00002336 << getTemplateArgumentBindingsText(
John McCallc373d482010-01-27 01:50:18 +00002337 cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
2338 *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
Douglas Gregord5a423b2009-09-25 18:43:00 +00002339
John McCallc373d482010-01-27 01:50:18 +00002340 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002341}
2342
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002343/// \brief Returns the more specialized class template partial specialization
2344/// according to the rules of partial ordering of class template partial
2345/// specializations (C++ [temp.class.order]).
2346///
2347/// \param PS1 the first class template partial specialization
2348///
2349/// \param PS2 the second class template partial specialization
2350///
2351/// \returns the more specialized class template partial specialization. If
2352/// neither partial specialization is more specialized, returns NULL.
2353ClassTemplatePartialSpecializationDecl *
2354Sema::getMoreSpecializedPartialSpecialization(
2355 ClassTemplatePartialSpecializationDecl *PS1,
John McCall5769d612010-02-08 23:07:23 +00002356 ClassTemplatePartialSpecializationDecl *PS2,
2357 SourceLocation Loc) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002358 // C++ [temp.class.order]p1:
2359 // For two class template partial specializations, the first is at least as
2360 // specialized as the second if, given the following rewrite to two
2361 // function templates, the first function template is at least as
2362 // specialized as the second according to the ordering rules for function
2363 // templates (14.6.6.2):
2364 // - the first function template has the same template parameters as the
2365 // first partial specialization and has a single function parameter
2366 // whose type is a class template specialization with the template
2367 // arguments of the first partial specialization, and
2368 // - the second function template has the same template parameters as the
2369 // second partial specialization and has a single function parameter
2370 // whose type is a class template specialization with the template
2371 // arguments of the second partial specialization.
2372 //
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002373 // Rather than synthesize function templates, we merely perform the
2374 // equivalent partial ordering by performing deduction directly on
2375 // the template arguments of the class template partial
2376 // specializations. This computation is slightly simpler than the
2377 // general problem of function template partial ordering, because
2378 // class template partial specializations are more constrained. We
2379 // know that every template parameter is deducible from the class
2380 // template partial specialization's template arguments, for
2381 // example.
Douglas Gregor02024a92010-03-28 02:42:43 +00002382 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
John McCall5769d612010-02-08 23:07:23 +00002383 Sema::TemplateDeductionInfo Info(Context, Loc);
John McCall31f17ec2010-04-27 00:57:59 +00002384
2385 QualType PT1 = PS1->getInjectedSpecializationType();
2386 QualType PT2 = PS2->getInjectedSpecializationType();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002387
2388 // Determine whether PS1 is at least as specialized as PS2
2389 Deduced.resize(PS2->getTemplateParameters()->size());
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002390 bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002391 PS2->getTemplateParameters(),
John McCall31f17ec2010-04-27 00:57:59 +00002392 PT2,
2393 PT1,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002394 Info,
2395 Deduced,
2396 0);
Douglas Gregor516e6e02010-04-29 06:31:36 +00002397 if (Better1)
2398 Better1 = !::FinishTemplateArgumentDeduction(*this, PS2,
2399 PS1->getTemplateArgs(),
2400 Deduced, Info);
2401
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002402 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregordb0d4b72009-11-11 23:06:43 +00002403 Deduced.clear();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002404 Deduced.resize(PS1->getTemplateParameters()->size());
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002405 bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002406 PS1->getTemplateParameters(),
John McCall31f17ec2010-04-27 00:57:59 +00002407 PT1,
2408 PT2,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002409 Info,
2410 Deduced,
2411 0);
Douglas Gregor516e6e02010-04-29 06:31:36 +00002412 if (Better2)
2413 Better2 = !::FinishTemplateArgumentDeduction(*this, PS1,
2414 PS2->getTemplateArgs(),
2415 Deduced, Info);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002416
2417 if (Better1 == Better2)
2418 return 0;
2419
2420 return Better1? PS1 : PS2;
2421}
2422
Mike Stump1eb44332009-09-09 15:08:12 +00002423static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002424MarkUsedTemplateParameters(Sema &SemaRef,
2425 const TemplateArgument &TemplateArg,
2426 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002427 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002428 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002429
Douglas Gregore73bb602009-09-14 21:25:05 +00002430/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002431/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00002432static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002433MarkUsedTemplateParameters(Sema &SemaRef,
2434 const Expr *E,
2435 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002436 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002437 llvm::SmallVectorImpl<bool> &Used) {
2438 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2439 // find other occurrences of template parameters.
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002440 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregorc781f9c2010-01-14 18:13:22 +00002441 if (!DRE)
Douglas Gregor031a5882009-06-13 00:26:55 +00002442 return;
2443
Mike Stump1eb44332009-09-09 15:08:12 +00002444 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00002445 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2446 if (!NTTP)
2447 return;
2448
Douglas Gregored9c0f92009-10-29 00:04:11 +00002449 if (NTTP->getDepth() == Depth)
2450 Used[NTTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002451}
2452
Douglas Gregore73bb602009-09-14 21:25:05 +00002453/// \brief Mark the template parameters that are used by the given
2454/// nested name specifier.
2455static void
2456MarkUsedTemplateParameters(Sema &SemaRef,
2457 NestedNameSpecifier *NNS,
2458 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002459 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002460 llvm::SmallVectorImpl<bool> &Used) {
2461 if (!NNS)
2462 return;
2463
Douglas Gregored9c0f92009-10-29 00:04:11 +00002464 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2465 Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002466 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002467 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002468}
2469
2470/// \brief Mark the template parameters that are used by the given
2471/// template name.
2472static void
2473MarkUsedTemplateParameters(Sema &SemaRef,
2474 TemplateName Name,
2475 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002476 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002477 llvm::SmallVectorImpl<bool> &Used) {
2478 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2479 if (TemplateTemplateParmDecl *TTP
Douglas Gregored9c0f92009-10-29 00:04:11 +00002480 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2481 if (TTP->getDepth() == Depth)
2482 Used[TTP->getIndex()] = true;
2483 }
Douglas Gregore73bb602009-09-14 21:25:05 +00002484 return;
2485 }
2486
Douglas Gregor788cd062009-11-11 01:00:40 +00002487 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2488 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2489 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002490 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
Douglas Gregored9c0f92009-10-29 00:04:11 +00002491 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2492 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002493}
2494
2495/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002496/// type.
Mike Stump1eb44332009-09-09 15:08:12 +00002497static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002498MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2499 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002500 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002501 llvm::SmallVectorImpl<bool> &Used) {
2502 if (T.isNull())
2503 return;
2504
Douglas Gregor031a5882009-06-13 00:26:55 +00002505 // Non-dependent types have nothing deducible
2506 if (!T->isDependentType())
2507 return;
2508
2509 T = SemaRef.Context.getCanonicalType(T);
2510 switch (T->getTypeClass()) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002511 case Type::Pointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002512 MarkUsedTemplateParameters(SemaRef,
2513 cast<PointerType>(T)->getPointeeType(),
2514 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002515 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002516 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002517 break;
2518
2519 case Type::BlockPointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002520 MarkUsedTemplateParameters(SemaRef,
2521 cast<BlockPointerType>(T)->getPointeeType(),
2522 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002523 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002524 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002525 break;
2526
2527 case Type::LValueReference:
2528 case Type::RValueReference:
Douglas Gregore73bb602009-09-14 21:25:05 +00002529 MarkUsedTemplateParameters(SemaRef,
2530 cast<ReferenceType>(T)->getPointeeType(),
2531 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002532 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002533 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002534 break;
2535
2536 case Type::MemberPointer: {
2537 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore73bb602009-09-14 21:25:05 +00002538 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002539 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002540 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002541 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002542 break;
2543 }
2544
2545 case Type::DependentSizedArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002546 MarkUsedTemplateParameters(SemaRef,
2547 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002548 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002549 // Fall through to check the element type
2550
2551 case Type::ConstantArray:
2552 case Type::IncompleteArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002553 MarkUsedTemplateParameters(SemaRef,
2554 cast<ArrayType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002555 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002556 break;
2557
2558 case Type::Vector:
2559 case Type::ExtVector:
Douglas Gregore73bb602009-09-14 21:25:05 +00002560 MarkUsedTemplateParameters(SemaRef,
2561 cast<VectorType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002562 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002563 break;
2564
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002565 case Type::DependentSizedExtVector: {
2566 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002567 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002568 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002569 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002570 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002571 Depth, Used);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002572 break;
2573 }
2574
Douglas Gregor031a5882009-06-13 00:26:55 +00002575 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002576 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002577 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002578 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002579 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore73bb602009-09-14 21:25:05 +00002580 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002581 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002582 break;
2583 }
2584
Douglas Gregored9c0f92009-10-29 00:04:11 +00002585 case Type::TemplateTypeParm: {
2586 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2587 if (TTP->getDepth() == Depth)
2588 Used[TTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002589 break;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002590 }
Douglas Gregor031a5882009-06-13 00:26:55 +00002591
John McCall31f17ec2010-04-27 00:57:59 +00002592 case Type::InjectedClassName:
2593 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
2594 // fall through
2595
Douglas Gregor031a5882009-06-13 00:26:55 +00002596 case Type::TemplateSpecialization: {
Mike Stump1eb44332009-09-09 15:08:12 +00002597 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002598 = cast<TemplateSpecializationType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002599 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002600 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002601 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002602 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2603 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002604 break;
2605 }
2606
Douglas Gregore73bb602009-09-14 21:25:05 +00002607 case Type::Complex:
2608 if (!OnlyDeduced)
2609 MarkUsedTemplateParameters(SemaRef,
2610 cast<ComplexType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002611 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002612 break;
2613
Douglas Gregor4714c122010-03-31 17:34:00 +00002614 case Type::DependentName:
Douglas Gregore73bb602009-09-14 21:25:05 +00002615 if (!OnlyDeduced)
2616 MarkUsedTemplateParameters(SemaRef,
Douglas Gregor4714c122010-03-31 17:34:00 +00002617 cast<DependentNameType>(T)->getQualifier(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002618 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002619 break;
2620
John McCallad5e7382010-03-01 23:49:17 +00002621 case Type::TypeOf:
2622 if (!OnlyDeduced)
2623 MarkUsedTemplateParameters(SemaRef,
2624 cast<TypeOfType>(T)->getUnderlyingType(),
2625 OnlyDeduced, Depth, Used);
2626 break;
2627
2628 case Type::TypeOfExpr:
2629 if (!OnlyDeduced)
2630 MarkUsedTemplateParameters(SemaRef,
2631 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
2632 OnlyDeduced, Depth, Used);
2633 break;
2634
2635 case Type::Decltype:
2636 if (!OnlyDeduced)
2637 MarkUsedTemplateParameters(SemaRef,
2638 cast<DecltypeType>(T)->getUnderlyingExpr(),
2639 OnlyDeduced, Depth, Used);
2640 break;
2641
Douglas Gregore73bb602009-09-14 21:25:05 +00002642 // None of these types have any template parameters in them.
Douglas Gregor031a5882009-06-13 00:26:55 +00002643 case Type::Builtin:
Douglas Gregor031a5882009-06-13 00:26:55 +00002644 case Type::VariableArray:
2645 case Type::FunctionNoProto:
2646 case Type::Record:
2647 case Type::Enum:
Douglas Gregor031a5882009-06-13 00:26:55 +00002648 case Type::ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002649 case Type::ObjCObjectPointer:
John McCalled976492009-12-04 22:46:56 +00002650 case Type::UnresolvedUsing:
Douglas Gregor031a5882009-06-13 00:26:55 +00002651#define TYPE(Class, Base)
2652#define ABSTRACT_TYPE(Class, Base)
2653#define DEPENDENT_TYPE(Class, Base)
2654#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2655#include "clang/AST/TypeNodes.def"
2656 break;
2657 }
2658}
2659
Douglas Gregore73bb602009-09-14 21:25:05 +00002660/// \brief Mark the template parameters that are used by this
Douglas Gregor031a5882009-06-13 00:26:55 +00002661/// template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00002662static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002663MarkUsedTemplateParameters(Sema &SemaRef,
2664 const TemplateArgument &TemplateArg,
2665 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002666 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002667 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002668 switch (TemplateArg.getKind()) {
2669 case TemplateArgument::Null:
2670 case TemplateArgument::Integral:
Douglas Gregor788cd062009-11-11 01:00:40 +00002671 case TemplateArgument::Declaration:
Douglas Gregor031a5882009-06-13 00:26:55 +00002672 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002673
Douglas Gregor031a5882009-06-13 00:26:55 +00002674 case TemplateArgument::Type:
Douglas Gregore73bb602009-09-14 21:25:05 +00002675 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002676 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002677 break;
2678
Douglas Gregor788cd062009-11-11 01:00:40 +00002679 case TemplateArgument::Template:
2680 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2681 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002682 break;
2683
2684 case TemplateArgument::Expression:
Douglas Gregore73bb602009-09-14 21:25:05 +00002685 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002686 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002687 break;
Douglas Gregore73bb602009-09-14 21:25:05 +00002688
Anders Carlssond01b1da2009-06-15 17:04:53 +00002689 case TemplateArgument::Pack:
Douglas Gregore73bb602009-09-14 21:25:05 +00002690 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2691 PEnd = TemplateArg.pack_end();
2692 P != PEnd; ++P)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002693 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssond01b1da2009-06-15 17:04:53 +00002694 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00002695 }
2696}
2697
2698/// \brief Mark the template parameters can be deduced by the given
2699/// template argument list.
2700///
2701/// \param TemplateArgs the template argument list from which template
2702/// parameters will be deduced.
2703///
2704/// \param Deduced a bit vector whose elements will be set to \c true
2705/// to indicate when the corresponding template parameter will be
2706/// deduced.
Mike Stump1eb44332009-09-09 15:08:12 +00002707void
Douglas Gregore73bb602009-09-14 21:25:05 +00002708Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002709 bool OnlyDeduced, unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002710 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002711 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002712 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2713 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002714}
Douglas Gregor63f07c52009-09-18 23:21:38 +00002715
2716/// \brief Marks all of the template parameters that will be deduced by a
2717/// call to the given function template.
Douglas Gregor02024a92010-03-28 02:42:43 +00002718void
2719Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
2720 llvm::SmallVectorImpl<bool> &Deduced) {
Douglas Gregor63f07c52009-09-18 23:21:38 +00002721 TemplateParameterList *TemplateParams
2722 = FunctionTemplate->getTemplateParameters();
2723 Deduced.clear();
2724 Deduced.resize(TemplateParams->size());
2725
2726 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2727 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
2728 ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002729 true, TemplateParams->getDepth(), Deduced);
Douglas Gregor63f07c52009-09-18 23:21:38 +00002730}