blob: 1d7e03eda3a6625594c7177f3996a40f1366b67d [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 Gregorf67875d2009-06-12 18:26:56 +0000159static Sema::TemplateDeductionResult
160DeduceTemplateArguments(ASTContext &Context,
161 TemplateParameterList *TemplateParams,
162 QualType ParamIn, QualType ArgIn,
163 Sema::TemplateDeductionInfo &Info,
164 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000165 // We only want to look at the canonical types, since typedefs and
166 // sugar are not part of template argument deduction.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000167 QualType Param = Context.getCanonicalType(ParamIn);
168 QualType Arg = Context.getCanonicalType(ArgIn);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000169
170 // If the parameter type is not dependent, just compare the types
171 // directly.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000172 if (!Param->isDependentType()) {
173 if (Param == Arg)
174 return Sema::TDK_Success;
175
176 Info.FirstArg = TemplateArgument(SourceLocation(), ParamIn);
177 Info.SecondArg = TemplateArgument(SourceLocation(), ArgIn);
178 return Sema::TDK_NonDeducedMismatch;
179 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000180
Douglas Gregor199d9912009-06-05 00:53:49 +0000181 // C++ [temp.deduct.type]p9:
Douglas Gregor199d9912009-06-05 00:53:49 +0000182 // A template type argument T, a template template argument TT or a
183 // template non-type argument i can be deduced if P and A have one of
184 // the following forms:
185 //
186 // T
187 // cv-list T
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000188 if (const TemplateTypeParmType *TemplateTypeParm
189 = Param->getAsTemplateTypeParmType()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000190 unsigned Index = TemplateTypeParm->getIndex();
191
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000192 // The argument type can not be less qualified than the parameter
193 // type.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000194 if (Param.isMoreQualifiedThan(Arg)) {
195 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
196 Info.FirstArg = Deduced[Index];
197 Info.SecondArg = TemplateArgument(SourceLocation(), Arg);
198 return Sema::TDK_InconsistentQuals;
199 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000200
201 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
202
203 unsigned Quals = Arg.getCVRQualifiers() & ~Param.getCVRQualifiers();
204 QualType DeducedType = Arg.getQualifiedType(Quals);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000205
206 if (Deduced[Index].isNull())
207 Deduced[Index] = TemplateArgument(SourceLocation(), DeducedType);
208 else {
209 // C++ [temp.deduct.type]p2:
210 // [...] If type deduction cannot be done for any P/A pair, or if for
211 // any pair the deduction leads to more than one possible set of
212 // deduced values, or if different pairs yield different deduced
213 // values, or if any template argument remains neither deduced nor
214 // explicitly specified, template argument deduction fails.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000215 if (Deduced[Index].getAsType() != DeducedType) {
216 Info.Param
217 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
218 Info.FirstArg = Deduced[Index];
219 Info.SecondArg = TemplateArgument(SourceLocation(), Arg);
220 return Sema::TDK_Inconsistent;
221 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000222 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000223 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000224 }
225
Douglas Gregorf67875d2009-06-12 18:26:56 +0000226 // Set up the template argument deduction information for a failure.
227 Info.FirstArg = TemplateArgument(SourceLocation(), ParamIn);
228 Info.SecondArg = TemplateArgument(SourceLocation(), ArgIn);
229
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000230 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000231 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000232
Douglas Gregord560d502009-06-04 00:21:18 +0000233 switch (Param->getTypeClass()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000234 // No deduction possible for these types
235 case Type::Builtin:
Douglas Gregorf67875d2009-06-12 18:26:56 +0000236 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000237
238 // T *
Douglas Gregord560d502009-06-04 00:21:18 +0000239 case Type::Pointer: {
240 const PointerType *PointerArg = Arg->getAsPointerType();
241 if (!PointerArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000242 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord560d502009-06-04 00:21:18 +0000243
Douglas Gregorf67875d2009-06-12 18:26:56 +0000244 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000245 cast<PointerType>(Param)->getPointeeType(),
246 PointerArg->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000247 Info, Deduced);
Douglas Gregord560d502009-06-04 00:21:18 +0000248 }
249
Douglas Gregor199d9912009-06-05 00:53:49 +0000250 // T &
Douglas Gregord560d502009-06-04 00:21:18 +0000251 case Type::LValueReference: {
252 const LValueReferenceType *ReferenceArg = Arg->getAsLValueReferenceType();
253 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000254 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord560d502009-06-04 00:21:18 +0000255
Douglas Gregorf67875d2009-06-12 18:26:56 +0000256 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000257 cast<LValueReferenceType>(Param)->getPointeeType(),
258 ReferenceArg->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000259 Info, Deduced);
Douglas Gregord560d502009-06-04 00:21:18 +0000260 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000261
Douglas Gregor199d9912009-06-05 00:53:49 +0000262 // T && [C++0x]
Douglas Gregord560d502009-06-04 00:21:18 +0000263 case Type::RValueReference: {
264 const RValueReferenceType *ReferenceArg = Arg->getAsRValueReferenceType();
265 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000266 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord560d502009-06-04 00:21:18 +0000267
Douglas Gregorf67875d2009-06-12 18:26:56 +0000268 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000269 cast<RValueReferenceType>(Param)->getPointeeType(),
270 ReferenceArg->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000271 Info, Deduced);
Douglas Gregord560d502009-06-04 00:21:18 +0000272 }
273
Douglas Gregor199d9912009-06-05 00:53:49 +0000274 // T [] (implied, but not stated explicitly)
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000275 case Type::IncompleteArray: {
276 const IncompleteArrayType *IncompleteArrayArg =
277 Context.getAsIncompleteArrayType(Arg);
278 if (!IncompleteArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000279 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000280
Douglas Gregorf67875d2009-06-12 18:26:56 +0000281 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000282 Context.getAsIncompleteArrayType(Param)->getElementType(),
283 IncompleteArrayArg->getElementType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000284 Info, Deduced);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000285 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000286
287 // T [integer-constant]
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000288 case Type::ConstantArray: {
289 const ConstantArrayType *ConstantArrayArg =
290 Context.getAsConstantArrayType(Arg);
291 if (!ConstantArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000292 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000293
294 const ConstantArrayType *ConstantArrayParm =
295 Context.getAsConstantArrayType(Param);
296 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000297 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000298
Douglas Gregorf67875d2009-06-12 18:26:56 +0000299 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000300 ConstantArrayParm->getElementType(),
301 ConstantArrayArg->getElementType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000302 Info, Deduced);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000303 }
304
Douglas Gregor199d9912009-06-05 00:53:49 +0000305 // type [i]
306 case Type::DependentSizedArray: {
307 const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg);
308 if (!ArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000309 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000310
311 // Check the element type of the arrays
312 const DependentSizedArrayType *DependentArrayParm
313 = cast<DependentSizedArrayType>(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000314 if (Sema::TemplateDeductionResult Result
315 = DeduceTemplateArguments(Context, TemplateParams,
316 DependentArrayParm->getElementType(),
317 ArrayArg->getElementType(),
318 Info, Deduced))
319 return Result;
Douglas Gregor199d9912009-06-05 00:53:49 +0000320
321 // Determine the array bound is something we can deduce.
322 NonTypeTemplateParmDecl *NTTP
323 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
324 if (!NTTP)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000325 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000326
327 // We can perform template argument deduction for the given non-type
328 // template parameter.
329 assert(NTTP->getDepth() == 0 &&
330 "Cannot deduce non-type template argument at depth > 0");
331 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson335e24a2009-06-16 22:44:31 +0000332 = dyn_cast<ConstantArrayType>(ArrayArg)) {
333 llvm::APSInt Size(ConstantArrayArg->getSize());
334 return DeduceNonTypeTemplateArgument(Context, NTTP, Size,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000335 Info, Deduced);
Anders Carlsson335e24a2009-06-16 22:44:31 +0000336 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000337 if (const DependentSizedArrayType *DependentArrayArg
338 = dyn_cast<DependentSizedArrayType>(ArrayArg))
339 return DeduceNonTypeTemplateArgument(Context, NTTP,
340 DependentArrayArg->getSizeExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000341 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000342
343 // Incomplete type does not match a dependently-sized array type
Douglas Gregorf67875d2009-06-12 18:26:56 +0000344 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000345 }
346
Douglas Gregor0fce0ae2009-06-08 15:59:14 +0000347 // type(*)(T)
348 // T(*)()
349 // T(*)(T)
Anders Carlssona27fad52009-06-08 15:19:08 +0000350 case Type::FunctionProto: {
351 const FunctionProtoType *FunctionProtoArg =
352 dyn_cast<FunctionProtoType>(Arg);
353 if (!FunctionProtoArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000354 return Sema::TDK_NonDeducedMismatch;
Anders Carlssona27fad52009-06-08 15:19:08 +0000355
356 const FunctionProtoType *FunctionProtoParam =
357 cast<FunctionProtoType>(Param);
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000358
359 if (FunctionProtoParam->getTypeQuals() !=
360 FunctionProtoArg->getTypeQuals())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000361 return Sema::TDK_NonDeducedMismatch;
Anders Carlssona27fad52009-06-08 15:19:08 +0000362
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000363 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000364 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000365
366 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000367 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000368
Anders Carlssona27fad52009-06-08 15:19:08 +0000369 // Check return types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000370 if (Sema::TemplateDeductionResult Result
371 = DeduceTemplateArguments(Context, TemplateParams,
372 FunctionProtoParam->getResultType(),
373 FunctionProtoArg->getResultType(),
374 Info, Deduced))
375 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000376
Anders Carlssona27fad52009-06-08 15:19:08 +0000377 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
378 // Check argument types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000379 if (Sema::TemplateDeductionResult Result
380 = DeduceTemplateArguments(Context, TemplateParams,
381 FunctionProtoParam->getArgType(I),
382 FunctionProtoArg->getArgType(I),
383 Info, Deduced))
384 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000385 }
386
Douglas Gregorf67875d2009-06-12 18:26:56 +0000387 return Sema::TDK_Success;
Anders Carlssona27fad52009-06-08 15:19:08 +0000388 }
Douglas Gregord708c722009-06-09 16:35:58 +0000389
390 // template-name<T> (wheretemplate-name refers to a class template)
391 // template-name<i>
392 // TT<T> (TODO)
393 // TT<i> (TODO)
394 // TT<> (TODO)
395 case Type::TemplateSpecialization: {
396 const TemplateSpecializationType *SpecParam
397 = cast<TemplateSpecializationType>(Param);
398
399 // Check whether the template argument is a dependent template-id.
400 // FIXME: This is untested code; it can be tested when we implement
401 // partial ordering of class template partial specializations.
402 if (const TemplateSpecializationType *SpecArg
403 = dyn_cast<TemplateSpecializationType>(Arg)) {
404 // Perform template argument deduction for the template name.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000405 if (Sema::TemplateDeductionResult Result
406 = DeduceTemplateArguments(Context,
407 SpecParam->getTemplateName(),
408 SpecArg->getTemplateName(),
409 Info, Deduced))
410 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000411
412 unsigned NumArgs = SpecParam->getNumArgs();
413
414 // FIXME: When one of the template-names refers to a
415 // declaration with default template arguments, do we need to
416 // fill in those default template arguments here? Most likely,
417 // the answer is "yes", but I don't see any references. This
418 // issue may be resolved elsewhere, because we may want to
419 // instantiate default template arguments when
420 if (SpecArg->getNumArgs() != NumArgs)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000421 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000422
423 // Perform template argument deduction on each template
424 // argument.
425 for (unsigned I = 0; I != NumArgs; ++I)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000426 if (Sema::TemplateDeductionResult Result
427 = DeduceTemplateArguments(Context, TemplateParams,
428 SpecParam->getArg(I),
429 SpecArg->getArg(I),
430 Info, Deduced))
431 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000432
Douglas Gregorf67875d2009-06-12 18:26:56 +0000433 return Sema::TDK_Success;
Douglas Gregord708c722009-06-09 16:35:58 +0000434 }
435
436 // If the argument type is a class template specialization, we
437 // perform template argument deduction using its template
438 // arguments.
439 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
440 if (!RecordArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000441 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000442
443 ClassTemplateSpecializationDecl *SpecArg
444 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
445 if (!SpecArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000446 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000447
448 // Perform template argument deduction for the template name.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000449 if (Sema::TemplateDeductionResult Result
450 = DeduceTemplateArguments(Context,
451 SpecParam->getTemplateName(),
452 TemplateName(SpecArg->getSpecializedTemplate()),
453 Info, Deduced))
454 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000455
456 // FIXME: Can the # of arguments in the parameter and the argument differ?
457 unsigned NumArgs = SpecParam->getNumArgs();
458 const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
459 if (NumArgs != ArgArgs.size())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000460 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000461
462 for (unsigned I = 0; I != NumArgs; ++I)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000463 if (Sema::TemplateDeductionResult Result
464 = DeduceTemplateArguments(Context, TemplateParams,
465 SpecParam->getArg(I),
466 ArgArgs.get(I),
467 Info, Deduced))
468 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000469
Douglas Gregorf67875d2009-06-12 18:26:56 +0000470 return Sema::TDK_Success;
Douglas Gregord708c722009-06-09 16:35:58 +0000471 }
472
Douglas Gregor637a4092009-06-10 23:47:09 +0000473 // T type::*
474 // T T::*
475 // T (type::*)()
476 // type (T::*)()
477 // type (type::*)(T)
478 // type (T::*)(T)
479 // T (type::*)(T)
480 // T (T::*)()
481 // T (T::*)(T)
482 case Type::MemberPointer: {
483 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
484 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
485 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000486 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +0000487
Douglas Gregorf67875d2009-06-12 18:26:56 +0000488 if (Sema::TemplateDeductionResult Result
489 = DeduceTemplateArguments(Context, TemplateParams,
490 MemPtrParam->getPointeeType(),
491 MemPtrArg->getPointeeType(),
492 Info, Deduced))
493 return Result;
494
495 return DeduceTemplateArguments(Context, TemplateParams,
496 QualType(MemPtrParam->getClass(), 0),
497 QualType(MemPtrArg->getClass(), 0),
498 Info, Deduced);
Douglas Gregor637a4092009-06-10 23:47:09 +0000499 }
500
Anders Carlsson9a917e42009-06-12 22:56:54 +0000501 // (clang extension)
502 //
Anders Carlsson859ba502009-06-12 16:23:10 +0000503 // type(^)(T)
504 // T(^)()
505 // T(^)(T)
506 case Type::BlockPointer: {
507 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
508 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
509
510 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000511 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson859ba502009-06-12 16:23:10 +0000512
Douglas Gregorf67875d2009-06-12 18:26:56 +0000513 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +0000514 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000515 BlockPtrArg->getPointeeType(), Info,
516 Deduced);
Anders Carlsson859ba502009-06-12 16:23:10 +0000517 }
518
Douglas Gregor637a4092009-06-10 23:47:09 +0000519 case Type::TypeOfExpr:
520 case Type::TypeOf:
521 case Type::Typename:
522 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +0000523 return Sema::TDK_Success;
Douglas Gregor637a4092009-06-10 23:47:09 +0000524
Douglas Gregord560d502009-06-04 00:21:18 +0000525 default:
526 break;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000527 }
528
529 // FIXME: Many more cases to go (to go).
Douglas Gregorf67875d2009-06-12 18:26:56 +0000530 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000531}
532
Douglas Gregorf67875d2009-06-12 18:26:56 +0000533static Sema::TemplateDeductionResult
534DeduceTemplateArguments(ASTContext &Context,
535 TemplateParameterList *TemplateParams,
536 const TemplateArgument &Param,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000537 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000538 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000539 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000540 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000541 case TemplateArgument::Null:
542 assert(false && "Null template argument in parameter list");
543 break;
544
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000545 case TemplateArgument::Type:
Douglas Gregor199d9912009-06-05 00:53:49 +0000546 assert(Arg.getKind() == TemplateArgument::Type && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000547 return DeduceTemplateArguments(Context, TemplateParams,
548 Param.getAsType(),
549 Arg.getAsType(), Info, Deduced);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000550
Douglas Gregor199d9912009-06-05 00:53:49 +0000551 case TemplateArgument::Declaration:
552 // FIXME: Implement this check
553 assert(false && "Unimplemented template argument deduction case");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000554 Info.FirstArg = Param;
555 Info.SecondArg = Arg;
556 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000557
558 case TemplateArgument::Integral:
559 if (Arg.getKind() == TemplateArgument::Integral) {
560 // FIXME: Zero extension + sign checking here?
Douglas Gregorf67875d2009-06-12 18:26:56 +0000561 if (*Param.getAsIntegral() == *Arg.getAsIntegral())
562 return Sema::TDK_Success;
563
564 Info.FirstArg = Param;
565 Info.SecondArg = Arg;
566 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000567 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000568
569 if (Arg.getKind() == TemplateArgument::Expression) {
570 Info.FirstArg = Param;
571 Info.SecondArg = Arg;
572 return Sema::TDK_NonDeducedMismatch;
573 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000574
575 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000576 Info.FirstArg = Param;
577 Info.SecondArg = Arg;
578 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000579
580 case TemplateArgument::Expression: {
581 if (NonTypeTemplateParmDecl *NTTP
582 = getDeducedParameterFromExpr(Param.getAsExpr())) {
583 if (Arg.getKind() == TemplateArgument::Integral)
584 // FIXME: Sign problems here
585 return DeduceNonTypeTemplateArgument(Context, NTTP,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000586 *Arg.getAsIntegral(),
587 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000588 if (Arg.getKind() == TemplateArgument::Expression)
589 return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000590 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000591
592 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000593 Info.FirstArg = Param;
594 Info.SecondArg = Arg;
595 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000596 }
597
598 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000599 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000600 }
Anders Carlssond01b1da2009-06-15 17:04:53 +0000601 case TemplateArgument::Pack:
602 assert(0 && "FIXME: Implement!");
603 break;
Douglas Gregor199d9912009-06-05 00:53:49 +0000604 }
605
Douglas Gregorf67875d2009-06-12 18:26:56 +0000606 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000607}
608
Douglas Gregorf67875d2009-06-12 18:26:56 +0000609static Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000610DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000611 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000612 const TemplateArgumentList &ParamList,
613 const TemplateArgumentList &ArgList,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000614 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000615 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
616 assert(ParamList.size() == ArgList.size());
617 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000618 if (Sema::TemplateDeductionResult Result
619 = DeduceTemplateArguments(Context, TemplateParams,
620 ParamList[I], ArgList[I],
621 Info, Deduced))
622 return Result;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000623 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000624 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000625}
626
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000627/// \brief Perform template argument deduction to determine whether
628/// the given template arguments match the given class template
629/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +0000630Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000631Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000632 const TemplateArgumentList &TemplateArgs,
633 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000634 // C++ [temp.class.spec.match]p2:
635 // A partial specialization matches a given actual template
636 // argument list if the template arguments of the partial
637 // specialization can be deduced from the actual template argument
638 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +0000639 SFINAETrap Trap(*this);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000640 llvm::SmallVector<TemplateArgument, 4> Deduced;
641 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000642 if (TemplateDeductionResult Result
643 = ::DeduceTemplateArguments(Context,
644 Partial->getTemplateParameters(),
645 Partial->getTemplateArgs(),
646 TemplateArgs, Info, Deduced))
647 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +0000648
Douglas Gregor637a4092009-06-10 23:47:09 +0000649 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
650 Deduced.data(), Deduced.size());
651 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000652 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +0000653
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000654 // C++ [temp.deduct.type]p2:
655 // [...] or if any template argument remains neither deduced nor
656 // explicitly specified, template argument deduction fails.
Anders Carlssonfb250522009-06-23 01:26:57 +0000657 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
658 Deduced.size());
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000659 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000660 if (Deduced[I].isNull()) {
661 Decl *Param
662 = const_cast<Decl *>(Partial->getTemplateParameters()->getParam(I));
663 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
664 Info.Param = TTP;
665 else if (NonTypeTemplateParmDecl *NTTP
666 = dyn_cast<NonTypeTemplateParmDecl>(Param))
667 Info.Param = NTTP;
668 else
669 Info.Param = cast<TemplateTemplateParmDecl>(Param);
670 return TDK_Incomplete;
671 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000672
Anders Carlssonfb250522009-06-23 01:26:57 +0000673 Builder.Append(Deduced[I]);
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000674 }
675
676 // Form the template argument list from the deduced template arguments.
677 TemplateArgumentList *DeducedArgumentList
Anders Carlssonfb250522009-06-23 01:26:57 +0000678 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000679 Info.reset(DeducedArgumentList);
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000680
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000681 // Substitute the deduced template arguments into the template
682 // arguments of the class template partial specialization, and
683 // verify that the instantiated template arguments are both valid
684 // and are equivalent to the template arguments originally provided
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000685 // to the class template.
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000686 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
687 const TemplateArgumentList &PartialTemplateArgs = Partial->getTemplateArgs();
688 for (unsigned I = 0, N = PartialTemplateArgs.flat_size(); I != N; ++I) {
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000689 Decl *Param = const_cast<Decl *>(
690 ClassTemplate->getTemplateParameters()->getParam(I));
691 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
692 TemplateArgument InstArg = Instantiate(PartialTemplateArgs[I],
693 *DeducedArgumentList);
694 if (InstArg.getKind() != TemplateArgument::Type) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000695 Info.Param = TTP;
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000696 Info.FirstArg = PartialTemplateArgs[I];
697 return TDK_SubstitutionFailure;
698 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000699
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000700 if (Context.getCanonicalType(InstArg.getAsType())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000701 != Context.getCanonicalType(TemplateArgs[I].getAsType())) {
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000702 Info.Param = TTP;
Douglas Gregorf67875d2009-06-12 18:26:56 +0000703 Info.FirstArg = TemplateArgs[I];
704 Info.SecondArg = InstArg;
705 return TDK_NonDeducedMismatch;
706 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000707
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000708 continue;
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000709 }
Douglas Gregorc9e5d252009-06-13 00:59:32 +0000710
711 // FIXME: Check template template arguments?
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000712 }
713
Douglas Gregorbb260412009-06-14 08:02:22 +0000714 if (Trap.hasErrorOccurred())
715 return TDK_SubstitutionFailure;
716
Douglas Gregorf67875d2009-06-12 18:26:56 +0000717 return TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000718}
Douglas Gregor031a5882009-06-13 00:26:55 +0000719
Douglas Gregore53060f2009-06-25 22:08:12 +0000720/// \brief Perform template argument deduction from a function call
721/// (C++ [temp.deduct.call]).
722///
723/// \param FunctionTemplate the function template for which we are performing
724/// template argument deduction.
725///
726/// \param Args the function call arguments
727///
728/// \param NumArgs the number of arguments in Args
729///
730/// \param Specialization if template argument deduction was successful,
731/// this will be set to the function template specialization produced by
732/// template argument deduction.
733///
734/// \param Info the argument will be updated to provide additional information
735/// about template argument deduction.
736///
737/// \returns the result of template argument deduction.
738///
739/// FIXME: We will also need to pass in any explicitly-specified template
740/// arguments.
741Sema::TemplateDeductionResult
742Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
743 Expr **Args, unsigned NumArgs,
744 FunctionDecl *&Specialization,
745 TemplateDeductionInfo &Info) {
746 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
747
748 // C++ [temp.deduct.call]p1:
749 // Template argument deduction is done by comparing each function template
750 // parameter type (call it P) with the type of the corresponding argument
751 // of the call (call it A) as described below.
752 unsigned CheckArgs = NumArgs;
753 if (NumArgs < Function->getNumParams())
754 return TDK_TooFewArguments;
755 else if (NumArgs > Function->getNumParams()) {
756 const FunctionProtoType *Proto
757 = Function->getType()->getAsFunctionProtoType();
758 if (!Proto->isVariadic())
759 return TDK_TooManyArguments;
760
761 CheckArgs = Function->getNumParams();
762 }
763
764 SFINAETrap Trap(*this);
765 llvm::SmallVector<TemplateArgument, 4> Deduced;
766 Deduced.resize(FunctionTemplate->getTemplateParameters()->size());
767 TemplateParameterList *TemplateParams
768 = FunctionTemplate->getTemplateParameters();
769 for (unsigned I = 0; I != CheckArgs; ++I) {
770 QualType ParamType = Function->getParamDecl(I)->getType();
771 QualType ArgType = Args[I]->getType();
772
773 // C++ [temp.deduct.call]p2:
774 // If P is not a reference type:
775 QualType CanonParamType = Context.getCanonicalType(ParamType);
776 if (!isa<ReferenceType>(CanonParamType)) {
777 // - If A is an array type, the pointer type produced by the
778 // array-to-pointer standard conversion (4.2) is used in place of
779 // A for type deduction; otherwise,
780 if (ArgType->isArrayType())
781 ArgType = Context.getArrayDecayedType(ArgType);
782 // - If A is a function type, the pointer type produced by the
783 // function-to-pointer standard conversion (4.3) is used in place
784 // of A for type deduction; otherwise,
785 else if (ArgType->isFunctionType())
786 ArgType = Context.getPointerType(ArgType);
787 else {
788 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
789 // type are ignored for type deduction.
790 QualType CanonArgType = Context.getCanonicalType(ArgType);
791 if (CanonArgType.getCVRQualifiers())
792 ArgType = CanonArgType.getUnqualifiedType();
793 }
794 }
795
796 // C++0x [temp.deduct.call]p3:
797 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
798 // are ignored for type deduction.
799 if (CanonParamType.getCVRQualifiers())
800 ParamType = CanonParamType.getUnqualifiedType();
801 if (const ReferenceType *ParamRefType = ParamType->getAsReferenceType()) {
802 // [...] If P is a reference type, the type referred to by P is used
803 // for type deduction.
804 ParamType = ParamRefType->getPointeeType();
805
806 // [...] If P is of the form T&&, where T is a template parameter, and
807 // the argument is an lvalue, the type A& is used in place of A for
808 // type deduction.
809 if (isa<RValueReferenceType>(ParamRefType) &&
810 ParamRefType->getAsTemplateTypeParmType() &&
811 Args[I]->isLvalue(Context) == Expr::LV_Valid)
812 ArgType = Context.getLValueReferenceType(ArgType);
813 }
814
815 // C++0x [temp.deduct.call]p4:
816 // In general, the deduction process attempts to find template argument
817 // values that will make the deduced A identical to A (after the type A
818 // is transformed as described above). [...]
819 //
820 // FIXME: we'll pass down a flag to indicate when these cases may apply,
821 // and then deal with them in the code that deduces template
822 // arguments from a type.
823 if (TemplateDeductionResult Result
824 = ::DeduceTemplateArguments(Context, TemplateParams,
825 ParamType, ArgType, Info, Deduced))
826 return Result;
827
828 // FIXME: C++ [temp.deduct.call] paragraphs 6-9 deal with function
829 // pointer parameters.
830 }
831
832 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
833 FunctionTemplate, Deduced.data(), Deduced.size());
834 if (Inst)
835 return TDK_InstantiationDepth;
836
837 // C++ [temp.deduct.type]p2:
838 // [...] or if any template argument remains neither deduced nor
839 // explicitly specified, template argument deduction fails.
840 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
841 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
842 if (Deduced[I].isNull()) {
843 Decl *Param
844 = const_cast<Decl *>(TemplateParams->getParam(I));
845 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
846 Info.Param = TTP;
847 else if (NonTypeTemplateParmDecl *NTTP
848 = dyn_cast<NonTypeTemplateParmDecl>(Param))
849 Info.Param = NTTP;
850 else
851 Info.Param = cast<TemplateTemplateParmDecl>(Param);
852 return TDK_Incomplete;
853 }
854
855 Builder.Append(Deduced[I]);
856 }
857
858 // Form the template argument list from the deduced template arguments.
859 TemplateArgumentList *DeducedArgumentList
860 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
861 Info.reset(DeducedArgumentList);
862
863 // Substitute the deduced template arguments into the function template
864 // declaration to produce the function template specialization.
865 Specialization = cast_or_null<FunctionDecl>(
866 InstantiateDecl(FunctionTemplate->getTemplatedDecl(),
867 FunctionTemplate->getDeclContext(),
868 *DeducedArgumentList));
869
870 if (!Specialization || Trap.hasErrorOccurred())
871 return TDK_SubstitutionFailure;
872
873 return TDK_Success;
874}
875
Douglas Gregor031a5882009-06-13 00:26:55 +0000876static void
877MarkDeducedTemplateParameters(Sema &SemaRef,
878 const TemplateArgument &TemplateArg,
879 llvm::SmallVectorImpl<bool> &Deduced);
880
881/// \brief Mark the template arguments that are deduced by the given
882/// expression.
883static void
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000884MarkDeducedTemplateParameters(const Expr *E,
885 llvm::SmallVectorImpl<bool> &Deduced) {
886 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregor031a5882009-06-13 00:26:55 +0000887 if (!E)
888 return;
889
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000890 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +0000891 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
892 if (!NTTP)
893 return;
894
895 Deduced[NTTP->getIndex()] = true;
896}
897
898/// \brief Mark the template parameters that are deduced by the given
899/// type.
900static void
901MarkDeducedTemplateParameters(Sema &SemaRef, QualType T,
902 llvm::SmallVectorImpl<bool> &Deduced) {
903 // Non-dependent types have nothing deducible
904 if (!T->isDependentType())
905 return;
906
907 T = SemaRef.Context.getCanonicalType(T);
908 switch (T->getTypeClass()) {
909 case Type::ExtQual:
910 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000911 QualType(cast<ExtQualType>(T)->getBaseType(), 0),
Douglas Gregor031a5882009-06-13 00:26:55 +0000912 Deduced);
913 break;
914
915 case Type::Pointer:
916 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000917 cast<PointerType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +0000918 Deduced);
919 break;
920
921 case Type::BlockPointer:
922 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000923 cast<BlockPointerType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +0000924 Deduced);
925 break;
926
927 case Type::LValueReference:
928 case Type::RValueReference:
929 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000930 cast<ReferenceType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +0000931 Deduced);
932 break;
933
934 case Type::MemberPointer: {
935 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
936 MarkDeducedTemplateParameters(SemaRef, MemPtr->getPointeeType(), Deduced);
937 MarkDeducedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
938 Deduced);
939 break;
940 }
941
942 case Type::DependentSizedArray:
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000943 MarkDeducedTemplateParameters(cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregor031a5882009-06-13 00:26:55 +0000944 Deduced);
945 // Fall through to check the element type
946
947 case Type::ConstantArray:
948 case Type::IncompleteArray:
949 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000950 cast<ArrayType>(T)->getElementType(),
Douglas Gregor031a5882009-06-13 00:26:55 +0000951 Deduced);
952 break;
953
954 case Type::Vector:
955 case Type::ExtVector:
956 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000957 cast<VectorType>(T)->getElementType(),
Douglas Gregor031a5882009-06-13 00:26:55 +0000958 Deduced);
959 break;
960
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000961 case Type::DependentSizedExtVector: {
962 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000963 = cast<DependentSizedExtVectorType>(T);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000964 MarkDeducedTemplateParameters(SemaRef, VecType->getElementType(), Deduced);
965 MarkDeducedTemplateParameters(VecType->getSizeExpr(), Deduced);
966 break;
967 }
968
Douglas Gregor031a5882009-06-13 00:26:55 +0000969 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000970 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregor031a5882009-06-13 00:26:55 +0000971 MarkDeducedTemplateParameters(SemaRef, Proto->getResultType(), Deduced);
972 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
973 MarkDeducedTemplateParameters(SemaRef, Proto->getArgType(I), Deduced);
974 break;
975 }
976
977 case Type::TemplateTypeParm:
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000978 Deduced[cast<TemplateTypeParmType>(T)->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +0000979 break;
980
981 case Type::TemplateSpecialization: {
982 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000983 = cast<TemplateSpecializationType>(T);
Douglas Gregor031a5882009-06-13 00:26:55 +0000984 if (TemplateDecl *Template = Spec->getTemplateName().getAsTemplateDecl())
985 if (TemplateTemplateParmDecl *TTP
986 = dyn_cast<TemplateTemplateParmDecl>(Template))
987 Deduced[TTP->getIndex()] = true;
988
989 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
990 MarkDeducedTemplateParameters(SemaRef, Spec->getArg(I), Deduced);
991
992 break;
993 }
994
995 // None of these types have any deducible parts.
996 case Type::Builtin:
997 case Type::FixedWidthInt:
998 case Type::Complex:
999 case Type::VariableArray:
1000 case Type::FunctionNoProto:
1001 case Type::Record:
1002 case Type::Enum:
1003 case Type::Typename:
1004 case Type::ObjCInterface:
1005 case Type::ObjCQualifiedInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001006 case Type::ObjCObjectPointer:
Douglas Gregor031a5882009-06-13 00:26:55 +00001007#define TYPE(Class, Base)
1008#define ABSTRACT_TYPE(Class, Base)
1009#define DEPENDENT_TYPE(Class, Base)
1010#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
1011#include "clang/AST/TypeNodes.def"
1012 break;
1013 }
1014}
1015
1016/// \brief Mark the template parameters that are deduced by this
1017/// template argument.
1018static void
1019MarkDeducedTemplateParameters(Sema &SemaRef,
1020 const TemplateArgument &TemplateArg,
1021 llvm::SmallVectorImpl<bool> &Deduced) {
1022 switch (TemplateArg.getKind()) {
1023 case TemplateArgument::Null:
1024 case TemplateArgument::Integral:
1025 break;
1026
1027 case TemplateArgument::Type:
1028 MarkDeducedTemplateParameters(SemaRef, TemplateArg.getAsType(), Deduced);
1029 break;
1030
1031 case TemplateArgument::Declaration:
1032 if (TemplateTemplateParmDecl *TTP
1033 = dyn_cast<TemplateTemplateParmDecl>(TemplateArg.getAsDecl()))
1034 Deduced[TTP->getIndex()] = true;
1035 break;
1036
1037 case TemplateArgument::Expression:
1038 MarkDeducedTemplateParameters(TemplateArg.getAsExpr(), Deduced);
1039 break;
Anders Carlssond01b1da2009-06-15 17:04:53 +00001040 case TemplateArgument::Pack:
1041 assert(0 && "FIXME: Implement!");
1042 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00001043 }
1044}
1045
1046/// \brief Mark the template parameters can be deduced by the given
1047/// template argument list.
1048///
1049/// \param TemplateArgs the template argument list from which template
1050/// parameters will be deduced.
1051///
1052/// \param Deduced a bit vector whose elements will be set to \c true
1053/// to indicate when the corresponding template parameter will be
1054/// deduced.
1055void
1056Sema::MarkDeducedTemplateParameters(const TemplateArgumentList &TemplateArgs,
1057 llvm::SmallVectorImpl<bool> &Deduced) {
1058 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
1059 ::MarkDeducedTemplateParameters(*this, TemplateArgs[I], Deduced);
1060}