blob: 252f2d1d0d07ac1dd4fc73188f49eabe1c3d8df0 [file] [log] [blame]
Douglas Gregorbf23a8a2009-06-04 00:03:07 +00001//===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template argument deduction.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/StmtVisitor.h"
17#include "clang/AST/Expr.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/Parse/DeclSpec.h"
20#include "llvm/Support/Compiler.h"
21using namespace clang;
22
Douglas Gregor623e2e02009-06-12 18:26:56 +000023static Sema::TemplateDeductionResult
24DeduceTemplateArguments(ASTContext &Context,
25 TemplateParameterList *TemplateParams,
26 const TemplateArgument &Param,
Douglas Gregor5069ae32009-06-09 16:35:58 +000027 const TemplateArgument &Arg,
Douglas Gregor623e2e02009-06-12 18:26:56 +000028 Sema::TemplateDeductionInfo &Info,
Douglas Gregor5069ae32009-06-09 16:35:58 +000029 llvm::SmallVectorImpl<TemplateArgument> &Deduced);
30
Douglas Gregorab9f71a2009-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 Gregor623e2e02009-06-12 18:26:56 +000046static Sema::TemplateDeductionResult
47DeduceNonTypeTemplateArgument(ASTContext &Context,
48 NonTypeTemplateParmDecl *NTTP,
49 llvm::APInt Value,
50 Sema::TemplateDeductionInfo &Info,
51 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregorab9f71a2009-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()) {
56 Deduced[NTTP->getIndex()] = TemplateArgument(SourceLocation(),
57 llvm::APSInt(Value),
58 NTTP->getType());
Douglas Gregor623e2e02009-06-12 18:26:56 +000059 return Sema::TDK_Success;
Douglas Gregorab9f71a2009-06-05 00:53:49 +000060 }
61
Douglas Gregor623e2e02009-06-12 18:26:56 +000062 assert(Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral);
Douglas Gregorab9f71a2009-06-05 00:53:49 +000063
64 // If the template argument was previously deduced to a negative value,
65 // then our deduction fails.
66 const llvm::APSInt *PrevValuePtr = Deduced[NTTP->getIndex()].getAsIntegral();
Douglas Gregor623e2e02009-06-12 18:26:56 +000067 if (PrevValuePtr->isSigned() && PrevValuePtr->isNegative()) {
68 // FIXME: This is wacky; we should be dealing with APSInts and
69 // checking the actual signs.
70 Info.Param = NTTP;
71 Info.FirstArg = Deduced[NTTP->getIndex()];
72 Info.SecondArg = TemplateArgument(SourceLocation(),
73 llvm::APSInt(Value),
74 NTTP->getType());
75 return Sema::TDK_Inconsistent;
76 }
77
Douglas Gregorab9f71a2009-06-05 00:53:49 +000078 llvm::APInt PrevValue = *PrevValuePtr;
79 if (Value.getBitWidth() > PrevValue.getBitWidth())
80 PrevValue.zext(Value.getBitWidth());
81 else if (Value.getBitWidth() < PrevValue.getBitWidth())
82 Value.zext(PrevValue.getBitWidth());
Douglas Gregor623e2e02009-06-12 18:26:56 +000083
84 if (Value != PrevValue) {
85 Info.Param = NTTP;
86 Info.FirstArg = Deduced[NTTP->getIndex()];
87 Info.SecondArg = TemplateArgument(SourceLocation(),
88 llvm::APSInt(Value),
89 NTTP->getType());
90 return Sema::TDK_Inconsistent;
91 }
92
93 return Sema::TDK_Success;
Douglas Gregorab9f71a2009-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 Gregor623e2e02009-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 Gregorab9f71a2009-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 Gregor623e2e02009-06-12 18:26:56 +0000115 return Sema::TDK_Success;
Douglas Gregorab9f71a2009-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 Gregor623e2e02009-06-12 18:26:56 +0000122 return Sema::TDK_Success;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000123 }
124
125 // FIXME: Compare the expressions for equality!
Douglas Gregor623e2e02009-06-12 18:26:56 +0000126 return Sema::TDK_Success;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000127}
128
Douglas Gregor623e2e02009-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 Gregor5069ae32009-06-09 16:35:58 +0000135 // FIXME: Implement template argument deduction for template
136 // template parameters.
137
Douglas Gregor623e2e02009-06-12 18:26:56 +0000138 // FIXME: this routine does not have enough information to produce
139 // good diagnostics.
140
Douglas Gregor5069ae32009-06-09 16:35:58 +0000141 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
142 TemplateDecl *ArgDecl = Arg.getAsTemplateDecl();
143
Douglas Gregor623e2e02009-06-12 18:26:56 +0000144 if (!ParamDecl || !ArgDecl) {
145 // FIXME: fill in Info.Param/Info.FirstArg
146 return Sema::TDK_Inconsistent;
147 }
Douglas Gregor5069ae32009-06-09 16:35:58 +0000148
149 ParamDecl = cast<TemplateDecl>(Context.getCanonicalDecl(ParamDecl));
150 ArgDecl = cast<TemplateDecl>(Context.getCanonicalDecl(ArgDecl));
Douglas Gregor623e2e02009-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 Gregor5069ae32009-06-09 16:35:58 +0000157}
158
Douglas Gregor623e2e02009-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 Gregorbf23a8a2009-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 Gregor623e2e02009-06-12 18:26:56 +0000167 QualType Param = Context.getCanonicalType(ParamIn);
168 QualType Arg = Context.getCanonicalType(ArgIn);
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000169
170 // If the parameter type is not dependent, just compare the types
171 // directly.
Douglas Gregor623e2e02009-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 Gregorbf23a8a2009-06-04 00:03:07 +0000180
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000181 // C++ [temp.deduct.type]p9:
Douglas Gregorab9f71a2009-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 Gregorbf23a8a2009-06-04 00:03:07 +0000188 if (const TemplateTypeParmType *TemplateTypeParm
189 = Param->getAsTemplateTypeParmType()) {
Douglas Gregor623e2e02009-06-12 18:26:56 +0000190 unsigned Index = TemplateTypeParm->getIndex();
191
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000192 // The argument type can not be less qualified than the parameter
193 // type.
Douglas Gregor623e2e02009-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 Gregorbf23a8a2009-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 Gregorbf23a8a2009-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 Gregor623e2e02009-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 Gregorbf23a8a2009-06-04 00:03:07 +0000222 }
Douglas Gregor623e2e02009-06-12 18:26:56 +0000223 return Sema::TDK_Success;
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000224 }
225
Douglas Gregor623e2e02009-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 Gregorbf23a8a2009-06-04 00:03:07 +0000230 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Douglas Gregor623e2e02009-06-12 18:26:56 +0000231 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000232
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000233 switch (Param->getTypeClass()) {
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000234 // No deduction possible for these types
235 case Type::Builtin:
Douglas Gregor623e2e02009-06-12 18:26:56 +0000236 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000237
238 // T *
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000239 case Type::Pointer: {
240 const PointerType *PointerArg = Arg->getAsPointerType();
241 if (!PointerArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000242 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000243
Douglas Gregor623e2e02009-06-12 18:26:56 +0000244 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000245 cast<PointerType>(Param)->getPointeeType(),
246 PointerArg->getPointeeType(),
Douglas Gregor623e2e02009-06-12 18:26:56 +0000247 Info, Deduced);
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000248 }
249
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000250 // T &
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000251 case Type::LValueReference: {
252 const LValueReferenceType *ReferenceArg = Arg->getAsLValueReferenceType();
253 if (!ReferenceArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000254 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000255
Douglas Gregor623e2e02009-06-12 18:26:56 +0000256 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000257 cast<LValueReferenceType>(Param)->getPointeeType(),
258 ReferenceArg->getPointeeType(),
Douglas Gregor623e2e02009-06-12 18:26:56 +0000259 Info, Deduced);
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000260 }
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000261
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000262 // T && [C++0x]
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000263 case Type::RValueReference: {
264 const RValueReferenceType *ReferenceArg = Arg->getAsRValueReferenceType();
265 if (!ReferenceArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000266 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000267
Douglas Gregor623e2e02009-06-12 18:26:56 +0000268 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000269 cast<RValueReferenceType>(Param)->getPointeeType(),
270 ReferenceArg->getPointeeType(),
Douglas Gregor623e2e02009-06-12 18:26:56 +0000271 Info, Deduced);
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000272 }
273
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000274 // T [] (implied, but not stated explicitly)
Anders Carlsson0bcee302009-06-04 04:11:30 +0000275 case Type::IncompleteArray: {
276 const IncompleteArrayType *IncompleteArrayArg =
277 Context.getAsIncompleteArrayType(Arg);
278 if (!IncompleteArrayArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000279 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson0bcee302009-06-04 04:11:30 +0000280
Douglas Gregor623e2e02009-06-12 18:26:56 +0000281 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson0bcee302009-06-04 04:11:30 +0000282 Context.getAsIncompleteArrayType(Param)->getElementType(),
283 IncompleteArrayArg->getElementType(),
Douglas Gregor623e2e02009-06-12 18:26:56 +0000284 Info, Deduced);
Anders Carlsson0bcee302009-06-04 04:11:30 +0000285 }
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000286
287 // T [integer-constant]
Anders Carlsson0bcee302009-06-04 04:11:30 +0000288 case Type::ConstantArray: {
289 const ConstantArrayType *ConstantArrayArg =
290 Context.getAsConstantArrayType(Arg);
291 if (!ConstantArrayArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000292 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson0bcee302009-06-04 04:11:30 +0000293
294 const ConstantArrayType *ConstantArrayParm =
295 Context.getAsConstantArrayType(Param);
296 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregor623e2e02009-06-12 18:26:56 +0000297 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson0bcee302009-06-04 04:11:30 +0000298
Douglas Gregor623e2e02009-06-12 18:26:56 +0000299 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson0bcee302009-06-04 04:11:30 +0000300 ConstantArrayParm->getElementType(),
301 ConstantArrayArg->getElementType(),
Douglas Gregor623e2e02009-06-12 18:26:56 +0000302 Info, Deduced);
Anders Carlsson0bcee302009-06-04 04:11:30 +0000303 }
304
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000305 // type [i]
306 case Type::DependentSizedArray: {
307 const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg);
308 if (!ArrayArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000309 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000310
311 // Check the element type of the arrays
312 const DependentSizedArrayType *DependentArrayParm
313 = cast<DependentSizedArrayType>(Param);
Douglas Gregor623e2e02009-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 Gregorab9f71a2009-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 Gregor623e2e02009-06-12 18:26:56 +0000325 return Sema::TDK_Success;
Douglas Gregorab9f71a2009-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
332 = dyn_cast<ConstantArrayType>(ArrayArg))
333 return DeduceNonTypeTemplateArgument(Context, NTTP,
334 ConstantArrayArg->getSize(),
Douglas Gregor623e2e02009-06-12 18:26:56 +0000335 Info, Deduced);
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000336 if (const DependentSizedArrayType *DependentArrayArg
337 = dyn_cast<DependentSizedArrayType>(ArrayArg))
338 return DeduceNonTypeTemplateArgument(Context, NTTP,
339 DependentArrayArg->getSizeExpr(),
Douglas Gregor623e2e02009-06-12 18:26:56 +0000340 Info, Deduced);
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000341
342 // Incomplete type does not match a dependently-sized array type
Douglas Gregor623e2e02009-06-12 18:26:56 +0000343 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000344 }
345
Douglas Gregor1c67c002009-06-08 15:59:14 +0000346 // type(*)(T)
347 // T(*)()
348 // T(*)(T)
Anders Carlsson8574e7f2009-06-08 15:19:08 +0000349 case Type::FunctionProto: {
350 const FunctionProtoType *FunctionProtoArg =
351 dyn_cast<FunctionProtoType>(Arg);
352 if (!FunctionProtoArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000353 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson8574e7f2009-06-08 15:19:08 +0000354
355 const FunctionProtoType *FunctionProtoParam =
356 cast<FunctionProtoType>(Param);
Anders Carlsson3e39f922009-06-08 19:22:23 +0000357
358 if (FunctionProtoParam->getTypeQuals() !=
359 FunctionProtoArg->getTypeQuals())
Douglas Gregor623e2e02009-06-12 18:26:56 +0000360 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson8574e7f2009-06-08 15:19:08 +0000361
Anders Carlsson3e39f922009-06-08 19:22:23 +0000362 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregor623e2e02009-06-12 18:26:56 +0000363 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson3e39f922009-06-08 19:22:23 +0000364
365 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregor623e2e02009-06-12 18:26:56 +0000366 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson3e39f922009-06-08 19:22:23 +0000367
Anders Carlsson8574e7f2009-06-08 15:19:08 +0000368 // Check return types.
Douglas Gregor623e2e02009-06-12 18:26:56 +0000369 if (Sema::TemplateDeductionResult Result
370 = DeduceTemplateArguments(Context, TemplateParams,
371 FunctionProtoParam->getResultType(),
372 FunctionProtoArg->getResultType(),
373 Info, Deduced))
374 return Result;
Anders Carlsson8574e7f2009-06-08 15:19:08 +0000375
Anders Carlsson8574e7f2009-06-08 15:19:08 +0000376 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
377 // Check argument types.
Douglas Gregor623e2e02009-06-12 18:26:56 +0000378 if (Sema::TemplateDeductionResult Result
379 = DeduceTemplateArguments(Context, TemplateParams,
380 FunctionProtoParam->getArgType(I),
381 FunctionProtoArg->getArgType(I),
382 Info, Deduced))
383 return Result;
Anders Carlsson8574e7f2009-06-08 15:19:08 +0000384 }
385
Douglas Gregor623e2e02009-06-12 18:26:56 +0000386 return Sema::TDK_Success;
Anders Carlsson8574e7f2009-06-08 15:19:08 +0000387 }
Douglas Gregor5069ae32009-06-09 16:35:58 +0000388
389 // template-name<T> (wheretemplate-name refers to a class template)
390 // template-name<i>
391 // TT<T> (TODO)
392 // TT<i> (TODO)
393 // TT<> (TODO)
394 case Type::TemplateSpecialization: {
395 const TemplateSpecializationType *SpecParam
396 = cast<TemplateSpecializationType>(Param);
397
398 // Check whether the template argument is a dependent template-id.
399 // FIXME: This is untested code; it can be tested when we implement
400 // partial ordering of class template partial specializations.
401 if (const TemplateSpecializationType *SpecArg
402 = dyn_cast<TemplateSpecializationType>(Arg)) {
403 // Perform template argument deduction for the template name.
Douglas Gregor623e2e02009-06-12 18:26:56 +0000404 if (Sema::TemplateDeductionResult Result
405 = DeduceTemplateArguments(Context,
406 SpecParam->getTemplateName(),
407 SpecArg->getTemplateName(),
408 Info, Deduced))
409 return Result;
Douglas Gregor5069ae32009-06-09 16:35:58 +0000410
411 unsigned NumArgs = SpecParam->getNumArgs();
412
413 // FIXME: When one of the template-names refers to a
414 // declaration with default template arguments, do we need to
415 // fill in those default template arguments here? Most likely,
416 // the answer is "yes", but I don't see any references. This
417 // issue may be resolved elsewhere, because we may want to
418 // instantiate default template arguments when
419 if (SpecArg->getNumArgs() != NumArgs)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000420 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor5069ae32009-06-09 16:35:58 +0000421
422 // Perform template argument deduction on each template
423 // argument.
424 for (unsigned I = 0; I != NumArgs; ++I)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000425 if (Sema::TemplateDeductionResult Result
426 = DeduceTemplateArguments(Context, TemplateParams,
427 SpecParam->getArg(I),
428 SpecArg->getArg(I),
429 Info, Deduced))
430 return Result;
Douglas Gregor5069ae32009-06-09 16:35:58 +0000431
Douglas Gregor623e2e02009-06-12 18:26:56 +0000432 return Sema::TDK_Success;
Douglas Gregor5069ae32009-06-09 16:35:58 +0000433 }
434
435 // If the argument type is a class template specialization, we
436 // perform template argument deduction using its template
437 // arguments.
438 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
439 if (!RecordArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000440 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor5069ae32009-06-09 16:35:58 +0000441
442 ClassTemplateSpecializationDecl *SpecArg
443 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
444 if (!SpecArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000445 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor5069ae32009-06-09 16:35:58 +0000446
447 // Perform template argument deduction for the template name.
Douglas Gregor623e2e02009-06-12 18:26:56 +0000448 if (Sema::TemplateDeductionResult Result
449 = DeduceTemplateArguments(Context,
450 SpecParam->getTemplateName(),
451 TemplateName(SpecArg->getSpecializedTemplate()),
452 Info, Deduced))
453 return Result;
Douglas Gregor5069ae32009-06-09 16:35:58 +0000454
455 // FIXME: Can the # of arguments in the parameter and the argument differ?
456 unsigned NumArgs = SpecParam->getNumArgs();
457 const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
458 if (NumArgs != ArgArgs.size())
Douglas Gregor623e2e02009-06-12 18:26:56 +0000459 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor5069ae32009-06-09 16:35:58 +0000460
461 for (unsigned I = 0; I != NumArgs; ++I)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000462 if (Sema::TemplateDeductionResult Result
463 = DeduceTemplateArguments(Context, TemplateParams,
464 SpecParam->getArg(I),
465 ArgArgs.get(I),
466 Info, Deduced))
467 return Result;
Anders Carlsson8574e7f2009-06-08 15:19:08 +0000468
Douglas Gregor623e2e02009-06-12 18:26:56 +0000469 return Sema::TDK_Success;
Douglas Gregor5069ae32009-06-09 16:35:58 +0000470 }
471
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000472 // T type::*
473 // T T::*
474 // T (type::*)()
475 // type (T::*)()
476 // type (type::*)(T)
477 // type (T::*)(T)
478 // T (type::*)(T)
479 // T (T::*)()
480 // T (T::*)(T)
481 case Type::MemberPointer: {
482 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
483 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
484 if (!MemPtrArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000485 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000486
Douglas Gregor623e2e02009-06-12 18:26:56 +0000487 if (Sema::TemplateDeductionResult Result
488 = DeduceTemplateArguments(Context, TemplateParams,
489 MemPtrParam->getPointeeType(),
490 MemPtrArg->getPointeeType(),
491 Info, Deduced))
492 return Result;
493
494 return DeduceTemplateArguments(Context, TemplateParams,
495 QualType(MemPtrParam->getClass(), 0),
496 QualType(MemPtrArg->getClass(), 0),
497 Info, Deduced);
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000498 }
499
Anders Carlsson9d914d32009-06-12 16:23:10 +0000500 // type(^)(T)
501 // T(^)()
502 // T(^)(T)
503 case Type::BlockPointer: {
504 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
505 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
506
507 if (!BlockPtrArg)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000508 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson9d914d32009-06-12 16:23:10 +0000509
Douglas Gregor623e2e02009-06-12 18:26:56 +0000510 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson9d914d32009-06-12 16:23:10 +0000511 BlockPtrParam->getPointeeType(),
Douglas Gregor623e2e02009-06-12 18:26:56 +0000512 BlockPtrArg->getPointeeType(), Info,
513 Deduced);
Anders Carlsson9d914d32009-06-12 16:23:10 +0000514 }
515
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000516 case Type::TypeOfExpr:
517 case Type::TypeOf:
518 case Type::Typename:
519 // No template argument deduction for these types
Douglas Gregor623e2e02009-06-12 18:26:56 +0000520 return Sema::TDK_Success;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000521
Douglas Gregor4ea653d2009-06-04 00:21:18 +0000522 default:
523 break;
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000524 }
525
526 // FIXME: Many more cases to go (to go).
Douglas Gregor623e2e02009-06-12 18:26:56 +0000527 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000528}
529
Douglas Gregor623e2e02009-06-12 18:26:56 +0000530static Sema::TemplateDeductionResult
531DeduceTemplateArguments(ASTContext &Context,
532 TemplateParameterList *TemplateParams,
533 const TemplateArgument &Param,
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000534 const TemplateArgument &Arg,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000535 Sema::TemplateDeductionInfo &Info,
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000536 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000537 switch (Param.getKind()) {
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000538 case TemplateArgument::Null:
539 assert(false && "Null template argument in parameter list");
540 break;
541
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000542 case TemplateArgument::Type:
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000543 assert(Arg.getKind() == TemplateArgument::Type && "Type/value mismatch");
Douglas Gregor623e2e02009-06-12 18:26:56 +0000544 return DeduceTemplateArguments(Context, TemplateParams,
545 Param.getAsType(),
546 Arg.getAsType(), Info, Deduced);
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000547
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000548 case TemplateArgument::Declaration:
549 // FIXME: Implement this check
550 assert(false && "Unimplemented template argument deduction case");
Douglas Gregor623e2e02009-06-12 18:26:56 +0000551 Info.FirstArg = Param;
552 Info.SecondArg = Arg;
553 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000554
555 case TemplateArgument::Integral:
556 if (Arg.getKind() == TemplateArgument::Integral) {
557 // FIXME: Zero extension + sign checking here?
Douglas Gregor623e2e02009-06-12 18:26:56 +0000558 if (*Param.getAsIntegral() == *Arg.getAsIntegral())
559 return Sema::TDK_Success;
560
561 Info.FirstArg = Param;
562 Info.SecondArg = Arg;
563 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000564 }
Douglas Gregor623e2e02009-06-12 18:26:56 +0000565
566 if (Arg.getKind() == TemplateArgument::Expression) {
567 Info.FirstArg = Param;
568 Info.SecondArg = Arg;
569 return Sema::TDK_NonDeducedMismatch;
570 }
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000571
572 assert(false && "Type/value mismatch");
Douglas Gregor623e2e02009-06-12 18:26:56 +0000573 Info.FirstArg = Param;
574 Info.SecondArg = Arg;
575 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000576
577 case TemplateArgument::Expression: {
578 if (NonTypeTemplateParmDecl *NTTP
579 = getDeducedParameterFromExpr(Param.getAsExpr())) {
580 if (Arg.getKind() == TemplateArgument::Integral)
581 // FIXME: Sign problems here
582 return DeduceNonTypeTemplateArgument(Context, NTTP,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000583 *Arg.getAsIntegral(),
584 Info, Deduced);
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000585 if (Arg.getKind() == TemplateArgument::Expression)
586 return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(),
Douglas Gregor623e2e02009-06-12 18:26:56 +0000587 Info, Deduced);
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000588
589 assert(false && "Type/value mismatch");
Douglas Gregor623e2e02009-06-12 18:26:56 +0000590 Info.FirstArg = Param;
591 Info.SecondArg = Arg;
592 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000593 }
594
595 // Can't deduce anything, but that's okay.
Douglas Gregor623e2e02009-06-12 18:26:56 +0000596 return Sema::TDK_Success;
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000597 }
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000598 }
599
Douglas Gregor623e2e02009-06-12 18:26:56 +0000600 return Sema::TDK_Success;
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000601}
602
Douglas Gregor623e2e02009-06-12 18:26:56 +0000603static Sema::TemplateDeductionResult
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000604DeduceTemplateArguments(ASTContext &Context,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000605 TemplateParameterList *TemplateParams,
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000606 const TemplateArgumentList &ParamList,
607 const TemplateArgumentList &ArgList,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000608 Sema::TemplateDeductionInfo &Info,
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000609 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
610 assert(ParamList.size() == ArgList.size());
611 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregor623e2e02009-06-12 18:26:56 +0000612 if (Sema::TemplateDeductionResult Result
613 = DeduceTemplateArguments(Context, TemplateParams,
614 ParamList[I], ArgList[I],
615 Info, Deduced))
616 return Result;
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000617 }
Douglas Gregor623e2e02009-06-12 18:26:56 +0000618 return Sema::TDK_Success;
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000619}
620
621
Douglas Gregor623e2e02009-06-12 18:26:56 +0000622Sema::TemplateDeductionResult
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000623Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregor623e2e02009-06-12 18:26:56 +0000624 const TemplateArgumentList &TemplateArgs,
625 TemplateDeductionInfo &Info) {
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000626 // Deduce the template arguments for the partial specialization
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000627 llvm::SmallVector<TemplateArgument, 4> Deduced;
628 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregor623e2e02009-06-12 18:26:56 +0000629 if (TemplateDeductionResult Result
630 = ::DeduceTemplateArguments(Context,
631 Partial->getTemplateParameters(),
632 Partial->getTemplateArgs(),
633 TemplateArgs, Info, Deduced))
634 return Result;
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000635
Douglas Gregorc5e01af2009-06-10 23:47:09 +0000636 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
637 Deduced.data(), Deduced.size());
638 if (Inst)
Douglas Gregor623e2e02009-06-12 18:26:56 +0000639 return TDK_InstantiationDepth;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000640
Douglas Gregor8f378a92009-06-11 18:10:32 +0000641 // C++ [temp.deduct.type]p2:
642 // [...] or if any template argument remains neither deduced nor
643 // explicitly specified, template argument deduction fails.
644 TemplateArgumentListBuilder Builder(Context);
645 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregor623e2e02009-06-12 18:26:56 +0000646 if (Deduced[I].isNull()) {
647 Decl *Param
648 = const_cast<Decl *>(Partial->getTemplateParameters()->getParam(I));
649 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
650 Info.Param = TTP;
651 else if (NonTypeTemplateParmDecl *NTTP
652 = dyn_cast<NonTypeTemplateParmDecl>(Param))
653 Info.Param = NTTP;
654 else
655 Info.Param = cast<TemplateTemplateParmDecl>(Param);
656 return TDK_Incomplete;
657 }
Douglas Gregor8f378a92009-06-11 18:10:32 +0000658
659 Builder.push_back(Deduced[I]);
660 }
661
662 // Form the template argument list from the deduced template arguments.
663 TemplateArgumentList *DeducedArgumentList
664 = new (Context) TemplateArgumentList(Context, Builder, /*CopyArgs=*/true,
665 /*FlattenArgs=*/true);
Douglas Gregor623e2e02009-06-12 18:26:56 +0000666 Info.reset(DeducedArgumentList);
Douglas Gregor8f378a92009-06-11 18:10:32 +0000667
668 // Now that we have all of the deduced template arguments, take
669 // another pass through them to convert any integral template
670 // arguments to the appropriate type.
671 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
672 TemplateArgument &Arg = Deduced[I];
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000673 if (Arg.getKind() == TemplateArgument::Integral) {
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000674 const NonTypeTemplateParmDecl *Parm
675 = cast<NonTypeTemplateParmDecl>(Partial->getTemplateParameters()
676 ->getParam(I));
Douglas Gregor8f378a92009-06-11 18:10:32 +0000677 QualType T = InstantiateType(Parm->getType(), *DeducedArgumentList,
678 Parm->getLocation(), Parm->getDeclName());
Douglas Gregor623e2e02009-06-12 18:26:56 +0000679 if (T.isNull()) {
680 Info.Param = const_cast<NonTypeTemplateParmDecl*>(Parm);
681 Info.FirstArg = TemplateArgument(Parm->getLocation(), Parm->getType());
682 return TDK_SubstitutionFailure;
683 }
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000684
685 // FIXME: Make sure we didn't overflow our data type!
686 llvm::APSInt &Value = *Arg.getAsIntegral();
687 unsigned AllowedBits = Context.getTypeSize(T);
688 if (Value.getBitWidth() != AllowedBits)
689 Value.extOrTrunc(AllowedBits);
690 Value.setIsSigned(T->isSignedIntegerType());
691 Arg.setIntegralType(T);
692 }
Douglas Gregor8f378a92009-06-11 18:10:32 +0000693
694 (*DeducedArgumentList)[I] = Arg;
Douglas Gregorab9f71a2009-06-05 00:53:49 +0000695 }
Douglas Gregor8f378a92009-06-11 18:10:32 +0000696
697 // Substitute the deduced template arguments into the template
698 // arguments of the class template partial specialization, and
699 // verify that the instantiated template arguments are both valid
700 // and are equivalent to the template arguments originally provided
701 // to the class template.
702 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
703 const TemplateArgumentList &PartialTemplateArgs = Partial->getTemplateArgs();
704 for (unsigned I = 0, N = PartialTemplateArgs.flat_size(); I != N; ++I) {
705 TemplateArgument InstArg = Instantiate(PartialTemplateArgs[I],
706 *DeducedArgumentList);
707 if (InstArg.isNull()) {
Douglas Gregor623e2e02009-06-12 18:26:56 +0000708 Decl *Param = const_cast<Decl *>(
709 ClassTemplate->getTemplateParameters()->getParam(I));
710 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
711 Info.Param = TTP;
712 else if (NonTypeTemplateParmDecl *NTTP
713 = dyn_cast<NonTypeTemplateParmDecl>(Param))
714 Info.Param = NTTP;
715 else
716 Info.Param = cast<TemplateTemplateParmDecl>(Param);
717 Info.FirstArg = PartialTemplateArgs[I];
718 return TDK_SubstitutionFailure;
Douglas Gregor8f378a92009-06-11 18:10:32 +0000719 }
720
721 Decl *Param
722 = const_cast<Decl *>(ClassTemplate->getTemplateParameters()->getParam(I));
723 if (isa<TemplateTypeParmDecl>(Param)) {
724 if (InstArg.getKind() != TemplateArgument::Type ||
725 Context.getCanonicalType(InstArg.getAsType())
Douglas Gregor623e2e02009-06-12 18:26:56 +0000726 != Context.getCanonicalType(TemplateArgs[I].getAsType())) {
727 Info.Param = cast<TemplateTypeParmDecl>(Param);
728 Info.FirstArg = TemplateArgs[I];
729 Info.SecondArg = InstArg;
730 return TDK_NonDeducedMismatch;
731 }
Douglas Gregor8f378a92009-06-11 18:10:32 +0000732 } else if (NonTypeTemplateParmDecl *NTTP
733 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
734 QualType T = InstantiateType(NTTP->getType(), TemplateArgs,
735 NTTP->getLocation(), NTTP->getDeclName());
Douglas Gregor623e2e02009-06-12 18:26:56 +0000736 if (T.isNull()) {
737 Info.Param = NTTP;
738 Info.FirstArg = TemplateArgs[I];
739 Info.SecondArg = InstArg;
740 return TDK_NonDeducedMismatch;
741 }
Douglas Gregor8f378a92009-06-11 18:10:32 +0000742
743 if (InstArg.getKind() == TemplateArgument::Declaration ||
744 InstArg.getKind() == TemplateArgument::Expression) {
745 // Turn the template argument into an expression, so that we can
746 // perform type checking on it and convert it to the type of the
747 // non-type template parameter. FIXME: Will this expression be
748 // leaked? It's hard to tell, since our ownership model for
749 // expressions in template arguments is so poor.
750 Expr *E = 0;
751 if (InstArg.getKind() == TemplateArgument::Declaration) {
752 NamedDecl *D = cast<NamedDecl>(InstArg.getAsDecl());
753 QualType T = Context.OverloadTy;
754 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
755 T = VD->getType().getNonReferenceType();
756 E = new (Context) DeclRefExpr(D, T, InstArg.getLocation());
757 } else {
758 E = InstArg.getAsExpr();
759 }
760
761 // Check that the template argument can be used to initialize
762 // the corresponding template parameter.
Douglas Gregor623e2e02009-06-12 18:26:56 +0000763 if (CheckTemplateArgument(NTTP, T, E, InstArg)) {
764 // FIXME: This isn't precisely the problem, but since it
765 // can't actually happen in well-formed C++ we don't care at
766 // the moment. Revisit this when we have template argument
767 // deduction for function templates.
768 Info.Param = NTTP;
769 Info.FirstArg = TemplateArgs[I];
770 Info.SecondArg = InstArg;
771 return TDK_NonDeducedMismatch;
772 }
Douglas Gregor8f378a92009-06-11 18:10:32 +0000773 }
774
775 switch (InstArg.getKind()) {
776 case TemplateArgument::Null:
777 assert(false && "Null template arguments cannot get here");
Douglas Gregor623e2e02009-06-12 18:26:56 +0000778 return TDK_NonDeducedMismatch;
Douglas Gregor8f378a92009-06-11 18:10:32 +0000779
780 case TemplateArgument::Type:
781 assert(false && "Type/value mismatch");
Douglas Gregor623e2e02009-06-12 18:26:56 +0000782 return TDK_NonDeducedMismatch;
Douglas Gregor8f378a92009-06-11 18:10:32 +0000783
784 case TemplateArgument::Integral: {
785 llvm::APSInt &Value = *InstArg.getAsIntegral();
786 if (T->isIntegralType() || T->isEnumeralType()) {
787 QualType IntegerType = Context.getCanonicalType(T);
788 if (const EnumType *Enum = dyn_cast<EnumType>(IntegerType))
789 IntegerType = Context.getCanonicalType(
790 Enum->getDecl()->getIntegerType());
791
792 // Check that an unsigned parameter does not receive a negative
793 // value.
794 if (IntegerType->isUnsignedIntegerType()
Douglas Gregor623e2e02009-06-12 18:26:56 +0000795 && (Value.isSigned() && Value.isNegative())) {
796 Info.Param = NTTP;
797 Info.FirstArg = TemplateArgs[I];
798 Info.SecondArg = InstArg;
799 return TDK_NonDeducedMismatch;
800 }
Douglas Gregor8f378a92009-06-11 18:10:32 +0000801
802 // Check for truncation. If the number of bits in the
803 // instantiated template argument exceeds what is allowed by
804 // the type, template argument deduction fails.
805 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor623e2e02009-06-12 18:26:56 +0000806 if (Value.getActiveBits() > AllowedBits) {
807 Info.Param = NTTP;
808 Info.FirstArg = TemplateArgs[I];
809 Info.SecondArg = InstArg;
810 return TDK_NonDeducedMismatch;
811 }
Douglas Gregor8f378a92009-06-11 18:10:32 +0000812
813 if (Value.getBitWidth() != AllowedBits)
814 Value.extOrTrunc(AllowedBits);
815 Value.setIsSigned(IntegerType->isSignedIntegerType());
816
817 // Check that the instantiated value is the same as the
818 // value provided as a template argument.
Douglas Gregor623e2e02009-06-12 18:26:56 +0000819 if (Value != *TemplateArgs[I].getAsIntegral()) {
820 Info.Param = NTTP;
821 Info.FirstArg = TemplateArgs[I];
822 Info.SecondArg = InstArg;
823 return TDK_NonDeducedMismatch;
824 }
Douglas Gregor8f378a92009-06-11 18:10:32 +0000825 } else if (T->isPointerType() || T->isMemberPointerType()) {
826 // Deal with NULL pointers that are used to initialize
827 // pointer and pointer-to-member non-type template
828 // parameters (C++0x).
Douglas Gregor623e2e02009-06-12 18:26:56 +0000829 if (TemplateArgs[I].getAsDecl()) {
830 // Not a NULL declaration
831 Info.Param = NTTP;
832 Info.FirstArg = TemplateArgs[I];
833 Info.SecondArg = InstArg;
834 return TDK_NonDeducedMismatch;
835 }
Douglas Gregor8f378a92009-06-11 18:10:32 +0000836 // Check that the integral value is 0, the NULL pointer
837 // constant.
Douglas Gregor623e2e02009-06-12 18:26:56 +0000838 if (Value != 0) {
839 Info.Param = NTTP;
840 Info.FirstArg = TemplateArgs[I];
841 Info.SecondArg = InstArg;
842 return TDK_NonDeducedMismatch;
843 }
844 } else {
845 Info.Param = NTTP;
846 Info.FirstArg = TemplateArgs[I];
847 Info.SecondArg = InstArg;
848 return TDK_NonDeducedMismatch;
849 }
850
Douglas Gregor8f378a92009-06-11 18:10:32 +0000851 break;
852 }
853
854 case TemplateArgument::Declaration:
855 if (Context.getCanonicalDecl(InstArg.getAsDecl())
Douglas Gregor623e2e02009-06-12 18:26:56 +0000856 != Context.getCanonicalDecl(TemplateArgs[I].getAsDecl())) {
857 Info.Param = NTTP;
858 Info.FirstArg = TemplateArgs[I];
859 Info.SecondArg = InstArg;
860 return TDK_NonDeducedMismatch;
861 }
Douglas Gregor8f378a92009-06-11 18:10:32 +0000862 break;
863
864 case TemplateArgument::Expression:
865 // FIXME: Check equality of expressions
866 break;
867 }
868 } else {
869 assert(isa<TemplateTemplateParmDecl>(Param));
870 // FIXME: Check template template arguments
871 }
872 }
873
Douglas Gregor623e2e02009-06-12 18:26:56 +0000874 return TDK_Success;
Douglas Gregorbf23a8a2009-06-04 00:03:07 +0000875}