blob: ee631b05891e89bb064e1fb3c66a7fec52d7d3d1 [file] [log] [blame]
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001//===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template argument deduction.
10//
11//===----------------------------------------------------------------------===/
12
Douglas Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Sema.h"
John McCall19510852010-08-20 18:27:03 +000014#include "clang/Sema/DeclSpec.h"
Douglas Gregor0b9247f2009-06-04 00:03:07 +000015#include "clang/AST/ASTContext.h"
16#include "clang/AST/DeclTemplate.h"
17#include "clang/AST/StmtVisitor.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/ExprCXX.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));
John McCall57e97782010-08-05 09:05:08 +0000431 Info.FirstArg = TemplateArgument(Param);
John McCall833ca992009-10-29 08:12:44 +0000432 Info.SecondArg = TemplateArgument(Arg);
John McCall57e97782010-08-05 09:05:08 +0000433 return Sema::TDK_Underqualified;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000434 }
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: {
John McCallc0008342010-05-13 07:48:05 +0000485 QualType PointeeType;
486 if (const PointerType *PointerArg = Arg->getAs<PointerType>()) {
487 PointeeType = PointerArg->getPointeeType();
488 } else if (const ObjCObjectPointerType *PointerArg
489 = Arg->getAs<ObjCObjectPointerType>()) {
490 PointeeType = PointerArg->getPointeeType();
491 } else {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000492 return Sema::TDK_NonDeducedMismatch;
John McCallc0008342010-05-13 07:48:05 +0000493 }
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Douglas Gregor41128772009-06-26 23:27:24 +0000495 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000496 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000497 cast<PointerType>(Param)->getPointeeType(),
John McCallc0008342010-05-13 07:48:05 +0000498 PointeeType,
Douglas Gregor41128772009-06-26 23:27:24 +0000499 Info, Deduced, SubTDF);
Douglas Gregord560d502009-06-04 00:21:18 +0000500 }
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Douglas Gregor199d9912009-06-05 00:53:49 +0000502 // T &
Douglas Gregord560d502009-06-04 00:21:18 +0000503 case Type::LValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000504 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000505 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000506 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000508 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000509 cast<LValueReferenceType>(Param)->getPointeeType(),
510 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000511 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000512 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000513
Douglas Gregor199d9912009-06-05 00:53:49 +0000514 // T && [C++0x]
Douglas Gregord560d502009-06-04 00:21:18 +0000515 case Type::RValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000516 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000517 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000518 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000520 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000521 cast<RValueReferenceType>(Param)->getPointeeType(),
522 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000523 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000524 }
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Douglas Gregor199d9912009-06-05 00:53:49 +0000526 // T [] (implied, but not stated explicitly)
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000527 case Type::IncompleteArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000528 const IncompleteArrayType *IncompleteArrayArg =
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000529 S.Context.getAsIncompleteArrayType(Arg);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000530 if (!IncompleteArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000531 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000532
John McCalle4f26e52010-08-19 00:20:19 +0000533 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000534 return DeduceTemplateArguments(S, TemplateParams,
535 S.Context.getAsIncompleteArrayType(Param)->getElementType(),
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000536 IncompleteArrayArg->getElementType(),
John McCalle4f26e52010-08-19 00:20:19 +0000537 Info, Deduced, SubTDF);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000538 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000539
540 // T [integer-constant]
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000541 case Type::ConstantArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000542 const ConstantArrayType *ConstantArrayArg =
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000543 S.Context.getAsConstantArrayType(Arg);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000544 if (!ConstantArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000545 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000546
547 const ConstantArrayType *ConstantArrayParm =
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000548 S.Context.getAsConstantArrayType(Param);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000549 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000550 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000551
John McCalle4f26e52010-08-19 00:20:19 +0000552 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000553 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000554 ConstantArrayParm->getElementType(),
555 ConstantArrayArg->getElementType(),
John McCalle4f26e52010-08-19 00:20:19 +0000556 Info, Deduced, SubTDF);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000557 }
558
Douglas Gregor199d9912009-06-05 00:53:49 +0000559 // type [i]
560 case Type::DependentSizedArray: {
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000561 const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
Douglas Gregor199d9912009-06-05 00:53:49 +0000562 if (!ArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000563 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000564
John McCalle4f26e52010-08-19 00:20:19 +0000565 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
566
Douglas Gregor199d9912009-06-05 00:53:49 +0000567 // Check the element type of the arrays
568 const DependentSizedArrayType *DependentArrayParm
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000569 = S.Context.getAsDependentSizedArrayType(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000570 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000571 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000572 DependentArrayParm->getElementType(),
573 ArrayArg->getElementType(),
John McCalle4f26e52010-08-19 00:20:19 +0000574 Info, Deduced, SubTDF))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000575 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Douglas Gregor199d9912009-06-05 00:53:49 +0000577 // Determine the array bound is something we can deduce.
Mike Stump1eb44332009-09-09 15:08:12 +0000578 NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000579 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
580 if (!NTTP)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000581 return Sema::TDK_Success;
Mike Stump1eb44332009-09-09 15:08:12 +0000582
583 // We can perform template argument deduction for the given non-type
Douglas Gregor199d9912009-06-05 00:53:49 +0000584 // template parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000585 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000586 "Cannot deduce non-type template argument at depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000587 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson335e24a2009-06-16 22:44:31 +0000588 = dyn_cast<ConstantArrayType>(ArrayArg)) {
589 llvm::APSInt Size(ConstantArrayArg->getSize());
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000590 return DeduceNonTypeTemplateArgument(S, NTTP, Size,
591 S.Context.getSizeType(),
Douglas Gregor02024a92010-03-28 02:42:43 +0000592 /*ArrayBound=*/true,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000593 Info, Deduced);
Anders Carlsson335e24a2009-06-16 22:44:31 +0000594 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000595 if (const DependentSizedArrayType *DependentArrayArg
596 = dyn_cast<DependentSizedArrayType>(ArrayArg))
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000597 return DeduceNonTypeTemplateArgument(S, NTTP,
Douglas Gregor199d9912009-06-05 00:53:49 +0000598 DependentArrayArg->getSizeExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000599 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Douglas Gregor199d9912009-06-05 00:53:49 +0000601 // Incomplete type does not match a dependently-sized array type
Douglas Gregorf67875d2009-06-12 18:26:56 +0000602 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000603 }
Mike Stump1eb44332009-09-09 15:08:12 +0000604
605 // type(*)(T)
606 // T(*)()
607 // T(*)(T)
Anders Carlssona27fad52009-06-08 15:19:08 +0000608 case Type::FunctionProto: {
Mike Stump1eb44332009-09-09 15:08:12 +0000609 const FunctionProtoType *FunctionProtoArg =
Anders Carlssona27fad52009-06-08 15:19:08 +0000610 dyn_cast<FunctionProtoType>(Arg);
611 if (!FunctionProtoArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000612 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000613
614 const FunctionProtoType *FunctionProtoParam =
Anders Carlssona27fad52009-06-08 15:19:08 +0000615 cast<FunctionProtoType>(Param);
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000616
Mike Stump1eb44332009-09-09 15:08:12 +0000617 if (FunctionProtoParam->getTypeQuals() !=
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000618 FunctionProtoArg->getTypeQuals())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000619 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000621 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000622 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000624 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000625 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000626
Anders Carlssona27fad52009-06-08 15:19:08 +0000627 // Check return types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000628 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000629 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000630 FunctionProtoParam->getResultType(),
631 FunctionProtoArg->getResultType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000632 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000633 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Anders Carlssona27fad52009-06-08 15:19:08 +0000635 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
636 // Check argument types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000637 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000638 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000639 FunctionProtoParam->getArgType(I),
640 FunctionProtoArg->getArgType(I),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000641 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000642 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000643 }
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Douglas Gregorf67875d2009-06-12 18:26:56 +0000645 return Sema::TDK_Success;
Anders Carlssona27fad52009-06-08 15:19:08 +0000646 }
Mike Stump1eb44332009-09-09 15:08:12 +0000647
John McCall3cb0ebd2010-03-10 03:28:59 +0000648 case Type::InjectedClassName: {
649 // Treat a template's injected-class-name as if the template
650 // specialization type had been used.
John McCall31f17ec2010-04-27 00:57:59 +0000651 Param = cast<InjectedClassNameType>(Param)
652 ->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +0000653 assert(isa<TemplateSpecializationType>(Param) &&
654 "injected class name is not a template specialization type");
655 // fall through
656 }
657
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000658 // template-name<T> (where template-name refers to a class template)
Douglas Gregord708c722009-06-09 16:35:58 +0000659 // template-name<i>
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000660 // TT<T>
661 // TT<i>
662 // TT<>
Douglas Gregord708c722009-06-09 16:35:58 +0000663 case Type::TemplateSpecialization: {
664 const TemplateSpecializationType *SpecParam
665 = cast<TemplateSpecializationType>(Param);
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000667 // Try to deduce template arguments from the template-id.
668 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000669 = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000670 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Douglas Gregor4a5c15f2009-09-30 22:13:51 +0000672 if (Result && (TDF & TDF_DerivedClass)) {
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000673 // C++ [temp.deduct.call]p3b3:
674 // If P is a class, and P has the form template-id, then A can be a
675 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump1eb44332009-09-09 15:08:12 +0000676 // class of the form template-id, A can be a pointer to a derived
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000677 // class pointed to by the deduced A.
678 //
679 // More importantly:
Mike Stump1eb44332009-09-09 15:08:12 +0000680 // These alternatives are considered only if type deduction would
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000681 // otherwise fail.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000682 if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
683 // We cannot inspect base classes as part of deduction when the type
684 // is incomplete, so either instantiate any templates necessary to
685 // complete the type, or skip over it if it cannot be completed.
John McCall5769d612010-02-08 23:07:23 +0000686 if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000687 return Result;
688
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000689 // Use data recursion to crawl through the list of base classes.
Mike Stump1eb44332009-09-09 15:08:12 +0000690 // Visited contains the set of nodes we have already visited, while
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000691 // ToVisit is our stack of records that we still need to visit.
692 llvm::SmallPtrSet<const RecordType *, 8> Visited;
693 llvm::SmallVector<const RecordType *, 8> ToVisit;
694 ToVisit.push_back(RecordT);
695 bool Successful = false;
696 while (!ToVisit.empty()) {
697 // Retrieve the next class in the inheritance hierarchy.
698 const RecordType *NextT = ToVisit.back();
699 ToVisit.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000701 // If we have already seen this type, skip it.
702 if (!Visited.insert(NextT))
703 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000705 // If this is a base class, try to perform template argument
706 // deduction from it.
707 if (NextT != RecordT) {
708 Sema::TemplateDeductionResult BaseResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000709 = DeduceTemplateArguments(S, TemplateParams, SpecParam,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000710 QualType(NextT, 0), Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000712 // If template argument deduction for this base was successful,
713 // note that we had some success.
714 if (BaseResult == Sema::TDK_Success)
715 Successful = true;
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000716 }
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000718 // Visit base classes
719 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
720 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
721 BaseEnd = Next->bases_end();
Sebastian Redl9994a342009-10-25 17:03:50 +0000722 Base != BaseEnd; ++Base) {
Mike Stump1eb44332009-09-09 15:08:12 +0000723 assert(Base->getType()->isRecordType() &&
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000724 "Base class that isn't a record?");
Ted Kremenek6217b802009-07-29 21:53:49 +0000725 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000726 }
727 }
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000729 if (Successful)
730 return Sema::TDK_Success;
731 }
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000733 }
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000735 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000736 }
737
Douglas Gregor637a4092009-06-10 23:47:09 +0000738 // T type::*
739 // T T::*
740 // T (type::*)()
741 // type (T::*)()
742 // type (type::*)(T)
743 // type (T::*)(T)
744 // T (type::*)(T)
745 // T (T::*)()
746 // T (T::*)(T)
747 case Type::MemberPointer: {
748 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
749 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
750 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000751 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +0000752
Douglas Gregorf67875d2009-06-12 18:26:56 +0000753 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000754 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000755 MemPtrParam->getPointeeType(),
756 MemPtrArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000757 Info, Deduced,
758 TDF & TDF_IgnoreQualifiers))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000759 return Result;
760
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000761 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000762 QualType(MemPtrParam->getClass(), 0),
763 QualType(MemPtrArg->getClass(), 0),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000764 Info, Deduced, 0);
Douglas Gregor637a4092009-06-10 23:47:09 +0000765 }
766
Anders Carlsson9a917e42009-06-12 22:56:54 +0000767 // (clang extension)
768 //
Mike Stump1eb44332009-09-09 15:08:12 +0000769 // type(^)(T)
770 // T(^)()
771 // T(^)(T)
Anders Carlsson859ba502009-06-12 16:23:10 +0000772 case Type::BlockPointer: {
773 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
774 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Anders Carlsson859ba502009-06-12 16:23:10 +0000776 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000777 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000779 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +0000780 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000781 BlockPtrArg->getPointeeType(), Info,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000782 Deduced, 0);
Anders Carlsson859ba502009-06-12 16:23:10 +0000783 }
784
Douglas Gregor637a4092009-06-10 23:47:09 +0000785 case Type::TypeOfExpr:
786 case Type::TypeOf:
Douglas Gregor4714c122010-03-31 17:34:00 +0000787 case Type::DependentName:
Douglas Gregor637a4092009-06-10 23:47:09 +0000788 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +0000789 return Sema::TDK_Success;
Douglas Gregor637a4092009-06-10 23:47:09 +0000790
Douglas Gregord560d502009-06-04 00:21:18 +0000791 default:
792 break;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000793 }
794
795 // FIXME: Many more cases to go (to go).
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000796 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000797}
798
Douglas Gregorf67875d2009-06-12 18:26:56 +0000799static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000800DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000801 TemplateParameterList *TemplateParams,
802 const TemplateArgument &Param,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000803 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000804 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000805 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000806 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000807 case TemplateArgument::Null:
808 assert(false && "Null template argument in parameter list");
809 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000810
811 case TemplateArgument::Type:
Douglas Gregor788cd062009-11-11 01:00:40 +0000812 if (Arg.getKind() == TemplateArgument::Type)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000813 return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
Douglas Gregor788cd062009-11-11 01:00:40 +0000814 Arg.getAsType(), Info, Deduced, 0);
815 Info.FirstArg = Param;
816 Info.SecondArg = Arg;
817 return Sema::TDK_NonDeducedMismatch;
818
819 case TemplateArgument::Template:
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000820 if (Arg.getKind() == TemplateArgument::Template)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000821 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor788cd062009-11-11 01:00:40 +0000822 Param.getAsTemplate(),
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000823 Arg.getAsTemplate(), Info, Deduced);
Douglas Gregor788cd062009-11-11 01:00:40 +0000824 Info.FirstArg = Param;
825 Info.SecondArg = Arg;
826 return Sema::TDK_NonDeducedMismatch;
827
Douglas Gregor199d9912009-06-05 00:53:49 +0000828 case TemplateArgument::Declaration:
Douglas Gregor788cd062009-11-11 01:00:40 +0000829 if (Arg.getKind() == TemplateArgument::Declaration &&
830 Param.getAsDecl()->getCanonicalDecl() ==
831 Arg.getAsDecl()->getCanonicalDecl())
832 return Sema::TDK_Success;
833
Douglas Gregorf67875d2009-06-12 18:26:56 +0000834 Info.FirstArg = Param;
835 Info.SecondArg = Arg;
836 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Douglas Gregor199d9912009-06-05 00:53:49 +0000838 case TemplateArgument::Integral:
839 if (Arg.getKind() == TemplateArgument::Integral) {
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000840 if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral()))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000841 return Sema::TDK_Success;
842
843 Info.FirstArg = Param;
844 Info.SecondArg = Arg;
845 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000846 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000847
848 if (Arg.getKind() == TemplateArgument::Expression) {
849 Info.FirstArg = Param;
850 Info.SecondArg = Arg;
851 return Sema::TDK_NonDeducedMismatch;
852 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000853
Douglas Gregorf67875d2009-06-12 18:26:56 +0000854 Info.FirstArg = Param;
855 Info.SecondArg = Arg;
856 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Douglas Gregor199d9912009-06-05 00:53:49 +0000858 case TemplateArgument::Expression: {
Mike Stump1eb44332009-09-09 15:08:12 +0000859 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000860 = getDeducedParameterFromExpr(Param.getAsExpr())) {
861 if (Arg.getKind() == TemplateArgument::Integral)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000862 return DeduceNonTypeTemplateArgument(S, NTTP,
Mike Stump1eb44332009-09-09 15:08:12 +0000863 *Arg.getAsIntegral(),
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000864 Arg.getIntegralType(),
Douglas Gregor02024a92010-03-28 02:42:43 +0000865 /*ArrayBound=*/false,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000866 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000867 if (Arg.getKind() == TemplateArgument::Expression)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000868 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000869 Info, Deduced);
Douglas Gregor15755cb2009-11-13 23:45:44 +0000870 if (Arg.getKind() == TemplateArgument::Declaration)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000871 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
Douglas Gregor15755cb2009-11-13 23:45:44 +0000872 Info, Deduced);
873
Douglas Gregorf67875d2009-06-12 18:26:56 +0000874 Info.FirstArg = Param;
875 Info.SecondArg = Arg;
876 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000877 }
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Douglas Gregor199d9912009-06-05 00:53:49 +0000879 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000880 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000881 }
Anders Carlssond01b1da2009-06-15 17:04:53 +0000882 case TemplateArgument::Pack:
883 assert(0 && "FIXME: Implement!");
884 break;
Douglas Gregor199d9912009-06-05 00:53:49 +0000885 }
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Douglas Gregorf67875d2009-06-12 18:26:56 +0000887 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000888}
889
Mike Stump1eb44332009-09-09 15:08:12 +0000890static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000891DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000892 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000893 const TemplateArgumentList &ParamList,
894 const TemplateArgumentList &ArgList,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000895 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000896 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000897 assert(ParamList.size() == ArgList.size());
898 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000899 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000900 = DeduceTemplateArguments(S, TemplateParams,
Mike Stump1eb44332009-09-09 15:08:12 +0000901 ParamList[I], ArgList[I],
Douglas Gregorf67875d2009-06-12 18:26:56 +0000902 Info, Deduced))
903 return Result;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000904 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000905 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000906}
907
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000908/// \brief Determine whether two template arguments are the same.
Mike Stump1eb44332009-09-09 15:08:12 +0000909static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000910 const TemplateArgument &X,
911 const TemplateArgument &Y) {
912 if (X.getKind() != Y.getKind())
913 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000915 switch (X.getKind()) {
916 case TemplateArgument::Null:
917 assert(false && "Comparing NULL template argument");
918 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000920 case TemplateArgument::Type:
921 return Context.getCanonicalType(X.getAsType()) ==
922 Context.getCanonicalType(Y.getAsType());
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000924 case TemplateArgument::Declaration:
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000925 return X.getAsDecl()->getCanonicalDecl() ==
926 Y.getAsDecl()->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Douglas Gregor788cd062009-11-11 01:00:40 +0000928 case TemplateArgument::Template:
929 return Context.getCanonicalTemplateName(X.getAsTemplate())
930 .getAsVoidPointer() ==
931 Context.getCanonicalTemplateName(Y.getAsTemplate())
932 .getAsVoidPointer();
933
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000934 case TemplateArgument::Integral:
935 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Douglas Gregor788cd062009-11-11 01:00:40 +0000937 case TemplateArgument::Expression: {
938 llvm::FoldingSetNodeID XID, YID;
939 X.getAsExpr()->Profile(XID, Context, true);
940 Y.getAsExpr()->Profile(YID, Context, true);
941 return XID == YID;
942 }
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000944 case TemplateArgument::Pack:
945 if (X.pack_size() != Y.pack_size())
946 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000947
948 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
949 XPEnd = X.pack_end(),
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000950 YP = Y.pack_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000951 XP != XPEnd; ++XP, ++YP)
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000952 if (!isSameTemplateArg(Context, *XP, *YP))
953 return false;
954
955 return true;
956 }
957
958 return false;
959}
960
961/// \brief Helper function to build a TemplateParameter when we don't
962/// know its type statically.
963static TemplateParameter makeTemplateParameter(Decl *D) {
964 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
965 return TemplateParameter(TTP);
966 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
967 return TemplateParameter(NTTP);
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000969 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
970}
971
Douglas Gregor31dce8f2010-04-29 06:21:43 +0000972/// Complete template argument deduction for a class template partial
973/// specialization.
974static Sema::TemplateDeductionResult
975FinishTemplateArgumentDeduction(Sema &S,
976 ClassTemplatePartialSpecializationDecl *Partial,
977 const TemplateArgumentList &TemplateArgs,
978 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
979 Sema::TemplateDeductionInfo &Info) {
980 // Trap errors.
981 Sema::SFINAETrap Trap(S);
982
983 Sema::ContextRAII SavedContext(S, Partial);
984
985 // C++ [temp.deduct.type]p2:
986 // [...] or if any template argument remains neither deduced nor
987 // explicitly specified, template argument deduction fails.
988 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
989 Deduced.size());
990 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
991 if (Deduced[I].isNull()) {
992 Decl *Param
993 = const_cast<NamedDecl *>(
994 Partial->getTemplateParameters()->getParam(I));
995 Info.Param = makeTemplateParameter(Param);
996 return Sema::TDK_Incomplete;
997 }
998
999 Builder.Append(Deduced[I]);
1000 }
1001
1002 // Form the template argument list from the deduced template arguments.
1003 TemplateArgumentList *DeducedArgumentList
1004 = new (S.Context) TemplateArgumentList(S.Context, Builder,
1005 /*TakeArgs=*/true);
1006 Info.reset(DeducedArgumentList);
1007
1008 // Substitute the deduced template arguments into the template
1009 // arguments of the class template partial specialization, and
1010 // verify that the instantiated template arguments are both valid
1011 // and are equivalent to the template arguments originally provided
1012 // to the class template.
1013 // FIXME: Do we have to correct the types of deduced non-type template
1014 // arguments (in particular, integral non-type template arguments?).
1015 Sema::LocalInstantiationScope InstScope(S);
1016 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
1017 const TemplateArgumentLoc *PartialTemplateArgs
1018 = Partial->getTemplateArgsAsWritten();
1019 unsigned N = Partial->getNumTemplateArgsAsWritten();
1020
1021 // Note that we don't provide the langle and rangle locations.
1022 TemplateArgumentListInfo InstArgs;
1023
1024 for (unsigned I = 0; I != N; ++I) {
1025 Decl *Param = const_cast<NamedDecl *>(
1026 ClassTemplate->getTemplateParameters()->getParam(I));
1027 TemplateArgumentLoc InstArg;
1028 if (S.Subst(PartialTemplateArgs[I], InstArg,
1029 MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
1030 Info.Param = makeTemplateParameter(Param);
1031 Info.FirstArg = PartialTemplateArgs[I].getArgument();
1032 return Sema::TDK_SubstitutionFailure;
1033 }
1034 InstArgs.addArgument(InstArg);
1035 }
1036
1037 TemplateArgumentListBuilder ConvertedInstArgs(
1038 ClassTemplate->getTemplateParameters(), N);
1039
1040 if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
Douglas Gregorec20f462010-05-08 20:07:26 +00001041 InstArgs, false, ConvertedInstArgs))
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001042 return Sema::TDK_SubstitutionFailure;
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001043
1044 for (unsigned I = 0, E = ConvertedInstArgs.flatSize(); I != E; ++I) {
1045 TemplateArgument InstArg = ConvertedInstArgs.getFlatArguments()[I];
1046
1047 Decl *Param = const_cast<NamedDecl *>(
1048 ClassTemplate->getTemplateParameters()->getParam(I));
1049
1050 if (InstArg.getKind() == TemplateArgument::Expression) {
1051 // When the argument is an expression, check the expression result
1052 // against the actual template parameter to get down to the canonical
1053 // template argument.
1054 Expr *InstExpr = InstArg.getAsExpr();
1055 if (NonTypeTemplateParmDecl *NTTP
1056 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1057 if (S.CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
1058 Info.Param = makeTemplateParameter(Param);
1059 Info.FirstArg = Partial->getTemplateArgs()[I];
1060 return Sema::TDK_SubstitutionFailure;
1061 }
1062 }
1063 }
1064
1065 if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
1066 Info.Param = makeTemplateParameter(Param);
1067 Info.FirstArg = TemplateArgs[I];
1068 Info.SecondArg = InstArg;
1069 return Sema::TDK_NonDeducedMismatch;
1070 }
1071 }
1072
1073 if (Trap.hasErrorOccurred())
1074 return Sema::TDK_SubstitutionFailure;
1075
1076 return Sema::TDK_Success;
1077}
1078
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001079/// \brief Perform template argument deduction to determine whether
1080/// the given template arguments match the given class template
1081/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +00001082Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001083Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001084 const TemplateArgumentList &TemplateArgs,
1085 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001086 // C++ [temp.class.spec.match]p2:
1087 // A partial specialization matches a given actual template
1088 // argument list if the template arguments of the partial
1089 // specialization can be deduced from the actual template argument
1090 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +00001091 SFINAETrap Trap(*this);
Douglas Gregor02024a92010-03-28 02:42:43 +00001092 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001093 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +00001094 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001095 = ::DeduceTemplateArguments(*this,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001096 Partial->getTemplateParameters(),
Mike Stump1eb44332009-09-09 15:08:12 +00001097 Partial->getTemplateArgs(),
Douglas Gregorf67875d2009-06-12 18:26:56 +00001098 TemplateArgs, Info, Deduced))
1099 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +00001100
Douglas Gregor637a4092009-06-10 23:47:09 +00001101 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
1102 Deduced.data(), Deduced.size());
1103 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001104 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +00001105
Douglas Gregorbb260412009-06-14 08:02:22 +00001106 if (Trap.hasErrorOccurred())
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001107 return Sema::TDK_SubstitutionFailure;
1108
1109 return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
1110 Deduced, Info);
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001111}
Douglas Gregor031a5882009-06-13 00:26:55 +00001112
Douglas Gregor41128772009-06-26 23:27:24 +00001113/// \brief Determine whether the given type T is a simple-template-id type.
1114static bool isSimpleTemplateIdType(QualType T) {
Mike Stump1eb44332009-09-09 15:08:12 +00001115 if (const TemplateSpecializationType *Spec
John McCall183700f2009-09-21 23:43:11 +00001116 = T->getAs<TemplateSpecializationType>())
Douglas Gregor41128772009-06-26 23:27:24 +00001117 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Douglas Gregor41128772009-06-26 23:27:24 +00001119 return false;
1120}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001121
1122/// \brief Substitute the explicitly-provided template arguments into the
1123/// given function template according to C++ [temp.arg.explicit].
1124///
1125/// \param FunctionTemplate the function template into which the explicit
1126/// template arguments will be substituted.
1127///
Mike Stump1eb44332009-09-09 15:08:12 +00001128/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001129/// arguments.
1130///
Mike Stump1eb44332009-09-09 15:08:12 +00001131/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor83314aa2009-07-08 20:55:45 +00001132/// with the converted and checked explicit template arguments.
1133///
Mike Stump1eb44332009-09-09 15:08:12 +00001134/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor83314aa2009-07-08 20:55:45 +00001135/// parameters.
1136///
1137/// \param FunctionType if non-NULL, the result type of the function template
1138/// will also be instantiated and the pointed-to value will be updated with
1139/// the instantiated function type.
1140///
1141/// \param Info if substitution fails for any reason, this object will be
1142/// populated with more information about the failure.
1143///
1144/// \returns TDK_Success if substitution was successful, or some failure
1145/// condition.
1146Sema::TemplateDeductionResult
1147Sema::SubstituteExplicitTemplateArguments(
1148 FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001149 const TemplateArgumentListInfo &ExplicitTemplateArgs,
Douglas Gregor02024a92010-03-28 02:42:43 +00001150 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001151 llvm::SmallVectorImpl<QualType> &ParamTypes,
1152 QualType *FunctionType,
1153 TemplateDeductionInfo &Info) {
1154 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1155 TemplateParameterList *TemplateParams
1156 = FunctionTemplate->getTemplateParameters();
1157
John McCalld5532b62009-11-23 01:53:49 +00001158 if (ExplicitTemplateArgs.size() == 0) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001159 // No arguments to substitute; just copy over the parameter types and
1160 // fill in the function type.
1161 for (FunctionDecl::param_iterator P = Function->param_begin(),
1162 PEnd = Function->param_end();
1163 P != PEnd;
1164 ++P)
1165 ParamTypes.push_back((*P)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Douglas Gregor83314aa2009-07-08 20:55:45 +00001167 if (FunctionType)
1168 *FunctionType = Function->getType();
1169 return TDK_Success;
1170 }
Mike Stump1eb44332009-09-09 15:08:12 +00001171
Douglas Gregor83314aa2009-07-08 20:55:45 +00001172 // Substitution of the explicit template arguments into a function template
1173 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001174 SFINAETrap Trap(*this);
1175
Douglas Gregor83314aa2009-07-08 20:55:45 +00001176 // C++ [temp.arg.explicit]p3:
Mike Stump1eb44332009-09-09 15:08:12 +00001177 // Template arguments that are present shall be specified in the
1178 // declaration order of their corresponding template-parameters. The
Douglas Gregor83314aa2009-07-08 20:55:45 +00001179 // template argument list shall not specify more template-arguments than
Mike Stump1eb44332009-09-09 15:08:12 +00001180 // there are corresponding template-parameters.
1181 TemplateArgumentListBuilder Builder(TemplateParams,
John McCalld5532b62009-11-23 01:53:49 +00001182 ExplicitTemplateArgs.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001183
1184 // Enter a new template instantiation context where we check the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001185 // explicitly-specified template arguments against this function template,
1186 // and then substitute them into the function parameter types.
Mike Stump1eb44332009-09-09 15:08:12 +00001187 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001188 FunctionTemplate, Deduced.data(), Deduced.size(),
1189 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution);
1190 if (Inst)
1191 return TDK_InstantiationDepth;
Mike Stump1eb44332009-09-09 15:08:12 +00001192
John McCall96db3102010-04-29 01:18:58 +00001193 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
John McCallf5813822010-04-29 00:35:03 +00001194
Douglas Gregor83314aa2009-07-08 20:55:45 +00001195 if (CheckTemplateArgumentList(FunctionTemplate,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001196 SourceLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001197 ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001198 true,
Douglas Gregorf1a84452010-05-08 19:15:54 +00001199 Builder) || Trap.hasErrorOccurred()) {
Douglas Gregorfe52c912010-05-09 01:26:06 +00001200 unsigned Index = Builder.structuredSize();
1201 if (Index >= TemplateParams->size())
1202 Index = TemplateParams->size() - 1;
1203 Info.Param = makeTemplateParameter(TemplateParams->getParam(Index));
Douglas Gregor83314aa2009-07-08 20:55:45 +00001204 return TDK_InvalidExplicitArguments;
Douglas Gregorf1a84452010-05-08 19:15:54 +00001205 }
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Douglas Gregor83314aa2009-07-08 20:55:45 +00001207 // Form the template argument list from the explicitly-specified
1208 // template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001209 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor83314aa2009-07-08 20:55:45 +00001210 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1211 Info.reset(ExplicitArgumentList);
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Douglas Gregor83314aa2009-07-08 20:55:45 +00001213 // Instantiate the types of each of the function parameters given the
1214 // explicitly-specified template arguments.
1215 for (FunctionDecl::param_iterator P = Function->param_begin(),
1216 PEnd = Function->param_end();
1217 P != PEnd;
1218 ++P) {
Mike Stump1eb44332009-09-09 15:08:12 +00001219 QualType ParamType
1220 = SubstType((*P)->getType(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001221 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1222 (*P)->getLocation(), (*P)->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001223 if (ParamType.isNull() || Trap.hasErrorOccurred())
1224 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Douglas Gregor83314aa2009-07-08 20:55:45 +00001226 ParamTypes.push_back(ParamType);
1227 }
1228
1229 // If the caller wants a full function type back, instantiate the return
1230 // type and form that function type.
1231 if (FunctionType) {
1232 // FIXME: exception-specifications?
Mike Stump1eb44332009-09-09 15:08:12 +00001233 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001234 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001235 assert(Proto && "Function template does not have a prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001236
1237 QualType ResultType
Douglas Gregor357bbd02009-08-28 20:50:45 +00001238 = SubstType(Proto->getResultType(),
1239 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1240 Function->getTypeSpecStartLoc(),
1241 Function->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001242 if (ResultType.isNull() || Trap.hasErrorOccurred())
1243 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001244
1245 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001246 ParamTypes.data(), ParamTypes.size(),
1247 Proto->isVariadic(),
1248 Proto->getTypeQuals(),
1249 Function->getLocation(),
Eli Friedmanfa869542010-08-05 02:54:05 +00001250 Function->getDeclName(),
1251 Proto->getExtInfo());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001252 if (FunctionType->isNull() || Trap.hasErrorOccurred())
1253 return TDK_SubstitutionFailure;
1254 }
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Douglas Gregor83314aa2009-07-08 20:55:45 +00001256 // C++ [temp.arg.explicit]p2:
Mike Stump1eb44332009-09-09 15:08:12 +00001257 // Trailing template arguments that can be deduced (14.8.2) may be
1258 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001259 // template arguments can be deduced, they may all be omitted; in this
1260 // case, the empty template argument list <> itself may also be omitted.
1261 //
1262 // Take all of the explicitly-specified arguments and put them into the
Mike Stump1eb44332009-09-09 15:08:12 +00001263 // set of deduced template arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001264 Deduced.reserve(TemplateParams->size());
1265 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001266 Deduced.push_back(ExplicitArgumentList->get(I));
1267
Douglas Gregor83314aa2009-07-08 20:55:45 +00001268 return TDK_Success;
1269}
1270
Douglas Gregor02024a92010-03-28 02:42:43 +00001271/// \brief Allocate a TemplateArgumentLoc where all locations have
1272/// been initialized to the given location.
1273///
1274/// \param S The semantic analysis object.
1275///
1276/// \param The template argument we are producing template argument
1277/// location information for.
1278///
1279/// \param NTTPType For a declaration template argument, the type of
1280/// the non-type template parameter that corresponds to this template
1281/// argument.
1282///
1283/// \param Loc The source location to use for the resulting template
1284/// argument.
1285static TemplateArgumentLoc
1286getTrivialTemplateArgumentLoc(Sema &S,
1287 const TemplateArgument &Arg,
1288 QualType NTTPType,
1289 SourceLocation Loc) {
1290 switch (Arg.getKind()) {
1291 case TemplateArgument::Null:
1292 llvm_unreachable("Can't get a NULL template argument here");
1293 break;
1294
1295 case TemplateArgument::Type:
1296 return TemplateArgumentLoc(Arg,
1297 S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
1298
1299 case TemplateArgument::Declaration: {
1300 Expr *E
1301 = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
1302 .takeAs<Expr>();
1303 return TemplateArgumentLoc(TemplateArgument(E), E);
1304 }
1305
1306 case TemplateArgument::Integral: {
1307 Expr *E
1308 = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>();
1309 return TemplateArgumentLoc(TemplateArgument(E), E);
1310 }
1311
1312 case TemplateArgument::Template:
1313 return TemplateArgumentLoc(Arg, SourceRange(), Loc);
1314
1315 case TemplateArgument::Expression:
1316 return TemplateArgumentLoc(Arg, Arg.getAsExpr());
1317
1318 case TemplateArgument::Pack:
1319 llvm_unreachable("Template parameter packs are not yet supported");
1320 }
1321
1322 return TemplateArgumentLoc();
1323}
1324
Mike Stump1eb44332009-09-09 15:08:12 +00001325/// \brief Finish template argument deduction for a function template,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001326/// checking the deduced template arguments for completeness and forming
1327/// the function template specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00001328Sema::TemplateDeductionResult
Douglas Gregor83314aa2009-07-08 20:55:45 +00001329Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor02024a92010-03-28 02:42:43 +00001330 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1331 unsigned NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001332 FunctionDecl *&Specialization,
1333 TemplateDeductionInfo &Info) {
1334 TemplateParameterList *TemplateParams
1335 = FunctionTemplate->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001336
Douglas Gregor83314aa2009-07-08 20:55:45 +00001337 // Template argument deduction for function templates in a SFINAE context.
1338 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001339 SFINAETrap Trap(*this);
1340
Douglas Gregor83314aa2009-07-08 20:55:45 +00001341 // Enter a new template instantiation context while we instantiate the
1342 // actual function declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001343 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001344 FunctionTemplate, Deduced.data(), Deduced.size(),
1345 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
1346 if (Inst)
Mike Stump1eb44332009-09-09 15:08:12 +00001347 return TDK_InstantiationDepth;
1348
John McCall96db3102010-04-29 01:18:58 +00001349 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
John McCallf5813822010-04-29 00:35:03 +00001350
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001351 // C++ [temp.deduct.type]p2:
1352 // [...] or if any template argument remains neither deduced nor
1353 // explicitly specified, template argument deduction fails.
1354 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
1355 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregor02024a92010-03-28 02:42:43 +00001356 NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001357 if (!Deduced[I].isNull()) {
Douglas Gregor02024a92010-03-28 02:42:43 +00001358 if (I < NumExplicitlySpecified ||
1359 Deduced[I].getKind() == TemplateArgument::Type) {
1360 // We have already fully type-checked and converted this
1361 // argument (because it was explicitly-specified) or no
1362 // additional checking is necessary (because it's a template
1363 // type parameter). Just record the presence of this
1364 // parameter.
1365 Builder.Append(Deduced[I]);
1366 continue;
1367 }
1368
1369 // We have deduced this argument, so it still needs to be
1370 // checked and converted.
1371
1372 // First, for a non-type template parameter type that is
1373 // initialized by a declaration, we need the type of the
1374 // corresponding non-type template parameter.
1375 QualType NTTPType;
1376 if (NonTypeTemplateParmDecl *NTTP
1377 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1378 if (Deduced[I].getKind() == TemplateArgument::Declaration) {
1379 NTTPType = NTTP->getType();
1380 if (NTTPType->isDependentType()) {
1381 TemplateArgumentList TemplateArgs(Context, Builder,
1382 /*TakeArgs=*/false);
1383 NTTPType = SubstType(NTTPType,
1384 MultiLevelTemplateArgumentList(TemplateArgs),
1385 NTTP->getLocation(),
1386 NTTP->getDeclName());
1387 if (NTTPType.isNull()) {
1388 Info.Param = makeTemplateParameter(Param);
Douglas Gregorec20f462010-05-08 20:07:26 +00001389 Info.reset(new (Context) TemplateArgumentList(Context, Builder,
1390 /*TakeArgs=*/true));
Douglas Gregor02024a92010-03-28 02:42:43 +00001391 return TDK_SubstitutionFailure;
1392 }
1393 }
1394 }
1395 }
1396
1397 // Convert the deduced template argument into a template
1398 // argument that we can check, almost as if the user had written
1399 // the template argument explicitly.
1400 TemplateArgumentLoc Arg = getTrivialTemplateArgumentLoc(*this,
1401 Deduced[I],
1402 NTTPType,
1403 SourceLocation());
1404
1405 // Check the template argument, converting it as necessary.
1406 if (CheckTemplateArgument(Param, Arg,
1407 FunctionTemplate,
1408 FunctionTemplate->getLocation(),
1409 FunctionTemplate->getSourceRange().getEnd(),
1410 Builder,
1411 Deduced[I].wasDeducedFromArrayBound()
1412 ? CTAK_DeducedFromArrayBound
1413 : CTAK_Deduced)) {
1414 Info.Param = makeTemplateParameter(
1415 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregorec20f462010-05-08 20:07:26 +00001416 Info.reset(new (Context) TemplateArgumentList(Context, Builder,
1417 /*TakeArgs=*/true));
Douglas Gregor02024a92010-03-28 02:42:43 +00001418 return TDK_SubstitutionFailure;
1419 }
1420
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001421 continue;
1422 }
1423
1424 // Substitute into the default template argument, if available.
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001425 TemplateArgumentLoc DefArg
1426 = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
1427 FunctionTemplate->getLocation(),
1428 FunctionTemplate->getSourceRange().getEnd(),
1429 Param,
1430 Builder);
1431
1432 // If there was no default argument, deduction is incomplete.
1433 if (DefArg.getArgument().isNull()) {
1434 Info.Param = makeTemplateParameter(
1435 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1436 return TDK_Incomplete;
1437 }
1438
1439 // Check whether we can actually use the default argument.
1440 if (CheckTemplateArgument(Param, DefArg,
1441 FunctionTemplate,
1442 FunctionTemplate->getLocation(),
1443 FunctionTemplate->getSourceRange().getEnd(),
Douglas Gregor02024a92010-03-28 02:42:43 +00001444 Builder,
1445 CTAK_Deduced)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001446 Info.Param = makeTemplateParameter(
1447 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregorec20f462010-05-08 20:07:26 +00001448 Info.reset(new (Context) TemplateArgumentList(Context, Builder,
1449 /*TakeArgs=*/true));
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001450 return TDK_SubstitutionFailure;
1451 }
1452
1453 // If we get here, we successfully used the default template argument.
1454 }
1455
1456 // Form the template argument list from the deduced template arguments.
1457 TemplateArgumentList *DeducedArgumentList
1458 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1459 Info.reset(DeducedArgumentList);
1460
Mike Stump1eb44332009-09-09 15:08:12 +00001461 // Substitute the deduced template arguments into the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001462 // declaration to produce the function template specialization.
Douglas Gregord4598a22010-04-28 04:52:24 +00001463 DeclContext *Owner = FunctionTemplate->getDeclContext();
1464 if (FunctionTemplate->getFriendObjectKind())
1465 Owner = FunctionTemplate->getLexicalDeclContext();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001466 Specialization = cast_or_null<FunctionDecl>(
Douglas Gregord4598a22010-04-28 04:52:24 +00001467 SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner,
Douglas Gregor357bbd02009-08-28 20:50:45 +00001468 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor83314aa2009-07-08 20:55:45 +00001469 if (!Specialization)
1470 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Douglas Gregorf8825742009-09-15 18:26:13 +00001472 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
1473 FunctionTemplate->getCanonicalDecl());
1474
Mike Stump1eb44332009-09-09 15:08:12 +00001475 // If the template argument list is owned by the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001476 // specialization, release it.
Douglas Gregorec20f462010-05-08 20:07:26 +00001477 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList &&
1478 !Trap.hasErrorOccurred())
Douglas Gregor83314aa2009-07-08 20:55:45 +00001479 Info.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001480
Douglas Gregor83314aa2009-07-08 20:55:45 +00001481 // There may have been an error that did not prevent us from constructing a
1482 // declaration. Mark the declaration invalid and return with a substitution
1483 // failure.
1484 if (Trap.hasErrorOccurred()) {
1485 Specialization->setInvalidDecl(true);
1486 return TDK_SubstitutionFailure;
1487 }
Mike Stump1eb44332009-09-09 15:08:12 +00001488
1489 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001490}
1491
John McCalleff92132010-02-02 02:21:27 +00001492static QualType GetTypeOfFunction(ASTContext &Context,
1493 bool isAddressOfOperand,
1494 FunctionDecl *Fn) {
1495 if (!isAddressOfOperand) return Fn->getType();
1496 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
1497 if (Method->isInstance())
1498 return Context.getMemberPointerType(Fn->getType(),
1499 Context.getTypeDeclType(Method->getParent()).getTypePtr());
1500 return Context.getPointerType(Fn->getType());
1501}
1502
1503/// Apply the deduction rules for overload sets.
1504///
1505/// \return the null type if this argument should be treated as an
1506/// undeduced context
1507static QualType
1508ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
1509 Expr *Arg, QualType ParamType) {
John McCall7bb12da2010-02-02 06:20:04 +00001510 llvm::PointerIntPair<OverloadExpr*,1> R = OverloadExpr::find(Arg);
John McCalleff92132010-02-02 02:21:27 +00001511
John McCall7bb12da2010-02-02 06:20:04 +00001512 bool isAddressOfOperand = bool(R.getInt());
1513 OverloadExpr *Ovl = R.getPointer();
John McCalleff92132010-02-02 02:21:27 +00001514
1515 // If there were explicit template arguments, we can only find
1516 // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
1517 // unambiguously name a full specialization.
John McCall7bb12da2010-02-02 06:20:04 +00001518 if (Ovl->hasExplicitTemplateArgs()) {
John McCalleff92132010-02-02 02:21:27 +00001519 // But we can still look for an explicit specialization.
1520 if (FunctionDecl *ExplicitSpec
John McCall7bb12da2010-02-02 06:20:04 +00001521 = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
1522 return GetTypeOfFunction(S.Context, isAddressOfOperand, ExplicitSpec);
John McCalleff92132010-02-02 02:21:27 +00001523 return QualType();
1524 }
1525
1526 // C++0x [temp.deduct.call]p6:
1527 // When P is a function type, pointer to function type, or pointer
1528 // to member function type:
1529
1530 if (!ParamType->isFunctionType() &&
1531 !ParamType->isFunctionPointerType() &&
1532 !ParamType->isMemberFunctionPointerType())
1533 return QualType();
1534
1535 QualType Match;
John McCall7bb12da2010-02-02 06:20:04 +00001536 for (UnresolvedSetIterator I = Ovl->decls_begin(),
1537 E = Ovl->decls_end(); I != E; ++I) {
John McCalleff92132010-02-02 02:21:27 +00001538 NamedDecl *D = (*I)->getUnderlyingDecl();
1539
1540 // - If the argument is an overload set containing one or more
1541 // function templates, the parameter is treated as a
1542 // non-deduced context.
1543 if (isa<FunctionTemplateDecl>(D))
1544 return QualType();
1545
1546 FunctionDecl *Fn = cast<FunctionDecl>(D);
1547 QualType ArgType = GetTypeOfFunction(S.Context, isAddressOfOperand, Fn);
1548
1549 // - If the argument is an overload set (not containing function
1550 // templates), trial argument deduction is attempted using each
1551 // of the members of the set. If deduction succeeds for only one
1552 // of the overload set members, that member is used as the
1553 // argument value for the deduction. If deduction succeeds for
1554 // more than one member of the overload set the parameter is
1555 // treated as a non-deduced context.
1556
1557 // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
1558 // Type deduction is done independently for each P/A pair, and
1559 // the deduced template argument values are then combined.
1560 // So we do not reject deductions which were made elsewhere.
Douglas Gregor02024a92010-03-28 02:42:43 +00001561 llvm::SmallVector<DeducedTemplateArgument, 8>
1562 Deduced(TemplateParams->size());
John McCall5769d612010-02-08 23:07:23 +00001563 Sema::TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
John McCalleff92132010-02-02 02:21:27 +00001564 unsigned TDF = 0;
1565
1566 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001567 = DeduceTemplateArguments(S, TemplateParams,
John McCalleff92132010-02-02 02:21:27 +00001568 ParamType, ArgType,
1569 Info, Deduced, TDF);
1570 if (Result) continue;
1571 if (!Match.isNull()) return QualType();
1572 Match = ArgType;
1573 }
1574
1575 return Match;
1576}
1577
Douglas Gregore53060f2009-06-25 22:08:12 +00001578/// \brief Perform template argument deduction from a function call
1579/// (C++ [temp.deduct.call]).
1580///
1581/// \param FunctionTemplate the function template for which we are performing
1582/// template argument deduction.
1583///
Douglas Gregor48026d22010-01-11 18:40:55 +00001584/// \param ExplicitTemplateArguments the explicit template arguments provided
1585/// for this call.
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001586///
Douglas Gregore53060f2009-06-25 22:08:12 +00001587/// \param Args the function call arguments
1588///
1589/// \param NumArgs the number of arguments in Args
1590///
Douglas Gregor48026d22010-01-11 18:40:55 +00001591/// \param Name the name of the function being called. This is only significant
1592/// when the function template is a conversion function template, in which
1593/// case this routine will also perform template argument deduction based on
1594/// the function to which
1595///
Douglas Gregore53060f2009-06-25 22:08:12 +00001596/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001597/// this will be set to the function template specialization produced by
Douglas Gregore53060f2009-06-25 22:08:12 +00001598/// template argument deduction.
1599///
1600/// \param Info the argument will be updated to provide additional information
1601/// about template argument deduction.
1602///
1603/// \returns the result of template argument deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001604Sema::TemplateDeductionResult
1605Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor48026d22010-01-11 18:40:55 +00001606 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregore53060f2009-06-25 22:08:12 +00001607 Expr **Args, unsigned NumArgs,
1608 FunctionDecl *&Specialization,
1609 TemplateDeductionInfo &Info) {
1610 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001611
Douglas Gregore53060f2009-06-25 22:08:12 +00001612 // C++ [temp.deduct.call]p1:
1613 // Template argument deduction is done by comparing each function template
1614 // parameter type (call it P) with the type of the corresponding argument
1615 // of the call (call it A) as described below.
1616 unsigned CheckArgs = NumArgs;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001617 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregore53060f2009-06-25 22:08:12 +00001618 return TDK_TooFewArguments;
1619 else if (NumArgs > Function->getNumParams()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001620 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001621 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregore53060f2009-06-25 22:08:12 +00001622 if (!Proto->isVariadic())
1623 return TDK_TooManyArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001624
Douglas Gregore53060f2009-06-25 22:08:12 +00001625 CheckArgs = Function->getNumParams();
1626 }
Mike Stump1eb44332009-09-09 15:08:12 +00001627
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001628 // The types of the parameters from which we will perform template argument
1629 // deduction.
Douglas Gregor2b0749a42010-03-25 15:38:42 +00001630 Sema::LocalInstantiationScope InstScope(*this);
Douglas Gregore53060f2009-06-25 22:08:12 +00001631 TemplateParameterList *TemplateParams
1632 = FunctionTemplate->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00001633 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001634 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregor02024a92010-03-28 02:42:43 +00001635 unsigned NumExplicitlySpecified = 0;
John McCalld5532b62009-11-23 01:53:49 +00001636 if (ExplicitTemplateArgs) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001637 TemplateDeductionResult Result =
1638 SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001639 *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001640 Deduced,
1641 ParamTypes,
1642 0,
1643 Info);
1644 if (Result)
1645 return Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00001646
1647 NumExplicitlySpecified = Deduced.size();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001648 } else {
1649 // Just fill in the parameter types from the function declaration.
1650 for (unsigned I = 0; I != CheckArgs; ++I)
1651 ParamTypes.push_back(Function->getParamDecl(I)->getType());
1652 }
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001654 // Deduce template arguments from the function parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001655 Deduced.resize(TemplateParams->size());
Douglas Gregore53060f2009-06-25 22:08:12 +00001656 for (unsigned I = 0; I != CheckArgs; ++I) {
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001657 QualType ParamType = ParamTypes[I];
Douglas Gregore53060f2009-06-25 22:08:12 +00001658 QualType ArgType = Args[I]->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001659
John McCalleff92132010-02-02 02:21:27 +00001660 // Overload sets usually make this parameter an undeduced
1661 // context, but there are sometimes special circumstances.
1662 if (ArgType == Context.OverloadTy) {
1663 ArgType = ResolveOverloadForDeduction(*this, TemplateParams,
1664 Args[I], ParamType);
1665 if (ArgType.isNull())
1666 continue;
1667 }
1668
Douglas Gregore53060f2009-06-25 22:08:12 +00001669 // C++ [temp.deduct.call]p2:
1670 // If P is not a reference type:
1671 QualType CanonParamType = Context.getCanonicalType(ParamType);
Douglas Gregor500d3312009-06-26 18:27:22 +00001672 bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1673 if (!ParamWasReference) {
Mike Stump1eb44332009-09-09 15:08:12 +00001674 // - If A is an array type, the pointer type produced by the
1675 // array-to-pointer standard conversion (4.2) is used in place of
Douglas Gregore53060f2009-06-25 22:08:12 +00001676 // A for type deduction; otherwise,
1677 if (ArgType->isArrayType())
1678 ArgType = Context.getArrayDecayedType(ArgType);
Mike Stump1eb44332009-09-09 15:08:12 +00001679 // - If A is a function type, the pointer type produced by the
1680 // function-to-pointer standard conversion (4.3) is used in place
Douglas Gregore53060f2009-06-25 22:08:12 +00001681 // of A for type deduction; otherwise,
1682 else if (ArgType->isFunctionType())
1683 ArgType = Context.getPointerType(ArgType);
1684 else {
1685 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1686 // type are ignored for type deduction.
1687 QualType CanonArgType = Context.getCanonicalType(ArgType);
Douglas Gregora4923eb2009-11-16 21:35:15 +00001688 if (CanonArgType.getLocalCVRQualifiers())
1689 ArgType = CanonArgType.getLocalUnqualifiedType();
Douglas Gregore53060f2009-06-25 22:08:12 +00001690 }
1691 }
Mike Stump1eb44332009-09-09 15:08:12 +00001692
Douglas Gregore53060f2009-06-25 22:08:12 +00001693 // C++0x [temp.deduct.call]p3:
1694 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
Mike Stump1eb44332009-09-09 15:08:12 +00001695 // are ignored for type deduction.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001696 if (CanonParamType.getLocalCVRQualifiers())
1697 ParamType = CanonParamType.getLocalUnqualifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001698 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001699 // [...] If P is a reference type, the type referred to by P is used
1700 // for type deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001701 ParamType = ParamRefType->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00001702
1703 // [...] If P is of the form T&&, where T is a template parameter, and
1704 // the argument is an lvalue, the type A& is used in place of A for
Douglas Gregore53060f2009-06-25 22:08:12 +00001705 // type deduction.
1706 if (isa<RValueReferenceType>(ParamRefType) &&
John McCall183700f2009-09-21 23:43:11 +00001707 ParamRefType->getAs<TemplateTypeParmType>() &&
Douglas Gregore53060f2009-06-25 22:08:12 +00001708 Args[I]->isLvalue(Context) == Expr::LV_Valid)
1709 ArgType = Context.getLValueReferenceType(ArgType);
1710 }
Mike Stump1eb44332009-09-09 15:08:12 +00001711
Douglas Gregore53060f2009-06-25 22:08:12 +00001712 // C++0x [temp.deduct.call]p4:
1713 // In general, the deduction process attempts to find template argument
1714 // values that will make the deduced A identical to A (after the type A
1715 // is transformed as described above). [...]
Douglas Gregor12820292009-09-14 20:00:47 +00001716 unsigned TDF = TDF_SkipNonDependent;
Mike Stump1eb44332009-09-09 15:08:12 +00001717
Douglas Gregor508f1c82009-06-26 23:10:12 +00001718 // - If the original P is a reference type, the deduced A (i.e., the
1719 // type referred to by the reference) can be more cv-qualified than
1720 // the transformed A.
1721 if (ParamWasReference)
1722 TDF |= TDF_ParamWithReferenceType;
Mike Stump1eb44332009-09-09 15:08:12 +00001723 // - The transformed A can be another pointer or pointer to member
1724 // type that can be converted to the deduced A via a qualification
Douglas Gregor508f1c82009-06-26 23:10:12 +00001725 // conversion (4.4).
John McCalldb0bc472010-08-05 05:30:45 +00001726 if (ArgType->isPointerType() || ArgType->isMemberPointerType() ||
1727 ArgType->isObjCObjectPointerType())
Douglas Gregor508f1c82009-06-26 23:10:12 +00001728 TDF |= TDF_IgnoreQualifiers;
Mike Stump1eb44332009-09-09 15:08:12 +00001729 // - If P is a class and P has the form simple-template-id, then the
Douglas Gregor41128772009-06-26 23:27:24 +00001730 // transformed A can be a derived class of the deduced A. Likewise,
1731 // if P is a pointer to a class of the form simple-template-id, the
1732 // transformed A can be a pointer to a derived class pointed to by
1733 // the deduced A.
1734 if (isSimpleTemplateIdType(ParamType) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001735 (isa<PointerType>(ParamType) &&
Douglas Gregor41128772009-06-26 23:27:24 +00001736 isSimpleTemplateIdType(
Ted Kremenek6217b802009-07-29 21:53:49 +00001737 ParamType->getAs<PointerType>()->getPointeeType())))
Douglas Gregor41128772009-06-26 23:27:24 +00001738 TDF |= TDF_DerivedClass;
Mike Stump1eb44332009-09-09 15:08:12 +00001739
Douglas Gregore53060f2009-06-25 22:08:12 +00001740 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001741 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor500d3312009-06-26 18:27:22 +00001742 ParamType, ArgType, Info, Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +00001743 TDF))
Douglas Gregore53060f2009-06-25 22:08:12 +00001744 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001745
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001746 // FIXME: we need to check that the deduced A is the same as A,
1747 // modulo the various allowed differences.
Douglas Gregore53060f2009-06-25 22:08:12 +00001748 }
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001749
Mike Stump1eb44332009-09-09 15:08:12 +00001750 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor02024a92010-03-28 02:42:43 +00001751 NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001752 Specialization, Info);
Douglas Gregore53060f2009-06-25 22:08:12 +00001753}
1754
Douglas Gregor83314aa2009-07-08 20:55:45 +00001755/// \brief Deduce template arguments when taking the address of a function
Douglas Gregor4b52e252009-12-21 23:17:24 +00001756/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
1757/// a template.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001758///
1759/// \param FunctionTemplate the function template for which we are performing
1760/// template argument deduction.
1761///
Douglas Gregor4b52e252009-12-21 23:17:24 +00001762/// \param ExplicitTemplateArguments the explicitly-specified template
1763/// arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001764///
1765/// \param ArgFunctionType the function type that will be used as the
1766/// "argument" type (A) when performing template argument deduction from the
Douglas Gregor4b52e252009-12-21 23:17:24 +00001767/// function template's function type. This type may be NULL, if there is no
1768/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001769///
1770/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001771/// this will be set to the function template specialization produced by
Douglas Gregor83314aa2009-07-08 20:55:45 +00001772/// template argument deduction.
1773///
1774/// \param Info the argument will be updated to provide additional information
1775/// about template argument deduction.
1776///
1777/// \returns the result of template argument deduction.
1778Sema::TemplateDeductionResult
1779Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001780 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001781 QualType ArgFunctionType,
1782 FunctionDecl *&Specialization,
1783 TemplateDeductionInfo &Info) {
1784 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1785 TemplateParameterList *TemplateParams
1786 = FunctionTemplate->getTemplateParameters();
1787 QualType FunctionType = Function->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001788
Douglas Gregor83314aa2009-07-08 20:55:45 +00001789 // Substitute any explicit template arguments.
Douglas Gregor2b0749a42010-03-25 15:38:42 +00001790 Sema::LocalInstantiationScope InstScope(*this);
Douglas Gregor02024a92010-03-28 02:42:43 +00001791 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
1792 unsigned NumExplicitlySpecified = 0;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001793 llvm::SmallVector<QualType, 4> ParamTypes;
John McCalld5532b62009-11-23 01:53:49 +00001794 if (ExplicitTemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001795 if (TemplateDeductionResult Result
1796 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001797 *ExplicitTemplateArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00001798 Deduced, ParamTypes,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001799 &FunctionType, Info))
1800 return Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00001801
1802 NumExplicitlySpecified = Deduced.size();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001803 }
1804
1805 // Template argument deduction for function templates in a SFINAE context.
1806 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001807 SFINAETrap Trap(*this);
1808
John McCalleff92132010-02-02 02:21:27 +00001809 Deduced.resize(TemplateParams->size());
1810
Douglas Gregor4b52e252009-12-21 23:17:24 +00001811 if (!ArgFunctionType.isNull()) {
1812 // Deduce template arguments from the function type.
Douglas Gregor4b52e252009-12-21 23:17:24 +00001813 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001814 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor4b52e252009-12-21 23:17:24 +00001815 FunctionType, ArgFunctionType, Info,
1816 Deduced, 0))
1817 return Result;
1818 }
1819
Mike Stump1eb44332009-09-09 15:08:12 +00001820 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor02024a92010-03-28 02:42:43 +00001821 NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001822 Specialization, Info);
1823}
1824
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001825/// \brief Deduce template arguments for a templated conversion
1826/// function (C++ [temp.deduct.conv]) and, if successful, produce a
1827/// conversion function template specialization.
1828Sema::TemplateDeductionResult
1829Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1830 QualType ToType,
1831 CXXConversionDecl *&Specialization,
1832 TemplateDeductionInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00001833 CXXConversionDecl *Conv
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001834 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
1835 QualType FromType = Conv->getConversionType();
1836
1837 // Canonicalize the types for deduction.
1838 QualType P = Context.getCanonicalType(FromType);
1839 QualType A = Context.getCanonicalType(ToType);
1840
1841 // C++0x [temp.deduct.conv]p3:
1842 // If P is a reference type, the type referred to by P is used for
1843 // type deduction.
1844 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
1845 P = PRef->getPointeeType();
1846
1847 // C++0x [temp.deduct.conv]p3:
1848 // If A is a reference type, the type referred to by A is used
1849 // for type deduction.
1850 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
1851 A = ARef->getPointeeType();
1852 // C++ [temp.deduct.conv]p2:
1853 //
Mike Stump1eb44332009-09-09 15:08:12 +00001854 // If A is not a reference type:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001855 else {
1856 assert(!A->isReferenceType() && "Reference types were handled above");
1857
1858 // - If P is an array type, the pointer type produced by the
Mike Stump1eb44332009-09-09 15:08:12 +00001859 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001860 // of P for type deduction; otherwise,
1861 if (P->isArrayType())
1862 P = Context.getArrayDecayedType(P);
1863 // - If P is a function type, the pointer type produced by the
1864 // function-to-pointer standard conversion (4.3) is used in
1865 // place of P for type deduction; otherwise,
1866 else if (P->isFunctionType())
1867 P = Context.getPointerType(P);
1868 // - If P is a cv-qualified type, the top level cv-qualifiers of
1869 // P’s type are ignored for type deduction.
1870 else
1871 P = P.getUnqualifiedType();
1872
1873 // C++0x [temp.deduct.conv]p3:
1874 // If A is a cv-qualified type, the top level cv-qualifiers of A’s
1875 // type are ignored for type deduction.
1876 A = A.getUnqualifiedType();
1877 }
1878
1879 // Template argument deduction for function templates in a SFINAE context.
1880 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001881 SFINAETrap Trap(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001882
1883 // C++ [temp.deduct.conv]p1:
1884 // Template argument deduction is done by comparing the return
1885 // type of the template conversion function (call it P) with the
1886 // type that is required as the result of the conversion (call it
1887 // A) as described in 14.8.2.4.
1888 TemplateParameterList *TemplateParams
1889 = FunctionTemplate->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00001890 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Mike Stump1eb44332009-09-09 15:08:12 +00001891 Deduced.resize(TemplateParams->size());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001892
1893 // C++0x [temp.deduct.conv]p4:
1894 // In general, the deduction process attempts to find template
1895 // argument values that will make the deduced A identical to
1896 // A. However, there are two cases that allow a difference:
1897 unsigned TDF = 0;
1898 // - If the original A is a reference type, A can be more
1899 // cv-qualified than the deduced A (i.e., the type referred to
1900 // by the reference)
1901 if (ToType->isReferenceType())
1902 TDF |= TDF_ParamWithReferenceType;
1903 // - The deduced A can be another pointer or pointer to member
1904 // type that can be converted to A via a qualification
1905 // conversion.
1906 //
1907 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
1908 // both P and A are pointers or member pointers. In this case, we
1909 // just ignore cv-qualifiers completely).
1910 if ((P->isPointerType() && A->isPointerType()) ||
1911 (P->isMemberPointerType() && P->isMemberPointerType()))
1912 TDF |= TDF_IgnoreQualifiers;
1913 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001914 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001915 P, A, Info, Deduced, TDF))
1916 return Result;
1917
1918 // FIXME: we need to check that the deduced A is the same as A,
1919 // modulo the various allowed differences.
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001921 // Finish template argument deduction.
Douglas Gregor2b0749a42010-03-25 15:38:42 +00001922 Sema::LocalInstantiationScope InstScope(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001923 FunctionDecl *Spec = 0;
1924 TemplateDeductionResult Result
Douglas Gregor02024a92010-03-28 02:42:43 +00001925 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec,
1926 Info);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001927 Specialization = cast_or_null<CXXConversionDecl>(Spec);
1928 return Result;
1929}
1930
Douglas Gregor4b52e252009-12-21 23:17:24 +00001931/// \brief Deduce template arguments for a function template when there is
1932/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
1933///
1934/// \param FunctionTemplate the function template for which we are performing
1935/// template argument deduction.
1936///
1937/// \param ExplicitTemplateArguments the explicitly-specified template
1938/// arguments.
1939///
1940/// \param Specialization if template argument deduction was successful,
1941/// this will be set to the function template specialization produced by
1942/// template argument deduction.
1943///
1944/// \param Info the argument will be updated to provide additional information
1945/// about template argument deduction.
1946///
1947/// \returns the result of template argument deduction.
1948Sema::TemplateDeductionResult
1949Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1950 const TemplateArgumentListInfo *ExplicitTemplateArgs,
1951 FunctionDecl *&Specialization,
1952 TemplateDeductionInfo &Info) {
1953 return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
1954 QualType(), Specialization, Info);
1955}
1956
Douglas Gregor8a514912009-09-14 18:39:43 +00001957/// \brief Stores the result of comparing the qualifiers of two types.
1958enum DeductionQualifierComparison {
1959 NeitherMoreQualified = 0,
1960 ParamMoreQualified,
1961 ArgMoreQualified
1962};
1963
1964/// \brief Deduce the template arguments during partial ordering by comparing
1965/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
1966///
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001967/// \param S the semantic analysis object within which we are deducing
Douglas Gregor8a514912009-09-14 18:39:43 +00001968///
1969/// \param TemplateParams the template parameters that we are deducing
1970///
1971/// \param ParamIn the parameter type
1972///
1973/// \param ArgIn the argument type
1974///
1975/// \param Info information about the template argument deduction itself
1976///
1977/// \param Deduced the deduced template arguments
1978///
1979/// \returns the result of template argument deduction so far. Note that a
1980/// "success" result means that template argument deduction has not yet failed,
1981/// but it may still fail, later, for other reasons.
1982static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001983DeduceTemplateArgumentsDuringPartialOrdering(Sema &S,
Douglas Gregor02024a92010-03-28 02:42:43 +00001984 TemplateParameterList *TemplateParams,
Douglas Gregor8a514912009-09-14 18:39:43 +00001985 QualType ParamIn, QualType ArgIn,
1986 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +00001987 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1988 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001989 CanQualType Param = S.Context.getCanonicalType(ParamIn);
1990 CanQualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor8a514912009-09-14 18:39:43 +00001991
1992 // C++0x [temp.deduct.partial]p5:
1993 // Before the partial ordering is done, certain transformations are
1994 // performed on the types used for partial ordering:
1995 // - If P is a reference type, P is replaced by the type referred to.
1996 CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00001997 if (!ParamRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00001998 Param = ParamRef->getPointeeType();
1999
2000 // - If A is a reference type, A is replaced by the type referred to.
2001 CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00002002 if (!ArgRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00002003 Arg = ArgRef->getPointeeType();
2004
John McCalle27ec8a2009-10-23 23:03:21 +00002005 if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
Douglas Gregor8a514912009-09-14 18:39:43 +00002006 // C++0x [temp.deduct.partial]p6:
2007 // If both P and A were reference types (before being replaced with the
2008 // type referred to above), determine which of the two types (if any) is
2009 // more cv-qualified than the other; otherwise the types are considered to
2010 // be equally cv-qualified for partial ordering purposes. The result of this
2011 // determination will be used below.
2012 //
2013 // We save this information for later, using it only when deduction
2014 // succeeds in both directions.
2015 DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
2016 if (Param.isMoreQualifiedThan(Arg))
2017 QualifierResult = ParamMoreQualified;
2018 else if (Arg.isMoreQualifiedThan(Param))
2019 QualifierResult = ArgMoreQualified;
2020 QualifierComparisons->push_back(QualifierResult);
2021 }
2022
2023 // C++0x [temp.deduct.partial]p7:
2024 // Remove any top-level cv-qualifiers:
2025 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
2026 // version of P.
2027 Param = Param.getUnqualifiedType();
2028 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
2029 // version of A.
2030 Arg = Arg.getUnqualifiedType();
2031
2032 // C++0x [temp.deduct.partial]p8:
2033 // Using the resulting types P and A the deduction is then done as
2034 // described in 14.9.2.5. If deduction succeeds for a given type, the type
2035 // from the argument template is considered to be at least as specialized
2036 // as the type from the parameter template.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002037 return DeduceTemplateArguments(S, TemplateParams, Param, Arg, Info,
Douglas Gregor8a514912009-09-14 18:39:43 +00002038 Deduced, TDF_None);
2039}
2040
2041static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002042MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2043 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002044 unsigned Level,
Douglas Gregore73bb602009-09-14 21:25:05 +00002045 llvm::SmallVectorImpl<bool> &Deduced);
Douglas Gregor8a514912009-09-14 18:39:43 +00002046
2047/// \brief Determine whether the function template \p FT1 is at least as
2048/// specialized as \p FT2.
2049static bool isAtLeastAsSpecializedAs(Sema &S,
John McCall5769d612010-02-08 23:07:23 +00002050 SourceLocation Loc,
Douglas Gregor8a514912009-09-14 18:39:43 +00002051 FunctionTemplateDecl *FT1,
2052 FunctionTemplateDecl *FT2,
2053 TemplatePartialOrderingContext TPOC,
2054 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
2055 FunctionDecl *FD1 = FT1->getTemplatedDecl();
2056 FunctionDecl *FD2 = FT2->getTemplatedDecl();
2057 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
2058 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
2059
2060 assert(Proto1 && Proto2 && "Function templates must have prototypes");
2061 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00002062 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor8a514912009-09-14 18:39:43 +00002063 Deduced.resize(TemplateParams->size());
2064
2065 // C++0x [temp.deduct.partial]p3:
2066 // The types used to determine the ordering depend on the context in which
2067 // the partial ordering is done:
John McCall5769d612010-02-08 23:07:23 +00002068 Sema::TemplateDeductionInfo Info(S.Context, Loc);
Douglas Gregor8a514912009-09-14 18:39:43 +00002069 switch (TPOC) {
2070 case TPOC_Call: {
2071 // - In the context of a function call, the function parameter types are
2072 // used.
2073 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
2074 for (unsigned I = 0; I != NumParams; ++I)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002075 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00002076 TemplateParams,
2077 Proto2->getArgType(I),
2078 Proto1->getArgType(I),
2079 Info,
2080 Deduced,
2081 QualifierComparisons))
2082 return false;
2083
2084 break;
2085 }
2086
2087 case TPOC_Conversion:
2088 // - In the context of a call to a conversion operator, the return types
2089 // of the conversion function templates are used.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002090 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00002091 TemplateParams,
2092 Proto2->getResultType(),
2093 Proto1->getResultType(),
2094 Info,
2095 Deduced,
2096 QualifierComparisons))
2097 return false;
2098 break;
2099
2100 case TPOC_Other:
2101 // - In other contexts (14.6.6.2) the function template’s function type
2102 // is used.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002103 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00002104 TemplateParams,
2105 FD2->getType(),
2106 FD1->getType(),
2107 Info,
2108 Deduced,
2109 QualifierComparisons))
2110 return false;
2111 break;
2112 }
2113
2114 // C++0x [temp.deduct.partial]p11:
2115 // In most cases, all template parameters must have values in order for
2116 // deduction to succeed, but for partial ordering purposes a template
2117 // parameter may remain without a value provided it is not used in the
2118 // types being used for partial ordering. [ Note: a template parameter used
2119 // in a non-deduced context is considered used. -end note]
2120 unsigned ArgIdx = 0, NumArgs = Deduced.size();
2121 for (; ArgIdx != NumArgs; ++ArgIdx)
2122 if (Deduced[ArgIdx].isNull())
2123 break;
2124
2125 if (ArgIdx == NumArgs) {
2126 // All template arguments were deduced. FT1 is at least as specialized
2127 // as FT2.
2128 return true;
2129 }
2130
Douglas Gregore73bb602009-09-14 21:25:05 +00002131 // Figure out which template parameters were used.
Douglas Gregor8a514912009-09-14 18:39:43 +00002132 llvm::SmallVector<bool, 4> UsedParameters;
2133 UsedParameters.resize(TemplateParams->size());
2134 switch (TPOC) {
2135 case TPOC_Call: {
2136 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
2137 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002138 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
2139 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00002140 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00002141 break;
2142 }
2143
2144 case TPOC_Conversion:
Douglas Gregored9c0f92009-10-29 00:04:11 +00002145 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
2146 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00002147 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00002148 break;
2149
2150 case TPOC_Other:
Douglas Gregored9c0f92009-10-29 00:04:11 +00002151 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
2152 TemplateParams->getDepth(),
2153 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00002154 break;
2155 }
2156
2157 for (; ArgIdx != NumArgs; ++ArgIdx)
2158 // If this argument had no value deduced but was used in one of the types
2159 // used for partial ordering, then deduction fails.
2160 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
2161 return false;
2162
2163 return true;
2164}
2165
2166
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002167/// \brief Returns the more specialized function template according
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002168/// to the rules of function template partial ordering (C++ [temp.func.order]).
2169///
2170/// \param FT1 the first function template
2171///
2172/// \param FT2 the second function template
2173///
Douglas Gregor8a514912009-09-14 18:39:43 +00002174/// \param TPOC the context in which we are performing partial ordering of
2175/// function templates.
Mike Stump1eb44332009-09-09 15:08:12 +00002176///
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002177/// \returns the more specialized function template. If neither
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002178/// template is more specialized, returns NULL.
2179FunctionTemplateDecl *
2180Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
2181 FunctionTemplateDecl *FT2,
John McCall5769d612010-02-08 23:07:23 +00002182 SourceLocation Loc,
Douglas Gregor8a514912009-09-14 18:39:43 +00002183 TemplatePartialOrderingContext TPOC) {
Douglas Gregor8a514912009-09-14 18:39:43 +00002184 llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
John McCall5769d612010-02-08 23:07:23 +00002185 bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, 0);
2186 bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
Douglas Gregor8a514912009-09-14 18:39:43 +00002187 &QualifierComparisons);
2188
2189 if (Better1 != Better2) // We have a clear winner
2190 return Better1? FT1 : FT2;
2191
2192 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002193 return 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00002194
2195
2196 // C++0x [temp.deduct.partial]p10:
2197 // If for each type being considered a given template is at least as
2198 // specialized for all types and more specialized for some set of types and
2199 // the other template is not more specialized for any types or is not at
2200 // least as specialized for any types, then the given template is more
2201 // specialized than the other template. Otherwise, neither template is more
2202 // specialized than the other.
2203 Better1 = false;
2204 Better2 = false;
2205 for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
2206 // C++0x [temp.deduct.partial]p9:
2207 // If, for a given type, deduction succeeds in both directions (i.e., the
2208 // types are identical after the transformations above) and if the type
2209 // from the argument template is more cv-qualified than the type from the
2210 // parameter template (as described above) that type is considered to be
2211 // more specialized than the other. If neither type is more cv-qualified
2212 // than the other then neither type is more specialized than the other.
2213 switch (QualifierComparisons[I]) {
2214 case NeitherMoreQualified:
2215 break;
2216
2217 case ParamMoreQualified:
2218 Better1 = true;
2219 if (Better2)
2220 return 0;
2221 break;
2222
2223 case ArgMoreQualified:
2224 Better2 = true;
2225 if (Better1)
2226 return 0;
2227 break;
2228 }
2229 }
2230
2231 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002232 if (Better1)
2233 return FT1;
Douglas Gregor8a514912009-09-14 18:39:43 +00002234 else if (Better2)
2235 return FT2;
2236 else
2237 return 0;
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002238}
Douglas Gregor83314aa2009-07-08 20:55:45 +00002239
Douglas Gregord5a423b2009-09-25 18:43:00 +00002240/// \brief Determine if the two templates are equivalent.
2241static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
2242 if (T1 == T2)
2243 return true;
2244
2245 if (!T1 || !T2)
2246 return false;
2247
2248 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
2249}
2250
2251/// \brief Retrieve the most specialized of the given function template
2252/// specializations.
2253///
John McCallc373d482010-01-27 01:50:18 +00002254/// \param SpecBegin the start iterator of the function template
2255/// specializations that we will be comparing.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002256///
John McCallc373d482010-01-27 01:50:18 +00002257/// \param SpecEnd the end iterator of the function template
2258/// specializations, paired with \p SpecBegin.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002259///
2260/// \param TPOC the partial ordering context to use to compare the function
2261/// template specializations.
2262///
2263/// \param Loc the location where the ambiguity or no-specializations
2264/// diagnostic should occur.
2265///
2266/// \param NoneDiag partial diagnostic used to diagnose cases where there are
2267/// no matching candidates.
2268///
2269/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
2270/// occurs.
2271///
2272/// \param CandidateDiag partial diagnostic used for each function template
2273/// specialization that is a candidate in the ambiguous ordering. One parameter
2274/// in this diagnostic should be unbound, which will correspond to the string
2275/// describing the template arguments for the function template specialization.
2276///
2277/// \param Index if non-NULL and the result of this function is non-nULL,
2278/// receives the index corresponding to the resulting function template
2279/// specialization.
2280///
2281/// \returns the most specialized function template specialization, if
John McCallc373d482010-01-27 01:50:18 +00002282/// found. Otherwise, returns SpecEnd.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002283///
2284/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
2285/// template argument deduction.
John McCallc373d482010-01-27 01:50:18 +00002286UnresolvedSetIterator
2287Sema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
2288 UnresolvedSetIterator SpecEnd,
2289 TemplatePartialOrderingContext TPOC,
2290 SourceLocation Loc,
2291 const PartialDiagnostic &NoneDiag,
2292 const PartialDiagnostic &AmbigDiag,
2293 const PartialDiagnostic &CandidateDiag) {
2294 if (SpecBegin == SpecEnd) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00002295 Diag(Loc, NoneDiag);
John McCallc373d482010-01-27 01:50:18 +00002296 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002297 }
2298
John McCallc373d482010-01-27 01:50:18 +00002299 if (SpecBegin + 1 == SpecEnd)
2300 return SpecBegin;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002301
2302 // Find the function template that is better than all of the templates it
2303 // has been compared to.
John McCallc373d482010-01-27 01:50:18 +00002304 UnresolvedSetIterator Best = SpecBegin;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002305 FunctionTemplateDecl *BestTemplate
John McCallc373d482010-01-27 01:50:18 +00002306 = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002307 assert(BestTemplate && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00002308 for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
2309 FunctionTemplateDecl *Challenger
2310 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002311 assert(Challenger && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00002312 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCall5769d612010-02-08 23:07:23 +00002313 Loc, TPOC),
Douglas Gregord5a423b2009-09-25 18:43:00 +00002314 Challenger)) {
2315 Best = I;
2316 BestTemplate = Challenger;
2317 }
2318 }
2319
2320 // Make sure that the "best" function template is more specialized than all
2321 // of the others.
2322 bool Ambiguous = false;
John McCallc373d482010-01-27 01:50:18 +00002323 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
2324 FunctionTemplateDecl *Challenger
2325 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002326 if (I != Best &&
2327 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCall5769d612010-02-08 23:07:23 +00002328 Loc, TPOC),
Douglas Gregord5a423b2009-09-25 18:43:00 +00002329 BestTemplate)) {
2330 Ambiguous = true;
2331 break;
2332 }
2333 }
2334
2335 if (!Ambiguous) {
2336 // We found an answer. Return it.
John McCallc373d482010-01-27 01:50:18 +00002337 return Best;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002338 }
2339
2340 // Diagnose the ambiguity.
2341 Diag(Loc, AmbigDiag);
2342
2343 // FIXME: Can we order the candidates in some sane way?
John McCallc373d482010-01-27 01:50:18 +00002344 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
2345 Diag((*I)->getLocation(), CandidateDiag)
Douglas Gregord5a423b2009-09-25 18:43:00 +00002346 << getTemplateArgumentBindingsText(
John McCallc373d482010-01-27 01:50:18 +00002347 cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
2348 *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
Douglas Gregord5a423b2009-09-25 18:43:00 +00002349
John McCallc373d482010-01-27 01:50:18 +00002350 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002351}
2352
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002353/// \brief Returns the more specialized class template partial specialization
2354/// according to the rules of partial ordering of class template partial
2355/// specializations (C++ [temp.class.order]).
2356///
2357/// \param PS1 the first class template partial specialization
2358///
2359/// \param PS2 the second class template partial specialization
2360///
2361/// \returns the more specialized class template partial specialization. If
2362/// neither partial specialization is more specialized, returns NULL.
2363ClassTemplatePartialSpecializationDecl *
2364Sema::getMoreSpecializedPartialSpecialization(
2365 ClassTemplatePartialSpecializationDecl *PS1,
John McCall5769d612010-02-08 23:07:23 +00002366 ClassTemplatePartialSpecializationDecl *PS2,
2367 SourceLocation Loc) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002368 // C++ [temp.class.order]p1:
2369 // For two class template partial specializations, the first is at least as
2370 // specialized as the second if, given the following rewrite to two
2371 // function templates, the first function template is at least as
2372 // specialized as the second according to the ordering rules for function
2373 // templates (14.6.6.2):
2374 // - the first function template has the same template parameters as the
2375 // first partial specialization and has a single function parameter
2376 // whose type is a class template specialization with the template
2377 // arguments of the first partial specialization, and
2378 // - the second function template has the same template parameters as the
2379 // second partial specialization and has a single function parameter
2380 // whose type is a class template specialization with the template
2381 // arguments of the second partial specialization.
2382 //
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002383 // Rather than synthesize function templates, we merely perform the
2384 // equivalent partial ordering by performing deduction directly on
2385 // the template arguments of the class template partial
2386 // specializations. This computation is slightly simpler than the
2387 // general problem of function template partial ordering, because
2388 // class template partial specializations are more constrained. We
2389 // know that every template parameter is deducible from the class
2390 // template partial specialization's template arguments, for
2391 // example.
Douglas Gregor02024a92010-03-28 02:42:43 +00002392 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
John McCall5769d612010-02-08 23:07:23 +00002393 Sema::TemplateDeductionInfo Info(Context, Loc);
John McCall31f17ec2010-04-27 00:57:59 +00002394
2395 QualType PT1 = PS1->getInjectedSpecializationType();
2396 QualType PT2 = PS2->getInjectedSpecializationType();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002397
2398 // Determine whether PS1 is at least as specialized as PS2
2399 Deduced.resize(PS2->getTemplateParameters()->size());
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002400 bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002401 PS2->getTemplateParameters(),
John McCall31f17ec2010-04-27 00:57:59 +00002402 PT2,
2403 PT1,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002404 Info,
2405 Deduced,
2406 0);
Douglas Gregor516e6e02010-04-29 06:31:36 +00002407 if (Better1)
2408 Better1 = !::FinishTemplateArgumentDeduction(*this, PS2,
2409 PS1->getTemplateArgs(),
2410 Deduced, Info);
2411
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002412 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregordb0d4b72009-11-11 23:06:43 +00002413 Deduced.clear();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002414 Deduced.resize(PS1->getTemplateParameters()->size());
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002415 bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002416 PS1->getTemplateParameters(),
John McCall31f17ec2010-04-27 00:57:59 +00002417 PT1,
2418 PT2,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002419 Info,
2420 Deduced,
2421 0);
Douglas Gregor516e6e02010-04-29 06:31:36 +00002422 if (Better2)
2423 Better2 = !::FinishTemplateArgumentDeduction(*this, PS1,
2424 PS2->getTemplateArgs(),
2425 Deduced, Info);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002426
2427 if (Better1 == Better2)
2428 return 0;
2429
2430 return Better1? PS1 : PS2;
2431}
2432
Mike Stump1eb44332009-09-09 15:08:12 +00002433static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002434MarkUsedTemplateParameters(Sema &SemaRef,
2435 const TemplateArgument &TemplateArg,
2436 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002437 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002438 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002439
Douglas Gregore73bb602009-09-14 21:25:05 +00002440/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002441/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00002442static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002443MarkUsedTemplateParameters(Sema &SemaRef,
2444 const Expr *E,
2445 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002446 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002447 llvm::SmallVectorImpl<bool> &Used) {
2448 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2449 // find other occurrences of template parameters.
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002450 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregorc781f9c2010-01-14 18:13:22 +00002451 if (!DRE)
Douglas Gregor031a5882009-06-13 00:26:55 +00002452 return;
2453
Mike Stump1eb44332009-09-09 15:08:12 +00002454 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00002455 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2456 if (!NTTP)
2457 return;
2458
Douglas Gregored9c0f92009-10-29 00:04:11 +00002459 if (NTTP->getDepth() == Depth)
2460 Used[NTTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002461}
2462
Douglas Gregore73bb602009-09-14 21:25:05 +00002463/// \brief Mark the template parameters that are used by the given
2464/// nested name specifier.
2465static void
2466MarkUsedTemplateParameters(Sema &SemaRef,
2467 NestedNameSpecifier *NNS,
2468 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002469 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002470 llvm::SmallVectorImpl<bool> &Used) {
2471 if (!NNS)
2472 return;
2473
Douglas Gregored9c0f92009-10-29 00:04:11 +00002474 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2475 Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002476 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002477 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002478}
2479
2480/// \brief Mark the template parameters that are used by the given
2481/// template name.
2482static void
2483MarkUsedTemplateParameters(Sema &SemaRef,
2484 TemplateName Name,
2485 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002486 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002487 llvm::SmallVectorImpl<bool> &Used) {
2488 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2489 if (TemplateTemplateParmDecl *TTP
Douglas Gregored9c0f92009-10-29 00:04:11 +00002490 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2491 if (TTP->getDepth() == Depth)
2492 Used[TTP->getIndex()] = true;
2493 }
Douglas Gregore73bb602009-09-14 21:25:05 +00002494 return;
2495 }
2496
Douglas Gregor788cd062009-11-11 01:00:40 +00002497 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2498 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2499 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002500 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
Douglas Gregored9c0f92009-10-29 00:04:11 +00002501 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2502 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002503}
2504
2505/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002506/// type.
Mike Stump1eb44332009-09-09 15:08:12 +00002507static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002508MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2509 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002510 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002511 llvm::SmallVectorImpl<bool> &Used) {
2512 if (T.isNull())
2513 return;
2514
Douglas Gregor031a5882009-06-13 00:26:55 +00002515 // Non-dependent types have nothing deducible
2516 if (!T->isDependentType())
2517 return;
2518
2519 T = SemaRef.Context.getCanonicalType(T);
2520 switch (T->getTypeClass()) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002521 case Type::Pointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002522 MarkUsedTemplateParameters(SemaRef,
2523 cast<PointerType>(T)->getPointeeType(),
2524 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002525 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002526 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002527 break;
2528
2529 case Type::BlockPointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002530 MarkUsedTemplateParameters(SemaRef,
2531 cast<BlockPointerType>(T)->getPointeeType(),
2532 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002533 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002534 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002535 break;
2536
2537 case Type::LValueReference:
2538 case Type::RValueReference:
Douglas Gregore73bb602009-09-14 21:25:05 +00002539 MarkUsedTemplateParameters(SemaRef,
2540 cast<ReferenceType>(T)->getPointeeType(),
2541 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002542 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002543 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002544 break;
2545
2546 case Type::MemberPointer: {
2547 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore73bb602009-09-14 21:25:05 +00002548 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002549 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002550 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002551 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002552 break;
2553 }
2554
2555 case Type::DependentSizedArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002556 MarkUsedTemplateParameters(SemaRef,
2557 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002558 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002559 // Fall through to check the element type
2560
2561 case Type::ConstantArray:
2562 case Type::IncompleteArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002563 MarkUsedTemplateParameters(SemaRef,
2564 cast<ArrayType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002565 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002566 break;
2567
2568 case Type::Vector:
2569 case Type::ExtVector:
Douglas Gregore73bb602009-09-14 21:25:05 +00002570 MarkUsedTemplateParameters(SemaRef,
2571 cast<VectorType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002572 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002573 break;
2574
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002575 case Type::DependentSizedExtVector: {
2576 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002577 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002578 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002579 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002580 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002581 Depth, Used);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002582 break;
2583 }
2584
Douglas Gregor031a5882009-06-13 00:26:55 +00002585 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002586 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002587 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002588 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002589 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore73bb602009-09-14 21:25:05 +00002590 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002591 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002592 break;
2593 }
2594
Douglas Gregored9c0f92009-10-29 00:04:11 +00002595 case Type::TemplateTypeParm: {
2596 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2597 if (TTP->getDepth() == Depth)
2598 Used[TTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002599 break;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002600 }
Douglas Gregor031a5882009-06-13 00:26:55 +00002601
John McCall31f17ec2010-04-27 00:57:59 +00002602 case Type::InjectedClassName:
2603 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
2604 // fall through
2605
Douglas Gregor031a5882009-06-13 00:26:55 +00002606 case Type::TemplateSpecialization: {
Mike Stump1eb44332009-09-09 15:08:12 +00002607 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002608 = cast<TemplateSpecializationType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002609 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002610 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002611 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002612 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2613 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002614 break;
2615 }
2616
Douglas Gregore73bb602009-09-14 21:25:05 +00002617 case Type::Complex:
2618 if (!OnlyDeduced)
2619 MarkUsedTemplateParameters(SemaRef,
2620 cast<ComplexType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002621 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002622 break;
2623
Douglas Gregor4714c122010-03-31 17:34:00 +00002624 case Type::DependentName:
Douglas Gregore73bb602009-09-14 21:25:05 +00002625 if (!OnlyDeduced)
2626 MarkUsedTemplateParameters(SemaRef,
Douglas Gregor4714c122010-03-31 17:34:00 +00002627 cast<DependentNameType>(T)->getQualifier(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002628 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002629 break;
2630
John McCall33500952010-06-11 00:33:02 +00002631 case Type::DependentTemplateSpecialization: {
2632 const DependentTemplateSpecializationType *Spec
2633 = cast<DependentTemplateSpecializationType>(T);
2634 if (!OnlyDeduced)
2635 MarkUsedTemplateParameters(SemaRef, Spec->getQualifier(),
2636 OnlyDeduced, Depth, Used);
2637 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
2638 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2639 Used);
2640 break;
2641 }
2642
John McCallad5e7382010-03-01 23:49:17 +00002643 case Type::TypeOf:
2644 if (!OnlyDeduced)
2645 MarkUsedTemplateParameters(SemaRef,
2646 cast<TypeOfType>(T)->getUnderlyingType(),
2647 OnlyDeduced, Depth, Used);
2648 break;
2649
2650 case Type::TypeOfExpr:
2651 if (!OnlyDeduced)
2652 MarkUsedTemplateParameters(SemaRef,
2653 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
2654 OnlyDeduced, Depth, Used);
2655 break;
2656
2657 case Type::Decltype:
2658 if (!OnlyDeduced)
2659 MarkUsedTemplateParameters(SemaRef,
2660 cast<DecltypeType>(T)->getUnderlyingExpr(),
2661 OnlyDeduced, Depth, Used);
2662 break;
2663
Douglas Gregore73bb602009-09-14 21:25:05 +00002664 // None of these types have any template parameters in them.
Douglas Gregor031a5882009-06-13 00:26:55 +00002665 case Type::Builtin:
Douglas Gregor031a5882009-06-13 00:26:55 +00002666 case Type::VariableArray:
2667 case Type::FunctionNoProto:
2668 case Type::Record:
2669 case Type::Enum:
Douglas Gregor031a5882009-06-13 00:26:55 +00002670 case Type::ObjCInterface:
John McCallc12c5bb2010-05-15 11:32:37 +00002671 case Type::ObjCObject:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002672 case Type::ObjCObjectPointer:
John McCalled976492009-12-04 22:46:56 +00002673 case Type::UnresolvedUsing:
Douglas Gregor031a5882009-06-13 00:26:55 +00002674#define TYPE(Class, Base)
2675#define ABSTRACT_TYPE(Class, Base)
2676#define DEPENDENT_TYPE(Class, Base)
2677#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2678#include "clang/AST/TypeNodes.def"
2679 break;
2680 }
2681}
2682
Douglas Gregore73bb602009-09-14 21:25:05 +00002683/// \brief Mark the template parameters that are used by this
Douglas Gregor031a5882009-06-13 00:26:55 +00002684/// template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00002685static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002686MarkUsedTemplateParameters(Sema &SemaRef,
2687 const TemplateArgument &TemplateArg,
2688 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002689 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002690 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002691 switch (TemplateArg.getKind()) {
2692 case TemplateArgument::Null:
2693 case TemplateArgument::Integral:
Douglas Gregor788cd062009-11-11 01:00:40 +00002694 case TemplateArgument::Declaration:
Douglas Gregor031a5882009-06-13 00:26:55 +00002695 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002696
Douglas Gregor031a5882009-06-13 00:26:55 +00002697 case TemplateArgument::Type:
Douglas Gregore73bb602009-09-14 21:25:05 +00002698 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002699 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002700 break;
2701
Douglas Gregor788cd062009-11-11 01:00:40 +00002702 case TemplateArgument::Template:
2703 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2704 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002705 break;
2706
2707 case TemplateArgument::Expression:
Douglas Gregore73bb602009-09-14 21:25:05 +00002708 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002709 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002710 break;
Douglas Gregore73bb602009-09-14 21:25:05 +00002711
Anders Carlssond01b1da2009-06-15 17:04:53 +00002712 case TemplateArgument::Pack:
Douglas Gregore73bb602009-09-14 21:25:05 +00002713 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2714 PEnd = TemplateArg.pack_end();
2715 P != PEnd; ++P)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002716 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssond01b1da2009-06-15 17:04:53 +00002717 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00002718 }
2719}
2720
2721/// \brief Mark the template parameters can be deduced by the given
2722/// template argument list.
2723///
2724/// \param TemplateArgs the template argument list from which template
2725/// parameters will be deduced.
2726///
2727/// \param Deduced a bit vector whose elements will be set to \c true
2728/// to indicate when the corresponding template parameter will be
2729/// deduced.
Mike Stump1eb44332009-09-09 15:08:12 +00002730void
Douglas Gregore73bb602009-09-14 21:25:05 +00002731Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002732 bool OnlyDeduced, unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002733 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002734 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002735 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2736 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002737}
Douglas Gregor63f07c52009-09-18 23:21:38 +00002738
2739/// \brief Marks all of the template parameters that will be deduced by a
2740/// call to the given function template.
Douglas Gregor02024a92010-03-28 02:42:43 +00002741void
2742Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
2743 llvm::SmallVectorImpl<bool> &Deduced) {
Douglas Gregor63f07c52009-09-18 23:21:38 +00002744 TemplateParameterList *TemplateParams
2745 = FunctionTemplate->getTemplateParameters();
2746 Deduced.clear();
2747 Deduced.resize(TemplateParams->size());
2748
2749 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2750 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
2751 ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002752 true, TemplateParams->getDepth(), Deduced);
Douglas Gregor63f07c52009-09-18 23:21:38 +00002753}