blob: 326519d3a3781e3f338506542b65d3f3839cff7b [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
Chandler Carrutha7ef1302010-02-07 21:33:28 +000052DeduceTemplateArguments(Sema &S,
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
Chandler Carrutha7ef1302010-02-07 21:33:28 +000075DeduceNonTypeTemplateArgument(Sema &S,
Mike Stump1eb44332009-09-09 15:08:12 +000076 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!
Chandler Carrutha7ef1302010-02-07 21:33:28 +000087 unsigned AllowedBits = S.Context.getTypeSize(T);
Anders Carlsson25af1ed2009-06-16 23:08:29 +000088 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
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000129DeduceNonTypeTemplateArgument(Sema &S,
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;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000155 Deduced[NTTP->getIndex()].getAsExpr()->Profile(ID1, S.Context, true);
156 Value->Profile(ID2, S.Context, true);
Douglas Gregor9eea08b2009-09-15 16:51:42 +0000157 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
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000172DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregor15755cb2009-11-13 23:45:44 +0000173 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
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000205DeduceTemplateArguments(Sema &S,
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.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000224 ExistingArg = TemplateArgument(S.Context.getCanonicalTemplateName(Arg));
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000225 return Sema::TDK_Success;
226 }
227
228 // Verify that the previous binding matches this deduction.
229 assert(ExistingArg.getKind() == TemplateArgument::Template);
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000230 if (S.Context.hasSameTemplateName(ExistingArg.getAsTemplate(), Arg))
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000231 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.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000241 if (S.Context.hasSameTemplateName(Param, Arg))
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000242 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///
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000253/// \param S the Sema
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000254///
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
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000269DeduceTemplateArguments(Sema &S,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000270 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
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000282 = DeduceTemplateArguments(S, 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
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000294 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000295 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
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000317 = DeduceTemplateArguments(S,
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
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000331 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000332 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///
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000343/// \param S the semantic analysis object within which we are deducing
Douglas Gregor500d3312009-06-26 18:27:22 +0000344///
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
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000362DeduceTemplateArguments(Sema &S,
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.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000370 QualType Param = S.Context.getCanonicalType(ParamIn);
371 QualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000372
Douglas Gregor500d3312009-06-26 18:27:22 +0000373 // C++0x [temp.deduct.call]p4 bullet 1:
374 // - If the original P is a reference type, the deduced A (i.e., the type
Mike Stump1eb44332009-09-09 15:08:12 +0000375 // referred to by the reference) can be more cv-qualified than the
Douglas Gregor500d3312009-06-26 18:27:22 +0000376 // transformed A.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000377 if (TDF & TDF_ParamWithReferenceType) {
Chandler Carruthe7242462009-12-30 04:10:01 +0000378 Qualifiers Quals;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000379 QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals);
Chandler Carruthe7242462009-12-30 04:10:01 +0000380 Quals.setCVRQualifiers(Quals.getCVRQualifiers() &
381 Arg.getCVRQualifiersThroughArrayTypes());
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000382 Param = S.Context.getQualifiedType(UnqualParam, Quals);
Douglas Gregor500d3312009-06-26 18:27:22 +0000383 }
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000385 // If the parameter type is not dependent, there is nothing to deduce.
Douglas Gregor12820292009-09-14 20:00:47 +0000386 if (!Param->isDependentType()) {
387 if (!(TDF & TDF_SkipNonDependent) && Param != Arg) {
388
389 return Sema::TDK_NonDeducedMismatch;
390 }
391
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000392 return Sema::TDK_Success;
Douglas Gregor12820292009-09-14 20:00:47 +0000393 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000394
Douglas Gregor199d9912009-06-05 00:53:49 +0000395 // C++ [temp.deduct.type]p9:
Mike Stump1eb44332009-09-09 15:08:12 +0000396 // A template type argument T, a template template argument TT or a
397 // template non-type argument i can be deduced if P and A have one of
Douglas Gregor199d9912009-06-05 00:53:49 +0000398 // the following forms:
399 //
400 // T
401 // cv-list T
Mike Stump1eb44332009-09-09 15:08:12 +0000402 if (const TemplateTypeParmType *TemplateTypeParm
John McCall183700f2009-09-21 23:43:11 +0000403 = Param->getAs<TemplateTypeParmType>()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000404 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000405 bool RecanonicalizeArg = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Douglas Gregor9e9fae42009-07-22 20:02:25 +0000407 // If the argument type is an array type, move the qualifiers up to the
408 // top level, so they can be matched with the qualifiers on the parameter.
409 // FIXME: address spaces, ObjC GC qualifiers
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000410 if (isa<ArrayType>(Arg)) {
John McCall0953e762009-09-24 19:53:00 +0000411 Qualifiers Quals;
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000412 Arg = S.Context.getUnqualifiedArrayType(Arg, Quals);
John McCall0953e762009-09-24 19:53:00 +0000413 if (Quals) {
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000414 Arg = S.Context.getQualifiedType(Arg, Quals);
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000415 RecanonicalizeArg = true;
416 }
417 }
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000419 // The argument type can not be less qualified than the parameter
420 // type.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000421 if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000422 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
423 Info.FirstArg = Deduced[Index];
John McCall833ca992009-10-29 08:12:44 +0000424 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000425 return Sema::TDK_InconsistentQuals;
426 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000427
428 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000429 assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function");
John McCall0953e762009-09-24 19:53:00 +0000430 QualType DeducedType = Arg;
431 DeducedType.removeCVRQualifiers(Param.getCVRQualifiers());
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000432 if (RecanonicalizeArg)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000433 DeducedType = S.Context.getCanonicalType(DeducedType);
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000435 if (Deduced[Index].isNull())
John McCall833ca992009-10-29 08:12:44 +0000436 Deduced[Index] = TemplateArgument(DeducedType);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000437 else {
Mike Stump1eb44332009-09-09 15:08:12 +0000438 // C++ [temp.deduct.type]p2:
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000439 // [...] If type deduction cannot be done for any P/A pair, or if for
Mike Stump1eb44332009-09-09 15:08:12 +0000440 // any pair the deduction leads to more than one possible set of
441 // deduced values, or if different pairs yield different deduced
442 // values, or if any template argument remains neither deduced nor
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000443 // explicitly specified, template argument deduction fails.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000444 if (Deduced[Index].getAsType() != DeducedType) {
Mike Stump1eb44332009-09-09 15:08:12 +0000445 Info.Param
Douglas Gregorf67875d2009-06-12 18:26:56 +0000446 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
447 Info.FirstArg = Deduced[Index];
John McCall833ca992009-10-29 08:12:44 +0000448 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000449 return Sema::TDK_Inconsistent;
450 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000451 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000452 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000453 }
454
Douglas Gregorf67875d2009-06-12 18:26:56 +0000455 // Set up the template argument deduction information for a failure.
John McCall833ca992009-10-29 08:12:44 +0000456 Info.FirstArg = TemplateArgument(ParamIn);
457 Info.SecondArg = TemplateArgument(ArgIn);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000458
Douglas Gregor508f1c82009-06-26 23:10:12 +0000459 // Check the cv-qualifiers on the parameter and argument types.
460 if (!(TDF & TDF_IgnoreQualifiers)) {
461 if (TDF & TDF_ParamWithReferenceType) {
462 if (Param.isMoreQualifiedThan(Arg))
463 return Sema::TDK_NonDeducedMismatch;
464 } else {
465 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Mike Stump1eb44332009-09-09 15:08:12 +0000466 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor508f1c82009-06-26 23:10:12 +0000467 }
468 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000469
Douglas Gregord560d502009-06-04 00:21:18 +0000470 switch (Param->getTypeClass()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000471 // No deduction possible for these types
472 case Type::Builtin:
Douglas Gregorf67875d2009-06-12 18:26:56 +0000473 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Douglas Gregor199d9912009-06-05 00:53:49 +0000475 // T *
Douglas Gregord560d502009-06-04 00:21:18 +0000476 case Type::Pointer: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000477 const PointerType *PointerArg = Arg->getAs<PointerType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000478 if (!PointerArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000479 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Douglas Gregor41128772009-06-26 23:27:24 +0000481 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000482 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000483 cast<PointerType>(Param)->getPointeeType(),
484 PointerArg->getPointeeType(),
Douglas Gregor41128772009-06-26 23:27:24 +0000485 Info, Deduced, SubTDF);
Douglas Gregord560d502009-06-04 00:21:18 +0000486 }
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Douglas Gregor199d9912009-06-05 00:53:49 +0000488 // T &
Douglas Gregord560d502009-06-04 00:21:18 +0000489 case Type::LValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000490 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000491 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000492 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000494 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000495 cast<LValueReferenceType>(Param)->getPointeeType(),
496 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000497 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000498 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000499
Douglas Gregor199d9912009-06-05 00:53:49 +0000500 // T && [C++0x]
Douglas Gregord560d502009-06-04 00:21:18 +0000501 case Type::RValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000502 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000503 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000504 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000506 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000507 cast<RValueReferenceType>(Param)->getPointeeType(),
508 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000509 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000510 }
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Douglas Gregor199d9912009-06-05 00:53:49 +0000512 // T [] (implied, but not stated explicitly)
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000513 case Type::IncompleteArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000514 const IncompleteArrayType *IncompleteArrayArg =
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000515 S.Context.getAsIncompleteArrayType(Arg);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000516 if (!IncompleteArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000517 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000519 return DeduceTemplateArguments(S, TemplateParams,
520 S.Context.getAsIncompleteArrayType(Param)->getElementType(),
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000521 IncompleteArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000522 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000523 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000524
525 // T [integer-constant]
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000526 case Type::ConstantArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000527 const ConstantArrayType *ConstantArrayArg =
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000528 S.Context.getAsConstantArrayType(Arg);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000529 if (!ConstantArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000530 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000531
532 const ConstantArrayType *ConstantArrayParm =
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000533 S.Context.getAsConstantArrayType(Param);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000534 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000535 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000537 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000538 ConstantArrayParm->getElementType(),
539 ConstantArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000540 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000541 }
542
Douglas Gregor199d9912009-06-05 00:53:49 +0000543 // type [i]
544 case Type::DependentSizedArray: {
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000545 const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
Douglas Gregor199d9912009-06-05 00:53:49 +0000546 if (!ArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000547 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Douglas Gregor199d9912009-06-05 00:53:49 +0000549 // Check the element type of the arrays
550 const DependentSizedArrayType *DependentArrayParm
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000551 = S.Context.getAsDependentSizedArrayType(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000552 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000553 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000554 DependentArrayParm->getElementType(),
555 ArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000556 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000557 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Douglas Gregor199d9912009-06-05 00:53:49 +0000559 // Determine the array bound is something we can deduce.
Mike Stump1eb44332009-09-09 15:08:12 +0000560 NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000561 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
562 if (!NTTP)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000563 return Sema::TDK_Success;
Mike Stump1eb44332009-09-09 15:08:12 +0000564
565 // We can perform template argument deduction for the given non-type
Douglas Gregor199d9912009-06-05 00:53:49 +0000566 // template parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000567 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000568 "Cannot deduce non-type template argument at depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000569 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson335e24a2009-06-16 22:44:31 +0000570 = dyn_cast<ConstantArrayType>(ArrayArg)) {
571 llvm::APSInt Size(ConstantArrayArg->getSize());
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000572 return DeduceNonTypeTemplateArgument(S, NTTP, Size,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000573 Info, Deduced);
Anders Carlsson335e24a2009-06-16 22:44:31 +0000574 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000575 if (const DependentSizedArrayType *DependentArrayArg
576 = dyn_cast<DependentSizedArrayType>(ArrayArg))
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000577 return DeduceNonTypeTemplateArgument(S, NTTP,
Douglas Gregor199d9912009-06-05 00:53:49 +0000578 DependentArrayArg->getSizeExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000579 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Douglas Gregor199d9912009-06-05 00:53:49 +0000581 // Incomplete type does not match a dependently-sized array type
Douglas Gregorf67875d2009-06-12 18:26:56 +0000582 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
585 // type(*)(T)
586 // T(*)()
587 // T(*)(T)
Anders Carlssona27fad52009-06-08 15:19:08 +0000588 case Type::FunctionProto: {
Mike Stump1eb44332009-09-09 15:08:12 +0000589 const FunctionProtoType *FunctionProtoArg =
Anders Carlssona27fad52009-06-08 15:19:08 +0000590 dyn_cast<FunctionProtoType>(Arg);
591 if (!FunctionProtoArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000592 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000593
594 const FunctionProtoType *FunctionProtoParam =
Anders Carlssona27fad52009-06-08 15:19:08 +0000595 cast<FunctionProtoType>(Param);
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000596
Mike Stump1eb44332009-09-09 15:08:12 +0000597 if (FunctionProtoParam->getTypeQuals() !=
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000598 FunctionProtoArg->getTypeQuals())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000599 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000601 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000602 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000604 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000605 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000606
Anders Carlssona27fad52009-06-08 15:19:08 +0000607 // Check return types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000608 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000609 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000610 FunctionProtoParam->getResultType(),
611 FunctionProtoArg->getResultType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000612 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000613 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Anders Carlssona27fad52009-06-08 15:19:08 +0000615 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
616 // Check argument types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000617 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000618 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000619 FunctionProtoParam->getArgType(I),
620 FunctionProtoArg->getArgType(I),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000621 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000622 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000623 }
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Douglas Gregorf67875d2009-06-12 18:26:56 +0000625 return Sema::TDK_Success;
Anders Carlssona27fad52009-06-08 15:19:08 +0000626 }
Mike Stump1eb44332009-09-09 15:08:12 +0000627
John McCall3cb0ebd2010-03-10 03:28:59 +0000628 case Type::InjectedClassName: {
629 // Treat a template's injected-class-name as if the template
630 // specialization type had been used.
631 Param = cast<InjectedClassNameType>(Param)->getUnderlyingType();
632 assert(isa<TemplateSpecializationType>(Param) &&
633 "injected class name is not a template specialization type");
634 // fall through
635 }
636
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000637 // template-name<T> (where template-name refers to a class template)
Douglas Gregord708c722009-06-09 16:35:58 +0000638 // template-name<i>
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000639 // TT<T>
640 // TT<i>
641 // TT<>
Douglas Gregord708c722009-06-09 16:35:58 +0000642 case Type::TemplateSpecialization: {
643 const TemplateSpecializationType *SpecParam
644 = cast<TemplateSpecializationType>(Param);
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000646 // Try to deduce template arguments from the template-id.
647 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000648 = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000649 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Douglas Gregor4a5c15f2009-09-30 22:13:51 +0000651 if (Result && (TDF & TDF_DerivedClass)) {
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000652 // C++ [temp.deduct.call]p3b3:
653 // If P is a class, and P has the form template-id, then A can be a
654 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump1eb44332009-09-09 15:08:12 +0000655 // class of the form template-id, A can be a pointer to a derived
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000656 // class pointed to by the deduced A.
657 //
658 // More importantly:
Mike Stump1eb44332009-09-09 15:08:12 +0000659 // These alternatives are considered only if type deduction would
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000660 // otherwise fail.
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000661 if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
662 // We cannot inspect base classes as part of deduction when the type
663 // is incomplete, so either instantiate any templates necessary to
664 // complete the type, or skip over it if it cannot be completed.
John McCall5769d612010-02-08 23:07:23 +0000665 if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000666 return Result;
667
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000668 // Use data recursion to crawl through the list of base classes.
Mike Stump1eb44332009-09-09 15:08:12 +0000669 // Visited contains the set of nodes we have already visited, while
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000670 // ToVisit is our stack of records that we still need to visit.
671 llvm::SmallPtrSet<const RecordType *, 8> Visited;
672 llvm::SmallVector<const RecordType *, 8> ToVisit;
673 ToVisit.push_back(RecordT);
674 bool Successful = false;
675 while (!ToVisit.empty()) {
676 // Retrieve the next class in the inheritance hierarchy.
677 const RecordType *NextT = ToVisit.back();
678 ToVisit.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000680 // If we have already seen this type, skip it.
681 if (!Visited.insert(NextT))
682 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000684 // If this is a base class, try to perform template argument
685 // deduction from it.
686 if (NextT != RecordT) {
687 Sema::TemplateDeductionResult BaseResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000688 = DeduceTemplateArguments(S, TemplateParams, SpecParam,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000689 QualType(NextT, 0), Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000691 // If template argument deduction for this base was successful,
692 // note that we had some success.
693 if (BaseResult == Sema::TDK_Success)
694 Successful = true;
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000695 }
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000697 // Visit base classes
698 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
699 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
700 BaseEnd = Next->bases_end();
Sebastian Redl9994a342009-10-25 17:03:50 +0000701 Base != BaseEnd; ++Base) {
Mike Stump1eb44332009-09-09 15:08:12 +0000702 assert(Base->getType()->isRecordType() &&
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000703 "Base class that isn't a record?");
Ted Kremenek6217b802009-07-29 21:53:49 +0000704 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000705 }
706 }
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000708 if (Successful)
709 return Sema::TDK_Success;
710 }
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000712 }
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000714 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000715 }
716
Douglas Gregor637a4092009-06-10 23:47:09 +0000717 // T type::*
718 // T T::*
719 // T (type::*)()
720 // type (T::*)()
721 // type (type::*)(T)
722 // type (T::*)(T)
723 // T (type::*)(T)
724 // T (T::*)()
725 // T (T::*)(T)
726 case Type::MemberPointer: {
727 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
728 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
729 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000730 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +0000731
Douglas Gregorf67875d2009-06-12 18:26:56 +0000732 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000733 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000734 MemPtrParam->getPointeeType(),
735 MemPtrArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000736 Info, Deduced,
737 TDF & TDF_IgnoreQualifiers))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000738 return Result;
739
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000740 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000741 QualType(MemPtrParam->getClass(), 0),
742 QualType(MemPtrArg->getClass(), 0),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000743 Info, Deduced, 0);
Douglas Gregor637a4092009-06-10 23:47:09 +0000744 }
745
Anders Carlsson9a917e42009-06-12 22:56:54 +0000746 // (clang extension)
747 //
Mike Stump1eb44332009-09-09 15:08:12 +0000748 // type(^)(T)
749 // T(^)()
750 // T(^)(T)
Anders Carlsson859ba502009-06-12 16:23:10 +0000751 case Type::BlockPointer: {
752 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
753 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Anders Carlsson859ba502009-06-12 16:23:10 +0000755 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000756 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000758 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +0000759 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000760 BlockPtrArg->getPointeeType(), Info,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000761 Deduced, 0);
Anders Carlsson859ba502009-06-12 16:23:10 +0000762 }
763
Douglas Gregor637a4092009-06-10 23:47:09 +0000764 case Type::TypeOfExpr:
765 case Type::TypeOf:
766 case Type::Typename:
767 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +0000768 return Sema::TDK_Success;
Douglas Gregor637a4092009-06-10 23:47:09 +0000769
Douglas Gregord560d502009-06-04 00:21:18 +0000770 default:
771 break;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000772 }
773
774 // FIXME: Many more cases to go (to go).
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000775 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000776}
777
Douglas Gregorf67875d2009-06-12 18:26:56 +0000778static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000779DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000780 TemplateParameterList *TemplateParams,
781 const TemplateArgument &Param,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000782 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000783 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000784 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000785 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000786 case TemplateArgument::Null:
787 assert(false && "Null template argument in parameter list");
788 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000789
790 case TemplateArgument::Type:
Douglas Gregor788cd062009-11-11 01:00:40 +0000791 if (Arg.getKind() == TemplateArgument::Type)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000792 return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
Douglas Gregor788cd062009-11-11 01:00:40 +0000793 Arg.getAsType(), Info, Deduced, 0);
794 Info.FirstArg = Param;
795 Info.SecondArg = Arg;
796 return Sema::TDK_NonDeducedMismatch;
797
798 case TemplateArgument::Template:
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000799 if (Arg.getKind() == TemplateArgument::Template)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000800 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor788cd062009-11-11 01:00:40 +0000801 Param.getAsTemplate(),
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000802 Arg.getAsTemplate(), Info, Deduced);
Douglas Gregor788cd062009-11-11 01:00:40 +0000803 Info.FirstArg = Param;
804 Info.SecondArg = Arg;
805 return Sema::TDK_NonDeducedMismatch;
806
Douglas Gregor199d9912009-06-05 00:53:49 +0000807 case TemplateArgument::Declaration:
Douglas Gregor788cd062009-11-11 01:00:40 +0000808 if (Arg.getKind() == TemplateArgument::Declaration &&
809 Param.getAsDecl()->getCanonicalDecl() ==
810 Arg.getAsDecl()->getCanonicalDecl())
811 return Sema::TDK_Success;
812
Douglas Gregorf67875d2009-06-12 18:26:56 +0000813 Info.FirstArg = Param;
814 Info.SecondArg = Arg;
815 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Douglas Gregor199d9912009-06-05 00:53:49 +0000817 case TemplateArgument::Integral:
818 if (Arg.getKind() == TemplateArgument::Integral) {
819 // FIXME: Zero extension + sign checking here?
Douglas Gregorf67875d2009-06-12 18:26:56 +0000820 if (*Param.getAsIntegral() == *Arg.getAsIntegral())
821 return Sema::TDK_Success;
822
823 Info.FirstArg = Param;
824 Info.SecondArg = Arg;
825 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000826 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000827
828 if (Arg.getKind() == TemplateArgument::Expression) {
829 Info.FirstArg = Param;
830 Info.SecondArg = Arg;
831 return Sema::TDK_NonDeducedMismatch;
832 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000833
834 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000835 Info.FirstArg = Param;
836 Info.SecondArg = Arg;
837 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Douglas Gregor199d9912009-06-05 00:53:49 +0000839 case TemplateArgument::Expression: {
Mike Stump1eb44332009-09-09 15:08:12 +0000840 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000841 = getDeducedParameterFromExpr(Param.getAsExpr())) {
842 if (Arg.getKind() == TemplateArgument::Integral)
843 // FIXME: Sign problems here
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000844 return DeduceNonTypeTemplateArgument(S, NTTP,
Mike Stump1eb44332009-09-09 15:08:12 +0000845 *Arg.getAsIntegral(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000846 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000847 if (Arg.getKind() == TemplateArgument::Expression)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000848 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000849 Info, Deduced);
Douglas Gregor15755cb2009-11-13 23:45:44 +0000850 if (Arg.getKind() == TemplateArgument::Declaration)
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000851 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
Douglas Gregor15755cb2009-11-13 23:45:44 +0000852 Info, Deduced);
853
Douglas Gregor199d9912009-06-05 00:53:49 +0000854 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000855 Info.FirstArg = Param;
856 Info.SecondArg = Arg;
857 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000858 }
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Douglas Gregor199d9912009-06-05 00:53:49 +0000860 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000861 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000862 }
Anders Carlssond01b1da2009-06-15 17:04:53 +0000863 case TemplateArgument::Pack:
864 assert(0 && "FIXME: Implement!");
865 break;
Douglas Gregor199d9912009-06-05 00:53:49 +0000866 }
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Douglas Gregorf67875d2009-06-12 18:26:56 +0000868 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000869}
870
Mike Stump1eb44332009-09-09 15:08:12 +0000871static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000872DeduceTemplateArguments(Sema &S,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000873 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000874 const TemplateArgumentList &ParamList,
875 const TemplateArgumentList &ArgList,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000876 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000877 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
878 assert(ParamList.size() == ArgList.size());
879 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000880 if (Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000881 = DeduceTemplateArguments(S, TemplateParams,
Mike Stump1eb44332009-09-09 15:08:12 +0000882 ParamList[I], ArgList[I],
Douglas Gregorf67875d2009-06-12 18:26:56 +0000883 Info, Deduced))
884 return Result;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000885 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000886 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000887}
888
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000889/// \brief Determine whether two template arguments are the same.
Mike Stump1eb44332009-09-09 15:08:12 +0000890static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000891 const TemplateArgument &X,
892 const TemplateArgument &Y) {
893 if (X.getKind() != Y.getKind())
894 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000896 switch (X.getKind()) {
897 case TemplateArgument::Null:
898 assert(false && "Comparing NULL template argument");
899 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000901 case TemplateArgument::Type:
902 return Context.getCanonicalType(X.getAsType()) ==
903 Context.getCanonicalType(Y.getAsType());
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000905 case TemplateArgument::Declaration:
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000906 return X.getAsDecl()->getCanonicalDecl() ==
907 Y.getAsDecl()->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Douglas Gregor788cd062009-11-11 01:00:40 +0000909 case TemplateArgument::Template:
910 return Context.getCanonicalTemplateName(X.getAsTemplate())
911 .getAsVoidPointer() ==
912 Context.getCanonicalTemplateName(Y.getAsTemplate())
913 .getAsVoidPointer();
914
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000915 case TemplateArgument::Integral:
916 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Douglas Gregor788cd062009-11-11 01:00:40 +0000918 case TemplateArgument::Expression: {
919 llvm::FoldingSetNodeID XID, YID;
920 X.getAsExpr()->Profile(XID, Context, true);
921 Y.getAsExpr()->Profile(YID, Context, true);
922 return XID == YID;
923 }
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000925 case TemplateArgument::Pack:
926 if (X.pack_size() != Y.pack_size())
927 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000928
929 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
930 XPEnd = X.pack_end(),
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000931 YP = Y.pack_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000932 XP != XPEnd; ++XP, ++YP)
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000933 if (!isSameTemplateArg(Context, *XP, *YP))
934 return false;
935
936 return true;
937 }
938
939 return false;
940}
941
942/// \brief Helper function to build a TemplateParameter when we don't
943/// know its type statically.
944static TemplateParameter makeTemplateParameter(Decl *D) {
945 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
946 return TemplateParameter(TTP);
947 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
948 return TemplateParameter(NTTP);
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000950 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
951}
952
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000953/// \brief Perform template argument deduction to determine whether
954/// the given template arguments match the given class template
955/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +0000956Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000957Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000958 const TemplateArgumentList &TemplateArgs,
959 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000960 // C++ [temp.class.spec.match]p2:
961 // A partial specialization matches a given actual template
962 // argument list if the template arguments of the partial
963 // specialization can be deduced from the actual template argument
964 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +0000965 SFINAETrap Trap(*this);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000966 llvm::SmallVector<TemplateArgument, 4> Deduced;
967 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000968 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +0000969 = ::DeduceTemplateArguments(*this,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000970 Partial->getTemplateParameters(),
Mike Stump1eb44332009-09-09 15:08:12 +0000971 Partial->getTemplateArgs(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000972 TemplateArgs, Info, Deduced))
973 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +0000974
Douglas Gregor637a4092009-06-10 23:47:09 +0000975 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
976 Deduced.data(), Deduced.size());
977 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000978 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +0000979
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000980 // C++ [temp.deduct.type]p2:
981 // [...] or if any template argument remains neither deduced nor
982 // explicitly specified, template argument deduction fails.
Anders Carlssonfb250522009-06-23 01:26:57 +0000983 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
984 Deduced.size());
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000985 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000986 if (Deduced[I].isNull()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000987 Decl *Param
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000988 = const_cast<NamedDecl *>(
989 Partial->getTemplateParameters()->getParam(I));
Douglas Gregorf67875d2009-06-12 18:26:56 +0000990 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
991 Info.Param = TTP;
Mike Stump1eb44332009-09-09 15:08:12 +0000992 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorf67875d2009-06-12 18:26:56 +0000993 = dyn_cast<NonTypeTemplateParmDecl>(Param))
994 Info.Param = NTTP;
995 else
996 Info.Param = cast<TemplateTemplateParmDecl>(Param);
997 return TDK_Incomplete;
998 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +0000999
Anders Carlssonfb250522009-06-23 01:26:57 +00001000 Builder.Append(Deduced[I]);
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001001 }
1002
1003 // Form the template argument list from the deduced template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001004 TemplateArgumentList *DeducedArgumentList
Anders Carlssonfb250522009-06-23 01:26:57 +00001005 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
Douglas Gregorf67875d2009-06-12 18:26:56 +00001006 Info.reset(DeducedArgumentList);
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001007
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001008 // Substitute the deduced template arguments into the template
1009 // arguments of the class template partial specialization, and
1010 // verify that the instantiated template arguments are both valid
1011 // and are equivalent to the template arguments originally provided
Mike Stump1eb44332009-09-09 15:08:12 +00001012 // to the class template.
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001013 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
John McCall833ca992009-10-29 08:12:44 +00001014 const TemplateArgumentLoc *PartialTemplateArgs
1015 = Partial->getTemplateArgsAsWritten();
1016 unsigned N = Partial->getNumTemplateArgsAsWritten();
John McCalld5532b62009-11-23 01:53:49 +00001017
1018 // Note that we don't provide the langle and rangle locations.
1019 TemplateArgumentListInfo InstArgs;
1020
John McCall833ca992009-10-29 08:12:44 +00001021 for (unsigned I = 0; I != N; ++I) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001022 Decl *Param = const_cast<NamedDecl *>(
Douglas Gregorc9e5d252009-06-13 00:59:32 +00001023 ClassTemplate->getTemplateParameters()->getParam(I));
John McCalld5532b62009-11-23 01:53:49 +00001024 TemplateArgumentLoc InstArg;
1025 if (Subst(PartialTemplateArgs[I], InstArg,
John McCall833ca992009-10-29 08:12:44 +00001026 MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001027 Info.Param = makeTemplateParameter(Param);
John McCall833ca992009-10-29 08:12:44 +00001028 Info.FirstArg = PartialTemplateArgs[I].getArgument();
Mike Stump1eb44332009-09-09 15:08:12 +00001029 return TDK_SubstitutionFailure;
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001030 }
John McCalld5532b62009-11-23 01:53:49 +00001031 InstArgs.addArgument(InstArg);
John McCall833ca992009-10-29 08:12:44 +00001032 }
1033
1034 TemplateArgumentListBuilder ConvertedInstArgs(
1035 ClassTemplate->getTemplateParameters(), N);
1036
1037 if (CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001038 InstArgs, false, ConvertedInstArgs)) {
John McCall833ca992009-10-29 08:12:44 +00001039 // FIXME: fail with more useful information?
1040 return TDK_SubstitutionFailure;
1041 }
1042
1043 for (unsigned I = 0, E = ConvertedInstArgs.flatSize(); I != E; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001044 TemplateArgument InstArg = ConvertedInstArgs.getFlatArguments()[I];
John McCall833ca992009-10-29 08:12:44 +00001045
1046 Decl *Param = const_cast<NamedDecl *>(
1047 ClassTemplate->getTemplateParameters()->getParam(I));
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001049 if (InstArg.getKind() == TemplateArgument::Expression) {
Mike Stump1eb44332009-09-09 15:08:12 +00001050 // When the argument is an expression, check the expression result
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001051 // against the actual template parameter to get down to the canonical
1052 // template argument.
1053 Expr *InstExpr = InstArg.getAsExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001054 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001055 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1056 if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
1057 Info.Param = makeTemplateParameter(Param);
John McCall833ca992009-10-29 08:12:44 +00001058 Info.FirstArg = Partial->getTemplateArgs()[I];
Mike Stump1eb44332009-09-09 15:08:12 +00001059 return TDK_SubstitutionFailure;
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001060 }
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001061 }
1062 }
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001064 if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) {
1065 Info.Param = makeTemplateParameter(Param);
1066 Info.FirstArg = TemplateArgs[I];
1067 Info.SecondArg = InstArg;
1068 return TDK_NonDeducedMismatch;
1069 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001070 }
1071
Douglas Gregorbb260412009-06-14 08:02:22 +00001072 if (Trap.hasErrorOccurred())
1073 return TDK_SubstitutionFailure;
1074
Douglas Gregorf67875d2009-06-12 18:26:56 +00001075 return TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001076}
Douglas Gregor031a5882009-06-13 00:26:55 +00001077
Douglas Gregor41128772009-06-26 23:27:24 +00001078/// \brief Determine whether the given type T is a simple-template-id type.
1079static bool isSimpleTemplateIdType(QualType T) {
Mike Stump1eb44332009-09-09 15:08:12 +00001080 if (const TemplateSpecializationType *Spec
John McCall183700f2009-09-21 23:43:11 +00001081 = T->getAs<TemplateSpecializationType>())
Douglas Gregor41128772009-06-26 23:27:24 +00001082 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Douglas Gregor41128772009-06-26 23:27:24 +00001084 return false;
1085}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001086
1087/// \brief Substitute the explicitly-provided template arguments into the
1088/// given function template according to C++ [temp.arg.explicit].
1089///
1090/// \param FunctionTemplate the function template into which the explicit
1091/// template arguments will be substituted.
1092///
Mike Stump1eb44332009-09-09 15:08:12 +00001093/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001094/// arguments.
1095///
Mike Stump1eb44332009-09-09 15:08:12 +00001096/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor83314aa2009-07-08 20:55:45 +00001097/// with the converted and checked explicit template arguments.
1098///
Mike Stump1eb44332009-09-09 15:08:12 +00001099/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor83314aa2009-07-08 20:55:45 +00001100/// parameters.
1101///
1102/// \param FunctionType if non-NULL, the result type of the function template
1103/// will also be instantiated and the pointed-to value will be updated with
1104/// the instantiated function type.
1105///
1106/// \param Info if substitution fails for any reason, this object will be
1107/// populated with more information about the failure.
1108///
1109/// \returns TDK_Success if substitution was successful, or some failure
1110/// condition.
1111Sema::TemplateDeductionResult
1112Sema::SubstituteExplicitTemplateArguments(
1113 FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001114 const TemplateArgumentListInfo &ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001115 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1116 llvm::SmallVectorImpl<QualType> &ParamTypes,
1117 QualType *FunctionType,
1118 TemplateDeductionInfo &Info) {
1119 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1120 TemplateParameterList *TemplateParams
1121 = FunctionTemplate->getTemplateParameters();
1122
John McCalld5532b62009-11-23 01:53:49 +00001123 if (ExplicitTemplateArgs.size() == 0) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001124 // No arguments to substitute; just copy over the parameter types and
1125 // fill in the function type.
1126 for (FunctionDecl::param_iterator P = Function->param_begin(),
1127 PEnd = Function->param_end();
1128 P != PEnd;
1129 ++P)
1130 ParamTypes.push_back((*P)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Douglas Gregor83314aa2009-07-08 20:55:45 +00001132 if (FunctionType)
1133 *FunctionType = Function->getType();
1134 return TDK_Success;
1135 }
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Douglas Gregor83314aa2009-07-08 20:55:45 +00001137 // Substitution of the explicit template arguments into a function template
1138 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001139 SFINAETrap Trap(*this);
1140
Douglas Gregor83314aa2009-07-08 20:55:45 +00001141 // C++ [temp.arg.explicit]p3:
Mike Stump1eb44332009-09-09 15:08:12 +00001142 // Template arguments that are present shall be specified in the
1143 // declaration order of their corresponding template-parameters. The
Douglas Gregor83314aa2009-07-08 20:55:45 +00001144 // template argument list shall not specify more template-arguments than
Mike Stump1eb44332009-09-09 15:08:12 +00001145 // there are corresponding template-parameters.
1146 TemplateArgumentListBuilder Builder(TemplateParams,
John McCalld5532b62009-11-23 01:53:49 +00001147 ExplicitTemplateArgs.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001148
1149 // Enter a new template instantiation context where we check the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001150 // explicitly-specified template arguments against this function template,
1151 // and then substitute them into the function parameter types.
Mike Stump1eb44332009-09-09 15:08:12 +00001152 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001153 FunctionTemplate, Deduced.data(), Deduced.size(),
1154 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution);
1155 if (Inst)
1156 return TDK_InstantiationDepth;
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Douglas Gregor83314aa2009-07-08 20:55:45 +00001158 if (CheckTemplateArgumentList(FunctionTemplate,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001159 SourceLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001160 ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001161 true,
1162 Builder) || Trap.hasErrorOccurred())
1163 return TDK_InvalidExplicitArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001164
Douglas Gregor83314aa2009-07-08 20:55:45 +00001165 // Form the template argument list from the explicitly-specified
1166 // template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001167 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor83314aa2009-07-08 20:55:45 +00001168 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1169 Info.reset(ExplicitArgumentList);
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Douglas Gregor83314aa2009-07-08 20:55:45 +00001171 // Instantiate the types of each of the function parameters given the
1172 // explicitly-specified template arguments.
1173 for (FunctionDecl::param_iterator P = Function->param_begin(),
1174 PEnd = Function->param_end();
1175 P != PEnd;
1176 ++P) {
Mike Stump1eb44332009-09-09 15:08:12 +00001177 QualType ParamType
1178 = SubstType((*P)->getType(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001179 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1180 (*P)->getLocation(), (*P)->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001181 if (ParamType.isNull() || Trap.hasErrorOccurred())
1182 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Douglas Gregor83314aa2009-07-08 20:55:45 +00001184 ParamTypes.push_back(ParamType);
1185 }
1186
1187 // If the caller wants a full function type back, instantiate the return
1188 // type and form that function type.
1189 if (FunctionType) {
1190 // FIXME: exception-specifications?
Mike Stump1eb44332009-09-09 15:08:12 +00001191 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001192 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001193 assert(Proto && "Function template does not have a prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001194
1195 QualType ResultType
Douglas Gregor357bbd02009-08-28 20:50:45 +00001196 = SubstType(Proto->getResultType(),
1197 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1198 Function->getTypeSpecStartLoc(),
1199 Function->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001200 if (ResultType.isNull() || Trap.hasErrorOccurred())
1201 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001202
1203 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001204 ParamTypes.data(), ParamTypes.size(),
1205 Proto->isVariadic(),
1206 Proto->getTypeQuals(),
1207 Function->getLocation(),
1208 Function->getDeclName());
1209 if (FunctionType->isNull() || Trap.hasErrorOccurred())
1210 return TDK_SubstitutionFailure;
1211 }
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Douglas Gregor83314aa2009-07-08 20:55:45 +00001213 // C++ [temp.arg.explicit]p2:
Mike Stump1eb44332009-09-09 15:08:12 +00001214 // Trailing template arguments that can be deduced (14.8.2) may be
1215 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001216 // template arguments can be deduced, they may all be omitted; in this
1217 // case, the empty template argument list <> itself may also be omitted.
1218 //
1219 // Take all of the explicitly-specified arguments and put them into the
Mike Stump1eb44332009-09-09 15:08:12 +00001220 // set of deduced template arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001221 Deduced.reserve(TemplateParams->size());
1222 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001223 Deduced.push_back(ExplicitArgumentList->get(I));
1224
Douglas Gregor83314aa2009-07-08 20:55:45 +00001225 return TDK_Success;
1226}
1227
Mike Stump1eb44332009-09-09 15:08:12 +00001228/// \brief Finish template argument deduction for a function template,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001229/// checking the deduced template arguments for completeness and forming
1230/// the function template specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00001231Sema::TemplateDeductionResult
Douglas Gregor83314aa2009-07-08 20:55:45 +00001232Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
1233 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1234 FunctionDecl *&Specialization,
1235 TemplateDeductionInfo &Info) {
1236 TemplateParameterList *TemplateParams
1237 = FunctionTemplate->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Douglas Gregor83314aa2009-07-08 20:55:45 +00001239 // Template argument deduction for function templates in a SFINAE context.
1240 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001241 SFINAETrap Trap(*this);
1242
Douglas Gregor83314aa2009-07-08 20:55:45 +00001243 // Enter a new template instantiation context while we instantiate the
1244 // actual function declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001245 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001246 FunctionTemplate, Deduced.data(), Deduced.size(),
1247 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
1248 if (Inst)
Mike Stump1eb44332009-09-09 15:08:12 +00001249 return TDK_InstantiationDepth;
1250
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001251 // C++ [temp.deduct.type]p2:
1252 // [...] or if any template argument remains neither deduced nor
1253 // explicitly specified, template argument deduction fails.
1254 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
1255 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
1256 if (!Deduced[I].isNull()) {
1257 Builder.Append(Deduced[I]);
1258 continue;
1259 }
1260
1261 // Substitute into the default template argument, if available.
1262 NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I);
1263 TemplateArgumentLoc DefArg
1264 = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
1265 FunctionTemplate->getLocation(),
1266 FunctionTemplate->getSourceRange().getEnd(),
1267 Param,
1268 Builder);
1269
1270 // If there was no default argument, deduction is incomplete.
1271 if (DefArg.getArgument().isNull()) {
1272 Info.Param = makeTemplateParameter(
1273 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1274 return TDK_Incomplete;
1275 }
1276
1277 // Check whether we can actually use the default argument.
1278 if (CheckTemplateArgument(Param, DefArg,
1279 FunctionTemplate,
1280 FunctionTemplate->getLocation(),
1281 FunctionTemplate->getSourceRange().getEnd(),
1282 Builder)) {
1283 Info.Param = makeTemplateParameter(
1284 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1285 return TDK_SubstitutionFailure;
1286 }
1287
1288 // If we get here, we successfully used the default template argument.
1289 }
1290
1291 // Form the template argument list from the deduced template arguments.
1292 TemplateArgumentList *DeducedArgumentList
1293 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1294 Info.reset(DeducedArgumentList);
1295
Mike Stump1eb44332009-09-09 15:08:12 +00001296 // Substitute the deduced template arguments into the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001297 // declaration to produce the function template specialization.
1298 Specialization = cast_or_null<FunctionDecl>(
John McCallce3ff2b2009-08-25 22:02:44 +00001299 SubstDecl(FunctionTemplate->getTemplatedDecl(),
1300 FunctionTemplate->getDeclContext(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001301 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor83314aa2009-07-08 20:55:45 +00001302 if (!Specialization)
1303 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001304
Douglas Gregorf8825742009-09-15 18:26:13 +00001305 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
1306 FunctionTemplate->getCanonicalDecl());
1307
Mike Stump1eb44332009-09-09 15:08:12 +00001308 // If the template argument list is owned by the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001309 // specialization, release it.
1310 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList)
1311 Info.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001312
Douglas Gregor83314aa2009-07-08 20:55:45 +00001313 // There may have been an error that did not prevent us from constructing a
1314 // declaration. Mark the declaration invalid and return with a substitution
1315 // failure.
1316 if (Trap.hasErrorOccurred()) {
1317 Specialization->setInvalidDecl(true);
1318 return TDK_SubstitutionFailure;
1319 }
Mike Stump1eb44332009-09-09 15:08:12 +00001320
1321 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001322}
1323
John McCalleff92132010-02-02 02:21:27 +00001324static QualType GetTypeOfFunction(ASTContext &Context,
1325 bool isAddressOfOperand,
1326 FunctionDecl *Fn) {
1327 if (!isAddressOfOperand) return Fn->getType();
1328 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
1329 if (Method->isInstance())
1330 return Context.getMemberPointerType(Fn->getType(),
1331 Context.getTypeDeclType(Method->getParent()).getTypePtr());
1332 return Context.getPointerType(Fn->getType());
1333}
1334
1335/// Apply the deduction rules for overload sets.
1336///
1337/// \return the null type if this argument should be treated as an
1338/// undeduced context
1339static QualType
1340ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
1341 Expr *Arg, QualType ParamType) {
John McCall7bb12da2010-02-02 06:20:04 +00001342 llvm::PointerIntPair<OverloadExpr*,1> R = OverloadExpr::find(Arg);
John McCalleff92132010-02-02 02:21:27 +00001343
John McCall7bb12da2010-02-02 06:20:04 +00001344 bool isAddressOfOperand = bool(R.getInt());
1345 OverloadExpr *Ovl = R.getPointer();
John McCalleff92132010-02-02 02:21:27 +00001346
1347 // If there were explicit template arguments, we can only find
1348 // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
1349 // unambiguously name a full specialization.
John McCall7bb12da2010-02-02 06:20:04 +00001350 if (Ovl->hasExplicitTemplateArgs()) {
John McCalleff92132010-02-02 02:21:27 +00001351 // But we can still look for an explicit specialization.
1352 if (FunctionDecl *ExplicitSpec
John McCall7bb12da2010-02-02 06:20:04 +00001353 = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
1354 return GetTypeOfFunction(S.Context, isAddressOfOperand, ExplicitSpec);
John McCalleff92132010-02-02 02:21:27 +00001355 return QualType();
1356 }
1357
1358 // C++0x [temp.deduct.call]p6:
1359 // When P is a function type, pointer to function type, or pointer
1360 // to member function type:
1361
1362 if (!ParamType->isFunctionType() &&
1363 !ParamType->isFunctionPointerType() &&
1364 !ParamType->isMemberFunctionPointerType())
1365 return QualType();
1366
1367 QualType Match;
John McCall7bb12da2010-02-02 06:20:04 +00001368 for (UnresolvedSetIterator I = Ovl->decls_begin(),
1369 E = Ovl->decls_end(); I != E; ++I) {
John McCalleff92132010-02-02 02:21:27 +00001370 NamedDecl *D = (*I)->getUnderlyingDecl();
1371
1372 // - If the argument is an overload set containing one or more
1373 // function templates, the parameter is treated as a
1374 // non-deduced context.
1375 if (isa<FunctionTemplateDecl>(D))
1376 return QualType();
1377
1378 FunctionDecl *Fn = cast<FunctionDecl>(D);
1379 QualType ArgType = GetTypeOfFunction(S.Context, isAddressOfOperand, Fn);
1380
1381 // - If the argument is an overload set (not containing function
1382 // templates), trial argument deduction is attempted using each
1383 // of the members of the set. If deduction succeeds for only one
1384 // of the overload set members, that member is used as the
1385 // argument value for the deduction. If deduction succeeds for
1386 // more than one member of the overload set the parameter is
1387 // treated as a non-deduced context.
1388
1389 // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
1390 // Type deduction is done independently for each P/A pair, and
1391 // the deduced template argument values are then combined.
1392 // So we do not reject deductions which were made elsewhere.
1393 llvm::SmallVector<TemplateArgument, 8> Deduced(TemplateParams->size());
John McCall5769d612010-02-08 23:07:23 +00001394 Sema::TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
John McCalleff92132010-02-02 02:21:27 +00001395 unsigned TDF = 0;
1396
1397 Sema::TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001398 = DeduceTemplateArguments(S, TemplateParams,
John McCalleff92132010-02-02 02:21:27 +00001399 ParamType, ArgType,
1400 Info, Deduced, TDF);
1401 if (Result) continue;
1402 if (!Match.isNull()) return QualType();
1403 Match = ArgType;
1404 }
1405
1406 return Match;
1407}
1408
Douglas Gregore53060f2009-06-25 22:08:12 +00001409/// \brief Perform template argument deduction from a function call
1410/// (C++ [temp.deduct.call]).
1411///
1412/// \param FunctionTemplate the function template for which we are performing
1413/// template argument deduction.
1414///
Douglas Gregor48026d22010-01-11 18:40:55 +00001415/// \param ExplicitTemplateArguments the explicit template arguments provided
1416/// for this call.
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001417///
Douglas Gregore53060f2009-06-25 22:08:12 +00001418/// \param Args the function call arguments
1419///
1420/// \param NumArgs the number of arguments in Args
1421///
Douglas Gregor48026d22010-01-11 18:40:55 +00001422/// \param Name the name of the function being called. This is only significant
1423/// when the function template is a conversion function template, in which
1424/// case this routine will also perform template argument deduction based on
1425/// the function to which
1426///
Douglas Gregore53060f2009-06-25 22:08:12 +00001427/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001428/// this will be set to the function template specialization produced by
Douglas Gregore53060f2009-06-25 22:08:12 +00001429/// template argument deduction.
1430///
1431/// \param Info the argument will be updated to provide additional information
1432/// about template argument deduction.
1433///
1434/// \returns the result of template argument deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001435Sema::TemplateDeductionResult
1436Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor48026d22010-01-11 18:40:55 +00001437 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregore53060f2009-06-25 22:08:12 +00001438 Expr **Args, unsigned NumArgs,
1439 FunctionDecl *&Specialization,
1440 TemplateDeductionInfo &Info) {
1441 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001442
Douglas Gregore53060f2009-06-25 22:08:12 +00001443 // C++ [temp.deduct.call]p1:
1444 // Template argument deduction is done by comparing each function template
1445 // parameter type (call it P) with the type of the corresponding argument
1446 // of the call (call it A) as described below.
1447 unsigned CheckArgs = NumArgs;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001448 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregore53060f2009-06-25 22:08:12 +00001449 return TDK_TooFewArguments;
1450 else if (NumArgs > Function->getNumParams()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001451 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001452 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregore53060f2009-06-25 22:08:12 +00001453 if (!Proto->isVariadic())
1454 return TDK_TooManyArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Douglas Gregore53060f2009-06-25 22:08:12 +00001456 CheckArgs = Function->getNumParams();
1457 }
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001459 // The types of the parameters from which we will perform template argument
1460 // deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001461 TemplateParameterList *TemplateParams
1462 = FunctionTemplate->getTemplateParameters();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001463 llvm::SmallVector<TemplateArgument, 4> Deduced;
1464 llvm::SmallVector<QualType, 4> ParamTypes;
John McCalld5532b62009-11-23 01:53:49 +00001465 if (ExplicitTemplateArgs) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001466 TemplateDeductionResult Result =
1467 SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001468 *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001469 Deduced,
1470 ParamTypes,
1471 0,
1472 Info);
1473 if (Result)
1474 return Result;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001475 } else {
1476 // Just fill in the parameter types from the function declaration.
1477 for (unsigned I = 0; I != CheckArgs; ++I)
1478 ParamTypes.push_back(Function->getParamDecl(I)->getType());
1479 }
Mike Stump1eb44332009-09-09 15:08:12 +00001480
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001481 // Deduce template arguments from the function parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001482 Deduced.resize(TemplateParams->size());
Douglas Gregore53060f2009-06-25 22:08:12 +00001483 for (unsigned I = 0; I != CheckArgs; ++I) {
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001484 QualType ParamType = ParamTypes[I];
Douglas Gregore53060f2009-06-25 22:08:12 +00001485 QualType ArgType = Args[I]->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001486
John McCalleff92132010-02-02 02:21:27 +00001487 // Overload sets usually make this parameter an undeduced
1488 // context, but there are sometimes special circumstances.
1489 if (ArgType == Context.OverloadTy) {
1490 ArgType = ResolveOverloadForDeduction(*this, TemplateParams,
1491 Args[I], ParamType);
1492 if (ArgType.isNull())
1493 continue;
1494 }
1495
Douglas Gregore53060f2009-06-25 22:08:12 +00001496 // C++ [temp.deduct.call]p2:
1497 // If P is not a reference type:
1498 QualType CanonParamType = Context.getCanonicalType(ParamType);
Douglas Gregor500d3312009-06-26 18:27:22 +00001499 bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1500 if (!ParamWasReference) {
Mike Stump1eb44332009-09-09 15:08:12 +00001501 // - If A is an array type, the pointer type produced by the
1502 // array-to-pointer standard conversion (4.2) is used in place of
Douglas Gregore53060f2009-06-25 22:08:12 +00001503 // A for type deduction; otherwise,
1504 if (ArgType->isArrayType())
1505 ArgType = Context.getArrayDecayedType(ArgType);
Mike Stump1eb44332009-09-09 15:08:12 +00001506 // - If A is a function type, the pointer type produced by the
1507 // function-to-pointer standard conversion (4.3) is used in place
Douglas Gregore53060f2009-06-25 22:08:12 +00001508 // of A for type deduction; otherwise,
1509 else if (ArgType->isFunctionType())
1510 ArgType = Context.getPointerType(ArgType);
1511 else {
1512 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1513 // type are ignored for type deduction.
1514 QualType CanonArgType = Context.getCanonicalType(ArgType);
Douglas Gregora4923eb2009-11-16 21:35:15 +00001515 if (CanonArgType.getLocalCVRQualifiers())
1516 ArgType = CanonArgType.getLocalUnqualifiedType();
Douglas Gregore53060f2009-06-25 22:08:12 +00001517 }
1518 }
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Douglas Gregore53060f2009-06-25 22:08:12 +00001520 // C++0x [temp.deduct.call]p3:
1521 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
Mike Stump1eb44332009-09-09 15:08:12 +00001522 // are ignored for type deduction.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001523 if (CanonParamType.getLocalCVRQualifiers())
1524 ParamType = CanonParamType.getLocalUnqualifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001525 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001526 // [...] If P is a reference type, the type referred to by P is used
1527 // for type deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001528 ParamType = ParamRefType->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00001529
1530 // [...] If P is of the form T&&, where T is a template parameter, and
1531 // the argument is an lvalue, the type A& is used in place of A for
Douglas Gregore53060f2009-06-25 22:08:12 +00001532 // type deduction.
1533 if (isa<RValueReferenceType>(ParamRefType) &&
John McCall183700f2009-09-21 23:43:11 +00001534 ParamRefType->getAs<TemplateTypeParmType>() &&
Douglas Gregore53060f2009-06-25 22:08:12 +00001535 Args[I]->isLvalue(Context) == Expr::LV_Valid)
1536 ArgType = Context.getLValueReferenceType(ArgType);
1537 }
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Douglas Gregore53060f2009-06-25 22:08:12 +00001539 // C++0x [temp.deduct.call]p4:
1540 // In general, the deduction process attempts to find template argument
1541 // values that will make the deduced A identical to A (after the type A
1542 // is transformed as described above). [...]
Douglas Gregor12820292009-09-14 20:00:47 +00001543 unsigned TDF = TDF_SkipNonDependent;
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Douglas Gregor508f1c82009-06-26 23:10:12 +00001545 // - If the original P is a reference type, the deduced A (i.e., the
1546 // type referred to by the reference) can be more cv-qualified than
1547 // the transformed A.
1548 if (ParamWasReference)
1549 TDF |= TDF_ParamWithReferenceType;
Mike Stump1eb44332009-09-09 15:08:12 +00001550 // - The transformed A can be another pointer or pointer to member
1551 // type that can be converted to the deduced A via a qualification
Douglas Gregor508f1c82009-06-26 23:10:12 +00001552 // conversion (4.4).
1553 if (ArgType->isPointerType() || ArgType->isMemberPointerType())
1554 TDF |= TDF_IgnoreQualifiers;
Mike Stump1eb44332009-09-09 15:08:12 +00001555 // - If P is a class and P has the form simple-template-id, then the
Douglas Gregor41128772009-06-26 23:27:24 +00001556 // transformed A can be a derived class of the deduced A. Likewise,
1557 // if P is a pointer to a class of the form simple-template-id, the
1558 // transformed A can be a pointer to a derived class pointed to by
1559 // the deduced A.
1560 if (isSimpleTemplateIdType(ParamType) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001561 (isa<PointerType>(ParamType) &&
Douglas Gregor41128772009-06-26 23:27:24 +00001562 isSimpleTemplateIdType(
Ted Kremenek6217b802009-07-29 21:53:49 +00001563 ParamType->getAs<PointerType>()->getPointeeType())))
Douglas Gregor41128772009-06-26 23:27:24 +00001564 TDF |= TDF_DerivedClass;
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Douglas Gregore53060f2009-06-25 22:08:12 +00001566 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001567 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor500d3312009-06-26 18:27:22 +00001568 ParamType, ArgType, Info, Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +00001569 TDF))
Douglas Gregore53060f2009-06-25 22:08:12 +00001570 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001572 // FIXME: we need to check that the deduced A is the same as A,
1573 // modulo the various allowed differences.
Douglas Gregore53060f2009-06-25 22:08:12 +00001574 }
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001575
Mike Stump1eb44332009-09-09 15:08:12 +00001576 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001577 Specialization, Info);
Douglas Gregore53060f2009-06-25 22:08:12 +00001578}
1579
Douglas Gregor83314aa2009-07-08 20:55:45 +00001580/// \brief Deduce template arguments when taking the address of a function
Douglas Gregor4b52e252009-12-21 23:17:24 +00001581/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
1582/// a template.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001583///
1584/// \param FunctionTemplate the function template for which we are performing
1585/// template argument deduction.
1586///
Douglas Gregor4b52e252009-12-21 23:17:24 +00001587/// \param ExplicitTemplateArguments the explicitly-specified template
1588/// arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001589///
1590/// \param ArgFunctionType the function type that will be used as the
1591/// "argument" type (A) when performing template argument deduction from the
Douglas Gregor4b52e252009-12-21 23:17:24 +00001592/// function template's function type. This type may be NULL, if there is no
1593/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001594///
1595/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001596/// this will be set to the function template specialization produced by
Douglas Gregor83314aa2009-07-08 20:55:45 +00001597/// template argument deduction.
1598///
1599/// \param Info the argument will be updated to provide additional information
1600/// about template argument deduction.
1601///
1602/// \returns the result of template argument deduction.
1603Sema::TemplateDeductionResult
1604Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001605 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001606 QualType ArgFunctionType,
1607 FunctionDecl *&Specialization,
1608 TemplateDeductionInfo &Info) {
1609 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1610 TemplateParameterList *TemplateParams
1611 = FunctionTemplate->getTemplateParameters();
1612 QualType FunctionType = Function->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001613
Douglas Gregor83314aa2009-07-08 20:55:45 +00001614 // Substitute any explicit template arguments.
1615 llvm::SmallVector<TemplateArgument, 4> Deduced;
1616 llvm::SmallVector<QualType, 4> ParamTypes;
John McCalld5532b62009-11-23 01:53:49 +00001617 if (ExplicitTemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001618 if (TemplateDeductionResult Result
1619 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001620 *ExplicitTemplateArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00001621 Deduced, ParamTypes,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001622 &FunctionType, Info))
1623 return Result;
1624 }
1625
1626 // Template argument deduction for function templates in a SFINAE context.
1627 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001628 SFINAETrap Trap(*this);
1629
John McCalleff92132010-02-02 02:21:27 +00001630 Deduced.resize(TemplateParams->size());
1631
Douglas Gregor4b52e252009-12-21 23:17:24 +00001632 if (!ArgFunctionType.isNull()) {
1633 // Deduce template arguments from the function type.
Douglas Gregor4b52e252009-12-21 23:17:24 +00001634 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001635 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor4b52e252009-12-21 23:17:24 +00001636 FunctionType, ArgFunctionType, Info,
1637 Deduced, 0))
1638 return Result;
1639 }
1640
Mike Stump1eb44332009-09-09 15:08:12 +00001641 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001642 Specialization, Info);
1643}
1644
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001645/// \brief Deduce template arguments for a templated conversion
1646/// function (C++ [temp.deduct.conv]) and, if successful, produce a
1647/// conversion function template specialization.
1648Sema::TemplateDeductionResult
1649Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1650 QualType ToType,
1651 CXXConversionDecl *&Specialization,
1652 TemplateDeductionInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00001653 CXXConversionDecl *Conv
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001654 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
1655 QualType FromType = Conv->getConversionType();
1656
1657 // Canonicalize the types for deduction.
1658 QualType P = Context.getCanonicalType(FromType);
1659 QualType A = Context.getCanonicalType(ToType);
1660
1661 // C++0x [temp.deduct.conv]p3:
1662 // If P is a reference type, the type referred to by P is used for
1663 // type deduction.
1664 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
1665 P = PRef->getPointeeType();
1666
1667 // C++0x [temp.deduct.conv]p3:
1668 // If A is a reference type, the type referred to by A is used
1669 // for type deduction.
1670 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
1671 A = ARef->getPointeeType();
1672 // C++ [temp.deduct.conv]p2:
1673 //
Mike Stump1eb44332009-09-09 15:08:12 +00001674 // If A is not a reference type:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001675 else {
1676 assert(!A->isReferenceType() && "Reference types were handled above");
1677
1678 // - If P is an array type, the pointer type produced by the
Mike Stump1eb44332009-09-09 15:08:12 +00001679 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001680 // of P for type deduction; otherwise,
1681 if (P->isArrayType())
1682 P = Context.getArrayDecayedType(P);
1683 // - If P is a function type, the pointer type produced by the
1684 // function-to-pointer standard conversion (4.3) is used in
1685 // place of P for type deduction; otherwise,
1686 else if (P->isFunctionType())
1687 P = Context.getPointerType(P);
1688 // - If P is a cv-qualified type, the top level cv-qualifiers of
1689 // P’s type are ignored for type deduction.
1690 else
1691 P = P.getUnqualifiedType();
1692
1693 // C++0x [temp.deduct.conv]p3:
1694 // If A is a cv-qualified type, the top level cv-qualifiers of A’s
1695 // type are ignored for type deduction.
1696 A = A.getUnqualifiedType();
1697 }
1698
1699 // Template argument deduction for function templates in a SFINAE context.
1700 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001701 SFINAETrap Trap(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001702
1703 // C++ [temp.deduct.conv]p1:
1704 // Template argument deduction is done by comparing the return
1705 // type of the template conversion function (call it P) with the
1706 // type that is required as the result of the conversion (call it
1707 // A) as described in 14.8.2.4.
1708 TemplateParameterList *TemplateParams
1709 = FunctionTemplate->getTemplateParameters();
1710 llvm::SmallVector<TemplateArgument, 4> Deduced;
Mike Stump1eb44332009-09-09 15:08:12 +00001711 Deduced.resize(TemplateParams->size());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001712
1713 // C++0x [temp.deduct.conv]p4:
1714 // In general, the deduction process attempts to find template
1715 // argument values that will make the deduced A identical to
1716 // A. However, there are two cases that allow a difference:
1717 unsigned TDF = 0;
1718 // - If the original A is a reference type, A can be more
1719 // cv-qualified than the deduced A (i.e., the type referred to
1720 // by the reference)
1721 if (ToType->isReferenceType())
1722 TDF |= TDF_ParamWithReferenceType;
1723 // - The deduced A can be another pointer or pointer to member
1724 // type that can be converted to A via a qualification
1725 // conversion.
1726 //
1727 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
1728 // both P and A are pointers or member pointers. In this case, we
1729 // just ignore cv-qualifiers completely).
1730 if ((P->isPointerType() && A->isPointerType()) ||
1731 (P->isMemberPointerType() && P->isMemberPointerType()))
1732 TDF |= TDF_IgnoreQualifiers;
1733 if (TemplateDeductionResult Result
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001734 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001735 P, A, Info, Deduced, TDF))
1736 return Result;
1737
1738 // FIXME: we need to check that the deduced A is the same as A,
1739 // modulo the various allowed differences.
Mike Stump1eb44332009-09-09 15:08:12 +00001740
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001741 // Finish template argument deduction.
1742 FunctionDecl *Spec = 0;
1743 TemplateDeductionResult Result
1744 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, Spec, Info);
1745 Specialization = cast_or_null<CXXConversionDecl>(Spec);
1746 return Result;
1747}
1748
Douglas Gregor4b52e252009-12-21 23:17:24 +00001749/// \brief Deduce template arguments for a function template when there is
1750/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
1751///
1752/// \param FunctionTemplate the function template for which we are performing
1753/// template argument deduction.
1754///
1755/// \param ExplicitTemplateArguments the explicitly-specified template
1756/// arguments.
1757///
1758/// \param Specialization if template argument deduction was successful,
1759/// this will be set to the function template specialization produced by
1760/// template argument deduction.
1761///
1762/// \param Info the argument will be updated to provide additional information
1763/// about template argument deduction.
1764///
1765/// \returns the result of template argument deduction.
1766Sema::TemplateDeductionResult
1767Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1768 const TemplateArgumentListInfo *ExplicitTemplateArgs,
1769 FunctionDecl *&Specialization,
1770 TemplateDeductionInfo &Info) {
1771 return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
1772 QualType(), Specialization, Info);
1773}
1774
Douglas Gregor8a514912009-09-14 18:39:43 +00001775/// \brief Stores the result of comparing the qualifiers of two types.
1776enum DeductionQualifierComparison {
1777 NeitherMoreQualified = 0,
1778 ParamMoreQualified,
1779 ArgMoreQualified
1780};
1781
1782/// \brief Deduce the template arguments during partial ordering by comparing
1783/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
1784///
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001785/// \param S the semantic analysis object within which we are deducing
Douglas Gregor8a514912009-09-14 18:39:43 +00001786///
1787/// \param TemplateParams the template parameters that we are deducing
1788///
1789/// \param ParamIn the parameter type
1790///
1791/// \param ArgIn the argument type
1792///
1793/// \param Info information about the template argument deduction itself
1794///
1795/// \param Deduced the deduced template arguments
1796///
1797/// \returns the result of template argument deduction so far. Note that a
1798/// "success" result means that template argument deduction has not yet failed,
1799/// but it may still fail, later, for other reasons.
1800static Sema::TemplateDeductionResult
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001801DeduceTemplateArgumentsDuringPartialOrdering(Sema &S,
Douglas Gregor8a514912009-09-14 18:39:43 +00001802 TemplateParameterList *TemplateParams,
1803 QualType ParamIn, QualType ArgIn,
1804 Sema::TemplateDeductionInfo &Info,
1805 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1806 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001807 CanQualType Param = S.Context.getCanonicalType(ParamIn);
1808 CanQualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor8a514912009-09-14 18:39:43 +00001809
1810 // C++0x [temp.deduct.partial]p5:
1811 // Before the partial ordering is done, certain transformations are
1812 // performed on the types used for partial ordering:
1813 // - If P is a reference type, P is replaced by the type referred to.
1814 CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00001815 if (!ParamRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00001816 Param = ParamRef->getPointeeType();
1817
1818 // - If A is a reference type, A is replaced by the type referred to.
1819 CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00001820 if (!ArgRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00001821 Arg = ArgRef->getPointeeType();
1822
John McCalle27ec8a2009-10-23 23:03:21 +00001823 if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
Douglas Gregor8a514912009-09-14 18:39:43 +00001824 // C++0x [temp.deduct.partial]p6:
1825 // If both P and A were reference types (before being replaced with the
1826 // type referred to above), determine which of the two types (if any) is
1827 // more cv-qualified than the other; otherwise the types are considered to
1828 // be equally cv-qualified for partial ordering purposes. The result of this
1829 // determination will be used below.
1830 //
1831 // We save this information for later, using it only when deduction
1832 // succeeds in both directions.
1833 DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
1834 if (Param.isMoreQualifiedThan(Arg))
1835 QualifierResult = ParamMoreQualified;
1836 else if (Arg.isMoreQualifiedThan(Param))
1837 QualifierResult = ArgMoreQualified;
1838 QualifierComparisons->push_back(QualifierResult);
1839 }
1840
1841 // C++0x [temp.deduct.partial]p7:
1842 // Remove any top-level cv-qualifiers:
1843 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
1844 // version of P.
1845 Param = Param.getUnqualifiedType();
1846 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
1847 // version of A.
1848 Arg = Arg.getUnqualifiedType();
1849
1850 // C++0x [temp.deduct.partial]p8:
1851 // Using the resulting types P and A the deduction is then done as
1852 // described in 14.9.2.5. If deduction succeeds for a given type, the type
1853 // from the argument template is considered to be at least as specialized
1854 // as the type from the parameter template.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001855 return DeduceTemplateArguments(S, TemplateParams, Param, Arg, Info,
Douglas Gregor8a514912009-09-14 18:39:43 +00001856 Deduced, TDF_None);
1857}
1858
1859static void
Douglas Gregore73bb602009-09-14 21:25:05 +00001860MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
1861 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001862 unsigned Level,
Douglas Gregore73bb602009-09-14 21:25:05 +00001863 llvm::SmallVectorImpl<bool> &Deduced);
Douglas Gregor8a514912009-09-14 18:39:43 +00001864
1865/// \brief Determine whether the function template \p FT1 is at least as
1866/// specialized as \p FT2.
1867static bool isAtLeastAsSpecializedAs(Sema &S,
John McCall5769d612010-02-08 23:07:23 +00001868 SourceLocation Loc,
Douglas Gregor8a514912009-09-14 18:39:43 +00001869 FunctionTemplateDecl *FT1,
1870 FunctionTemplateDecl *FT2,
1871 TemplatePartialOrderingContext TPOC,
1872 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1873 FunctionDecl *FD1 = FT1->getTemplatedDecl();
1874 FunctionDecl *FD2 = FT2->getTemplatedDecl();
1875 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
1876 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
1877
1878 assert(Proto1 && Proto2 && "Function templates must have prototypes");
1879 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
1880 llvm::SmallVector<TemplateArgument, 4> Deduced;
1881 Deduced.resize(TemplateParams->size());
1882
1883 // C++0x [temp.deduct.partial]p3:
1884 // The types used to determine the ordering depend on the context in which
1885 // the partial ordering is done:
John McCall5769d612010-02-08 23:07:23 +00001886 Sema::TemplateDeductionInfo Info(S.Context, Loc);
Douglas Gregor8a514912009-09-14 18:39:43 +00001887 switch (TPOC) {
1888 case TPOC_Call: {
1889 // - In the context of a function call, the function parameter types are
1890 // used.
1891 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
1892 for (unsigned I = 0; I != NumParams; ++I)
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001893 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00001894 TemplateParams,
1895 Proto2->getArgType(I),
1896 Proto1->getArgType(I),
1897 Info,
1898 Deduced,
1899 QualifierComparisons))
1900 return false;
1901
1902 break;
1903 }
1904
1905 case TPOC_Conversion:
1906 // - In the context of a call to a conversion operator, the return types
1907 // of the conversion function templates are used.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001908 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00001909 TemplateParams,
1910 Proto2->getResultType(),
1911 Proto1->getResultType(),
1912 Info,
1913 Deduced,
1914 QualifierComparisons))
1915 return false;
1916 break;
1917
1918 case TPOC_Other:
1919 // - In other contexts (14.6.6.2) the function template’s function type
1920 // is used.
Chandler Carrutha7ef1302010-02-07 21:33:28 +00001921 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor8a514912009-09-14 18:39:43 +00001922 TemplateParams,
1923 FD2->getType(),
1924 FD1->getType(),
1925 Info,
1926 Deduced,
1927 QualifierComparisons))
1928 return false;
1929 break;
1930 }
1931
1932 // C++0x [temp.deduct.partial]p11:
1933 // In most cases, all template parameters must have values in order for
1934 // deduction to succeed, but for partial ordering purposes a template
1935 // parameter may remain without a value provided it is not used in the
1936 // types being used for partial ordering. [ Note: a template parameter used
1937 // in a non-deduced context is considered used. -end note]
1938 unsigned ArgIdx = 0, NumArgs = Deduced.size();
1939 for (; ArgIdx != NumArgs; ++ArgIdx)
1940 if (Deduced[ArgIdx].isNull())
1941 break;
1942
1943 if (ArgIdx == NumArgs) {
1944 // All template arguments were deduced. FT1 is at least as specialized
1945 // as FT2.
1946 return true;
1947 }
1948
Douglas Gregore73bb602009-09-14 21:25:05 +00001949 // Figure out which template parameters were used.
Douglas Gregor8a514912009-09-14 18:39:43 +00001950 llvm::SmallVector<bool, 4> UsedParameters;
1951 UsedParameters.resize(TemplateParams->size());
1952 switch (TPOC) {
1953 case TPOC_Call: {
1954 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
1955 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00001956 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
1957 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00001958 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00001959 break;
1960 }
1961
1962 case TPOC_Conversion:
Douglas Gregored9c0f92009-10-29 00:04:11 +00001963 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
1964 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00001965 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00001966 break;
1967
1968 case TPOC_Other:
Douglas Gregored9c0f92009-10-29 00:04:11 +00001969 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
1970 TemplateParams->getDepth(),
1971 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00001972 break;
1973 }
1974
1975 for (; ArgIdx != NumArgs; ++ArgIdx)
1976 // If this argument had no value deduced but was used in one of the types
1977 // used for partial ordering, then deduction fails.
1978 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
1979 return false;
1980
1981 return true;
1982}
1983
1984
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001985/// \brief Returns the more specialized function template according
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001986/// to the rules of function template partial ordering (C++ [temp.func.order]).
1987///
1988/// \param FT1 the first function template
1989///
1990/// \param FT2 the second function template
1991///
Douglas Gregor8a514912009-09-14 18:39:43 +00001992/// \param TPOC the context in which we are performing partial ordering of
1993/// function templates.
Mike Stump1eb44332009-09-09 15:08:12 +00001994///
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001995/// \returns the more specialized function template. If neither
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001996/// template is more specialized, returns NULL.
1997FunctionTemplateDecl *
1998Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
1999 FunctionTemplateDecl *FT2,
John McCall5769d612010-02-08 23:07:23 +00002000 SourceLocation Loc,
Douglas Gregor8a514912009-09-14 18:39:43 +00002001 TemplatePartialOrderingContext TPOC) {
Douglas Gregor8a514912009-09-14 18:39:43 +00002002 llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
John McCall5769d612010-02-08 23:07:23 +00002003 bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, 0);
2004 bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
Douglas Gregor8a514912009-09-14 18:39:43 +00002005 &QualifierComparisons);
2006
2007 if (Better1 != Better2) // We have a clear winner
2008 return Better1? FT1 : FT2;
2009
2010 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002011 return 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00002012
2013
2014 // C++0x [temp.deduct.partial]p10:
2015 // If for each type being considered a given template is at least as
2016 // specialized for all types and more specialized for some set of types and
2017 // the other template is not more specialized for any types or is not at
2018 // least as specialized for any types, then the given template is more
2019 // specialized than the other template. Otherwise, neither template is more
2020 // specialized than the other.
2021 Better1 = false;
2022 Better2 = false;
2023 for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
2024 // C++0x [temp.deduct.partial]p9:
2025 // If, for a given type, deduction succeeds in both directions (i.e., the
2026 // types are identical after the transformations above) and if the type
2027 // from the argument template is more cv-qualified than the type from the
2028 // parameter template (as described above) that type is considered to be
2029 // more specialized than the other. If neither type is more cv-qualified
2030 // than the other then neither type is more specialized than the other.
2031 switch (QualifierComparisons[I]) {
2032 case NeitherMoreQualified:
2033 break;
2034
2035 case ParamMoreQualified:
2036 Better1 = true;
2037 if (Better2)
2038 return 0;
2039 break;
2040
2041 case ArgMoreQualified:
2042 Better2 = true;
2043 if (Better1)
2044 return 0;
2045 break;
2046 }
2047 }
2048
2049 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002050 if (Better1)
2051 return FT1;
Douglas Gregor8a514912009-09-14 18:39:43 +00002052 else if (Better2)
2053 return FT2;
2054 else
2055 return 0;
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002056}
Douglas Gregor83314aa2009-07-08 20:55:45 +00002057
Douglas Gregord5a423b2009-09-25 18:43:00 +00002058/// \brief Determine if the two templates are equivalent.
2059static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
2060 if (T1 == T2)
2061 return true;
2062
2063 if (!T1 || !T2)
2064 return false;
2065
2066 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
2067}
2068
2069/// \brief Retrieve the most specialized of the given function template
2070/// specializations.
2071///
John McCallc373d482010-01-27 01:50:18 +00002072/// \param SpecBegin the start iterator of the function template
2073/// specializations that we will be comparing.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002074///
John McCallc373d482010-01-27 01:50:18 +00002075/// \param SpecEnd the end iterator of the function template
2076/// specializations, paired with \p SpecBegin.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002077///
2078/// \param TPOC the partial ordering context to use to compare the function
2079/// template specializations.
2080///
2081/// \param Loc the location where the ambiguity or no-specializations
2082/// diagnostic should occur.
2083///
2084/// \param NoneDiag partial diagnostic used to diagnose cases where there are
2085/// no matching candidates.
2086///
2087/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
2088/// occurs.
2089///
2090/// \param CandidateDiag partial diagnostic used for each function template
2091/// specialization that is a candidate in the ambiguous ordering. One parameter
2092/// in this diagnostic should be unbound, which will correspond to the string
2093/// describing the template arguments for the function template specialization.
2094///
2095/// \param Index if non-NULL and the result of this function is non-nULL,
2096/// receives the index corresponding to the resulting function template
2097/// specialization.
2098///
2099/// \returns the most specialized function template specialization, if
John McCallc373d482010-01-27 01:50:18 +00002100/// found. Otherwise, returns SpecEnd.
Douglas Gregord5a423b2009-09-25 18:43:00 +00002101///
2102/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
2103/// template argument deduction.
John McCallc373d482010-01-27 01:50:18 +00002104UnresolvedSetIterator
2105Sema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
2106 UnresolvedSetIterator SpecEnd,
2107 TemplatePartialOrderingContext TPOC,
2108 SourceLocation Loc,
2109 const PartialDiagnostic &NoneDiag,
2110 const PartialDiagnostic &AmbigDiag,
2111 const PartialDiagnostic &CandidateDiag) {
2112 if (SpecBegin == SpecEnd) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00002113 Diag(Loc, NoneDiag);
John McCallc373d482010-01-27 01:50:18 +00002114 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002115 }
2116
John McCallc373d482010-01-27 01:50:18 +00002117 if (SpecBegin + 1 == SpecEnd)
2118 return SpecBegin;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002119
2120 // Find the function template that is better than all of the templates it
2121 // has been compared to.
John McCallc373d482010-01-27 01:50:18 +00002122 UnresolvedSetIterator Best = SpecBegin;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002123 FunctionTemplateDecl *BestTemplate
John McCallc373d482010-01-27 01:50:18 +00002124 = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002125 assert(BestTemplate && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00002126 for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
2127 FunctionTemplateDecl *Challenger
2128 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002129 assert(Challenger && "Not a function template specialization?");
John McCallc373d482010-01-27 01:50:18 +00002130 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCall5769d612010-02-08 23:07:23 +00002131 Loc, TPOC),
Douglas Gregord5a423b2009-09-25 18:43:00 +00002132 Challenger)) {
2133 Best = I;
2134 BestTemplate = Challenger;
2135 }
2136 }
2137
2138 // Make sure that the "best" function template is more specialized than all
2139 // of the others.
2140 bool Ambiguous = false;
John McCallc373d482010-01-27 01:50:18 +00002141 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
2142 FunctionTemplateDecl *Challenger
2143 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregord5a423b2009-09-25 18:43:00 +00002144 if (I != Best &&
2145 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCall5769d612010-02-08 23:07:23 +00002146 Loc, TPOC),
Douglas Gregord5a423b2009-09-25 18:43:00 +00002147 BestTemplate)) {
2148 Ambiguous = true;
2149 break;
2150 }
2151 }
2152
2153 if (!Ambiguous) {
2154 // We found an answer. Return it.
John McCallc373d482010-01-27 01:50:18 +00002155 return Best;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002156 }
2157
2158 // Diagnose the ambiguity.
2159 Diag(Loc, AmbigDiag);
2160
2161 // FIXME: Can we order the candidates in some sane way?
John McCallc373d482010-01-27 01:50:18 +00002162 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
2163 Diag((*I)->getLocation(), CandidateDiag)
Douglas Gregord5a423b2009-09-25 18:43:00 +00002164 << getTemplateArgumentBindingsText(
John McCallc373d482010-01-27 01:50:18 +00002165 cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
2166 *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
Douglas Gregord5a423b2009-09-25 18:43:00 +00002167
John McCallc373d482010-01-27 01:50:18 +00002168 return SpecEnd;
Douglas Gregord5a423b2009-09-25 18:43:00 +00002169}
2170
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002171/// \brief Returns the more specialized class template partial specialization
2172/// according to the rules of partial ordering of class template partial
2173/// specializations (C++ [temp.class.order]).
2174///
2175/// \param PS1 the first class template partial specialization
2176///
2177/// \param PS2 the second class template partial specialization
2178///
2179/// \returns the more specialized class template partial specialization. If
2180/// neither partial specialization is more specialized, returns NULL.
2181ClassTemplatePartialSpecializationDecl *
2182Sema::getMoreSpecializedPartialSpecialization(
2183 ClassTemplatePartialSpecializationDecl *PS1,
John McCall5769d612010-02-08 23:07:23 +00002184 ClassTemplatePartialSpecializationDecl *PS2,
2185 SourceLocation Loc) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002186 // C++ [temp.class.order]p1:
2187 // For two class template partial specializations, the first is at least as
2188 // specialized as the second if, given the following rewrite to two
2189 // function templates, the first function template is at least as
2190 // specialized as the second according to the ordering rules for function
2191 // templates (14.6.6.2):
2192 // - the first function template has the same template parameters as the
2193 // first partial specialization and has a single function parameter
2194 // whose type is a class template specialization with the template
2195 // arguments of the first partial specialization, and
2196 // - the second function template has the same template parameters as the
2197 // second partial specialization and has a single function parameter
2198 // whose type is a class template specialization with the template
2199 // arguments of the second partial specialization.
2200 //
2201 // Rather than synthesize function templates, we merely perform the
2202 // equivalent partial ordering by performing deduction directly on the
2203 // template arguments of the class template partial specializations. This
2204 // computation is slightly simpler than the general problem of function
2205 // template partial ordering, because class template partial specializations
2206 // are more constrained. We know that every template parameter is deduc
2207 llvm::SmallVector<TemplateArgument, 4> Deduced;
John McCall5769d612010-02-08 23:07:23 +00002208 Sema::TemplateDeductionInfo Info(Context, Loc);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002209
2210 // Determine whether PS1 is at least as specialized as PS2
2211 Deduced.resize(PS2->getTemplateParameters()->size());
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002212 bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002213 PS2->getTemplateParameters(),
2214 Context.getTypeDeclType(PS2),
2215 Context.getTypeDeclType(PS1),
2216 Info,
2217 Deduced,
2218 0);
2219
2220 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregordb0d4b72009-11-11 23:06:43 +00002221 Deduced.clear();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002222 Deduced.resize(PS1->getTemplateParameters()->size());
Chandler Carrutha7ef1302010-02-07 21:33:28 +00002223 bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002224 PS1->getTemplateParameters(),
2225 Context.getTypeDeclType(PS1),
2226 Context.getTypeDeclType(PS2),
2227 Info,
2228 Deduced,
2229 0);
2230
2231 if (Better1 == Better2)
2232 return 0;
2233
2234 return Better1? PS1 : PS2;
2235}
2236
Mike Stump1eb44332009-09-09 15:08:12 +00002237static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002238MarkUsedTemplateParameters(Sema &SemaRef,
2239 const TemplateArgument &TemplateArg,
2240 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002241 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002242 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002243
Douglas Gregore73bb602009-09-14 21:25:05 +00002244/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002245/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00002246static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002247MarkUsedTemplateParameters(Sema &SemaRef,
2248 const Expr *E,
2249 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002250 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002251 llvm::SmallVectorImpl<bool> &Used) {
2252 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2253 // find other occurrences of template parameters.
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002254 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregorc781f9c2010-01-14 18:13:22 +00002255 if (!DRE)
Douglas Gregor031a5882009-06-13 00:26:55 +00002256 return;
2257
Mike Stump1eb44332009-09-09 15:08:12 +00002258 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00002259 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2260 if (!NTTP)
2261 return;
2262
Douglas Gregored9c0f92009-10-29 00:04:11 +00002263 if (NTTP->getDepth() == Depth)
2264 Used[NTTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002265}
2266
Douglas Gregore73bb602009-09-14 21:25:05 +00002267/// \brief Mark the template parameters that are used by the given
2268/// nested name specifier.
2269static void
2270MarkUsedTemplateParameters(Sema &SemaRef,
2271 NestedNameSpecifier *NNS,
2272 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002273 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002274 llvm::SmallVectorImpl<bool> &Used) {
2275 if (!NNS)
2276 return;
2277
Douglas Gregored9c0f92009-10-29 00:04:11 +00002278 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2279 Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002280 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002281 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002282}
2283
2284/// \brief Mark the template parameters that are used by the given
2285/// template name.
2286static void
2287MarkUsedTemplateParameters(Sema &SemaRef,
2288 TemplateName Name,
2289 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002290 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002291 llvm::SmallVectorImpl<bool> &Used) {
2292 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2293 if (TemplateTemplateParmDecl *TTP
Douglas Gregored9c0f92009-10-29 00:04:11 +00002294 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2295 if (TTP->getDepth() == Depth)
2296 Used[TTP->getIndex()] = true;
2297 }
Douglas Gregore73bb602009-09-14 21:25:05 +00002298 return;
2299 }
2300
Douglas Gregor788cd062009-11-11 01:00:40 +00002301 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2302 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2303 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002304 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
Douglas Gregored9c0f92009-10-29 00:04:11 +00002305 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2306 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002307}
2308
2309/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002310/// type.
Mike Stump1eb44332009-09-09 15:08:12 +00002311static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002312MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2313 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002314 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002315 llvm::SmallVectorImpl<bool> &Used) {
2316 if (T.isNull())
2317 return;
2318
Douglas Gregor031a5882009-06-13 00:26:55 +00002319 // Non-dependent types have nothing deducible
2320 if (!T->isDependentType())
2321 return;
2322
2323 T = SemaRef.Context.getCanonicalType(T);
2324 switch (T->getTypeClass()) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002325 case Type::Pointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002326 MarkUsedTemplateParameters(SemaRef,
2327 cast<PointerType>(T)->getPointeeType(),
2328 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002329 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002330 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002331 break;
2332
2333 case Type::BlockPointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002334 MarkUsedTemplateParameters(SemaRef,
2335 cast<BlockPointerType>(T)->getPointeeType(),
2336 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002337 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002338 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002339 break;
2340
2341 case Type::LValueReference:
2342 case Type::RValueReference:
Douglas Gregore73bb602009-09-14 21:25:05 +00002343 MarkUsedTemplateParameters(SemaRef,
2344 cast<ReferenceType>(T)->getPointeeType(),
2345 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002346 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002347 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002348 break;
2349
2350 case Type::MemberPointer: {
2351 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore73bb602009-09-14 21:25:05 +00002352 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002353 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002354 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002355 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002356 break;
2357 }
2358
2359 case Type::DependentSizedArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002360 MarkUsedTemplateParameters(SemaRef,
2361 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002362 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002363 // Fall through to check the element type
2364
2365 case Type::ConstantArray:
2366 case Type::IncompleteArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002367 MarkUsedTemplateParameters(SemaRef,
2368 cast<ArrayType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002369 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002370 break;
2371
2372 case Type::Vector:
2373 case Type::ExtVector:
Douglas Gregore73bb602009-09-14 21:25:05 +00002374 MarkUsedTemplateParameters(SemaRef,
2375 cast<VectorType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002376 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002377 break;
2378
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002379 case Type::DependentSizedExtVector: {
2380 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002381 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002382 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002383 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002384 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002385 Depth, Used);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002386 break;
2387 }
2388
Douglas Gregor031a5882009-06-13 00:26:55 +00002389 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002390 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002391 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002392 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002393 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore73bb602009-09-14 21:25:05 +00002394 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002395 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002396 break;
2397 }
2398
Douglas Gregored9c0f92009-10-29 00:04:11 +00002399 case Type::TemplateTypeParm: {
2400 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2401 if (TTP->getDepth() == Depth)
2402 Used[TTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002403 break;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002404 }
Douglas Gregor031a5882009-06-13 00:26:55 +00002405
2406 case Type::TemplateSpecialization: {
Mike Stump1eb44332009-09-09 15:08:12 +00002407 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002408 = cast<TemplateSpecializationType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002409 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002410 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002411 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002412 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2413 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002414 break;
2415 }
2416
Douglas Gregore73bb602009-09-14 21:25:05 +00002417 case Type::Complex:
2418 if (!OnlyDeduced)
2419 MarkUsedTemplateParameters(SemaRef,
2420 cast<ComplexType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002421 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002422 break;
2423
2424 case Type::Typename:
2425 if (!OnlyDeduced)
2426 MarkUsedTemplateParameters(SemaRef,
2427 cast<TypenameType>(T)->getQualifier(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002428 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002429 break;
2430
John McCallad5e7382010-03-01 23:49:17 +00002431 case Type::TypeOf:
2432 if (!OnlyDeduced)
2433 MarkUsedTemplateParameters(SemaRef,
2434 cast<TypeOfType>(T)->getUnderlyingType(),
2435 OnlyDeduced, Depth, Used);
2436 break;
2437
2438 case Type::TypeOfExpr:
2439 if (!OnlyDeduced)
2440 MarkUsedTemplateParameters(SemaRef,
2441 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
2442 OnlyDeduced, Depth, Used);
2443 break;
2444
2445 case Type::Decltype:
2446 if (!OnlyDeduced)
2447 MarkUsedTemplateParameters(SemaRef,
2448 cast<DecltypeType>(T)->getUnderlyingExpr(),
2449 OnlyDeduced, Depth, Used);
2450 break;
2451
Douglas Gregore73bb602009-09-14 21:25:05 +00002452 // None of these types have any template parameters in them.
Douglas Gregor031a5882009-06-13 00:26:55 +00002453 case Type::Builtin:
Douglas Gregor031a5882009-06-13 00:26:55 +00002454 case Type::VariableArray:
2455 case Type::FunctionNoProto:
2456 case Type::Record:
2457 case Type::Enum:
Douglas Gregor031a5882009-06-13 00:26:55 +00002458 case Type::ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002459 case Type::ObjCObjectPointer:
John McCalled976492009-12-04 22:46:56 +00002460 case Type::UnresolvedUsing:
Douglas Gregor031a5882009-06-13 00:26:55 +00002461#define TYPE(Class, Base)
2462#define ABSTRACT_TYPE(Class, Base)
2463#define DEPENDENT_TYPE(Class, Base)
2464#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2465#include "clang/AST/TypeNodes.def"
2466 break;
2467 }
2468}
2469
Douglas Gregore73bb602009-09-14 21:25:05 +00002470/// \brief Mark the template parameters that are used by this
Douglas Gregor031a5882009-06-13 00:26:55 +00002471/// template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00002472static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002473MarkUsedTemplateParameters(Sema &SemaRef,
2474 const TemplateArgument &TemplateArg,
2475 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002476 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002477 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002478 switch (TemplateArg.getKind()) {
2479 case TemplateArgument::Null:
2480 case TemplateArgument::Integral:
Douglas Gregor788cd062009-11-11 01:00:40 +00002481 case TemplateArgument::Declaration:
Douglas Gregor031a5882009-06-13 00:26:55 +00002482 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002483
Douglas Gregor031a5882009-06-13 00:26:55 +00002484 case TemplateArgument::Type:
Douglas Gregore73bb602009-09-14 21:25:05 +00002485 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002486 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002487 break;
2488
Douglas Gregor788cd062009-11-11 01:00:40 +00002489 case TemplateArgument::Template:
2490 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2491 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002492 break;
2493
2494 case TemplateArgument::Expression:
Douglas Gregore73bb602009-09-14 21:25:05 +00002495 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002496 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002497 break;
Douglas Gregore73bb602009-09-14 21:25:05 +00002498
Anders Carlssond01b1da2009-06-15 17:04:53 +00002499 case TemplateArgument::Pack:
Douglas Gregore73bb602009-09-14 21:25:05 +00002500 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2501 PEnd = TemplateArg.pack_end();
2502 P != PEnd; ++P)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002503 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssond01b1da2009-06-15 17:04:53 +00002504 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00002505 }
2506}
2507
2508/// \brief Mark the template parameters can be deduced by the given
2509/// template argument list.
2510///
2511/// \param TemplateArgs the template argument list from which template
2512/// parameters will be deduced.
2513///
2514/// \param Deduced a bit vector whose elements will be set to \c true
2515/// to indicate when the corresponding template parameter will be
2516/// deduced.
Mike Stump1eb44332009-09-09 15:08:12 +00002517void
Douglas Gregore73bb602009-09-14 21:25:05 +00002518Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002519 bool OnlyDeduced, unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002520 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002521 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002522 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2523 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002524}
Douglas Gregor63f07c52009-09-18 23:21:38 +00002525
2526/// \brief Marks all of the template parameters that will be deduced by a
2527/// call to the given function template.
2528void Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
2529 llvm::SmallVectorImpl<bool> &Deduced) {
2530 TemplateParameterList *TemplateParams
2531 = FunctionTemplate->getTemplateParameters();
2532 Deduced.clear();
2533 Deduced.resize(TemplateParams->size());
2534
2535 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2536 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
2537 ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002538 true, TemplateParams->getDepth(), Deduced);
Douglas Gregor63f07c52009-09-18 23:21:38 +00002539}