blob: 784e451804aa90e9d54b50b37472d4ed5e565101 [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.
657 TemplateArgumentListBuilder Builder(Context);
658 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000659 if (Deduced[I].isNull()) {
660 Decl *Param
661 = const_cast<Decl *>(Partial->getTemplateParameters()->getParam(I));
662 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
663 Info.Param = TTP;
664 else if (NonTypeTemplateParmDecl *NTTP
665 = dyn_cast<NonTypeTemplateParmDecl>(Param))
666 Info.Param = NTTP;
667 else
668 Info.Param = cast<TemplateTemplateParmDecl>(Param);
669 return TDK_Incomplete;
670 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000671
672 Builder.push_back(Deduced[I]);
673 }
674
675 // Form the template argument list from the deduced template arguments.
676 TemplateArgumentList *DeducedArgumentList
677 = new (Context) TemplateArgumentList(Context, Builder, /*CopyArgs=*/true,
678 /*FlattenArgs=*/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
720static void
721MarkDeducedTemplateParameters(Sema &SemaRef,
722 const TemplateArgument &TemplateArg,
723 llvm::SmallVectorImpl<bool> &Deduced);
724
725/// \brief Mark the template arguments that are deduced by the given
726/// expression.
727static void
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000728MarkDeducedTemplateParameters(const Expr *E,
729 llvm::SmallVectorImpl<bool> &Deduced) {
730 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregor031a5882009-06-13 00:26:55 +0000731 if (!E)
732 return;
733
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000734 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +0000735 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
736 if (!NTTP)
737 return;
738
739 Deduced[NTTP->getIndex()] = true;
740}
741
742/// \brief Mark the template parameters that are deduced by the given
743/// type.
744static void
745MarkDeducedTemplateParameters(Sema &SemaRef, QualType T,
746 llvm::SmallVectorImpl<bool> &Deduced) {
747 // Non-dependent types have nothing deducible
748 if (!T->isDependentType())
749 return;
750
751 T = SemaRef.Context.getCanonicalType(T);
752 switch (T->getTypeClass()) {
753 case Type::ExtQual:
754 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000755 QualType(cast<ExtQualType>(T)->getBaseType(), 0),
Douglas Gregor031a5882009-06-13 00:26:55 +0000756 Deduced);
757 break;
758
759 case Type::Pointer:
760 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000761 cast<PointerType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +0000762 Deduced);
763 break;
764
765 case Type::BlockPointer:
766 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000767 cast<BlockPointerType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +0000768 Deduced);
769 break;
770
771 case Type::LValueReference:
772 case Type::RValueReference:
773 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000774 cast<ReferenceType>(T)->getPointeeType(),
Douglas Gregor031a5882009-06-13 00:26:55 +0000775 Deduced);
776 break;
777
778 case Type::MemberPointer: {
779 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
780 MarkDeducedTemplateParameters(SemaRef, MemPtr->getPointeeType(), Deduced);
781 MarkDeducedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
782 Deduced);
783 break;
784 }
785
786 case Type::DependentSizedArray:
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000787 MarkDeducedTemplateParameters(cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregor031a5882009-06-13 00:26:55 +0000788 Deduced);
789 // Fall through to check the element type
790
791 case Type::ConstantArray:
792 case Type::IncompleteArray:
793 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000794 cast<ArrayType>(T)->getElementType(),
Douglas Gregor031a5882009-06-13 00:26:55 +0000795 Deduced);
796 break;
797
798 case Type::Vector:
799 case Type::ExtVector:
800 MarkDeducedTemplateParameters(SemaRef,
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000801 cast<VectorType>(T)->getElementType(),
Douglas Gregor031a5882009-06-13 00:26:55 +0000802 Deduced);
803 break;
804
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000805 case Type::DependentSizedExtVector: {
806 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000807 = cast<DependentSizedExtVectorType>(T);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000808 MarkDeducedTemplateParameters(SemaRef, VecType->getElementType(), Deduced);
809 MarkDeducedTemplateParameters(VecType->getSizeExpr(), Deduced);
810 break;
811 }
812
Douglas Gregor031a5882009-06-13 00:26:55 +0000813 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000814 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregor031a5882009-06-13 00:26:55 +0000815 MarkDeducedTemplateParameters(SemaRef, Proto->getResultType(), Deduced);
816 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
817 MarkDeducedTemplateParameters(SemaRef, Proto->getArgType(I), Deduced);
818 break;
819 }
820
821 case Type::TemplateTypeParm:
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000822 Deduced[cast<TemplateTypeParmType>(T)->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +0000823 break;
824
825 case Type::TemplateSpecialization: {
826 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +0000827 = cast<TemplateSpecializationType>(T);
Douglas Gregor031a5882009-06-13 00:26:55 +0000828 if (TemplateDecl *Template = Spec->getTemplateName().getAsTemplateDecl())
829 if (TemplateTemplateParmDecl *TTP
830 = dyn_cast<TemplateTemplateParmDecl>(Template))
831 Deduced[TTP->getIndex()] = true;
832
833 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
834 MarkDeducedTemplateParameters(SemaRef, Spec->getArg(I), Deduced);
835
836 break;
837 }
838
839 // None of these types have any deducible parts.
840 case Type::Builtin:
841 case Type::FixedWidthInt:
842 case Type::Complex:
843 case Type::VariableArray:
844 case Type::FunctionNoProto:
845 case Type::Record:
846 case Type::Enum:
847 case Type::Typename:
848 case Type::ObjCInterface:
849 case Type::ObjCQualifiedInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000850 case Type::ObjCObjectPointer:
Douglas Gregor031a5882009-06-13 00:26:55 +0000851#define TYPE(Class, Base)
852#define ABSTRACT_TYPE(Class, Base)
853#define DEPENDENT_TYPE(Class, Base)
854#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
855#include "clang/AST/TypeNodes.def"
856 break;
857 }
858}
859
860/// \brief Mark the template parameters that are deduced by this
861/// template argument.
862static void
863MarkDeducedTemplateParameters(Sema &SemaRef,
864 const TemplateArgument &TemplateArg,
865 llvm::SmallVectorImpl<bool> &Deduced) {
866 switch (TemplateArg.getKind()) {
867 case TemplateArgument::Null:
868 case TemplateArgument::Integral:
869 break;
870
871 case TemplateArgument::Type:
872 MarkDeducedTemplateParameters(SemaRef, TemplateArg.getAsType(), Deduced);
873 break;
874
875 case TemplateArgument::Declaration:
876 if (TemplateTemplateParmDecl *TTP
877 = dyn_cast<TemplateTemplateParmDecl>(TemplateArg.getAsDecl()))
878 Deduced[TTP->getIndex()] = true;
879 break;
880
881 case TemplateArgument::Expression:
882 MarkDeducedTemplateParameters(TemplateArg.getAsExpr(), Deduced);
883 break;
Anders Carlssond01b1da2009-06-15 17:04:53 +0000884 case TemplateArgument::Pack:
885 assert(0 && "FIXME: Implement!");
886 break;
Douglas Gregor031a5882009-06-13 00:26:55 +0000887 }
888}
889
890/// \brief Mark the template parameters can be deduced by the given
891/// template argument list.
892///
893/// \param TemplateArgs the template argument list from which template
894/// parameters will be deduced.
895///
896/// \param Deduced a bit vector whose elements will be set to \c true
897/// to indicate when the corresponding template parameter will be
898/// deduced.
899void
900Sema::MarkDeducedTemplateParameters(const TemplateArgumentList &TemplateArgs,
901 llvm::SmallVectorImpl<bool> &Deduced) {
902 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
903 ::MarkDeducedTemplateParameters(*this, TemplateArgs[I], Deduced);
904}