blob: 810de048c2133a1a12e414f4d8a7e43ca956fdcb [file] [log] [blame]
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001//===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template argument deduction.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/StmtVisitor.h"
17#include "clang/AST/Expr.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/Parse/DeclSpec.h"
20#include "llvm/Support/Compiler.h"
Douglas Gregor508f1c82009-06-26 23:10:12 +000021
22namespace clang {
23 /// \brief Various flags that control template argument deduction.
24 ///
25 /// These flags can be bitwise-OR'd together.
26 enum TemplateDeductionFlags {
27 /// \brief No template argument deduction flags, which indicates the
28 /// strictest results for template argument deduction (as used for, e.g.,
29 /// matching class template partial specializations).
30 TDF_None = 0,
31 /// \brief Within template argument deduction from a function call, we are
32 /// matching with a parameter type for which the original parameter was
33 /// a reference.
34 TDF_ParamWithReferenceType = 0x1,
35 /// \brief Within template argument deduction from a function call, we
36 /// are matching in a case where we ignore cv-qualifiers.
37 TDF_IgnoreQualifiers = 0x02,
38 /// \brief Within template argument deduction from a function call,
39 /// we are matching in a case where we can perform template argument
Douglas Gregor41128772009-06-26 23:27:24 +000040 /// deduction from a template-id of a derived class of the argument type.
Douglas Gregor508f1c82009-06-26 23:10:12 +000041 TDF_DerivedClass = 0x04
42 };
43}
44
Douglas Gregor0b9247f2009-06-04 00:03:07 +000045using namespace clang;
46
Douglas Gregorf67875d2009-06-12 18:26:56 +000047static Sema::TemplateDeductionResult
48DeduceTemplateArguments(ASTContext &Context,
49 TemplateParameterList *TemplateParams,
50 const TemplateArgument &Param,
Douglas Gregord708c722009-06-09 16:35:58 +000051 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +000052 Sema::TemplateDeductionInfo &Info,
Douglas Gregord708c722009-06-09 16:35:58 +000053 llvm::SmallVectorImpl<TemplateArgument> &Deduced);
54
Douglas Gregor199d9912009-06-05 00:53:49 +000055/// \brief If the given expression is of a form that permits the deduction
56/// of a non-type template parameter, return the declaration of that
57/// non-type template parameter.
58static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
59 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
60 E = IC->getSubExpr();
61
62 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
63 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
64
65 return 0;
66}
67
68/// \brief Deduce the value of the given non-type template parameter
69/// from the given constant.
Douglas Gregorf67875d2009-06-12 18:26:56 +000070static Sema::TemplateDeductionResult
71DeduceNonTypeTemplateArgument(ASTContext &Context,
72 NonTypeTemplateParmDecl *NTTP,
Anders Carlsson335e24a2009-06-16 22:44:31 +000073 llvm::APSInt Value,
Douglas Gregorf67875d2009-06-12 18:26:56 +000074 Sema::TemplateDeductionInfo &Info,
75 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor199d9912009-06-05 00:53:49 +000076 assert(NTTP->getDepth() == 0 &&
77 "Cannot deduce non-type template argument with depth > 0");
78
79 if (Deduced[NTTP->getIndex()].isNull()) {
Anders Carlsson25af1ed2009-06-16 23:08:29 +000080 QualType T = NTTP->getType();
81
82 // FIXME: Make sure we didn't overflow our data type!
83 unsigned AllowedBits = Context.getTypeSize(T);
84 if (Value.getBitWidth() != AllowedBits)
85 Value.extOrTrunc(AllowedBits);
86 Value.setIsSigned(T->isSignedIntegerType());
87
88 Deduced[NTTP->getIndex()] = TemplateArgument(SourceLocation(), Value, T);
Douglas Gregorf67875d2009-06-12 18:26:56 +000089 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +000090 }
91
Douglas Gregorf67875d2009-06-12 18:26:56 +000092 assert(Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral);
Douglas Gregor199d9912009-06-05 00:53:49 +000093
94 // If the template argument was previously deduced to a negative value,
95 // then our deduction fails.
96 const llvm::APSInt *PrevValuePtr = Deduced[NTTP->getIndex()].getAsIntegral();
Anders Carlsson335e24a2009-06-16 22:44:31 +000097 if (PrevValuePtr->isNegative()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +000098 Info.Param = NTTP;
99 Info.FirstArg = Deduced[NTTP->getIndex()];
Anders Carlsson335e24a2009-06-16 22:44:31 +0000100 Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000101 return Sema::TDK_Inconsistent;
102 }
103
Anders Carlsson335e24a2009-06-16 22:44:31 +0000104 llvm::APSInt PrevValue = *PrevValuePtr;
Douglas Gregor199d9912009-06-05 00:53:49 +0000105 if (Value.getBitWidth() > PrevValue.getBitWidth())
106 PrevValue.zext(Value.getBitWidth());
107 else if (Value.getBitWidth() < PrevValue.getBitWidth())
108 Value.zext(PrevValue.getBitWidth());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000109
110 if (Value != PrevValue) {
111 Info.Param = NTTP;
112 Info.FirstArg = Deduced[NTTP->getIndex()];
Anders Carlsson335e24a2009-06-16 22:44:31 +0000113 Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000114 return Sema::TDK_Inconsistent;
115 }
116
117 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000118}
119
120/// \brief Deduce the value of the given non-type template parameter
121/// from the given type- or value-dependent expression.
122///
123/// \returns true if deduction succeeded, false otherwise.
124
Douglas Gregorf67875d2009-06-12 18:26:56 +0000125static Sema::TemplateDeductionResult
126DeduceNonTypeTemplateArgument(ASTContext &Context,
127 NonTypeTemplateParmDecl *NTTP,
128 Expr *Value,
129 Sema::TemplateDeductionInfo &Info,
130 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000131 assert(NTTP->getDepth() == 0 &&
132 "Cannot deduce non-type template argument with depth > 0");
133 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
134 "Expression template argument must be type- or value-dependent.");
135
136 if (Deduced[NTTP->getIndex()].isNull()) {
137 // FIXME: Clone the Value?
138 Deduced[NTTP->getIndex()] = TemplateArgument(Value);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000139 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000140 }
141
142 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
143 // Okay, we deduced a constant in one case and a dependent expression
144 // in another case. FIXME: Later, we will check that instantiating the
145 // dependent expression gives us the constant value.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000146 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000147 }
148
149 // FIXME: Compare the expressions for equality!
Douglas Gregorf67875d2009-06-12 18:26:56 +0000150 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000151}
152
Douglas Gregorf67875d2009-06-12 18:26:56 +0000153static Sema::TemplateDeductionResult
154DeduceTemplateArguments(ASTContext &Context,
155 TemplateName Param,
156 TemplateName Arg,
157 Sema::TemplateDeductionInfo &Info,
158 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregord708c722009-06-09 16:35:58 +0000159 // FIXME: Implement template argument deduction for template
160 // template parameters.
161
Douglas Gregorf67875d2009-06-12 18:26:56 +0000162 // FIXME: this routine does not have enough information to produce
163 // good diagnostics.
164
Douglas Gregord708c722009-06-09 16:35:58 +0000165 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
166 TemplateDecl *ArgDecl = Arg.getAsTemplateDecl();
167
Douglas Gregorf67875d2009-06-12 18:26:56 +0000168 if (!ParamDecl || !ArgDecl) {
169 // FIXME: fill in Info.Param/Info.FirstArg
170 return Sema::TDK_Inconsistent;
171 }
Douglas Gregord708c722009-06-09 16:35:58 +0000172
173 ParamDecl = cast<TemplateDecl>(Context.getCanonicalDecl(ParamDecl));
174 ArgDecl = cast<TemplateDecl>(Context.getCanonicalDecl(ArgDecl));
Douglas Gregorf67875d2009-06-12 18:26:56 +0000175 if (ParamDecl != ArgDecl) {
176 // FIXME: fill in Info.Param/Info.FirstArg
177 return Sema::TDK_Inconsistent;
178 }
179
180 return Sema::TDK_Success;
Douglas Gregord708c722009-06-09 16:35:58 +0000181}
182
Douglas Gregor500d3312009-06-26 18:27:22 +0000183/// \brief Deduce the template arguments by comparing the parameter type and
184/// the argument type (C++ [temp.deduct.type]).
185///
186/// \param Context the AST context in which this deduction occurs.
187///
188/// \param TemplateParams the template parameters that we are deducing
189///
190/// \param ParamIn the parameter type
191///
192/// \param ArgIn the argument type
193///
194/// \param Info information about the template argument deduction itself
195///
196/// \param Deduced the deduced template arguments
197///
Douglas Gregor508f1c82009-06-26 23:10:12 +0000198/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
199/// how template argument deduction is performed.
Douglas Gregor500d3312009-06-26 18:27:22 +0000200///
201/// \returns the result of template argument deduction so far. Note that a
202/// "success" result means that template argument deduction has not yet failed,
203/// but it may still fail, later, for other reasons.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000204static Sema::TemplateDeductionResult
205DeduceTemplateArguments(ASTContext &Context,
206 TemplateParameterList *TemplateParams,
207 QualType ParamIn, QualType ArgIn,
208 Sema::TemplateDeductionInfo &Info,
Douglas Gregor500d3312009-06-26 18:27:22 +0000209 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000210 unsigned TDF) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000211 // We only want to look at the canonical types, since typedefs and
212 // sugar are not part of template argument deduction.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000213 QualType Param = Context.getCanonicalType(ParamIn);
214 QualType Arg = Context.getCanonicalType(ArgIn);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000215
Douglas Gregor500d3312009-06-26 18:27:22 +0000216 // C++0x [temp.deduct.call]p4 bullet 1:
217 // - If the original P is a reference type, the deduced A (i.e., the type
218 // referred to by the reference) can be more cv-qualified than the
219 // transformed A.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000220 if (TDF & TDF_ParamWithReferenceType) {
Douglas Gregor500d3312009-06-26 18:27:22 +0000221 unsigned ExtraQualsOnParam
222 = Param.getCVRQualifiers() & ~Arg.getCVRQualifiers();
223 Param.setCVRQualifiers(Param.getCVRQualifiers() & ~ExtraQualsOnParam);
224 }
225
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000226 // If the parameter type is not dependent, there is nothing to deduce.
227 if (!Param->isDependentType())
228 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000229
Douglas Gregor199d9912009-06-05 00:53:49 +0000230 // C++ [temp.deduct.type]p9:
Douglas Gregor199d9912009-06-05 00:53:49 +0000231 // A template type argument T, a template template argument TT or a
232 // template non-type argument i can be deduced if P and A have one of
233 // the following forms:
234 //
235 // T
236 // cv-list T
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000237 if (const TemplateTypeParmType *TemplateTypeParm
238 = Param->getAsTemplateTypeParmType()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000239 unsigned Index = TemplateTypeParm->getIndex();
240
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000241 // The argument type can not be less qualified than the parameter
242 // type.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000243 if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000244 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
245 Info.FirstArg = Deduced[Index];
246 Info.SecondArg = TemplateArgument(SourceLocation(), Arg);
247 return Sema::TDK_InconsistentQuals;
248 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000249
250 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
251
252 unsigned Quals = Arg.getCVRQualifiers() & ~Param.getCVRQualifiers();
253 QualType DeducedType = Arg.getQualifiedType(Quals);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000254
255 if (Deduced[Index].isNull())
256 Deduced[Index] = TemplateArgument(SourceLocation(), DeducedType);
257 else {
258 // C++ [temp.deduct.type]p2:
259 // [...] If type deduction cannot be done for any P/A pair, or if for
260 // any pair the deduction leads to more than one possible set of
261 // deduced values, or if different pairs yield different deduced
262 // values, or if any template argument remains neither deduced nor
263 // explicitly specified, template argument deduction fails.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000264 if (Deduced[Index].getAsType() != DeducedType) {
265 Info.Param
266 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
267 Info.FirstArg = Deduced[Index];
268 Info.SecondArg = TemplateArgument(SourceLocation(), Arg);
269 return Sema::TDK_Inconsistent;
270 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000271 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000272 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000273 }
274
Douglas Gregorf67875d2009-06-12 18:26:56 +0000275 // Set up the template argument deduction information for a failure.
276 Info.FirstArg = TemplateArgument(SourceLocation(), ParamIn);
277 Info.SecondArg = TemplateArgument(SourceLocation(), ArgIn);
278
Douglas Gregor508f1c82009-06-26 23:10:12 +0000279 // Check the cv-qualifiers on the parameter and argument types.
280 if (!(TDF & TDF_IgnoreQualifiers)) {
281 if (TDF & TDF_ParamWithReferenceType) {
282 if (Param.isMoreQualifiedThan(Arg))
283 return Sema::TDK_NonDeducedMismatch;
284 } else {
285 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
286 return Sema::TDK_NonDeducedMismatch;
287 }
288 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000289
Douglas Gregord560d502009-06-04 00:21:18 +0000290 switch (Param->getTypeClass()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000291 // No deduction possible for these types
292 case Type::Builtin:
Douglas Gregorf67875d2009-06-12 18:26:56 +0000293 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000294
295 // T *
Douglas Gregord560d502009-06-04 00:21:18 +0000296 case Type::Pointer: {
297 const PointerType *PointerArg = Arg->getAsPointerType();
298 if (!PointerArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000299 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord560d502009-06-04 00:21:18 +0000300
Douglas Gregor41128772009-06-26 23:27:24 +0000301 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000302 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000303 cast<PointerType>(Param)->getPointeeType(),
304 PointerArg->getPointeeType(),
Douglas Gregor41128772009-06-26 23:27:24 +0000305 Info, Deduced, SubTDF);
Douglas Gregord560d502009-06-04 00:21:18 +0000306 }
307
Douglas Gregor199d9912009-06-05 00:53:49 +0000308 // T &
Douglas Gregord560d502009-06-04 00:21:18 +0000309 case Type::LValueReference: {
310 const LValueReferenceType *ReferenceArg = Arg->getAsLValueReferenceType();
311 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000312 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord560d502009-06-04 00:21:18 +0000313
Douglas Gregorf67875d2009-06-12 18:26:56 +0000314 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000315 cast<LValueReferenceType>(Param)->getPointeeType(),
316 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000317 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000318 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000319
Douglas Gregor199d9912009-06-05 00:53:49 +0000320 // T && [C++0x]
Douglas Gregord560d502009-06-04 00:21:18 +0000321 case Type::RValueReference: {
322 const RValueReferenceType *ReferenceArg = Arg->getAsRValueReferenceType();
323 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000324 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord560d502009-06-04 00:21:18 +0000325
Douglas Gregorf67875d2009-06-12 18:26:56 +0000326 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000327 cast<RValueReferenceType>(Param)->getPointeeType(),
328 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000329 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000330 }
331
Douglas Gregor199d9912009-06-05 00:53:49 +0000332 // T [] (implied, but not stated explicitly)
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000333 case Type::IncompleteArray: {
334 const IncompleteArrayType *IncompleteArrayArg =
335 Context.getAsIncompleteArrayType(Arg);
336 if (!IncompleteArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000337 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000338
Douglas Gregorf67875d2009-06-12 18:26:56 +0000339 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000340 Context.getAsIncompleteArrayType(Param)->getElementType(),
341 IncompleteArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000342 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000343 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000344
345 // T [integer-constant]
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000346 case Type::ConstantArray: {
347 const ConstantArrayType *ConstantArrayArg =
348 Context.getAsConstantArrayType(Arg);
349 if (!ConstantArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000350 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000351
352 const ConstantArrayType *ConstantArrayParm =
353 Context.getAsConstantArrayType(Param);
354 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000355 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000356
Douglas Gregorf67875d2009-06-12 18:26:56 +0000357 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000358 ConstantArrayParm->getElementType(),
359 ConstantArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000360 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000361 }
362
Douglas Gregor199d9912009-06-05 00:53:49 +0000363 // type [i]
364 case Type::DependentSizedArray: {
365 const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg);
366 if (!ArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000367 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000368
369 // Check the element type of the arrays
370 const DependentSizedArrayType *DependentArrayParm
371 = cast<DependentSizedArrayType>(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000372 if (Sema::TemplateDeductionResult Result
373 = DeduceTemplateArguments(Context, TemplateParams,
374 DependentArrayParm->getElementType(),
375 ArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000376 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000377 return Result;
Douglas Gregor199d9912009-06-05 00:53:49 +0000378
379 // Determine the array bound is something we can deduce.
380 NonTypeTemplateParmDecl *NTTP
381 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
382 if (!NTTP)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000383 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000384
385 // We can perform template argument deduction for the given non-type
386 // template parameter.
387 assert(NTTP->getDepth() == 0 &&
388 "Cannot deduce non-type template argument at depth > 0");
389 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson335e24a2009-06-16 22:44:31 +0000390 = dyn_cast<ConstantArrayType>(ArrayArg)) {
391 llvm::APSInt Size(ConstantArrayArg->getSize());
392 return DeduceNonTypeTemplateArgument(Context, NTTP, Size,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000393 Info, Deduced);
Anders Carlsson335e24a2009-06-16 22:44:31 +0000394 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000395 if (const DependentSizedArrayType *DependentArrayArg
396 = dyn_cast<DependentSizedArrayType>(ArrayArg))
397 return DeduceNonTypeTemplateArgument(Context, NTTP,
398 DependentArrayArg->getSizeExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000399 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000400
401 // Incomplete type does not match a dependently-sized array type
Douglas Gregorf67875d2009-06-12 18:26:56 +0000402 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000403 }
404
Douglas Gregor0fce0ae2009-06-08 15:59:14 +0000405 // type(*)(T)
406 // T(*)()
407 // T(*)(T)
Anders Carlssona27fad52009-06-08 15:19:08 +0000408 case Type::FunctionProto: {
409 const FunctionProtoType *FunctionProtoArg =
410 dyn_cast<FunctionProtoType>(Arg);
411 if (!FunctionProtoArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000412 return Sema::TDK_NonDeducedMismatch;
Anders Carlssona27fad52009-06-08 15:19:08 +0000413
414 const FunctionProtoType *FunctionProtoParam =
415 cast<FunctionProtoType>(Param);
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000416
417 if (FunctionProtoParam->getTypeQuals() !=
418 FunctionProtoArg->getTypeQuals())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000419 return Sema::TDK_NonDeducedMismatch;
Anders Carlssona27fad52009-06-08 15:19:08 +0000420
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000421 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000422 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000423
424 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000425 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000426
Anders Carlssona27fad52009-06-08 15:19:08 +0000427 // Check return types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000428 if (Sema::TemplateDeductionResult Result
429 = DeduceTemplateArguments(Context, TemplateParams,
430 FunctionProtoParam->getResultType(),
431 FunctionProtoArg->getResultType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000432 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000433 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000434
Anders Carlssona27fad52009-06-08 15:19:08 +0000435 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
436 // Check argument types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000437 if (Sema::TemplateDeductionResult Result
438 = DeduceTemplateArguments(Context, TemplateParams,
439 FunctionProtoParam->getArgType(I),
440 FunctionProtoArg->getArgType(I),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000441 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000442 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000443 }
444
Douglas Gregorf67875d2009-06-12 18:26:56 +0000445 return Sema::TDK_Success;
Anders Carlssona27fad52009-06-08 15:19:08 +0000446 }
Douglas Gregord708c722009-06-09 16:35:58 +0000447
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000448 // template-name<T> (where template-name refers to a class template)
Douglas Gregord708c722009-06-09 16:35:58 +0000449 // template-name<i>
450 // TT<T> (TODO)
451 // TT<i> (TODO)
452 // TT<> (TODO)
453 case Type::TemplateSpecialization: {
454 const TemplateSpecializationType *SpecParam
455 = cast<TemplateSpecializationType>(Param);
456
457 // Check whether the template argument is a dependent template-id.
458 // FIXME: This is untested code; it can be tested when we implement
459 // partial ordering of class template partial specializations.
460 if (const TemplateSpecializationType *SpecArg
461 = dyn_cast<TemplateSpecializationType>(Arg)) {
462 // Perform template argument deduction for the template name.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000463 if (Sema::TemplateDeductionResult Result
464 = DeduceTemplateArguments(Context,
465 SpecParam->getTemplateName(),
466 SpecArg->getTemplateName(),
467 Info, Deduced))
468 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000469
470 unsigned NumArgs = SpecParam->getNumArgs();
471
472 // FIXME: When one of the template-names refers to a
473 // declaration with default template arguments, do we need to
474 // fill in those default template arguments here? Most likely,
475 // the answer is "yes", but I don't see any references. This
476 // issue may be resolved elsewhere, because we may want to
477 // instantiate default template arguments when
478 if (SpecArg->getNumArgs() != NumArgs)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000479 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000480
481 // Perform template argument deduction on each template
482 // argument.
483 for (unsigned I = 0; I != NumArgs; ++I)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000484 if (Sema::TemplateDeductionResult Result
485 = DeduceTemplateArguments(Context, TemplateParams,
486 SpecParam->getArg(I),
487 SpecArg->getArg(I),
488 Info, Deduced))
489 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000490
Douglas Gregorf67875d2009-06-12 18:26:56 +0000491 return Sema::TDK_Success;
Douglas Gregord708c722009-06-09 16:35:58 +0000492 }
493
494 // If the argument type is a class template specialization, we
495 // perform template argument deduction using its template
496 // arguments.
497 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
498 if (!RecordArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000499 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000500
Douglas Gregor41128772009-06-26 23:27:24 +0000501 // FIXME: Check TDF_DerivedClass here. When this flag is set, we need
502 // to troll through the base classes of the argument and try matching
503 // all of them. Failure to match does not mean that there is a problem,
504 // of course.
505
Douglas Gregord708c722009-06-09 16:35:58 +0000506 ClassTemplateSpecializationDecl *SpecArg
507 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
508 if (!SpecArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000509 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000510
511 // Perform template argument deduction for the template name.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000512 if (Sema::TemplateDeductionResult Result
513 = DeduceTemplateArguments(Context,
514 SpecParam->getTemplateName(),
515 TemplateName(SpecArg->getSpecializedTemplate()),
516 Info, Deduced))
517 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000518
519 // FIXME: Can the # of arguments in the parameter and the argument differ?
520 unsigned NumArgs = SpecParam->getNumArgs();
521 const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
522 if (NumArgs != ArgArgs.size())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000523 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000524
525 for (unsigned I = 0; I != NumArgs; ++I)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000526 if (Sema::TemplateDeductionResult Result
527 = DeduceTemplateArguments(Context, TemplateParams,
528 SpecParam->getArg(I),
529 ArgArgs.get(I),
530 Info, Deduced))
531 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000532
Douglas Gregorf67875d2009-06-12 18:26:56 +0000533 return Sema::TDK_Success;
Douglas Gregord708c722009-06-09 16:35:58 +0000534 }
535
Douglas Gregor637a4092009-06-10 23:47:09 +0000536 // T type::*
537 // T T::*
538 // T (type::*)()
539 // type (T::*)()
540 // type (type::*)(T)
541 // type (T::*)(T)
542 // T (type::*)(T)
543 // T (T::*)()
544 // T (T::*)(T)
545 case Type::MemberPointer: {
546 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
547 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
548 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000549 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +0000550
Douglas Gregorf67875d2009-06-12 18:26:56 +0000551 if (Sema::TemplateDeductionResult Result
552 = DeduceTemplateArguments(Context, TemplateParams,
553 MemPtrParam->getPointeeType(),
554 MemPtrArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000555 Info, Deduced,
556 TDF & TDF_IgnoreQualifiers))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000557 return Result;
558
559 return DeduceTemplateArguments(Context, TemplateParams,
560 QualType(MemPtrParam->getClass(), 0),
561 QualType(MemPtrArg->getClass(), 0),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000562 Info, Deduced, 0);
Douglas Gregor637a4092009-06-10 23:47:09 +0000563 }
564
Anders Carlsson9a917e42009-06-12 22:56:54 +0000565 // (clang extension)
566 //
Anders Carlsson859ba502009-06-12 16:23:10 +0000567 // type(^)(T)
568 // T(^)()
569 // T(^)(T)
570 case Type::BlockPointer: {
571 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
572 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
573
574 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000575 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson859ba502009-06-12 16:23:10 +0000576
Douglas Gregorf67875d2009-06-12 18:26:56 +0000577 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +0000578 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000579 BlockPtrArg->getPointeeType(), Info,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000580 Deduced, 0);
Anders Carlsson859ba502009-06-12 16:23:10 +0000581 }
582
Douglas Gregor637a4092009-06-10 23:47:09 +0000583 case Type::TypeOfExpr:
584 case Type::TypeOf:
585 case Type::Typename:
586 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +0000587 return Sema::TDK_Success;
Douglas Gregor637a4092009-06-10 23:47:09 +0000588
Douglas Gregord560d502009-06-04 00:21:18 +0000589 default:
590 break;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000591 }
592
593 // FIXME: Many more cases to go (to go).
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000594 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000595}
596
Douglas Gregorf67875d2009-06-12 18:26:56 +0000597static Sema::TemplateDeductionResult
598DeduceTemplateArguments(ASTContext &Context,
599 TemplateParameterList *TemplateParams,
600 const TemplateArgument &Param,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000601 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000602 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000603 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000604 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000605 case TemplateArgument::Null:
606 assert(false && "Null template argument in parameter list");
607 break;
608
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000609 case TemplateArgument::Type:
Douglas Gregor199d9912009-06-05 00:53:49 +0000610 assert(Arg.getKind() == TemplateArgument::Type && "Type/value mismatch");
Douglas Gregor508f1c82009-06-26 23:10:12 +0000611 return DeduceTemplateArguments(Context, TemplateParams, Param.getAsType(),
612 Arg.getAsType(), Info, Deduced, 0);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000613
Douglas Gregor199d9912009-06-05 00:53:49 +0000614 case TemplateArgument::Declaration:
615 // FIXME: Implement this check
616 assert(false && "Unimplemented template argument deduction case");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000617 Info.FirstArg = Param;
618 Info.SecondArg = Arg;
619 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000620
621 case TemplateArgument::Integral:
622 if (Arg.getKind() == TemplateArgument::Integral) {
623 // FIXME: Zero extension + sign checking here?
Douglas Gregorf67875d2009-06-12 18:26:56 +0000624 if (*Param.getAsIntegral() == *Arg.getAsIntegral())
625 return Sema::TDK_Success;
626
627 Info.FirstArg = Param;
628 Info.SecondArg = Arg;
629 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000630 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000631
632 if (Arg.getKind() == TemplateArgument::Expression) {
633 Info.FirstArg = Param;
634 Info.SecondArg = Arg;
635 return Sema::TDK_NonDeducedMismatch;
636 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000637
638 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000639 Info.FirstArg = Param;
640 Info.SecondArg = Arg;
641 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000642
643 case TemplateArgument::Expression: {
644 if (NonTypeTemplateParmDecl *NTTP
645 = getDeducedParameterFromExpr(Param.getAsExpr())) {
646 if (Arg.getKind() == TemplateArgument::Integral)
647 // FIXME: Sign problems here
648 return DeduceNonTypeTemplateArgument(Context, NTTP,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000649 *Arg.getAsIntegral(),
650 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000651 if (Arg.getKind() == TemplateArgument::Expression)
652 return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000653 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000654
655 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000656 Info.FirstArg = Param;
657 Info.SecondArg = Arg;
658 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000659 }
660
661 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000662 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000663 }
Anders Carlssond01b1da2009-06-15 17:04:53 +0000664 case TemplateArgument::Pack:
665 assert(0 && "FIXME: Implement!");
666 break;
Douglas Gregor199d9912009-06-05 00:53:49 +0000667 }
668
Douglas Gregorf67875d2009-06-12 18:26:56 +0000669 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000670}
671
Douglas Gregorf67875d2009-06-12 18:26:56 +0000672static Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000673DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000674 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000675 const TemplateArgumentList &ParamList,
676 const TemplateArgumentList &ArgList,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000677 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000678 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
679 assert(ParamList.size() == ArgList.size());
680 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000681 if (Sema::TemplateDeductionResult Result
682 = DeduceTemplateArguments(Context, TemplateParams,
683 ParamList[I], ArgList[I],
684 Info, Deduced))
685 return Result;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000686 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000687 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000688}
689
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000690/// \brief Determine whether two template arguments are the same.
691static bool isSameTemplateArg(ASTContext &Context,
692 const TemplateArgument &X,
693 const TemplateArgument &Y) {
694 if (X.getKind() != Y.getKind())
695 return false;
696
697 switch (X.getKind()) {
698 case TemplateArgument::Null:
699 assert(false && "Comparing NULL template argument");
700 break;
701
702 case TemplateArgument::Type:
703 return Context.getCanonicalType(X.getAsType()) ==
704 Context.getCanonicalType(Y.getAsType());
705
706 case TemplateArgument::Declaration:
707 return Context.getCanonicalDecl(X.getAsDecl()) ==
708 Context.getCanonicalDecl(Y.getAsDecl());
709
710 case TemplateArgument::Integral:
711 return *X.getAsIntegral() == *Y.getAsIntegral();
712
713 case TemplateArgument::Expression:
714 // FIXME: We assume that all expressions are distinct, but we should
715 // really check their canonical forms.
716 return false;
717
718 case TemplateArgument::Pack:
719 if (X.pack_size() != Y.pack_size())
720 return false;
721
722 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
723 XPEnd = X.pack_end(),
724 YP = Y.pack_begin();
725 XP != XPEnd; ++XP, ++YP)
726 if (!isSameTemplateArg(Context, *XP, *YP))
727 return false;
728
729 return true;
730 }
731
732 return false;
733}
734
735/// \brief Helper function to build a TemplateParameter when we don't
736/// know its type statically.
737static TemplateParameter makeTemplateParameter(Decl *D) {
738 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
739 return TemplateParameter(TTP);
740 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
741 return TemplateParameter(NTTP);
742
743 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
744}
745
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000746/// \brief Perform template argument deduction to determine whether
747/// the given template arguments match the given class template
748/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +0000749Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000750Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000751 const TemplateArgumentList &TemplateArgs,
752 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000753 // C++ [temp.class.spec.match]p2:
754 // A partial specialization matches a given actual template
755 // argument list if the template arguments of the partial
756 // specialization can be deduced from the actual template argument
757 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +0000758 SFINAETrap Trap(*this);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000759 llvm::SmallVector<TemplateArgument, 4> Deduced;
760 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000761 if (TemplateDeductionResult Result
762 = ::DeduceTemplateArguments(Context,
763 Partial->getTemplateParameters(),
764 Partial->getTemplateArgs(),
765 TemplateArgs, Info, Deduced))
766 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +0000767
Douglas Gregor637a4092009-06-10 23:47:09 +0000768 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
769 Deduced.data(), Deduced.size());
770 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000771 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +0000772
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000773 // C++ [temp.deduct.type]p2:
774 // [...] or if any template argument remains neither deduced nor
775 // explicitly specified, template argument deduction fails.
Anders Carlssonfb250522009-06-23 01:26:57 +0000776 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
777 Deduced.size());
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000778 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000779 if (Deduced[I].isNull()) {
780 Decl *Param
781 = const_cast<Decl *>(Partial->getTemplateParameters()->getParam(I));
782 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
783 Info.Param = TTP;
784 else if (NonTypeTemplateParmDecl *NTTP
785 = dyn_cast<NonTypeTemplateParmDecl>(Param))
786 Info.Param = NTTP;
787 else
788 Info.Param = cast<TemplateTemplateParmDecl>(Param);
789 return TDK_Incomplete;
790 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000791
Anders Carlssonfb250522009-06-23 01:26:57 +0000792 Builder.Append(Deduced[I]);
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000793 }
794
795 // Form the template argument list from the deduced template arguments.
796 TemplateArgumentList *DeducedArgumentList
Anders Carlssonfb250522009-06-23 01:26:57 +0000797 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000798 Info.reset(DeducedArgumentList);
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000799
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000800 // Substitute the deduced template arguments into the template
801 // arguments of the class template partial specialization, and
802 // verify that the instantiated template arguments are both valid
803 // and are equivalent to the template arguments originally provided
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000804 // to the class template.
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000805 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
806 const TemplateArgumentList &PartialTemplateArgs = Partial->getTemplateArgs();
807 for (unsigned I = 0, N = PartialTemplateArgs.flat_size(); I != N; ++I) {
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000808 Decl *Param = const_cast<Decl *>(
809 ClassTemplate->getTemplateParameters()->getParam(I));
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000810 TemplateArgument InstArg = Instantiate(PartialTemplateArgs[I],
811 *DeducedArgumentList);
812 if (InstArg.isNull()) {
813 Info.Param = makeTemplateParameter(Param);
814 Info.FirstArg = PartialTemplateArgs[I];
815 return TDK_SubstitutionFailure;
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000816 }
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000817
818 if (InstArg.getKind() == TemplateArgument::Expression) {
819 // When the argument is an expression, check the expression result
820 // against the actual template parameter to get down to the canonical
821 // template argument.
822 Expr *InstExpr = InstArg.getAsExpr();
823 if (NonTypeTemplateParmDecl *NTTP
824 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
825 if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
826 Info.Param = makeTemplateParameter(Param);
827 Info.FirstArg = PartialTemplateArgs[I];
828 return TDK_SubstitutionFailure;
829 }
830 } else if (TemplateTemplateParmDecl *TTP
831 = dyn_cast<TemplateTemplateParmDecl>(Param)) {
832 // FIXME: template template arguments should really resolve to decls
833 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InstExpr);
834 if (!DRE || CheckTemplateArgument(TTP, DRE)) {
835 Info.Param = makeTemplateParameter(Param);
836 Info.FirstArg = PartialTemplateArgs[I];
837 return TDK_SubstitutionFailure;
838 }
839 }
840 }
841
842 if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) {
843 Info.Param = makeTemplateParameter(Param);
844 Info.FirstArg = TemplateArgs[I];
845 Info.SecondArg = InstArg;
846 return TDK_NonDeducedMismatch;
847 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000848 }
849
Douglas Gregorbb260412009-06-14 08:02:22 +0000850 if (Trap.hasErrorOccurred())
851 return TDK_SubstitutionFailure;
852
Douglas Gregorf67875d2009-06-12 18:26:56 +0000853 return TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000854}
Douglas Gregor031a5882009-06-13 00:26:55 +0000855
Douglas Gregor41128772009-06-26 23:27:24 +0000856/// \brief Determine whether the given type T is a simple-template-id type.
857static bool isSimpleTemplateIdType(QualType T) {
858 if (const TemplateSpecializationType *Spec
859 = T->getAsTemplateSpecializationType())
860 return Spec->getTemplateName().getAsTemplateDecl() != 0;
861
862 return false;
863}
864
Douglas Gregore53060f2009-06-25 22:08:12 +0000865/// \brief Perform template argument deduction from a function call
866/// (C++ [temp.deduct.call]).
867///
868/// \param FunctionTemplate the function template for which we are performing
869/// template argument deduction.
870///
Douglas Gregor6db8ed42009-06-30 23:57:56 +0000871/// \param HasExplicitTemplateArgs whether any template arguments were
872/// explicitly specified.
873///
874/// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
875/// the explicitly-specified template arguments.
876///
877/// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
878/// the number of explicitly-specified template arguments in
879/// @p ExplicitTemplateArguments. This value may be zero.
880///
Douglas Gregore53060f2009-06-25 22:08:12 +0000881/// \param Args the function call arguments
882///
883/// \param NumArgs the number of arguments in Args
884///
885/// \param Specialization if template argument deduction was successful,
886/// this will be set to the function template specialization produced by
887/// template argument deduction.
888///
889/// \param Info the argument will be updated to provide additional information
890/// about template argument deduction.
891///
892/// \returns the result of template argument deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +0000893Sema::TemplateDeductionResult
894Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor6db8ed42009-06-30 23:57:56 +0000895 bool HasExplicitTemplateArgs,
896 const TemplateArgument *ExplicitTemplateArgs,
897 unsigned NumExplicitTemplateArgs,
Douglas Gregore53060f2009-06-25 22:08:12 +0000898 Expr **Args, unsigned NumArgs,
899 FunctionDecl *&Specialization,
900 TemplateDeductionInfo &Info) {
901 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor6db8ed42009-06-30 23:57:56 +0000902
Douglas Gregore53060f2009-06-25 22:08:12 +0000903 // C++ [temp.deduct.call]p1:
904 // Template argument deduction is done by comparing each function template
905 // parameter type (call it P) with the type of the corresponding argument
906 // of the call (call it A) as described below.
907 unsigned CheckArgs = NumArgs;
Douglas Gregor6db8ed42009-06-30 23:57:56 +0000908 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregore53060f2009-06-25 22:08:12 +0000909 return TDK_TooFewArguments;
910 else if (NumArgs > Function->getNumParams()) {
911 const FunctionProtoType *Proto
912 = Function->getType()->getAsFunctionProtoType();
913 if (!Proto->isVariadic())
914 return TDK_TooManyArguments;
915
916 CheckArgs = Function->getNumParams();
917 }
Douglas Gregor6db8ed42009-06-30 23:57:56 +0000918
Douglas Gregor500d3312009-06-26 18:27:22 +0000919 // Template argument deduction for function templates in a SFINAE context.
920 // Trap any errors that might occur.
Douglas Gregore53060f2009-06-25 22:08:12 +0000921 SFINAETrap Trap(*this);
Douglas Gregor6db8ed42009-06-30 23:57:56 +0000922
923 // The types of the parameters from which we will perform template argument
924 // deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +0000925 TemplateParameterList *TemplateParams
926 = FunctionTemplate->getTemplateParameters();
Douglas Gregor6db8ed42009-06-30 23:57:56 +0000927 llvm::SmallVector<TemplateArgument, 4> Deduced;
928 llvm::SmallVector<QualType, 4> ParamTypes;
929 if (NumExplicitTemplateArgs) {
930 // C++ [temp.arg.explicit]p3:
931 // Template arguments that are present shall be specified in the
932 // declaration order of their corresponding template-parameters. The
933 // template argument list shall not specify more template-arguments than
934 // there are corresponding template-parameters.
935 TemplateArgumentListBuilder Builder(TemplateParams,
936 NumExplicitTemplateArgs);
937 if (CheckTemplateArgumentList(FunctionTemplate,
938 SourceLocation(), SourceLocation(),
939 ExplicitTemplateArgs,
940 NumExplicitTemplateArgs,
941 SourceLocation(),
942 Builder) || Trap.hasErrorOccurred())
943 return TDK_InvalidExplicitArguments;
944
945 // Enter a new template instantiation context for the substitution of the
946 // explicitly-specified template arguments into the
947 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
948 FunctionTemplate, Deduced.data(), Deduced.size());
949 if (Inst)
950 return TDK_InstantiationDepth;
951
952 // Form the template argument list from the explicitly-specified
953 // template arguments.
954 TemplateArgumentList *ExplicitArgumentList
955 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
956 Info.reset(ExplicitArgumentList);
957
958 // Instantiate the types of each of the function parameters given the
959 // explicitly-specified template arguments.
960 for (FunctionDecl::param_iterator P = Function->param_begin(),
961 PEnd = Function->param_end();
962 P != PEnd;
963 ++P) {
964 QualType ParamType = InstantiateType((*P)->getType(),
965 *ExplicitArgumentList,
966 (*P)->getLocation(),
967 (*P)->getDeclName());
968 if (ParamType.isNull() || Trap.hasErrorOccurred())
969 return TDK_SubstitutionFailure;
970
971 ParamTypes.push_back(ParamType);
972 }
973
974 // C++ [temp.arg.explicit]p2:
975 // Trailing template arguments that can be deduced (14.8.2) may be
976 // omitted from the list of explicit template- arguments. If all of the
977 // template arguments can be deduced, they may all be omitted; in this
978 // case, the empty template argument list <> itself may also be omitted.
979 //
980 // Take all of the explicitly-specified arguments and put them into the
981 // set of deduced template arguments.
982 Deduced.reserve(TemplateParams->size());
983 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
984 Deduced.push_back(ExplicitArgumentList->get(I));
985 } else {
986 // Just fill in the parameter types from the function declaration.
987 for (unsigned I = 0; I != CheckArgs; ++I)
988 ParamTypes.push_back(Function->getParamDecl(I)->getType());
989 }
990
991 // Deduce template arguments from the function parameters.
992 Deduced.resize(TemplateParams->size());
Douglas Gregore53060f2009-06-25 22:08:12 +0000993 for (unsigned I = 0; I != CheckArgs; ++I) {
Douglas Gregor6db8ed42009-06-30 23:57:56 +0000994 QualType ParamType = ParamTypes[I];
Douglas Gregore53060f2009-06-25 22:08:12 +0000995 QualType ArgType = Args[I]->getType();
Douglas Gregor500d3312009-06-26 18:27:22 +0000996
Douglas Gregore53060f2009-06-25 22:08:12 +0000997 // C++ [temp.deduct.call]p2:
998 // If P is not a reference type:
999 QualType CanonParamType = Context.getCanonicalType(ParamType);
Douglas Gregor500d3312009-06-26 18:27:22 +00001000 bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1001 if (!ParamWasReference) {
Douglas Gregore53060f2009-06-25 22:08:12 +00001002 // - If A is an array type, the pointer type produced by the
1003 // array-to-pointer standard conversion (4.2) is used in place of
1004 // A for type deduction; otherwise,
1005 if (ArgType->isArrayType())
1006 ArgType = Context.getArrayDecayedType(ArgType);
1007 // - If A is a function type, the pointer type produced by the
1008 // function-to-pointer standard conversion (4.3) is used in place
1009 // of A for type deduction; otherwise,
1010 else if (ArgType->isFunctionType())
1011 ArgType = Context.getPointerType(ArgType);
1012 else {
1013 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1014 // type are ignored for type deduction.
1015 QualType CanonArgType = Context.getCanonicalType(ArgType);
1016 if (CanonArgType.getCVRQualifiers())
1017 ArgType = CanonArgType.getUnqualifiedType();
1018 }
1019 }
1020
1021 // C++0x [temp.deduct.call]p3:
1022 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
1023 // are ignored for type deduction.
1024 if (CanonParamType.getCVRQualifiers())
1025 ParamType = CanonParamType.getUnqualifiedType();
1026 if (const ReferenceType *ParamRefType = ParamType->getAsReferenceType()) {
1027 // [...] If P is a reference type, the type referred to by P is used
1028 // for type deduction.
1029 ParamType = ParamRefType->getPointeeType();
1030
1031 // [...] If P is of the form T&&, where T is a template parameter, and
1032 // the argument is an lvalue, the type A& is used in place of A for
1033 // type deduction.
1034 if (isa<RValueReferenceType>(ParamRefType) &&
1035 ParamRefType->getAsTemplateTypeParmType() &&
1036 Args[I]->isLvalue(Context) == Expr::LV_Valid)
1037 ArgType = Context.getLValueReferenceType(ArgType);
1038 }
1039
1040 // C++0x [temp.deduct.call]p4:
1041 // In general, the deduction process attempts to find template argument
1042 // values that will make the deduced A identical to A (after the type A
1043 // is transformed as described above). [...]
Douglas Gregor508f1c82009-06-26 23:10:12 +00001044 unsigned TDF = 0;
1045
1046 // - If the original P is a reference type, the deduced A (i.e., the
1047 // type referred to by the reference) can be more cv-qualified than
1048 // the transformed A.
1049 if (ParamWasReference)
1050 TDF |= TDF_ParamWithReferenceType;
1051 // - The transformed A can be another pointer or pointer to member
1052 // type that can be converted to the deduced A via a qualification
1053 // conversion (4.4).
1054 if (ArgType->isPointerType() || ArgType->isMemberPointerType())
1055 TDF |= TDF_IgnoreQualifiers;
Douglas Gregor41128772009-06-26 23:27:24 +00001056 // - If P is a class and P has the form simple-template-id, then the
1057 // transformed A can be a derived class of the deduced A. Likewise,
1058 // if P is a pointer to a class of the form simple-template-id, the
1059 // transformed A can be a pointer to a derived class pointed to by
1060 // the deduced A.
1061 if (isSimpleTemplateIdType(ParamType) ||
1062 (isa<PointerType>(ParamType) &&
1063 isSimpleTemplateIdType(
1064 ParamType->getAsPointerType()->getPointeeType())))
1065 TDF |= TDF_DerivedClass;
1066
Douglas Gregore53060f2009-06-25 22:08:12 +00001067 if (TemplateDeductionResult Result
1068 = ::DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor500d3312009-06-26 18:27:22 +00001069 ParamType, ArgType, Info, Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +00001070 TDF))
Douglas Gregore53060f2009-06-25 22:08:12 +00001071 return Result;
1072
1073 // FIXME: C++ [temp.deduct.call] paragraphs 6-9 deal with function
1074 // pointer parameters.
1075 }
Douglas Gregore53060f2009-06-25 22:08:12 +00001076
1077 // C++ [temp.deduct.type]p2:
1078 // [...] or if any template argument remains neither deduced nor
1079 // explicitly specified, template argument deduction fails.
1080 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
1081 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
1082 if (Deduced[I].isNull()) {
1083 Decl *Param
1084 = const_cast<Decl *>(TemplateParams->getParam(I));
1085 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
1086 Info.Param = TTP;
1087 else if (NonTypeTemplateParmDecl *NTTP
1088 = dyn_cast<NonTypeTemplateParmDecl>(Param))
1089 Info.Param = NTTP;
1090 else
1091 Info.Param = cast<TemplateTemplateParmDecl>(Param);
1092 return TDK_Incomplete;
1093 }
1094
1095 Builder.Append(Deduced[I]);
1096 }
1097
1098 // Form the template argument list from the deduced template arguments.
1099 TemplateArgumentList *DeducedArgumentList
1100 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1101 Info.reset(DeducedArgumentList);
1102
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001103 // Enter a new template instantiation context while we instantiate the
1104 // actual function declaration.
1105 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
1106 FunctionTemplate, Deduced.data(), Deduced.size());
1107 if (Inst)
1108 return TDK_InstantiationDepth;
1109
Douglas Gregore53060f2009-06-25 22:08:12 +00001110 // Substitute the deduced template arguments into the function template
1111 // declaration to produce the function template specialization.
1112 Specialization = cast_or_null<FunctionDecl>(
1113 InstantiateDecl(FunctionTemplate->getTemplatedDecl(),
1114 FunctionTemplate->getDeclContext(),
1115 *DeducedArgumentList));
Douglas Gregor127102b2009-06-29 20:59:39 +00001116 if (!Specialization)
Douglas Gregore53060f2009-06-25 22:08:12 +00001117 return TDK_SubstitutionFailure;
Douglas Gregor127102b2009-06-29 20:59:39 +00001118
1119 // If the template argument list is owned by the function template
1120 // specialization, release it.
1121 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList)
1122 Info.take();
Douglas Gregore53060f2009-06-25 22:08:12 +00001123
Douglas Gregor127102b2009-06-29 20:59:39 +00001124 // There may have been an error that did not prevent us from constructing a
1125 // declaration. Mark the declaration invalid and return with a substitution
1126 // failure.
1127 if (Trap.hasErrorOccurred()) {
1128 Specialization->setInvalidDecl(true);
1129 return TDK_SubstitutionFailure;
1130 }
1131
Douglas Gregore53060f2009-06-25 22:08:12 +00001132 return TDK_Success;
1133}
1134
Douglas Gregor031a5882009-06-13 00:26:55 +00001135static void
1136MarkDeducedTemplateParameters(Sema &SemaRef,
1137 const TemplateArgument &TemplateArg,
1138 llvm::SmallVectorImpl<bool> &Deduced);
1139
1140/// \brief Mark the template arguments that are deduced by the given
1141/// expression.
1142static void
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001143MarkDeducedTemplateParameters(const Expr *E,
1144 llvm::SmallVectorImpl<bool> &Deduced) {
1145 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregor031a5882009-06-13 00:26:55 +00001146 if (!E)
1147 return;
1148
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001149 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00001150 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
1151 if (!NTTP)
1152 return;
1153
1154 Deduced[NTTP->getIndex()] = true;
1155}
1156
1157/// \brief Mark the template parameters that are deduced by the given
1158/// type.
1159static void
1160MarkDeducedTemplateParameters(Sema &SemaRef, QualType T,
1161 llvm::SmallVectorImpl<bool> &Deduced) {
1162 // Non-dependent types have nothing deducible
1163 if (!T->isDependentType())
1164 return;
1165
1166 T = SemaRef.Context.getCanonicalType(T);
1167 switch (T->getTypeClass()) {
1168 case Type::ExtQual:
1169 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001170 QualType(cast<ExtQualType>(T)->getBaseType(), 0),
Douglas Gregor031a5882009-06-13 00:26:55 +00001171 Deduced);
1172 break;
1173
1174 case Type::Pointer:
1175 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001176 cast<PointerType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001177 Deduced);
1178 break;
1179
1180 case Type::BlockPointer:
1181 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001182 cast<BlockPointerType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001183 Deduced);
1184 break;
1185
1186 case Type::LValueReference:
1187 case Type::RValueReference:
1188 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001189 cast<ReferenceType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001190 Deduced);
1191 break;
1192
1193 case Type::MemberPointer: {
1194 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
1195 MarkDeducedTemplateParameters(SemaRef, MemPtr->getPointeeType(), Deduced);
1196 MarkDeducedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
1197 Deduced);
1198 break;
1199 }
1200
1201 case Type::DependentSizedArray:
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001202 MarkDeducedTemplateParameters(cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001203 Deduced);
1204 // Fall through to check the element type
1205
1206 case Type::ConstantArray:
1207 case Type::IncompleteArray:
1208 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001209 cast<ArrayType>(T)->getElementType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001210 Deduced);
1211 break;
1212
1213 case Type::Vector:
1214 case Type::ExtVector:
1215 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001216 cast<VectorType>(T)->getElementType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001217 Deduced);
1218 break;
1219
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001220 case Type::DependentSizedExtVector: {
1221 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001222 = cast<DependentSizedExtVectorType>(T);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001223 MarkDeducedTemplateParameters(SemaRef, VecType->getElementType(), Deduced);
1224 MarkDeducedTemplateParameters(VecType->getSizeExpr(), Deduced);
1225 break;
1226 }
1227
Douglas Gregor031a5882009-06-13 00:26:55 +00001228 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001229 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregor031a5882009-06-13 00:26:55 +00001230 MarkDeducedTemplateParameters(SemaRef, Proto->getResultType(), Deduced);
1231 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
1232 MarkDeducedTemplateParameters(SemaRef, Proto->getArgType(I), Deduced);
1233 break;
1234 }
1235
1236 case Type::TemplateTypeParm:
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001237 Deduced[cast<TemplateTypeParmType>(T)->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00001238 break;
1239
1240 case Type::TemplateSpecialization: {
1241 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001242 = cast<TemplateSpecializationType>(T);
Douglas Gregor031a5882009-06-13 00:26:55 +00001243 if (TemplateDecl *Template = Spec->getTemplateName().getAsTemplateDecl())
1244 if (TemplateTemplateParmDecl *TTP
1245 = dyn_cast<TemplateTemplateParmDecl>(Template))
1246 Deduced[TTP->getIndex()] = true;
1247
1248 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
1249 MarkDeducedTemplateParameters(SemaRef, Spec->getArg(I), Deduced);
1250
1251 break;
1252 }
1253
1254 // None of these types have any deducible parts.
1255 case Type::Builtin:
1256 case Type::FixedWidthInt:
1257 case Type::Complex:
1258 case Type::VariableArray:
1259 case Type::FunctionNoProto:
1260 case Type::Record:
1261 case Type::Enum:
1262 case Type::Typename:
1263 case Type::ObjCInterface:
1264 case Type::ObjCQualifiedInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001265 case Type::ObjCObjectPointer:
Douglas Gregor031a5882009-06-13 00:26:55 +00001266#define TYPE(Class, Base)
1267#define ABSTRACT_TYPE(Class, Base)
1268#define DEPENDENT_TYPE(Class, Base)
1269#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
1270#include "clang/AST/TypeNodes.def"
1271 break;
1272 }
1273}
1274
1275/// \brief Mark the template parameters that are deduced by this
1276/// template argument.
1277static void
1278MarkDeducedTemplateParameters(Sema &SemaRef,
1279 const TemplateArgument &TemplateArg,
1280 llvm::SmallVectorImpl<bool> &Deduced) {
1281 switch (TemplateArg.getKind()) {
1282 case TemplateArgument::Null:
1283 case TemplateArgument::Integral:
1284 break;
1285
1286 case TemplateArgument::Type:
1287 MarkDeducedTemplateParameters(SemaRef, TemplateArg.getAsType(), Deduced);
1288 break;
1289
1290 case TemplateArgument::Declaration:
1291 if (TemplateTemplateParmDecl *TTP
1292 = dyn_cast<TemplateTemplateParmDecl>(TemplateArg.getAsDecl()))
1293 Deduced[TTP->getIndex()] = true;
1294 break;
1295
1296 case TemplateArgument::Expression:
1297 MarkDeducedTemplateParameters(TemplateArg.getAsExpr(), Deduced);
1298 break;
Anders Carlssond01b1da2009-06-15 17:04:53 +00001299 case TemplateArgument::Pack:
1300 assert(0 && "FIXME: Implement!");
1301 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00001302 }
1303}
1304
1305/// \brief Mark the template parameters can be deduced by the given
1306/// template argument list.
1307///
1308/// \param TemplateArgs the template argument list from which template
1309/// parameters will be deduced.
1310///
1311/// \param Deduced a bit vector whose elements will be set to \c true
1312/// to indicate when the corresponding template parameter will be
1313/// deduced.
1314void
1315Sema::MarkDeducedTemplateParameters(const TemplateArgumentList &TemplateArgs,
1316 llvm::SmallVectorImpl<bool> &Deduced) {
1317 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
1318 ::MarkDeducedTemplateParameters(*this, TemplateArgs[I], Deduced);
1319}