blob: 20219d205e027860e04c4fa934e6867568cbb86d [file] [log] [blame]
Douglas Gregorbf23a8a2009-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 Gregor61112762009-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
40 /// deduction from a derived class of the argument type.
41 /// FIXME: this is completely unsupported right now.
42 TDF_DerivedClass = 0x04
43 };
44}
45
Douglas Gregorbf23a8a2009-06-04 00:03:07 +000046using namespace clang;
47
Douglas Gregor623e2e02009-06-12 18:26:56 +000048static Sema::TemplateDeductionResult
49DeduceTemplateArguments(ASTContext &Context,
50 TemplateParameterList *TemplateParams,
51 const TemplateArgument &Param,
Douglas Gregor5069ae32009-06-09 16:35:58 +000052 const TemplateArgument &Arg,
Douglas Gregor623e2e02009-06-12 18:26:56 +000053 Sema::TemplateDeductionInfo &Info,
Douglas Gregor5069ae32009-06-09 16:35:58 +000054 llvm::SmallVectorImpl<TemplateArgument> &Deduced);
55
Douglas Gregorab9f71a2009-06-05 00:53:49 +000056/// \brief If the given expression is of a form that permits the deduction
57/// of a non-type template parameter, return the declaration of that
58/// non-type template parameter.
59static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
60 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
61 E = IC->getSubExpr();
62
63 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
64 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
65
66 return 0;
67}
68
69/// \brief Deduce the value of the given non-type template parameter
70/// from the given constant.
Douglas Gregor623e2e02009-06-12 18:26:56 +000071static Sema::TemplateDeductionResult
72DeduceNonTypeTemplateArgument(ASTContext &Context,
73 NonTypeTemplateParmDecl *NTTP,
Anders Carlssoncfb830e2009-06-16 22:44:31 +000074 llvm::APSInt Value,
Douglas Gregor623e2e02009-06-12 18:26:56 +000075 Sema::TemplateDeductionInfo &Info,
76 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregorab9f71a2009-06-05 00:53:49 +000077 assert(NTTP->getDepth() == 0 &&
78 "Cannot deduce non-type template argument with depth > 0");
79
80 if (Deduced[NTTP->getIndex()].isNull()) {
Anders Carlsson1de70892009-06-16 23:08:29 +000081 QualType T = NTTP->getType();
82
83 // FIXME: Make sure we didn't overflow our data type!
84 unsigned AllowedBits = Context.getTypeSize(T);
85 if (Value.getBitWidth() != AllowedBits)
86 Value.extOrTrunc(AllowedBits);
87 Value.setIsSigned(T->isSignedIntegerType());
88
89 Deduced[NTTP->getIndex()] = TemplateArgument(SourceLocation(), Value, T);
Douglas Gregor623e2e02009-06-12 18:26:56 +000090 return Sema::TDK_Success;
Douglas Gregorab9f71a2009-06-05 00:53:49 +000091 }
92
Douglas Gregor623e2e02009-06-12 18:26:56 +000093 assert(Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral);
Douglas Gregorab9f71a2009-06-05 00:53:49 +000094
95 // If the template argument was previously deduced to a negative value,
96 // then our deduction fails.
97 const llvm::APSInt *PrevValuePtr = Deduced[NTTP->getIndex()].getAsIntegral();
Anders Carlssoncfb830e2009-06-16 22:44:31 +000098 if (PrevValuePtr->isNegative()) {
Douglas Gregor623e2e02009-06-12 18:26:56 +000099 Info.Param = NTTP;
100 Info.FirstArg = Deduced[NTTP->getIndex()];
Anders Carlssoncfb830e2009-06-16 22:44:31 +0000101 Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType());
Douglas Gregor623e2e02009-06-12 18:26:56 +0000102 return Sema::TDK_Inconsistent;
103 }
104
Anders Carlssoncfb830e2009-06-16 22:44:31 +0000105 llvm::APSInt PrevValue = *PrevValuePtr;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000106 if (Value.getBitWidth() > PrevValue.getBitWidth())
107 PrevValue.zext(Value.getBitWidth());
108 else if (Value.getBitWidth() < PrevValue.getBitWidth())
109 Value.zext(PrevValue.getBitWidth());
Douglas Gregor623e2e02009-06-12 18:26:56 +0000110
111 if (Value != PrevValue) {
112 Info.Param = NTTP;
113 Info.FirstArg = Deduced[NTTP->getIndex()];
Anders Carlssoncfb830e2009-06-16 22:44:31 +0000114 Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType());
Douglas Gregor623e2e02009-06-12 18:26:56 +0000115 return Sema::TDK_Inconsistent;
116 }
117
118 return Sema::TDK_Success;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000119}
120
121/// \brief Deduce the value of the given non-type template parameter
122/// from the given type- or value-dependent expression.
123///
124/// \returns true if deduction succeeded, false otherwise.
125
Douglas Gregor623e2e02009-06-12 18:26:56 +0000126static Sema::TemplateDeductionResult
127DeduceNonTypeTemplateArgument(ASTContext &Context,
128 NonTypeTemplateParmDecl *NTTP,
129 Expr *Value,
130 Sema::TemplateDeductionInfo &Info,
131 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000132 assert(NTTP->getDepth() == 0 &&
133 "Cannot deduce non-type template argument with depth > 0");
134 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
135 "Expression template argument must be type- or value-dependent.");
136
137 if (Deduced[NTTP->getIndex()].isNull()) {
138 // FIXME: Clone the Value?
139 Deduced[NTTP->getIndex()] = TemplateArgument(Value);
Douglas Gregor623e2e02009-06-12 18:26:56 +0000140 return Sema::TDK_Success;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000141 }
142
143 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
144 // Okay, we deduced a constant in one case and a dependent expression
145 // in another case. FIXME: Later, we will check that instantiating the
146 // dependent expression gives us the constant value.
Douglas Gregor623e2e02009-06-12 18:26:56 +0000147 return Sema::TDK_Success;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000148 }
149
150 // FIXME: Compare the expressions for equality!
Douglas Gregor623e2e02009-06-12 18:26:56 +0000151 return Sema::TDK_Success;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000152}
153
Douglas Gregor623e2e02009-06-12 18:26:56 +0000154static Sema::TemplateDeductionResult
155DeduceTemplateArguments(ASTContext &Context,
156 TemplateName Param,
157 TemplateName Arg,
158 Sema::TemplateDeductionInfo &Info,
159 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor5069ae32009-06-09 16:35:58 +0000160 // FIXME: Implement template argument deduction for template
161 // template parameters.
162
Douglas Gregor623e2e02009-06-12 18:26:56 +0000163 // FIXME: this routine does not have enough information to produce
164 // good diagnostics.
165
Douglas Gregor5069ae32009-06-09 16:35:58 +0000166 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
167 TemplateDecl *ArgDecl = Arg.getAsTemplateDecl();
168
Douglas Gregor623e2e02009-06-12 18:26:56 +0000169 if (!ParamDecl || !ArgDecl) {
170 // FIXME: fill in Info.Param/Info.FirstArg
171 return Sema::TDK_Inconsistent;
172 }
Douglas Gregor5069ae32009-06-09 16:35:58 +0000173
174 ParamDecl = cast<TemplateDecl>(Context.getCanonicalDecl(ParamDecl));
175 ArgDecl = cast<TemplateDecl>(Context.getCanonicalDecl(ArgDecl));
Douglas Gregor623e2e02009-06-12 18:26:56 +0000176 if (ParamDecl != ArgDecl) {
177 // FIXME: fill in Info.Param/Info.FirstArg
178 return Sema::TDK_Inconsistent;
179 }
180
181 return Sema::TDK_Success;
Douglas Gregor5069ae32009-06-09 16:35:58 +0000182}
183
Douglas Gregor78b6f562009-06-26 18:27:22 +0000184/// \brief Deduce the template arguments by comparing the parameter type and
185/// the argument type (C++ [temp.deduct.type]).
186///
187/// \param Context the AST context in which this deduction occurs.
188///
189/// \param TemplateParams the template parameters that we are deducing
190///
191/// \param ParamIn the parameter type
192///
193/// \param ArgIn the argument type
194///
195/// \param Info information about the template argument deduction itself
196///
197/// \param Deduced the deduced template arguments
198///
Douglas Gregor61112762009-06-26 23:10:12 +0000199/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
200/// how template argument deduction is performed.
Douglas Gregor78b6f562009-06-26 18:27:22 +0000201///
202/// \returns the result of template argument deduction so far. Note that a
203/// "success" result means that template argument deduction has not yet failed,
204/// but it may still fail, later, for other reasons.
Douglas Gregor623e2e02009-06-12 18:26:56 +0000205static Sema::TemplateDeductionResult
206DeduceTemplateArguments(ASTContext &Context,
207 TemplateParameterList *TemplateParams,
208 QualType ParamIn, QualType ArgIn,
209 Sema::TemplateDeductionInfo &Info,
Douglas Gregor78b6f562009-06-26 18:27:22 +0000210 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
Douglas Gregor61112762009-06-26 23:10:12 +0000211 unsigned TDF) {
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000212 // We only want to look at the canonical types, since typedefs and
213 // sugar are not part of template argument deduction.
Douglas Gregor623e2e02009-06-12 18:26:56 +0000214 QualType Param = Context.getCanonicalType(ParamIn);
215 QualType Arg = Context.getCanonicalType(ArgIn);
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000216
Douglas Gregor78b6f562009-06-26 18:27:22 +0000217 // C++0x [temp.deduct.call]p4 bullet 1:
218 // - If the original P is a reference type, the deduced A (i.e., the type
219 // referred to by the reference) can be more cv-qualified than the
220 // transformed A.
Douglas Gregor61112762009-06-26 23:10:12 +0000221 if (TDF & TDF_ParamWithReferenceType) {
Douglas Gregor78b6f562009-06-26 18:27:22 +0000222 unsigned ExtraQualsOnParam
223 = Param.getCVRQualifiers() & ~Arg.getCVRQualifiers();
224 Param.setCVRQualifiers(Param.getCVRQualifiers() & ~ExtraQualsOnParam);
225 }
226
Douglas Gregorad538e52009-06-26 20:57:09 +0000227 // If the parameter type is not dependent, there is nothing to deduce.
228 if (!Param->isDependentType())
229 return Sema::TDK_Success;
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000230
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000231 // C++ [temp.deduct.type]p9:
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000232 // A template type argument T, a template template argument TT or a
233 // template non-type argument i can be deduced if P and A have one of
234 // the following forms:
235 //
236 // T
237 // cv-list T
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000238 if (const TemplateTypeParmType *TemplateTypeParm
239 = Param->getAsTemplateTypeParmType()) {
Douglas Gregor623e2e02009-06-12 18:26:56 +0000240 unsigned Index = TemplateTypeParm->getIndex();
241
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000242 // The argument type can not be less qualified than the parameter
243 // type.
Douglas Gregor61112762009-06-26 23:10:12 +0000244 if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
Douglas Gregor623e2e02009-06-12 18:26:56 +0000245 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
246 Info.FirstArg = Deduced[Index];
247 Info.SecondArg = TemplateArgument(SourceLocation(), Arg);
248 return Sema::TDK_InconsistentQuals;
249 }
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000250
251 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
252
253 unsigned Quals = Arg.getCVRQualifiers() & ~Param.getCVRQualifiers();
254 QualType DeducedType = Arg.getQualifiedType(Quals);
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000255
256 if (Deduced[Index].isNull())
257 Deduced[Index] = TemplateArgument(SourceLocation(), DeducedType);
258 else {
259 // C++ [temp.deduct.type]p2:
260 // [...] If type deduction cannot be done for any P/A pair, or if for
261 // any pair the deduction leads to more than one possible set of
262 // deduced values, or if different pairs yield different deduced
263 // values, or if any template argument remains neither deduced nor
264 // explicitly specified, template argument deduction fails.
Douglas Gregor623e2e02009-06-12 18:26:56 +0000265 if (Deduced[Index].getAsType() != DeducedType) {
266 Info.Param
267 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
268 Info.FirstArg = Deduced[Index];
269 Info.SecondArg = TemplateArgument(SourceLocation(), Arg);
270 return Sema::TDK_Inconsistent;
271 }
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000272 }
Douglas Gregor623e2e02009-06-12 18:26:56 +0000273 return Sema::TDK_Success;
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000274 }
275
Douglas Gregor623e2e02009-06-12 18:26:56 +0000276 // Set up the template argument deduction information for a failure.
277 Info.FirstArg = TemplateArgument(SourceLocation(), ParamIn);
278 Info.SecondArg = TemplateArgument(SourceLocation(), ArgIn);
279
Douglas Gregor61112762009-06-26 23:10:12 +0000280 // Check the cv-qualifiers on the parameter and argument types.
281 if (!(TDF & TDF_IgnoreQualifiers)) {
282 if (TDF & TDF_ParamWithReferenceType) {
283 if (Param.isMoreQualifiedThan(Arg))
284 return Sema::TDK_NonDeducedMismatch;
285 } else {
286 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
287 return Sema::TDK_NonDeducedMismatch;
288 }
289 }
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000290
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000291 switch (Param->getTypeClass()) {
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000292 // No deduction possible for these types
293 case Type::Builtin:
Douglas Gregor623e2e02009-06-12 18:26:56 +0000294 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000295
296 // T *
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000297 case Type::Pointer: {
298 const PointerType *PointerArg = Arg->getAsPointerType();
299 if (!PointerArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000300 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000301
Douglas Gregor623e2e02009-06-12 18:26:56 +0000302 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000303 cast<PointerType>(Param)->getPointeeType(),
304 PointerArg->getPointeeType(),
Douglas Gregor61112762009-06-26 23:10:12 +0000305 Info, Deduced,
306 TDF & TDF_IgnoreQualifiers);
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000307 }
308
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000309 // T &
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000310 case Type::LValueReference: {
311 const LValueReferenceType *ReferenceArg = Arg->getAsLValueReferenceType();
312 if (!ReferenceArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000313 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000314
Douglas Gregor623e2e02009-06-12 18:26:56 +0000315 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000316 cast<LValueReferenceType>(Param)->getPointeeType(),
317 ReferenceArg->getPointeeType(),
Douglas Gregor61112762009-06-26 23:10:12 +0000318 Info, Deduced, 0);
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000319 }
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000320
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000321 // T && [C++0x]
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000322 case Type::RValueReference: {
323 const RValueReferenceType *ReferenceArg = Arg->getAsRValueReferenceType();
324 if (!ReferenceArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000325 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000326
Douglas Gregor623e2e02009-06-12 18:26:56 +0000327 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000328 cast<RValueReferenceType>(Param)->getPointeeType(),
329 ReferenceArg->getPointeeType(),
Douglas Gregor61112762009-06-26 23:10:12 +0000330 Info, Deduced, 0);
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000331 }
332
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000333 // T [] (implied, but not stated explicitly)
Anders Carlsson0bcee302009-06-04 04:11:30 +0000334 case Type::IncompleteArray: {
335 const IncompleteArrayType *IncompleteArrayArg =
336 Context.getAsIncompleteArrayType(Arg);
337 if (!IncompleteArrayArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000338 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson0bcee302009-06-04 04:11:30 +0000339
Douglas Gregor623e2e02009-06-12 18:26:56 +0000340 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson0bcee302009-06-04 04:11:30 +0000341 Context.getAsIncompleteArrayType(Param)->getElementType(),
342 IncompleteArrayArg->getElementType(),
Douglas Gregor61112762009-06-26 23:10:12 +0000343 Info, Deduced, 0);
Anders Carlsson0bcee302009-06-04 04:11:30 +0000344 }
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000345
346 // T [integer-constant]
Anders Carlsson0bcee302009-06-04 04:11:30 +0000347 case Type::ConstantArray: {
348 const ConstantArrayType *ConstantArrayArg =
349 Context.getAsConstantArrayType(Arg);
350 if (!ConstantArrayArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000351 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson0bcee302009-06-04 04:11:30 +0000352
353 const ConstantArrayType *ConstantArrayParm =
354 Context.getAsConstantArrayType(Param);
355 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregor623e2e02009-06-12 18:26:56 +0000356 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson0bcee302009-06-04 04:11:30 +0000357
Douglas Gregor623e2e02009-06-12 18:26:56 +0000358 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson0bcee302009-06-04 04:11:30 +0000359 ConstantArrayParm->getElementType(),
360 ConstantArrayArg->getElementType(),
Douglas Gregor61112762009-06-26 23:10:12 +0000361 Info, Deduced, 0);
Anders Carlsson0bcee302009-06-04 04:11:30 +0000362 }
363
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000364 // type [i]
365 case Type::DependentSizedArray: {
366 const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg);
367 if (!ArrayArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000368 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000369
370 // Check the element type of the arrays
371 const DependentSizedArrayType *DependentArrayParm
372 = cast<DependentSizedArrayType>(Param);
Douglas Gregor623e2e02009-06-12 18:26:56 +0000373 if (Sema::TemplateDeductionResult Result
374 = DeduceTemplateArguments(Context, TemplateParams,
375 DependentArrayParm->getElementType(),
376 ArrayArg->getElementType(),
Douglas Gregor61112762009-06-26 23:10:12 +0000377 Info, Deduced, 0))
Douglas Gregor623e2e02009-06-12 18:26:56 +0000378 return Result;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000379
380 // Determine the array bound is something we can deduce.
381 NonTypeTemplateParmDecl *NTTP
382 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
383 if (!NTTP)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000384 return Sema::TDK_Success;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000385
386 // We can perform template argument deduction for the given non-type
387 // template parameter.
388 assert(NTTP->getDepth() == 0 &&
389 "Cannot deduce non-type template argument at depth > 0");
390 if (const ConstantArrayType *ConstantArrayArg
Anders Carlssoncfb830e2009-06-16 22:44:31 +0000391 = dyn_cast<ConstantArrayType>(ArrayArg)) {
392 llvm::APSInt Size(ConstantArrayArg->getSize());
393 return DeduceNonTypeTemplateArgument(Context, NTTP, Size,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000394 Info, Deduced);
Anders Carlssoncfb830e2009-06-16 22:44:31 +0000395 }
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000396 if (const DependentSizedArrayType *DependentArrayArg
397 = dyn_cast<DependentSizedArrayType>(ArrayArg))
398 return DeduceNonTypeTemplateArgument(Context, NTTP,
399 DependentArrayArg->getSizeExpr(),
Douglas Gregor623e2e02009-06-12 18:26:56 +0000400 Info, Deduced);
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000401
402 // Incomplete type does not match a dependently-sized array type
Douglas Gregor623e2e02009-06-12 18:26:56 +0000403 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000404 }
405
Douglas Gregor1c67c002009-06-08 15:59:14 +0000406 // type(*)(T)
407 // T(*)()
408 // T(*)(T)
Anders Carlsson8574e7f2009-06-08 15:19:08 +0000409 case Type::FunctionProto: {
410 const FunctionProtoType *FunctionProtoArg =
411 dyn_cast<FunctionProtoType>(Arg);
412 if (!FunctionProtoArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000413 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson8574e7f2009-06-08 15:19:08 +0000414
415 const FunctionProtoType *FunctionProtoParam =
416 cast<FunctionProtoType>(Param);
Anders Carlsson3e39f922009-06-08 19:22:23 +0000417
418 if (FunctionProtoParam->getTypeQuals() !=
419 FunctionProtoArg->getTypeQuals())
Douglas Gregor623e2e02009-06-12 18:26:56 +0000420 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson8574e7f2009-06-08 15:19:08 +0000421
Anders Carlsson3e39f922009-06-08 19:22:23 +0000422 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregor623e2e02009-06-12 18:26:56 +0000423 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson3e39f922009-06-08 19:22:23 +0000424
425 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregor623e2e02009-06-12 18:26:56 +0000426 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson3e39f922009-06-08 19:22:23 +0000427
Anders Carlsson8574e7f2009-06-08 15:19:08 +0000428 // Check return types.
Douglas Gregor623e2e02009-06-12 18:26:56 +0000429 if (Sema::TemplateDeductionResult Result
430 = DeduceTemplateArguments(Context, TemplateParams,
431 FunctionProtoParam->getResultType(),
432 FunctionProtoArg->getResultType(),
Douglas Gregor61112762009-06-26 23:10:12 +0000433 Info, Deduced, 0))
Douglas Gregor623e2e02009-06-12 18:26:56 +0000434 return Result;
Anders Carlsson8574e7f2009-06-08 15:19:08 +0000435
Anders Carlsson8574e7f2009-06-08 15:19:08 +0000436 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
437 // Check argument types.
Douglas Gregor623e2e02009-06-12 18:26:56 +0000438 if (Sema::TemplateDeductionResult Result
439 = DeduceTemplateArguments(Context, TemplateParams,
440 FunctionProtoParam->getArgType(I),
441 FunctionProtoArg->getArgType(I),
Douglas Gregor61112762009-06-26 23:10:12 +0000442 Info, Deduced, 0))
Douglas Gregor623e2e02009-06-12 18:26:56 +0000443 return Result;
Anders Carlsson8574e7f2009-06-08 15:19:08 +0000444 }
445
Douglas Gregor623e2e02009-06-12 18:26:56 +0000446 return Sema::TDK_Success;
Anders Carlsson8574e7f2009-06-08 15:19:08 +0000447 }
Douglas Gregor5069ae32009-06-09 16:35:58 +0000448
Douglas Gregorad538e52009-06-26 20:57:09 +0000449 // template-name<T> (where template-name refers to a class template)
Douglas Gregor5069ae32009-06-09 16:35:58 +0000450 // template-name<i>
451 // TT<T> (TODO)
452 // TT<i> (TODO)
453 // TT<> (TODO)
454 case Type::TemplateSpecialization: {
455 const TemplateSpecializationType *SpecParam
456 = cast<TemplateSpecializationType>(Param);
457
458 // Check whether the template argument is a dependent template-id.
459 // FIXME: This is untested code; it can be tested when we implement
460 // partial ordering of class template partial specializations.
461 if (const TemplateSpecializationType *SpecArg
462 = dyn_cast<TemplateSpecializationType>(Arg)) {
463 // Perform template argument deduction for the template name.
Douglas Gregor623e2e02009-06-12 18:26:56 +0000464 if (Sema::TemplateDeductionResult Result
465 = DeduceTemplateArguments(Context,
466 SpecParam->getTemplateName(),
467 SpecArg->getTemplateName(),
468 Info, Deduced))
469 return Result;
Douglas Gregor5069ae32009-06-09 16:35:58 +0000470
471 unsigned NumArgs = SpecParam->getNumArgs();
472
473 // FIXME: When one of the template-names refers to a
474 // declaration with default template arguments, do we need to
475 // fill in those default template arguments here? Most likely,
476 // the answer is "yes", but I don't see any references. This
477 // issue may be resolved elsewhere, because we may want to
478 // instantiate default template arguments when
479 if (SpecArg->getNumArgs() != NumArgs)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000480 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor5069ae32009-06-09 16:35:58 +0000481
482 // Perform template argument deduction on each template
483 // argument.
484 for (unsigned I = 0; I != NumArgs; ++I)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000485 if (Sema::TemplateDeductionResult Result
486 = DeduceTemplateArguments(Context, TemplateParams,
487 SpecParam->getArg(I),
488 SpecArg->getArg(I),
489 Info, Deduced))
490 return Result;
Douglas Gregor5069ae32009-06-09 16:35:58 +0000491
Douglas Gregor623e2e02009-06-12 18:26:56 +0000492 return Sema::TDK_Success;
Douglas Gregor5069ae32009-06-09 16:35:58 +0000493 }
494
495 // If the argument type is a class template specialization, we
496 // perform template argument deduction using its template
497 // arguments.
498 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
499 if (!RecordArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000500 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor5069ae32009-06-09 16:35:58 +0000501
502 ClassTemplateSpecializationDecl *SpecArg
503 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
504 if (!SpecArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000505 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor5069ae32009-06-09 16:35:58 +0000506
507 // Perform template argument deduction for the template name.
Douglas Gregor623e2e02009-06-12 18:26:56 +0000508 if (Sema::TemplateDeductionResult Result
509 = DeduceTemplateArguments(Context,
510 SpecParam->getTemplateName(),
511 TemplateName(SpecArg->getSpecializedTemplate()),
512 Info, Deduced))
513 return Result;
Douglas Gregor5069ae32009-06-09 16:35:58 +0000514
515 // FIXME: Can the # of arguments in the parameter and the argument differ?
516 unsigned NumArgs = SpecParam->getNumArgs();
517 const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
518 if (NumArgs != ArgArgs.size())
Douglas Gregor623e2e02009-06-12 18:26:56 +0000519 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor5069ae32009-06-09 16:35:58 +0000520
521 for (unsigned I = 0; I != NumArgs; ++I)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000522 if (Sema::TemplateDeductionResult Result
523 = DeduceTemplateArguments(Context, TemplateParams,
524 SpecParam->getArg(I),
525 ArgArgs.get(I),
526 Info, Deduced))
527 return Result;
Anders Carlsson8574e7f2009-06-08 15:19:08 +0000528
Douglas Gregor623e2e02009-06-12 18:26:56 +0000529 return Sema::TDK_Success;
Douglas Gregor5069ae32009-06-09 16:35:58 +0000530 }
531
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000532 // T type::*
533 // T T::*
534 // T (type::*)()
535 // type (T::*)()
536 // type (type::*)(T)
537 // type (T::*)(T)
538 // T (type::*)(T)
539 // T (T::*)()
540 // T (T::*)(T)
541 case Type::MemberPointer: {
542 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
543 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
544 if (!MemPtrArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000545 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000546
Douglas Gregor623e2e02009-06-12 18:26:56 +0000547 if (Sema::TemplateDeductionResult Result
548 = DeduceTemplateArguments(Context, TemplateParams,
549 MemPtrParam->getPointeeType(),
550 MemPtrArg->getPointeeType(),
Douglas Gregor61112762009-06-26 23:10:12 +0000551 Info, Deduced,
552 TDF & TDF_IgnoreQualifiers))
Douglas Gregor623e2e02009-06-12 18:26:56 +0000553 return Result;
554
555 return DeduceTemplateArguments(Context, TemplateParams,
556 QualType(MemPtrParam->getClass(), 0),
557 QualType(MemPtrArg->getClass(), 0),
Douglas Gregor61112762009-06-26 23:10:12 +0000558 Info, Deduced, 0);
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000559 }
560
Anders Carlssondb52d342009-06-12 22:56:54 +0000561 // (clang extension)
562 //
Anders Carlsson9d914d32009-06-12 16:23:10 +0000563 // type(^)(T)
564 // T(^)()
565 // T(^)(T)
566 case Type::BlockPointer: {
567 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
568 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
569
570 if (!BlockPtrArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000571 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson9d914d32009-06-12 16:23:10 +0000572
Douglas Gregor623e2e02009-06-12 18:26:56 +0000573 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson9d914d32009-06-12 16:23:10 +0000574 BlockPtrParam->getPointeeType(),
Douglas Gregor623e2e02009-06-12 18:26:56 +0000575 BlockPtrArg->getPointeeType(), Info,
Douglas Gregor61112762009-06-26 23:10:12 +0000576 Deduced, 0);
Anders Carlsson9d914d32009-06-12 16:23:10 +0000577 }
578
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000579 case Type::TypeOfExpr:
580 case Type::TypeOf:
581 case Type::Typename:
582 // No template argument deduction for these types
Douglas Gregor623e2e02009-06-12 18:26:56 +0000583 return Sema::TDK_Success;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000584
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000585 default:
586 break;
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000587 }
588
589 // FIXME: Many more cases to go (to go).
Douglas Gregorad538e52009-06-26 20:57:09 +0000590 return Sema::TDK_Success;
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000591}
592
Douglas Gregor623e2e02009-06-12 18:26:56 +0000593static Sema::TemplateDeductionResult
594DeduceTemplateArguments(ASTContext &Context,
595 TemplateParameterList *TemplateParams,
596 const TemplateArgument &Param,
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000597 const TemplateArgument &Arg,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000598 Sema::TemplateDeductionInfo &Info,
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000599 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000600 switch (Param.getKind()) {
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000601 case TemplateArgument::Null:
602 assert(false && "Null template argument in parameter list");
603 break;
604
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000605 case TemplateArgument::Type:
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000606 assert(Arg.getKind() == TemplateArgument::Type && "Type/value mismatch");
Douglas Gregor61112762009-06-26 23:10:12 +0000607 return DeduceTemplateArguments(Context, TemplateParams, Param.getAsType(),
608 Arg.getAsType(), Info, Deduced, 0);
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000609
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000610 case TemplateArgument::Declaration:
611 // FIXME: Implement this check
612 assert(false && "Unimplemented template argument deduction case");
Douglas Gregor623e2e02009-06-12 18:26:56 +0000613 Info.FirstArg = Param;
614 Info.SecondArg = Arg;
615 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000616
617 case TemplateArgument::Integral:
618 if (Arg.getKind() == TemplateArgument::Integral) {
619 // FIXME: Zero extension + sign checking here?
Douglas Gregor623e2e02009-06-12 18:26:56 +0000620 if (*Param.getAsIntegral() == *Arg.getAsIntegral())
621 return Sema::TDK_Success;
622
623 Info.FirstArg = Param;
624 Info.SecondArg = Arg;
625 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000626 }
Douglas Gregor623e2e02009-06-12 18:26:56 +0000627
628 if (Arg.getKind() == TemplateArgument::Expression) {
629 Info.FirstArg = Param;
630 Info.SecondArg = Arg;
631 return Sema::TDK_NonDeducedMismatch;
632 }
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000633
634 assert(false && "Type/value mismatch");
Douglas Gregor623e2e02009-06-12 18:26:56 +0000635 Info.FirstArg = Param;
636 Info.SecondArg = Arg;
637 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000638
639 case TemplateArgument::Expression: {
640 if (NonTypeTemplateParmDecl *NTTP
641 = getDeducedParameterFromExpr(Param.getAsExpr())) {
642 if (Arg.getKind() == TemplateArgument::Integral)
643 // FIXME: Sign problems here
644 return DeduceNonTypeTemplateArgument(Context, NTTP,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000645 *Arg.getAsIntegral(),
646 Info, Deduced);
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000647 if (Arg.getKind() == TemplateArgument::Expression)
648 return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(),
Douglas Gregor623e2e02009-06-12 18:26:56 +0000649 Info, Deduced);
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000650
651 assert(false && "Type/value mismatch");
Douglas Gregor623e2e02009-06-12 18:26:56 +0000652 Info.FirstArg = Param;
653 Info.SecondArg = Arg;
654 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000655 }
656
657 // Can't deduce anything, but that's okay.
Douglas Gregor623e2e02009-06-12 18:26:56 +0000658 return Sema::TDK_Success;
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000659 }
Anders Carlsson584b5062009-06-15 17:04:53 +0000660 case TemplateArgument::Pack:
661 assert(0 && "FIXME: Implement!");
662 break;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000663 }
664
Douglas Gregor623e2e02009-06-12 18:26:56 +0000665 return Sema::TDK_Success;
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000666}
667
Douglas Gregor623e2e02009-06-12 18:26:56 +0000668static Sema::TemplateDeductionResult
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000669DeduceTemplateArguments(ASTContext &Context,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000670 TemplateParameterList *TemplateParams,
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000671 const TemplateArgumentList &ParamList,
672 const TemplateArgumentList &ArgList,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000673 Sema::TemplateDeductionInfo &Info,
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000674 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
675 assert(ParamList.size() == ArgList.size());
676 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregor623e2e02009-06-12 18:26:56 +0000677 if (Sema::TemplateDeductionResult Result
678 = DeduceTemplateArguments(Context, TemplateParams,
679 ParamList[I], ArgList[I],
680 Info, Deduced))
681 return Result;
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000682 }
Douglas Gregor623e2e02009-06-12 18:26:56 +0000683 return Sema::TDK_Success;
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000684}
685
Douglas Gregorad538e52009-06-26 20:57:09 +0000686/// \brief Determine whether two template arguments are the same.
687static bool isSameTemplateArg(ASTContext &Context,
688 const TemplateArgument &X,
689 const TemplateArgument &Y) {
690 if (X.getKind() != Y.getKind())
691 return false;
692
693 switch (X.getKind()) {
694 case TemplateArgument::Null:
695 assert(false && "Comparing NULL template argument");
696 break;
697
698 case TemplateArgument::Type:
699 return Context.getCanonicalType(X.getAsType()) ==
700 Context.getCanonicalType(Y.getAsType());
701
702 case TemplateArgument::Declaration:
703 return Context.getCanonicalDecl(X.getAsDecl()) ==
704 Context.getCanonicalDecl(Y.getAsDecl());
705
706 case TemplateArgument::Integral:
707 return *X.getAsIntegral() == *Y.getAsIntegral();
708
709 case TemplateArgument::Expression:
710 // FIXME: We assume that all expressions are distinct, but we should
711 // really check their canonical forms.
712 return false;
713
714 case TemplateArgument::Pack:
715 if (X.pack_size() != Y.pack_size())
716 return false;
717
718 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
719 XPEnd = X.pack_end(),
720 YP = Y.pack_begin();
721 XP != XPEnd; ++XP, ++YP)
722 if (!isSameTemplateArg(Context, *XP, *YP))
723 return false;
724
725 return true;
726 }
727
728 return false;
729}
730
731/// \brief Helper function to build a TemplateParameter when we don't
732/// know its type statically.
733static TemplateParameter makeTemplateParameter(Decl *D) {
734 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
735 return TemplateParameter(TTP);
736 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
737 return TemplateParameter(NTTP);
738
739 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
740}
741
Douglas Gregor21530c52009-06-12 22:31:52 +0000742/// \brief Perform template argument deduction to determine whether
743/// the given template arguments match the given class template
744/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregor623e2e02009-06-12 18:26:56 +0000745Sema::TemplateDeductionResult
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000746Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000747 const TemplateArgumentList &TemplateArgs,
748 TemplateDeductionInfo &Info) {
Douglas Gregor21530c52009-06-12 22:31:52 +0000749 // C++ [temp.class.spec.match]p2:
750 // A partial specialization matches a given actual template
751 // argument list if the template arguments of the partial
752 // specialization can be deduced from the actual template argument
753 // list (14.8.2).
Douglas Gregor003d7402009-06-14 08:02:22 +0000754 SFINAETrap Trap(*this);
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000755 llvm::SmallVector<TemplateArgument, 4> Deduced;
756 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregor623e2e02009-06-12 18:26:56 +0000757 if (TemplateDeductionResult Result
758 = ::DeduceTemplateArguments(Context,
759 Partial->getTemplateParameters(),
760 Partial->getTemplateArgs(),
761 TemplateArgs, Info, Deduced))
762 return Result;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000763
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000764 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
765 Deduced.data(), Deduced.size());
766 if (Inst)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000767 return TDK_InstantiationDepth;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000768
Douglas Gregor8f378a92009-06-11 18:10:32 +0000769 // C++ [temp.deduct.type]p2:
770 // [...] or if any template argument remains neither deduced nor
771 // explicitly specified, template argument deduction fails.
Anders Carlssonb0fc9992009-06-23 01:26:57 +0000772 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
773 Deduced.size());
Douglas Gregor8f378a92009-06-11 18:10:32 +0000774 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregor623e2e02009-06-12 18:26:56 +0000775 if (Deduced[I].isNull()) {
776 Decl *Param
777 = const_cast<Decl *>(Partial->getTemplateParameters()->getParam(I));
778 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
779 Info.Param = TTP;
780 else if (NonTypeTemplateParmDecl *NTTP
781 = dyn_cast<NonTypeTemplateParmDecl>(Param))
782 Info.Param = NTTP;
783 else
784 Info.Param = cast<TemplateTemplateParmDecl>(Param);
785 return TDK_Incomplete;
786 }
Douglas Gregor8f378a92009-06-11 18:10:32 +0000787
Anders Carlssonb0fc9992009-06-23 01:26:57 +0000788 Builder.Append(Deduced[I]);
Douglas Gregor8f378a92009-06-11 18:10:32 +0000789 }
790
791 // Form the template argument list from the deduced template arguments.
792 TemplateArgumentList *DeducedArgumentList
Anders Carlssonb0fc9992009-06-23 01:26:57 +0000793 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
Douglas Gregor623e2e02009-06-12 18:26:56 +0000794 Info.reset(DeducedArgumentList);
Douglas Gregor8f378a92009-06-11 18:10:32 +0000795
Douglas Gregor8f378a92009-06-11 18:10:32 +0000796 // Substitute the deduced template arguments into the template
797 // arguments of the class template partial specialization, and
798 // verify that the instantiated template arguments are both valid
799 // and are equivalent to the template arguments originally provided
Douglas Gregor7573b3b2009-06-13 00:59:32 +0000800 // to the class template.
Douglas Gregor8f378a92009-06-11 18:10:32 +0000801 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
802 const TemplateArgumentList &PartialTemplateArgs = Partial->getTemplateArgs();
803 for (unsigned I = 0, N = PartialTemplateArgs.flat_size(); I != N; ++I) {
Douglas Gregor7573b3b2009-06-13 00:59:32 +0000804 Decl *Param = const_cast<Decl *>(
805 ClassTemplate->getTemplateParameters()->getParam(I));
Douglas Gregorad538e52009-06-26 20:57:09 +0000806 TemplateArgument InstArg = Instantiate(PartialTemplateArgs[I],
807 *DeducedArgumentList);
808 if (InstArg.isNull()) {
809 Info.Param = makeTemplateParameter(Param);
810 Info.FirstArg = PartialTemplateArgs[I];
811 return TDK_SubstitutionFailure;
Douglas Gregor8f378a92009-06-11 18:10:32 +0000812 }
Douglas Gregorad538e52009-06-26 20:57:09 +0000813
814 if (InstArg.getKind() == TemplateArgument::Expression) {
815 // When the argument is an expression, check the expression result
816 // against the actual template parameter to get down to the canonical
817 // template argument.
818 Expr *InstExpr = InstArg.getAsExpr();
819 if (NonTypeTemplateParmDecl *NTTP
820 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
821 if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
822 Info.Param = makeTemplateParameter(Param);
823 Info.FirstArg = PartialTemplateArgs[I];
824 return TDK_SubstitutionFailure;
825 }
826 } else if (TemplateTemplateParmDecl *TTP
827 = dyn_cast<TemplateTemplateParmDecl>(Param)) {
828 // FIXME: template template arguments should really resolve to decls
829 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InstExpr);
830 if (!DRE || CheckTemplateArgument(TTP, DRE)) {
831 Info.Param = makeTemplateParameter(Param);
832 Info.FirstArg = PartialTemplateArgs[I];
833 return TDK_SubstitutionFailure;
834 }
835 }
836 }
837
838 if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) {
839 Info.Param = makeTemplateParameter(Param);
840 Info.FirstArg = TemplateArgs[I];
841 Info.SecondArg = InstArg;
842 return TDK_NonDeducedMismatch;
843 }
Douglas Gregor8f378a92009-06-11 18:10:32 +0000844 }
845
Douglas Gregor003d7402009-06-14 08:02:22 +0000846 if (Trap.hasErrorOccurred())
847 return TDK_SubstitutionFailure;
848
Douglas Gregor623e2e02009-06-12 18:26:56 +0000849 return TDK_Success;
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000850}
Douglas Gregorf90c2132009-06-13 00:26:55 +0000851
Douglas Gregorb60eb752009-06-25 22:08:12 +0000852/// \brief Perform template argument deduction from a function call
853/// (C++ [temp.deduct.call]).
854///
855/// \param FunctionTemplate the function template for which we are performing
856/// template argument deduction.
857///
858/// \param Args the function call arguments
859///
860/// \param NumArgs the number of arguments in Args
861///
862/// \param Specialization if template argument deduction was successful,
863/// this will be set to the function template specialization produced by
864/// template argument deduction.
865///
866/// \param Info the argument will be updated to provide additional information
867/// about template argument deduction.
868///
869/// \returns the result of template argument deduction.
870///
871/// FIXME: We will also need to pass in any explicitly-specified template
872/// arguments.
873Sema::TemplateDeductionResult
874Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
875 Expr **Args, unsigned NumArgs,
876 FunctionDecl *&Specialization,
877 TemplateDeductionInfo &Info) {
878 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
879
880 // C++ [temp.deduct.call]p1:
881 // Template argument deduction is done by comparing each function template
882 // parameter type (call it P) with the type of the corresponding argument
883 // of the call (call it A) as described below.
884 unsigned CheckArgs = NumArgs;
885 if (NumArgs < Function->getNumParams())
886 return TDK_TooFewArguments;
887 else if (NumArgs > Function->getNumParams()) {
888 const FunctionProtoType *Proto
889 = Function->getType()->getAsFunctionProtoType();
890 if (!Proto->isVariadic())
891 return TDK_TooManyArguments;
892
893 CheckArgs = Function->getNumParams();
894 }
895
Douglas Gregor78b6f562009-06-26 18:27:22 +0000896 // Template argument deduction for function templates in a SFINAE context.
897 // Trap any errors that might occur.
Douglas Gregorb60eb752009-06-25 22:08:12 +0000898 SFINAETrap Trap(*this);
Douglas Gregor78b6f562009-06-26 18:27:22 +0000899
900 // Deduce template arguments from the function parameters.
Douglas Gregorb60eb752009-06-25 22:08:12 +0000901 llvm::SmallVector<TemplateArgument, 4> Deduced;
902 Deduced.resize(FunctionTemplate->getTemplateParameters()->size());
903 TemplateParameterList *TemplateParams
904 = FunctionTemplate->getTemplateParameters();
905 for (unsigned I = 0; I != CheckArgs; ++I) {
906 QualType ParamType = Function->getParamDecl(I)->getType();
907 QualType ArgType = Args[I]->getType();
Douglas Gregor78b6f562009-06-26 18:27:22 +0000908
Douglas Gregorb60eb752009-06-25 22:08:12 +0000909 // C++ [temp.deduct.call]p2:
910 // If P is not a reference type:
911 QualType CanonParamType = Context.getCanonicalType(ParamType);
Douglas Gregor78b6f562009-06-26 18:27:22 +0000912 bool ParamWasReference = isa<ReferenceType>(CanonParamType);
913 if (!ParamWasReference) {
Douglas Gregorb60eb752009-06-25 22:08:12 +0000914 // - If A is an array type, the pointer type produced by the
915 // array-to-pointer standard conversion (4.2) is used in place of
916 // A for type deduction; otherwise,
917 if (ArgType->isArrayType())
918 ArgType = Context.getArrayDecayedType(ArgType);
919 // - If A is a function type, the pointer type produced by the
920 // function-to-pointer standard conversion (4.3) is used in place
921 // of A for type deduction; otherwise,
922 else if (ArgType->isFunctionType())
923 ArgType = Context.getPointerType(ArgType);
924 else {
925 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
926 // type are ignored for type deduction.
927 QualType CanonArgType = Context.getCanonicalType(ArgType);
928 if (CanonArgType.getCVRQualifiers())
929 ArgType = CanonArgType.getUnqualifiedType();
930 }
931 }
932
933 // C++0x [temp.deduct.call]p3:
934 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
935 // are ignored for type deduction.
936 if (CanonParamType.getCVRQualifiers())
937 ParamType = CanonParamType.getUnqualifiedType();
938 if (const ReferenceType *ParamRefType = ParamType->getAsReferenceType()) {
939 // [...] If P is a reference type, the type referred to by P is used
940 // for type deduction.
941 ParamType = ParamRefType->getPointeeType();
942
943 // [...] If P is of the form T&&, where T is a template parameter, and
944 // the argument is an lvalue, the type A& is used in place of A for
945 // type deduction.
946 if (isa<RValueReferenceType>(ParamRefType) &&
947 ParamRefType->getAsTemplateTypeParmType() &&
948 Args[I]->isLvalue(Context) == Expr::LV_Valid)
949 ArgType = Context.getLValueReferenceType(ArgType);
950 }
951
952 // C++0x [temp.deduct.call]p4:
953 // In general, the deduction process attempts to find template argument
954 // values that will make the deduced A identical to A (after the type A
955 // is transformed as described above). [...]
Douglas Gregor61112762009-06-26 23:10:12 +0000956 unsigned TDF = 0;
957
958 // - If the original P is a reference type, the deduced A (i.e., the
959 // type referred to by the reference) can be more cv-qualified than
960 // the transformed A.
961 if (ParamWasReference)
962 TDF |= TDF_ParamWithReferenceType;
963 // - The transformed A can be another pointer or pointer to member
964 // type that can be converted to the deduced A via a qualification
965 // conversion (4.4).
966 if (ArgType->isPointerType() || ArgType->isMemberPointerType())
967 TDF |= TDF_IgnoreQualifiers;
968 // FIXME: derived -> base checks
Douglas Gregorb60eb752009-06-25 22:08:12 +0000969 if (TemplateDeductionResult Result
970 = ::DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor78b6f562009-06-26 18:27:22 +0000971 ParamType, ArgType, Info, Deduced,
Douglas Gregor61112762009-06-26 23:10:12 +0000972 TDF))
Douglas Gregorb60eb752009-06-25 22:08:12 +0000973 return Result;
974
975 // FIXME: C++ [temp.deduct.call] paragraphs 6-9 deal with function
976 // pointer parameters.
977 }
978
979 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
980 FunctionTemplate, Deduced.data(), Deduced.size());
981 if (Inst)
982 return TDK_InstantiationDepth;
983
984 // C++ [temp.deduct.type]p2:
985 // [...] or if any template argument remains neither deduced nor
986 // explicitly specified, template argument deduction fails.
987 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
988 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
989 if (Deduced[I].isNull()) {
990 Decl *Param
991 = const_cast<Decl *>(TemplateParams->getParam(I));
992 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
993 Info.Param = TTP;
994 else if (NonTypeTemplateParmDecl *NTTP
995 = dyn_cast<NonTypeTemplateParmDecl>(Param))
996 Info.Param = NTTP;
997 else
998 Info.Param = cast<TemplateTemplateParmDecl>(Param);
999 return TDK_Incomplete;
1000 }
1001
1002 Builder.Append(Deduced[I]);
1003 }
1004
1005 // Form the template argument list from the deduced template arguments.
1006 TemplateArgumentList *DeducedArgumentList
1007 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1008 Info.reset(DeducedArgumentList);
1009
1010 // Substitute the deduced template arguments into the function template
1011 // declaration to produce the function template specialization.
1012 Specialization = cast_or_null<FunctionDecl>(
1013 InstantiateDecl(FunctionTemplate->getTemplatedDecl(),
1014 FunctionTemplate->getDeclContext(),
1015 *DeducedArgumentList));
1016
1017 if (!Specialization || Trap.hasErrorOccurred())
1018 return TDK_SubstitutionFailure;
1019
Douglas Gregor6f5e0542009-06-26 00:10:03 +00001020 // Turn the specialization into an actual function template specialization.
1021 Specialization->setFunctionTemplateSpecialization(Context,
1022 FunctionTemplate,
1023 Info.take());
Douglas Gregorb60eb752009-06-25 22:08:12 +00001024 return TDK_Success;
1025}
1026
Douglas Gregorf90c2132009-06-13 00:26:55 +00001027static void
1028MarkDeducedTemplateParameters(Sema &SemaRef,
1029 const TemplateArgument &TemplateArg,
1030 llvm::SmallVectorImpl<bool> &Deduced);
1031
1032/// \brief Mark the template arguments that are deduced by the given
1033/// expression.
1034static void
Douglas Gregor68a7a6d2009-06-18 18:45:36 +00001035MarkDeducedTemplateParameters(const Expr *E,
1036 llvm::SmallVectorImpl<bool> &Deduced) {
1037 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregorf90c2132009-06-13 00:26:55 +00001038 if (!E)
1039 return;
1040
Douglas Gregor68a7a6d2009-06-18 18:45:36 +00001041 const NonTypeTemplateParmDecl *NTTP
Douglas Gregorf90c2132009-06-13 00:26:55 +00001042 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
1043 if (!NTTP)
1044 return;
1045
1046 Deduced[NTTP->getIndex()] = true;
1047}
1048
1049/// \brief Mark the template parameters that are deduced by the given
1050/// type.
1051static void
1052MarkDeducedTemplateParameters(Sema &SemaRef, QualType T,
1053 llvm::SmallVectorImpl<bool> &Deduced) {
1054 // Non-dependent types have nothing deducible
1055 if (!T->isDependentType())
1056 return;
1057
1058 T = SemaRef.Context.getCanonicalType(T);
1059 switch (T->getTypeClass()) {
1060 case Type::ExtQual:
1061 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregor68a7a6d2009-06-18 18:45:36 +00001062 QualType(cast<ExtQualType>(T)->getBaseType(), 0),
Douglas Gregorf90c2132009-06-13 00:26:55 +00001063 Deduced);
1064 break;
1065
1066 case Type::Pointer:
1067 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregor68a7a6d2009-06-18 18:45:36 +00001068 cast<PointerType>(T)->getPointeeType(),
Douglas Gregorf90c2132009-06-13 00:26:55 +00001069 Deduced);
1070 break;
1071
1072 case Type::BlockPointer:
1073 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregor68a7a6d2009-06-18 18:45:36 +00001074 cast<BlockPointerType>(T)->getPointeeType(),
Douglas Gregorf90c2132009-06-13 00:26:55 +00001075 Deduced);
1076 break;
1077
1078 case Type::LValueReference:
1079 case Type::RValueReference:
1080 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregor68a7a6d2009-06-18 18:45:36 +00001081 cast<ReferenceType>(T)->getPointeeType(),
Douglas Gregorf90c2132009-06-13 00:26:55 +00001082 Deduced);
1083 break;
1084
1085 case Type::MemberPointer: {
1086 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
1087 MarkDeducedTemplateParameters(SemaRef, MemPtr->getPointeeType(), Deduced);
1088 MarkDeducedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
1089 Deduced);
1090 break;
1091 }
1092
1093 case Type::DependentSizedArray:
Douglas Gregor68a7a6d2009-06-18 18:45:36 +00001094 MarkDeducedTemplateParameters(cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregorf90c2132009-06-13 00:26:55 +00001095 Deduced);
1096 // Fall through to check the element type
1097
1098 case Type::ConstantArray:
1099 case Type::IncompleteArray:
1100 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregor68a7a6d2009-06-18 18:45:36 +00001101 cast<ArrayType>(T)->getElementType(),
Douglas Gregorf90c2132009-06-13 00:26:55 +00001102 Deduced);
1103 break;
1104
1105 case Type::Vector:
1106 case Type::ExtVector:
1107 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregor68a7a6d2009-06-18 18:45:36 +00001108 cast<VectorType>(T)->getElementType(),
Douglas Gregorf90c2132009-06-13 00:26:55 +00001109 Deduced);
1110 break;
1111
Douglas Gregor2a2e0402009-06-17 21:51:59 +00001112 case Type::DependentSizedExtVector: {
1113 const DependentSizedExtVectorType *VecType
Douglas Gregor68a7a6d2009-06-18 18:45:36 +00001114 = cast<DependentSizedExtVectorType>(T);
Douglas Gregor2a2e0402009-06-17 21:51:59 +00001115 MarkDeducedTemplateParameters(SemaRef, VecType->getElementType(), Deduced);
1116 MarkDeducedTemplateParameters(VecType->getSizeExpr(), Deduced);
1117 break;
1118 }
1119
Douglas Gregorf90c2132009-06-13 00:26:55 +00001120 case Type::FunctionProto: {
Douglas Gregor68a7a6d2009-06-18 18:45:36 +00001121 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregorf90c2132009-06-13 00:26:55 +00001122 MarkDeducedTemplateParameters(SemaRef, Proto->getResultType(), Deduced);
1123 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
1124 MarkDeducedTemplateParameters(SemaRef, Proto->getArgType(I), Deduced);
1125 break;
1126 }
1127
1128 case Type::TemplateTypeParm:
Douglas Gregor68a7a6d2009-06-18 18:45:36 +00001129 Deduced[cast<TemplateTypeParmType>(T)->getIndex()] = true;
Douglas Gregorf90c2132009-06-13 00:26:55 +00001130 break;
1131
1132 case Type::TemplateSpecialization: {
1133 const TemplateSpecializationType *Spec
Douglas Gregor68a7a6d2009-06-18 18:45:36 +00001134 = cast<TemplateSpecializationType>(T);
Douglas Gregorf90c2132009-06-13 00:26:55 +00001135 if (TemplateDecl *Template = Spec->getTemplateName().getAsTemplateDecl())
1136 if (TemplateTemplateParmDecl *TTP
1137 = dyn_cast<TemplateTemplateParmDecl>(Template))
1138 Deduced[TTP->getIndex()] = true;
1139
1140 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
1141 MarkDeducedTemplateParameters(SemaRef, Spec->getArg(I), Deduced);
1142
1143 break;
1144 }
1145
1146 // None of these types have any deducible parts.
1147 case Type::Builtin:
1148 case Type::FixedWidthInt:
1149 case Type::Complex:
1150 case Type::VariableArray:
1151 case Type::FunctionNoProto:
1152 case Type::Record:
1153 case Type::Enum:
1154 case Type::Typename:
1155 case Type::ObjCInterface:
1156 case Type::ObjCQualifiedInterface:
Steve Naroffc75c1a82009-06-17 22:40:22 +00001157 case Type::ObjCObjectPointer:
Douglas Gregorf90c2132009-06-13 00:26:55 +00001158#define TYPE(Class, Base)
1159#define ABSTRACT_TYPE(Class, Base)
1160#define DEPENDENT_TYPE(Class, Base)
1161#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
1162#include "clang/AST/TypeNodes.def"
1163 break;
1164 }
1165}
1166
1167/// \brief Mark the template parameters that are deduced by this
1168/// template argument.
1169static void
1170MarkDeducedTemplateParameters(Sema &SemaRef,
1171 const TemplateArgument &TemplateArg,
1172 llvm::SmallVectorImpl<bool> &Deduced) {
1173 switch (TemplateArg.getKind()) {
1174 case TemplateArgument::Null:
1175 case TemplateArgument::Integral:
1176 break;
1177
1178 case TemplateArgument::Type:
1179 MarkDeducedTemplateParameters(SemaRef, TemplateArg.getAsType(), Deduced);
1180 break;
1181
1182 case TemplateArgument::Declaration:
1183 if (TemplateTemplateParmDecl *TTP
1184 = dyn_cast<TemplateTemplateParmDecl>(TemplateArg.getAsDecl()))
1185 Deduced[TTP->getIndex()] = true;
1186 break;
1187
1188 case TemplateArgument::Expression:
1189 MarkDeducedTemplateParameters(TemplateArg.getAsExpr(), Deduced);
1190 break;
Anders Carlsson584b5062009-06-15 17:04:53 +00001191 case TemplateArgument::Pack:
1192 assert(0 && "FIXME: Implement!");
1193 break;
Douglas Gregorf90c2132009-06-13 00:26:55 +00001194 }
1195}
1196
1197/// \brief Mark the template parameters can be deduced by the given
1198/// template argument list.
1199///
1200/// \param TemplateArgs the template argument list from which template
1201/// parameters will be deduced.
1202///
1203/// \param Deduced a bit vector whose elements will be set to \c true
1204/// to indicate when the corresponding template parameter will be
1205/// deduced.
1206void
1207Sema::MarkDeducedTemplateParameters(const TemplateArgumentList &TemplateArgs,
1208 llvm::SmallVectorImpl<bool> &Deduced) {
1209 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
1210 ::MarkDeducedTemplateParameters(*this, TemplateArgs[I], Deduced);
1211}