blob: 66e12fefe6b9ba1adf8fee1641553bde51d142d1 [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 Gregor0b9247f2009-06-04 00:03:07 +0000202 // If the parameter type is not dependent, just compare the types
203 // directly.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000204 if (!Param->isDependentType()) {
205 if (Param == Arg)
206 return Sema::TDK_Success;
207
208 Info.FirstArg = TemplateArgument(SourceLocation(), ParamIn);
209 Info.SecondArg = TemplateArgument(SourceLocation(), ArgIn);
210 return Sema::TDK_NonDeducedMismatch;
211 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000212
Douglas Gregor199d9912009-06-05 00:53:49 +0000213 // C++ [temp.deduct.type]p9:
Douglas Gregor199d9912009-06-05 00:53:49 +0000214 // A template type argument T, a template template argument TT or a
215 // template non-type argument i can be deduced if P and A have one of
216 // the following forms:
217 //
218 // T
219 // cv-list T
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000220 if (const TemplateTypeParmType *TemplateTypeParm
221 = Param->getAsTemplateTypeParmType()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000222 unsigned Index = TemplateTypeParm->getIndex();
223
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000224 // The argument type can not be less qualified than the parameter
225 // type.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000226 if (Param.isMoreQualifiedThan(Arg)) {
227 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
228 Info.FirstArg = Deduced[Index];
229 Info.SecondArg = TemplateArgument(SourceLocation(), Arg);
230 return Sema::TDK_InconsistentQuals;
231 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000232
233 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
234
235 unsigned Quals = Arg.getCVRQualifiers() & ~Param.getCVRQualifiers();
236 QualType DeducedType = Arg.getQualifiedType(Quals);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000237
238 if (Deduced[Index].isNull())
239 Deduced[Index] = TemplateArgument(SourceLocation(), DeducedType);
240 else {
241 // C++ [temp.deduct.type]p2:
242 // [...] If type deduction cannot be done for any P/A pair, or if for
243 // any pair the deduction leads to more than one possible set of
244 // deduced values, or if different pairs yield different deduced
245 // values, or if any template argument remains neither deduced nor
246 // explicitly specified, template argument deduction fails.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000247 if (Deduced[Index].getAsType() != DeducedType) {
248 Info.Param
249 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
250 Info.FirstArg = Deduced[Index];
251 Info.SecondArg = TemplateArgument(SourceLocation(), Arg);
252 return Sema::TDK_Inconsistent;
253 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000254 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000255 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000256 }
257
Douglas Gregorf67875d2009-06-12 18:26:56 +0000258 // Set up the template argument deduction information for a failure.
259 Info.FirstArg = TemplateArgument(SourceLocation(), ParamIn);
260 Info.SecondArg = TemplateArgument(SourceLocation(), ArgIn);
261
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000262 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000263 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000264
Douglas Gregord560d502009-06-04 00:21:18 +0000265 switch (Param->getTypeClass()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000266 // No deduction possible for these types
267 case Type::Builtin:
Douglas Gregorf67875d2009-06-12 18:26:56 +0000268 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000269
270 // T *
Douglas Gregord560d502009-06-04 00:21:18 +0000271 case Type::Pointer: {
272 const PointerType *PointerArg = Arg->getAsPointerType();
273 if (!PointerArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000274 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord560d502009-06-04 00:21:18 +0000275
Douglas Gregorf67875d2009-06-12 18:26:56 +0000276 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000277 cast<PointerType>(Param)->getPointeeType(),
278 PointerArg->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000279 Info, Deduced);
Douglas Gregord560d502009-06-04 00:21:18 +0000280 }
281
Douglas Gregor199d9912009-06-05 00:53:49 +0000282 // T &
Douglas Gregord560d502009-06-04 00:21:18 +0000283 case Type::LValueReference: {
284 const LValueReferenceType *ReferenceArg = Arg->getAsLValueReferenceType();
285 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000286 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord560d502009-06-04 00:21:18 +0000287
Douglas Gregorf67875d2009-06-12 18:26:56 +0000288 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000289 cast<LValueReferenceType>(Param)->getPointeeType(),
290 ReferenceArg->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000291 Info, Deduced);
Douglas Gregord560d502009-06-04 00:21:18 +0000292 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000293
Douglas Gregor199d9912009-06-05 00:53:49 +0000294 // T && [C++0x]
Douglas Gregord560d502009-06-04 00:21:18 +0000295 case Type::RValueReference: {
296 const RValueReferenceType *ReferenceArg = Arg->getAsRValueReferenceType();
297 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000298 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord560d502009-06-04 00:21:18 +0000299
Douglas Gregorf67875d2009-06-12 18:26:56 +0000300 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000301 cast<RValueReferenceType>(Param)->getPointeeType(),
302 ReferenceArg->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000303 Info, Deduced);
Douglas Gregord560d502009-06-04 00:21:18 +0000304 }
305
Douglas Gregor199d9912009-06-05 00:53:49 +0000306 // T [] (implied, but not stated explicitly)
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000307 case Type::IncompleteArray: {
308 const IncompleteArrayType *IncompleteArrayArg =
309 Context.getAsIncompleteArrayType(Arg);
310 if (!IncompleteArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000311 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000312
Douglas Gregorf67875d2009-06-12 18:26:56 +0000313 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000314 Context.getAsIncompleteArrayType(Param)->getElementType(),
315 IncompleteArrayArg->getElementType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000316 Info, Deduced);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000317 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000318
319 // T [integer-constant]
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000320 case Type::ConstantArray: {
321 const ConstantArrayType *ConstantArrayArg =
322 Context.getAsConstantArrayType(Arg);
323 if (!ConstantArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000324 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000325
326 const ConstantArrayType *ConstantArrayParm =
327 Context.getAsConstantArrayType(Param);
328 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000329 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000330
Douglas Gregorf67875d2009-06-12 18:26:56 +0000331 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000332 ConstantArrayParm->getElementType(),
333 ConstantArrayArg->getElementType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000334 Info, Deduced);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000335 }
336
Douglas Gregor199d9912009-06-05 00:53:49 +0000337 // type [i]
338 case Type::DependentSizedArray: {
339 const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg);
340 if (!ArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000341 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000342
343 // Check the element type of the arrays
344 const DependentSizedArrayType *DependentArrayParm
345 = cast<DependentSizedArrayType>(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000346 if (Sema::TemplateDeductionResult Result
347 = DeduceTemplateArguments(Context, TemplateParams,
348 DependentArrayParm->getElementType(),
349 ArrayArg->getElementType(),
350 Info, Deduced))
351 return Result;
Douglas Gregor199d9912009-06-05 00:53:49 +0000352
353 // Determine the array bound is something we can deduce.
354 NonTypeTemplateParmDecl *NTTP
355 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
356 if (!NTTP)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000357 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000358
359 // We can perform template argument deduction for the given non-type
360 // template parameter.
361 assert(NTTP->getDepth() == 0 &&
362 "Cannot deduce non-type template argument at depth > 0");
363 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson335e24a2009-06-16 22:44:31 +0000364 = dyn_cast<ConstantArrayType>(ArrayArg)) {
365 llvm::APSInt Size(ConstantArrayArg->getSize());
366 return DeduceNonTypeTemplateArgument(Context, NTTP, Size,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000367 Info, Deduced);
Anders Carlsson335e24a2009-06-16 22:44:31 +0000368 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000369 if (const DependentSizedArrayType *DependentArrayArg
370 = dyn_cast<DependentSizedArrayType>(ArrayArg))
371 return DeduceNonTypeTemplateArgument(Context, NTTP,
372 DependentArrayArg->getSizeExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000373 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000374
375 // Incomplete type does not match a dependently-sized array type
Douglas Gregorf67875d2009-06-12 18:26:56 +0000376 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000377 }
378
Douglas Gregor0fce0ae2009-06-08 15:59:14 +0000379 // type(*)(T)
380 // T(*)()
381 // T(*)(T)
Anders Carlssona27fad52009-06-08 15:19:08 +0000382 case Type::FunctionProto: {
383 const FunctionProtoType *FunctionProtoArg =
384 dyn_cast<FunctionProtoType>(Arg);
385 if (!FunctionProtoArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000386 return Sema::TDK_NonDeducedMismatch;
Anders Carlssona27fad52009-06-08 15:19:08 +0000387
388 const FunctionProtoType *FunctionProtoParam =
389 cast<FunctionProtoType>(Param);
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000390
391 if (FunctionProtoParam->getTypeQuals() !=
392 FunctionProtoArg->getTypeQuals())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000393 return Sema::TDK_NonDeducedMismatch;
Anders Carlssona27fad52009-06-08 15:19:08 +0000394
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000395 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000396 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000397
398 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000399 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000400
Anders Carlssona27fad52009-06-08 15:19:08 +0000401 // Check return types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000402 if (Sema::TemplateDeductionResult Result
403 = DeduceTemplateArguments(Context, TemplateParams,
404 FunctionProtoParam->getResultType(),
405 FunctionProtoArg->getResultType(),
406 Info, Deduced))
407 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000408
Anders Carlssona27fad52009-06-08 15:19:08 +0000409 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
410 // Check argument types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000411 if (Sema::TemplateDeductionResult Result
412 = DeduceTemplateArguments(Context, TemplateParams,
413 FunctionProtoParam->getArgType(I),
414 FunctionProtoArg->getArgType(I),
415 Info, Deduced))
416 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000417 }
418
Douglas Gregorf67875d2009-06-12 18:26:56 +0000419 return Sema::TDK_Success;
Anders Carlssona27fad52009-06-08 15:19:08 +0000420 }
Douglas Gregord708c722009-06-09 16:35:58 +0000421
422 // template-name<T> (wheretemplate-name refers to a class template)
423 // template-name<i>
424 // TT<T> (TODO)
425 // TT<i> (TODO)
426 // TT<> (TODO)
427 case Type::TemplateSpecialization: {
428 const TemplateSpecializationType *SpecParam
429 = cast<TemplateSpecializationType>(Param);
430
431 // Check whether the template argument is a dependent template-id.
432 // FIXME: This is untested code; it can be tested when we implement
433 // partial ordering of class template partial specializations.
434 if (const TemplateSpecializationType *SpecArg
435 = dyn_cast<TemplateSpecializationType>(Arg)) {
436 // Perform template argument deduction for the template name.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000437 if (Sema::TemplateDeductionResult Result
438 = DeduceTemplateArguments(Context,
439 SpecParam->getTemplateName(),
440 SpecArg->getTemplateName(),
441 Info, Deduced))
442 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000443
444 unsigned NumArgs = SpecParam->getNumArgs();
445
446 // FIXME: When one of the template-names refers to a
447 // declaration with default template arguments, do we need to
448 // fill in those default template arguments here? Most likely,
449 // the answer is "yes", but I don't see any references. This
450 // issue may be resolved elsewhere, because we may want to
451 // instantiate default template arguments when
452 if (SpecArg->getNumArgs() != NumArgs)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000453 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000454
455 // Perform template argument deduction on each template
456 // argument.
457 for (unsigned I = 0; I != NumArgs; ++I)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000458 if (Sema::TemplateDeductionResult Result
459 = DeduceTemplateArguments(Context, TemplateParams,
460 SpecParam->getArg(I),
461 SpecArg->getArg(I),
462 Info, Deduced))
463 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000464
Douglas Gregorf67875d2009-06-12 18:26:56 +0000465 return Sema::TDK_Success;
Douglas Gregord708c722009-06-09 16:35:58 +0000466 }
467
468 // If the argument type is a class template specialization, we
469 // perform template argument deduction using its template
470 // arguments.
471 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
472 if (!RecordArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000473 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000474
475 ClassTemplateSpecializationDecl *SpecArg
476 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
477 if (!SpecArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000478 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000479
480 // Perform template argument deduction for the template name.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000481 if (Sema::TemplateDeductionResult Result
482 = DeduceTemplateArguments(Context,
483 SpecParam->getTemplateName(),
484 TemplateName(SpecArg->getSpecializedTemplate()),
485 Info, Deduced))
486 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000487
488 // FIXME: Can the # of arguments in the parameter and the argument differ?
489 unsigned NumArgs = SpecParam->getNumArgs();
490 const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
491 if (NumArgs != ArgArgs.size())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000492 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000493
494 for (unsigned I = 0; I != NumArgs; ++I)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000495 if (Sema::TemplateDeductionResult Result
496 = DeduceTemplateArguments(Context, TemplateParams,
497 SpecParam->getArg(I),
498 ArgArgs.get(I),
499 Info, Deduced))
500 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000501
Douglas Gregorf67875d2009-06-12 18:26:56 +0000502 return Sema::TDK_Success;
Douglas Gregord708c722009-06-09 16:35:58 +0000503 }
504
Douglas Gregor637a4092009-06-10 23:47:09 +0000505 // T type::*
506 // T T::*
507 // T (type::*)()
508 // type (T::*)()
509 // type (type::*)(T)
510 // type (T::*)(T)
511 // T (type::*)(T)
512 // T (T::*)()
513 // T (T::*)(T)
514 case Type::MemberPointer: {
515 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
516 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
517 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000518 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +0000519
Douglas Gregorf67875d2009-06-12 18:26:56 +0000520 if (Sema::TemplateDeductionResult Result
521 = DeduceTemplateArguments(Context, TemplateParams,
522 MemPtrParam->getPointeeType(),
523 MemPtrArg->getPointeeType(),
524 Info, Deduced))
525 return Result;
526
527 return DeduceTemplateArguments(Context, TemplateParams,
528 QualType(MemPtrParam->getClass(), 0),
529 QualType(MemPtrArg->getClass(), 0),
530 Info, Deduced);
Douglas Gregor637a4092009-06-10 23:47:09 +0000531 }
532
Anders Carlsson9a917e42009-06-12 22:56:54 +0000533 // (clang extension)
534 //
Anders Carlsson859ba502009-06-12 16:23:10 +0000535 // type(^)(T)
536 // T(^)()
537 // T(^)(T)
538 case Type::BlockPointer: {
539 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
540 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
541
542 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000543 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson859ba502009-06-12 16:23:10 +0000544
Douglas Gregorf67875d2009-06-12 18:26:56 +0000545 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +0000546 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000547 BlockPtrArg->getPointeeType(), Info,
548 Deduced);
Anders Carlsson859ba502009-06-12 16:23:10 +0000549 }
550
Douglas Gregor637a4092009-06-10 23:47:09 +0000551 case Type::TypeOfExpr:
552 case Type::TypeOf:
553 case Type::Typename:
554 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +0000555 return Sema::TDK_Success;
Douglas Gregor637a4092009-06-10 23:47:09 +0000556
Douglas Gregord560d502009-06-04 00:21:18 +0000557 default:
558 break;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000559 }
560
561 // FIXME: Many more cases to go (to go).
Douglas Gregorf67875d2009-06-12 18:26:56 +0000562 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000563}
564
Douglas Gregorf67875d2009-06-12 18:26:56 +0000565static Sema::TemplateDeductionResult
566DeduceTemplateArguments(ASTContext &Context,
567 TemplateParameterList *TemplateParams,
568 const TemplateArgument &Param,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000569 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000570 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000571 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000572 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000573 case TemplateArgument::Null:
574 assert(false && "Null template argument in parameter list");
575 break;
576
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000577 case TemplateArgument::Type:
Douglas Gregor199d9912009-06-05 00:53:49 +0000578 assert(Arg.getKind() == TemplateArgument::Type && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000579 return DeduceTemplateArguments(Context, TemplateParams,
580 Param.getAsType(),
581 Arg.getAsType(), Info, Deduced);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000582
Douglas Gregor199d9912009-06-05 00:53:49 +0000583 case TemplateArgument::Declaration:
584 // FIXME: Implement this check
585 assert(false && "Unimplemented template argument deduction case");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000586 Info.FirstArg = Param;
587 Info.SecondArg = Arg;
588 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000589
590 case TemplateArgument::Integral:
591 if (Arg.getKind() == TemplateArgument::Integral) {
592 // FIXME: Zero extension + sign checking here?
Douglas Gregorf67875d2009-06-12 18:26:56 +0000593 if (*Param.getAsIntegral() == *Arg.getAsIntegral())
594 return Sema::TDK_Success;
595
596 Info.FirstArg = Param;
597 Info.SecondArg = Arg;
598 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000599 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000600
601 if (Arg.getKind() == TemplateArgument::Expression) {
602 Info.FirstArg = Param;
603 Info.SecondArg = Arg;
604 return Sema::TDK_NonDeducedMismatch;
605 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000606
607 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000608 Info.FirstArg = Param;
609 Info.SecondArg = Arg;
610 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000611
612 case TemplateArgument::Expression: {
613 if (NonTypeTemplateParmDecl *NTTP
614 = getDeducedParameterFromExpr(Param.getAsExpr())) {
615 if (Arg.getKind() == TemplateArgument::Integral)
616 // FIXME: Sign problems here
617 return DeduceNonTypeTemplateArgument(Context, NTTP,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000618 *Arg.getAsIntegral(),
619 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000620 if (Arg.getKind() == TemplateArgument::Expression)
621 return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000622 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000623
624 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000625 Info.FirstArg = Param;
626 Info.SecondArg = Arg;
627 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000628 }
629
630 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000631 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000632 }
Anders Carlssond01b1da2009-06-15 17:04:53 +0000633 case TemplateArgument::Pack:
634 assert(0 && "FIXME: Implement!");
635 break;
Douglas Gregor199d9912009-06-05 00:53:49 +0000636 }
637
Douglas Gregorf67875d2009-06-12 18:26:56 +0000638 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000639}
640
Douglas Gregorf67875d2009-06-12 18:26:56 +0000641static Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000642DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000643 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000644 const TemplateArgumentList &ParamList,
645 const TemplateArgumentList &ArgList,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000646 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000647 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
648 assert(ParamList.size() == ArgList.size());
649 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000650 if (Sema::TemplateDeductionResult Result
651 = DeduceTemplateArguments(Context, TemplateParams,
652 ParamList[I], ArgList[I],
653 Info, Deduced))
654 return Result;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000655 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000656 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000657}
658
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000659/// \brief Perform template argument deduction to determine whether
660/// the given template arguments match the given class template
661/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +0000662Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000663Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000664 const TemplateArgumentList &TemplateArgs,
665 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000666 // C++ [temp.class.spec.match]p2:
667 // A partial specialization matches a given actual template
668 // argument list if the template arguments of the partial
669 // specialization can be deduced from the actual template argument
670 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +0000671 SFINAETrap Trap(*this);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000672 llvm::SmallVector<TemplateArgument, 4> Deduced;
673 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000674 if (TemplateDeductionResult Result
675 = ::DeduceTemplateArguments(Context,
676 Partial->getTemplateParameters(),
677 Partial->getTemplateArgs(),
678 TemplateArgs, Info, Deduced))
679 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +0000680
Douglas Gregor637a4092009-06-10 23:47:09 +0000681 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
682 Deduced.data(), Deduced.size());
683 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000684 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +0000685
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000686 // C++ [temp.deduct.type]p2:
687 // [...] or if any template argument remains neither deduced nor
688 // explicitly specified, template argument deduction fails.
Anders Carlssonfb250522009-06-23 01:26:57 +0000689 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
690 Deduced.size());
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000691 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000692 if (Deduced[I].isNull()) {
693 Decl *Param
694 = const_cast<Decl *>(Partial->getTemplateParameters()->getParam(I));
695 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
696 Info.Param = TTP;
697 else if (NonTypeTemplateParmDecl *NTTP
698 = dyn_cast<NonTypeTemplateParmDecl>(Param))
699 Info.Param = NTTP;
700 else
701 Info.Param = cast<TemplateTemplateParmDecl>(Param);
702 return TDK_Incomplete;
703 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000704
Anders Carlssonfb250522009-06-23 01:26:57 +0000705 Builder.Append(Deduced[I]);
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000706 }
707
708 // Form the template argument list from the deduced template arguments.
709 TemplateArgumentList *DeducedArgumentList
Anders Carlssonfb250522009-06-23 01:26:57 +0000710 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000711 Info.reset(DeducedArgumentList);
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000712
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000713 // Substitute the deduced template arguments into the template
714 // arguments of the class template partial specialization, and
715 // verify that the instantiated template arguments are both valid
716 // and are equivalent to the template arguments originally provided
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000717 // to the class template.
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000718 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
719 const TemplateArgumentList &PartialTemplateArgs = Partial->getTemplateArgs();
720 for (unsigned I = 0, N = PartialTemplateArgs.flat_size(); I != N; ++I) {
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000721 Decl *Param = const_cast<Decl *>(
722 ClassTemplate->getTemplateParameters()->getParam(I));
723 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
724 TemplateArgument InstArg = Instantiate(PartialTemplateArgs[I],
725 *DeducedArgumentList);
726 if (InstArg.getKind() != TemplateArgument::Type) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000727 Info.Param = TTP;
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000728 Info.FirstArg = PartialTemplateArgs[I];
729 return TDK_SubstitutionFailure;
730 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000731
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000732 if (Context.getCanonicalType(InstArg.getAsType())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000733 != Context.getCanonicalType(TemplateArgs[I].getAsType())) {
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000734 Info.Param = TTP;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000735 Info.FirstArg = TemplateArgs[I];
736 Info.SecondArg = InstArg;
737 return TDK_NonDeducedMismatch;
738 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000739
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000740 continue;
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000741 }
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000742
743 // FIXME: Check template template arguments?
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000744 }
745
Douglas Gregorbb260412009-06-14 08:02:22 +0000746 if (Trap.hasErrorOccurred())
747 return TDK_SubstitutionFailure;
748
Douglas Gregorf67875d2009-06-12 18:26:56 +0000749 return TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000750}
Douglas Gregor031a5882009-06-13 00:26:55 +0000751
Douglas Gregore53060f2009-06-25 22:08:12 +0000752/// \brief Perform template argument deduction from a function call
753/// (C++ [temp.deduct.call]).
754///
755/// \param FunctionTemplate the function template for which we are performing
756/// template argument deduction.
757///
758/// \param Args the function call arguments
759///
760/// \param NumArgs the number of arguments in Args
761///
762/// \param Specialization if template argument deduction was successful,
763/// this will be set to the function template specialization produced by
764/// template argument deduction.
765///
766/// \param Info the argument will be updated to provide additional information
767/// about template argument deduction.
768///
769/// \returns the result of template argument deduction.
770///
771/// FIXME: We will also need to pass in any explicitly-specified template
772/// arguments.
773Sema::TemplateDeductionResult
774Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
775 Expr **Args, unsigned NumArgs,
776 FunctionDecl *&Specialization,
777 TemplateDeductionInfo &Info) {
778 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
779
780 // C++ [temp.deduct.call]p1:
781 // Template argument deduction is done by comparing each function template
782 // parameter type (call it P) with the type of the corresponding argument
783 // of the call (call it A) as described below.
784 unsigned CheckArgs = NumArgs;
785 if (NumArgs < Function->getNumParams())
786 return TDK_TooFewArguments;
787 else if (NumArgs > Function->getNumParams()) {
788 const FunctionProtoType *Proto
789 = Function->getType()->getAsFunctionProtoType();
790 if (!Proto->isVariadic())
791 return TDK_TooManyArguments;
792
793 CheckArgs = Function->getNumParams();
794 }
795
Douglas Gregor500d3312009-06-26 18:27:22 +0000796 // Template argument deduction for function templates in a SFINAE context.
797 // Trap any errors that might occur.
Douglas Gregore53060f2009-06-25 22:08:12 +0000798 SFINAETrap Trap(*this);
Douglas Gregor500d3312009-06-26 18:27:22 +0000799
800 // Deduce template arguments from the function parameters.
Douglas Gregore53060f2009-06-25 22:08:12 +0000801 llvm::SmallVector<TemplateArgument, 4> Deduced;
802 Deduced.resize(FunctionTemplate->getTemplateParameters()->size());
803 TemplateParameterList *TemplateParams
804 = FunctionTemplate->getTemplateParameters();
805 for (unsigned I = 0; I != CheckArgs; ++I) {
806 QualType ParamType = Function->getParamDecl(I)->getType();
807 QualType ArgType = Args[I]->getType();
Douglas Gregor500d3312009-06-26 18:27:22 +0000808
Douglas Gregore53060f2009-06-25 22:08:12 +0000809 // C++ [temp.deduct.call]p2:
810 // If P is not a reference type:
811 QualType CanonParamType = Context.getCanonicalType(ParamType);
Douglas Gregor500d3312009-06-26 18:27:22 +0000812 bool ParamWasReference = isa<ReferenceType>(CanonParamType);
813 if (!ParamWasReference) {
Douglas Gregore53060f2009-06-25 22:08:12 +0000814 // - If A is an array type, the pointer type produced by the
815 // array-to-pointer standard conversion (4.2) is used in place of
816 // A for type deduction; otherwise,
817 if (ArgType->isArrayType())
818 ArgType = Context.getArrayDecayedType(ArgType);
819 // - If A is a function type, the pointer type produced by the
820 // function-to-pointer standard conversion (4.3) is used in place
821 // of A for type deduction; otherwise,
822 else if (ArgType->isFunctionType())
823 ArgType = Context.getPointerType(ArgType);
824 else {
825 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
826 // type are ignored for type deduction.
827 QualType CanonArgType = Context.getCanonicalType(ArgType);
828 if (CanonArgType.getCVRQualifiers())
829 ArgType = CanonArgType.getUnqualifiedType();
830 }
831 }
832
833 // C++0x [temp.deduct.call]p3:
834 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
835 // are ignored for type deduction.
836 if (CanonParamType.getCVRQualifiers())
837 ParamType = CanonParamType.getUnqualifiedType();
838 if (const ReferenceType *ParamRefType = ParamType->getAsReferenceType()) {
839 // [...] If P is a reference type, the type referred to by P is used
840 // for type deduction.
841 ParamType = ParamRefType->getPointeeType();
842
843 // [...] If P is of the form T&&, where T is a template parameter, and
844 // the argument is an lvalue, the type A& is used in place of A for
845 // type deduction.
846 if (isa<RValueReferenceType>(ParamRefType) &&
847 ParamRefType->getAsTemplateTypeParmType() &&
848 Args[I]->isLvalue(Context) == Expr::LV_Valid)
849 ArgType = Context.getLValueReferenceType(ArgType);
850 }
851
852 // C++0x [temp.deduct.call]p4:
853 // In general, the deduction process attempts to find template argument
854 // values that will make the deduced A identical to A (after the type A
855 // is transformed as described above). [...]
856 //
857 // FIXME: we'll pass down a flag to indicate when these cases may apply,
858 // and then deal with them in the code that deduces template
859 // arguments from a type.
860 if (TemplateDeductionResult Result
861 = ::DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor500d3312009-06-26 18:27:22 +0000862 ParamType, ArgType, Info, Deduced,
863 ParamWasReference))
Douglas Gregore53060f2009-06-25 22:08:12 +0000864 return Result;
865
866 // FIXME: C++ [temp.deduct.call] paragraphs 6-9 deal with function
867 // pointer parameters.
868 }
869
870 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
871 FunctionTemplate, Deduced.data(), Deduced.size());
872 if (Inst)
873 return TDK_InstantiationDepth;
874
875 // C++ [temp.deduct.type]p2:
876 // [...] or if any template argument remains neither deduced nor
877 // explicitly specified, template argument deduction fails.
878 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
879 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
880 if (Deduced[I].isNull()) {
881 Decl *Param
882 = const_cast<Decl *>(TemplateParams->getParam(I));
883 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
884 Info.Param = TTP;
885 else if (NonTypeTemplateParmDecl *NTTP
886 = dyn_cast<NonTypeTemplateParmDecl>(Param))
887 Info.Param = NTTP;
888 else
889 Info.Param = cast<TemplateTemplateParmDecl>(Param);
890 return TDK_Incomplete;
891 }
892
893 Builder.Append(Deduced[I]);
894 }
895
896 // Form the template argument list from the deduced template arguments.
897 TemplateArgumentList *DeducedArgumentList
898 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
899 Info.reset(DeducedArgumentList);
900
901 // Substitute the deduced template arguments into the function template
902 // declaration to produce the function template specialization.
903 Specialization = cast_or_null<FunctionDecl>(
904 InstantiateDecl(FunctionTemplate->getTemplatedDecl(),
905 FunctionTemplate->getDeclContext(),
906 *DeducedArgumentList));
907
908 if (!Specialization || Trap.hasErrorOccurred())
909 return TDK_SubstitutionFailure;
910
Douglas Gregor1637be72009-06-26 00:10:03 +0000911 // Turn the specialization into an actual function template specialization.
912 Specialization->setFunctionTemplateSpecialization(Context,
913 FunctionTemplate,
914 Info.take());
Douglas Gregore53060f2009-06-25 22:08:12 +0000915 return TDK_Success;
916}
917
Douglas Gregor031a5882009-06-13 00:26:55 +0000918static void
919MarkDeducedTemplateParameters(Sema &SemaRef,
920 const TemplateArgument &TemplateArg,
921 llvm::SmallVectorImpl<bool> &Deduced);
922
923/// \brief Mark the template arguments that are deduced by the given
924/// expression.
925static void
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000926MarkDeducedTemplateParameters(const Expr *E,
927 llvm::SmallVectorImpl<bool> &Deduced) {
928 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregor031a5882009-06-13 00:26:55 +0000929 if (!E)
930 return;
931
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000932 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +0000933 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
934 if (!NTTP)
935 return;
936
937 Deduced[NTTP->getIndex()] = true;
938}
939
940/// \brief Mark the template parameters that are deduced by the given
941/// type.
942static void
943MarkDeducedTemplateParameters(Sema &SemaRef, QualType T,
944 llvm::SmallVectorImpl<bool> &Deduced) {
945 // Non-dependent types have nothing deducible
946 if (!T->isDependentType())
947 return;
948
949 T = SemaRef.Context.getCanonicalType(T);
950 switch (T->getTypeClass()) {
951 case Type::ExtQual:
952 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000953 QualType(cast<ExtQualType>(T)->getBaseType(), 0),
Douglas Gregor031a5882009-06-13 00:26:55 +0000954 Deduced);
955 break;
956
957 case Type::Pointer:
958 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000959 cast<PointerType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +0000960 Deduced);
961 break;
962
963 case Type::BlockPointer:
964 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000965 cast<BlockPointerType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +0000966 Deduced);
967 break;
968
969 case Type::LValueReference:
970 case Type::RValueReference:
971 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000972 cast<ReferenceType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +0000973 Deduced);
974 break;
975
976 case Type::MemberPointer: {
977 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
978 MarkDeducedTemplateParameters(SemaRef, MemPtr->getPointeeType(), Deduced);
979 MarkDeducedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
980 Deduced);
981 break;
982 }
983
984 case Type::DependentSizedArray:
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000985 MarkDeducedTemplateParameters(cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregor031a5882009-06-13 00:26:55 +0000986 Deduced);
987 // Fall through to check the element type
988
989 case Type::ConstantArray:
990 case Type::IncompleteArray:
991 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000992 cast<ArrayType>(T)->getElementType(),
Douglas Gregor031a5882009-06-13 00:26:55 +0000993 Deduced);
994 break;
995
996 case Type::Vector:
997 case Type::ExtVector:
998 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000999 cast<VectorType>(T)->getElementType(),
Douglas Gregor031a5882009-06-13 00:26:55 +00001000 Deduced);
1001 break;
1002
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001003 case Type::DependentSizedExtVector: {
1004 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001005 = cast<DependentSizedExtVectorType>(T);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001006 MarkDeducedTemplateParameters(SemaRef, VecType->getElementType(), Deduced);
1007 MarkDeducedTemplateParameters(VecType->getSizeExpr(), Deduced);
1008 break;
1009 }
1010
Douglas Gregor031a5882009-06-13 00:26:55 +00001011 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001012 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregor031a5882009-06-13 00:26:55 +00001013 MarkDeducedTemplateParameters(SemaRef, Proto->getResultType(), Deduced);
1014 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
1015 MarkDeducedTemplateParameters(SemaRef, Proto->getArgType(I), Deduced);
1016 break;
1017 }
1018
1019 case Type::TemplateTypeParm:
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001020 Deduced[cast<TemplateTypeParmType>(T)->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00001021 break;
1022
1023 case Type::TemplateSpecialization: {
1024 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00001025 = cast<TemplateSpecializationType>(T);
Douglas Gregor031a5882009-06-13 00:26:55 +00001026 if (TemplateDecl *Template = Spec->getTemplateName().getAsTemplateDecl())
1027 if (TemplateTemplateParmDecl *TTP
1028 = dyn_cast<TemplateTemplateParmDecl>(Template))
1029 Deduced[TTP->getIndex()] = true;
1030
1031 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
1032 MarkDeducedTemplateParameters(SemaRef, Spec->getArg(I), Deduced);
1033
1034 break;
1035 }
1036
1037 // None of these types have any deducible parts.
1038 case Type::Builtin:
1039 case Type::FixedWidthInt:
1040 case Type::Complex:
1041 case Type::VariableArray:
1042 case Type::FunctionNoProto:
1043 case Type::Record:
1044 case Type::Enum:
1045 case Type::Typename:
1046 case Type::ObjCInterface:
1047 case Type::ObjCQualifiedInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001048 case Type::ObjCObjectPointer:
Douglas Gregor031a5882009-06-13 00:26:55 +00001049#define TYPE(Class, Base)
1050#define ABSTRACT_TYPE(Class, Base)
1051#define DEPENDENT_TYPE(Class, Base)
1052#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
1053#include "clang/AST/TypeNodes.def"
1054 break;
1055 }
1056}
1057
1058/// \brief Mark the template parameters that are deduced by this
1059/// template argument.
1060static void
1061MarkDeducedTemplateParameters(Sema &SemaRef,
1062 const TemplateArgument &TemplateArg,
1063 llvm::SmallVectorImpl<bool> &Deduced) {
1064 switch (TemplateArg.getKind()) {
1065 case TemplateArgument::Null:
1066 case TemplateArgument::Integral:
1067 break;
1068
1069 case TemplateArgument::Type:
1070 MarkDeducedTemplateParameters(SemaRef, TemplateArg.getAsType(), Deduced);
1071 break;
1072
1073 case TemplateArgument::Declaration:
1074 if (TemplateTemplateParmDecl *TTP
1075 = dyn_cast<TemplateTemplateParmDecl>(TemplateArg.getAsDecl()))
1076 Deduced[TTP->getIndex()] = true;
1077 break;
1078
1079 case TemplateArgument::Expression:
1080 MarkDeducedTemplateParameters(TemplateArg.getAsExpr(), Deduced);
1081 break;
Anders Carlssond01b1da2009-06-15 17:04:53 +00001082 case TemplateArgument::Pack:
1083 assert(0 && "FIXME: Implement!");
1084 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00001085 }
1086}
1087
1088/// \brief Mark the template parameters can be deduced by the given
1089/// template argument list.
1090///
1091/// \param TemplateArgs the template argument list from which template
1092/// parameters will be deduced.
1093///
1094/// \param Deduced a bit vector whose elements will be set to \c true
1095/// to indicate when the corresponding template parameter will be
1096/// deduced.
1097void
1098Sema::MarkDeducedTemplateParameters(const TemplateArgumentList &TemplateArgs,
1099 llvm::SmallVectorImpl<bool> &Deduced) {
1100 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
1101 ::MarkDeducedTemplateParameters(*this, TemplateArgs[I], Deduced);
1102}