blob: b861dcabfe114461d520ac563123d9ba7202b236 [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 Gregor508f1c82009-06-26 23:10:12 +000021
22namespace clang {
23 /// \brief Various flags that control template argument deduction.
24 ///
25 /// These flags can be bitwise-OR'd together.
26 enum TemplateDeductionFlags {
27 /// \brief No template argument deduction flags, which indicates the
28 /// strictest results for template argument deduction (as used for, e.g.,
29 /// matching class template partial specializations).
30 TDF_None = 0,
31 /// \brief Within template argument deduction from a function call, we are
32 /// matching with a parameter type for which the original parameter was
33 /// a reference.
34 TDF_ParamWithReferenceType = 0x1,
35 /// \brief Within template argument deduction from a function call, we
36 /// are matching in a case where we ignore cv-qualifiers.
37 TDF_IgnoreQualifiers = 0x02,
38 /// \brief Within template argument deduction from a function call,
39 /// we are matching in a case where we can perform template argument
Douglas Gregor41128772009-06-26 23:27:24 +000040 /// deduction from a template-id of a derived class of the argument type.
Douglas Gregor508f1c82009-06-26 23:10:12 +000041 TDF_DerivedClass = 0x04
42 };
43}
44
Douglas Gregor0b9247f2009-06-04 00:03:07 +000045using namespace clang;
46
Douglas Gregorf67875d2009-06-12 18:26:56 +000047static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +000048DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +000049 TemplateParameterList *TemplateParams,
50 const TemplateArgument &Param,
Douglas Gregord708c722009-06-09 16:35:58 +000051 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +000052 Sema::TemplateDeductionInfo &Info,
Douglas Gregord708c722009-06-09 16:35:58 +000053 llvm::SmallVectorImpl<TemplateArgument> &Deduced);
54
Douglas Gregor199d9912009-06-05 00:53:49 +000055/// \brief If the given expression is of a form that permits the deduction
56/// of a non-type template parameter, return the declaration of that
57/// non-type template parameter.
58static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
59 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
60 E = IC->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +000061
Douglas Gregor199d9912009-06-05 00:53:49 +000062 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
63 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +000064
Douglas Gregor199d9912009-06-05 00:53:49 +000065 return 0;
66}
67
Mike Stump1eb44332009-09-09 15:08:12 +000068/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +000069/// from the given constant.
Douglas Gregorf67875d2009-06-12 18:26:56 +000070static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +000071DeduceNonTypeTemplateArgument(ASTContext &Context,
72 NonTypeTemplateParmDecl *NTTP,
Anders Carlsson335e24a2009-06-16 22:44:31 +000073 llvm::APSInt Value,
Douglas Gregorf67875d2009-06-12 18:26:56 +000074 Sema::TemplateDeductionInfo &Info,
75 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +000076 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +000077 "Cannot deduce non-type template argument with depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +000078
Douglas Gregor199d9912009-06-05 00:53:49 +000079 if (Deduced[NTTP->getIndex()].isNull()) {
Anders Carlsson25af1ed2009-06-16 23:08:29 +000080 QualType T = NTTP->getType();
Mike Stump1eb44332009-09-09 15:08:12 +000081
Anders Carlsson25af1ed2009-06-16 23:08:29 +000082 // FIXME: Make sure we didn't overflow our data type!
83 unsigned AllowedBits = Context.getTypeSize(T);
84 if (Value.getBitWidth() != AllowedBits)
85 Value.extOrTrunc(AllowedBits);
86 Value.setIsSigned(T->isSignedIntegerType());
87
88 Deduced[NTTP->getIndex()] = TemplateArgument(SourceLocation(), Value, T);
Douglas Gregorf67875d2009-06-12 18:26:56 +000089 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +000090 }
Mike Stump1eb44332009-09-09 15:08:12 +000091
Douglas Gregorf67875d2009-06-12 18:26:56 +000092 assert(Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral);
Mike Stump1eb44332009-09-09 15:08:12 +000093
94 // If the template argument was previously deduced to a negative value,
Douglas Gregor199d9912009-06-05 00:53:49 +000095 // then our deduction fails.
96 const llvm::APSInt *PrevValuePtr = Deduced[NTTP->getIndex()].getAsIntegral();
Anders Carlsson335e24a2009-06-16 22:44:31 +000097 if (PrevValuePtr->isNegative()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +000098 Info.Param = NTTP;
99 Info.FirstArg = Deduced[NTTP->getIndex()];
Anders Carlsson335e24a2009-06-16 22:44:31 +0000100 Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000101 return Sema::TDK_Inconsistent;
102 }
103
Anders Carlsson335e24a2009-06-16 22:44:31 +0000104 llvm::APSInt PrevValue = *PrevValuePtr;
Douglas Gregor199d9912009-06-05 00:53:49 +0000105 if (Value.getBitWidth() > PrevValue.getBitWidth())
106 PrevValue.zext(Value.getBitWidth());
107 else if (Value.getBitWidth() < PrevValue.getBitWidth())
108 Value.zext(PrevValue.getBitWidth());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000109
110 if (Value != PrevValue) {
111 Info.Param = NTTP;
112 Info.FirstArg = Deduced[NTTP->getIndex()];
Anders Carlsson335e24a2009-06-16 22:44:31 +0000113 Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000114 return Sema::TDK_Inconsistent;
115 }
116
117 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000118}
119
Mike Stump1eb44332009-09-09 15:08:12 +0000120/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +0000121/// from the given type- or value-dependent expression.
122///
123/// \returns true if deduction succeeded, false otherwise.
124
Douglas Gregorf67875d2009-06-12 18:26:56 +0000125static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +0000126DeduceNonTypeTemplateArgument(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000127 NonTypeTemplateParmDecl *NTTP,
128 Expr *Value,
129 Sema::TemplateDeductionInfo &Info,
130 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +0000131 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000132 "Cannot deduce non-type template argument with depth > 0");
133 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
134 "Expression template argument must be type- or value-dependent.");
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Douglas Gregor199d9912009-06-05 00:53:49 +0000136 if (Deduced[NTTP->getIndex()].isNull()) {
137 // FIXME: Clone the Value?
138 Deduced[NTTP->getIndex()] = TemplateArgument(Value);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000139 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000140 }
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Douglas Gregor199d9912009-06-05 00:53:49 +0000142 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
Mike Stump1eb44332009-09-09 15:08:12 +0000143 // Okay, we deduced a constant in one case and a dependent expression
144 // in another case. FIXME: Later, we will check that instantiating the
Douglas Gregor199d9912009-06-05 00:53:49 +0000145 // dependent expression gives us the constant value.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000146 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000147 }
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Douglas Gregor199d9912009-06-05 00:53:49 +0000149 // FIXME: Compare the expressions for equality!
Douglas Gregorf67875d2009-06-12 18:26:56 +0000150 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000151}
152
Douglas Gregorf67875d2009-06-12 18:26:56 +0000153static Sema::TemplateDeductionResult
154DeduceTemplateArguments(ASTContext &Context,
155 TemplateName Param,
156 TemplateName Arg,
157 Sema::TemplateDeductionInfo &Info,
158 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregord708c722009-06-09 16:35:58 +0000159 // FIXME: Implement template argument deduction for template
160 // template parameters.
161
Douglas Gregorf67875d2009-06-12 18:26:56 +0000162 // FIXME: this routine does not have enough information to produce
163 // good diagnostics.
164
Douglas Gregord708c722009-06-09 16:35:58 +0000165 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
166 TemplateDecl *ArgDecl = Arg.getAsTemplateDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Douglas Gregorf67875d2009-06-12 18:26:56 +0000168 if (!ParamDecl || !ArgDecl) {
169 // FIXME: fill in Info.Param/Info.FirstArg
170 return Sema::TDK_Inconsistent;
171 }
Douglas Gregord708c722009-06-09 16:35:58 +0000172
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000173 ParamDecl = cast<TemplateDecl>(ParamDecl->getCanonicalDecl());
174 ArgDecl = cast<TemplateDecl>(ArgDecl->getCanonicalDecl());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000175 if (ParamDecl != ArgDecl) {
176 // FIXME: fill in Info.Param/Info.FirstArg
177 return Sema::TDK_Inconsistent;
178 }
179
180 return Sema::TDK_Success;
Douglas Gregord708c722009-06-09 16:35:58 +0000181}
182
Mike Stump1eb44332009-09-09 15:08:12 +0000183/// \brief Deduce the template arguments by comparing the template parameter
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000184/// type (which is a template-id) with the template argument type.
185///
186/// \param Context the AST context in which this deduction occurs.
187///
188/// \param TemplateParams the template parameters that we are deducing
189///
190/// \param Param the parameter type
191///
192/// \param Arg the argument type
193///
194/// \param Info information about the template argument deduction itself
195///
196/// \param Deduced the deduced template arguments
197///
198/// \returns the result of template argument deduction so far. Note that a
199/// "success" result means that template argument deduction has not yet failed,
200/// but it may still fail, later, for other reasons.
201static Sema::TemplateDeductionResult
202DeduceTemplateArguments(ASTContext &Context,
203 TemplateParameterList *TemplateParams,
204 const TemplateSpecializationType *Param,
205 QualType Arg,
206 Sema::TemplateDeductionInfo &Info,
207 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
208 assert(Arg->isCanonical() && "Argument type must be canonical");
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000210 // Check whether the template argument is a dependent template-id.
211 // FIXME: This is untested code; it can be tested when we implement
212 // partial ordering of class template partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +0000213 if (const TemplateSpecializationType *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000214 = dyn_cast<TemplateSpecializationType>(Arg)) {
215 // Perform template argument deduction for the template name.
216 if (Sema::TemplateDeductionResult Result
217 = DeduceTemplateArguments(Context,
218 Param->getTemplateName(),
219 SpecArg->getTemplateName(),
220 Info, Deduced))
221 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000223 unsigned NumArgs = Param->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000225 // FIXME: When one of the template-names refers to a
226 // declaration with default template arguments, do we need to
227 // fill in those default template arguments here? Most likely,
228 // the answer is "yes", but I don't see any references. This
229 // issue may be resolved elsewhere, because we may want to
230 // instantiate default template arguments when we actually write
231 // the template-id.
232 if (SpecArg->getNumArgs() != NumArgs)
233 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000235 // Perform template argument deduction on each template
236 // argument.
237 for (unsigned I = 0; I != NumArgs; ++I)
238 if (Sema::TemplateDeductionResult Result
239 = DeduceTemplateArguments(Context, TemplateParams,
240 Param->getArg(I),
241 SpecArg->getArg(I),
242 Info, Deduced))
243 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000245 return Sema::TDK_Success;
246 }
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000248 // If the argument type is a class template specialization, we
249 // perform template argument deduction using its template
250 // arguments.
251 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
252 if (!RecordArg)
253 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000254
255 ClassTemplateSpecializationDecl *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000256 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
257 if (!SpecArg)
258 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000260 // Perform template argument deduction for the template name.
261 if (Sema::TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000262 = DeduceTemplateArguments(Context,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000263 Param->getTemplateName(),
264 TemplateName(SpecArg->getSpecializedTemplate()),
265 Info, Deduced))
266 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000268 // FIXME: Can the # of arguments in the parameter and the argument
269 // differ due to default arguments?
270 unsigned NumArgs = Param->getNumArgs();
271 const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
272 if (NumArgs != ArgArgs.size())
273 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000275 for (unsigned I = 0; I != NumArgs; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000276 if (Sema::TemplateDeductionResult Result
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000277 = DeduceTemplateArguments(Context, TemplateParams,
278 Param->getArg(I),
279 ArgArgs.get(I),
280 Info, Deduced))
281 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000283 return Sema::TDK_Success;
284}
285
Mike Stump1eb44332009-09-09 15:08:12 +0000286/// \brief Returns a completely-unqualified array type, capturing the
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000287/// qualifiers in CVRQuals.
288///
289/// \param Context the AST context in which the array type was built.
290///
291/// \param T a canonical type that may be an array type.
292///
293/// \param CVRQuals will receive the set of const/volatile/restrict qualifiers
294/// that were applied to the element type of the array.
295///
296/// \returns if \p T is an array type, the completely unqualified array type
297/// that corresponds to T. Otherwise, returns T.
298static QualType getUnqualifiedArrayType(ASTContext &Context, QualType T,
299 unsigned &CVRQuals) {
300 assert(T->isCanonical() && "Only operates on canonical types");
301 if (!isa<ArrayType>(T)) {
302 CVRQuals = T.getCVRQualifiers();
303 return T.getUnqualifiedType();
304 }
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000306 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) {
307 QualType Elt = getUnqualifiedArrayType(Context, CAT->getElementType(),
308 CVRQuals);
309 if (Elt == CAT->getElementType())
310 return T;
311
Mike Stump1eb44332009-09-09 15:08:12 +0000312 return Context.getConstantArrayType(Elt, CAT->getSize(),
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000313 CAT->getSizeModifier(), 0);
314 }
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000316 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) {
317 QualType Elt = getUnqualifiedArrayType(Context, IAT->getElementType(),
318 CVRQuals);
319 if (Elt == IAT->getElementType())
320 return T;
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000322 return Context.getIncompleteArrayType(Elt, IAT->getSizeModifier(), 0);
323 }
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000325 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T);
326 QualType Elt = getUnqualifiedArrayType(Context, DSAT->getElementType(),
327 CVRQuals);
328 if (Elt == DSAT->getElementType())
329 return T;
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Anders Carlssond4972062009-08-08 02:50:17 +0000331 return Context.getDependentSizedArrayType(Elt, DSAT->getSizeExpr()->Retain(),
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000332 DSAT->getSizeModifier(), 0,
333 SourceRange());
334}
335
Douglas Gregor500d3312009-06-26 18:27:22 +0000336/// \brief Deduce the template arguments by comparing the parameter type and
337/// the argument type (C++ [temp.deduct.type]).
338///
339/// \param Context the AST context in which this deduction occurs.
340///
341/// \param TemplateParams the template parameters that we are deducing
342///
343/// \param ParamIn the parameter type
344///
345/// \param ArgIn the argument type
346///
347/// \param Info information about the template argument deduction itself
348///
349/// \param Deduced the deduced template arguments
350///
Douglas Gregor508f1c82009-06-26 23:10:12 +0000351/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
Mike Stump1eb44332009-09-09 15:08:12 +0000352/// how template argument deduction is performed.
Douglas Gregor500d3312009-06-26 18:27:22 +0000353///
354/// \returns the result of template argument deduction so far. Note that a
355/// "success" result means that template argument deduction has not yet failed,
356/// but it may still fail, later, for other reasons.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000357static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +0000358DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000359 TemplateParameterList *TemplateParams,
360 QualType ParamIn, QualType ArgIn,
361 Sema::TemplateDeductionInfo &Info,
Douglas Gregor500d3312009-06-26 18:27:22 +0000362 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000363 unsigned TDF) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000364 // We only want to look at the canonical types, since typedefs and
365 // sugar are not part of template argument deduction.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000366 QualType Param = Context.getCanonicalType(ParamIn);
367 QualType Arg = Context.getCanonicalType(ArgIn);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000368
Douglas Gregor500d3312009-06-26 18:27:22 +0000369 // C++0x [temp.deduct.call]p4 bullet 1:
370 // - If the original P is a reference type, the deduced A (i.e., the type
Mike Stump1eb44332009-09-09 15:08:12 +0000371 // referred to by the reference) can be more cv-qualified than the
Douglas Gregor500d3312009-06-26 18:27:22 +0000372 // transformed A.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000373 if (TDF & TDF_ParamWithReferenceType) {
Mike Stump1eb44332009-09-09 15:08:12 +0000374 unsigned ExtraQualsOnParam
Douglas Gregor500d3312009-06-26 18:27:22 +0000375 = Param.getCVRQualifiers() & ~Arg.getCVRQualifiers();
376 Param.setCVRQualifiers(Param.getCVRQualifiers() & ~ExtraQualsOnParam);
377 }
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000379 // If the parameter type is not dependent, there is nothing to deduce.
380 if (!Param->isDependentType())
381 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000382
Douglas Gregor199d9912009-06-05 00:53:49 +0000383 // C++ [temp.deduct.type]p9:
Mike Stump1eb44332009-09-09 15:08:12 +0000384 // A template type argument T, a template template argument TT or a
385 // template non-type argument i can be deduced if P and A have one of
Douglas Gregor199d9912009-06-05 00:53:49 +0000386 // the following forms:
387 //
388 // T
389 // cv-list T
Mike Stump1eb44332009-09-09 15:08:12 +0000390 if (const TemplateTypeParmType *TemplateTypeParm
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000391 = Param->getAsTemplateTypeParmType()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000392 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000393 bool RecanonicalizeArg = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Douglas Gregor9e9fae42009-07-22 20:02:25 +0000395 // If the argument type is an array type, move the qualifiers up to the
396 // top level, so they can be matched with the qualifiers on the parameter.
397 // FIXME: address spaces, ObjC GC qualifiers
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000398 if (isa<ArrayType>(Arg)) {
399 unsigned CVRQuals = 0;
400 Arg = getUnqualifiedArrayType(Context, Arg, CVRQuals);
401 if (CVRQuals) {
402 Arg = Arg.getWithAdditionalQualifiers(CVRQuals);
403 RecanonicalizeArg = true;
404 }
405 }
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000407 // The argument type can not be less qualified than the parameter
408 // type.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000409 if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000410 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
411 Info.FirstArg = Deduced[Index];
412 Info.SecondArg = TemplateArgument(SourceLocation(), Arg);
413 return Sema::TDK_InconsistentQuals;
414 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000415
416 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000418 unsigned Quals = Arg.getCVRQualifiers() & ~Param.getCVRQualifiers();
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000419 QualType DeducedType = Arg.getQualifiedType(Quals);
420 if (RecanonicalizeArg)
421 DeducedType = Context.getCanonicalType(DeducedType);
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000423 if (Deduced[Index].isNull())
424 Deduced[Index] = TemplateArgument(SourceLocation(), DeducedType);
425 else {
Mike Stump1eb44332009-09-09 15:08:12 +0000426 // C++ [temp.deduct.type]p2:
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000427 // [...] If type deduction cannot be done for any P/A pair, or if for
Mike Stump1eb44332009-09-09 15:08:12 +0000428 // any pair the deduction leads to more than one possible set of
429 // deduced values, or if different pairs yield different deduced
430 // values, or if any template argument remains neither deduced nor
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000431 // explicitly specified, template argument deduction fails.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000432 if (Deduced[Index].getAsType() != DeducedType) {
Mike Stump1eb44332009-09-09 15:08:12 +0000433 Info.Param
Douglas Gregorf67875d2009-06-12 18:26:56 +0000434 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
435 Info.FirstArg = Deduced[Index];
436 Info.SecondArg = TemplateArgument(SourceLocation(), Arg);
437 return Sema::TDK_Inconsistent;
438 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000439 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000440 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000441 }
442
Douglas Gregorf67875d2009-06-12 18:26:56 +0000443 // Set up the template argument deduction information for a failure.
444 Info.FirstArg = TemplateArgument(SourceLocation(), ParamIn);
445 Info.SecondArg = TemplateArgument(SourceLocation(), ArgIn);
446
Douglas Gregor508f1c82009-06-26 23:10:12 +0000447 // Check the cv-qualifiers on the parameter and argument types.
448 if (!(TDF & TDF_IgnoreQualifiers)) {
449 if (TDF & TDF_ParamWithReferenceType) {
450 if (Param.isMoreQualifiedThan(Arg))
451 return Sema::TDK_NonDeducedMismatch;
452 } else {
453 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Mike Stump1eb44332009-09-09 15:08:12 +0000454 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor508f1c82009-06-26 23:10:12 +0000455 }
456 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000457
Douglas Gregord560d502009-06-04 00:21:18 +0000458 switch (Param->getTypeClass()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000459 // No deduction possible for these types
460 case Type::Builtin:
Douglas Gregorf67875d2009-06-12 18:26:56 +0000461 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Douglas Gregor199d9912009-06-05 00:53:49 +0000463 // T *
Douglas Gregord560d502009-06-04 00:21:18 +0000464 case Type::Pointer: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000465 const PointerType *PointerArg = Arg->getAs<PointerType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000466 if (!PointerArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000467 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Douglas Gregor41128772009-06-26 23:27:24 +0000469 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000470 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000471 cast<PointerType>(Param)->getPointeeType(),
472 PointerArg->getPointeeType(),
Douglas Gregor41128772009-06-26 23:27:24 +0000473 Info, Deduced, SubTDF);
Douglas Gregord560d502009-06-04 00:21:18 +0000474 }
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Douglas Gregor199d9912009-06-05 00:53:49 +0000476 // T &
Douglas Gregord560d502009-06-04 00:21:18 +0000477 case Type::LValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000478 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000479 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000480 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Douglas Gregorf67875d2009-06-12 18:26:56 +0000482 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000483 cast<LValueReferenceType>(Param)->getPointeeType(),
484 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000485 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000486 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000487
Douglas Gregor199d9912009-06-05 00:53:49 +0000488 // T && [C++0x]
Douglas Gregord560d502009-06-04 00:21:18 +0000489 case Type::RValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000490 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000491 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000492 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Douglas Gregorf67875d2009-06-12 18:26:56 +0000494 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000495 cast<RValueReferenceType>(Param)->getPointeeType(),
496 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000497 Info, Deduced, 0);
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 [] (implied, but not stated explicitly)
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000501 case Type::IncompleteArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000502 const IncompleteArrayType *IncompleteArrayArg =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000503 Context.getAsIncompleteArrayType(Arg);
504 if (!IncompleteArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000505 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Douglas Gregorf67875d2009-06-12 18:26:56 +0000507 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000508 Context.getAsIncompleteArrayType(Param)->getElementType(),
509 IncompleteArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000510 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000511 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000512
513 // T [integer-constant]
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000514 case Type::ConstantArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000515 const ConstantArrayType *ConstantArrayArg =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000516 Context.getAsConstantArrayType(Arg);
517 if (!ConstantArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000518 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000519
520 const ConstantArrayType *ConstantArrayParm =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000521 Context.getAsConstantArrayType(Param);
522 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000523 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Douglas Gregorf67875d2009-06-12 18:26:56 +0000525 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000526 ConstantArrayParm->getElementType(),
527 ConstantArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000528 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000529 }
530
Douglas Gregor199d9912009-06-05 00:53:49 +0000531 // type [i]
532 case Type::DependentSizedArray: {
533 const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg);
534 if (!ArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000535 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Douglas Gregor199d9912009-06-05 00:53:49 +0000537 // Check the element type of the arrays
538 const DependentSizedArrayType *DependentArrayParm
539 = cast<DependentSizedArrayType>(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000540 if (Sema::TemplateDeductionResult Result
541 = DeduceTemplateArguments(Context, TemplateParams,
542 DependentArrayParm->getElementType(),
543 ArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000544 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000545 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Douglas Gregor199d9912009-06-05 00:53:49 +0000547 // Determine the array bound is something we can deduce.
Mike Stump1eb44332009-09-09 15:08:12 +0000548 NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000549 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
550 if (!NTTP)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000551 return Sema::TDK_Success;
Mike Stump1eb44332009-09-09 15:08:12 +0000552
553 // We can perform template argument deduction for the given non-type
Douglas Gregor199d9912009-06-05 00:53:49 +0000554 // template parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000555 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000556 "Cannot deduce non-type template argument at depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000557 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson335e24a2009-06-16 22:44:31 +0000558 = dyn_cast<ConstantArrayType>(ArrayArg)) {
559 llvm::APSInt Size(ConstantArrayArg->getSize());
560 return DeduceNonTypeTemplateArgument(Context, NTTP, Size,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000561 Info, Deduced);
Anders Carlsson335e24a2009-06-16 22:44:31 +0000562 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000563 if (const DependentSizedArrayType *DependentArrayArg
564 = dyn_cast<DependentSizedArrayType>(ArrayArg))
565 return DeduceNonTypeTemplateArgument(Context, NTTP,
566 DependentArrayArg->getSizeExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000567 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Douglas Gregor199d9912009-06-05 00:53:49 +0000569 // Incomplete type does not match a dependently-sized array type
Douglas Gregorf67875d2009-06-12 18:26:56 +0000570 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000571 }
Mike Stump1eb44332009-09-09 15:08:12 +0000572
573 // type(*)(T)
574 // T(*)()
575 // T(*)(T)
Anders Carlssona27fad52009-06-08 15:19:08 +0000576 case Type::FunctionProto: {
Mike Stump1eb44332009-09-09 15:08:12 +0000577 const FunctionProtoType *FunctionProtoArg =
Anders Carlssona27fad52009-06-08 15:19:08 +0000578 dyn_cast<FunctionProtoType>(Arg);
579 if (!FunctionProtoArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000580 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000581
582 const FunctionProtoType *FunctionProtoParam =
Anders Carlssona27fad52009-06-08 15:19:08 +0000583 cast<FunctionProtoType>(Param);
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000584
Mike Stump1eb44332009-09-09 15:08:12 +0000585 if (FunctionProtoParam->getTypeQuals() !=
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000586 FunctionProtoArg->getTypeQuals())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000587 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000589 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000590 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000592 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000593 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000594
Anders Carlssona27fad52009-06-08 15:19:08 +0000595 // Check return types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000596 if (Sema::TemplateDeductionResult Result
597 = DeduceTemplateArguments(Context, TemplateParams,
598 FunctionProtoParam->getResultType(),
599 FunctionProtoArg->getResultType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000600 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000601 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Anders Carlssona27fad52009-06-08 15:19:08 +0000603 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
604 // Check argument types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000605 if (Sema::TemplateDeductionResult Result
606 = DeduceTemplateArguments(Context, TemplateParams,
607 FunctionProtoParam->getArgType(I),
608 FunctionProtoArg->getArgType(I),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000609 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000610 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000611 }
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Douglas Gregorf67875d2009-06-12 18:26:56 +0000613 return Sema::TDK_Success;
Anders Carlssona27fad52009-06-08 15:19:08 +0000614 }
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000616 // template-name<T> (where template-name refers to a class template)
Douglas Gregord708c722009-06-09 16:35:58 +0000617 // template-name<i>
618 // TT<T> (TODO)
619 // TT<i> (TODO)
620 // TT<> (TODO)
621 case Type::TemplateSpecialization: {
622 const TemplateSpecializationType *SpecParam
623 = cast<TemplateSpecializationType>(Param);
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000625 // Try to deduce template arguments from the template-id.
626 Sema::TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000627 = DeduceTemplateArguments(Context, TemplateParams, SpecParam, Arg,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000628 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000629
630 if (Result && (TDF & TDF_DerivedClass) &&
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000631 Result != Sema::TDK_Inconsistent) {
632 // C++ [temp.deduct.call]p3b3:
633 // If P is a class, and P has the form template-id, then A can be a
634 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump1eb44332009-09-09 15:08:12 +0000635 // class of the form template-id, A can be a pointer to a derived
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000636 // class pointed to by the deduced A.
637 //
638 // More importantly:
Mike Stump1eb44332009-09-09 15:08:12 +0000639 // These alternatives are considered only if type deduction would
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000640 // otherwise fail.
641 if (const RecordType *RecordT = dyn_cast<RecordType>(Arg)) {
642 // Use data recursion to crawl through the list of base classes.
Mike Stump1eb44332009-09-09 15:08:12 +0000643 // Visited contains the set of nodes we have already visited, while
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000644 // ToVisit is our stack of records that we still need to visit.
645 llvm::SmallPtrSet<const RecordType *, 8> Visited;
646 llvm::SmallVector<const RecordType *, 8> ToVisit;
647 ToVisit.push_back(RecordT);
648 bool Successful = false;
649 while (!ToVisit.empty()) {
650 // Retrieve the next class in the inheritance hierarchy.
651 const RecordType *NextT = ToVisit.back();
652 ToVisit.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000654 // If we have already seen this type, skip it.
655 if (!Visited.insert(NextT))
656 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000658 // If this is a base class, try to perform template argument
659 // deduction from it.
660 if (NextT != RecordT) {
661 Sema::TemplateDeductionResult BaseResult
662 = DeduceTemplateArguments(Context, TemplateParams, SpecParam,
663 QualType(NextT, 0), Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000665 // If template argument deduction for this base was successful,
666 // note that we had some success.
667 if (BaseResult == Sema::TDK_Success)
668 Successful = true;
669 // If deduction against this base resulted in an inconsistent
670 // set of deduced template arguments, template argument
671 // deduction fails.
672 else if (BaseResult == Sema::TDK_Inconsistent)
673 return BaseResult;
674 }
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000676 // Visit base classes
677 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
678 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
679 BaseEnd = Next->bases_end();
680 Base != BaseEnd; ++Base) {
Mike Stump1eb44332009-09-09 15:08:12 +0000681 assert(Base->getType()->isRecordType() &&
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000682 "Base class that isn't a record?");
Ted Kremenek6217b802009-07-29 21:53:49 +0000683 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000684 }
685 }
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000687 if (Successful)
688 return Sema::TDK_Success;
689 }
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000691 }
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000693 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000694 }
695
Douglas Gregor637a4092009-06-10 23:47:09 +0000696 // T type::*
697 // T T::*
698 // T (type::*)()
699 // type (T::*)()
700 // type (type::*)(T)
701 // type (T::*)(T)
702 // T (type::*)(T)
703 // T (T::*)()
704 // T (T::*)(T)
705 case Type::MemberPointer: {
706 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
707 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
708 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000709 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +0000710
Douglas Gregorf67875d2009-06-12 18:26:56 +0000711 if (Sema::TemplateDeductionResult Result
712 = DeduceTemplateArguments(Context, TemplateParams,
713 MemPtrParam->getPointeeType(),
714 MemPtrArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000715 Info, Deduced,
716 TDF & TDF_IgnoreQualifiers))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000717 return Result;
718
719 return DeduceTemplateArguments(Context, TemplateParams,
720 QualType(MemPtrParam->getClass(), 0),
721 QualType(MemPtrArg->getClass(), 0),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000722 Info, Deduced, 0);
Douglas Gregor637a4092009-06-10 23:47:09 +0000723 }
724
Anders Carlsson9a917e42009-06-12 22:56:54 +0000725 // (clang extension)
726 //
Mike Stump1eb44332009-09-09 15:08:12 +0000727 // type(^)(T)
728 // T(^)()
729 // T(^)(T)
Anders Carlsson859ba502009-06-12 16:23:10 +0000730 case Type::BlockPointer: {
731 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
732 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Anders Carlsson859ba502009-06-12 16:23:10 +0000734 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000735 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Douglas Gregorf67875d2009-06-12 18:26:56 +0000737 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +0000738 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000739 BlockPtrArg->getPointeeType(), Info,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000740 Deduced, 0);
Anders Carlsson859ba502009-06-12 16:23:10 +0000741 }
742
Douglas Gregor637a4092009-06-10 23:47:09 +0000743 case Type::TypeOfExpr:
744 case Type::TypeOf:
745 case Type::Typename:
746 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +0000747 return Sema::TDK_Success;
Douglas Gregor637a4092009-06-10 23:47:09 +0000748
Douglas Gregord560d502009-06-04 00:21:18 +0000749 default:
750 break;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000751 }
752
753 // FIXME: Many more cases to go (to go).
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000754 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000755}
756
Douglas Gregorf67875d2009-06-12 18:26:56 +0000757static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +0000758DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000759 TemplateParameterList *TemplateParams,
760 const TemplateArgument &Param,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000761 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000762 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000763 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000764 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000765 case TemplateArgument::Null:
766 assert(false && "Null template argument in parameter list");
767 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000768
769 case TemplateArgument::Type:
Douglas Gregor199d9912009-06-05 00:53:49 +0000770 assert(Arg.getKind() == TemplateArgument::Type && "Type/value mismatch");
Douglas Gregor508f1c82009-06-26 23:10:12 +0000771 return DeduceTemplateArguments(Context, TemplateParams, Param.getAsType(),
772 Arg.getAsType(), Info, Deduced, 0);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000773
Douglas Gregor199d9912009-06-05 00:53:49 +0000774 case TemplateArgument::Declaration:
775 // FIXME: Implement this check
776 assert(false && "Unimplemented template argument deduction case");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000777 Info.FirstArg = Param;
778 Info.SecondArg = Arg;
779 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Douglas Gregor199d9912009-06-05 00:53:49 +0000781 case TemplateArgument::Integral:
782 if (Arg.getKind() == TemplateArgument::Integral) {
783 // FIXME: Zero extension + sign checking here?
Douglas Gregorf67875d2009-06-12 18:26:56 +0000784 if (*Param.getAsIntegral() == *Arg.getAsIntegral())
785 return Sema::TDK_Success;
786
787 Info.FirstArg = Param;
788 Info.SecondArg = Arg;
789 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000790 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000791
792 if (Arg.getKind() == TemplateArgument::Expression) {
793 Info.FirstArg = Param;
794 Info.SecondArg = Arg;
795 return Sema::TDK_NonDeducedMismatch;
796 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000797
798 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000799 Info.FirstArg = Param;
800 Info.SecondArg = Arg;
801 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Douglas Gregor199d9912009-06-05 00:53:49 +0000803 case TemplateArgument::Expression: {
Mike Stump1eb44332009-09-09 15:08:12 +0000804 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000805 = getDeducedParameterFromExpr(Param.getAsExpr())) {
806 if (Arg.getKind() == TemplateArgument::Integral)
807 // FIXME: Sign problems here
Mike Stump1eb44332009-09-09 15:08:12 +0000808 return DeduceNonTypeTemplateArgument(Context, NTTP,
809 *Arg.getAsIntegral(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000810 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000811 if (Arg.getKind() == TemplateArgument::Expression)
812 return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000813 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Douglas Gregor199d9912009-06-05 00:53:49 +0000815 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000816 Info.FirstArg = Param;
817 Info.SecondArg = Arg;
818 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000819 }
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Douglas Gregor199d9912009-06-05 00:53:49 +0000821 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000822 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000823 }
Anders Carlssond01b1da2009-06-15 17:04:53 +0000824 case TemplateArgument::Pack:
825 assert(0 && "FIXME: Implement!");
826 break;
Douglas Gregor199d9912009-06-05 00:53:49 +0000827 }
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Douglas Gregorf67875d2009-06-12 18:26:56 +0000829 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000830}
831
Mike Stump1eb44332009-09-09 15:08:12 +0000832static Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000833DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000834 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000835 const TemplateArgumentList &ParamList,
836 const TemplateArgumentList &ArgList,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000837 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000838 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
839 assert(ParamList.size() == ArgList.size());
840 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000841 if (Sema::TemplateDeductionResult Result
842 = DeduceTemplateArguments(Context, TemplateParams,
Mike Stump1eb44332009-09-09 15:08:12 +0000843 ParamList[I], ArgList[I],
Douglas Gregorf67875d2009-06-12 18:26:56 +0000844 Info, Deduced))
845 return Result;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000846 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000847 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000848}
849
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000850/// \brief Determine whether two template arguments are the same.
Mike Stump1eb44332009-09-09 15:08:12 +0000851static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000852 const TemplateArgument &X,
853 const TemplateArgument &Y) {
854 if (X.getKind() != Y.getKind())
855 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000857 switch (X.getKind()) {
858 case TemplateArgument::Null:
859 assert(false && "Comparing NULL template argument");
860 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000862 case TemplateArgument::Type:
863 return Context.getCanonicalType(X.getAsType()) ==
864 Context.getCanonicalType(Y.getAsType());
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000866 case TemplateArgument::Declaration:
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000867 return X.getAsDecl()->getCanonicalDecl() ==
868 Y.getAsDecl()->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000870 case TemplateArgument::Integral:
871 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000873 case TemplateArgument::Expression:
874 // FIXME: We assume that all expressions are distinct, but we should
875 // really check their canonical forms.
876 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000878 case TemplateArgument::Pack:
879 if (X.pack_size() != Y.pack_size())
880 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000881
882 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
883 XPEnd = X.pack_end(),
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000884 YP = Y.pack_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000885 XP != XPEnd; ++XP, ++YP)
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000886 if (!isSameTemplateArg(Context, *XP, *YP))
887 return false;
888
889 return true;
890 }
891
892 return false;
893}
894
895/// \brief Helper function to build a TemplateParameter when we don't
896/// know its type statically.
897static TemplateParameter makeTemplateParameter(Decl *D) {
898 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
899 return TemplateParameter(TTP);
900 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
901 return TemplateParameter(NTTP);
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000903 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
904}
905
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000906/// \brief Perform template argument deduction to determine whether
907/// the given template arguments match the given class template
908/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +0000909Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000910Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000911 const TemplateArgumentList &TemplateArgs,
912 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000913 // C++ [temp.class.spec.match]p2:
914 // A partial specialization matches a given actual template
915 // argument list if the template arguments of the partial
916 // specialization can be deduced from the actual template argument
917 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +0000918 SFINAETrap Trap(*this);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000919 llvm::SmallVector<TemplateArgument, 4> Deduced;
920 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000921 if (TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000922 = ::DeduceTemplateArguments(Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000923 Partial->getTemplateParameters(),
Mike Stump1eb44332009-09-09 15:08:12 +0000924 Partial->getTemplateArgs(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000925 TemplateArgs, Info, Deduced))
926 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +0000927
Douglas Gregor637a4092009-06-10 23:47:09 +0000928 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
929 Deduced.data(), Deduced.size());
930 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000931 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +0000932
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000933 // C++ [temp.deduct.type]p2:
934 // [...] or if any template argument remains neither deduced nor
935 // explicitly specified, template argument deduction fails.
Anders Carlssonfb250522009-06-23 01:26:57 +0000936 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
937 Deduced.size());
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000938 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000939 if (Deduced[I].isNull()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000940 Decl *Param
Douglas Gregorf67875d2009-06-12 18:26:56 +0000941 = const_cast<Decl *>(Partial->getTemplateParameters()->getParam(I));
942 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
943 Info.Param = TTP;
Mike Stump1eb44332009-09-09 15:08:12 +0000944 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorf67875d2009-06-12 18:26:56 +0000945 = dyn_cast<NonTypeTemplateParmDecl>(Param))
946 Info.Param = NTTP;
947 else
948 Info.Param = cast<TemplateTemplateParmDecl>(Param);
949 return TDK_Incomplete;
950 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000951
Anders Carlssonfb250522009-06-23 01:26:57 +0000952 Builder.Append(Deduced[I]);
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000953 }
954
955 // Form the template argument list from the deduced template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +0000956 TemplateArgumentList *DeducedArgumentList
Anders Carlssonfb250522009-06-23 01:26:57 +0000957 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000958 Info.reset(DeducedArgumentList);
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000959
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000960 // Substitute the deduced template arguments into the template
961 // arguments of the class template partial specialization, and
962 // verify that the instantiated template arguments are both valid
963 // and are equivalent to the template arguments originally provided
Mike Stump1eb44332009-09-09 15:08:12 +0000964 // to the class template.
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000965 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
966 const TemplateArgumentList &PartialTemplateArgs = Partial->getTemplateArgs();
967 for (unsigned I = 0, N = PartialTemplateArgs.flat_size(); I != N; ++I) {
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000968 Decl *Param = const_cast<Decl *>(
969 ClassTemplate->getTemplateParameters()->getParam(I));
Mike Stump1eb44332009-09-09 15:08:12 +0000970 TemplateArgument InstArg
Douglas Gregor357bbd02009-08-28 20:50:45 +0000971 = Subst(PartialTemplateArgs[I],
972 MultiLevelTemplateArgumentList(*DeducedArgumentList));
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000973 if (InstArg.isNull()) {
974 Info.Param = makeTemplateParameter(Param);
975 Info.FirstArg = PartialTemplateArgs[I];
Mike Stump1eb44332009-09-09 15:08:12 +0000976 return TDK_SubstitutionFailure;
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000977 }
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000979 if (InstArg.getKind() == TemplateArgument::Expression) {
Mike Stump1eb44332009-09-09 15:08:12 +0000980 // When the argument is an expression, check the expression result
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000981 // against the actual template parameter to get down to the canonical
982 // template argument.
983 Expr *InstExpr = InstArg.getAsExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000984 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000985 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
986 if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
987 Info.Param = makeTemplateParameter(Param);
988 Info.FirstArg = PartialTemplateArgs[I];
Mike Stump1eb44332009-09-09 15:08:12 +0000989 return TDK_SubstitutionFailure;
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991 } else if (TemplateTemplateParmDecl *TTP
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000992 = dyn_cast<TemplateTemplateParmDecl>(Param)) {
993 // FIXME: template template arguments should really resolve to decls
994 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InstExpr);
995 if (!DRE || CheckTemplateArgument(TTP, DRE)) {
996 Info.Param = makeTemplateParameter(Param);
997 Info.FirstArg = PartialTemplateArgs[I];
Mike Stump1eb44332009-09-09 15:08:12 +0000998 return TDK_SubstitutionFailure;
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000999 }
1000 }
1001 }
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001003 if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) {
1004 Info.Param = makeTemplateParameter(Param);
1005 Info.FirstArg = TemplateArgs[I];
1006 Info.SecondArg = InstArg;
1007 return TDK_NonDeducedMismatch;
1008 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001009 }
1010
Douglas Gregorbb260412009-06-14 08:02:22 +00001011 if (Trap.hasErrorOccurred())
1012 return TDK_SubstitutionFailure;
1013
Douglas Gregorf67875d2009-06-12 18:26:56 +00001014 return TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001015}
Douglas Gregor031a5882009-06-13 00:26:55 +00001016
Douglas Gregor41128772009-06-26 23:27:24 +00001017/// \brief Determine whether the given type T is a simple-template-id type.
1018static bool isSimpleTemplateIdType(QualType T) {
Mike Stump1eb44332009-09-09 15:08:12 +00001019 if (const TemplateSpecializationType *Spec
Douglas Gregor41128772009-06-26 23:27:24 +00001020 = T->getAsTemplateSpecializationType())
1021 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Douglas Gregor41128772009-06-26 23:27:24 +00001023 return false;
1024}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001025
1026/// \brief Substitute the explicitly-provided template arguments into the
1027/// given function template according to C++ [temp.arg.explicit].
1028///
1029/// \param FunctionTemplate the function template into which the explicit
1030/// template arguments will be substituted.
1031///
Mike Stump1eb44332009-09-09 15:08:12 +00001032/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001033/// arguments.
1034///
Mike Stump1eb44332009-09-09 15:08:12 +00001035/// \param NumExplicitTemplateArguments the number of explicitly-specified
Douglas Gregor83314aa2009-07-08 20:55:45 +00001036/// template arguments in @p ExplicitTemplateArguments. This value may be zero.
1037///
Mike Stump1eb44332009-09-09 15:08:12 +00001038/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor83314aa2009-07-08 20:55:45 +00001039/// with the converted and checked explicit template arguments.
1040///
Mike Stump1eb44332009-09-09 15:08:12 +00001041/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor83314aa2009-07-08 20:55:45 +00001042/// parameters.
1043///
1044/// \param FunctionType if non-NULL, the result type of the function template
1045/// will also be instantiated and the pointed-to value will be updated with
1046/// the instantiated function type.
1047///
1048/// \param Info if substitution fails for any reason, this object will be
1049/// populated with more information about the failure.
1050///
1051/// \returns TDK_Success if substitution was successful, or some failure
1052/// condition.
1053Sema::TemplateDeductionResult
1054Sema::SubstituteExplicitTemplateArguments(
1055 FunctionTemplateDecl *FunctionTemplate,
1056 const TemplateArgument *ExplicitTemplateArgs,
1057 unsigned NumExplicitTemplateArgs,
1058 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1059 llvm::SmallVectorImpl<QualType> &ParamTypes,
1060 QualType *FunctionType,
1061 TemplateDeductionInfo &Info) {
1062 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1063 TemplateParameterList *TemplateParams
1064 = FunctionTemplate->getTemplateParameters();
1065
1066 if (NumExplicitTemplateArgs == 0) {
1067 // No arguments to substitute; just copy over the parameter types and
1068 // fill in the function type.
1069 for (FunctionDecl::param_iterator P = Function->param_begin(),
1070 PEnd = Function->param_end();
1071 P != PEnd;
1072 ++P)
1073 ParamTypes.push_back((*P)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Douglas Gregor83314aa2009-07-08 20:55:45 +00001075 if (FunctionType)
1076 *FunctionType = Function->getType();
1077 return TDK_Success;
1078 }
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Douglas Gregor83314aa2009-07-08 20:55:45 +00001080 // Substitution of the explicit template arguments into a function template
1081 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001082 SFINAETrap Trap(*this);
1083
Douglas Gregor83314aa2009-07-08 20:55:45 +00001084 // C++ [temp.arg.explicit]p3:
Mike Stump1eb44332009-09-09 15:08:12 +00001085 // Template arguments that are present shall be specified in the
1086 // declaration order of their corresponding template-parameters. The
Douglas Gregor83314aa2009-07-08 20:55:45 +00001087 // template argument list shall not specify more template-arguments than
Mike Stump1eb44332009-09-09 15:08:12 +00001088 // there are corresponding template-parameters.
1089 TemplateArgumentListBuilder Builder(TemplateParams,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001090 NumExplicitTemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001091
1092 // Enter a new template instantiation context where we check the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001093 // explicitly-specified template arguments against this function template,
1094 // and then substitute them into the function parameter types.
Mike Stump1eb44332009-09-09 15:08:12 +00001095 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001096 FunctionTemplate, Deduced.data(), Deduced.size(),
1097 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution);
1098 if (Inst)
1099 return TDK_InstantiationDepth;
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Douglas Gregor83314aa2009-07-08 20:55:45 +00001101 if (CheckTemplateArgumentList(FunctionTemplate,
1102 SourceLocation(), SourceLocation(),
1103 ExplicitTemplateArgs,
1104 NumExplicitTemplateArgs,
1105 SourceLocation(),
1106 true,
1107 Builder) || Trap.hasErrorOccurred())
1108 return TDK_InvalidExplicitArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Douglas Gregor83314aa2009-07-08 20:55:45 +00001110 // Form the template argument list from the explicitly-specified
1111 // template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001112 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor83314aa2009-07-08 20:55:45 +00001113 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1114 Info.reset(ExplicitArgumentList);
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Douglas Gregor83314aa2009-07-08 20:55:45 +00001116 // Instantiate the types of each of the function parameters given the
1117 // explicitly-specified template arguments.
1118 for (FunctionDecl::param_iterator P = Function->param_begin(),
1119 PEnd = Function->param_end();
1120 P != PEnd;
1121 ++P) {
Mike Stump1eb44332009-09-09 15:08:12 +00001122 QualType ParamType
1123 = SubstType((*P)->getType(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001124 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1125 (*P)->getLocation(), (*P)->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001126 if (ParamType.isNull() || Trap.hasErrorOccurred())
1127 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Douglas Gregor83314aa2009-07-08 20:55:45 +00001129 ParamTypes.push_back(ParamType);
1130 }
1131
1132 // If the caller wants a full function type back, instantiate the return
1133 // type and form that function type.
1134 if (FunctionType) {
1135 // FIXME: exception-specifications?
Mike Stump1eb44332009-09-09 15:08:12 +00001136 const FunctionProtoType *Proto
Douglas Gregor83314aa2009-07-08 20:55:45 +00001137 = Function->getType()->getAsFunctionProtoType();
1138 assert(Proto && "Function template does not have a prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001139
1140 QualType ResultType
Douglas Gregor357bbd02009-08-28 20:50:45 +00001141 = SubstType(Proto->getResultType(),
1142 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1143 Function->getTypeSpecStartLoc(),
1144 Function->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001145 if (ResultType.isNull() || Trap.hasErrorOccurred())
1146 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001147
1148 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001149 ParamTypes.data(), ParamTypes.size(),
1150 Proto->isVariadic(),
1151 Proto->getTypeQuals(),
1152 Function->getLocation(),
1153 Function->getDeclName());
1154 if (FunctionType->isNull() || Trap.hasErrorOccurred())
1155 return TDK_SubstitutionFailure;
1156 }
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Douglas Gregor83314aa2009-07-08 20:55:45 +00001158 // C++ [temp.arg.explicit]p2:
Mike Stump1eb44332009-09-09 15:08:12 +00001159 // Trailing template arguments that can be deduced (14.8.2) may be
1160 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001161 // template arguments can be deduced, they may all be omitted; in this
1162 // case, the empty template argument list <> itself may also be omitted.
1163 //
1164 // Take all of the explicitly-specified arguments and put them into the
Mike Stump1eb44332009-09-09 15:08:12 +00001165 // set of deduced template arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001166 Deduced.reserve(TemplateParams->size());
1167 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001168 Deduced.push_back(ExplicitArgumentList->get(I));
1169
Douglas Gregor83314aa2009-07-08 20:55:45 +00001170 return TDK_Success;
1171}
1172
Mike Stump1eb44332009-09-09 15:08:12 +00001173/// \brief Finish template argument deduction for a function template,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001174/// checking the deduced template arguments for completeness and forming
1175/// the function template specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00001176Sema::TemplateDeductionResult
Douglas Gregor83314aa2009-07-08 20:55:45 +00001177Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
1178 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1179 FunctionDecl *&Specialization,
1180 TemplateDeductionInfo &Info) {
1181 TemplateParameterList *TemplateParams
1182 = FunctionTemplate->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Douglas Gregor83314aa2009-07-08 20:55:45 +00001184 // C++ [temp.deduct.type]p2:
1185 // [...] or if any template argument remains neither deduced nor
1186 // explicitly specified, template argument deduction fails.
1187 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
1188 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
1189 if (Deduced[I].isNull()) {
1190 Info.Param = makeTemplateParameter(
1191 const_cast<Decl *>(TemplateParams->getParam(I)));
1192 return TDK_Incomplete;
1193 }
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Douglas Gregor83314aa2009-07-08 20:55:45 +00001195 Builder.Append(Deduced[I]);
1196 }
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Douglas Gregor83314aa2009-07-08 20:55:45 +00001198 // Form the template argument list from the deduced template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001199 TemplateArgumentList *DeducedArgumentList
Douglas Gregor83314aa2009-07-08 20:55:45 +00001200 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1201 Info.reset(DeducedArgumentList);
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Douglas Gregor83314aa2009-07-08 20:55:45 +00001203 // Template argument deduction for function templates in a SFINAE context.
1204 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001205 SFINAETrap Trap(*this);
1206
Douglas Gregor83314aa2009-07-08 20:55:45 +00001207 // Enter a new template instantiation context while we instantiate the
1208 // actual function declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001209 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001210 FunctionTemplate, Deduced.data(), Deduced.size(),
1211 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
1212 if (Inst)
Mike Stump1eb44332009-09-09 15:08:12 +00001213 return TDK_InstantiationDepth;
1214
1215 // Substitute the deduced template arguments into the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001216 // declaration to produce the function template specialization.
1217 Specialization = cast_or_null<FunctionDecl>(
John McCallce3ff2b2009-08-25 22:02:44 +00001218 SubstDecl(FunctionTemplate->getTemplatedDecl(),
1219 FunctionTemplate->getDeclContext(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001220 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor83314aa2009-07-08 20:55:45 +00001221 if (!Specialization)
1222 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001223
1224 // If the template argument list is owned by the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001225 // specialization, release it.
1226 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList)
1227 Info.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Douglas Gregor83314aa2009-07-08 20:55:45 +00001229 // There may have been an error that did not prevent us from constructing a
1230 // declaration. Mark the declaration invalid and return with a substitution
1231 // failure.
1232 if (Trap.hasErrorOccurred()) {
1233 Specialization->setInvalidDecl(true);
1234 return TDK_SubstitutionFailure;
1235 }
Mike Stump1eb44332009-09-09 15:08:12 +00001236
1237 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001238}
1239
Douglas Gregore53060f2009-06-25 22:08:12 +00001240/// \brief Perform template argument deduction from a function call
1241/// (C++ [temp.deduct.call]).
1242///
1243/// \param FunctionTemplate the function template for which we are performing
1244/// template argument deduction.
1245///
Mike Stump1eb44332009-09-09 15:08:12 +00001246/// \param HasExplicitTemplateArgs whether any template arguments were
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001247/// explicitly specified.
1248///
1249/// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
1250/// the explicitly-specified template arguments.
1251///
1252/// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
Mike Stump1eb44332009-09-09 15:08:12 +00001253/// the number of explicitly-specified template arguments in
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001254/// @p ExplicitTemplateArguments. This value may be zero.
1255///
Douglas Gregore53060f2009-06-25 22:08:12 +00001256/// \param Args the function call arguments
1257///
1258/// \param NumArgs the number of arguments in Args
1259///
1260/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001261/// this will be set to the function template specialization produced by
Douglas Gregore53060f2009-06-25 22:08:12 +00001262/// template argument deduction.
1263///
1264/// \param Info the argument will be updated to provide additional information
1265/// about template argument deduction.
1266///
1267/// \returns the result of template argument deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001268Sema::TemplateDeductionResult
1269Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001270 bool HasExplicitTemplateArgs,
1271 const TemplateArgument *ExplicitTemplateArgs,
1272 unsigned NumExplicitTemplateArgs,
Douglas Gregore53060f2009-06-25 22:08:12 +00001273 Expr **Args, unsigned NumArgs,
1274 FunctionDecl *&Specialization,
1275 TemplateDeductionInfo &Info) {
1276 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001277
Douglas Gregore53060f2009-06-25 22:08:12 +00001278 // C++ [temp.deduct.call]p1:
1279 // Template argument deduction is done by comparing each function template
1280 // parameter type (call it P) with the type of the corresponding argument
1281 // of the call (call it A) as described below.
1282 unsigned CheckArgs = NumArgs;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001283 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregore53060f2009-06-25 22:08:12 +00001284 return TDK_TooFewArguments;
1285 else if (NumArgs > Function->getNumParams()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001286 const FunctionProtoType *Proto
Douglas Gregore53060f2009-06-25 22:08:12 +00001287 = Function->getType()->getAsFunctionProtoType();
1288 if (!Proto->isVariadic())
1289 return TDK_TooManyArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Douglas Gregore53060f2009-06-25 22:08:12 +00001291 CheckArgs = Function->getNumParams();
1292 }
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001294 // The types of the parameters from which we will perform template argument
1295 // deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001296 TemplateParameterList *TemplateParams
1297 = FunctionTemplate->getTemplateParameters();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001298 llvm::SmallVector<TemplateArgument, 4> Deduced;
1299 llvm::SmallVector<QualType, 4> ParamTypes;
1300 if (NumExplicitTemplateArgs) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001301 TemplateDeductionResult Result =
1302 SubstituteExplicitTemplateArguments(FunctionTemplate,
1303 ExplicitTemplateArgs,
1304 NumExplicitTemplateArgs,
1305 Deduced,
1306 ParamTypes,
1307 0,
1308 Info);
1309 if (Result)
1310 return Result;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001311 } else {
1312 // Just fill in the parameter types from the function declaration.
1313 for (unsigned I = 0; I != CheckArgs; ++I)
1314 ParamTypes.push_back(Function->getParamDecl(I)->getType());
1315 }
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001317 // Deduce template arguments from the function parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001318 Deduced.resize(TemplateParams->size());
Douglas Gregore53060f2009-06-25 22:08:12 +00001319 for (unsigned I = 0; I != CheckArgs; ++I) {
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001320 QualType ParamType = ParamTypes[I];
Douglas Gregore53060f2009-06-25 22:08:12 +00001321 QualType ArgType = Args[I]->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Douglas Gregore53060f2009-06-25 22:08:12 +00001323 // C++ [temp.deduct.call]p2:
1324 // If P is not a reference type:
1325 QualType CanonParamType = Context.getCanonicalType(ParamType);
Douglas Gregor500d3312009-06-26 18:27:22 +00001326 bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1327 if (!ParamWasReference) {
Mike Stump1eb44332009-09-09 15:08:12 +00001328 // - If A is an array type, the pointer type produced by the
1329 // array-to-pointer standard conversion (4.2) is used in place of
Douglas Gregore53060f2009-06-25 22:08:12 +00001330 // A for type deduction; otherwise,
1331 if (ArgType->isArrayType())
1332 ArgType = Context.getArrayDecayedType(ArgType);
Mike Stump1eb44332009-09-09 15:08:12 +00001333 // - If A is a function type, the pointer type produced by the
1334 // function-to-pointer standard conversion (4.3) is used in place
Douglas Gregore53060f2009-06-25 22:08:12 +00001335 // of A for type deduction; otherwise,
1336 else if (ArgType->isFunctionType())
1337 ArgType = Context.getPointerType(ArgType);
1338 else {
1339 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1340 // type are ignored for type deduction.
1341 QualType CanonArgType = Context.getCanonicalType(ArgType);
1342 if (CanonArgType.getCVRQualifiers())
1343 ArgType = CanonArgType.getUnqualifiedType();
1344 }
1345 }
Mike Stump1eb44332009-09-09 15:08:12 +00001346
Douglas Gregore53060f2009-06-25 22:08:12 +00001347 // C++0x [temp.deduct.call]p3:
1348 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
Mike Stump1eb44332009-09-09 15:08:12 +00001349 // are ignored for type deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001350 if (CanonParamType.getCVRQualifiers())
1351 ParamType = CanonParamType.getUnqualifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001352 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001353 // [...] If P is a reference type, the type referred to by P is used
1354 // for type deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001355 ParamType = ParamRefType->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00001356
1357 // [...] If P is of the form T&&, where T is a template parameter, and
1358 // the argument is an lvalue, the type A& is used in place of A for
Douglas Gregore53060f2009-06-25 22:08:12 +00001359 // type deduction.
1360 if (isa<RValueReferenceType>(ParamRefType) &&
1361 ParamRefType->getAsTemplateTypeParmType() &&
1362 Args[I]->isLvalue(Context) == Expr::LV_Valid)
1363 ArgType = Context.getLValueReferenceType(ArgType);
1364 }
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Douglas Gregore53060f2009-06-25 22:08:12 +00001366 // C++0x [temp.deduct.call]p4:
1367 // In general, the deduction process attempts to find template argument
1368 // values that will make the deduced A identical to A (after the type A
1369 // is transformed as described above). [...]
Douglas Gregor508f1c82009-06-26 23:10:12 +00001370 unsigned TDF = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001371
Douglas Gregor508f1c82009-06-26 23:10:12 +00001372 // - If the original P is a reference type, the deduced A (i.e., the
1373 // type referred to by the reference) can be more cv-qualified than
1374 // the transformed A.
1375 if (ParamWasReference)
1376 TDF |= TDF_ParamWithReferenceType;
Mike Stump1eb44332009-09-09 15:08:12 +00001377 // - The transformed A can be another pointer or pointer to member
1378 // type that can be converted to the deduced A via a qualification
Douglas Gregor508f1c82009-06-26 23:10:12 +00001379 // conversion (4.4).
1380 if (ArgType->isPointerType() || ArgType->isMemberPointerType())
1381 TDF |= TDF_IgnoreQualifiers;
Mike Stump1eb44332009-09-09 15:08:12 +00001382 // - If P is a class and P has the form simple-template-id, then the
Douglas Gregor41128772009-06-26 23:27:24 +00001383 // transformed A can be a derived class of the deduced A. Likewise,
1384 // if P is a pointer to a class of the form simple-template-id, the
1385 // transformed A can be a pointer to a derived class pointed to by
1386 // the deduced A.
1387 if (isSimpleTemplateIdType(ParamType) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001388 (isa<PointerType>(ParamType) &&
Douglas Gregor41128772009-06-26 23:27:24 +00001389 isSimpleTemplateIdType(
Ted Kremenek6217b802009-07-29 21:53:49 +00001390 ParamType->getAs<PointerType>()->getPointeeType())))
Douglas Gregor41128772009-06-26 23:27:24 +00001391 TDF |= TDF_DerivedClass;
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Douglas Gregore53060f2009-06-25 22:08:12 +00001393 if (TemplateDeductionResult Result
1394 = ::DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor500d3312009-06-26 18:27:22 +00001395 ParamType, ArgType, Info, Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +00001396 TDF))
Douglas Gregore53060f2009-06-25 22:08:12 +00001397 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001398
Douglas Gregor8fdc3c42009-07-07 23:12:18 +00001399 // FIXME: C++0x [temp.deduct.call] paragraphs 6-9 deal with function
Mike Stump1eb44332009-09-09 15:08:12 +00001400 // pointer parameters.
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001401
1402 // FIXME: we need to check that the deduced A is the same as A,
1403 // modulo the various allowed differences.
Douglas Gregore53060f2009-06-25 22:08:12 +00001404 }
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001405
Mike Stump1eb44332009-09-09 15:08:12 +00001406 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001407 Specialization, Info);
Douglas Gregore53060f2009-06-25 22:08:12 +00001408}
1409
Douglas Gregor83314aa2009-07-08 20:55:45 +00001410/// \brief Deduce template arguments when taking the address of a function
1411/// template (C++ [temp.deduct.funcaddr]).
1412///
1413/// \param FunctionTemplate the function template for which we are performing
1414/// template argument deduction.
1415///
Mike Stump1eb44332009-09-09 15:08:12 +00001416/// \param HasExplicitTemplateArgs whether any template arguments were
Douglas Gregor83314aa2009-07-08 20:55:45 +00001417/// explicitly specified.
1418///
1419/// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
1420/// the explicitly-specified template arguments.
1421///
1422/// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
Mike Stump1eb44332009-09-09 15:08:12 +00001423/// the number of explicitly-specified template arguments in
Douglas Gregor83314aa2009-07-08 20:55:45 +00001424/// @p ExplicitTemplateArguments. This value may be zero.
1425///
1426/// \param ArgFunctionType the function type that will be used as the
1427/// "argument" type (A) when performing template argument deduction from the
1428/// function template's function type.
1429///
1430/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001431/// this will be set to the function template specialization produced by
Douglas Gregor83314aa2009-07-08 20:55:45 +00001432/// template argument deduction.
1433///
1434/// \param Info the argument will be updated to provide additional information
1435/// about template argument deduction.
1436///
1437/// \returns the result of template argument deduction.
1438Sema::TemplateDeductionResult
1439Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1440 bool HasExplicitTemplateArgs,
1441 const TemplateArgument *ExplicitTemplateArgs,
1442 unsigned NumExplicitTemplateArgs,
1443 QualType ArgFunctionType,
1444 FunctionDecl *&Specialization,
1445 TemplateDeductionInfo &Info) {
1446 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1447 TemplateParameterList *TemplateParams
1448 = FunctionTemplate->getTemplateParameters();
1449 QualType FunctionType = Function->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Douglas Gregor83314aa2009-07-08 20:55:45 +00001451 // Substitute any explicit template arguments.
1452 llvm::SmallVector<TemplateArgument, 4> Deduced;
1453 llvm::SmallVector<QualType, 4> ParamTypes;
1454 if (HasExplicitTemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001455 if (TemplateDeductionResult Result
1456 = SubstituteExplicitTemplateArguments(FunctionTemplate,
1457 ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001458 NumExplicitTemplateArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00001459 Deduced, ParamTypes,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001460 &FunctionType, Info))
1461 return Result;
1462 }
1463
1464 // Template argument deduction for function templates in a SFINAE context.
1465 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001466 SFINAETrap Trap(*this);
1467
Douglas Gregor83314aa2009-07-08 20:55:45 +00001468 // Deduce template arguments from the function type.
Mike Stump1eb44332009-09-09 15:08:12 +00001469 Deduced.resize(TemplateParams->size());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001470 if (TemplateDeductionResult Result
1471 = ::DeduceTemplateArguments(Context, TemplateParams,
Mike Stump1eb44332009-09-09 15:08:12 +00001472 FunctionType, ArgFunctionType, Info,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001473 Deduced, 0))
1474 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001475
1476 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001477 Specialization, Info);
1478}
1479
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001480/// \brief Deduce template arguments for a templated conversion
1481/// function (C++ [temp.deduct.conv]) and, if successful, produce a
1482/// conversion function template specialization.
1483Sema::TemplateDeductionResult
1484Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1485 QualType ToType,
1486 CXXConversionDecl *&Specialization,
1487 TemplateDeductionInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00001488 CXXConversionDecl *Conv
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001489 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
1490 QualType FromType = Conv->getConversionType();
1491
1492 // Canonicalize the types for deduction.
1493 QualType P = Context.getCanonicalType(FromType);
1494 QualType A = Context.getCanonicalType(ToType);
1495
1496 // C++0x [temp.deduct.conv]p3:
1497 // If P is a reference type, the type referred to by P is used for
1498 // type deduction.
1499 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
1500 P = PRef->getPointeeType();
1501
1502 // C++0x [temp.deduct.conv]p3:
1503 // If A is a reference type, the type referred to by A is used
1504 // for type deduction.
1505 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
1506 A = ARef->getPointeeType();
1507 // C++ [temp.deduct.conv]p2:
1508 //
Mike Stump1eb44332009-09-09 15:08:12 +00001509 // If A is not a reference type:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001510 else {
1511 assert(!A->isReferenceType() && "Reference types were handled above");
1512
1513 // - If P is an array type, the pointer type produced by the
Mike Stump1eb44332009-09-09 15:08:12 +00001514 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001515 // of P for type deduction; otherwise,
1516 if (P->isArrayType())
1517 P = Context.getArrayDecayedType(P);
1518 // - If P is a function type, the pointer type produced by the
1519 // function-to-pointer standard conversion (4.3) is used in
1520 // place of P for type deduction; otherwise,
1521 else if (P->isFunctionType())
1522 P = Context.getPointerType(P);
1523 // - If P is a cv-qualified type, the top level cv-qualifiers of
1524 // P’s type are ignored for type deduction.
1525 else
1526 P = P.getUnqualifiedType();
1527
1528 // C++0x [temp.deduct.conv]p3:
1529 // If A is a cv-qualified type, the top level cv-qualifiers of A’s
1530 // type are ignored for type deduction.
1531 A = A.getUnqualifiedType();
1532 }
1533
1534 // Template argument deduction for function templates in a SFINAE context.
1535 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001536 SFINAETrap Trap(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001537
1538 // C++ [temp.deduct.conv]p1:
1539 // Template argument deduction is done by comparing the return
1540 // type of the template conversion function (call it P) with the
1541 // type that is required as the result of the conversion (call it
1542 // A) as described in 14.8.2.4.
1543 TemplateParameterList *TemplateParams
1544 = FunctionTemplate->getTemplateParameters();
1545 llvm::SmallVector<TemplateArgument, 4> Deduced;
Mike Stump1eb44332009-09-09 15:08:12 +00001546 Deduced.resize(TemplateParams->size());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001547
1548 // C++0x [temp.deduct.conv]p4:
1549 // In general, the deduction process attempts to find template
1550 // argument values that will make the deduced A identical to
1551 // A. However, there are two cases that allow a difference:
1552 unsigned TDF = 0;
1553 // - If the original A is a reference type, A can be more
1554 // cv-qualified than the deduced A (i.e., the type referred to
1555 // by the reference)
1556 if (ToType->isReferenceType())
1557 TDF |= TDF_ParamWithReferenceType;
1558 // - The deduced A can be another pointer or pointer to member
1559 // type that can be converted to A via a qualification
1560 // conversion.
1561 //
1562 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
1563 // both P and A are pointers or member pointers. In this case, we
1564 // just ignore cv-qualifiers completely).
1565 if ((P->isPointerType() && A->isPointerType()) ||
1566 (P->isMemberPointerType() && P->isMemberPointerType()))
1567 TDF |= TDF_IgnoreQualifiers;
1568 if (TemplateDeductionResult Result
1569 = ::DeduceTemplateArguments(Context, TemplateParams,
1570 P, A, Info, Deduced, TDF))
1571 return Result;
1572
1573 // FIXME: we need to check that the deduced A is the same as A,
1574 // modulo the various allowed differences.
Mike Stump1eb44332009-09-09 15:08:12 +00001575
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001576 // Finish template argument deduction.
1577 FunctionDecl *Spec = 0;
1578 TemplateDeductionResult Result
1579 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, Spec, Info);
1580 Specialization = cast_or_null<CXXConversionDecl>(Spec);
1581 return Result;
1582}
1583
1584/// \brief Returns the more specialization function template according
1585/// to the rules of function template partial ordering (C++ [temp.func.order]).
1586///
1587/// \param FT1 the first function template
1588///
1589/// \param FT2 the second function template
1590///
1591/// \param isCallContext whether partial ordering is being performed
1592/// for a function call (which ignores the return types of the
1593/// functions).
Mike Stump1eb44332009-09-09 15:08:12 +00001594///
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001595/// \returns the more specialization function template. If neither
1596/// template is more specialized, returns NULL.
1597FunctionTemplateDecl *
1598Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
1599 FunctionTemplateDecl *FT2,
1600 bool isCallContext) {
1601#if 0
1602 // FIXME: Implement this
1603 bool Better1 = isAtLeastAsSpecializedAs(*this, FT1, FT2, isCallContext);
1604 bool Better2 = isAtLeastAsSpecializedAs(*this, FT2, FT1, isCallContext);
1605 if (Better1 == Better2)
1606 return 0;
1607 if (Better1)
1608 return FT1;
1609 return FT2;
1610#else
1611 Diag(SourceLocation(), diag::unsup_function_template_partial_ordering);
1612 return 0;
1613#endif
1614}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001615
Mike Stump1eb44332009-09-09 15:08:12 +00001616static void
Douglas Gregor031a5882009-06-13 00:26:55 +00001617MarkDeducedTemplateParameters(Sema &SemaRef,
1618 const TemplateArgument &TemplateArg,
1619 llvm::SmallVectorImpl<bool> &Deduced);
1620
1621/// \brief Mark the template arguments that are deduced by the given
1622/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001623static void
1624MarkDeducedTemplateParameters(const Expr *E,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001625 llvm::SmallVectorImpl<bool> &Deduced) {
1626 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregor031a5882009-06-13 00:26:55 +00001627 if (!E)
1628 return;
1629
Mike Stump1eb44332009-09-09 15:08:12 +00001630 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00001631 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
1632 if (!NTTP)
1633 return;
1634
1635 Deduced[NTTP->getIndex()] = true;
1636}
1637
1638/// \brief Mark the template parameters that are deduced by the given
1639/// type.
Mike Stump1eb44332009-09-09 15:08:12 +00001640static void
Douglas Gregor031a5882009-06-13 00:26:55 +00001641MarkDeducedTemplateParameters(Sema &SemaRef, QualType T,
1642 llvm::SmallVectorImpl<bool> &Deduced) {
1643 // Non-dependent types have nothing deducible
1644 if (!T->isDependentType())
1645 return;
1646
1647 T = SemaRef.Context.getCanonicalType(T);
1648 switch (T->getTypeClass()) {
1649 case Type::ExtQual:
Mike Stump1eb44332009-09-09 15:08:12 +00001650 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001651 QualType(cast<ExtQualType>(T)->getBaseType(), 0),
Douglas Gregor031a5882009-06-13 00:26:55 +00001652 Deduced);
1653 break;
1654
1655 case Type::Pointer:
1656 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001657 cast<PointerType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001658 Deduced);
1659 break;
1660
1661 case Type::BlockPointer:
1662 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001663 cast<BlockPointerType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001664 Deduced);
1665 break;
1666
1667 case Type::LValueReference:
1668 case Type::RValueReference:
1669 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001670 cast<ReferenceType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001671 Deduced);
1672 break;
1673
1674 case Type::MemberPointer: {
1675 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
1676 MarkDeducedTemplateParameters(SemaRef, MemPtr->getPointeeType(), Deduced);
1677 MarkDeducedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
1678 Deduced);
1679 break;
1680 }
1681
1682 case Type::DependentSizedArray:
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001683 MarkDeducedTemplateParameters(cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001684 Deduced);
1685 // Fall through to check the element type
1686
1687 case Type::ConstantArray:
1688 case Type::IncompleteArray:
1689 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001690 cast<ArrayType>(T)->getElementType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001691 Deduced);
1692 break;
1693
1694 case Type::Vector:
1695 case Type::ExtVector:
1696 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001697 cast<VectorType>(T)->getElementType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001698 Deduced);
1699 break;
1700
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001701 case Type::DependentSizedExtVector: {
1702 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001703 = cast<DependentSizedExtVectorType>(T);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001704 MarkDeducedTemplateParameters(SemaRef, VecType->getElementType(), Deduced);
1705 MarkDeducedTemplateParameters(VecType->getSizeExpr(), Deduced);
1706 break;
1707 }
1708
Douglas Gregor031a5882009-06-13 00:26:55 +00001709 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001710 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregor031a5882009-06-13 00:26:55 +00001711 MarkDeducedTemplateParameters(SemaRef, Proto->getResultType(), Deduced);
1712 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
1713 MarkDeducedTemplateParameters(SemaRef, Proto->getArgType(I), Deduced);
1714 break;
1715 }
1716
1717 case Type::TemplateTypeParm:
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001718 Deduced[cast<TemplateTypeParmType>(T)->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00001719 break;
1720
1721 case Type::TemplateSpecialization: {
Mike Stump1eb44332009-09-09 15:08:12 +00001722 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001723 = cast<TemplateSpecializationType>(T);
Douglas Gregor031a5882009-06-13 00:26:55 +00001724 if (TemplateDecl *Template = Spec->getTemplateName().getAsTemplateDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00001725 if (TemplateTemplateParmDecl *TTP
Douglas Gregor031a5882009-06-13 00:26:55 +00001726 = dyn_cast<TemplateTemplateParmDecl>(Template))
1727 Deduced[TTP->getIndex()] = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001728
Douglas Gregor031a5882009-06-13 00:26:55 +00001729 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
1730 MarkDeducedTemplateParameters(SemaRef, Spec->getArg(I), Deduced);
1731
1732 break;
1733 }
1734
1735 // None of these types have any deducible parts.
1736 case Type::Builtin:
1737 case Type::FixedWidthInt:
1738 case Type::Complex:
1739 case Type::VariableArray:
1740 case Type::FunctionNoProto:
1741 case Type::Record:
1742 case Type::Enum:
1743 case Type::Typename:
1744 case Type::ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001745 case Type::ObjCObjectPointer:
Douglas Gregor031a5882009-06-13 00:26:55 +00001746#define TYPE(Class, Base)
1747#define ABSTRACT_TYPE(Class, Base)
1748#define DEPENDENT_TYPE(Class, Base)
1749#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
1750#include "clang/AST/TypeNodes.def"
1751 break;
1752 }
1753}
1754
1755/// \brief Mark the template parameters that are deduced by this
1756/// template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00001757static void
Douglas Gregor031a5882009-06-13 00:26:55 +00001758MarkDeducedTemplateParameters(Sema &SemaRef,
1759 const TemplateArgument &TemplateArg,
1760 llvm::SmallVectorImpl<bool> &Deduced) {
1761 switch (TemplateArg.getKind()) {
1762 case TemplateArgument::Null:
1763 case TemplateArgument::Integral:
1764 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001765
Douglas Gregor031a5882009-06-13 00:26:55 +00001766 case TemplateArgument::Type:
1767 MarkDeducedTemplateParameters(SemaRef, TemplateArg.getAsType(), Deduced);
1768 break;
1769
1770 case TemplateArgument::Declaration:
Mike Stump1eb44332009-09-09 15:08:12 +00001771 if (TemplateTemplateParmDecl *TTP
Douglas Gregor031a5882009-06-13 00:26:55 +00001772 = dyn_cast<TemplateTemplateParmDecl>(TemplateArg.getAsDecl()))
1773 Deduced[TTP->getIndex()] = true;
1774 break;
1775
1776 case TemplateArgument::Expression:
1777 MarkDeducedTemplateParameters(TemplateArg.getAsExpr(), Deduced);
1778 break;
Anders Carlssond01b1da2009-06-15 17:04:53 +00001779 case TemplateArgument::Pack:
1780 assert(0 && "FIXME: Implement!");
1781 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00001782 }
1783}
1784
1785/// \brief Mark the template parameters can be deduced by the given
1786/// template argument list.
1787///
1788/// \param TemplateArgs the template argument list from which template
1789/// parameters will be deduced.
1790///
1791/// \param Deduced a bit vector whose elements will be set to \c true
1792/// to indicate when the corresponding template parameter will be
1793/// deduced.
Mike Stump1eb44332009-09-09 15:08:12 +00001794void
Douglas Gregor031a5882009-06-13 00:26:55 +00001795Sema::MarkDeducedTemplateParameters(const TemplateArgumentList &TemplateArgs,
1796 llvm::SmallVectorImpl<bool> &Deduced) {
1797 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
1798 ::MarkDeducedTemplateParameters(*this, TemplateArgs[I], Deduced);
1799}