blob: 1bd041d9eff8584d130871aa5688bdefe915f338 [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"
Douglas Gregor0b9247f2009-06-04 00:03:07 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/StmtVisitor.h"
17#include "clang/AST/Expr.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/Parse/DeclSpec.h"
Douglas Gregor8a514912009-09-14 18:39:43 +000020#include <algorithm>
Douglas Gregor508f1c82009-06-26 23:10:12 +000021
22namespace clang {
23 /// \brief Various flags that control template argument deduction.
24 ///
25 /// These flags can be bitwise-OR'd together.
26 enum TemplateDeductionFlags {
27 /// \brief No template argument deduction flags, which indicates the
28 /// strictest results for template argument deduction (as used for, e.g.,
29 /// matching class template partial specializations).
30 TDF_None = 0,
31 /// \brief Within template argument deduction from a function call, we are
32 /// matching with a parameter type for which the original parameter was
33 /// a reference.
34 TDF_ParamWithReferenceType = 0x1,
35 /// \brief Within template argument deduction from a function call, we
36 /// are matching in a case where we ignore cv-qualifiers.
37 TDF_IgnoreQualifiers = 0x02,
38 /// \brief Within template argument deduction from a function call,
39 /// we are matching in a case where we can perform template argument
Douglas Gregor41128772009-06-26 23:27:24 +000040 /// deduction from a template-id of a derived class of the argument type.
Douglas Gregor12820292009-09-14 20:00:47 +000041 TDF_DerivedClass = 0x04,
42 /// \brief Allow non-dependent types to differ, e.g., when performing
43 /// template argument deduction from a function call where conversions
44 /// may apply.
45 TDF_SkipNonDependent = 0x08
Douglas Gregor508f1c82009-06-26 23:10:12 +000046 };
47}
48
Douglas Gregor0b9247f2009-06-04 00:03:07 +000049using namespace clang;
50
Douglas Gregor9d0e4412010-03-26 05:50:28 +000051/// \brief Compare two APSInts, extending and switching the sign as
52/// necessary to compare their values regardless of underlying type.
53static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) {
54 if (Y.getBitWidth() > X.getBitWidth())
55 X.extend(Y.getBitWidth());
56 else if (Y.getBitWidth() < X.getBitWidth())
57 Y.extend(X.getBitWidth());
58
59 // If there is a signedness mismatch, correct it.
60 if (X.isSigned() != Y.isSigned()) {
61 // If the signed value is negative, then the values cannot be the same.
62 if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative()))
63 return false;
64
65 Y.setIsSigned(true);
66 X.setIsSigned(true);
67 }
68
69 return X == Y;
70}
71
Douglas Gregorf67875d2009-06-12 18:26:56 +000072static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +000073DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +000074 TemplateParameterList *TemplateParams,
75 const TemplateArgument &Param,
Douglas Gregord708c722009-06-09 16:35:58 +000076 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +000077 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +000078 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced);
Douglas Gregord708c722009-06-09 16:35:58 +000079
Douglas Gregor199d9912009-06-05 00:53:49 +000080/// \brief If the given expression is of a form that permits the deduction
81/// of a non-type template parameter, return the declaration of that
82/// non-type template parameter.
83static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
84 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
85 E = IC->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +000086
Douglas Gregor199d9912009-06-05 00:53:49 +000087 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
88 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +000089
Douglas Gregor199d9912009-06-05 00:53:49 +000090 return 0;
91}
92
Mike Stump1eb44332009-09-09 15:08:12 +000093/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +000094/// from the given constant.
Douglas Gregorf67875d2009-06-12 18:26:56 +000095static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +000096DeduceNonTypeTemplateArgument(Sema &S,
Mike Stump1eb44332009-09-09 15:08:12 +000097 NonTypeTemplateParmDecl *NTTP,
Douglas Gregor9d0e4412010-03-26 05:50:28 +000098 llvm::APSInt Value, QualType ValueType,
Douglas Gregor02024a92010-03-28 02:42:43 +000099 bool DeducedFromArrayBound,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000100 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000101 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +0000102 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000103 "Cannot deduce non-type template argument with depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Douglas Gregor199d9912009-06-05 00:53:49 +0000105 if (Deduced[NTTP->getIndex()].isNull()) {
Douglas Gregor02024a92010-03-28 02:42:43 +0000106 Deduced[NTTP->getIndex()] = DeducedTemplateArgument(Value, ValueType,
107 DeducedFromArrayBound);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000108 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000109 }
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000111 if (Deduced[NTTP->getIndex()].getKind() != TemplateArgument::Integral) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000112 Info.Param = NTTP;
113 Info.FirstArg = Deduced[NTTP->getIndex()];
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000114 Info.SecondArg = TemplateArgument(Value, ValueType);
115 return Sema::TDK_Inconsistent;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000116 }
117
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000118 // Extent the smaller of the two values.
119 llvm::APSInt PrevValue = *Deduced[NTTP->getIndex()].getAsIntegral();
120 if (!hasSameExtendedValue(PrevValue, Value)) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000121 Info.Param = NTTP;
122 Info.FirstArg = Deduced[NTTP->getIndex()];
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000123 Info.SecondArg = TemplateArgument(Value, ValueType);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000124 return Sema::TDK_Inconsistent;
125 }
126
Douglas Gregor02024a92010-03-28 02:42:43 +0000127 if (!DeducedFromArrayBound)
128 Deduced[NTTP->getIndex()].setDeducedFromArrayBound(false);
129
Douglas Gregorf67875d2009-06-12 18:26:56 +0000130 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000131}
132
Mike Stump1eb44332009-09-09 15:08:12 +0000133/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +0000134/// from the given type- or value-dependent expression.
135///
136/// \returns true if deduction succeeded, false otherwise.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000137static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000138DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000139 NonTypeTemplateParmDecl *NTTP,
140 Expr *Value,
141 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000142 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +0000143 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000144 "Cannot deduce non-type template argument with depth > 0");
145 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
146 "Expression template argument must be type- or value-dependent.");
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Douglas Gregor199d9912009-06-05 00:53:49 +0000148 if (Deduced[NTTP->getIndex()].isNull()) {
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000149 Deduced[NTTP->getIndex()] = TemplateArgument(Value->Retain());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000150 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000151 }
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Douglas Gregor199d9912009-06-05 00:53:49 +0000153 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
Mike Stump1eb44332009-09-09 15:08:12 +0000154 // Okay, we deduced a constant in one case and a dependent expression
155 // in another case. FIXME: Later, we will check that instantiating the
Douglas Gregor199d9912009-06-05 00:53:49 +0000156 // dependent expression gives us the constant value.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000157 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000158 }
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Douglas Gregor9eea08b2009-09-15 16:51:42 +0000160 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
161 // Compare the expressions for equality
162 llvm::FoldingSetNodeID ID1, ID2;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000163 Deduced[NTTP->getIndex()].getAsExpr()->Profile(ID1, S.Context, true);
164 Value->Profile(ID2, S.Context, true);
Douglas Gregor9eea08b2009-09-15 16:51:42 +0000165 if (ID1 == ID2)
166 return Sema::TDK_Success;
167
168 // FIXME: Fill in argument mismatch information
169 return Sema::TDK_NonDeducedMismatch;
170 }
171
Douglas Gregorf67875d2009-06-12 18:26:56 +0000172 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000173}
174
Douglas Gregor15755cb2009-11-13 23:45:44 +0000175/// \brief Deduce the value of the given non-type template parameter
176/// from the given declaration.
177///
178/// \returns true if deduction succeeded, false otherwise.
179static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000180DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregor15755cb2009-11-13 23:45:44 +0000181 NonTypeTemplateParmDecl *NTTP,
182 Decl *D,
183 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000184 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor15755cb2009-11-13 23:45:44 +0000185 assert(NTTP->getDepth() == 0 &&
186 "Cannot deduce non-type template argument with depth > 0");
187
188 if (Deduced[NTTP->getIndex()].isNull()) {
189 Deduced[NTTP->getIndex()] = TemplateArgument(D->getCanonicalDecl());
190 return Sema::TDK_Success;
191 }
192
193 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
194 // Okay, we deduced a declaration in one case and a dependent expression
195 // in another case.
196 return Sema::TDK_Success;
197 }
198
199 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Declaration) {
200 // Compare the declarations for equality
201 if (Deduced[NTTP->getIndex()].getAsDecl()->getCanonicalDecl() ==
202 D->getCanonicalDecl())
203 return Sema::TDK_Success;
204
205 // FIXME: Fill in argument mismatch information
206 return Sema::TDK_NonDeducedMismatch;
207 }
208
209 return Sema::TDK_Success;
210}
211
Douglas Gregorf67875d2009-06-12 18:26:56 +0000212static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000213DeduceTemplateArguments(Sema &S,
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000214 TemplateParameterList *TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000215 TemplateName Param,
216 TemplateName Arg,
217 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000218 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregord708c722009-06-09 16:35:58 +0000219 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000220 if (!ParamDecl) {
221 // The parameter type is dependent and is not a template template parameter,
222 // so there is nothing that we can deduce.
223 return Sema::TDK_Success;
224 }
225
226 if (TemplateTemplateParmDecl *TempParam
227 = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) {
228 // Bind the template template parameter to the given template name.
229 TemplateArgument &ExistingArg = Deduced[TempParam->getIndex()];
230 if (ExistingArg.isNull()) {
231 // This is the first deduction for this template template parameter.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000232 ExistingArg = TemplateArgument(S.Context.getCanonicalTemplateName(Arg));
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000233 return Sema::TDK_Success;
234 }
235
236 // Verify that the previous binding matches this deduction.
237 assert(ExistingArg.getKind() == TemplateArgument::Template);
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000238 if (S.Context.hasSameTemplateName(ExistingArg.getAsTemplate(), Arg))
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000239 return Sema::TDK_Success;
240
241 // Inconsistent deduction.
242 Info.Param = TempParam;
243 Info.FirstArg = ExistingArg;
244 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000245 return Sema::TDK_Inconsistent;
246 }
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000247
248 // Verify that the two template names are equivalent.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000249 if (S.Context.hasSameTemplateName(Param, Arg))
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000250 return Sema::TDK_Success;
251
252 // Mismatch of non-dependent template parameter to argument.
253 Info.FirstArg = TemplateArgument(Param);
254 Info.SecondArg = TemplateArgument(Arg);
255 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000256}
257
Mike Stump1eb44332009-09-09 15:08:12 +0000258/// \brief Deduce the template arguments by comparing the template parameter
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000259/// type (which is a template-id) with the template argument type.
260///
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000261/// \param S the Sema
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000262///
263/// \param TemplateParams the template parameters that we are deducing
264///
265/// \param Param the parameter type
266///
267/// \param Arg the argument type
268///
269/// \param Info information about the template argument deduction itself
270///
271/// \param Deduced the deduced template arguments
272///
273/// \returns the result of template argument deduction so far. Note that a
274/// "success" result means that template argument deduction has not yet failed,
275/// but it may still fail, later, for other reasons.
276static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000277DeduceTemplateArguments(Sema &S,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000278 TemplateParameterList *TemplateParams,
279 const TemplateSpecializationType *Param,
280 QualType Arg,
281 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000282 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
John McCall467b27b2009-10-22 20:10:53 +0000283 assert(Arg.isCanonical() && "Argument type must be canonical");
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000285 // Check whether the template argument is a dependent template-id.
Mike Stump1eb44332009-09-09 15:08:12 +0000286 if (const TemplateSpecializationType *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000287 = dyn_cast<TemplateSpecializationType>(Arg)) {
288 // Perform template argument deduction for the template name.
289 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000290 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000291 Param->getTemplateName(),
292 SpecArg->getTemplateName(),
293 Info, Deduced))
294 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000297 // Perform template argument deduction on each template
298 // argument.
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000299 unsigned NumArgs = std::min(SpecArg->getNumArgs(), Param->getNumArgs());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000300 for (unsigned I = 0; I != NumArgs; ++I)
301 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000302 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000303 Param->getArg(I),
304 SpecArg->getArg(I),
305 Info, Deduced))
306 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000307
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000308 return Sema::TDK_Success;
309 }
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000311 // If the argument type is a class template specialization, we
312 // perform template argument deduction using its template
313 // arguments.
314 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
315 if (!RecordArg)
316 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000317
318 ClassTemplateSpecializationDecl *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000319 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
320 if (!SpecArg)
321 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000323 // Perform template argument deduction for the template name.
324 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000325 = DeduceTemplateArguments(S,
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000326 TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000327 Param->getTemplateName(),
328 TemplateName(SpecArg->getSpecializedTemplate()),
329 Info, Deduced))
330 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000332 unsigned NumArgs = Param->getNumArgs();
333 const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
334 if (NumArgs != ArgArgs.size())
335 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000337 for (unsigned I = 0; I != NumArgs; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000338 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000339 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000340 Param->getArg(I),
341 ArgArgs.get(I),
342 Info, Deduced))
343 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000345 return Sema::TDK_Success;
346}
347
Douglas Gregor500d3312009-06-26 18:27:22 +0000348/// \brief Deduce the template arguments by comparing the parameter type and
349/// the argument type (C++ [temp.deduct.type]).
350///
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000351/// \param S the semantic analysis object within which we are deducing
Douglas Gregor500d3312009-06-26 18:27:22 +0000352///
353/// \param TemplateParams the template parameters that we are deducing
354///
355/// \param ParamIn the parameter type
356///
357/// \param ArgIn the argument type
358///
359/// \param Info information about the template argument deduction itself
360///
361/// \param Deduced the deduced template arguments
362///
Douglas Gregor508f1c82009-06-26 23:10:12 +0000363/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
Mike Stump1eb44332009-09-09 15:08:12 +0000364/// how template argument deduction is performed.
Douglas Gregor500d3312009-06-26 18:27:22 +0000365///
366/// \returns the result of template argument deduction so far. Note that a
367/// "success" result means that template argument deduction has not yet failed,
368/// but it may still fail, later, for other reasons.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000369static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000370DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000371 TemplateParameterList *TemplateParams,
372 QualType ParamIn, QualType ArgIn,
373 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000374 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000375 unsigned TDF) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000376 // We only want to look at the canonical types, since typedefs and
377 // sugar are not part of template argument deduction.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000378 QualType Param = S.Context.getCanonicalType(ParamIn);
379 QualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000380
Douglas Gregor500d3312009-06-26 18:27:22 +0000381 // C++0x [temp.deduct.call]p4 bullet 1:
382 // - If the original P is a reference type, the deduced A (i.e., the type
Mike Stump1eb44332009-09-09 15:08:12 +0000383 // referred to by the reference) can be more cv-qualified than the
Douglas Gregor500d3312009-06-26 18:27:22 +0000384 // transformed A.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000385 if (TDF & TDF_ParamWithReferenceType) {
Chandler Carruthe7242462009-12-30 04:10:01 +0000386 Qualifiers Quals;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000387 QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals);
Chandler Carruthe7242462009-12-30 04:10:01 +0000388 Quals.setCVRQualifiers(Quals.getCVRQualifiers() &
389 Arg.getCVRQualifiersThroughArrayTypes());
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000390 Param = S.Context.getQualifiedType(UnqualParam, Quals);
Douglas Gregor500d3312009-06-26 18:27:22 +0000391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000393 // If the parameter type is not dependent, there is nothing to deduce.
Douglas Gregor12820292009-09-14 20:00:47 +0000394 if (!Param->isDependentType()) {
395 if (!(TDF & TDF_SkipNonDependent) && Param != Arg) {
396
397 return Sema::TDK_NonDeducedMismatch;
398 }
399
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000400 return Sema::TDK_Success;
Douglas Gregor12820292009-09-14 20:00:47 +0000401 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000402
Douglas Gregor199d9912009-06-05 00:53:49 +0000403 // C++ [temp.deduct.type]p9:
Mike Stump1eb44332009-09-09 15:08:12 +0000404 // A template type argument T, a template template argument TT or a
405 // template non-type argument i can be deduced if P and A have one of
Douglas Gregor199d9912009-06-05 00:53:49 +0000406 // the following forms:
407 //
408 // T
409 // cv-list T
Mike Stump1eb44332009-09-09 15:08:12 +0000410 if (const TemplateTypeParmType *TemplateTypeParm
John McCall183700f2009-09-21 23:43:11 +0000411 = Param->getAs<TemplateTypeParmType>()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000412 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000413 bool RecanonicalizeArg = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Douglas Gregor9e9fae42009-07-22 20:02:25 +0000415 // If the argument type is an array type, move the qualifiers up to the
416 // top level, so they can be matched with the qualifiers on the parameter.
417 // FIXME: address spaces, ObjC GC qualifiers
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000418 if (isa<ArrayType>(Arg)) {
John McCall0953e762009-09-24 19:53:00 +0000419 Qualifiers Quals;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000420 Arg = S.Context.getUnqualifiedArrayType(Arg, Quals);
John McCall0953e762009-09-24 19:53:00 +0000421 if (Quals) {
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000422 Arg = S.Context.getQualifiedType(Arg, Quals);
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000423 RecanonicalizeArg = true;
424 }
425 }
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000427 // The argument type can not be less qualified than the parameter
428 // type.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000429 if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000430 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
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
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000533 return DeduceTemplateArguments(S, TemplateParams,
534 S.Context.getAsIncompleteArrayType(Param)->getElementType(),
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000535 IncompleteArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000536 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000537 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000538
539 // T [integer-constant]
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000540 case Type::ConstantArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000541 const ConstantArrayType *ConstantArrayArg =
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000542 S.Context.getAsConstantArrayType(Arg);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000543 if (!ConstantArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000544 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000545
546 const ConstantArrayType *ConstantArrayParm =
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000547 S.Context.getAsConstantArrayType(Param);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000548 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000549 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000551 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000552 ConstantArrayParm->getElementType(),
553 ConstantArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000554 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000555 }
556
Douglas Gregor199d9912009-06-05 00:53:49 +0000557 // type [i]
558 case Type::DependentSizedArray: {
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000559 const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
Douglas Gregor199d9912009-06-05 00:53:49 +0000560 if (!ArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000561 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Douglas Gregor199d9912009-06-05 00:53:49 +0000563 // Check the element type of the arrays
564 const DependentSizedArrayType *DependentArrayParm
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000565 = S.Context.getAsDependentSizedArrayType(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000566 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000567 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000568 DependentArrayParm->getElementType(),
569 ArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000570 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000571 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Douglas Gregor199d9912009-06-05 00:53:49 +0000573 // Determine the array bound is something we can deduce.
Mike Stump1eb44332009-09-09 15:08:12 +0000574 NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000575 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
576 if (!NTTP)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000577 return Sema::TDK_Success;
Mike Stump1eb44332009-09-09 15:08:12 +0000578
579 // We can perform template argument deduction for the given non-type
Douglas Gregor199d9912009-06-05 00:53:49 +0000580 // template parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000581 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000582 "Cannot deduce non-type template argument at depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000583 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson335e24a2009-06-16 22:44:31 +0000584 = dyn_cast<ConstantArrayType>(ArrayArg)) {
585 llvm::APSInt Size(ConstantArrayArg->getSize());
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000586 return DeduceNonTypeTemplateArgument(S, NTTP, Size,
587 S.Context.getSizeType(),
Douglas Gregor02024a92010-03-28 02:42:43 +0000588 /*ArrayBound=*/true,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000589 Info, Deduced);
Anders Carlsson335e24a2009-06-16 22:44:31 +0000590 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000591 if (const DependentSizedArrayType *DependentArrayArg
592 = dyn_cast<DependentSizedArrayType>(ArrayArg))
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000593 return DeduceNonTypeTemplateArgument(S, NTTP,
Douglas Gregor199d9912009-06-05 00:53:49 +0000594 DependentArrayArg->getSizeExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000595 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Douglas Gregor199d9912009-06-05 00:53:49 +0000597 // Incomplete type does not match a dependently-sized array type
Douglas Gregorf67875d2009-06-12 18:26:56 +0000598 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000599 }
Mike Stump1eb44332009-09-09 15:08:12 +0000600
601 // type(*)(T)
602 // T(*)()
603 // T(*)(T)
Anders Carlssona27fad52009-06-08 15:19:08 +0000604 case Type::FunctionProto: {
Mike Stump1eb44332009-09-09 15:08:12 +0000605 const FunctionProtoType *FunctionProtoArg =
Anders Carlssona27fad52009-06-08 15:19:08 +0000606 dyn_cast<FunctionProtoType>(Arg);
607 if (!FunctionProtoArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000608 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000609
610 const FunctionProtoType *FunctionProtoParam =
Anders Carlssona27fad52009-06-08 15:19:08 +0000611 cast<FunctionProtoType>(Param);
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000612
Mike Stump1eb44332009-09-09 15:08:12 +0000613 if (FunctionProtoParam->getTypeQuals() !=
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000614 FunctionProtoArg->getTypeQuals())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000615 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000617 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000618 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000620 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000621 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000622
Anders Carlssona27fad52009-06-08 15:19:08 +0000623 // Check return types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000624 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000625 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000626 FunctionProtoParam->getResultType(),
627 FunctionProtoArg->getResultType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000628 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000629 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Anders Carlssona27fad52009-06-08 15:19:08 +0000631 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
632 // Check argument types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000633 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000634 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000635 FunctionProtoParam->getArgType(I),
636 FunctionProtoArg->getArgType(I),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000637 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000638 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000639 }
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Douglas Gregorf67875d2009-06-12 18:26:56 +0000641 return Sema::TDK_Success;
Anders Carlssona27fad52009-06-08 15:19:08 +0000642 }
Mike Stump1eb44332009-09-09 15:08:12 +0000643
John McCall3cb0ebd2010-03-10 03:28:59 +0000644 case Type::InjectedClassName: {
645 // Treat a template's injected-class-name as if the template
646 // specialization type had been used.
John McCall31f17ec2010-04-27 00:57:59 +0000647 Param = cast<InjectedClassNameType>(Param)
648 ->getInjectedSpecializationType();
John McCall3cb0ebd2010-03-10 03:28:59 +0000649 assert(isa<TemplateSpecializationType>(Param) &&
650 "injected class name is not a template specialization type");
651 // fall through
652 }
653
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000654 // template-name<T> (where template-name refers to a class template)
Douglas Gregord708c722009-06-09 16:35:58 +0000655 // template-name<i>
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000656 // TT<T>
657 // TT<i>
658 // TT<>
Douglas Gregord708c722009-06-09 16:35:58 +0000659 case Type::TemplateSpecialization: {
660 const TemplateSpecializationType *SpecParam
661 = cast<TemplateSpecializationType>(Param);
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000663 // Try to deduce template arguments from the template-id.
664 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000665 = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000666 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Douglas Gregor4a5c15f2009-09-30 22:13:51 +0000668 if (Result && (TDF & TDF_DerivedClass)) {
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000669 // C++ [temp.deduct.call]p3b3:
670 // If P is a class, and P has the form template-id, then A can be a
671 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump1eb44332009-09-09 15:08:12 +0000672 // class of the form template-id, A can be a pointer to a derived
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000673 // class pointed to by the deduced A.
674 //
675 // More importantly:
Mike Stump1eb44332009-09-09 15:08:12 +0000676 // These alternatives are considered only if type deduction would
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000677 // otherwise fail.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000678 if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
679 // We cannot inspect base classes as part of deduction when the type
680 // is incomplete, so either instantiate any templates necessary to
681 // complete the type, or skip over it if it cannot be completed.
John McCall5769d612010-02-08 23:07:23 +0000682 if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000683 return Result;
684
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000685 // Use data recursion to crawl through the list of base classes.
Mike Stump1eb44332009-09-09 15:08:12 +0000686 // Visited contains the set of nodes we have already visited, while
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000687 // ToVisit is our stack of records that we still need to visit.
688 llvm::SmallPtrSet<const RecordType *, 8> Visited;
689 llvm::SmallVector<const RecordType *, 8> ToVisit;
690 ToVisit.push_back(RecordT);
691 bool Successful = false;
692 while (!ToVisit.empty()) {
693 // Retrieve the next class in the inheritance hierarchy.
694 const RecordType *NextT = ToVisit.back();
695 ToVisit.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000697 // If we have already seen this type, skip it.
698 if (!Visited.insert(NextT))
699 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000701 // If this is a base class, try to perform template argument
702 // deduction from it.
703 if (NextT != RecordT) {
704 Sema::TemplateDeductionResult BaseResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000705 = DeduceTemplateArguments(S, TemplateParams, SpecParam,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000706 QualType(NextT, 0), Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000708 // If template argument deduction for this base was successful,
709 // note that we had some success.
710 if (BaseResult == Sema::TDK_Success)
711 Successful = true;
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000712 }
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000714 // Visit base classes
715 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
716 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
717 BaseEnd = Next->bases_end();
Sebastian Redl9994a342009-10-25 17:03:50 +0000718 Base != BaseEnd; ++Base) {
Mike Stump1eb44332009-09-09 15:08:12 +0000719 assert(Base->getType()->isRecordType() &&
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000720 "Base class that isn't a record?");
Ted Kremenek6217b802009-07-29 21:53:49 +0000721 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000722 }
723 }
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000725 if (Successful)
726 return Sema::TDK_Success;
727 }
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000729 }
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000731 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000732 }
733
Douglas Gregor637a4092009-06-10 23:47:09 +0000734 // T type::*
735 // T T::*
736 // T (type::*)()
737 // type (T::*)()
738 // type (type::*)(T)
739 // type (T::*)(T)
740 // T (type::*)(T)
741 // T (T::*)()
742 // T (T::*)(T)
743 case Type::MemberPointer: {
744 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
745 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
746 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000747 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +0000748
Douglas Gregorf67875d2009-06-12 18:26:56 +0000749 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000750 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000751 MemPtrParam->getPointeeType(),
752 MemPtrArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000753 Info, Deduced,
754 TDF & TDF_IgnoreQualifiers))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000755 return Result;
756
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000757 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000758 QualType(MemPtrParam->getClass(), 0),
759 QualType(MemPtrArg->getClass(), 0),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000760 Info, Deduced, 0);
Douglas Gregor637a4092009-06-10 23:47:09 +0000761 }
762
Anders Carlsson9a917e42009-06-12 22:56:54 +0000763 // (clang extension)
764 //
Mike Stump1eb44332009-09-09 15:08:12 +0000765 // type(^)(T)
766 // T(^)()
767 // T(^)(T)
Anders Carlsson859ba502009-06-12 16:23:10 +0000768 case Type::BlockPointer: {
769 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
770 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Anders Carlsson859ba502009-06-12 16:23:10 +0000772 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000773 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000775 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +0000776 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000777 BlockPtrArg->getPointeeType(), Info,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000778 Deduced, 0);
Anders Carlsson859ba502009-06-12 16:23:10 +0000779 }
780
Douglas Gregor637a4092009-06-10 23:47:09 +0000781 case Type::TypeOfExpr:
782 case Type::TypeOf:
Douglas Gregor4714c122010-03-31 17:34:00 +0000783 case Type::DependentName:
Douglas Gregor637a4092009-06-10 23:47:09 +0000784 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +0000785 return Sema::TDK_Success;
Douglas Gregor637a4092009-06-10 23:47:09 +0000786
Douglas Gregord560d502009-06-04 00:21:18 +0000787 default:
788 break;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000789 }
790
791 // FIXME: Many more cases to go (to go).
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000792 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000793}
794
Douglas Gregorf67875d2009-06-12 18:26:56 +0000795static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000796DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000797 TemplateParameterList *TemplateParams,
798 const TemplateArgument &Param,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000799 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000800 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000801 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000802 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000803 case TemplateArgument::Null:
804 assert(false && "Null template argument in parameter list");
805 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000806
807 case TemplateArgument::Type:
Douglas Gregor788cd062009-11-11 01:00:40 +0000808 if (Arg.getKind() == TemplateArgument::Type)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000809 return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
Douglas Gregor788cd062009-11-11 01:00:40 +0000810 Arg.getAsType(), Info, Deduced, 0);
811 Info.FirstArg = Param;
812 Info.SecondArg = Arg;
813 return Sema::TDK_NonDeducedMismatch;
814
815 case TemplateArgument::Template:
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000816 if (Arg.getKind() == TemplateArgument::Template)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000817 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor788cd062009-11-11 01:00:40 +0000818 Param.getAsTemplate(),
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000819 Arg.getAsTemplate(), Info, Deduced);
Douglas Gregor788cd062009-11-11 01:00:40 +0000820 Info.FirstArg = Param;
821 Info.SecondArg = Arg;
822 return Sema::TDK_NonDeducedMismatch;
823
Douglas Gregor199d9912009-06-05 00:53:49 +0000824 case TemplateArgument::Declaration:
Douglas Gregor788cd062009-11-11 01:00:40 +0000825 if (Arg.getKind() == TemplateArgument::Declaration &&
826 Param.getAsDecl()->getCanonicalDecl() ==
827 Arg.getAsDecl()->getCanonicalDecl())
828 return Sema::TDK_Success;
829
Douglas Gregorf67875d2009-06-12 18:26:56 +0000830 Info.FirstArg = Param;
831 Info.SecondArg = Arg;
832 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Douglas Gregor199d9912009-06-05 00:53:49 +0000834 case TemplateArgument::Integral:
835 if (Arg.getKind() == TemplateArgument::Integral) {
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000836 if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral()))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000837 return Sema::TDK_Success;
838
839 Info.FirstArg = Param;
840 Info.SecondArg = Arg;
841 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000842 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000843
844 if (Arg.getKind() == TemplateArgument::Expression) {
845 Info.FirstArg = Param;
846 Info.SecondArg = Arg;
847 return Sema::TDK_NonDeducedMismatch;
848 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000849
Douglas Gregorf67875d2009-06-12 18:26:56 +0000850 Info.FirstArg = Param;
851 Info.SecondArg = Arg;
852 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Douglas Gregor199d9912009-06-05 00:53:49 +0000854 case TemplateArgument::Expression: {
Mike Stump1eb44332009-09-09 15:08:12 +0000855 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000856 = getDeducedParameterFromExpr(Param.getAsExpr())) {
857 if (Arg.getKind() == TemplateArgument::Integral)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000858 return DeduceNonTypeTemplateArgument(S, NTTP,
Mike Stump1eb44332009-09-09 15:08:12 +0000859 *Arg.getAsIntegral(),
Douglas Gregor9d0e4412010-03-26 05:50:28 +0000860 Arg.getIntegralType(),
Douglas Gregor02024a92010-03-28 02:42:43 +0000861 /*ArrayBound=*/false,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000862 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000863 if (Arg.getKind() == TemplateArgument::Expression)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000864 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000865 Info, Deduced);
Douglas Gregor15755cb2009-11-13 23:45:44 +0000866 if (Arg.getKind() == TemplateArgument::Declaration)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000867 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
Douglas Gregor15755cb2009-11-13 23:45:44 +0000868 Info, Deduced);
869
Douglas Gregorf67875d2009-06-12 18:26:56 +0000870 Info.FirstArg = Param;
871 Info.SecondArg = Arg;
872 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000873 }
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Douglas Gregor199d9912009-06-05 00:53:49 +0000875 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000876 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000877 }
Anders Carlssond01b1da2009-06-15 17:04:53 +0000878 case TemplateArgument::Pack:
879 assert(0 && "FIXME: Implement!");
880 break;
Douglas Gregor199d9912009-06-05 00:53:49 +0000881 }
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Douglas Gregorf67875d2009-06-12 18:26:56 +0000883 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000884}
885
Mike Stump1eb44332009-09-09 15:08:12 +0000886static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000887DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000888 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000889 const TemplateArgumentList &ParamList,
890 const TemplateArgumentList &ArgList,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000891 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +0000892 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000893 assert(ParamList.size() == ArgList.size());
894 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000895 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000896 = DeduceTemplateArguments(S, TemplateParams,
Mike Stump1eb44332009-09-09 15:08:12 +0000897 ParamList[I], ArgList[I],
Douglas Gregorf67875d2009-06-12 18:26:56 +0000898 Info, Deduced))
899 return Result;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000900 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000901 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000902}
903
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000904/// \brief Determine whether two template arguments are the same.
Mike Stump1eb44332009-09-09 15:08:12 +0000905static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000906 const TemplateArgument &X,
907 const TemplateArgument &Y) {
908 if (X.getKind() != Y.getKind())
909 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000911 switch (X.getKind()) {
912 case TemplateArgument::Null:
913 assert(false && "Comparing NULL template argument");
914 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000916 case TemplateArgument::Type:
917 return Context.getCanonicalType(X.getAsType()) ==
918 Context.getCanonicalType(Y.getAsType());
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000920 case TemplateArgument::Declaration:
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000921 return X.getAsDecl()->getCanonicalDecl() ==
922 Y.getAsDecl()->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Douglas Gregor788cd062009-11-11 01:00:40 +0000924 case TemplateArgument::Template:
925 return Context.getCanonicalTemplateName(X.getAsTemplate())
926 .getAsVoidPointer() ==
927 Context.getCanonicalTemplateName(Y.getAsTemplate())
928 .getAsVoidPointer();
929
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000930 case TemplateArgument::Integral:
931 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Douglas Gregor788cd062009-11-11 01:00:40 +0000933 case TemplateArgument::Expression: {
934 llvm::FoldingSetNodeID XID, YID;
935 X.getAsExpr()->Profile(XID, Context, true);
936 Y.getAsExpr()->Profile(YID, Context, true);
937 return XID == YID;
938 }
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000940 case TemplateArgument::Pack:
941 if (X.pack_size() != Y.pack_size())
942 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000943
944 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
945 XPEnd = X.pack_end(),
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000946 YP = Y.pack_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000947 XP != XPEnd; ++XP, ++YP)
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000948 if (!isSameTemplateArg(Context, *XP, *YP))
949 return false;
950
951 return true;
952 }
953
954 return false;
955}
956
957/// \brief Helper function to build a TemplateParameter when we don't
958/// know its type statically.
959static TemplateParameter makeTemplateParameter(Decl *D) {
960 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
961 return TemplateParameter(TTP);
962 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
963 return TemplateParameter(NTTP);
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000965 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
966}
967
Douglas Gregor31dce8f2010-04-29 06:21:43 +0000968/// Complete template argument deduction for a class template partial
969/// specialization.
970static Sema::TemplateDeductionResult
971FinishTemplateArgumentDeduction(Sema &S,
972 ClassTemplatePartialSpecializationDecl *Partial,
973 const TemplateArgumentList &TemplateArgs,
974 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
975 Sema::TemplateDeductionInfo &Info) {
976 // Trap errors.
977 Sema::SFINAETrap Trap(S);
978
979 Sema::ContextRAII SavedContext(S, Partial);
980
981 // C++ [temp.deduct.type]p2:
982 // [...] or if any template argument remains neither deduced nor
983 // explicitly specified, template argument deduction fails.
984 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
985 Deduced.size());
986 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
987 if (Deduced[I].isNull()) {
988 Decl *Param
989 = const_cast<NamedDecl *>(
990 Partial->getTemplateParameters()->getParam(I));
991 Info.Param = makeTemplateParameter(Param);
992 return Sema::TDK_Incomplete;
993 }
994
995 Builder.Append(Deduced[I]);
996 }
997
998 // Form the template argument list from the deduced template arguments.
999 TemplateArgumentList *DeducedArgumentList
1000 = new (S.Context) TemplateArgumentList(S.Context, Builder,
1001 /*TakeArgs=*/true);
1002 Info.reset(DeducedArgumentList);
1003
1004 // Substitute the deduced template arguments into the template
1005 // arguments of the class template partial specialization, and
1006 // verify that the instantiated template arguments are both valid
1007 // and are equivalent to the template arguments originally provided
1008 // to the class template.
1009 // FIXME: Do we have to correct the types of deduced non-type template
1010 // arguments (in particular, integral non-type template arguments?).
1011 Sema::LocalInstantiationScope InstScope(S);
1012 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
1013 const TemplateArgumentLoc *PartialTemplateArgs
1014 = Partial->getTemplateArgsAsWritten();
1015 unsigned N = Partial->getNumTemplateArgsAsWritten();
1016
1017 // Note that we don't provide the langle and rangle locations.
1018 TemplateArgumentListInfo InstArgs;
1019
1020 for (unsigned I = 0; I != N; ++I) {
1021 Decl *Param = const_cast<NamedDecl *>(
1022 ClassTemplate->getTemplateParameters()->getParam(I));
1023 TemplateArgumentLoc InstArg;
1024 if (S.Subst(PartialTemplateArgs[I], InstArg,
1025 MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
1026 Info.Param = makeTemplateParameter(Param);
1027 Info.FirstArg = PartialTemplateArgs[I].getArgument();
1028 return Sema::TDK_SubstitutionFailure;
1029 }
1030 InstArgs.addArgument(InstArg);
1031 }
1032
1033 TemplateArgumentListBuilder ConvertedInstArgs(
1034 ClassTemplate->getTemplateParameters(), N);
1035
1036 if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
Douglas Gregorec20f462010-05-08 20:07:26 +00001037 InstArgs, false, ConvertedInstArgs))
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001038 return Sema::TDK_SubstitutionFailure;
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001039
1040 for (unsigned I = 0, E = ConvertedInstArgs.flatSize(); I != E; ++I) {
1041 TemplateArgument InstArg = ConvertedInstArgs.getFlatArguments()[I];
1042
1043 Decl *Param = const_cast<NamedDecl *>(
1044 ClassTemplate->getTemplateParameters()->getParam(I));
1045
1046 if (InstArg.getKind() == TemplateArgument::Expression) {
1047 // When the argument is an expression, check the expression result
1048 // against the actual template parameter to get down to the canonical
1049 // template argument.
1050 Expr *InstExpr = InstArg.getAsExpr();
1051 if (NonTypeTemplateParmDecl *NTTP
1052 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1053 if (S.CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
1054 Info.Param = makeTemplateParameter(Param);
1055 Info.FirstArg = Partial->getTemplateArgs()[I];
1056 return Sema::TDK_SubstitutionFailure;
1057 }
1058 }
1059 }
1060
1061 if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
1062 Info.Param = makeTemplateParameter(Param);
1063 Info.FirstArg = TemplateArgs[I];
1064 Info.SecondArg = InstArg;
1065 return Sema::TDK_NonDeducedMismatch;
1066 }
1067 }
1068
1069 if (Trap.hasErrorOccurred())
1070 return Sema::TDK_SubstitutionFailure;
1071
1072 return Sema::TDK_Success;
1073}
1074
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001075/// \brief Perform template argument deduction to determine whether
1076/// the given template arguments match the given class template
1077/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +00001078Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001079Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001080 const TemplateArgumentList &TemplateArgs,
1081 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +00001082 // C++ [temp.class.spec.match]p2:
1083 // A partial specialization matches a given actual template
1084 // argument list if the template arguments of the partial
1085 // specialization can be deduced from the actual template argument
1086 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +00001087 SFINAETrap Trap(*this);
Douglas Gregor02024a92010-03-28 02:42:43 +00001088 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001089 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +00001090 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001091 = ::DeduceTemplateArguments(*this,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001092 Partial->getTemplateParameters(),
Mike Stump1eb44332009-09-09 15:08:12 +00001093 Partial->getTemplateArgs(),
Douglas Gregorf67875d2009-06-12 18:26:56 +00001094 TemplateArgs, Info, Deduced))
1095 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +00001096
Douglas Gregor637a4092009-06-10 23:47:09 +00001097 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
1098 Deduced.data(), Deduced.size());
1099 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001100 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +00001101
Douglas Gregorbb260412009-06-14 08:02:22 +00001102 if (Trap.hasErrorOccurred())
Douglas Gregor31dce8f2010-04-29 06:21:43 +00001103 return Sema::TDK_SubstitutionFailure;
1104
1105 return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
1106 Deduced, Info);
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001107}
Douglas Gregor031a5882009-06-13 00:26:55 +00001108
Douglas Gregor41128772009-06-26 23:27:24 +00001109/// \brief Determine whether the given type T is a simple-template-id type.
1110static bool isSimpleTemplateIdType(QualType T) {
Mike Stump1eb44332009-09-09 15:08:12 +00001111 if (const TemplateSpecializationType *Spec
John McCall183700f2009-09-21 23:43:11 +00001112 = T->getAs<TemplateSpecializationType>())
Douglas Gregor41128772009-06-26 23:27:24 +00001113 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Douglas Gregor41128772009-06-26 23:27:24 +00001115 return false;
1116}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001117
1118/// \brief Substitute the explicitly-provided template arguments into the
1119/// given function template according to C++ [temp.arg.explicit].
1120///
1121/// \param FunctionTemplate the function template into which the explicit
1122/// template arguments will be substituted.
1123///
Mike Stump1eb44332009-09-09 15:08:12 +00001124/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001125/// arguments.
1126///
Mike Stump1eb44332009-09-09 15:08:12 +00001127/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor83314aa2009-07-08 20:55:45 +00001128/// with the converted and checked explicit template arguments.
1129///
Mike Stump1eb44332009-09-09 15:08:12 +00001130/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor83314aa2009-07-08 20:55:45 +00001131/// parameters.
1132///
1133/// \param FunctionType if non-NULL, the result type of the function template
1134/// will also be instantiated and the pointed-to value will be updated with
1135/// the instantiated function type.
1136///
1137/// \param Info if substitution fails for any reason, this object will be
1138/// populated with more information about the failure.
1139///
1140/// \returns TDK_Success if substitution was successful, or some failure
1141/// condition.
1142Sema::TemplateDeductionResult
1143Sema::SubstituteExplicitTemplateArguments(
1144 FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001145 const TemplateArgumentListInfo &ExplicitTemplateArgs,
Douglas Gregor02024a92010-03-28 02:42:43 +00001146 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001147 llvm::SmallVectorImpl<QualType> &ParamTypes,
1148 QualType *FunctionType,
1149 TemplateDeductionInfo &Info) {
1150 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1151 TemplateParameterList *TemplateParams
1152 = FunctionTemplate->getTemplateParameters();
1153
John McCalld5532b62009-11-23 01:53:49 +00001154 if (ExplicitTemplateArgs.size() == 0) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001155 // No arguments to substitute; just copy over the parameter types and
1156 // fill in the function type.
1157 for (FunctionDecl::param_iterator P = Function->param_begin(),
1158 PEnd = Function->param_end();
1159 P != PEnd;
1160 ++P)
1161 ParamTypes.push_back((*P)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Douglas Gregor83314aa2009-07-08 20:55:45 +00001163 if (FunctionType)
1164 *FunctionType = Function->getType();
1165 return TDK_Success;
1166 }
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Douglas Gregor83314aa2009-07-08 20:55:45 +00001168 // Substitution of the explicit template arguments into a function template
1169 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001170 SFINAETrap Trap(*this);
1171
Douglas Gregor83314aa2009-07-08 20:55:45 +00001172 // C++ [temp.arg.explicit]p3:
Mike Stump1eb44332009-09-09 15:08:12 +00001173 // Template arguments that are present shall be specified in the
1174 // declaration order of their corresponding template-parameters. The
Douglas Gregor83314aa2009-07-08 20:55:45 +00001175 // template argument list shall not specify more template-arguments than
Mike Stump1eb44332009-09-09 15:08:12 +00001176 // there are corresponding template-parameters.
1177 TemplateArgumentListBuilder Builder(TemplateParams,
John McCalld5532b62009-11-23 01:53:49 +00001178 ExplicitTemplateArgs.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001179
1180 // Enter a new template instantiation context where we check the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001181 // explicitly-specified template arguments against this function template,
1182 // and then substitute them into the function parameter types.
Mike Stump1eb44332009-09-09 15:08:12 +00001183 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001184 FunctionTemplate, Deduced.data(), Deduced.size(),
1185 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution);
1186 if (Inst)
1187 return TDK_InstantiationDepth;
Mike Stump1eb44332009-09-09 15:08:12 +00001188
John McCall96db3102010-04-29 01:18:58 +00001189 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
John McCallf5813822010-04-29 00:35:03 +00001190
Douglas Gregor83314aa2009-07-08 20:55:45 +00001191 if (CheckTemplateArgumentList(FunctionTemplate,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001192 SourceLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001193 ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001194 true,
Douglas Gregorf1a84452010-05-08 19:15:54 +00001195 Builder) || Trap.hasErrorOccurred()) {
Douglas Gregorfe52c912010-05-09 01:26:06 +00001196 unsigned Index = Builder.structuredSize();
1197 if (Index >= TemplateParams->size())
1198 Index = TemplateParams->size() - 1;
1199 Info.Param = makeTemplateParameter(TemplateParams->getParam(Index));
Douglas Gregor83314aa2009-07-08 20:55:45 +00001200 return TDK_InvalidExplicitArguments;
Douglas Gregorf1a84452010-05-08 19:15:54 +00001201 }
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Douglas Gregor83314aa2009-07-08 20:55:45 +00001203 // Form the template argument list from the explicitly-specified
1204 // template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001205 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor83314aa2009-07-08 20:55:45 +00001206 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1207 Info.reset(ExplicitArgumentList);
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Douglas Gregor83314aa2009-07-08 20:55:45 +00001209 // Instantiate the types of each of the function parameters given the
1210 // explicitly-specified template arguments.
1211 for (FunctionDecl::param_iterator P = Function->param_begin(),
1212 PEnd = Function->param_end();
1213 P != PEnd;
1214 ++P) {
Mike Stump1eb44332009-09-09 15:08:12 +00001215 QualType ParamType
1216 = SubstType((*P)->getType(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001217 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1218 (*P)->getLocation(), (*P)->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001219 if (ParamType.isNull() || Trap.hasErrorOccurred())
1220 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Douglas Gregor83314aa2009-07-08 20:55:45 +00001222 ParamTypes.push_back(ParamType);
1223 }
1224
1225 // If the caller wants a full function type back, instantiate the return
1226 // type and form that function type.
1227 if (FunctionType) {
1228 // FIXME: exception-specifications?
Mike Stump1eb44332009-09-09 15:08:12 +00001229 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001230 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001231 assert(Proto && "Function template does not have a prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001232
1233 QualType ResultType
Douglas Gregor357bbd02009-08-28 20:50:45 +00001234 = SubstType(Proto->getResultType(),
1235 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1236 Function->getTypeSpecStartLoc(),
1237 Function->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001238 if (ResultType.isNull() || Trap.hasErrorOccurred())
1239 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001240
1241 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001242 ParamTypes.data(), ParamTypes.size(),
1243 Proto->isVariadic(),
1244 Proto->getTypeQuals(),
1245 Function->getLocation(),
Eli Friedmanfa869542010-08-05 02:54:05 +00001246 Function->getDeclName(),
1247 Proto->getExtInfo());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001248 if (FunctionType->isNull() || Trap.hasErrorOccurred())
1249 return TDK_SubstitutionFailure;
1250 }
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Douglas Gregor83314aa2009-07-08 20:55:45 +00001252 // C++ [temp.arg.explicit]p2:
Mike Stump1eb44332009-09-09 15:08:12 +00001253 // Trailing template arguments that can be deduced (14.8.2) may be
1254 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001255 // template arguments can be deduced, they may all be omitted; in this
1256 // case, the empty template argument list <> itself may also be omitted.
1257 //
1258 // Take all of the explicitly-specified arguments and put them into the
Mike Stump1eb44332009-09-09 15:08:12 +00001259 // set of deduced template arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001260 Deduced.reserve(TemplateParams->size());
1261 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001262 Deduced.push_back(ExplicitArgumentList->get(I));
1263
Douglas Gregor83314aa2009-07-08 20:55:45 +00001264 return TDK_Success;
1265}
1266
Douglas Gregor02024a92010-03-28 02:42:43 +00001267/// \brief Allocate a TemplateArgumentLoc where all locations have
1268/// been initialized to the given location.
1269///
1270/// \param S The semantic analysis object.
1271///
1272/// \param The template argument we are producing template argument
1273/// location information for.
1274///
1275/// \param NTTPType For a declaration template argument, the type of
1276/// the non-type template parameter that corresponds to this template
1277/// argument.
1278///
1279/// \param Loc The source location to use for the resulting template
1280/// argument.
1281static TemplateArgumentLoc
1282getTrivialTemplateArgumentLoc(Sema &S,
1283 const TemplateArgument &Arg,
1284 QualType NTTPType,
1285 SourceLocation Loc) {
1286 switch (Arg.getKind()) {
1287 case TemplateArgument::Null:
1288 llvm_unreachable("Can't get a NULL template argument here");
1289 break;
1290
1291 case TemplateArgument::Type:
1292 return TemplateArgumentLoc(Arg,
1293 S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
1294
1295 case TemplateArgument::Declaration: {
1296 Expr *E
1297 = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
1298 .takeAs<Expr>();
1299 return TemplateArgumentLoc(TemplateArgument(E), E);
1300 }
1301
1302 case TemplateArgument::Integral: {
1303 Expr *E
1304 = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>();
1305 return TemplateArgumentLoc(TemplateArgument(E), E);
1306 }
1307
1308 case TemplateArgument::Template:
1309 return TemplateArgumentLoc(Arg, SourceRange(), Loc);
1310
1311 case TemplateArgument::Expression:
1312 return TemplateArgumentLoc(Arg, Arg.getAsExpr());
1313
1314 case TemplateArgument::Pack:
1315 llvm_unreachable("Template parameter packs are not yet supported");
1316 }
1317
1318 return TemplateArgumentLoc();
1319}
1320
Mike Stump1eb44332009-09-09 15:08:12 +00001321/// \brief Finish template argument deduction for a function template,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001322/// checking the deduced template arguments for completeness and forming
1323/// the function template specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00001324Sema::TemplateDeductionResult
Douglas Gregor83314aa2009-07-08 20:55:45 +00001325Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor02024a92010-03-28 02:42:43 +00001326 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1327 unsigned NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001328 FunctionDecl *&Specialization,
1329 TemplateDeductionInfo &Info) {
1330 TemplateParameterList *TemplateParams
1331 = FunctionTemplate->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001332
Douglas Gregor83314aa2009-07-08 20:55:45 +00001333 // Template argument deduction for function templates in a SFINAE context.
1334 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001335 SFINAETrap Trap(*this);
1336
Douglas Gregor83314aa2009-07-08 20:55:45 +00001337 // Enter a new template instantiation context while we instantiate the
1338 // actual function declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001339 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001340 FunctionTemplate, Deduced.data(), Deduced.size(),
1341 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
1342 if (Inst)
Mike Stump1eb44332009-09-09 15:08:12 +00001343 return TDK_InstantiationDepth;
1344
John McCall96db3102010-04-29 01:18:58 +00001345 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
John McCallf5813822010-04-29 00:35:03 +00001346
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001347 // C++ [temp.deduct.type]p2:
1348 // [...] or if any template argument remains neither deduced nor
1349 // explicitly specified, template argument deduction fails.
1350 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
1351 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregor02024a92010-03-28 02:42:43 +00001352 NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001353 if (!Deduced[I].isNull()) {
Douglas Gregor02024a92010-03-28 02:42:43 +00001354 if (I < NumExplicitlySpecified ||
1355 Deduced[I].getKind() == TemplateArgument::Type) {
1356 // We have already fully type-checked and converted this
1357 // argument (because it was explicitly-specified) or no
1358 // additional checking is necessary (because it's a template
1359 // type parameter). Just record the presence of this
1360 // parameter.
1361 Builder.Append(Deduced[I]);
1362 continue;
1363 }
1364
1365 // We have deduced this argument, so it still needs to be
1366 // checked and converted.
1367
1368 // First, for a non-type template parameter type that is
1369 // initialized by a declaration, we need the type of the
1370 // corresponding non-type template parameter.
1371 QualType NTTPType;
1372 if (NonTypeTemplateParmDecl *NTTP
1373 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1374 if (Deduced[I].getKind() == TemplateArgument::Declaration) {
1375 NTTPType = NTTP->getType();
1376 if (NTTPType->isDependentType()) {
1377 TemplateArgumentList TemplateArgs(Context, Builder,
1378 /*TakeArgs=*/false);
1379 NTTPType = SubstType(NTTPType,
1380 MultiLevelTemplateArgumentList(TemplateArgs),
1381 NTTP->getLocation(),
1382 NTTP->getDeclName());
1383 if (NTTPType.isNull()) {
1384 Info.Param = makeTemplateParameter(Param);
Douglas Gregorec20f462010-05-08 20:07:26 +00001385 Info.reset(new (Context) TemplateArgumentList(Context, Builder,
1386 /*TakeArgs=*/true));
Douglas Gregor02024a92010-03-28 02:42:43 +00001387 return TDK_SubstitutionFailure;
1388 }
1389 }
1390 }
1391 }
1392
1393 // Convert the deduced template argument into a template
1394 // argument that we can check, almost as if the user had written
1395 // the template argument explicitly.
1396 TemplateArgumentLoc Arg = getTrivialTemplateArgumentLoc(*this,
1397 Deduced[I],
1398 NTTPType,
1399 SourceLocation());
1400
1401 // Check the template argument, converting it as necessary.
1402 if (CheckTemplateArgument(Param, Arg,
1403 FunctionTemplate,
1404 FunctionTemplate->getLocation(),
1405 FunctionTemplate->getSourceRange().getEnd(),
1406 Builder,
1407 Deduced[I].wasDeducedFromArrayBound()
1408 ? CTAK_DeducedFromArrayBound
1409 : CTAK_Deduced)) {
1410 Info.Param = makeTemplateParameter(
1411 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregorec20f462010-05-08 20:07:26 +00001412 Info.reset(new (Context) TemplateArgumentList(Context, Builder,
1413 /*TakeArgs=*/true));
Douglas Gregor02024a92010-03-28 02:42:43 +00001414 return TDK_SubstitutionFailure;
1415 }
1416
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001417 continue;
1418 }
1419
1420 // Substitute into the default template argument, if available.
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001421 TemplateArgumentLoc DefArg
1422 = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
1423 FunctionTemplate->getLocation(),
1424 FunctionTemplate->getSourceRange().getEnd(),
1425 Param,
1426 Builder);
1427
1428 // If there was no default argument, deduction is incomplete.
1429 if (DefArg.getArgument().isNull()) {
1430 Info.Param = makeTemplateParameter(
1431 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1432 return TDK_Incomplete;
1433 }
1434
1435 // Check whether we can actually use the default argument.
1436 if (CheckTemplateArgument(Param, DefArg,
1437 FunctionTemplate,
1438 FunctionTemplate->getLocation(),
1439 FunctionTemplate->getSourceRange().getEnd(),
Douglas Gregor02024a92010-03-28 02:42:43 +00001440 Builder,
1441 CTAK_Deduced)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001442 Info.Param = makeTemplateParameter(
1443 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregorec20f462010-05-08 20:07:26 +00001444 Info.reset(new (Context) TemplateArgumentList(Context, Builder,
1445 /*TakeArgs=*/true));
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001446 return TDK_SubstitutionFailure;
1447 }
1448
1449 // If we get here, we successfully used the default template argument.
1450 }
1451
1452 // Form the template argument list from the deduced template arguments.
1453 TemplateArgumentList *DeducedArgumentList
1454 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1455 Info.reset(DeducedArgumentList);
1456
Mike Stump1eb44332009-09-09 15:08:12 +00001457 // Substitute the deduced template arguments into the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001458 // declaration to produce the function template specialization.
Douglas Gregord4598a22010-04-28 04:52:24 +00001459 DeclContext *Owner = FunctionTemplate->getDeclContext();
1460 if (FunctionTemplate->getFriendObjectKind())
1461 Owner = FunctionTemplate->getLexicalDeclContext();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001462 Specialization = cast_or_null<FunctionDecl>(
Douglas Gregord4598a22010-04-28 04:52:24 +00001463 SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner,
Douglas Gregor357bbd02009-08-28 20:50:45 +00001464 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor83314aa2009-07-08 20:55:45 +00001465 if (!Specialization)
1466 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001467
Douglas Gregorf8825742009-09-15 18:26:13 +00001468 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
1469 FunctionTemplate->getCanonicalDecl());
1470
Mike Stump1eb44332009-09-09 15:08:12 +00001471 // If the template argument list is owned by the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001472 // specialization, release it.
Douglas Gregorec20f462010-05-08 20:07:26 +00001473 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList &&
1474 !Trap.hasErrorOccurred())
Douglas Gregor83314aa2009-07-08 20:55:45 +00001475 Info.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001476
Douglas Gregor83314aa2009-07-08 20:55:45 +00001477 // There may have been an error that did not prevent us from constructing a
1478 // declaration. Mark the declaration invalid and return with a substitution
1479 // failure.
1480 if (Trap.hasErrorOccurred()) {
1481 Specialization->setInvalidDecl(true);
1482 return TDK_SubstitutionFailure;
1483 }
Mike Stump1eb44332009-09-09 15:08:12 +00001484
1485 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001486}
1487
John McCalleff92132010-02-02 02:21:27 +00001488static QualType GetTypeOfFunction(ASTContext &Context,
1489 bool isAddressOfOperand,
1490 FunctionDecl *Fn) {
1491 if (!isAddressOfOperand) return Fn->getType();
1492 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
1493 if (Method->isInstance())
1494 return Context.getMemberPointerType(Fn->getType(),
1495 Context.getTypeDeclType(Method->getParent()).getTypePtr());
1496 return Context.getPointerType(Fn->getType());
1497}
1498
1499/// Apply the deduction rules for overload sets.
1500///
1501/// \return the null type if this argument should be treated as an
1502/// undeduced context
1503static QualType
1504ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
1505 Expr *Arg, QualType ParamType) {
John McCall7bb12da2010-02-02 06:20:04 +00001506 llvm::PointerIntPair<OverloadExpr*,1> R = OverloadExpr::find(Arg);
John McCalleff92132010-02-02 02:21:27 +00001507
John McCall7bb12da2010-02-02 06:20:04 +00001508 bool isAddressOfOperand = bool(R.getInt());
1509 OverloadExpr *Ovl = R.getPointer();
John McCalleff92132010-02-02 02:21:27 +00001510
1511 // If there were explicit template arguments, we can only find
1512 // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
1513 // unambiguously name a full specialization.
John McCall7bb12da2010-02-02 06:20:04 +00001514 if (Ovl->hasExplicitTemplateArgs()) {
John McCalleff92132010-02-02 02:21:27 +00001515 // But we can still look for an explicit specialization.
1516 if (FunctionDecl *ExplicitSpec
John McCall7bb12da2010-02-02 06:20:04 +00001517 = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
1518 return GetTypeOfFunction(S.Context, isAddressOfOperand, ExplicitSpec);
John McCalleff92132010-02-02 02:21:27 +00001519 return QualType();
1520 }
1521
1522 // C++0x [temp.deduct.call]p6:
1523 // When P is a function type, pointer to function type, or pointer
1524 // to member function type:
1525
1526 if (!ParamType->isFunctionType() &&
1527 !ParamType->isFunctionPointerType() &&
1528 !ParamType->isMemberFunctionPointerType())
1529 return QualType();
1530
1531 QualType Match;
John McCall7bb12da2010-02-02 06:20:04 +00001532 for (UnresolvedSetIterator I = Ovl->decls_begin(),
1533 E = Ovl->decls_end(); I != E; ++I) {
John McCalleff92132010-02-02 02:21:27 +00001534 NamedDecl *D = (*I)->getUnderlyingDecl();
1535
1536 // - If the argument is an overload set containing one or more
1537 // function templates, the parameter is treated as a
1538 // non-deduced context.
1539 if (isa<FunctionTemplateDecl>(D))
1540 return QualType();
1541
1542 FunctionDecl *Fn = cast<FunctionDecl>(D);
1543 QualType ArgType = GetTypeOfFunction(S.Context, isAddressOfOperand, Fn);
1544
1545 // - If the argument is an overload set (not containing function
1546 // templates), trial argument deduction is attempted using each
1547 // of the members of the set. If deduction succeeds for only one
1548 // of the overload set members, that member is used as the
1549 // argument value for the deduction. If deduction succeeds for
1550 // more than one member of the overload set the parameter is
1551 // treated as a non-deduced context.
1552
1553 // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
1554 // Type deduction is done independently for each P/A pair, and
1555 // the deduced template argument values are then combined.
1556 // So we do not reject deductions which were made elsewhere.
Douglas Gregor02024a92010-03-28 02:42:43 +00001557 llvm::SmallVector<DeducedTemplateArgument, 8>
1558 Deduced(TemplateParams->size());
John McCall5769d612010-02-08 23:07:23 +00001559 Sema::TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
John McCalleff92132010-02-02 02:21:27 +00001560 unsigned TDF = 0;
1561
1562 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001563 = DeduceTemplateArguments(S, TemplateParams,
John McCalleff92132010-02-02 02:21:27 +00001564 ParamType, ArgType,
1565 Info, Deduced, TDF);
1566 if (Result) continue;
1567 if (!Match.isNull()) return QualType();
1568 Match = ArgType;
1569 }
1570
1571 return Match;
1572}
1573
Douglas Gregore53060f2009-06-25 22:08:12 +00001574/// \brief Perform template argument deduction from a function call
1575/// (C++ [temp.deduct.call]).
1576///
1577/// \param FunctionTemplate the function template for which we are performing
1578/// template argument deduction.
1579///
Douglas Gregor48026d22010-01-11 18:40:55 +00001580/// \param ExplicitTemplateArguments the explicit template arguments provided
1581/// for this call.
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001582///
Douglas Gregore53060f2009-06-25 22:08:12 +00001583/// \param Args the function call arguments
1584///
1585/// \param NumArgs the number of arguments in Args
1586///
Douglas Gregor48026d22010-01-11 18:40:55 +00001587/// \param Name the name of the function being called. This is only significant
1588/// when the function template is a conversion function template, in which
1589/// case this routine will also perform template argument deduction based on
1590/// the function to which
1591///
Douglas Gregore53060f2009-06-25 22:08:12 +00001592/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001593/// this will be set to the function template specialization produced by
Douglas Gregore53060f2009-06-25 22:08:12 +00001594/// template argument deduction.
1595///
1596/// \param Info the argument will be updated to provide additional information
1597/// about template argument deduction.
1598///
1599/// \returns the result of template argument deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001600Sema::TemplateDeductionResult
1601Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor48026d22010-01-11 18:40:55 +00001602 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregore53060f2009-06-25 22:08:12 +00001603 Expr **Args, unsigned NumArgs,
1604 FunctionDecl *&Specialization,
1605 TemplateDeductionInfo &Info) {
1606 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001607
Douglas Gregore53060f2009-06-25 22:08:12 +00001608 // C++ [temp.deduct.call]p1:
1609 // Template argument deduction is done by comparing each function template
1610 // parameter type (call it P) with the type of the corresponding argument
1611 // of the call (call it A) as described below.
1612 unsigned CheckArgs = NumArgs;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001613 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregore53060f2009-06-25 22:08:12 +00001614 return TDK_TooFewArguments;
1615 else if (NumArgs > Function->getNumParams()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001616 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001617 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregore53060f2009-06-25 22:08:12 +00001618 if (!Proto->isVariadic())
1619 return TDK_TooManyArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001620
Douglas Gregore53060f2009-06-25 22:08:12 +00001621 CheckArgs = Function->getNumParams();
1622 }
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001624 // The types of the parameters from which we will perform template argument
1625 // deduction.
Douglas Gregor2b0749a42010-03-25 15:38:42 +00001626 Sema::LocalInstantiationScope InstScope(*this);
Douglas Gregore53060f2009-06-25 22:08:12 +00001627 TemplateParameterList *TemplateParams
1628 = FunctionTemplate->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00001629 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001630 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregor02024a92010-03-28 02:42:43 +00001631 unsigned NumExplicitlySpecified = 0;
John McCalld5532b62009-11-23 01:53:49 +00001632 if (ExplicitTemplateArgs) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001633 TemplateDeductionResult Result =
1634 SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001635 *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001636 Deduced,
1637 ParamTypes,
1638 0,
1639 Info);
1640 if (Result)
1641 return Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00001642
1643 NumExplicitlySpecified = Deduced.size();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001644 } else {
1645 // Just fill in the parameter types from the function declaration.
1646 for (unsigned I = 0; I != CheckArgs; ++I)
1647 ParamTypes.push_back(Function->getParamDecl(I)->getType());
1648 }
Mike Stump1eb44332009-09-09 15:08:12 +00001649
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001650 // Deduce template arguments from the function parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001651 Deduced.resize(TemplateParams->size());
Douglas Gregore53060f2009-06-25 22:08:12 +00001652 for (unsigned I = 0; I != CheckArgs; ++I) {
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001653 QualType ParamType = ParamTypes[I];
Douglas Gregore53060f2009-06-25 22:08:12 +00001654 QualType ArgType = Args[I]->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001655
John McCalleff92132010-02-02 02:21:27 +00001656 // Overload sets usually make this parameter an undeduced
1657 // context, but there are sometimes special circumstances.
1658 if (ArgType == Context.OverloadTy) {
1659 ArgType = ResolveOverloadForDeduction(*this, TemplateParams,
1660 Args[I], ParamType);
1661 if (ArgType.isNull())
1662 continue;
1663 }
1664
Douglas Gregore53060f2009-06-25 22:08:12 +00001665 // C++ [temp.deduct.call]p2:
1666 // If P is not a reference type:
1667 QualType CanonParamType = Context.getCanonicalType(ParamType);
Douglas Gregor500d3312009-06-26 18:27:22 +00001668 bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1669 if (!ParamWasReference) {
Mike Stump1eb44332009-09-09 15:08:12 +00001670 // - If A is an array type, the pointer type produced by the
1671 // array-to-pointer standard conversion (4.2) is used in place of
Douglas Gregore53060f2009-06-25 22:08:12 +00001672 // A for type deduction; otherwise,
1673 if (ArgType->isArrayType())
1674 ArgType = Context.getArrayDecayedType(ArgType);
Mike Stump1eb44332009-09-09 15:08:12 +00001675 // - If A is a function type, the pointer type produced by the
1676 // function-to-pointer standard conversion (4.3) is used in place
Douglas Gregore53060f2009-06-25 22:08:12 +00001677 // of A for type deduction; otherwise,
1678 else if (ArgType->isFunctionType())
1679 ArgType = Context.getPointerType(ArgType);
1680 else {
1681 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1682 // type are ignored for type deduction.
1683 QualType CanonArgType = Context.getCanonicalType(ArgType);
Douglas Gregora4923eb2009-11-16 21:35:15 +00001684 if (CanonArgType.getLocalCVRQualifiers())
1685 ArgType = CanonArgType.getLocalUnqualifiedType();
Douglas Gregore53060f2009-06-25 22:08:12 +00001686 }
1687 }
Mike Stump1eb44332009-09-09 15:08:12 +00001688
Douglas Gregore53060f2009-06-25 22:08:12 +00001689 // C++0x [temp.deduct.call]p3:
1690 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
Mike Stump1eb44332009-09-09 15:08:12 +00001691 // are ignored for type deduction.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001692 if (CanonParamType.getLocalCVRQualifiers())
1693 ParamType = CanonParamType.getLocalUnqualifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001694 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001695 // [...] If P is a reference type, the type referred to by P is used
1696 // for type deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001697 ParamType = ParamRefType->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00001698
1699 // [...] If P is of the form T&&, where T is a template parameter, and
1700 // the argument is an lvalue, the type A& is used in place of A for
Douglas Gregore53060f2009-06-25 22:08:12 +00001701 // type deduction.
1702 if (isa<RValueReferenceType>(ParamRefType) &&
John McCall183700f2009-09-21 23:43:11 +00001703 ParamRefType->getAs<TemplateTypeParmType>() &&
Douglas Gregore53060f2009-06-25 22:08:12 +00001704 Args[I]->isLvalue(Context) == Expr::LV_Valid)
1705 ArgType = Context.getLValueReferenceType(ArgType);
1706 }
Mike Stump1eb44332009-09-09 15:08:12 +00001707
Douglas Gregore53060f2009-06-25 22:08:12 +00001708 // C++0x [temp.deduct.call]p4:
1709 // In general, the deduction process attempts to find template argument
1710 // values that will make the deduced A identical to A (after the type A
1711 // is transformed as described above). [...]
Douglas Gregor12820292009-09-14 20:00:47 +00001712 unsigned TDF = TDF_SkipNonDependent;
Mike Stump1eb44332009-09-09 15:08:12 +00001713
Douglas Gregor508f1c82009-06-26 23:10:12 +00001714 // - If the original P is a reference type, the deduced A (i.e., the
1715 // type referred to by the reference) can be more cv-qualified than
1716 // the transformed A.
1717 if (ParamWasReference)
1718 TDF |= TDF_ParamWithReferenceType;
Mike Stump1eb44332009-09-09 15:08:12 +00001719 // - The transformed A can be another pointer or pointer to member
1720 // type that can be converted to the deduced A via a qualification
Douglas Gregor508f1c82009-06-26 23:10:12 +00001721 // conversion (4.4).
John McCalldb0bc472010-08-05 05:30:45 +00001722 if (ArgType->isPointerType() || ArgType->isMemberPointerType() ||
1723 ArgType->isObjCObjectPointerType())
Douglas Gregor508f1c82009-06-26 23:10:12 +00001724 TDF |= TDF_IgnoreQualifiers;
Mike Stump1eb44332009-09-09 15:08:12 +00001725 // - If P is a class and P has the form simple-template-id, then the
Douglas Gregor41128772009-06-26 23:27:24 +00001726 // transformed A can be a derived class of the deduced A. Likewise,
1727 // if P is a pointer to a class of the form simple-template-id, the
1728 // transformed A can be a pointer to a derived class pointed to by
1729 // the deduced A.
1730 if (isSimpleTemplateIdType(ParamType) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001731 (isa<PointerType>(ParamType) &&
Douglas Gregor41128772009-06-26 23:27:24 +00001732 isSimpleTemplateIdType(
Ted Kremenek6217b802009-07-29 21:53:49 +00001733 ParamType->getAs<PointerType>()->getPointeeType())))
Douglas Gregor41128772009-06-26 23:27:24 +00001734 TDF |= TDF_DerivedClass;
Mike Stump1eb44332009-09-09 15:08:12 +00001735
Douglas Gregore53060f2009-06-25 22:08:12 +00001736 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001737 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor500d3312009-06-26 18:27:22 +00001738 ParamType, ArgType, Info, Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +00001739 TDF))
Douglas Gregore53060f2009-06-25 22:08:12 +00001740 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001741
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001742 // FIXME: we need to check that the deduced A is the same as A,
1743 // modulo the various allowed differences.
Douglas Gregore53060f2009-06-25 22:08:12 +00001744 }
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001745
Mike Stump1eb44332009-09-09 15:08:12 +00001746 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor02024a92010-03-28 02:42:43 +00001747 NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001748 Specialization, Info);
Douglas Gregore53060f2009-06-25 22:08:12 +00001749}
1750
Douglas Gregor83314aa2009-07-08 20:55:45 +00001751/// \brief Deduce template arguments when taking the address of a function
Douglas Gregor4b52e252009-12-21 23:17:24 +00001752/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
1753/// a template.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001754///
1755/// \param FunctionTemplate the function template for which we are performing
1756/// template argument deduction.
1757///
Douglas Gregor4b52e252009-12-21 23:17:24 +00001758/// \param ExplicitTemplateArguments the explicitly-specified template
1759/// arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001760///
1761/// \param ArgFunctionType the function type that will be used as the
1762/// "argument" type (A) when performing template argument deduction from the
Douglas Gregor4b52e252009-12-21 23:17:24 +00001763/// function template's function type. This type may be NULL, if there is no
1764/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001765///
1766/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001767/// this will be set to the function template specialization produced by
Douglas Gregor83314aa2009-07-08 20:55:45 +00001768/// template argument deduction.
1769///
1770/// \param Info the argument will be updated to provide additional information
1771/// about template argument deduction.
1772///
1773/// \returns the result of template argument deduction.
1774Sema::TemplateDeductionResult
1775Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001776 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001777 QualType ArgFunctionType,
1778 FunctionDecl *&Specialization,
1779 TemplateDeductionInfo &Info) {
1780 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1781 TemplateParameterList *TemplateParams
1782 = FunctionTemplate->getTemplateParameters();
1783 QualType FunctionType = Function->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001784
Douglas Gregor83314aa2009-07-08 20:55:45 +00001785 // Substitute any explicit template arguments.
Douglas Gregor2b0749a42010-03-25 15:38:42 +00001786 Sema::LocalInstantiationScope InstScope(*this);
Douglas Gregor02024a92010-03-28 02:42:43 +00001787 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
1788 unsigned NumExplicitlySpecified = 0;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001789 llvm::SmallVector<QualType, 4> ParamTypes;
John McCalld5532b62009-11-23 01:53:49 +00001790 if (ExplicitTemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001791 if (TemplateDeductionResult Result
1792 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001793 *ExplicitTemplateArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00001794 Deduced, ParamTypes,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001795 &FunctionType, Info))
1796 return Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00001797
1798 NumExplicitlySpecified = Deduced.size();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001799 }
1800
1801 // Template argument deduction for function templates in a SFINAE context.
1802 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001803 SFINAETrap Trap(*this);
1804
John McCalleff92132010-02-02 02:21:27 +00001805 Deduced.resize(TemplateParams->size());
1806
Douglas Gregor4b52e252009-12-21 23:17:24 +00001807 if (!ArgFunctionType.isNull()) {
1808 // Deduce template arguments from the function type.
Douglas Gregor4b52e252009-12-21 23:17:24 +00001809 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001810 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor4b52e252009-12-21 23:17:24 +00001811 FunctionType, ArgFunctionType, Info,
1812 Deduced, 0))
1813 return Result;
1814 }
1815
Mike Stump1eb44332009-09-09 15:08:12 +00001816 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor02024a92010-03-28 02:42:43 +00001817 NumExplicitlySpecified,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001818 Specialization, Info);
1819}
1820
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001821/// \brief Deduce template arguments for a templated conversion
1822/// function (C++ [temp.deduct.conv]) and, if successful, produce a
1823/// conversion function template specialization.
1824Sema::TemplateDeductionResult
1825Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1826 QualType ToType,
1827 CXXConversionDecl *&Specialization,
1828 TemplateDeductionInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00001829 CXXConversionDecl *Conv
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001830 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
1831 QualType FromType = Conv->getConversionType();
1832
1833 // Canonicalize the types for deduction.
1834 QualType P = Context.getCanonicalType(FromType);
1835 QualType A = Context.getCanonicalType(ToType);
1836
1837 // C++0x [temp.deduct.conv]p3:
1838 // If P is a reference type, the type referred to by P is used for
1839 // type deduction.
1840 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
1841 P = PRef->getPointeeType();
1842
1843 // C++0x [temp.deduct.conv]p3:
1844 // If A is a reference type, the type referred to by A is used
1845 // for type deduction.
1846 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
1847 A = ARef->getPointeeType();
1848 // C++ [temp.deduct.conv]p2:
1849 //
Mike Stump1eb44332009-09-09 15:08:12 +00001850 // If A is not a reference type:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001851 else {
1852 assert(!A->isReferenceType() && "Reference types were handled above");
1853
1854 // - If P is an array type, the pointer type produced by the
Mike Stump1eb44332009-09-09 15:08:12 +00001855 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001856 // of P for type deduction; otherwise,
1857 if (P->isArrayType())
1858 P = Context.getArrayDecayedType(P);
1859 // - If P is a function type, the pointer type produced by the
1860 // function-to-pointer standard conversion (4.3) is used in
1861 // place of P for type deduction; otherwise,
1862 else if (P->isFunctionType())
1863 P = Context.getPointerType(P);
1864 // - If P is a cv-qualified type, the top level cv-qualifiers of
1865 // P’s type are ignored for type deduction.
1866 else
1867 P = P.getUnqualifiedType();
1868
1869 // C++0x [temp.deduct.conv]p3:
1870 // If A is a cv-qualified type, the top level cv-qualifiers of A’s
1871 // type are ignored for type deduction.
1872 A = A.getUnqualifiedType();
1873 }
1874
1875 // Template argument deduction for function templates in a SFINAE context.
1876 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001877 SFINAETrap Trap(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001878
1879 // C++ [temp.deduct.conv]p1:
1880 // Template argument deduction is done by comparing the return
1881 // type of the template conversion function (call it P) with the
1882 // type that is required as the result of the conversion (call it
1883 // A) as described in 14.8.2.4.
1884 TemplateParameterList *TemplateParams
1885 = FunctionTemplate->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00001886 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Mike Stump1eb44332009-09-09 15:08:12 +00001887 Deduced.resize(TemplateParams->size());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001888
1889 // C++0x [temp.deduct.conv]p4:
1890 // In general, the deduction process attempts to find template
1891 // argument values that will make the deduced A identical to
1892 // A. However, there are two cases that allow a difference:
1893 unsigned TDF = 0;
1894 // - If the original A is a reference type, A can be more
1895 // cv-qualified than the deduced A (i.e., the type referred to
1896 // by the reference)
1897 if (ToType->isReferenceType())
1898 TDF |= TDF_ParamWithReferenceType;
1899 // - The deduced A can be another pointer or pointer to member
1900 // type that can be converted to A via a qualification
1901 // conversion.
1902 //
1903 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
1904 // both P and A are pointers or member pointers. In this case, we
1905 // just ignore cv-qualifiers completely).
1906 if ((P->isPointerType() && A->isPointerType()) ||
1907 (P->isMemberPointerType() && P->isMemberPointerType()))
1908 TDF |= TDF_IgnoreQualifiers;
1909 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001910 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001911 P, A, Info, Deduced, TDF))
1912 return Result;
1913
1914 // FIXME: we need to check that the deduced A is the same as A,
1915 // modulo the various allowed differences.
Mike Stump1eb44332009-09-09 15:08:12 +00001916
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001917 // Finish template argument deduction.
Douglas Gregor2b0749a42010-03-25 15:38:42 +00001918 Sema::LocalInstantiationScope InstScope(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001919 FunctionDecl *Spec = 0;
1920 TemplateDeductionResult Result
Douglas Gregor02024a92010-03-28 02:42:43 +00001921 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec,
1922 Info);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001923 Specialization = cast_or_null<CXXConversionDecl>(Spec);
1924 return Result;
1925}
1926
Douglas Gregor4b52e252009-12-21 23:17:24 +00001927/// \brief Deduce template arguments for a function template when there is
1928/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
1929///
1930/// \param FunctionTemplate the function template for which we are performing
1931/// template argument deduction.
1932///
1933/// \param ExplicitTemplateArguments the explicitly-specified template
1934/// arguments.
1935///
1936/// \param Specialization if template argument deduction was successful,
1937/// this will be set to the function template specialization produced by
1938/// template argument deduction.
1939///
1940/// \param Info the argument will be updated to provide additional information
1941/// about template argument deduction.
1942///
1943/// \returns the result of template argument deduction.
1944Sema::TemplateDeductionResult
1945Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1946 const TemplateArgumentListInfo *ExplicitTemplateArgs,
1947 FunctionDecl *&Specialization,
1948 TemplateDeductionInfo &Info) {
1949 return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
1950 QualType(), Specialization, Info);
1951}
1952
Douglas Gregor8a514912009-09-14 18:39:43 +00001953/// \brief Stores the result of comparing the qualifiers of two types.
1954enum DeductionQualifierComparison {
1955 NeitherMoreQualified = 0,
1956 ParamMoreQualified,
1957 ArgMoreQualified
1958};
1959
1960/// \brief Deduce the template arguments during partial ordering by comparing
1961/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
1962///
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001963/// \param S the semantic analysis object within which we are deducing
Douglas Gregor8a514912009-09-14 18:39:43 +00001964///
1965/// \param TemplateParams the template parameters that we are deducing
1966///
1967/// \param ParamIn the parameter type
1968///
1969/// \param ArgIn the argument type
1970///
1971/// \param Info information about the template argument deduction itself
1972///
1973/// \param Deduced the deduced template arguments
1974///
1975/// \returns the result of template argument deduction so far. Note that a
1976/// "success" result means that template argument deduction has not yet failed,
1977/// but it may still fail, later, for other reasons.
1978static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001979DeduceTemplateArgumentsDuringPartialOrdering(Sema &S,
Douglas Gregor02024a92010-03-28 02:42:43 +00001980 TemplateParameterList *TemplateParams,
Douglas Gregor8a514912009-09-14 18:39:43 +00001981 QualType ParamIn, QualType ArgIn,
1982 Sema::TemplateDeductionInfo &Info,
Douglas Gregor02024a92010-03-28 02:42:43 +00001983 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1984 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001985 CanQualType Param = S.Context.getCanonicalType(ParamIn);
1986 CanQualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor8a514912009-09-14 18:39:43 +00001987
1988 // C++0x [temp.deduct.partial]p5:
1989 // Before the partial ordering is done, certain transformations are
1990 // performed on the types used for partial ordering:
1991 // - If P is a reference type, P is replaced by the type referred to.
1992 CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00001993 if (!ParamRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00001994 Param = ParamRef->getPointeeType();
1995
1996 // - If A is a reference type, A is replaced by the type referred to.
1997 CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00001998 if (!ArgRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00001999 Arg = ArgRef->getPointeeType();
2000
John McCalle27ec8a2009-10-23 23:03:21 +00002001 if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
Douglas Gregor8a514912009-09-14 18:39:43 +00002002 // C++0x [temp.deduct.partial]p6:
2003 // If both P and A were reference types (before being replaced with the
2004 // type referred to above), determine which of the two types (if any) is
2005 // more cv-qualified than the other; otherwise the types are considered to
2006 // be equally cv-qualified for partial ordering purposes. The result of this
2007 // determination will be used below.
2008 //
2009 // We save this information for later, using it only when deduction
2010 // succeeds in both directions.
2011 DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
2012 if (Param.isMoreQualifiedThan(Arg))
2013 QualifierResult = ParamMoreQualified;
2014 else if (Arg.isMoreQualifiedThan(Param))
2015 QualifierResult = ArgMoreQualified;
2016 QualifierComparisons->push_back(QualifierResult);
2017 }
2018
2019 // C++0x [temp.deduct.partial]p7:
2020 // Remove any top-level cv-qualifiers:
2021 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
2022 // version of P.
2023 Param = Param.getUnqualifiedType();
2024 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
2025 // version of A.
2026 Arg = Arg.getUnqualifiedType();
2027
2028 // C++0x [temp.deduct.partial]p8:
2029 // Using the resulting types P and A the deduction is then done as
2030 // described in 14.9.2.5. If deduction succeeds for a given type, the type
2031 // from the argument template is considered to be at least as specialized
2032 // as the type from the parameter template.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002033 return DeduceTemplateArguments(S, TemplateParams, Param, Arg, Info,
Douglas Gregor8a514912009-09-14 18:39:43 +00002034 Deduced, TDF_None);
2035}
2036
2037static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002038MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2039 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002040 unsigned Level,
Douglas Gregore73bb602009-09-14 21:25:05 +00002041 llvm::SmallVectorImpl<bool> &Deduced);
Douglas Gregor8a514912009-09-14 18:39:43 +00002042
2043/// \brief Determine whether the function template \p FT1 is at least as
2044/// specialized as \p FT2.
2045static bool isAtLeastAsSpecializedAs(Sema &S,
John McCall5769d612010-02-08 23:07:23 +00002046 SourceLocation Loc,
Douglas Gregor8a514912009-09-14 18:39:43 +00002047 FunctionTemplateDecl *FT1,
2048 FunctionTemplateDecl *FT2,
2049 TemplatePartialOrderingContext TPOC,
2050 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
2051 FunctionDecl *FD1 = FT1->getTemplatedDecl();
2052 FunctionDecl *FD2 = FT2->getTemplatedDecl();
2053 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
2054 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
2055
2056 assert(Proto1 && Proto2 && "Function templates must have prototypes");
2057 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
Douglas Gregor02024a92010-03-28 02:42:43 +00002058 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor8a514912009-09-14 18:39:43 +00002059 Deduced.resize(TemplateParams->size());
2060
2061 // C++0x [temp.deduct.partial]p3:
2062 // The types used to determine the ordering depend on the context in which
2063 // the partial ordering is done:
John McCall5769d612010-02-08 23:07:23 +00002064 Sema::TemplateDeductionInfo Info(S.Context, Loc);
Douglas Gregor8a514912009-09-14 18:39:43 +00002065 switch (TPOC) {
2066 case TPOC_Call: {
2067 // - In the context of a function call, the function parameter types are
2068 // used.
2069 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
2070 for (unsigned I = 0; I != NumParams; ++I)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002071 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00002072 TemplateParams,
2073 Proto2->getArgType(I),
2074 Proto1->getArgType(I),
2075 Info,
2076 Deduced,
2077 QualifierComparisons))
2078 return false;
2079
2080 break;
2081 }
2082
2083 case TPOC_Conversion:
2084 // - In the context of a call to a conversion operator, the return types
2085 // of the conversion function templates are used.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002086 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00002087 TemplateParams,
2088 Proto2->getResultType(),
2089 Proto1->getResultType(),
2090 Info,
2091 Deduced,
2092 QualifierComparisons))
2093 return false;
2094 break;
2095
2096 case TPOC_Other:
2097 // - In other contexts (14.6.6.2) the function template’s function type
2098 // is used.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002099 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00002100 TemplateParams,
2101 FD2->getType(),
2102 FD1->getType(),
2103 Info,
2104 Deduced,
2105 QualifierComparisons))
2106 return false;
2107 break;
2108 }
2109
2110 // C++0x [temp.deduct.partial]p11:
2111 // In most cases, all template parameters must have values in order for
2112 // deduction to succeed, but for partial ordering purposes a template
2113 // parameter may remain without a value provided it is not used in the
2114 // types being used for partial ordering. [ Note: a template parameter used
2115 // in a non-deduced context is considered used. -end note]
2116 unsigned ArgIdx = 0, NumArgs = Deduced.size();
2117 for (; ArgIdx != NumArgs; ++ArgIdx)
2118 if (Deduced[ArgIdx].isNull())
2119 break;
2120
2121 if (ArgIdx == NumArgs) {
2122 // All template arguments were deduced. FT1 is at least as specialized
2123 // as FT2.
2124 return true;
2125 }
2126
Douglas Gregore73bb602009-09-14 21:25:05 +00002127 // Figure out which template parameters were used.
Douglas Gregor8a514912009-09-14 18:39:43 +00002128 llvm::SmallVector<bool, 4> UsedParameters;
2129 UsedParameters.resize(TemplateParams->size());
2130 switch (TPOC) {
2131 case TPOC_Call: {
2132 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
2133 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002134 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
2135 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00002136 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00002137 break;
2138 }
2139
2140 case TPOC_Conversion:
Douglas Gregored9c0f92009-10-29 00:04:11 +00002141 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
2142 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00002143 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00002144 break;
2145
2146 case TPOC_Other:
Douglas Gregored9c0f92009-10-29 00:04:11 +00002147 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
2148 TemplateParams->getDepth(),
2149 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00002150 break;
2151 }
2152
2153 for (; ArgIdx != NumArgs; ++ArgIdx)
2154 // If this argument had no value deduced but was used in one of the types
2155 // used for partial ordering, then deduction fails.
2156 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
2157 return false;
2158
2159 return true;
2160}
2161
2162
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002163/// \brief Returns the more specialized function template according
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002164/// to the rules of function template partial ordering (C++ [temp.func.order]).
2165///
2166/// \param FT1 the first function template
2167///
2168/// \param FT2 the second function template
2169///
Douglas Gregor8a514912009-09-14 18:39:43 +00002170/// \param TPOC the context in which we are performing partial ordering of
2171/// function templates.
Mike Stump1eb44332009-09-09 15:08:12 +00002172///
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002173/// \returns the more specialized function template. If neither
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002174/// template is more specialized, returns NULL.
2175FunctionTemplateDecl *
2176Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
2177 FunctionTemplateDecl *FT2,
John McCall5769d612010-02-08 23:07:23 +00002178 SourceLocation Loc,
Douglas Gregor8a514912009-09-14 18:39:43 +00002179 TemplatePartialOrderingContext TPOC) {
Douglas Gregor8a514912009-09-14 18:39:43 +00002180 llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
John McCall5769d612010-02-08 23:07:23 +00002181 bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, 0);
2182 bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
Douglas Gregor8a514912009-09-14 18:39:43 +00002183 &QualifierComparisons);
2184
2185 if (Better1 != Better2) // We have a clear winner
2186 return Better1? FT1 : FT2;
2187
2188 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002189 return 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00002190
2191
2192 // C++0x [temp.deduct.partial]p10:
2193 // If for each type being considered a given template is at least as
2194 // specialized for all types and more specialized for some set of types and
2195 // the other template is not more specialized for any types or is not at
2196 // least as specialized for any types, then the given template is more
2197 // specialized than the other template. Otherwise, neither template is more
2198 // specialized than the other.
2199 Better1 = false;
2200 Better2 = false;
2201 for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
2202 // C++0x [temp.deduct.partial]p9:
2203 // If, for a given type, deduction succeeds in both directions (i.e., the
2204 // types are identical after the transformations above) and if the type
2205 // from the argument template is more cv-qualified than the type from the
2206 // parameter template (as described above) that type is considered to be
2207 // more specialized than the other. If neither type is more cv-qualified
2208 // than the other then neither type is more specialized than the other.
2209 switch (QualifierComparisons[I]) {
2210 case NeitherMoreQualified:
2211 break;
2212
2213 case ParamMoreQualified:
2214 Better1 = true;
2215 if (Better2)
2216 return 0;
2217 break;
2218
2219 case ArgMoreQualified:
2220 Better2 = true;
2221 if (Better1)
2222 return 0;
2223 break;
2224 }
2225 }
2226
2227 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002228 if (Better1)
2229 return FT1;
Douglas Gregor8a514912009-09-14 18:39:43 +00002230 else if (Better2)
2231 return FT2;
2232 else
2233 return 0;
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002234}
Douglas Gregor83314aa2009-07-08 20:55:45 +00002235
Douglas Gregord5a423b2009-09-25 18:43:00 +00002236/// \brief Determine if the two templates are equivalent.
2237static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
2238 if (T1 == T2)
2239 return true;
2240
2241 if (!T1 || !T2)
2242 return false;
2243
2244 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
2245}
2246
2247/// \brief Retrieve the most specialized of the given function template
2248/// specializations.
2249///
John McCallc373d482010-01-27 01:50:18 +00002250/// \param SpecBegin the start iterator of the function template
2251/// specializations that we will be comparing.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002252///
John McCallc373d482010-01-27 01:50:18 +00002253/// \param SpecEnd the end iterator of the function template
2254/// specializations, paired with \p SpecBegin.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002255///
2256/// \param TPOC the partial ordering context to use to compare the function
2257/// template specializations.
2258///
2259/// \param Loc the location where the ambiguity or no-specializations
2260/// diagnostic should occur.
2261///
2262/// \param NoneDiag partial diagnostic used to diagnose cases where there are
2263/// no matching candidates.
2264///
2265/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
2266/// occurs.
2267///
2268/// \param CandidateDiag partial diagnostic used for each function template
2269/// specialization that is a candidate in the ambiguous ordering. One parameter
2270/// in this diagnostic should be unbound, which will correspond to the string
2271/// describing the template arguments for the function template specialization.
2272///
2273/// \param Index if non-NULL and the result of this function is non-nULL,
2274/// receives the index corresponding to the resulting function template
2275/// specialization.
2276///
2277/// \returns the most specialized function template specialization, if
John McCallc373d482010-01-27 01:50:18 +00002278/// found. Otherwise, returns SpecEnd.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002279///
2280/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
2281/// template argument deduction.
John McCallc373d482010-01-27 01:50:18 +00002282UnresolvedSetIterator
2283Sema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
2284 UnresolvedSetIterator SpecEnd,
2285 TemplatePartialOrderingContext TPOC,
2286 SourceLocation Loc,
2287 const PartialDiagnostic &NoneDiag,
2288 const PartialDiagnostic &AmbigDiag,
2289 const PartialDiagnostic &CandidateDiag) {
2290 if (SpecBegin == SpecEnd) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00002291 Diag(Loc, NoneDiag);
John McCallc373d482010-01-27 01:50:18 +00002292 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002293 }
2294
John McCallc373d482010-01-27 01:50:18 +00002295 if (SpecBegin + 1 == SpecEnd)
2296 return SpecBegin;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002297
2298 // Find the function template that is better than all of the templates it
2299 // has been compared to.
John McCallc373d482010-01-27 01:50:18 +00002300 UnresolvedSetIterator Best = SpecBegin;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002301 FunctionTemplateDecl *BestTemplate
John McCallc373d482010-01-27 01:50:18 +00002302 = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002303 assert(BestTemplate && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00002304 for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
2305 FunctionTemplateDecl *Challenger
2306 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002307 assert(Challenger && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00002308 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCall5769d612010-02-08 23:07:23 +00002309 Loc, TPOC),
Douglas Gregord5a423b2009-09-25 18:43:00 +00002310 Challenger)) {
2311 Best = I;
2312 BestTemplate = Challenger;
2313 }
2314 }
2315
2316 // Make sure that the "best" function template is more specialized than all
2317 // of the others.
2318 bool Ambiguous = false;
John McCallc373d482010-01-27 01:50:18 +00002319 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
2320 FunctionTemplateDecl *Challenger
2321 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002322 if (I != Best &&
2323 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCall5769d612010-02-08 23:07:23 +00002324 Loc, TPOC),
Douglas Gregord5a423b2009-09-25 18:43:00 +00002325 BestTemplate)) {
2326 Ambiguous = true;
2327 break;
2328 }
2329 }
2330
2331 if (!Ambiguous) {
2332 // We found an answer. Return it.
John McCallc373d482010-01-27 01:50:18 +00002333 return Best;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002334 }
2335
2336 // Diagnose the ambiguity.
2337 Diag(Loc, AmbigDiag);
2338
2339 // FIXME: Can we order the candidates in some sane way?
John McCallc373d482010-01-27 01:50:18 +00002340 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
2341 Diag((*I)->getLocation(), CandidateDiag)
Douglas Gregord5a423b2009-09-25 18:43:00 +00002342 << getTemplateArgumentBindingsText(
John McCallc373d482010-01-27 01:50:18 +00002343 cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
2344 *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
Douglas Gregord5a423b2009-09-25 18:43:00 +00002345
John McCallc373d482010-01-27 01:50:18 +00002346 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002347}
2348
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002349/// \brief Returns the more specialized class template partial specialization
2350/// according to the rules of partial ordering of class template partial
2351/// specializations (C++ [temp.class.order]).
2352///
2353/// \param PS1 the first class template partial specialization
2354///
2355/// \param PS2 the second class template partial specialization
2356///
2357/// \returns the more specialized class template partial specialization. If
2358/// neither partial specialization is more specialized, returns NULL.
2359ClassTemplatePartialSpecializationDecl *
2360Sema::getMoreSpecializedPartialSpecialization(
2361 ClassTemplatePartialSpecializationDecl *PS1,
John McCall5769d612010-02-08 23:07:23 +00002362 ClassTemplatePartialSpecializationDecl *PS2,
2363 SourceLocation Loc) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002364 // C++ [temp.class.order]p1:
2365 // For two class template partial specializations, the first is at least as
2366 // specialized as the second if, given the following rewrite to two
2367 // function templates, the first function template is at least as
2368 // specialized as the second according to the ordering rules for function
2369 // templates (14.6.6.2):
2370 // - the first function template has the same template parameters as the
2371 // first partial specialization and has a single function parameter
2372 // whose type is a class template specialization with the template
2373 // arguments of the first partial specialization, and
2374 // - the second function template has the same template parameters as the
2375 // second partial specialization and has a single function parameter
2376 // whose type is a class template specialization with the template
2377 // arguments of the second partial specialization.
2378 //
Douglas Gregor31dce8f2010-04-29 06:21:43 +00002379 // Rather than synthesize function templates, we merely perform the
2380 // equivalent partial ordering by performing deduction directly on
2381 // the template arguments of the class template partial
2382 // specializations. This computation is slightly simpler than the
2383 // general problem of function template partial ordering, because
2384 // class template partial specializations are more constrained. We
2385 // know that every template parameter is deducible from the class
2386 // template partial specialization's template arguments, for
2387 // example.
Douglas Gregor02024a92010-03-28 02:42:43 +00002388 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
John McCall5769d612010-02-08 23:07:23 +00002389 Sema::TemplateDeductionInfo Info(Context, Loc);
John McCall31f17ec2010-04-27 00:57:59 +00002390
2391 QualType PT1 = PS1->getInjectedSpecializationType();
2392 QualType PT2 = PS2->getInjectedSpecializationType();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002393
2394 // Determine whether PS1 is at least as specialized as PS2
2395 Deduced.resize(PS2->getTemplateParameters()->size());
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002396 bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002397 PS2->getTemplateParameters(),
John McCall31f17ec2010-04-27 00:57:59 +00002398 PT2,
2399 PT1,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002400 Info,
2401 Deduced,
2402 0);
Douglas Gregor516e6e02010-04-29 06:31:36 +00002403 if (Better1)
2404 Better1 = !::FinishTemplateArgumentDeduction(*this, PS2,
2405 PS1->getTemplateArgs(),
2406 Deduced, Info);
2407
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002408 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregordb0d4b72009-11-11 23:06:43 +00002409 Deduced.clear();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002410 Deduced.resize(PS1->getTemplateParameters()->size());
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002411 bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002412 PS1->getTemplateParameters(),
John McCall31f17ec2010-04-27 00:57:59 +00002413 PT1,
2414 PT2,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002415 Info,
2416 Deduced,
2417 0);
Douglas Gregor516e6e02010-04-29 06:31:36 +00002418 if (Better2)
2419 Better2 = !::FinishTemplateArgumentDeduction(*this, PS1,
2420 PS2->getTemplateArgs(),
2421 Deduced, Info);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002422
2423 if (Better1 == Better2)
2424 return 0;
2425
2426 return Better1? PS1 : PS2;
2427}
2428
Mike Stump1eb44332009-09-09 15:08:12 +00002429static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002430MarkUsedTemplateParameters(Sema &SemaRef,
2431 const TemplateArgument &TemplateArg,
2432 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002433 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002434 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002435
Douglas Gregore73bb602009-09-14 21:25:05 +00002436/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002437/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00002438static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002439MarkUsedTemplateParameters(Sema &SemaRef,
2440 const Expr *E,
2441 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002442 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002443 llvm::SmallVectorImpl<bool> &Used) {
2444 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2445 // find other occurrences of template parameters.
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002446 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregorc781f9c2010-01-14 18:13:22 +00002447 if (!DRE)
Douglas Gregor031a5882009-06-13 00:26:55 +00002448 return;
2449
Mike Stump1eb44332009-09-09 15:08:12 +00002450 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00002451 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2452 if (!NTTP)
2453 return;
2454
Douglas Gregored9c0f92009-10-29 00:04:11 +00002455 if (NTTP->getDepth() == Depth)
2456 Used[NTTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002457}
2458
Douglas Gregore73bb602009-09-14 21:25:05 +00002459/// \brief Mark the template parameters that are used by the given
2460/// nested name specifier.
2461static void
2462MarkUsedTemplateParameters(Sema &SemaRef,
2463 NestedNameSpecifier *NNS,
2464 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002465 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002466 llvm::SmallVectorImpl<bool> &Used) {
2467 if (!NNS)
2468 return;
2469
Douglas Gregored9c0f92009-10-29 00:04:11 +00002470 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2471 Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002472 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002473 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002474}
2475
2476/// \brief Mark the template parameters that are used by the given
2477/// template name.
2478static void
2479MarkUsedTemplateParameters(Sema &SemaRef,
2480 TemplateName Name,
2481 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002482 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002483 llvm::SmallVectorImpl<bool> &Used) {
2484 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2485 if (TemplateTemplateParmDecl *TTP
Douglas Gregored9c0f92009-10-29 00:04:11 +00002486 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2487 if (TTP->getDepth() == Depth)
2488 Used[TTP->getIndex()] = true;
2489 }
Douglas Gregore73bb602009-09-14 21:25:05 +00002490 return;
2491 }
2492
Douglas Gregor788cd062009-11-11 01:00:40 +00002493 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2494 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2495 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002496 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
Douglas Gregored9c0f92009-10-29 00:04:11 +00002497 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2498 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002499}
2500
2501/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002502/// type.
Mike Stump1eb44332009-09-09 15:08:12 +00002503static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002504MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2505 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002506 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002507 llvm::SmallVectorImpl<bool> &Used) {
2508 if (T.isNull())
2509 return;
2510
Douglas Gregor031a5882009-06-13 00:26:55 +00002511 // Non-dependent types have nothing deducible
2512 if (!T->isDependentType())
2513 return;
2514
2515 T = SemaRef.Context.getCanonicalType(T);
2516 switch (T->getTypeClass()) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002517 case Type::Pointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002518 MarkUsedTemplateParameters(SemaRef,
2519 cast<PointerType>(T)->getPointeeType(),
2520 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002521 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002522 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002523 break;
2524
2525 case Type::BlockPointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002526 MarkUsedTemplateParameters(SemaRef,
2527 cast<BlockPointerType>(T)->getPointeeType(),
2528 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002529 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002530 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002531 break;
2532
2533 case Type::LValueReference:
2534 case Type::RValueReference:
Douglas Gregore73bb602009-09-14 21:25:05 +00002535 MarkUsedTemplateParameters(SemaRef,
2536 cast<ReferenceType>(T)->getPointeeType(),
2537 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002538 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002539 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002540 break;
2541
2542 case Type::MemberPointer: {
2543 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore73bb602009-09-14 21:25:05 +00002544 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002545 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002546 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002547 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002548 break;
2549 }
2550
2551 case Type::DependentSizedArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002552 MarkUsedTemplateParameters(SemaRef,
2553 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002554 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002555 // Fall through to check the element type
2556
2557 case Type::ConstantArray:
2558 case Type::IncompleteArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002559 MarkUsedTemplateParameters(SemaRef,
2560 cast<ArrayType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002561 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002562 break;
2563
2564 case Type::Vector:
2565 case Type::ExtVector:
Douglas Gregore73bb602009-09-14 21:25:05 +00002566 MarkUsedTemplateParameters(SemaRef,
2567 cast<VectorType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002568 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002569 break;
2570
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002571 case Type::DependentSizedExtVector: {
2572 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002573 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002574 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002575 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002576 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002577 Depth, Used);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002578 break;
2579 }
2580
Douglas Gregor031a5882009-06-13 00:26:55 +00002581 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002582 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002583 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002584 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002585 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore73bb602009-09-14 21:25:05 +00002586 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002587 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002588 break;
2589 }
2590
Douglas Gregored9c0f92009-10-29 00:04:11 +00002591 case Type::TemplateTypeParm: {
2592 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2593 if (TTP->getDepth() == Depth)
2594 Used[TTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002595 break;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002596 }
Douglas Gregor031a5882009-06-13 00:26:55 +00002597
John McCall31f17ec2010-04-27 00:57:59 +00002598 case Type::InjectedClassName:
2599 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
2600 // fall through
2601
Douglas Gregor031a5882009-06-13 00:26:55 +00002602 case Type::TemplateSpecialization: {
Mike Stump1eb44332009-09-09 15:08:12 +00002603 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002604 = cast<TemplateSpecializationType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002605 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002606 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002607 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002608 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2609 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002610 break;
2611 }
2612
Douglas Gregore73bb602009-09-14 21:25:05 +00002613 case Type::Complex:
2614 if (!OnlyDeduced)
2615 MarkUsedTemplateParameters(SemaRef,
2616 cast<ComplexType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002617 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002618 break;
2619
Douglas Gregor4714c122010-03-31 17:34:00 +00002620 case Type::DependentName:
Douglas Gregore73bb602009-09-14 21:25:05 +00002621 if (!OnlyDeduced)
2622 MarkUsedTemplateParameters(SemaRef,
Douglas Gregor4714c122010-03-31 17:34:00 +00002623 cast<DependentNameType>(T)->getQualifier(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002624 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002625 break;
2626
John McCall33500952010-06-11 00:33:02 +00002627 case Type::DependentTemplateSpecialization: {
2628 const DependentTemplateSpecializationType *Spec
2629 = cast<DependentTemplateSpecializationType>(T);
2630 if (!OnlyDeduced)
2631 MarkUsedTemplateParameters(SemaRef, Spec->getQualifier(),
2632 OnlyDeduced, Depth, Used);
2633 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
2634 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2635 Used);
2636 break;
2637 }
2638
John McCallad5e7382010-03-01 23:49:17 +00002639 case Type::TypeOf:
2640 if (!OnlyDeduced)
2641 MarkUsedTemplateParameters(SemaRef,
2642 cast<TypeOfType>(T)->getUnderlyingType(),
2643 OnlyDeduced, Depth, Used);
2644 break;
2645
2646 case Type::TypeOfExpr:
2647 if (!OnlyDeduced)
2648 MarkUsedTemplateParameters(SemaRef,
2649 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
2650 OnlyDeduced, Depth, Used);
2651 break;
2652
2653 case Type::Decltype:
2654 if (!OnlyDeduced)
2655 MarkUsedTemplateParameters(SemaRef,
2656 cast<DecltypeType>(T)->getUnderlyingExpr(),
2657 OnlyDeduced, Depth, Used);
2658 break;
2659
Douglas Gregore73bb602009-09-14 21:25:05 +00002660 // None of these types have any template parameters in them.
Douglas Gregor031a5882009-06-13 00:26:55 +00002661 case Type::Builtin:
Douglas Gregor031a5882009-06-13 00:26:55 +00002662 case Type::VariableArray:
2663 case Type::FunctionNoProto:
2664 case Type::Record:
2665 case Type::Enum:
Douglas Gregor031a5882009-06-13 00:26:55 +00002666 case Type::ObjCInterface:
John McCallc12c5bb2010-05-15 11:32:37 +00002667 case Type::ObjCObject:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002668 case Type::ObjCObjectPointer:
John McCalled976492009-12-04 22:46:56 +00002669 case Type::UnresolvedUsing:
Douglas Gregor031a5882009-06-13 00:26:55 +00002670#define TYPE(Class, Base)
2671#define ABSTRACT_TYPE(Class, Base)
2672#define DEPENDENT_TYPE(Class, Base)
2673#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2674#include "clang/AST/TypeNodes.def"
2675 break;
2676 }
2677}
2678
Douglas Gregore73bb602009-09-14 21:25:05 +00002679/// \brief Mark the template parameters that are used by this
Douglas Gregor031a5882009-06-13 00:26:55 +00002680/// template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00002681static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002682MarkUsedTemplateParameters(Sema &SemaRef,
2683 const TemplateArgument &TemplateArg,
2684 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002685 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002686 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002687 switch (TemplateArg.getKind()) {
2688 case TemplateArgument::Null:
2689 case TemplateArgument::Integral:
Douglas Gregor788cd062009-11-11 01:00:40 +00002690 case TemplateArgument::Declaration:
Douglas Gregor031a5882009-06-13 00:26:55 +00002691 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002692
Douglas Gregor031a5882009-06-13 00:26:55 +00002693 case TemplateArgument::Type:
Douglas Gregore73bb602009-09-14 21:25:05 +00002694 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002695 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002696 break;
2697
Douglas Gregor788cd062009-11-11 01:00:40 +00002698 case TemplateArgument::Template:
2699 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2700 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002701 break;
2702
2703 case TemplateArgument::Expression:
Douglas Gregore73bb602009-09-14 21:25:05 +00002704 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002705 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002706 break;
Douglas Gregore73bb602009-09-14 21:25:05 +00002707
Anders Carlssond01b1da2009-06-15 17:04:53 +00002708 case TemplateArgument::Pack:
Douglas Gregore73bb602009-09-14 21:25:05 +00002709 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2710 PEnd = TemplateArg.pack_end();
2711 P != PEnd; ++P)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002712 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssond01b1da2009-06-15 17:04:53 +00002713 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00002714 }
2715}
2716
2717/// \brief Mark the template parameters can be deduced by the given
2718/// template argument list.
2719///
2720/// \param TemplateArgs the template argument list from which template
2721/// parameters will be deduced.
2722///
2723/// \param Deduced a bit vector whose elements will be set to \c true
2724/// to indicate when the corresponding template parameter will be
2725/// deduced.
Mike Stump1eb44332009-09-09 15:08:12 +00002726void
Douglas Gregore73bb602009-09-14 21:25:05 +00002727Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002728 bool OnlyDeduced, unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002729 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002730 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002731 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2732 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002733}
Douglas Gregor63f07c52009-09-18 23:21:38 +00002734
2735/// \brief Marks all of the template parameters that will be deduced by a
2736/// call to the given function template.
Douglas Gregor02024a92010-03-28 02:42:43 +00002737void
2738Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
2739 llvm::SmallVectorImpl<bool> &Deduced) {
Douglas Gregor63f07c52009-09-18 23:21:38 +00002740 TemplateParameterList *TemplateParams
2741 = FunctionTemplate->getTemplateParameters();
2742 Deduced.clear();
2743 Deduced.resize(TemplateParams->size());
2744
2745 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2746 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
2747 ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002748 true, TemplateParams->getDepth(), Deduced);
Douglas Gregor63f07c52009-09-18 23:21:38 +00002749}