blob: e31c05cf2af30a78e467970873782e91a06a1423 [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) {
John McCall0953e762009-09-24 19:53:00 +0000378 Qualifiers Quals = Param.getQualifiers();
379 Quals.setCVRQualifiers(Quals.getCVRQualifiers() & Arg.getCVRQualifiers());
380 Param = Context.getQualifiedType(Param.getUnqualifiedType(), Quals);
Douglas Gregor500d3312009-06-26 18:27:22 +0000381 }
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000383 // If the parameter type is not dependent, there is nothing to deduce.
Douglas Gregor12820292009-09-14 20:00:47 +0000384 if (!Param->isDependentType()) {
385 if (!(TDF & TDF_SkipNonDependent) && Param != Arg) {
386
387 return Sema::TDK_NonDeducedMismatch;
388 }
389
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000390 return Sema::TDK_Success;
Douglas Gregor12820292009-09-14 20:00:47 +0000391 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000392
Douglas Gregor199d9912009-06-05 00:53:49 +0000393 // C++ [temp.deduct.type]p9:
Mike Stump1eb44332009-09-09 15:08:12 +0000394 // A template type argument T, a template template argument TT or a
395 // template non-type argument i can be deduced if P and A have one of
Douglas Gregor199d9912009-06-05 00:53:49 +0000396 // the following forms:
397 //
398 // T
399 // cv-list T
Mike Stump1eb44332009-09-09 15:08:12 +0000400 if (const TemplateTypeParmType *TemplateTypeParm
John McCall183700f2009-09-21 23:43:11 +0000401 = Param->getAs<TemplateTypeParmType>()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000402 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000403 bool RecanonicalizeArg = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Douglas Gregor9e9fae42009-07-22 20:02:25 +0000405 // If the argument type is an array type, move the qualifiers up to the
406 // top level, so they can be matched with the qualifiers on the parameter.
407 // FIXME: address spaces, ObjC GC qualifiers
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000408 if (isa<ArrayType>(Arg)) {
John McCall0953e762009-09-24 19:53:00 +0000409 Qualifiers Quals;
Chandler Carruth28e318c2009-12-29 07:16:59 +0000410 Arg = Context.getUnqualifiedArrayType(Arg, Quals);
John McCall0953e762009-09-24 19:53:00 +0000411 if (Quals) {
412 Arg = Context.getQualifiedType(Arg, Quals);
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000413 RecanonicalizeArg = true;
414 }
415 }
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000417 // The argument type can not be less qualified than the parameter
418 // type.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000419 if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000420 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
421 Info.FirstArg = Deduced[Index];
John McCall833ca992009-10-29 08:12:44 +0000422 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000423 return Sema::TDK_InconsistentQuals;
424 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000425
426 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
Douglas Gregorc78a69d2009-12-21 21:27:38 +0000427 assert(Arg != Context.OverloadTy && "Unresolved overloaded function");
John McCall0953e762009-09-24 19:53:00 +0000428 QualType DeducedType = Arg;
429 DeducedType.removeCVRQualifiers(Param.getCVRQualifiers());
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000430 if (RecanonicalizeArg)
431 DeducedType = Context.getCanonicalType(DeducedType);
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000433 if (Deduced[Index].isNull())
John McCall833ca992009-10-29 08:12:44 +0000434 Deduced[Index] = TemplateArgument(DeducedType);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000435 else {
Mike Stump1eb44332009-09-09 15:08:12 +0000436 // C++ [temp.deduct.type]p2:
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000437 // [...] If type deduction cannot be done for any P/A pair, or if for
Mike Stump1eb44332009-09-09 15:08:12 +0000438 // any pair the deduction leads to more than one possible set of
439 // deduced values, or if different pairs yield different deduced
440 // values, or if any template argument remains neither deduced nor
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000441 // explicitly specified, template argument deduction fails.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000442 if (Deduced[Index].getAsType() != DeducedType) {
Mike Stump1eb44332009-09-09 15:08:12 +0000443 Info.Param
Douglas Gregorf67875d2009-06-12 18:26:56 +0000444 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
445 Info.FirstArg = Deduced[Index];
John McCall833ca992009-10-29 08:12:44 +0000446 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000447 return Sema::TDK_Inconsistent;
448 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000449 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000450 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000451 }
452
Douglas Gregorf67875d2009-06-12 18:26:56 +0000453 // Set up the template argument deduction information for a failure.
John McCall833ca992009-10-29 08:12:44 +0000454 Info.FirstArg = TemplateArgument(ParamIn);
455 Info.SecondArg = TemplateArgument(ArgIn);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000456
Douglas Gregor508f1c82009-06-26 23:10:12 +0000457 // Check the cv-qualifiers on the parameter and argument types.
458 if (!(TDF & TDF_IgnoreQualifiers)) {
459 if (TDF & TDF_ParamWithReferenceType) {
460 if (Param.isMoreQualifiedThan(Arg))
461 return Sema::TDK_NonDeducedMismatch;
462 } else {
463 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Mike Stump1eb44332009-09-09 15:08:12 +0000464 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor508f1c82009-06-26 23:10:12 +0000465 }
466 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000467
Douglas Gregord560d502009-06-04 00:21:18 +0000468 switch (Param->getTypeClass()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000469 // No deduction possible for these types
470 case Type::Builtin:
Douglas Gregorf67875d2009-06-12 18:26:56 +0000471 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Douglas Gregor199d9912009-06-05 00:53:49 +0000473 // T *
Douglas Gregord560d502009-06-04 00:21:18 +0000474 case Type::Pointer: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000475 const PointerType *PointerArg = Arg->getAs<PointerType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000476 if (!PointerArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000477 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Douglas Gregor41128772009-06-26 23:27:24 +0000479 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000480 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000481 cast<PointerType>(Param)->getPointeeType(),
482 PointerArg->getPointeeType(),
Douglas Gregor41128772009-06-26 23:27:24 +0000483 Info, Deduced, SubTDF);
Douglas Gregord560d502009-06-04 00:21:18 +0000484 }
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Douglas Gregor199d9912009-06-05 00:53:49 +0000486 // T &
Douglas Gregord560d502009-06-04 00:21:18 +0000487 case Type::LValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000488 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000489 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000490 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Douglas Gregorf67875d2009-06-12 18:26:56 +0000492 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000493 cast<LValueReferenceType>(Param)->getPointeeType(),
494 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000495 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000496 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000497
Douglas Gregor199d9912009-06-05 00:53:49 +0000498 // T && [C++0x]
Douglas Gregord560d502009-06-04 00:21:18 +0000499 case Type::RValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000500 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000501 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000502 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Douglas Gregorf67875d2009-06-12 18:26:56 +0000504 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000505 cast<RValueReferenceType>(Param)->getPointeeType(),
506 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000507 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000508 }
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Douglas Gregor199d9912009-06-05 00:53:49 +0000510 // T [] (implied, but not stated explicitly)
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000511 case Type::IncompleteArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000512 const IncompleteArrayType *IncompleteArrayArg =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000513 Context.getAsIncompleteArrayType(Arg);
514 if (!IncompleteArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000515 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Douglas Gregorf67875d2009-06-12 18:26:56 +0000517 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000518 Context.getAsIncompleteArrayType(Param)->getElementType(),
519 IncompleteArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000520 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000521 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000522
523 // T [integer-constant]
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000524 case Type::ConstantArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000525 const ConstantArrayType *ConstantArrayArg =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000526 Context.getAsConstantArrayType(Arg);
527 if (!ConstantArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000528 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000529
530 const ConstantArrayType *ConstantArrayParm =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000531 Context.getAsConstantArrayType(Param);
532 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000533 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Douglas Gregorf67875d2009-06-12 18:26:56 +0000535 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000536 ConstantArrayParm->getElementType(),
537 ConstantArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000538 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000539 }
540
Douglas Gregor199d9912009-06-05 00:53:49 +0000541 // type [i]
542 case Type::DependentSizedArray: {
543 const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg);
544 if (!ArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000545 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Douglas Gregor199d9912009-06-05 00:53:49 +0000547 // Check the element type of the arrays
548 const DependentSizedArrayType *DependentArrayParm
549 = cast<DependentSizedArrayType>(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000550 if (Sema::TemplateDeductionResult Result
551 = DeduceTemplateArguments(Context, TemplateParams,
552 DependentArrayParm->getElementType(),
553 ArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000554 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000555 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Douglas Gregor199d9912009-06-05 00:53:49 +0000557 // Determine the array bound is something we can deduce.
Mike Stump1eb44332009-09-09 15:08:12 +0000558 NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000559 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
560 if (!NTTP)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000561 return Sema::TDK_Success;
Mike Stump1eb44332009-09-09 15:08:12 +0000562
563 // We can perform template argument deduction for the given non-type
Douglas Gregor199d9912009-06-05 00:53:49 +0000564 // template parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000565 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000566 "Cannot deduce non-type template argument at depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000567 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson335e24a2009-06-16 22:44:31 +0000568 = dyn_cast<ConstantArrayType>(ArrayArg)) {
569 llvm::APSInt Size(ConstantArrayArg->getSize());
570 return DeduceNonTypeTemplateArgument(Context, NTTP, Size,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000571 Info, Deduced);
Anders Carlsson335e24a2009-06-16 22:44:31 +0000572 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000573 if (const DependentSizedArrayType *DependentArrayArg
574 = dyn_cast<DependentSizedArrayType>(ArrayArg))
575 return DeduceNonTypeTemplateArgument(Context, NTTP,
576 DependentArrayArg->getSizeExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000577 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Douglas Gregor199d9912009-06-05 00:53:49 +0000579 // Incomplete type does not match a dependently-sized array type
Douglas Gregorf67875d2009-06-12 18:26:56 +0000580 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000581 }
Mike Stump1eb44332009-09-09 15:08:12 +0000582
583 // type(*)(T)
584 // T(*)()
585 // T(*)(T)
Anders Carlssona27fad52009-06-08 15:19:08 +0000586 case Type::FunctionProto: {
Mike Stump1eb44332009-09-09 15:08:12 +0000587 const FunctionProtoType *FunctionProtoArg =
Anders Carlssona27fad52009-06-08 15:19:08 +0000588 dyn_cast<FunctionProtoType>(Arg);
589 if (!FunctionProtoArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000590 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000591
592 const FunctionProtoType *FunctionProtoParam =
Anders Carlssona27fad52009-06-08 15:19:08 +0000593 cast<FunctionProtoType>(Param);
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000594
Mike Stump1eb44332009-09-09 15:08:12 +0000595 if (FunctionProtoParam->getTypeQuals() !=
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000596 FunctionProtoArg->getTypeQuals())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000597 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000599 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000600 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000602 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000603 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000604
Anders Carlssona27fad52009-06-08 15:19:08 +0000605 // Check return types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000606 if (Sema::TemplateDeductionResult Result
607 = DeduceTemplateArguments(Context, TemplateParams,
608 FunctionProtoParam->getResultType(),
609 FunctionProtoArg->getResultType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000610 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000611 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Anders Carlssona27fad52009-06-08 15:19:08 +0000613 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
614 // Check argument types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000615 if (Sema::TemplateDeductionResult Result
616 = DeduceTemplateArguments(Context, TemplateParams,
617 FunctionProtoParam->getArgType(I),
618 FunctionProtoArg->getArgType(I),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000619 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000620 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000621 }
Mike Stump1eb44332009-09-09 15:08:12 +0000622
Douglas Gregorf67875d2009-06-12 18:26:56 +0000623 return Sema::TDK_Success;
Anders Carlssona27fad52009-06-08 15:19:08 +0000624 }
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000626 // template-name<T> (where template-name refers to a class template)
Douglas Gregord708c722009-06-09 16:35:58 +0000627 // template-name<i>
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000628 // TT<T>
629 // TT<i>
630 // TT<>
Douglas Gregord708c722009-06-09 16:35:58 +0000631 case Type::TemplateSpecialization: {
632 const TemplateSpecializationType *SpecParam
633 = cast<TemplateSpecializationType>(Param);
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000635 // Try to deduce template arguments from the template-id.
636 Sema::TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000637 = DeduceTemplateArguments(Context, TemplateParams, SpecParam, Arg,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000638 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Douglas Gregor4a5c15f2009-09-30 22:13:51 +0000640 if (Result && (TDF & TDF_DerivedClass)) {
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000641 // C++ [temp.deduct.call]p3b3:
642 // If P is a class, and P has the form template-id, then A can be a
643 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump1eb44332009-09-09 15:08:12 +0000644 // class of the form template-id, A can be a pointer to a derived
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000645 // class pointed to by the deduced A.
646 //
647 // More importantly:
Mike Stump1eb44332009-09-09 15:08:12 +0000648 // These alternatives are considered only if type deduction would
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000649 // otherwise fail.
650 if (const RecordType *RecordT = dyn_cast<RecordType>(Arg)) {
651 // Use data recursion to crawl through the list of base classes.
Mike Stump1eb44332009-09-09 15:08:12 +0000652 // Visited contains the set of nodes we have already visited, while
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000653 // ToVisit is our stack of records that we still need to visit.
654 llvm::SmallPtrSet<const RecordType *, 8> Visited;
655 llvm::SmallVector<const RecordType *, 8> ToVisit;
656 ToVisit.push_back(RecordT);
657 bool Successful = false;
658 while (!ToVisit.empty()) {
659 // Retrieve the next class in the inheritance hierarchy.
660 const RecordType *NextT = ToVisit.back();
661 ToVisit.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000663 // If we have already seen this type, skip it.
664 if (!Visited.insert(NextT))
665 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000667 // If this is a base class, try to perform template argument
668 // deduction from it.
669 if (NextT != RecordT) {
670 Sema::TemplateDeductionResult BaseResult
671 = DeduceTemplateArguments(Context, TemplateParams, SpecParam,
672 QualType(NextT, 0), Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000674 // If template argument deduction for this base was successful,
675 // note that we had some success.
676 if (BaseResult == Sema::TDK_Success)
677 Successful = true;
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000678 }
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000680 // Visit base classes
681 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
682 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
683 BaseEnd = Next->bases_end();
Sebastian Redl9994a342009-10-25 17:03:50 +0000684 Base != BaseEnd; ++Base) {
Mike Stump1eb44332009-09-09 15:08:12 +0000685 assert(Base->getType()->isRecordType() &&
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000686 "Base class that isn't a record?");
Ted Kremenek6217b802009-07-29 21:53:49 +0000687 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000688 }
689 }
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000691 if (Successful)
692 return Sema::TDK_Success;
693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000695 }
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000697 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000698 }
699
Douglas Gregor637a4092009-06-10 23:47:09 +0000700 // T type::*
701 // T T::*
702 // T (type::*)()
703 // type (T::*)()
704 // type (type::*)(T)
705 // type (T::*)(T)
706 // T (type::*)(T)
707 // T (T::*)()
708 // T (T::*)(T)
709 case Type::MemberPointer: {
710 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
711 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
712 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000713 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +0000714
Douglas Gregorf67875d2009-06-12 18:26:56 +0000715 if (Sema::TemplateDeductionResult Result
716 = DeduceTemplateArguments(Context, TemplateParams,
717 MemPtrParam->getPointeeType(),
718 MemPtrArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000719 Info, Deduced,
720 TDF & TDF_IgnoreQualifiers))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000721 return Result;
722
723 return DeduceTemplateArguments(Context, TemplateParams,
724 QualType(MemPtrParam->getClass(), 0),
725 QualType(MemPtrArg->getClass(), 0),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000726 Info, Deduced, 0);
Douglas Gregor637a4092009-06-10 23:47:09 +0000727 }
728
Anders Carlsson9a917e42009-06-12 22:56:54 +0000729 // (clang extension)
730 //
Mike Stump1eb44332009-09-09 15:08:12 +0000731 // type(^)(T)
732 // T(^)()
733 // T(^)(T)
Anders Carlsson859ba502009-06-12 16:23:10 +0000734 case Type::BlockPointer: {
735 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
736 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Anders Carlsson859ba502009-06-12 16:23:10 +0000738 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000739 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Douglas Gregorf67875d2009-06-12 18:26:56 +0000741 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +0000742 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000743 BlockPtrArg->getPointeeType(), Info,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000744 Deduced, 0);
Anders Carlsson859ba502009-06-12 16:23:10 +0000745 }
746
Douglas Gregor637a4092009-06-10 23:47:09 +0000747 case Type::TypeOfExpr:
748 case Type::TypeOf:
749 case Type::Typename:
750 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +0000751 return Sema::TDK_Success;
Douglas Gregor637a4092009-06-10 23:47:09 +0000752
Douglas Gregord560d502009-06-04 00:21:18 +0000753 default:
754 break;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000755 }
756
757 // FIXME: Many more cases to go (to go).
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000758 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000759}
760
Douglas Gregorf67875d2009-06-12 18:26:56 +0000761static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +0000762DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000763 TemplateParameterList *TemplateParams,
764 const TemplateArgument &Param,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000765 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000766 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000767 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000768 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000769 case TemplateArgument::Null:
770 assert(false && "Null template argument in parameter list");
771 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000772
773 case TemplateArgument::Type:
Douglas Gregor788cd062009-11-11 01:00:40 +0000774 if (Arg.getKind() == TemplateArgument::Type)
775 return DeduceTemplateArguments(Context, TemplateParams, Param.getAsType(),
776 Arg.getAsType(), Info, Deduced, 0);
777 Info.FirstArg = Param;
778 Info.SecondArg = Arg;
779 return Sema::TDK_NonDeducedMismatch;
780
781 case TemplateArgument::Template:
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000782 if (Arg.getKind() == TemplateArgument::Template)
Douglas Gregor788cd062009-11-11 01:00:40 +0000783 return DeduceTemplateArguments(Context, TemplateParams,
784 Param.getAsTemplate(),
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000785 Arg.getAsTemplate(), Info, Deduced);
Douglas Gregor788cd062009-11-11 01:00:40 +0000786 Info.FirstArg = Param;
787 Info.SecondArg = Arg;
788 return Sema::TDK_NonDeducedMismatch;
789
Douglas Gregor199d9912009-06-05 00:53:49 +0000790 case TemplateArgument::Declaration:
Douglas Gregor788cd062009-11-11 01:00:40 +0000791 if (Arg.getKind() == TemplateArgument::Declaration &&
792 Param.getAsDecl()->getCanonicalDecl() ==
793 Arg.getAsDecl()->getCanonicalDecl())
794 return Sema::TDK_Success;
795
Douglas Gregorf67875d2009-06-12 18:26:56 +0000796 Info.FirstArg = Param;
797 Info.SecondArg = Arg;
798 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Douglas Gregor199d9912009-06-05 00:53:49 +0000800 case TemplateArgument::Integral:
801 if (Arg.getKind() == TemplateArgument::Integral) {
802 // FIXME: Zero extension + sign checking here?
Douglas Gregorf67875d2009-06-12 18:26:56 +0000803 if (*Param.getAsIntegral() == *Arg.getAsIntegral())
804 return Sema::TDK_Success;
805
806 Info.FirstArg = Param;
807 Info.SecondArg = Arg;
808 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000809 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000810
811 if (Arg.getKind() == TemplateArgument::Expression) {
812 Info.FirstArg = Param;
813 Info.SecondArg = Arg;
814 return Sema::TDK_NonDeducedMismatch;
815 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000816
817 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000818 Info.FirstArg = Param;
819 Info.SecondArg = Arg;
820 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Douglas Gregor199d9912009-06-05 00:53:49 +0000822 case TemplateArgument::Expression: {
Mike Stump1eb44332009-09-09 15:08:12 +0000823 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000824 = getDeducedParameterFromExpr(Param.getAsExpr())) {
825 if (Arg.getKind() == TemplateArgument::Integral)
826 // FIXME: Sign problems here
Mike Stump1eb44332009-09-09 15:08:12 +0000827 return DeduceNonTypeTemplateArgument(Context, NTTP,
828 *Arg.getAsIntegral(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000829 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000830 if (Arg.getKind() == TemplateArgument::Expression)
831 return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000832 Info, Deduced);
Douglas Gregor15755cb2009-11-13 23:45:44 +0000833 if (Arg.getKind() == TemplateArgument::Declaration)
834 return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsDecl(),
835 Info, Deduced);
836
Douglas Gregor199d9912009-06-05 00:53:49 +0000837 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000838 Info.FirstArg = Param;
839 Info.SecondArg = Arg;
840 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000841 }
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Douglas Gregor199d9912009-06-05 00:53:49 +0000843 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000844 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000845 }
Anders Carlssond01b1da2009-06-15 17:04:53 +0000846 case TemplateArgument::Pack:
847 assert(0 && "FIXME: Implement!");
848 break;
Douglas Gregor199d9912009-06-05 00:53:49 +0000849 }
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Douglas Gregorf67875d2009-06-12 18:26:56 +0000851 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000852}
853
Mike Stump1eb44332009-09-09 15:08:12 +0000854static Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000855DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000856 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000857 const TemplateArgumentList &ParamList,
858 const TemplateArgumentList &ArgList,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000859 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000860 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
861 assert(ParamList.size() == ArgList.size());
862 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000863 if (Sema::TemplateDeductionResult Result
864 = DeduceTemplateArguments(Context, TemplateParams,
Mike Stump1eb44332009-09-09 15:08:12 +0000865 ParamList[I], ArgList[I],
Douglas Gregorf67875d2009-06-12 18:26:56 +0000866 Info, Deduced))
867 return Result;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000868 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000869 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000870}
871
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000872/// \brief Determine whether two template arguments are the same.
Mike Stump1eb44332009-09-09 15:08:12 +0000873static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000874 const TemplateArgument &X,
875 const TemplateArgument &Y) {
876 if (X.getKind() != Y.getKind())
877 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000879 switch (X.getKind()) {
880 case TemplateArgument::Null:
881 assert(false && "Comparing NULL template argument");
882 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000884 case TemplateArgument::Type:
885 return Context.getCanonicalType(X.getAsType()) ==
886 Context.getCanonicalType(Y.getAsType());
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000888 case TemplateArgument::Declaration:
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000889 return X.getAsDecl()->getCanonicalDecl() ==
890 Y.getAsDecl()->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Douglas Gregor788cd062009-11-11 01:00:40 +0000892 case TemplateArgument::Template:
893 return Context.getCanonicalTemplateName(X.getAsTemplate())
894 .getAsVoidPointer() ==
895 Context.getCanonicalTemplateName(Y.getAsTemplate())
896 .getAsVoidPointer();
897
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000898 case TemplateArgument::Integral:
899 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Douglas Gregor788cd062009-11-11 01:00:40 +0000901 case TemplateArgument::Expression: {
902 llvm::FoldingSetNodeID XID, YID;
903 X.getAsExpr()->Profile(XID, Context, true);
904 Y.getAsExpr()->Profile(YID, Context, true);
905 return XID == YID;
906 }
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000908 case TemplateArgument::Pack:
909 if (X.pack_size() != Y.pack_size())
910 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000911
912 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
913 XPEnd = X.pack_end(),
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000914 YP = Y.pack_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000915 XP != XPEnd; ++XP, ++YP)
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000916 if (!isSameTemplateArg(Context, *XP, *YP))
917 return false;
918
919 return true;
920 }
921
922 return false;
923}
924
925/// \brief Helper function to build a TemplateParameter when we don't
926/// know its type statically.
927static TemplateParameter makeTemplateParameter(Decl *D) {
928 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
929 return TemplateParameter(TTP);
930 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
931 return TemplateParameter(NTTP);
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000933 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
934}
935
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000936/// \brief Perform template argument deduction to determine whether
937/// the given template arguments match the given class template
938/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +0000939Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000940Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000941 const TemplateArgumentList &TemplateArgs,
942 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000943 // C++ [temp.class.spec.match]p2:
944 // A partial specialization matches a given actual template
945 // argument list if the template arguments of the partial
946 // specialization can be deduced from the actual template argument
947 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +0000948 SFINAETrap Trap(*this);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000949 llvm::SmallVector<TemplateArgument, 4> Deduced;
950 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000951 if (TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000952 = ::DeduceTemplateArguments(Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000953 Partial->getTemplateParameters(),
Mike Stump1eb44332009-09-09 15:08:12 +0000954 Partial->getTemplateArgs(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000955 TemplateArgs, Info, Deduced))
956 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +0000957
Douglas Gregor637a4092009-06-10 23:47:09 +0000958 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
959 Deduced.data(), Deduced.size());
960 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000961 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +0000962
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000963 // C++ [temp.deduct.type]p2:
964 // [...] or if any template argument remains neither deduced nor
965 // explicitly specified, template argument deduction fails.
Anders Carlssonfb250522009-06-23 01:26:57 +0000966 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
967 Deduced.size());
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000968 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000969 if (Deduced[I].isNull()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000970 Decl *Param
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000971 = const_cast<NamedDecl *>(
972 Partial->getTemplateParameters()->getParam(I));
Douglas Gregorf67875d2009-06-12 18:26:56 +0000973 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
974 Info.Param = TTP;
Mike Stump1eb44332009-09-09 15:08:12 +0000975 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorf67875d2009-06-12 18:26:56 +0000976 = dyn_cast<NonTypeTemplateParmDecl>(Param))
977 Info.Param = NTTP;
978 else
979 Info.Param = cast<TemplateTemplateParmDecl>(Param);
980 return TDK_Incomplete;
981 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000982
Anders Carlssonfb250522009-06-23 01:26:57 +0000983 Builder.Append(Deduced[I]);
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000984 }
985
986 // Form the template argument list from the deduced template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +0000987 TemplateArgumentList *DeducedArgumentList
Anders Carlssonfb250522009-06-23 01:26:57 +0000988 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000989 Info.reset(DeducedArgumentList);
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000990
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000991 // Substitute the deduced template arguments into the template
992 // arguments of the class template partial specialization, and
993 // verify that the instantiated template arguments are both valid
994 // and are equivalent to the template arguments originally provided
Mike Stump1eb44332009-09-09 15:08:12 +0000995 // to the class template.
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000996 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
John McCall833ca992009-10-29 08:12:44 +0000997 const TemplateArgumentLoc *PartialTemplateArgs
998 = Partial->getTemplateArgsAsWritten();
999 unsigned N = Partial->getNumTemplateArgsAsWritten();
John McCalld5532b62009-11-23 01:53:49 +00001000
1001 // Note that we don't provide the langle and rangle locations.
1002 TemplateArgumentListInfo InstArgs;
1003
John McCall833ca992009-10-29 08:12:44 +00001004 for (unsigned I = 0; I != N; ++I) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001005 Decl *Param = const_cast<NamedDecl *>(
Douglas Gregorc9e5d252009-06-13 00:59:32 +00001006 ClassTemplate->getTemplateParameters()->getParam(I));
John McCalld5532b62009-11-23 01:53:49 +00001007 TemplateArgumentLoc InstArg;
1008 if (Subst(PartialTemplateArgs[I], InstArg,
John McCall833ca992009-10-29 08:12:44 +00001009 MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001010 Info.Param = makeTemplateParameter(Param);
John McCall833ca992009-10-29 08:12:44 +00001011 Info.FirstArg = PartialTemplateArgs[I].getArgument();
Mike Stump1eb44332009-09-09 15:08:12 +00001012 return TDK_SubstitutionFailure;
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001013 }
John McCalld5532b62009-11-23 01:53:49 +00001014 InstArgs.addArgument(InstArg);
John McCall833ca992009-10-29 08:12:44 +00001015 }
1016
1017 TemplateArgumentListBuilder ConvertedInstArgs(
1018 ClassTemplate->getTemplateParameters(), N);
1019
1020 if (CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001021 InstArgs, false, ConvertedInstArgs)) {
John McCall833ca992009-10-29 08:12:44 +00001022 // FIXME: fail with more useful information?
1023 return TDK_SubstitutionFailure;
1024 }
1025
1026 for (unsigned I = 0, E = ConvertedInstArgs.flatSize(); I != E; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001027 TemplateArgument InstArg = ConvertedInstArgs.getFlatArguments()[I];
John McCall833ca992009-10-29 08:12:44 +00001028
1029 Decl *Param = const_cast<NamedDecl *>(
1030 ClassTemplate->getTemplateParameters()->getParam(I));
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001032 if (InstArg.getKind() == TemplateArgument::Expression) {
Mike Stump1eb44332009-09-09 15:08:12 +00001033 // When the argument is an expression, check the expression result
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001034 // against the actual template parameter to get down to the canonical
1035 // template argument.
1036 Expr *InstExpr = InstArg.getAsExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001037 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001038 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1039 if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
1040 Info.Param = makeTemplateParameter(Param);
John McCall833ca992009-10-29 08:12:44 +00001041 Info.FirstArg = Partial->getTemplateArgs()[I];
Mike Stump1eb44332009-09-09 15:08:12 +00001042 return TDK_SubstitutionFailure;
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001043 }
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001044 }
1045 }
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001047 if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) {
1048 Info.Param = makeTemplateParameter(Param);
1049 Info.FirstArg = TemplateArgs[I];
1050 Info.SecondArg = InstArg;
1051 return TDK_NonDeducedMismatch;
1052 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001053 }
1054
Douglas Gregorbb260412009-06-14 08:02:22 +00001055 if (Trap.hasErrorOccurred())
1056 return TDK_SubstitutionFailure;
1057
Douglas Gregorf67875d2009-06-12 18:26:56 +00001058 return TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001059}
Douglas Gregor031a5882009-06-13 00:26:55 +00001060
Douglas Gregor41128772009-06-26 23:27:24 +00001061/// \brief Determine whether the given type T is a simple-template-id type.
1062static bool isSimpleTemplateIdType(QualType T) {
Mike Stump1eb44332009-09-09 15:08:12 +00001063 if (const TemplateSpecializationType *Spec
John McCall183700f2009-09-21 23:43:11 +00001064 = T->getAs<TemplateSpecializationType>())
Douglas Gregor41128772009-06-26 23:27:24 +00001065 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Douglas Gregor41128772009-06-26 23:27:24 +00001067 return false;
1068}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001069
1070/// \brief Substitute the explicitly-provided template arguments into the
1071/// given function template according to C++ [temp.arg.explicit].
1072///
1073/// \param FunctionTemplate the function template into which the explicit
1074/// template arguments will be substituted.
1075///
Mike Stump1eb44332009-09-09 15:08:12 +00001076/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001077/// arguments.
1078///
Mike Stump1eb44332009-09-09 15:08:12 +00001079/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor83314aa2009-07-08 20:55:45 +00001080/// with the converted and checked explicit template arguments.
1081///
Mike Stump1eb44332009-09-09 15:08:12 +00001082/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor83314aa2009-07-08 20:55:45 +00001083/// parameters.
1084///
1085/// \param FunctionType if non-NULL, the result type of the function template
1086/// will also be instantiated and the pointed-to value will be updated with
1087/// the instantiated function type.
1088///
1089/// \param Info if substitution fails for any reason, this object will be
1090/// populated with more information about the failure.
1091///
1092/// \returns TDK_Success if substitution was successful, or some failure
1093/// condition.
1094Sema::TemplateDeductionResult
1095Sema::SubstituteExplicitTemplateArguments(
1096 FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001097 const TemplateArgumentListInfo &ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001098 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1099 llvm::SmallVectorImpl<QualType> &ParamTypes,
1100 QualType *FunctionType,
1101 TemplateDeductionInfo &Info) {
1102 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1103 TemplateParameterList *TemplateParams
1104 = FunctionTemplate->getTemplateParameters();
1105
John McCalld5532b62009-11-23 01:53:49 +00001106 if (ExplicitTemplateArgs.size() == 0) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001107 // No arguments to substitute; just copy over the parameter types and
1108 // fill in the function type.
1109 for (FunctionDecl::param_iterator P = Function->param_begin(),
1110 PEnd = Function->param_end();
1111 P != PEnd;
1112 ++P)
1113 ParamTypes.push_back((*P)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Douglas Gregor83314aa2009-07-08 20:55:45 +00001115 if (FunctionType)
1116 *FunctionType = Function->getType();
1117 return TDK_Success;
1118 }
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Douglas Gregor83314aa2009-07-08 20:55:45 +00001120 // Substitution of the explicit template arguments into a function template
1121 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001122 SFINAETrap Trap(*this);
1123
Douglas Gregor83314aa2009-07-08 20:55:45 +00001124 // C++ [temp.arg.explicit]p3:
Mike Stump1eb44332009-09-09 15:08:12 +00001125 // Template arguments that are present shall be specified in the
1126 // declaration order of their corresponding template-parameters. The
Douglas Gregor83314aa2009-07-08 20:55:45 +00001127 // template argument list shall not specify more template-arguments than
Mike Stump1eb44332009-09-09 15:08:12 +00001128 // there are corresponding template-parameters.
1129 TemplateArgumentListBuilder Builder(TemplateParams,
John McCalld5532b62009-11-23 01:53:49 +00001130 ExplicitTemplateArgs.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001131
1132 // Enter a new template instantiation context where we check the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001133 // explicitly-specified template arguments against this function template,
1134 // and then substitute them into the function parameter types.
Mike Stump1eb44332009-09-09 15:08:12 +00001135 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001136 FunctionTemplate, Deduced.data(), Deduced.size(),
1137 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution);
1138 if (Inst)
1139 return TDK_InstantiationDepth;
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Douglas Gregor83314aa2009-07-08 20:55:45 +00001141 if (CheckTemplateArgumentList(FunctionTemplate,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001142 SourceLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001143 ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001144 true,
1145 Builder) || Trap.hasErrorOccurred())
1146 return TDK_InvalidExplicitArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Douglas Gregor83314aa2009-07-08 20:55:45 +00001148 // Form the template argument list from the explicitly-specified
1149 // template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001150 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor83314aa2009-07-08 20:55:45 +00001151 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1152 Info.reset(ExplicitArgumentList);
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Douglas Gregor83314aa2009-07-08 20:55:45 +00001154 // Instantiate the types of each of the function parameters given the
1155 // explicitly-specified template arguments.
1156 for (FunctionDecl::param_iterator P = Function->param_begin(),
1157 PEnd = Function->param_end();
1158 P != PEnd;
1159 ++P) {
Mike Stump1eb44332009-09-09 15:08:12 +00001160 QualType ParamType
1161 = SubstType((*P)->getType(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001162 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1163 (*P)->getLocation(), (*P)->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001164 if (ParamType.isNull() || Trap.hasErrorOccurred())
1165 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Douglas Gregor83314aa2009-07-08 20:55:45 +00001167 ParamTypes.push_back(ParamType);
1168 }
1169
1170 // If the caller wants a full function type back, instantiate the return
1171 // type and form that function type.
1172 if (FunctionType) {
1173 // FIXME: exception-specifications?
Mike Stump1eb44332009-09-09 15:08:12 +00001174 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001175 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001176 assert(Proto && "Function template does not have a prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001177
1178 QualType ResultType
Douglas Gregor357bbd02009-08-28 20:50:45 +00001179 = SubstType(Proto->getResultType(),
1180 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1181 Function->getTypeSpecStartLoc(),
1182 Function->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001183 if (ResultType.isNull() || Trap.hasErrorOccurred())
1184 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001185
1186 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001187 ParamTypes.data(), ParamTypes.size(),
1188 Proto->isVariadic(),
1189 Proto->getTypeQuals(),
1190 Function->getLocation(),
1191 Function->getDeclName());
1192 if (FunctionType->isNull() || Trap.hasErrorOccurred())
1193 return TDK_SubstitutionFailure;
1194 }
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Douglas Gregor83314aa2009-07-08 20:55:45 +00001196 // C++ [temp.arg.explicit]p2:
Mike Stump1eb44332009-09-09 15:08:12 +00001197 // Trailing template arguments that can be deduced (14.8.2) may be
1198 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001199 // template arguments can be deduced, they may all be omitted; in this
1200 // case, the empty template argument list <> itself may also be omitted.
1201 //
1202 // Take all of the explicitly-specified arguments and put them into the
Mike Stump1eb44332009-09-09 15:08:12 +00001203 // set of deduced template arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001204 Deduced.reserve(TemplateParams->size());
1205 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001206 Deduced.push_back(ExplicitArgumentList->get(I));
1207
Douglas Gregor83314aa2009-07-08 20:55:45 +00001208 return TDK_Success;
1209}
1210
Mike Stump1eb44332009-09-09 15:08:12 +00001211/// \brief Finish template argument deduction for a function template,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001212/// checking the deduced template arguments for completeness and forming
1213/// the function template specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00001214Sema::TemplateDeductionResult
Douglas Gregor83314aa2009-07-08 20:55:45 +00001215Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
1216 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1217 FunctionDecl *&Specialization,
1218 TemplateDeductionInfo &Info) {
1219 TemplateParameterList *TemplateParams
1220 = FunctionTemplate->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Douglas Gregor83314aa2009-07-08 20:55:45 +00001222 // Template argument deduction for function templates in a SFINAE context.
1223 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001224 SFINAETrap Trap(*this);
1225
Douglas Gregor83314aa2009-07-08 20:55:45 +00001226 // Enter a new template instantiation context while we instantiate the
1227 // actual function declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001228 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001229 FunctionTemplate, Deduced.data(), Deduced.size(),
1230 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
1231 if (Inst)
Mike Stump1eb44332009-09-09 15:08:12 +00001232 return TDK_InstantiationDepth;
1233
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001234 // C++ [temp.deduct.type]p2:
1235 // [...] or if any template argument remains neither deduced nor
1236 // explicitly specified, template argument deduction fails.
1237 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
1238 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
1239 if (!Deduced[I].isNull()) {
1240 Builder.Append(Deduced[I]);
1241 continue;
1242 }
1243
1244 // Substitute into the default template argument, if available.
1245 NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I);
1246 TemplateArgumentLoc DefArg
1247 = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
1248 FunctionTemplate->getLocation(),
1249 FunctionTemplate->getSourceRange().getEnd(),
1250 Param,
1251 Builder);
1252
1253 // If there was no default argument, deduction is incomplete.
1254 if (DefArg.getArgument().isNull()) {
1255 Info.Param = makeTemplateParameter(
1256 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1257 return TDK_Incomplete;
1258 }
1259
1260 // Check whether we can actually use the default argument.
1261 if (CheckTemplateArgument(Param, DefArg,
1262 FunctionTemplate,
1263 FunctionTemplate->getLocation(),
1264 FunctionTemplate->getSourceRange().getEnd(),
1265 Builder)) {
1266 Info.Param = makeTemplateParameter(
1267 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1268 return TDK_SubstitutionFailure;
1269 }
1270
1271 // If we get here, we successfully used the default template argument.
1272 }
1273
1274 // Form the template argument list from the deduced template arguments.
1275 TemplateArgumentList *DeducedArgumentList
1276 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1277 Info.reset(DeducedArgumentList);
1278
Mike Stump1eb44332009-09-09 15:08:12 +00001279 // Substitute the deduced template arguments into the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001280 // declaration to produce the function template specialization.
1281 Specialization = cast_or_null<FunctionDecl>(
John McCallce3ff2b2009-08-25 22:02:44 +00001282 SubstDecl(FunctionTemplate->getTemplatedDecl(),
1283 FunctionTemplate->getDeclContext(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001284 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor83314aa2009-07-08 20:55:45 +00001285 if (!Specialization)
1286 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Douglas Gregorf8825742009-09-15 18:26:13 +00001288 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
1289 FunctionTemplate->getCanonicalDecl());
1290
Mike Stump1eb44332009-09-09 15:08:12 +00001291 // If the template argument list is owned by the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001292 // specialization, release it.
1293 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList)
1294 Info.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Douglas Gregor83314aa2009-07-08 20:55:45 +00001296 // There may have been an error that did not prevent us from constructing a
1297 // declaration. Mark the declaration invalid and return with a substitution
1298 // failure.
1299 if (Trap.hasErrorOccurred()) {
1300 Specialization->setInvalidDecl(true);
1301 return TDK_SubstitutionFailure;
1302 }
Mike Stump1eb44332009-09-09 15:08:12 +00001303
1304 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001305}
1306
Douglas Gregore53060f2009-06-25 22:08:12 +00001307/// \brief Perform template argument deduction from a function call
1308/// (C++ [temp.deduct.call]).
1309///
1310/// \param FunctionTemplate the function template for which we are performing
1311/// template argument deduction.
1312///
Mike Stump1eb44332009-09-09 15:08:12 +00001313/// \param HasExplicitTemplateArgs whether any template arguments were
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001314/// explicitly specified.
1315///
1316/// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
1317/// the explicitly-specified template arguments.
1318///
1319/// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
Mike Stump1eb44332009-09-09 15:08:12 +00001320/// the number of explicitly-specified template arguments in
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001321/// @p ExplicitTemplateArguments. This value may be zero.
1322///
Douglas Gregore53060f2009-06-25 22:08:12 +00001323/// \param Args the function call arguments
1324///
1325/// \param NumArgs the number of arguments in Args
1326///
1327/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001328/// this will be set to the function template specialization produced by
Douglas Gregore53060f2009-06-25 22:08:12 +00001329/// template argument deduction.
1330///
1331/// \param Info the argument will be updated to provide additional information
1332/// about template argument deduction.
1333///
1334/// \returns the result of template argument deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001335Sema::TemplateDeductionResult
1336Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001337 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregore53060f2009-06-25 22:08:12 +00001338 Expr **Args, unsigned NumArgs,
1339 FunctionDecl *&Specialization,
1340 TemplateDeductionInfo &Info) {
1341 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001342
Douglas Gregore53060f2009-06-25 22:08:12 +00001343 // C++ [temp.deduct.call]p1:
1344 // Template argument deduction is done by comparing each function template
1345 // parameter type (call it P) with the type of the corresponding argument
1346 // of the call (call it A) as described below.
1347 unsigned CheckArgs = NumArgs;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001348 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregore53060f2009-06-25 22:08:12 +00001349 return TDK_TooFewArguments;
1350 else if (NumArgs > Function->getNumParams()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001351 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001352 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregore53060f2009-06-25 22:08:12 +00001353 if (!Proto->isVariadic())
1354 return TDK_TooManyArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001355
Douglas Gregore53060f2009-06-25 22:08:12 +00001356 CheckArgs = Function->getNumParams();
1357 }
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001359 // The types of the parameters from which we will perform template argument
1360 // deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001361 TemplateParameterList *TemplateParams
1362 = FunctionTemplate->getTemplateParameters();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001363 llvm::SmallVector<TemplateArgument, 4> Deduced;
1364 llvm::SmallVector<QualType, 4> ParamTypes;
John McCalld5532b62009-11-23 01:53:49 +00001365 if (ExplicitTemplateArgs) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001366 TemplateDeductionResult Result =
1367 SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001368 *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001369 Deduced,
1370 ParamTypes,
1371 0,
1372 Info);
1373 if (Result)
1374 return Result;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001375 } else {
1376 // Just fill in the parameter types from the function declaration.
1377 for (unsigned I = 0; I != CheckArgs; ++I)
1378 ParamTypes.push_back(Function->getParamDecl(I)->getType());
1379 }
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001381 // Deduce template arguments from the function parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001382 Deduced.resize(TemplateParams->size());
Douglas Gregore53060f2009-06-25 22:08:12 +00001383 for (unsigned I = 0; I != CheckArgs; ++I) {
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001384 QualType ParamType = ParamTypes[I];
Douglas Gregore53060f2009-06-25 22:08:12 +00001385 QualType ArgType = Args[I]->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Douglas Gregore53060f2009-06-25 22:08:12 +00001387 // C++ [temp.deduct.call]p2:
1388 // If P is not a reference type:
1389 QualType CanonParamType = Context.getCanonicalType(ParamType);
Douglas Gregor500d3312009-06-26 18:27:22 +00001390 bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1391 if (!ParamWasReference) {
Mike Stump1eb44332009-09-09 15:08:12 +00001392 // - If A is an array type, the pointer type produced by the
1393 // array-to-pointer standard conversion (4.2) is used in place of
Douglas Gregore53060f2009-06-25 22:08:12 +00001394 // A for type deduction; otherwise,
1395 if (ArgType->isArrayType())
1396 ArgType = Context.getArrayDecayedType(ArgType);
Mike Stump1eb44332009-09-09 15:08:12 +00001397 // - If A is a function type, the pointer type produced by the
1398 // function-to-pointer standard conversion (4.3) is used in place
Douglas Gregore53060f2009-06-25 22:08:12 +00001399 // of A for type deduction; otherwise,
1400 else if (ArgType->isFunctionType())
1401 ArgType = Context.getPointerType(ArgType);
1402 else {
1403 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1404 // type are ignored for type deduction.
1405 QualType CanonArgType = Context.getCanonicalType(ArgType);
Douglas Gregora4923eb2009-11-16 21:35:15 +00001406 if (CanonArgType.getLocalCVRQualifiers())
1407 ArgType = CanonArgType.getLocalUnqualifiedType();
Douglas Gregore53060f2009-06-25 22:08:12 +00001408 }
1409 }
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Douglas Gregore53060f2009-06-25 22:08:12 +00001411 // C++0x [temp.deduct.call]p3:
1412 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
Mike Stump1eb44332009-09-09 15:08:12 +00001413 // are ignored for type deduction.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001414 if (CanonParamType.getLocalCVRQualifiers())
1415 ParamType = CanonParamType.getLocalUnqualifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001416 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001417 // [...] If P is a reference type, the type referred to by P is used
1418 // for type deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001419 ParamType = ParamRefType->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00001420
1421 // [...] If P is of the form T&&, where T is a template parameter, and
1422 // the argument is an lvalue, the type A& is used in place of A for
Douglas Gregore53060f2009-06-25 22:08:12 +00001423 // type deduction.
1424 if (isa<RValueReferenceType>(ParamRefType) &&
John McCall183700f2009-09-21 23:43:11 +00001425 ParamRefType->getAs<TemplateTypeParmType>() &&
Douglas Gregore53060f2009-06-25 22:08:12 +00001426 Args[I]->isLvalue(Context) == Expr::LV_Valid)
1427 ArgType = Context.getLValueReferenceType(ArgType);
1428 }
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Douglas Gregore53060f2009-06-25 22:08:12 +00001430 // C++0x [temp.deduct.call]p4:
1431 // In general, the deduction process attempts to find template argument
1432 // values that will make the deduced A identical to A (after the type A
1433 // is transformed as described above). [...]
Douglas Gregor12820292009-09-14 20:00:47 +00001434 unsigned TDF = TDF_SkipNonDependent;
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Douglas Gregor508f1c82009-06-26 23:10:12 +00001436 // - If the original P is a reference type, the deduced A (i.e., the
1437 // type referred to by the reference) can be more cv-qualified than
1438 // the transformed A.
1439 if (ParamWasReference)
1440 TDF |= TDF_ParamWithReferenceType;
Mike Stump1eb44332009-09-09 15:08:12 +00001441 // - The transformed A can be another pointer or pointer to member
1442 // type that can be converted to the deduced A via a qualification
Douglas Gregor508f1c82009-06-26 23:10:12 +00001443 // conversion (4.4).
1444 if (ArgType->isPointerType() || ArgType->isMemberPointerType())
1445 TDF |= TDF_IgnoreQualifiers;
Mike Stump1eb44332009-09-09 15:08:12 +00001446 // - If P is a class and P has the form simple-template-id, then the
Douglas Gregor41128772009-06-26 23:27:24 +00001447 // transformed A can be a derived class of the deduced A. Likewise,
1448 // if P is a pointer to a class of the form simple-template-id, the
1449 // transformed A can be a pointer to a derived class pointed to by
1450 // the deduced A.
1451 if (isSimpleTemplateIdType(ParamType) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001452 (isa<PointerType>(ParamType) &&
Douglas Gregor41128772009-06-26 23:27:24 +00001453 isSimpleTemplateIdType(
Ted Kremenek6217b802009-07-29 21:53:49 +00001454 ParamType->getAs<PointerType>()->getPointeeType())))
Douglas Gregor41128772009-06-26 23:27:24 +00001455 TDF |= TDF_DerivedClass;
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Douglas Gregor4b52e252009-12-21 23:17:24 +00001457 // FIXME: C++0x [temp.deduct.call] paragraphs 6-9 deal with function
1458 // pointer parameters.
1459
1460 if (Context.hasSameUnqualifiedType(ArgType, Context.OverloadTy)) {
1461 // We know that template argument deduction will fail if the argument is
1462 // still an overloaded function. Check whether we can resolve this
1463 // argument as a single function template specialization per
1464 // C++ [temp.arg.explicit]p3.
1465 FunctionDecl *ExplicitSpec
1466 = ResolveSingleFunctionTemplateSpecialization(Args[I]);
1467 Expr *ResolvedArg = 0;
1468 if (ExplicitSpec)
1469 ResolvedArg = FixOverloadedFunctionReference(Args[I], ExplicitSpec);
1470 if (!ExplicitSpec || !ResolvedArg) {
1471 // Template argument deduction fails if we can't resolve the overloaded
1472 // function.
1473 return TDK_FailedOverloadResolution;
1474 }
1475
1476 // Get the type of the resolved argument.
1477 ArgType = ResolvedArg->getType();
1478 if (ArgType->isPointerType() || ArgType->isMemberPointerType())
1479 TDF |= TDF_IgnoreQualifiers;
1480
1481 ResolvedArg->Destroy(Context);
1482 }
1483
Douglas Gregore53060f2009-06-25 22:08:12 +00001484 if (TemplateDeductionResult Result
1485 = ::DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor500d3312009-06-26 18:27:22 +00001486 ParamType, ArgType, Info, Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +00001487 TDF))
Douglas Gregore53060f2009-06-25 22:08:12 +00001488 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001489
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001490 // FIXME: we need to check that the deduced A is the same as A,
1491 // modulo the various allowed differences.
Douglas Gregore53060f2009-06-25 22:08:12 +00001492 }
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001493
Mike Stump1eb44332009-09-09 15:08:12 +00001494 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001495 Specialization, Info);
Douglas Gregore53060f2009-06-25 22:08:12 +00001496}
1497
Douglas Gregor83314aa2009-07-08 20:55:45 +00001498/// \brief Deduce template arguments when taking the address of a function
Douglas Gregor4b52e252009-12-21 23:17:24 +00001499/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
1500/// a template.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001501///
1502/// \param FunctionTemplate the function template for which we are performing
1503/// template argument deduction.
1504///
Douglas Gregor4b52e252009-12-21 23:17:24 +00001505/// \param ExplicitTemplateArguments the explicitly-specified template
1506/// arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001507///
1508/// \param ArgFunctionType the function type that will be used as the
1509/// "argument" type (A) when performing template argument deduction from the
Douglas Gregor4b52e252009-12-21 23:17:24 +00001510/// function template's function type. This type may be NULL, if there is no
1511/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001512///
1513/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001514/// this will be set to the function template specialization produced by
Douglas Gregor83314aa2009-07-08 20:55:45 +00001515/// template argument deduction.
1516///
1517/// \param Info the argument will be updated to provide additional information
1518/// about template argument deduction.
1519///
1520/// \returns the result of template argument deduction.
1521Sema::TemplateDeductionResult
1522Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001523 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001524 QualType ArgFunctionType,
1525 FunctionDecl *&Specialization,
1526 TemplateDeductionInfo &Info) {
1527 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1528 TemplateParameterList *TemplateParams
1529 = FunctionTemplate->getTemplateParameters();
1530 QualType FunctionType = Function->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001531
Douglas Gregor83314aa2009-07-08 20:55:45 +00001532 // Substitute any explicit template arguments.
1533 llvm::SmallVector<TemplateArgument, 4> Deduced;
1534 llvm::SmallVector<QualType, 4> ParamTypes;
John McCalld5532b62009-11-23 01:53:49 +00001535 if (ExplicitTemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001536 if (TemplateDeductionResult Result
1537 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001538 *ExplicitTemplateArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00001539 Deduced, ParamTypes,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001540 &FunctionType, Info))
1541 return Result;
1542 }
1543
1544 // Template argument deduction for function templates in a SFINAE context.
1545 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001546 SFINAETrap Trap(*this);
1547
Douglas Gregor4b52e252009-12-21 23:17:24 +00001548 if (!ArgFunctionType.isNull()) {
1549 // Deduce template arguments from the function type.
1550 Deduced.resize(TemplateParams->size());
1551 if (TemplateDeductionResult Result
1552 = ::DeduceTemplateArguments(Context, TemplateParams,
1553 FunctionType, ArgFunctionType, Info,
1554 Deduced, 0))
1555 return Result;
1556 }
1557
Mike Stump1eb44332009-09-09 15:08:12 +00001558 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001559 Specialization, Info);
1560}
1561
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001562/// \brief Deduce template arguments for a templated conversion
1563/// function (C++ [temp.deduct.conv]) and, if successful, produce a
1564/// conversion function template specialization.
1565Sema::TemplateDeductionResult
1566Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1567 QualType ToType,
1568 CXXConversionDecl *&Specialization,
1569 TemplateDeductionInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00001570 CXXConversionDecl *Conv
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001571 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
1572 QualType FromType = Conv->getConversionType();
1573
1574 // Canonicalize the types for deduction.
1575 QualType P = Context.getCanonicalType(FromType);
1576 QualType A = Context.getCanonicalType(ToType);
1577
1578 // C++0x [temp.deduct.conv]p3:
1579 // If P is a reference type, the type referred to by P is used for
1580 // type deduction.
1581 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
1582 P = PRef->getPointeeType();
1583
1584 // C++0x [temp.deduct.conv]p3:
1585 // If A is a reference type, the type referred to by A is used
1586 // for type deduction.
1587 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
1588 A = ARef->getPointeeType();
1589 // C++ [temp.deduct.conv]p2:
1590 //
Mike Stump1eb44332009-09-09 15:08:12 +00001591 // If A is not a reference type:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001592 else {
1593 assert(!A->isReferenceType() && "Reference types were handled above");
1594
1595 // - If P is an array type, the pointer type produced by the
Mike Stump1eb44332009-09-09 15:08:12 +00001596 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001597 // of P for type deduction; otherwise,
1598 if (P->isArrayType())
1599 P = Context.getArrayDecayedType(P);
1600 // - If P is a function type, the pointer type produced by the
1601 // function-to-pointer standard conversion (4.3) is used in
1602 // place of P for type deduction; otherwise,
1603 else if (P->isFunctionType())
1604 P = Context.getPointerType(P);
1605 // - If P is a cv-qualified type, the top level cv-qualifiers of
1606 // P’s type are ignored for type deduction.
1607 else
1608 P = P.getUnqualifiedType();
1609
1610 // C++0x [temp.deduct.conv]p3:
1611 // If A is a cv-qualified type, the top level cv-qualifiers of A’s
1612 // type are ignored for type deduction.
1613 A = A.getUnqualifiedType();
1614 }
1615
1616 // Template argument deduction for function templates in a SFINAE context.
1617 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001618 SFINAETrap Trap(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001619
1620 // C++ [temp.deduct.conv]p1:
1621 // Template argument deduction is done by comparing the return
1622 // type of the template conversion function (call it P) with the
1623 // type that is required as the result of the conversion (call it
1624 // A) as described in 14.8.2.4.
1625 TemplateParameterList *TemplateParams
1626 = FunctionTemplate->getTemplateParameters();
1627 llvm::SmallVector<TemplateArgument, 4> Deduced;
Mike Stump1eb44332009-09-09 15:08:12 +00001628 Deduced.resize(TemplateParams->size());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001629
1630 // C++0x [temp.deduct.conv]p4:
1631 // In general, the deduction process attempts to find template
1632 // argument values that will make the deduced A identical to
1633 // A. However, there are two cases that allow a difference:
1634 unsigned TDF = 0;
1635 // - If the original A is a reference type, A can be more
1636 // cv-qualified than the deduced A (i.e., the type referred to
1637 // by the reference)
1638 if (ToType->isReferenceType())
1639 TDF |= TDF_ParamWithReferenceType;
1640 // - The deduced A can be another pointer or pointer to member
1641 // type that can be converted to A via a qualification
1642 // conversion.
1643 //
1644 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
1645 // both P and A are pointers or member pointers. In this case, we
1646 // just ignore cv-qualifiers completely).
1647 if ((P->isPointerType() && A->isPointerType()) ||
1648 (P->isMemberPointerType() && P->isMemberPointerType()))
1649 TDF |= TDF_IgnoreQualifiers;
1650 if (TemplateDeductionResult Result
1651 = ::DeduceTemplateArguments(Context, TemplateParams,
1652 P, A, Info, Deduced, TDF))
1653 return Result;
1654
1655 // FIXME: we need to check that the deduced A is the same as A,
1656 // modulo the various allowed differences.
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001658 // Finish template argument deduction.
1659 FunctionDecl *Spec = 0;
1660 TemplateDeductionResult Result
1661 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, Spec, Info);
1662 Specialization = cast_or_null<CXXConversionDecl>(Spec);
1663 return Result;
1664}
1665
Douglas Gregor4b52e252009-12-21 23:17:24 +00001666/// \brief Deduce template arguments for a function template when there is
1667/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
1668///
1669/// \param FunctionTemplate the function template for which we are performing
1670/// template argument deduction.
1671///
1672/// \param ExplicitTemplateArguments the explicitly-specified template
1673/// arguments.
1674///
1675/// \param Specialization if template argument deduction was successful,
1676/// this will be set to the function template specialization produced by
1677/// template argument deduction.
1678///
1679/// \param Info the argument will be updated to provide additional information
1680/// about template argument deduction.
1681///
1682/// \returns the result of template argument deduction.
1683Sema::TemplateDeductionResult
1684Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1685 const TemplateArgumentListInfo *ExplicitTemplateArgs,
1686 FunctionDecl *&Specialization,
1687 TemplateDeductionInfo &Info) {
1688 return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
1689 QualType(), Specialization, Info);
1690}
1691
Douglas Gregor8a514912009-09-14 18:39:43 +00001692/// \brief Stores the result of comparing the qualifiers of two types.
1693enum DeductionQualifierComparison {
1694 NeitherMoreQualified = 0,
1695 ParamMoreQualified,
1696 ArgMoreQualified
1697};
1698
1699/// \brief Deduce the template arguments during partial ordering by comparing
1700/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
1701///
1702/// \param Context the AST context in which this deduction occurs.
1703///
1704/// \param TemplateParams the template parameters that we are deducing
1705///
1706/// \param ParamIn the parameter type
1707///
1708/// \param ArgIn the argument type
1709///
1710/// \param Info information about the template argument deduction itself
1711///
1712/// \param Deduced the deduced template arguments
1713///
1714/// \returns the result of template argument deduction so far. Note that a
1715/// "success" result means that template argument deduction has not yet failed,
1716/// but it may still fail, later, for other reasons.
1717static Sema::TemplateDeductionResult
1718DeduceTemplateArgumentsDuringPartialOrdering(ASTContext &Context,
1719 TemplateParameterList *TemplateParams,
1720 QualType ParamIn, QualType ArgIn,
1721 Sema::TemplateDeductionInfo &Info,
1722 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1723 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1724 CanQualType Param = Context.getCanonicalType(ParamIn);
1725 CanQualType Arg = Context.getCanonicalType(ArgIn);
1726
1727 // C++0x [temp.deduct.partial]p5:
1728 // Before the partial ordering is done, certain transformations are
1729 // performed on the types used for partial ordering:
1730 // - If P is a reference type, P is replaced by the type referred to.
1731 CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00001732 if (!ParamRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00001733 Param = ParamRef->getPointeeType();
1734
1735 // - If A is a reference type, A is replaced by the type referred to.
1736 CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00001737 if (!ArgRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00001738 Arg = ArgRef->getPointeeType();
1739
John McCalle27ec8a2009-10-23 23:03:21 +00001740 if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
Douglas Gregor8a514912009-09-14 18:39:43 +00001741 // C++0x [temp.deduct.partial]p6:
1742 // If both P and A were reference types (before being replaced with the
1743 // type referred to above), determine which of the two types (if any) is
1744 // more cv-qualified than the other; otherwise the types are considered to
1745 // be equally cv-qualified for partial ordering purposes. The result of this
1746 // determination will be used below.
1747 //
1748 // We save this information for later, using it only when deduction
1749 // succeeds in both directions.
1750 DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
1751 if (Param.isMoreQualifiedThan(Arg))
1752 QualifierResult = ParamMoreQualified;
1753 else if (Arg.isMoreQualifiedThan(Param))
1754 QualifierResult = ArgMoreQualified;
1755 QualifierComparisons->push_back(QualifierResult);
1756 }
1757
1758 // C++0x [temp.deduct.partial]p7:
1759 // Remove any top-level cv-qualifiers:
1760 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
1761 // version of P.
1762 Param = Param.getUnqualifiedType();
1763 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
1764 // version of A.
1765 Arg = Arg.getUnqualifiedType();
1766
1767 // C++0x [temp.deduct.partial]p8:
1768 // Using the resulting types P and A the deduction is then done as
1769 // described in 14.9.2.5. If deduction succeeds for a given type, the type
1770 // from the argument template is considered to be at least as specialized
1771 // as the type from the parameter template.
1772 return DeduceTemplateArguments(Context, TemplateParams, Param, Arg, Info,
1773 Deduced, TDF_None);
1774}
1775
1776static void
Douglas Gregore73bb602009-09-14 21:25:05 +00001777MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
1778 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001779 unsigned Level,
Douglas Gregore73bb602009-09-14 21:25:05 +00001780 llvm::SmallVectorImpl<bool> &Deduced);
Douglas Gregor8a514912009-09-14 18:39:43 +00001781
1782/// \brief Determine whether the function template \p FT1 is at least as
1783/// specialized as \p FT2.
1784static bool isAtLeastAsSpecializedAs(Sema &S,
1785 FunctionTemplateDecl *FT1,
1786 FunctionTemplateDecl *FT2,
1787 TemplatePartialOrderingContext TPOC,
1788 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1789 FunctionDecl *FD1 = FT1->getTemplatedDecl();
1790 FunctionDecl *FD2 = FT2->getTemplatedDecl();
1791 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
1792 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
1793
1794 assert(Proto1 && Proto2 && "Function templates must have prototypes");
1795 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
1796 llvm::SmallVector<TemplateArgument, 4> Deduced;
1797 Deduced.resize(TemplateParams->size());
1798
1799 // C++0x [temp.deduct.partial]p3:
1800 // The types used to determine the ordering depend on the context in which
1801 // the partial ordering is done:
1802 Sema::TemplateDeductionInfo Info(S.Context);
1803 switch (TPOC) {
1804 case TPOC_Call: {
1805 // - In the context of a function call, the function parameter types are
1806 // used.
1807 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
1808 for (unsigned I = 0; I != NumParams; ++I)
1809 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1810 TemplateParams,
1811 Proto2->getArgType(I),
1812 Proto1->getArgType(I),
1813 Info,
1814 Deduced,
1815 QualifierComparisons))
1816 return false;
1817
1818 break;
1819 }
1820
1821 case TPOC_Conversion:
1822 // - In the context of a call to a conversion operator, the return types
1823 // of the conversion function templates are used.
1824 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1825 TemplateParams,
1826 Proto2->getResultType(),
1827 Proto1->getResultType(),
1828 Info,
1829 Deduced,
1830 QualifierComparisons))
1831 return false;
1832 break;
1833
1834 case TPOC_Other:
1835 // - In other contexts (14.6.6.2) the function template’s function type
1836 // is used.
1837 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1838 TemplateParams,
1839 FD2->getType(),
1840 FD1->getType(),
1841 Info,
1842 Deduced,
1843 QualifierComparisons))
1844 return false;
1845 break;
1846 }
1847
1848 // C++0x [temp.deduct.partial]p11:
1849 // In most cases, all template parameters must have values in order for
1850 // deduction to succeed, but for partial ordering purposes a template
1851 // parameter may remain without a value provided it is not used in the
1852 // types being used for partial ordering. [ Note: a template parameter used
1853 // in a non-deduced context is considered used. -end note]
1854 unsigned ArgIdx = 0, NumArgs = Deduced.size();
1855 for (; ArgIdx != NumArgs; ++ArgIdx)
1856 if (Deduced[ArgIdx].isNull())
1857 break;
1858
1859 if (ArgIdx == NumArgs) {
1860 // All template arguments were deduced. FT1 is at least as specialized
1861 // as FT2.
1862 return true;
1863 }
1864
Douglas Gregore73bb602009-09-14 21:25:05 +00001865 // Figure out which template parameters were used.
Douglas Gregor8a514912009-09-14 18:39:43 +00001866 llvm::SmallVector<bool, 4> UsedParameters;
1867 UsedParameters.resize(TemplateParams->size());
1868 switch (TPOC) {
1869 case TPOC_Call: {
1870 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
1871 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00001872 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
1873 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00001874 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00001875 break;
1876 }
1877
1878 case TPOC_Conversion:
Douglas Gregored9c0f92009-10-29 00:04:11 +00001879 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
1880 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00001881 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00001882 break;
1883
1884 case TPOC_Other:
Douglas Gregored9c0f92009-10-29 00:04:11 +00001885 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
1886 TemplateParams->getDepth(),
1887 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00001888 break;
1889 }
1890
1891 for (; ArgIdx != NumArgs; ++ArgIdx)
1892 // If this argument had no value deduced but was used in one of the types
1893 // used for partial ordering, then deduction fails.
1894 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
1895 return false;
1896
1897 return true;
1898}
1899
1900
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001901/// \brief Returns the more specialized function template according
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001902/// to the rules of function template partial ordering (C++ [temp.func.order]).
1903///
1904/// \param FT1 the first function template
1905///
1906/// \param FT2 the second function template
1907///
Douglas Gregor8a514912009-09-14 18:39:43 +00001908/// \param TPOC the context in which we are performing partial ordering of
1909/// function templates.
Mike Stump1eb44332009-09-09 15:08:12 +00001910///
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001911/// \returns the more specialized function template. If neither
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001912/// template is more specialized, returns NULL.
1913FunctionTemplateDecl *
1914Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
1915 FunctionTemplateDecl *FT2,
Douglas Gregor8a514912009-09-14 18:39:43 +00001916 TemplatePartialOrderingContext TPOC) {
Douglas Gregor8a514912009-09-14 18:39:43 +00001917 llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
1918 bool Better1 = isAtLeastAsSpecializedAs(*this, FT1, FT2, TPOC, 0);
1919 bool Better2 = isAtLeastAsSpecializedAs(*this, FT2, FT1, TPOC,
1920 &QualifierComparisons);
1921
1922 if (Better1 != Better2) // We have a clear winner
1923 return Better1? FT1 : FT2;
1924
1925 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001926 return 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00001927
1928
1929 // C++0x [temp.deduct.partial]p10:
1930 // If for each type being considered a given template is at least as
1931 // specialized for all types and more specialized for some set of types and
1932 // the other template is not more specialized for any types or is not at
1933 // least as specialized for any types, then the given template is more
1934 // specialized than the other template. Otherwise, neither template is more
1935 // specialized than the other.
1936 Better1 = false;
1937 Better2 = false;
1938 for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
1939 // C++0x [temp.deduct.partial]p9:
1940 // If, for a given type, deduction succeeds in both directions (i.e., the
1941 // types are identical after the transformations above) and if the type
1942 // from the argument template is more cv-qualified than the type from the
1943 // parameter template (as described above) that type is considered to be
1944 // more specialized than the other. If neither type is more cv-qualified
1945 // than the other then neither type is more specialized than the other.
1946 switch (QualifierComparisons[I]) {
1947 case NeitherMoreQualified:
1948 break;
1949
1950 case ParamMoreQualified:
1951 Better1 = true;
1952 if (Better2)
1953 return 0;
1954 break;
1955
1956 case ArgMoreQualified:
1957 Better2 = true;
1958 if (Better1)
1959 return 0;
1960 break;
1961 }
1962 }
1963
1964 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001965 if (Better1)
1966 return FT1;
Douglas Gregor8a514912009-09-14 18:39:43 +00001967 else if (Better2)
1968 return FT2;
1969 else
1970 return 0;
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001971}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001972
Douglas Gregord5a423b2009-09-25 18:43:00 +00001973/// \brief Determine if the two templates are equivalent.
1974static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
1975 if (T1 == T2)
1976 return true;
1977
1978 if (!T1 || !T2)
1979 return false;
1980
1981 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
1982}
1983
1984/// \brief Retrieve the most specialized of the given function template
1985/// specializations.
1986///
1987/// \param Specializations the set of function template specializations that
1988/// we will be comparing.
1989///
1990/// \param NumSpecializations the number of function template specializations in
1991/// \p Specializations
1992///
1993/// \param TPOC the partial ordering context to use to compare the function
1994/// template specializations.
1995///
1996/// \param Loc the location where the ambiguity or no-specializations
1997/// diagnostic should occur.
1998///
1999/// \param NoneDiag partial diagnostic used to diagnose cases where there are
2000/// no matching candidates.
2001///
2002/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
2003/// occurs.
2004///
2005/// \param CandidateDiag partial diagnostic used for each function template
2006/// specialization that is a candidate in the ambiguous ordering. One parameter
2007/// in this diagnostic should be unbound, which will correspond to the string
2008/// describing the template arguments for the function template specialization.
2009///
2010/// \param Index if non-NULL and the result of this function is non-nULL,
2011/// receives the index corresponding to the resulting function template
2012/// specialization.
2013///
2014/// \returns the most specialized function template specialization, if
2015/// found. Otherwise, returns NULL.
2016///
2017/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
2018/// template argument deduction.
2019FunctionDecl *Sema::getMostSpecialized(FunctionDecl **Specializations,
2020 unsigned NumSpecializations,
2021 TemplatePartialOrderingContext TPOC,
2022 SourceLocation Loc,
2023 const PartialDiagnostic &NoneDiag,
2024 const PartialDiagnostic &AmbigDiag,
2025 const PartialDiagnostic &CandidateDiag,
2026 unsigned *Index) {
2027 if (NumSpecializations == 0) {
2028 Diag(Loc, NoneDiag);
2029 return 0;
2030 }
2031
2032 if (NumSpecializations == 1) {
2033 if (Index)
2034 *Index = 0;
2035
2036 return Specializations[0];
2037 }
2038
2039
2040 // Find the function template that is better than all of the templates it
2041 // has been compared to.
2042 unsigned Best = 0;
2043 FunctionTemplateDecl *BestTemplate
2044 = Specializations[Best]->getPrimaryTemplate();
2045 assert(BestTemplate && "Not a function template specialization?");
2046 for (unsigned I = 1; I != NumSpecializations; ++I) {
2047 FunctionTemplateDecl *Challenger = Specializations[I]->getPrimaryTemplate();
2048 assert(Challenger && "Not a function template specialization?");
2049 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
2050 TPOC),
2051 Challenger)) {
2052 Best = I;
2053 BestTemplate = Challenger;
2054 }
2055 }
2056
2057 // Make sure that the "best" function template is more specialized than all
2058 // of the others.
2059 bool Ambiguous = false;
2060 for (unsigned I = 0; I != NumSpecializations; ++I) {
2061 FunctionTemplateDecl *Challenger = Specializations[I]->getPrimaryTemplate();
2062 if (I != Best &&
2063 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
2064 TPOC),
2065 BestTemplate)) {
2066 Ambiguous = true;
2067 break;
2068 }
2069 }
2070
2071 if (!Ambiguous) {
2072 // We found an answer. Return it.
2073 if (Index)
2074 *Index = Best;
2075 return Specializations[Best];
2076 }
2077
2078 // Diagnose the ambiguity.
2079 Diag(Loc, AmbigDiag);
2080
2081 // FIXME: Can we order the candidates in some sane way?
2082 for (unsigned I = 0; I != NumSpecializations; ++I)
2083 Diag(Specializations[I]->getLocation(), CandidateDiag)
2084 << getTemplateArgumentBindingsText(
2085 Specializations[I]->getPrimaryTemplate()->getTemplateParameters(),
2086 *Specializations[I]->getTemplateSpecializationArgs());
2087
2088 return 0;
2089}
2090
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002091/// \brief Returns the more specialized class template partial specialization
2092/// according to the rules of partial ordering of class template partial
2093/// specializations (C++ [temp.class.order]).
2094///
2095/// \param PS1 the first class template partial specialization
2096///
2097/// \param PS2 the second class template partial specialization
2098///
2099/// \returns the more specialized class template partial specialization. If
2100/// neither partial specialization is more specialized, returns NULL.
2101ClassTemplatePartialSpecializationDecl *
2102Sema::getMoreSpecializedPartialSpecialization(
2103 ClassTemplatePartialSpecializationDecl *PS1,
2104 ClassTemplatePartialSpecializationDecl *PS2) {
2105 // C++ [temp.class.order]p1:
2106 // For two class template partial specializations, the first is at least as
2107 // specialized as the second if, given the following rewrite to two
2108 // function templates, the first function template is at least as
2109 // specialized as the second according to the ordering rules for function
2110 // templates (14.6.6.2):
2111 // - the first function template has the same template parameters as the
2112 // first partial specialization and has a single function parameter
2113 // whose type is a class template specialization with the template
2114 // arguments of the first partial specialization, and
2115 // - the second function template has the same template parameters as the
2116 // second partial specialization and has a single function parameter
2117 // whose type is a class template specialization with the template
2118 // arguments of the second partial specialization.
2119 //
2120 // Rather than synthesize function templates, we merely perform the
2121 // equivalent partial ordering by performing deduction directly on the
2122 // template arguments of the class template partial specializations. This
2123 // computation is slightly simpler than the general problem of function
2124 // template partial ordering, because class template partial specializations
2125 // are more constrained. We know that every template parameter is deduc
2126 llvm::SmallVector<TemplateArgument, 4> Deduced;
2127 Sema::TemplateDeductionInfo Info(Context);
2128
2129 // Determine whether PS1 is at least as specialized as PS2
2130 Deduced.resize(PS2->getTemplateParameters()->size());
2131 bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(Context,
2132 PS2->getTemplateParameters(),
2133 Context.getTypeDeclType(PS2),
2134 Context.getTypeDeclType(PS1),
2135 Info,
2136 Deduced,
2137 0);
2138
2139 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregordb0d4b72009-11-11 23:06:43 +00002140 Deduced.clear();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002141 Deduced.resize(PS1->getTemplateParameters()->size());
2142 bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(Context,
2143 PS1->getTemplateParameters(),
2144 Context.getTypeDeclType(PS1),
2145 Context.getTypeDeclType(PS2),
2146 Info,
2147 Deduced,
2148 0);
2149
2150 if (Better1 == Better2)
2151 return 0;
2152
2153 return Better1? PS1 : PS2;
2154}
2155
Mike Stump1eb44332009-09-09 15:08:12 +00002156static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002157MarkUsedTemplateParameters(Sema &SemaRef,
2158 const TemplateArgument &TemplateArg,
2159 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002160 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002161 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002162
Douglas Gregore73bb602009-09-14 21:25:05 +00002163/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002164/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00002165static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002166MarkUsedTemplateParameters(Sema &SemaRef,
2167 const Expr *E,
2168 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002169 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002170 llvm::SmallVectorImpl<bool> &Used) {
2171 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2172 // find other occurrences of template parameters.
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002173 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregor031a5882009-06-13 00:26:55 +00002174 if (!E)
2175 return;
2176
Mike Stump1eb44332009-09-09 15:08:12 +00002177 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00002178 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2179 if (!NTTP)
2180 return;
2181
Douglas Gregored9c0f92009-10-29 00:04:11 +00002182 if (NTTP->getDepth() == Depth)
2183 Used[NTTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002184}
2185
Douglas Gregore73bb602009-09-14 21:25:05 +00002186/// \brief Mark the template parameters that are used by the given
2187/// nested name specifier.
2188static void
2189MarkUsedTemplateParameters(Sema &SemaRef,
2190 NestedNameSpecifier *NNS,
2191 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002192 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002193 llvm::SmallVectorImpl<bool> &Used) {
2194 if (!NNS)
2195 return;
2196
Douglas Gregored9c0f92009-10-29 00:04:11 +00002197 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2198 Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002199 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002200 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002201}
2202
2203/// \brief Mark the template parameters that are used by the given
2204/// template name.
2205static void
2206MarkUsedTemplateParameters(Sema &SemaRef,
2207 TemplateName Name,
2208 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002209 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002210 llvm::SmallVectorImpl<bool> &Used) {
2211 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2212 if (TemplateTemplateParmDecl *TTP
Douglas Gregored9c0f92009-10-29 00:04:11 +00002213 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2214 if (TTP->getDepth() == Depth)
2215 Used[TTP->getIndex()] = true;
2216 }
Douglas Gregore73bb602009-09-14 21:25:05 +00002217 return;
2218 }
2219
Douglas Gregor788cd062009-11-11 01:00:40 +00002220 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2221 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2222 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002223 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
Douglas Gregored9c0f92009-10-29 00:04:11 +00002224 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2225 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002226}
2227
2228/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002229/// type.
Mike Stump1eb44332009-09-09 15:08:12 +00002230static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002231MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2232 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002233 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002234 llvm::SmallVectorImpl<bool> &Used) {
2235 if (T.isNull())
2236 return;
2237
Douglas Gregor031a5882009-06-13 00:26:55 +00002238 // Non-dependent types have nothing deducible
2239 if (!T->isDependentType())
2240 return;
2241
2242 T = SemaRef.Context.getCanonicalType(T);
2243 switch (T->getTypeClass()) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002244 case Type::Pointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002245 MarkUsedTemplateParameters(SemaRef,
2246 cast<PointerType>(T)->getPointeeType(),
2247 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002248 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002249 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002250 break;
2251
2252 case Type::BlockPointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002253 MarkUsedTemplateParameters(SemaRef,
2254 cast<BlockPointerType>(T)->getPointeeType(),
2255 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002256 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002257 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002258 break;
2259
2260 case Type::LValueReference:
2261 case Type::RValueReference:
Douglas Gregore73bb602009-09-14 21:25:05 +00002262 MarkUsedTemplateParameters(SemaRef,
2263 cast<ReferenceType>(T)->getPointeeType(),
2264 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002265 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002266 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002267 break;
2268
2269 case Type::MemberPointer: {
2270 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore73bb602009-09-14 21:25:05 +00002271 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002272 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002273 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002274 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002275 break;
2276 }
2277
2278 case Type::DependentSizedArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002279 MarkUsedTemplateParameters(SemaRef,
2280 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002281 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002282 // Fall through to check the element type
2283
2284 case Type::ConstantArray:
2285 case Type::IncompleteArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002286 MarkUsedTemplateParameters(SemaRef,
2287 cast<ArrayType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002288 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002289 break;
2290
2291 case Type::Vector:
2292 case Type::ExtVector:
Douglas Gregore73bb602009-09-14 21:25:05 +00002293 MarkUsedTemplateParameters(SemaRef,
2294 cast<VectorType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002295 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002296 break;
2297
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002298 case Type::DependentSizedExtVector: {
2299 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002300 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002301 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002302 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002303 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002304 Depth, Used);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002305 break;
2306 }
2307
Douglas Gregor031a5882009-06-13 00:26:55 +00002308 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002309 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002310 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002311 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002312 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore73bb602009-09-14 21:25:05 +00002313 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002314 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002315 break;
2316 }
2317
Douglas Gregored9c0f92009-10-29 00:04:11 +00002318 case Type::TemplateTypeParm: {
2319 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2320 if (TTP->getDepth() == Depth)
2321 Used[TTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002322 break;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002323 }
Douglas Gregor031a5882009-06-13 00:26:55 +00002324
2325 case Type::TemplateSpecialization: {
Mike Stump1eb44332009-09-09 15:08:12 +00002326 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002327 = cast<TemplateSpecializationType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002328 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002329 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002330 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002331 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2332 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002333 break;
2334 }
2335
Douglas Gregore73bb602009-09-14 21:25:05 +00002336 case Type::Complex:
2337 if (!OnlyDeduced)
2338 MarkUsedTemplateParameters(SemaRef,
2339 cast<ComplexType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002340 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002341 break;
2342
2343 case Type::Typename:
2344 if (!OnlyDeduced)
2345 MarkUsedTemplateParameters(SemaRef,
2346 cast<TypenameType>(T)->getQualifier(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002347 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002348 break;
2349
2350 // None of these types have any template parameters in them.
Douglas Gregor031a5882009-06-13 00:26:55 +00002351 case Type::Builtin:
Douglas Gregor031a5882009-06-13 00:26:55 +00002352 case Type::VariableArray:
2353 case Type::FunctionNoProto:
2354 case Type::Record:
2355 case Type::Enum:
Douglas Gregor031a5882009-06-13 00:26:55 +00002356 case Type::ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002357 case Type::ObjCObjectPointer:
John McCalled976492009-12-04 22:46:56 +00002358 case Type::UnresolvedUsing:
Douglas Gregor031a5882009-06-13 00:26:55 +00002359#define TYPE(Class, Base)
2360#define ABSTRACT_TYPE(Class, Base)
2361#define DEPENDENT_TYPE(Class, Base)
2362#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2363#include "clang/AST/TypeNodes.def"
2364 break;
2365 }
2366}
2367
Douglas Gregore73bb602009-09-14 21:25:05 +00002368/// \brief Mark the template parameters that are used by this
Douglas Gregor031a5882009-06-13 00:26:55 +00002369/// template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00002370static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002371MarkUsedTemplateParameters(Sema &SemaRef,
2372 const TemplateArgument &TemplateArg,
2373 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002374 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002375 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002376 switch (TemplateArg.getKind()) {
2377 case TemplateArgument::Null:
2378 case TemplateArgument::Integral:
Douglas Gregor788cd062009-11-11 01:00:40 +00002379 case TemplateArgument::Declaration:
Douglas Gregor031a5882009-06-13 00:26:55 +00002380 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002381
Douglas Gregor031a5882009-06-13 00:26:55 +00002382 case TemplateArgument::Type:
Douglas Gregore73bb602009-09-14 21:25:05 +00002383 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002384 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002385 break;
2386
Douglas Gregor788cd062009-11-11 01:00:40 +00002387 case TemplateArgument::Template:
2388 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2389 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002390 break;
2391
2392 case TemplateArgument::Expression:
Douglas Gregore73bb602009-09-14 21:25:05 +00002393 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002394 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002395 break;
Douglas Gregore73bb602009-09-14 21:25:05 +00002396
Anders Carlssond01b1da2009-06-15 17:04:53 +00002397 case TemplateArgument::Pack:
Douglas Gregore73bb602009-09-14 21:25:05 +00002398 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2399 PEnd = TemplateArg.pack_end();
2400 P != PEnd; ++P)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002401 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssond01b1da2009-06-15 17:04:53 +00002402 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00002403 }
2404}
2405
2406/// \brief Mark the template parameters can be deduced by the given
2407/// template argument list.
2408///
2409/// \param TemplateArgs the template argument list from which template
2410/// parameters will be deduced.
2411///
2412/// \param Deduced a bit vector whose elements will be set to \c true
2413/// to indicate when the corresponding template parameter will be
2414/// deduced.
Mike Stump1eb44332009-09-09 15:08:12 +00002415void
Douglas Gregore73bb602009-09-14 21:25:05 +00002416Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002417 bool OnlyDeduced, unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002418 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002419 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002420 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2421 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002422}
Douglas Gregor63f07c52009-09-18 23:21:38 +00002423
2424/// \brief Marks all of the template parameters that will be deduced by a
2425/// call to the given function template.
2426void Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
2427 llvm::SmallVectorImpl<bool> &Deduced) {
2428 TemplateParameterList *TemplateParams
2429 = FunctionTemplate->getTemplateParameters();
2430 Deduced.clear();
2431 Deduced.resize(TemplateParams->size());
2432
2433 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2434 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
2435 ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002436 true, TemplateParams->getDepth(), Deduced);
Douglas Gregor63f07c52009-09-18 23:21:38 +00002437}