blob: 81ecdc018533efeb2fe9a629d2be132aadb59169 [file] [log] [blame]
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001//===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template argument deduction.
10//
11//===----------------------------------------------------------------------===/
12
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000013#include "clang/Sema/Sema.h"
John McCall8b0666c2010-08-20 18:27:03 +000014#include "clang/Sema/DeclSpec.h"
John McCallde6836a2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
Douglas Gregor55ca8f62009-06-04 00:03:07 +000016#include "clang/AST/ASTContext.h"
John McCallde6836a2010-08-24 07:21:54 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor55ca8f62009-06-04 00:03:07 +000018#include "clang/AST/DeclTemplate.h"
19#include "clang/AST/StmtVisitor.h"
20#include "clang/AST/Expr.h"
21#include "clang/AST/ExprCXX.h"
Douglas Gregor0ff7d922009-09-14 18:39:43 +000022#include <algorithm>
Douglas Gregorcf0b47d2009-06-26 23:10:12 +000023
24namespace clang {
25 /// \brief Various flags that control template argument deduction.
26 ///
27 /// These flags can be bitwise-OR'd together.
28 enum TemplateDeductionFlags {
29 /// \brief No template argument deduction flags, which indicates the
30 /// strictest results for template argument deduction (as used for, e.g.,
31 /// matching class template partial specializations).
32 TDF_None = 0,
33 /// \brief Within template argument deduction from a function call, we are
34 /// matching with a parameter type for which the original parameter was
35 /// a reference.
36 TDF_ParamWithReferenceType = 0x1,
37 /// \brief Within template argument deduction from a function call, we
38 /// are matching in a case where we ignore cv-qualifiers.
39 TDF_IgnoreQualifiers = 0x02,
40 /// \brief Within template argument deduction from a function call,
41 /// we are matching in a case where we can perform template argument
Douglas Gregorfc516c92009-06-26 23:27:24 +000042 /// deduction from a template-id of a derived class of the argument type.
Douglas Gregor406f6342009-09-14 20:00:47 +000043 TDF_DerivedClass = 0x04,
44 /// \brief Allow non-dependent types to differ, e.g., when performing
45 /// template argument deduction from a function call where conversions
46 /// may apply.
47 TDF_SkipNonDependent = 0x08
Douglas Gregorcf0b47d2009-06-26 23:10:12 +000048 };
49}
50
Douglas Gregor55ca8f62009-06-04 00:03:07 +000051using namespace clang;
52
Douglas Gregor0a29a052010-03-26 05:50:28 +000053/// \brief Compare two APSInts, extending and switching the sign as
54/// necessary to compare their values regardless of underlying type.
55static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) {
56 if (Y.getBitWidth() > X.getBitWidth())
57 X.extend(Y.getBitWidth());
58 else if (Y.getBitWidth() < X.getBitWidth())
59 Y.extend(X.getBitWidth());
60
61 // If there is a signedness mismatch, correct it.
62 if (X.isSigned() != Y.isSigned()) {
63 // If the signed value is negative, then the values cannot be the same.
64 if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative()))
65 return false;
66
67 Y.setIsSigned(true);
68 X.setIsSigned(true);
69 }
70
71 return X == Y;
72}
73
Douglas Gregor181aa4a2009-06-12 18:26:56 +000074static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +000075DeduceTemplateArguments(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +000076 TemplateParameterList *TemplateParams,
77 const TemplateArgument &Param,
Douglas Gregor4fbe3e32009-06-09 16:35:58 +000078 const TemplateArgument &Arg,
Douglas Gregor181aa4a2009-06-12 18:26:56 +000079 Sema::TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +000080 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced);
Douglas Gregor4fbe3e32009-06-09 16:35:58 +000081
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000082/// \brief If the given expression is of a form that permits the deduction
83/// of a non-type template parameter, return the declaration of that
84/// non-type template parameter.
85static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
86 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
87 E = IC->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +000088
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000089 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
90 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
Mike Stump11289f42009-09-09 15:08:12 +000091
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000092 return 0;
93}
94
Mike Stump11289f42009-09-09 15:08:12 +000095/// \brief Deduce the value of the given non-type template parameter
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000096/// from the given constant.
Douglas Gregor181aa4a2009-06-12 18:26:56 +000097static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +000098DeduceNonTypeTemplateArgument(Sema &S,
Mike Stump11289f42009-09-09 15:08:12 +000099 NonTypeTemplateParmDecl *NTTP,
Douglas Gregor0a29a052010-03-26 05:50:28 +0000100 llvm::APSInt Value, QualType ValueType,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000101 bool DeducedFromArrayBound,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000102 Sema::TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000103 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump11289f42009-09-09 15:08:12 +0000104 assert(NTTP->getDepth() == 0 &&
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000105 "Cannot deduce non-type template argument with depth > 0");
Mike Stump11289f42009-09-09 15:08:12 +0000106
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000107 if (Deduced[NTTP->getIndex()].isNull()) {
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000108 Deduced[NTTP->getIndex()] = DeducedTemplateArgument(Value, ValueType,
109 DeducedFromArrayBound);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000110 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000111 }
Mike Stump11289f42009-09-09 15:08:12 +0000112
Douglas Gregor0a29a052010-03-26 05:50:28 +0000113 if (Deduced[NTTP->getIndex()].getKind() != TemplateArgument::Integral) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000114 Info.Param = NTTP;
115 Info.FirstArg = Deduced[NTTP->getIndex()];
Douglas Gregor0a29a052010-03-26 05:50:28 +0000116 Info.SecondArg = TemplateArgument(Value, ValueType);
117 return Sema::TDK_Inconsistent;
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000118 }
119
Douglas Gregor0a29a052010-03-26 05:50:28 +0000120 // Extent the smaller of the two values.
121 llvm::APSInt PrevValue = *Deduced[NTTP->getIndex()].getAsIntegral();
122 if (!hasSameExtendedValue(PrevValue, Value)) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000123 Info.Param = NTTP;
124 Info.FirstArg = Deduced[NTTP->getIndex()];
Douglas Gregor0a29a052010-03-26 05:50:28 +0000125 Info.SecondArg = TemplateArgument(Value, ValueType);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000126 return Sema::TDK_Inconsistent;
127 }
128
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000129 if (!DeducedFromArrayBound)
130 Deduced[NTTP->getIndex()].setDeducedFromArrayBound(false);
131
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000132 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000133}
134
Mike Stump11289f42009-09-09 15:08:12 +0000135/// \brief Deduce the value of the given non-type template parameter
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000136/// from the given type- or value-dependent expression.
137///
138/// \returns true if deduction succeeded, false otherwise.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000139static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000140DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000141 NonTypeTemplateParmDecl *NTTP,
142 Expr *Value,
143 Sema::TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000144 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump11289f42009-09-09 15:08:12 +0000145 assert(NTTP->getDepth() == 0 &&
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000146 "Cannot deduce non-type template argument with depth > 0");
147 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
148 "Expression template argument must be type- or value-dependent.");
Mike Stump11289f42009-09-09 15:08:12 +0000149
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000150 if (Deduced[NTTP->getIndex()].isNull()) {
Douglas Gregor0a29a052010-03-26 05:50:28 +0000151 Deduced[NTTP->getIndex()] = TemplateArgument(Value->Retain());
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000152 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000153 }
Mike Stump11289f42009-09-09 15:08:12 +0000154
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000155 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
Mike Stump11289f42009-09-09 15:08:12 +0000156 // Okay, we deduced a constant in one case and a dependent expression
157 // in another case. FIXME: Later, we will check that instantiating the
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000158 // dependent expression gives us the constant value.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000159 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000160 }
Mike Stump11289f42009-09-09 15:08:12 +0000161
Douglas Gregor00a511f2009-09-15 16:51:42 +0000162 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
163 // Compare the expressions for equality
164 llvm::FoldingSetNodeID ID1, ID2;
Chandler Carruthc1263112010-02-07 21:33:28 +0000165 Deduced[NTTP->getIndex()].getAsExpr()->Profile(ID1, S.Context, true);
166 Value->Profile(ID2, S.Context, true);
Douglas Gregor00a511f2009-09-15 16:51:42 +0000167 if (ID1 == ID2)
168 return Sema::TDK_Success;
169
170 // FIXME: Fill in argument mismatch information
171 return Sema::TDK_NonDeducedMismatch;
172 }
173
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000174 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000175}
176
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000177/// \brief Deduce the value of the given non-type template parameter
178/// from the given declaration.
179///
180/// \returns true if deduction succeeded, false otherwise.
181static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000182DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000183 NonTypeTemplateParmDecl *NTTP,
184 Decl *D,
185 Sema::TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000186 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000187 assert(NTTP->getDepth() == 0 &&
188 "Cannot deduce non-type template argument with depth > 0");
189
190 if (Deduced[NTTP->getIndex()].isNull()) {
191 Deduced[NTTP->getIndex()] = TemplateArgument(D->getCanonicalDecl());
192 return Sema::TDK_Success;
193 }
194
195 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
196 // Okay, we deduced a declaration in one case and a dependent expression
197 // in another case.
198 return Sema::TDK_Success;
199 }
200
201 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Declaration) {
202 // Compare the declarations for equality
203 if (Deduced[NTTP->getIndex()].getAsDecl()->getCanonicalDecl() ==
204 D->getCanonicalDecl())
205 return Sema::TDK_Success;
206
207 // FIXME: Fill in argument mismatch information
208 return Sema::TDK_NonDeducedMismatch;
209 }
210
211 return Sema::TDK_Success;
212}
213
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000214static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000215DeduceTemplateArguments(Sema &S,
Douglas Gregoradee3e32009-11-11 23:06:43 +0000216 TemplateParameterList *TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000217 TemplateName Param,
218 TemplateName Arg,
219 Sema::TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000220 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000221 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
Douglas Gregoradee3e32009-11-11 23:06:43 +0000222 if (!ParamDecl) {
223 // The parameter type is dependent and is not a template template parameter,
224 // so there is nothing that we can deduce.
225 return Sema::TDK_Success;
226 }
227
228 if (TemplateTemplateParmDecl *TempParam
229 = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) {
230 // Bind the template template parameter to the given template name.
231 TemplateArgument &ExistingArg = Deduced[TempParam->getIndex()];
232 if (ExistingArg.isNull()) {
233 // This is the first deduction for this template template parameter.
Chandler Carruthc1263112010-02-07 21:33:28 +0000234 ExistingArg = TemplateArgument(S.Context.getCanonicalTemplateName(Arg));
Douglas Gregoradee3e32009-11-11 23:06:43 +0000235 return Sema::TDK_Success;
236 }
237
238 // Verify that the previous binding matches this deduction.
239 assert(ExistingArg.getKind() == TemplateArgument::Template);
Chandler Carruthc1263112010-02-07 21:33:28 +0000240 if (S.Context.hasSameTemplateName(ExistingArg.getAsTemplate(), Arg))
Douglas Gregoradee3e32009-11-11 23:06:43 +0000241 return Sema::TDK_Success;
242
243 // Inconsistent deduction.
244 Info.Param = TempParam;
245 Info.FirstArg = ExistingArg;
246 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000247 return Sema::TDK_Inconsistent;
248 }
Douglas Gregoradee3e32009-11-11 23:06:43 +0000249
250 // Verify that the two template names are equivalent.
Chandler Carruthc1263112010-02-07 21:33:28 +0000251 if (S.Context.hasSameTemplateName(Param, Arg))
Douglas Gregoradee3e32009-11-11 23:06:43 +0000252 return Sema::TDK_Success;
253
254 // Mismatch of non-dependent template parameter to argument.
255 Info.FirstArg = TemplateArgument(Param);
256 Info.SecondArg = TemplateArgument(Arg);
257 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000258}
259
Mike Stump11289f42009-09-09 15:08:12 +0000260/// \brief Deduce the template arguments by comparing the template parameter
Douglas Gregore81f3e72009-07-07 23:09:34 +0000261/// type (which is a template-id) with the template argument type.
262///
Chandler Carruthc1263112010-02-07 21:33:28 +0000263/// \param S the Sema
Douglas Gregore81f3e72009-07-07 23:09:34 +0000264///
265/// \param TemplateParams the template parameters that we are deducing
266///
267/// \param Param the parameter type
268///
269/// \param Arg the argument type
270///
271/// \param Info information about the template argument deduction itself
272///
273/// \param Deduced the deduced template arguments
274///
275/// \returns the result of template argument deduction so far. Note that a
276/// "success" result means that template argument deduction has not yet failed,
277/// but it may still fail, later, for other reasons.
278static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000279DeduceTemplateArguments(Sema &S,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000280 TemplateParameterList *TemplateParams,
281 const TemplateSpecializationType *Param,
282 QualType Arg,
283 Sema::TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000284 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
John McCallb692a092009-10-22 20:10:53 +0000285 assert(Arg.isCanonical() && "Argument type must be canonical");
Mike Stump11289f42009-09-09 15:08:12 +0000286
Douglas Gregore81f3e72009-07-07 23:09:34 +0000287 // Check whether the template argument is a dependent template-id.
Mike Stump11289f42009-09-09 15:08:12 +0000288 if (const TemplateSpecializationType *SpecArg
Douglas Gregore81f3e72009-07-07 23:09:34 +0000289 = dyn_cast<TemplateSpecializationType>(Arg)) {
290 // Perform template argument deduction for the template name.
291 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000292 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000293 Param->getTemplateName(),
294 SpecArg->getTemplateName(),
295 Info, Deduced))
296 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000297
Mike Stump11289f42009-09-09 15:08:12 +0000298
Douglas Gregore81f3e72009-07-07 23:09:34 +0000299 // Perform template argument deduction on each template
300 // argument.
Douglas Gregoradee3e32009-11-11 23:06:43 +0000301 unsigned NumArgs = std::min(SpecArg->getNumArgs(), Param->getNumArgs());
Douglas Gregore81f3e72009-07-07 23:09:34 +0000302 for (unsigned I = 0; I != NumArgs; ++I)
303 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000304 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000305 Param->getArg(I),
306 SpecArg->getArg(I),
307 Info, Deduced))
308 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000309
Douglas Gregore81f3e72009-07-07 23:09:34 +0000310 return Sema::TDK_Success;
311 }
Mike Stump11289f42009-09-09 15:08:12 +0000312
Douglas Gregore81f3e72009-07-07 23:09:34 +0000313 // If the argument type is a class template specialization, we
314 // perform template argument deduction using its template
315 // arguments.
316 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
317 if (!RecordArg)
318 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000319
320 ClassTemplateSpecializationDecl *SpecArg
Douglas Gregore81f3e72009-07-07 23:09:34 +0000321 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
322 if (!SpecArg)
323 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000324
Douglas Gregore81f3e72009-07-07 23:09:34 +0000325 // Perform template argument deduction for the template name.
326 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000327 = DeduceTemplateArguments(S,
Douglas Gregoradee3e32009-11-11 23:06:43 +0000328 TemplateParams,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000329 Param->getTemplateName(),
330 TemplateName(SpecArg->getSpecializedTemplate()),
331 Info, Deduced))
332 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000333
Douglas Gregore81f3e72009-07-07 23:09:34 +0000334 unsigned NumArgs = Param->getNumArgs();
335 const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
336 if (NumArgs != ArgArgs.size())
337 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000338
Douglas Gregore81f3e72009-07-07 23:09:34 +0000339 for (unsigned I = 0; I != NumArgs; ++I)
Mike Stump11289f42009-09-09 15:08:12 +0000340 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000341 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000342 Param->getArg(I),
343 ArgArgs.get(I),
344 Info, Deduced))
345 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000346
Douglas Gregore81f3e72009-07-07 23:09:34 +0000347 return Sema::TDK_Success;
348}
349
Douglas Gregorcceb9752009-06-26 18:27:22 +0000350/// \brief Deduce the template arguments by comparing the parameter type and
351/// the argument type (C++ [temp.deduct.type]).
352///
Chandler Carruthc1263112010-02-07 21:33:28 +0000353/// \param S the semantic analysis object within which we are deducing
Douglas Gregorcceb9752009-06-26 18:27:22 +0000354///
355/// \param TemplateParams the template parameters that we are deducing
356///
357/// \param ParamIn the parameter type
358///
359/// \param ArgIn the argument type
360///
361/// \param Info information about the template argument deduction itself
362///
363/// \param Deduced the deduced template arguments
364///
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000365/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
Mike Stump11289f42009-09-09 15:08:12 +0000366/// how template argument deduction is performed.
Douglas Gregorcceb9752009-06-26 18:27:22 +0000367///
368/// \returns the result of template argument deduction so far. Note that a
369/// "success" result means that template argument deduction has not yet failed,
370/// but it may still fail, later, for other reasons.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000371static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000372DeduceTemplateArguments(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000373 TemplateParameterList *TemplateParams,
374 QualType ParamIn, QualType ArgIn,
375 Sema::TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000376 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000377 unsigned TDF) {
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000378 // We only want to look at the canonical types, since typedefs and
379 // sugar are not part of template argument deduction.
Chandler Carruthc1263112010-02-07 21:33:28 +0000380 QualType Param = S.Context.getCanonicalType(ParamIn);
381 QualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000382
Douglas Gregorcceb9752009-06-26 18:27:22 +0000383 // C++0x [temp.deduct.call]p4 bullet 1:
384 // - If the original P is a reference type, the deduced A (i.e., the type
Mike Stump11289f42009-09-09 15:08:12 +0000385 // referred to by the reference) can be more cv-qualified than the
Douglas Gregorcceb9752009-06-26 18:27:22 +0000386 // transformed A.
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000387 if (TDF & TDF_ParamWithReferenceType) {
Chandler Carruthc712ce12009-12-30 04:10:01 +0000388 Qualifiers Quals;
Chandler Carruthc1263112010-02-07 21:33:28 +0000389 QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals);
Chandler Carruthc712ce12009-12-30 04:10:01 +0000390 Quals.setCVRQualifiers(Quals.getCVRQualifiers() &
391 Arg.getCVRQualifiersThroughArrayTypes());
Chandler Carruthc1263112010-02-07 21:33:28 +0000392 Param = S.Context.getQualifiedType(UnqualParam, Quals);
Douglas Gregorcceb9752009-06-26 18:27:22 +0000393 }
Mike Stump11289f42009-09-09 15:08:12 +0000394
Douglas Gregor705c9002009-06-26 20:57:09 +0000395 // If the parameter type is not dependent, there is nothing to deduce.
Douglas Gregor406f6342009-09-14 20:00:47 +0000396 if (!Param->isDependentType()) {
397 if (!(TDF & TDF_SkipNonDependent) && Param != Arg) {
398
399 return Sema::TDK_NonDeducedMismatch;
400 }
401
Douglas Gregor705c9002009-06-26 20:57:09 +0000402 return Sema::TDK_Success;
Douglas Gregor406f6342009-09-14 20:00:47 +0000403 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000404
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000405 // C++ [temp.deduct.type]p9:
Mike Stump11289f42009-09-09 15:08:12 +0000406 // A template type argument T, a template template argument TT or a
407 // template non-type argument i can be deduced if P and A have one of
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000408 // the following forms:
409 //
410 // T
411 // cv-list T
Mike Stump11289f42009-09-09 15:08:12 +0000412 if (const TemplateTypeParmType *TemplateTypeParm
John McCall9dd450b2009-09-21 23:43:11 +0000413 = Param->getAs<TemplateTypeParmType>()) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000414 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregord6605db2009-07-22 21:30:48 +0000415 bool RecanonicalizeArg = false;
Mike Stump11289f42009-09-09 15:08:12 +0000416
Douglas Gregor60454822009-07-22 20:02:25 +0000417 // If the argument type is an array type, move the qualifiers up to the
418 // top level, so they can be matched with the qualifiers on the parameter.
419 // FIXME: address spaces, ObjC GC qualifiers
Douglas Gregord6605db2009-07-22 21:30:48 +0000420 if (isa<ArrayType>(Arg)) {
John McCall8ccfcb52009-09-24 19:53:00 +0000421 Qualifiers Quals;
Chandler Carruthc1263112010-02-07 21:33:28 +0000422 Arg = S.Context.getUnqualifiedArrayType(Arg, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +0000423 if (Quals) {
Chandler Carruthc1263112010-02-07 21:33:28 +0000424 Arg = S.Context.getQualifiedType(Arg, Quals);
Douglas Gregord6605db2009-07-22 21:30:48 +0000425 RecanonicalizeArg = true;
426 }
427 }
Mike Stump11289f42009-09-09 15:08:12 +0000428
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000429 // The argument type can not be less qualified than the parameter
430 // type.
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000431 if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000432 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
John McCall42d7d192010-08-05 09:05:08 +0000433 Info.FirstArg = TemplateArgument(Param);
John McCall0ad16662009-10-29 08:12:44 +0000434 Info.SecondArg = TemplateArgument(Arg);
John McCall42d7d192010-08-05 09:05:08 +0000435 return Sema::TDK_Underqualified;
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000436 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000437
438 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
Chandler Carruthc1263112010-02-07 21:33:28 +0000439 assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function");
John McCall8ccfcb52009-09-24 19:53:00 +0000440 QualType DeducedType = Arg;
441 DeducedType.removeCVRQualifiers(Param.getCVRQualifiers());
Douglas Gregord6605db2009-07-22 21:30:48 +0000442 if (RecanonicalizeArg)
Chandler Carruthc1263112010-02-07 21:33:28 +0000443 DeducedType = S.Context.getCanonicalType(DeducedType);
Mike Stump11289f42009-09-09 15:08:12 +0000444
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000445 if (Deduced[Index].isNull())
John McCall0ad16662009-10-29 08:12:44 +0000446 Deduced[Index] = TemplateArgument(DeducedType);
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000447 else {
Mike Stump11289f42009-09-09 15:08:12 +0000448 // C++ [temp.deduct.type]p2:
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000449 // [...] If type deduction cannot be done for any P/A pair, or if for
Mike Stump11289f42009-09-09 15:08:12 +0000450 // any pair the deduction leads to more than one possible set of
451 // deduced values, or if different pairs yield different deduced
452 // values, or if any template argument remains neither deduced nor
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000453 // explicitly specified, template argument deduction fails.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000454 if (Deduced[Index].getAsType() != DeducedType) {
Mike Stump11289f42009-09-09 15:08:12 +0000455 Info.Param
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000456 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
457 Info.FirstArg = Deduced[Index];
John McCall0ad16662009-10-29 08:12:44 +0000458 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000459 return Sema::TDK_Inconsistent;
460 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000461 }
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000462 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000463 }
464
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000465 // Set up the template argument deduction information for a failure.
John McCall0ad16662009-10-29 08:12:44 +0000466 Info.FirstArg = TemplateArgument(ParamIn);
467 Info.SecondArg = TemplateArgument(ArgIn);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000468
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000469 // Check the cv-qualifiers on the parameter and argument types.
470 if (!(TDF & TDF_IgnoreQualifiers)) {
471 if (TDF & TDF_ParamWithReferenceType) {
472 if (Param.isMoreQualifiedThan(Arg))
473 return Sema::TDK_NonDeducedMismatch;
474 } else {
475 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Mike Stump11289f42009-09-09 15:08:12 +0000476 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000477 }
478 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000479
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000480 switch (Param->getTypeClass()) {
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000481 // No deduction possible for these types
482 case Type::Builtin:
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000483 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000484
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000485 // T *
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000486 case Type::Pointer: {
John McCallbb4ea812010-05-13 07:48:05 +0000487 QualType PointeeType;
488 if (const PointerType *PointerArg = Arg->getAs<PointerType>()) {
489 PointeeType = PointerArg->getPointeeType();
490 } else if (const ObjCObjectPointerType *PointerArg
491 = Arg->getAs<ObjCObjectPointerType>()) {
492 PointeeType = PointerArg->getPointeeType();
493 } else {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000494 return Sema::TDK_NonDeducedMismatch;
John McCallbb4ea812010-05-13 07:48:05 +0000495 }
Mike Stump11289f42009-09-09 15:08:12 +0000496
Douglas Gregorfc516c92009-06-26 23:27:24 +0000497 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Chandler Carruthc1263112010-02-07 21:33:28 +0000498 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000499 cast<PointerType>(Param)->getPointeeType(),
John McCallbb4ea812010-05-13 07:48:05 +0000500 PointeeType,
Douglas Gregorfc516c92009-06-26 23:27:24 +0000501 Info, Deduced, SubTDF);
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000502 }
Mike Stump11289f42009-09-09 15:08:12 +0000503
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000504 // T &
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000505 case Type::LValueReference: {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000506 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000507 if (!ReferenceArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000508 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000509
Chandler Carruthc1263112010-02-07 21:33:28 +0000510 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000511 cast<LValueReferenceType>(Param)->getPointeeType(),
512 ReferenceArg->getPointeeType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000513 Info, Deduced, 0);
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000514 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000515
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000516 // T && [C++0x]
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000517 case Type::RValueReference: {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000518 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000519 if (!ReferenceArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000520 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000521
Chandler Carruthc1263112010-02-07 21:33:28 +0000522 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000523 cast<RValueReferenceType>(Param)->getPointeeType(),
524 ReferenceArg->getPointeeType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000525 Info, Deduced, 0);
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000526 }
Mike Stump11289f42009-09-09 15:08:12 +0000527
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000528 // T [] (implied, but not stated explicitly)
Anders Carlsson35533d12009-06-04 04:11:30 +0000529 case Type::IncompleteArray: {
Mike Stump11289f42009-09-09 15:08:12 +0000530 const IncompleteArrayType *IncompleteArrayArg =
Chandler Carruthc1263112010-02-07 21:33:28 +0000531 S.Context.getAsIncompleteArrayType(Arg);
Anders Carlsson35533d12009-06-04 04:11:30 +0000532 if (!IncompleteArrayArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000533 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000534
John McCallf7332682010-08-19 00:20:19 +0000535 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
Chandler Carruthc1263112010-02-07 21:33:28 +0000536 return DeduceTemplateArguments(S, TemplateParams,
537 S.Context.getAsIncompleteArrayType(Param)->getElementType(),
Anders Carlsson35533d12009-06-04 04:11:30 +0000538 IncompleteArrayArg->getElementType(),
John McCallf7332682010-08-19 00:20:19 +0000539 Info, Deduced, SubTDF);
Anders Carlsson35533d12009-06-04 04:11:30 +0000540 }
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000541
542 // T [integer-constant]
Anders Carlsson35533d12009-06-04 04:11:30 +0000543 case Type::ConstantArray: {
Mike Stump11289f42009-09-09 15:08:12 +0000544 const ConstantArrayType *ConstantArrayArg =
Chandler Carruthc1263112010-02-07 21:33:28 +0000545 S.Context.getAsConstantArrayType(Arg);
Anders Carlsson35533d12009-06-04 04:11:30 +0000546 if (!ConstantArrayArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000547 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000548
549 const ConstantArrayType *ConstantArrayParm =
Chandler Carruthc1263112010-02-07 21:33:28 +0000550 S.Context.getAsConstantArrayType(Param);
Anders Carlsson35533d12009-06-04 04:11:30 +0000551 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000552 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000553
John McCallf7332682010-08-19 00:20:19 +0000554 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
Chandler Carruthc1263112010-02-07 21:33:28 +0000555 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson35533d12009-06-04 04:11:30 +0000556 ConstantArrayParm->getElementType(),
557 ConstantArrayArg->getElementType(),
John McCallf7332682010-08-19 00:20:19 +0000558 Info, Deduced, SubTDF);
Anders Carlsson35533d12009-06-04 04:11:30 +0000559 }
560
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000561 // type [i]
562 case Type::DependentSizedArray: {
Chandler Carruthc1263112010-02-07 21:33:28 +0000563 const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000564 if (!ArrayArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000565 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000566
John McCallf7332682010-08-19 00:20:19 +0000567 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
568
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000569 // Check the element type of the arrays
570 const DependentSizedArrayType *DependentArrayParm
Chandler Carruthc1263112010-02-07 21:33:28 +0000571 = S.Context.getAsDependentSizedArrayType(Param);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000572 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000573 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000574 DependentArrayParm->getElementType(),
575 ArrayArg->getElementType(),
John McCallf7332682010-08-19 00:20:19 +0000576 Info, Deduced, SubTDF))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000577 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000578
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000579 // Determine the array bound is something we can deduce.
Mike Stump11289f42009-09-09 15:08:12 +0000580 NonTypeTemplateParmDecl *NTTP
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000581 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
582 if (!NTTP)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000583 return Sema::TDK_Success;
Mike Stump11289f42009-09-09 15:08:12 +0000584
585 // We can perform template argument deduction for the given non-type
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000586 // template parameter.
Mike Stump11289f42009-09-09 15:08:12 +0000587 assert(NTTP->getDepth() == 0 &&
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000588 "Cannot deduce non-type template argument at depth > 0");
Mike Stump11289f42009-09-09 15:08:12 +0000589 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson3a106e02009-06-16 22:44:31 +0000590 = dyn_cast<ConstantArrayType>(ArrayArg)) {
591 llvm::APSInt Size(ConstantArrayArg->getSize());
Douglas Gregor0a29a052010-03-26 05:50:28 +0000592 return DeduceNonTypeTemplateArgument(S, NTTP, Size,
593 S.Context.getSizeType(),
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000594 /*ArrayBound=*/true,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000595 Info, Deduced);
Anders Carlsson3a106e02009-06-16 22:44:31 +0000596 }
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000597 if (const DependentSizedArrayType *DependentArrayArg
598 = dyn_cast<DependentSizedArrayType>(ArrayArg))
Chandler Carruthc1263112010-02-07 21:33:28 +0000599 return DeduceNonTypeTemplateArgument(S, NTTP,
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000600 DependentArrayArg->getSizeExpr(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000601 Info, Deduced);
Mike Stump11289f42009-09-09 15:08:12 +0000602
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000603 // Incomplete type does not match a dependently-sized array type
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000604 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000605 }
Mike Stump11289f42009-09-09 15:08:12 +0000606
607 // type(*)(T)
608 // T(*)()
609 // T(*)(T)
Anders Carlsson2128ec72009-06-08 15:19:08 +0000610 case Type::FunctionProto: {
Mike Stump11289f42009-09-09 15:08:12 +0000611 const FunctionProtoType *FunctionProtoArg =
Anders Carlsson2128ec72009-06-08 15:19:08 +0000612 dyn_cast<FunctionProtoType>(Arg);
613 if (!FunctionProtoArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000614 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000615
616 const FunctionProtoType *FunctionProtoParam =
Anders Carlsson2128ec72009-06-08 15:19:08 +0000617 cast<FunctionProtoType>(Param);
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000618
Mike Stump11289f42009-09-09 15:08:12 +0000619 if (FunctionProtoParam->getTypeQuals() !=
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000620 FunctionProtoArg->getTypeQuals())
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000621 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000622
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000623 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000624 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000625
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000626 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000627 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000628
Anders Carlsson2128ec72009-06-08 15:19:08 +0000629 // Check return types.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000630 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000631 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000632 FunctionProtoParam->getResultType(),
633 FunctionProtoArg->getResultType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000634 Info, Deduced, 0))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000635 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000636
Anders Carlsson2128ec72009-06-08 15:19:08 +0000637 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
638 // Check argument types.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000639 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000640 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000641 FunctionProtoParam->getArgType(I),
642 FunctionProtoArg->getArgType(I),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000643 Info, Deduced, 0))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000644 return Result;
Anders Carlsson2128ec72009-06-08 15:19:08 +0000645 }
Mike Stump11289f42009-09-09 15:08:12 +0000646
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000647 return Sema::TDK_Success;
Anders Carlsson2128ec72009-06-08 15:19:08 +0000648 }
Mike Stump11289f42009-09-09 15:08:12 +0000649
John McCalle78aac42010-03-10 03:28:59 +0000650 case Type::InjectedClassName: {
651 // Treat a template's injected-class-name as if the template
652 // specialization type had been used.
John McCall2408e322010-04-27 00:57:59 +0000653 Param = cast<InjectedClassNameType>(Param)
654 ->getInjectedSpecializationType();
John McCalle78aac42010-03-10 03:28:59 +0000655 assert(isa<TemplateSpecializationType>(Param) &&
656 "injected class name is not a template specialization type");
657 // fall through
658 }
659
Douglas Gregor705c9002009-06-26 20:57:09 +0000660 // template-name<T> (where template-name refers to a class template)
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000661 // template-name<i>
Douglas Gregoradee3e32009-11-11 23:06:43 +0000662 // TT<T>
663 // TT<i>
664 // TT<>
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000665 case Type::TemplateSpecialization: {
666 const TemplateSpecializationType *SpecParam
667 = cast<TemplateSpecializationType>(Param);
Mike Stump11289f42009-09-09 15:08:12 +0000668
Douglas Gregore81f3e72009-07-07 23:09:34 +0000669 // Try to deduce template arguments from the template-id.
670 Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000671 = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000672 Info, Deduced);
Mike Stump11289f42009-09-09 15:08:12 +0000673
Douglas Gregor42909752009-09-30 22:13:51 +0000674 if (Result && (TDF & TDF_DerivedClass)) {
Douglas Gregore81f3e72009-07-07 23:09:34 +0000675 // C++ [temp.deduct.call]p3b3:
676 // If P is a class, and P has the form template-id, then A can be a
677 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump11289f42009-09-09 15:08:12 +0000678 // class of the form template-id, A can be a pointer to a derived
Douglas Gregore81f3e72009-07-07 23:09:34 +0000679 // class pointed to by the deduced A.
680 //
681 // More importantly:
Mike Stump11289f42009-09-09 15:08:12 +0000682 // These alternatives are considered only if type deduction would
Douglas Gregore81f3e72009-07-07 23:09:34 +0000683 // otherwise fail.
Chandler Carruthc1263112010-02-07 21:33:28 +0000684 if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
685 // We cannot inspect base classes as part of deduction when the type
686 // is incomplete, so either instantiate any templates necessary to
687 // complete the type, or skip over it if it cannot be completed.
John McCallbc077cf2010-02-08 23:07:23 +0000688 if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
Chandler Carruthc1263112010-02-07 21:33:28 +0000689 return Result;
690
Douglas Gregore81f3e72009-07-07 23:09:34 +0000691 // Use data recursion to crawl through the list of base classes.
Mike Stump11289f42009-09-09 15:08:12 +0000692 // Visited contains the set of nodes we have already visited, while
Douglas Gregore81f3e72009-07-07 23:09:34 +0000693 // ToVisit is our stack of records that we still need to visit.
694 llvm::SmallPtrSet<const RecordType *, 8> Visited;
695 llvm::SmallVector<const RecordType *, 8> ToVisit;
696 ToVisit.push_back(RecordT);
697 bool Successful = false;
698 while (!ToVisit.empty()) {
699 // Retrieve the next class in the inheritance hierarchy.
700 const RecordType *NextT = ToVisit.back();
701 ToVisit.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000702
Douglas Gregore81f3e72009-07-07 23:09:34 +0000703 // If we have already seen this type, skip it.
704 if (!Visited.insert(NextT))
705 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000706
Douglas Gregore81f3e72009-07-07 23:09:34 +0000707 // If this is a base class, try to perform template argument
708 // deduction from it.
709 if (NextT != RecordT) {
710 Sema::TemplateDeductionResult BaseResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000711 = DeduceTemplateArguments(S, TemplateParams, SpecParam,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000712 QualType(NextT, 0), Info, Deduced);
Mike Stump11289f42009-09-09 15:08:12 +0000713
Douglas Gregore81f3e72009-07-07 23:09:34 +0000714 // If template argument deduction for this base was successful,
715 // note that we had some success.
716 if (BaseResult == Sema::TDK_Success)
717 Successful = true;
Douglas Gregore81f3e72009-07-07 23:09:34 +0000718 }
Mike Stump11289f42009-09-09 15:08:12 +0000719
Douglas Gregore81f3e72009-07-07 23:09:34 +0000720 // Visit base classes
721 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
722 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
723 BaseEnd = Next->bases_end();
Sebastian Redl1054fae2009-10-25 17:03:50 +0000724 Base != BaseEnd; ++Base) {
Mike Stump11289f42009-09-09 15:08:12 +0000725 assert(Base->getType()->isRecordType() &&
Douglas Gregore81f3e72009-07-07 23:09:34 +0000726 "Base class that isn't a record?");
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000727 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregore81f3e72009-07-07 23:09:34 +0000728 }
729 }
Mike Stump11289f42009-09-09 15:08:12 +0000730
Douglas Gregore81f3e72009-07-07 23:09:34 +0000731 if (Successful)
732 return Sema::TDK_Success;
733 }
Mike Stump11289f42009-09-09 15:08:12 +0000734
Douglas Gregore81f3e72009-07-07 23:09:34 +0000735 }
Mike Stump11289f42009-09-09 15:08:12 +0000736
Douglas Gregore81f3e72009-07-07 23:09:34 +0000737 return Result;
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000738 }
739
Douglas Gregor637d9982009-06-10 23:47:09 +0000740 // T type::*
741 // T T::*
742 // T (type::*)()
743 // type (T::*)()
744 // type (type::*)(T)
745 // type (T::*)(T)
746 // T (type::*)(T)
747 // T (T::*)()
748 // T (T::*)(T)
749 case Type::MemberPointer: {
750 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
751 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
752 if (!MemPtrArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000753 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637d9982009-06-10 23:47:09 +0000754
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000755 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000756 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000757 MemPtrParam->getPointeeType(),
758 MemPtrArg->getPointeeType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000759 Info, Deduced,
760 TDF & TDF_IgnoreQualifiers))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000761 return Result;
762
Chandler Carruthc1263112010-02-07 21:33:28 +0000763 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000764 QualType(MemPtrParam->getClass(), 0),
765 QualType(MemPtrArg->getClass(), 0),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000766 Info, Deduced, 0);
Douglas Gregor637d9982009-06-10 23:47:09 +0000767 }
768
Anders Carlsson15f1dd12009-06-12 22:56:54 +0000769 // (clang extension)
770 //
Mike Stump11289f42009-09-09 15:08:12 +0000771 // type(^)(T)
772 // T(^)()
773 // T(^)(T)
Anders Carlssona767eee2009-06-12 16:23:10 +0000774 case Type::BlockPointer: {
775 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
776 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump11289f42009-09-09 15:08:12 +0000777
Anders Carlssona767eee2009-06-12 16:23:10 +0000778 if (!BlockPtrArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000779 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000780
Chandler Carruthc1263112010-02-07 21:33:28 +0000781 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlssona767eee2009-06-12 16:23:10 +0000782 BlockPtrParam->getPointeeType(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000783 BlockPtrArg->getPointeeType(), Info,
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000784 Deduced, 0);
Anders Carlssona767eee2009-06-12 16:23:10 +0000785 }
786
Douglas Gregor637d9982009-06-10 23:47:09 +0000787 case Type::TypeOfExpr:
788 case Type::TypeOf:
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000789 case Type::DependentName:
Douglas Gregor637d9982009-06-10 23:47:09 +0000790 // No template argument deduction for these types
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000791 return Sema::TDK_Success;
Douglas Gregor637d9982009-06-10 23:47:09 +0000792
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000793 default:
794 break;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000795 }
796
797 // FIXME: Many more cases to go (to go).
Douglas Gregor705c9002009-06-26 20:57:09 +0000798 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000799}
800
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000801static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000802DeduceTemplateArguments(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000803 TemplateParameterList *TemplateParams,
804 const TemplateArgument &Param,
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000805 const TemplateArgument &Arg,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000806 Sema::TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000807 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000808 switch (Param.getKind()) {
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000809 case TemplateArgument::Null:
810 assert(false && "Null template argument in parameter list");
811 break;
Mike Stump11289f42009-09-09 15:08:12 +0000812
813 case TemplateArgument::Type:
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000814 if (Arg.getKind() == TemplateArgument::Type)
Chandler Carruthc1263112010-02-07 21:33:28 +0000815 return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000816 Arg.getAsType(), Info, Deduced, 0);
817 Info.FirstArg = Param;
818 Info.SecondArg = Arg;
819 return Sema::TDK_NonDeducedMismatch;
820
821 case TemplateArgument::Template:
Douglas Gregoradee3e32009-11-11 23:06:43 +0000822 if (Arg.getKind() == TemplateArgument::Template)
Chandler Carruthc1263112010-02-07 21:33:28 +0000823 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000824 Param.getAsTemplate(),
Douglas Gregoradee3e32009-11-11 23:06:43 +0000825 Arg.getAsTemplate(), Info, Deduced);
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000826 Info.FirstArg = Param;
827 Info.SecondArg = Arg;
828 return Sema::TDK_NonDeducedMismatch;
829
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000830 case TemplateArgument::Declaration:
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000831 if (Arg.getKind() == TemplateArgument::Declaration &&
832 Param.getAsDecl()->getCanonicalDecl() ==
833 Arg.getAsDecl()->getCanonicalDecl())
834 return Sema::TDK_Success;
835
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000836 Info.FirstArg = Param;
837 Info.SecondArg = Arg;
838 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000839
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000840 case TemplateArgument::Integral:
841 if (Arg.getKind() == TemplateArgument::Integral) {
Douglas Gregor0a29a052010-03-26 05:50:28 +0000842 if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral()))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000843 return Sema::TDK_Success;
844
845 Info.FirstArg = Param;
846 Info.SecondArg = Arg;
847 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000848 }
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000849
850 if (Arg.getKind() == TemplateArgument::Expression) {
851 Info.FirstArg = Param;
852 Info.SecondArg = Arg;
853 return Sema::TDK_NonDeducedMismatch;
854 }
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000855
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000856 Info.FirstArg = Param;
857 Info.SecondArg = Arg;
858 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000859
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000860 case TemplateArgument::Expression: {
Mike Stump11289f42009-09-09 15:08:12 +0000861 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000862 = getDeducedParameterFromExpr(Param.getAsExpr())) {
863 if (Arg.getKind() == TemplateArgument::Integral)
Chandler Carruthc1263112010-02-07 21:33:28 +0000864 return DeduceNonTypeTemplateArgument(S, NTTP,
Mike Stump11289f42009-09-09 15:08:12 +0000865 *Arg.getAsIntegral(),
Douglas Gregor0a29a052010-03-26 05:50:28 +0000866 Arg.getIntegralType(),
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000867 /*ArrayBound=*/false,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000868 Info, Deduced);
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000869 if (Arg.getKind() == TemplateArgument::Expression)
Chandler Carruthc1263112010-02-07 21:33:28 +0000870 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000871 Info, Deduced);
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000872 if (Arg.getKind() == TemplateArgument::Declaration)
Chandler Carruthc1263112010-02-07 21:33:28 +0000873 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000874 Info, Deduced);
875
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000876 Info.FirstArg = Param;
877 Info.SecondArg = Arg;
878 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000879 }
Mike Stump11289f42009-09-09 15:08:12 +0000880
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000881 // Can't deduce anything, but that's okay.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000882 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000883 }
Anders Carlssonbc343912009-06-15 17:04:53 +0000884 case TemplateArgument::Pack:
885 assert(0 && "FIXME: Implement!");
886 break;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000887 }
Mike Stump11289f42009-09-09 15:08:12 +0000888
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000889 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000890}
891
Mike Stump11289f42009-09-09 15:08:12 +0000892static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000893DeduceTemplateArguments(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000894 TemplateParameterList *TemplateParams,
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000895 const TemplateArgumentList &ParamList,
896 const TemplateArgumentList &ArgList,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000897 Sema::TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000898 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000899 assert(ParamList.size() == ArgList.size());
900 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000901 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000902 = DeduceTemplateArguments(S, TemplateParams,
Mike Stump11289f42009-09-09 15:08:12 +0000903 ParamList[I], ArgList[I],
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000904 Info, Deduced))
905 return Result;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000906 }
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000907 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000908}
909
Douglas Gregor705c9002009-06-26 20:57:09 +0000910/// \brief Determine whether two template arguments are the same.
Mike Stump11289f42009-09-09 15:08:12 +0000911static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregor705c9002009-06-26 20:57:09 +0000912 const TemplateArgument &X,
913 const TemplateArgument &Y) {
914 if (X.getKind() != Y.getKind())
915 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000916
Douglas Gregor705c9002009-06-26 20:57:09 +0000917 switch (X.getKind()) {
918 case TemplateArgument::Null:
919 assert(false && "Comparing NULL template argument");
920 break;
Mike Stump11289f42009-09-09 15:08:12 +0000921
Douglas Gregor705c9002009-06-26 20:57:09 +0000922 case TemplateArgument::Type:
923 return Context.getCanonicalType(X.getAsType()) ==
924 Context.getCanonicalType(Y.getAsType());
Mike Stump11289f42009-09-09 15:08:12 +0000925
Douglas Gregor705c9002009-06-26 20:57:09 +0000926 case TemplateArgument::Declaration:
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +0000927 return X.getAsDecl()->getCanonicalDecl() ==
928 Y.getAsDecl()->getCanonicalDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000929
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000930 case TemplateArgument::Template:
931 return Context.getCanonicalTemplateName(X.getAsTemplate())
932 .getAsVoidPointer() ==
933 Context.getCanonicalTemplateName(Y.getAsTemplate())
934 .getAsVoidPointer();
935
Douglas Gregor705c9002009-06-26 20:57:09 +0000936 case TemplateArgument::Integral:
937 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump11289f42009-09-09 15:08:12 +0000938
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000939 case TemplateArgument::Expression: {
940 llvm::FoldingSetNodeID XID, YID;
941 X.getAsExpr()->Profile(XID, Context, true);
942 Y.getAsExpr()->Profile(YID, Context, true);
943 return XID == YID;
944 }
Mike Stump11289f42009-09-09 15:08:12 +0000945
Douglas Gregor705c9002009-06-26 20:57:09 +0000946 case TemplateArgument::Pack:
947 if (X.pack_size() != Y.pack_size())
948 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000949
950 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
951 XPEnd = X.pack_end(),
Douglas Gregor705c9002009-06-26 20:57:09 +0000952 YP = Y.pack_begin();
Mike Stump11289f42009-09-09 15:08:12 +0000953 XP != XPEnd; ++XP, ++YP)
Douglas Gregor705c9002009-06-26 20:57:09 +0000954 if (!isSameTemplateArg(Context, *XP, *YP))
955 return false;
956
957 return true;
958 }
959
960 return false;
961}
962
963/// \brief Helper function to build a TemplateParameter when we don't
964/// know its type statically.
965static TemplateParameter makeTemplateParameter(Decl *D) {
966 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
967 return TemplateParameter(TTP);
968 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
969 return TemplateParameter(NTTP);
Mike Stump11289f42009-09-09 15:08:12 +0000970
Douglas Gregor705c9002009-06-26 20:57:09 +0000971 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
972}
973
Douglas Gregor684268d2010-04-29 06:21:43 +0000974/// Complete template argument deduction for a class template partial
975/// specialization.
976static Sema::TemplateDeductionResult
977FinishTemplateArgumentDeduction(Sema &S,
978 ClassTemplatePartialSpecializationDecl *Partial,
979 const TemplateArgumentList &TemplateArgs,
980 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
981 Sema::TemplateDeductionInfo &Info) {
982 // Trap errors.
983 Sema::SFINAETrap Trap(S);
984
985 Sema::ContextRAII SavedContext(S, Partial);
986
987 // C++ [temp.deduct.type]p2:
988 // [...] or if any template argument remains neither deduced nor
989 // explicitly specified, template argument deduction fails.
990 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
991 Deduced.size());
992 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
993 if (Deduced[I].isNull()) {
994 Decl *Param
995 = const_cast<NamedDecl *>(
996 Partial->getTemplateParameters()->getParam(I));
997 Info.Param = makeTemplateParameter(Param);
998 return Sema::TDK_Incomplete;
999 }
1000
1001 Builder.Append(Deduced[I]);
1002 }
1003
1004 // Form the template argument list from the deduced template arguments.
1005 TemplateArgumentList *DeducedArgumentList
1006 = new (S.Context) TemplateArgumentList(S.Context, Builder,
1007 /*TakeArgs=*/true);
1008 Info.reset(DeducedArgumentList);
1009
1010 // Substitute the deduced template arguments into the template
1011 // arguments of the class template partial specialization, and
1012 // verify that the instantiated template arguments are both valid
1013 // and are equivalent to the template arguments originally provided
1014 // to the class template.
1015 // FIXME: Do we have to correct the types of deduced non-type template
1016 // arguments (in particular, integral non-type template arguments?).
1017 Sema::LocalInstantiationScope InstScope(S);
1018 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
1019 const TemplateArgumentLoc *PartialTemplateArgs
1020 = Partial->getTemplateArgsAsWritten();
1021 unsigned N = Partial->getNumTemplateArgsAsWritten();
1022
1023 // Note that we don't provide the langle and rangle locations.
1024 TemplateArgumentListInfo InstArgs;
1025
1026 for (unsigned I = 0; I != N; ++I) {
1027 Decl *Param = const_cast<NamedDecl *>(
1028 ClassTemplate->getTemplateParameters()->getParam(I));
1029 TemplateArgumentLoc InstArg;
1030 if (S.Subst(PartialTemplateArgs[I], InstArg,
1031 MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
1032 Info.Param = makeTemplateParameter(Param);
1033 Info.FirstArg = PartialTemplateArgs[I].getArgument();
1034 return Sema::TDK_SubstitutionFailure;
1035 }
1036 InstArgs.addArgument(InstArg);
1037 }
1038
1039 TemplateArgumentListBuilder ConvertedInstArgs(
1040 ClassTemplate->getTemplateParameters(), N);
1041
1042 if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
Douglas Gregord09efd42010-05-08 20:07:26 +00001043 InstArgs, false, ConvertedInstArgs))
Douglas Gregor684268d2010-04-29 06:21:43 +00001044 return Sema::TDK_SubstitutionFailure;
Douglas Gregor684268d2010-04-29 06:21:43 +00001045
1046 for (unsigned I = 0, E = ConvertedInstArgs.flatSize(); I != E; ++I) {
1047 TemplateArgument InstArg = ConvertedInstArgs.getFlatArguments()[I];
1048
1049 Decl *Param = const_cast<NamedDecl *>(
1050 ClassTemplate->getTemplateParameters()->getParam(I));
1051
1052 if (InstArg.getKind() == TemplateArgument::Expression) {
1053 // When the argument is an expression, check the expression result
1054 // against the actual template parameter to get down to the canonical
1055 // template argument.
1056 Expr *InstExpr = InstArg.getAsExpr();
1057 if (NonTypeTemplateParmDecl *NTTP
1058 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1059 if (S.CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
1060 Info.Param = makeTemplateParameter(Param);
1061 Info.FirstArg = Partial->getTemplateArgs()[I];
1062 return Sema::TDK_SubstitutionFailure;
1063 }
1064 }
1065 }
1066
1067 if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
1068 Info.Param = makeTemplateParameter(Param);
1069 Info.FirstArg = TemplateArgs[I];
1070 Info.SecondArg = InstArg;
1071 return Sema::TDK_NonDeducedMismatch;
1072 }
1073 }
1074
1075 if (Trap.hasErrorOccurred())
1076 return Sema::TDK_SubstitutionFailure;
1077
1078 return Sema::TDK_Success;
1079}
1080
Douglas Gregor170bc422009-06-12 22:31:52 +00001081/// \brief Perform template argument deduction to determine whether
1082/// the given template arguments match the given class template
1083/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001084Sema::TemplateDeductionResult
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001085Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001086 const TemplateArgumentList &TemplateArgs,
1087 TemplateDeductionInfo &Info) {
Douglas Gregor170bc422009-06-12 22:31:52 +00001088 // C++ [temp.class.spec.match]p2:
1089 // A partial specialization matches a given actual template
1090 // argument list if the template arguments of the partial
1091 // specialization can be deduced from the actual template argument
1092 // list (14.8.2).
Douglas Gregore1416332009-06-14 08:02:22 +00001093 SFINAETrap Trap(*this);
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001094 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001095 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001096 if (TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +00001097 = ::DeduceTemplateArguments(*this,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001098 Partial->getTemplateParameters(),
Mike Stump11289f42009-09-09 15:08:12 +00001099 Partial->getTemplateArgs(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001100 TemplateArgs, Info, Deduced))
1101 return Result;
Douglas Gregor637d9982009-06-10 23:47:09 +00001102
Douglas Gregor637d9982009-06-10 23:47:09 +00001103 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
1104 Deduced.data(), Deduced.size());
1105 if (Inst)
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001106 return TDK_InstantiationDepth;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001107
Douglas Gregore1416332009-06-14 08:02:22 +00001108 if (Trap.hasErrorOccurred())
Douglas Gregor684268d2010-04-29 06:21:43 +00001109 return Sema::TDK_SubstitutionFailure;
1110
1111 return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
1112 Deduced, Info);
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001113}
Douglas Gregor91772d12009-06-13 00:26:55 +00001114
Douglas Gregorfc516c92009-06-26 23:27:24 +00001115/// \brief Determine whether the given type T is a simple-template-id type.
1116static bool isSimpleTemplateIdType(QualType T) {
Mike Stump11289f42009-09-09 15:08:12 +00001117 if (const TemplateSpecializationType *Spec
John McCall9dd450b2009-09-21 23:43:11 +00001118 = T->getAs<TemplateSpecializationType>())
Douglas Gregorfc516c92009-06-26 23:27:24 +00001119 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump11289f42009-09-09 15:08:12 +00001120
Douglas Gregorfc516c92009-06-26 23:27:24 +00001121 return false;
1122}
Douglas Gregor9b146582009-07-08 20:55:45 +00001123
1124/// \brief Substitute the explicitly-provided template arguments into the
1125/// given function template according to C++ [temp.arg.explicit].
1126///
1127/// \param FunctionTemplate the function template into which the explicit
1128/// template arguments will be substituted.
1129///
Mike Stump11289f42009-09-09 15:08:12 +00001130/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor9b146582009-07-08 20:55:45 +00001131/// arguments.
1132///
Mike Stump11289f42009-09-09 15:08:12 +00001133/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor9b146582009-07-08 20:55:45 +00001134/// with the converted and checked explicit template arguments.
1135///
Mike Stump11289f42009-09-09 15:08:12 +00001136/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor9b146582009-07-08 20:55:45 +00001137/// parameters.
1138///
1139/// \param FunctionType if non-NULL, the result type of the function template
1140/// will also be instantiated and the pointed-to value will be updated with
1141/// the instantiated function type.
1142///
1143/// \param Info if substitution fails for any reason, this object will be
1144/// populated with more information about the failure.
1145///
1146/// \returns TDK_Success if substitution was successful, or some failure
1147/// condition.
1148Sema::TemplateDeductionResult
1149Sema::SubstituteExplicitTemplateArguments(
1150 FunctionTemplateDecl *FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001151 const TemplateArgumentListInfo &ExplicitTemplateArgs,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001152 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor9b146582009-07-08 20:55:45 +00001153 llvm::SmallVectorImpl<QualType> &ParamTypes,
1154 QualType *FunctionType,
1155 TemplateDeductionInfo &Info) {
1156 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1157 TemplateParameterList *TemplateParams
1158 = FunctionTemplate->getTemplateParameters();
1159
John McCall6b51f282009-11-23 01:53:49 +00001160 if (ExplicitTemplateArgs.size() == 0) {
Douglas Gregor9b146582009-07-08 20:55:45 +00001161 // No arguments to substitute; just copy over the parameter types and
1162 // fill in the function type.
1163 for (FunctionDecl::param_iterator P = Function->param_begin(),
1164 PEnd = Function->param_end();
1165 P != PEnd;
1166 ++P)
1167 ParamTypes.push_back((*P)->getType());
Mike Stump11289f42009-09-09 15:08:12 +00001168
Douglas Gregor9b146582009-07-08 20:55:45 +00001169 if (FunctionType)
1170 *FunctionType = Function->getType();
1171 return TDK_Success;
1172 }
Mike Stump11289f42009-09-09 15:08:12 +00001173
Douglas Gregor9b146582009-07-08 20:55:45 +00001174 // Substitution of the explicit template arguments into a function template
1175 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump11289f42009-09-09 15:08:12 +00001176 SFINAETrap Trap(*this);
1177
Douglas Gregor9b146582009-07-08 20:55:45 +00001178 // C++ [temp.arg.explicit]p3:
Mike Stump11289f42009-09-09 15:08:12 +00001179 // Template arguments that are present shall be specified in the
1180 // declaration order of their corresponding template-parameters. The
Douglas Gregor9b146582009-07-08 20:55:45 +00001181 // template argument list shall not specify more template-arguments than
Mike Stump11289f42009-09-09 15:08:12 +00001182 // there are corresponding template-parameters.
1183 TemplateArgumentListBuilder Builder(TemplateParams,
John McCall6b51f282009-11-23 01:53:49 +00001184 ExplicitTemplateArgs.size());
Mike Stump11289f42009-09-09 15:08:12 +00001185
1186 // Enter a new template instantiation context where we check the
Douglas Gregor9b146582009-07-08 20:55:45 +00001187 // explicitly-specified template arguments against this function template,
1188 // and then substitute them into the function parameter types.
Mike Stump11289f42009-09-09 15:08:12 +00001189 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor9b146582009-07-08 20:55:45 +00001190 FunctionTemplate, Deduced.data(), Deduced.size(),
1191 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution);
1192 if (Inst)
1193 return TDK_InstantiationDepth;
Mike Stump11289f42009-09-09 15:08:12 +00001194
John McCalle23b8712010-04-29 01:18:58 +00001195 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
John McCall80e58cd2010-04-29 00:35:03 +00001196
Douglas Gregor9b146582009-07-08 20:55:45 +00001197 if (CheckTemplateArgumentList(FunctionTemplate,
Douglas Gregor9b146582009-07-08 20:55:45 +00001198 SourceLocation(),
John McCall6b51f282009-11-23 01:53:49 +00001199 ExplicitTemplateArgs,
Douglas Gregor9b146582009-07-08 20:55:45 +00001200 true,
Douglas Gregor1d72edd2010-05-08 19:15:54 +00001201 Builder) || Trap.hasErrorOccurred()) {
Douglas Gregor62c281a2010-05-09 01:26:06 +00001202 unsigned Index = Builder.structuredSize();
1203 if (Index >= TemplateParams->size())
1204 Index = TemplateParams->size() - 1;
1205 Info.Param = makeTemplateParameter(TemplateParams->getParam(Index));
Douglas Gregor9b146582009-07-08 20:55:45 +00001206 return TDK_InvalidExplicitArguments;
Douglas Gregor1d72edd2010-05-08 19:15:54 +00001207 }
Mike Stump11289f42009-09-09 15:08:12 +00001208
Douglas Gregor9b146582009-07-08 20:55:45 +00001209 // Form the template argument list from the explicitly-specified
1210 // template arguments.
Mike Stump11289f42009-09-09 15:08:12 +00001211 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor9b146582009-07-08 20:55:45 +00001212 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1213 Info.reset(ExplicitArgumentList);
Mike Stump11289f42009-09-09 15:08:12 +00001214
Douglas Gregor9b146582009-07-08 20:55:45 +00001215 // Instantiate the types of each of the function parameters given the
1216 // explicitly-specified template arguments.
1217 for (FunctionDecl::param_iterator P = Function->param_begin(),
1218 PEnd = Function->param_end();
1219 P != PEnd;
1220 ++P) {
Mike Stump11289f42009-09-09 15:08:12 +00001221 QualType ParamType
1222 = SubstType((*P)->getType(),
Douglas Gregor39cacdb2009-08-28 20:50:45 +00001223 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1224 (*P)->getLocation(), (*P)->getDeclName());
Douglas Gregor9b146582009-07-08 20:55:45 +00001225 if (ParamType.isNull() || Trap.hasErrorOccurred())
1226 return TDK_SubstitutionFailure;
Mike Stump11289f42009-09-09 15:08:12 +00001227
Douglas Gregor9b146582009-07-08 20:55:45 +00001228 ParamTypes.push_back(ParamType);
1229 }
1230
1231 // If the caller wants a full function type back, instantiate the return
1232 // type and form that function type.
1233 if (FunctionType) {
1234 // FIXME: exception-specifications?
Mike Stump11289f42009-09-09 15:08:12 +00001235 const FunctionProtoType *Proto
John McCall9dd450b2009-09-21 23:43:11 +00001236 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor9b146582009-07-08 20:55:45 +00001237 assert(Proto && "Function template does not have a prototype?");
Mike Stump11289f42009-09-09 15:08:12 +00001238
1239 QualType ResultType
Douglas Gregor39cacdb2009-08-28 20:50:45 +00001240 = SubstType(Proto->getResultType(),
1241 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1242 Function->getTypeSpecStartLoc(),
1243 Function->getDeclName());
Douglas Gregor9b146582009-07-08 20:55:45 +00001244 if (ResultType.isNull() || Trap.hasErrorOccurred())
1245 return TDK_SubstitutionFailure;
Mike Stump11289f42009-09-09 15:08:12 +00001246
1247 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor9b146582009-07-08 20:55:45 +00001248 ParamTypes.data(), ParamTypes.size(),
1249 Proto->isVariadic(),
1250 Proto->getTypeQuals(),
1251 Function->getLocation(),
Eli Friedmand8725a92010-08-05 02:54:05 +00001252 Function->getDeclName(),
1253 Proto->getExtInfo());
Douglas Gregor9b146582009-07-08 20:55:45 +00001254 if (FunctionType->isNull() || Trap.hasErrorOccurred())
1255 return TDK_SubstitutionFailure;
1256 }
Mike Stump11289f42009-09-09 15:08:12 +00001257
Douglas Gregor9b146582009-07-08 20:55:45 +00001258 // C++ [temp.arg.explicit]p2:
Mike Stump11289f42009-09-09 15:08:12 +00001259 // Trailing template arguments that can be deduced (14.8.2) may be
1260 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor9b146582009-07-08 20:55:45 +00001261 // template arguments can be deduced, they may all be omitted; in this
1262 // case, the empty template argument list <> itself may also be omitted.
1263 //
1264 // Take all of the explicitly-specified arguments and put them into the
Mike Stump11289f42009-09-09 15:08:12 +00001265 // set of deduced template arguments.
Douglas Gregor9b146582009-07-08 20:55:45 +00001266 Deduced.reserve(TemplateParams->size());
1267 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001268 Deduced.push_back(ExplicitArgumentList->get(I));
1269
Douglas Gregor9b146582009-07-08 20:55:45 +00001270 return TDK_Success;
1271}
1272
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001273/// \brief Allocate a TemplateArgumentLoc where all locations have
1274/// been initialized to the given location.
1275///
1276/// \param S The semantic analysis object.
1277///
1278/// \param The template argument we are producing template argument
1279/// location information for.
1280///
1281/// \param NTTPType For a declaration template argument, the type of
1282/// the non-type template parameter that corresponds to this template
1283/// argument.
1284///
1285/// \param Loc The source location to use for the resulting template
1286/// argument.
1287static TemplateArgumentLoc
1288getTrivialTemplateArgumentLoc(Sema &S,
1289 const TemplateArgument &Arg,
1290 QualType NTTPType,
1291 SourceLocation Loc) {
1292 switch (Arg.getKind()) {
1293 case TemplateArgument::Null:
1294 llvm_unreachable("Can't get a NULL template argument here");
1295 break;
1296
1297 case TemplateArgument::Type:
1298 return TemplateArgumentLoc(Arg,
1299 S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
1300
1301 case TemplateArgument::Declaration: {
1302 Expr *E
1303 = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
1304 .takeAs<Expr>();
1305 return TemplateArgumentLoc(TemplateArgument(E), E);
1306 }
1307
1308 case TemplateArgument::Integral: {
1309 Expr *E
1310 = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>();
1311 return TemplateArgumentLoc(TemplateArgument(E), E);
1312 }
1313
1314 case TemplateArgument::Template:
1315 return TemplateArgumentLoc(Arg, SourceRange(), Loc);
1316
1317 case TemplateArgument::Expression:
1318 return TemplateArgumentLoc(Arg, Arg.getAsExpr());
1319
1320 case TemplateArgument::Pack:
1321 llvm_unreachable("Template parameter packs are not yet supported");
1322 }
1323
1324 return TemplateArgumentLoc();
1325}
1326
Mike Stump11289f42009-09-09 15:08:12 +00001327/// \brief Finish template argument deduction for a function template,
Douglas Gregor9b146582009-07-08 20:55:45 +00001328/// checking the deduced template arguments for completeness and forming
1329/// the function template specialization.
Mike Stump11289f42009-09-09 15:08:12 +00001330Sema::TemplateDeductionResult
Douglas Gregor9b146582009-07-08 20:55:45 +00001331Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001332 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1333 unsigned NumExplicitlySpecified,
Douglas Gregor9b146582009-07-08 20:55:45 +00001334 FunctionDecl *&Specialization,
1335 TemplateDeductionInfo &Info) {
1336 TemplateParameterList *TemplateParams
1337 = FunctionTemplate->getTemplateParameters();
Mike Stump11289f42009-09-09 15:08:12 +00001338
Douglas Gregor9b146582009-07-08 20:55:45 +00001339 // Template argument deduction for function templates in a SFINAE context.
1340 // Trap any errors that might occur.
Mike Stump11289f42009-09-09 15:08:12 +00001341 SFINAETrap Trap(*this);
1342
Douglas Gregor9b146582009-07-08 20:55:45 +00001343 // Enter a new template instantiation context while we instantiate the
1344 // actual function declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001345 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor9b146582009-07-08 20:55:45 +00001346 FunctionTemplate, Deduced.data(), Deduced.size(),
1347 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
1348 if (Inst)
Mike Stump11289f42009-09-09 15:08:12 +00001349 return TDK_InstantiationDepth;
1350
John McCalle23b8712010-04-29 01:18:58 +00001351 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
John McCall80e58cd2010-04-29 00:35:03 +00001352
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001353 // C++ [temp.deduct.type]p2:
1354 // [...] or if any template argument remains neither deduced nor
1355 // explicitly specified, template argument deduction fails.
1356 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
1357 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001358 NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I);
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001359 if (!Deduced[I].isNull()) {
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001360 if (I < NumExplicitlySpecified ||
1361 Deduced[I].getKind() == TemplateArgument::Type) {
1362 // We have already fully type-checked and converted this
1363 // argument (because it was explicitly-specified) or no
1364 // additional checking is necessary (because it's a template
1365 // type parameter). Just record the presence of this
1366 // parameter.
1367 Builder.Append(Deduced[I]);
1368 continue;
1369 }
1370
1371 // We have deduced this argument, so it still needs to be
1372 // checked and converted.
1373
1374 // First, for a non-type template parameter type that is
1375 // initialized by a declaration, we need the type of the
1376 // corresponding non-type template parameter.
1377 QualType NTTPType;
1378 if (NonTypeTemplateParmDecl *NTTP
1379 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1380 if (Deduced[I].getKind() == TemplateArgument::Declaration) {
1381 NTTPType = NTTP->getType();
1382 if (NTTPType->isDependentType()) {
1383 TemplateArgumentList TemplateArgs(Context, Builder,
1384 /*TakeArgs=*/false);
1385 NTTPType = SubstType(NTTPType,
1386 MultiLevelTemplateArgumentList(TemplateArgs),
1387 NTTP->getLocation(),
1388 NTTP->getDeclName());
1389 if (NTTPType.isNull()) {
1390 Info.Param = makeTemplateParameter(Param);
Douglas Gregord09efd42010-05-08 20:07:26 +00001391 Info.reset(new (Context) TemplateArgumentList(Context, Builder,
1392 /*TakeArgs=*/true));
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001393 return TDK_SubstitutionFailure;
1394 }
1395 }
1396 }
1397 }
1398
1399 // Convert the deduced template argument into a template
1400 // argument that we can check, almost as if the user had written
1401 // the template argument explicitly.
1402 TemplateArgumentLoc Arg = getTrivialTemplateArgumentLoc(*this,
1403 Deduced[I],
1404 NTTPType,
1405 SourceLocation());
1406
1407 // Check the template argument, converting it as necessary.
1408 if (CheckTemplateArgument(Param, Arg,
1409 FunctionTemplate,
1410 FunctionTemplate->getLocation(),
1411 FunctionTemplate->getSourceRange().getEnd(),
1412 Builder,
1413 Deduced[I].wasDeducedFromArrayBound()
1414 ? CTAK_DeducedFromArrayBound
1415 : CTAK_Deduced)) {
1416 Info.Param = makeTemplateParameter(
1417 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregord09efd42010-05-08 20:07:26 +00001418 Info.reset(new (Context) TemplateArgumentList(Context, Builder,
1419 /*TakeArgs=*/true));
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001420 return TDK_SubstitutionFailure;
1421 }
1422
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001423 continue;
1424 }
1425
1426 // Substitute into the default template argument, if available.
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001427 TemplateArgumentLoc DefArg
1428 = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
1429 FunctionTemplate->getLocation(),
1430 FunctionTemplate->getSourceRange().getEnd(),
1431 Param,
1432 Builder);
1433
1434 // If there was no default argument, deduction is incomplete.
1435 if (DefArg.getArgument().isNull()) {
1436 Info.Param = makeTemplateParameter(
1437 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1438 return TDK_Incomplete;
1439 }
1440
1441 // Check whether we can actually use the default argument.
1442 if (CheckTemplateArgument(Param, DefArg,
1443 FunctionTemplate,
1444 FunctionTemplate->getLocation(),
1445 FunctionTemplate->getSourceRange().getEnd(),
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001446 Builder,
1447 CTAK_Deduced)) {
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001448 Info.Param = makeTemplateParameter(
1449 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregord09efd42010-05-08 20:07:26 +00001450 Info.reset(new (Context) TemplateArgumentList(Context, Builder,
1451 /*TakeArgs=*/true));
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001452 return TDK_SubstitutionFailure;
1453 }
1454
1455 // If we get here, we successfully used the default template argument.
1456 }
1457
1458 // Form the template argument list from the deduced template arguments.
1459 TemplateArgumentList *DeducedArgumentList
1460 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1461 Info.reset(DeducedArgumentList);
1462
Mike Stump11289f42009-09-09 15:08:12 +00001463 // Substitute the deduced template arguments into the function template
Douglas Gregor9b146582009-07-08 20:55:45 +00001464 // declaration to produce the function template specialization.
Douglas Gregor16142372010-04-28 04:52:24 +00001465 DeclContext *Owner = FunctionTemplate->getDeclContext();
1466 if (FunctionTemplate->getFriendObjectKind())
1467 Owner = FunctionTemplate->getLexicalDeclContext();
Douglas Gregor9b146582009-07-08 20:55:45 +00001468 Specialization = cast_or_null<FunctionDecl>(
Douglas Gregor16142372010-04-28 04:52:24 +00001469 SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner,
Douglas Gregor39cacdb2009-08-28 20:50:45 +00001470 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor9b146582009-07-08 20:55:45 +00001471 if (!Specialization)
1472 return TDK_SubstitutionFailure;
Mike Stump11289f42009-09-09 15:08:12 +00001473
Douglas Gregor31fae892009-09-15 18:26:13 +00001474 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
1475 FunctionTemplate->getCanonicalDecl());
1476
Mike Stump11289f42009-09-09 15:08:12 +00001477 // If the template argument list is owned by the function template
Douglas Gregor9b146582009-07-08 20:55:45 +00001478 // specialization, release it.
Douglas Gregord09efd42010-05-08 20:07:26 +00001479 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList &&
1480 !Trap.hasErrorOccurred())
Douglas Gregor9b146582009-07-08 20:55:45 +00001481 Info.take();
Mike Stump11289f42009-09-09 15:08:12 +00001482
Douglas Gregor9b146582009-07-08 20:55:45 +00001483 // There may have been an error that did not prevent us from constructing a
1484 // declaration. Mark the declaration invalid and return with a substitution
1485 // failure.
1486 if (Trap.hasErrorOccurred()) {
1487 Specialization->setInvalidDecl(true);
1488 return TDK_SubstitutionFailure;
1489 }
Mike Stump11289f42009-09-09 15:08:12 +00001490
1491 return TDK_Success;
Douglas Gregor9b146582009-07-08 20:55:45 +00001492}
1493
John McCallc1f69982010-02-02 02:21:27 +00001494static QualType GetTypeOfFunction(ASTContext &Context,
1495 bool isAddressOfOperand,
1496 FunctionDecl *Fn) {
1497 if (!isAddressOfOperand) return Fn->getType();
1498 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
1499 if (Method->isInstance())
1500 return Context.getMemberPointerType(Fn->getType(),
1501 Context.getTypeDeclType(Method->getParent()).getTypePtr());
1502 return Context.getPointerType(Fn->getType());
1503}
1504
1505/// Apply the deduction rules for overload sets.
1506///
1507/// \return the null type if this argument should be treated as an
1508/// undeduced context
1509static QualType
1510ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
1511 Expr *Arg, QualType ParamType) {
John McCall1acbbb52010-02-02 06:20:04 +00001512 llvm::PointerIntPair<OverloadExpr*,1> R = OverloadExpr::find(Arg);
John McCallc1f69982010-02-02 02:21:27 +00001513
John McCall1acbbb52010-02-02 06:20:04 +00001514 bool isAddressOfOperand = bool(R.getInt());
1515 OverloadExpr *Ovl = R.getPointer();
John McCallc1f69982010-02-02 02:21:27 +00001516
1517 // If there were explicit template arguments, we can only find
1518 // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
1519 // unambiguously name a full specialization.
John McCall1acbbb52010-02-02 06:20:04 +00001520 if (Ovl->hasExplicitTemplateArgs()) {
John McCallc1f69982010-02-02 02:21:27 +00001521 // But we can still look for an explicit specialization.
1522 if (FunctionDecl *ExplicitSpec
John McCall1acbbb52010-02-02 06:20:04 +00001523 = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
1524 return GetTypeOfFunction(S.Context, isAddressOfOperand, ExplicitSpec);
John McCallc1f69982010-02-02 02:21:27 +00001525 return QualType();
1526 }
1527
1528 // C++0x [temp.deduct.call]p6:
1529 // When P is a function type, pointer to function type, or pointer
1530 // to member function type:
1531
1532 if (!ParamType->isFunctionType() &&
1533 !ParamType->isFunctionPointerType() &&
1534 !ParamType->isMemberFunctionPointerType())
1535 return QualType();
1536
1537 QualType Match;
John McCall1acbbb52010-02-02 06:20:04 +00001538 for (UnresolvedSetIterator I = Ovl->decls_begin(),
1539 E = Ovl->decls_end(); I != E; ++I) {
John McCallc1f69982010-02-02 02:21:27 +00001540 NamedDecl *D = (*I)->getUnderlyingDecl();
1541
1542 // - If the argument is an overload set containing one or more
1543 // function templates, the parameter is treated as a
1544 // non-deduced context.
1545 if (isa<FunctionTemplateDecl>(D))
1546 return QualType();
1547
1548 FunctionDecl *Fn = cast<FunctionDecl>(D);
1549 QualType ArgType = GetTypeOfFunction(S.Context, isAddressOfOperand, Fn);
1550
1551 // - If the argument is an overload set (not containing function
1552 // templates), trial argument deduction is attempted using each
1553 // of the members of the set. If deduction succeeds for only one
1554 // of the overload set members, that member is used as the
1555 // argument value for the deduction. If deduction succeeds for
1556 // more than one member of the overload set the parameter is
1557 // treated as a non-deduced context.
1558
1559 // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
1560 // Type deduction is done independently for each P/A pair, and
1561 // the deduced template argument values are then combined.
1562 // So we do not reject deductions which were made elsewhere.
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001563 llvm::SmallVector<DeducedTemplateArgument, 8>
1564 Deduced(TemplateParams->size());
John McCallbc077cf2010-02-08 23:07:23 +00001565 Sema::TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
John McCallc1f69982010-02-02 02:21:27 +00001566 unsigned TDF = 0;
1567
1568 Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +00001569 = DeduceTemplateArguments(S, TemplateParams,
John McCallc1f69982010-02-02 02:21:27 +00001570 ParamType, ArgType,
1571 Info, Deduced, TDF);
1572 if (Result) continue;
1573 if (!Match.isNull()) return QualType();
1574 Match = ArgType;
1575 }
1576
1577 return Match;
1578}
1579
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001580/// \brief Perform template argument deduction from a function call
1581/// (C++ [temp.deduct.call]).
1582///
1583/// \param FunctionTemplate the function template for which we are performing
1584/// template argument deduction.
1585///
Douglas Gregorea0a0a92010-01-11 18:40:55 +00001586/// \param ExplicitTemplateArguments the explicit template arguments provided
1587/// for this call.
Douglas Gregor89026b52009-06-30 23:57:56 +00001588///
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001589/// \param Args the function call arguments
1590///
1591/// \param NumArgs the number of arguments in Args
1592///
Douglas Gregorea0a0a92010-01-11 18:40:55 +00001593/// \param Name the name of the function being called. This is only significant
1594/// when the function template is a conversion function template, in which
1595/// case this routine will also perform template argument deduction based on
1596/// the function to which
1597///
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001598/// \param Specialization if template argument deduction was successful,
Mike Stump11289f42009-09-09 15:08:12 +00001599/// this will be set to the function template specialization produced by
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001600/// template argument deduction.
1601///
1602/// \param Info the argument will be updated to provide additional information
1603/// about template argument deduction.
1604///
1605/// \returns the result of template argument deduction.
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001606Sema::TemplateDeductionResult
1607Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregorea0a0a92010-01-11 18:40:55 +00001608 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001609 Expr **Args, unsigned NumArgs,
1610 FunctionDecl *&Specialization,
1611 TemplateDeductionInfo &Info) {
1612 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor89026b52009-06-30 23:57:56 +00001613
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001614 // C++ [temp.deduct.call]p1:
1615 // Template argument deduction is done by comparing each function template
1616 // parameter type (call it P) with the type of the corresponding argument
1617 // of the call (call it A) as described below.
1618 unsigned CheckArgs = NumArgs;
Douglas Gregor89026b52009-06-30 23:57:56 +00001619 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001620 return TDK_TooFewArguments;
1621 else if (NumArgs > Function->getNumParams()) {
Mike Stump11289f42009-09-09 15:08:12 +00001622 const FunctionProtoType *Proto
John McCall9dd450b2009-09-21 23:43:11 +00001623 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001624 if (!Proto->isVariadic())
1625 return TDK_TooManyArguments;
Mike Stump11289f42009-09-09 15:08:12 +00001626
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001627 CheckArgs = Function->getNumParams();
1628 }
Mike Stump11289f42009-09-09 15:08:12 +00001629
Douglas Gregor89026b52009-06-30 23:57:56 +00001630 // The types of the parameters from which we will perform template argument
1631 // deduction.
Douglas Gregorda61afa2010-03-25 15:38:42 +00001632 Sema::LocalInstantiationScope InstScope(*this);
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001633 TemplateParameterList *TemplateParams
1634 = FunctionTemplate->getTemplateParameters();
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001635 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor89026b52009-06-30 23:57:56 +00001636 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001637 unsigned NumExplicitlySpecified = 0;
John McCall6b51f282009-11-23 01:53:49 +00001638 if (ExplicitTemplateArgs) {
Douglas Gregor9b146582009-07-08 20:55:45 +00001639 TemplateDeductionResult Result =
1640 SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001641 *ExplicitTemplateArgs,
Douglas Gregor9b146582009-07-08 20:55:45 +00001642 Deduced,
1643 ParamTypes,
1644 0,
1645 Info);
1646 if (Result)
1647 return Result;
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001648
1649 NumExplicitlySpecified = Deduced.size();
Douglas Gregor89026b52009-06-30 23:57:56 +00001650 } else {
1651 // Just fill in the parameter types from the function declaration.
1652 for (unsigned I = 0; I != CheckArgs; ++I)
1653 ParamTypes.push_back(Function->getParamDecl(I)->getType());
1654 }
Mike Stump11289f42009-09-09 15:08:12 +00001655
Douglas Gregor89026b52009-06-30 23:57:56 +00001656 // Deduce template arguments from the function parameters.
Mike Stump11289f42009-09-09 15:08:12 +00001657 Deduced.resize(TemplateParams->size());
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001658 for (unsigned I = 0; I != CheckArgs; ++I) {
Douglas Gregor89026b52009-06-30 23:57:56 +00001659 QualType ParamType = ParamTypes[I];
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001660 QualType ArgType = Args[I]->getType();
Mike Stump11289f42009-09-09 15:08:12 +00001661
John McCallc1f69982010-02-02 02:21:27 +00001662 // Overload sets usually make this parameter an undeduced
1663 // context, but there are sometimes special circumstances.
1664 if (ArgType == Context.OverloadTy) {
1665 ArgType = ResolveOverloadForDeduction(*this, TemplateParams,
1666 Args[I], ParamType);
1667 if (ArgType.isNull())
1668 continue;
1669 }
1670
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001671 // C++ [temp.deduct.call]p2:
1672 // If P is not a reference type:
1673 QualType CanonParamType = Context.getCanonicalType(ParamType);
Douglas Gregorcceb9752009-06-26 18:27:22 +00001674 bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1675 if (!ParamWasReference) {
Mike Stump11289f42009-09-09 15:08:12 +00001676 // - If A is an array type, the pointer type produced by the
1677 // array-to-pointer standard conversion (4.2) is used in place of
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001678 // A for type deduction; otherwise,
1679 if (ArgType->isArrayType())
1680 ArgType = Context.getArrayDecayedType(ArgType);
Mike Stump11289f42009-09-09 15:08:12 +00001681 // - If A is a function type, the pointer type produced by the
1682 // function-to-pointer standard conversion (4.3) is used in place
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001683 // of A for type deduction; otherwise,
1684 else if (ArgType->isFunctionType())
1685 ArgType = Context.getPointerType(ArgType);
1686 else {
1687 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1688 // type are ignored for type deduction.
1689 QualType CanonArgType = Context.getCanonicalType(ArgType);
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001690 if (CanonArgType.getLocalCVRQualifiers())
1691 ArgType = CanonArgType.getLocalUnqualifiedType();
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001692 }
1693 }
Mike Stump11289f42009-09-09 15:08:12 +00001694
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001695 // C++0x [temp.deduct.call]p3:
1696 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
Mike Stump11289f42009-09-09 15:08:12 +00001697 // are ignored for type deduction.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001698 if (CanonParamType.getLocalCVRQualifiers())
1699 ParamType = CanonParamType.getLocalUnqualifiedType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001700 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Mike Stump11289f42009-09-09 15:08:12 +00001701 // [...] If P is a reference type, the type referred to by P is used
1702 // for type deduction.
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001703 ParamType = ParamRefType->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +00001704
1705 // [...] If P is of the form T&&, where T is a template parameter, and
1706 // the argument is an lvalue, the type A& is used in place of A for
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001707 // type deduction.
1708 if (isa<RValueReferenceType>(ParamRefType) &&
John McCall9dd450b2009-09-21 23:43:11 +00001709 ParamRefType->getAs<TemplateTypeParmType>() &&
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001710 Args[I]->isLvalue(Context) == Expr::LV_Valid)
1711 ArgType = Context.getLValueReferenceType(ArgType);
1712 }
Mike Stump11289f42009-09-09 15:08:12 +00001713
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001714 // C++0x [temp.deduct.call]p4:
1715 // In general, the deduction process attempts to find template argument
1716 // values that will make the deduced A identical to A (after the type A
1717 // is transformed as described above). [...]
Douglas Gregor406f6342009-09-14 20:00:47 +00001718 unsigned TDF = TDF_SkipNonDependent;
Mike Stump11289f42009-09-09 15:08:12 +00001719
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001720 // - If the original P is a reference type, the deduced A (i.e., the
1721 // type referred to by the reference) can be more cv-qualified than
1722 // the transformed A.
1723 if (ParamWasReference)
1724 TDF |= TDF_ParamWithReferenceType;
Mike Stump11289f42009-09-09 15:08:12 +00001725 // - The transformed A can be another pointer or pointer to member
1726 // type that can be converted to the deduced A via a qualification
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001727 // conversion (4.4).
John McCallda518412010-08-05 05:30:45 +00001728 if (ArgType->isPointerType() || ArgType->isMemberPointerType() ||
1729 ArgType->isObjCObjectPointerType())
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001730 TDF |= TDF_IgnoreQualifiers;
Mike Stump11289f42009-09-09 15:08:12 +00001731 // - If P is a class and P has the form simple-template-id, then the
Douglas Gregorfc516c92009-06-26 23:27:24 +00001732 // transformed A can be a derived class of the deduced A. Likewise,
1733 // if P is a pointer to a class of the form simple-template-id, the
1734 // transformed A can be a pointer to a derived class pointed to by
1735 // the deduced A.
1736 if (isSimpleTemplateIdType(ParamType) ||
Mike Stump11289f42009-09-09 15:08:12 +00001737 (isa<PointerType>(ParamType) &&
Douglas Gregorfc516c92009-06-26 23:27:24 +00001738 isSimpleTemplateIdType(
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001739 ParamType->getAs<PointerType>()->getPointeeType())))
Douglas Gregorfc516c92009-06-26 23:27:24 +00001740 TDF |= TDF_DerivedClass;
Mike Stump11289f42009-09-09 15:08:12 +00001741
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001742 if (TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +00001743 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregorcceb9752009-06-26 18:27:22 +00001744 ParamType, ArgType, Info, Deduced,
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001745 TDF))
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001746 return Result;
Mike Stump11289f42009-09-09 15:08:12 +00001747
Douglas Gregor05155d82009-08-21 23:19:43 +00001748 // FIXME: we need to check that the deduced A is the same as A,
1749 // modulo the various allowed differences.
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001750 }
Douglas Gregor05155d82009-08-21 23:19:43 +00001751
Mike Stump11289f42009-09-09 15:08:12 +00001752 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001753 NumExplicitlySpecified,
Douglas Gregor9b146582009-07-08 20:55:45 +00001754 Specialization, Info);
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001755}
1756
Douglas Gregor9b146582009-07-08 20:55:45 +00001757/// \brief Deduce template arguments when taking the address of a function
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001758/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
1759/// a template.
Douglas Gregor9b146582009-07-08 20:55:45 +00001760///
1761/// \param FunctionTemplate the function template for which we are performing
1762/// template argument deduction.
1763///
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001764/// \param ExplicitTemplateArguments the explicitly-specified template
1765/// arguments.
Douglas Gregor9b146582009-07-08 20:55:45 +00001766///
1767/// \param ArgFunctionType the function type that will be used as the
1768/// "argument" type (A) when performing template argument deduction from the
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001769/// function template's function type. This type may be NULL, if there is no
1770/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
Douglas Gregor9b146582009-07-08 20:55:45 +00001771///
1772/// \param Specialization if template argument deduction was successful,
Mike Stump11289f42009-09-09 15:08:12 +00001773/// this will be set to the function template specialization produced by
Douglas Gregor9b146582009-07-08 20:55:45 +00001774/// template argument deduction.
1775///
1776/// \param Info the argument will be updated to provide additional information
1777/// about template argument deduction.
1778///
1779/// \returns the result of template argument deduction.
1780Sema::TemplateDeductionResult
1781Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001782 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor9b146582009-07-08 20:55:45 +00001783 QualType ArgFunctionType,
1784 FunctionDecl *&Specialization,
1785 TemplateDeductionInfo &Info) {
1786 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1787 TemplateParameterList *TemplateParams
1788 = FunctionTemplate->getTemplateParameters();
1789 QualType FunctionType = Function->getType();
Mike Stump11289f42009-09-09 15:08:12 +00001790
Douglas Gregor9b146582009-07-08 20:55:45 +00001791 // Substitute any explicit template arguments.
Douglas Gregorda61afa2010-03-25 15:38:42 +00001792 Sema::LocalInstantiationScope InstScope(*this);
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001793 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
1794 unsigned NumExplicitlySpecified = 0;
Douglas Gregor9b146582009-07-08 20:55:45 +00001795 llvm::SmallVector<QualType, 4> ParamTypes;
John McCall6b51f282009-11-23 01:53:49 +00001796 if (ExplicitTemplateArgs) {
Mike Stump11289f42009-09-09 15:08:12 +00001797 if (TemplateDeductionResult Result
1798 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001799 *ExplicitTemplateArgs,
Mike Stump11289f42009-09-09 15:08:12 +00001800 Deduced, ParamTypes,
Douglas Gregor9b146582009-07-08 20:55:45 +00001801 &FunctionType, Info))
1802 return Result;
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001803
1804 NumExplicitlySpecified = Deduced.size();
Douglas Gregor9b146582009-07-08 20:55:45 +00001805 }
1806
1807 // Template argument deduction for function templates in a SFINAE context.
1808 // Trap any errors that might occur.
Mike Stump11289f42009-09-09 15:08:12 +00001809 SFINAETrap Trap(*this);
1810
John McCallc1f69982010-02-02 02:21:27 +00001811 Deduced.resize(TemplateParams->size());
1812
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001813 if (!ArgFunctionType.isNull()) {
1814 // Deduce template arguments from the function type.
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001815 if (TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +00001816 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001817 FunctionType, ArgFunctionType, Info,
1818 Deduced, 0))
1819 return Result;
1820 }
1821
Mike Stump11289f42009-09-09 15:08:12 +00001822 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001823 NumExplicitlySpecified,
Douglas Gregor9b146582009-07-08 20:55:45 +00001824 Specialization, Info);
1825}
1826
Douglas Gregor05155d82009-08-21 23:19:43 +00001827/// \brief Deduce template arguments for a templated conversion
1828/// function (C++ [temp.deduct.conv]) and, if successful, produce a
1829/// conversion function template specialization.
1830Sema::TemplateDeductionResult
1831Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1832 QualType ToType,
1833 CXXConversionDecl *&Specialization,
1834 TemplateDeductionInfo &Info) {
Mike Stump11289f42009-09-09 15:08:12 +00001835 CXXConversionDecl *Conv
Douglas Gregor05155d82009-08-21 23:19:43 +00001836 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
1837 QualType FromType = Conv->getConversionType();
1838
1839 // Canonicalize the types for deduction.
1840 QualType P = Context.getCanonicalType(FromType);
1841 QualType A = Context.getCanonicalType(ToType);
1842
1843 // C++0x [temp.deduct.conv]p3:
1844 // If P is a reference type, the type referred to by P is used for
1845 // type deduction.
1846 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
1847 P = PRef->getPointeeType();
1848
1849 // C++0x [temp.deduct.conv]p3:
1850 // If A is a reference type, the type referred to by A is used
1851 // for type deduction.
1852 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
1853 A = ARef->getPointeeType();
1854 // C++ [temp.deduct.conv]p2:
1855 //
Mike Stump11289f42009-09-09 15:08:12 +00001856 // If A is not a reference type:
Douglas Gregor05155d82009-08-21 23:19:43 +00001857 else {
1858 assert(!A->isReferenceType() && "Reference types were handled above");
1859
1860 // - If P is an array type, the pointer type produced by the
Mike Stump11289f42009-09-09 15:08:12 +00001861 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor05155d82009-08-21 23:19:43 +00001862 // of P for type deduction; otherwise,
1863 if (P->isArrayType())
1864 P = Context.getArrayDecayedType(P);
1865 // - If P is a function type, the pointer type produced by the
1866 // function-to-pointer standard conversion (4.3) is used in
1867 // place of P for type deduction; otherwise,
1868 else if (P->isFunctionType())
1869 P = Context.getPointerType(P);
1870 // - If P is a cv-qualified type, the top level cv-qualifiers of
1871 // P’s type are ignored for type deduction.
1872 else
1873 P = P.getUnqualifiedType();
1874
1875 // C++0x [temp.deduct.conv]p3:
1876 // If A is a cv-qualified type, the top level cv-qualifiers of A’s
1877 // type are ignored for type deduction.
1878 A = A.getUnqualifiedType();
1879 }
1880
1881 // Template argument deduction for function templates in a SFINAE context.
1882 // Trap any errors that might occur.
Mike Stump11289f42009-09-09 15:08:12 +00001883 SFINAETrap Trap(*this);
Douglas Gregor05155d82009-08-21 23:19:43 +00001884
1885 // C++ [temp.deduct.conv]p1:
1886 // Template argument deduction is done by comparing the return
1887 // type of the template conversion function (call it P) with the
1888 // type that is required as the result of the conversion (call it
1889 // A) as described in 14.8.2.4.
1890 TemplateParameterList *TemplateParams
1891 = FunctionTemplate->getTemplateParameters();
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001892 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Mike Stump11289f42009-09-09 15:08:12 +00001893 Deduced.resize(TemplateParams->size());
Douglas Gregor05155d82009-08-21 23:19:43 +00001894
1895 // C++0x [temp.deduct.conv]p4:
1896 // In general, the deduction process attempts to find template
1897 // argument values that will make the deduced A identical to
1898 // A. However, there are two cases that allow a difference:
1899 unsigned TDF = 0;
1900 // - If the original A is a reference type, A can be more
1901 // cv-qualified than the deduced A (i.e., the type referred to
1902 // by the reference)
1903 if (ToType->isReferenceType())
1904 TDF |= TDF_ParamWithReferenceType;
1905 // - The deduced A can be another pointer or pointer to member
1906 // type that can be converted to A via a qualification
1907 // conversion.
1908 //
1909 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
1910 // both P and A are pointers or member pointers. In this case, we
1911 // just ignore cv-qualifiers completely).
1912 if ((P->isPointerType() && A->isPointerType()) ||
1913 (P->isMemberPointerType() && P->isMemberPointerType()))
1914 TDF |= TDF_IgnoreQualifiers;
1915 if (TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +00001916 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor05155d82009-08-21 23:19:43 +00001917 P, A, Info, Deduced, TDF))
1918 return Result;
1919
1920 // FIXME: we need to check that the deduced A is the same as A,
1921 // modulo the various allowed differences.
Mike Stump11289f42009-09-09 15:08:12 +00001922
Douglas Gregor05155d82009-08-21 23:19:43 +00001923 // Finish template argument deduction.
Douglas Gregorda61afa2010-03-25 15:38:42 +00001924 Sema::LocalInstantiationScope InstScope(*this);
Douglas Gregor05155d82009-08-21 23:19:43 +00001925 FunctionDecl *Spec = 0;
1926 TemplateDeductionResult Result
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001927 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec,
1928 Info);
Douglas Gregor05155d82009-08-21 23:19:43 +00001929 Specialization = cast_or_null<CXXConversionDecl>(Spec);
1930 return Result;
1931}
1932
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001933/// \brief Deduce template arguments for a function template when there is
1934/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
1935///
1936/// \param FunctionTemplate the function template for which we are performing
1937/// template argument deduction.
1938///
1939/// \param ExplicitTemplateArguments the explicitly-specified template
1940/// arguments.
1941///
1942/// \param Specialization if template argument deduction was successful,
1943/// this will be set to the function template specialization produced by
1944/// template argument deduction.
1945///
1946/// \param Info the argument will be updated to provide additional information
1947/// about template argument deduction.
1948///
1949/// \returns the result of template argument deduction.
1950Sema::TemplateDeductionResult
1951Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1952 const TemplateArgumentListInfo *ExplicitTemplateArgs,
1953 FunctionDecl *&Specialization,
1954 TemplateDeductionInfo &Info) {
1955 return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
1956 QualType(), Specialization, Info);
1957}
1958
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001959/// \brief Stores the result of comparing the qualifiers of two types.
1960enum DeductionQualifierComparison {
1961 NeitherMoreQualified = 0,
1962 ParamMoreQualified,
1963 ArgMoreQualified
1964};
1965
1966/// \brief Deduce the template arguments during partial ordering by comparing
1967/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
1968///
Chandler Carruthc1263112010-02-07 21:33:28 +00001969/// \param S the semantic analysis object within which we are deducing
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001970///
1971/// \param TemplateParams the template parameters that we are deducing
1972///
1973/// \param ParamIn the parameter type
1974///
1975/// \param ArgIn the argument type
1976///
1977/// \param Info information about the template argument deduction itself
1978///
1979/// \param Deduced the deduced template arguments
1980///
1981/// \returns the result of template argument deduction so far. Note that a
1982/// "success" result means that template argument deduction has not yet failed,
1983/// but it may still fail, later, for other reasons.
1984static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +00001985DeduceTemplateArgumentsDuringPartialOrdering(Sema &S,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001986 TemplateParameterList *TemplateParams,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001987 QualType ParamIn, QualType ArgIn,
1988 Sema::TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001989 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1990 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
Chandler Carruthc1263112010-02-07 21:33:28 +00001991 CanQualType Param = S.Context.getCanonicalType(ParamIn);
1992 CanQualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001993
1994 // C++0x [temp.deduct.partial]p5:
1995 // Before the partial ordering is done, certain transformations are
1996 // performed on the types used for partial ordering:
1997 // - If P is a reference type, P is replaced by the type referred to.
1998 CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
John McCall48f2d582009-10-23 23:03:21 +00001999 if (!ParamRef.isNull())
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002000 Param = ParamRef->getPointeeType();
2001
2002 // - If A is a reference type, A is replaced by the type referred to.
2003 CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
John McCall48f2d582009-10-23 23:03:21 +00002004 if (!ArgRef.isNull())
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002005 Arg = ArgRef->getPointeeType();
2006
John McCall48f2d582009-10-23 23:03:21 +00002007 if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002008 // C++0x [temp.deduct.partial]p6:
2009 // If both P and A were reference types (before being replaced with the
2010 // type referred to above), determine which of the two types (if any) is
2011 // more cv-qualified than the other; otherwise the types are considered to
2012 // be equally cv-qualified for partial ordering purposes. The result of this
2013 // determination will be used below.
2014 //
2015 // We save this information for later, using it only when deduction
2016 // succeeds in both directions.
2017 DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
2018 if (Param.isMoreQualifiedThan(Arg))
2019 QualifierResult = ParamMoreQualified;
2020 else if (Arg.isMoreQualifiedThan(Param))
2021 QualifierResult = ArgMoreQualified;
2022 QualifierComparisons->push_back(QualifierResult);
2023 }
2024
2025 // C++0x [temp.deduct.partial]p7:
2026 // Remove any top-level cv-qualifiers:
2027 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
2028 // version of P.
2029 Param = Param.getUnqualifiedType();
2030 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
2031 // version of A.
2032 Arg = Arg.getUnqualifiedType();
2033
2034 // C++0x [temp.deduct.partial]p8:
2035 // Using the resulting types P and A the deduction is then done as
2036 // described in 14.9.2.5. If deduction succeeds for a given type, the type
2037 // from the argument template is considered to be at least as specialized
2038 // as the type from the parameter template.
Chandler Carruthc1263112010-02-07 21:33:28 +00002039 return DeduceTemplateArguments(S, TemplateParams, Param, Arg, Info,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002040 Deduced, TDF_None);
2041}
2042
2043static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002044MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2045 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002046 unsigned Level,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002047 llvm::SmallVectorImpl<bool> &Deduced);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002048
2049/// \brief Determine whether the function template \p FT1 is at least as
2050/// specialized as \p FT2.
2051static bool isAtLeastAsSpecializedAs(Sema &S,
John McCallbc077cf2010-02-08 23:07:23 +00002052 SourceLocation Loc,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002053 FunctionTemplateDecl *FT1,
2054 FunctionTemplateDecl *FT2,
2055 TemplatePartialOrderingContext TPOC,
2056 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
2057 FunctionDecl *FD1 = FT1->getTemplatedDecl();
2058 FunctionDecl *FD2 = FT2->getTemplatedDecl();
2059 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
2060 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
2061
2062 assert(Proto1 && Proto2 && "Function templates must have prototypes");
2063 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00002064 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002065 Deduced.resize(TemplateParams->size());
2066
2067 // C++0x [temp.deduct.partial]p3:
2068 // The types used to determine the ordering depend on the context in which
2069 // the partial ordering is done:
John McCallbc077cf2010-02-08 23:07:23 +00002070 Sema::TemplateDeductionInfo Info(S.Context, Loc);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002071 switch (TPOC) {
2072 case TPOC_Call: {
2073 // - In the context of a function call, the function parameter types are
2074 // used.
2075 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
2076 for (unsigned I = 0; I != NumParams; ++I)
Chandler Carruthc1263112010-02-07 21:33:28 +00002077 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002078 TemplateParams,
2079 Proto2->getArgType(I),
2080 Proto1->getArgType(I),
2081 Info,
2082 Deduced,
2083 QualifierComparisons))
2084 return false;
2085
2086 break;
2087 }
2088
2089 case TPOC_Conversion:
2090 // - In the context of a call to a conversion operator, the return types
2091 // of the conversion function templates are used.
Chandler Carruthc1263112010-02-07 21:33:28 +00002092 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002093 TemplateParams,
2094 Proto2->getResultType(),
2095 Proto1->getResultType(),
2096 Info,
2097 Deduced,
2098 QualifierComparisons))
2099 return false;
2100 break;
2101
2102 case TPOC_Other:
2103 // - In other contexts (14.6.6.2) the function template’s function type
2104 // is used.
Chandler Carruthc1263112010-02-07 21:33:28 +00002105 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002106 TemplateParams,
2107 FD2->getType(),
2108 FD1->getType(),
2109 Info,
2110 Deduced,
2111 QualifierComparisons))
2112 return false;
2113 break;
2114 }
2115
2116 // C++0x [temp.deduct.partial]p11:
2117 // In most cases, all template parameters must have values in order for
2118 // deduction to succeed, but for partial ordering purposes a template
2119 // parameter may remain without a value provided it is not used in the
2120 // types being used for partial ordering. [ Note: a template parameter used
2121 // in a non-deduced context is considered used. -end note]
2122 unsigned ArgIdx = 0, NumArgs = Deduced.size();
2123 for (; ArgIdx != NumArgs; ++ArgIdx)
2124 if (Deduced[ArgIdx].isNull())
2125 break;
2126
2127 if (ArgIdx == NumArgs) {
2128 // All template arguments were deduced. FT1 is at least as specialized
2129 // as FT2.
2130 return true;
2131 }
2132
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002133 // Figure out which template parameters were used.
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002134 llvm::SmallVector<bool, 4> UsedParameters;
2135 UsedParameters.resize(TemplateParams->size());
2136 switch (TPOC) {
2137 case TPOC_Call: {
2138 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
2139 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor21610382009-10-29 00:04:11 +00002140 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
2141 TemplateParams->getDepth(),
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002142 UsedParameters);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002143 break;
2144 }
2145
2146 case TPOC_Conversion:
Douglas Gregor21610382009-10-29 00:04:11 +00002147 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
2148 TemplateParams->getDepth(),
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002149 UsedParameters);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002150 break;
2151
2152 case TPOC_Other:
Douglas Gregor21610382009-10-29 00:04:11 +00002153 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
2154 TemplateParams->getDepth(),
2155 UsedParameters);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002156 break;
2157 }
2158
2159 for (; ArgIdx != NumArgs; ++ArgIdx)
2160 // If this argument had no value deduced but was used in one of the types
2161 // used for partial ordering, then deduction fails.
2162 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
2163 return false;
2164
2165 return true;
2166}
2167
2168
Douglas Gregorbe999392009-09-15 16:23:51 +00002169/// \brief Returns the more specialized function template according
Douglas Gregor05155d82009-08-21 23:19:43 +00002170/// to the rules of function template partial ordering (C++ [temp.func.order]).
2171///
2172/// \param FT1 the first function template
2173///
2174/// \param FT2 the second function template
2175///
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002176/// \param TPOC the context in which we are performing partial ordering of
2177/// function templates.
Mike Stump11289f42009-09-09 15:08:12 +00002178///
Douglas Gregorbe999392009-09-15 16:23:51 +00002179/// \returns the more specialized function template. If neither
Douglas Gregor05155d82009-08-21 23:19:43 +00002180/// template is more specialized, returns NULL.
2181FunctionTemplateDecl *
2182Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
2183 FunctionTemplateDecl *FT2,
John McCallbc077cf2010-02-08 23:07:23 +00002184 SourceLocation Loc,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002185 TemplatePartialOrderingContext TPOC) {
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002186 llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
John McCallbc077cf2010-02-08 23:07:23 +00002187 bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, 0);
2188 bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002189 &QualifierComparisons);
2190
2191 if (Better1 != Better2) // We have a clear winner
2192 return Better1? FT1 : FT2;
2193
2194 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor05155d82009-08-21 23:19:43 +00002195 return 0;
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002196
2197
2198 // C++0x [temp.deduct.partial]p10:
2199 // If for each type being considered a given template is at least as
2200 // specialized for all types and more specialized for some set of types and
2201 // the other template is not more specialized for any types or is not at
2202 // least as specialized for any types, then the given template is more
2203 // specialized than the other template. Otherwise, neither template is more
2204 // specialized than the other.
2205 Better1 = false;
2206 Better2 = false;
2207 for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
2208 // C++0x [temp.deduct.partial]p9:
2209 // If, for a given type, deduction succeeds in both directions (i.e., the
2210 // types are identical after the transformations above) and if the type
2211 // from the argument template is more cv-qualified than the type from the
2212 // parameter template (as described above) that type is considered to be
2213 // more specialized than the other. If neither type is more cv-qualified
2214 // than the other then neither type is more specialized than the other.
2215 switch (QualifierComparisons[I]) {
2216 case NeitherMoreQualified:
2217 break;
2218
2219 case ParamMoreQualified:
2220 Better1 = true;
2221 if (Better2)
2222 return 0;
2223 break;
2224
2225 case ArgMoreQualified:
2226 Better2 = true;
2227 if (Better1)
2228 return 0;
2229 break;
2230 }
2231 }
2232
2233 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor05155d82009-08-21 23:19:43 +00002234 if (Better1)
2235 return FT1;
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002236 else if (Better2)
2237 return FT2;
2238 else
2239 return 0;
Douglas Gregor05155d82009-08-21 23:19:43 +00002240}
Douglas Gregor9b146582009-07-08 20:55:45 +00002241
Douglas Gregor450f00842009-09-25 18:43:00 +00002242/// \brief Determine if the two templates are equivalent.
2243static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
2244 if (T1 == T2)
2245 return true;
2246
2247 if (!T1 || !T2)
2248 return false;
2249
2250 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
2251}
2252
2253/// \brief Retrieve the most specialized of the given function template
2254/// specializations.
2255///
John McCall58cc69d2010-01-27 01:50:18 +00002256/// \param SpecBegin the start iterator of the function template
2257/// specializations that we will be comparing.
Douglas Gregor450f00842009-09-25 18:43:00 +00002258///
John McCall58cc69d2010-01-27 01:50:18 +00002259/// \param SpecEnd the end iterator of the function template
2260/// specializations, paired with \p SpecBegin.
Douglas Gregor450f00842009-09-25 18:43:00 +00002261///
2262/// \param TPOC the partial ordering context to use to compare the function
2263/// template specializations.
2264///
2265/// \param Loc the location where the ambiguity or no-specializations
2266/// diagnostic should occur.
2267///
2268/// \param NoneDiag partial diagnostic used to diagnose cases where there are
2269/// no matching candidates.
2270///
2271/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
2272/// occurs.
2273///
2274/// \param CandidateDiag partial diagnostic used for each function template
2275/// specialization that is a candidate in the ambiguous ordering. One parameter
2276/// in this diagnostic should be unbound, which will correspond to the string
2277/// describing the template arguments for the function template specialization.
2278///
2279/// \param Index if non-NULL and the result of this function is non-nULL,
2280/// receives the index corresponding to the resulting function template
2281/// specialization.
2282///
2283/// \returns the most specialized function template specialization, if
John McCall58cc69d2010-01-27 01:50:18 +00002284/// found. Otherwise, returns SpecEnd.
Douglas Gregor450f00842009-09-25 18:43:00 +00002285///
2286/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
2287/// template argument deduction.
John McCall58cc69d2010-01-27 01:50:18 +00002288UnresolvedSetIterator
2289Sema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
2290 UnresolvedSetIterator SpecEnd,
2291 TemplatePartialOrderingContext TPOC,
2292 SourceLocation Loc,
2293 const PartialDiagnostic &NoneDiag,
2294 const PartialDiagnostic &AmbigDiag,
2295 const PartialDiagnostic &CandidateDiag) {
2296 if (SpecBegin == SpecEnd) {
Douglas Gregor450f00842009-09-25 18:43:00 +00002297 Diag(Loc, NoneDiag);
John McCall58cc69d2010-01-27 01:50:18 +00002298 return SpecEnd;
Douglas Gregor450f00842009-09-25 18:43:00 +00002299 }
2300
John McCall58cc69d2010-01-27 01:50:18 +00002301 if (SpecBegin + 1 == SpecEnd)
2302 return SpecBegin;
Douglas Gregor450f00842009-09-25 18:43:00 +00002303
2304 // Find the function template that is better than all of the templates it
2305 // has been compared to.
John McCall58cc69d2010-01-27 01:50:18 +00002306 UnresolvedSetIterator Best = SpecBegin;
Douglas Gregor450f00842009-09-25 18:43:00 +00002307 FunctionTemplateDecl *BestTemplate
John McCall58cc69d2010-01-27 01:50:18 +00002308 = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
Douglas Gregor450f00842009-09-25 18:43:00 +00002309 assert(BestTemplate && "Not a function template specialization?");
John McCall58cc69d2010-01-27 01:50:18 +00002310 for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
2311 FunctionTemplateDecl *Challenger
2312 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregor450f00842009-09-25 18:43:00 +00002313 assert(Challenger && "Not a function template specialization?");
John McCall58cc69d2010-01-27 01:50:18 +00002314 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCallbc077cf2010-02-08 23:07:23 +00002315 Loc, TPOC),
Douglas Gregor450f00842009-09-25 18:43:00 +00002316 Challenger)) {
2317 Best = I;
2318 BestTemplate = Challenger;
2319 }
2320 }
2321
2322 // Make sure that the "best" function template is more specialized than all
2323 // of the others.
2324 bool Ambiguous = false;
John McCall58cc69d2010-01-27 01:50:18 +00002325 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
2326 FunctionTemplateDecl *Challenger
2327 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregor450f00842009-09-25 18:43:00 +00002328 if (I != Best &&
2329 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCallbc077cf2010-02-08 23:07:23 +00002330 Loc, TPOC),
Douglas Gregor450f00842009-09-25 18:43:00 +00002331 BestTemplate)) {
2332 Ambiguous = true;
2333 break;
2334 }
2335 }
2336
2337 if (!Ambiguous) {
2338 // We found an answer. Return it.
John McCall58cc69d2010-01-27 01:50:18 +00002339 return Best;
Douglas Gregor450f00842009-09-25 18:43:00 +00002340 }
2341
2342 // Diagnose the ambiguity.
2343 Diag(Loc, AmbigDiag);
2344
2345 // FIXME: Can we order the candidates in some sane way?
John McCall58cc69d2010-01-27 01:50:18 +00002346 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
2347 Diag((*I)->getLocation(), CandidateDiag)
Douglas Gregor450f00842009-09-25 18:43:00 +00002348 << getTemplateArgumentBindingsText(
John McCall58cc69d2010-01-27 01:50:18 +00002349 cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
2350 *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
Douglas Gregor450f00842009-09-25 18:43:00 +00002351
John McCall58cc69d2010-01-27 01:50:18 +00002352 return SpecEnd;
Douglas Gregor450f00842009-09-25 18:43:00 +00002353}
2354
Douglas Gregorbe999392009-09-15 16:23:51 +00002355/// \brief Returns the more specialized class template partial specialization
2356/// according to the rules of partial ordering of class template partial
2357/// specializations (C++ [temp.class.order]).
2358///
2359/// \param PS1 the first class template partial specialization
2360///
2361/// \param PS2 the second class template partial specialization
2362///
2363/// \returns the more specialized class template partial specialization. If
2364/// neither partial specialization is more specialized, returns NULL.
2365ClassTemplatePartialSpecializationDecl *
2366Sema::getMoreSpecializedPartialSpecialization(
2367 ClassTemplatePartialSpecializationDecl *PS1,
John McCallbc077cf2010-02-08 23:07:23 +00002368 ClassTemplatePartialSpecializationDecl *PS2,
2369 SourceLocation Loc) {
Douglas Gregorbe999392009-09-15 16:23:51 +00002370 // C++ [temp.class.order]p1:
2371 // For two class template partial specializations, the first is at least as
2372 // specialized as the second if, given the following rewrite to two
2373 // function templates, the first function template is at least as
2374 // specialized as the second according to the ordering rules for function
2375 // templates (14.6.6.2):
2376 // - the first function template has the same template parameters as the
2377 // first partial specialization and has a single function parameter
2378 // whose type is a class template specialization with the template
2379 // arguments of the first partial specialization, and
2380 // - the second function template has the same template parameters as the
2381 // second partial specialization and has a single function parameter
2382 // whose type is a class template specialization with the template
2383 // arguments of the second partial specialization.
2384 //
Douglas Gregor684268d2010-04-29 06:21:43 +00002385 // Rather than synthesize function templates, we merely perform the
2386 // equivalent partial ordering by performing deduction directly on
2387 // the template arguments of the class template partial
2388 // specializations. This computation is slightly simpler than the
2389 // general problem of function template partial ordering, because
2390 // class template partial specializations are more constrained. We
2391 // know that every template parameter is deducible from the class
2392 // template partial specialization's template arguments, for
2393 // example.
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00002394 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
John McCallbc077cf2010-02-08 23:07:23 +00002395 Sema::TemplateDeductionInfo Info(Context, Loc);
John McCall2408e322010-04-27 00:57:59 +00002396
2397 QualType PT1 = PS1->getInjectedSpecializationType();
2398 QualType PT2 = PS2->getInjectedSpecializationType();
Douglas Gregorbe999392009-09-15 16:23:51 +00002399
2400 // Determine whether PS1 is at least as specialized as PS2
2401 Deduced.resize(PS2->getTemplateParameters()->size());
Chandler Carruthc1263112010-02-07 21:33:28 +00002402 bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbe999392009-09-15 16:23:51 +00002403 PS2->getTemplateParameters(),
John McCall2408e322010-04-27 00:57:59 +00002404 PT2,
2405 PT1,
Douglas Gregorbe999392009-09-15 16:23:51 +00002406 Info,
2407 Deduced,
2408 0);
Douglas Gregor9225b022010-04-29 06:31:36 +00002409 if (Better1)
2410 Better1 = !::FinishTemplateArgumentDeduction(*this, PS2,
2411 PS1->getTemplateArgs(),
2412 Deduced, Info);
2413
Douglas Gregorbe999392009-09-15 16:23:51 +00002414 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregoradee3e32009-11-11 23:06:43 +00002415 Deduced.clear();
Douglas Gregorbe999392009-09-15 16:23:51 +00002416 Deduced.resize(PS1->getTemplateParameters()->size());
Chandler Carruthc1263112010-02-07 21:33:28 +00002417 bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbe999392009-09-15 16:23:51 +00002418 PS1->getTemplateParameters(),
John McCall2408e322010-04-27 00:57:59 +00002419 PT1,
2420 PT2,
Douglas Gregorbe999392009-09-15 16:23:51 +00002421 Info,
2422 Deduced,
2423 0);
Douglas Gregor9225b022010-04-29 06:31:36 +00002424 if (Better2)
2425 Better2 = !::FinishTemplateArgumentDeduction(*this, PS1,
2426 PS2->getTemplateArgs(),
2427 Deduced, Info);
Douglas Gregorbe999392009-09-15 16:23:51 +00002428
2429 if (Better1 == Better2)
2430 return 0;
2431
2432 return Better1? PS1 : PS2;
2433}
2434
Mike Stump11289f42009-09-09 15:08:12 +00002435static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002436MarkUsedTemplateParameters(Sema &SemaRef,
2437 const TemplateArgument &TemplateArg,
2438 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002439 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002440 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002441
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002442/// \brief Mark the template parameters that are used by the given
Douglas Gregor91772d12009-06-13 00:26:55 +00002443/// expression.
Mike Stump11289f42009-09-09 15:08:12 +00002444static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002445MarkUsedTemplateParameters(Sema &SemaRef,
2446 const Expr *E,
2447 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002448 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002449 llvm::SmallVectorImpl<bool> &Used) {
2450 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2451 // find other occurrences of template parameters.
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00002452 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregor04b11522010-01-14 18:13:22 +00002453 if (!DRE)
Douglas Gregor91772d12009-06-13 00:26:55 +00002454 return;
2455
Mike Stump11289f42009-09-09 15:08:12 +00002456 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor91772d12009-06-13 00:26:55 +00002457 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2458 if (!NTTP)
2459 return;
2460
Douglas Gregor21610382009-10-29 00:04:11 +00002461 if (NTTP->getDepth() == Depth)
2462 Used[NTTP->getIndex()] = true;
Douglas Gregor91772d12009-06-13 00:26:55 +00002463}
2464
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002465/// \brief Mark the template parameters that are used by the given
2466/// nested name specifier.
2467static void
2468MarkUsedTemplateParameters(Sema &SemaRef,
2469 NestedNameSpecifier *NNS,
2470 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002471 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002472 llvm::SmallVectorImpl<bool> &Used) {
2473 if (!NNS)
2474 return;
2475
Douglas Gregor21610382009-10-29 00:04:11 +00002476 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2477 Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002478 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregor21610382009-10-29 00:04:11 +00002479 OnlyDeduced, Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002480}
2481
2482/// \brief Mark the template parameters that are used by the given
2483/// template name.
2484static void
2485MarkUsedTemplateParameters(Sema &SemaRef,
2486 TemplateName Name,
2487 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002488 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002489 llvm::SmallVectorImpl<bool> &Used) {
2490 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2491 if (TemplateTemplateParmDecl *TTP
Douglas Gregor21610382009-10-29 00:04:11 +00002492 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2493 if (TTP->getDepth() == Depth)
2494 Used[TTP->getIndex()] = true;
2495 }
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002496 return;
2497 }
2498
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002499 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2500 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2501 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002502 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
Douglas Gregor21610382009-10-29 00:04:11 +00002503 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2504 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002505}
2506
2507/// \brief Mark the template parameters that are used by the given
Douglas Gregor91772d12009-06-13 00:26:55 +00002508/// type.
Mike Stump11289f42009-09-09 15:08:12 +00002509static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002510MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2511 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002512 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002513 llvm::SmallVectorImpl<bool> &Used) {
2514 if (T.isNull())
2515 return;
2516
Douglas Gregor91772d12009-06-13 00:26:55 +00002517 // Non-dependent types have nothing deducible
2518 if (!T->isDependentType())
2519 return;
2520
2521 T = SemaRef.Context.getCanonicalType(T);
2522 switch (T->getTypeClass()) {
Douglas Gregor91772d12009-06-13 00:26:55 +00002523 case Type::Pointer:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002524 MarkUsedTemplateParameters(SemaRef,
2525 cast<PointerType>(T)->getPointeeType(),
2526 OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002527 Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002528 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002529 break;
2530
2531 case Type::BlockPointer:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002532 MarkUsedTemplateParameters(SemaRef,
2533 cast<BlockPointerType>(T)->getPointeeType(),
2534 OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002535 Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002536 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002537 break;
2538
2539 case Type::LValueReference:
2540 case Type::RValueReference:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002541 MarkUsedTemplateParameters(SemaRef,
2542 cast<ReferenceType>(T)->getPointeeType(),
2543 OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002544 Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002545 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002546 break;
2547
2548 case Type::MemberPointer: {
2549 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002550 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002551 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002552 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregor21610382009-10-29 00:04:11 +00002553 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002554 break;
2555 }
2556
2557 case Type::DependentSizedArray:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002558 MarkUsedTemplateParameters(SemaRef,
2559 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregor21610382009-10-29 00:04:11 +00002560 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002561 // Fall through to check the element type
2562
2563 case Type::ConstantArray:
2564 case Type::IncompleteArray:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002565 MarkUsedTemplateParameters(SemaRef,
2566 cast<ArrayType>(T)->getElementType(),
Douglas Gregor21610382009-10-29 00:04:11 +00002567 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002568 break;
2569
2570 case Type::Vector:
2571 case Type::ExtVector:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002572 MarkUsedTemplateParameters(SemaRef,
2573 cast<VectorType>(T)->getElementType(),
Douglas Gregor21610382009-10-29 00:04:11 +00002574 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002575 break;
2576
Douglas Gregor758a8692009-06-17 21:51:59 +00002577 case Type::DependentSizedExtVector: {
2578 const DependentSizedExtVectorType *VecType
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00002579 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002580 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002581 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002582 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002583 Depth, Used);
Douglas Gregor758a8692009-06-17 21:51:59 +00002584 break;
2585 }
2586
Douglas Gregor91772d12009-06-13 00:26:55 +00002587 case Type::FunctionProto: {
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00002588 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002589 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002590 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002591 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002592 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002593 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002594 break;
2595 }
2596
Douglas Gregor21610382009-10-29 00:04:11 +00002597 case Type::TemplateTypeParm: {
2598 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2599 if (TTP->getDepth() == Depth)
2600 Used[TTP->getIndex()] = true;
Douglas Gregor91772d12009-06-13 00:26:55 +00002601 break;
Douglas Gregor21610382009-10-29 00:04:11 +00002602 }
Douglas Gregor91772d12009-06-13 00:26:55 +00002603
John McCall2408e322010-04-27 00:57:59 +00002604 case Type::InjectedClassName:
2605 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
2606 // fall through
2607
Douglas Gregor91772d12009-06-13 00:26:55 +00002608 case Type::TemplateSpecialization: {
Mike Stump11289f42009-09-09 15:08:12 +00002609 const TemplateSpecializationType *Spec
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00002610 = cast<TemplateSpecializationType>(T);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002611 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002612 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002613 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregor21610382009-10-29 00:04:11 +00002614 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2615 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002616 break;
2617 }
2618
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002619 case Type::Complex:
2620 if (!OnlyDeduced)
2621 MarkUsedTemplateParameters(SemaRef,
2622 cast<ComplexType>(T)->getElementType(),
Douglas Gregor21610382009-10-29 00:04:11 +00002623 OnlyDeduced, Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002624 break;
2625
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002626 case Type::DependentName:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002627 if (!OnlyDeduced)
2628 MarkUsedTemplateParameters(SemaRef,
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002629 cast<DependentNameType>(T)->getQualifier(),
Douglas Gregor21610382009-10-29 00:04:11 +00002630 OnlyDeduced, Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002631 break;
2632
John McCallc392f372010-06-11 00:33:02 +00002633 case Type::DependentTemplateSpecialization: {
2634 const DependentTemplateSpecializationType *Spec
2635 = cast<DependentTemplateSpecializationType>(T);
2636 if (!OnlyDeduced)
2637 MarkUsedTemplateParameters(SemaRef, Spec->getQualifier(),
2638 OnlyDeduced, Depth, Used);
2639 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
2640 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2641 Used);
2642 break;
2643 }
2644
John McCallbd8d9bd2010-03-01 23:49:17 +00002645 case Type::TypeOf:
2646 if (!OnlyDeduced)
2647 MarkUsedTemplateParameters(SemaRef,
2648 cast<TypeOfType>(T)->getUnderlyingType(),
2649 OnlyDeduced, Depth, Used);
2650 break;
2651
2652 case Type::TypeOfExpr:
2653 if (!OnlyDeduced)
2654 MarkUsedTemplateParameters(SemaRef,
2655 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
2656 OnlyDeduced, Depth, Used);
2657 break;
2658
2659 case Type::Decltype:
2660 if (!OnlyDeduced)
2661 MarkUsedTemplateParameters(SemaRef,
2662 cast<DecltypeType>(T)->getUnderlyingExpr(),
2663 OnlyDeduced, Depth, Used);
2664 break;
2665
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002666 // None of these types have any template parameters in them.
Douglas Gregor91772d12009-06-13 00:26:55 +00002667 case Type::Builtin:
Douglas Gregor91772d12009-06-13 00:26:55 +00002668 case Type::VariableArray:
2669 case Type::FunctionNoProto:
2670 case Type::Record:
2671 case Type::Enum:
Douglas Gregor91772d12009-06-13 00:26:55 +00002672 case Type::ObjCInterface:
John McCall8b07ec22010-05-15 11:32:37 +00002673 case Type::ObjCObject:
Steve Narofffb4330f2009-06-17 22:40:22 +00002674 case Type::ObjCObjectPointer:
John McCallb96ec562009-12-04 22:46:56 +00002675 case Type::UnresolvedUsing:
Douglas Gregor91772d12009-06-13 00:26:55 +00002676#define TYPE(Class, Base)
2677#define ABSTRACT_TYPE(Class, Base)
2678#define DEPENDENT_TYPE(Class, Base)
2679#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2680#include "clang/AST/TypeNodes.def"
2681 break;
2682 }
2683}
2684
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002685/// \brief Mark the template parameters that are used by this
Douglas Gregor91772d12009-06-13 00:26:55 +00002686/// template argument.
Mike Stump11289f42009-09-09 15:08:12 +00002687static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002688MarkUsedTemplateParameters(Sema &SemaRef,
2689 const TemplateArgument &TemplateArg,
2690 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002691 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002692 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor91772d12009-06-13 00:26:55 +00002693 switch (TemplateArg.getKind()) {
2694 case TemplateArgument::Null:
2695 case TemplateArgument::Integral:
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002696 case TemplateArgument::Declaration:
Douglas Gregor91772d12009-06-13 00:26:55 +00002697 break;
Mike Stump11289f42009-09-09 15:08:12 +00002698
Douglas Gregor91772d12009-06-13 00:26:55 +00002699 case TemplateArgument::Type:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002700 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002701 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002702 break;
2703
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002704 case TemplateArgument::Template:
2705 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2706 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002707 break;
2708
2709 case TemplateArgument::Expression:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002710 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002711 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002712 break;
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002713
Anders Carlssonbc343912009-06-15 17:04:53 +00002714 case TemplateArgument::Pack:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002715 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2716 PEnd = TemplateArg.pack_end();
2717 P != PEnd; ++P)
Douglas Gregor21610382009-10-29 00:04:11 +00002718 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssonbc343912009-06-15 17:04:53 +00002719 break;
Douglas Gregor91772d12009-06-13 00:26:55 +00002720 }
2721}
2722
2723/// \brief Mark the template parameters can be deduced by the given
2724/// template argument list.
2725///
2726/// \param TemplateArgs the template argument list from which template
2727/// parameters will be deduced.
2728///
2729/// \param Deduced a bit vector whose elements will be set to \c true
2730/// to indicate when the corresponding template parameter will be
2731/// deduced.
Mike Stump11289f42009-09-09 15:08:12 +00002732void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002733Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregor21610382009-10-29 00:04:11 +00002734 bool OnlyDeduced, unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002735 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor91772d12009-06-13 00:26:55 +00002736 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
Douglas Gregor21610382009-10-29 00:04:11 +00002737 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2738 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002739}
Douglas Gregorce23bae2009-09-18 23:21:38 +00002740
2741/// \brief Marks all of the template parameters that will be deduced by a
2742/// call to the given function template.
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00002743void
2744Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
2745 llvm::SmallVectorImpl<bool> &Deduced) {
Douglas Gregorce23bae2009-09-18 23:21:38 +00002746 TemplateParameterList *TemplateParams
2747 = FunctionTemplate->getTemplateParameters();
2748 Deduced.clear();
2749 Deduced.resize(TemplateParams->size());
2750
2751 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2752 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
2753 ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
Douglas Gregor21610382009-10-29 00:04:11 +00002754 true, TemplateParams->getDepth(), Deduced);
Douglas Gregorce23bae2009-09-18 23:21:38 +00002755}