blob: 1ade29869db987dec490d685c3e61699bf0af493 [file] [log] [blame]
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001//===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template argument deduction.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/StmtVisitor.h"
17#include "clang/AST/Expr.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/Parse/DeclSpec.h"
20#include "llvm/Support/Compiler.h"
Douglas Gregor8a514912009-09-14 18:39:43 +000021#include <algorithm>
Douglas Gregor508f1c82009-06-26 23:10:12 +000022
23namespace clang {
24 /// \brief Various flags that control template argument deduction.
25 ///
26 /// These flags can be bitwise-OR'd together.
27 enum TemplateDeductionFlags {
28 /// \brief No template argument deduction flags, which indicates the
29 /// strictest results for template argument deduction (as used for, e.g.,
30 /// matching class template partial specializations).
31 TDF_None = 0,
32 /// \brief Within template argument deduction from a function call, we are
33 /// matching with a parameter type for which the original parameter was
34 /// a reference.
35 TDF_ParamWithReferenceType = 0x1,
36 /// \brief Within template argument deduction from a function call, we
37 /// are matching in a case where we ignore cv-qualifiers.
38 TDF_IgnoreQualifiers = 0x02,
39 /// \brief Within template argument deduction from a function call,
40 /// we are matching in a case where we can perform template argument
Douglas Gregor41128772009-06-26 23:27:24 +000041 /// deduction from a template-id of a derived class of the argument type.
Douglas Gregor508f1c82009-06-26 23:10:12 +000042 TDF_DerivedClass = 0x04
43 };
44}
45
Douglas Gregor0b9247f2009-06-04 00:03:07 +000046using namespace clang;
47
Douglas Gregorf67875d2009-06-12 18:26:56 +000048static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +000049DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +000050 TemplateParameterList *TemplateParams,
51 const TemplateArgument &Param,
Douglas Gregord708c722009-06-09 16:35:58 +000052 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +000053 Sema::TemplateDeductionInfo &Info,
Douglas Gregord708c722009-06-09 16:35:58 +000054 llvm::SmallVectorImpl<TemplateArgument> &Deduced);
55
Douglas Gregor199d9912009-06-05 00:53:49 +000056/// \brief If the given expression is of a form that permits the deduction
57/// of a non-type template parameter, return the declaration of that
58/// non-type template parameter.
59static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
60 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
61 E = IC->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +000062
Douglas Gregor199d9912009-06-05 00:53:49 +000063 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
64 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +000065
Douglas Gregor199d9912009-06-05 00:53:49 +000066 return 0;
67}
68
Mike Stump1eb44332009-09-09 15:08:12 +000069/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +000070/// from the given constant.
Douglas Gregorf67875d2009-06-12 18:26:56 +000071static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +000072DeduceNonTypeTemplateArgument(ASTContext &Context,
73 NonTypeTemplateParmDecl *NTTP,
Anders Carlsson335e24a2009-06-16 22:44:31 +000074 llvm::APSInt Value,
Douglas Gregorf67875d2009-06-12 18:26:56 +000075 Sema::TemplateDeductionInfo &Info,
76 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +000077 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +000078 "Cannot deduce non-type template argument with depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +000079
Douglas Gregor199d9912009-06-05 00:53:49 +000080 if (Deduced[NTTP->getIndex()].isNull()) {
Anders Carlsson25af1ed2009-06-16 23:08:29 +000081 QualType T = NTTP->getType();
Mike Stump1eb44332009-09-09 15:08:12 +000082
Anders Carlsson25af1ed2009-06-16 23:08:29 +000083 // FIXME: Make sure we didn't overflow our data type!
84 unsigned AllowedBits = Context.getTypeSize(T);
85 if (Value.getBitWidth() != AllowedBits)
86 Value.extOrTrunc(AllowedBits);
87 Value.setIsSigned(T->isSignedIntegerType());
88
89 Deduced[NTTP->getIndex()] = TemplateArgument(SourceLocation(), Value, T);
Douglas Gregorf67875d2009-06-12 18:26:56 +000090 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +000091 }
Mike Stump1eb44332009-09-09 15:08:12 +000092
Douglas Gregorf67875d2009-06-12 18:26:56 +000093 assert(Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral);
Mike Stump1eb44332009-09-09 15:08:12 +000094
95 // If the template argument was previously deduced to a negative value,
Douglas Gregor199d9912009-06-05 00:53:49 +000096 // then our deduction fails.
97 const llvm::APSInt *PrevValuePtr = Deduced[NTTP->getIndex()].getAsIntegral();
Anders Carlsson335e24a2009-06-16 22:44:31 +000098 if (PrevValuePtr->isNegative()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +000099 Info.Param = NTTP;
100 Info.FirstArg = Deduced[NTTP->getIndex()];
Anders Carlsson335e24a2009-06-16 22:44:31 +0000101 Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000102 return Sema::TDK_Inconsistent;
103 }
104
Anders Carlsson335e24a2009-06-16 22:44:31 +0000105 llvm::APSInt PrevValue = *PrevValuePtr;
Douglas Gregor199d9912009-06-05 00:53:49 +0000106 if (Value.getBitWidth() > PrevValue.getBitWidth())
107 PrevValue.zext(Value.getBitWidth());
108 else if (Value.getBitWidth() < PrevValue.getBitWidth())
109 Value.zext(PrevValue.getBitWidth());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000110
111 if (Value != PrevValue) {
112 Info.Param = NTTP;
113 Info.FirstArg = Deduced[NTTP->getIndex()];
Anders Carlsson335e24a2009-06-16 22:44:31 +0000114 Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000115 return Sema::TDK_Inconsistent;
116 }
117
118 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000119}
120
Mike Stump1eb44332009-09-09 15:08:12 +0000121/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +0000122/// from the given type- or value-dependent expression.
123///
124/// \returns true if deduction succeeded, false otherwise.
125
Douglas Gregorf67875d2009-06-12 18:26:56 +0000126static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +0000127DeduceNonTypeTemplateArgument(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000128 NonTypeTemplateParmDecl *NTTP,
129 Expr *Value,
130 Sema::TemplateDeductionInfo &Info,
131 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +0000132 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000133 "Cannot deduce non-type template argument with depth > 0");
134 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
135 "Expression template argument must be type- or value-dependent.");
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Douglas Gregor199d9912009-06-05 00:53:49 +0000137 if (Deduced[NTTP->getIndex()].isNull()) {
138 // FIXME: Clone the Value?
139 Deduced[NTTP->getIndex()] = TemplateArgument(Value);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000140 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000141 }
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Douglas Gregor199d9912009-06-05 00:53:49 +0000143 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
Mike Stump1eb44332009-09-09 15:08:12 +0000144 // Okay, we deduced a constant in one case and a dependent expression
145 // in another case. FIXME: Later, we will check that instantiating the
Douglas Gregor199d9912009-06-05 00:53:49 +0000146 // dependent expression gives us the constant value.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000147 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000148 }
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Douglas Gregor199d9912009-06-05 00:53:49 +0000150 // FIXME: Compare the expressions for equality!
Douglas Gregorf67875d2009-06-12 18:26:56 +0000151 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000152}
153
Douglas Gregorf67875d2009-06-12 18:26:56 +0000154static Sema::TemplateDeductionResult
155DeduceTemplateArguments(ASTContext &Context,
156 TemplateName Param,
157 TemplateName Arg,
158 Sema::TemplateDeductionInfo &Info,
159 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregord708c722009-06-09 16:35:58 +0000160 // FIXME: Implement template argument deduction for template
161 // template parameters.
162
Douglas Gregorf67875d2009-06-12 18:26:56 +0000163 // FIXME: this routine does not have enough information to produce
164 // good diagnostics.
165
Douglas Gregord708c722009-06-09 16:35:58 +0000166 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
167 TemplateDecl *ArgDecl = Arg.getAsTemplateDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Douglas Gregorf67875d2009-06-12 18:26:56 +0000169 if (!ParamDecl || !ArgDecl) {
170 // FIXME: fill in Info.Param/Info.FirstArg
171 return Sema::TDK_Inconsistent;
172 }
Douglas Gregord708c722009-06-09 16:35:58 +0000173
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000174 ParamDecl = cast<TemplateDecl>(ParamDecl->getCanonicalDecl());
175 ArgDecl = cast<TemplateDecl>(ArgDecl->getCanonicalDecl());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000176 if (ParamDecl != ArgDecl) {
177 // FIXME: fill in Info.Param/Info.FirstArg
178 return Sema::TDK_Inconsistent;
179 }
180
181 return Sema::TDK_Success;
Douglas Gregord708c722009-06-09 16:35:58 +0000182}
183
Mike Stump1eb44332009-09-09 15:08:12 +0000184/// \brief Deduce the template arguments by comparing the template parameter
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000185/// type (which is a template-id) with the template argument type.
186///
187/// \param Context the AST context in which this deduction occurs.
188///
189/// \param TemplateParams the template parameters that we are deducing
190///
191/// \param Param the parameter type
192///
193/// \param Arg the argument type
194///
195/// \param Info information about the template argument deduction itself
196///
197/// \param Deduced the deduced template arguments
198///
199/// \returns the result of template argument deduction so far. Note that a
200/// "success" result means that template argument deduction has not yet failed,
201/// but it may still fail, later, for other reasons.
202static Sema::TemplateDeductionResult
203DeduceTemplateArguments(ASTContext &Context,
204 TemplateParameterList *TemplateParams,
205 const TemplateSpecializationType *Param,
206 QualType Arg,
207 Sema::TemplateDeductionInfo &Info,
208 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
209 assert(Arg->isCanonical() && "Argument type must be canonical");
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000211 // Check whether the template argument is a dependent template-id.
212 // FIXME: This is untested code; it can be tested when we implement
213 // partial ordering of class template partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +0000214 if (const TemplateSpecializationType *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000215 = dyn_cast<TemplateSpecializationType>(Arg)) {
216 // Perform template argument deduction for the template name.
217 if (Sema::TemplateDeductionResult Result
218 = DeduceTemplateArguments(Context,
219 Param->getTemplateName(),
220 SpecArg->getTemplateName(),
221 Info, Deduced))
222 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000224 unsigned NumArgs = Param->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000226 // FIXME: When one of the template-names refers to a
227 // declaration with default template arguments, do we need to
228 // fill in those default template arguments here? Most likely,
229 // the answer is "yes", but I don't see any references. This
230 // issue may be resolved elsewhere, because we may want to
231 // instantiate default template arguments when we actually write
232 // the template-id.
233 if (SpecArg->getNumArgs() != NumArgs)
234 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000236 // Perform template argument deduction on each template
237 // argument.
238 for (unsigned I = 0; I != NumArgs; ++I)
239 if (Sema::TemplateDeductionResult Result
240 = DeduceTemplateArguments(Context, TemplateParams,
241 Param->getArg(I),
242 SpecArg->getArg(I),
243 Info, Deduced))
244 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000246 return Sema::TDK_Success;
247 }
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000249 // If the argument type is a class template specialization, we
250 // perform template argument deduction using its template
251 // arguments.
252 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
253 if (!RecordArg)
254 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000255
256 ClassTemplateSpecializationDecl *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000257 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
258 if (!SpecArg)
259 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000261 // Perform template argument deduction for the template name.
262 if (Sema::TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000263 = DeduceTemplateArguments(Context,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000264 Param->getTemplateName(),
265 TemplateName(SpecArg->getSpecializedTemplate()),
266 Info, Deduced))
267 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000269 // FIXME: Can the # of arguments in the parameter and the argument
270 // differ due to default arguments?
271 unsigned NumArgs = Param->getNumArgs();
272 const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
273 if (NumArgs != ArgArgs.size())
274 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000276 for (unsigned I = 0; I != NumArgs; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000277 if (Sema::TemplateDeductionResult Result
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000278 = DeduceTemplateArguments(Context, TemplateParams,
279 Param->getArg(I),
280 ArgArgs.get(I),
281 Info, Deduced))
282 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000284 return Sema::TDK_Success;
285}
286
Mike Stump1eb44332009-09-09 15:08:12 +0000287/// \brief Returns a completely-unqualified array type, capturing the
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000288/// qualifiers in CVRQuals.
289///
290/// \param Context the AST context in which the array type was built.
291///
292/// \param T a canonical type that may be an array type.
293///
294/// \param CVRQuals will receive the set of const/volatile/restrict qualifiers
295/// that were applied to the element type of the array.
296///
297/// \returns if \p T is an array type, the completely unqualified array type
298/// that corresponds to T. Otherwise, returns T.
299static QualType getUnqualifiedArrayType(ASTContext &Context, QualType T,
300 unsigned &CVRQuals) {
301 assert(T->isCanonical() && "Only operates on canonical types");
302 if (!isa<ArrayType>(T)) {
303 CVRQuals = T.getCVRQualifiers();
304 return T.getUnqualifiedType();
305 }
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000307 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) {
308 QualType Elt = getUnqualifiedArrayType(Context, CAT->getElementType(),
309 CVRQuals);
310 if (Elt == CAT->getElementType())
311 return T;
312
Mike Stump1eb44332009-09-09 15:08:12 +0000313 return Context.getConstantArrayType(Elt, CAT->getSize(),
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000314 CAT->getSizeModifier(), 0);
315 }
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000317 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) {
318 QualType Elt = getUnqualifiedArrayType(Context, IAT->getElementType(),
319 CVRQuals);
320 if (Elt == IAT->getElementType())
321 return T;
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000323 return Context.getIncompleteArrayType(Elt, IAT->getSizeModifier(), 0);
324 }
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000326 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T);
327 QualType Elt = getUnqualifiedArrayType(Context, DSAT->getElementType(),
328 CVRQuals);
329 if (Elt == DSAT->getElementType())
330 return T;
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Anders Carlssond4972062009-08-08 02:50:17 +0000332 return Context.getDependentSizedArrayType(Elt, DSAT->getSizeExpr()->Retain(),
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000333 DSAT->getSizeModifier(), 0,
334 SourceRange());
335}
336
Douglas Gregor500d3312009-06-26 18:27:22 +0000337/// \brief Deduce the template arguments by comparing the parameter type and
338/// the argument type (C++ [temp.deduct.type]).
339///
340/// \param Context the AST context in which this deduction occurs.
341///
342/// \param TemplateParams the template parameters that we are deducing
343///
344/// \param ParamIn the parameter type
345///
346/// \param ArgIn the argument type
347///
348/// \param Info information about the template argument deduction itself
349///
350/// \param Deduced the deduced template arguments
351///
Douglas Gregor508f1c82009-06-26 23:10:12 +0000352/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
Mike Stump1eb44332009-09-09 15:08:12 +0000353/// how template argument deduction is performed.
Douglas Gregor500d3312009-06-26 18:27:22 +0000354///
355/// \returns the result of template argument deduction so far. Note that a
356/// "success" result means that template argument deduction has not yet failed,
357/// but it may still fail, later, for other reasons.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000358static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +0000359DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000360 TemplateParameterList *TemplateParams,
361 QualType ParamIn, QualType ArgIn,
362 Sema::TemplateDeductionInfo &Info,
Douglas Gregor500d3312009-06-26 18:27:22 +0000363 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000364 unsigned TDF) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000365 // We only want to look at the canonical types, since typedefs and
366 // sugar are not part of template argument deduction.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000367 QualType Param = Context.getCanonicalType(ParamIn);
368 QualType Arg = Context.getCanonicalType(ArgIn);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000369
Douglas Gregor500d3312009-06-26 18:27:22 +0000370 // C++0x [temp.deduct.call]p4 bullet 1:
371 // - If the original P is a reference type, the deduced A (i.e., the type
Mike Stump1eb44332009-09-09 15:08:12 +0000372 // referred to by the reference) can be more cv-qualified than the
Douglas Gregor500d3312009-06-26 18:27:22 +0000373 // transformed A.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000374 if (TDF & TDF_ParamWithReferenceType) {
Mike Stump1eb44332009-09-09 15:08:12 +0000375 unsigned ExtraQualsOnParam
Douglas Gregor500d3312009-06-26 18:27:22 +0000376 = Param.getCVRQualifiers() & ~Arg.getCVRQualifiers();
377 Param.setCVRQualifiers(Param.getCVRQualifiers() & ~ExtraQualsOnParam);
378 }
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000380 // If the parameter type is not dependent, there is nothing to deduce.
381 if (!Param->isDependentType())
382 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000383
Douglas Gregor199d9912009-06-05 00:53:49 +0000384 // C++ [temp.deduct.type]p9:
Mike Stump1eb44332009-09-09 15:08:12 +0000385 // A template type argument T, a template template argument TT or a
386 // template non-type argument i can be deduced if P and A have one of
Douglas Gregor199d9912009-06-05 00:53:49 +0000387 // the following forms:
388 //
389 // T
390 // cv-list T
Mike Stump1eb44332009-09-09 15:08:12 +0000391 if (const TemplateTypeParmType *TemplateTypeParm
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000392 = Param->getAsTemplateTypeParmType()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000393 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000394 bool RecanonicalizeArg = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Douglas Gregor9e9fae42009-07-22 20:02:25 +0000396 // If the argument type is an array type, move the qualifiers up to the
397 // top level, so they can be matched with the qualifiers on the parameter.
398 // FIXME: address spaces, ObjC GC qualifiers
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000399 if (isa<ArrayType>(Arg)) {
400 unsigned CVRQuals = 0;
401 Arg = getUnqualifiedArrayType(Context, Arg, CVRQuals);
402 if (CVRQuals) {
403 Arg = Arg.getWithAdditionalQualifiers(CVRQuals);
404 RecanonicalizeArg = true;
405 }
406 }
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000408 // The argument type can not be less qualified than the parameter
409 // type.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000410 if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000411 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
412 Info.FirstArg = Deduced[Index];
413 Info.SecondArg = TemplateArgument(SourceLocation(), Arg);
414 return Sema::TDK_InconsistentQuals;
415 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000416
417 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000419 unsigned Quals = Arg.getCVRQualifiers() & ~Param.getCVRQualifiers();
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000420 QualType DeducedType = Arg.getQualifiedType(Quals);
421 if (RecanonicalizeArg)
422 DeducedType = Context.getCanonicalType(DeducedType);
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000424 if (Deduced[Index].isNull())
425 Deduced[Index] = TemplateArgument(SourceLocation(), DeducedType);
426 else {
Mike Stump1eb44332009-09-09 15:08:12 +0000427 // C++ [temp.deduct.type]p2:
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000428 // [...] If type deduction cannot be done for any P/A pair, or if for
Mike Stump1eb44332009-09-09 15:08:12 +0000429 // any pair the deduction leads to more than one possible set of
430 // deduced values, or if different pairs yield different deduced
431 // values, or if any template argument remains neither deduced nor
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000432 // explicitly specified, template argument deduction fails.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000433 if (Deduced[Index].getAsType() != DeducedType) {
Mike Stump1eb44332009-09-09 15:08:12 +0000434 Info.Param
Douglas Gregorf67875d2009-06-12 18:26:56 +0000435 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
436 Info.FirstArg = Deduced[Index];
437 Info.SecondArg = TemplateArgument(SourceLocation(), Arg);
438 return Sema::TDK_Inconsistent;
439 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000440 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000441 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000442 }
443
Douglas Gregorf67875d2009-06-12 18:26:56 +0000444 // Set up the template argument deduction information for a failure.
445 Info.FirstArg = TemplateArgument(SourceLocation(), ParamIn);
446 Info.SecondArg = TemplateArgument(SourceLocation(), ArgIn);
447
Douglas Gregor508f1c82009-06-26 23:10:12 +0000448 // Check the cv-qualifiers on the parameter and argument types.
449 if (!(TDF & TDF_IgnoreQualifiers)) {
450 if (TDF & TDF_ParamWithReferenceType) {
451 if (Param.isMoreQualifiedThan(Arg))
452 return Sema::TDK_NonDeducedMismatch;
453 } else {
454 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Mike Stump1eb44332009-09-09 15:08:12 +0000455 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor508f1c82009-06-26 23:10:12 +0000456 }
457 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000458
Douglas Gregord560d502009-06-04 00:21:18 +0000459 switch (Param->getTypeClass()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000460 // No deduction possible for these types
461 case Type::Builtin:
Douglas Gregorf67875d2009-06-12 18:26:56 +0000462 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Douglas Gregor199d9912009-06-05 00:53:49 +0000464 // T *
Douglas Gregord560d502009-06-04 00:21:18 +0000465 case Type::Pointer: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000466 const PointerType *PointerArg = Arg->getAs<PointerType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000467 if (!PointerArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000468 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Douglas Gregor41128772009-06-26 23:27:24 +0000470 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000471 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000472 cast<PointerType>(Param)->getPointeeType(),
473 PointerArg->getPointeeType(),
Douglas Gregor41128772009-06-26 23:27:24 +0000474 Info, Deduced, SubTDF);
Douglas Gregord560d502009-06-04 00:21:18 +0000475 }
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Douglas Gregor199d9912009-06-05 00:53:49 +0000477 // T &
Douglas Gregord560d502009-06-04 00:21:18 +0000478 case Type::LValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000479 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000480 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000481 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Douglas Gregorf67875d2009-06-12 18:26:56 +0000483 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000484 cast<LValueReferenceType>(Param)->getPointeeType(),
485 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000486 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000487 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000488
Douglas Gregor199d9912009-06-05 00:53:49 +0000489 // T && [C++0x]
Douglas Gregord560d502009-06-04 00:21:18 +0000490 case Type::RValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000491 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000492 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000493 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Douglas Gregorf67875d2009-06-12 18:26:56 +0000495 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000496 cast<RValueReferenceType>(Param)->getPointeeType(),
497 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000498 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000499 }
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Douglas Gregor199d9912009-06-05 00:53:49 +0000501 // T [] (implied, but not stated explicitly)
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000502 case Type::IncompleteArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000503 const IncompleteArrayType *IncompleteArrayArg =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000504 Context.getAsIncompleteArrayType(Arg);
505 if (!IncompleteArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000506 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Douglas Gregorf67875d2009-06-12 18:26:56 +0000508 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000509 Context.getAsIncompleteArrayType(Param)->getElementType(),
510 IncompleteArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000511 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000512 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000513
514 // T [integer-constant]
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000515 case Type::ConstantArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000516 const ConstantArrayType *ConstantArrayArg =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000517 Context.getAsConstantArrayType(Arg);
518 if (!ConstantArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000519 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000520
521 const ConstantArrayType *ConstantArrayParm =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000522 Context.getAsConstantArrayType(Param);
523 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000524 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Douglas Gregorf67875d2009-06-12 18:26:56 +0000526 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000527 ConstantArrayParm->getElementType(),
528 ConstantArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000529 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000530 }
531
Douglas Gregor199d9912009-06-05 00:53:49 +0000532 // type [i]
533 case Type::DependentSizedArray: {
534 const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg);
535 if (!ArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000536 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Douglas Gregor199d9912009-06-05 00:53:49 +0000538 // Check the element type of the arrays
539 const DependentSizedArrayType *DependentArrayParm
540 = cast<DependentSizedArrayType>(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000541 if (Sema::TemplateDeductionResult Result
542 = DeduceTemplateArguments(Context, TemplateParams,
543 DependentArrayParm->getElementType(),
544 ArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000545 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000546 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Douglas Gregor199d9912009-06-05 00:53:49 +0000548 // Determine the array bound is something we can deduce.
Mike Stump1eb44332009-09-09 15:08:12 +0000549 NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000550 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
551 if (!NTTP)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000552 return Sema::TDK_Success;
Mike Stump1eb44332009-09-09 15:08:12 +0000553
554 // We can perform template argument deduction for the given non-type
Douglas Gregor199d9912009-06-05 00:53:49 +0000555 // template parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000556 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000557 "Cannot deduce non-type template argument at depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000558 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson335e24a2009-06-16 22:44:31 +0000559 = dyn_cast<ConstantArrayType>(ArrayArg)) {
560 llvm::APSInt Size(ConstantArrayArg->getSize());
561 return DeduceNonTypeTemplateArgument(Context, NTTP, Size,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000562 Info, Deduced);
Anders Carlsson335e24a2009-06-16 22:44:31 +0000563 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000564 if (const DependentSizedArrayType *DependentArrayArg
565 = dyn_cast<DependentSizedArrayType>(ArrayArg))
566 return DeduceNonTypeTemplateArgument(Context, NTTP,
567 DependentArrayArg->getSizeExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000568 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Douglas Gregor199d9912009-06-05 00:53:49 +0000570 // Incomplete type does not match a dependently-sized array type
Douglas Gregorf67875d2009-06-12 18:26:56 +0000571 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000572 }
Mike Stump1eb44332009-09-09 15:08:12 +0000573
574 // type(*)(T)
575 // T(*)()
576 // T(*)(T)
Anders Carlssona27fad52009-06-08 15:19:08 +0000577 case Type::FunctionProto: {
Mike Stump1eb44332009-09-09 15:08:12 +0000578 const FunctionProtoType *FunctionProtoArg =
Anders Carlssona27fad52009-06-08 15:19:08 +0000579 dyn_cast<FunctionProtoType>(Arg);
580 if (!FunctionProtoArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000581 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000582
583 const FunctionProtoType *FunctionProtoParam =
Anders Carlssona27fad52009-06-08 15:19:08 +0000584 cast<FunctionProtoType>(Param);
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000585
Mike Stump1eb44332009-09-09 15:08:12 +0000586 if (FunctionProtoParam->getTypeQuals() !=
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000587 FunctionProtoArg->getTypeQuals())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000588 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000590 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000591 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000593 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000594 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000595
Anders Carlssona27fad52009-06-08 15:19:08 +0000596 // Check return types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000597 if (Sema::TemplateDeductionResult Result
598 = DeduceTemplateArguments(Context, TemplateParams,
599 FunctionProtoParam->getResultType(),
600 FunctionProtoArg->getResultType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000601 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000602 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Anders Carlssona27fad52009-06-08 15:19:08 +0000604 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
605 // Check argument types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000606 if (Sema::TemplateDeductionResult Result
607 = DeduceTemplateArguments(Context, TemplateParams,
608 FunctionProtoParam->getArgType(I),
609 FunctionProtoArg->getArgType(I),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000610 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000611 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000612 }
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Douglas Gregorf67875d2009-06-12 18:26:56 +0000614 return Sema::TDK_Success;
Anders Carlssona27fad52009-06-08 15:19:08 +0000615 }
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000617 // template-name<T> (where template-name refers to a class template)
Douglas Gregord708c722009-06-09 16:35:58 +0000618 // template-name<i>
619 // TT<T> (TODO)
620 // TT<i> (TODO)
621 // TT<> (TODO)
622 case Type::TemplateSpecialization: {
623 const TemplateSpecializationType *SpecParam
624 = cast<TemplateSpecializationType>(Param);
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000626 // Try to deduce template arguments from the template-id.
627 Sema::TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000628 = DeduceTemplateArguments(Context, TemplateParams, SpecParam, Arg,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000629 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000630
631 if (Result && (TDF & TDF_DerivedClass) &&
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000632 Result != Sema::TDK_Inconsistent) {
633 // C++ [temp.deduct.call]p3b3:
634 // If P is a class, and P has the form template-id, then A can be a
635 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump1eb44332009-09-09 15:08:12 +0000636 // class of the form template-id, A can be a pointer to a derived
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000637 // class pointed to by the deduced A.
638 //
639 // More importantly:
Mike Stump1eb44332009-09-09 15:08:12 +0000640 // These alternatives are considered only if type deduction would
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000641 // otherwise fail.
642 if (const RecordType *RecordT = dyn_cast<RecordType>(Arg)) {
643 // Use data recursion to crawl through the list of base classes.
Mike Stump1eb44332009-09-09 15:08:12 +0000644 // Visited contains the set of nodes we have already visited, while
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000645 // ToVisit is our stack of records that we still need to visit.
646 llvm::SmallPtrSet<const RecordType *, 8> Visited;
647 llvm::SmallVector<const RecordType *, 8> ToVisit;
648 ToVisit.push_back(RecordT);
649 bool Successful = false;
650 while (!ToVisit.empty()) {
651 // Retrieve the next class in the inheritance hierarchy.
652 const RecordType *NextT = ToVisit.back();
653 ToVisit.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000655 // If we have already seen this type, skip it.
656 if (!Visited.insert(NextT))
657 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000659 // If this is a base class, try to perform template argument
660 // deduction from it.
661 if (NextT != RecordT) {
662 Sema::TemplateDeductionResult BaseResult
663 = DeduceTemplateArguments(Context, TemplateParams, SpecParam,
664 QualType(NextT, 0), Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000666 // If template argument deduction for this base was successful,
667 // note that we had some success.
668 if (BaseResult == Sema::TDK_Success)
669 Successful = true;
670 // If deduction against this base resulted in an inconsistent
671 // set of deduced template arguments, template argument
672 // deduction fails.
673 else if (BaseResult == Sema::TDK_Inconsistent)
674 return BaseResult;
675 }
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000677 // Visit base classes
678 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
679 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
680 BaseEnd = Next->bases_end();
681 Base != BaseEnd; ++Base) {
Mike Stump1eb44332009-09-09 15:08:12 +0000682 assert(Base->getType()->isRecordType() &&
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000683 "Base class that isn't a record?");
Ted Kremenek6217b802009-07-29 21:53:49 +0000684 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000685 }
686 }
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000688 if (Successful)
689 return Sema::TDK_Success;
690 }
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000692 }
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000694 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000695 }
696
Douglas Gregor637a4092009-06-10 23:47:09 +0000697 // T type::*
698 // T T::*
699 // T (type::*)()
700 // type (T::*)()
701 // type (type::*)(T)
702 // type (T::*)(T)
703 // T (type::*)(T)
704 // T (T::*)()
705 // T (T::*)(T)
706 case Type::MemberPointer: {
707 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
708 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
709 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000710 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +0000711
Douglas Gregorf67875d2009-06-12 18:26:56 +0000712 if (Sema::TemplateDeductionResult Result
713 = DeduceTemplateArguments(Context, TemplateParams,
714 MemPtrParam->getPointeeType(),
715 MemPtrArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000716 Info, Deduced,
717 TDF & TDF_IgnoreQualifiers))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000718 return Result;
719
720 return DeduceTemplateArguments(Context, TemplateParams,
721 QualType(MemPtrParam->getClass(), 0),
722 QualType(MemPtrArg->getClass(), 0),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000723 Info, Deduced, 0);
Douglas Gregor637a4092009-06-10 23:47:09 +0000724 }
725
Anders Carlsson9a917e42009-06-12 22:56:54 +0000726 // (clang extension)
727 //
Mike Stump1eb44332009-09-09 15:08:12 +0000728 // type(^)(T)
729 // T(^)()
730 // T(^)(T)
Anders Carlsson859ba502009-06-12 16:23:10 +0000731 case Type::BlockPointer: {
732 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
733 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Anders Carlsson859ba502009-06-12 16:23:10 +0000735 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000736 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Douglas Gregorf67875d2009-06-12 18:26:56 +0000738 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +0000739 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000740 BlockPtrArg->getPointeeType(), Info,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000741 Deduced, 0);
Anders Carlsson859ba502009-06-12 16:23:10 +0000742 }
743
Douglas Gregor637a4092009-06-10 23:47:09 +0000744 case Type::TypeOfExpr:
745 case Type::TypeOf:
746 case Type::Typename:
747 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +0000748 return Sema::TDK_Success;
Douglas Gregor637a4092009-06-10 23:47:09 +0000749
Douglas Gregord560d502009-06-04 00:21:18 +0000750 default:
751 break;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000752 }
753
754 // FIXME: Many more cases to go (to go).
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000755 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000756}
757
Douglas Gregorf67875d2009-06-12 18:26:56 +0000758static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +0000759DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000760 TemplateParameterList *TemplateParams,
761 const TemplateArgument &Param,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000762 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000763 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000764 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000765 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000766 case TemplateArgument::Null:
767 assert(false && "Null template argument in parameter list");
768 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000769
770 case TemplateArgument::Type:
Douglas Gregor199d9912009-06-05 00:53:49 +0000771 assert(Arg.getKind() == TemplateArgument::Type && "Type/value mismatch");
Douglas Gregor508f1c82009-06-26 23:10:12 +0000772 return DeduceTemplateArguments(Context, TemplateParams, Param.getAsType(),
773 Arg.getAsType(), Info, Deduced, 0);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000774
Douglas Gregor199d9912009-06-05 00:53:49 +0000775 case TemplateArgument::Declaration:
776 // FIXME: Implement this check
777 assert(false && "Unimplemented template argument deduction case");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000778 Info.FirstArg = Param;
779 Info.SecondArg = Arg;
780 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Douglas Gregor199d9912009-06-05 00:53:49 +0000782 case TemplateArgument::Integral:
783 if (Arg.getKind() == TemplateArgument::Integral) {
784 // FIXME: Zero extension + sign checking here?
Douglas Gregorf67875d2009-06-12 18:26:56 +0000785 if (*Param.getAsIntegral() == *Arg.getAsIntegral())
786 return Sema::TDK_Success;
787
788 Info.FirstArg = Param;
789 Info.SecondArg = Arg;
790 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000791 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000792
793 if (Arg.getKind() == TemplateArgument::Expression) {
794 Info.FirstArg = Param;
795 Info.SecondArg = Arg;
796 return Sema::TDK_NonDeducedMismatch;
797 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000798
799 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000800 Info.FirstArg = Param;
801 Info.SecondArg = Arg;
802 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Douglas Gregor199d9912009-06-05 00:53:49 +0000804 case TemplateArgument::Expression: {
Mike Stump1eb44332009-09-09 15:08:12 +0000805 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000806 = getDeducedParameterFromExpr(Param.getAsExpr())) {
807 if (Arg.getKind() == TemplateArgument::Integral)
808 // FIXME: Sign problems here
Mike Stump1eb44332009-09-09 15:08:12 +0000809 return DeduceNonTypeTemplateArgument(Context, NTTP,
810 *Arg.getAsIntegral(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000811 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000812 if (Arg.getKind() == TemplateArgument::Expression)
813 return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000814 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Douglas Gregor199d9912009-06-05 00:53:49 +0000816 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000817 Info.FirstArg = Param;
818 Info.SecondArg = Arg;
819 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000820 }
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Douglas Gregor199d9912009-06-05 00:53:49 +0000822 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000823 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000824 }
Anders Carlssond01b1da2009-06-15 17:04:53 +0000825 case TemplateArgument::Pack:
826 assert(0 && "FIXME: Implement!");
827 break;
Douglas Gregor199d9912009-06-05 00:53:49 +0000828 }
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Douglas Gregorf67875d2009-06-12 18:26:56 +0000830 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000831}
832
Mike Stump1eb44332009-09-09 15:08:12 +0000833static Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000834DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000835 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000836 const TemplateArgumentList &ParamList,
837 const TemplateArgumentList &ArgList,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000838 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000839 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
840 assert(ParamList.size() == ArgList.size());
841 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000842 if (Sema::TemplateDeductionResult Result
843 = DeduceTemplateArguments(Context, TemplateParams,
Mike Stump1eb44332009-09-09 15:08:12 +0000844 ParamList[I], ArgList[I],
Douglas Gregorf67875d2009-06-12 18:26:56 +0000845 Info, Deduced))
846 return Result;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000847 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000848 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000849}
850
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000851/// \brief Determine whether two template arguments are the same.
Mike Stump1eb44332009-09-09 15:08:12 +0000852static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000853 const TemplateArgument &X,
854 const TemplateArgument &Y) {
855 if (X.getKind() != Y.getKind())
856 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000858 switch (X.getKind()) {
859 case TemplateArgument::Null:
860 assert(false && "Comparing NULL template argument");
861 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000863 case TemplateArgument::Type:
864 return Context.getCanonicalType(X.getAsType()) ==
865 Context.getCanonicalType(Y.getAsType());
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000867 case TemplateArgument::Declaration:
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000868 return X.getAsDecl()->getCanonicalDecl() ==
869 Y.getAsDecl()->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000871 case TemplateArgument::Integral:
872 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000874 case TemplateArgument::Expression:
875 // FIXME: We assume that all expressions are distinct, but we should
876 // really check their canonical forms.
877 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000879 case TemplateArgument::Pack:
880 if (X.pack_size() != Y.pack_size())
881 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000882
883 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
884 XPEnd = X.pack_end(),
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000885 YP = Y.pack_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000886 XP != XPEnd; ++XP, ++YP)
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000887 if (!isSameTemplateArg(Context, *XP, *YP))
888 return false;
889
890 return true;
891 }
892
893 return false;
894}
895
896/// \brief Helper function to build a TemplateParameter when we don't
897/// know its type statically.
898static TemplateParameter makeTemplateParameter(Decl *D) {
899 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
900 return TemplateParameter(TTP);
901 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
902 return TemplateParameter(NTTP);
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000904 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
905}
906
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000907/// \brief Perform template argument deduction to determine whether
908/// the given template arguments match the given class template
909/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +0000910Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000911Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000912 const TemplateArgumentList &TemplateArgs,
913 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000914 // C++ [temp.class.spec.match]p2:
915 // A partial specialization matches a given actual template
916 // argument list if the template arguments of the partial
917 // specialization can be deduced from the actual template argument
918 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +0000919 SFINAETrap Trap(*this);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000920 llvm::SmallVector<TemplateArgument, 4> Deduced;
921 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000922 if (TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000923 = ::DeduceTemplateArguments(Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000924 Partial->getTemplateParameters(),
Mike Stump1eb44332009-09-09 15:08:12 +0000925 Partial->getTemplateArgs(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000926 TemplateArgs, Info, Deduced))
927 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +0000928
Douglas Gregor637a4092009-06-10 23:47:09 +0000929 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
930 Deduced.data(), Deduced.size());
931 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000932 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +0000933
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000934 // C++ [temp.deduct.type]p2:
935 // [...] or if any template argument remains neither deduced nor
936 // explicitly specified, template argument deduction fails.
Anders Carlssonfb250522009-06-23 01:26:57 +0000937 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
938 Deduced.size());
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000939 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000940 if (Deduced[I].isNull()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000941 Decl *Param
Douglas Gregorf67875d2009-06-12 18:26:56 +0000942 = const_cast<Decl *>(Partial->getTemplateParameters()->getParam(I));
943 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
944 Info.Param = TTP;
Mike Stump1eb44332009-09-09 15:08:12 +0000945 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorf67875d2009-06-12 18:26:56 +0000946 = dyn_cast<NonTypeTemplateParmDecl>(Param))
947 Info.Param = NTTP;
948 else
949 Info.Param = cast<TemplateTemplateParmDecl>(Param);
950 return TDK_Incomplete;
951 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000952
Anders Carlssonfb250522009-06-23 01:26:57 +0000953 Builder.Append(Deduced[I]);
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000954 }
955
956 // Form the template argument list from the deduced template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +0000957 TemplateArgumentList *DeducedArgumentList
Anders Carlssonfb250522009-06-23 01:26:57 +0000958 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000959 Info.reset(DeducedArgumentList);
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000960
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000961 // Substitute the deduced template arguments into the template
962 // arguments of the class template partial specialization, and
963 // verify that the instantiated template arguments are both valid
964 // and are equivalent to the template arguments originally provided
Mike Stump1eb44332009-09-09 15:08:12 +0000965 // to the class template.
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000966 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
967 const TemplateArgumentList &PartialTemplateArgs = Partial->getTemplateArgs();
968 for (unsigned I = 0, N = PartialTemplateArgs.flat_size(); I != N; ++I) {
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000969 Decl *Param = const_cast<Decl *>(
970 ClassTemplate->getTemplateParameters()->getParam(I));
Mike Stump1eb44332009-09-09 15:08:12 +0000971 TemplateArgument InstArg
Douglas Gregor357bbd02009-08-28 20:50:45 +0000972 = Subst(PartialTemplateArgs[I],
973 MultiLevelTemplateArgumentList(*DeducedArgumentList));
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000974 if (InstArg.isNull()) {
975 Info.Param = makeTemplateParameter(Param);
976 Info.FirstArg = PartialTemplateArgs[I];
Mike Stump1eb44332009-09-09 15:08:12 +0000977 return TDK_SubstitutionFailure;
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000978 }
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000980 if (InstArg.getKind() == TemplateArgument::Expression) {
Mike Stump1eb44332009-09-09 15:08:12 +0000981 // When the argument is an expression, check the expression result
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000982 // against the actual template parameter to get down to the canonical
983 // template argument.
984 Expr *InstExpr = InstArg.getAsExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000985 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000986 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
987 if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
988 Info.Param = makeTemplateParameter(Param);
989 Info.FirstArg = PartialTemplateArgs[I];
Mike Stump1eb44332009-09-09 15:08:12 +0000990 return TDK_SubstitutionFailure;
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000991 }
Mike Stump1eb44332009-09-09 15:08:12 +0000992 } else if (TemplateTemplateParmDecl *TTP
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000993 = dyn_cast<TemplateTemplateParmDecl>(Param)) {
994 // FIXME: template template arguments should really resolve to decls
995 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InstExpr);
996 if (!DRE || CheckTemplateArgument(TTP, DRE)) {
997 Info.Param = makeTemplateParameter(Param);
998 Info.FirstArg = PartialTemplateArgs[I];
Mike Stump1eb44332009-09-09 15:08:12 +0000999 return TDK_SubstitutionFailure;
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001000 }
1001 }
1002 }
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001004 if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) {
1005 Info.Param = makeTemplateParameter(Param);
1006 Info.FirstArg = TemplateArgs[I];
1007 Info.SecondArg = InstArg;
1008 return TDK_NonDeducedMismatch;
1009 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001010 }
1011
Douglas Gregorbb260412009-06-14 08:02:22 +00001012 if (Trap.hasErrorOccurred())
1013 return TDK_SubstitutionFailure;
1014
Douglas Gregorf67875d2009-06-12 18:26:56 +00001015 return TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001016}
Douglas Gregor031a5882009-06-13 00:26:55 +00001017
Douglas Gregor41128772009-06-26 23:27:24 +00001018/// \brief Determine whether the given type T is a simple-template-id type.
1019static bool isSimpleTemplateIdType(QualType T) {
Mike Stump1eb44332009-09-09 15:08:12 +00001020 if (const TemplateSpecializationType *Spec
Douglas Gregor41128772009-06-26 23:27:24 +00001021 = T->getAsTemplateSpecializationType())
1022 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Douglas Gregor41128772009-06-26 23:27:24 +00001024 return false;
1025}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001026
1027/// \brief Substitute the explicitly-provided template arguments into the
1028/// given function template according to C++ [temp.arg.explicit].
1029///
1030/// \param FunctionTemplate the function template into which the explicit
1031/// template arguments will be substituted.
1032///
Mike Stump1eb44332009-09-09 15:08:12 +00001033/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001034/// arguments.
1035///
Mike Stump1eb44332009-09-09 15:08:12 +00001036/// \param NumExplicitTemplateArguments the number of explicitly-specified
Douglas Gregor83314aa2009-07-08 20:55:45 +00001037/// template arguments in @p ExplicitTemplateArguments. This value may be zero.
1038///
Mike Stump1eb44332009-09-09 15:08:12 +00001039/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor83314aa2009-07-08 20:55:45 +00001040/// with the converted and checked explicit template arguments.
1041///
Mike Stump1eb44332009-09-09 15:08:12 +00001042/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor83314aa2009-07-08 20:55:45 +00001043/// parameters.
1044///
1045/// \param FunctionType if non-NULL, the result type of the function template
1046/// will also be instantiated and the pointed-to value will be updated with
1047/// the instantiated function type.
1048///
1049/// \param Info if substitution fails for any reason, this object will be
1050/// populated with more information about the failure.
1051///
1052/// \returns TDK_Success if substitution was successful, or some failure
1053/// condition.
1054Sema::TemplateDeductionResult
1055Sema::SubstituteExplicitTemplateArguments(
1056 FunctionTemplateDecl *FunctionTemplate,
1057 const TemplateArgument *ExplicitTemplateArgs,
1058 unsigned NumExplicitTemplateArgs,
1059 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1060 llvm::SmallVectorImpl<QualType> &ParamTypes,
1061 QualType *FunctionType,
1062 TemplateDeductionInfo &Info) {
1063 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1064 TemplateParameterList *TemplateParams
1065 = FunctionTemplate->getTemplateParameters();
1066
1067 if (NumExplicitTemplateArgs == 0) {
1068 // No arguments to substitute; just copy over the parameter types and
1069 // fill in the function type.
1070 for (FunctionDecl::param_iterator P = Function->param_begin(),
1071 PEnd = Function->param_end();
1072 P != PEnd;
1073 ++P)
1074 ParamTypes.push_back((*P)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Douglas Gregor83314aa2009-07-08 20:55:45 +00001076 if (FunctionType)
1077 *FunctionType = Function->getType();
1078 return TDK_Success;
1079 }
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Douglas Gregor83314aa2009-07-08 20:55:45 +00001081 // Substitution of the explicit template arguments into a function template
1082 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001083 SFINAETrap Trap(*this);
1084
Douglas Gregor83314aa2009-07-08 20:55:45 +00001085 // C++ [temp.arg.explicit]p3:
Mike Stump1eb44332009-09-09 15:08:12 +00001086 // Template arguments that are present shall be specified in the
1087 // declaration order of their corresponding template-parameters. The
Douglas Gregor83314aa2009-07-08 20:55:45 +00001088 // template argument list shall not specify more template-arguments than
Mike Stump1eb44332009-09-09 15:08:12 +00001089 // there are corresponding template-parameters.
1090 TemplateArgumentListBuilder Builder(TemplateParams,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001091 NumExplicitTemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001092
1093 // Enter a new template instantiation context where we check the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001094 // explicitly-specified template arguments against this function template,
1095 // and then substitute them into the function parameter types.
Mike Stump1eb44332009-09-09 15:08:12 +00001096 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001097 FunctionTemplate, Deduced.data(), Deduced.size(),
1098 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution);
1099 if (Inst)
1100 return TDK_InstantiationDepth;
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Douglas Gregor83314aa2009-07-08 20:55:45 +00001102 if (CheckTemplateArgumentList(FunctionTemplate,
1103 SourceLocation(), SourceLocation(),
1104 ExplicitTemplateArgs,
1105 NumExplicitTemplateArgs,
1106 SourceLocation(),
1107 true,
1108 Builder) || Trap.hasErrorOccurred())
1109 return TDK_InvalidExplicitArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Douglas Gregor83314aa2009-07-08 20:55:45 +00001111 // Form the template argument list from the explicitly-specified
1112 // template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001113 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor83314aa2009-07-08 20:55:45 +00001114 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1115 Info.reset(ExplicitArgumentList);
Mike Stump1eb44332009-09-09 15:08:12 +00001116
Douglas Gregor83314aa2009-07-08 20:55:45 +00001117 // Instantiate the types of each of the function parameters given the
1118 // explicitly-specified template arguments.
1119 for (FunctionDecl::param_iterator P = Function->param_begin(),
1120 PEnd = Function->param_end();
1121 P != PEnd;
1122 ++P) {
Mike Stump1eb44332009-09-09 15:08:12 +00001123 QualType ParamType
1124 = SubstType((*P)->getType(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001125 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1126 (*P)->getLocation(), (*P)->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001127 if (ParamType.isNull() || Trap.hasErrorOccurred())
1128 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Douglas Gregor83314aa2009-07-08 20:55:45 +00001130 ParamTypes.push_back(ParamType);
1131 }
1132
1133 // If the caller wants a full function type back, instantiate the return
1134 // type and form that function type.
1135 if (FunctionType) {
1136 // FIXME: exception-specifications?
Mike Stump1eb44332009-09-09 15:08:12 +00001137 const FunctionProtoType *Proto
Douglas Gregor83314aa2009-07-08 20:55:45 +00001138 = Function->getType()->getAsFunctionProtoType();
1139 assert(Proto && "Function template does not have a prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001140
1141 QualType ResultType
Douglas Gregor357bbd02009-08-28 20:50:45 +00001142 = SubstType(Proto->getResultType(),
1143 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1144 Function->getTypeSpecStartLoc(),
1145 Function->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001146 if (ResultType.isNull() || Trap.hasErrorOccurred())
1147 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001148
1149 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001150 ParamTypes.data(), ParamTypes.size(),
1151 Proto->isVariadic(),
1152 Proto->getTypeQuals(),
1153 Function->getLocation(),
1154 Function->getDeclName());
1155 if (FunctionType->isNull() || Trap.hasErrorOccurred())
1156 return TDK_SubstitutionFailure;
1157 }
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Douglas Gregor83314aa2009-07-08 20:55:45 +00001159 // C++ [temp.arg.explicit]p2:
Mike Stump1eb44332009-09-09 15:08:12 +00001160 // Trailing template arguments that can be deduced (14.8.2) may be
1161 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001162 // template arguments can be deduced, they may all be omitted; in this
1163 // case, the empty template argument list <> itself may also be omitted.
1164 //
1165 // Take all of the explicitly-specified arguments and put them into the
Mike Stump1eb44332009-09-09 15:08:12 +00001166 // set of deduced template arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001167 Deduced.reserve(TemplateParams->size());
1168 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001169 Deduced.push_back(ExplicitArgumentList->get(I));
1170
Douglas Gregor83314aa2009-07-08 20:55:45 +00001171 return TDK_Success;
1172}
1173
Mike Stump1eb44332009-09-09 15:08:12 +00001174/// \brief Finish template argument deduction for a function template,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001175/// checking the deduced template arguments for completeness and forming
1176/// the function template specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00001177Sema::TemplateDeductionResult
Douglas Gregor83314aa2009-07-08 20:55:45 +00001178Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
1179 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1180 FunctionDecl *&Specialization,
1181 TemplateDeductionInfo &Info) {
1182 TemplateParameterList *TemplateParams
1183 = FunctionTemplate->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Douglas Gregor83314aa2009-07-08 20:55:45 +00001185 // C++ [temp.deduct.type]p2:
1186 // [...] or if any template argument remains neither deduced nor
1187 // explicitly specified, template argument deduction fails.
1188 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
1189 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
1190 if (Deduced[I].isNull()) {
1191 Info.Param = makeTemplateParameter(
1192 const_cast<Decl *>(TemplateParams->getParam(I)));
1193 return TDK_Incomplete;
1194 }
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Douglas Gregor83314aa2009-07-08 20:55:45 +00001196 Builder.Append(Deduced[I]);
1197 }
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Douglas Gregor83314aa2009-07-08 20:55:45 +00001199 // Form the template argument list from the deduced template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001200 TemplateArgumentList *DeducedArgumentList
Douglas Gregor83314aa2009-07-08 20:55:45 +00001201 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1202 Info.reset(DeducedArgumentList);
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Douglas Gregor83314aa2009-07-08 20:55:45 +00001204 // Template argument deduction for function templates in a SFINAE context.
1205 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001206 SFINAETrap Trap(*this);
1207
Douglas Gregor83314aa2009-07-08 20:55:45 +00001208 // Enter a new template instantiation context while we instantiate the
1209 // actual function declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001210 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001211 FunctionTemplate, Deduced.data(), Deduced.size(),
1212 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
1213 if (Inst)
Mike Stump1eb44332009-09-09 15:08:12 +00001214 return TDK_InstantiationDepth;
1215
1216 // Substitute the deduced template arguments into the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001217 // declaration to produce the function template specialization.
1218 Specialization = cast_or_null<FunctionDecl>(
John McCallce3ff2b2009-08-25 22:02:44 +00001219 SubstDecl(FunctionTemplate->getTemplatedDecl(),
1220 FunctionTemplate->getDeclContext(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001221 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor83314aa2009-07-08 20:55:45 +00001222 if (!Specialization)
1223 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001224
1225 // If the template argument list is owned by the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001226 // specialization, release it.
1227 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList)
1228 Info.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Douglas Gregor83314aa2009-07-08 20:55:45 +00001230 // There may have been an error that did not prevent us from constructing a
1231 // declaration. Mark the declaration invalid and return with a substitution
1232 // failure.
1233 if (Trap.hasErrorOccurred()) {
1234 Specialization->setInvalidDecl(true);
1235 return TDK_SubstitutionFailure;
1236 }
Mike Stump1eb44332009-09-09 15:08:12 +00001237
1238 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001239}
1240
Douglas Gregore53060f2009-06-25 22:08:12 +00001241/// \brief Perform template argument deduction from a function call
1242/// (C++ [temp.deduct.call]).
1243///
1244/// \param FunctionTemplate the function template for which we are performing
1245/// template argument deduction.
1246///
Mike Stump1eb44332009-09-09 15:08:12 +00001247/// \param HasExplicitTemplateArgs whether any template arguments were
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001248/// explicitly specified.
1249///
1250/// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
1251/// the explicitly-specified template arguments.
1252///
1253/// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
Mike Stump1eb44332009-09-09 15:08:12 +00001254/// the number of explicitly-specified template arguments in
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001255/// @p ExplicitTemplateArguments. This value may be zero.
1256///
Douglas Gregore53060f2009-06-25 22:08:12 +00001257/// \param Args the function call arguments
1258///
1259/// \param NumArgs the number of arguments in Args
1260///
1261/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001262/// this will be set to the function template specialization produced by
Douglas Gregore53060f2009-06-25 22:08:12 +00001263/// template argument deduction.
1264///
1265/// \param Info the argument will be updated to provide additional information
1266/// about template argument deduction.
1267///
1268/// \returns the result of template argument deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001269Sema::TemplateDeductionResult
1270Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001271 bool HasExplicitTemplateArgs,
1272 const TemplateArgument *ExplicitTemplateArgs,
1273 unsigned NumExplicitTemplateArgs,
Douglas Gregore53060f2009-06-25 22:08:12 +00001274 Expr **Args, unsigned NumArgs,
1275 FunctionDecl *&Specialization,
1276 TemplateDeductionInfo &Info) {
1277 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001278
Douglas Gregore53060f2009-06-25 22:08:12 +00001279 // C++ [temp.deduct.call]p1:
1280 // Template argument deduction is done by comparing each function template
1281 // parameter type (call it P) with the type of the corresponding argument
1282 // of the call (call it A) as described below.
1283 unsigned CheckArgs = NumArgs;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001284 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregore53060f2009-06-25 22:08:12 +00001285 return TDK_TooFewArguments;
1286 else if (NumArgs > Function->getNumParams()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001287 const FunctionProtoType *Proto
Douglas Gregore53060f2009-06-25 22:08:12 +00001288 = Function->getType()->getAsFunctionProtoType();
1289 if (!Proto->isVariadic())
1290 return TDK_TooManyArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Douglas Gregore53060f2009-06-25 22:08:12 +00001292 CheckArgs = Function->getNumParams();
1293 }
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001295 // The types of the parameters from which we will perform template argument
1296 // deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001297 TemplateParameterList *TemplateParams
1298 = FunctionTemplate->getTemplateParameters();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001299 llvm::SmallVector<TemplateArgument, 4> Deduced;
1300 llvm::SmallVector<QualType, 4> ParamTypes;
1301 if (NumExplicitTemplateArgs) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001302 TemplateDeductionResult Result =
1303 SubstituteExplicitTemplateArguments(FunctionTemplate,
1304 ExplicitTemplateArgs,
1305 NumExplicitTemplateArgs,
1306 Deduced,
1307 ParamTypes,
1308 0,
1309 Info);
1310 if (Result)
1311 return Result;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001312 } else {
1313 // Just fill in the parameter types from the function declaration.
1314 for (unsigned I = 0; I != CheckArgs; ++I)
1315 ParamTypes.push_back(Function->getParamDecl(I)->getType());
1316 }
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001318 // Deduce template arguments from the function parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001319 Deduced.resize(TemplateParams->size());
Douglas Gregore53060f2009-06-25 22:08:12 +00001320 for (unsigned I = 0; I != CheckArgs; ++I) {
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001321 QualType ParamType = ParamTypes[I];
Douglas Gregore53060f2009-06-25 22:08:12 +00001322 QualType ArgType = Args[I]->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001323
Douglas Gregore53060f2009-06-25 22:08:12 +00001324 // C++ [temp.deduct.call]p2:
1325 // If P is not a reference type:
1326 QualType CanonParamType = Context.getCanonicalType(ParamType);
Douglas Gregor500d3312009-06-26 18:27:22 +00001327 bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1328 if (!ParamWasReference) {
Mike Stump1eb44332009-09-09 15:08:12 +00001329 // - If A is an array type, the pointer type produced by the
1330 // array-to-pointer standard conversion (4.2) is used in place of
Douglas Gregore53060f2009-06-25 22:08:12 +00001331 // A for type deduction; otherwise,
1332 if (ArgType->isArrayType())
1333 ArgType = Context.getArrayDecayedType(ArgType);
Mike Stump1eb44332009-09-09 15:08:12 +00001334 // - If A is a function type, the pointer type produced by the
1335 // function-to-pointer standard conversion (4.3) is used in place
Douglas Gregore53060f2009-06-25 22:08:12 +00001336 // of A for type deduction; otherwise,
1337 else if (ArgType->isFunctionType())
1338 ArgType = Context.getPointerType(ArgType);
1339 else {
1340 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1341 // type are ignored for type deduction.
1342 QualType CanonArgType = Context.getCanonicalType(ArgType);
1343 if (CanonArgType.getCVRQualifiers())
1344 ArgType = CanonArgType.getUnqualifiedType();
1345 }
1346 }
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Douglas Gregore53060f2009-06-25 22:08:12 +00001348 // C++0x [temp.deduct.call]p3:
1349 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
Mike Stump1eb44332009-09-09 15:08:12 +00001350 // are ignored for type deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001351 if (CanonParamType.getCVRQualifiers())
1352 ParamType = CanonParamType.getUnqualifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001353 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001354 // [...] If P is a reference type, the type referred to by P is used
1355 // for type deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001356 ParamType = ParamRefType->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00001357
1358 // [...] If P is of the form T&&, where T is a template parameter, and
1359 // the argument is an lvalue, the type A& is used in place of A for
Douglas Gregore53060f2009-06-25 22:08:12 +00001360 // type deduction.
1361 if (isa<RValueReferenceType>(ParamRefType) &&
1362 ParamRefType->getAsTemplateTypeParmType() &&
1363 Args[I]->isLvalue(Context) == Expr::LV_Valid)
1364 ArgType = Context.getLValueReferenceType(ArgType);
1365 }
Mike Stump1eb44332009-09-09 15:08:12 +00001366
Douglas Gregore53060f2009-06-25 22:08:12 +00001367 // C++0x [temp.deduct.call]p4:
1368 // In general, the deduction process attempts to find template argument
1369 // values that will make the deduced A identical to A (after the type A
1370 // is transformed as described above). [...]
Douglas Gregor508f1c82009-06-26 23:10:12 +00001371 unsigned TDF = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Douglas Gregor508f1c82009-06-26 23:10:12 +00001373 // - If the original P is a reference type, the deduced A (i.e., the
1374 // type referred to by the reference) can be more cv-qualified than
1375 // the transformed A.
1376 if (ParamWasReference)
1377 TDF |= TDF_ParamWithReferenceType;
Mike Stump1eb44332009-09-09 15:08:12 +00001378 // - The transformed A can be another pointer or pointer to member
1379 // type that can be converted to the deduced A via a qualification
Douglas Gregor508f1c82009-06-26 23:10:12 +00001380 // conversion (4.4).
1381 if (ArgType->isPointerType() || ArgType->isMemberPointerType())
1382 TDF |= TDF_IgnoreQualifiers;
Mike Stump1eb44332009-09-09 15:08:12 +00001383 // - If P is a class and P has the form simple-template-id, then the
Douglas Gregor41128772009-06-26 23:27:24 +00001384 // transformed A can be a derived class of the deduced A. Likewise,
1385 // if P is a pointer to a class of the form simple-template-id, the
1386 // transformed A can be a pointer to a derived class pointed to by
1387 // the deduced A.
1388 if (isSimpleTemplateIdType(ParamType) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001389 (isa<PointerType>(ParamType) &&
Douglas Gregor41128772009-06-26 23:27:24 +00001390 isSimpleTemplateIdType(
Ted Kremenek6217b802009-07-29 21:53:49 +00001391 ParamType->getAs<PointerType>()->getPointeeType())))
Douglas Gregor41128772009-06-26 23:27:24 +00001392 TDF |= TDF_DerivedClass;
Mike Stump1eb44332009-09-09 15:08:12 +00001393
Douglas Gregore53060f2009-06-25 22:08:12 +00001394 if (TemplateDeductionResult Result
1395 = ::DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor500d3312009-06-26 18:27:22 +00001396 ParamType, ArgType, Info, Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +00001397 TDF))
Douglas Gregore53060f2009-06-25 22:08:12 +00001398 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Douglas Gregor8fdc3c42009-07-07 23:12:18 +00001400 // FIXME: C++0x [temp.deduct.call] paragraphs 6-9 deal with function
Mike Stump1eb44332009-09-09 15:08:12 +00001401 // pointer parameters.
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001402
1403 // FIXME: we need to check that the deduced A is the same as A,
1404 // modulo the various allowed differences.
Douglas Gregore53060f2009-06-25 22:08:12 +00001405 }
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001406
Mike Stump1eb44332009-09-09 15:08:12 +00001407 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001408 Specialization, Info);
Douglas Gregore53060f2009-06-25 22:08:12 +00001409}
1410
Douglas Gregor83314aa2009-07-08 20:55:45 +00001411/// \brief Deduce template arguments when taking the address of a function
1412/// template (C++ [temp.deduct.funcaddr]).
1413///
1414/// \param FunctionTemplate the function template for which we are performing
1415/// template argument deduction.
1416///
Mike Stump1eb44332009-09-09 15:08:12 +00001417/// \param HasExplicitTemplateArgs whether any template arguments were
Douglas Gregor83314aa2009-07-08 20:55:45 +00001418/// explicitly specified.
1419///
1420/// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
1421/// the explicitly-specified template arguments.
1422///
1423/// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
Mike Stump1eb44332009-09-09 15:08:12 +00001424/// the number of explicitly-specified template arguments in
Douglas Gregor83314aa2009-07-08 20:55:45 +00001425/// @p ExplicitTemplateArguments. This value may be zero.
1426///
1427/// \param ArgFunctionType the function type that will be used as the
1428/// "argument" type (A) when performing template argument deduction from the
1429/// function template's function type.
1430///
1431/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001432/// this will be set to the function template specialization produced by
Douglas Gregor83314aa2009-07-08 20:55:45 +00001433/// template argument deduction.
1434///
1435/// \param Info the argument will be updated to provide additional information
1436/// about template argument deduction.
1437///
1438/// \returns the result of template argument deduction.
1439Sema::TemplateDeductionResult
1440Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1441 bool HasExplicitTemplateArgs,
1442 const TemplateArgument *ExplicitTemplateArgs,
1443 unsigned NumExplicitTemplateArgs,
1444 QualType ArgFunctionType,
1445 FunctionDecl *&Specialization,
1446 TemplateDeductionInfo &Info) {
1447 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1448 TemplateParameterList *TemplateParams
1449 = FunctionTemplate->getTemplateParameters();
1450 QualType FunctionType = Function->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Douglas Gregor83314aa2009-07-08 20:55:45 +00001452 // Substitute any explicit template arguments.
1453 llvm::SmallVector<TemplateArgument, 4> Deduced;
1454 llvm::SmallVector<QualType, 4> ParamTypes;
1455 if (HasExplicitTemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001456 if (TemplateDeductionResult Result
1457 = SubstituteExplicitTemplateArguments(FunctionTemplate,
1458 ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001459 NumExplicitTemplateArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00001460 Deduced, ParamTypes,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001461 &FunctionType, Info))
1462 return Result;
1463 }
1464
1465 // Template argument deduction for function templates in a SFINAE context.
1466 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001467 SFINAETrap Trap(*this);
1468
Douglas Gregor83314aa2009-07-08 20:55:45 +00001469 // Deduce template arguments from the function type.
Mike Stump1eb44332009-09-09 15:08:12 +00001470 Deduced.resize(TemplateParams->size());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001471 if (TemplateDeductionResult Result
1472 = ::DeduceTemplateArguments(Context, TemplateParams,
Mike Stump1eb44332009-09-09 15:08:12 +00001473 FunctionType, ArgFunctionType, Info,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001474 Deduced, 0))
1475 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001476
1477 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001478 Specialization, Info);
1479}
1480
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001481/// \brief Deduce template arguments for a templated conversion
1482/// function (C++ [temp.deduct.conv]) and, if successful, produce a
1483/// conversion function template specialization.
1484Sema::TemplateDeductionResult
1485Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1486 QualType ToType,
1487 CXXConversionDecl *&Specialization,
1488 TemplateDeductionInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00001489 CXXConversionDecl *Conv
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001490 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
1491 QualType FromType = Conv->getConversionType();
1492
1493 // Canonicalize the types for deduction.
1494 QualType P = Context.getCanonicalType(FromType);
1495 QualType A = Context.getCanonicalType(ToType);
1496
1497 // C++0x [temp.deduct.conv]p3:
1498 // If P is a reference type, the type referred to by P is used for
1499 // type deduction.
1500 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
1501 P = PRef->getPointeeType();
1502
1503 // C++0x [temp.deduct.conv]p3:
1504 // If A is a reference type, the type referred to by A is used
1505 // for type deduction.
1506 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
1507 A = ARef->getPointeeType();
1508 // C++ [temp.deduct.conv]p2:
1509 //
Mike Stump1eb44332009-09-09 15:08:12 +00001510 // If A is not a reference type:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001511 else {
1512 assert(!A->isReferenceType() && "Reference types were handled above");
1513
1514 // - If P is an array type, the pointer type produced by the
Mike Stump1eb44332009-09-09 15:08:12 +00001515 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001516 // of P for type deduction; otherwise,
1517 if (P->isArrayType())
1518 P = Context.getArrayDecayedType(P);
1519 // - If P is a function type, the pointer type produced by the
1520 // function-to-pointer standard conversion (4.3) is used in
1521 // place of P for type deduction; otherwise,
1522 else if (P->isFunctionType())
1523 P = Context.getPointerType(P);
1524 // - If P is a cv-qualified type, the top level cv-qualifiers of
1525 // P’s type are ignored for type deduction.
1526 else
1527 P = P.getUnqualifiedType();
1528
1529 // C++0x [temp.deduct.conv]p3:
1530 // If A is a cv-qualified type, the top level cv-qualifiers of A’s
1531 // type are ignored for type deduction.
1532 A = A.getUnqualifiedType();
1533 }
1534
1535 // Template argument deduction for function templates in a SFINAE context.
1536 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001537 SFINAETrap Trap(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001538
1539 // C++ [temp.deduct.conv]p1:
1540 // Template argument deduction is done by comparing the return
1541 // type of the template conversion function (call it P) with the
1542 // type that is required as the result of the conversion (call it
1543 // A) as described in 14.8.2.4.
1544 TemplateParameterList *TemplateParams
1545 = FunctionTemplate->getTemplateParameters();
1546 llvm::SmallVector<TemplateArgument, 4> Deduced;
Mike Stump1eb44332009-09-09 15:08:12 +00001547 Deduced.resize(TemplateParams->size());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001548
1549 // C++0x [temp.deduct.conv]p4:
1550 // In general, the deduction process attempts to find template
1551 // argument values that will make the deduced A identical to
1552 // A. However, there are two cases that allow a difference:
1553 unsigned TDF = 0;
1554 // - If the original A is a reference type, A can be more
1555 // cv-qualified than the deduced A (i.e., the type referred to
1556 // by the reference)
1557 if (ToType->isReferenceType())
1558 TDF |= TDF_ParamWithReferenceType;
1559 // - The deduced A can be another pointer or pointer to member
1560 // type that can be converted to A via a qualification
1561 // conversion.
1562 //
1563 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
1564 // both P and A are pointers or member pointers. In this case, we
1565 // just ignore cv-qualifiers completely).
1566 if ((P->isPointerType() && A->isPointerType()) ||
1567 (P->isMemberPointerType() && P->isMemberPointerType()))
1568 TDF |= TDF_IgnoreQualifiers;
1569 if (TemplateDeductionResult Result
1570 = ::DeduceTemplateArguments(Context, TemplateParams,
1571 P, A, Info, Deduced, TDF))
1572 return Result;
1573
1574 // FIXME: we need to check that the deduced A is the same as A,
1575 // modulo the various allowed differences.
Mike Stump1eb44332009-09-09 15:08:12 +00001576
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001577 // Finish template argument deduction.
1578 FunctionDecl *Spec = 0;
1579 TemplateDeductionResult Result
1580 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, Spec, Info);
1581 Specialization = cast_or_null<CXXConversionDecl>(Spec);
1582 return Result;
1583}
1584
Douglas Gregor8a514912009-09-14 18:39:43 +00001585/// \brief Stores the result of comparing the qualifiers of two types.
1586enum DeductionQualifierComparison {
1587 NeitherMoreQualified = 0,
1588 ParamMoreQualified,
1589 ArgMoreQualified
1590};
1591
1592/// \brief Deduce the template arguments during partial ordering by comparing
1593/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
1594///
1595/// \param Context the AST context in which this deduction occurs.
1596///
1597/// \param TemplateParams the template parameters that we are deducing
1598///
1599/// \param ParamIn the parameter type
1600///
1601/// \param ArgIn the argument type
1602///
1603/// \param Info information about the template argument deduction itself
1604///
1605/// \param Deduced the deduced template arguments
1606///
1607/// \returns the result of template argument deduction so far. Note that a
1608/// "success" result means that template argument deduction has not yet failed,
1609/// but it may still fail, later, for other reasons.
1610static Sema::TemplateDeductionResult
1611DeduceTemplateArgumentsDuringPartialOrdering(ASTContext &Context,
1612 TemplateParameterList *TemplateParams,
1613 QualType ParamIn, QualType ArgIn,
1614 Sema::TemplateDeductionInfo &Info,
1615 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1616 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1617 CanQualType Param = Context.getCanonicalType(ParamIn);
1618 CanQualType Arg = Context.getCanonicalType(ArgIn);
1619
1620 // C++0x [temp.deduct.partial]p5:
1621 // Before the partial ordering is done, certain transformations are
1622 // performed on the types used for partial ordering:
1623 // - If P is a reference type, P is replaced by the type referred to.
1624 CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
1625 if (ParamRef)
1626 Param = ParamRef->getPointeeType();
1627
1628 // - If A is a reference type, A is replaced by the type referred to.
1629 CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
1630 if (ArgRef)
1631 Arg = ArgRef->getPointeeType();
1632
1633 if (QualifierComparisons && ParamRef && ArgRef) {
1634 // C++0x [temp.deduct.partial]p6:
1635 // If both P and A were reference types (before being replaced with the
1636 // type referred to above), determine which of the two types (if any) is
1637 // more cv-qualified than the other; otherwise the types are considered to
1638 // be equally cv-qualified for partial ordering purposes. The result of this
1639 // determination will be used below.
1640 //
1641 // We save this information for later, using it only when deduction
1642 // succeeds in both directions.
1643 DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
1644 if (Param.isMoreQualifiedThan(Arg))
1645 QualifierResult = ParamMoreQualified;
1646 else if (Arg.isMoreQualifiedThan(Param))
1647 QualifierResult = ArgMoreQualified;
1648 QualifierComparisons->push_back(QualifierResult);
1649 }
1650
1651 // C++0x [temp.deduct.partial]p7:
1652 // Remove any top-level cv-qualifiers:
1653 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
1654 // version of P.
1655 Param = Param.getUnqualifiedType();
1656 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
1657 // version of A.
1658 Arg = Arg.getUnqualifiedType();
1659
1660 // C++0x [temp.deduct.partial]p8:
1661 // Using the resulting types P and A the deduction is then done as
1662 // described in 14.9.2.5. If deduction succeeds for a given type, the type
1663 // from the argument template is considered to be at least as specialized
1664 // as the type from the parameter template.
1665 return DeduceTemplateArguments(Context, TemplateParams, Param, Arg, Info,
1666 Deduced, TDF_None);
1667}
1668
1669static void
1670MarkDeducedTemplateParameters(Sema &SemaRef, QualType T,
1671 llvm::SmallVectorImpl<bool> &Deduced);
1672
1673/// \brief Determine whether the function template \p FT1 is at least as
1674/// specialized as \p FT2.
1675static bool isAtLeastAsSpecializedAs(Sema &S,
1676 FunctionTemplateDecl *FT1,
1677 FunctionTemplateDecl *FT2,
1678 TemplatePartialOrderingContext TPOC,
1679 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1680 FunctionDecl *FD1 = FT1->getTemplatedDecl();
1681 FunctionDecl *FD2 = FT2->getTemplatedDecl();
1682 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
1683 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
1684
1685 assert(Proto1 && Proto2 && "Function templates must have prototypes");
1686 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
1687 llvm::SmallVector<TemplateArgument, 4> Deduced;
1688 Deduced.resize(TemplateParams->size());
1689
1690 // C++0x [temp.deduct.partial]p3:
1691 // The types used to determine the ordering depend on the context in which
1692 // the partial ordering is done:
1693 Sema::TemplateDeductionInfo Info(S.Context);
1694 switch (TPOC) {
1695 case TPOC_Call: {
1696 // - In the context of a function call, the function parameter types are
1697 // used.
1698 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
1699 for (unsigned I = 0; I != NumParams; ++I)
1700 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1701 TemplateParams,
1702 Proto2->getArgType(I),
1703 Proto1->getArgType(I),
1704 Info,
1705 Deduced,
1706 QualifierComparisons))
1707 return false;
1708
1709 break;
1710 }
1711
1712 case TPOC_Conversion:
1713 // - In the context of a call to a conversion operator, the return types
1714 // of the conversion function templates are used.
1715 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1716 TemplateParams,
1717 Proto2->getResultType(),
1718 Proto1->getResultType(),
1719 Info,
1720 Deduced,
1721 QualifierComparisons))
1722 return false;
1723 break;
1724
1725 case TPOC_Other:
1726 // - In other contexts (14.6.6.2) the function template’s function type
1727 // is used.
1728 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1729 TemplateParams,
1730 FD2->getType(),
1731 FD1->getType(),
1732 Info,
1733 Deduced,
1734 QualifierComparisons))
1735 return false;
1736 break;
1737 }
1738
1739 // C++0x [temp.deduct.partial]p11:
1740 // In most cases, all template parameters must have values in order for
1741 // deduction to succeed, but for partial ordering purposes a template
1742 // parameter may remain without a value provided it is not used in the
1743 // types being used for partial ordering. [ Note: a template parameter used
1744 // in a non-deduced context is considered used. -end note]
1745 unsigned ArgIdx = 0, NumArgs = Deduced.size();
1746 for (; ArgIdx != NumArgs; ++ArgIdx)
1747 if (Deduced[ArgIdx].isNull())
1748 break;
1749
1750 if (ArgIdx == NumArgs) {
1751 // All template arguments were deduced. FT1 is at least as specialized
1752 // as FT2.
1753 return true;
1754 }
1755
1756 // FIXME: MarkDeducedTemplateParameters needs to become
1757 // MarkUsedTemplateParameters with a flag that tells us whether to mark
1758 // template parameters that are used in non-deduced contexts.
1759 llvm::SmallVector<bool, 4> UsedParameters;
1760 UsedParameters.resize(TemplateParams->size());
1761 switch (TPOC) {
1762 case TPOC_Call: {
1763 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
1764 for (unsigned I = 0; I != NumParams; ++I)
1765 ::MarkDeducedTemplateParameters(S, Proto2->getArgType(I), UsedParameters);
1766 break;
1767 }
1768
1769 case TPOC_Conversion:
1770 ::MarkDeducedTemplateParameters(S, Proto2->getResultType(), UsedParameters);
1771 break;
1772
1773 case TPOC_Other:
1774 ::MarkDeducedTemplateParameters(S, FD2->getType(), UsedParameters);
1775 break;
1776 }
1777
1778 for (; ArgIdx != NumArgs; ++ArgIdx)
1779 // If this argument had no value deduced but was used in one of the types
1780 // used for partial ordering, then deduction fails.
1781 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
1782 return false;
1783
1784 return true;
1785}
1786
1787
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001788/// \brief Returns the more specialization function template according
1789/// to the rules of function template partial ordering (C++ [temp.func.order]).
1790///
1791/// \param FT1 the first function template
1792///
1793/// \param FT2 the second function template
1794///
Douglas Gregor8a514912009-09-14 18:39:43 +00001795/// \param TPOC the context in which we are performing partial ordering of
1796/// function templates.
Mike Stump1eb44332009-09-09 15:08:12 +00001797///
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001798/// \returns the more specialization function template. If neither
1799/// template is more specialized, returns NULL.
1800FunctionTemplateDecl *
1801Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
1802 FunctionTemplateDecl *FT2,
Douglas Gregor8a514912009-09-14 18:39:43 +00001803 TemplatePartialOrderingContext TPOC) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001804 // FIXME: Implement this
Douglas Gregor8a514912009-09-14 18:39:43 +00001805 llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
1806 bool Better1 = isAtLeastAsSpecializedAs(*this, FT1, FT2, TPOC, 0);
1807 bool Better2 = isAtLeastAsSpecializedAs(*this, FT2, FT1, TPOC,
1808 &QualifierComparisons);
1809
1810 if (Better1 != Better2) // We have a clear winner
1811 return Better1? FT1 : FT2;
1812
1813 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001814 return 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00001815
1816
1817 // C++0x [temp.deduct.partial]p10:
1818 // If for each type being considered a given template is at least as
1819 // specialized for all types and more specialized for some set of types and
1820 // the other template is not more specialized for any types or is not at
1821 // least as specialized for any types, then the given template is more
1822 // specialized than the other template. Otherwise, neither template is more
1823 // specialized than the other.
1824 Better1 = false;
1825 Better2 = false;
1826 for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
1827 // C++0x [temp.deduct.partial]p9:
1828 // If, for a given type, deduction succeeds in both directions (i.e., the
1829 // types are identical after the transformations above) and if the type
1830 // from the argument template is more cv-qualified than the type from the
1831 // parameter template (as described above) that type is considered to be
1832 // more specialized than the other. If neither type is more cv-qualified
1833 // than the other then neither type is more specialized than the other.
1834 switch (QualifierComparisons[I]) {
1835 case NeitherMoreQualified:
1836 break;
1837
1838 case ParamMoreQualified:
1839 Better1 = true;
1840 if (Better2)
1841 return 0;
1842 break;
1843
1844 case ArgMoreQualified:
1845 Better2 = true;
1846 if (Better1)
1847 return 0;
1848 break;
1849 }
1850 }
1851
1852 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001853 if (Better1)
1854 return FT1;
Douglas Gregor8a514912009-09-14 18:39:43 +00001855 else if (Better2)
1856 return FT2;
1857 else
1858 return 0;
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001859}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001860
Mike Stump1eb44332009-09-09 15:08:12 +00001861static void
Douglas Gregor031a5882009-06-13 00:26:55 +00001862MarkDeducedTemplateParameters(Sema &SemaRef,
1863 const TemplateArgument &TemplateArg,
1864 llvm::SmallVectorImpl<bool> &Deduced);
1865
1866/// \brief Mark the template arguments that are deduced by the given
1867/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001868static void
1869MarkDeducedTemplateParameters(const Expr *E,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001870 llvm::SmallVectorImpl<bool> &Deduced) {
1871 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregor031a5882009-06-13 00:26:55 +00001872 if (!E)
1873 return;
1874
Mike Stump1eb44332009-09-09 15:08:12 +00001875 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00001876 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
1877 if (!NTTP)
1878 return;
1879
1880 Deduced[NTTP->getIndex()] = true;
1881}
1882
1883/// \brief Mark the template parameters that are deduced by the given
1884/// type.
Mike Stump1eb44332009-09-09 15:08:12 +00001885static void
Douglas Gregor031a5882009-06-13 00:26:55 +00001886MarkDeducedTemplateParameters(Sema &SemaRef, QualType T,
1887 llvm::SmallVectorImpl<bool> &Deduced) {
1888 // Non-dependent types have nothing deducible
1889 if (!T->isDependentType())
1890 return;
1891
1892 T = SemaRef.Context.getCanonicalType(T);
1893 switch (T->getTypeClass()) {
1894 case Type::ExtQual:
Mike Stump1eb44332009-09-09 15:08:12 +00001895 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001896 QualType(cast<ExtQualType>(T)->getBaseType(), 0),
Douglas Gregor031a5882009-06-13 00:26:55 +00001897 Deduced);
1898 break;
1899
1900 case Type::Pointer:
1901 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001902 cast<PointerType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001903 Deduced);
1904 break;
1905
1906 case Type::BlockPointer:
1907 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001908 cast<BlockPointerType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001909 Deduced);
1910 break;
1911
1912 case Type::LValueReference:
1913 case Type::RValueReference:
1914 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001915 cast<ReferenceType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001916 Deduced);
1917 break;
1918
1919 case Type::MemberPointer: {
1920 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
1921 MarkDeducedTemplateParameters(SemaRef, MemPtr->getPointeeType(), Deduced);
1922 MarkDeducedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
1923 Deduced);
1924 break;
1925 }
1926
1927 case Type::DependentSizedArray:
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001928 MarkDeducedTemplateParameters(cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001929 Deduced);
1930 // Fall through to check the element type
1931
1932 case Type::ConstantArray:
1933 case Type::IncompleteArray:
1934 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001935 cast<ArrayType>(T)->getElementType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001936 Deduced);
1937 break;
1938
1939 case Type::Vector:
1940 case Type::ExtVector:
1941 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001942 cast<VectorType>(T)->getElementType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001943 Deduced);
1944 break;
1945
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001946 case Type::DependentSizedExtVector: {
1947 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001948 = cast<DependentSizedExtVectorType>(T);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001949 MarkDeducedTemplateParameters(SemaRef, VecType->getElementType(), Deduced);
1950 MarkDeducedTemplateParameters(VecType->getSizeExpr(), Deduced);
1951 break;
1952 }
1953
Douglas Gregor031a5882009-06-13 00:26:55 +00001954 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001955 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregor031a5882009-06-13 00:26:55 +00001956 MarkDeducedTemplateParameters(SemaRef, Proto->getResultType(), Deduced);
1957 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
1958 MarkDeducedTemplateParameters(SemaRef, Proto->getArgType(I), Deduced);
1959 break;
1960 }
1961
1962 case Type::TemplateTypeParm:
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001963 Deduced[cast<TemplateTypeParmType>(T)->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00001964 break;
1965
1966 case Type::TemplateSpecialization: {
Mike Stump1eb44332009-09-09 15:08:12 +00001967 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001968 = cast<TemplateSpecializationType>(T);
Douglas Gregor031a5882009-06-13 00:26:55 +00001969 if (TemplateDecl *Template = Spec->getTemplateName().getAsTemplateDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00001970 if (TemplateTemplateParmDecl *TTP
Douglas Gregor031a5882009-06-13 00:26:55 +00001971 = dyn_cast<TemplateTemplateParmDecl>(Template))
1972 Deduced[TTP->getIndex()] = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001973
Douglas Gregor031a5882009-06-13 00:26:55 +00001974 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
1975 MarkDeducedTemplateParameters(SemaRef, Spec->getArg(I), Deduced);
1976
1977 break;
1978 }
1979
1980 // None of these types have any deducible parts.
1981 case Type::Builtin:
1982 case Type::FixedWidthInt:
1983 case Type::Complex:
1984 case Type::VariableArray:
1985 case Type::FunctionNoProto:
1986 case Type::Record:
1987 case Type::Enum:
1988 case Type::Typename:
1989 case Type::ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001990 case Type::ObjCObjectPointer:
Douglas Gregor031a5882009-06-13 00:26:55 +00001991#define TYPE(Class, Base)
1992#define ABSTRACT_TYPE(Class, Base)
1993#define DEPENDENT_TYPE(Class, Base)
1994#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
1995#include "clang/AST/TypeNodes.def"
1996 break;
1997 }
1998}
1999
2000/// \brief Mark the template parameters that are deduced by this
2001/// template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00002002static void
Douglas Gregor031a5882009-06-13 00:26:55 +00002003MarkDeducedTemplateParameters(Sema &SemaRef,
2004 const TemplateArgument &TemplateArg,
2005 llvm::SmallVectorImpl<bool> &Deduced) {
2006 switch (TemplateArg.getKind()) {
2007 case TemplateArgument::Null:
2008 case TemplateArgument::Integral:
2009 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002010
Douglas Gregor031a5882009-06-13 00:26:55 +00002011 case TemplateArgument::Type:
2012 MarkDeducedTemplateParameters(SemaRef, TemplateArg.getAsType(), Deduced);
2013 break;
2014
2015 case TemplateArgument::Declaration:
Mike Stump1eb44332009-09-09 15:08:12 +00002016 if (TemplateTemplateParmDecl *TTP
Douglas Gregor031a5882009-06-13 00:26:55 +00002017 = dyn_cast<TemplateTemplateParmDecl>(TemplateArg.getAsDecl()))
2018 Deduced[TTP->getIndex()] = true;
2019 break;
2020
2021 case TemplateArgument::Expression:
2022 MarkDeducedTemplateParameters(TemplateArg.getAsExpr(), Deduced);
2023 break;
Anders Carlssond01b1da2009-06-15 17:04:53 +00002024 case TemplateArgument::Pack:
2025 assert(0 && "FIXME: Implement!");
2026 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00002027 }
2028}
2029
2030/// \brief Mark the template parameters can be deduced by the given
2031/// template argument list.
2032///
2033/// \param TemplateArgs the template argument list from which template
2034/// parameters will be deduced.
2035///
2036/// \param Deduced a bit vector whose elements will be set to \c true
2037/// to indicate when the corresponding template parameter will be
2038/// deduced.
Mike Stump1eb44332009-09-09 15:08:12 +00002039void
Douglas Gregor031a5882009-06-13 00:26:55 +00002040Sema::MarkDeducedTemplateParameters(const TemplateArgumentList &TemplateArgs,
2041 llvm::SmallVectorImpl<bool> &Deduced) {
2042 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
2043 ::MarkDeducedTemplateParameters(*this, TemplateArgs[I], Deduced);
2044}