blob: 469118111146d92e6f97442190de26b63721bc53 [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"
John McCall19c1bfd2010-08-25 05:32:35 +000016#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor55ca8f62009-06-04 00:03:07 +000017#include "clang/AST/ASTContext.h"
John McCallde6836a2010-08-24 07:21:54 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor55ca8f62009-06-04 00:03:07 +000019#include "clang/AST/DeclTemplate.h"
20#include "clang/AST/StmtVisitor.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/ExprCXX.h"
Douglas Gregor0ff7d922009-09-14 18:39:43 +000023#include <algorithm>
Douglas Gregorcf0b47d2009-06-26 23:10:12 +000024
25namespace clang {
John McCall19c1bfd2010-08-25 05:32:35 +000026 using namespace sema;
27
Douglas Gregorcf0b47d2009-06-26 23:10:12 +000028 /// \brief Various flags that control template argument deduction.
29 ///
30 /// These flags can be bitwise-OR'd together.
31 enum TemplateDeductionFlags {
32 /// \brief No template argument deduction flags, which indicates the
33 /// strictest results for template argument deduction (as used for, e.g.,
34 /// matching class template partial specializations).
35 TDF_None = 0,
36 /// \brief Within template argument deduction from a function call, we are
37 /// matching with a parameter type for which the original parameter was
38 /// a reference.
39 TDF_ParamWithReferenceType = 0x1,
40 /// \brief Within template argument deduction from a function call, we
41 /// are matching in a case where we ignore cv-qualifiers.
42 TDF_IgnoreQualifiers = 0x02,
43 /// \brief Within template argument deduction from a function call,
44 /// we are matching in a case where we can perform template argument
Douglas Gregorfc516c92009-06-26 23:27:24 +000045 /// deduction from a template-id of a derived class of the argument type.
Douglas Gregor406f6342009-09-14 20:00:47 +000046 TDF_DerivedClass = 0x04,
47 /// \brief Allow non-dependent types to differ, e.g., when performing
48 /// template argument deduction from a function call where conversions
49 /// may apply.
50 TDF_SkipNonDependent = 0x08
Douglas Gregorcf0b47d2009-06-26 23:10:12 +000051 };
52}
53
Douglas Gregor55ca8f62009-06-04 00:03:07 +000054using namespace clang;
55
Douglas Gregor0a29a052010-03-26 05:50:28 +000056/// \brief Compare two APSInts, extending and switching the sign as
57/// necessary to compare their values regardless of underlying type.
58static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) {
59 if (Y.getBitWidth() > X.getBitWidth())
60 X.extend(Y.getBitWidth());
61 else if (Y.getBitWidth() < X.getBitWidth())
62 Y.extend(X.getBitWidth());
63
64 // If there is a signedness mismatch, correct it.
65 if (X.isSigned() != Y.isSigned()) {
66 // If the signed value is negative, then the values cannot be the same.
67 if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative()))
68 return false;
69
70 Y.setIsSigned(true);
71 X.setIsSigned(true);
72 }
73
74 return X == Y;
75}
76
Douglas Gregor181aa4a2009-06-12 18:26:56 +000077static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +000078DeduceTemplateArguments(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +000079 TemplateParameterList *TemplateParams,
80 const TemplateArgument &Param,
Douglas Gregor4fbe3e32009-06-09 16:35:58 +000081 const TemplateArgument &Arg,
John McCall19c1bfd2010-08-25 05:32:35 +000082 TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +000083 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced);
Douglas Gregor4fbe3e32009-06-09 16:35:58 +000084
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000085/// \brief If the given expression is of a form that permits the deduction
86/// of a non-type template parameter, return the declaration of that
87/// non-type template parameter.
88static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
89 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
90 E = IC->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +000091
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000092 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
93 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
Mike Stump11289f42009-09-09 15:08:12 +000094
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000095 return 0;
96}
97
Mike Stump11289f42009-09-09 15:08:12 +000098/// \brief Deduce the value of the given non-type template parameter
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000099/// from the given constant.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000100static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000101DeduceNonTypeTemplateArgument(Sema &S,
Mike Stump11289f42009-09-09 15:08:12 +0000102 NonTypeTemplateParmDecl *NTTP,
Douglas Gregor0a29a052010-03-26 05:50:28 +0000103 llvm::APSInt Value, QualType ValueType,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000104 bool DeducedFromArrayBound,
John McCall19c1bfd2010-08-25 05:32:35 +0000105 TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000106 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump11289f42009-09-09 15:08:12 +0000107 assert(NTTP->getDepth() == 0 &&
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000108 "Cannot deduce non-type template argument with depth > 0");
Mike Stump11289f42009-09-09 15:08:12 +0000109
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000110 if (Deduced[NTTP->getIndex()].isNull()) {
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000111 Deduced[NTTP->getIndex()] = DeducedTemplateArgument(Value, ValueType,
112 DeducedFromArrayBound);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000113 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000114 }
Mike Stump11289f42009-09-09 15:08:12 +0000115
Douglas Gregor0a29a052010-03-26 05:50:28 +0000116 if (Deduced[NTTP->getIndex()].getKind() != TemplateArgument::Integral) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000117 Info.Param = NTTP;
118 Info.FirstArg = Deduced[NTTP->getIndex()];
Douglas Gregor0a29a052010-03-26 05:50:28 +0000119 Info.SecondArg = TemplateArgument(Value, ValueType);
120 return Sema::TDK_Inconsistent;
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000121 }
122
Douglas Gregor0a29a052010-03-26 05:50:28 +0000123 // Extent the smaller of the two values.
124 llvm::APSInt PrevValue = *Deduced[NTTP->getIndex()].getAsIntegral();
125 if (!hasSameExtendedValue(PrevValue, Value)) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000126 Info.Param = NTTP;
127 Info.FirstArg = Deduced[NTTP->getIndex()];
Douglas Gregor0a29a052010-03-26 05:50:28 +0000128 Info.SecondArg = TemplateArgument(Value, ValueType);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000129 return Sema::TDK_Inconsistent;
130 }
131
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000132 if (!DeducedFromArrayBound)
133 Deduced[NTTP->getIndex()].setDeducedFromArrayBound(false);
134
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000135 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000136}
137
Mike Stump11289f42009-09-09 15:08:12 +0000138/// \brief Deduce the value of the given non-type template parameter
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000139/// from the given type- or value-dependent expression.
140///
141/// \returns true if deduction succeeded, false otherwise.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000142static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000143DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000144 NonTypeTemplateParmDecl *NTTP,
145 Expr *Value,
John McCall19c1bfd2010-08-25 05:32:35 +0000146 TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000147 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump11289f42009-09-09 15:08:12 +0000148 assert(NTTP->getDepth() == 0 &&
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000149 "Cannot deduce non-type template argument with depth > 0");
150 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
151 "Expression template argument must be type- or value-dependent.");
Mike Stump11289f42009-09-09 15:08:12 +0000152
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000153 if (Deduced[NTTP->getIndex()].isNull()) {
Douglas Gregor0a29a052010-03-26 05:50:28 +0000154 Deduced[NTTP->getIndex()] = TemplateArgument(Value->Retain());
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000155 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000156 }
Mike Stump11289f42009-09-09 15:08:12 +0000157
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000158 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
Mike Stump11289f42009-09-09 15:08:12 +0000159 // Okay, we deduced a constant in one case and a dependent expression
160 // in another case. FIXME: Later, we will check that instantiating the
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000161 // dependent expression gives us the constant value.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000162 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000163 }
Mike Stump11289f42009-09-09 15:08:12 +0000164
Douglas Gregor00a511f2009-09-15 16:51:42 +0000165 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
166 // Compare the expressions for equality
167 llvm::FoldingSetNodeID ID1, ID2;
Chandler Carruthc1263112010-02-07 21:33:28 +0000168 Deduced[NTTP->getIndex()].getAsExpr()->Profile(ID1, S.Context, true);
169 Value->Profile(ID2, S.Context, true);
Douglas Gregor00a511f2009-09-15 16:51:42 +0000170 if (ID1 == ID2)
171 return Sema::TDK_Success;
172
173 // FIXME: Fill in argument mismatch information
174 return Sema::TDK_NonDeducedMismatch;
175 }
176
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000177 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000178}
179
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000180/// \brief Deduce the value of the given non-type template parameter
181/// from the given declaration.
182///
183/// \returns true if deduction succeeded, false otherwise.
184static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000185DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000186 NonTypeTemplateParmDecl *NTTP,
187 Decl *D,
John McCall19c1bfd2010-08-25 05:32:35 +0000188 TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000189 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000190 assert(NTTP->getDepth() == 0 &&
191 "Cannot deduce non-type template argument with depth > 0");
192
193 if (Deduced[NTTP->getIndex()].isNull()) {
194 Deduced[NTTP->getIndex()] = TemplateArgument(D->getCanonicalDecl());
195 return Sema::TDK_Success;
196 }
197
198 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
199 // Okay, we deduced a declaration in one case and a dependent expression
200 // in another case.
201 return Sema::TDK_Success;
202 }
203
204 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Declaration) {
205 // Compare the declarations for equality
206 if (Deduced[NTTP->getIndex()].getAsDecl()->getCanonicalDecl() ==
207 D->getCanonicalDecl())
208 return Sema::TDK_Success;
209
210 // FIXME: Fill in argument mismatch information
211 return Sema::TDK_NonDeducedMismatch;
212 }
213
214 return Sema::TDK_Success;
215}
216
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000217static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000218DeduceTemplateArguments(Sema &S,
Douglas Gregoradee3e32009-11-11 23:06:43 +0000219 TemplateParameterList *TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000220 TemplateName Param,
221 TemplateName Arg,
John McCall19c1bfd2010-08-25 05:32:35 +0000222 TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000223 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000224 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
Douglas Gregoradee3e32009-11-11 23:06:43 +0000225 if (!ParamDecl) {
226 // The parameter type is dependent and is not a template template parameter,
227 // so there is nothing that we can deduce.
228 return Sema::TDK_Success;
229 }
230
231 if (TemplateTemplateParmDecl *TempParam
232 = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) {
233 // Bind the template template parameter to the given template name.
234 TemplateArgument &ExistingArg = Deduced[TempParam->getIndex()];
235 if (ExistingArg.isNull()) {
236 // This is the first deduction for this template template parameter.
Chandler Carruthc1263112010-02-07 21:33:28 +0000237 ExistingArg = TemplateArgument(S.Context.getCanonicalTemplateName(Arg));
Douglas Gregoradee3e32009-11-11 23:06:43 +0000238 return Sema::TDK_Success;
239 }
240
241 // Verify that the previous binding matches this deduction.
242 assert(ExistingArg.getKind() == TemplateArgument::Template);
Chandler Carruthc1263112010-02-07 21:33:28 +0000243 if (S.Context.hasSameTemplateName(ExistingArg.getAsTemplate(), Arg))
Douglas Gregoradee3e32009-11-11 23:06:43 +0000244 return Sema::TDK_Success;
245
246 // Inconsistent deduction.
247 Info.Param = TempParam;
248 Info.FirstArg = ExistingArg;
249 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000250 return Sema::TDK_Inconsistent;
251 }
Douglas Gregoradee3e32009-11-11 23:06:43 +0000252
253 // Verify that the two template names are equivalent.
Chandler Carruthc1263112010-02-07 21:33:28 +0000254 if (S.Context.hasSameTemplateName(Param, Arg))
Douglas Gregoradee3e32009-11-11 23:06:43 +0000255 return Sema::TDK_Success;
256
257 // Mismatch of non-dependent template parameter to argument.
258 Info.FirstArg = TemplateArgument(Param);
259 Info.SecondArg = TemplateArgument(Arg);
260 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000261}
262
Mike Stump11289f42009-09-09 15:08:12 +0000263/// \brief Deduce the template arguments by comparing the template parameter
Douglas Gregore81f3e72009-07-07 23:09:34 +0000264/// type (which is a template-id) with the template argument type.
265///
Chandler Carruthc1263112010-02-07 21:33:28 +0000266/// \param S the Sema
Douglas Gregore81f3e72009-07-07 23:09:34 +0000267///
268/// \param TemplateParams the template parameters that we are deducing
269///
270/// \param Param the parameter type
271///
272/// \param Arg the argument type
273///
274/// \param Info information about the template argument deduction itself
275///
276/// \param Deduced the deduced template arguments
277///
278/// \returns the result of template argument deduction so far. Note that a
279/// "success" result means that template argument deduction has not yet failed,
280/// but it may still fail, later, for other reasons.
281static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000282DeduceTemplateArguments(Sema &S,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000283 TemplateParameterList *TemplateParams,
284 const TemplateSpecializationType *Param,
285 QualType Arg,
John McCall19c1bfd2010-08-25 05:32:35 +0000286 TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000287 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
John McCallb692a092009-10-22 20:10:53 +0000288 assert(Arg.isCanonical() && "Argument type must be canonical");
Mike Stump11289f42009-09-09 15:08:12 +0000289
Douglas Gregore81f3e72009-07-07 23:09:34 +0000290 // Check whether the template argument is a dependent template-id.
Mike Stump11289f42009-09-09 15:08:12 +0000291 if (const TemplateSpecializationType *SpecArg
Douglas Gregore81f3e72009-07-07 23:09:34 +0000292 = dyn_cast<TemplateSpecializationType>(Arg)) {
293 // Perform template argument deduction for the template name.
294 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000295 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000296 Param->getTemplateName(),
297 SpecArg->getTemplateName(),
298 Info, Deduced))
299 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000300
Mike Stump11289f42009-09-09 15:08:12 +0000301
Douglas Gregore81f3e72009-07-07 23:09:34 +0000302 // Perform template argument deduction on each template
303 // argument.
Douglas Gregoradee3e32009-11-11 23:06:43 +0000304 unsigned NumArgs = std::min(SpecArg->getNumArgs(), Param->getNumArgs());
Douglas Gregore81f3e72009-07-07 23:09:34 +0000305 for (unsigned I = 0; I != NumArgs; ++I)
306 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000307 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000308 Param->getArg(I),
309 SpecArg->getArg(I),
310 Info, Deduced))
311 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000312
Douglas Gregore81f3e72009-07-07 23:09:34 +0000313 return Sema::TDK_Success;
314 }
Mike Stump11289f42009-09-09 15:08:12 +0000315
Douglas Gregore81f3e72009-07-07 23:09:34 +0000316 // If the argument type is a class template specialization, we
317 // perform template argument deduction using its template
318 // arguments.
319 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
320 if (!RecordArg)
321 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000322
323 ClassTemplateSpecializationDecl *SpecArg
Douglas Gregore81f3e72009-07-07 23:09:34 +0000324 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
325 if (!SpecArg)
326 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000327
Douglas Gregore81f3e72009-07-07 23:09:34 +0000328 // Perform template argument deduction for the template name.
329 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000330 = DeduceTemplateArguments(S,
Douglas Gregoradee3e32009-11-11 23:06:43 +0000331 TemplateParams,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000332 Param->getTemplateName(),
333 TemplateName(SpecArg->getSpecializedTemplate()),
334 Info, Deduced))
335 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000336
Douglas Gregore81f3e72009-07-07 23:09:34 +0000337 unsigned NumArgs = Param->getNumArgs();
338 const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
339 if (NumArgs != ArgArgs.size())
340 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000341
Douglas Gregore81f3e72009-07-07 23:09:34 +0000342 for (unsigned I = 0; I != NumArgs; ++I)
Mike Stump11289f42009-09-09 15:08:12 +0000343 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000344 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000345 Param->getArg(I),
346 ArgArgs.get(I),
347 Info, Deduced))
348 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000349
Douglas Gregore81f3e72009-07-07 23:09:34 +0000350 return Sema::TDK_Success;
351}
352
Douglas Gregorcceb9752009-06-26 18:27:22 +0000353/// \brief Deduce the template arguments by comparing the parameter type and
354/// the argument type (C++ [temp.deduct.type]).
355///
Chandler Carruthc1263112010-02-07 21:33:28 +0000356/// \param S the semantic analysis object within which we are deducing
Douglas Gregorcceb9752009-06-26 18:27:22 +0000357///
358/// \param TemplateParams the template parameters that we are deducing
359///
360/// \param ParamIn the parameter type
361///
362/// \param ArgIn the argument type
363///
364/// \param Info information about the template argument deduction itself
365///
366/// \param Deduced the deduced template arguments
367///
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000368/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
Mike Stump11289f42009-09-09 15:08:12 +0000369/// how template argument deduction is performed.
Douglas Gregorcceb9752009-06-26 18:27:22 +0000370///
371/// \returns the result of template argument deduction so far. Note that a
372/// "success" result means that template argument deduction has not yet failed,
373/// but it may still fail, later, for other reasons.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000374static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000375DeduceTemplateArguments(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000376 TemplateParameterList *TemplateParams,
377 QualType ParamIn, QualType ArgIn,
John McCall19c1bfd2010-08-25 05:32:35 +0000378 TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000379 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000380 unsigned TDF) {
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000381 // We only want to look at the canonical types, since typedefs and
382 // sugar are not part of template argument deduction.
Chandler Carruthc1263112010-02-07 21:33:28 +0000383 QualType Param = S.Context.getCanonicalType(ParamIn);
384 QualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000385
Douglas Gregorcceb9752009-06-26 18:27:22 +0000386 // C++0x [temp.deduct.call]p4 bullet 1:
387 // - If the original P is a reference type, the deduced A (i.e., the type
Mike Stump11289f42009-09-09 15:08:12 +0000388 // referred to by the reference) can be more cv-qualified than the
Douglas Gregorcceb9752009-06-26 18:27:22 +0000389 // transformed A.
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000390 if (TDF & TDF_ParamWithReferenceType) {
Chandler Carruthc712ce12009-12-30 04:10:01 +0000391 Qualifiers Quals;
Chandler Carruthc1263112010-02-07 21:33:28 +0000392 QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals);
Chandler Carruthc712ce12009-12-30 04:10:01 +0000393 Quals.setCVRQualifiers(Quals.getCVRQualifiers() &
394 Arg.getCVRQualifiersThroughArrayTypes());
Chandler Carruthc1263112010-02-07 21:33:28 +0000395 Param = S.Context.getQualifiedType(UnqualParam, Quals);
Douglas Gregorcceb9752009-06-26 18:27:22 +0000396 }
Mike Stump11289f42009-09-09 15:08:12 +0000397
Douglas Gregor705c9002009-06-26 20:57:09 +0000398 // If the parameter type is not dependent, there is nothing to deduce.
Douglas Gregor406f6342009-09-14 20:00:47 +0000399 if (!Param->isDependentType()) {
400 if (!(TDF & TDF_SkipNonDependent) && Param != Arg) {
401
402 return Sema::TDK_NonDeducedMismatch;
403 }
404
Douglas Gregor705c9002009-06-26 20:57:09 +0000405 return Sema::TDK_Success;
Douglas Gregor406f6342009-09-14 20:00:47 +0000406 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000407
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000408 // C++ [temp.deduct.type]p9:
Mike Stump11289f42009-09-09 15:08:12 +0000409 // A template type argument T, a template template argument TT or a
410 // template non-type argument i can be deduced if P and A have one of
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000411 // the following forms:
412 //
413 // T
414 // cv-list T
Mike Stump11289f42009-09-09 15:08:12 +0000415 if (const TemplateTypeParmType *TemplateTypeParm
John McCall9dd450b2009-09-21 23:43:11 +0000416 = Param->getAs<TemplateTypeParmType>()) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000417 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregord6605db2009-07-22 21:30:48 +0000418 bool RecanonicalizeArg = false;
Mike Stump11289f42009-09-09 15:08:12 +0000419
Douglas Gregor60454822009-07-22 20:02:25 +0000420 // If the argument type is an array type, move the qualifiers up to the
421 // top level, so they can be matched with the qualifiers on the parameter.
422 // FIXME: address spaces, ObjC GC qualifiers
Douglas Gregord6605db2009-07-22 21:30:48 +0000423 if (isa<ArrayType>(Arg)) {
John McCall8ccfcb52009-09-24 19:53:00 +0000424 Qualifiers Quals;
Chandler Carruthc1263112010-02-07 21:33:28 +0000425 Arg = S.Context.getUnqualifiedArrayType(Arg, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +0000426 if (Quals) {
Chandler Carruthc1263112010-02-07 21:33:28 +0000427 Arg = S.Context.getQualifiedType(Arg, Quals);
Douglas Gregord6605db2009-07-22 21:30:48 +0000428 RecanonicalizeArg = true;
429 }
430 }
Mike Stump11289f42009-09-09 15:08:12 +0000431
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000432 // The argument type can not be less qualified than the parameter
433 // type.
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000434 if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000435 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
John McCall42d7d192010-08-05 09:05:08 +0000436 Info.FirstArg = TemplateArgument(Param);
John McCall0ad16662009-10-29 08:12:44 +0000437 Info.SecondArg = TemplateArgument(Arg);
John McCall42d7d192010-08-05 09:05:08 +0000438 return Sema::TDK_Underqualified;
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000439 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000440
441 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
Chandler Carruthc1263112010-02-07 21:33:28 +0000442 assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function");
John McCall8ccfcb52009-09-24 19:53:00 +0000443 QualType DeducedType = Arg;
444 DeducedType.removeCVRQualifiers(Param.getCVRQualifiers());
Douglas Gregord6605db2009-07-22 21:30:48 +0000445 if (RecanonicalizeArg)
Chandler Carruthc1263112010-02-07 21:33:28 +0000446 DeducedType = S.Context.getCanonicalType(DeducedType);
Mike Stump11289f42009-09-09 15:08:12 +0000447
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000448 if (Deduced[Index].isNull())
John McCall0ad16662009-10-29 08:12:44 +0000449 Deduced[Index] = TemplateArgument(DeducedType);
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000450 else {
Mike Stump11289f42009-09-09 15:08:12 +0000451 // C++ [temp.deduct.type]p2:
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000452 // [...] If type deduction cannot be done for any P/A pair, or if for
Mike Stump11289f42009-09-09 15:08:12 +0000453 // any pair the deduction leads to more than one possible set of
454 // deduced values, or if different pairs yield different deduced
455 // values, or if any template argument remains neither deduced nor
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000456 // explicitly specified, template argument deduction fails.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000457 if (Deduced[Index].getAsType() != DeducedType) {
Mike Stump11289f42009-09-09 15:08:12 +0000458 Info.Param
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000459 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
460 Info.FirstArg = Deduced[Index];
John McCall0ad16662009-10-29 08:12:44 +0000461 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000462 return Sema::TDK_Inconsistent;
463 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000464 }
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000465 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000466 }
467
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000468 // Set up the template argument deduction information for a failure.
John McCall0ad16662009-10-29 08:12:44 +0000469 Info.FirstArg = TemplateArgument(ParamIn);
470 Info.SecondArg = TemplateArgument(ArgIn);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000471
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000472 // Check the cv-qualifiers on the parameter and argument types.
473 if (!(TDF & TDF_IgnoreQualifiers)) {
474 if (TDF & TDF_ParamWithReferenceType) {
475 if (Param.isMoreQualifiedThan(Arg))
476 return Sema::TDK_NonDeducedMismatch;
477 } else {
478 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Mike Stump11289f42009-09-09 15:08:12 +0000479 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000480 }
481 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000482
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000483 switch (Param->getTypeClass()) {
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000484 // No deduction possible for these types
485 case Type::Builtin:
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000486 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000487
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000488 // T *
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000489 case Type::Pointer: {
John McCallbb4ea812010-05-13 07:48:05 +0000490 QualType PointeeType;
491 if (const PointerType *PointerArg = Arg->getAs<PointerType>()) {
492 PointeeType = PointerArg->getPointeeType();
493 } else if (const ObjCObjectPointerType *PointerArg
494 = Arg->getAs<ObjCObjectPointerType>()) {
495 PointeeType = PointerArg->getPointeeType();
496 } else {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000497 return Sema::TDK_NonDeducedMismatch;
John McCallbb4ea812010-05-13 07:48:05 +0000498 }
Mike Stump11289f42009-09-09 15:08:12 +0000499
Douglas Gregorfc516c92009-06-26 23:27:24 +0000500 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Chandler Carruthc1263112010-02-07 21:33:28 +0000501 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000502 cast<PointerType>(Param)->getPointeeType(),
John McCallbb4ea812010-05-13 07:48:05 +0000503 PointeeType,
Douglas Gregorfc516c92009-06-26 23:27:24 +0000504 Info, Deduced, SubTDF);
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000505 }
Mike Stump11289f42009-09-09 15:08:12 +0000506
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000507 // T &
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000508 case Type::LValueReference: {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000509 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000510 if (!ReferenceArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000511 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000512
Chandler Carruthc1263112010-02-07 21:33:28 +0000513 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000514 cast<LValueReferenceType>(Param)->getPointeeType(),
515 ReferenceArg->getPointeeType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000516 Info, Deduced, 0);
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000517 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000518
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000519 // T && [C++0x]
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000520 case Type::RValueReference: {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000521 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000522 if (!ReferenceArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000523 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000524
Chandler Carruthc1263112010-02-07 21:33:28 +0000525 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000526 cast<RValueReferenceType>(Param)->getPointeeType(),
527 ReferenceArg->getPointeeType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000528 Info, Deduced, 0);
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000529 }
Mike Stump11289f42009-09-09 15:08:12 +0000530
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000531 // T [] (implied, but not stated explicitly)
Anders Carlsson35533d12009-06-04 04:11:30 +0000532 case Type::IncompleteArray: {
Mike Stump11289f42009-09-09 15:08:12 +0000533 const IncompleteArrayType *IncompleteArrayArg =
Chandler Carruthc1263112010-02-07 21:33:28 +0000534 S.Context.getAsIncompleteArrayType(Arg);
Anders Carlsson35533d12009-06-04 04:11:30 +0000535 if (!IncompleteArrayArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000536 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000537
John McCallf7332682010-08-19 00:20:19 +0000538 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
Chandler Carruthc1263112010-02-07 21:33:28 +0000539 return DeduceTemplateArguments(S, TemplateParams,
540 S.Context.getAsIncompleteArrayType(Param)->getElementType(),
Anders Carlsson35533d12009-06-04 04:11:30 +0000541 IncompleteArrayArg->getElementType(),
John McCallf7332682010-08-19 00:20:19 +0000542 Info, Deduced, SubTDF);
Anders Carlsson35533d12009-06-04 04:11:30 +0000543 }
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000544
545 // T [integer-constant]
Anders Carlsson35533d12009-06-04 04:11:30 +0000546 case Type::ConstantArray: {
Mike Stump11289f42009-09-09 15:08:12 +0000547 const ConstantArrayType *ConstantArrayArg =
Chandler Carruthc1263112010-02-07 21:33:28 +0000548 S.Context.getAsConstantArrayType(Arg);
Anders Carlsson35533d12009-06-04 04:11:30 +0000549 if (!ConstantArrayArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000550 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000551
552 const ConstantArrayType *ConstantArrayParm =
Chandler Carruthc1263112010-02-07 21:33:28 +0000553 S.Context.getAsConstantArrayType(Param);
Anders Carlsson35533d12009-06-04 04:11:30 +0000554 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000555 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000556
John McCallf7332682010-08-19 00:20:19 +0000557 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
Chandler Carruthc1263112010-02-07 21:33:28 +0000558 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson35533d12009-06-04 04:11:30 +0000559 ConstantArrayParm->getElementType(),
560 ConstantArrayArg->getElementType(),
John McCallf7332682010-08-19 00:20:19 +0000561 Info, Deduced, SubTDF);
Anders Carlsson35533d12009-06-04 04:11:30 +0000562 }
563
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000564 // type [i]
565 case Type::DependentSizedArray: {
Chandler Carruthc1263112010-02-07 21:33:28 +0000566 const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000567 if (!ArrayArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000568 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000569
John McCallf7332682010-08-19 00:20:19 +0000570 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
571
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000572 // Check the element type of the arrays
573 const DependentSizedArrayType *DependentArrayParm
Chandler Carruthc1263112010-02-07 21:33:28 +0000574 = S.Context.getAsDependentSizedArrayType(Param);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000575 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000576 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000577 DependentArrayParm->getElementType(),
578 ArrayArg->getElementType(),
John McCallf7332682010-08-19 00:20:19 +0000579 Info, Deduced, SubTDF))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000580 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000581
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000582 // Determine the array bound is something we can deduce.
Mike Stump11289f42009-09-09 15:08:12 +0000583 NonTypeTemplateParmDecl *NTTP
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000584 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
585 if (!NTTP)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000586 return Sema::TDK_Success;
Mike Stump11289f42009-09-09 15:08:12 +0000587
588 // We can perform template argument deduction for the given non-type
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000589 // template parameter.
Mike Stump11289f42009-09-09 15:08:12 +0000590 assert(NTTP->getDepth() == 0 &&
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000591 "Cannot deduce non-type template argument at depth > 0");
Mike Stump11289f42009-09-09 15:08:12 +0000592 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson3a106e02009-06-16 22:44:31 +0000593 = dyn_cast<ConstantArrayType>(ArrayArg)) {
594 llvm::APSInt Size(ConstantArrayArg->getSize());
Douglas Gregor0a29a052010-03-26 05:50:28 +0000595 return DeduceNonTypeTemplateArgument(S, NTTP, Size,
596 S.Context.getSizeType(),
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000597 /*ArrayBound=*/true,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000598 Info, Deduced);
Anders Carlsson3a106e02009-06-16 22:44:31 +0000599 }
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000600 if (const DependentSizedArrayType *DependentArrayArg
601 = dyn_cast<DependentSizedArrayType>(ArrayArg))
Chandler Carruthc1263112010-02-07 21:33:28 +0000602 return DeduceNonTypeTemplateArgument(S, NTTP,
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000603 DependentArrayArg->getSizeExpr(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000604 Info, Deduced);
Mike Stump11289f42009-09-09 15:08:12 +0000605
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000606 // Incomplete type does not match a dependently-sized array type
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000607 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000608 }
Mike Stump11289f42009-09-09 15:08:12 +0000609
610 // type(*)(T)
611 // T(*)()
612 // T(*)(T)
Anders Carlsson2128ec72009-06-08 15:19:08 +0000613 case Type::FunctionProto: {
Mike Stump11289f42009-09-09 15:08:12 +0000614 const FunctionProtoType *FunctionProtoArg =
Anders Carlsson2128ec72009-06-08 15:19:08 +0000615 dyn_cast<FunctionProtoType>(Arg);
616 if (!FunctionProtoArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000617 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000618
619 const FunctionProtoType *FunctionProtoParam =
Anders Carlsson2128ec72009-06-08 15:19:08 +0000620 cast<FunctionProtoType>(Param);
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000621
Mike Stump11289f42009-09-09 15:08:12 +0000622 if (FunctionProtoParam->getTypeQuals() !=
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000623 FunctionProtoArg->getTypeQuals())
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->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000627 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000628
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000629 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000630 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000631
Anders Carlsson2128ec72009-06-08 15:19:08 +0000632 // Check return types.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000633 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000634 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000635 FunctionProtoParam->getResultType(),
636 FunctionProtoArg->getResultType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000637 Info, Deduced, 0))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000638 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000639
Anders Carlsson2128ec72009-06-08 15:19:08 +0000640 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
641 // Check argument types.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000642 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000643 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000644 FunctionProtoParam->getArgType(I),
645 FunctionProtoArg->getArgType(I),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000646 Info, Deduced, 0))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000647 return Result;
Anders Carlsson2128ec72009-06-08 15:19:08 +0000648 }
Mike Stump11289f42009-09-09 15:08:12 +0000649
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000650 return Sema::TDK_Success;
Anders Carlsson2128ec72009-06-08 15:19:08 +0000651 }
Mike Stump11289f42009-09-09 15:08:12 +0000652
John McCalle78aac42010-03-10 03:28:59 +0000653 case Type::InjectedClassName: {
654 // Treat a template's injected-class-name as if the template
655 // specialization type had been used.
John McCall2408e322010-04-27 00:57:59 +0000656 Param = cast<InjectedClassNameType>(Param)
657 ->getInjectedSpecializationType();
John McCalle78aac42010-03-10 03:28:59 +0000658 assert(isa<TemplateSpecializationType>(Param) &&
659 "injected class name is not a template specialization type");
660 // fall through
661 }
662
Douglas Gregor705c9002009-06-26 20:57:09 +0000663 // template-name<T> (where template-name refers to a class template)
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000664 // template-name<i>
Douglas Gregoradee3e32009-11-11 23:06:43 +0000665 // TT<T>
666 // TT<i>
667 // TT<>
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000668 case Type::TemplateSpecialization: {
669 const TemplateSpecializationType *SpecParam
670 = cast<TemplateSpecializationType>(Param);
Mike Stump11289f42009-09-09 15:08:12 +0000671
Douglas Gregore81f3e72009-07-07 23:09:34 +0000672 // Try to deduce template arguments from the template-id.
673 Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000674 = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000675 Info, Deduced);
Mike Stump11289f42009-09-09 15:08:12 +0000676
Douglas Gregor42909752009-09-30 22:13:51 +0000677 if (Result && (TDF & TDF_DerivedClass)) {
Douglas Gregore81f3e72009-07-07 23:09:34 +0000678 // C++ [temp.deduct.call]p3b3:
679 // If P is a class, and P has the form template-id, then A can be a
680 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump11289f42009-09-09 15:08:12 +0000681 // class of the form template-id, A can be a pointer to a derived
Douglas Gregore81f3e72009-07-07 23:09:34 +0000682 // class pointed to by the deduced A.
683 //
684 // More importantly:
Mike Stump11289f42009-09-09 15:08:12 +0000685 // These alternatives are considered only if type deduction would
Douglas Gregore81f3e72009-07-07 23:09:34 +0000686 // otherwise fail.
Chandler Carruthc1263112010-02-07 21:33:28 +0000687 if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
688 // We cannot inspect base classes as part of deduction when the type
689 // is incomplete, so either instantiate any templates necessary to
690 // complete the type, or skip over it if it cannot be completed.
John McCallbc077cf2010-02-08 23:07:23 +0000691 if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
Chandler Carruthc1263112010-02-07 21:33:28 +0000692 return Result;
693
Douglas Gregore81f3e72009-07-07 23:09:34 +0000694 // Use data recursion to crawl through the list of base classes.
Mike Stump11289f42009-09-09 15:08:12 +0000695 // Visited contains the set of nodes we have already visited, while
Douglas Gregore81f3e72009-07-07 23:09:34 +0000696 // ToVisit is our stack of records that we still need to visit.
697 llvm::SmallPtrSet<const RecordType *, 8> Visited;
698 llvm::SmallVector<const RecordType *, 8> ToVisit;
699 ToVisit.push_back(RecordT);
700 bool Successful = false;
701 while (!ToVisit.empty()) {
702 // Retrieve the next class in the inheritance hierarchy.
703 const RecordType *NextT = ToVisit.back();
704 ToVisit.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000705
Douglas Gregore81f3e72009-07-07 23:09:34 +0000706 // If we have already seen this type, skip it.
707 if (!Visited.insert(NextT))
708 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000709
Douglas Gregore81f3e72009-07-07 23:09:34 +0000710 // If this is a base class, try to perform template argument
711 // deduction from it.
712 if (NextT != RecordT) {
713 Sema::TemplateDeductionResult BaseResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000714 = DeduceTemplateArguments(S, TemplateParams, SpecParam,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000715 QualType(NextT, 0), Info, Deduced);
Mike Stump11289f42009-09-09 15:08:12 +0000716
Douglas Gregore81f3e72009-07-07 23:09:34 +0000717 // If template argument deduction for this base was successful,
718 // note that we had some success.
719 if (BaseResult == Sema::TDK_Success)
720 Successful = true;
Douglas Gregore81f3e72009-07-07 23:09:34 +0000721 }
Mike Stump11289f42009-09-09 15:08:12 +0000722
Douglas Gregore81f3e72009-07-07 23:09:34 +0000723 // Visit base classes
724 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
725 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
726 BaseEnd = Next->bases_end();
Sebastian Redl1054fae2009-10-25 17:03:50 +0000727 Base != BaseEnd; ++Base) {
Mike Stump11289f42009-09-09 15:08:12 +0000728 assert(Base->getType()->isRecordType() &&
Douglas Gregore81f3e72009-07-07 23:09:34 +0000729 "Base class that isn't a record?");
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000730 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregore81f3e72009-07-07 23:09:34 +0000731 }
732 }
Mike Stump11289f42009-09-09 15:08:12 +0000733
Douglas Gregore81f3e72009-07-07 23:09:34 +0000734 if (Successful)
735 return Sema::TDK_Success;
736 }
Mike Stump11289f42009-09-09 15:08:12 +0000737
Douglas Gregore81f3e72009-07-07 23:09:34 +0000738 }
Mike Stump11289f42009-09-09 15:08:12 +0000739
Douglas Gregore81f3e72009-07-07 23:09:34 +0000740 return Result;
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000741 }
742
Douglas Gregor637d9982009-06-10 23:47:09 +0000743 // T type::*
744 // T T::*
745 // T (type::*)()
746 // type (T::*)()
747 // type (type::*)(T)
748 // type (T::*)(T)
749 // T (type::*)(T)
750 // T (T::*)()
751 // T (T::*)(T)
752 case Type::MemberPointer: {
753 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
754 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
755 if (!MemPtrArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000756 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637d9982009-06-10 23:47:09 +0000757
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000758 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000759 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000760 MemPtrParam->getPointeeType(),
761 MemPtrArg->getPointeeType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000762 Info, Deduced,
763 TDF & TDF_IgnoreQualifiers))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000764 return Result;
765
Chandler Carruthc1263112010-02-07 21:33:28 +0000766 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000767 QualType(MemPtrParam->getClass(), 0),
768 QualType(MemPtrArg->getClass(), 0),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000769 Info, Deduced, 0);
Douglas Gregor637d9982009-06-10 23:47:09 +0000770 }
771
Anders Carlsson15f1dd12009-06-12 22:56:54 +0000772 // (clang extension)
773 //
Mike Stump11289f42009-09-09 15:08:12 +0000774 // type(^)(T)
775 // T(^)()
776 // T(^)(T)
Anders Carlssona767eee2009-06-12 16:23:10 +0000777 case Type::BlockPointer: {
778 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
779 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump11289f42009-09-09 15:08:12 +0000780
Anders Carlssona767eee2009-06-12 16:23:10 +0000781 if (!BlockPtrArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000782 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000783
Chandler Carruthc1263112010-02-07 21:33:28 +0000784 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlssona767eee2009-06-12 16:23:10 +0000785 BlockPtrParam->getPointeeType(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000786 BlockPtrArg->getPointeeType(), Info,
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000787 Deduced, 0);
Anders Carlssona767eee2009-06-12 16:23:10 +0000788 }
789
Douglas Gregor637d9982009-06-10 23:47:09 +0000790 case Type::TypeOfExpr:
791 case Type::TypeOf:
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000792 case Type::DependentName:
Douglas Gregor637d9982009-06-10 23:47:09 +0000793 // No template argument deduction for these types
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000794 return Sema::TDK_Success;
Douglas Gregor637d9982009-06-10 23:47:09 +0000795
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000796 default:
797 break;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000798 }
799
800 // FIXME: Many more cases to go (to go).
Douglas Gregor705c9002009-06-26 20:57:09 +0000801 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000802}
803
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000804static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000805DeduceTemplateArguments(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000806 TemplateParameterList *TemplateParams,
807 const TemplateArgument &Param,
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000808 const TemplateArgument &Arg,
John McCall19c1bfd2010-08-25 05:32:35 +0000809 TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000810 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000811 switch (Param.getKind()) {
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000812 case TemplateArgument::Null:
813 assert(false && "Null template argument in parameter list");
814 break;
Mike Stump11289f42009-09-09 15:08:12 +0000815
816 case TemplateArgument::Type:
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000817 if (Arg.getKind() == TemplateArgument::Type)
Chandler Carruthc1263112010-02-07 21:33:28 +0000818 return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000819 Arg.getAsType(), Info, Deduced, 0);
820 Info.FirstArg = Param;
821 Info.SecondArg = Arg;
822 return Sema::TDK_NonDeducedMismatch;
823
824 case TemplateArgument::Template:
Douglas Gregoradee3e32009-11-11 23:06:43 +0000825 if (Arg.getKind() == TemplateArgument::Template)
Chandler Carruthc1263112010-02-07 21:33:28 +0000826 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000827 Param.getAsTemplate(),
Douglas Gregoradee3e32009-11-11 23:06:43 +0000828 Arg.getAsTemplate(), Info, Deduced);
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000829 Info.FirstArg = Param;
830 Info.SecondArg = Arg;
831 return Sema::TDK_NonDeducedMismatch;
832
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000833 case TemplateArgument::Declaration:
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000834 if (Arg.getKind() == TemplateArgument::Declaration &&
835 Param.getAsDecl()->getCanonicalDecl() ==
836 Arg.getAsDecl()->getCanonicalDecl())
837 return Sema::TDK_Success;
838
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000839 Info.FirstArg = Param;
840 Info.SecondArg = Arg;
841 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000842
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000843 case TemplateArgument::Integral:
844 if (Arg.getKind() == TemplateArgument::Integral) {
Douglas Gregor0a29a052010-03-26 05:50:28 +0000845 if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral()))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000846 return Sema::TDK_Success;
847
848 Info.FirstArg = Param;
849 Info.SecondArg = Arg;
850 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000851 }
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000852
853 if (Arg.getKind() == TemplateArgument::Expression) {
854 Info.FirstArg = Param;
855 Info.SecondArg = Arg;
856 return Sema::TDK_NonDeducedMismatch;
857 }
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000858
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000859 Info.FirstArg = Param;
860 Info.SecondArg = Arg;
861 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000862
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000863 case TemplateArgument::Expression: {
Mike Stump11289f42009-09-09 15:08:12 +0000864 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000865 = getDeducedParameterFromExpr(Param.getAsExpr())) {
866 if (Arg.getKind() == TemplateArgument::Integral)
Chandler Carruthc1263112010-02-07 21:33:28 +0000867 return DeduceNonTypeTemplateArgument(S, NTTP,
Mike Stump11289f42009-09-09 15:08:12 +0000868 *Arg.getAsIntegral(),
Douglas Gregor0a29a052010-03-26 05:50:28 +0000869 Arg.getIntegralType(),
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000870 /*ArrayBound=*/false,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000871 Info, Deduced);
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000872 if (Arg.getKind() == TemplateArgument::Expression)
Chandler Carruthc1263112010-02-07 21:33:28 +0000873 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000874 Info, Deduced);
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000875 if (Arg.getKind() == TemplateArgument::Declaration)
Chandler Carruthc1263112010-02-07 21:33:28 +0000876 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000877 Info, Deduced);
878
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000879 Info.FirstArg = Param;
880 Info.SecondArg = Arg;
881 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000882 }
Mike Stump11289f42009-09-09 15:08:12 +0000883
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000884 // Can't deduce anything, but that's okay.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000885 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000886 }
Anders Carlssonbc343912009-06-15 17:04:53 +0000887 case TemplateArgument::Pack:
888 assert(0 && "FIXME: Implement!");
889 break;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000890 }
Mike Stump11289f42009-09-09 15:08:12 +0000891
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000892 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000893}
894
Mike Stump11289f42009-09-09 15:08:12 +0000895static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000896DeduceTemplateArguments(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000897 TemplateParameterList *TemplateParams,
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000898 const TemplateArgumentList &ParamList,
899 const TemplateArgumentList &ArgList,
John McCall19c1bfd2010-08-25 05:32:35 +0000900 TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000901 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000902 assert(ParamList.size() == ArgList.size());
903 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000904 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000905 = DeduceTemplateArguments(S, TemplateParams,
Mike Stump11289f42009-09-09 15:08:12 +0000906 ParamList[I], ArgList[I],
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000907 Info, Deduced))
908 return Result;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000909 }
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000910 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000911}
912
Douglas Gregor705c9002009-06-26 20:57:09 +0000913/// \brief Determine whether two template arguments are the same.
Mike Stump11289f42009-09-09 15:08:12 +0000914static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregor705c9002009-06-26 20:57:09 +0000915 const TemplateArgument &X,
916 const TemplateArgument &Y) {
917 if (X.getKind() != Y.getKind())
918 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000919
Douglas Gregor705c9002009-06-26 20:57:09 +0000920 switch (X.getKind()) {
921 case TemplateArgument::Null:
922 assert(false && "Comparing NULL template argument");
923 break;
Mike Stump11289f42009-09-09 15:08:12 +0000924
Douglas Gregor705c9002009-06-26 20:57:09 +0000925 case TemplateArgument::Type:
926 return Context.getCanonicalType(X.getAsType()) ==
927 Context.getCanonicalType(Y.getAsType());
Mike Stump11289f42009-09-09 15:08:12 +0000928
Douglas Gregor705c9002009-06-26 20:57:09 +0000929 case TemplateArgument::Declaration:
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +0000930 return X.getAsDecl()->getCanonicalDecl() ==
931 Y.getAsDecl()->getCanonicalDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000932
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000933 case TemplateArgument::Template:
934 return Context.getCanonicalTemplateName(X.getAsTemplate())
935 .getAsVoidPointer() ==
936 Context.getCanonicalTemplateName(Y.getAsTemplate())
937 .getAsVoidPointer();
938
Douglas Gregor705c9002009-06-26 20:57:09 +0000939 case TemplateArgument::Integral:
940 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump11289f42009-09-09 15:08:12 +0000941
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000942 case TemplateArgument::Expression: {
943 llvm::FoldingSetNodeID XID, YID;
944 X.getAsExpr()->Profile(XID, Context, true);
945 Y.getAsExpr()->Profile(YID, Context, true);
946 return XID == YID;
947 }
Mike Stump11289f42009-09-09 15:08:12 +0000948
Douglas Gregor705c9002009-06-26 20:57:09 +0000949 case TemplateArgument::Pack:
950 if (X.pack_size() != Y.pack_size())
951 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000952
953 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
954 XPEnd = X.pack_end(),
Douglas Gregor705c9002009-06-26 20:57:09 +0000955 YP = Y.pack_begin();
Mike Stump11289f42009-09-09 15:08:12 +0000956 XP != XPEnd; ++XP, ++YP)
Douglas Gregor705c9002009-06-26 20:57:09 +0000957 if (!isSameTemplateArg(Context, *XP, *YP))
958 return false;
959
960 return true;
961 }
962
963 return false;
964}
965
966/// \brief Helper function to build a TemplateParameter when we don't
967/// know its type statically.
968static TemplateParameter makeTemplateParameter(Decl *D) {
969 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
970 return TemplateParameter(TTP);
971 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
972 return TemplateParameter(NTTP);
Mike Stump11289f42009-09-09 15:08:12 +0000973
Douglas Gregor705c9002009-06-26 20:57:09 +0000974 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
975}
976
Douglas Gregor684268d2010-04-29 06:21:43 +0000977/// Complete template argument deduction for a class template partial
978/// specialization.
979static Sema::TemplateDeductionResult
980FinishTemplateArgumentDeduction(Sema &S,
981 ClassTemplatePartialSpecializationDecl *Partial,
982 const TemplateArgumentList &TemplateArgs,
983 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
John McCall19c1bfd2010-08-25 05:32:35 +0000984 TemplateDeductionInfo &Info) {
Douglas Gregor684268d2010-04-29 06:21:43 +0000985 // Trap errors.
986 Sema::SFINAETrap Trap(S);
987
988 Sema::ContextRAII SavedContext(S, Partial);
989
990 // C++ [temp.deduct.type]p2:
991 // [...] or if any template argument remains neither deduced nor
992 // explicitly specified, template argument deduction fails.
993 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
994 Deduced.size());
995 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
996 if (Deduced[I].isNull()) {
997 Decl *Param
998 = const_cast<NamedDecl *>(
999 Partial->getTemplateParameters()->getParam(I));
1000 Info.Param = makeTemplateParameter(Param);
1001 return Sema::TDK_Incomplete;
1002 }
1003
1004 Builder.Append(Deduced[I]);
1005 }
1006
1007 // Form the template argument list from the deduced template arguments.
1008 TemplateArgumentList *DeducedArgumentList
1009 = new (S.Context) TemplateArgumentList(S.Context, Builder,
1010 /*TakeArgs=*/true);
1011 Info.reset(DeducedArgumentList);
1012
1013 // Substitute the deduced template arguments into the template
1014 // arguments of the class template partial specialization, and
1015 // verify that the instantiated template arguments are both valid
1016 // and are equivalent to the template arguments originally provided
1017 // to the class template.
1018 // FIXME: Do we have to correct the types of deduced non-type template
1019 // arguments (in particular, integral non-type template arguments?).
John McCall19c1bfd2010-08-25 05:32:35 +00001020 LocalInstantiationScope InstScope(S);
Douglas Gregor684268d2010-04-29 06:21:43 +00001021 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
1022 const TemplateArgumentLoc *PartialTemplateArgs
1023 = Partial->getTemplateArgsAsWritten();
1024 unsigned N = Partial->getNumTemplateArgsAsWritten();
1025
1026 // Note that we don't provide the langle and rangle locations.
1027 TemplateArgumentListInfo InstArgs;
1028
1029 for (unsigned I = 0; I != N; ++I) {
1030 Decl *Param = const_cast<NamedDecl *>(
1031 ClassTemplate->getTemplateParameters()->getParam(I));
1032 TemplateArgumentLoc InstArg;
1033 if (S.Subst(PartialTemplateArgs[I], InstArg,
1034 MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
1035 Info.Param = makeTemplateParameter(Param);
1036 Info.FirstArg = PartialTemplateArgs[I].getArgument();
1037 return Sema::TDK_SubstitutionFailure;
1038 }
1039 InstArgs.addArgument(InstArg);
1040 }
1041
1042 TemplateArgumentListBuilder ConvertedInstArgs(
1043 ClassTemplate->getTemplateParameters(), N);
1044
1045 if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
Douglas Gregord09efd42010-05-08 20:07:26 +00001046 InstArgs, false, ConvertedInstArgs))
Douglas Gregor684268d2010-04-29 06:21:43 +00001047 return Sema::TDK_SubstitutionFailure;
Douglas Gregor684268d2010-04-29 06:21:43 +00001048
1049 for (unsigned I = 0, E = ConvertedInstArgs.flatSize(); I != E; ++I) {
1050 TemplateArgument InstArg = ConvertedInstArgs.getFlatArguments()[I];
1051
1052 Decl *Param = const_cast<NamedDecl *>(
1053 ClassTemplate->getTemplateParameters()->getParam(I));
1054
1055 if (InstArg.getKind() == TemplateArgument::Expression) {
1056 // When the argument is an expression, check the expression result
1057 // against the actual template parameter to get down to the canonical
1058 // template argument.
1059 Expr *InstExpr = InstArg.getAsExpr();
1060 if (NonTypeTemplateParmDecl *NTTP
1061 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1062 if (S.CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
1063 Info.Param = makeTemplateParameter(Param);
1064 Info.FirstArg = Partial->getTemplateArgs()[I];
1065 return Sema::TDK_SubstitutionFailure;
1066 }
1067 }
1068 }
1069
1070 if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
1071 Info.Param = makeTemplateParameter(Param);
1072 Info.FirstArg = TemplateArgs[I];
1073 Info.SecondArg = InstArg;
1074 return Sema::TDK_NonDeducedMismatch;
1075 }
1076 }
1077
1078 if (Trap.hasErrorOccurred())
1079 return Sema::TDK_SubstitutionFailure;
1080
1081 return Sema::TDK_Success;
1082}
1083
Douglas Gregor170bc422009-06-12 22:31:52 +00001084/// \brief Perform template argument deduction to determine whether
1085/// the given template arguments match the given class template
1086/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001087Sema::TemplateDeductionResult
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001088Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001089 const TemplateArgumentList &TemplateArgs,
1090 TemplateDeductionInfo &Info) {
Douglas Gregor170bc422009-06-12 22:31:52 +00001091 // C++ [temp.class.spec.match]p2:
1092 // A partial specialization matches a given actual template
1093 // argument list if the template arguments of the partial
1094 // specialization can be deduced from the actual template argument
1095 // list (14.8.2).
Douglas Gregore1416332009-06-14 08:02:22 +00001096 SFINAETrap Trap(*this);
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001097 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001098 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001099 if (TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +00001100 = ::DeduceTemplateArguments(*this,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001101 Partial->getTemplateParameters(),
Mike Stump11289f42009-09-09 15:08:12 +00001102 Partial->getTemplateArgs(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001103 TemplateArgs, Info, Deduced))
1104 return Result;
Douglas Gregor637d9982009-06-10 23:47:09 +00001105
Douglas Gregor637d9982009-06-10 23:47:09 +00001106 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
1107 Deduced.data(), Deduced.size());
1108 if (Inst)
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001109 return TDK_InstantiationDepth;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001110
Douglas Gregore1416332009-06-14 08:02:22 +00001111 if (Trap.hasErrorOccurred())
Douglas Gregor684268d2010-04-29 06:21:43 +00001112 return Sema::TDK_SubstitutionFailure;
1113
1114 return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
1115 Deduced, Info);
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001116}
Douglas Gregor91772d12009-06-13 00:26:55 +00001117
Douglas Gregorfc516c92009-06-26 23:27:24 +00001118/// \brief Determine whether the given type T is a simple-template-id type.
1119static bool isSimpleTemplateIdType(QualType T) {
Mike Stump11289f42009-09-09 15:08:12 +00001120 if (const TemplateSpecializationType *Spec
John McCall9dd450b2009-09-21 23:43:11 +00001121 = T->getAs<TemplateSpecializationType>())
Douglas Gregorfc516c92009-06-26 23:27:24 +00001122 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump11289f42009-09-09 15:08:12 +00001123
Douglas Gregorfc516c92009-06-26 23:27:24 +00001124 return false;
1125}
Douglas Gregor9b146582009-07-08 20:55:45 +00001126
1127/// \brief Substitute the explicitly-provided template arguments into the
1128/// given function template according to C++ [temp.arg.explicit].
1129///
1130/// \param FunctionTemplate the function template into which the explicit
1131/// template arguments will be substituted.
1132///
Mike Stump11289f42009-09-09 15:08:12 +00001133/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor9b146582009-07-08 20:55:45 +00001134/// arguments.
1135///
Mike Stump11289f42009-09-09 15:08:12 +00001136/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor9b146582009-07-08 20:55:45 +00001137/// with the converted and checked explicit template arguments.
1138///
Mike Stump11289f42009-09-09 15:08:12 +00001139/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor9b146582009-07-08 20:55:45 +00001140/// parameters.
1141///
1142/// \param FunctionType if non-NULL, the result type of the function template
1143/// will also be instantiated and the pointed-to value will be updated with
1144/// the instantiated function type.
1145///
1146/// \param Info if substitution fails for any reason, this object will be
1147/// populated with more information about the failure.
1148///
1149/// \returns TDK_Success if substitution was successful, or some failure
1150/// condition.
1151Sema::TemplateDeductionResult
1152Sema::SubstituteExplicitTemplateArguments(
1153 FunctionTemplateDecl *FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001154 const TemplateArgumentListInfo &ExplicitTemplateArgs,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001155 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregor9b146582009-07-08 20:55:45 +00001156 llvm::SmallVectorImpl<QualType> &ParamTypes,
1157 QualType *FunctionType,
1158 TemplateDeductionInfo &Info) {
1159 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1160 TemplateParameterList *TemplateParams
1161 = FunctionTemplate->getTemplateParameters();
1162
John McCall6b51f282009-11-23 01:53:49 +00001163 if (ExplicitTemplateArgs.size() == 0) {
Douglas Gregor9b146582009-07-08 20:55:45 +00001164 // No arguments to substitute; just copy over the parameter types and
1165 // fill in the function type.
1166 for (FunctionDecl::param_iterator P = Function->param_begin(),
1167 PEnd = Function->param_end();
1168 P != PEnd;
1169 ++P)
1170 ParamTypes.push_back((*P)->getType());
Mike Stump11289f42009-09-09 15:08:12 +00001171
Douglas Gregor9b146582009-07-08 20:55:45 +00001172 if (FunctionType)
1173 *FunctionType = Function->getType();
1174 return TDK_Success;
1175 }
Mike Stump11289f42009-09-09 15:08:12 +00001176
Douglas Gregor9b146582009-07-08 20:55:45 +00001177 // Substitution of the explicit template arguments into a function template
1178 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump11289f42009-09-09 15:08:12 +00001179 SFINAETrap Trap(*this);
1180
Douglas Gregor9b146582009-07-08 20:55:45 +00001181 // C++ [temp.arg.explicit]p3:
Mike Stump11289f42009-09-09 15:08:12 +00001182 // Template arguments that are present shall be specified in the
1183 // declaration order of their corresponding template-parameters. The
Douglas Gregor9b146582009-07-08 20:55:45 +00001184 // template argument list shall not specify more template-arguments than
Mike Stump11289f42009-09-09 15:08:12 +00001185 // there are corresponding template-parameters.
1186 TemplateArgumentListBuilder Builder(TemplateParams,
John McCall6b51f282009-11-23 01:53:49 +00001187 ExplicitTemplateArgs.size());
Mike Stump11289f42009-09-09 15:08:12 +00001188
1189 // Enter a new template instantiation context where we check the
Douglas Gregor9b146582009-07-08 20:55:45 +00001190 // explicitly-specified template arguments against this function template,
1191 // and then substitute them into the function parameter types.
Mike Stump11289f42009-09-09 15:08:12 +00001192 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor9b146582009-07-08 20:55:45 +00001193 FunctionTemplate, Deduced.data(), Deduced.size(),
1194 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution);
1195 if (Inst)
1196 return TDK_InstantiationDepth;
Mike Stump11289f42009-09-09 15:08:12 +00001197
John McCalle23b8712010-04-29 01:18:58 +00001198 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
John McCall80e58cd2010-04-29 00:35:03 +00001199
Douglas Gregor9b146582009-07-08 20:55:45 +00001200 if (CheckTemplateArgumentList(FunctionTemplate,
Douglas Gregor9b146582009-07-08 20:55:45 +00001201 SourceLocation(),
John McCall6b51f282009-11-23 01:53:49 +00001202 ExplicitTemplateArgs,
Douglas Gregor9b146582009-07-08 20:55:45 +00001203 true,
Douglas Gregor1d72edd2010-05-08 19:15:54 +00001204 Builder) || Trap.hasErrorOccurred()) {
Douglas Gregor62c281a2010-05-09 01:26:06 +00001205 unsigned Index = Builder.structuredSize();
1206 if (Index >= TemplateParams->size())
1207 Index = TemplateParams->size() - 1;
1208 Info.Param = makeTemplateParameter(TemplateParams->getParam(Index));
Douglas Gregor9b146582009-07-08 20:55:45 +00001209 return TDK_InvalidExplicitArguments;
Douglas Gregor1d72edd2010-05-08 19:15:54 +00001210 }
Mike Stump11289f42009-09-09 15:08:12 +00001211
Douglas Gregor9b146582009-07-08 20:55:45 +00001212 // Form the template argument list from the explicitly-specified
1213 // template arguments.
Mike Stump11289f42009-09-09 15:08:12 +00001214 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor9b146582009-07-08 20:55:45 +00001215 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1216 Info.reset(ExplicitArgumentList);
Mike Stump11289f42009-09-09 15:08:12 +00001217
Douglas Gregor9b146582009-07-08 20:55:45 +00001218 // Instantiate the types of each of the function parameters given the
1219 // explicitly-specified template arguments.
1220 for (FunctionDecl::param_iterator P = Function->param_begin(),
1221 PEnd = Function->param_end();
1222 P != PEnd;
1223 ++P) {
Mike Stump11289f42009-09-09 15:08:12 +00001224 QualType ParamType
1225 = SubstType((*P)->getType(),
Douglas Gregor39cacdb2009-08-28 20:50:45 +00001226 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1227 (*P)->getLocation(), (*P)->getDeclName());
Douglas Gregor9b146582009-07-08 20:55:45 +00001228 if (ParamType.isNull() || Trap.hasErrorOccurred())
1229 return TDK_SubstitutionFailure;
Mike Stump11289f42009-09-09 15:08:12 +00001230
Douglas Gregor9b146582009-07-08 20:55:45 +00001231 ParamTypes.push_back(ParamType);
1232 }
1233
1234 // If the caller wants a full function type back, instantiate the return
1235 // type and form that function type.
1236 if (FunctionType) {
1237 // FIXME: exception-specifications?
Mike Stump11289f42009-09-09 15:08:12 +00001238 const FunctionProtoType *Proto
John McCall9dd450b2009-09-21 23:43:11 +00001239 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor9b146582009-07-08 20:55:45 +00001240 assert(Proto && "Function template does not have a prototype?");
Mike Stump11289f42009-09-09 15:08:12 +00001241
1242 QualType ResultType
Douglas Gregor39cacdb2009-08-28 20:50:45 +00001243 = SubstType(Proto->getResultType(),
1244 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1245 Function->getTypeSpecStartLoc(),
1246 Function->getDeclName());
Douglas Gregor9b146582009-07-08 20:55:45 +00001247 if (ResultType.isNull() || Trap.hasErrorOccurred())
1248 return TDK_SubstitutionFailure;
Mike Stump11289f42009-09-09 15:08:12 +00001249
1250 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor9b146582009-07-08 20:55:45 +00001251 ParamTypes.data(), ParamTypes.size(),
1252 Proto->isVariadic(),
1253 Proto->getTypeQuals(),
1254 Function->getLocation(),
Eli Friedmand8725a92010-08-05 02:54:05 +00001255 Function->getDeclName(),
1256 Proto->getExtInfo());
Douglas Gregor9b146582009-07-08 20:55:45 +00001257 if (FunctionType->isNull() || Trap.hasErrorOccurred())
1258 return TDK_SubstitutionFailure;
1259 }
Mike Stump11289f42009-09-09 15:08:12 +00001260
Douglas Gregor9b146582009-07-08 20:55:45 +00001261 // C++ [temp.arg.explicit]p2:
Mike Stump11289f42009-09-09 15:08:12 +00001262 // Trailing template arguments that can be deduced (14.8.2) may be
1263 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor9b146582009-07-08 20:55:45 +00001264 // template arguments can be deduced, they may all be omitted; in this
1265 // case, the empty template argument list <> itself may also be omitted.
1266 //
1267 // Take all of the explicitly-specified arguments and put them into the
Mike Stump11289f42009-09-09 15:08:12 +00001268 // set of deduced template arguments.
Douglas Gregor9b146582009-07-08 20:55:45 +00001269 Deduced.reserve(TemplateParams->size());
1270 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001271 Deduced.push_back(ExplicitArgumentList->get(I));
1272
Douglas Gregor9b146582009-07-08 20:55:45 +00001273 return TDK_Success;
1274}
1275
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001276/// \brief Allocate a TemplateArgumentLoc where all locations have
1277/// been initialized to the given location.
1278///
1279/// \param S The semantic analysis object.
1280///
1281/// \param The template argument we are producing template argument
1282/// location information for.
1283///
1284/// \param NTTPType For a declaration template argument, the type of
1285/// the non-type template parameter that corresponds to this template
1286/// argument.
1287///
1288/// \param Loc The source location to use for the resulting template
1289/// argument.
1290static TemplateArgumentLoc
1291getTrivialTemplateArgumentLoc(Sema &S,
1292 const TemplateArgument &Arg,
1293 QualType NTTPType,
1294 SourceLocation Loc) {
1295 switch (Arg.getKind()) {
1296 case TemplateArgument::Null:
1297 llvm_unreachable("Can't get a NULL template argument here");
1298 break;
1299
1300 case TemplateArgument::Type:
1301 return TemplateArgumentLoc(Arg,
1302 S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
1303
1304 case TemplateArgument::Declaration: {
1305 Expr *E
1306 = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
1307 .takeAs<Expr>();
1308 return TemplateArgumentLoc(TemplateArgument(E), E);
1309 }
1310
1311 case TemplateArgument::Integral: {
1312 Expr *E
1313 = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>();
1314 return TemplateArgumentLoc(TemplateArgument(E), E);
1315 }
1316
1317 case TemplateArgument::Template:
1318 return TemplateArgumentLoc(Arg, SourceRange(), Loc);
1319
1320 case TemplateArgument::Expression:
1321 return TemplateArgumentLoc(Arg, Arg.getAsExpr());
1322
1323 case TemplateArgument::Pack:
1324 llvm_unreachable("Template parameter packs are not yet supported");
1325 }
1326
1327 return TemplateArgumentLoc();
1328}
1329
Mike Stump11289f42009-09-09 15:08:12 +00001330/// \brief Finish template argument deduction for a function template,
Douglas Gregor9b146582009-07-08 20:55:45 +00001331/// checking the deduced template arguments for completeness and forming
1332/// the function template specialization.
Mike Stump11289f42009-09-09 15:08:12 +00001333Sema::TemplateDeductionResult
Douglas Gregor9b146582009-07-08 20:55:45 +00001334Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001335 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1336 unsigned NumExplicitlySpecified,
Douglas Gregor9b146582009-07-08 20:55:45 +00001337 FunctionDecl *&Specialization,
1338 TemplateDeductionInfo &Info) {
1339 TemplateParameterList *TemplateParams
1340 = FunctionTemplate->getTemplateParameters();
Mike Stump11289f42009-09-09 15:08:12 +00001341
Douglas Gregor9b146582009-07-08 20:55:45 +00001342 // Template argument deduction for function templates in a SFINAE context.
1343 // Trap any errors that might occur.
Mike Stump11289f42009-09-09 15:08:12 +00001344 SFINAETrap Trap(*this);
1345
Douglas Gregor9b146582009-07-08 20:55:45 +00001346 // Enter a new template instantiation context while we instantiate the
1347 // actual function declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001348 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor9b146582009-07-08 20:55:45 +00001349 FunctionTemplate, Deduced.data(), Deduced.size(),
1350 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
1351 if (Inst)
Mike Stump11289f42009-09-09 15:08:12 +00001352 return TDK_InstantiationDepth;
1353
John McCalle23b8712010-04-29 01:18:58 +00001354 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
John McCall80e58cd2010-04-29 00:35:03 +00001355
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001356 // C++ [temp.deduct.type]p2:
1357 // [...] or if any template argument remains neither deduced nor
1358 // explicitly specified, template argument deduction fails.
1359 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
1360 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001361 NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I);
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001362 if (!Deduced[I].isNull()) {
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001363 if (I < NumExplicitlySpecified ||
1364 Deduced[I].getKind() == TemplateArgument::Type) {
1365 // We have already fully type-checked and converted this
1366 // argument (because it was explicitly-specified) or no
1367 // additional checking is necessary (because it's a template
1368 // type parameter). Just record the presence of this
1369 // parameter.
1370 Builder.Append(Deduced[I]);
1371 continue;
1372 }
1373
1374 // We have deduced this argument, so it still needs to be
1375 // checked and converted.
1376
1377 // First, for a non-type template parameter type that is
1378 // initialized by a declaration, we need the type of the
1379 // corresponding non-type template parameter.
1380 QualType NTTPType;
1381 if (NonTypeTemplateParmDecl *NTTP
1382 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1383 if (Deduced[I].getKind() == TemplateArgument::Declaration) {
1384 NTTPType = NTTP->getType();
1385 if (NTTPType->isDependentType()) {
1386 TemplateArgumentList TemplateArgs(Context, Builder,
1387 /*TakeArgs=*/false);
1388 NTTPType = SubstType(NTTPType,
1389 MultiLevelTemplateArgumentList(TemplateArgs),
1390 NTTP->getLocation(),
1391 NTTP->getDeclName());
1392 if (NTTPType.isNull()) {
1393 Info.Param = makeTemplateParameter(Param);
Douglas Gregord09efd42010-05-08 20:07:26 +00001394 Info.reset(new (Context) TemplateArgumentList(Context, Builder,
1395 /*TakeArgs=*/true));
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001396 return TDK_SubstitutionFailure;
1397 }
1398 }
1399 }
1400 }
1401
1402 // Convert the deduced template argument into a template
1403 // argument that we can check, almost as if the user had written
1404 // the template argument explicitly.
1405 TemplateArgumentLoc Arg = getTrivialTemplateArgumentLoc(*this,
1406 Deduced[I],
1407 NTTPType,
1408 SourceLocation());
1409
1410 // Check the template argument, converting it as necessary.
1411 if (CheckTemplateArgument(Param, Arg,
1412 FunctionTemplate,
1413 FunctionTemplate->getLocation(),
1414 FunctionTemplate->getSourceRange().getEnd(),
1415 Builder,
1416 Deduced[I].wasDeducedFromArrayBound()
1417 ? CTAK_DeducedFromArrayBound
1418 : CTAK_Deduced)) {
1419 Info.Param = makeTemplateParameter(
1420 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregord09efd42010-05-08 20:07:26 +00001421 Info.reset(new (Context) TemplateArgumentList(Context, Builder,
1422 /*TakeArgs=*/true));
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001423 return TDK_SubstitutionFailure;
1424 }
1425
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001426 continue;
1427 }
1428
1429 // Substitute into the default template argument, if available.
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001430 TemplateArgumentLoc DefArg
1431 = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
1432 FunctionTemplate->getLocation(),
1433 FunctionTemplate->getSourceRange().getEnd(),
1434 Param,
1435 Builder);
1436
1437 // If there was no default argument, deduction is incomplete.
1438 if (DefArg.getArgument().isNull()) {
1439 Info.Param = makeTemplateParameter(
1440 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1441 return TDK_Incomplete;
1442 }
1443
1444 // Check whether we can actually use the default argument.
1445 if (CheckTemplateArgument(Param, DefArg,
1446 FunctionTemplate,
1447 FunctionTemplate->getLocation(),
1448 FunctionTemplate->getSourceRange().getEnd(),
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001449 Builder,
1450 CTAK_Deduced)) {
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001451 Info.Param = makeTemplateParameter(
1452 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregord09efd42010-05-08 20:07:26 +00001453 Info.reset(new (Context) TemplateArgumentList(Context, Builder,
1454 /*TakeArgs=*/true));
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001455 return TDK_SubstitutionFailure;
1456 }
1457
1458 // If we get here, we successfully used the default template argument.
1459 }
1460
1461 // Form the template argument list from the deduced template arguments.
1462 TemplateArgumentList *DeducedArgumentList
1463 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1464 Info.reset(DeducedArgumentList);
1465
Mike Stump11289f42009-09-09 15:08:12 +00001466 // Substitute the deduced template arguments into the function template
Douglas Gregor9b146582009-07-08 20:55:45 +00001467 // declaration to produce the function template specialization.
Douglas Gregor16142372010-04-28 04:52:24 +00001468 DeclContext *Owner = FunctionTemplate->getDeclContext();
1469 if (FunctionTemplate->getFriendObjectKind())
1470 Owner = FunctionTemplate->getLexicalDeclContext();
Douglas Gregor9b146582009-07-08 20:55:45 +00001471 Specialization = cast_or_null<FunctionDecl>(
Douglas Gregor16142372010-04-28 04:52:24 +00001472 SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner,
Douglas Gregor39cacdb2009-08-28 20:50:45 +00001473 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor9b146582009-07-08 20:55:45 +00001474 if (!Specialization)
1475 return TDK_SubstitutionFailure;
Mike Stump11289f42009-09-09 15:08:12 +00001476
Douglas Gregor31fae892009-09-15 18:26:13 +00001477 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
1478 FunctionTemplate->getCanonicalDecl());
1479
Mike Stump11289f42009-09-09 15:08:12 +00001480 // If the template argument list is owned by the function template
Douglas Gregor9b146582009-07-08 20:55:45 +00001481 // specialization, release it.
Douglas Gregord09efd42010-05-08 20:07:26 +00001482 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList &&
1483 !Trap.hasErrorOccurred())
Douglas Gregor9b146582009-07-08 20:55:45 +00001484 Info.take();
Mike Stump11289f42009-09-09 15:08:12 +00001485
Douglas Gregor9b146582009-07-08 20:55:45 +00001486 // There may have been an error that did not prevent us from constructing a
1487 // declaration. Mark the declaration invalid and return with a substitution
1488 // failure.
1489 if (Trap.hasErrorOccurred()) {
1490 Specialization->setInvalidDecl(true);
1491 return TDK_SubstitutionFailure;
1492 }
Mike Stump11289f42009-09-09 15:08:12 +00001493
1494 return TDK_Success;
Douglas Gregor9b146582009-07-08 20:55:45 +00001495}
1496
John McCall8d08b9b2010-08-27 09:08:28 +00001497/// Gets the type of a function for template-argument-deducton
1498/// purposes when it's considered as part of an overload set.
John McCallc1f69982010-02-02 02:21:27 +00001499static QualType GetTypeOfFunction(ASTContext &Context,
John McCall8d08b9b2010-08-27 09:08:28 +00001500 const OverloadExpr::FindResult &R,
John McCallc1f69982010-02-02 02:21:27 +00001501 FunctionDecl *Fn) {
John McCallc1f69982010-02-02 02:21:27 +00001502 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
John McCall8d08b9b2010-08-27 09:08:28 +00001503 if (Method->isInstance()) {
1504 // An instance method that's referenced in a form that doesn't
1505 // look like a member pointer is just invalid.
1506 if (!R.HasFormOfMemberPointer) return QualType();
1507
John McCallc1f69982010-02-02 02:21:27 +00001508 return Context.getMemberPointerType(Fn->getType(),
1509 Context.getTypeDeclType(Method->getParent()).getTypePtr());
John McCall8d08b9b2010-08-27 09:08:28 +00001510 }
1511
1512 if (!R.IsAddressOfOperand) return Fn->getType();
John McCallc1f69982010-02-02 02:21:27 +00001513 return Context.getPointerType(Fn->getType());
1514}
1515
1516/// Apply the deduction rules for overload sets.
1517///
1518/// \return the null type if this argument should be treated as an
1519/// undeduced context
1520static QualType
1521ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
1522 Expr *Arg, QualType ParamType) {
John McCall8d08b9b2010-08-27 09:08:28 +00001523
1524 OverloadExpr::FindResult R = OverloadExpr::find(Arg);
John McCallc1f69982010-02-02 02:21:27 +00001525
John McCall8d08b9b2010-08-27 09:08:28 +00001526 OverloadExpr *Ovl = R.Expression;
John McCallc1f69982010-02-02 02:21:27 +00001527
1528 // If there were explicit template arguments, we can only find
1529 // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
1530 // unambiguously name a full specialization.
John McCall1acbbb52010-02-02 06:20:04 +00001531 if (Ovl->hasExplicitTemplateArgs()) {
John McCallc1f69982010-02-02 02:21:27 +00001532 // But we can still look for an explicit specialization.
1533 if (FunctionDecl *ExplicitSpec
John McCall1acbbb52010-02-02 06:20:04 +00001534 = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
John McCall8d08b9b2010-08-27 09:08:28 +00001535 return GetTypeOfFunction(S.Context, R, ExplicitSpec);
John McCallc1f69982010-02-02 02:21:27 +00001536 return QualType();
1537 }
1538
1539 // C++0x [temp.deduct.call]p6:
1540 // When P is a function type, pointer to function type, or pointer
1541 // to member function type:
1542
1543 if (!ParamType->isFunctionType() &&
1544 !ParamType->isFunctionPointerType() &&
1545 !ParamType->isMemberFunctionPointerType())
1546 return QualType();
1547
1548 QualType Match;
John McCall1acbbb52010-02-02 06:20:04 +00001549 for (UnresolvedSetIterator I = Ovl->decls_begin(),
1550 E = Ovl->decls_end(); I != E; ++I) {
John McCallc1f69982010-02-02 02:21:27 +00001551 NamedDecl *D = (*I)->getUnderlyingDecl();
1552
1553 // - If the argument is an overload set containing one or more
1554 // function templates, the parameter is treated as a
1555 // non-deduced context.
1556 if (isa<FunctionTemplateDecl>(D))
1557 return QualType();
1558
1559 FunctionDecl *Fn = cast<FunctionDecl>(D);
John McCall8d08b9b2010-08-27 09:08:28 +00001560 QualType ArgType = GetTypeOfFunction(S.Context, R, Fn);
1561 if (ArgType.isNull()) continue;
John McCallc1f69982010-02-02 02:21:27 +00001562
1563 // - If the argument is an overload set (not containing function
1564 // templates), trial argument deduction is attempted using each
1565 // of the members of the set. If deduction succeeds for only one
1566 // of the overload set members, that member is used as the
1567 // argument value for the deduction. If deduction succeeds for
1568 // more than one member of the overload set the parameter is
1569 // treated as a non-deduced context.
1570
1571 // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
1572 // Type deduction is done independently for each P/A pair, and
1573 // the deduced template argument values are then combined.
1574 // So we do not reject deductions which were made elsewhere.
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001575 llvm::SmallVector<DeducedTemplateArgument, 8>
1576 Deduced(TemplateParams->size());
John McCall19c1bfd2010-08-25 05:32:35 +00001577 TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
John McCallc1f69982010-02-02 02:21:27 +00001578 unsigned TDF = 0;
1579
1580 Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +00001581 = DeduceTemplateArguments(S, TemplateParams,
John McCallc1f69982010-02-02 02:21:27 +00001582 ParamType, ArgType,
1583 Info, Deduced, TDF);
1584 if (Result) continue;
1585 if (!Match.isNull()) return QualType();
1586 Match = ArgType;
1587 }
1588
1589 return Match;
1590}
1591
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001592/// \brief Perform template argument deduction from a function call
1593/// (C++ [temp.deduct.call]).
1594///
1595/// \param FunctionTemplate the function template for which we are performing
1596/// template argument deduction.
1597///
Douglas Gregorea0a0a92010-01-11 18:40:55 +00001598/// \param ExplicitTemplateArguments the explicit template arguments provided
1599/// for this call.
Douglas Gregor89026b52009-06-30 23:57:56 +00001600///
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001601/// \param Args the function call arguments
1602///
1603/// \param NumArgs the number of arguments in Args
1604///
Douglas Gregorea0a0a92010-01-11 18:40:55 +00001605/// \param Name the name of the function being called. This is only significant
1606/// when the function template is a conversion function template, in which
1607/// case this routine will also perform template argument deduction based on
1608/// the function to which
1609///
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001610/// \param Specialization if template argument deduction was successful,
Mike Stump11289f42009-09-09 15:08:12 +00001611/// this will be set to the function template specialization produced by
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001612/// template argument deduction.
1613///
1614/// \param Info the argument will be updated to provide additional information
1615/// about template argument deduction.
1616///
1617/// \returns the result of template argument deduction.
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001618Sema::TemplateDeductionResult
1619Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregorea0a0a92010-01-11 18:40:55 +00001620 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001621 Expr **Args, unsigned NumArgs,
1622 FunctionDecl *&Specialization,
1623 TemplateDeductionInfo &Info) {
1624 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor89026b52009-06-30 23:57:56 +00001625
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001626 // C++ [temp.deduct.call]p1:
1627 // Template argument deduction is done by comparing each function template
1628 // parameter type (call it P) with the type of the corresponding argument
1629 // of the call (call it A) as described below.
1630 unsigned CheckArgs = NumArgs;
Douglas Gregor89026b52009-06-30 23:57:56 +00001631 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001632 return TDK_TooFewArguments;
1633 else if (NumArgs > Function->getNumParams()) {
Mike Stump11289f42009-09-09 15:08:12 +00001634 const FunctionProtoType *Proto
John McCall9dd450b2009-09-21 23:43:11 +00001635 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001636 if (!Proto->isVariadic())
1637 return TDK_TooManyArguments;
Mike Stump11289f42009-09-09 15:08:12 +00001638
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001639 CheckArgs = Function->getNumParams();
1640 }
Mike Stump11289f42009-09-09 15:08:12 +00001641
Douglas Gregor89026b52009-06-30 23:57:56 +00001642 // The types of the parameters from which we will perform template argument
1643 // deduction.
John McCall19c1bfd2010-08-25 05:32:35 +00001644 LocalInstantiationScope InstScope(*this);
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001645 TemplateParameterList *TemplateParams
1646 = FunctionTemplate->getTemplateParameters();
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001647 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor89026b52009-06-30 23:57:56 +00001648 llvm::SmallVector<QualType, 4> ParamTypes;
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001649 unsigned NumExplicitlySpecified = 0;
John McCall6b51f282009-11-23 01:53:49 +00001650 if (ExplicitTemplateArgs) {
Douglas Gregor9b146582009-07-08 20:55:45 +00001651 TemplateDeductionResult Result =
1652 SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001653 *ExplicitTemplateArgs,
Douglas Gregor9b146582009-07-08 20:55:45 +00001654 Deduced,
1655 ParamTypes,
1656 0,
1657 Info);
1658 if (Result)
1659 return Result;
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001660
1661 NumExplicitlySpecified = Deduced.size();
Douglas Gregor89026b52009-06-30 23:57:56 +00001662 } else {
1663 // Just fill in the parameter types from the function declaration.
1664 for (unsigned I = 0; I != CheckArgs; ++I)
1665 ParamTypes.push_back(Function->getParamDecl(I)->getType());
1666 }
Mike Stump11289f42009-09-09 15:08:12 +00001667
Douglas Gregor89026b52009-06-30 23:57:56 +00001668 // Deduce template arguments from the function parameters.
Mike Stump11289f42009-09-09 15:08:12 +00001669 Deduced.resize(TemplateParams->size());
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001670 for (unsigned I = 0; I != CheckArgs; ++I) {
Douglas Gregor89026b52009-06-30 23:57:56 +00001671 QualType ParamType = ParamTypes[I];
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001672 QualType ArgType = Args[I]->getType();
Mike Stump11289f42009-09-09 15:08:12 +00001673
John McCallc1f69982010-02-02 02:21:27 +00001674 // Overload sets usually make this parameter an undeduced
1675 // context, but there are sometimes special circumstances.
1676 if (ArgType == Context.OverloadTy) {
1677 ArgType = ResolveOverloadForDeduction(*this, TemplateParams,
1678 Args[I], ParamType);
1679 if (ArgType.isNull())
1680 continue;
1681 }
1682
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001683 // C++ [temp.deduct.call]p2:
1684 // If P is not a reference type:
1685 QualType CanonParamType = Context.getCanonicalType(ParamType);
Douglas Gregorcceb9752009-06-26 18:27:22 +00001686 bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1687 if (!ParamWasReference) {
Mike Stump11289f42009-09-09 15:08:12 +00001688 // - If A is an array type, the pointer type produced by the
1689 // array-to-pointer standard conversion (4.2) is used in place of
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001690 // A for type deduction; otherwise,
1691 if (ArgType->isArrayType())
1692 ArgType = Context.getArrayDecayedType(ArgType);
Mike Stump11289f42009-09-09 15:08:12 +00001693 // - If A is a function type, the pointer type produced by the
1694 // function-to-pointer standard conversion (4.3) is used in place
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001695 // of A for type deduction; otherwise,
1696 else if (ArgType->isFunctionType())
1697 ArgType = Context.getPointerType(ArgType);
1698 else {
1699 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1700 // type are ignored for type deduction.
1701 QualType CanonArgType = Context.getCanonicalType(ArgType);
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001702 if (CanonArgType.getLocalCVRQualifiers())
1703 ArgType = CanonArgType.getLocalUnqualifiedType();
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001704 }
1705 }
Mike Stump11289f42009-09-09 15:08:12 +00001706
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001707 // C++0x [temp.deduct.call]p3:
1708 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
Mike Stump11289f42009-09-09 15:08:12 +00001709 // are ignored for type deduction.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001710 if (CanonParamType.getLocalCVRQualifiers())
1711 ParamType = CanonParamType.getLocalUnqualifiedType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001712 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Mike Stump11289f42009-09-09 15:08:12 +00001713 // [...] If P is a reference type, the type referred to by P is used
1714 // for type deduction.
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001715 ParamType = ParamRefType->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +00001716
1717 // [...] If P is of the form T&&, where T is a template parameter, and
1718 // the argument is an lvalue, the type A& is used in place of A for
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001719 // type deduction.
1720 if (isa<RValueReferenceType>(ParamRefType) &&
John McCall9dd450b2009-09-21 23:43:11 +00001721 ParamRefType->getAs<TemplateTypeParmType>() &&
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001722 Args[I]->isLvalue(Context) == Expr::LV_Valid)
1723 ArgType = Context.getLValueReferenceType(ArgType);
1724 }
Mike Stump11289f42009-09-09 15:08:12 +00001725
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001726 // C++0x [temp.deduct.call]p4:
1727 // In general, the deduction process attempts to find template argument
1728 // values that will make the deduced A identical to A (after the type A
1729 // is transformed as described above). [...]
Douglas Gregor406f6342009-09-14 20:00:47 +00001730 unsigned TDF = TDF_SkipNonDependent;
Mike Stump11289f42009-09-09 15:08:12 +00001731
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001732 // - If the original P is a reference type, the deduced A (i.e., the
1733 // type referred to by the reference) can be more cv-qualified than
1734 // the transformed A.
1735 if (ParamWasReference)
1736 TDF |= TDF_ParamWithReferenceType;
Mike Stump11289f42009-09-09 15:08:12 +00001737 // - The transformed A can be another pointer or pointer to member
1738 // type that can be converted to the deduced A via a qualification
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001739 // conversion (4.4).
John McCallda518412010-08-05 05:30:45 +00001740 if (ArgType->isPointerType() || ArgType->isMemberPointerType() ||
1741 ArgType->isObjCObjectPointerType())
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001742 TDF |= TDF_IgnoreQualifiers;
Mike Stump11289f42009-09-09 15:08:12 +00001743 // - If P is a class and P has the form simple-template-id, then the
Douglas Gregorfc516c92009-06-26 23:27:24 +00001744 // transformed A can be a derived class of the deduced A. Likewise,
1745 // if P is a pointer to a class of the form simple-template-id, the
1746 // transformed A can be a pointer to a derived class pointed to by
1747 // the deduced A.
1748 if (isSimpleTemplateIdType(ParamType) ||
Mike Stump11289f42009-09-09 15:08:12 +00001749 (isa<PointerType>(ParamType) &&
Douglas Gregorfc516c92009-06-26 23:27:24 +00001750 isSimpleTemplateIdType(
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001751 ParamType->getAs<PointerType>()->getPointeeType())))
Douglas Gregorfc516c92009-06-26 23:27:24 +00001752 TDF |= TDF_DerivedClass;
Mike Stump11289f42009-09-09 15:08:12 +00001753
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001754 if (TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +00001755 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregorcceb9752009-06-26 18:27:22 +00001756 ParamType, ArgType, Info, Deduced,
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001757 TDF))
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001758 return Result;
Mike Stump11289f42009-09-09 15:08:12 +00001759
Douglas Gregor05155d82009-08-21 23:19:43 +00001760 // FIXME: we need to check that the deduced A is the same as A,
1761 // modulo the various allowed differences.
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001762 }
Douglas Gregor05155d82009-08-21 23:19:43 +00001763
Mike Stump11289f42009-09-09 15:08:12 +00001764 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001765 NumExplicitlySpecified,
Douglas Gregor9b146582009-07-08 20:55:45 +00001766 Specialization, Info);
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001767}
1768
Douglas Gregor9b146582009-07-08 20:55:45 +00001769/// \brief Deduce template arguments when taking the address of a function
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001770/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
1771/// a template.
Douglas Gregor9b146582009-07-08 20:55:45 +00001772///
1773/// \param FunctionTemplate the function template for which we are performing
1774/// template argument deduction.
1775///
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001776/// \param ExplicitTemplateArguments the explicitly-specified template
1777/// arguments.
Douglas Gregor9b146582009-07-08 20:55:45 +00001778///
1779/// \param ArgFunctionType the function type that will be used as the
1780/// "argument" type (A) when performing template argument deduction from the
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001781/// function template's function type. This type may be NULL, if there is no
1782/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
Douglas Gregor9b146582009-07-08 20:55:45 +00001783///
1784/// \param Specialization if template argument deduction was successful,
Mike Stump11289f42009-09-09 15:08:12 +00001785/// this will be set to the function template specialization produced by
Douglas Gregor9b146582009-07-08 20:55:45 +00001786/// template argument deduction.
1787///
1788/// \param Info the argument will be updated to provide additional information
1789/// about template argument deduction.
1790///
1791/// \returns the result of template argument deduction.
1792Sema::TemplateDeductionResult
1793Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001794 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor9b146582009-07-08 20:55:45 +00001795 QualType ArgFunctionType,
1796 FunctionDecl *&Specialization,
1797 TemplateDeductionInfo &Info) {
1798 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1799 TemplateParameterList *TemplateParams
1800 = FunctionTemplate->getTemplateParameters();
1801 QualType FunctionType = Function->getType();
Mike Stump11289f42009-09-09 15:08:12 +00001802
Douglas Gregor9b146582009-07-08 20:55:45 +00001803 // Substitute any explicit template arguments.
John McCall19c1bfd2010-08-25 05:32:35 +00001804 LocalInstantiationScope InstScope(*this);
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001805 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
1806 unsigned NumExplicitlySpecified = 0;
Douglas Gregor9b146582009-07-08 20:55:45 +00001807 llvm::SmallVector<QualType, 4> ParamTypes;
John McCall6b51f282009-11-23 01:53:49 +00001808 if (ExplicitTemplateArgs) {
Mike Stump11289f42009-09-09 15:08:12 +00001809 if (TemplateDeductionResult Result
1810 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001811 *ExplicitTemplateArgs,
Mike Stump11289f42009-09-09 15:08:12 +00001812 Deduced, ParamTypes,
Douglas Gregor9b146582009-07-08 20:55:45 +00001813 &FunctionType, Info))
1814 return Result;
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001815
1816 NumExplicitlySpecified = Deduced.size();
Douglas Gregor9b146582009-07-08 20:55:45 +00001817 }
1818
1819 // Template argument deduction for function templates in a SFINAE context.
1820 // Trap any errors that might occur.
Mike Stump11289f42009-09-09 15:08:12 +00001821 SFINAETrap Trap(*this);
1822
John McCallc1f69982010-02-02 02:21:27 +00001823 Deduced.resize(TemplateParams->size());
1824
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001825 if (!ArgFunctionType.isNull()) {
1826 // Deduce template arguments from the function type.
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001827 if (TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +00001828 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001829 FunctionType, ArgFunctionType, Info,
1830 Deduced, 0))
1831 return Result;
1832 }
1833
Mike Stump11289f42009-09-09 15:08:12 +00001834 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001835 NumExplicitlySpecified,
Douglas Gregor9b146582009-07-08 20:55:45 +00001836 Specialization, Info);
1837}
1838
Douglas Gregor05155d82009-08-21 23:19:43 +00001839/// \brief Deduce template arguments for a templated conversion
1840/// function (C++ [temp.deduct.conv]) and, if successful, produce a
1841/// conversion function template specialization.
1842Sema::TemplateDeductionResult
1843Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1844 QualType ToType,
1845 CXXConversionDecl *&Specialization,
1846 TemplateDeductionInfo &Info) {
Mike Stump11289f42009-09-09 15:08:12 +00001847 CXXConversionDecl *Conv
Douglas Gregor05155d82009-08-21 23:19:43 +00001848 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
1849 QualType FromType = Conv->getConversionType();
1850
1851 // Canonicalize the types for deduction.
1852 QualType P = Context.getCanonicalType(FromType);
1853 QualType A = Context.getCanonicalType(ToType);
1854
1855 // C++0x [temp.deduct.conv]p3:
1856 // If P is a reference type, the type referred to by P is used for
1857 // type deduction.
1858 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
1859 P = PRef->getPointeeType();
1860
1861 // C++0x [temp.deduct.conv]p3:
1862 // If A is a reference type, the type referred to by A is used
1863 // for type deduction.
1864 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
1865 A = ARef->getPointeeType();
1866 // C++ [temp.deduct.conv]p2:
1867 //
Mike Stump11289f42009-09-09 15:08:12 +00001868 // If A is not a reference type:
Douglas Gregor05155d82009-08-21 23:19:43 +00001869 else {
1870 assert(!A->isReferenceType() && "Reference types were handled above");
1871
1872 // - If P is an array type, the pointer type produced by the
Mike Stump11289f42009-09-09 15:08:12 +00001873 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor05155d82009-08-21 23:19:43 +00001874 // of P for type deduction; otherwise,
1875 if (P->isArrayType())
1876 P = Context.getArrayDecayedType(P);
1877 // - If P is a function type, the pointer type produced by the
1878 // function-to-pointer standard conversion (4.3) is used in
1879 // place of P for type deduction; otherwise,
1880 else if (P->isFunctionType())
1881 P = Context.getPointerType(P);
1882 // - If P is a cv-qualified type, the top level cv-qualifiers of
1883 // P’s type are ignored for type deduction.
1884 else
1885 P = P.getUnqualifiedType();
1886
1887 // C++0x [temp.deduct.conv]p3:
1888 // If A is a cv-qualified type, the top level cv-qualifiers of A’s
1889 // type are ignored for type deduction.
1890 A = A.getUnqualifiedType();
1891 }
1892
1893 // Template argument deduction for function templates in a SFINAE context.
1894 // Trap any errors that might occur.
Mike Stump11289f42009-09-09 15:08:12 +00001895 SFINAETrap Trap(*this);
Douglas Gregor05155d82009-08-21 23:19:43 +00001896
1897 // C++ [temp.deduct.conv]p1:
1898 // Template argument deduction is done by comparing the return
1899 // type of the template conversion function (call it P) with the
1900 // type that is required as the result of the conversion (call it
1901 // A) as described in 14.8.2.4.
1902 TemplateParameterList *TemplateParams
1903 = FunctionTemplate->getTemplateParameters();
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001904 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Mike Stump11289f42009-09-09 15:08:12 +00001905 Deduced.resize(TemplateParams->size());
Douglas Gregor05155d82009-08-21 23:19:43 +00001906
1907 // C++0x [temp.deduct.conv]p4:
1908 // In general, the deduction process attempts to find template
1909 // argument values that will make the deduced A identical to
1910 // A. However, there are two cases that allow a difference:
1911 unsigned TDF = 0;
1912 // - If the original A is a reference type, A can be more
1913 // cv-qualified than the deduced A (i.e., the type referred to
1914 // by the reference)
1915 if (ToType->isReferenceType())
1916 TDF |= TDF_ParamWithReferenceType;
1917 // - The deduced A can be another pointer or pointer to member
1918 // type that can be converted to A via a qualification
1919 // conversion.
1920 //
1921 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
1922 // both P and A are pointers or member pointers. In this case, we
1923 // just ignore cv-qualifiers completely).
1924 if ((P->isPointerType() && A->isPointerType()) ||
1925 (P->isMemberPointerType() && P->isMemberPointerType()))
1926 TDF |= TDF_IgnoreQualifiers;
1927 if (TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +00001928 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor05155d82009-08-21 23:19:43 +00001929 P, A, Info, Deduced, TDF))
1930 return Result;
1931
1932 // FIXME: we need to check that the deduced A is the same as A,
1933 // modulo the various allowed differences.
Mike Stump11289f42009-09-09 15:08:12 +00001934
Douglas Gregor05155d82009-08-21 23:19:43 +00001935 // Finish template argument deduction.
John McCall19c1bfd2010-08-25 05:32:35 +00001936 LocalInstantiationScope InstScope(*this);
Douglas Gregor05155d82009-08-21 23:19:43 +00001937 FunctionDecl *Spec = 0;
1938 TemplateDeductionResult Result
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001939 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec,
1940 Info);
Douglas Gregor05155d82009-08-21 23:19:43 +00001941 Specialization = cast_or_null<CXXConversionDecl>(Spec);
1942 return Result;
1943}
1944
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001945/// \brief Deduce template arguments for a function template when there is
1946/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
1947///
1948/// \param FunctionTemplate the function template for which we are performing
1949/// template argument deduction.
1950///
1951/// \param ExplicitTemplateArguments the explicitly-specified template
1952/// arguments.
1953///
1954/// \param Specialization if template argument deduction was successful,
1955/// this will be set to the function template specialization produced by
1956/// template argument deduction.
1957///
1958/// \param Info the argument will be updated to provide additional information
1959/// about template argument deduction.
1960///
1961/// \returns the result of template argument deduction.
1962Sema::TemplateDeductionResult
1963Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1964 const TemplateArgumentListInfo *ExplicitTemplateArgs,
1965 FunctionDecl *&Specialization,
1966 TemplateDeductionInfo &Info) {
1967 return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
1968 QualType(), Specialization, Info);
1969}
1970
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001971/// \brief Stores the result of comparing the qualifiers of two types.
1972enum DeductionQualifierComparison {
1973 NeitherMoreQualified = 0,
1974 ParamMoreQualified,
1975 ArgMoreQualified
1976};
1977
1978/// \brief Deduce the template arguments during partial ordering by comparing
1979/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
1980///
Chandler Carruthc1263112010-02-07 21:33:28 +00001981/// \param S the semantic analysis object within which we are deducing
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001982///
1983/// \param TemplateParams the template parameters that we are deducing
1984///
1985/// \param ParamIn the parameter type
1986///
1987/// \param ArgIn the argument type
1988///
1989/// \param Info information about the template argument deduction itself
1990///
1991/// \param Deduced the deduced template arguments
1992///
1993/// \returns the result of template argument deduction so far. Note that a
1994/// "success" result means that template argument deduction has not yet failed,
1995/// but it may still fail, later, for other reasons.
1996static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +00001997DeduceTemplateArgumentsDuringPartialOrdering(Sema &S,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001998 TemplateParameterList *TemplateParams,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001999 QualType ParamIn, QualType ArgIn,
John McCall19c1bfd2010-08-25 05:32:35 +00002000 TemplateDeductionInfo &Info,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00002001 llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
2002 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
Chandler Carruthc1263112010-02-07 21:33:28 +00002003 CanQualType Param = S.Context.getCanonicalType(ParamIn);
2004 CanQualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002005
2006 // C++0x [temp.deduct.partial]p5:
2007 // Before the partial ordering is done, certain transformations are
2008 // performed on the types used for partial ordering:
2009 // - If P is a reference type, P is replaced by the type referred to.
2010 CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
John McCall48f2d582009-10-23 23:03:21 +00002011 if (!ParamRef.isNull())
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002012 Param = ParamRef->getPointeeType();
2013
2014 // - If A is a reference type, A is replaced by the type referred to.
2015 CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
John McCall48f2d582009-10-23 23:03:21 +00002016 if (!ArgRef.isNull())
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002017 Arg = ArgRef->getPointeeType();
2018
John McCall48f2d582009-10-23 23:03:21 +00002019 if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002020 // C++0x [temp.deduct.partial]p6:
2021 // If both P and A were reference types (before being replaced with the
2022 // type referred to above), determine which of the two types (if any) is
2023 // more cv-qualified than the other; otherwise the types are considered to
2024 // be equally cv-qualified for partial ordering purposes. The result of this
2025 // determination will be used below.
2026 //
2027 // We save this information for later, using it only when deduction
2028 // succeeds in both directions.
2029 DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
2030 if (Param.isMoreQualifiedThan(Arg))
2031 QualifierResult = ParamMoreQualified;
2032 else if (Arg.isMoreQualifiedThan(Param))
2033 QualifierResult = ArgMoreQualified;
2034 QualifierComparisons->push_back(QualifierResult);
2035 }
2036
2037 // C++0x [temp.deduct.partial]p7:
2038 // Remove any top-level cv-qualifiers:
2039 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
2040 // version of P.
2041 Param = Param.getUnqualifiedType();
2042 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
2043 // version of A.
2044 Arg = Arg.getUnqualifiedType();
2045
2046 // C++0x [temp.deduct.partial]p8:
2047 // Using the resulting types P and A the deduction is then done as
2048 // described in 14.9.2.5. If deduction succeeds for a given type, the type
2049 // from the argument template is considered to be at least as specialized
2050 // as the type from the parameter template.
Chandler Carruthc1263112010-02-07 21:33:28 +00002051 return DeduceTemplateArguments(S, TemplateParams, Param, Arg, Info,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002052 Deduced, TDF_None);
2053}
2054
2055static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002056MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2057 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002058 unsigned Level,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002059 llvm::SmallVectorImpl<bool> &Deduced);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002060
2061/// \brief Determine whether the function template \p FT1 is at least as
2062/// specialized as \p FT2.
2063static bool isAtLeastAsSpecializedAs(Sema &S,
John McCallbc077cf2010-02-08 23:07:23 +00002064 SourceLocation Loc,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002065 FunctionTemplateDecl *FT1,
2066 FunctionTemplateDecl *FT2,
2067 TemplatePartialOrderingContext TPOC,
2068 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
2069 FunctionDecl *FD1 = FT1->getTemplatedDecl();
2070 FunctionDecl *FD2 = FT2->getTemplatedDecl();
2071 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
2072 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
2073
2074 assert(Proto1 && Proto2 && "Function templates must have prototypes");
2075 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00002076 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002077 Deduced.resize(TemplateParams->size());
2078
2079 // C++0x [temp.deduct.partial]p3:
2080 // The types used to determine the ordering depend on the context in which
2081 // the partial ordering is done:
John McCall19c1bfd2010-08-25 05:32:35 +00002082 TemplateDeductionInfo Info(S.Context, Loc);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002083 switch (TPOC) {
2084 case TPOC_Call: {
2085 // - In the context of a function call, the function parameter types are
2086 // used.
2087 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
2088 for (unsigned I = 0; I != NumParams; ++I)
Chandler Carruthc1263112010-02-07 21:33:28 +00002089 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002090 TemplateParams,
2091 Proto2->getArgType(I),
2092 Proto1->getArgType(I),
2093 Info,
2094 Deduced,
2095 QualifierComparisons))
2096 return false;
2097
2098 break;
2099 }
2100
2101 case TPOC_Conversion:
2102 // - In the context of a call to a conversion operator, the return types
2103 // of the conversion function templates are used.
Chandler Carruthc1263112010-02-07 21:33:28 +00002104 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002105 TemplateParams,
2106 Proto2->getResultType(),
2107 Proto1->getResultType(),
2108 Info,
2109 Deduced,
2110 QualifierComparisons))
2111 return false;
2112 break;
2113
2114 case TPOC_Other:
2115 // - In other contexts (14.6.6.2) the function template’s function type
2116 // is used.
Chandler Carruthc1263112010-02-07 21:33:28 +00002117 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002118 TemplateParams,
2119 FD2->getType(),
2120 FD1->getType(),
2121 Info,
2122 Deduced,
2123 QualifierComparisons))
2124 return false;
2125 break;
2126 }
2127
2128 // C++0x [temp.deduct.partial]p11:
2129 // In most cases, all template parameters must have values in order for
2130 // deduction to succeed, but for partial ordering purposes a template
2131 // parameter may remain without a value provided it is not used in the
2132 // types being used for partial ordering. [ Note: a template parameter used
2133 // in a non-deduced context is considered used. -end note]
2134 unsigned ArgIdx = 0, NumArgs = Deduced.size();
2135 for (; ArgIdx != NumArgs; ++ArgIdx)
2136 if (Deduced[ArgIdx].isNull())
2137 break;
2138
2139 if (ArgIdx == NumArgs) {
2140 // All template arguments were deduced. FT1 is at least as specialized
2141 // as FT2.
2142 return true;
2143 }
2144
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002145 // Figure out which template parameters were used.
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002146 llvm::SmallVector<bool, 4> UsedParameters;
2147 UsedParameters.resize(TemplateParams->size());
2148 switch (TPOC) {
2149 case TPOC_Call: {
2150 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
2151 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor21610382009-10-29 00:04:11 +00002152 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
2153 TemplateParams->getDepth(),
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002154 UsedParameters);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002155 break;
2156 }
2157
2158 case TPOC_Conversion:
Douglas Gregor21610382009-10-29 00:04:11 +00002159 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
2160 TemplateParams->getDepth(),
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002161 UsedParameters);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002162 break;
2163
2164 case TPOC_Other:
Douglas Gregor21610382009-10-29 00:04:11 +00002165 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
2166 TemplateParams->getDepth(),
2167 UsedParameters);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002168 break;
2169 }
2170
2171 for (; ArgIdx != NumArgs; ++ArgIdx)
2172 // If this argument had no value deduced but was used in one of the types
2173 // used for partial ordering, then deduction fails.
2174 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
2175 return false;
2176
2177 return true;
2178}
2179
2180
Douglas Gregorbe999392009-09-15 16:23:51 +00002181/// \brief Returns the more specialized function template according
Douglas Gregor05155d82009-08-21 23:19:43 +00002182/// to the rules of function template partial ordering (C++ [temp.func.order]).
2183///
2184/// \param FT1 the first function template
2185///
2186/// \param FT2 the second function template
2187///
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002188/// \param TPOC the context in which we are performing partial ordering of
2189/// function templates.
Mike Stump11289f42009-09-09 15:08:12 +00002190///
Douglas Gregorbe999392009-09-15 16:23:51 +00002191/// \returns the more specialized function template. If neither
Douglas Gregor05155d82009-08-21 23:19:43 +00002192/// template is more specialized, returns NULL.
2193FunctionTemplateDecl *
2194Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
2195 FunctionTemplateDecl *FT2,
John McCallbc077cf2010-02-08 23:07:23 +00002196 SourceLocation Loc,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002197 TemplatePartialOrderingContext TPOC) {
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002198 llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
John McCallbc077cf2010-02-08 23:07:23 +00002199 bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, 0);
2200 bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002201 &QualifierComparisons);
2202
2203 if (Better1 != Better2) // We have a clear winner
2204 return Better1? FT1 : FT2;
2205
2206 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor05155d82009-08-21 23:19:43 +00002207 return 0;
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002208
2209
2210 // C++0x [temp.deduct.partial]p10:
2211 // If for each type being considered a given template is at least as
2212 // specialized for all types and more specialized for some set of types and
2213 // the other template is not more specialized for any types or is not at
2214 // least as specialized for any types, then the given template is more
2215 // specialized than the other template. Otherwise, neither template is more
2216 // specialized than the other.
2217 Better1 = false;
2218 Better2 = false;
2219 for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
2220 // C++0x [temp.deduct.partial]p9:
2221 // If, for a given type, deduction succeeds in both directions (i.e., the
2222 // types are identical after the transformations above) and if the type
2223 // from the argument template is more cv-qualified than the type from the
2224 // parameter template (as described above) that type is considered to be
2225 // more specialized than the other. If neither type is more cv-qualified
2226 // than the other then neither type is more specialized than the other.
2227 switch (QualifierComparisons[I]) {
2228 case NeitherMoreQualified:
2229 break;
2230
2231 case ParamMoreQualified:
2232 Better1 = true;
2233 if (Better2)
2234 return 0;
2235 break;
2236
2237 case ArgMoreQualified:
2238 Better2 = true;
2239 if (Better1)
2240 return 0;
2241 break;
2242 }
2243 }
2244
2245 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor05155d82009-08-21 23:19:43 +00002246 if (Better1)
2247 return FT1;
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002248 else if (Better2)
2249 return FT2;
2250 else
2251 return 0;
Douglas Gregor05155d82009-08-21 23:19:43 +00002252}
Douglas Gregor9b146582009-07-08 20:55:45 +00002253
Douglas Gregor450f00842009-09-25 18:43:00 +00002254/// \brief Determine if the two templates are equivalent.
2255static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
2256 if (T1 == T2)
2257 return true;
2258
2259 if (!T1 || !T2)
2260 return false;
2261
2262 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
2263}
2264
2265/// \brief Retrieve the most specialized of the given function template
2266/// specializations.
2267///
John McCall58cc69d2010-01-27 01:50:18 +00002268/// \param SpecBegin the start iterator of the function template
2269/// specializations that we will be comparing.
Douglas Gregor450f00842009-09-25 18:43:00 +00002270///
John McCall58cc69d2010-01-27 01:50:18 +00002271/// \param SpecEnd the end iterator of the function template
2272/// specializations, paired with \p SpecBegin.
Douglas Gregor450f00842009-09-25 18:43:00 +00002273///
2274/// \param TPOC the partial ordering context to use to compare the function
2275/// template specializations.
2276///
2277/// \param Loc the location where the ambiguity or no-specializations
2278/// diagnostic should occur.
2279///
2280/// \param NoneDiag partial diagnostic used to diagnose cases where there are
2281/// no matching candidates.
2282///
2283/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
2284/// occurs.
2285///
2286/// \param CandidateDiag partial diagnostic used for each function template
2287/// specialization that is a candidate in the ambiguous ordering. One parameter
2288/// in this diagnostic should be unbound, which will correspond to the string
2289/// describing the template arguments for the function template specialization.
2290///
2291/// \param Index if non-NULL and the result of this function is non-nULL,
2292/// receives the index corresponding to the resulting function template
2293/// specialization.
2294///
2295/// \returns the most specialized function template specialization, if
John McCall58cc69d2010-01-27 01:50:18 +00002296/// found. Otherwise, returns SpecEnd.
Douglas Gregor450f00842009-09-25 18:43:00 +00002297///
2298/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
2299/// template argument deduction.
John McCall58cc69d2010-01-27 01:50:18 +00002300UnresolvedSetIterator
2301Sema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
2302 UnresolvedSetIterator SpecEnd,
2303 TemplatePartialOrderingContext TPOC,
2304 SourceLocation Loc,
2305 const PartialDiagnostic &NoneDiag,
2306 const PartialDiagnostic &AmbigDiag,
2307 const PartialDiagnostic &CandidateDiag) {
2308 if (SpecBegin == SpecEnd) {
Douglas Gregor450f00842009-09-25 18:43:00 +00002309 Diag(Loc, NoneDiag);
John McCall58cc69d2010-01-27 01:50:18 +00002310 return SpecEnd;
Douglas Gregor450f00842009-09-25 18:43:00 +00002311 }
2312
John McCall58cc69d2010-01-27 01:50:18 +00002313 if (SpecBegin + 1 == SpecEnd)
2314 return SpecBegin;
Douglas Gregor450f00842009-09-25 18:43:00 +00002315
2316 // Find the function template that is better than all of the templates it
2317 // has been compared to.
John McCall58cc69d2010-01-27 01:50:18 +00002318 UnresolvedSetIterator Best = SpecBegin;
Douglas Gregor450f00842009-09-25 18:43:00 +00002319 FunctionTemplateDecl *BestTemplate
John McCall58cc69d2010-01-27 01:50:18 +00002320 = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
Douglas Gregor450f00842009-09-25 18:43:00 +00002321 assert(BestTemplate && "Not a function template specialization?");
John McCall58cc69d2010-01-27 01:50:18 +00002322 for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
2323 FunctionTemplateDecl *Challenger
2324 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregor450f00842009-09-25 18:43:00 +00002325 assert(Challenger && "Not a function template specialization?");
John McCall58cc69d2010-01-27 01:50:18 +00002326 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCallbc077cf2010-02-08 23:07:23 +00002327 Loc, TPOC),
Douglas Gregor450f00842009-09-25 18:43:00 +00002328 Challenger)) {
2329 Best = I;
2330 BestTemplate = Challenger;
2331 }
2332 }
2333
2334 // Make sure that the "best" function template is more specialized than all
2335 // of the others.
2336 bool Ambiguous = false;
John McCall58cc69d2010-01-27 01:50:18 +00002337 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
2338 FunctionTemplateDecl *Challenger
2339 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregor450f00842009-09-25 18:43:00 +00002340 if (I != Best &&
2341 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCallbc077cf2010-02-08 23:07:23 +00002342 Loc, TPOC),
Douglas Gregor450f00842009-09-25 18:43:00 +00002343 BestTemplate)) {
2344 Ambiguous = true;
2345 break;
2346 }
2347 }
2348
2349 if (!Ambiguous) {
2350 // We found an answer. Return it.
John McCall58cc69d2010-01-27 01:50:18 +00002351 return Best;
Douglas Gregor450f00842009-09-25 18:43:00 +00002352 }
2353
2354 // Diagnose the ambiguity.
2355 Diag(Loc, AmbigDiag);
2356
2357 // FIXME: Can we order the candidates in some sane way?
John McCall58cc69d2010-01-27 01:50:18 +00002358 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
2359 Diag((*I)->getLocation(), CandidateDiag)
Douglas Gregor450f00842009-09-25 18:43:00 +00002360 << getTemplateArgumentBindingsText(
John McCall58cc69d2010-01-27 01:50:18 +00002361 cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
2362 *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
Douglas Gregor450f00842009-09-25 18:43:00 +00002363
John McCall58cc69d2010-01-27 01:50:18 +00002364 return SpecEnd;
Douglas Gregor450f00842009-09-25 18:43:00 +00002365}
2366
Douglas Gregorbe999392009-09-15 16:23:51 +00002367/// \brief Returns the more specialized class template partial specialization
2368/// according to the rules of partial ordering of class template partial
2369/// specializations (C++ [temp.class.order]).
2370///
2371/// \param PS1 the first class template partial specialization
2372///
2373/// \param PS2 the second class template partial specialization
2374///
2375/// \returns the more specialized class template partial specialization. If
2376/// neither partial specialization is more specialized, returns NULL.
2377ClassTemplatePartialSpecializationDecl *
2378Sema::getMoreSpecializedPartialSpecialization(
2379 ClassTemplatePartialSpecializationDecl *PS1,
John McCallbc077cf2010-02-08 23:07:23 +00002380 ClassTemplatePartialSpecializationDecl *PS2,
2381 SourceLocation Loc) {
Douglas Gregorbe999392009-09-15 16:23:51 +00002382 // C++ [temp.class.order]p1:
2383 // For two class template partial specializations, the first is at least as
2384 // specialized as the second if, given the following rewrite to two
2385 // function templates, the first function template is at least as
2386 // specialized as the second according to the ordering rules for function
2387 // templates (14.6.6.2):
2388 // - the first function template has the same template parameters as the
2389 // first partial specialization and has a single function parameter
2390 // whose type is a class template specialization with the template
2391 // arguments of the first partial specialization, and
2392 // - the second function template has the same template parameters as the
2393 // second partial specialization and has a single function parameter
2394 // whose type is a class template specialization with the template
2395 // arguments of the second partial specialization.
2396 //
Douglas Gregor684268d2010-04-29 06:21:43 +00002397 // Rather than synthesize function templates, we merely perform the
2398 // equivalent partial ordering by performing deduction directly on
2399 // the template arguments of the class template partial
2400 // specializations. This computation is slightly simpler than the
2401 // general problem of function template partial ordering, because
2402 // class template partial specializations are more constrained. We
2403 // know that every template parameter is deducible from the class
2404 // template partial specialization's template arguments, for
2405 // example.
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00002406 llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
John McCall19c1bfd2010-08-25 05:32:35 +00002407 TemplateDeductionInfo Info(Context, Loc);
John McCall2408e322010-04-27 00:57:59 +00002408
2409 QualType PT1 = PS1->getInjectedSpecializationType();
2410 QualType PT2 = PS2->getInjectedSpecializationType();
Douglas Gregorbe999392009-09-15 16:23:51 +00002411
2412 // Determine whether PS1 is at least as specialized as PS2
2413 Deduced.resize(PS2->getTemplateParameters()->size());
Chandler Carruthc1263112010-02-07 21:33:28 +00002414 bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbe999392009-09-15 16:23:51 +00002415 PS2->getTemplateParameters(),
John McCall2408e322010-04-27 00:57:59 +00002416 PT2,
2417 PT1,
Douglas Gregorbe999392009-09-15 16:23:51 +00002418 Info,
2419 Deduced,
2420 0);
Douglas Gregor9225b022010-04-29 06:31:36 +00002421 if (Better1)
2422 Better1 = !::FinishTemplateArgumentDeduction(*this, PS2,
2423 PS1->getTemplateArgs(),
2424 Deduced, Info);
2425
Douglas Gregorbe999392009-09-15 16:23:51 +00002426 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregoradee3e32009-11-11 23:06:43 +00002427 Deduced.clear();
Douglas Gregorbe999392009-09-15 16:23:51 +00002428 Deduced.resize(PS1->getTemplateParameters()->size());
Chandler Carruthc1263112010-02-07 21:33:28 +00002429 bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbe999392009-09-15 16:23:51 +00002430 PS1->getTemplateParameters(),
John McCall2408e322010-04-27 00:57:59 +00002431 PT1,
2432 PT2,
Douglas Gregorbe999392009-09-15 16:23:51 +00002433 Info,
2434 Deduced,
2435 0);
Douglas Gregor9225b022010-04-29 06:31:36 +00002436 if (Better2)
2437 Better2 = !::FinishTemplateArgumentDeduction(*this, PS1,
2438 PS2->getTemplateArgs(),
2439 Deduced, Info);
Douglas Gregorbe999392009-09-15 16:23:51 +00002440
2441 if (Better1 == Better2)
2442 return 0;
2443
2444 return Better1? PS1 : PS2;
2445}
2446
Mike Stump11289f42009-09-09 15:08:12 +00002447static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002448MarkUsedTemplateParameters(Sema &SemaRef,
2449 const TemplateArgument &TemplateArg,
2450 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002451 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002452 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002453
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002454/// \brief Mark the template parameters that are used by the given
Douglas Gregor91772d12009-06-13 00:26:55 +00002455/// expression.
Mike Stump11289f42009-09-09 15:08:12 +00002456static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002457MarkUsedTemplateParameters(Sema &SemaRef,
2458 const Expr *E,
2459 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002460 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002461 llvm::SmallVectorImpl<bool> &Used) {
2462 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2463 // find other occurrences of template parameters.
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00002464 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregor04b11522010-01-14 18:13:22 +00002465 if (!DRE)
Douglas Gregor91772d12009-06-13 00:26:55 +00002466 return;
2467
Mike Stump11289f42009-09-09 15:08:12 +00002468 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor91772d12009-06-13 00:26:55 +00002469 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2470 if (!NTTP)
2471 return;
2472
Douglas Gregor21610382009-10-29 00:04:11 +00002473 if (NTTP->getDepth() == Depth)
2474 Used[NTTP->getIndex()] = true;
Douglas Gregor91772d12009-06-13 00:26:55 +00002475}
2476
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002477/// \brief Mark the template parameters that are used by the given
2478/// nested name specifier.
2479static void
2480MarkUsedTemplateParameters(Sema &SemaRef,
2481 NestedNameSpecifier *NNS,
2482 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002483 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002484 llvm::SmallVectorImpl<bool> &Used) {
2485 if (!NNS)
2486 return;
2487
Douglas Gregor21610382009-10-29 00:04:11 +00002488 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2489 Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002490 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregor21610382009-10-29 00:04:11 +00002491 OnlyDeduced, Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002492}
2493
2494/// \brief Mark the template parameters that are used by the given
2495/// template name.
2496static void
2497MarkUsedTemplateParameters(Sema &SemaRef,
2498 TemplateName Name,
2499 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002500 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002501 llvm::SmallVectorImpl<bool> &Used) {
2502 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2503 if (TemplateTemplateParmDecl *TTP
Douglas Gregor21610382009-10-29 00:04:11 +00002504 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2505 if (TTP->getDepth() == Depth)
2506 Used[TTP->getIndex()] = true;
2507 }
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002508 return;
2509 }
2510
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002511 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2512 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2513 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002514 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
Douglas Gregor21610382009-10-29 00:04:11 +00002515 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2516 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002517}
2518
2519/// \brief Mark the template parameters that are used by the given
Douglas Gregor91772d12009-06-13 00:26:55 +00002520/// type.
Mike Stump11289f42009-09-09 15:08:12 +00002521static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002522MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2523 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002524 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002525 llvm::SmallVectorImpl<bool> &Used) {
2526 if (T.isNull())
2527 return;
2528
Douglas Gregor91772d12009-06-13 00:26:55 +00002529 // Non-dependent types have nothing deducible
2530 if (!T->isDependentType())
2531 return;
2532
2533 T = SemaRef.Context.getCanonicalType(T);
2534 switch (T->getTypeClass()) {
Douglas Gregor91772d12009-06-13 00:26:55 +00002535 case Type::Pointer:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002536 MarkUsedTemplateParameters(SemaRef,
2537 cast<PointerType>(T)->getPointeeType(),
2538 OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002539 Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002540 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002541 break;
2542
2543 case Type::BlockPointer:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002544 MarkUsedTemplateParameters(SemaRef,
2545 cast<BlockPointerType>(T)->getPointeeType(),
2546 OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002547 Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002548 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002549 break;
2550
2551 case Type::LValueReference:
2552 case Type::RValueReference:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002553 MarkUsedTemplateParameters(SemaRef,
2554 cast<ReferenceType>(T)->getPointeeType(),
2555 OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002556 Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002557 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002558 break;
2559
2560 case Type::MemberPointer: {
2561 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002562 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002563 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002564 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregor21610382009-10-29 00:04:11 +00002565 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002566 break;
2567 }
2568
2569 case Type::DependentSizedArray:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002570 MarkUsedTemplateParameters(SemaRef,
2571 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregor21610382009-10-29 00:04:11 +00002572 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002573 // Fall through to check the element type
2574
2575 case Type::ConstantArray:
2576 case Type::IncompleteArray:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002577 MarkUsedTemplateParameters(SemaRef,
2578 cast<ArrayType>(T)->getElementType(),
Douglas Gregor21610382009-10-29 00:04:11 +00002579 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002580 break;
2581
2582 case Type::Vector:
2583 case Type::ExtVector:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002584 MarkUsedTemplateParameters(SemaRef,
2585 cast<VectorType>(T)->getElementType(),
Douglas Gregor21610382009-10-29 00:04:11 +00002586 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002587 break;
2588
Douglas Gregor758a8692009-06-17 21:51:59 +00002589 case Type::DependentSizedExtVector: {
2590 const DependentSizedExtVectorType *VecType
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00002591 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002592 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002593 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002594 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002595 Depth, Used);
Douglas Gregor758a8692009-06-17 21:51:59 +00002596 break;
2597 }
2598
Douglas Gregor91772d12009-06-13 00:26:55 +00002599 case Type::FunctionProto: {
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00002600 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002601 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002602 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002603 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002604 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002605 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002606 break;
2607 }
2608
Douglas Gregor21610382009-10-29 00:04:11 +00002609 case Type::TemplateTypeParm: {
2610 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2611 if (TTP->getDepth() == Depth)
2612 Used[TTP->getIndex()] = true;
Douglas Gregor91772d12009-06-13 00:26:55 +00002613 break;
Douglas Gregor21610382009-10-29 00:04:11 +00002614 }
Douglas Gregor91772d12009-06-13 00:26:55 +00002615
John McCall2408e322010-04-27 00:57:59 +00002616 case Type::InjectedClassName:
2617 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
2618 // fall through
2619
Douglas Gregor91772d12009-06-13 00:26:55 +00002620 case Type::TemplateSpecialization: {
Mike Stump11289f42009-09-09 15:08:12 +00002621 const TemplateSpecializationType *Spec
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00002622 = cast<TemplateSpecializationType>(T);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002623 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002624 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002625 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregor21610382009-10-29 00:04:11 +00002626 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2627 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002628 break;
2629 }
2630
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002631 case Type::Complex:
2632 if (!OnlyDeduced)
2633 MarkUsedTemplateParameters(SemaRef,
2634 cast<ComplexType>(T)->getElementType(),
Douglas Gregor21610382009-10-29 00:04:11 +00002635 OnlyDeduced, Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002636 break;
2637
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002638 case Type::DependentName:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002639 if (!OnlyDeduced)
2640 MarkUsedTemplateParameters(SemaRef,
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00002641 cast<DependentNameType>(T)->getQualifier(),
Douglas Gregor21610382009-10-29 00:04:11 +00002642 OnlyDeduced, Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002643 break;
2644
John McCallc392f372010-06-11 00:33:02 +00002645 case Type::DependentTemplateSpecialization: {
2646 const DependentTemplateSpecializationType *Spec
2647 = cast<DependentTemplateSpecializationType>(T);
2648 if (!OnlyDeduced)
2649 MarkUsedTemplateParameters(SemaRef, Spec->getQualifier(),
2650 OnlyDeduced, Depth, Used);
2651 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
2652 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2653 Used);
2654 break;
2655 }
2656
John McCallbd8d9bd2010-03-01 23:49:17 +00002657 case Type::TypeOf:
2658 if (!OnlyDeduced)
2659 MarkUsedTemplateParameters(SemaRef,
2660 cast<TypeOfType>(T)->getUnderlyingType(),
2661 OnlyDeduced, Depth, Used);
2662 break;
2663
2664 case Type::TypeOfExpr:
2665 if (!OnlyDeduced)
2666 MarkUsedTemplateParameters(SemaRef,
2667 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
2668 OnlyDeduced, Depth, Used);
2669 break;
2670
2671 case Type::Decltype:
2672 if (!OnlyDeduced)
2673 MarkUsedTemplateParameters(SemaRef,
2674 cast<DecltypeType>(T)->getUnderlyingExpr(),
2675 OnlyDeduced, Depth, Used);
2676 break;
2677
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002678 // None of these types have any template parameters in them.
Douglas Gregor91772d12009-06-13 00:26:55 +00002679 case Type::Builtin:
Douglas Gregor91772d12009-06-13 00:26:55 +00002680 case Type::VariableArray:
2681 case Type::FunctionNoProto:
2682 case Type::Record:
2683 case Type::Enum:
Douglas Gregor91772d12009-06-13 00:26:55 +00002684 case Type::ObjCInterface:
John McCall8b07ec22010-05-15 11:32:37 +00002685 case Type::ObjCObject:
Steve Narofffb4330f2009-06-17 22:40:22 +00002686 case Type::ObjCObjectPointer:
John McCallb96ec562009-12-04 22:46:56 +00002687 case Type::UnresolvedUsing:
Douglas Gregor91772d12009-06-13 00:26:55 +00002688#define TYPE(Class, Base)
2689#define ABSTRACT_TYPE(Class, Base)
2690#define DEPENDENT_TYPE(Class, Base)
2691#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2692#include "clang/AST/TypeNodes.def"
2693 break;
2694 }
2695}
2696
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002697/// \brief Mark the template parameters that are used by this
Douglas Gregor91772d12009-06-13 00:26:55 +00002698/// template argument.
Mike Stump11289f42009-09-09 15:08:12 +00002699static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002700MarkUsedTemplateParameters(Sema &SemaRef,
2701 const TemplateArgument &TemplateArg,
2702 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002703 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002704 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor91772d12009-06-13 00:26:55 +00002705 switch (TemplateArg.getKind()) {
2706 case TemplateArgument::Null:
2707 case TemplateArgument::Integral:
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002708 case TemplateArgument::Declaration:
Douglas Gregor91772d12009-06-13 00:26:55 +00002709 break;
Mike Stump11289f42009-09-09 15:08:12 +00002710
Douglas Gregor91772d12009-06-13 00:26:55 +00002711 case TemplateArgument::Type:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002712 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002713 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002714 break;
2715
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002716 case TemplateArgument::Template:
2717 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2718 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002719 break;
2720
2721 case TemplateArgument::Expression:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002722 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002723 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002724 break;
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002725
Anders Carlssonbc343912009-06-15 17:04:53 +00002726 case TemplateArgument::Pack:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002727 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2728 PEnd = TemplateArg.pack_end();
2729 P != PEnd; ++P)
Douglas Gregor21610382009-10-29 00:04:11 +00002730 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssonbc343912009-06-15 17:04:53 +00002731 break;
Douglas Gregor91772d12009-06-13 00:26:55 +00002732 }
2733}
2734
2735/// \brief Mark the template parameters can be deduced by the given
2736/// template argument list.
2737///
2738/// \param TemplateArgs the template argument list from which template
2739/// parameters will be deduced.
2740///
2741/// \param Deduced a bit vector whose elements will be set to \c true
2742/// to indicate when the corresponding template parameter will be
2743/// deduced.
Mike Stump11289f42009-09-09 15:08:12 +00002744void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002745Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregor21610382009-10-29 00:04:11 +00002746 bool OnlyDeduced, unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002747 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor91772d12009-06-13 00:26:55 +00002748 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
Douglas Gregor21610382009-10-29 00:04:11 +00002749 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2750 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002751}
Douglas Gregorce23bae2009-09-18 23:21:38 +00002752
2753/// \brief Marks all of the template parameters that will be deduced by a
2754/// call to the given function template.
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00002755void
2756Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
2757 llvm::SmallVectorImpl<bool> &Deduced) {
Douglas Gregorce23bae2009-09-18 23:21:38 +00002758 TemplateParameterList *TemplateParams
2759 = FunctionTemplate->getTemplateParameters();
2760 Deduced.clear();
2761 Deduced.resize(TemplateParams->size());
2762
2763 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2764 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
2765 ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
Douglas Gregor21610382009-10-29 00:04:11 +00002766 true, TemplateParams->getDepth(), Deduced);
Douglas Gregorce23bae2009-09-18 23:21:38 +00002767}