blob: d9d1483c67c10f60234cbe9babc34c661f0ddd90 [file] [log] [blame]
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001//===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template argument deduction.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/StmtVisitor.h"
17#include "clang/AST/Expr.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/Parse/DeclSpec.h"
20#include "llvm/Support/Compiler.h"
Douglas Gregor8a514912009-09-14 18:39:43 +000021#include <algorithm>
Douglas Gregor508f1c82009-06-26 23:10:12 +000022
23namespace clang {
24 /// \brief Various flags that control template argument deduction.
25 ///
26 /// These flags can be bitwise-OR'd together.
27 enum TemplateDeductionFlags {
28 /// \brief No template argument deduction flags, which indicates the
29 /// strictest results for template argument deduction (as used for, e.g.,
30 /// matching class template partial specializations).
31 TDF_None = 0,
32 /// \brief Within template argument deduction from a function call, we are
33 /// matching with a parameter type for which the original parameter was
34 /// a reference.
35 TDF_ParamWithReferenceType = 0x1,
36 /// \brief Within template argument deduction from a function call, we
37 /// are matching in a case where we ignore cv-qualifiers.
38 TDF_IgnoreQualifiers = 0x02,
39 /// \brief Within template argument deduction from a function call,
40 /// we are matching in a case where we can perform template argument
Douglas Gregor41128772009-06-26 23:27:24 +000041 /// deduction from a template-id of a derived class of the argument type.
Douglas Gregor12820292009-09-14 20:00:47 +000042 TDF_DerivedClass = 0x04,
43 /// \brief Allow non-dependent types to differ, e.g., when performing
44 /// template argument deduction from a function call where conversions
45 /// may apply.
46 TDF_SkipNonDependent = 0x08
Douglas Gregor508f1c82009-06-26 23:10:12 +000047 };
48}
49
Douglas Gregor0b9247f2009-06-04 00:03:07 +000050using namespace clang;
51
Douglas Gregorf67875d2009-06-12 18:26:56 +000052static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +000053DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +000054 TemplateParameterList *TemplateParams,
55 const TemplateArgument &Param,
Douglas Gregord708c722009-06-09 16:35:58 +000056 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +000057 Sema::TemplateDeductionInfo &Info,
Douglas Gregord708c722009-06-09 16:35:58 +000058 llvm::SmallVectorImpl<TemplateArgument> &Deduced);
59
Douglas Gregor199d9912009-06-05 00:53:49 +000060/// \brief If the given expression is of a form that permits the deduction
61/// of a non-type template parameter, return the declaration of that
62/// non-type template parameter.
63static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
64 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
65 E = IC->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +000066
Douglas Gregor199d9912009-06-05 00:53:49 +000067 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
68 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +000069
Douglas Gregor199d9912009-06-05 00:53:49 +000070 return 0;
71}
72
Mike Stump1eb44332009-09-09 15:08:12 +000073/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +000074/// from the given constant.
Douglas Gregorf67875d2009-06-12 18:26:56 +000075static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +000076DeduceNonTypeTemplateArgument(ASTContext &Context,
77 NonTypeTemplateParmDecl *NTTP,
Anders Carlsson335e24a2009-06-16 22:44:31 +000078 llvm::APSInt Value,
Douglas Gregorf67875d2009-06-12 18:26:56 +000079 Sema::TemplateDeductionInfo &Info,
80 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +000081 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +000082 "Cannot deduce non-type template argument with depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +000083
Douglas Gregor199d9912009-06-05 00:53:49 +000084 if (Deduced[NTTP->getIndex()].isNull()) {
Anders Carlsson25af1ed2009-06-16 23:08:29 +000085 QualType T = NTTP->getType();
Mike Stump1eb44332009-09-09 15:08:12 +000086
Anders Carlsson25af1ed2009-06-16 23:08:29 +000087 // FIXME: Make sure we didn't overflow our data type!
88 unsigned AllowedBits = Context.getTypeSize(T);
89 if (Value.getBitWidth() != AllowedBits)
90 Value.extOrTrunc(AllowedBits);
91 Value.setIsSigned(T->isSignedIntegerType());
92
93 Deduced[NTTP->getIndex()] = TemplateArgument(SourceLocation(), Value, T);
Douglas Gregorf67875d2009-06-12 18:26:56 +000094 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +000095 }
Mike Stump1eb44332009-09-09 15:08:12 +000096
Douglas Gregorf67875d2009-06-12 18:26:56 +000097 assert(Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral);
Mike Stump1eb44332009-09-09 15:08:12 +000098
99 // If the template argument was previously deduced to a negative value,
Douglas Gregor199d9912009-06-05 00:53:49 +0000100 // then our deduction fails.
101 const llvm::APSInt *PrevValuePtr = Deduced[NTTP->getIndex()].getAsIntegral();
Anders Carlsson335e24a2009-06-16 22:44:31 +0000102 if (PrevValuePtr->isNegative()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000103 Info.Param = NTTP;
104 Info.FirstArg = Deduced[NTTP->getIndex()];
Anders Carlsson335e24a2009-06-16 22:44:31 +0000105 Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000106 return Sema::TDK_Inconsistent;
107 }
108
Anders Carlsson335e24a2009-06-16 22:44:31 +0000109 llvm::APSInt PrevValue = *PrevValuePtr;
Douglas Gregor199d9912009-06-05 00:53:49 +0000110 if (Value.getBitWidth() > PrevValue.getBitWidth())
111 PrevValue.zext(Value.getBitWidth());
112 else if (Value.getBitWidth() < PrevValue.getBitWidth())
113 Value.zext(PrevValue.getBitWidth());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000114
115 if (Value != PrevValue) {
116 Info.Param = NTTP;
117 Info.FirstArg = Deduced[NTTP->getIndex()];
Anders Carlsson335e24a2009-06-16 22:44:31 +0000118 Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000119 return Sema::TDK_Inconsistent;
120 }
121
122 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000123}
124
Mike Stump1eb44332009-09-09 15:08:12 +0000125/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +0000126/// from the given type- or value-dependent expression.
127///
128/// \returns true if deduction succeeded, false otherwise.
129
Douglas Gregorf67875d2009-06-12 18:26:56 +0000130static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +0000131DeduceNonTypeTemplateArgument(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000132 NonTypeTemplateParmDecl *NTTP,
133 Expr *Value,
134 Sema::TemplateDeductionInfo &Info,
135 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +0000136 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000137 "Cannot deduce non-type template argument with depth > 0");
138 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
139 "Expression template argument must be type- or value-dependent.");
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Douglas Gregor199d9912009-06-05 00:53:49 +0000141 if (Deduced[NTTP->getIndex()].isNull()) {
142 // FIXME: Clone the Value?
143 Deduced[NTTP->getIndex()] = TemplateArgument(Value);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000144 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000145 }
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Douglas Gregor199d9912009-06-05 00:53:49 +0000147 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
Mike Stump1eb44332009-09-09 15:08:12 +0000148 // Okay, we deduced a constant in one case and a dependent expression
149 // in another case. FIXME: Later, we will check that instantiating the
Douglas Gregor199d9912009-06-05 00:53:49 +0000150 // dependent expression gives us the constant value.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000151 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000152 }
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Douglas Gregor199d9912009-06-05 00:53:49 +0000154 // FIXME: Compare the expressions for equality!
Douglas Gregorf67875d2009-06-12 18:26:56 +0000155 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000156}
157
Douglas Gregorf67875d2009-06-12 18:26:56 +0000158static Sema::TemplateDeductionResult
159DeduceTemplateArguments(ASTContext &Context,
160 TemplateName Param,
161 TemplateName Arg,
162 Sema::TemplateDeductionInfo &Info,
163 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregord708c722009-06-09 16:35:58 +0000164 // FIXME: Implement template argument deduction for template
165 // template parameters.
166
Douglas Gregorf67875d2009-06-12 18:26:56 +0000167 // FIXME: this routine does not have enough information to produce
168 // good diagnostics.
169
Douglas Gregord708c722009-06-09 16:35:58 +0000170 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
171 TemplateDecl *ArgDecl = Arg.getAsTemplateDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Douglas Gregorf67875d2009-06-12 18:26:56 +0000173 if (!ParamDecl || !ArgDecl) {
174 // FIXME: fill in Info.Param/Info.FirstArg
175 return Sema::TDK_Inconsistent;
176 }
Douglas Gregord708c722009-06-09 16:35:58 +0000177
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000178 ParamDecl = cast<TemplateDecl>(ParamDecl->getCanonicalDecl());
179 ArgDecl = cast<TemplateDecl>(ArgDecl->getCanonicalDecl());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000180 if (ParamDecl != ArgDecl) {
181 // FIXME: fill in Info.Param/Info.FirstArg
182 return Sema::TDK_Inconsistent;
183 }
184
185 return Sema::TDK_Success;
Douglas Gregord708c722009-06-09 16:35:58 +0000186}
187
Mike Stump1eb44332009-09-09 15:08:12 +0000188/// \brief Deduce the template arguments by comparing the template parameter
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000189/// type (which is a template-id) with the template argument type.
190///
191/// \param Context the AST context in which this deduction occurs.
192///
193/// \param TemplateParams the template parameters that we are deducing
194///
195/// \param Param the parameter type
196///
197/// \param Arg the argument type
198///
199/// \param Info information about the template argument deduction itself
200///
201/// \param Deduced the deduced template arguments
202///
203/// \returns the result of template argument deduction so far. Note that a
204/// "success" result means that template argument deduction has not yet failed,
205/// but it may still fail, later, for other reasons.
206static Sema::TemplateDeductionResult
207DeduceTemplateArguments(ASTContext &Context,
208 TemplateParameterList *TemplateParams,
209 const TemplateSpecializationType *Param,
210 QualType Arg,
211 Sema::TemplateDeductionInfo &Info,
212 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
213 assert(Arg->isCanonical() && "Argument type must be canonical");
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000215 // Check whether the template argument is a dependent template-id.
216 // FIXME: This is untested code; it can be tested when we implement
217 // partial ordering of class template partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +0000218 if (const TemplateSpecializationType *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000219 = dyn_cast<TemplateSpecializationType>(Arg)) {
220 // Perform template argument deduction for the template name.
221 if (Sema::TemplateDeductionResult Result
222 = DeduceTemplateArguments(Context,
223 Param->getTemplateName(),
224 SpecArg->getTemplateName(),
225 Info, Deduced))
226 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000228 unsigned NumArgs = Param->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000230 // FIXME: When one of the template-names refers to a
231 // declaration with default template arguments, do we need to
232 // fill in those default template arguments here? Most likely,
233 // the answer is "yes", but I don't see any references. This
234 // issue may be resolved elsewhere, because we may want to
235 // instantiate default template arguments when we actually write
236 // the template-id.
237 if (SpecArg->getNumArgs() != NumArgs)
238 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000240 // Perform template argument deduction on each template
241 // argument.
242 for (unsigned I = 0; I != NumArgs; ++I)
243 if (Sema::TemplateDeductionResult Result
244 = DeduceTemplateArguments(Context, TemplateParams,
245 Param->getArg(I),
246 SpecArg->getArg(I),
247 Info, Deduced))
248 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000250 return Sema::TDK_Success;
251 }
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000253 // If the argument type is a class template specialization, we
254 // perform template argument deduction using its template
255 // arguments.
256 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
257 if (!RecordArg)
258 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000259
260 ClassTemplateSpecializationDecl *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000261 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
262 if (!SpecArg)
263 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000265 // Perform template argument deduction for the template name.
266 if (Sema::TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000267 = DeduceTemplateArguments(Context,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000268 Param->getTemplateName(),
269 TemplateName(SpecArg->getSpecializedTemplate()),
270 Info, Deduced))
271 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000273 // FIXME: Can the # of arguments in the parameter and the argument
274 // differ due to default arguments?
275 unsigned NumArgs = Param->getNumArgs();
276 const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
277 if (NumArgs != ArgArgs.size())
278 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000280 for (unsigned I = 0; I != NumArgs; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000281 if (Sema::TemplateDeductionResult Result
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000282 = DeduceTemplateArguments(Context, TemplateParams,
283 Param->getArg(I),
284 ArgArgs.get(I),
285 Info, Deduced))
286 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000288 return Sema::TDK_Success;
289}
290
Mike Stump1eb44332009-09-09 15:08:12 +0000291/// \brief Returns a completely-unqualified array type, capturing the
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000292/// qualifiers in CVRQuals.
293///
294/// \param Context the AST context in which the array type was built.
295///
296/// \param T a canonical type that may be an array type.
297///
298/// \param CVRQuals will receive the set of const/volatile/restrict qualifiers
299/// that were applied to the element type of the array.
300///
301/// \returns if \p T is an array type, the completely unqualified array type
302/// that corresponds to T. Otherwise, returns T.
303static QualType getUnqualifiedArrayType(ASTContext &Context, QualType T,
304 unsigned &CVRQuals) {
305 assert(T->isCanonical() && "Only operates on canonical types");
306 if (!isa<ArrayType>(T)) {
307 CVRQuals = T.getCVRQualifiers();
308 return T.getUnqualifiedType();
309 }
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000311 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) {
312 QualType Elt = getUnqualifiedArrayType(Context, CAT->getElementType(),
313 CVRQuals);
314 if (Elt == CAT->getElementType())
315 return T;
316
Mike Stump1eb44332009-09-09 15:08:12 +0000317 return Context.getConstantArrayType(Elt, CAT->getSize(),
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000318 CAT->getSizeModifier(), 0);
319 }
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000321 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) {
322 QualType Elt = getUnqualifiedArrayType(Context, IAT->getElementType(),
323 CVRQuals);
324 if (Elt == IAT->getElementType())
325 return T;
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000327 return Context.getIncompleteArrayType(Elt, IAT->getSizeModifier(), 0);
328 }
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000330 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T);
331 QualType Elt = getUnqualifiedArrayType(Context, DSAT->getElementType(),
332 CVRQuals);
333 if (Elt == DSAT->getElementType())
334 return T;
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Anders Carlssond4972062009-08-08 02:50:17 +0000336 return Context.getDependentSizedArrayType(Elt, DSAT->getSizeExpr()->Retain(),
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000337 DSAT->getSizeModifier(), 0,
338 SourceRange());
339}
340
Douglas Gregor500d3312009-06-26 18:27:22 +0000341/// \brief Deduce the template arguments by comparing the parameter type and
342/// the argument type (C++ [temp.deduct.type]).
343///
344/// \param Context the AST context in which this deduction occurs.
345///
346/// \param TemplateParams the template parameters that we are deducing
347///
348/// \param ParamIn the parameter type
349///
350/// \param ArgIn the argument type
351///
352/// \param Info information about the template argument deduction itself
353///
354/// \param Deduced the deduced template arguments
355///
Douglas Gregor508f1c82009-06-26 23:10:12 +0000356/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
Mike Stump1eb44332009-09-09 15:08:12 +0000357/// how template argument deduction is performed.
Douglas Gregor500d3312009-06-26 18:27:22 +0000358///
359/// \returns the result of template argument deduction so far. Note that a
360/// "success" result means that template argument deduction has not yet failed,
361/// but it may still fail, later, for other reasons.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000362static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +0000363DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000364 TemplateParameterList *TemplateParams,
365 QualType ParamIn, QualType ArgIn,
366 Sema::TemplateDeductionInfo &Info,
Douglas Gregor500d3312009-06-26 18:27:22 +0000367 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000368 unsigned TDF) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000369 // We only want to look at the canonical types, since typedefs and
370 // sugar are not part of template argument deduction.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000371 QualType Param = Context.getCanonicalType(ParamIn);
372 QualType Arg = Context.getCanonicalType(ArgIn);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000373
Douglas Gregor500d3312009-06-26 18:27:22 +0000374 // C++0x [temp.deduct.call]p4 bullet 1:
375 // - If the original P is a reference type, the deduced A (i.e., the type
Mike Stump1eb44332009-09-09 15:08:12 +0000376 // referred to by the reference) can be more cv-qualified than the
Douglas Gregor500d3312009-06-26 18:27:22 +0000377 // transformed A.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000378 if (TDF & TDF_ParamWithReferenceType) {
Mike Stump1eb44332009-09-09 15:08:12 +0000379 unsigned ExtraQualsOnParam
Douglas Gregor500d3312009-06-26 18:27:22 +0000380 = Param.getCVRQualifiers() & ~Arg.getCVRQualifiers();
381 Param.setCVRQualifiers(Param.getCVRQualifiers() & ~ExtraQualsOnParam);
382 }
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000384 // If the parameter type is not dependent, there is nothing to deduce.
Douglas Gregor12820292009-09-14 20:00:47 +0000385 if (!Param->isDependentType()) {
386 if (!(TDF & TDF_SkipNonDependent) && Param != Arg) {
387
388 return Sema::TDK_NonDeducedMismatch;
389 }
390
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000391 return Sema::TDK_Success;
Douglas Gregor12820292009-09-14 20:00:47 +0000392 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000393
Douglas Gregor199d9912009-06-05 00:53:49 +0000394 // C++ [temp.deduct.type]p9:
Mike Stump1eb44332009-09-09 15:08:12 +0000395 // A template type argument T, a template template argument TT or a
396 // template non-type argument i can be deduced if P and A have one of
Douglas Gregor199d9912009-06-05 00:53:49 +0000397 // the following forms:
398 //
399 // T
400 // cv-list T
Mike Stump1eb44332009-09-09 15:08:12 +0000401 if (const TemplateTypeParmType *TemplateTypeParm
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000402 = Param->getAsTemplateTypeParmType()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000403 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000404 bool RecanonicalizeArg = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Douglas Gregor9e9fae42009-07-22 20:02:25 +0000406 // If the argument type is an array type, move the qualifiers up to the
407 // top level, so they can be matched with the qualifiers on the parameter.
408 // FIXME: address spaces, ObjC GC qualifiers
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000409 if (isa<ArrayType>(Arg)) {
410 unsigned CVRQuals = 0;
411 Arg = getUnqualifiedArrayType(Context, Arg, CVRQuals);
412 if (CVRQuals) {
413 Arg = Arg.getWithAdditionalQualifiers(CVRQuals);
414 RecanonicalizeArg = true;
415 }
416 }
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000418 // The argument type can not be less qualified than the parameter
419 // type.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000420 if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000421 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
422 Info.FirstArg = Deduced[Index];
423 Info.SecondArg = TemplateArgument(SourceLocation(), Arg);
424 return Sema::TDK_InconsistentQuals;
425 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000426
427 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000429 unsigned Quals = Arg.getCVRQualifiers() & ~Param.getCVRQualifiers();
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000430 QualType DeducedType = Arg.getQualifiedType(Quals);
431 if (RecanonicalizeArg)
432 DeducedType = Context.getCanonicalType(DeducedType);
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000434 if (Deduced[Index].isNull())
435 Deduced[Index] = TemplateArgument(SourceLocation(), DeducedType);
436 else {
Mike Stump1eb44332009-09-09 15:08:12 +0000437 // C++ [temp.deduct.type]p2:
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000438 // [...] If type deduction cannot be done for any P/A pair, or if for
Mike Stump1eb44332009-09-09 15:08:12 +0000439 // any pair the deduction leads to more than one possible set of
440 // deduced values, or if different pairs yield different deduced
441 // values, or if any template argument remains neither deduced nor
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000442 // explicitly specified, template argument deduction fails.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000443 if (Deduced[Index].getAsType() != DeducedType) {
Mike Stump1eb44332009-09-09 15:08:12 +0000444 Info.Param
Douglas Gregorf67875d2009-06-12 18:26:56 +0000445 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
446 Info.FirstArg = Deduced[Index];
447 Info.SecondArg = TemplateArgument(SourceLocation(), Arg);
448 return Sema::TDK_Inconsistent;
449 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000450 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000451 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000452 }
453
Douglas Gregorf67875d2009-06-12 18:26:56 +0000454 // Set up the template argument deduction information for a failure.
455 Info.FirstArg = TemplateArgument(SourceLocation(), ParamIn);
456 Info.SecondArg = TemplateArgument(SourceLocation(), ArgIn);
457
Douglas Gregor508f1c82009-06-26 23:10:12 +0000458 // Check the cv-qualifiers on the parameter and argument types.
459 if (!(TDF & TDF_IgnoreQualifiers)) {
460 if (TDF & TDF_ParamWithReferenceType) {
461 if (Param.isMoreQualifiedThan(Arg))
462 return Sema::TDK_NonDeducedMismatch;
463 } else {
464 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Mike Stump1eb44332009-09-09 15:08:12 +0000465 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor508f1c82009-06-26 23:10:12 +0000466 }
467 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000468
Douglas Gregord560d502009-06-04 00:21:18 +0000469 switch (Param->getTypeClass()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000470 // No deduction possible for these types
471 case Type::Builtin:
Douglas Gregorf67875d2009-06-12 18:26:56 +0000472 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Douglas Gregor199d9912009-06-05 00:53:49 +0000474 // T *
Douglas Gregord560d502009-06-04 00:21:18 +0000475 case Type::Pointer: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000476 const PointerType *PointerArg = Arg->getAs<PointerType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000477 if (!PointerArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000478 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Douglas Gregor41128772009-06-26 23:27:24 +0000480 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000481 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000482 cast<PointerType>(Param)->getPointeeType(),
483 PointerArg->getPointeeType(),
Douglas Gregor41128772009-06-26 23:27:24 +0000484 Info, Deduced, SubTDF);
Douglas Gregord560d502009-06-04 00:21:18 +0000485 }
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Douglas Gregor199d9912009-06-05 00:53:49 +0000487 // T &
Douglas Gregord560d502009-06-04 00:21:18 +0000488 case Type::LValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000489 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000490 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000491 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Douglas Gregorf67875d2009-06-12 18:26:56 +0000493 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000494 cast<LValueReferenceType>(Param)->getPointeeType(),
495 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000496 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000497 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000498
Douglas Gregor199d9912009-06-05 00:53:49 +0000499 // T && [C++0x]
Douglas Gregord560d502009-06-04 00:21:18 +0000500 case Type::RValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000501 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000502 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000503 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Douglas Gregorf67875d2009-06-12 18:26:56 +0000505 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000506 cast<RValueReferenceType>(Param)->getPointeeType(),
507 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000508 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000509 }
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Douglas Gregor199d9912009-06-05 00:53:49 +0000511 // T [] (implied, but not stated explicitly)
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000512 case Type::IncompleteArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000513 const IncompleteArrayType *IncompleteArrayArg =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000514 Context.getAsIncompleteArrayType(Arg);
515 if (!IncompleteArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000516 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Douglas Gregorf67875d2009-06-12 18:26:56 +0000518 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000519 Context.getAsIncompleteArrayType(Param)->getElementType(),
520 IncompleteArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000521 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000522 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000523
524 // T [integer-constant]
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000525 case Type::ConstantArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000526 const ConstantArrayType *ConstantArrayArg =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000527 Context.getAsConstantArrayType(Arg);
528 if (!ConstantArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000529 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000530
531 const ConstantArrayType *ConstantArrayParm =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000532 Context.getAsConstantArrayType(Param);
533 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000534 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Douglas Gregorf67875d2009-06-12 18:26:56 +0000536 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000537 ConstantArrayParm->getElementType(),
538 ConstantArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000539 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000540 }
541
Douglas Gregor199d9912009-06-05 00:53:49 +0000542 // type [i]
543 case Type::DependentSizedArray: {
544 const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg);
545 if (!ArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000546 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Douglas Gregor199d9912009-06-05 00:53:49 +0000548 // Check the element type of the arrays
549 const DependentSizedArrayType *DependentArrayParm
550 = cast<DependentSizedArrayType>(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000551 if (Sema::TemplateDeductionResult Result
552 = DeduceTemplateArguments(Context, TemplateParams,
553 DependentArrayParm->getElementType(),
554 ArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000555 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000556 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Douglas Gregor199d9912009-06-05 00:53:49 +0000558 // Determine the array bound is something we can deduce.
Mike Stump1eb44332009-09-09 15:08:12 +0000559 NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000560 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
561 if (!NTTP)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000562 return Sema::TDK_Success;
Mike Stump1eb44332009-09-09 15:08:12 +0000563
564 // We can perform template argument deduction for the given non-type
Douglas Gregor199d9912009-06-05 00:53:49 +0000565 // template parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000566 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000567 "Cannot deduce non-type template argument at depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000568 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson335e24a2009-06-16 22:44:31 +0000569 = dyn_cast<ConstantArrayType>(ArrayArg)) {
570 llvm::APSInt Size(ConstantArrayArg->getSize());
571 return DeduceNonTypeTemplateArgument(Context, NTTP, Size,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000572 Info, Deduced);
Anders Carlsson335e24a2009-06-16 22:44:31 +0000573 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000574 if (const DependentSizedArrayType *DependentArrayArg
575 = dyn_cast<DependentSizedArrayType>(ArrayArg))
576 return DeduceNonTypeTemplateArgument(Context, NTTP,
577 DependentArrayArg->getSizeExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000578 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Douglas Gregor199d9912009-06-05 00:53:49 +0000580 // Incomplete type does not match a dependently-sized array type
Douglas Gregorf67875d2009-06-12 18:26:56 +0000581 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000582 }
Mike Stump1eb44332009-09-09 15:08:12 +0000583
584 // type(*)(T)
585 // T(*)()
586 // T(*)(T)
Anders Carlssona27fad52009-06-08 15:19:08 +0000587 case Type::FunctionProto: {
Mike Stump1eb44332009-09-09 15:08:12 +0000588 const FunctionProtoType *FunctionProtoArg =
Anders Carlssona27fad52009-06-08 15:19:08 +0000589 dyn_cast<FunctionProtoType>(Arg);
590 if (!FunctionProtoArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000591 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000592
593 const FunctionProtoType *FunctionProtoParam =
Anders Carlssona27fad52009-06-08 15:19:08 +0000594 cast<FunctionProtoType>(Param);
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000595
Mike Stump1eb44332009-09-09 15:08:12 +0000596 if (FunctionProtoParam->getTypeQuals() !=
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000597 FunctionProtoArg->getTypeQuals())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000598 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000600 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000601 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000603 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000604 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000605
Anders Carlssona27fad52009-06-08 15:19:08 +0000606 // Check return types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000607 if (Sema::TemplateDeductionResult Result
608 = DeduceTemplateArguments(Context, TemplateParams,
609 FunctionProtoParam->getResultType(),
610 FunctionProtoArg->getResultType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000611 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000612 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Anders Carlssona27fad52009-06-08 15:19:08 +0000614 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
615 // Check argument types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000616 if (Sema::TemplateDeductionResult Result
617 = DeduceTemplateArguments(Context, TemplateParams,
618 FunctionProtoParam->getArgType(I),
619 FunctionProtoArg->getArgType(I),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000620 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000621 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000622 }
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Douglas Gregorf67875d2009-06-12 18:26:56 +0000624 return Sema::TDK_Success;
Anders Carlssona27fad52009-06-08 15:19:08 +0000625 }
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000627 // template-name<T> (where template-name refers to a class template)
Douglas Gregord708c722009-06-09 16:35:58 +0000628 // template-name<i>
629 // TT<T> (TODO)
630 // TT<i> (TODO)
631 // TT<> (TODO)
632 case Type::TemplateSpecialization: {
633 const TemplateSpecializationType *SpecParam
634 = cast<TemplateSpecializationType>(Param);
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000636 // Try to deduce template arguments from the template-id.
637 Sema::TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000638 = DeduceTemplateArguments(Context, TemplateParams, SpecParam, Arg,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000639 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000640
641 if (Result && (TDF & TDF_DerivedClass) &&
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000642 Result != Sema::TDK_Inconsistent) {
643 // C++ [temp.deduct.call]p3b3:
644 // If P is a class, and P has the form template-id, then A can be a
645 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump1eb44332009-09-09 15:08:12 +0000646 // class of the form template-id, A can be a pointer to a derived
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000647 // class pointed to by the deduced A.
648 //
649 // More importantly:
Mike Stump1eb44332009-09-09 15:08:12 +0000650 // These alternatives are considered only if type deduction would
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000651 // otherwise fail.
652 if (const RecordType *RecordT = dyn_cast<RecordType>(Arg)) {
653 // Use data recursion to crawl through the list of base classes.
Mike Stump1eb44332009-09-09 15:08:12 +0000654 // Visited contains the set of nodes we have already visited, while
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000655 // ToVisit is our stack of records that we still need to visit.
656 llvm::SmallPtrSet<const RecordType *, 8> Visited;
657 llvm::SmallVector<const RecordType *, 8> ToVisit;
658 ToVisit.push_back(RecordT);
659 bool Successful = false;
660 while (!ToVisit.empty()) {
661 // Retrieve the next class in the inheritance hierarchy.
662 const RecordType *NextT = ToVisit.back();
663 ToVisit.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000665 // If we have already seen this type, skip it.
666 if (!Visited.insert(NextT))
667 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000669 // If this is a base class, try to perform template argument
670 // deduction from it.
671 if (NextT != RecordT) {
672 Sema::TemplateDeductionResult BaseResult
673 = DeduceTemplateArguments(Context, TemplateParams, SpecParam,
674 QualType(NextT, 0), Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000676 // If template argument deduction for this base was successful,
677 // note that we had some success.
678 if (BaseResult == Sema::TDK_Success)
679 Successful = true;
680 // If deduction against this base resulted in an inconsistent
681 // set of deduced template arguments, template argument
682 // deduction fails.
683 else if (BaseResult == Sema::TDK_Inconsistent)
684 return BaseResult;
685 }
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000687 // Visit base classes
688 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
689 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
690 BaseEnd = Next->bases_end();
691 Base != BaseEnd; ++Base) {
Mike Stump1eb44332009-09-09 15:08:12 +0000692 assert(Base->getType()->isRecordType() &&
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000693 "Base class that isn't a record?");
Ted Kremenek6217b802009-07-29 21:53:49 +0000694 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000695 }
696 }
Mike Stump1eb44332009-09-09 15:08:12 +0000697
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000698 if (Successful)
699 return Sema::TDK_Success;
700 }
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000702 }
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000704 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000705 }
706
Douglas Gregor637a4092009-06-10 23:47:09 +0000707 // T type::*
708 // T T::*
709 // T (type::*)()
710 // type (T::*)()
711 // type (type::*)(T)
712 // type (T::*)(T)
713 // T (type::*)(T)
714 // T (T::*)()
715 // T (T::*)(T)
716 case Type::MemberPointer: {
717 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
718 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
719 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000720 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +0000721
Douglas Gregorf67875d2009-06-12 18:26:56 +0000722 if (Sema::TemplateDeductionResult Result
723 = DeduceTemplateArguments(Context, TemplateParams,
724 MemPtrParam->getPointeeType(),
725 MemPtrArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000726 Info, Deduced,
727 TDF & TDF_IgnoreQualifiers))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000728 return Result;
729
730 return DeduceTemplateArguments(Context, TemplateParams,
731 QualType(MemPtrParam->getClass(), 0),
732 QualType(MemPtrArg->getClass(), 0),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000733 Info, Deduced, 0);
Douglas Gregor637a4092009-06-10 23:47:09 +0000734 }
735
Anders Carlsson9a917e42009-06-12 22:56:54 +0000736 // (clang extension)
737 //
Mike Stump1eb44332009-09-09 15:08:12 +0000738 // type(^)(T)
739 // T(^)()
740 // T(^)(T)
Anders Carlsson859ba502009-06-12 16:23:10 +0000741 case Type::BlockPointer: {
742 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
743 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Anders Carlsson859ba502009-06-12 16:23:10 +0000745 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000746 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Douglas Gregorf67875d2009-06-12 18:26:56 +0000748 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +0000749 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000750 BlockPtrArg->getPointeeType(), Info,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000751 Deduced, 0);
Anders Carlsson859ba502009-06-12 16:23:10 +0000752 }
753
Douglas Gregor637a4092009-06-10 23:47:09 +0000754 case Type::TypeOfExpr:
755 case Type::TypeOf:
756 case Type::Typename:
757 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +0000758 return Sema::TDK_Success;
Douglas Gregor637a4092009-06-10 23:47:09 +0000759
Douglas Gregord560d502009-06-04 00:21:18 +0000760 default:
761 break;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000762 }
763
764 // FIXME: Many more cases to go (to go).
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000765 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000766}
767
Douglas Gregorf67875d2009-06-12 18:26:56 +0000768static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +0000769DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000770 TemplateParameterList *TemplateParams,
771 const TemplateArgument &Param,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000772 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000773 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000774 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000775 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000776 case TemplateArgument::Null:
777 assert(false && "Null template argument in parameter list");
778 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000779
780 case TemplateArgument::Type:
Douglas Gregor199d9912009-06-05 00:53:49 +0000781 assert(Arg.getKind() == TemplateArgument::Type && "Type/value mismatch");
Douglas Gregor508f1c82009-06-26 23:10:12 +0000782 return DeduceTemplateArguments(Context, TemplateParams, Param.getAsType(),
783 Arg.getAsType(), Info, Deduced, 0);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000784
Douglas Gregor199d9912009-06-05 00:53:49 +0000785 case TemplateArgument::Declaration:
786 // FIXME: Implement this check
787 assert(false && "Unimplemented template argument deduction case");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000788 Info.FirstArg = Param;
789 Info.SecondArg = Arg;
790 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Douglas Gregor199d9912009-06-05 00:53:49 +0000792 case TemplateArgument::Integral:
793 if (Arg.getKind() == TemplateArgument::Integral) {
794 // FIXME: Zero extension + sign checking here?
Douglas Gregorf67875d2009-06-12 18:26:56 +0000795 if (*Param.getAsIntegral() == *Arg.getAsIntegral())
796 return Sema::TDK_Success;
797
798 Info.FirstArg = Param;
799 Info.SecondArg = Arg;
800 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000801 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000802
803 if (Arg.getKind() == TemplateArgument::Expression) {
804 Info.FirstArg = Param;
805 Info.SecondArg = Arg;
806 return Sema::TDK_NonDeducedMismatch;
807 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000808
809 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000810 Info.FirstArg = Param;
811 Info.SecondArg = Arg;
812 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Douglas Gregor199d9912009-06-05 00:53:49 +0000814 case TemplateArgument::Expression: {
Mike Stump1eb44332009-09-09 15:08:12 +0000815 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000816 = getDeducedParameterFromExpr(Param.getAsExpr())) {
817 if (Arg.getKind() == TemplateArgument::Integral)
818 // FIXME: Sign problems here
Mike Stump1eb44332009-09-09 15:08:12 +0000819 return DeduceNonTypeTemplateArgument(Context, NTTP,
820 *Arg.getAsIntegral(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000821 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000822 if (Arg.getKind() == TemplateArgument::Expression)
823 return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000824 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Douglas Gregor199d9912009-06-05 00:53:49 +0000826 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000827 Info.FirstArg = Param;
828 Info.SecondArg = Arg;
829 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000830 }
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Douglas Gregor199d9912009-06-05 00:53:49 +0000832 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000833 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000834 }
Anders Carlssond01b1da2009-06-15 17:04:53 +0000835 case TemplateArgument::Pack:
836 assert(0 && "FIXME: Implement!");
837 break;
Douglas Gregor199d9912009-06-05 00:53:49 +0000838 }
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Douglas Gregorf67875d2009-06-12 18:26:56 +0000840 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000841}
842
Mike Stump1eb44332009-09-09 15:08:12 +0000843static Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000844DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000845 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000846 const TemplateArgumentList &ParamList,
847 const TemplateArgumentList &ArgList,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000848 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000849 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
850 assert(ParamList.size() == ArgList.size());
851 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000852 if (Sema::TemplateDeductionResult Result
853 = DeduceTemplateArguments(Context, TemplateParams,
Mike Stump1eb44332009-09-09 15:08:12 +0000854 ParamList[I], ArgList[I],
Douglas Gregorf67875d2009-06-12 18:26:56 +0000855 Info, Deduced))
856 return Result;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000857 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000858 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000859}
860
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000861/// \brief Determine whether two template arguments are the same.
Mike Stump1eb44332009-09-09 15:08:12 +0000862static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000863 const TemplateArgument &X,
864 const TemplateArgument &Y) {
865 if (X.getKind() != Y.getKind())
866 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000868 switch (X.getKind()) {
869 case TemplateArgument::Null:
870 assert(false && "Comparing NULL template argument");
871 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000873 case TemplateArgument::Type:
874 return Context.getCanonicalType(X.getAsType()) ==
875 Context.getCanonicalType(Y.getAsType());
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000877 case TemplateArgument::Declaration:
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000878 return X.getAsDecl()->getCanonicalDecl() ==
879 Y.getAsDecl()->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000881 case TemplateArgument::Integral:
882 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000884 case TemplateArgument::Expression:
885 // FIXME: We assume that all expressions are distinct, but we should
886 // really check their canonical forms.
887 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000889 case TemplateArgument::Pack:
890 if (X.pack_size() != Y.pack_size())
891 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000892
893 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
894 XPEnd = X.pack_end(),
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000895 YP = Y.pack_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000896 XP != XPEnd; ++XP, ++YP)
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000897 if (!isSameTemplateArg(Context, *XP, *YP))
898 return false;
899
900 return true;
901 }
902
903 return false;
904}
905
906/// \brief Helper function to build a TemplateParameter when we don't
907/// know its type statically.
908static TemplateParameter makeTemplateParameter(Decl *D) {
909 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
910 return TemplateParameter(TTP);
911 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
912 return TemplateParameter(NTTP);
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000914 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
915}
916
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000917/// \brief Perform template argument deduction to determine whether
918/// the given template arguments match the given class template
919/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +0000920Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000921Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000922 const TemplateArgumentList &TemplateArgs,
923 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000924 // C++ [temp.class.spec.match]p2:
925 // A partial specialization matches a given actual template
926 // argument list if the template arguments of the partial
927 // specialization can be deduced from the actual template argument
928 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +0000929 SFINAETrap Trap(*this);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000930 llvm::SmallVector<TemplateArgument, 4> Deduced;
931 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000932 if (TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000933 = ::DeduceTemplateArguments(Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000934 Partial->getTemplateParameters(),
Mike Stump1eb44332009-09-09 15:08:12 +0000935 Partial->getTemplateArgs(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000936 TemplateArgs, Info, Deduced))
937 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +0000938
Douglas Gregor637a4092009-06-10 23:47:09 +0000939 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
940 Deduced.data(), Deduced.size());
941 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000942 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +0000943
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000944 // C++ [temp.deduct.type]p2:
945 // [...] or if any template argument remains neither deduced nor
946 // explicitly specified, template argument deduction fails.
Anders Carlssonfb250522009-06-23 01:26:57 +0000947 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
948 Deduced.size());
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000949 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000950 if (Deduced[I].isNull()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000951 Decl *Param
Douglas Gregorf67875d2009-06-12 18:26:56 +0000952 = const_cast<Decl *>(Partial->getTemplateParameters()->getParam(I));
953 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
954 Info.Param = TTP;
Mike Stump1eb44332009-09-09 15:08:12 +0000955 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorf67875d2009-06-12 18:26:56 +0000956 = dyn_cast<NonTypeTemplateParmDecl>(Param))
957 Info.Param = NTTP;
958 else
959 Info.Param = cast<TemplateTemplateParmDecl>(Param);
960 return TDK_Incomplete;
961 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000962
Anders Carlssonfb250522009-06-23 01:26:57 +0000963 Builder.Append(Deduced[I]);
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000964 }
965
966 // Form the template argument list from the deduced template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +0000967 TemplateArgumentList *DeducedArgumentList
Anders Carlssonfb250522009-06-23 01:26:57 +0000968 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000969 Info.reset(DeducedArgumentList);
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000970
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000971 // Substitute the deduced template arguments into the template
972 // arguments of the class template partial specialization, and
973 // verify that the instantiated template arguments are both valid
974 // and are equivalent to the template arguments originally provided
Mike Stump1eb44332009-09-09 15:08:12 +0000975 // to the class template.
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000976 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
977 const TemplateArgumentList &PartialTemplateArgs = Partial->getTemplateArgs();
978 for (unsigned I = 0, N = PartialTemplateArgs.flat_size(); I != N; ++I) {
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000979 Decl *Param = const_cast<Decl *>(
980 ClassTemplate->getTemplateParameters()->getParam(I));
Mike Stump1eb44332009-09-09 15:08:12 +0000981 TemplateArgument InstArg
Douglas Gregor357bbd02009-08-28 20:50:45 +0000982 = Subst(PartialTemplateArgs[I],
983 MultiLevelTemplateArgumentList(*DeducedArgumentList));
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000984 if (InstArg.isNull()) {
985 Info.Param = makeTemplateParameter(Param);
986 Info.FirstArg = PartialTemplateArgs[I];
Mike Stump1eb44332009-09-09 15:08:12 +0000987 return TDK_SubstitutionFailure;
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000988 }
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000990 if (InstArg.getKind() == TemplateArgument::Expression) {
Mike Stump1eb44332009-09-09 15:08:12 +0000991 // When the argument is an expression, check the expression result
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000992 // against the actual template parameter to get down to the canonical
993 // template argument.
994 Expr *InstExpr = InstArg.getAsExpr();
Mike Stump1eb44332009-09-09 15:08:12 +0000995 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000996 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
997 if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
998 Info.Param = makeTemplateParameter(Param);
999 Info.FirstArg = PartialTemplateArgs[I];
Mike Stump1eb44332009-09-09 15:08:12 +00001000 return TDK_SubstitutionFailure;
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001001 }
Mike Stump1eb44332009-09-09 15:08:12 +00001002 } else if (TemplateTemplateParmDecl *TTP
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001003 = dyn_cast<TemplateTemplateParmDecl>(Param)) {
1004 // FIXME: template template arguments should really resolve to decls
1005 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InstExpr);
1006 if (!DRE || CheckTemplateArgument(TTP, DRE)) {
1007 Info.Param = makeTemplateParameter(Param);
1008 Info.FirstArg = PartialTemplateArgs[I];
Mike Stump1eb44332009-09-09 15:08:12 +00001009 return TDK_SubstitutionFailure;
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001010 }
1011 }
1012 }
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001014 if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) {
1015 Info.Param = makeTemplateParameter(Param);
1016 Info.FirstArg = TemplateArgs[I];
1017 Info.SecondArg = InstArg;
1018 return TDK_NonDeducedMismatch;
1019 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001020 }
1021
Douglas Gregorbb260412009-06-14 08:02:22 +00001022 if (Trap.hasErrorOccurred())
1023 return TDK_SubstitutionFailure;
1024
Douglas Gregorf67875d2009-06-12 18:26:56 +00001025 return TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001026}
Douglas Gregor031a5882009-06-13 00:26:55 +00001027
Douglas Gregor41128772009-06-26 23:27:24 +00001028/// \brief Determine whether the given type T is a simple-template-id type.
1029static bool isSimpleTemplateIdType(QualType T) {
Mike Stump1eb44332009-09-09 15:08:12 +00001030 if (const TemplateSpecializationType *Spec
Douglas Gregor41128772009-06-26 23:27:24 +00001031 = T->getAsTemplateSpecializationType())
1032 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Douglas Gregor41128772009-06-26 23:27:24 +00001034 return false;
1035}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001036
1037/// \brief Substitute the explicitly-provided template arguments into the
1038/// given function template according to C++ [temp.arg.explicit].
1039///
1040/// \param FunctionTemplate the function template into which the explicit
1041/// template arguments will be substituted.
1042///
Mike Stump1eb44332009-09-09 15:08:12 +00001043/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001044/// arguments.
1045///
Mike Stump1eb44332009-09-09 15:08:12 +00001046/// \param NumExplicitTemplateArguments the number of explicitly-specified
Douglas Gregor83314aa2009-07-08 20:55:45 +00001047/// template arguments in @p ExplicitTemplateArguments. This value may be zero.
1048///
Mike Stump1eb44332009-09-09 15:08:12 +00001049/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor83314aa2009-07-08 20:55:45 +00001050/// with the converted and checked explicit template arguments.
1051///
Mike Stump1eb44332009-09-09 15:08:12 +00001052/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor83314aa2009-07-08 20:55:45 +00001053/// parameters.
1054///
1055/// \param FunctionType if non-NULL, the result type of the function template
1056/// will also be instantiated and the pointed-to value will be updated with
1057/// the instantiated function type.
1058///
1059/// \param Info if substitution fails for any reason, this object will be
1060/// populated with more information about the failure.
1061///
1062/// \returns TDK_Success if substitution was successful, or some failure
1063/// condition.
1064Sema::TemplateDeductionResult
1065Sema::SubstituteExplicitTemplateArguments(
1066 FunctionTemplateDecl *FunctionTemplate,
1067 const TemplateArgument *ExplicitTemplateArgs,
1068 unsigned NumExplicitTemplateArgs,
1069 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1070 llvm::SmallVectorImpl<QualType> &ParamTypes,
1071 QualType *FunctionType,
1072 TemplateDeductionInfo &Info) {
1073 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1074 TemplateParameterList *TemplateParams
1075 = FunctionTemplate->getTemplateParameters();
1076
1077 if (NumExplicitTemplateArgs == 0) {
1078 // No arguments to substitute; just copy over the parameter types and
1079 // fill in the function type.
1080 for (FunctionDecl::param_iterator P = Function->param_begin(),
1081 PEnd = Function->param_end();
1082 P != PEnd;
1083 ++P)
1084 ParamTypes.push_back((*P)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Douglas Gregor83314aa2009-07-08 20:55:45 +00001086 if (FunctionType)
1087 *FunctionType = Function->getType();
1088 return TDK_Success;
1089 }
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Douglas Gregor83314aa2009-07-08 20:55:45 +00001091 // Substitution of the explicit template arguments into a function template
1092 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001093 SFINAETrap Trap(*this);
1094
Douglas Gregor83314aa2009-07-08 20:55:45 +00001095 // C++ [temp.arg.explicit]p3:
Mike Stump1eb44332009-09-09 15:08:12 +00001096 // Template arguments that are present shall be specified in the
1097 // declaration order of their corresponding template-parameters. The
Douglas Gregor83314aa2009-07-08 20:55:45 +00001098 // template argument list shall not specify more template-arguments than
Mike Stump1eb44332009-09-09 15:08:12 +00001099 // there are corresponding template-parameters.
1100 TemplateArgumentListBuilder Builder(TemplateParams,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001101 NumExplicitTemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001102
1103 // Enter a new template instantiation context where we check the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001104 // explicitly-specified template arguments against this function template,
1105 // and then substitute them into the function parameter types.
Mike Stump1eb44332009-09-09 15:08:12 +00001106 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001107 FunctionTemplate, Deduced.data(), Deduced.size(),
1108 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution);
1109 if (Inst)
1110 return TDK_InstantiationDepth;
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Douglas Gregor83314aa2009-07-08 20:55:45 +00001112 if (CheckTemplateArgumentList(FunctionTemplate,
1113 SourceLocation(), SourceLocation(),
1114 ExplicitTemplateArgs,
1115 NumExplicitTemplateArgs,
1116 SourceLocation(),
1117 true,
1118 Builder) || Trap.hasErrorOccurred())
1119 return TDK_InvalidExplicitArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Douglas Gregor83314aa2009-07-08 20:55:45 +00001121 // Form the template argument list from the explicitly-specified
1122 // template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001123 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor83314aa2009-07-08 20:55:45 +00001124 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1125 Info.reset(ExplicitArgumentList);
Mike Stump1eb44332009-09-09 15:08:12 +00001126
Douglas Gregor83314aa2009-07-08 20:55:45 +00001127 // Instantiate the types of each of the function parameters given the
1128 // explicitly-specified template arguments.
1129 for (FunctionDecl::param_iterator P = Function->param_begin(),
1130 PEnd = Function->param_end();
1131 P != PEnd;
1132 ++P) {
Mike Stump1eb44332009-09-09 15:08:12 +00001133 QualType ParamType
1134 = SubstType((*P)->getType(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001135 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1136 (*P)->getLocation(), (*P)->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001137 if (ParamType.isNull() || Trap.hasErrorOccurred())
1138 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Douglas Gregor83314aa2009-07-08 20:55:45 +00001140 ParamTypes.push_back(ParamType);
1141 }
1142
1143 // If the caller wants a full function type back, instantiate the return
1144 // type and form that function type.
1145 if (FunctionType) {
1146 // FIXME: exception-specifications?
Mike Stump1eb44332009-09-09 15:08:12 +00001147 const FunctionProtoType *Proto
Douglas Gregor83314aa2009-07-08 20:55:45 +00001148 = Function->getType()->getAsFunctionProtoType();
1149 assert(Proto && "Function template does not have a prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001150
1151 QualType ResultType
Douglas Gregor357bbd02009-08-28 20:50:45 +00001152 = SubstType(Proto->getResultType(),
1153 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1154 Function->getTypeSpecStartLoc(),
1155 Function->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001156 if (ResultType.isNull() || Trap.hasErrorOccurred())
1157 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001158
1159 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001160 ParamTypes.data(), ParamTypes.size(),
1161 Proto->isVariadic(),
1162 Proto->getTypeQuals(),
1163 Function->getLocation(),
1164 Function->getDeclName());
1165 if (FunctionType->isNull() || Trap.hasErrorOccurred())
1166 return TDK_SubstitutionFailure;
1167 }
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Douglas Gregor83314aa2009-07-08 20:55:45 +00001169 // C++ [temp.arg.explicit]p2:
Mike Stump1eb44332009-09-09 15:08:12 +00001170 // Trailing template arguments that can be deduced (14.8.2) may be
1171 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001172 // template arguments can be deduced, they may all be omitted; in this
1173 // case, the empty template argument list <> itself may also be omitted.
1174 //
1175 // Take all of the explicitly-specified arguments and put them into the
Mike Stump1eb44332009-09-09 15:08:12 +00001176 // set of deduced template arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001177 Deduced.reserve(TemplateParams->size());
1178 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001179 Deduced.push_back(ExplicitArgumentList->get(I));
1180
Douglas Gregor83314aa2009-07-08 20:55:45 +00001181 return TDK_Success;
1182}
1183
Mike Stump1eb44332009-09-09 15:08:12 +00001184/// \brief Finish template argument deduction for a function template,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001185/// checking the deduced template arguments for completeness and forming
1186/// the function template specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00001187Sema::TemplateDeductionResult
Douglas Gregor83314aa2009-07-08 20:55:45 +00001188Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
1189 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1190 FunctionDecl *&Specialization,
1191 TemplateDeductionInfo &Info) {
1192 TemplateParameterList *TemplateParams
1193 = FunctionTemplate->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Douglas Gregor83314aa2009-07-08 20:55:45 +00001195 // C++ [temp.deduct.type]p2:
1196 // [...] or if any template argument remains neither deduced nor
1197 // explicitly specified, template argument deduction fails.
1198 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
1199 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
1200 if (Deduced[I].isNull()) {
1201 Info.Param = makeTemplateParameter(
1202 const_cast<Decl *>(TemplateParams->getParam(I)));
1203 return TDK_Incomplete;
1204 }
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Douglas Gregor83314aa2009-07-08 20:55:45 +00001206 Builder.Append(Deduced[I]);
1207 }
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Douglas Gregor83314aa2009-07-08 20:55:45 +00001209 // Form the template argument list from the deduced template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001210 TemplateArgumentList *DeducedArgumentList
Douglas Gregor83314aa2009-07-08 20:55:45 +00001211 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1212 Info.reset(DeducedArgumentList);
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Douglas Gregor83314aa2009-07-08 20:55:45 +00001214 // Template argument deduction for function templates in a SFINAE context.
1215 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001216 SFINAETrap Trap(*this);
1217
Douglas Gregor83314aa2009-07-08 20:55:45 +00001218 // Enter a new template instantiation context while we instantiate the
1219 // actual function declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001220 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001221 FunctionTemplate, Deduced.data(), Deduced.size(),
1222 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
1223 if (Inst)
Mike Stump1eb44332009-09-09 15:08:12 +00001224 return TDK_InstantiationDepth;
1225
1226 // Substitute the deduced template arguments into the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001227 // declaration to produce the function template specialization.
1228 Specialization = cast_or_null<FunctionDecl>(
John McCallce3ff2b2009-08-25 22:02:44 +00001229 SubstDecl(FunctionTemplate->getTemplatedDecl(),
1230 FunctionTemplate->getDeclContext(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001231 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor83314aa2009-07-08 20:55:45 +00001232 if (!Specialization)
1233 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001234
1235 // If the template argument list is owned by the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001236 // specialization, release it.
1237 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList)
1238 Info.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Douglas Gregor83314aa2009-07-08 20:55:45 +00001240 // There may have been an error that did not prevent us from constructing a
1241 // declaration. Mark the declaration invalid and return with a substitution
1242 // failure.
1243 if (Trap.hasErrorOccurred()) {
1244 Specialization->setInvalidDecl(true);
1245 return TDK_SubstitutionFailure;
1246 }
Mike Stump1eb44332009-09-09 15:08:12 +00001247
1248 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001249}
1250
Douglas Gregore53060f2009-06-25 22:08:12 +00001251/// \brief Perform template argument deduction from a function call
1252/// (C++ [temp.deduct.call]).
1253///
1254/// \param FunctionTemplate the function template for which we are performing
1255/// template argument deduction.
1256///
Mike Stump1eb44332009-09-09 15:08:12 +00001257/// \param HasExplicitTemplateArgs whether any template arguments were
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001258/// explicitly specified.
1259///
1260/// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
1261/// the explicitly-specified template arguments.
1262///
1263/// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
Mike Stump1eb44332009-09-09 15:08:12 +00001264/// the number of explicitly-specified template arguments in
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001265/// @p ExplicitTemplateArguments. This value may be zero.
1266///
Douglas Gregore53060f2009-06-25 22:08:12 +00001267/// \param Args the function call arguments
1268///
1269/// \param NumArgs the number of arguments in Args
1270///
1271/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001272/// this will be set to the function template specialization produced by
Douglas Gregore53060f2009-06-25 22:08:12 +00001273/// template argument deduction.
1274///
1275/// \param Info the argument will be updated to provide additional information
1276/// about template argument deduction.
1277///
1278/// \returns the result of template argument deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001279Sema::TemplateDeductionResult
1280Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001281 bool HasExplicitTemplateArgs,
1282 const TemplateArgument *ExplicitTemplateArgs,
1283 unsigned NumExplicitTemplateArgs,
Douglas Gregore53060f2009-06-25 22:08:12 +00001284 Expr **Args, unsigned NumArgs,
1285 FunctionDecl *&Specialization,
1286 TemplateDeductionInfo &Info) {
1287 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001288
Douglas Gregore53060f2009-06-25 22:08:12 +00001289 // C++ [temp.deduct.call]p1:
1290 // Template argument deduction is done by comparing each function template
1291 // parameter type (call it P) with the type of the corresponding argument
1292 // of the call (call it A) as described below.
1293 unsigned CheckArgs = NumArgs;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001294 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregore53060f2009-06-25 22:08:12 +00001295 return TDK_TooFewArguments;
1296 else if (NumArgs > Function->getNumParams()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001297 const FunctionProtoType *Proto
Douglas Gregore53060f2009-06-25 22:08:12 +00001298 = Function->getType()->getAsFunctionProtoType();
1299 if (!Proto->isVariadic())
1300 return TDK_TooManyArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Douglas Gregore53060f2009-06-25 22:08:12 +00001302 CheckArgs = Function->getNumParams();
1303 }
Mike Stump1eb44332009-09-09 15:08:12 +00001304
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001305 // The types of the parameters from which we will perform template argument
1306 // deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001307 TemplateParameterList *TemplateParams
1308 = FunctionTemplate->getTemplateParameters();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001309 llvm::SmallVector<TemplateArgument, 4> Deduced;
1310 llvm::SmallVector<QualType, 4> ParamTypes;
1311 if (NumExplicitTemplateArgs) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001312 TemplateDeductionResult Result =
1313 SubstituteExplicitTemplateArguments(FunctionTemplate,
1314 ExplicitTemplateArgs,
1315 NumExplicitTemplateArgs,
1316 Deduced,
1317 ParamTypes,
1318 0,
1319 Info);
1320 if (Result)
1321 return Result;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001322 } else {
1323 // Just fill in the parameter types from the function declaration.
1324 for (unsigned I = 0; I != CheckArgs; ++I)
1325 ParamTypes.push_back(Function->getParamDecl(I)->getType());
1326 }
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001328 // Deduce template arguments from the function parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001329 Deduced.resize(TemplateParams->size());
Douglas Gregore53060f2009-06-25 22:08:12 +00001330 for (unsigned I = 0; I != CheckArgs; ++I) {
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001331 QualType ParamType = ParamTypes[I];
Douglas Gregore53060f2009-06-25 22:08:12 +00001332 QualType ArgType = Args[I]->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001333
Douglas Gregore53060f2009-06-25 22:08:12 +00001334 // C++ [temp.deduct.call]p2:
1335 // If P is not a reference type:
1336 QualType CanonParamType = Context.getCanonicalType(ParamType);
Douglas Gregor500d3312009-06-26 18:27:22 +00001337 bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1338 if (!ParamWasReference) {
Mike Stump1eb44332009-09-09 15:08:12 +00001339 // - If A is an array type, the pointer type produced by the
1340 // array-to-pointer standard conversion (4.2) is used in place of
Douglas Gregore53060f2009-06-25 22:08:12 +00001341 // A for type deduction; otherwise,
1342 if (ArgType->isArrayType())
1343 ArgType = Context.getArrayDecayedType(ArgType);
Mike Stump1eb44332009-09-09 15:08:12 +00001344 // - If A is a function type, the pointer type produced by the
1345 // function-to-pointer standard conversion (4.3) is used in place
Douglas Gregore53060f2009-06-25 22:08:12 +00001346 // of A for type deduction; otherwise,
1347 else if (ArgType->isFunctionType())
1348 ArgType = Context.getPointerType(ArgType);
1349 else {
1350 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1351 // type are ignored for type deduction.
1352 QualType CanonArgType = Context.getCanonicalType(ArgType);
1353 if (CanonArgType.getCVRQualifiers())
1354 ArgType = CanonArgType.getUnqualifiedType();
1355 }
1356 }
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Douglas Gregore53060f2009-06-25 22:08:12 +00001358 // C++0x [temp.deduct.call]p3:
1359 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
Mike Stump1eb44332009-09-09 15:08:12 +00001360 // are ignored for type deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001361 if (CanonParamType.getCVRQualifiers())
1362 ParamType = CanonParamType.getUnqualifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001363 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001364 // [...] If P is a reference type, the type referred to by P is used
1365 // for type deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001366 ParamType = ParamRefType->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00001367
1368 // [...] If P is of the form T&&, where T is a template parameter, and
1369 // the argument is an lvalue, the type A& is used in place of A for
Douglas Gregore53060f2009-06-25 22:08:12 +00001370 // type deduction.
1371 if (isa<RValueReferenceType>(ParamRefType) &&
1372 ParamRefType->getAsTemplateTypeParmType() &&
1373 Args[I]->isLvalue(Context) == Expr::LV_Valid)
1374 ArgType = Context.getLValueReferenceType(ArgType);
1375 }
Mike Stump1eb44332009-09-09 15:08:12 +00001376
Douglas Gregore53060f2009-06-25 22:08:12 +00001377 // C++0x [temp.deduct.call]p4:
1378 // In general, the deduction process attempts to find template argument
1379 // values that will make the deduced A identical to A (after the type A
1380 // is transformed as described above). [...]
Douglas Gregor12820292009-09-14 20:00:47 +00001381 unsigned TDF = TDF_SkipNonDependent;
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Douglas Gregor508f1c82009-06-26 23:10:12 +00001383 // - If the original P is a reference type, the deduced A (i.e., the
1384 // type referred to by the reference) can be more cv-qualified than
1385 // the transformed A.
1386 if (ParamWasReference)
1387 TDF |= TDF_ParamWithReferenceType;
Mike Stump1eb44332009-09-09 15:08:12 +00001388 // - The transformed A can be another pointer or pointer to member
1389 // type that can be converted to the deduced A via a qualification
Douglas Gregor508f1c82009-06-26 23:10:12 +00001390 // conversion (4.4).
1391 if (ArgType->isPointerType() || ArgType->isMemberPointerType())
1392 TDF |= TDF_IgnoreQualifiers;
Mike Stump1eb44332009-09-09 15:08:12 +00001393 // - If P is a class and P has the form simple-template-id, then the
Douglas Gregor41128772009-06-26 23:27:24 +00001394 // transformed A can be a derived class of the deduced A. Likewise,
1395 // if P is a pointer to a class of the form simple-template-id, the
1396 // transformed A can be a pointer to a derived class pointed to by
1397 // the deduced A.
1398 if (isSimpleTemplateIdType(ParamType) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001399 (isa<PointerType>(ParamType) &&
Douglas Gregor41128772009-06-26 23:27:24 +00001400 isSimpleTemplateIdType(
Ted Kremenek6217b802009-07-29 21:53:49 +00001401 ParamType->getAs<PointerType>()->getPointeeType())))
Douglas Gregor41128772009-06-26 23:27:24 +00001402 TDF |= TDF_DerivedClass;
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Douglas Gregore53060f2009-06-25 22:08:12 +00001404 if (TemplateDeductionResult Result
1405 = ::DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor500d3312009-06-26 18:27:22 +00001406 ParamType, ArgType, Info, Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +00001407 TDF))
Douglas Gregore53060f2009-06-25 22:08:12 +00001408 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Douglas Gregor8fdc3c42009-07-07 23:12:18 +00001410 // FIXME: C++0x [temp.deduct.call] paragraphs 6-9 deal with function
Mike Stump1eb44332009-09-09 15:08:12 +00001411 // pointer parameters.
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001412
1413 // FIXME: we need to check that the deduced A is the same as A,
1414 // modulo the various allowed differences.
Douglas Gregore53060f2009-06-25 22:08:12 +00001415 }
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001416
Mike Stump1eb44332009-09-09 15:08:12 +00001417 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001418 Specialization, Info);
Douglas Gregore53060f2009-06-25 22:08:12 +00001419}
1420
Douglas Gregor83314aa2009-07-08 20:55:45 +00001421/// \brief Deduce template arguments when taking the address of a function
1422/// template (C++ [temp.deduct.funcaddr]).
1423///
1424/// \param FunctionTemplate the function template for which we are performing
1425/// template argument deduction.
1426///
Mike Stump1eb44332009-09-09 15:08:12 +00001427/// \param HasExplicitTemplateArgs whether any template arguments were
Douglas Gregor83314aa2009-07-08 20:55:45 +00001428/// explicitly specified.
1429///
1430/// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
1431/// the explicitly-specified template arguments.
1432///
1433/// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
Mike Stump1eb44332009-09-09 15:08:12 +00001434/// the number of explicitly-specified template arguments in
Douglas Gregor83314aa2009-07-08 20:55:45 +00001435/// @p ExplicitTemplateArguments. This value may be zero.
1436///
1437/// \param ArgFunctionType the function type that will be used as the
1438/// "argument" type (A) when performing template argument deduction from the
1439/// function template's function type.
1440///
1441/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001442/// this will be set to the function template specialization produced by
Douglas Gregor83314aa2009-07-08 20:55:45 +00001443/// template argument deduction.
1444///
1445/// \param Info the argument will be updated to provide additional information
1446/// about template argument deduction.
1447///
1448/// \returns the result of template argument deduction.
1449Sema::TemplateDeductionResult
1450Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1451 bool HasExplicitTemplateArgs,
1452 const TemplateArgument *ExplicitTemplateArgs,
1453 unsigned NumExplicitTemplateArgs,
1454 QualType ArgFunctionType,
1455 FunctionDecl *&Specialization,
1456 TemplateDeductionInfo &Info) {
1457 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1458 TemplateParameterList *TemplateParams
1459 = FunctionTemplate->getTemplateParameters();
1460 QualType FunctionType = Function->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001461
Douglas Gregor83314aa2009-07-08 20:55:45 +00001462 // Substitute any explicit template arguments.
1463 llvm::SmallVector<TemplateArgument, 4> Deduced;
1464 llvm::SmallVector<QualType, 4> ParamTypes;
1465 if (HasExplicitTemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001466 if (TemplateDeductionResult Result
1467 = SubstituteExplicitTemplateArguments(FunctionTemplate,
1468 ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001469 NumExplicitTemplateArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00001470 Deduced, ParamTypes,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001471 &FunctionType, Info))
1472 return Result;
1473 }
1474
1475 // Template argument deduction for function templates in a SFINAE context.
1476 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001477 SFINAETrap Trap(*this);
1478
Douglas Gregor83314aa2009-07-08 20:55:45 +00001479 // Deduce template arguments from the function type.
Mike Stump1eb44332009-09-09 15:08:12 +00001480 Deduced.resize(TemplateParams->size());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001481 if (TemplateDeductionResult Result
1482 = ::DeduceTemplateArguments(Context, TemplateParams,
Mike Stump1eb44332009-09-09 15:08:12 +00001483 FunctionType, ArgFunctionType, Info,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001484 Deduced, 0))
1485 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001486
1487 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001488 Specialization, Info);
1489}
1490
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001491/// \brief Deduce template arguments for a templated conversion
1492/// function (C++ [temp.deduct.conv]) and, if successful, produce a
1493/// conversion function template specialization.
1494Sema::TemplateDeductionResult
1495Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1496 QualType ToType,
1497 CXXConversionDecl *&Specialization,
1498 TemplateDeductionInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00001499 CXXConversionDecl *Conv
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001500 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
1501 QualType FromType = Conv->getConversionType();
1502
1503 // Canonicalize the types for deduction.
1504 QualType P = Context.getCanonicalType(FromType);
1505 QualType A = Context.getCanonicalType(ToType);
1506
1507 // C++0x [temp.deduct.conv]p3:
1508 // If P is a reference type, the type referred to by P is used for
1509 // type deduction.
1510 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
1511 P = PRef->getPointeeType();
1512
1513 // C++0x [temp.deduct.conv]p3:
1514 // If A is a reference type, the type referred to by A is used
1515 // for type deduction.
1516 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
1517 A = ARef->getPointeeType();
1518 // C++ [temp.deduct.conv]p2:
1519 //
Mike Stump1eb44332009-09-09 15:08:12 +00001520 // If A is not a reference type:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001521 else {
1522 assert(!A->isReferenceType() && "Reference types were handled above");
1523
1524 // - If P is an array type, the pointer type produced by the
Mike Stump1eb44332009-09-09 15:08:12 +00001525 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001526 // of P for type deduction; otherwise,
1527 if (P->isArrayType())
1528 P = Context.getArrayDecayedType(P);
1529 // - If P is a function type, the pointer type produced by the
1530 // function-to-pointer standard conversion (4.3) is used in
1531 // place of P for type deduction; otherwise,
1532 else if (P->isFunctionType())
1533 P = Context.getPointerType(P);
1534 // - If P is a cv-qualified type, the top level cv-qualifiers of
1535 // P’s type are ignored for type deduction.
1536 else
1537 P = P.getUnqualifiedType();
1538
1539 // C++0x [temp.deduct.conv]p3:
1540 // If A is a cv-qualified type, the top level cv-qualifiers of A’s
1541 // type are ignored for type deduction.
1542 A = A.getUnqualifiedType();
1543 }
1544
1545 // Template argument deduction for function templates in a SFINAE context.
1546 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001547 SFINAETrap Trap(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001548
1549 // C++ [temp.deduct.conv]p1:
1550 // Template argument deduction is done by comparing the return
1551 // type of the template conversion function (call it P) with the
1552 // type that is required as the result of the conversion (call it
1553 // A) as described in 14.8.2.4.
1554 TemplateParameterList *TemplateParams
1555 = FunctionTemplate->getTemplateParameters();
1556 llvm::SmallVector<TemplateArgument, 4> Deduced;
Mike Stump1eb44332009-09-09 15:08:12 +00001557 Deduced.resize(TemplateParams->size());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001558
1559 // C++0x [temp.deduct.conv]p4:
1560 // In general, the deduction process attempts to find template
1561 // argument values that will make the deduced A identical to
1562 // A. However, there are two cases that allow a difference:
1563 unsigned TDF = 0;
1564 // - If the original A is a reference type, A can be more
1565 // cv-qualified than the deduced A (i.e., the type referred to
1566 // by the reference)
1567 if (ToType->isReferenceType())
1568 TDF |= TDF_ParamWithReferenceType;
1569 // - The deduced A can be another pointer or pointer to member
1570 // type that can be converted to A via a qualification
1571 // conversion.
1572 //
1573 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
1574 // both P and A are pointers or member pointers. In this case, we
1575 // just ignore cv-qualifiers completely).
1576 if ((P->isPointerType() && A->isPointerType()) ||
1577 (P->isMemberPointerType() && P->isMemberPointerType()))
1578 TDF |= TDF_IgnoreQualifiers;
1579 if (TemplateDeductionResult Result
1580 = ::DeduceTemplateArguments(Context, TemplateParams,
1581 P, A, Info, Deduced, TDF))
1582 return Result;
1583
1584 // FIXME: we need to check that the deduced A is the same as A,
1585 // modulo the various allowed differences.
Mike Stump1eb44332009-09-09 15:08:12 +00001586
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001587 // Finish template argument deduction.
1588 FunctionDecl *Spec = 0;
1589 TemplateDeductionResult Result
1590 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, Spec, Info);
1591 Specialization = cast_or_null<CXXConversionDecl>(Spec);
1592 return Result;
1593}
1594
Douglas Gregor8a514912009-09-14 18:39:43 +00001595/// \brief Stores the result of comparing the qualifiers of two types.
1596enum DeductionQualifierComparison {
1597 NeitherMoreQualified = 0,
1598 ParamMoreQualified,
1599 ArgMoreQualified
1600};
1601
1602/// \brief Deduce the template arguments during partial ordering by comparing
1603/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
1604///
1605/// \param Context the AST context in which this deduction occurs.
1606///
1607/// \param TemplateParams the template parameters that we are deducing
1608///
1609/// \param ParamIn the parameter type
1610///
1611/// \param ArgIn the argument type
1612///
1613/// \param Info information about the template argument deduction itself
1614///
1615/// \param Deduced the deduced template arguments
1616///
1617/// \returns the result of template argument deduction so far. Note that a
1618/// "success" result means that template argument deduction has not yet failed,
1619/// but it may still fail, later, for other reasons.
1620static Sema::TemplateDeductionResult
1621DeduceTemplateArgumentsDuringPartialOrdering(ASTContext &Context,
1622 TemplateParameterList *TemplateParams,
1623 QualType ParamIn, QualType ArgIn,
1624 Sema::TemplateDeductionInfo &Info,
1625 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1626 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1627 CanQualType Param = Context.getCanonicalType(ParamIn);
1628 CanQualType Arg = Context.getCanonicalType(ArgIn);
1629
1630 // C++0x [temp.deduct.partial]p5:
1631 // Before the partial ordering is done, certain transformations are
1632 // performed on the types used for partial ordering:
1633 // - If P is a reference type, P is replaced by the type referred to.
1634 CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
1635 if (ParamRef)
1636 Param = ParamRef->getPointeeType();
1637
1638 // - If A is a reference type, A is replaced by the type referred to.
1639 CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
1640 if (ArgRef)
1641 Arg = ArgRef->getPointeeType();
1642
1643 if (QualifierComparisons && ParamRef && ArgRef) {
1644 // C++0x [temp.deduct.partial]p6:
1645 // If both P and A were reference types (before being replaced with the
1646 // type referred to above), determine which of the two types (if any) is
1647 // more cv-qualified than the other; otherwise the types are considered to
1648 // be equally cv-qualified for partial ordering purposes. The result of this
1649 // determination will be used below.
1650 //
1651 // We save this information for later, using it only when deduction
1652 // succeeds in both directions.
1653 DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
1654 if (Param.isMoreQualifiedThan(Arg))
1655 QualifierResult = ParamMoreQualified;
1656 else if (Arg.isMoreQualifiedThan(Param))
1657 QualifierResult = ArgMoreQualified;
1658 QualifierComparisons->push_back(QualifierResult);
1659 }
1660
1661 // C++0x [temp.deduct.partial]p7:
1662 // Remove any top-level cv-qualifiers:
1663 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
1664 // version of P.
1665 Param = Param.getUnqualifiedType();
1666 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
1667 // version of A.
1668 Arg = Arg.getUnqualifiedType();
1669
1670 // C++0x [temp.deduct.partial]p8:
1671 // Using the resulting types P and A the deduction is then done as
1672 // described in 14.9.2.5. If deduction succeeds for a given type, the type
1673 // from the argument template is considered to be at least as specialized
1674 // as the type from the parameter template.
1675 return DeduceTemplateArguments(Context, TemplateParams, Param, Arg, Info,
1676 Deduced, TDF_None);
1677}
1678
1679static void
1680MarkDeducedTemplateParameters(Sema &SemaRef, QualType T,
1681 llvm::SmallVectorImpl<bool> &Deduced);
1682
1683/// \brief Determine whether the function template \p FT1 is at least as
1684/// specialized as \p FT2.
1685static bool isAtLeastAsSpecializedAs(Sema &S,
1686 FunctionTemplateDecl *FT1,
1687 FunctionTemplateDecl *FT2,
1688 TemplatePartialOrderingContext TPOC,
1689 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1690 FunctionDecl *FD1 = FT1->getTemplatedDecl();
1691 FunctionDecl *FD2 = FT2->getTemplatedDecl();
1692 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
1693 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
1694
1695 assert(Proto1 && Proto2 && "Function templates must have prototypes");
1696 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
1697 llvm::SmallVector<TemplateArgument, 4> Deduced;
1698 Deduced.resize(TemplateParams->size());
1699
1700 // C++0x [temp.deduct.partial]p3:
1701 // The types used to determine the ordering depend on the context in which
1702 // the partial ordering is done:
1703 Sema::TemplateDeductionInfo Info(S.Context);
1704 switch (TPOC) {
1705 case TPOC_Call: {
1706 // - In the context of a function call, the function parameter types are
1707 // used.
1708 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
1709 for (unsigned I = 0; I != NumParams; ++I)
1710 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1711 TemplateParams,
1712 Proto2->getArgType(I),
1713 Proto1->getArgType(I),
1714 Info,
1715 Deduced,
1716 QualifierComparisons))
1717 return false;
1718
1719 break;
1720 }
1721
1722 case TPOC_Conversion:
1723 // - In the context of a call to a conversion operator, the return types
1724 // of the conversion function templates are used.
1725 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1726 TemplateParams,
1727 Proto2->getResultType(),
1728 Proto1->getResultType(),
1729 Info,
1730 Deduced,
1731 QualifierComparisons))
1732 return false;
1733 break;
1734
1735 case TPOC_Other:
1736 // - In other contexts (14.6.6.2) the function template’s function type
1737 // is used.
1738 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1739 TemplateParams,
1740 FD2->getType(),
1741 FD1->getType(),
1742 Info,
1743 Deduced,
1744 QualifierComparisons))
1745 return false;
1746 break;
1747 }
1748
1749 // C++0x [temp.deduct.partial]p11:
1750 // In most cases, all template parameters must have values in order for
1751 // deduction to succeed, but for partial ordering purposes a template
1752 // parameter may remain without a value provided it is not used in the
1753 // types being used for partial ordering. [ Note: a template parameter used
1754 // in a non-deduced context is considered used. -end note]
1755 unsigned ArgIdx = 0, NumArgs = Deduced.size();
1756 for (; ArgIdx != NumArgs; ++ArgIdx)
1757 if (Deduced[ArgIdx].isNull())
1758 break;
1759
1760 if (ArgIdx == NumArgs) {
1761 // All template arguments were deduced. FT1 is at least as specialized
1762 // as FT2.
1763 return true;
1764 }
1765
1766 // FIXME: MarkDeducedTemplateParameters needs to become
1767 // MarkUsedTemplateParameters with a flag that tells us whether to mark
1768 // template parameters that are used in non-deduced contexts.
1769 llvm::SmallVector<bool, 4> UsedParameters;
1770 UsedParameters.resize(TemplateParams->size());
1771 switch (TPOC) {
1772 case TPOC_Call: {
1773 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
1774 for (unsigned I = 0; I != NumParams; ++I)
1775 ::MarkDeducedTemplateParameters(S, Proto2->getArgType(I), UsedParameters);
1776 break;
1777 }
1778
1779 case TPOC_Conversion:
1780 ::MarkDeducedTemplateParameters(S, Proto2->getResultType(), UsedParameters);
1781 break;
1782
1783 case TPOC_Other:
1784 ::MarkDeducedTemplateParameters(S, FD2->getType(), UsedParameters);
1785 break;
1786 }
1787
1788 for (; ArgIdx != NumArgs; ++ArgIdx)
1789 // If this argument had no value deduced but was used in one of the types
1790 // used for partial ordering, then deduction fails.
1791 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
1792 return false;
1793
1794 return true;
1795}
1796
1797
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001798/// \brief Returns the more specialization function template according
1799/// to the rules of function template partial ordering (C++ [temp.func.order]).
1800///
1801/// \param FT1 the first function template
1802///
1803/// \param FT2 the second function template
1804///
Douglas Gregor8a514912009-09-14 18:39:43 +00001805/// \param TPOC the context in which we are performing partial ordering of
1806/// function templates.
Mike Stump1eb44332009-09-09 15:08:12 +00001807///
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001808/// \returns the more specialization function template. If neither
1809/// template is more specialized, returns NULL.
1810FunctionTemplateDecl *
1811Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
1812 FunctionTemplateDecl *FT2,
Douglas Gregor8a514912009-09-14 18:39:43 +00001813 TemplatePartialOrderingContext TPOC) {
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001814 // FIXME: Implement this
Douglas Gregor8a514912009-09-14 18:39:43 +00001815 llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
1816 bool Better1 = isAtLeastAsSpecializedAs(*this, FT1, FT2, TPOC, 0);
1817 bool Better2 = isAtLeastAsSpecializedAs(*this, FT2, FT1, TPOC,
1818 &QualifierComparisons);
1819
1820 if (Better1 != Better2) // We have a clear winner
1821 return Better1? FT1 : FT2;
1822
1823 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001824 return 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00001825
1826
1827 // C++0x [temp.deduct.partial]p10:
1828 // If for each type being considered a given template is at least as
1829 // specialized for all types and more specialized for some set of types and
1830 // the other template is not more specialized for any types or is not at
1831 // least as specialized for any types, then the given template is more
1832 // specialized than the other template. Otherwise, neither template is more
1833 // specialized than the other.
1834 Better1 = false;
1835 Better2 = false;
1836 for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
1837 // C++0x [temp.deduct.partial]p9:
1838 // If, for a given type, deduction succeeds in both directions (i.e., the
1839 // types are identical after the transformations above) and if the type
1840 // from the argument template is more cv-qualified than the type from the
1841 // parameter template (as described above) that type is considered to be
1842 // more specialized than the other. If neither type is more cv-qualified
1843 // than the other then neither type is more specialized than the other.
1844 switch (QualifierComparisons[I]) {
1845 case NeitherMoreQualified:
1846 break;
1847
1848 case ParamMoreQualified:
1849 Better1 = true;
1850 if (Better2)
1851 return 0;
1852 break;
1853
1854 case ArgMoreQualified:
1855 Better2 = true;
1856 if (Better1)
1857 return 0;
1858 break;
1859 }
1860 }
1861
1862 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001863 if (Better1)
1864 return FT1;
Douglas Gregor8a514912009-09-14 18:39:43 +00001865 else if (Better2)
1866 return FT2;
1867 else
1868 return 0;
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001869}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001870
Mike Stump1eb44332009-09-09 15:08:12 +00001871static void
Douglas Gregor031a5882009-06-13 00:26:55 +00001872MarkDeducedTemplateParameters(Sema &SemaRef,
1873 const TemplateArgument &TemplateArg,
1874 llvm::SmallVectorImpl<bool> &Deduced);
1875
1876/// \brief Mark the template arguments that are deduced by the given
1877/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001878static void
1879MarkDeducedTemplateParameters(const Expr *E,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001880 llvm::SmallVectorImpl<bool> &Deduced) {
1881 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregor031a5882009-06-13 00:26:55 +00001882 if (!E)
1883 return;
1884
Mike Stump1eb44332009-09-09 15:08:12 +00001885 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00001886 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
1887 if (!NTTP)
1888 return;
1889
1890 Deduced[NTTP->getIndex()] = true;
1891}
1892
1893/// \brief Mark the template parameters that are deduced by the given
1894/// type.
Mike Stump1eb44332009-09-09 15:08:12 +00001895static void
Douglas Gregor031a5882009-06-13 00:26:55 +00001896MarkDeducedTemplateParameters(Sema &SemaRef, QualType T,
1897 llvm::SmallVectorImpl<bool> &Deduced) {
1898 // Non-dependent types have nothing deducible
1899 if (!T->isDependentType())
1900 return;
1901
1902 T = SemaRef.Context.getCanonicalType(T);
1903 switch (T->getTypeClass()) {
1904 case Type::ExtQual:
Mike Stump1eb44332009-09-09 15:08:12 +00001905 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001906 QualType(cast<ExtQualType>(T)->getBaseType(), 0),
Douglas Gregor031a5882009-06-13 00:26:55 +00001907 Deduced);
1908 break;
1909
1910 case Type::Pointer:
1911 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001912 cast<PointerType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001913 Deduced);
1914 break;
1915
1916 case Type::BlockPointer:
1917 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001918 cast<BlockPointerType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001919 Deduced);
1920 break;
1921
1922 case Type::LValueReference:
1923 case Type::RValueReference:
1924 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001925 cast<ReferenceType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001926 Deduced);
1927 break;
1928
1929 case Type::MemberPointer: {
1930 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
1931 MarkDeducedTemplateParameters(SemaRef, MemPtr->getPointeeType(), Deduced);
1932 MarkDeducedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
1933 Deduced);
1934 break;
1935 }
1936
1937 case Type::DependentSizedArray:
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001938 MarkDeducedTemplateParameters(cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001939 Deduced);
1940 // Fall through to check the element type
1941
1942 case Type::ConstantArray:
1943 case Type::IncompleteArray:
1944 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001945 cast<ArrayType>(T)->getElementType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001946 Deduced);
1947 break;
1948
1949 case Type::Vector:
1950 case Type::ExtVector:
1951 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001952 cast<VectorType>(T)->getElementType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001953 Deduced);
1954 break;
1955
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001956 case Type::DependentSizedExtVector: {
1957 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001958 = cast<DependentSizedExtVectorType>(T);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001959 MarkDeducedTemplateParameters(SemaRef, VecType->getElementType(), Deduced);
1960 MarkDeducedTemplateParameters(VecType->getSizeExpr(), Deduced);
1961 break;
1962 }
1963
Douglas Gregor031a5882009-06-13 00:26:55 +00001964 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001965 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregor031a5882009-06-13 00:26:55 +00001966 MarkDeducedTemplateParameters(SemaRef, Proto->getResultType(), Deduced);
1967 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
1968 MarkDeducedTemplateParameters(SemaRef, Proto->getArgType(I), Deduced);
1969 break;
1970 }
1971
1972 case Type::TemplateTypeParm:
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001973 Deduced[cast<TemplateTypeParmType>(T)->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00001974 break;
1975
1976 case Type::TemplateSpecialization: {
Mike Stump1eb44332009-09-09 15:08:12 +00001977 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001978 = cast<TemplateSpecializationType>(T);
Douglas Gregor031a5882009-06-13 00:26:55 +00001979 if (TemplateDecl *Template = Spec->getTemplateName().getAsTemplateDecl())
Mike Stump1eb44332009-09-09 15:08:12 +00001980 if (TemplateTemplateParmDecl *TTP
Douglas Gregor031a5882009-06-13 00:26:55 +00001981 = dyn_cast<TemplateTemplateParmDecl>(Template))
1982 Deduced[TTP->getIndex()] = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001983
Douglas Gregor031a5882009-06-13 00:26:55 +00001984 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
1985 MarkDeducedTemplateParameters(SemaRef, Spec->getArg(I), Deduced);
1986
1987 break;
1988 }
1989
1990 // None of these types have any deducible parts.
1991 case Type::Builtin:
1992 case Type::FixedWidthInt:
1993 case Type::Complex:
1994 case Type::VariableArray:
1995 case Type::FunctionNoProto:
1996 case Type::Record:
1997 case Type::Enum:
1998 case Type::Typename:
1999 case Type::ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002000 case Type::ObjCObjectPointer:
Douglas Gregor031a5882009-06-13 00:26:55 +00002001#define TYPE(Class, Base)
2002#define ABSTRACT_TYPE(Class, Base)
2003#define DEPENDENT_TYPE(Class, Base)
2004#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2005#include "clang/AST/TypeNodes.def"
2006 break;
2007 }
2008}
2009
2010/// \brief Mark the template parameters that are deduced by this
2011/// template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00002012static void
Douglas Gregor031a5882009-06-13 00:26:55 +00002013MarkDeducedTemplateParameters(Sema &SemaRef,
2014 const TemplateArgument &TemplateArg,
2015 llvm::SmallVectorImpl<bool> &Deduced) {
2016 switch (TemplateArg.getKind()) {
2017 case TemplateArgument::Null:
2018 case TemplateArgument::Integral:
2019 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002020
Douglas Gregor031a5882009-06-13 00:26:55 +00002021 case TemplateArgument::Type:
2022 MarkDeducedTemplateParameters(SemaRef, TemplateArg.getAsType(), Deduced);
2023 break;
2024
2025 case TemplateArgument::Declaration:
Mike Stump1eb44332009-09-09 15:08:12 +00002026 if (TemplateTemplateParmDecl *TTP
Douglas Gregor031a5882009-06-13 00:26:55 +00002027 = dyn_cast<TemplateTemplateParmDecl>(TemplateArg.getAsDecl()))
2028 Deduced[TTP->getIndex()] = true;
2029 break;
2030
2031 case TemplateArgument::Expression:
2032 MarkDeducedTemplateParameters(TemplateArg.getAsExpr(), Deduced);
2033 break;
Anders Carlssond01b1da2009-06-15 17:04:53 +00002034 case TemplateArgument::Pack:
2035 assert(0 && "FIXME: Implement!");
2036 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00002037 }
2038}
2039
2040/// \brief Mark the template parameters can be deduced by the given
2041/// template argument list.
2042///
2043/// \param TemplateArgs the template argument list from which template
2044/// parameters will be deduced.
2045///
2046/// \param Deduced a bit vector whose elements will be set to \c true
2047/// to indicate when the corresponding template parameter will be
2048/// deduced.
Mike Stump1eb44332009-09-09 15:08:12 +00002049void
Douglas Gregor031a5882009-06-13 00:26:55 +00002050Sema::MarkDeducedTemplateParameters(const TemplateArgumentList &TemplateArgs,
2051 llvm::SmallVectorImpl<bool> &Deduced) {
2052 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
2053 ::MarkDeducedTemplateParameters(*this, TemplateArgs[I], Deduced);
2054}