blob: 2eb3af2b6034135b1688a883a9964071a86ffa85 [file] [log] [blame]
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001//===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template argument deduction.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/StmtVisitor.h"
17#include "clang/AST/Expr.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/Parse/DeclSpec.h"
20#include "llvm/Support/Compiler.h"
Douglas Gregor8a514912009-09-14 18:39:43 +000021#include <algorithm>
Douglas Gregor508f1c82009-06-26 23:10:12 +000022
23namespace clang {
24 /// \brief Various flags that control template argument deduction.
25 ///
26 /// These flags can be bitwise-OR'd together.
27 enum TemplateDeductionFlags {
28 /// \brief No template argument deduction flags, which indicates the
29 /// strictest results for template argument deduction (as used for, e.g.,
30 /// matching class template partial specializations).
31 TDF_None = 0,
32 /// \brief Within template argument deduction from a function call, we are
33 /// matching with a parameter type for which the original parameter was
34 /// a reference.
35 TDF_ParamWithReferenceType = 0x1,
36 /// \brief Within template argument deduction from a function call, we
37 /// are matching in a case where we ignore cv-qualifiers.
38 TDF_IgnoreQualifiers = 0x02,
39 /// \brief Within template argument deduction from a function call,
40 /// we are matching in a case where we can perform template argument
Douglas Gregor41128772009-06-26 23:27:24 +000041 /// deduction from a template-id of a derived class of the argument type.
Douglas Gregor12820292009-09-14 20:00:47 +000042 TDF_DerivedClass = 0x04,
43 /// \brief Allow non-dependent types to differ, e.g., when performing
44 /// template argument deduction from a function call where conversions
45 /// may apply.
46 TDF_SkipNonDependent = 0x08
Douglas Gregor508f1c82009-06-26 23:10:12 +000047 };
48}
49
Douglas Gregor0b9247f2009-06-04 00:03:07 +000050using namespace clang;
51
Douglas Gregorf67875d2009-06-12 18:26:56 +000052static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +000053DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +000054 TemplateParameterList *TemplateParams,
55 const TemplateArgument &Param,
Douglas Gregord708c722009-06-09 16:35:58 +000056 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +000057 Sema::TemplateDeductionInfo &Info,
Douglas Gregord708c722009-06-09 16:35:58 +000058 llvm::SmallVectorImpl<TemplateArgument> &Deduced);
59
Douglas Gregor199d9912009-06-05 00:53:49 +000060/// \brief If the given expression is of a form that permits the deduction
61/// of a non-type template parameter, return the declaration of that
62/// non-type template parameter.
63static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
64 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
65 E = IC->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +000066
Douglas Gregor199d9912009-06-05 00:53:49 +000067 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
68 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +000069
Douglas Gregor199d9912009-06-05 00:53:49 +000070 return 0;
71}
72
Mike Stump1eb44332009-09-09 15:08:12 +000073/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +000074/// from the given constant.
Douglas Gregorf67875d2009-06-12 18:26:56 +000075static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +000076DeduceNonTypeTemplateArgument(ASTContext &Context,
77 NonTypeTemplateParmDecl *NTTP,
Anders Carlsson335e24a2009-06-16 22:44:31 +000078 llvm::APSInt Value,
Douglas Gregorf67875d2009-06-12 18:26:56 +000079 Sema::TemplateDeductionInfo &Info,
80 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +000081 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +000082 "Cannot deduce non-type template argument with depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +000083
Douglas Gregor199d9912009-06-05 00:53:49 +000084 if (Deduced[NTTP->getIndex()].isNull()) {
Anders Carlsson25af1ed2009-06-16 23:08:29 +000085 QualType T = NTTP->getType();
Mike Stump1eb44332009-09-09 15:08:12 +000086
Anders Carlsson25af1ed2009-06-16 23:08:29 +000087 // FIXME: Make sure we didn't overflow our data type!
88 unsigned AllowedBits = Context.getTypeSize(T);
89 if (Value.getBitWidth() != AllowedBits)
90 Value.extOrTrunc(AllowedBits);
91 Value.setIsSigned(T->isSignedIntegerType());
92
John McCall833ca992009-10-29 08:12:44 +000093 Deduced[NTTP->getIndex()] = TemplateArgument(Value, T);
Douglas Gregorf67875d2009-06-12 18:26:56 +000094 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +000095 }
Mike Stump1eb44332009-09-09 15:08:12 +000096
Douglas Gregorf67875d2009-06-12 18:26:56 +000097 assert(Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral);
Mike Stump1eb44332009-09-09 15:08:12 +000098
99 // If the template argument was previously deduced to a negative value,
Douglas Gregor199d9912009-06-05 00:53:49 +0000100 // then our deduction fails.
101 const llvm::APSInt *PrevValuePtr = Deduced[NTTP->getIndex()].getAsIntegral();
Anders Carlsson335e24a2009-06-16 22:44:31 +0000102 if (PrevValuePtr->isNegative()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000103 Info.Param = NTTP;
104 Info.FirstArg = Deduced[NTTP->getIndex()];
John McCall833ca992009-10-29 08:12:44 +0000105 Info.SecondArg = TemplateArgument(Value, NTTP->getType());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000106 return Sema::TDK_Inconsistent;
107 }
108
Anders Carlsson335e24a2009-06-16 22:44:31 +0000109 llvm::APSInt PrevValue = *PrevValuePtr;
Douglas Gregor199d9912009-06-05 00:53:49 +0000110 if (Value.getBitWidth() > PrevValue.getBitWidth())
111 PrevValue.zext(Value.getBitWidth());
112 else if (Value.getBitWidth() < PrevValue.getBitWidth())
113 Value.zext(PrevValue.getBitWidth());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000114
115 if (Value != PrevValue) {
116 Info.Param = NTTP;
117 Info.FirstArg = Deduced[NTTP->getIndex()];
John McCall833ca992009-10-29 08:12:44 +0000118 Info.SecondArg = TemplateArgument(Value, NTTP->getType());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000119 return Sema::TDK_Inconsistent;
120 }
121
122 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000123}
124
Mike Stump1eb44332009-09-09 15:08:12 +0000125/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +0000126/// from the given type- or value-dependent expression.
127///
128/// \returns true if deduction succeeded, false otherwise.
129
Douglas Gregorf67875d2009-06-12 18:26:56 +0000130static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +0000131DeduceNonTypeTemplateArgument(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000132 NonTypeTemplateParmDecl *NTTP,
133 Expr *Value,
134 Sema::TemplateDeductionInfo &Info,
135 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +0000136 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000137 "Cannot deduce non-type template argument with depth > 0");
138 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
139 "Expression template argument must be type- or value-dependent.");
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Douglas Gregor199d9912009-06-05 00:53:49 +0000141 if (Deduced[NTTP->getIndex()].isNull()) {
142 // FIXME: Clone the Value?
143 Deduced[NTTP->getIndex()] = TemplateArgument(Value);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000144 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000145 }
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Douglas Gregor199d9912009-06-05 00:53:49 +0000147 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
Mike Stump1eb44332009-09-09 15:08:12 +0000148 // Okay, we deduced a constant in one case and a dependent expression
149 // in another case. FIXME: Later, we will check that instantiating the
Douglas Gregor199d9912009-06-05 00:53:49 +0000150 // dependent expression gives us the constant value.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000151 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000152 }
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Douglas Gregor9eea08b2009-09-15 16:51:42 +0000154 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
155 // Compare the expressions for equality
156 llvm::FoldingSetNodeID ID1, ID2;
157 Deduced[NTTP->getIndex()].getAsExpr()->Profile(ID1, Context, true);
158 Value->Profile(ID2, Context, true);
159 if (ID1 == ID2)
160 return Sema::TDK_Success;
161
162 // FIXME: Fill in argument mismatch information
163 return Sema::TDK_NonDeducedMismatch;
164 }
165
Douglas Gregorf67875d2009-06-12 18:26:56 +0000166 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000167}
168
Douglas Gregorf67875d2009-06-12 18:26:56 +0000169static Sema::TemplateDeductionResult
170DeduceTemplateArguments(ASTContext &Context,
171 TemplateName Param,
172 TemplateName Arg,
173 Sema::TemplateDeductionInfo &Info,
174 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregord708c722009-06-09 16:35:58 +0000175 // FIXME: Implement template argument deduction for template
176 // template parameters.
177
Douglas Gregorf67875d2009-06-12 18:26:56 +0000178 // FIXME: this routine does not have enough information to produce
179 // good diagnostics.
180
Douglas Gregord708c722009-06-09 16:35:58 +0000181 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
182 TemplateDecl *ArgDecl = Arg.getAsTemplateDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Douglas Gregorf67875d2009-06-12 18:26:56 +0000184 if (!ParamDecl || !ArgDecl) {
185 // FIXME: fill in Info.Param/Info.FirstArg
186 return Sema::TDK_Inconsistent;
187 }
Douglas Gregord708c722009-06-09 16:35:58 +0000188
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000189 ParamDecl = cast<TemplateDecl>(ParamDecl->getCanonicalDecl());
190 ArgDecl = cast<TemplateDecl>(ArgDecl->getCanonicalDecl());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000191 if (ParamDecl != ArgDecl) {
192 // FIXME: fill in Info.Param/Info.FirstArg
193 return Sema::TDK_Inconsistent;
194 }
195
196 return Sema::TDK_Success;
Douglas Gregord708c722009-06-09 16:35:58 +0000197}
198
Mike Stump1eb44332009-09-09 15:08:12 +0000199/// \brief Deduce the template arguments by comparing the template parameter
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000200/// type (which is a template-id) with the template argument type.
201///
202/// \param Context the AST context in which this deduction occurs.
203///
204/// \param TemplateParams the template parameters that we are deducing
205///
206/// \param Param the parameter type
207///
208/// \param Arg the argument type
209///
210/// \param Info information about the template argument deduction itself
211///
212/// \param Deduced the deduced template arguments
213///
214/// \returns the result of template argument deduction so far. Note that a
215/// "success" result means that template argument deduction has not yet failed,
216/// but it may still fail, later, for other reasons.
217static Sema::TemplateDeductionResult
218DeduceTemplateArguments(ASTContext &Context,
219 TemplateParameterList *TemplateParams,
220 const TemplateSpecializationType *Param,
221 QualType Arg,
222 Sema::TemplateDeductionInfo &Info,
223 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
John McCall467b27b2009-10-22 20:10:53 +0000224 assert(Arg.isCanonical() && "Argument type must be canonical");
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000226 // Check whether the template argument is a dependent template-id.
227 // FIXME: This is untested code; it can be tested when we implement
228 // partial ordering of class template partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +0000229 if (const TemplateSpecializationType *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000230 = dyn_cast<TemplateSpecializationType>(Arg)) {
231 // Perform template argument deduction for the template name.
232 if (Sema::TemplateDeductionResult Result
233 = DeduceTemplateArguments(Context,
234 Param->getTemplateName(),
235 SpecArg->getTemplateName(),
236 Info, Deduced))
237 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000239 unsigned NumArgs = Param->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000241 // FIXME: When one of the template-names refers to a
242 // declaration with default template arguments, do we need to
243 // fill in those default template arguments here? Most likely,
244 // the answer is "yes", but I don't see any references. This
245 // issue may be resolved elsewhere, because we may want to
246 // instantiate default template arguments when we actually write
247 // the template-id.
248 if (SpecArg->getNumArgs() != NumArgs)
249 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000251 // Perform template argument deduction on each template
252 // argument.
253 for (unsigned I = 0; I != NumArgs; ++I)
254 if (Sema::TemplateDeductionResult Result
255 = DeduceTemplateArguments(Context, TemplateParams,
256 Param->getArg(I),
257 SpecArg->getArg(I),
258 Info, Deduced))
259 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000261 return Sema::TDK_Success;
262 }
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000264 // If the argument type is a class template specialization, we
265 // perform template argument deduction using its template
266 // arguments.
267 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
268 if (!RecordArg)
269 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000270
271 ClassTemplateSpecializationDecl *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000272 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
273 if (!SpecArg)
274 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000276 // Perform template argument deduction for the template name.
277 if (Sema::TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000278 = DeduceTemplateArguments(Context,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000279 Param->getTemplateName(),
280 TemplateName(SpecArg->getSpecializedTemplate()),
281 Info, Deduced))
282 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000284 // FIXME: Can the # of arguments in the parameter and the argument
285 // differ due to default arguments?
286 unsigned NumArgs = Param->getNumArgs();
287 const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
288 if (NumArgs != ArgArgs.size())
289 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000291 for (unsigned I = 0; I != NumArgs; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000292 if (Sema::TemplateDeductionResult Result
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000293 = DeduceTemplateArguments(Context, TemplateParams,
294 Param->getArg(I),
295 ArgArgs.get(I),
296 Info, Deduced))
297 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000299 return Sema::TDK_Success;
300}
301
Mike Stump1eb44332009-09-09 15:08:12 +0000302/// \brief Returns a completely-unqualified array type, capturing the
John McCall0953e762009-09-24 19:53:00 +0000303/// qualifiers in Quals.
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000304///
305/// \param Context the AST context in which the array type was built.
306///
307/// \param T a canonical type that may be an array type.
308///
John McCall0953e762009-09-24 19:53:00 +0000309/// \param Quals will receive the full set of qualifiers that were
310/// applied to the element type of the array.
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000311///
312/// \returns if \p T is an array type, the completely unqualified array type
313/// that corresponds to T. Otherwise, returns T.
314static QualType getUnqualifiedArrayType(ASTContext &Context, QualType T,
John McCall0953e762009-09-24 19:53:00 +0000315 Qualifiers &Quals) {
John McCall467b27b2009-10-22 20:10:53 +0000316 assert(T.isCanonical() && "Only operates on canonical types");
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000317 if (!isa<ArrayType>(T)) {
John McCall0953e762009-09-24 19:53:00 +0000318 Quals = T.getQualifiers();
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000319 return T.getUnqualifiedType();
320 }
Mike Stump1eb44332009-09-09 15:08:12 +0000321
John McCall0953e762009-09-24 19:53:00 +0000322 assert(!T.hasQualifiers() && "canonical array type has qualifiers!");
323
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000324 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) {
325 QualType Elt = getUnqualifiedArrayType(Context, CAT->getElementType(),
John McCall0953e762009-09-24 19:53:00 +0000326 Quals);
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000327 if (Elt == CAT->getElementType())
328 return T;
329
Mike Stump1eb44332009-09-09 15:08:12 +0000330 return Context.getConstantArrayType(Elt, CAT->getSize(),
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000331 CAT->getSizeModifier(), 0);
332 }
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000334 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) {
335 QualType Elt = getUnqualifiedArrayType(Context, IAT->getElementType(),
John McCall0953e762009-09-24 19:53:00 +0000336 Quals);
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000337 if (Elt == IAT->getElementType())
338 return T;
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000340 return Context.getIncompleteArrayType(Elt, IAT->getSizeModifier(), 0);
341 }
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000343 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T);
344 QualType Elt = getUnqualifiedArrayType(Context, DSAT->getElementType(),
John McCall0953e762009-09-24 19:53:00 +0000345 Quals);
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000346 if (Elt == DSAT->getElementType())
347 return T;
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Anders Carlssond4972062009-08-08 02:50:17 +0000349 return Context.getDependentSizedArrayType(Elt, DSAT->getSizeExpr()->Retain(),
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000350 DSAT->getSizeModifier(), 0,
351 SourceRange());
352}
353
Douglas Gregor500d3312009-06-26 18:27:22 +0000354/// \brief Deduce the template arguments by comparing the parameter type and
355/// the argument type (C++ [temp.deduct.type]).
356///
357/// \param Context the AST context in which this deduction occurs.
358///
359/// \param TemplateParams the template parameters that we are deducing
360///
361/// \param ParamIn the parameter type
362///
363/// \param ArgIn the argument type
364///
365/// \param Info information about the template argument deduction itself
366///
367/// \param Deduced the deduced template arguments
368///
Douglas Gregor508f1c82009-06-26 23:10:12 +0000369/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
Mike Stump1eb44332009-09-09 15:08:12 +0000370/// how template argument deduction is performed.
Douglas Gregor500d3312009-06-26 18:27:22 +0000371///
372/// \returns the result of template argument deduction so far. Note that a
373/// "success" result means that template argument deduction has not yet failed,
374/// but it may still fail, later, for other reasons.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000375static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +0000376DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000377 TemplateParameterList *TemplateParams,
378 QualType ParamIn, QualType ArgIn,
379 Sema::TemplateDeductionInfo &Info,
Douglas Gregor500d3312009-06-26 18:27:22 +0000380 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000381 unsigned TDF) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000382 // We only want to look at the canonical types, since typedefs and
383 // sugar are not part of template argument deduction.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000384 QualType Param = Context.getCanonicalType(ParamIn);
385 QualType Arg = Context.getCanonicalType(ArgIn);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000386
Douglas Gregor500d3312009-06-26 18:27:22 +0000387 // C++0x [temp.deduct.call]p4 bullet 1:
388 // - If the original P is a reference type, the deduced A (i.e., the type
Mike Stump1eb44332009-09-09 15:08:12 +0000389 // referred to by the reference) can be more cv-qualified than the
Douglas Gregor500d3312009-06-26 18:27:22 +0000390 // transformed A.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000391 if (TDF & TDF_ParamWithReferenceType) {
John McCall0953e762009-09-24 19:53:00 +0000392 Qualifiers Quals = Param.getQualifiers();
393 Quals.setCVRQualifiers(Quals.getCVRQualifiers() & Arg.getCVRQualifiers());
394 Param = Context.getQualifiedType(Param.getUnqualifiedType(), Quals);
Douglas Gregor500d3312009-06-26 18:27:22 +0000395 }
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000397 // If the parameter type is not dependent, there is nothing to deduce.
Douglas Gregor12820292009-09-14 20:00:47 +0000398 if (!Param->isDependentType()) {
399 if (!(TDF & TDF_SkipNonDependent) && Param != Arg) {
400
401 return Sema::TDK_NonDeducedMismatch;
402 }
403
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000404 return Sema::TDK_Success;
Douglas Gregor12820292009-09-14 20:00:47 +0000405 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000406
Douglas Gregor199d9912009-06-05 00:53:49 +0000407 // C++ [temp.deduct.type]p9:
Mike Stump1eb44332009-09-09 15:08:12 +0000408 // A template type argument T, a template template argument TT or a
409 // template non-type argument i can be deduced if P and A have one of
Douglas Gregor199d9912009-06-05 00:53:49 +0000410 // the following forms:
411 //
412 // T
413 // cv-list T
Mike Stump1eb44332009-09-09 15:08:12 +0000414 if (const TemplateTypeParmType *TemplateTypeParm
John McCall183700f2009-09-21 23:43:11 +0000415 = Param->getAs<TemplateTypeParmType>()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000416 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000417 bool RecanonicalizeArg = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Douglas Gregor9e9fae42009-07-22 20:02:25 +0000419 // If the argument type is an array type, move the qualifiers up to the
420 // top level, so they can be matched with the qualifiers on the parameter.
421 // FIXME: address spaces, ObjC GC qualifiers
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000422 if (isa<ArrayType>(Arg)) {
John McCall0953e762009-09-24 19:53:00 +0000423 Qualifiers Quals;
424 Arg = getUnqualifiedArrayType(Context, Arg, Quals);
425 if (Quals) {
426 Arg = Context.getQualifiedType(Arg, Quals);
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000427 RecanonicalizeArg = true;
428 }
429 }
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000431 // The argument type can not be less qualified than the parameter
432 // type.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000433 if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000434 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
435 Info.FirstArg = Deduced[Index];
John McCall833ca992009-10-29 08:12:44 +0000436 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000437 return Sema::TDK_InconsistentQuals;
438 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000439
440 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000441
John McCall0953e762009-09-24 19:53:00 +0000442 QualType DeducedType = Arg;
443 DeducedType.removeCVRQualifiers(Param.getCVRQualifiers());
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000444 if (RecanonicalizeArg)
445 DeducedType = Context.getCanonicalType(DeducedType);
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000447 if (Deduced[Index].isNull())
John McCall833ca992009-10-29 08:12:44 +0000448 Deduced[Index] = TemplateArgument(DeducedType);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000449 else {
Mike Stump1eb44332009-09-09 15:08:12 +0000450 // C++ [temp.deduct.type]p2:
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000451 // [...] If type deduction cannot be done for any P/A pair, or if for
Mike Stump1eb44332009-09-09 15:08:12 +0000452 // any pair the deduction leads to more than one possible set of
453 // deduced values, or if different pairs yield different deduced
454 // values, or if any template argument remains neither deduced nor
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000455 // explicitly specified, template argument deduction fails.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000456 if (Deduced[Index].getAsType() != DeducedType) {
Mike Stump1eb44332009-09-09 15:08:12 +0000457 Info.Param
Douglas Gregorf67875d2009-06-12 18:26:56 +0000458 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
459 Info.FirstArg = Deduced[Index];
John McCall833ca992009-10-29 08:12:44 +0000460 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000461 return Sema::TDK_Inconsistent;
462 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000463 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000464 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000465 }
466
Douglas Gregorf67875d2009-06-12 18:26:56 +0000467 // Set up the template argument deduction information for a failure.
John McCall833ca992009-10-29 08:12:44 +0000468 Info.FirstArg = TemplateArgument(ParamIn);
469 Info.SecondArg = TemplateArgument(ArgIn);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000470
Douglas Gregor508f1c82009-06-26 23:10:12 +0000471 // Check the cv-qualifiers on the parameter and argument types.
472 if (!(TDF & TDF_IgnoreQualifiers)) {
473 if (TDF & TDF_ParamWithReferenceType) {
474 if (Param.isMoreQualifiedThan(Arg))
475 return Sema::TDK_NonDeducedMismatch;
476 } else {
477 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Mike Stump1eb44332009-09-09 15:08:12 +0000478 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor508f1c82009-06-26 23:10:12 +0000479 }
480 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000481
Douglas Gregord560d502009-06-04 00:21:18 +0000482 switch (Param->getTypeClass()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000483 // No deduction possible for these types
484 case Type::Builtin:
Douglas Gregorf67875d2009-06-12 18:26:56 +0000485 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Douglas Gregor199d9912009-06-05 00:53:49 +0000487 // T *
Douglas Gregord560d502009-06-04 00:21:18 +0000488 case Type::Pointer: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000489 const PointerType *PointerArg = Arg->getAs<PointerType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000490 if (!PointerArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000491 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Douglas Gregor41128772009-06-26 23:27:24 +0000493 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000494 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000495 cast<PointerType>(Param)->getPointeeType(),
496 PointerArg->getPointeeType(),
Douglas Gregor41128772009-06-26 23:27:24 +0000497 Info, Deduced, SubTDF);
Douglas Gregord560d502009-06-04 00:21:18 +0000498 }
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Douglas Gregor199d9912009-06-05 00:53:49 +0000500 // T &
Douglas Gregord560d502009-06-04 00:21:18 +0000501 case Type::LValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000502 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000503 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000504 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Douglas Gregorf67875d2009-06-12 18:26:56 +0000506 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000507 cast<LValueReferenceType>(Param)->getPointeeType(),
508 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000509 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000510 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000511
Douglas Gregor199d9912009-06-05 00:53:49 +0000512 // T && [C++0x]
Douglas Gregord560d502009-06-04 00:21:18 +0000513 case Type::RValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000514 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000515 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000516 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Douglas Gregorf67875d2009-06-12 18:26:56 +0000518 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000519 cast<RValueReferenceType>(Param)->getPointeeType(),
520 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000521 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000522 }
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Douglas Gregor199d9912009-06-05 00:53:49 +0000524 // T [] (implied, but not stated explicitly)
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000525 case Type::IncompleteArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000526 const IncompleteArrayType *IncompleteArrayArg =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000527 Context.getAsIncompleteArrayType(Arg);
528 if (!IncompleteArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000529 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Douglas Gregorf67875d2009-06-12 18:26:56 +0000531 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000532 Context.getAsIncompleteArrayType(Param)->getElementType(),
533 IncompleteArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000534 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000535 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000536
537 // T [integer-constant]
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000538 case Type::ConstantArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000539 const ConstantArrayType *ConstantArrayArg =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000540 Context.getAsConstantArrayType(Arg);
541 if (!ConstantArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000542 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000543
544 const ConstantArrayType *ConstantArrayParm =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000545 Context.getAsConstantArrayType(Param);
546 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000547 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Douglas Gregorf67875d2009-06-12 18:26:56 +0000549 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000550 ConstantArrayParm->getElementType(),
551 ConstantArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000552 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000553 }
554
Douglas Gregor199d9912009-06-05 00:53:49 +0000555 // type [i]
556 case Type::DependentSizedArray: {
557 const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg);
558 if (!ArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000559 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Douglas Gregor199d9912009-06-05 00:53:49 +0000561 // Check the element type of the arrays
562 const DependentSizedArrayType *DependentArrayParm
563 = cast<DependentSizedArrayType>(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000564 if (Sema::TemplateDeductionResult Result
565 = DeduceTemplateArguments(Context, TemplateParams,
566 DependentArrayParm->getElementType(),
567 ArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000568 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000569 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Douglas Gregor199d9912009-06-05 00:53:49 +0000571 // Determine the array bound is something we can deduce.
Mike Stump1eb44332009-09-09 15:08:12 +0000572 NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000573 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
574 if (!NTTP)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000575 return Sema::TDK_Success;
Mike Stump1eb44332009-09-09 15:08:12 +0000576
577 // We can perform template argument deduction for the given non-type
Douglas Gregor199d9912009-06-05 00:53:49 +0000578 // template parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000579 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000580 "Cannot deduce non-type template argument at depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000581 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson335e24a2009-06-16 22:44:31 +0000582 = dyn_cast<ConstantArrayType>(ArrayArg)) {
583 llvm::APSInt Size(ConstantArrayArg->getSize());
584 return DeduceNonTypeTemplateArgument(Context, NTTP, Size,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000585 Info, Deduced);
Anders Carlsson335e24a2009-06-16 22:44:31 +0000586 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000587 if (const DependentSizedArrayType *DependentArrayArg
588 = dyn_cast<DependentSizedArrayType>(ArrayArg))
589 return DeduceNonTypeTemplateArgument(Context, NTTP,
590 DependentArrayArg->getSizeExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000591 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Douglas Gregor199d9912009-06-05 00:53:49 +0000593 // Incomplete type does not match a dependently-sized array type
Douglas Gregorf67875d2009-06-12 18:26:56 +0000594 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000595 }
Mike Stump1eb44332009-09-09 15:08:12 +0000596
597 // type(*)(T)
598 // T(*)()
599 // T(*)(T)
Anders Carlssona27fad52009-06-08 15:19:08 +0000600 case Type::FunctionProto: {
Mike Stump1eb44332009-09-09 15:08:12 +0000601 const FunctionProtoType *FunctionProtoArg =
Anders Carlssona27fad52009-06-08 15:19:08 +0000602 dyn_cast<FunctionProtoType>(Arg);
603 if (!FunctionProtoArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000604 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000605
606 const FunctionProtoType *FunctionProtoParam =
Anders Carlssona27fad52009-06-08 15:19:08 +0000607 cast<FunctionProtoType>(Param);
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000608
Mike Stump1eb44332009-09-09 15:08:12 +0000609 if (FunctionProtoParam->getTypeQuals() !=
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000610 FunctionProtoArg->getTypeQuals())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000611 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000613 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000614 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000616 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000617 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000618
Anders Carlssona27fad52009-06-08 15:19:08 +0000619 // Check return types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000620 if (Sema::TemplateDeductionResult Result
621 = DeduceTemplateArguments(Context, TemplateParams,
622 FunctionProtoParam->getResultType(),
623 FunctionProtoArg->getResultType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000624 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000625 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Anders Carlssona27fad52009-06-08 15:19:08 +0000627 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
628 // Check argument types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000629 if (Sema::TemplateDeductionResult Result
630 = DeduceTemplateArguments(Context, TemplateParams,
631 FunctionProtoParam->getArgType(I),
632 FunctionProtoArg->getArgType(I),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000633 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000634 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000635 }
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Douglas Gregorf67875d2009-06-12 18:26:56 +0000637 return Sema::TDK_Success;
Anders Carlssona27fad52009-06-08 15:19:08 +0000638 }
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000640 // template-name<T> (where template-name refers to a class template)
Douglas Gregord708c722009-06-09 16:35:58 +0000641 // template-name<i>
642 // TT<T> (TODO)
643 // TT<i> (TODO)
644 // TT<> (TODO)
645 case Type::TemplateSpecialization: {
646 const TemplateSpecializationType *SpecParam
647 = cast<TemplateSpecializationType>(Param);
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000649 // Try to deduce template arguments from the template-id.
650 Sema::TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000651 = DeduceTemplateArguments(Context, TemplateParams, SpecParam, Arg,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000652 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Douglas Gregor4a5c15f2009-09-30 22:13:51 +0000654 if (Result && (TDF & TDF_DerivedClass)) {
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000655 // C++ [temp.deduct.call]p3b3:
656 // If P is a class, and P has the form template-id, then A can be a
657 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump1eb44332009-09-09 15:08:12 +0000658 // class of the form template-id, A can be a pointer to a derived
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000659 // class pointed to by the deduced A.
660 //
661 // More importantly:
Mike Stump1eb44332009-09-09 15:08:12 +0000662 // These alternatives are considered only if type deduction would
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000663 // otherwise fail.
664 if (const RecordType *RecordT = dyn_cast<RecordType>(Arg)) {
665 // Use data recursion to crawl through the list of base classes.
Mike Stump1eb44332009-09-09 15:08:12 +0000666 // Visited contains the set of nodes we have already visited, while
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000667 // ToVisit is our stack of records that we still need to visit.
668 llvm::SmallPtrSet<const RecordType *, 8> Visited;
669 llvm::SmallVector<const RecordType *, 8> ToVisit;
670 ToVisit.push_back(RecordT);
671 bool Successful = false;
672 while (!ToVisit.empty()) {
673 // Retrieve the next class in the inheritance hierarchy.
674 const RecordType *NextT = ToVisit.back();
675 ToVisit.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000677 // If we have already seen this type, skip it.
678 if (!Visited.insert(NextT))
679 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000681 // If this is a base class, try to perform template argument
682 // deduction from it.
683 if (NextT != RecordT) {
684 Sema::TemplateDeductionResult BaseResult
685 = DeduceTemplateArguments(Context, TemplateParams, SpecParam,
686 QualType(NextT, 0), Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000688 // If template argument deduction for this base was successful,
689 // note that we had some success.
690 if (BaseResult == Sema::TDK_Success)
691 Successful = true;
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000692 }
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000694 // Visit base classes
695 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
696 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
697 BaseEnd = Next->bases_end();
Sebastian Redl9994a342009-10-25 17:03:50 +0000698 Base != BaseEnd; ++Base) {
Mike Stump1eb44332009-09-09 15:08:12 +0000699 assert(Base->getType()->isRecordType() &&
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000700 "Base class that isn't a record?");
Ted Kremenek6217b802009-07-29 21:53:49 +0000701 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000702 }
703 }
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000705 if (Successful)
706 return Sema::TDK_Success;
707 }
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000709 }
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000711 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000712 }
713
Douglas Gregor637a4092009-06-10 23:47:09 +0000714 // T type::*
715 // T T::*
716 // T (type::*)()
717 // type (T::*)()
718 // type (type::*)(T)
719 // type (T::*)(T)
720 // T (type::*)(T)
721 // T (T::*)()
722 // T (T::*)(T)
723 case Type::MemberPointer: {
724 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
725 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
726 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000727 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +0000728
Douglas Gregorf67875d2009-06-12 18:26:56 +0000729 if (Sema::TemplateDeductionResult Result
730 = DeduceTemplateArguments(Context, TemplateParams,
731 MemPtrParam->getPointeeType(),
732 MemPtrArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000733 Info, Deduced,
734 TDF & TDF_IgnoreQualifiers))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000735 return Result;
736
737 return DeduceTemplateArguments(Context, TemplateParams,
738 QualType(MemPtrParam->getClass(), 0),
739 QualType(MemPtrArg->getClass(), 0),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000740 Info, Deduced, 0);
Douglas Gregor637a4092009-06-10 23:47:09 +0000741 }
742
Anders Carlsson9a917e42009-06-12 22:56:54 +0000743 // (clang extension)
744 //
Mike Stump1eb44332009-09-09 15:08:12 +0000745 // type(^)(T)
746 // T(^)()
747 // T(^)(T)
Anders Carlsson859ba502009-06-12 16:23:10 +0000748 case Type::BlockPointer: {
749 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
750 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Anders Carlsson859ba502009-06-12 16:23:10 +0000752 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000753 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Douglas Gregorf67875d2009-06-12 18:26:56 +0000755 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +0000756 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000757 BlockPtrArg->getPointeeType(), Info,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000758 Deduced, 0);
Anders Carlsson859ba502009-06-12 16:23:10 +0000759 }
760
Douglas Gregor637a4092009-06-10 23:47:09 +0000761 case Type::TypeOfExpr:
762 case Type::TypeOf:
763 case Type::Typename:
764 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +0000765 return Sema::TDK_Success;
Douglas Gregor637a4092009-06-10 23:47:09 +0000766
Douglas Gregord560d502009-06-04 00:21:18 +0000767 default:
768 break;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000769 }
770
771 // FIXME: Many more cases to go (to go).
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000772 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000773}
774
Douglas Gregorf67875d2009-06-12 18:26:56 +0000775static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +0000776DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000777 TemplateParameterList *TemplateParams,
778 const TemplateArgument &Param,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000779 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000780 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000781 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000782 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000783 case TemplateArgument::Null:
784 assert(false && "Null template argument in parameter list");
785 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000786
787 case TemplateArgument::Type:
Douglas Gregor788cd062009-11-11 01:00:40 +0000788 if (Arg.getKind() == TemplateArgument::Type)
789 return DeduceTemplateArguments(Context, TemplateParams, Param.getAsType(),
790 Arg.getAsType(), Info, Deduced, 0);
791 Info.FirstArg = Param;
792 Info.SecondArg = Arg;
793 return Sema::TDK_NonDeducedMismatch;
794
795 case TemplateArgument::Template:
796#if 0
797 // FIXME: We need template argument deduction for template template
798 // parameters.
799 if (Arg.getKind() == TemplateArgument::Template)
800 return DeduceTemplateArguments(Context, TemplateParams,
801 Param.getAsTemplate(),
802 Arg.getAsTemplate(), Info, Deduced, 0);
803#endif
804 Info.FirstArg = Param;
805 Info.SecondArg = Arg;
806 return Sema::TDK_NonDeducedMismatch;
807
Douglas Gregor199d9912009-06-05 00:53:49 +0000808 case TemplateArgument::Declaration:
Douglas Gregor788cd062009-11-11 01:00:40 +0000809 if (Arg.getKind() == TemplateArgument::Declaration &&
810 Param.getAsDecl()->getCanonicalDecl() ==
811 Arg.getAsDecl()->getCanonicalDecl())
812 return Sema::TDK_Success;
813
Douglas Gregorf67875d2009-06-12 18:26:56 +0000814 Info.FirstArg = Param;
815 Info.SecondArg = Arg;
816 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Douglas Gregor199d9912009-06-05 00:53:49 +0000818 case TemplateArgument::Integral:
819 if (Arg.getKind() == TemplateArgument::Integral) {
820 // FIXME: Zero extension + sign checking here?
Douglas Gregorf67875d2009-06-12 18:26:56 +0000821 if (*Param.getAsIntegral() == *Arg.getAsIntegral())
822 return Sema::TDK_Success;
823
824 Info.FirstArg = Param;
825 Info.SecondArg = Arg;
826 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000827 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000828
829 if (Arg.getKind() == TemplateArgument::Expression) {
830 Info.FirstArg = Param;
831 Info.SecondArg = Arg;
832 return Sema::TDK_NonDeducedMismatch;
833 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000834
835 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000836 Info.FirstArg = Param;
837 Info.SecondArg = Arg;
838 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Douglas Gregor199d9912009-06-05 00:53:49 +0000840 case TemplateArgument::Expression: {
Mike Stump1eb44332009-09-09 15:08:12 +0000841 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000842 = getDeducedParameterFromExpr(Param.getAsExpr())) {
843 if (Arg.getKind() == TemplateArgument::Integral)
844 // FIXME: Sign problems here
Mike Stump1eb44332009-09-09 15:08:12 +0000845 return DeduceNonTypeTemplateArgument(Context, NTTP,
846 *Arg.getAsIntegral(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000847 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000848 if (Arg.getKind() == TemplateArgument::Expression)
849 return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000850 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Douglas Gregor199d9912009-06-05 00:53:49 +0000852 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000853 Info.FirstArg = Param;
854 Info.SecondArg = Arg;
855 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000856 }
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Douglas Gregor199d9912009-06-05 00:53:49 +0000858 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000859 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000860 }
Anders Carlssond01b1da2009-06-15 17:04:53 +0000861 case TemplateArgument::Pack:
862 assert(0 && "FIXME: Implement!");
863 break;
Douglas Gregor199d9912009-06-05 00:53:49 +0000864 }
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Douglas Gregorf67875d2009-06-12 18:26:56 +0000866 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000867}
868
Mike Stump1eb44332009-09-09 15:08:12 +0000869static Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000870DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000871 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000872 const TemplateArgumentList &ParamList,
873 const TemplateArgumentList &ArgList,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000874 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000875 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
876 assert(ParamList.size() == ArgList.size());
877 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000878 if (Sema::TemplateDeductionResult Result
879 = DeduceTemplateArguments(Context, TemplateParams,
Mike Stump1eb44332009-09-09 15:08:12 +0000880 ParamList[I], ArgList[I],
Douglas Gregorf67875d2009-06-12 18:26:56 +0000881 Info, Deduced))
882 return Result;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000883 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000884 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000885}
886
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000887/// \brief Determine whether two template arguments are the same.
Mike Stump1eb44332009-09-09 15:08:12 +0000888static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000889 const TemplateArgument &X,
890 const TemplateArgument &Y) {
891 if (X.getKind() != Y.getKind())
892 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000894 switch (X.getKind()) {
895 case TemplateArgument::Null:
896 assert(false && "Comparing NULL template argument");
897 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000899 case TemplateArgument::Type:
900 return Context.getCanonicalType(X.getAsType()) ==
901 Context.getCanonicalType(Y.getAsType());
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000903 case TemplateArgument::Declaration:
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000904 return X.getAsDecl()->getCanonicalDecl() ==
905 Y.getAsDecl()->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Douglas Gregor788cd062009-11-11 01:00:40 +0000907 case TemplateArgument::Template:
908 return Context.getCanonicalTemplateName(X.getAsTemplate())
909 .getAsVoidPointer() ==
910 Context.getCanonicalTemplateName(Y.getAsTemplate())
911 .getAsVoidPointer();
912
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000913 case TemplateArgument::Integral:
914 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Douglas Gregor788cd062009-11-11 01:00:40 +0000916 case TemplateArgument::Expression: {
917 llvm::FoldingSetNodeID XID, YID;
918 X.getAsExpr()->Profile(XID, Context, true);
919 Y.getAsExpr()->Profile(YID, Context, true);
920 return XID == YID;
921 }
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000923 case TemplateArgument::Pack:
924 if (X.pack_size() != Y.pack_size())
925 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000926
927 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
928 XPEnd = X.pack_end(),
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000929 YP = Y.pack_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000930 XP != XPEnd; ++XP, ++YP)
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000931 if (!isSameTemplateArg(Context, *XP, *YP))
932 return false;
933
934 return true;
935 }
936
937 return false;
938}
939
940/// \brief Helper function to build a TemplateParameter when we don't
941/// know its type statically.
942static TemplateParameter makeTemplateParameter(Decl *D) {
943 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
944 return TemplateParameter(TTP);
945 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
946 return TemplateParameter(NTTP);
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000948 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
949}
950
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000951/// \brief Perform template argument deduction to determine whether
952/// the given template arguments match the given class template
953/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +0000954Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000955Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000956 const TemplateArgumentList &TemplateArgs,
957 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000958 // C++ [temp.class.spec.match]p2:
959 // A partial specialization matches a given actual template
960 // argument list if the template arguments of the partial
961 // specialization can be deduced from the actual template argument
962 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +0000963 SFINAETrap Trap(*this);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000964 llvm::SmallVector<TemplateArgument, 4> Deduced;
965 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000966 if (TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000967 = ::DeduceTemplateArguments(Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000968 Partial->getTemplateParameters(),
Mike Stump1eb44332009-09-09 15:08:12 +0000969 Partial->getTemplateArgs(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000970 TemplateArgs, Info, Deduced))
971 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +0000972
Douglas Gregor637a4092009-06-10 23:47:09 +0000973 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
974 Deduced.data(), Deduced.size());
975 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000976 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +0000977
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000978 // C++ [temp.deduct.type]p2:
979 // [...] or if any template argument remains neither deduced nor
980 // explicitly specified, template argument deduction fails.
Anders Carlssonfb250522009-06-23 01:26:57 +0000981 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
982 Deduced.size());
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000983 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000984 if (Deduced[I].isNull()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000985 Decl *Param
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000986 = const_cast<NamedDecl *>(
987 Partial->getTemplateParameters()->getParam(I));
Douglas Gregorf67875d2009-06-12 18:26:56 +0000988 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
989 Info.Param = TTP;
Mike Stump1eb44332009-09-09 15:08:12 +0000990 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorf67875d2009-06-12 18:26:56 +0000991 = dyn_cast<NonTypeTemplateParmDecl>(Param))
992 Info.Param = NTTP;
993 else
994 Info.Param = cast<TemplateTemplateParmDecl>(Param);
995 return TDK_Incomplete;
996 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000997
Anders Carlssonfb250522009-06-23 01:26:57 +0000998 Builder.Append(Deduced[I]);
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000999 }
1000
1001 // Form the template argument list from the deduced template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001002 TemplateArgumentList *DeducedArgumentList
Anders Carlssonfb250522009-06-23 01:26:57 +00001003 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
Douglas Gregorf67875d2009-06-12 18:26:56 +00001004 Info.reset(DeducedArgumentList);
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001005
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001006 // Substitute the deduced template arguments into the template
1007 // arguments of the class template partial specialization, and
1008 // verify that the instantiated template arguments are both valid
1009 // and are equivalent to the template arguments originally provided
Mike Stump1eb44332009-09-09 15:08:12 +00001010 // to the class template.
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001011 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
John McCall833ca992009-10-29 08:12:44 +00001012 const TemplateArgumentLoc *PartialTemplateArgs
1013 = Partial->getTemplateArgsAsWritten();
1014 unsigned N = Partial->getNumTemplateArgsAsWritten();
1015 llvm::SmallVector<TemplateArgumentLoc, 16> InstArgs(N);
1016 for (unsigned I = 0; I != N; ++I) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001017 Decl *Param = const_cast<NamedDecl *>(
Douglas Gregorc9e5d252009-06-13 00:59:32 +00001018 ClassTemplate->getTemplateParameters()->getParam(I));
John McCall833ca992009-10-29 08:12:44 +00001019 if (Subst(PartialTemplateArgs[I], InstArgs[I],
1020 MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001021 Info.Param = makeTemplateParameter(Param);
John McCall833ca992009-10-29 08:12:44 +00001022 Info.FirstArg = PartialTemplateArgs[I].getArgument();
Mike Stump1eb44332009-09-09 15:08:12 +00001023 return TDK_SubstitutionFailure;
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001024 }
John McCall833ca992009-10-29 08:12:44 +00001025 }
1026
1027 TemplateArgumentListBuilder ConvertedInstArgs(
1028 ClassTemplate->getTemplateParameters(), N);
1029
1030 if (CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
1031 /*LAngle*/ SourceLocation(),
1032 InstArgs.data(), N,
1033 /*RAngle*/ SourceLocation(),
1034 false, ConvertedInstArgs)) {
1035 // FIXME: fail with more useful information?
1036 return TDK_SubstitutionFailure;
1037 }
1038
1039 for (unsigned I = 0, E = ConvertedInstArgs.flatSize(); I != E; ++I) {
1040 // We don't really care if we overwrite the internal structures of
1041 // the arg list builder, because we're going to throw it all away.
1042 TemplateArgument &InstArg
1043 = const_cast<TemplateArgument&>(ConvertedInstArgs.getFlatArguments()[I]);
1044
1045 Decl *Param = const_cast<NamedDecl *>(
1046 ClassTemplate->getTemplateParameters()->getParam(I));
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001048 if (InstArg.getKind() == TemplateArgument::Expression) {
Mike Stump1eb44332009-09-09 15:08:12 +00001049 // When the argument is an expression, check the expression result
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001050 // against the actual template parameter to get down to the canonical
1051 // template argument.
1052 Expr *InstExpr = InstArg.getAsExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001053 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001054 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1055 if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
1056 Info.Param = makeTemplateParameter(Param);
John McCall833ca992009-10-29 08:12:44 +00001057 Info.FirstArg = Partial->getTemplateArgs()[I];
Mike Stump1eb44332009-09-09 15:08:12 +00001058 return TDK_SubstitutionFailure;
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001059 }
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001060 }
1061 }
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001063 if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) {
1064 Info.Param = makeTemplateParameter(Param);
1065 Info.FirstArg = TemplateArgs[I];
1066 Info.SecondArg = InstArg;
1067 return TDK_NonDeducedMismatch;
1068 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001069 }
1070
Douglas Gregorbb260412009-06-14 08:02:22 +00001071 if (Trap.hasErrorOccurred())
1072 return TDK_SubstitutionFailure;
1073
Douglas Gregorf67875d2009-06-12 18:26:56 +00001074 return TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001075}
Douglas Gregor031a5882009-06-13 00:26:55 +00001076
Douglas Gregor41128772009-06-26 23:27:24 +00001077/// \brief Determine whether the given type T is a simple-template-id type.
1078static bool isSimpleTemplateIdType(QualType T) {
Mike Stump1eb44332009-09-09 15:08:12 +00001079 if (const TemplateSpecializationType *Spec
John McCall183700f2009-09-21 23:43:11 +00001080 = T->getAs<TemplateSpecializationType>())
Douglas Gregor41128772009-06-26 23:27:24 +00001081 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Douglas Gregor41128772009-06-26 23:27:24 +00001083 return false;
1084}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001085
1086/// \brief Substitute the explicitly-provided template arguments into the
1087/// given function template according to C++ [temp.arg.explicit].
1088///
1089/// \param FunctionTemplate the function template into which the explicit
1090/// template arguments will be substituted.
1091///
Mike Stump1eb44332009-09-09 15:08:12 +00001092/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001093/// arguments.
1094///
Mike Stump1eb44332009-09-09 15:08:12 +00001095/// \param NumExplicitTemplateArguments the number of explicitly-specified
Douglas Gregor83314aa2009-07-08 20:55:45 +00001096/// template arguments in @p ExplicitTemplateArguments. This value may be zero.
1097///
Mike Stump1eb44332009-09-09 15:08:12 +00001098/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor83314aa2009-07-08 20:55:45 +00001099/// with the converted and checked explicit template arguments.
1100///
Mike Stump1eb44332009-09-09 15:08:12 +00001101/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor83314aa2009-07-08 20:55:45 +00001102/// parameters.
1103///
1104/// \param FunctionType if non-NULL, the result type of the function template
1105/// will also be instantiated and the pointed-to value will be updated with
1106/// the instantiated function type.
1107///
1108/// \param Info if substitution fails for any reason, this object will be
1109/// populated with more information about the failure.
1110///
1111/// \returns TDK_Success if substitution was successful, or some failure
1112/// condition.
1113Sema::TemplateDeductionResult
1114Sema::SubstituteExplicitTemplateArguments(
1115 FunctionTemplateDecl *FunctionTemplate,
John McCall833ca992009-10-29 08:12:44 +00001116 const TemplateArgumentLoc *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001117 unsigned NumExplicitTemplateArgs,
1118 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1119 llvm::SmallVectorImpl<QualType> &ParamTypes,
1120 QualType *FunctionType,
1121 TemplateDeductionInfo &Info) {
1122 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1123 TemplateParameterList *TemplateParams
1124 = FunctionTemplate->getTemplateParameters();
1125
1126 if (NumExplicitTemplateArgs == 0) {
1127 // No arguments to substitute; just copy over the parameter types and
1128 // fill in the function type.
1129 for (FunctionDecl::param_iterator P = Function->param_begin(),
1130 PEnd = Function->param_end();
1131 P != PEnd;
1132 ++P)
1133 ParamTypes.push_back((*P)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Douglas Gregor83314aa2009-07-08 20:55:45 +00001135 if (FunctionType)
1136 *FunctionType = Function->getType();
1137 return TDK_Success;
1138 }
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Douglas Gregor83314aa2009-07-08 20:55:45 +00001140 // Substitution of the explicit template arguments into a function template
1141 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001142 SFINAETrap Trap(*this);
1143
Douglas Gregor83314aa2009-07-08 20:55:45 +00001144 // C++ [temp.arg.explicit]p3:
Mike Stump1eb44332009-09-09 15:08:12 +00001145 // Template arguments that are present shall be specified in the
1146 // declaration order of their corresponding template-parameters. The
Douglas Gregor83314aa2009-07-08 20:55:45 +00001147 // template argument list shall not specify more template-arguments than
Mike Stump1eb44332009-09-09 15:08:12 +00001148 // there are corresponding template-parameters.
1149 TemplateArgumentListBuilder Builder(TemplateParams,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001150 NumExplicitTemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001151
1152 // Enter a new template instantiation context where we check the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001153 // explicitly-specified template arguments against this function template,
1154 // and then substitute them into the function parameter types.
Mike Stump1eb44332009-09-09 15:08:12 +00001155 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001156 FunctionTemplate, Deduced.data(), Deduced.size(),
1157 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution);
1158 if (Inst)
1159 return TDK_InstantiationDepth;
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Douglas Gregor83314aa2009-07-08 20:55:45 +00001161 if (CheckTemplateArgumentList(FunctionTemplate,
1162 SourceLocation(), SourceLocation(),
1163 ExplicitTemplateArgs,
1164 NumExplicitTemplateArgs,
1165 SourceLocation(),
1166 true,
1167 Builder) || Trap.hasErrorOccurred())
1168 return TDK_InvalidExplicitArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001169
Douglas Gregor83314aa2009-07-08 20:55:45 +00001170 // Form the template argument list from the explicitly-specified
1171 // template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001172 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor83314aa2009-07-08 20:55:45 +00001173 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1174 Info.reset(ExplicitArgumentList);
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Douglas Gregor83314aa2009-07-08 20:55:45 +00001176 // Instantiate the types of each of the function parameters given the
1177 // explicitly-specified template arguments.
1178 for (FunctionDecl::param_iterator P = Function->param_begin(),
1179 PEnd = Function->param_end();
1180 P != PEnd;
1181 ++P) {
Mike Stump1eb44332009-09-09 15:08:12 +00001182 QualType ParamType
1183 = SubstType((*P)->getType(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001184 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1185 (*P)->getLocation(), (*P)->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001186 if (ParamType.isNull() || Trap.hasErrorOccurred())
1187 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Douglas Gregor83314aa2009-07-08 20:55:45 +00001189 ParamTypes.push_back(ParamType);
1190 }
1191
1192 // If the caller wants a full function type back, instantiate the return
1193 // type and form that function type.
1194 if (FunctionType) {
1195 // FIXME: exception-specifications?
Mike Stump1eb44332009-09-09 15:08:12 +00001196 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001197 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001198 assert(Proto && "Function template does not have a prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001199
1200 QualType ResultType
Douglas Gregor357bbd02009-08-28 20:50:45 +00001201 = SubstType(Proto->getResultType(),
1202 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1203 Function->getTypeSpecStartLoc(),
1204 Function->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001205 if (ResultType.isNull() || Trap.hasErrorOccurred())
1206 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001207
1208 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001209 ParamTypes.data(), ParamTypes.size(),
1210 Proto->isVariadic(),
1211 Proto->getTypeQuals(),
1212 Function->getLocation(),
1213 Function->getDeclName());
1214 if (FunctionType->isNull() || Trap.hasErrorOccurred())
1215 return TDK_SubstitutionFailure;
1216 }
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Douglas Gregor83314aa2009-07-08 20:55:45 +00001218 // C++ [temp.arg.explicit]p2:
Mike Stump1eb44332009-09-09 15:08:12 +00001219 // Trailing template arguments that can be deduced (14.8.2) may be
1220 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001221 // template arguments can be deduced, they may all be omitted; in this
1222 // case, the empty template argument list <> itself may also be omitted.
1223 //
1224 // Take all of the explicitly-specified arguments and put them into the
Mike Stump1eb44332009-09-09 15:08:12 +00001225 // set of deduced template arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001226 Deduced.reserve(TemplateParams->size());
1227 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001228 Deduced.push_back(ExplicitArgumentList->get(I));
1229
Douglas Gregor83314aa2009-07-08 20:55:45 +00001230 return TDK_Success;
1231}
1232
Mike Stump1eb44332009-09-09 15:08:12 +00001233/// \brief Finish template argument deduction for a function template,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001234/// checking the deduced template arguments for completeness and forming
1235/// the function template specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00001236Sema::TemplateDeductionResult
Douglas Gregor83314aa2009-07-08 20:55:45 +00001237Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
1238 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1239 FunctionDecl *&Specialization,
1240 TemplateDeductionInfo &Info) {
1241 TemplateParameterList *TemplateParams
1242 = FunctionTemplate->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Douglas Gregor83314aa2009-07-08 20:55:45 +00001244 // C++ [temp.deduct.type]p2:
1245 // [...] or if any template argument remains neither deduced nor
1246 // explicitly specified, template argument deduction fails.
1247 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
1248 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
1249 if (Deduced[I].isNull()) {
1250 Info.Param = makeTemplateParameter(
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001251 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregor83314aa2009-07-08 20:55:45 +00001252 return TDK_Incomplete;
1253 }
Mike Stump1eb44332009-09-09 15:08:12 +00001254
Douglas Gregor83314aa2009-07-08 20:55:45 +00001255 Builder.Append(Deduced[I]);
1256 }
Mike Stump1eb44332009-09-09 15:08:12 +00001257
Douglas Gregor83314aa2009-07-08 20:55:45 +00001258 // Form the template argument list from the deduced template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001259 TemplateArgumentList *DeducedArgumentList
Douglas Gregor83314aa2009-07-08 20:55:45 +00001260 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1261 Info.reset(DeducedArgumentList);
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Douglas Gregor83314aa2009-07-08 20:55:45 +00001263 // Template argument deduction for function templates in a SFINAE context.
1264 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001265 SFINAETrap Trap(*this);
1266
Douglas Gregor83314aa2009-07-08 20:55:45 +00001267 // Enter a new template instantiation context while we instantiate the
1268 // actual function declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001269 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001270 FunctionTemplate, Deduced.data(), Deduced.size(),
1271 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
1272 if (Inst)
Mike Stump1eb44332009-09-09 15:08:12 +00001273 return TDK_InstantiationDepth;
1274
1275 // Substitute the deduced template arguments into the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001276 // declaration to produce the function template specialization.
1277 Specialization = cast_or_null<FunctionDecl>(
John McCallce3ff2b2009-08-25 22:02:44 +00001278 SubstDecl(FunctionTemplate->getTemplatedDecl(),
1279 FunctionTemplate->getDeclContext(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001280 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor83314aa2009-07-08 20:55:45 +00001281 if (!Specialization)
1282 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001283
Douglas Gregorf8825742009-09-15 18:26:13 +00001284 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
1285 FunctionTemplate->getCanonicalDecl());
1286
Mike Stump1eb44332009-09-09 15:08:12 +00001287 // If the template argument list is owned by the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001288 // specialization, release it.
1289 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList)
1290 Info.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Douglas Gregor83314aa2009-07-08 20:55:45 +00001292 // There may have been an error that did not prevent us from constructing a
1293 // declaration. Mark the declaration invalid and return with a substitution
1294 // failure.
1295 if (Trap.hasErrorOccurred()) {
1296 Specialization->setInvalidDecl(true);
1297 return TDK_SubstitutionFailure;
1298 }
Mike Stump1eb44332009-09-09 15:08:12 +00001299
1300 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001301}
1302
Douglas Gregore53060f2009-06-25 22:08:12 +00001303/// \brief Perform template argument deduction from a function call
1304/// (C++ [temp.deduct.call]).
1305///
1306/// \param FunctionTemplate the function template for which we are performing
1307/// template argument deduction.
1308///
Mike Stump1eb44332009-09-09 15:08:12 +00001309/// \param HasExplicitTemplateArgs whether any template arguments were
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001310/// explicitly specified.
1311///
1312/// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
1313/// the explicitly-specified template arguments.
1314///
1315/// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
Mike Stump1eb44332009-09-09 15:08:12 +00001316/// the number of explicitly-specified template arguments in
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001317/// @p ExplicitTemplateArguments. This value may be zero.
1318///
Douglas Gregore53060f2009-06-25 22:08:12 +00001319/// \param Args the function call arguments
1320///
1321/// \param NumArgs the number of arguments in Args
1322///
1323/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001324/// this will be set to the function template specialization produced by
Douglas Gregore53060f2009-06-25 22:08:12 +00001325/// template argument deduction.
1326///
1327/// \param Info the argument will be updated to provide additional information
1328/// about template argument deduction.
1329///
1330/// \returns the result of template argument deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001331Sema::TemplateDeductionResult
1332Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001333 bool HasExplicitTemplateArgs,
John McCall833ca992009-10-29 08:12:44 +00001334 const TemplateArgumentLoc *ExplicitTemplateArgs,
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001335 unsigned NumExplicitTemplateArgs,
Douglas Gregore53060f2009-06-25 22:08:12 +00001336 Expr **Args, unsigned NumArgs,
1337 FunctionDecl *&Specialization,
1338 TemplateDeductionInfo &Info) {
1339 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001340
Douglas Gregore53060f2009-06-25 22:08:12 +00001341 // C++ [temp.deduct.call]p1:
1342 // Template argument deduction is done by comparing each function template
1343 // parameter type (call it P) with the type of the corresponding argument
1344 // of the call (call it A) as described below.
1345 unsigned CheckArgs = NumArgs;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001346 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregore53060f2009-06-25 22:08:12 +00001347 return TDK_TooFewArguments;
1348 else if (NumArgs > Function->getNumParams()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001349 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001350 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregore53060f2009-06-25 22:08:12 +00001351 if (!Proto->isVariadic())
1352 return TDK_TooManyArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Douglas Gregore53060f2009-06-25 22:08:12 +00001354 CheckArgs = Function->getNumParams();
1355 }
Mike Stump1eb44332009-09-09 15:08:12 +00001356
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001357 // The types of the parameters from which we will perform template argument
1358 // deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001359 TemplateParameterList *TemplateParams
1360 = FunctionTemplate->getTemplateParameters();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001361 llvm::SmallVector<TemplateArgument, 4> Deduced;
1362 llvm::SmallVector<QualType, 4> ParamTypes;
1363 if (NumExplicitTemplateArgs) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001364 TemplateDeductionResult Result =
1365 SubstituteExplicitTemplateArguments(FunctionTemplate,
1366 ExplicitTemplateArgs,
1367 NumExplicitTemplateArgs,
1368 Deduced,
1369 ParamTypes,
1370 0,
1371 Info);
1372 if (Result)
1373 return Result;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001374 } else {
1375 // Just fill in the parameter types from the function declaration.
1376 for (unsigned I = 0; I != CheckArgs; ++I)
1377 ParamTypes.push_back(Function->getParamDecl(I)->getType());
1378 }
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001380 // Deduce template arguments from the function parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001381 Deduced.resize(TemplateParams->size());
Douglas Gregore53060f2009-06-25 22:08:12 +00001382 for (unsigned I = 0; I != CheckArgs; ++I) {
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001383 QualType ParamType = ParamTypes[I];
Douglas Gregore53060f2009-06-25 22:08:12 +00001384 QualType ArgType = Args[I]->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Douglas Gregore53060f2009-06-25 22:08:12 +00001386 // C++ [temp.deduct.call]p2:
1387 // If P is not a reference type:
1388 QualType CanonParamType = Context.getCanonicalType(ParamType);
Douglas Gregor500d3312009-06-26 18:27:22 +00001389 bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1390 if (!ParamWasReference) {
Mike Stump1eb44332009-09-09 15:08:12 +00001391 // - If A is an array type, the pointer type produced by the
1392 // array-to-pointer standard conversion (4.2) is used in place of
Douglas Gregore53060f2009-06-25 22:08:12 +00001393 // A for type deduction; otherwise,
1394 if (ArgType->isArrayType())
1395 ArgType = Context.getArrayDecayedType(ArgType);
Mike Stump1eb44332009-09-09 15:08:12 +00001396 // - If A is a function type, the pointer type produced by the
1397 // function-to-pointer standard conversion (4.3) is used in place
Douglas Gregore53060f2009-06-25 22:08:12 +00001398 // of A for type deduction; otherwise,
1399 else if (ArgType->isFunctionType())
1400 ArgType = Context.getPointerType(ArgType);
1401 else {
1402 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1403 // type are ignored for type deduction.
1404 QualType CanonArgType = Context.getCanonicalType(ArgType);
1405 if (CanonArgType.getCVRQualifiers())
1406 ArgType = CanonArgType.getUnqualifiedType();
1407 }
1408 }
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Douglas Gregore53060f2009-06-25 22:08:12 +00001410 // C++0x [temp.deduct.call]p3:
1411 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
Mike Stump1eb44332009-09-09 15:08:12 +00001412 // are ignored for type deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001413 if (CanonParamType.getCVRQualifiers())
1414 ParamType = CanonParamType.getUnqualifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001415 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001416 // [...] If P is a reference type, the type referred to by P is used
1417 // for type deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001418 ParamType = ParamRefType->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00001419
1420 // [...] If P is of the form T&&, where T is a template parameter, and
1421 // the argument is an lvalue, the type A& is used in place of A for
Douglas Gregore53060f2009-06-25 22:08:12 +00001422 // type deduction.
1423 if (isa<RValueReferenceType>(ParamRefType) &&
John McCall183700f2009-09-21 23:43:11 +00001424 ParamRefType->getAs<TemplateTypeParmType>() &&
Douglas Gregore53060f2009-06-25 22:08:12 +00001425 Args[I]->isLvalue(Context) == Expr::LV_Valid)
1426 ArgType = Context.getLValueReferenceType(ArgType);
1427 }
Mike Stump1eb44332009-09-09 15:08:12 +00001428
Douglas Gregore53060f2009-06-25 22:08:12 +00001429 // C++0x [temp.deduct.call]p4:
1430 // In general, the deduction process attempts to find template argument
1431 // values that will make the deduced A identical to A (after the type A
1432 // is transformed as described above). [...]
Douglas Gregor12820292009-09-14 20:00:47 +00001433 unsigned TDF = TDF_SkipNonDependent;
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Douglas Gregor508f1c82009-06-26 23:10:12 +00001435 // - If the original P is a reference type, the deduced A (i.e., the
1436 // type referred to by the reference) can be more cv-qualified than
1437 // the transformed A.
1438 if (ParamWasReference)
1439 TDF |= TDF_ParamWithReferenceType;
Mike Stump1eb44332009-09-09 15:08:12 +00001440 // - The transformed A can be another pointer or pointer to member
1441 // type that can be converted to the deduced A via a qualification
Douglas Gregor508f1c82009-06-26 23:10:12 +00001442 // conversion (4.4).
1443 if (ArgType->isPointerType() || ArgType->isMemberPointerType())
1444 TDF |= TDF_IgnoreQualifiers;
Mike Stump1eb44332009-09-09 15:08:12 +00001445 // - If P is a class and P has the form simple-template-id, then the
Douglas Gregor41128772009-06-26 23:27:24 +00001446 // transformed A can be a derived class of the deduced A. Likewise,
1447 // if P is a pointer to a class of the form simple-template-id, the
1448 // transformed A can be a pointer to a derived class pointed to by
1449 // the deduced A.
1450 if (isSimpleTemplateIdType(ParamType) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001451 (isa<PointerType>(ParamType) &&
Douglas Gregor41128772009-06-26 23:27:24 +00001452 isSimpleTemplateIdType(
Ted Kremenek6217b802009-07-29 21:53:49 +00001453 ParamType->getAs<PointerType>()->getPointeeType())))
Douglas Gregor41128772009-06-26 23:27:24 +00001454 TDF |= TDF_DerivedClass;
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Douglas Gregore53060f2009-06-25 22:08:12 +00001456 if (TemplateDeductionResult Result
1457 = ::DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor500d3312009-06-26 18:27:22 +00001458 ParamType, ArgType, Info, Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +00001459 TDF))
Douglas Gregore53060f2009-06-25 22:08:12 +00001460 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001461
Douglas Gregor8fdc3c42009-07-07 23:12:18 +00001462 // FIXME: C++0x [temp.deduct.call] paragraphs 6-9 deal with function
Mike Stump1eb44332009-09-09 15:08:12 +00001463 // pointer parameters.
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001464
1465 // FIXME: we need to check that the deduced A is the same as A,
1466 // modulo the various allowed differences.
Douglas Gregore53060f2009-06-25 22:08:12 +00001467 }
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001468
Mike Stump1eb44332009-09-09 15:08:12 +00001469 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001470 Specialization, Info);
Douglas Gregore53060f2009-06-25 22:08:12 +00001471}
1472
Douglas Gregor83314aa2009-07-08 20:55:45 +00001473/// \brief Deduce template arguments when taking the address of a function
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001474/// template (C++ [temp.deduct.funcaddr]) or matching a
Douglas Gregor83314aa2009-07-08 20:55:45 +00001475///
1476/// \param FunctionTemplate the function template for which we are performing
1477/// template argument deduction.
1478///
Mike Stump1eb44332009-09-09 15:08:12 +00001479/// \param HasExplicitTemplateArgs whether any template arguments were
Douglas Gregor83314aa2009-07-08 20:55:45 +00001480/// explicitly specified.
1481///
1482/// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
1483/// the explicitly-specified template arguments.
1484///
1485/// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
Mike Stump1eb44332009-09-09 15:08:12 +00001486/// the number of explicitly-specified template arguments in
Douglas Gregor83314aa2009-07-08 20:55:45 +00001487/// @p ExplicitTemplateArguments. This value may be zero.
1488///
1489/// \param ArgFunctionType the function type that will be used as the
1490/// "argument" type (A) when performing template argument deduction from the
1491/// function template's function type.
1492///
1493/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001494/// this will be set to the function template specialization produced by
Douglas Gregor83314aa2009-07-08 20:55:45 +00001495/// template argument deduction.
1496///
1497/// \param Info the argument will be updated to provide additional information
1498/// about template argument deduction.
1499///
1500/// \returns the result of template argument deduction.
1501Sema::TemplateDeductionResult
1502Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1503 bool HasExplicitTemplateArgs,
John McCall833ca992009-10-29 08:12:44 +00001504 const TemplateArgumentLoc *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001505 unsigned NumExplicitTemplateArgs,
1506 QualType ArgFunctionType,
1507 FunctionDecl *&Specialization,
1508 TemplateDeductionInfo &Info) {
1509 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1510 TemplateParameterList *TemplateParams
1511 = FunctionTemplate->getTemplateParameters();
1512 QualType FunctionType = Function->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Douglas Gregor83314aa2009-07-08 20:55:45 +00001514 // Substitute any explicit template arguments.
1515 llvm::SmallVector<TemplateArgument, 4> Deduced;
1516 llvm::SmallVector<QualType, 4> ParamTypes;
1517 if (HasExplicitTemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001518 if (TemplateDeductionResult Result
1519 = SubstituteExplicitTemplateArguments(FunctionTemplate,
1520 ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001521 NumExplicitTemplateArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00001522 Deduced, ParamTypes,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001523 &FunctionType, Info))
1524 return Result;
1525 }
1526
1527 // Template argument deduction for function templates in a SFINAE context.
1528 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001529 SFINAETrap Trap(*this);
1530
Douglas Gregor83314aa2009-07-08 20:55:45 +00001531 // Deduce template arguments from the function type.
Mike Stump1eb44332009-09-09 15:08:12 +00001532 Deduced.resize(TemplateParams->size());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001533 if (TemplateDeductionResult Result
1534 = ::DeduceTemplateArguments(Context, TemplateParams,
Mike Stump1eb44332009-09-09 15:08:12 +00001535 FunctionType, ArgFunctionType, Info,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001536 Deduced, 0))
1537 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001538
1539 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001540 Specialization, Info);
1541}
1542
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001543/// \brief Deduce template arguments for a templated conversion
1544/// function (C++ [temp.deduct.conv]) and, if successful, produce a
1545/// conversion function template specialization.
1546Sema::TemplateDeductionResult
1547Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1548 QualType ToType,
1549 CXXConversionDecl *&Specialization,
1550 TemplateDeductionInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00001551 CXXConversionDecl *Conv
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001552 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
1553 QualType FromType = Conv->getConversionType();
1554
1555 // Canonicalize the types for deduction.
1556 QualType P = Context.getCanonicalType(FromType);
1557 QualType A = Context.getCanonicalType(ToType);
1558
1559 // C++0x [temp.deduct.conv]p3:
1560 // If P is a reference type, the type referred to by P is used for
1561 // type deduction.
1562 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
1563 P = PRef->getPointeeType();
1564
1565 // C++0x [temp.deduct.conv]p3:
1566 // If A is a reference type, the type referred to by A is used
1567 // for type deduction.
1568 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
1569 A = ARef->getPointeeType();
1570 // C++ [temp.deduct.conv]p2:
1571 //
Mike Stump1eb44332009-09-09 15:08:12 +00001572 // If A is not a reference type:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001573 else {
1574 assert(!A->isReferenceType() && "Reference types were handled above");
1575
1576 // - If P is an array type, the pointer type produced by the
Mike Stump1eb44332009-09-09 15:08:12 +00001577 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001578 // of P for type deduction; otherwise,
1579 if (P->isArrayType())
1580 P = Context.getArrayDecayedType(P);
1581 // - If P is a function type, the pointer type produced by the
1582 // function-to-pointer standard conversion (4.3) is used in
1583 // place of P for type deduction; otherwise,
1584 else if (P->isFunctionType())
1585 P = Context.getPointerType(P);
1586 // - If P is a cv-qualified type, the top level cv-qualifiers of
1587 // P’s type are ignored for type deduction.
1588 else
1589 P = P.getUnqualifiedType();
1590
1591 // C++0x [temp.deduct.conv]p3:
1592 // If A is a cv-qualified type, the top level cv-qualifiers of A’s
1593 // type are ignored for type deduction.
1594 A = A.getUnqualifiedType();
1595 }
1596
1597 // Template argument deduction for function templates in a SFINAE context.
1598 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001599 SFINAETrap Trap(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001600
1601 // C++ [temp.deduct.conv]p1:
1602 // Template argument deduction is done by comparing the return
1603 // type of the template conversion function (call it P) with the
1604 // type that is required as the result of the conversion (call it
1605 // A) as described in 14.8.2.4.
1606 TemplateParameterList *TemplateParams
1607 = FunctionTemplate->getTemplateParameters();
1608 llvm::SmallVector<TemplateArgument, 4> Deduced;
Mike Stump1eb44332009-09-09 15:08:12 +00001609 Deduced.resize(TemplateParams->size());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001610
1611 // C++0x [temp.deduct.conv]p4:
1612 // In general, the deduction process attempts to find template
1613 // argument values that will make the deduced A identical to
1614 // A. However, there are two cases that allow a difference:
1615 unsigned TDF = 0;
1616 // - If the original A is a reference type, A can be more
1617 // cv-qualified than the deduced A (i.e., the type referred to
1618 // by the reference)
1619 if (ToType->isReferenceType())
1620 TDF |= TDF_ParamWithReferenceType;
1621 // - The deduced A can be another pointer or pointer to member
1622 // type that can be converted to A via a qualification
1623 // conversion.
1624 //
1625 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
1626 // both P and A are pointers or member pointers. In this case, we
1627 // just ignore cv-qualifiers completely).
1628 if ((P->isPointerType() && A->isPointerType()) ||
1629 (P->isMemberPointerType() && P->isMemberPointerType()))
1630 TDF |= TDF_IgnoreQualifiers;
1631 if (TemplateDeductionResult Result
1632 = ::DeduceTemplateArguments(Context, TemplateParams,
1633 P, A, Info, Deduced, TDF))
1634 return Result;
1635
1636 // FIXME: we need to check that the deduced A is the same as A,
1637 // modulo the various allowed differences.
Mike Stump1eb44332009-09-09 15:08:12 +00001638
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001639 // Finish template argument deduction.
1640 FunctionDecl *Spec = 0;
1641 TemplateDeductionResult Result
1642 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, Spec, Info);
1643 Specialization = cast_or_null<CXXConversionDecl>(Spec);
1644 return Result;
1645}
1646
Douglas Gregor8a514912009-09-14 18:39:43 +00001647/// \brief Stores the result of comparing the qualifiers of two types.
1648enum DeductionQualifierComparison {
1649 NeitherMoreQualified = 0,
1650 ParamMoreQualified,
1651 ArgMoreQualified
1652};
1653
1654/// \brief Deduce the template arguments during partial ordering by comparing
1655/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
1656///
1657/// \param Context the AST context in which this deduction occurs.
1658///
1659/// \param TemplateParams the template parameters that we are deducing
1660///
1661/// \param ParamIn the parameter type
1662///
1663/// \param ArgIn the argument type
1664///
1665/// \param Info information about the template argument deduction itself
1666///
1667/// \param Deduced the deduced template arguments
1668///
1669/// \returns the result of template argument deduction so far. Note that a
1670/// "success" result means that template argument deduction has not yet failed,
1671/// but it may still fail, later, for other reasons.
1672static Sema::TemplateDeductionResult
1673DeduceTemplateArgumentsDuringPartialOrdering(ASTContext &Context,
1674 TemplateParameterList *TemplateParams,
1675 QualType ParamIn, QualType ArgIn,
1676 Sema::TemplateDeductionInfo &Info,
1677 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1678 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1679 CanQualType Param = Context.getCanonicalType(ParamIn);
1680 CanQualType Arg = Context.getCanonicalType(ArgIn);
1681
1682 // C++0x [temp.deduct.partial]p5:
1683 // Before the partial ordering is done, certain transformations are
1684 // performed on the types used for partial ordering:
1685 // - If P is a reference type, P is replaced by the type referred to.
1686 CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00001687 if (!ParamRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00001688 Param = ParamRef->getPointeeType();
1689
1690 // - If A is a reference type, A is replaced by the type referred to.
1691 CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00001692 if (!ArgRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00001693 Arg = ArgRef->getPointeeType();
1694
John McCalle27ec8a2009-10-23 23:03:21 +00001695 if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
Douglas Gregor8a514912009-09-14 18:39:43 +00001696 // C++0x [temp.deduct.partial]p6:
1697 // If both P and A were reference types (before being replaced with the
1698 // type referred to above), determine which of the two types (if any) is
1699 // more cv-qualified than the other; otherwise the types are considered to
1700 // be equally cv-qualified for partial ordering purposes. The result of this
1701 // determination will be used below.
1702 //
1703 // We save this information for later, using it only when deduction
1704 // succeeds in both directions.
1705 DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
1706 if (Param.isMoreQualifiedThan(Arg))
1707 QualifierResult = ParamMoreQualified;
1708 else if (Arg.isMoreQualifiedThan(Param))
1709 QualifierResult = ArgMoreQualified;
1710 QualifierComparisons->push_back(QualifierResult);
1711 }
1712
1713 // C++0x [temp.deduct.partial]p7:
1714 // Remove any top-level cv-qualifiers:
1715 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
1716 // version of P.
1717 Param = Param.getUnqualifiedType();
1718 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
1719 // version of A.
1720 Arg = Arg.getUnqualifiedType();
1721
1722 // C++0x [temp.deduct.partial]p8:
1723 // Using the resulting types P and A the deduction is then done as
1724 // described in 14.9.2.5. If deduction succeeds for a given type, the type
1725 // from the argument template is considered to be at least as specialized
1726 // as the type from the parameter template.
1727 return DeduceTemplateArguments(Context, TemplateParams, Param, Arg, Info,
1728 Deduced, TDF_None);
1729}
1730
1731static void
Douglas Gregore73bb602009-09-14 21:25:05 +00001732MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
1733 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001734 unsigned Level,
Douglas Gregore73bb602009-09-14 21:25:05 +00001735 llvm::SmallVectorImpl<bool> &Deduced);
Douglas Gregor8a514912009-09-14 18:39:43 +00001736
1737/// \brief Determine whether the function template \p FT1 is at least as
1738/// specialized as \p FT2.
1739static bool isAtLeastAsSpecializedAs(Sema &S,
1740 FunctionTemplateDecl *FT1,
1741 FunctionTemplateDecl *FT2,
1742 TemplatePartialOrderingContext TPOC,
1743 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1744 FunctionDecl *FD1 = FT1->getTemplatedDecl();
1745 FunctionDecl *FD2 = FT2->getTemplatedDecl();
1746 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
1747 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
1748
1749 assert(Proto1 && Proto2 && "Function templates must have prototypes");
1750 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
1751 llvm::SmallVector<TemplateArgument, 4> Deduced;
1752 Deduced.resize(TemplateParams->size());
1753
1754 // C++0x [temp.deduct.partial]p3:
1755 // The types used to determine the ordering depend on the context in which
1756 // the partial ordering is done:
1757 Sema::TemplateDeductionInfo Info(S.Context);
1758 switch (TPOC) {
1759 case TPOC_Call: {
1760 // - In the context of a function call, the function parameter types are
1761 // used.
1762 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
1763 for (unsigned I = 0; I != NumParams; ++I)
1764 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1765 TemplateParams,
1766 Proto2->getArgType(I),
1767 Proto1->getArgType(I),
1768 Info,
1769 Deduced,
1770 QualifierComparisons))
1771 return false;
1772
1773 break;
1774 }
1775
1776 case TPOC_Conversion:
1777 // - In the context of a call to a conversion operator, the return types
1778 // of the conversion function templates are used.
1779 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1780 TemplateParams,
1781 Proto2->getResultType(),
1782 Proto1->getResultType(),
1783 Info,
1784 Deduced,
1785 QualifierComparisons))
1786 return false;
1787 break;
1788
1789 case TPOC_Other:
1790 // - In other contexts (14.6.6.2) the function template’s function type
1791 // is used.
1792 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1793 TemplateParams,
1794 FD2->getType(),
1795 FD1->getType(),
1796 Info,
1797 Deduced,
1798 QualifierComparisons))
1799 return false;
1800 break;
1801 }
1802
1803 // C++0x [temp.deduct.partial]p11:
1804 // In most cases, all template parameters must have values in order for
1805 // deduction to succeed, but for partial ordering purposes a template
1806 // parameter may remain without a value provided it is not used in the
1807 // types being used for partial ordering. [ Note: a template parameter used
1808 // in a non-deduced context is considered used. -end note]
1809 unsigned ArgIdx = 0, NumArgs = Deduced.size();
1810 for (; ArgIdx != NumArgs; ++ArgIdx)
1811 if (Deduced[ArgIdx].isNull())
1812 break;
1813
1814 if (ArgIdx == NumArgs) {
1815 // All template arguments were deduced. FT1 is at least as specialized
1816 // as FT2.
1817 return true;
1818 }
1819
Douglas Gregore73bb602009-09-14 21:25:05 +00001820 // Figure out which template parameters were used.
Douglas Gregor8a514912009-09-14 18:39:43 +00001821 llvm::SmallVector<bool, 4> UsedParameters;
1822 UsedParameters.resize(TemplateParams->size());
1823 switch (TPOC) {
1824 case TPOC_Call: {
1825 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
1826 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00001827 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
1828 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00001829 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00001830 break;
1831 }
1832
1833 case TPOC_Conversion:
Douglas Gregored9c0f92009-10-29 00:04:11 +00001834 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
1835 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00001836 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00001837 break;
1838
1839 case TPOC_Other:
Douglas Gregored9c0f92009-10-29 00:04:11 +00001840 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
1841 TemplateParams->getDepth(),
1842 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00001843 break;
1844 }
1845
1846 for (; ArgIdx != NumArgs; ++ArgIdx)
1847 // If this argument had no value deduced but was used in one of the types
1848 // used for partial ordering, then deduction fails.
1849 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
1850 return false;
1851
1852 return true;
1853}
1854
1855
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001856/// \brief Returns the more specialized function template according
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001857/// to the rules of function template partial ordering (C++ [temp.func.order]).
1858///
1859/// \param FT1 the first function template
1860///
1861/// \param FT2 the second function template
1862///
Douglas Gregor8a514912009-09-14 18:39:43 +00001863/// \param TPOC the context in which we are performing partial ordering of
1864/// function templates.
Mike Stump1eb44332009-09-09 15:08:12 +00001865///
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001866/// \returns the more specialized function template. If neither
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001867/// template is more specialized, returns NULL.
1868FunctionTemplateDecl *
1869Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
1870 FunctionTemplateDecl *FT2,
Douglas Gregor8a514912009-09-14 18:39:43 +00001871 TemplatePartialOrderingContext TPOC) {
Douglas Gregor8a514912009-09-14 18:39:43 +00001872 llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
1873 bool Better1 = isAtLeastAsSpecializedAs(*this, FT1, FT2, TPOC, 0);
1874 bool Better2 = isAtLeastAsSpecializedAs(*this, FT2, FT1, TPOC,
1875 &QualifierComparisons);
1876
1877 if (Better1 != Better2) // We have a clear winner
1878 return Better1? FT1 : FT2;
1879
1880 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001881 return 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00001882
1883
1884 // C++0x [temp.deduct.partial]p10:
1885 // If for each type being considered a given template is at least as
1886 // specialized for all types and more specialized for some set of types and
1887 // the other template is not more specialized for any types or is not at
1888 // least as specialized for any types, then the given template is more
1889 // specialized than the other template. Otherwise, neither template is more
1890 // specialized than the other.
1891 Better1 = false;
1892 Better2 = false;
1893 for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
1894 // C++0x [temp.deduct.partial]p9:
1895 // If, for a given type, deduction succeeds in both directions (i.e., the
1896 // types are identical after the transformations above) and if the type
1897 // from the argument template is more cv-qualified than the type from the
1898 // parameter template (as described above) that type is considered to be
1899 // more specialized than the other. If neither type is more cv-qualified
1900 // than the other then neither type is more specialized than the other.
1901 switch (QualifierComparisons[I]) {
1902 case NeitherMoreQualified:
1903 break;
1904
1905 case ParamMoreQualified:
1906 Better1 = true;
1907 if (Better2)
1908 return 0;
1909 break;
1910
1911 case ArgMoreQualified:
1912 Better2 = true;
1913 if (Better1)
1914 return 0;
1915 break;
1916 }
1917 }
1918
1919 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001920 if (Better1)
1921 return FT1;
Douglas Gregor8a514912009-09-14 18:39:43 +00001922 else if (Better2)
1923 return FT2;
1924 else
1925 return 0;
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001926}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001927
Douglas Gregord5a423b2009-09-25 18:43:00 +00001928/// \brief Determine if the two templates are equivalent.
1929static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
1930 if (T1 == T2)
1931 return true;
1932
1933 if (!T1 || !T2)
1934 return false;
1935
1936 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
1937}
1938
1939/// \brief Retrieve the most specialized of the given function template
1940/// specializations.
1941///
1942/// \param Specializations the set of function template specializations that
1943/// we will be comparing.
1944///
1945/// \param NumSpecializations the number of function template specializations in
1946/// \p Specializations
1947///
1948/// \param TPOC the partial ordering context to use to compare the function
1949/// template specializations.
1950///
1951/// \param Loc the location where the ambiguity or no-specializations
1952/// diagnostic should occur.
1953///
1954/// \param NoneDiag partial diagnostic used to diagnose cases where there are
1955/// no matching candidates.
1956///
1957/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
1958/// occurs.
1959///
1960/// \param CandidateDiag partial diagnostic used for each function template
1961/// specialization that is a candidate in the ambiguous ordering. One parameter
1962/// in this diagnostic should be unbound, which will correspond to the string
1963/// describing the template arguments for the function template specialization.
1964///
1965/// \param Index if non-NULL and the result of this function is non-nULL,
1966/// receives the index corresponding to the resulting function template
1967/// specialization.
1968///
1969/// \returns the most specialized function template specialization, if
1970/// found. Otherwise, returns NULL.
1971///
1972/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
1973/// template argument deduction.
1974FunctionDecl *Sema::getMostSpecialized(FunctionDecl **Specializations,
1975 unsigned NumSpecializations,
1976 TemplatePartialOrderingContext TPOC,
1977 SourceLocation Loc,
1978 const PartialDiagnostic &NoneDiag,
1979 const PartialDiagnostic &AmbigDiag,
1980 const PartialDiagnostic &CandidateDiag,
1981 unsigned *Index) {
1982 if (NumSpecializations == 0) {
1983 Diag(Loc, NoneDiag);
1984 return 0;
1985 }
1986
1987 if (NumSpecializations == 1) {
1988 if (Index)
1989 *Index = 0;
1990
1991 return Specializations[0];
1992 }
1993
1994
1995 // Find the function template that is better than all of the templates it
1996 // has been compared to.
1997 unsigned Best = 0;
1998 FunctionTemplateDecl *BestTemplate
1999 = Specializations[Best]->getPrimaryTemplate();
2000 assert(BestTemplate && "Not a function template specialization?");
2001 for (unsigned I = 1; I != NumSpecializations; ++I) {
2002 FunctionTemplateDecl *Challenger = Specializations[I]->getPrimaryTemplate();
2003 assert(Challenger && "Not a function template specialization?");
2004 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
2005 TPOC),
2006 Challenger)) {
2007 Best = I;
2008 BestTemplate = Challenger;
2009 }
2010 }
2011
2012 // Make sure that the "best" function template is more specialized than all
2013 // of the others.
2014 bool Ambiguous = false;
2015 for (unsigned I = 0; I != NumSpecializations; ++I) {
2016 FunctionTemplateDecl *Challenger = Specializations[I]->getPrimaryTemplate();
2017 if (I != Best &&
2018 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
2019 TPOC),
2020 BestTemplate)) {
2021 Ambiguous = true;
2022 break;
2023 }
2024 }
2025
2026 if (!Ambiguous) {
2027 // We found an answer. Return it.
2028 if (Index)
2029 *Index = Best;
2030 return Specializations[Best];
2031 }
2032
2033 // Diagnose the ambiguity.
2034 Diag(Loc, AmbigDiag);
2035
2036 // FIXME: Can we order the candidates in some sane way?
2037 for (unsigned I = 0; I != NumSpecializations; ++I)
2038 Diag(Specializations[I]->getLocation(), CandidateDiag)
2039 << getTemplateArgumentBindingsText(
2040 Specializations[I]->getPrimaryTemplate()->getTemplateParameters(),
2041 *Specializations[I]->getTemplateSpecializationArgs());
2042
2043 return 0;
2044}
2045
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002046/// \brief Returns the more specialized class template partial specialization
2047/// according to the rules of partial ordering of class template partial
2048/// specializations (C++ [temp.class.order]).
2049///
2050/// \param PS1 the first class template partial specialization
2051///
2052/// \param PS2 the second class template partial specialization
2053///
2054/// \returns the more specialized class template partial specialization. If
2055/// neither partial specialization is more specialized, returns NULL.
2056ClassTemplatePartialSpecializationDecl *
2057Sema::getMoreSpecializedPartialSpecialization(
2058 ClassTemplatePartialSpecializationDecl *PS1,
2059 ClassTemplatePartialSpecializationDecl *PS2) {
2060 // C++ [temp.class.order]p1:
2061 // For two class template partial specializations, the first is at least as
2062 // specialized as the second if, given the following rewrite to two
2063 // function templates, the first function template is at least as
2064 // specialized as the second according to the ordering rules for function
2065 // templates (14.6.6.2):
2066 // - the first function template has the same template parameters as the
2067 // first partial specialization and has a single function parameter
2068 // whose type is a class template specialization with the template
2069 // arguments of the first partial specialization, and
2070 // - the second function template has the same template parameters as the
2071 // second partial specialization and has a single function parameter
2072 // whose type is a class template specialization with the template
2073 // arguments of the second partial specialization.
2074 //
2075 // Rather than synthesize function templates, we merely perform the
2076 // equivalent partial ordering by performing deduction directly on the
2077 // template arguments of the class template partial specializations. This
2078 // computation is slightly simpler than the general problem of function
2079 // template partial ordering, because class template partial specializations
2080 // are more constrained. We know that every template parameter is deduc
2081 llvm::SmallVector<TemplateArgument, 4> Deduced;
2082 Sema::TemplateDeductionInfo Info(Context);
2083
2084 // Determine whether PS1 is at least as specialized as PS2
2085 Deduced.resize(PS2->getTemplateParameters()->size());
2086 bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(Context,
2087 PS2->getTemplateParameters(),
2088 Context.getTypeDeclType(PS2),
2089 Context.getTypeDeclType(PS1),
2090 Info,
2091 Deduced,
2092 0);
2093
2094 // Determine whether PS2 is at least as specialized as PS1
2095 Deduced.resize(PS1->getTemplateParameters()->size());
2096 bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(Context,
2097 PS1->getTemplateParameters(),
2098 Context.getTypeDeclType(PS1),
2099 Context.getTypeDeclType(PS2),
2100 Info,
2101 Deduced,
2102 0);
2103
2104 if (Better1 == Better2)
2105 return 0;
2106
2107 return Better1? PS1 : PS2;
2108}
2109
Mike Stump1eb44332009-09-09 15:08:12 +00002110static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002111MarkUsedTemplateParameters(Sema &SemaRef,
2112 const TemplateArgument &TemplateArg,
2113 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002114 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002115 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002116
Douglas Gregore73bb602009-09-14 21:25:05 +00002117/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002118/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00002119static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002120MarkUsedTemplateParameters(Sema &SemaRef,
2121 const Expr *E,
2122 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002123 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002124 llvm::SmallVectorImpl<bool> &Used) {
2125 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2126 // find other occurrences of template parameters.
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002127 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregor031a5882009-06-13 00:26:55 +00002128 if (!E)
2129 return;
2130
Mike Stump1eb44332009-09-09 15:08:12 +00002131 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00002132 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2133 if (!NTTP)
2134 return;
2135
Douglas Gregored9c0f92009-10-29 00:04:11 +00002136 if (NTTP->getDepth() == Depth)
2137 Used[NTTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002138}
2139
Douglas Gregore73bb602009-09-14 21:25:05 +00002140/// \brief Mark the template parameters that are used by the given
2141/// nested name specifier.
2142static void
2143MarkUsedTemplateParameters(Sema &SemaRef,
2144 NestedNameSpecifier *NNS,
2145 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002146 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002147 llvm::SmallVectorImpl<bool> &Used) {
2148 if (!NNS)
2149 return;
2150
Douglas Gregored9c0f92009-10-29 00:04:11 +00002151 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2152 Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002153 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002154 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002155}
2156
2157/// \brief Mark the template parameters that are used by the given
2158/// template name.
2159static void
2160MarkUsedTemplateParameters(Sema &SemaRef,
2161 TemplateName Name,
2162 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002163 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002164 llvm::SmallVectorImpl<bool> &Used) {
2165 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2166 if (TemplateTemplateParmDecl *TTP
Douglas Gregored9c0f92009-10-29 00:04:11 +00002167 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2168 if (TTP->getDepth() == Depth)
2169 Used[TTP->getIndex()] = true;
2170 }
Douglas Gregore73bb602009-09-14 21:25:05 +00002171 return;
2172 }
2173
Douglas Gregor788cd062009-11-11 01:00:40 +00002174 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2175 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2176 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002177 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
Douglas Gregored9c0f92009-10-29 00:04:11 +00002178 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2179 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002180}
2181
2182/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002183/// type.
Mike Stump1eb44332009-09-09 15:08:12 +00002184static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002185MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2186 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002187 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002188 llvm::SmallVectorImpl<bool> &Used) {
2189 if (T.isNull())
2190 return;
2191
Douglas Gregor031a5882009-06-13 00:26:55 +00002192 // Non-dependent types have nothing deducible
2193 if (!T->isDependentType())
2194 return;
2195
2196 T = SemaRef.Context.getCanonicalType(T);
2197 switch (T->getTypeClass()) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002198 case Type::Pointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002199 MarkUsedTemplateParameters(SemaRef,
2200 cast<PointerType>(T)->getPointeeType(),
2201 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002202 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002203 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002204 break;
2205
2206 case Type::BlockPointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002207 MarkUsedTemplateParameters(SemaRef,
2208 cast<BlockPointerType>(T)->getPointeeType(),
2209 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002210 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002211 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002212 break;
2213
2214 case Type::LValueReference:
2215 case Type::RValueReference:
Douglas Gregore73bb602009-09-14 21:25:05 +00002216 MarkUsedTemplateParameters(SemaRef,
2217 cast<ReferenceType>(T)->getPointeeType(),
2218 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002219 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002220 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002221 break;
2222
2223 case Type::MemberPointer: {
2224 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore73bb602009-09-14 21:25:05 +00002225 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002226 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002227 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002228 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002229 break;
2230 }
2231
2232 case Type::DependentSizedArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002233 MarkUsedTemplateParameters(SemaRef,
2234 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002235 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002236 // Fall through to check the element type
2237
2238 case Type::ConstantArray:
2239 case Type::IncompleteArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002240 MarkUsedTemplateParameters(SemaRef,
2241 cast<ArrayType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002242 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002243 break;
2244
2245 case Type::Vector:
2246 case Type::ExtVector:
Douglas Gregore73bb602009-09-14 21:25:05 +00002247 MarkUsedTemplateParameters(SemaRef,
2248 cast<VectorType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002249 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002250 break;
2251
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002252 case Type::DependentSizedExtVector: {
2253 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002254 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002255 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002256 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002257 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002258 Depth, Used);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002259 break;
2260 }
2261
Douglas Gregor031a5882009-06-13 00:26:55 +00002262 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002263 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002264 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002265 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002266 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore73bb602009-09-14 21:25:05 +00002267 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002268 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002269 break;
2270 }
2271
Douglas Gregored9c0f92009-10-29 00:04:11 +00002272 case Type::TemplateTypeParm: {
2273 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2274 if (TTP->getDepth() == Depth)
2275 Used[TTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002276 break;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002277 }
Douglas Gregor031a5882009-06-13 00:26:55 +00002278
2279 case Type::TemplateSpecialization: {
Mike Stump1eb44332009-09-09 15:08:12 +00002280 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002281 = cast<TemplateSpecializationType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002282 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002283 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002284 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002285 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2286 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002287 break;
2288 }
2289
Douglas Gregore73bb602009-09-14 21:25:05 +00002290 case Type::Complex:
2291 if (!OnlyDeduced)
2292 MarkUsedTemplateParameters(SemaRef,
2293 cast<ComplexType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002294 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002295 break;
2296
2297 case Type::Typename:
2298 if (!OnlyDeduced)
2299 MarkUsedTemplateParameters(SemaRef,
2300 cast<TypenameType>(T)->getQualifier(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002301 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002302 break;
2303
2304 // None of these types have any template parameters in them.
Douglas Gregor031a5882009-06-13 00:26:55 +00002305 case Type::Builtin:
2306 case Type::FixedWidthInt:
Douglas Gregor031a5882009-06-13 00:26:55 +00002307 case Type::VariableArray:
2308 case Type::FunctionNoProto:
2309 case Type::Record:
2310 case Type::Enum:
Douglas Gregor031a5882009-06-13 00:26:55 +00002311 case Type::ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002312 case Type::ObjCObjectPointer:
Douglas Gregor031a5882009-06-13 00:26:55 +00002313#define TYPE(Class, Base)
2314#define ABSTRACT_TYPE(Class, Base)
2315#define DEPENDENT_TYPE(Class, Base)
2316#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2317#include "clang/AST/TypeNodes.def"
2318 break;
2319 }
2320}
2321
Douglas Gregore73bb602009-09-14 21:25:05 +00002322/// \brief Mark the template parameters that are used by this
Douglas Gregor031a5882009-06-13 00:26:55 +00002323/// template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00002324static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002325MarkUsedTemplateParameters(Sema &SemaRef,
2326 const TemplateArgument &TemplateArg,
2327 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002328 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002329 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002330 switch (TemplateArg.getKind()) {
2331 case TemplateArgument::Null:
2332 case TemplateArgument::Integral:
Douglas Gregor788cd062009-11-11 01:00:40 +00002333 case TemplateArgument::Declaration:
Douglas Gregor031a5882009-06-13 00:26:55 +00002334 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002335
Douglas Gregor031a5882009-06-13 00:26:55 +00002336 case TemplateArgument::Type:
Douglas Gregore73bb602009-09-14 21:25:05 +00002337 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002338 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002339 break;
2340
Douglas Gregor788cd062009-11-11 01:00:40 +00002341 case TemplateArgument::Template:
2342 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2343 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002344 break;
2345
2346 case TemplateArgument::Expression:
Douglas Gregore73bb602009-09-14 21:25:05 +00002347 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002348 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002349 break;
Douglas Gregore73bb602009-09-14 21:25:05 +00002350
Anders Carlssond01b1da2009-06-15 17:04:53 +00002351 case TemplateArgument::Pack:
Douglas Gregore73bb602009-09-14 21:25:05 +00002352 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2353 PEnd = TemplateArg.pack_end();
2354 P != PEnd; ++P)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002355 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssond01b1da2009-06-15 17:04:53 +00002356 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00002357 }
2358}
2359
2360/// \brief Mark the template parameters can be deduced by the given
2361/// template argument list.
2362///
2363/// \param TemplateArgs the template argument list from which template
2364/// parameters will be deduced.
2365///
2366/// \param Deduced a bit vector whose elements will be set to \c true
2367/// to indicate when the corresponding template parameter will be
2368/// deduced.
Mike Stump1eb44332009-09-09 15:08:12 +00002369void
Douglas Gregore73bb602009-09-14 21:25:05 +00002370Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002371 bool OnlyDeduced, unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002372 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002373 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002374 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2375 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002376}
Douglas Gregor63f07c52009-09-18 23:21:38 +00002377
2378/// \brief Marks all of the template parameters that will be deduced by a
2379/// call to the given function template.
2380void Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
2381 llvm::SmallVectorImpl<bool> &Deduced) {
2382 TemplateParameterList *TemplateParams
2383 = FunctionTemplate->getTemplateParameters();
2384 Deduced.clear();
2385 Deduced.resize(TemplateParams->size());
2386
2387 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2388 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
2389 ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002390 true, TemplateParams->getDepth(), Deduced);
Douglas Gregor63f07c52009-09-18 23:21:38 +00002391}