blob: bad9e7cf61cc7c4995bfad046024e2ac8856429f [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"
21using namespace clang;
22
Douglas Gregorf67875d2009-06-12 18:26:56 +000023static Sema::TemplateDeductionResult
24DeduceTemplateArguments(ASTContext &Context,
25 TemplateParameterList *TemplateParams,
26 const TemplateArgument &Param,
Douglas Gregord708c722009-06-09 16:35:58 +000027 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +000028 Sema::TemplateDeductionInfo &Info,
Douglas Gregord708c722009-06-09 16:35:58 +000029 llvm::SmallVectorImpl<TemplateArgument> &Deduced);
30
Douglas Gregor199d9912009-06-05 00:53:49 +000031/// \brief If the given expression is of a form that permits the deduction
32/// of a non-type template parameter, return the declaration of that
33/// non-type template parameter.
34static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
35 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
36 E = IC->getSubExpr();
37
38 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
39 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
40
41 return 0;
42}
43
44/// \brief Deduce the value of the given non-type template parameter
45/// from the given constant.
Douglas Gregorf67875d2009-06-12 18:26:56 +000046static Sema::TemplateDeductionResult
47DeduceNonTypeTemplateArgument(ASTContext &Context,
48 NonTypeTemplateParmDecl *NTTP,
Anders Carlsson335e24a2009-06-16 22:44:31 +000049 llvm::APSInt Value,
Douglas Gregorf67875d2009-06-12 18:26:56 +000050 Sema::TemplateDeductionInfo &Info,
51 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor199d9912009-06-05 00:53:49 +000052 assert(NTTP->getDepth() == 0 &&
53 "Cannot deduce non-type template argument with depth > 0");
54
55 if (Deduced[NTTP->getIndex()].isNull()) {
Anders Carlsson25af1ed2009-06-16 23:08:29 +000056 QualType T = NTTP->getType();
57
58 // FIXME: Make sure we didn't overflow our data type!
59 unsigned AllowedBits = Context.getTypeSize(T);
60 if (Value.getBitWidth() != AllowedBits)
61 Value.extOrTrunc(AllowedBits);
62 Value.setIsSigned(T->isSignedIntegerType());
63
64 Deduced[NTTP->getIndex()] = TemplateArgument(SourceLocation(), Value, T);
Douglas Gregorf67875d2009-06-12 18:26:56 +000065 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +000066 }
67
Douglas Gregorf67875d2009-06-12 18:26:56 +000068 assert(Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral);
Douglas Gregor199d9912009-06-05 00:53:49 +000069
70 // If the template argument was previously deduced to a negative value,
71 // then our deduction fails.
72 const llvm::APSInt *PrevValuePtr = Deduced[NTTP->getIndex()].getAsIntegral();
Anders Carlsson335e24a2009-06-16 22:44:31 +000073 if (PrevValuePtr->isNegative()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +000074 Info.Param = NTTP;
75 Info.FirstArg = Deduced[NTTP->getIndex()];
Anders Carlsson335e24a2009-06-16 22:44:31 +000076 Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType());
Douglas Gregorf67875d2009-06-12 18:26:56 +000077 return Sema::TDK_Inconsistent;
78 }
79
Anders Carlsson335e24a2009-06-16 22:44:31 +000080 llvm::APSInt PrevValue = *PrevValuePtr;
Douglas Gregor199d9912009-06-05 00:53:49 +000081 if (Value.getBitWidth() > PrevValue.getBitWidth())
82 PrevValue.zext(Value.getBitWidth());
83 else if (Value.getBitWidth() < PrevValue.getBitWidth())
84 Value.zext(PrevValue.getBitWidth());
Douglas Gregorf67875d2009-06-12 18:26:56 +000085
86 if (Value != PrevValue) {
87 Info.Param = NTTP;
88 Info.FirstArg = Deduced[NTTP->getIndex()];
Anders Carlsson335e24a2009-06-16 22:44:31 +000089 Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType());
Douglas Gregorf67875d2009-06-12 18:26:56 +000090 return Sema::TDK_Inconsistent;
91 }
92
93 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +000094}
95
96/// \brief Deduce the value of the given non-type template parameter
97/// from the given type- or value-dependent expression.
98///
99/// \returns true if deduction succeeded, false otherwise.
100
Douglas Gregorf67875d2009-06-12 18:26:56 +0000101static Sema::TemplateDeductionResult
102DeduceNonTypeTemplateArgument(ASTContext &Context,
103 NonTypeTemplateParmDecl *NTTP,
104 Expr *Value,
105 Sema::TemplateDeductionInfo &Info,
106 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000107 assert(NTTP->getDepth() == 0 &&
108 "Cannot deduce non-type template argument with depth > 0");
109 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
110 "Expression template argument must be type- or value-dependent.");
111
112 if (Deduced[NTTP->getIndex()].isNull()) {
113 // FIXME: Clone the Value?
114 Deduced[NTTP->getIndex()] = TemplateArgument(Value);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000115 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000116 }
117
118 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
119 // Okay, we deduced a constant in one case and a dependent expression
120 // in another case. FIXME: Later, we will check that instantiating the
121 // dependent expression gives us the constant value.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000122 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000123 }
124
125 // FIXME: Compare the expressions for equality!
Douglas Gregorf67875d2009-06-12 18:26:56 +0000126 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000127}
128
Douglas Gregorf67875d2009-06-12 18:26:56 +0000129static Sema::TemplateDeductionResult
130DeduceTemplateArguments(ASTContext &Context,
131 TemplateName Param,
132 TemplateName Arg,
133 Sema::TemplateDeductionInfo &Info,
134 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregord708c722009-06-09 16:35:58 +0000135 // FIXME: Implement template argument deduction for template
136 // template parameters.
137
Douglas Gregorf67875d2009-06-12 18:26:56 +0000138 // FIXME: this routine does not have enough information to produce
139 // good diagnostics.
140
Douglas Gregord708c722009-06-09 16:35:58 +0000141 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
142 TemplateDecl *ArgDecl = Arg.getAsTemplateDecl();
143
Douglas Gregorf67875d2009-06-12 18:26:56 +0000144 if (!ParamDecl || !ArgDecl) {
145 // FIXME: fill in Info.Param/Info.FirstArg
146 return Sema::TDK_Inconsistent;
147 }
Douglas Gregord708c722009-06-09 16:35:58 +0000148
149 ParamDecl = cast<TemplateDecl>(Context.getCanonicalDecl(ParamDecl));
150 ArgDecl = cast<TemplateDecl>(Context.getCanonicalDecl(ArgDecl));
Douglas Gregorf67875d2009-06-12 18:26:56 +0000151 if (ParamDecl != ArgDecl) {
152 // FIXME: fill in Info.Param/Info.FirstArg
153 return Sema::TDK_Inconsistent;
154 }
155
156 return Sema::TDK_Success;
Douglas Gregord708c722009-06-09 16:35:58 +0000157}
158
Douglas Gregor500d3312009-06-26 18:27:22 +0000159/// \brief Deduce the template arguments by comparing the parameter type and
160/// the argument type (C++ [temp.deduct.type]).
161///
162/// \param Context the AST context in which this deduction occurs.
163///
164/// \param TemplateParams the template parameters that we are deducing
165///
166/// \param ParamIn the parameter type
167///
168/// \param ArgIn the argument type
169///
170/// \param Info information about the template argument deduction itself
171///
172/// \param Deduced the deduced template arguments
173///
174/// \param ParamTypeWasReference if true, the original parameter type was
175/// a reference type (C++0x [temp.deduct.type]p4 bullet 1).
176///
177/// \returns the result of template argument deduction so far. Note that a
178/// "success" result means that template argument deduction has not yet failed,
179/// but it may still fail, later, for other reasons.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000180static Sema::TemplateDeductionResult
181DeduceTemplateArguments(ASTContext &Context,
182 TemplateParameterList *TemplateParams,
183 QualType ParamIn, QualType ArgIn,
184 Sema::TemplateDeductionInfo &Info,
Douglas Gregor500d3312009-06-26 18:27:22 +0000185 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
186 bool ParamTypeWasReference = false) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000187 // We only want to look at the canonical types, since typedefs and
188 // sugar are not part of template argument deduction.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000189 QualType Param = Context.getCanonicalType(ParamIn);
190 QualType Arg = Context.getCanonicalType(ArgIn);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000191
Douglas Gregor500d3312009-06-26 18:27:22 +0000192 // C++0x [temp.deduct.call]p4 bullet 1:
193 // - If the original P is a reference type, the deduced A (i.e., the type
194 // referred to by the reference) can be more cv-qualified than the
195 // transformed A.
196 if (ParamTypeWasReference) {
197 unsigned ExtraQualsOnParam
198 = Param.getCVRQualifiers() & ~Arg.getCVRQualifiers();
199 Param.setCVRQualifiers(Param.getCVRQualifiers() & ~ExtraQualsOnParam);
200 }
201
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000202 // If the parameter type is not dependent, there is nothing to deduce.
203 if (!Param->isDependentType())
204 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000205
Douglas Gregor199d9912009-06-05 00:53:49 +0000206 // C++ [temp.deduct.type]p9:
Douglas Gregor199d9912009-06-05 00:53:49 +0000207 // A template type argument T, a template template argument TT or a
208 // template non-type argument i can be deduced if P and A have one of
209 // the following forms:
210 //
211 // T
212 // cv-list T
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000213 if (const TemplateTypeParmType *TemplateTypeParm
214 = Param->getAsTemplateTypeParmType()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000215 unsigned Index = TemplateTypeParm->getIndex();
216
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000217 // The argument type can not be less qualified than the parameter
218 // type.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000219 if (Param.isMoreQualifiedThan(Arg)) {
220 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
221 Info.FirstArg = Deduced[Index];
222 Info.SecondArg = TemplateArgument(SourceLocation(), Arg);
223 return Sema::TDK_InconsistentQuals;
224 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000225
226 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
227
228 unsigned Quals = Arg.getCVRQualifiers() & ~Param.getCVRQualifiers();
229 QualType DeducedType = Arg.getQualifiedType(Quals);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000230
231 if (Deduced[Index].isNull())
232 Deduced[Index] = TemplateArgument(SourceLocation(), DeducedType);
233 else {
234 // C++ [temp.deduct.type]p2:
235 // [...] If type deduction cannot be done for any P/A pair, or if for
236 // any pair the deduction leads to more than one possible set of
237 // deduced values, or if different pairs yield different deduced
238 // values, or if any template argument remains neither deduced nor
239 // explicitly specified, template argument deduction fails.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000240 if (Deduced[Index].getAsType() != DeducedType) {
241 Info.Param
242 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
243 Info.FirstArg = Deduced[Index];
244 Info.SecondArg = TemplateArgument(SourceLocation(), Arg);
245 return Sema::TDK_Inconsistent;
246 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000247 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000248 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000249 }
250
Douglas Gregorf67875d2009-06-12 18:26:56 +0000251 // Set up the template argument deduction information for a failure.
252 Info.FirstArg = TemplateArgument(SourceLocation(), ParamIn);
253 Info.SecondArg = TemplateArgument(SourceLocation(), ArgIn);
254
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000255 if ((ParamTypeWasReference && Param.isMoreQualifiedThan(Arg)) ||
256 (!ParamTypeWasReference &&
257 (Param.getCVRQualifiers() != Arg.getCVRQualifiers())))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000258 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000259
Douglas Gregord560d502009-06-04 00:21:18 +0000260 switch (Param->getTypeClass()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000261 // No deduction possible for these types
262 case Type::Builtin:
Douglas Gregorf67875d2009-06-12 18:26:56 +0000263 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000264
265 // T *
Douglas Gregord560d502009-06-04 00:21:18 +0000266 case Type::Pointer: {
267 const PointerType *PointerArg = Arg->getAsPointerType();
268 if (!PointerArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000269 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord560d502009-06-04 00:21:18 +0000270
Douglas Gregorf67875d2009-06-12 18:26:56 +0000271 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000272 cast<PointerType>(Param)->getPointeeType(),
273 PointerArg->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000274 Info, Deduced);
Douglas Gregord560d502009-06-04 00:21:18 +0000275 }
276
Douglas Gregor199d9912009-06-05 00:53:49 +0000277 // T &
Douglas Gregord560d502009-06-04 00:21:18 +0000278 case Type::LValueReference: {
279 const LValueReferenceType *ReferenceArg = Arg->getAsLValueReferenceType();
280 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000281 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord560d502009-06-04 00:21:18 +0000282
Douglas Gregorf67875d2009-06-12 18:26:56 +0000283 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000284 cast<LValueReferenceType>(Param)->getPointeeType(),
285 ReferenceArg->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000286 Info, Deduced);
Douglas Gregord560d502009-06-04 00:21:18 +0000287 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000288
Douglas Gregor199d9912009-06-05 00:53:49 +0000289 // T && [C++0x]
Douglas Gregord560d502009-06-04 00:21:18 +0000290 case Type::RValueReference: {
291 const RValueReferenceType *ReferenceArg = Arg->getAsRValueReferenceType();
292 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000293 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord560d502009-06-04 00:21:18 +0000294
Douglas Gregorf67875d2009-06-12 18:26:56 +0000295 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000296 cast<RValueReferenceType>(Param)->getPointeeType(),
297 ReferenceArg->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000298 Info, Deduced);
Douglas Gregord560d502009-06-04 00:21:18 +0000299 }
300
Douglas Gregor199d9912009-06-05 00:53:49 +0000301 // T [] (implied, but not stated explicitly)
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000302 case Type::IncompleteArray: {
303 const IncompleteArrayType *IncompleteArrayArg =
304 Context.getAsIncompleteArrayType(Arg);
305 if (!IncompleteArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000306 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000307
Douglas Gregorf67875d2009-06-12 18:26:56 +0000308 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000309 Context.getAsIncompleteArrayType(Param)->getElementType(),
310 IncompleteArrayArg->getElementType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000311 Info, Deduced);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000312 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000313
314 // T [integer-constant]
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000315 case Type::ConstantArray: {
316 const ConstantArrayType *ConstantArrayArg =
317 Context.getAsConstantArrayType(Arg);
318 if (!ConstantArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000319 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000320
321 const ConstantArrayType *ConstantArrayParm =
322 Context.getAsConstantArrayType(Param);
323 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000324 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000325
Douglas Gregorf67875d2009-06-12 18:26:56 +0000326 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000327 ConstantArrayParm->getElementType(),
328 ConstantArrayArg->getElementType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000329 Info, Deduced);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000330 }
331
Douglas Gregor199d9912009-06-05 00:53:49 +0000332 // type [i]
333 case Type::DependentSizedArray: {
334 const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg);
335 if (!ArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000336 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000337
338 // Check the element type of the arrays
339 const DependentSizedArrayType *DependentArrayParm
340 = cast<DependentSizedArrayType>(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000341 if (Sema::TemplateDeductionResult Result
342 = DeduceTemplateArguments(Context, TemplateParams,
343 DependentArrayParm->getElementType(),
344 ArrayArg->getElementType(),
345 Info, Deduced))
346 return Result;
Douglas Gregor199d9912009-06-05 00:53:49 +0000347
348 // Determine the array bound is something we can deduce.
349 NonTypeTemplateParmDecl *NTTP
350 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
351 if (!NTTP)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000352 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000353
354 // We can perform template argument deduction for the given non-type
355 // template parameter.
356 assert(NTTP->getDepth() == 0 &&
357 "Cannot deduce non-type template argument at depth > 0");
358 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson335e24a2009-06-16 22:44:31 +0000359 = dyn_cast<ConstantArrayType>(ArrayArg)) {
360 llvm::APSInt Size(ConstantArrayArg->getSize());
361 return DeduceNonTypeTemplateArgument(Context, NTTP, Size,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000362 Info, Deduced);
Anders Carlsson335e24a2009-06-16 22:44:31 +0000363 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000364 if (const DependentSizedArrayType *DependentArrayArg
365 = dyn_cast<DependentSizedArrayType>(ArrayArg))
366 return DeduceNonTypeTemplateArgument(Context, NTTP,
367 DependentArrayArg->getSizeExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000368 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000369
370 // Incomplete type does not match a dependently-sized array type
Douglas Gregorf67875d2009-06-12 18:26:56 +0000371 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000372 }
373
Douglas Gregor0fce0ae2009-06-08 15:59:14 +0000374 // type(*)(T)
375 // T(*)()
376 // T(*)(T)
Anders Carlssona27fad52009-06-08 15:19:08 +0000377 case Type::FunctionProto: {
378 const FunctionProtoType *FunctionProtoArg =
379 dyn_cast<FunctionProtoType>(Arg);
380 if (!FunctionProtoArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000381 return Sema::TDK_NonDeducedMismatch;
Anders Carlssona27fad52009-06-08 15:19:08 +0000382
383 const FunctionProtoType *FunctionProtoParam =
384 cast<FunctionProtoType>(Param);
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000385
386 if (FunctionProtoParam->getTypeQuals() !=
387 FunctionProtoArg->getTypeQuals())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000388 return Sema::TDK_NonDeducedMismatch;
Anders Carlssona27fad52009-06-08 15:19:08 +0000389
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000390 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000391 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000392
393 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000394 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000395
Anders Carlssona27fad52009-06-08 15:19:08 +0000396 // Check return types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000397 if (Sema::TemplateDeductionResult Result
398 = DeduceTemplateArguments(Context, TemplateParams,
399 FunctionProtoParam->getResultType(),
400 FunctionProtoArg->getResultType(),
401 Info, Deduced))
402 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000403
Anders Carlssona27fad52009-06-08 15:19:08 +0000404 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
405 // Check argument types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000406 if (Sema::TemplateDeductionResult Result
407 = DeduceTemplateArguments(Context, TemplateParams,
408 FunctionProtoParam->getArgType(I),
409 FunctionProtoArg->getArgType(I),
410 Info, Deduced))
411 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000412 }
413
Douglas Gregorf67875d2009-06-12 18:26:56 +0000414 return Sema::TDK_Success;
Anders Carlssona27fad52009-06-08 15:19:08 +0000415 }
Douglas Gregord708c722009-06-09 16:35:58 +0000416
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000417 // template-name<T> (where template-name refers to a class template)
Douglas Gregord708c722009-06-09 16:35:58 +0000418 // template-name<i>
419 // TT<T> (TODO)
420 // TT<i> (TODO)
421 // TT<> (TODO)
422 case Type::TemplateSpecialization: {
423 const TemplateSpecializationType *SpecParam
424 = cast<TemplateSpecializationType>(Param);
425
426 // Check whether the template argument is a dependent template-id.
427 // FIXME: This is untested code; it can be tested when we implement
428 // partial ordering of class template partial specializations.
429 if (const TemplateSpecializationType *SpecArg
430 = dyn_cast<TemplateSpecializationType>(Arg)) {
431 // Perform template argument deduction for the template name.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000432 if (Sema::TemplateDeductionResult Result
433 = DeduceTemplateArguments(Context,
434 SpecParam->getTemplateName(),
435 SpecArg->getTemplateName(),
436 Info, Deduced))
437 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000438
439 unsigned NumArgs = SpecParam->getNumArgs();
440
441 // FIXME: When one of the template-names refers to a
442 // declaration with default template arguments, do we need to
443 // fill in those default template arguments here? Most likely,
444 // the answer is "yes", but I don't see any references. This
445 // issue may be resolved elsewhere, because we may want to
446 // instantiate default template arguments when
447 if (SpecArg->getNumArgs() != NumArgs)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000448 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000449
450 // Perform template argument deduction on each template
451 // argument.
452 for (unsigned I = 0; I != NumArgs; ++I)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000453 if (Sema::TemplateDeductionResult Result
454 = DeduceTemplateArguments(Context, TemplateParams,
455 SpecParam->getArg(I),
456 SpecArg->getArg(I),
457 Info, Deduced))
458 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000459
Douglas Gregorf67875d2009-06-12 18:26:56 +0000460 return Sema::TDK_Success;
Douglas Gregord708c722009-06-09 16:35:58 +0000461 }
462
463 // If the argument type is a class template specialization, we
464 // perform template argument deduction using its template
465 // arguments.
466 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
467 if (!RecordArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000468 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000469
470 ClassTemplateSpecializationDecl *SpecArg
471 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
472 if (!SpecArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000473 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000474
475 // Perform template argument deduction for the template name.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000476 if (Sema::TemplateDeductionResult Result
477 = DeduceTemplateArguments(Context,
478 SpecParam->getTemplateName(),
479 TemplateName(SpecArg->getSpecializedTemplate()),
480 Info, Deduced))
481 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000482
483 // FIXME: Can the # of arguments in the parameter and the argument differ?
484 unsigned NumArgs = SpecParam->getNumArgs();
485 const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
486 if (NumArgs != ArgArgs.size())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000487 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000488
489 for (unsigned I = 0; I != NumArgs; ++I)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000490 if (Sema::TemplateDeductionResult Result
491 = DeduceTemplateArguments(Context, TemplateParams,
492 SpecParam->getArg(I),
493 ArgArgs.get(I),
494 Info, Deduced))
495 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000496
Douglas Gregorf67875d2009-06-12 18:26:56 +0000497 return Sema::TDK_Success;
Douglas Gregord708c722009-06-09 16:35:58 +0000498 }
499
Douglas Gregor637a4092009-06-10 23:47:09 +0000500 // T type::*
501 // T T::*
502 // T (type::*)()
503 // type (T::*)()
504 // type (type::*)(T)
505 // type (T::*)(T)
506 // T (type::*)(T)
507 // T (T::*)()
508 // T (T::*)(T)
509 case Type::MemberPointer: {
510 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
511 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
512 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000513 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +0000514
Douglas Gregorf67875d2009-06-12 18:26:56 +0000515 if (Sema::TemplateDeductionResult Result
516 = DeduceTemplateArguments(Context, TemplateParams,
517 MemPtrParam->getPointeeType(),
518 MemPtrArg->getPointeeType(),
519 Info, Deduced))
520 return Result;
521
522 return DeduceTemplateArguments(Context, TemplateParams,
523 QualType(MemPtrParam->getClass(), 0),
524 QualType(MemPtrArg->getClass(), 0),
525 Info, Deduced);
Douglas Gregor637a4092009-06-10 23:47:09 +0000526 }
527
Anders Carlsson9a917e42009-06-12 22:56:54 +0000528 // (clang extension)
529 //
Anders Carlsson859ba502009-06-12 16:23:10 +0000530 // type(^)(T)
531 // T(^)()
532 // T(^)(T)
533 case Type::BlockPointer: {
534 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
535 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
536
537 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000538 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson859ba502009-06-12 16:23:10 +0000539
Douglas Gregorf67875d2009-06-12 18:26:56 +0000540 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +0000541 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000542 BlockPtrArg->getPointeeType(), Info,
543 Deduced);
Anders Carlsson859ba502009-06-12 16:23:10 +0000544 }
545
Douglas Gregor637a4092009-06-10 23:47:09 +0000546 case Type::TypeOfExpr:
547 case Type::TypeOf:
548 case Type::Typename:
549 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +0000550 return Sema::TDK_Success;
Douglas Gregor637a4092009-06-10 23:47:09 +0000551
Douglas Gregord560d502009-06-04 00:21:18 +0000552 default:
553 break;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000554 }
555
556 // FIXME: Many more cases to go (to go).
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000557 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000558}
559
Douglas Gregorf67875d2009-06-12 18:26:56 +0000560static Sema::TemplateDeductionResult
561DeduceTemplateArguments(ASTContext &Context,
562 TemplateParameterList *TemplateParams,
563 const TemplateArgument &Param,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000564 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000565 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000566 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000567 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000568 case TemplateArgument::Null:
569 assert(false && "Null template argument in parameter list");
570 break;
571
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000572 case TemplateArgument::Type:
Douglas Gregor199d9912009-06-05 00:53:49 +0000573 assert(Arg.getKind() == TemplateArgument::Type && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000574 return DeduceTemplateArguments(Context, TemplateParams,
575 Param.getAsType(),
576 Arg.getAsType(), Info, Deduced);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000577
Douglas Gregor199d9912009-06-05 00:53:49 +0000578 case TemplateArgument::Declaration:
579 // FIXME: Implement this check
580 assert(false && "Unimplemented template argument deduction case");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000581 Info.FirstArg = Param;
582 Info.SecondArg = Arg;
583 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000584
585 case TemplateArgument::Integral:
586 if (Arg.getKind() == TemplateArgument::Integral) {
587 // FIXME: Zero extension + sign checking here?
Douglas Gregorf67875d2009-06-12 18:26:56 +0000588 if (*Param.getAsIntegral() == *Arg.getAsIntegral())
589 return Sema::TDK_Success;
590
591 Info.FirstArg = Param;
592 Info.SecondArg = Arg;
593 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000594 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000595
596 if (Arg.getKind() == TemplateArgument::Expression) {
597 Info.FirstArg = Param;
598 Info.SecondArg = Arg;
599 return Sema::TDK_NonDeducedMismatch;
600 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000601
602 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000603 Info.FirstArg = Param;
604 Info.SecondArg = Arg;
605 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000606
607 case TemplateArgument::Expression: {
608 if (NonTypeTemplateParmDecl *NTTP
609 = getDeducedParameterFromExpr(Param.getAsExpr())) {
610 if (Arg.getKind() == TemplateArgument::Integral)
611 // FIXME: Sign problems here
612 return DeduceNonTypeTemplateArgument(Context, NTTP,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000613 *Arg.getAsIntegral(),
614 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000615 if (Arg.getKind() == TemplateArgument::Expression)
616 return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000617 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000618
619 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000620 Info.FirstArg = Param;
621 Info.SecondArg = Arg;
622 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000623 }
624
625 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000626 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000627 }
Anders Carlssond01b1da2009-06-15 17:04:53 +0000628 case TemplateArgument::Pack:
629 assert(0 && "FIXME: Implement!");
630 break;
Douglas Gregor199d9912009-06-05 00:53:49 +0000631 }
632
Douglas Gregorf67875d2009-06-12 18:26:56 +0000633 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000634}
635
Douglas Gregorf67875d2009-06-12 18:26:56 +0000636static Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000637DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000638 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000639 const TemplateArgumentList &ParamList,
640 const TemplateArgumentList &ArgList,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000641 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000642 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
643 assert(ParamList.size() == ArgList.size());
644 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000645 if (Sema::TemplateDeductionResult Result
646 = DeduceTemplateArguments(Context, TemplateParams,
647 ParamList[I], ArgList[I],
648 Info, Deduced))
649 return Result;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000650 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000651 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000652}
653
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000654/// \brief Determine whether two template arguments are the same.
655static bool isSameTemplateArg(ASTContext &Context,
656 const TemplateArgument &X,
657 const TemplateArgument &Y) {
658 if (X.getKind() != Y.getKind())
659 return false;
660
661 switch (X.getKind()) {
662 case TemplateArgument::Null:
663 assert(false && "Comparing NULL template argument");
664 break;
665
666 case TemplateArgument::Type:
667 return Context.getCanonicalType(X.getAsType()) ==
668 Context.getCanonicalType(Y.getAsType());
669
670 case TemplateArgument::Declaration:
671 return Context.getCanonicalDecl(X.getAsDecl()) ==
672 Context.getCanonicalDecl(Y.getAsDecl());
673
674 case TemplateArgument::Integral:
675 return *X.getAsIntegral() == *Y.getAsIntegral();
676
677 case TemplateArgument::Expression:
678 // FIXME: We assume that all expressions are distinct, but we should
679 // really check their canonical forms.
680 return false;
681
682 case TemplateArgument::Pack:
683 if (X.pack_size() != Y.pack_size())
684 return false;
685
686 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
687 XPEnd = X.pack_end(),
688 YP = Y.pack_begin();
689 XP != XPEnd; ++XP, ++YP)
690 if (!isSameTemplateArg(Context, *XP, *YP))
691 return false;
692
693 return true;
694 }
695
696 return false;
697}
698
699/// \brief Helper function to build a TemplateParameter when we don't
700/// know its type statically.
701static TemplateParameter makeTemplateParameter(Decl *D) {
702 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
703 return TemplateParameter(TTP);
704 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
705 return TemplateParameter(NTTP);
706
707 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
708}
709
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000710/// \brief Perform template argument deduction to determine whether
711/// the given template arguments match the given class template
712/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +0000713Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000714Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000715 const TemplateArgumentList &TemplateArgs,
716 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000717 // C++ [temp.class.spec.match]p2:
718 // A partial specialization matches a given actual template
719 // argument list if the template arguments of the partial
720 // specialization can be deduced from the actual template argument
721 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +0000722 SFINAETrap Trap(*this);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000723 llvm::SmallVector<TemplateArgument, 4> Deduced;
724 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000725 if (TemplateDeductionResult Result
726 = ::DeduceTemplateArguments(Context,
727 Partial->getTemplateParameters(),
728 Partial->getTemplateArgs(),
729 TemplateArgs, Info, Deduced))
730 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +0000731
Douglas Gregor637a4092009-06-10 23:47:09 +0000732 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
733 Deduced.data(), Deduced.size());
734 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000735 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +0000736
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000737 // C++ [temp.deduct.type]p2:
738 // [...] or if any template argument remains neither deduced nor
739 // explicitly specified, template argument deduction fails.
Anders Carlssonfb250522009-06-23 01:26:57 +0000740 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
741 Deduced.size());
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000742 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000743 if (Deduced[I].isNull()) {
744 Decl *Param
745 = const_cast<Decl *>(Partial->getTemplateParameters()->getParam(I));
746 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
747 Info.Param = TTP;
748 else if (NonTypeTemplateParmDecl *NTTP
749 = dyn_cast<NonTypeTemplateParmDecl>(Param))
750 Info.Param = NTTP;
751 else
752 Info.Param = cast<TemplateTemplateParmDecl>(Param);
753 return TDK_Incomplete;
754 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000755
Anders Carlssonfb250522009-06-23 01:26:57 +0000756 Builder.Append(Deduced[I]);
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000757 }
758
759 // Form the template argument list from the deduced template arguments.
760 TemplateArgumentList *DeducedArgumentList
Anders Carlssonfb250522009-06-23 01:26:57 +0000761 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000762 Info.reset(DeducedArgumentList);
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000763
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000764 // Substitute the deduced template arguments into the template
765 // arguments of the class template partial specialization, and
766 // verify that the instantiated template arguments are both valid
767 // and are equivalent to the template arguments originally provided
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000768 // to the class template.
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000769 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
770 const TemplateArgumentList &PartialTemplateArgs = Partial->getTemplateArgs();
771 for (unsigned I = 0, N = PartialTemplateArgs.flat_size(); I != N; ++I) {
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000772 Decl *Param = const_cast<Decl *>(
773 ClassTemplate->getTemplateParameters()->getParam(I));
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000774 TemplateArgument InstArg = Instantiate(PartialTemplateArgs[I],
775 *DeducedArgumentList);
776 if (InstArg.isNull()) {
777 Info.Param = makeTemplateParameter(Param);
778 Info.FirstArg = PartialTemplateArgs[I];
779 return TDK_SubstitutionFailure;
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000780 }
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000781
782 if (InstArg.getKind() == TemplateArgument::Expression) {
783 // When the argument is an expression, check the expression result
784 // against the actual template parameter to get down to the canonical
785 // template argument.
786 Expr *InstExpr = InstArg.getAsExpr();
787 if (NonTypeTemplateParmDecl *NTTP
788 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
789 if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
790 Info.Param = makeTemplateParameter(Param);
791 Info.FirstArg = PartialTemplateArgs[I];
792 return TDK_SubstitutionFailure;
793 }
794 } else if (TemplateTemplateParmDecl *TTP
795 = dyn_cast<TemplateTemplateParmDecl>(Param)) {
796 // FIXME: template template arguments should really resolve to decls
797 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InstExpr);
798 if (!DRE || CheckTemplateArgument(TTP, DRE)) {
799 Info.Param = makeTemplateParameter(Param);
800 Info.FirstArg = PartialTemplateArgs[I];
801 return TDK_SubstitutionFailure;
802 }
803 }
804 }
805
806 if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) {
807 Info.Param = makeTemplateParameter(Param);
808 Info.FirstArg = TemplateArgs[I];
809 Info.SecondArg = InstArg;
810 return TDK_NonDeducedMismatch;
811 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000812 }
813
Douglas Gregorbb260412009-06-14 08:02:22 +0000814 if (Trap.hasErrorOccurred())
815 return TDK_SubstitutionFailure;
816
Douglas Gregorf67875d2009-06-12 18:26:56 +0000817 return TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000818}
Douglas Gregor031a5882009-06-13 00:26:55 +0000819
Douglas Gregore53060f2009-06-25 22:08:12 +0000820/// \brief Perform template argument deduction from a function call
821/// (C++ [temp.deduct.call]).
822///
823/// \param FunctionTemplate the function template for which we are performing
824/// template argument deduction.
825///
826/// \param Args the function call arguments
827///
828/// \param NumArgs the number of arguments in Args
829///
830/// \param Specialization if template argument deduction was successful,
831/// this will be set to the function template specialization produced by
832/// template argument deduction.
833///
834/// \param Info the argument will be updated to provide additional information
835/// about template argument deduction.
836///
837/// \returns the result of template argument deduction.
838///
839/// FIXME: We will also need to pass in any explicitly-specified template
840/// arguments.
841Sema::TemplateDeductionResult
842Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
843 Expr **Args, unsigned NumArgs,
844 FunctionDecl *&Specialization,
845 TemplateDeductionInfo &Info) {
846 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
847
848 // C++ [temp.deduct.call]p1:
849 // Template argument deduction is done by comparing each function template
850 // parameter type (call it P) with the type of the corresponding argument
851 // of the call (call it A) as described below.
852 unsigned CheckArgs = NumArgs;
853 if (NumArgs < Function->getNumParams())
854 return TDK_TooFewArguments;
855 else if (NumArgs > Function->getNumParams()) {
856 const FunctionProtoType *Proto
857 = Function->getType()->getAsFunctionProtoType();
858 if (!Proto->isVariadic())
859 return TDK_TooManyArguments;
860
861 CheckArgs = Function->getNumParams();
862 }
863
Douglas Gregor500d3312009-06-26 18:27:22 +0000864 // Template argument deduction for function templates in a SFINAE context.
865 // Trap any errors that might occur.
Douglas Gregore53060f2009-06-25 22:08:12 +0000866 SFINAETrap Trap(*this);
Douglas Gregor500d3312009-06-26 18:27:22 +0000867
868 // Deduce template arguments from the function parameters.
Douglas Gregore53060f2009-06-25 22:08:12 +0000869 llvm::SmallVector<TemplateArgument, 4> Deduced;
870 Deduced.resize(FunctionTemplate->getTemplateParameters()->size());
871 TemplateParameterList *TemplateParams
872 = FunctionTemplate->getTemplateParameters();
873 for (unsigned I = 0; I != CheckArgs; ++I) {
874 QualType ParamType = Function->getParamDecl(I)->getType();
875 QualType ArgType = Args[I]->getType();
Douglas Gregor500d3312009-06-26 18:27:22 +0000876
Douglas Gregore53060f2009-06-25 22:08:12 +0000877 // C++ [temp.deduct.call]p2:
878 // If P is not a reference type:
879 QualType CanonParamType = Context.getCanonicalType(ParamType);
Douglas Gregor500d3312009-06-26 18:27:22 +0000880 bool ParamWasReference = isa<ReferenceType>(CanonParamType);
881 if (!ParamWasReference) {
Douglas Gregore53060f2009-06-25 22:08:12 +0000882 // - If A is an array type, the pointer type produced by the
883 // array-to-pointer standard conversion (4.2) is used in place of
884 // A for type deduction; otherwise,
885 if (ArgType->isArrayType())
886 ArgType = Context.getArrayDecayedType(ArgType);
887 // - If A is a function type, the pointer type produced by the
888 // function-to-pointer standard conversion (4.3) is used in place
889 // of A for type deduction; otherwise,
890 else if (ArgType->isFunctionType())
891 ArgType = Context.getPointerType(ArgType);
892 else {
893 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
894 // type are ignored for type deduction.
895 QualType CanonArgType = Context.getCanonicalType(ArgType);
896 if (CanonArgType.getCVRQualifiers())
897 ArgType = CanonArgType.getUnqualifiedType();
898 }
899 }
900
901 // C++0x [temp.deduct.call]p3:
902 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
903 // are ignored for type deduction.
904 if (CanonParamType.getCVRQualifiers())
905 ParamType = CanonParamType.getUnqualifiedType();
906 if (const ReferenceType *ParamRefType = ParamType->getAsReferenceType()) {
907 // [...] If P is a reference type, the type referred to by P is used
908 // for type deduction.
909 ParamType = ParamRefType->getPointeeType();
910
911 // [...] If P is of the form T&&, where T is a template parameter, and
912 // the argument is an lvalue, the type A& is used in place of A for
913 // type deduction.
914 if (isa<RValueReferenceType>(ParamRefType) &&
915 ParamRefType->getAsTemplateTypeParmType() &&
916 Args[I]->isLvalue(Context) == Expr::LV_Valid)
917 ArgType = Context.getLValueReferenceType(ArgType);
918 }
919
920 // C++0x [temp.deduct.call]p4:
921 // In general, the deduction process attempts to find template argument
922 // values that will make the deduced A identical to A (after the type A
923 // is transformed as described above). [...]
924 //
925 // FIXME: we'll pass down a flag to indicate when these cases may apply,
926 // and then deal with them in the code that deduces template
927 // arguments from a type.
928 if (TemplateDeductionResult Result
929 = ::DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor500d3312009-06-26 18:27:22 +0000930 ParamType, ArgType, Info, Deduced,
931 ParamWasReference))
Douglas Gregore53060f2009-06-25 22:08:12 +0000932 return Result;
933
934 // FIXME: C++ [temp.deduct.call] paragraphs 6-9 deal with function
935 // pointer parameters.
936 }
937
938 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
939 FunctionTemplate, Deduced.data(), Deduced.size());
940 if (Inst)
941 return TDK_InstantiationDepth;
942
943 // C++ [temp.deduct.type]p2:
944 // [...] or if any template argument remains neither deduced nor
945 // explicitly specified, template argument deduction fails.
946 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
947 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
948 if (Deduced[I].isNull()) {
949 Decl *Param
950 = const_cast<Decl *>(TemplateParams->getParam(I));
951 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
952 Info.Param = TTP;
953 else if (NonTypeTemplateParmDecl *NTTP
954 = dyn_cast<NonTypeTemplateParmDecl>(Param))
955 Info.Param = NTTP;
956 else
957 Info.Param = cast<TemplateTemplateParmDecl>(Param);
958 return TDK_Incomplete;
959 }
960
961 Builder.Append(Deduced[I]);
962 }
963
964 // Form the template argument list from the deduced template arguments.
965 TemplateArgumentList *DeducedArgumentList
966 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
967 Info.reset(DeducedArgumentList);
968
969 // Substitute the deduced template arguments into the function template
970 // declaration to produce the function template specialization.
971 Specialization = cast_or_null<FunctionDecl>(
972 InstantiateDecl(FunctionTemplate->getTemplatedDecl(),
973 FunctionTemplate->getDeclContext(),
974 *DeducedArgumentList));
975
976 if (!Specialization || Trap.hasErrorOccurred())
977 return TDK_SubstitutionFailure;
978
Douglas Gregor1637be72009-06-26 00:10:03 +0000979 // Turn the specialization into an actual function template specialization.
980 Specialization->setFunctionTemplateSpecialization(Context,
981 FunctionTemplate,
982 Info.take());
Douglas Gregore53060f2009-06-25 22:08:12 +0000983 return TDK_Success;
984}
985
Douglas Gregor031a5882009-06-13 00:26:55 +0000986static void
987MarkDeducedTemplateParameters(Sema &SemaRef,
988 const TemplateArgument &TemplateArg,
989 llvm::SmallVectorImpl<bool> &Deduced);
990
991/// \brief Mark the template arguments that are deduced by the given
992/// expression.
993static void
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000994MarkDeducedTemplateParameters(const Expr *E,
995 llvm::SmallVectorImpl<bool> &Deduced) {
996 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregor031a5882009-06-13 00:26:55 +0000997 if (!E)
998 return;
999
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001000 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00001001 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
1002 if (!NTTP)
1003 return;
1004
1005 Deduced[NTTP->getIndex()] = true;
1006}
1007
1008/// \brief Mark the template parameters that are deduced by the given
1009/// type.
1010static void
1011MarkDeducedTemplateParameters(Sema &SemaRef, QualType T,
1012 llvm::SmallVectorImpl<bool> &Deduced) {
1013 // Non-dependent types have nothing deducible
1014 if (!T->isDependentType())
1015 return;
1016
1017 T = SemaRef.Context.getCanonicalType(T);
1018 switch (T->getTypeClass()) {
1019 case Type::ExtQual:
1020 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001021 QualType(cast<ExtQualType>(T)->getBaseType(), 0),
Douglas Gregor031a5882009-06-13 00:26:55 +00001022 Deduced);
1023 break;
1024
1025 case Type::Pointer:
1026 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001027 cast<PointerType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001028 Deduced);
1029 break;
1030
1031 case Type::BlockPointer:
1032 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001033 cast<BlockPointerType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001034 Deduced);
1035 break;
1036
1037 case Type::LValueReference:
1038 case Type::RValueReference:
1039 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001040 cast<ReferenceType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001041 Deduced);
1042 break;
1043
1044 case Type::MemberPointer: {
1045 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
1046 MarkDeducedTemplateParameters(SemaRef, MemPtr->getPointeeType(), Deduced);
1047 MarkDeducedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
1048 Deduced);
1049 break;
1050 }
1051
1052 case Type::DependentSizedArray:
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001053 MarkDeducedTemplateParameters(cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001054 Deduced);
1055 // Fall through to check the element type
1056
1057 case Type::ConstantArray:
1058 case Type::IncompleteArray:
1059 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001060 cast<ArrayType>(T)->getElementType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001061 Deduced);
1062 break;
1063
1064 case Type::Vector:
1065 case Type::ExtVector:
1066 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001067 cast<VectorType>(T)->getElementType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001068 Deduced);
1069 break;
1070
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001071 case Type::DependentSizedExtVector: {
1072 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001073 = cast<DependentSizedExtVectorType>(T);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001074 MarkDeducedTemplateParameters(SemaRef, VecType->getElementType(), Deduced);
1075 MarkDeducedTemplateParameters(VecType->getSizeExpr(), Deduced);
1076 break;
1077 }
1078
Douglas Gregor031a5882009-06-13 00:26:55 +00001079 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001080 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregor031a5882009-06-13 00:26:55 +00001081 MarkDeducedTemplateParameters(SemaRef, Proto->getResultType(), Deduced);
1082 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
1083 MarkDeducedTemplateParameters(SemaRef, Proto->getArgType(I), Deduced);
1084 break;
1085 }
1086
1087 case Type::TemplateTypeParm:
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001088 Deduced[cast<TemplateTypeParmType>(T)->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00001089 break;
1090
1091 case Type::TemplateSpecialization: {
1092 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001093 = cast<TemplateSpecializationType>(T);
Douglas Gregor031a5882009-06-13 00:26:55 +00001094 if (TemplateDecl *Template = Spec->getTemplateName().getAsTemplateDecl())
1095 if (TemplateTemplateParmDecl *TTP
1096 = dyn_cast<TemplateTemplateParmDecl>(Template))
1097 Deduced[TTP->getIndex()] = true;
1098
1099 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
1100 MarkDeducedTemplateParameters(SemaRef, Spec->getArg(I), Deduced);
1101
1102 break;
1103 }
1104
1105 // None of these types have any deducible parts.
1106 case Type::Builtin:
1107 case Type::FixedWidthInt:
1108 case Type::Complex:
1109 case Type::VariableArray:
1110 case Type::FunctionNoProto:
1111 case Type::Record:
1112 case Type::Enum:
1113 case Type::Typename:
1114 case Type::ObjCInterface:
1115 case Type::ObjCQualifiedInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001116 case Type::ObjCObjectPointer:
Douglas Gregor031a5882009-06-13 00:26:55 +00001117#define TYPE(Class, Base)
1118#define ABSTRACT_TYPE(Class, Base)
1119#define DEPENDENT_TYPE(Class, Base)
1120#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
1121#include "clang/AST/TypeNodes.def"
1122 break;
1123 }
1124}
1125
1126/// \brief Mark the template parameters that are deduced by this
1127/// template argument.
1128static void
1129MarkDeducedTemplateParameters(Sema &SemaRef,
1130 const TemplateArgument &TemplateArg,
1131 llvm::SmallVectorImpl<bool> &Deduced) {
1132 switch (TemplateArg.getKind()) {
1133 case TemplateArgument::Null:
1134 case TemplateArgument::Integral:
1135 break;
1136
1137 case TemplateArgument::Type:
1138 MarkDeducedTemplateParameters(SemaRef, TemplateArg.getAsType(), Deduced);
1139 break;
1140
1141 case TemplateArgument::Declaration:
1142 if (TemplateTemplateParmDecl *TTP
1143 = dyn_cast<TemplateTemplateParmDecl>(TemplateArg.getAsDecl()))
1144 Deduced[TTP->getIndex()] = true;
1145 break;
1146
1147 case TemplateArgument::Expression:
1148 MarkDeducedTemplateParameters(TemplateArg.getAsExpr(), Deduced);
1149 break;
Anders Carlssond01b1da2009-06-15 17:04:53 +00001150 case TemplateArgument::Pack:
1151 assert(0 && "FIXME: Implement!");
1152 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00001153 }
1154}
1155
1156/// \brief Mark the template parameters can be deduced by the given
1157/// template argument list.
1158///
1159/// \param TemplateArgs the template argument list from which template
1160/// parameters will be deduced.
1161///
1162/// \param Deduced a bit vector whose elements will be set to \c true
1163/// to indicate when the corresponding template parameter will be
1164/// deduced.
1165void
1166Sema::MarkDeducedTemplateParameters(const TemplateArgumentList &TemplateArgs,
1167 llvm::SmallVectorImpl<bool> &Deduced) {
1168 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
1169 ::MarkDeducedTemplateParameters(*this, TemplateArgs[I], Deduced);
1170}