blob: 984aa6bb61b02ccda800a1f89bf79eea10859d6d [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"
Douglas Gregor8a514912009-09-14 18:39:43 +000020#include <algorithm>
Douglas Gregor508f1c82009-06-26 23:10:12 +000021
22namespace clang {
23 /// \brief Various flags that control template argument deduction.
24 ///
25 /// These flags can be bitwise-OR'd together.
26 enum TemplateDeductionFlags {
27 /// \brief No template argument deduction flags, which indicates the
28 /// strictest results for template argument deduction (as used for, e.g.,
29 /// matching class template partial specializations).
30 TDF_None = 0,
31 /// \brief Within template argument deduction from a function call, we are
32 /// matching with a parameter type for which the original parameter was
33 /// a reference.
34 TDF_ParamWithReferenceType = 0x1,
35 /// \brief Within template argument deduction from a function call, we
36 /// are matching in a case where we ignore cv-qualifiers.
37 TDF_IgnoreQualifiers = 0x02,
38 /// \brief Within template argument deduction from a function call,
39 /// we are matching in a case where we can perform template argument
Douglas Gregor41128772009-06-26 23:27:24 +000040 /// deduction from a template-id of a derived class of the argument type.
Douglas Gregor12820292009-09-14 20:00:47 +000041 TDF_DerivedClass = 0x04,
42 /// \brief Allow non-dependent types to differ, e.g., when performing
43 /// template argument deduction from a function call where conversions
44 /// may apply.
45 TDF_SkipNonDependent = 0x08
Douglas Gregor508f1c82009-06-26 23:10:12 +000046 };
47}
48
Douglas Gregor0b9247f2009-06-04 00:03:07 +000049using namespace clang;
50
Douglas Gregorf67875d2009-06-12 18:26:56 +000051static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +000052DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +000053 TemplateParameterList *TemplateParams,
54 const TemplateArgument &Param,
Douglas Gregord708c722009-06-09 16:35:58 +000055 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +000056 Sema::TemplateDeductionInfo &Info,
Douglas Gregord708c722009-06-09 16:35:58 +000057 llvm::SmallVectorImpl<TemplateArgument> &Deduced);
58
Douglas Gregor199d9912009-06-05 00:53:49 +000059/// \brief If the given expression is of a form that permits the deduction
60/// of a non-type template parameter, return the declaration of that
61/// non-type template parameter.
62static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
63 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
64 E = IC->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +000065
Douglas Gregor199d9912009-06-05 00:53:49 +000066 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
67 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +000068
Douglas Gregor199d9912009-06-05 00:53:49 +000069 return 0;
70}
71
Mike Stump1eb44332009-09-09 15:08:12 +000072/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +000073/// from the given constant.
Douglas Gregorf67875d2009-06-12 18:26:56 +000074static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +000075DeduceNonTypeTemplateArgument(ASTContext &Context,
76 NonTypeTemplateParmDecl *NTTP,
Anders Carlsson335e24a2009-06-16 22:44:31 +000077 llvm::APSInt Value,
Douglas Gregorf67875d2009-06-12 18:26:56 +000078 Sema::TemplateDeductionInfo &Info,
79 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +000080 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +000081 "Cannot deduce non-type template argument with depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +000082
Douglas Gregor199d9912009-06-05 00:53:49 +000083 if (Deduced[NTTP->getIndex()].isNull()) {
Anders Carlsson25af1ed2009-06-16 23:08:29 +000084 QualType T = NTTP->getType();
Mike Stump1eb44332009-09-09 15:08:12 +000085
Anders Carlsson25af1ed2009-06-16 23:08:29 +000086 // FIXME: Make sure we didn't overflow our data type!
87 unsigned AllowedBits = Context.getTypeSize(T);
88 if (Value.getBitWidth() != AllowedBits)
89 Value.extOrTrunc(AllowedBits);
90 Value.setIsSigned(T->isSignedIntegerType());
91
John McCall833ca992009-10-29 08:12:44 +000092 Deduced[NTTP->getIndex()] = TemplateArgument(Value, T);
Douglas Gregorf67875d2009-06-12 18:26:56 +000093 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +000094 }
Mike Stump1eb44332009-09-09 15:08:12 +000095
Douglas Gregorf67875d2009-06-12 18:26:56 +000096 assert(Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral);
Mike Stump1eb44332009-09-09 15:08:12 +000097
98 // If the template argument was previously deduced to a negative value,
Douglas Gregor199d9912009-06-05 00:53:49 +000099 // then our deduction fails.
100 const llvm::APSInt *PrevValuePtr = Deduced[NTTP->getIndex()].getAsIntegral();
Anders Carlsson335e24a2009-06-16 22:44:31 +0000101 if (PrevValuePtr->isNegative()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000102 Info.Param = NTTP;
103 Info.FirstArg = Deduced[NTTP->getIndex()];
John McCall833ca992009-10-29 08:12:44 +0000104 Info.SecondArg = TemplateArgument(Value, NTTP->getType());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000105 return Sema::TDK_Inconsistent;
106 }
107
Anders Carlsson335e24a2009-06-16 22:44:31 +0000108 llvm::APSInt PrevValue = *PrevValuePtr;
Douglas Gregor199d9912009-06-05 00:53:49 +0000109 if (Value.getBitWidth() > PrevValue.getBitWidth())
110 PrevValue.zext(Value.getBitWidth());
111 else if (Value.getBitWidth() < PrevValue.getBitWidth())
112 Value.zext(PrevValue.getBitWidth());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000113
114 if (Value != PrevValue) {
115 Info.Param = NTTP;
116 Info.FirstArg = Deduced[NTTP->getIndex()];
John McCall833ca992009-10-29 08:12:44 +0000117 Info.SecondArg = TemplateArgument(Value, NTTP->getType());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000118 return Sema::TDK_Inconsistent;
119 }
120
121 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000122}
123
Mike Stump1eb44332009-09-09 15:08:12 +0000124/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +0000125/// from the given type- or value-dependent expression.
126///
127/// \returns true if deduction succeeded, false otherwise.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000128static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +0000129DeduceNonTypeTemplateArgument(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000130 NonTypeTemplateParmDecl *NTTP,
131 Expr *Value,
132 Sema::TemplateDeductionInfo &Info,
133 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +0000134 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000135 "Cannot deduce non-type template argument with depth > 0");
136 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
137 "Expression template argument must be type- or value-dependent.");
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Douglas Gregor199d9912009-06-05 00:53:49 +0000139 if (Deduced[NTTP->getIndex()].isNull()) {
140 // FIXME: Clone the Value?
141 Deduced[NTTP->getIndex()] = TemplateArgument(Value);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000142 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000143 }
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Douglas Gregor199d9912009-06-05 00:53:49 +0000145 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
Mike Stump1eb44332009-09-09 15:08:12 +0000146 // Okay, we deduced a constant in one case and a dependent expression
147 // in another case. FIXME: Later, we will check that instantiating the
Douglas Gregor199d9912009-06-05 00:53:49 +0000148 // dependent expression gives us the constant value.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000149 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000150 }
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Douglas Gregor9eea08b2009-09-15 16:51:42 +0000152 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
153 // Compare the expressions for equality
154 llvm::FoldingSetNodeID ID1, ID2;
155 Deduced[NTTP->getIndex()].getAsExpr()->Profile(ID1, Context, true);
156 Value->Profile(ID2, Context, true);
157 if (ID1 == ID2)
158 return Sema::TDK_Success;
159
160 // FIXME: Fill in argument mismatch information
161 return Sema::TDK_NonDeducedMismatch;
162 }
163
Douglas Gregorf67875d2009-06-12 18:26:56 +0000164 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000165}
166
Douglas Gregor15755cb2009-11-13 23:45:44 +0000167/// \brief Deduce the value of the given non-type template parameter
168/// from the given declaration.
169///
170/// \returns true if deduction succeeded, false otherwise.
171static Sema::TemplateDeductionResult
172DeduceNonTypeTemplateArgument(ASTContext &Context,
173 NonTypeTemplateParmDecl *NTTP,
174 Decl *D,
175 Sema::TemplateDeductionInfo &Info,
176 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
177 assert(NTTP->getDepth() == 0 &&
178 "Cannot deduce non-type template argument with depth > 0");
179
180 if (Deduced[NTTP->getIndex()].isNull()) {
181 Deduced[NTTP->getIndex()] = TemplateArgument(D->getCanonicalDecl());
182 return Sema::TDK_Success;
183 }
184
185 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
186 // Okay, we deduced a declaration in one case and a dependent expression
187 // in another case.
188 return Sema::TDK_Success;
189 }
190
191 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Declaration) {
192 // Compare the declarations for equality
193 if (Deduced[NTTP->getIndex()].getAsDecl()->getCanonicalDecl() ==
194 D->getCanonicalDecl())
195 return Sema::TDK_Success;
196
197 // FIXME: Fill in argument mismatch information
198 return Sema::TDK_NonDeducedMismatch;
199 }
200
201 return Sema::TDK_Success;
202}
203
Douglas Gregorf67875d2009-06-12 18:26:56 +0000204static Sema::TemplateDeductionResult
205DeduceTemplateArguments(ASTContext &Context,
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000206 TemplateParameterList *TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000207 TemplateName Param,
208 TemplateName Arg,
209 Sema::TemplateDeductionInfo &Info,
210 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregord708c722009-06-09 16:35:58 +0000211 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000212 if (!ParamDecl) {
213 // The parameter type is dependent and is not a template template parameter,
214 // so there is nothing that we can deduce.
215 return Sema::TDK_Success;
216 }
217
218 if (TemplateTemplateParmDecl *TempParam
219 = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) {
220 // Bind the template template parameter to the given template name.
221 TemplateArgument &ExistingArg = Deduced[TempParam->getIndex()];
222 if (ExistingArg.isNull()) {
223 // This is the first deduction for this template template parameter.
224 ExistingArg = TemplateArgument(Context.getCanonicalTemplateName(Arg));
225 return Sema::TDK_Success;
226 }
227
228 // Verify that the previous binding matches this deduction.
229 assert(ExistingArg.getKind() == TemplateArgument::Template);
230 if (Context.hasSameTemplateName(ExistingArg.getAsTemplate(), Arg))
231 return Sema::TDK_Success;
232
233 // Inconsistent deduction.
234 Info.Param = TempParam;
235 Info.FirstArg = ExistingArg;
236 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000237 return Sema::TDK_Inconsistent;
238 }
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000239
240 // Verify that the two template names are equivalent.
241 if (Context.hasSameTemplateName(Param, Arg))
242 return Sema::TDK_Success;
243
244 // Mismatch of non-dependent template parameter to argument.
245 Info.FirstArg = TemplateArgument(Param);
246 Info.SecondArg = TemplateArgument(Arg);
247 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000248}
249
Mike Stump1eb44332009-09-09 15:08:12 +0000250/// \brief Deduce the template arguments by comparing the template parameter
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000251/// type (which is a template-id) with the template argument type.
252///
253/// \param Context the AST context in which this deduction occurs.
254///
255/// \param TemplateParams the template parameters that we are deducing
256///
257/// \param Param the parameter type
258///
259/// \param Arg the argument type
260///
261/// \param Info information about the template argument deduction itself
262///
263/// \param Deduced the deduced template arguments
264///
265/// \returns the result of template argument deduction so far. Note that a
266/// "success" result means that template argument deduction has not yet failed,
267/// but it may still fail, later, for other reasons.
268static Sema::TemplateDeductionResult
269DeduceTemplateArguments(ASTContext &Context,
270 TemplateParameterList *TemplateParams,
271 const TemplateSpecializationType *Param,
272 QualType Arg,
273 Sema::TemplateDeductionInfo &Info,
274 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
John McCall467b27b2009-10-22 20:10:53 +0000275 assert(Arg.isCanonical() && "Argument type must be canonical");
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000277 // Check whether the template argument is a dependent template-id.
Mike Stump1eb44332009-09-09 15:08:12 +0000278 if (const TemplateSpecializationType *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000279 = dyn_cast<TemplateSpecializationType>(Arg)) {
280 // Perform template argument deduction for the template name.
281 if (Sema::TemplateDeductionResult Result
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000282 = DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000283 Param->getTemplateName(),
284 SpecArg->getTemplateName(),
285 Info, Deduced))
286 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000289 // Perform template argument deduction on each template
290 // argument.
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000291 unsigned NumArgs = std::min(SpecArg->getNumArgs(), Param->getNumArgs());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000292 for (unsigned I = 0; I != NumArgs; ++I)
293 if (Sema::TemplateDeductionResult Result
294 = DeduceTemplateArguments(Context, TemplateParams,
295 Param->getArg(I),
296 SpecArg->getArg(I),
297 Info, Deduced))
298 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000300 return Sema::TDK_Success;
301 }
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000303 // If the argument type is a class template specialization, we
304 // perform template argument deduction using its template
305 // arguments.
306 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
307 if (!RecordArg)
308 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000309
310 ClassTemplateSpecializationDecl *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000311 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
312 if (!SpecArg)
313 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000315 // Perform template argument deduction for the template name.
316 if (Sema::TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000317 = DeduceTemplateArguments(Context,
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000318 TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000319 Param->getTemplateName(),
320 TemplateName(SpecArg->getSpecializedTemplate()),
321 Info, Deduced))
322 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000324 unsigned NumArgs = Param->getNumArgs();
325 const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
326 if (NumArgs != ArgArgs.size())
327 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000329 for (unsigned I = 0; I != NumArgs; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000330 if (Sema::TemplateDeductionResult Result
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000331 = DeduceTemplateArguments(Context, TemplateParams,
332 Param->getArg(I),
333 ArgArgs.get(I),
334 Info, Deduced))
335 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000337 return Sema::TDK_Success;
338}
339
Douglas Gregor500d3312009-06-26 18:27:22 +0000340/// \brief Deduce the template arguments by comparing the parameter type and
341/// the argument type (C++ [temp.deduct.type]).
342///
343/// \param Context the AST context in which this deduction occurs.
344///
345/// \param TemplateParams the template parameters that we are deducing
346///
347/// \param ParamIn the parameter type
348///
349/// \param ArgIn the argument type
350///
351/// \param Info information about the template argument deduction itself
352///
353/// \param Deduced the deduced template arguments
354///
Douglas Gregor508f1c82009-06-26 23:10:12 +0000355/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
Mike Stump1eb44332009-09-09 15:08:12 +0000356/// how template argument deduction is performed.
Douglas Gregor500d3312009-06-26 18:27:22 +0000357///
358/// \returns the result of template argument deduction so far. Note that a
359/// "success" result means that template argument deduction has not yet failed,
360/// but it may still fail, later, for other reasons.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000361static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +0000362DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000363 TemplateParameterList *TemplateParams,
364 QualType ParamIn, QualType ArgIn,
365 Sema::TemplateDeductionInfo &Info,
Douglas Gregor500d3312009-06-26 18:27:22 +0000366 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000367 unsigned TDF) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000368 // We only want to look at the canonical types, since typedefs and
369 // sugar are not part of template argument deduction.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000370 QualType Param = Context.getCanonicalType(ParamIn);
371 QualType Arg = Context.getCanonicalType(ArgIn);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000372
Douglas Gregor500d3312009-06-26 18:27:22 +0000373 // C++0x [temp.deduct.call]p4 bullet 1:
374 // - If the original P is a reference type, the deduced A (i.e., the type
Mike Stump1eb44332009-09-09 15:08:12 +0000375 // referred to by the reference) can be more cv-qualified than the
Douglas Gregor500d3312009-06-26 18:27:22 +0000376 // transformed A.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000377 if (TDF & TDF_ParamWithReferenceType) {
Chandler Carruthe7242462009-12-30 04:10:01 +0000378 Qualifiers Quals;
379 QualType UnqualParam = Context.getUnqualifiedArrayType(Param, Quals);
380 Quals.setCVRQualifiers(Quals.getCVRQualifiers() &
381 Arg.getCVRQualifiersThroughArrayTypes());
382 Param = Context.getQualifiedType(UnqualParam, Quals);
Douglas Gregor500d3312009-06-26 18:27:22 +0000383 }
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000385 // If the parameter type is not dependent, there is nothing to deduce.
Douglas Gregor12820292009-09-14 20:00:47 +0000386 if (!Param->isDependentType()) {
387 if (!(TDF & TDF_SkipNonDependent) && Param != Arg) {
388
389 return Sema::TDK_NonDeducedMismatch;
390 }
391
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000392 return Sema::TDK_Success;
Douglas Gregor12820292009-09-14 20:00:47 +0000393 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000394
Douglas Gregor199d9912009-06-05 00:53:49 +0000395 // C++ [temp.deduct.type]p9:
Mike Stump1eb44332009-09-09 15:08:12 +0000396 // A template type argument T, a template template argument TT or a
397 // template non-type argument i can be deduced if P and A have one of
Douglas Gregor199d9912009-06-05 00:53:49 +0000398 // the following forms:
399 //
400 // T
401 // cv-list T
Mike Stump1eb44332009-09-09 15:08:12 +0000402 if (const TemplateTypeParmType *TemplateTypeParm
John McCall183700f2009-09-21 23:43:11 +0000403 = Param->getAs<TemplateTypeParmType>()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000404 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000405 bool RecanonicalizeArg = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Douglas Gregor9e9fae42009-07-22 20:02:25 +0000407 // If the argument type is an array type, move the qualifiers up to the
408 // top level, so they can be matched with the qualifiers on the parameter.
409 // FIXME: address spaces, ObjC GC qualifiers
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000410 if (isa<ArrayType>(Arg)) {
John McCall0953e762009-09-24 19:53:00 +0000411 Qualifiers Quals;
Chandler Carruth28e318c2009-12-29 07:16:59 +0000412 Arg = Context.getUnqualifiedArrayType(Arg, Quals);
John McCall0953e762009-09-24 19:53:00 +0000413 if (Quals) {
414 Arg = Context.getQualifiedType(Arg, Quals);
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000415 RecanonicalizeArg = true;
416 }
417 }
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000419 // The argument type can not be less qualified than the parameter
420 // type.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000421 if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000422 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
423 Info.FirstArg = Deduced[Index];
John McCall833ca992009-10-29 08:12:44 +0000424 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000425 return Sema::TDK_InconsistentQuals;
426 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000427
428 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
Douglas Gregorc78a69d2009-12-21 21:27:38 +0000429 assert(Arg != Context.OverloadTy && "Unresolved overloaded function");
John McCall0953e762009-09-24 19:53:00 +0000430 QualType DeducedType = Arg;
431 DeducedType.removeCVRQualifiers(Param.getCVRQualifiers());
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000432 if (RecanonicalizeArg)
433 DeducedType = Context.getCanonicalType(DeducedType);
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000435 if (Deduced[Index].isNull())
John McCall833ca992009-10-29 08:12:44 +0000436 Deduced[Index] = TemplateArgument(DeducedType);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000437 else {
Mike Stump1eb44332009-09-09 15:08:12 +0000438 // C++ [temp.deduct.type]p2:
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000439 // [...] If type deduction cannot be done for any P/A pair, or if for
Mike Stump1eb44332009-09-09 15:08:12 +0000440 // any pair the deduction leads to more than one possible set of
441 // deduced values, or if different pairs yield different deduced
442 // values, or if any template argument remains neither deduced nor
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000443 // explicitly specified, template argument deduction fails.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000444 if (Deduced[Index].getAsType() != DeducedType) {
Mike Stump1eb44332009-09-09 15:08:12 +0000445 Info.Param
Douglas Gregorf67875d2009-06-12 18:26:56 +0000446 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
447 Info.FirstArg = Deduced[Index];
John McCall833ca992009-10-29 08:12:44 +0000448 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000449 return Sema::TDK_Inconsistent;
450 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000451 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000452 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000453 }
454
Douglas Gregorf67875d2009-06-12 18:26:56 +0000455 // Set up the template argument deduction information for a failure.
John McCall833ca992009-10-29 08:12:44 +0000456 Info.FirstArg = TemplateArgument(ParamIn);
457 Info.SecondArg = TemplateArgument(ArgIn);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000458
Douglas Gregor508f1c82009-06-26 23:10:12 +0000459 // Check the cv-qualifiers on the parameter and argument types.
460 if (!(TDF & TDF_IgnoreQualifiers)) {
461 if (TDF & TDF_ParamWithReferenceType) {
462 if (Param.isMoreQualifiedThan(Arg))
463 return Sema::TDK_NonDeducedMismatch;
464 } else {
465 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Mike Stump1eb44332009-09-09 15:08:12 +0000466 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor508f1c82009-06-26 23:10:12 +0000467 }
468 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000469
Douglas Gregord560d502009-06-04 00:21:18 +0000470 switch (Param->getTypeClass()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000471 // No deduction possible for these types
472 case Type::Builtin:
Douglas Gregorf67875d2009-06-12 18:26:56 +0000473 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Douglas Gregor199d9912009-06-05 00:53:49 +0000475 // T *
Douglas Gregord560d502009-06-04 00:21:18 +0000476 case Type::Pointer: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000477 const PointerType *PointerArg = Arg->getAs<PointerType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000478 if (!PointerArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000479 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Douglas Gregor41128772009-06-26 23:27:24 +0000481 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000482 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000483 cast<PointerType>(Param)->getPointeeType(),
484 PointerArg->getPointeeType(),
Douglas Gregor41128772009-06-26 23:27:24 +0000485 Info, Deduced, SubTDF);
Douglas Gregord560d502009-06-04 00:21:18 +0000486 }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Douglas Gregor199d9912009-06-05 00:53:49 +0000488 // T &
Douglas Gregord560d502009-06-04 00:21:18 +0000489 case Type::LValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000490 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000491 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000492 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Douglas Gregorf67875d2009-06-12 18:26:56 +0000494 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000495 cast<LValueReferenceType>(Param)->getPointeeType(),
496 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000497 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000498 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000499
Douglas Gregor199d9912009-06-05 00:53:49 +0000500 // T && [C++0x]
Douglas Gregord560d502009-06-04 00:21:18 +0000501 case Type::RValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000502 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000503 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000504 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Douglas Gregorf67875d2009-06-12 18:26:56 +0000506 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000507 cast<RValueReferenceType>(Param)->getPointeeType(),
508 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000509 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000510 }
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Douglas Gregor199d9912009-06-05 00:53:49 +0000512 // T [] (implied, but not stated explicitly)
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000513 case Type::IncompleteArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000514 const IncompleteArrayType *IncompleteArrayArg =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000515 Context.getAsIncompleteArrayType(Arg);
516 if (!IncompleteArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000517 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Douglas Gregorf67875d2009-06-12 18:26:56 +0000519 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000520 Context.getAsIncompleteArrayType(Param)->getElementType(),
521 IncompleteArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000522 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000523 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000524
525 // T [integer-constant]
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000526 case Type::ConstantArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000527 const ConstantArrayType *ConstantArrayArg =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000528 Context.getAsConstantArrayType(Arg);
529 if (!ConstantArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000530 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000531
532 const ConstantArrayType *ConstantArrayParm =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000533 Context.getAsConstantArrayType(Param);
534 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000535 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Douglas Gregorf67875d2009-06-12 18:26:56 +0000537 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000538 ConstantArrayParm->getElementType(),
539 ConstantArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000540 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000541 }
542
Douglas Gregor199d9912009-06-05 00:53:49 +0000543 // type [i]
544 case Type::DependentSizedArray: {
Douglas Gregor44247132010-01-04 22:11:45 +0000545 const ArrayType *ArrayArg = Context.getAsArrayType(Arg);
Douglas Gregor199d9912009-06-05 00:53:49 +0000546 if (!ArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000547 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Douglas Gregor199d9912009-06-05 00:53:49 +0000549 // Check the element type of the arrays
550 const DependentSizedArrayType *DependentArrayParm
Douglas Gregor44247132010-01-04 22:11:45 +0000551 = Context.getAsDependentSizedArrayType(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000552 if (Sema::TemplateDeductionResult Result
553 = DeduceTemplateArguments(Context, TemplateParams,
554 DependentArrayParm->getElementType(),
555 ArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000556 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000557 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Douglas Gregor199d9912009-06-05 00:53:49 +0000559 // Determine the array bound is something we can deduce.
Mike Stump1eb44332009-09-09 15:08:12 +0000560 NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000561 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
562 if (!NTTP)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000563 return Sema::TDK_Success;
Mike Stump1eb44332009-09-09 15:08:12 +0000564
565 // We can perform template argument deduction for the given non-type
Douglas Gregor199d9912009-06-05 00:53:49 +0000566 // template parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000567 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000568 "Cannot deduce non-type template argument at depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000569 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson335e24a2009-06-16 22:44:31 +0000570 = dyn_cast<ConstantArrayType>(ArrayArg)) {
571 llvm::APSInt Size(ConstantArrayArg->getSize());
572 return DeduceNonTypeTemplateArgument(Context, NTTP, Size,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000573 Info, Deduced);
Anders Carlsson335e24a2009-06-16 22:44:31 +0000574 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000575 if (const DependentSizedArrayType *DependentArrayArg
576 = dyn_cast<DependentSizedArrayType>(ArrayArg))
577 return DeduceNonTypeTemplateArgument(Context, NTTP,
578 DependentArrayArg->getSizeExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000579 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Douglas Gregor199d9912009-06-05 00:53:49 +0000581 // Incomplete type does not match a dependently-sized array type
Douglas Gregorf67875d2009-06-12 18:26:56 +0000582 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
585 // type(*)(T)
586 // T(*)()
587 // T(*)(T)
Anders Carlssona27fad52009-06-08 15:19:08 +0000588 case Type::FunctionProto: {
Mike Stump1eb44332009-09-09 15:08:12 +0000589 const FunctionProtoType *FunctionProtoArg =
Anders Carlssona27fad52009-06-08 15:19:08 +0000590 dyn_cast<FunctionProtoType>(Arg);
591 if (!FunctionProtoArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000592 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000593
594 const FunctionProtoType *FunctionProtoParam =
Anders Carlssona27fad52009-06-08 15:19:08 +0000595 cast<FunctionProtoType>(Param);
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000596
Mike Stump1eb44332009-09-09 15:08:12 +0000597 if (FunctionProtoParam->getTypeQuals() !=
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000598 FunctionProtoArg->getTypeQuals())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000599 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000601 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000602 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000604 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000605 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000606
Anders Carlssona27fad52009-06-08 15:19:08 +0000607 // Check return types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000608 if (Sema::TemplateDeductionResult Result
609 = DeduceTemplateArguments(Context, TemplateParams,
610 FunctionProtoParam->getResultType(),
611 FunctionProtoArg->getResultType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000612 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000613 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Anders Carlssona27fad52009-06-08 15:19:08 +0000615 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
616 // Check argument types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000617 if (Sema::TemplateDeductionResult Result
618 = DeduceTemplateArguments(Context, TemplateParams,
619 FunctionProtoParam->getArgType(I),
620 FunctionProtoArg->getArgType(I),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000621 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000622 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000623 }
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Douglas Gregorf67875d2009-06-12 18:26:56 +0000625 return Sema::TDK_Success;
Anders Carlssona27fad52009-06-08 15:19:08 +0000626 }
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000628 // template-name<T> (where template-name refers to a class template)
Douglas Gregord708c722009-06-09 16:35:58 +0000629 // template-name<i>
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000630 // TT<T>
631 // TT<i>
632 // TT<>
Douglas Gregord708c722009-06-09 16:35:58 +0000633 case Type::TemplateSpecialization: {
634 const TemplateSpecializationType *SpecParam
635 = cast<TemplateSpecializationType>(Param);
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000637 // Try to deduce template arguments from the template-id.
638 Sema::TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000639 = DeduceTemplateArguments(Context, TemplateParams, SpecParam, Arg,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000640 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Douglas Gregor4a5c15f2009-09-30 22:13:51 +0000642 if (Result && (TDF & TDF_DerivedClass)) {
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000643 // C++ [temp.deduct.call]p3b3:
644 // If P is a class, and P has the form template-id, then A can be a
645 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump1eb44332009-09-09 15:08:12 +0000646 // class of the form template-id, A can be a pointer to a derived
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000647 // class pointed to by the deduced A.
648 //
649 // More importantly:
Mike Stump1eb44332009-09-09 15:08:12 +0000650 // These alternatives are considered only if type deduction would
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000651 // otherwise fail.
652 if (const RecordType *RecordT = dyn_cast<RecordType>(Arg)) {
653 // Use data recursion to crawl through the list of base classes.
Mike Stump1eb44332009-09-09 15:08:12 +0000654 // Visited contains the set of nodes we have already visited, while
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000655 // ToVisit is our stack of records that we still need to visit.
656 llvm::SmallPtrSet<const RecordType *, 8> Visited;
657 llvm::SmallVector<const RecordType *, 8> ToVisit;
658 ToVisit.push_back(RecordT);
659 bool Successful = false;
660 while (!ToVisit.empty()) {
661 // Retrieve the next class in the inheritance hierarchy.
662 const RecordType *NextT = ToVisit.back();
663 ToVisit.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000665 // If we have already seen this type, skip it.
666 if (!Visited.insert(NextT))
667 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000669 // If this is a base class, try to perform template argument
670 // deduction from it.
671 if (NextT != RecordT) {
672 Sema::TemplateDeductionResult BaseResult
673 = DeduceTemplateArguments(Context, TemplateParams, SpecParam,
674 QualType(NextT, 0), Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000676 // If template argument deduction for this base was successful,
677 // note that we had some success.
678 if (BaseResult == Sema::TDK_Success)
679 Successful = true;
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000680 }
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000682 // Visit base classes
683 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
684 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
685 BaseEnd = Next->bases_end();
Sebastian Redl9994a342009-10-25 17:03:50 +0000686 Base != BaseEnd; ++Base) {
Mike Stump1eb44332009-09-09 15:08:12 +0000687 assert(Base->getType()->isRecordType() &&
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000688 "Base class that isn't a record?");
Ted Kremenek6217b802009-07-29 21:53:49 +0000689 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000690 }
691 }
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000693 if (Successful)
694 return Sema::TDK_Success;
695 }
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000697 }
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000699 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000700 }
701
Douglas Gregor637a4092009-06-10 23:47:09 +0000702 // T type::*
703 // T T::*
704 // T (type::*)()
705 // type (T::*)()
706 // type (type::*)(T)
707 // type (T::*)(T)
708 // T (type::*)(T)
709 // T (T::*)()
710 // T (T::*)(T)
711 case Type::MemberPointer: {
712 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
713 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
714 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000715 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +0000716
Douglas Gregorf67875d2009-06-12 18:26:56 +0000717 if (Sema::TemplateDeductionResult Result
718 = DeduceTemplateArguments(Context, TemplateParams,
719 MemPtrParam->getPointeeType(),
720 MemPtrArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000721 Info, Deduced,
722 TDF & TDF_IgnoreQualifiers))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000723 return Result;
724
725 return DeduceTemplateArguments(Context, TemplateParams,
726 QualType(MemPtrParam->getClass(), 0),
727 QualType(MemPtrArg->getClass(), 0),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000728 Info, Deduced, 0);
Douglas Gregor637a4092009-06-10 23:47:09 +0000729 }
730
Anders Carlsson9a917e42009-06-12 22:56:54 +0000731 // (clang extension)
732 //
Mike Stump1eb44332009-09-09 15:08:12 +0000733 // type(^)(T)
734 // T(^)()
735 // T(^)(T)
Anders Carlsson859ba502009-06-12 16:23:10 +0000736 case Type::BlockPointer: {
737 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
738 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Anders Carlsson859ba502009-06-12 16:23:10 +0000740 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000741 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Douglas Gregorf67875d2009-06-12 18:26:56 +0000743 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +0000744 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000745 BlockPtrArg->getPointeeType(), Info,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000746 Deduced, 0);
Anders Carlsson859ba502009-06-12 16:23:10 +0000747 }
748
Douglas Gregor637a4092009-06-10 23:47:09 +0000749 case Type::TypeOfExpr:
750 case Type::TypeOf:
751 case Type::Typename:
752 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +0000753 return Sema::TDK_Success;
Douglas Gregor637a4092009-06-10 23:47:09 +0000754
Douglas Gregord560d502009-06-04 00:21:18 +0000755 default:
756 break;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000757 }
758
759 // FIXME: Many more cases to go (to go).
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000760 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000761}
762
Douglas Gregorf67875d2009-06-12 18:26:56 +0000763static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +0000764DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000765 TemplateParameterList *TemplateParams,
766 const TemplateArgument &Param,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000767 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000768 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000769 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000770 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000771 case TemplateArgument::Null:
772 assert(false && "Null template argument in parameter list");
773 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000774
775 case TemplateArgument::Type:
Douglas Gregor788cd062009-11-11 01:00:40 +0000776 if (Arg.getKind() == TemplateArgument::Type)
777 return DeduceTemplateArguments(Context, TemplateParams, Param.getAsType(),
778 Arg.getAsType(), Info, Deduced, 0);
779 Info.FirstArg = Param;
780 Info.SecondArg = Arg;
781 return Sema::TDK_NonDeducedMismatch;
782
783 case TemplateArgument::Template:
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000784 if (Arg.getKind() == TemplateArgument::Template)
Douglas Gregor788cd062009-11-11 01:00:40 +0000785 return DeduceTemplateArguments(Context, TemplateParams,
786 Param.getAsTemplate(),
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000787 Arg.getAsTemplate(), Info, Deduced);
Douglas Gregor788cd062009-11-11 01:00:40 +0000788 Info.FirstArg = Param;
789 Info.SecondArg = Arg;
790 return Sema::TDK_NonDeducedMismatch;
791
Douglas Gregor199d9912009-06-05 00:53:49 +0000792 case TemplateArgument::Declaration:
Douglas Gregor788cd062009-11-11 01:00:40 +0000793 if (Arg.getKind() == TemplateArgument::Declaration &&
794 Param.getAsDecl()->getCanonicalDecl() ==
795 Arg.getAsDecl()->getCanonicalDecl())
796 return Sema::TDK_Success;
797
Douglas Gregorf67875d2009-06-12 18:26:56 +0000798 Info.FirstArg = Param;
799 Info.SecondArg = Arg;
800 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Douglas Gregor199d9912009-06-05 00:53:49 +0000802 case TemplateArgument::Integral:
803 if (Arg.getKind() == TemplateArgument::Integral) {
804 // FIXME: Zero extension + sign checking here?
Douglas Gregorf67875d2009-06-12 18:26:56 +0000805 if (*Param.getAsIntegral() == *Arg.getAsIntegral())
806 return Sema::TDK_Success;
807
808 Info.FirstArg = Param;
809 Info.SecondArg = Arg;
810 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000811 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000812
813 if (Arg.getKind() == TemplateArgument::Expression) {
814 Info.FirstArg = Param;
815 Info.SecondArg = Arg;
816 return Sema::TDK_NonDeducedMismatch;
817 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000818
819 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000820 Info.FirstArg = Param;
821 Info.SecondArg = Arg;
822 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Douglas Gregor199d9912009-06-05 00:53:49 +0000824 case TemplateArgument::Expression: {
Mike Stump1eb44332009-09-09 15:08:12 +0000825 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000826 = getDeducedParameterFromExpr(Param.getAsExpr())) {
827 if (Arg.getKind() == TemplateArgument::Integral)
828 // FIXME: Sign problems here
Mike Stump1eb44332009-09-09 15:08:12 +0000829 return DeduceNonTypeTemplateArgument(Context, NTTP,
830 *Arg.getAsIntegral(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000831 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000832 if (Arg.getKind() == TemplateArgument::Expression)
833 return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000834 Info, Deduced);
Douglas Gregor15755cb2009-11-13 23:45:44 +0000835 if (Arg.getKind() == TemplateArgument::Declaration)
836 return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsDecl(),
837 Info, Deduced);
838
Douglas Gregor199d9912009-06-05 00:53:49 +0000839 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000840 Info.FirstArg = Param;
841 Info.SecondArg = Arg;
842 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000843 }
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Douglas Gregor199d9912009-06-05 00:53:49 +0000845 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000846 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000847 }
Anders Carlssond01b1da2009-06-15 17:04:53 +0000848 case TemplateArgument::Pack:
849 assert(0 && "FIXME: Implement!");
850 break;
Douglas Gregor199d9912009-06-05 00:53:49 +0000851 }
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Douglas Gregorf67875d2009-06-12 18:26:56 +0000853 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000854}
855
Mike Stump1eb44332009-09-09 15:08:12 +0000856static Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000857DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000858 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000859 const TemplateArgumentList &ParamList,
860 const TemplateArgumentList &ArgList,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000861 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000862 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
863 assert(ParamList.size() == ArgList.size());
864 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000865 if (Sema::TemplateDeductionResult Result
866 = DeduceTemplateArguments(Context, TemplateParams,
Mike Stump1eb44332009-09-09 15:08:12 +0000867 ParamList[I], ArgList[I],
Douglas Gregorf67875d2009-06-12 18:26:56 +0000868 Info, Deduced))
869 return Result;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000870 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000871 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000872}
873
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000874/// \brief Determine whether two template arguments are the same.
Mike Stump1eb44332009-09-09 15:08:12 +0000875static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000876 const TemplateArgument &X,
877 const TemplateArgument &Y) {
878 if (X.getKind() != Y.getKind())
879 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000881 switch (X.getKind()) {
882 case TemplateArgument::Null:
883 assert(false && "Comparing NULL template argument");
884 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000886 case TemplateArgument::Type:
887 return Context.getCanonicalType(X.getAsType()) ==
888 Context.getCanonicalType(Y.getAsType());
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000890 case TemplateArgument::Declaration:
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000891 return X.getAsDecl()->getCanonicalDecl() ==
892 Y.getAsDecl()->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Douglas Gregor788cd062009-11-11 01:00:40 +0000894 case TemplateArgument::Template:
895 return Context.getCanonicalTemplateName(X.getAsTemplate())
896 .getAsVoidPointer() ==
897 Context.getCanonicalTemplateName(Y.getAsTemplate())
898 .getAsVoidPointer();
899
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000900 case TemplateArgument::Integral:
901 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Douglas Gregor788cd062009-11-11 01:00:40 +0000903 case TemplateArgument::Expression: {
904 llvm::FoldingSetNodeID XID, YID;
905 X.getAsExpr()->Profile(XID, Context, true);
906 Y.getAsExpr()->Profile(YID, Context, true);
907 return XID == YID;
908 }
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000910 case TemplateArgument::Pack:
911 if (X.pack_size() != Y.pack_size())
912 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000913
914 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
915 XPEnd = X.pack_end(),
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000916 YP = Y.pack_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000917 XP != XPEnd; ++XP, ++YP)
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000918 if (!isSameTemplateArg(Context, *XP, *YP))
919 return false;
920
921 return true;
922 }
923
924 return false;
925}
926
927/// \brief Helper function to build a TemplateParameter when we don't
928/// know its type statically.
929static TemplateParameter makeTemplateParameter(Decl *D) {
930 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
931 return TemplateParameter(TTP);
932 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
933 return TemplateParameter(NTTP);
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000935 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
936}
937
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000938/// \brief Perform template argument deduction to determine whether
939/// the given template arguments match the given class template
940/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +0000941Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000942Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000943 const TemplateArgumentList &TemplateArgs,
944 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000945 // C++ [temp.class.spec.match]p2:
946 // A partial specialization matches a given actual template
947 // argument list if the template arguments of the partial
948 // specialization can be deduced from the actual template argument
949 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +0000950 SFINAETrap Trap(*this);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000951 llvm::SmallVector<TemplateArgument, 4> Deduced;
952 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000953 if (TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000954 = ::DeduceTemplateArguments(Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000955 Partial->getTemplateParameters(),
Mike Stump1eb44332009-09-09 15:08:12 +0000956 Partial->getTemplateArgs(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000957 TemplateArgs, Info, Deduced))
958 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +0000959
Douglas Gregor637a4092009-06-10 23:47:09 +0000960 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
961 Deduced.data(), Deduced.size());
962 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000963 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +0000964
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000965 // C++ [temp.deduct.type]p2:
966 // [...] or if any template argument remains neither deduced nor
967 // explicitly specified, template argument deduction fails.
Anders Carlssonfb250522009-06-23 01:26:57 +0000968 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
969 Deduced.size());
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000970 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000971 if (Deduced[I].isNull()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000972 Decl *Param
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000973 = const_cast<NamedDecl *>(
974 Partial->getTemplateParameters()->getParam(I));
Douglas Gregorf67875d2009-06-12 18:26:56 +0000975 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
976 Info.Param = TTP;
Mike Stump1eb44332009-09-09 15:08:12 +0000977 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorf67875d2009-06-12 18:26:56 +0000978 = dyn_cast<NonTypeTemplateParmDecl>(Param))
979 Info.Param = NTTP;
980 else
981 Info.Param = cast<TemplateTemplateParmDecl>(Param);
982 return TDK_Incomplete;
983 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000984
Anders Carlssonfb250522009-06-23 01:26:57 +0000985 Builder.Append(Deduced[I]);
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000986 }
987
988 // Form the template argument list from the deduced template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +0000989 TemplateArgumentList *DeducedArgumentList
Anders Carlssonfb250522009-06-23 01:26:57 +0000990 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000991 Info.reset(DeducedArgumentList);
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000992
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000993 // Substitute the deduced template arguments into the template
994 // arguments of the class template partial specialization, and
995 // verify that the instantiated template arguments are both valid
996 // and are equivalent to the template arguments originally provided
Mike Stump1eb44332009-09-09 15:08:12 +0000997 // to the class template.
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000998 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
John McCall833ca992009-10-29 08:12:44 +0000999 const TemplateArgumentLoc *PartialTemplateArgs
1000 = Partial->getTemplateArgsAsWritten();
1001 unsigned N = Partial->getNumTemplateArgsAsWritten();
John McCalld5532b62009-11-23 01:53:49 +00001002
1003 // Note that we don't provide the langle and rangle locations.
1004 TemplateArgumentListInfo InstArgs;
1005
John McCall833ca992009-10-29 08:12:44 +00001006 for (unsigned I = 0; I != N; ++I) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001007 Decl *Param = const_cast<NamedDecl *>(
Douglas Gregorc9e5d252009-06-13 00:59:32 +00001008 ClassTemplate->getTemplateParameters()->getParam(I));
John McCalld5532b62009-11-23 01:53:49 +00001009 TemplateArgumentLoc InstArg;
1010 if (Subst(PartialTemplateArgs[I], InstArg,
John McCall833ca992009-10-29 08:12:44 +00001011 MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001012 Info.Param = makeTemplateParameter(Param);
John McCall833ca992009-10-29 08:12:44 +00001013 Info.FirstArg = PartialTemplateArgs[I].getArgument();
Mike Stump1eb44332009-09-09 15:08:12 +00001014 return TDK_SubstitutionFailure;
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001015 }
John McCalld5532b62009-11-23 01:53:49 +00001016 InstArgs.addArgument(InstArg);
John McCall833ca992009-10-29 08:12:44 +00001017 }
1018
1019 TemplateArgumentListBuilder ConvertedInstArgs(
1020 ClassTemplate->getTemplateParameters(), N);
1021
1022 if (CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001023 InstArgs, false, ConvertedInstArgs)) {
John McCall833ca992009-10-29 08:12:44 +00001024 // FIXME: fail with more useful information?
1025 return TDK_SubstitutionFailure;
1026 }
1027
1028 for (unsigned I = 0, E = ConvertedInstArgs.flatSize(); I != E; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001029 TemplateArgument InstArg = ConvertedInstArgs.getFlatArguments()[I];
John McCall833ca992009-10-29 08:12:44 +00001030
1031 Decl *Param = const_cast<NamedDecl *>(
1032 ClassTemplate->getTemplateParameters()->getParam(I));
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001034 if (InstArg.getKind() == TemplateArgument::Expression) {
Mike Stump1eb44332009-09-09 15:08:12 +00001035 // When the argument is an expression, check the expression result
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001036 // against the actual template parameter to get down to the canonical
1037 // template argument.
1038 Expr *InstExpr = InstArg.getAsExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001039 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001040 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1041 if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
1042 Info.Param = makeTemplateParameter(Param);
John McCall833ca992009-10-29 08:12:44 +00001043 Info.FirstArg = Partial->getTemplateArgs()[I];
Mike Stump1eb44332009-09-09 15:08:12 +00001044 return TDK_SubstitutionFailure;
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001045 }
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001046 }
1047 }
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001049 if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) {
1050 Info.Param = makeTemplateParameter(Param);
1051 Info.FirstArg = TemplateArgs[I];
1052 Info.SecondArg = InstArg;
1053 return TDK_NonDeducedMismatch;
1054 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001055 }
1056
Douglas Gregorbb260412009-06-14 08:02:22 +00001057 if (Trap.hasErrorOccurred())
1058 return TDK_SubstitutionFailure;
1059
Douglas Gregorf67875d2009-06-12 18:26:56 +00001060 return TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001061}
Douglas Gregor031a5882009-06-13 00:26:55 +00001062
Douglas Gregor41128772009-06-26 23:27:24 +00001063/// \brief Determine whether the given type T is a simple-template-id type.
1064static bool isSimpleTemplateIdType(QualType T) {
Mike Stump1eb44332009-09-09 15:08:12 +00001065 if (const TemplateSpecializationType *Spec
John McCall183700f2009-09-21 23:43:11 +00001066 = T->getAs<TemplateSpecializationType>())
Douglas Gregor41128772009-06-26 23:27:24 +00001067 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Douglas Gregor41128772009-06-26 23:27:24 +00001069 return false;
1070}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001071
1072/// \brief Substitute the explicitly-provided template arguments into the
1073/// given function template according to C++ [temp.arg.explicit].
1074///
1075/// \param FunctionTemplate the function template into which the explicit
1076/// template arguments will be substituted.
1077///
Mike Stump1eb44332009-09-09 15:08:12 +00001078/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001079/// arguments.
1080///
Mike Stump1eb44332009-09-09 15:08:12 +00001081/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor83314aa2009-07-08 20:55:45 +00001082/// with the converted and checked explicit template arguments.
1083///
Mike Stump1eb44332009-09-09 15:08:12 +00001084/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor83314aa2009-07-08 20:55:45 +00001085/// parameters.
1086///
1087/// \param FunctionType if non-NULL, the result type of the function template
1088/// will also be instantiated and the pointed-to value will be updated with
1089/// the instantiated function type.
1090///
1091/// \param Info if substitution fails for any reason, this object will be
1092/// populated with more information about the failure.
1093///
1094/// \returns TDK_Success if substitution was successful, or some failure
1095/// condition.
1096Sema::TemplateDeductionResult
1097Sema::SubstituteExplicitTemplateArguments(
1098 FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001099 const TemplateArgumentListInfo &ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001100 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1101 llvm::SmallVectorImpl<QualType> &ParamTypes,
1102 QualType *FunctionType,
1103 TemplateDeductionInfo &Info) {
1104 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1105 TemplateParameterList *TemplateParams
1106 = FunctionTemplate->getTemplateParameters();
1107
John McCalld5532b62009-11-23 01:53:49 +00001108 if (ExplicitTemplateArgs.size() == 0) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001109 // No arguments to substitute; just copy over the parameter types and
1110 // fill in the function type.
1111 for (FunctionDecl::param_iterator P = Function->param_begin(),
1112 PEnd = Function->param_end();
1113 P != PEnd;
1114 ++P)
1115 ParamTypes.push_back((*P)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001116
Douglas Gregor83314aa2009-07-08 20:55:45 +00001117 if (FunctionType)
1118 *FunctionType = Function->getType();
1119 return TDK_Success;
1120 }
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Douglas Gregor83314aa2009-07-08 20:55:45 +00001122 // Substitution of the explicit template arguments into a function template
1123 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001124 SFINAETrap Trap(*this);
1125
Douglas Gregor83314aa2009-07-08 20:55:45 +00001126 // C++ [temp.arg.explicit]p3:
Mike Stump1eb44332009-09-09 15:08:12 +00001127 // Template arguments that are present shall be specified in the
1128 // declaration order of their corresponding template-parameters. The
Douglas Gregor83314aa2009-07-08 20:55:45 +00001129 // template argument list shall not specify more template-arguments than
Mike Stump1eb44332009-09-09 15:08:12 +00001130 // there are corresponding template-parameters.
1131 TemplateArgumentListBuilder Builder(TemplateParams,
John McCalld5532b62009-11-23 01:53:49 +00001132 ExplicitTemplateArgs.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001133
1134 // Enter a new template instantiation context where we check the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001135 // explicitly-specified template arguments against this function template,
1136 // and then substitute them into the function parameter types.
Mike Stump1eb44332009-09-09 15:08:12 +00001137 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001138 FunctionTemplate, Deduced.data(), Deduced.size(),
1139 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution);
1140 if (Inst)
1141 return TDK_InstantiationDepth;
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Douglas Gregor83314aa2009-07-08 20:55:45 +00001143 if (CheckTemplateArgumentList(FunctionTemplate,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001144 SourceLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001145 ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001146 true,
1147 Builder) || Trap.hasErrorOccurred())
1148 return TDK_InvalidExplicitArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Douglas Gregor83314aa2009-07-08 20:55:45 +00001150 // Form the template argument list from the explicitly-specified
1151 // template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001152 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor83314aa2009-07-08 20:55:45 +00001153 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1154 Info.reset(ExplicitArgumentList);
Mike Stump1eb44332009-09-09 15:08:12 +00001155
Douglas Gregor83314aa2009-07-08 20:55:45 +00001156 // Instantiate the types of each of the function parameters given the
1157 // explicitly-specified template arguments.
1158 for (FunctionDecl::param_iterator P = Function->param_begin(),
1159 PEnd = Function->param_end();
1160 P != PEnd;
1161 ++P) {
Mike Stump1eb44332009-09-09 15:08:12 +00001162 QualType ParamType
1163 = SubstType((*P)->getType(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001164 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1165 (*P)->getLocation(), (*P)->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001166 if (ParamType.isNull() || Trap.hasErrorOccurred())
1167 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Douglas Gregor83314aa2009-07-08 20:55:45 +00001169 ParamTypes.push_back(ParamType);
1170 }
1171
1172 // If the caller wants a full function type back, instantiate the return
1173 // type and form that function type.
1174 if (FunctionType) {
1175 // FIXME: exception-specifications?
Mike Stump1eb44332009-09-09 15:08:12 +00001176 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001177 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001178 assert(Proto && "Function template does not have a prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001179
1180 QualType ResultType
Douglas Gregor357bbd02009-08-28 20:50:45 +00001181 = SubstType(Proto->getResultType(),
1182 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1183 Function->getTypeSpecStartLoc(),
1184 Function->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001185 if (ResultType.isNull() || Trap.hasErrorOccurred())
1186 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001187
1188 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001189 ParamTypes.data(), ParamTypes.size(),
1190 Proto->isVariadic(),
1191 Proto->getTypeQuals(),
1192 Function->getLocation(),
1193 Function->getDeclName());
1194 if (FunctionType->isNull() || Trap.hasErrorOccurred())
1195 return TDK_SubstitutionFailure;
1196 }
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Douglas Gregor83314aa2009-07-08 20:55:45 +00001198 // C++ [temp.arg.explicit]p2:
Mike Stump1eb44332009-09-09 15:08:12 +00001199 // Trailing template arguments that can be deduced (14.8.2) may be
1200 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001201 // template arguments can be deduced, they may all be omitted; in this
1202 // case, the empty template argument list <> itself may also be omitted.
1203 //
1204 // Take all of the explicitly-specified arguments and put them into the
Mike Stump1eb44332009-09-09 15:08:12 +00001205 // set of deduced template arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001206 Deduced.reserve(TemplateParams->size());
1207 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001208 Deduced.push_back(ExplicitArgumentList->get(I));
1209
Douglas Gregor83314aa2009-07-08 20:55:45 +00001210 return TDK_Success;
1211}
1212
Mike Stump1eb44332009-09-09 15:08:12 +00001213/// \brief Finish template argument deduction for a function template,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001214/// checking the deduced template arguments for completeness and forming
1215/// the function template specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00001216Sema::TemplateDeductionResult
Douglas Gregor83314aa2009-07-08 20:55:45 +00001217Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
1218 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1219 FunctionDecl *&Specialization,
1220 TemplateDeductionInfo &Info) {
1221 TemplateParameterList *TemplateParams
1222 = FunctionTemplate->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Douglas Gregor83314aa2009-07-08 20:55:45 +00001224 // Template argument deduction for function templates in a SFINAE context.
1225 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001226 SFINAETrap Trap(*this);
1227
Douglas Gregor83314aa2009-07-08 20:55:45 +00001228 // Enter a new template instantiation context while we instantiate the
1229 // actual function declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001230 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001231 FunctionTemplate, Deduced.data(), Deduced.size(),
1232 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
1233 if (Inst)
Mike Stump1eb44332009-09-09 15:08:12 +00001234 return TDK_InstantiationDepth;
1235
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001236 // C++ [temp.deduct.type]p2:
1237 // [...] or if any template argument remains neither deduced nor
1238 // explicitly specified, template argument deduction fails.
1239 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
1240 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
1241 if (!Deduced[I].isNull()) {
1242 Builder.Append(Deduced[I]);
1243 continue;
1244 }
1245
1246 // Substitute into the default template argument, if available.
1247 NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I);
1248 TemplateArgumentLoc DefArg
1249 = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
1250 FunctionTemplate->getLocation(),
1251 FunctionTemplate->getSourceRange().getEnd(),
1252 Param,
1253 Builder);
1254
1255 // If there was no default argument, deduction is incomplete.
1256 if (DefArg.getArgument().isNull()) {
1257 Info.Param = makeTemplateParameter(
1258 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1259 return TDK_Incomplete;
1260 }
1261
1262 // Check whether we can actually use the default argument.
1263 if (CheckTemplateArgument(Param, DefArg,
1264 FunctionTemplate,
1265 FunctionTemplate->getLocation(),
1266 FunctionTemplate->getSourceRange().getEnd(),
1267 Builder)) {
1268 Info.Param = makeTemplateParameter(
1269 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1270 return TDK_SubstitutionFailure;
1271 }
1272
1273 // If we get here, we successfully used the default template argument.
1274 }
1275
1276 // Form the template argument list from the deduced template arguments.
1277 TemplateArgumentList *DeducedArgumentList
1278 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1279 Info.reset(DeducedArgumentList);
1280
Mike Stump1eb44332009-09-09 15:08:12 +00001281 // Substitute the deduced template arguments into the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001282 // declaration to produce the function template specialization.
1283 Specialization = cast_or_null<FunctionDecl>(
John McCallce3ff2b2009-08-25 22:02:44 +00001284 SubstDecl(FunctionTemplate->getTemplatedDecl(),
1285 FunctionTemplate->getDeclContext(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001286 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor83314aa2009-07-08 20:55:45 +00001287 if (!Specialization)
1288 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Douglas Gregorf8825742009-09-15 18:26:13 +00001290 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
1291 FunctionTemplate->getCanonicalDecl());
1292
Mike Stump1eb44332009-09-09 15:08:12 +00001293 // If the template argument list is owned by the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001294 // specialization, release it.
1295 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList)
1296 Info.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Douglas Gregor83314aa2009-07-08 20:55:45 +00001298 // There may have been an error that did not prevent us from constructing a
1299 // declaration. Mark the declaration invalid and return with a substitution
1300 // failure.
1301 if (Trap.hasErrorOccurred()) {
1302 Specialization->setInvalidDecl(true);
1303 return TDK_SubstitutionFailure;
1304 }
Mike Stump1eb44332009-09-09 15:08:12 +00001305
1306 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001307}
1308
John McCalleff92132010-02-02 02:21:27 +00001309static QualType GetTypeOfFunction(ASTContext &Context,
1310 bool isAddressOfOperand,
1311 FunctionDecl *Fn) {
1312 if (!isAddressOfOperand) return Fn->getType();
1313 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
1314 if (Method->isInstance())
1315 return Context.getMemberPointerType(Fn->getType(),
1316 Context.getTypeDeclType(Method->getParent()).getTypePtr());
1317 return Context.getPointerType(Fn->getType());
1318}
1319
1320/// Apply the deduction rules for overload sets.
1321///
1322/// \return the null type if this argument should be treated as an
1323/// undeduced context
1324static QualType
1325ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
1326 Expr *Arg, QualType ParamType) {
John McCall7bb12da2010-02-02 06:20:04 +00001327 llvm::PointerIntPair<OverloadExpr*,1> R = OverloadExpr::find(Arg);
John McCalleff92132010-02-02 02:21:27 +00001328
John McCall7bb12da2010-02-02 06:20:04 +00001329 bool isAddressOfOperand = bool(R.getInt());
1330 OverloadExpr *Ovl = R.getPointer();
John McCalleff92132010-02-02 02:21:27 +00001331
1332 // If there were explicit template arguments, we can only find
1333 // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
1334 // unambiguously name a full specialization.
John McCall7bb12da2010-02-02 06:20:04 +00001335 if (Ovl->hasExplicitTemplateArgs()) {
John McCalleff92132010-02-02 02:21:27 +00001336 // But we can still look for an explicit specialization.
1337 if (FunctionDecl *ExplicitSpec
John McCall7bb12da2010-02-02 06:20:04 +00001338 = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
1339 return GetTypeOfFunction(S.Context, isAddressOfOperand, ExplicitSpec);
John McCalleff92132010-02-02 02:21:27 +00001340 return QualType();
1341 }
1342
1343 // C++0x [temp.deduct.call]p6:
1344 // When P is a function type, pointer to function type, or pointer
1345 // to member function type:
1346
1347 if (!ParamType->isFunctionType() &&
1348 !ParamType->isFunctionPointerType() &&
1349 !ParamType->isMemberFunctionPointerType())
1350 return QualType();
1351
1352 QualType Match;
John McCall7bb12da2010-02-02 06:20:04 +00001353 for (UnresolvedSetIterator I = Ovl->decls_begin(),
1354 E = Ovl->decls_end(); I != E; ++I) {
John McCalleff92132010-02-02 02:21:27 +00001355 NamedDecl *D = (*I)->getUnderlyingDecl();
1356
1357 // - If the argument is an overload set containing one or more
1358 // function templates, the parameter is treated as a
1359 // non-deduced context.
1360 if (isa<FunctionTemplateDecl>(D))
1361 return QualType();
1362
1363 FunctionDecl *Fn = cast<FunctionDecl>(D);
1364 QualType ArgType = GetTypeOfFunction(S.Context, isAddressOfOperand, Fn);
1365
1366 // - If the argument is an overload set (not containing function
1367 // templates), trial argument deduction is attempted using each
1368 // of the members of the set. If deduction succeeds for only one
1369 // of the overload set members, that member is used as the
1370 // argument value for the deduction. If deduction succeeds for
1371 // more than one member of the overload set the parameter is
1372 // treated as a non-deduced context.
1373
1374 // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
1375 // Type deduction is done independently for each P/A pair, and
1376 // the deduced template argument values are then combined.
1377 // So we do not reject deductions which were made elsewhere.
1378 llvm::SmallVector<TemplateArgument, 8> Deduced(TemplateParams->size());
1379 Sema::TemplateDeductionInfo Info(S.Context);
1380 unsigned TDF = 0;
1381
1382 Sema::TemplateDeductionResult Result
1383 = DeduceTemplateArguments(S.Context, TemplateParams,
1384 ParamType, ArgType,
1385 Info, Deduced, TDF);
1386 if (Result) continue;
1387 if (!Match.isNull()) return QualType();
1388 Match = ArgType;
1389 }
1390
1391 return Match;
1392}
1393
Douglas Gregore53060f2009-06-25 22:08:12 +00001394/// \brief Perform template argument deduction from a function call
1395/// (C++ [temp.deduct.call]).
1396///
1397/// \param FunctionTemplate the function template for which we are performing
1398/// template argument deduction.
1399///
Douglas Gregor48026d22010-01-11 18:40:55 +00001400/// \param ExplicitTemplateArguments the explicit template arguments provided
1401/// for this call.
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001402///
Douglas Gregore53060f2009-06-25 22:08:12 +00001403/// \param Args the function call arguments
1404///
1405/// \param NumArgs the number of arguments in Args
1406///
Douglas Gregor48026d22010-01-11 18:40:55 +00001407/// \param Name the name of the function being called. This is only significant
1408/// when the function template is a conversion function template, in which
1409/// case this routine will also perform template argument deduction based on
1410/// the function to which
1411///
Douglas Gregore53060f2009-06-25 22:08:12 +00001412/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001413/// this will be set to the function template specialization produced by
Douglas Gregore53060f2009-06-25 22:08:12 +00001414/// template argument deduction.
1415///
1416/// \param Info the argument will be updated to provide additional information
1417/// about template argument deduction.
1418///
1419/// \returns the result of template argument deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001420Sema::TemplateDeductionResult
1421Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor48026d22010-01-11 18:40:55 +00001422 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregore53060f2009-06-25 22:08:12 +00001423 Expr **Args, unsigned NumArgs,
1424 FunctionDecl *&Specialization,
1425 TemplateDeductionInfo &Info) {
1426 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001427
Douglas Gregore53060f2009-06-25 22:08:12 +00001428 // C++ [temp.deduct.call]p1:
1429 // Template argument deduction is done by comparing each function template
1430 // parameter type (call it P) with the type of the corresponding argument
1431 // of the call (call it A) as described below.
1432 unsigned CheckArgs = NumArgs;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001433 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregore53060f2009-06-25 22:08:12 +00001434 return TDK_TooFewArguments;
1435 else if (NumArgs > Function->getNumParams()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001436 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001437 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregore53060f2009-06-25 22:08:12 +00001438 if (!Proto->isVariadic())
1439 return TDK_TooManyArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Douglas Gregore53060f2009-06-25 22:08:12 +00001441 CheckArgs = Function->getNumParams();
1442 }
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001444 // The types of the parameters from which we will perform template argument
1445 // deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001446 TemplateParameterList *TemplateParams
1447 = FunctionTemplate->getTemplateParameters();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001448 llvm::SmallVector<TemplateArgument, 4> Deduced;
1449 llvm::SmallVector<QualType, 4> ParamTypes;
John McCalld5532b62009-11-23 01:53:49 +00001450 if (ExplicitTemplateArgs) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001451 TemplateDeductionResult Result =
1452 SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001453 *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001454 Deduced,
1455 ParamTypes,
1456 0,
1457 Info);
1458 if (Result)
1459 return Result;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001460 } else {
1461 // Just fill in the parameter types from the function declaration.
1462 for (unsigned I = 0; I != CheckArgs; ++I)
1463 ParamTypes.push_back(Function->getParamDecl(I)->getType());
1464 }
Mike Stump1eb44332009-09-09 15:08:12 +00001465
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001466 // Deduce template arguments from the function parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001467 Deduced.resize(TemplateParams->size());
Douglas Gregore53060f2009-06-25 22:08:12 +00001468 for (unsigned I = 0; I != CheckArgs; ++I) {
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001469 QualType ParamType = ParamTypes[I];
Douglas Gregore53060f2009-06-25 22:08:12 +00001470 QualType ArgType = Args[I]->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001471
John McCalleff92132010-02-02 02:21:27 +00001472 // Overload sets usually make this parameter an undeduced
1473 // context, but there are sometimes special circumstances.
1474 if (ArgType == Context.OverloadTy) {
1475 ArgType = ResolveOverloadForDeduction(*this, TemplateParams,
1476 Args[I], ParamType);
1477 if (ArgType.isNull())
1478 continue;
1479 }
1480
Douglas Gregore53060f2009-06-25 22:08:12 +00001481 // C++ [temp.deduct.call]p2:
1482 // If P is not a reference type:
1483 QualType CanonParamType = Context.getCanonicalType(ParamType);
Douglas Gregor500d3312009-06-26 18:27:22 +00001484 bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1485 if (!ParamWasReference) {
Mike Stump1eb44332009-09-09 15:08:12 +00001486 // - If A is an array type, the pointer type produced by the
1487 // array-to-pointer standard conversion (4.2) is used in place of
Douglas Gregore53060f2009-06-25 22:08:12 +00001488 // A for type deduction; otherwise,
1489 if (ArgType->isArrayType())
1490 ArgType = Context.getArrayDecayedType(ArgType);
Mike Stump1eb44332009-09-09 15:08:12 +00001491 // - If A is a function type, the pointer type produced by the
1492 // function-to-pointer standard conversion (4.3) is used in place
Douglas Gregore53060f2009-06-25 22:08:12 +00001493 // of A for type deduction; otherwise,
1494 else if (ArgType->isFunctionType())
1495 ArgType = Context.getPointerType(ArgType);
1496 else {
1497 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1498 // type are ignored for type deduction.
1499 QualType CanonArgType = Context.getCanonicalType(ArgType);
Douglas Gregora4923eb2009-11-16 21:35:15 +00001500 if (CanonArgType.getLocalCVRQualifiers())
1501 ArgType = CanonArgType.getLocalUnqualifiedType();
Douglas Gregore53060f2009-06-25 22:08:12 +00001502 }
1503 }
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Douglas Gregore53060f2009-06-25 22:08:12 +00001505 // C++0x [temp.deduct.call]p3:
1506 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
Mike Stump1eb44332009-09-09 15:08:12 +00001507 // are ignored for type deduction.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001508 if (CanonParamType.getLocalCVRQualifiers())
1509 ParamType = CanonParamType.getLocalUnqualifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001510 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001511 // [...] If P is a reference type, the type referred to by P is used
1512 // for type deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001513 ParamType = ParamRefType->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00001514
1515 // [...] If P is of the form T&&, where T is a template parameter, and
1516 // the argument is an lvalue, the type A& is used in place of A for
Douglas Gregore53060f2009-06-25 22:08:12 +00001517 // type deduction.
1518 if (isa<RValueReferenceType>(ParamRefType) &&
John McCall183700f2009-09-21 23:43:11 +00001519 ParamRefType->getAs<TemplateTypeParmType>() &&
Douglas Gregore53060f2009-06-25 22:08:12 +00001520 Args[I]->isLvalue(Context) == Expr::LV_Valid)
1521 ArgType = Context.getLValueReferenceType(ArgType);
1522 }
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Douglas Gregore53060f2009-06-25 22:08:12 +00001524 // C++0x [temp.deduct.call]p4:
1525 // In general, the deduction process attempts to find template argument
1526 // values that will make the deduced A identical to A (after the type A
1527 // is transformed as described above). [...]
Douglas Gregor12820292009-09-14 20:00:47 +00001528 unsigned TDF = TDF_SkipNonDependent;
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Douglas Gregor508f1c82009-06-26 23:10:12 +00001530 // - If the original P is a reference type, the deduced A (i.e., the
1531 // type referred to by the reference) can be more cv-qualified than
1532 // the transformed A.
1533 if (ParamWasReference)
1534 TDF |= TDF_ParamWithReferenceType;
Mike Stump1eb44332009-09-09 15:08:12 +00001535 // - The transformed A can be another pointer or pointer to member
1536 // type that can be converted to the deduced A via a qualification
Douglas Gregor508f1c82009-06-26 23:10:12 +00001537 // conversion (4.4).
1538 if (ArgType->isPointerType() || ArgType->isMemberPointerType())
1539 TDF |= TDF_IgnoreQualifiers;
Mike Stump1eb44332009-09-09 15:08:12 +00001540 // - If P is a class and P has the form simple-template-id, then the
Douglas Gregor41128772009-06-26 23:27:24 +00001541 // transformed A can be a derived class of the deduced A. Likewise,
1542 // if P is a pointer to a class of the form simple-template-id, the
1543 // transformed A can be a pointer to a derived class pointed to by
1544 // the deduced A.
1545 if (isSimpleTemplateIdType(ParamType) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001546 (isa<PointerType>(ParamType) &&
Douglas Gregor41128772009-06-26 23:27:24 +00001547 isSimpleTemplateIdType(
Ted Kremenek6217b802009-07-29 21:53:49 +00001548 ParamType->getAs<PointerType>()->getPointeeType())))
Douglas Gregor41128772009-06-26 23:27:24 +00001549 TDF |= TDF_DerivedClass;
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Douglas Gregore53060f2009-06-25 22:08:12 +00001551 if (TemplateDeductionResult Result
1552 = ::DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor500d3312009-06-26 18:27:22 +00001553 ParamType, ArgType, Info, Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +00001554 TDF))
Douglas Gregore53060f2009-06-25 22:08:12 +00001555 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001557 // FIXME: we need to check that the deduced A is the same as A,
1558 // modulo the various allowed differences.
Douglas Gregore53060f2009-06-25 22:08:12 +00001559 }
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001560
Mike Stump1eb44332009-09-09 15:08:12 +00001561 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001562 Specialization, Info);
Douglas Gregore53060f2009-06-25 22:08:12 +00001563}
1564
Douglas Gregor83314aa2009-07-08 20:55:45 +00001565/// \brief Deduce template arguments when taking the address of a function
Douglas Gregor4b52e252009-12-21 23:17:24 +00001566/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
1567/// a template.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001568///
1569/// \param FunctionTemplate the function template for which we are performing
1570/// template argument deduction.
1571///
Douglas Gregor4b52e252009-12-21 23:17:24 +00001572/// \param ExplicitTemplateArguments the explicitly-specified template
1573/// arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001574///
1575/// \param ArgFunctionType the function type that will be used as the
1576/// "argument" type (A) when performing template argument deduction from the
Douglas Gregor4b52e252009-12-21 23:17:24 +00001577/// function template's function type. This type may be NULL, if there is no
1578/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001579///
1580/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001581/// this will be set to the function template specialization produced by
Douglas Gregor83314aa2009-07-08 20:55:45 +00001582/// template argument deduction.
1583///
1584/// \param Info the argument will be updated to provide additional information
1585/// about template argument deduction.
1586///
1587/// \returns the result of template argument deduction.
1588Sema::TemplateDeductionResult
1589Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001590 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001591 QualType ArgFunctionType,
1592 FunctionDecl *&Specialization,
1593 TemplateDeductionInfo &Info) {
1594 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1595 TemplateParameterList *TemplateParams
1596 = FunctionTemplate->getTemplateParameters();
1597 QualType FunctionType = Function->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Douglas Gregor83314aa2009-07-08 20:55:45 +00001599 // Substitute any explicit template arguments.
1600 llvm::SmallVector<TemplateArgument, 4> Deduced;
1601 llvm::SmallVector<QualType, 4> ParamTypes;
John McCalld5532b62009-11-23 01:53:49 +00001602 if (ExplicitTemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001603 if (TemplateDeductionResult Result
1604 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001605 *ExplicitTemplateArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00001606 Deduced, ParamTypes,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001607 &FunctionType, Info))
1608 return Result;
1609 }
1610
1611 // Template argument deduction for function templates in a SFINAE context.
1612 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001613 SFINAETrap Trap(*this);
1614
John McCalleff92132010-02-02 02:21:27 +00001615 Deduced.resize(TemplateParams->size());
1616
Douglas Gregor4b52e252009-12-21 23:17:24 +00001617 if (!ArgFunctionType.isNull()) {
1618 // Deduce template arguments from the function type.
Douglas Gregor4b52e252009-12-21 23:17:24 +00001619 if (TemplateDeductionResult Result
1620 = ::DeduceTemplateArguments(Context, TemplateParams,
1621 FunctionType, ArgFunctionType, Info,
1622 Deduced, 0))
1623 return Result;
1624 }
1625
Mike Stump1eb44332009-09-09 15:08:12 +00001626 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001627 Specialization, Info);
1628}
1629
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001630/// \brief Deduce template arguments for a templated conversion
1631/// function (C++ [temp.deduct.conv]) and, if successful, produce a
1632/// conversion function template specialization.
1633Sema::TemplateDeductionResult
1634Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1635 QualType ToType,
1636 CXXConversionDecl *&Specialization,
1637 TemplateDeductionInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00001638 CXXConversionDecl *Conv
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001639 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
1640 QualType FromType = Conv->getConversionType();
1641
1642 // Canonicalize the types for deduction.
1643 QualType P = Context.getCanonicalType(FromType);
1644 QualType A = Context.getCanonicalType(ToType);
1645
1646 // C++0x [temp.deduct.conv]p3:
1647 // If P is a reference type, the type referred to by P is used for
1648 // type deduction.
1649 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
1650 P = PRef->getPointeeType();
1651
1652 // C++0x [temp.deduct.conv]p3:
1653 // If A is a reference type, the type referred to by A is used
1654 // for type deduction.
1655 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
1656 A = ARef->getPointeeType();
1657 // C++ [temp.deduct.conv]p2:
1658 //
Mike Stump1eb44332009-09-09 15:08:12 +00001659 // If A is not a reference type:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001660 else {
1661 assert(!A->isReferenceType() && "Reference types were handled above");
1662
1663 // - If P is an array type, the pointer type produced by the
Mike Stump1eb44332009-09-09 15:08:12 +00001664 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001665 // of P for type deduction; otherwise,
1666 if (P->isArrayType())
1667 P = Context.getArrayDecayedType(P);
1668 // - If P is a function type, the pointer type produced by the
1669 // function-to-pointer standard conversion (4.3) is used in
1670 // place of P for type deduction; otherwise,
1671 else if (P->isFunctionType())
1672 P = Context.getPointerType(P);
1673 // - If P is a cv-qualified type, the top level cv-qualifiers of
1674 // P’s type are ignored for type deduction.
1675 else
1676 P = P.getUnqualifiedType();
1677
1678 // C++0x [temp.deduct.conv]p3:
1679 // If A is a cv-qualified type, the top level cv-qualifiers of A’s
1680 // type are ignored for type deduction.
1681 A = A.getUnqualifiedType();
1682 }
1683
1684 // Template argument deduction for function templates in a SFINAE context.
1685 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001686 SFINAETrap Trap(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001687
1688 // C++ [temp.deduct.conv]p1:
1689 // Template argument deduction is done by comparing the return
1690 // type of the template conversion function (call it P) with the
1691 // type that is required as the result of the conversion (call it
1692 // A) as described in 14.8.2.4.
1693 TemplateParameterList *TemplateParams
1694 = FunctionTemplate->getTemplateParameters();
1695 llvm::SmallVector<TemplateArgument, 4> Deduced;
Mike Stump1eb44332009-09-09 15:08:12 +00001696 Deduced.resize(TemplateParams->size());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001697
1698 // C++0x [temp.deduct.conv]p4:
1699 // In general, the deduction process attempts to find template
1700 // argument values that will make the deduced A identical to
1701 // A. However, there are two cases that allow a difference:
1702 unsigned TDF = 0;
1703 // - If the original A is a reference type, A can be more
1704 // cv-qualified than the deduced A (i.e., the type referred to
1705 // by the reference)
1706 if (ToType->isReferenceType())
1707 TDF |= TDF_ParamWithReferenceType;
1708 // - The deduced A can be another pointer or pointer to member
1709 // type that can be converted to A via a qualification
1710 // conversion.
1711 //
1712 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
1713 // both P and A are pointers or member pointers. In this case, we
1714 // just ignore cv-qualifiers completely).
1715 if ((P->isPointerType() && A->isPointerType()) ||
1716 (P->isMemberPointerType() && P->isMemberPointerType()))
1717 TDF |= TDF_IgnoreQualifiers;
1718 if (TemplateDeductionResult Result
1719 = ::DeduceTemplateArguments(Context, TemplateParams,
1720 P, A, Info, Deduced, TDF))
1721 return Result;
1722
1723 // FIXME: we need to check that the deduced A is the same as A,
1724 // modulo the various allowed differences.
Mike Stump1eb44332009-09-09 15:08:12 +00001725
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001726 // Finish template argument deduction.
1727 FunctionDecl *Spec = 0;
1728 TemplateDeductionResult Result
1729 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, Spec, Info);
1730 Specialization = cast_or_null<CXXConversionDecl>(Spec);
1731 return Result;
1732}
1733
Douglas Gregor4b52e252009-12-21 23:17:24 +00001734/// \brief Deduce template arguments for a function template when there is
1735/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
1736///
1737/// \param FunctionTemplate the function template for which we are performing
1738/// template argument deduction.
1739///
1740/// \param ExplicitTemplateArguments the explicitly-specified template
1741/// arguments.
1742///
1743/// \param Specialization if template argument deduction was successful,
1744/// this will be set to the function template specialization produced by
1745/// template argument deduction.
1746///
1747/// \param Info the argument will be updated to provide additional information
1748/// about template argument deduction.
1749///
1750/// \returns the result of template argument deduction.
1751Sema::TemplateDeductionResult
1752Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1753 const TemplateArgumentListInfo *ExplicitTemplateArgs,
1754 FunctionDecl *&Specialization,
1755 TemplateDeductionInfo &Info) {
1756 return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
1757 QualType(), Specialization, Info);
1758}
1759
Douglas Gregor8a514912009-09-14 18:39:43 +00001760/// \brief Stores the result of comparing the qualifiers of two types.
1761enum DeductionQualifierComparison {
1762 NeitherMoreQualified = 0,
1763 ParamMoreQualified,
1764 ArgMoreQualified
1765};
1766
1767/// \brief Deduce the template arguments during partial ordering by comparing
1768/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
1769///
1770/// \param Context the AST context in which this deduction occurs.
1771///
1772/// \param TemplateParams the template parameters that we are deducing
1773///
1774/// \param ParamIn the parameter type
1775///
1776/// \param ArgIn the argument type
1777///
1778/// \param Info information about the template argument deduction itself
1779///
1780/// \param Deduced the deduced template arguments
1781///
1782/// \returns the result of template argument deduction so far. Note that a
1783/// "success" result means that template argument deduction has not yet failed,
1784/// but it may still fail, later, for other reasons.
1785static Sema::TemplateDeductionResult
1786DeduceTemplateArgumentsDuringPartialOrdering(ASTContext &Context,
1787 TemplateParameterList *TemplateParams,
1788 QualType ParamIn, QualType ArgIn,
1789 Sema::TemplateDeductionInfo &Info,
1790 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1791 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1792 CanQualType Param = Context.getCanonicalType(ParamIn);
1793 CanQualType Arg = Context.getCanonicalType(ArgIn);
1794
1795 // C++0x [temp.deduct.partial]p5:
1796 // Before the partial ordering is done, certain transformations are
1797 // performed on the types used for partial ordering:
1798 // - If P is a reference type, P is replaced by the type referred to.
1799 CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00001800 if (!ParamRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00001801 Param = ParamRef->getPointeeType();
1802
1803 // - If A is a reference type, A is replaced by the type referred to.
1804 CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00001805 if (!ArgRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00001806 Arg = ArgRef->getPointeeType();
1807
John McCalle27ec8a2009-10-23 23:03:21 +00001808 if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
Douglas Gregor8a514912009-09-14 18:39:43 +00001809 // C++0x [temp.deduct.partial]p6:
1810 // If both P and A were reference types (before being replaced with the
1811 // type referred to above), determine which of the two types (if any) is
1812 // more cv-qualified than the other; otherwise the types are considered to
1813 // be equally cv-qualified for partial ordering purposes. The result of this
1814 // determination will be used below.
1815 //
1816 // We save this information for later, using it only when deduction
1817 // succeeds in both directions.
1818 DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
1819 if (Param.isMoreQualifiedThan(Arg))
1820 QualifierResult = ParamMoreQualified;
1821 else if (Arg.isMoreQualifiedThan(Param))
1822 QualifierResult = ArgMoreQualified;
1823 QualifierComparisons->push_back(QualifierResult);
1824 }
1825
1826 // C++0x [temp.deduct.partial]p7:
1827 // Remove any top-level cv-qualifiers:
1828 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
1829 // version of P.
1830 Param = Param.getUnqualifiedType();
1831 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
1832 // version of A.
1833 Arg = Arg.getUnqualifiedType();
1834
1835 // C++0x [temp.deduct.partial]p8:
1836 // Using the resulting types P and A the deduction is then done as
1837 // described in 14.9.2.5. If deduction succeeds for a given type, the type
1838 // from the argument template is considered to be at least as specialized
1839 // as the type from the parameter template.
1840 return DeduceTemplateArguments(Context, TemplateParams, Param, Arg, Info,
1841 Deduced, TDF_None);
1842}
1843
1844static void
Douglas Gregore73bb602009-09-14 21:25:05 +00001845MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
1846 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001847 unsigned Level,
Douglas Gregore73bb602009-09-14 21:25:05 +00001848 llvm::SmallVectorImpl<bool> &Deduced);
Douglas Gregor8a514912009-09-14 18:39:43 +00001849
1850/// \brief Determine whether the function template \p FT1 is at least as
1851/// specialized as \p FT2.
1852static bool isAtLeastAsSpecializedAs(Sema &S,
1853 FunctionTemplateDecl *FT1,
1854 FunctionTemplateDecl *FT2,
1855 TemplatePartialOrderingContext TPOC,
1856 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1857 FunctionDecl *FD1 = FT1->getTemplatedDecl();
1858 FunctionDecl *FD2 = FT2->getTemplatedDecl();
1859 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
1860 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
1861
1862 assert(Proto1 && Proto2 && "Function templates must have prototypes");
1863 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
1864 llvm::SmallVector<TemplateArgument, 4> Deduced;
1865 Deduced.resize(TemplateParams->size());
1866
1867 // C++0x [temp.deduct.partial]p3:
1868 // The types used to determine the ordering depend on the context in which
1869 // the partial ordering is done:
1870 Sema::TemplateDeductionInfo Info(S.Context);
1871 switch (TPOC) {
1872 case TPOC_Call: {
1873 // - In the context of a function call, the function parameter types are
1874 // used.
1875 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
1876 for (unsigned I = 0; I != NumParams; ++I)
1877 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1878 TemplateParams,
1879 Proto2->getArgType(I),
1880 Proto1->getArgType(I),
1881 Info,
1882 Deduced,
1883 QualifierComparisons))
1884 return false;
1885
1886 break;
1887 }
1888
1889 case TPOC_Conversion:
1890 // - In the context of a call to a conversion operator, the return types
1891 // of the conversion function templates are used.
1892 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1893 TemplateParams,
1894 Proto2->getResultType(),
1895 Proto1->getResultType(),
1896 Info,
1897 Deduced,
1898 QualifierComparisons))
1899 return false;
1900 break;
1901
1902 case TPOC_Other:
1903 // - In other contexts (14.6.6.2) the function template’s function type
1904 // is used.
1905 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1906 TemplateParams,
1907 FD2->getType(),
1908 FD1->getType(),
1909 Info,
1910 Deduced,
1911 QualifierComparisons))
1912 return false;
1913 break;
1914 }
1915
1916 // C++0x [temp.deduct.partial]p11:
1917 // In most cases, all template parameters must have values in order for
1918 // deduction to succeed, but for partial ordering purposes a template
1919 // parameter may remain without a value provided it is not used in the
1920 // types being used for partial ordering. [ Note: a template parameter used
1921 // in a non-deduced context is considered used. -end note]
1922 unsigned ArgIdx = 0, NumArgs = Deduced.size();
1923 for (; ArgIdx != NumArgs; ++ArgIdx)
1924 if (Deduced[ArgIdx].isNull())
1925 break;
1926
1927 if (ArgIdx == NumArgs) {
1928 // All template arguments were deduced. FT1 is at least as specialized
1929 // as FT2.
1930 return true;
1931 }
1932
Douglas Gregore73bb602009-09-14 21:25:05 +00001933 // Figure out which template parameters were used.
Douglas Gregor8a514912009-09-14 18:39:43 +00001934 llvm::SmallVector<bool, 4> UsedParameters;
1935 UsedParameters.resize(TemplateParams->size());
1936 switch (TPOC) {
1937 case TPOC_Call: {
1938 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
1939 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00001940 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
1941 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00001942 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00001943 break;
1944 }
1945
1946 case TPOC_Conversion:
Douglas Gregored9c0f92009-10-29 00:04:11 +00001947 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
1948 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00001949 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00001950 break;
1951
1952 case TPOC_Other:
Douglas Gregored9c0f92009-10-29 00:04:11 +00001953 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
1954 TemplateParams->getDepth(),
1955 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00001956 break;
1957 }
1958
1959 for (; ArgIdx != NumArgs; ++ArgIdx)
1960 // If this argument had no value deduced but was used in one of the types
1961 // used for partial ordering, then deduction fails.
1962 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
1963 return false;
1964
1965 return true;
1966}
1967
1968
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001969/// \brief Returns the more specialized function template according
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001970/// to the rules of function template partial ordering (C++ [temp.func.order]).
1971///
1972/// \param FT1 the first function template
1973///
1974/// \param FT2 the second function template
1975///
Douglas Gregor8a514912009-09-14 18:39:43 +00001976/// \param TPOC the context in which we are performing partial ordering of
1977/// function templates.
Mike Stump1eb44332009-09-09 15:08:12 +00001978///
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001979/// \returns the more specialized function template. If neither
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001980/// template is more specialized, returns NULL.
1981FunctionTemplateDecl *
1982Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
1983 FunctionTemplateDecl *FT2,
Douglas Gregor8a514912009-09-14 18:39:43 +00001984 TemplatePartialOrderingContext TPOC) {
Douglas Gregor8a514912009-09-14 18:39:43 +00001985 llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
1986 bool Better1 = isAtLeastAsSpecializedAs(*this, FT1, FT2, TPOC, 0);
1987 bool Better2 = isAtLeastAsSpecializedAs(*this, FT2, FT1, TPOC,
1988 &QualifierComparisons);
1989
1990 if (Better1 != Better2) // We have a clear winner
1991 return Better1? FT1 : FT2;
1992
1993 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001994 return 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00001995
1996
1997 // C++0x [temp.deduct.partial]p10:
1998 // If for each type being considered a given template is at least as
1999 // specialized for all types and more specialized for some set of types and
2000 // the other template is not more specialized for any types or is not at
2001 // least as specialized for any types, then the given template is more
2002 // specialized than the other template. Otherwise, neither template is more
2003 // specialized than the other.
2004 Better1 = false;
2005 Better2 = false;
2006 for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
2007 // C++0x [temp.deduct.partial]p9:
2008 // If, for a given type, deduction succeeds in both directions (i.e., the
2009 // types are identical after the transformations above) and if the type
2010 // from the argument template is more cv-qualified than the type from the
2011 // parameter template (as described above) that type is considered to be
2012 // more specialized than the other. If neither type is more cv-qualified
2013 // than the other then neither type is more specialized than the other.
2014 switch (QualifierComparisons[I]) {
2015 case NeitherMoreQualified:
2016 break;
2017
2018 case ParamMoreQualified:
2019 Better1 = true;
2020 if (Better2)
2021 return 0;
2022 break;
2023
2024 case ArgMoreQualified:
2025 Better2 = true;
2026 if (Better1)
2027 return 0;
2028 break;
2029 }
2030 }
2031
2032 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002033 if (Better1)
2034 return FT1;
Douglas Gregor8a514912009-09-14 18:39:43 +00002035 else if (Better2)
2036 return FT2;
2037 else
2038 return 0;
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002039}
Douglas Gregor83314aa2009-07-08 20:55:45 +00002040
Douglas Gregord5a423b2009-09-25 18:43:00 +00002041/// \brief Determine if the two templates are equivalent.
2042static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
2043 if (T1 == T2)
2044 return true;
2045
2046 if (!T1 || !T2)
2047 return false;
2048
2049 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
2050}
2051
2052/// \brief Retrieve the most specialized of the given function template
2053/// specializations.
2054///
John McCallc373d482010-01-27 01:50:18 +00002055/// \param SpecBegin the start iterator of the function template
2056/// specializations that we will be comparing.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002057///
John McCallc373d482010-01-27 01:50:18 +00002058/// \param SpecEnd the end iterator of the function template
2059/// specializations, paired with \p SpecBegin.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002060///
2061/// \param TPOC the partial ordering context to use to compare the function
2062/// template specializations.
2063///
2064/// \param Loc the location where the ambiguity or no-specializations
2065/// diagnostic should occur.
2066///
2067/// \param NoneDiag partial diagnostic used to diagnose cases where there are
2068/// no matching candidates.
2069///
2070/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
2071/// occurs.
2072///
2073/// \param CandidateDiag partial diagnostic used for each function template
2074/// specialization that is a candidate in the ambiguous ordering. One parameter
2075/// in this diagnostic should be unbound, which will correspond to the string
2076/// describing the template arguments for the function template specialization.
2077///
2078/// \param Index if non-NULL and the result of this function is non-nULL,
2079/// receives the index corresponding to the resulting function template
2080/// specialization.
2081///
2082/// \returns the most specialized function template specialization, if
John McCallc373d482010-01-27 01:50:18 +00002083/// found. Otherwise, returns SpecEnd.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002084///
2085/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
2086/// template argument deduction.
John McCallc373d482010-01-27 01:50:18 +00002087UnresolvedSetIterator
2088Sema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
2089 UnresolvedSetIterator SpecEnd,
2090 TemplatePartialOrderingContext TPOC,
2091 SourceLocation Loc,
2092 const PartialDiagnostic &NoneDiag,
2093 const PartialDiagnostic &AmbigDiag,
2094 const PartialDiagnostic &CandidateDiag) {
2095 if (SpecBegin == SpecEnd) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00002096 Diag(Loc, NoneDiag);
John McCallc373d482010-01-27 01:50:18 +00002097 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002098 }
2099
John McCallc373d482010-01-27 01:50:18 +00002100 if (SpecBegin + 1 == SpecEnd)
2101 return SpecBegin;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002102
2103 // Find the function template that is better than all of the templates it
2104 // has been compared to.
John McCallc373d482010-01-27 01:50:18 +00002105 UnresolvedSetIterator Best = SpecBegin;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002106 FunctionTemplateDecl *BestTemplate
John McCallc373d482010-01-27 01:50:18 +00002107 = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002108 assert(BestTemplate && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00002109 for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
2110 FunctionTemplateDecl *Challenger
2111 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002112 assert(Challenger && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00002113 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
Douglas Gregord5a423b2009-09-25 18:43:00 +00002114 TPOC),
2115 Challenger)) {
2116 Best = I;
2117 BestTemplate = Challenger;
2118 }
2119 }
2120
2121 // Make sure that the "best" function template is more specialized than all
2122 // of the others.
2123 bool Ambiguous = false;
John McCallc373d482010-01-27 01:50:18 +00002124 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
2125 FunctionTemplateDecl *Challenger
2126 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002127 if (I != Best &&
2128 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
2129 TPOC),
2130 BestTemplate)) {
2131 Ambiguous = true;
2132 break;
2133 }
2134 }
2135
2136 if (!Ambiguous) {
2137 // We found an answer. Return it.
John McCallc373d482010-01-27 01:50:18 +00002138 return Best;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002139 }
2140
2141 // Diagnose the ambiguity.
2142 Diag(Loc, AmbigDiag);
2143
2144 // FIXME: Can we order the candidates in some sane way?
John McCallc373d482010-01-27 01:50:18 +00002145 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
2146 Diag((*I)->getLocation(), CandidateDiag)
Douglas Gregord5a423b2009-09-25 18:43:00 +00002147 << getTemplateArgumentBindingsText(
John McCallc373d482010-01-27 01:50:18 +00002148 cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
2149 *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
Douglas Gregord5a423b2009-09-25 18:43:00 +00002150
John McCallc373d482010-01-27 01:50:18 +00002151 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002152}
2153
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002154/// \brief Returns the more specialized class template partial specialization
2155/// according to the rules of partial ordering of class template partial
2156/// specializations (C++ [temp.class.order]).
2157///
2158/// \param PS1 the first class template partial specialization
2159///
2160/// \param PS2 the second class template partial specialization
2161///
2162/// \returns the more specialized class template partial specialization. If
2163/// neither partial specialization is more specialized, returns NULL.
2164ClassTemplatePartialSpecializationDecl *
2165Sema::getMoreSpecializedPartialSpecialization(
2166 ClassTemplatePartialSpecializationDecl *PS1,
2167 ClassTemplatePartialSpecializationDecl *PS2) {
2168 // C++ [temp.class.order]p1:
2169 // For two class template partial specializations, the first is at least as
2170 // specialized as the second if, given the following rewrite to two
2171 // function templates, the first function template is at least as
2172 // specialized as the second according to the ordering rules for function
2173 // templates (14.6.6.2):
2174 // - the first function template has the same template parameters as the
2175 // first partial specialization and has a single function parameter
2176 // whose type is a class template specialization with the template
2177 // arguments of the first partial specialization, and
2178 // - the second function template has the same template parameters as the
2179 // second partial specialization and has a single function parameter
2180 // whose type is a class template specialization with the template
2181 // arguments of the second partial specialization.
2182 //
2183 // Rather than synthesize function templates, we merely perform the
2184 // equivalent partial ordering by performing deduction directly on the
2185 // template arguments of the class template partial specializations. This
2186 // computation is slightly simpler than the general problem of function
2187 // template partial ordering, because class template partial specializations
2188 // are more constrained. We know that every template parameter is deduc
2189 llvm::SmallVector<TemplateArgument, 4> Deduced;
2190 Sema::TemplateDeductionInfo Info(Context);
2191
2192 // Determine whether PS1 is at least as specialized as PS2
2193 Deduced.resize(PS2->getTemplateParameters()->size());
2194 bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(Context,
2195 PS2->getTemplateParameters(),
2196 Context.getTypeDeclType(PS2),
2197 Context.getTypeDeclType(PS1),
2198 Info,
2199 Deduced,
2200 0);
2201
2202 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregordb0d4b72009-11-11 23:06:43 +00002203 Deduced.clear();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002204 Deduced.resize(PS1->getTemplateParameters()->size());
2205 bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(Context,
2206 PS1->getTemplateParameters(),
2207 Context.getTypeDeclType(PS1),
2208 Context.getTypeDeclType(PS2),
2209 Info,
2210 Deduced,
2211 0);
2212
2213 if (Better1 == Better2)
2214 return 0;
2215
2216 return Better1? PS1 : PS2;
2217}
2218
Mike Stump1eb44332009-09-09 15:08:12 +00002219static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002220MarkUsedTemplateParameters(Sema &SemaRef,
2221 const TemplateArgument &TemplateArg,
2222 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002223 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002224 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002225
Douglas Gregore73bb602009-09-14 21:25:05 +00002226/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002227/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00002228static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002229MarkUsedTemplateParameters(Sema &SemaRef,
2230 const Expr *E,
2231 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002232 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002233 llvm::SmallVectorImpl<bool> &Used) {
2234 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2235 // find other occurrences of template parameters.
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002236 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregorc781f9c2010-01-14 18:13:22 +00002237 if (!DRE)
Douglas Gregor031a5882009-06-13 00:26:55 +00002238 return;
2239
Mike Stump1eb44332009-09-09 15:08:12 +00002240 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00002241 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2242 if (!NTTP)
2243 return;
2244
Douglas Gregored9c0f92009-10-29 00:04:11 +00002245 if (NTTP->getDepth() == Depth)
2246 Used[NTTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002247}
2248
Douglas Gregore73bb602009-09-14 21:25:05 +00002249/// \brief Mark the template parameters that are used by the given
2250/// nested name specifier.
2251static void
2252MarkUsedTemplateParameters(Sema &SemaRef,
2253 NestedNameSpecifier *NNS,
2254 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002255 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002256 llvm::SmallVectorImpl<bool> &Used) {
2257 if (!NNS)
2258 return;
2259
Douglas Gregored9c0f92009-10-29 00:04:11 +00002260 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2261 Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002262 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002263 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002264}
2265
2266/// \brief Mark the template parameters that are used by the given
2267/// template name.
2268static void
2269MarkUsedTemplateParameters(Sema &SemaRef,
2270 TemplateName Name,
2271 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002272 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002273 llvm::SmallVectorImpl<bool> &Used) {
2274 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2275 if (TemplateTemplateParmDecl *TTP
Douglas Gregored9c0f92009-10-29 00:04:11 +00002276 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2277 if (TTP->getDepth() == Depth)
2278 Used[TTP->getIndex()] = true;
2279 }
Douglas Gregore73bb602009-09-14 21:25:05 +00002280 return;
2281 }
2282
Douglas Gregor788cd062009-11-11 01:00:40 +00002283 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2284 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2285 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002286 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
Douglas Gregored9c0f92009-10-29 00:04:11 +00002287 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2288 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002289}
2290
2291/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002292/// type.
Mike Stump1eb44332009-09-09 15:08:12 +00002293static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002294MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2295 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002296 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002297 llvm::SmallVectorImpl<bool> &Used) {
2298 if (T.isNull())
2299 return;
2300
Douglas Gregor031a5882009-06-13 00:26:55 +00002301 // Non-dependent types have nothing deducible
2302 if (!T->isDependentType())
2303 return;
2304
2305 T = SemaRef.Context.getCanonicalType(T);
2306 switch (T->getTypeClass()) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002307 case Type::Pointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002308 MarkUsedTemplateParameters(SemaRef,
2309 cast<PointerType>(T)->getPointeeType(),
2310 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002311 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002312 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002313 break;
2314
2315 case Type::BlockPointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002316 MarkUsedTemplateParameters(SemaRef,
2317 cast<BlockPointerType>(T)->getPointeeType(),
2318 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002319 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002320 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002321 break;
2322
2323 case Type::LValueReference:
2324 case Type::RValueReference:
Douglas Gregore73bb602009-09-14 21:25:05 +00002325 MarkUsedTemplateParameters(SemaRef,
2326 cast<ReferenceType>(T)->getPointeeType(),
2327 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002328 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002329 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002330 break;
2331
2332 case Type::MemberPointer: {
2333 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore73bb602009-09-14 21:25:05 +00002334 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002335 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002336 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002337 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002338 break;
2339 }
2340
2341 case Type::DependentSizedArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002342 MarkUsedTemplateParameters(SemaRef,
2343 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002344 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002345 // Fall through to check the element type
2346
2347 case Type::ConstantArray:
2348 case Type::IncompleteArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002349 MarkUsedTemplateParameters(SemaRef,
2350 cast<ArrayType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002351 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002352 break;
2353
2354 case Type::Vector:
2355 case Type::ExtVector:
Douglas Gregore73bb602009-09-14 21:25:05 +00002356 MarkUsedTemplateParameters(SemaRef,
2357 cast<VectorType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002358 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002359 break;
2360
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002361 case Type::DependentSizedExtVector: {
2362 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002363 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002364 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002365 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002366 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002367 Depth, Used);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002368 break;
2369 }
2370
Douglas Gregor031a5882009-06-13 00:26:55 +00002371 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002372 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002373 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002374 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002375 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore73bb602009-09-14 21:25:05 +00002376 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002377 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002378 break;
2379 }
2380
Douglas Gregored9c0f92009-10-29 00:04:11 +00002381 case Type::TemplateTypeParm: {
2382 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2383 if (TTP->getDepth() == Depth)
2384 Used[TTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002385 break;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002386 }
Douglas Gregor031a5882009-06-13 00:26:55 +00002387
2388 case Type::TemplateSpecialization: {
Mike Stump1eb44332009-09-09 15:08:12 +00002389 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002390 = cast<TemplateSpecializationType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002391 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002392 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002393 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002394 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2395 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002396 break;
2397 }
2398
Douglas Gregore73bb602009-09-14 21:25:05 +00002399 case Type::Complex:
2400 if (!OnlyDeduced)
2401 MarkUsedTemplateParameters(SemaRef,
2402 cast<ComplexType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002403 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002404 break;
2405
2406 case Type::Typename:
2407 if (!OnlyDeduced)
2408 MarkUsedTemplateParameters(SemaRef,
2409 cast<TypenameType>(T)->getQualifier(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002410 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002411 break;
2412
2413 // None of these types have any template parameters in them.
Douglas Gregor031a5882009-06-13 00:26:55 +00002414 case Type::Builtin:
Douglas Gregor031a5882009-06-13 00:26:55 +00002415 case Type::VariableArray:
2416 case Type::FunctionNoProto:
2417 case Type::Record:
2418 case Type::Enum:
Douglas Gregor031a5882009-06-13 00:26:55 +00002419 case Type::ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002420 case Type::ObjCObjectPointer:
John McCalled976492009-12-04 22:46:56 +00002421 case Type::UnresolvedUsing:
Douglas Gregor031a5882009-06-13 00:26:55 +00002422#define TYPE(Class, Base)
2423#define ABSTRACT_TYPE(Class, Base)
2424#define DEPENDENT_TYPE(Class, Base)
2425#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2426#include "clang/AST/TypeNodes.def"
2427 break;
2428 }
2429}
2430
Douglas Gregore73bb602009-09-14 21:25:05 +00002431/// \brief Mark the template parameters that are used by this
Douglas Gregor031a5882009-06-13 00:26:55 +00002432/// template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00002433static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002434MarkUsedTemplateParameters(Sema &SemaRef,
2435 const TemplateArgument &TemplateArg,
2436 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002437 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002438 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002439 switch (TemplateArg.getKind()) {
2440 case TemplateArgument::Null:
2441 case TemplateArgument::Integral:
Douglas Gregor788cd062009-11-11 01:00:40 +00002442 case TemplateArgument::Declaration:
Douglas Gregor031a5882009-06-13 00:26:55 +00002443 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002444
Douglas Gregor031a5882009-06-13 00:26:55 +00002445 case TemplateArgument::Type:
Douglas Gregore73bb602009-09-14 21:25:05 +00002446 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002447 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002448 break;
2449
Douglas Gregor788cd062009-11-11 01:00:40 +00002450 case TemplateArgument::Template:
2451 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2452 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002453 break;
2454
2455 case TemplateArgument::Expression:
Douglas Gregore73bb602009-09-14 21:25:05 +00002456 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002457 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002458 break;
Douglas Gregore73bb602009-09-14 21:25:05 +00002459
Anders Carlssond01b1da2009-06-15 17:04:53 +00002460 case TemplateArgument::Pack:
Douglas Gregore73bb602009-09-14 21:25:05 +00002461 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2462 PEnd = TemplateArg.pack_end();
2463 P != PEnd; ++P)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002464 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssond01b1da2009-06-15 17:04:53 +00002465 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00002466 }
2467}
2468
2469/// \brief Mark the template parameters can be deduced by the given
2470/// template argument list.
2471///
2472/// \param TemplateArgs the template argument list from which template
2473/// parameters will be deduced.
2474///
2475/// \param Deduced a bit vector whose elements will be set to \c true
2476/// to indicate when the corresponding template parameter will be
2477/// deduced.
Mike Stump1eb44332009-09-09 15:08:12 +00002478void
Douglas Gregore73bb602009-09-14 21:25:05 +00002479Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002480 bool OnlyDeduced, unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002481 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002482 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002483 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2484 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002485}
Douglas Gregor63f07c52009-09-18 23:21:38 +00002486
2487/// \brief Marks all of the template parameters that will be deduced by a
2488/// call to the given function template.
2489void Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
2490 llvm::SmallVectorImpl<bool> &Deduced) {
2491 TemplateParameterList *TemplateParams
2492 = FunctionTemplate->getTemplateParameters();
2493 Deduced.clear();
2494 Deduced.resize(TemplateParams->size());
2495
2496 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2497 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
2498 ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002499 true, TemplateParams->getDepth(), Deduced);
Douglas Gregor63f07c52009-09-18 23:21:38 +00002500}