blob: ed3e3c4c22eb57695464cc66ca2f717943e4c70c [file] [log] [blame]
Douglas Gregor55ca8f62009-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 Gregor0ff7d922009-09-14 18:39:43 +000020#include <algorithm>
Douglas Gregorcf0b47d2009-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 Gregorfc516c92009-06-26 23:27:24 +000040 /// deduction from a template-id of a derived class of the argument type.
Douglas Gregor406f6342009-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 Gregorcf0b47d2009-06-26 23:10:12 +000046 };
47}
48
Douglas Gregor55ca8f62009-06-04 00:03:07 +000049using namespace clang;
50
Douglas Gregor0a29a052010-03-26 05:50:28 +000051/// \brief Compare two APSInts, extending and switching the sign as
52/// necessary to compare their values regardless of underlying type.
53static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) {
54 if (Y.getBitWidth() > X.getBitWidth())
55 X.extend(Y.getBitWidth());
56 else if (Y.getBitWidth() < X.getBitWidth())
57 Y.extend(X.getBitWidth());
58
59 // If there is a signedness mismatch, correct it.
60 if (X.isSigned() != Y.isSigned()) {
61 // If the signed value is negative, then the values cannot be the same.
62 if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative()))
63 return false;
64
65 Y.setIsSigned(true);
66 X.setIsSigned(true);
67 }
68
69 return X == Y;
70}
71
Douglas Gregor181aa4a2009-06-12 18:26:56 +000072static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +000073DeduceTemplateArguments(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +000074 TemplateParameterList *TemplateParams,
75 const TemplateArgument &Param,
Douglas Gregor4fbe3e32009-06-09 16:35:58 +000076 const TemplateArgument &Arg,
Douglas Gregor181aa4a2009-06-12 18:26:56 +000077 Sema::TemplateDeductionInfo &Info,
Douglas Gregor4fbe3e32009-06-09 16:35:58 +000078 llvm::SmallVectorImpl<TemplateArgument> &Deduced);
79
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000080/// \brief If the given expression is of a form that permits the deduction
81/// of a non-type template parameter, return the declaration of that
82/// non-type template parameter.
83static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
84 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
85 E = IC->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +000086
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000087 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
88 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
Mike Stump11289f42009-09-09 15:08:12 +000089
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000090 return 0;
91}
92
Mike Stump11289f42009-09-09 15:08:12 +000093/// \brief Deduce the value of the given non-type template parameter
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000094/// from the given constant.
Douglas Gregor181aa4a2009-06-12 18:26:56 +000095static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +000096DeduceNonTypeTemplateArgument(Sema &S,
Mike Stump11289f42009-09-09 15:08:12 +000097 NonTypeTemplateParmDecl *NTTP,
Douglas Gregor0a29a052010-03-26 05:50:28 +000098 llvm::APSInt Value, QualType ValueType,
Douglas Gregor181aa4a2009-06-12 18:26:56 +000099 Sema::TemplateDeductionInfo &Info,
100 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Mike Stump11289f42009-09-09 15:08:12 +0000101 assert(NTTP->getDepth() == 0 &&
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000102 "Cannot deduce non-type template argument with depth > 0");
Mike Stump11289f42009-09-09 15:08:12 +0000103
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000104 if (Deduced[NTTP->getIndex()].isNull()) {
Douglas Gregor0a29a052010-03-26 05:50:28 +0000105 Deduced[NTTP->getIndex()] = TemplateArgument(Value, ValueType);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000106 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000107 }
Mike Stump11289f42009-09-09 15:08:12 +0000108
Douglas Gregor0a29a052010-03-26 05:50:28 +0000109 if (Deduced[NTTP->getIndex()].getKind() != TemplateArgument::Integral) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000110 Info.Param = NTTP;
111 Info.FirstArg = Deduced[NTTP->getIndex()];
Douglas Gregor0a29a052010-03-26 05:50:28 +0000112 Info.SecondArg = TemplateArgument(Value, ValueType);
113 return Sema::TDK_Inconsistent;
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000114 }
115
Douglas Gregor0a29a052010-03-26 05:50:28 +0000116 // Extent the smaller of the two values.
117 llvm::APSInt PrevValue = *Deduced[NTTP->getIndex()].getAsIntegral();
118 if (!hasSameExtendedValue(PrevValue, Value)) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000119 Info.Param = NTTP;
120 Info.FirstArg = Deduced[NTTP->getIndex()];
Douglas Gregor0a29a052010-03-26 05:50:28 +0000121 Info.SecondArg = TemplateArgument(Value, ValueType);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000122 return Sema::TDK_Inconsistent;
123 }
124
125 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000126}
127
Mike Stump11289f42009-09-09 15:08:12 +0000128/// \brief Deduce the value of the given non-type template parameter
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000129/// from the given type- or value-dependent expression.
130///
131/// \returns true if deduction succeeded, false otherwise.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000132static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000133DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000134 NonTypeTemplateParmDecl *NTTP,
135 Expr *Value,
136 Sema::TemplateDeductionInfo &Info,
137 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Mike Stump11289f42009-09-09 15:08:12 +0000138 assert(NTTP->getDepth() == 0 &&
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000139 "Cannot deduce non-type template argument with depth > 0");
140 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
141 "Expression template argument must be type- or value-dependent.");
Mike Stump11289f42009-09-09 15:08:12 +0000142
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000143 if (Deduced[NTTP->getIndex()].isNull()) {
Douglas Gregor0a29a052010-03-26 05:50:28 +0000144 Deduced[NTTP->getIndex()] = TemplateArgument(Value->Retain());
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000145 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000146 }
Mike Stump11289f42009-09-09 15:08:12 +0000147
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000148 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
Mike Stump11289f42009-09-09 15:08:12 +0000149 // Okay, we deduced a constant in one case and a dependent expression
150 // in another case. FIXME: Later, we will check that instantiating the
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000151 // dependent expression gives us the constant value.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000152 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000153 }
Mike Stump11289f42009-09-09 15:08:12 +0000154
Douglas Gregor00a511f2009-09-15 16:51:42 +0000155 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
156 // Compare the expressions for equality
157 llvm::FoldingSetNodeID ID1, ID2;
Chandler Carruthc1263112010-02-07 21:33:28 +0000158 Deduced[NTTP->getIndex()].getAsExpr()->Profile(ID1, S.Context, true);
159 Value->Profile(ID2, S.Context, true);
Douglas Gregor00a511f2009-09-15 16:51:42 +0000160 if (ID1 == ID2)
161 return Sema::TDK_Success;
162
163 // FIXME: Fill in argument mismatch information
164 return Sema::TDK_NonDeducedMismatch;
165 }
166
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000167 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000168}
169
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000170/// \brief Deduce the value of the given non-type template parameter
171/// from the given declaration.
172///
173/// \returns true if deduction succeeded, false otherwise.
174static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000175DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000176 NonTypeTemplateParmDecl *NTTP,
177 Decl *D,
178 Sema::TemplateDeductionInfo &Info,
179 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
180 assert(NTTP->getDepth() == 0 &&
181 "Cannot deduce non-type template argument with depth > 0");
182
183 if (Deduced[NTTP->getIndex()].isNull()) {
184 Deduced[NTTP->getIndex()] = TemplateArgument(D->getCanonicalDecl());
185 return Sema::TDK_Success;
186 }
187
188 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
189 // Okay, we deduced a declaration in one case and a dependent expression
190 // in another case.
191 return Sema::TDK_Success;
192 }
193
194 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Declaration) {
195 // Compare the declarations for equality
196 if (Deduced[NTTP->getIndex()].getAsDecl()->getCanonicalDecl() ==
197 D->getCanonicalDecl())
198 return Sema::TDK_Success;
199
200 // FIXME: Fill in argument mismatch information
201 return Sema::TDK_NonDeducedMismatch;
202 }
203
204 return Sema::TDK_Success;
205}
206
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000207static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000208DeduceTemplateArguments(Sema &S,
Douglas Gregoradee3e32009-11-11 23:06:43 +0000209 TemplateParameterList *TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000210 TemplateName Param,
211 TemplateName Arg,
212 Sema::TemplateDeductionInfo &Info,
213 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000214 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
Douglas Gregoradee3e32009-11-11 23:06:43 +0000215 if (!ParamDecl) {
216 // The parameter type is dependent and is not a template template parameter,
217 // so there is nothing that we can deduce.
218 return Sema::TDK_Success;
219 }
220
221 if (TemplateTemplateParmDecl *TempParam
222 = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) {
223 // Bind the template template parameter to the given template name.
224 TemplateArgument &ExistingArg = Deduced[TempParam->getIndex()];
225 if (ExistingArg.isNull()) {
226 // This is the first deduction for this template template parameter.
Chandler Carruthc1263112010-02-07 21:33:28 +0000227 ExistingArg = TemplateArgument(S.Context.getCanonicalTemplateName(Arg));
Douglas Gregoradee3e32009-11-11 23:06:43 +0000228 return Sema::TDK_Success;
229 }
230
231 // Verify that the previous binding matches this deduction.
232 assert(ExistingArg.getKind() == TemplateArgument::Template);
Chandler Carruthc1263112010-02-07 21:33:28 +0000233 if (S.Context.hasSameTemplateName(ExistingArg.getAsTemplate(), Arg))
Douglas Gregoradee3e32009-11-11 23:06:43 +0000234 return Sema::TDK_Success;
235
236 // Inconsistent deduction.
237 Info.Param = TempParam;
238 Info.FirstArg = ExistingArg;
239 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000240 return Sema::TDK_Inconsistent;
241 }
Douglas Gregoradee3e32009-11-11 23:06:43 +0000242
243 // Verify that the two template names are equivalent.
Chandler Carruthc1263112010-02-07 21:33:28 +0000244 if (S.Context.hasSameTemplateName(Param, Arg))
Douglas Gregoradee3e32009-11-11 23:06:43 +0000245 return Sema::TDK_Success;
246
247 // Mismatch of non-dependent template parameter to argument.
248 Info.FirstArg = TemplateArgument(Param);
249 Info.SecondArg = TemplateArgument(Arg);
250 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000251}
252
Mike Stump11289f42009-09-09 15:08:12 +0000253/// \brief Deduce the template arguments by comparing the template parameter
Douglas Gregore81f3e72009-07-07 23:09:34 +0000254/// type (which is a template-id) with the template argument type.
255///
Chandler Carruthc1263112010-02-07 21:33:28 +0000256/// \param S the Sema
Douglas Gregore81f3e72009-07-07 23:09:34 +0000257///
258/// \param TemplateParams the template parameters that we are deducing
259///
260/// \param Param the parameter type
261///
262/// \param Arg the argument type
263///
264/// \param Info information about the template argument deduction itself
265///
266/// \param Deduced the deduced template arguments
267///
268/// \returns the result of template argument deduction so far. Note that a
269/// "success" result means that template argument deduction has not yet failed,
270/// but it may still fail, later, for other reasons.
271static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000272DeduceTemplateArguments(Sema &S,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000273 TemplateParameterList *TemplateParams,
274 const TemplateSpecializationType *Param,
275 QualType Arg,
276 Sema::TemplateDeductionInfo &Info,
277 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
John McCallb692a092009-10-22 20:10:53 +0000278 assert(Arg.isCanonical() && "Argument type must be canonical");
Mike Stump11289f42009-09-09 15:08:12 +0000279
Douglas Gregore81f3e72009-07-07 23:09:34 +0000280 // Check whether the template argument is a dependent template-id.
Mike Stump11289f42009-09-09 15:08:12 +0000281 if (const TemplateSpecializationType *SpecArg
Douglas Gregore81f3e72009-07-07 23:09:34 +0000282 = dyn_cast<TemplateSpecializationType>(Arg)) {
283 // Perform template argument deduction for the template name.
284 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000285 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000286 Param->getTemplateName(),
287 SpecArg->getTemplateName(),
288 Info, Deduced))
289 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000290
Mike Stump11289f42009-09-09 15:08:12 +0000291
Douglas Gregore81f3e72009-07-07 23:09:34 +0000292 // Perform template argument deduction on each template
293 // argument.
Douglas Gregoradee3e32009-11-11 23:06:43 +0000294 unsigned NumArgs = std::min(SpecArg->getNumArgs(), Param->getNumArgs());
Douglas Gregore81f3e72009-07-07 23:09:34 +0000295 for (unsigned I = 0; I != NumArgs; ++I)
296 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000297 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000298 Param->getArg(I),
299 SpecArg->getArg(I),
300 Info, Deduced))
301 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000302
Douglas Gregore81f3e72009-07-07 23:09:34 +0000303 return Sema::TDK_Success;
304 }
Mike Stump11289f42009-09-09 15:08:12 +0000305
Douglas Gregore81f3e72009-07-07 23:09:34 +0000306 // If the argument type is a class template specialization, we
307 // perform template argument deduction using its template
308 // arguments.
309 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
310 if (!RecordArg)
311 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000312
313 ClassTemplateSpecializationDecl *SpecArg
Douglas Gregore81f3e72009-07-07 23:09:34 +0000314 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
315 if (!SpecArg)
316 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000317
Douglas Gregore81f3e72009-07-07 23:09:34 +0000318 // Perform template argument deduction for the template name.
319 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000320 = DeduceTemplateArguments(S,
Douglas Gregoradee3e32009-11-11 23:06:43 +0000321 TemplateParams,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000322 Param->getTemplateName(),
323 TemplateName(SpecArg->getSpecializedTemplate()),
324 Info, Deduced))
325 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000326
Douglas Gregore81f3e72009-07-07 23:09:34 +0000327 unsigned NumArgs = Param->getNumArgs();
328 const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
329 if (NumArgs != ArgArgs.size())
330 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000331
Douglas Gregore81f3e72009-07-07 23:09:34 +0000332 for (unsigned I = 0; I != NumArgs; ++I)
Mike Stump11289f42009-09-09 15:08:12 +0000333 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000334 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000335 Param->getArg(I),
336 ArgArgs.get(I),
337 Info, Deduced))
338 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000339
Douglas Gregore81f3e72009-07-07 23:09:34 +0000340 return Sema::TDK_Success;
341}
342
Douglas Gregorcceb9752009-06-26 18:27:22 +0000343/// \brief Deduce the template arguments by comparing the parameter type and
344/// the argument type (C++ [temp.deduct.type]).
345///
Chandler Carruthc1263112010-02-07 21:33:28 +0000346/// \param S the semantic analysis object within which we are deducing
Douglas Gregorcceb9752009-06-26 18:27:22 +0000347///
348/// \param TemplateParams the template parameters that we are deducing
349///
350/// \param ParamIn the parameter type
351///
352/// \param ArgIn the argument type
353///
354/// \param Info information about the template argument deduction itself
355///
356/// \param Deduced the deduced template arguments
357///
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000358/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
Mike Stump11289f42009-09-09 15:08:12 +0000359/// how template argument deduction is performed.
Douglas Gregorcceb9752009-06-26 18:27:22 +0000360///
361/// \returns the result of template argument deduction so far. Note that a
362/// "success" result means that template argument deduction has not yet failed,
363/// but it may still fail, later, for other reasons.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000364static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000365DeduceTemplateArguments(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000366 TemplateParameterList *TemplateParams,
367 QualType ParamIn, QualType ArgIn,
368 Sema::TemplateDeductionInfo &Info,
Douglas Gregorcceb9752009-06-26 18:27:22 +0000369 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000370 unsigned TDF) {
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000371 // We only want to look at the canonical types, since typedefs and
372 // sugar are not part of template argument deduction.
Chandler Carruthc1263112010-02-07 21:33:28 +0000373 QualType Param = S.Context.getCanonicalType(ParamIn);
374 QualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000375
Douglas Gregorcceb9752009-06-26 18:27:22 +0000376 // C++0x [temp.deduct.call]p4 bullet 1:
377 // - If the original P is a reference type, the deduced A (i.e., the type
Mike Stump11289f42009-09-09 15:08:12 +0000378 // referred to by the reference) can be more cv-qualified than the
Douglas Gregorcceb9752009-06-26 18:27:22 +0000379 // transformed A.
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000380 if (TDF & TDF_ParamWithReferenceType) {
Chandler Carruthc712ce12009-12-30 04:10:01 +0000381 Qualifiers Quals;
Chandler Carruthc1263112010-02-07 21:33:28 +0000382 QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals);
Chandler Carruthc712ce12009-12-30 04:10:01 +0000383 Quals.setCVRQualifiers(Quals.getCVRQualifiers() &
384 Arg.getCVRQualifiersThroughArrayTypes());
Chandler Carruthc1263112010-02-07 21:33:28 +0000385 Param = S.Context.getQualifiedType(UnqualParam, Quals);
Douglas Gregorcceb9752009-06-26 18:27:22 +0000386 }
Mike Stump11289f42009-09-09 15:08:12 +0000387
Douglas Gregor705c9002009-06-26 20:57:09 +0000388 // If the parameter type is not dependent, there is nothing to deduce.
Douglas Gregor406f6342009-09-14 20:00:47 +0000389 if (!Param->isDependentType()) {
390 if (!(TDF & TDF_SkipNonDependent) && Param != Arg) {
391
392 return Sema::TDK_NonDeducedMismatch;
393 }
394
Douglas Gregor705c9002009-06-26 20:57:09 +0000395 return Sema::TDK_Success;
Douglas Gregor406f6342009-09-14 20:00:47 +0000396 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000397
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000398 // C++ [temp.deduct.type]p9:
Mike Stump11289f42009-09-09 15:08:12 +0000399 // A template type argument T, a template template argument TT or a
400 // template non-type argument i can be deduced if P and A have one of
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000401 // the following forms:
402 //
403 // T
404 // cv-list T
Mike Stump11289f42009-09-09 15:08:12 +0000405 if (const TemplateTypeParmType *TemplateTypeParm
John McCall9dd450b2009-09-21 23:43:11 +0000406 = Param->getAs<TemplateTypeParmType>()) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000407 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregord6605db2009-07-22 21:30:48 +0000408 bool RecanonicalizeArg = false;
Mike Stump11289f42009-09-09 15:08:12 +0000409
Douglas Gregor60454822009-07-22 20:02:25 +0000410 // If the argument type is an array type, move the qualifiers up to the
411 // top level, so they can be matched with the qualifiers on the parameter.
412 // FIXME: address spaces, ObjC GC qualifiers
Douglas Gregord6605db2009-07-22 21:30:48 +0000413 if (isa<ArrayType>(Arg)) {
John McCall8ccfcb52009-09-24 19:53:00 +0000414 Qualifiers Quals;
Chandler Carruthc1263112010-02-07 21:33:28 +0000415 Arg = S.Context.getUnqualifiedArrayType(Arg, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +0000416 if (Quals) {
Chandler Carruthc1263112010-02-07 21:33:28 +0000417 Arg = S.Context.getQualifiedType(Arg, Quals);
Douglas Gregord6605db2009-07-22 21:30:48 +0000418 RecanonicalizeArg = true;
419 }
420 }
Mike Stump11289f42009-09-09 15:08:12 +0000421
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000422 // The argument type can not be less qualified than the parameter
423 // type.
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000424 if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000425 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
426 Info.FirstArg = Deduced[Index];
John McCall0ad16662009-10-29 08:12:44 +0000427 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000428 return Sema::TDK_InconsistentQuals;
429 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000430
431 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
Chandler Carruthc1263112010-02-07 21:33:28 +0000432 assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function");
John McCall8ccfcb52009-09-24 19:53:00 +0000433 QualType DeducedType = Arg;
434 DeducedType.removeCVRQualifiers(Param.getCVRQualifiers());
Douglas Gregord6605db2009-07-22 21:30:48 +0000435 if (RecanonicalizeArg)
Chandler Carruthc1263112010-02-07 21:33:28 +0000436 DeducedType = S.Context.getCanonicalType(DeducedType);
Mike Stump11289f42009-09-09 15:08:12 +0000437
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000438 if (Deduced[Index].isNull())
John McCall0ad16662009-10-29 08:12:44 +0000439 Deduced[Index] = TemplateArgument(DeducedType);
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000440 else {
Mike Stump11289f42009-09-09 15:08:12 +0000441 // C++ [temp.deduct.type]p2:
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000442 // [...] If type deduction cannot be done for any P/A pair, or if for
Mike Stump11289f42009-09-09 15:08:12 +0000443 // any pair the deduction leads to more than one possible set of
444 // deduced values, or if different pairs yield different deduced
445 // values, or if any template argument remains neither deduced nor
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000446 // explicitly specified, template argument deduction fails.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000447 if (Deduced[Index].getAsType() != DeducedType) {
Mike Stump11289f42009-09-09 15:08:12 +0000448 Info.Param
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000449 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
450 Info.FirstArg = Deduced[Index];
John McCall0ad16662009-10-29 08:12:44 +0000451 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000452 return Sema::TDK_Inconsistent;
453 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000454 }
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000455 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000456 }
457
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000458 // Set up the template argument deduction information for a failure.
John McCall0ad16662009-10-29 08:12:44 +0000459 Info.FirstArg = TemplateArgument(ParamIn);
460 Info.SecondArg = TemplateArgument(ArgIn);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000461
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000462 // Check the cv-qualifiers on the parameter and argument types.
463 if (!(TDF & TDF_IgnoreQualifiers)) {
464 if (TDF & TDF_ParamWithReferenceType) {
465 if (Param.isMoreQualifiedThan(Arg))
466 return Sema::TDK_NonDeducedMismatch;
467 } else {
468 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Mike Stump11289f42009-09-09 15:08:12 +0000469 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000470 }
471 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000472
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000473 switch (Param->getTypeClass()) {
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000474 // No deduction possible for these types
475 case Type::Builtin:
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000476 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000477
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000478 // T *
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000479 case Type::Pointer: {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000480 const PointerType *PointerArg = Arg->getAs<PointerType>();
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000481 if (!PointerArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000482 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000483
Douglas Gregorfc516c92009-06-26 23:27:24 +0000484 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Chandler Carruthc1263112010-02-07 21:33:28 +0000485 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000486 cast<PointerType>(Param)->getPointeeType(),
487 PointerArg->getPointeeType(),
Douglas Gregorfc516c92009-06-26 23:27:24 +0000488 Info, Deduced, SubTDF);
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000489 }
Mike Stump11289f42009-09-09 15:08:12 +0000490
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000491 // T &
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000492 case Type::LValueReference: {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000493 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000494 if (!ReferenceArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000495 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000496
Chandler Carruthc1263112010-02-07 21:33:28 +0000497 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000498 cast<LValueReferenceType>(Param)->getPointeeType(),
499 ReferenceArg->getPointeeType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000500 Info, Deduced, 0);
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000501 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000502
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000503 // T && [C++0x]
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000504 case Type::RValueReference: {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000505 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000506 if (!ReferenceArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000507 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000508
Chandler Carruthc1263112010-02-07 21:33:28 +0000509 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000510 cast<RValueReferenceType>(Param)->getPointeeType(),
511 ReferenceArg->getPointeeType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000512 Info, Deduced, 0);
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000513 }
Mike Stump11289f42009-09-09 15:08:12 +0000514
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000515 // T [] (implied, but not stated explicitly)
Anders Carlsson35533d12009-06-04 04:11:30 +0000516 case Type::IncompleteArray: {
Mike Stump11289f42009-09-09 15:08:12 +0000517 const IncompleteArrayType *IncompleteArrayArg =
Chandler Carruthc1263112010-02-07 21:33:28 +0000518 S.Context.getAsIncompleteArrayType(Arg);
Anders Carlsson35533d12009-06-04 04:11:30 +0000519 if (!IncompleteArrayArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000520 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000521
Chandler Carruthc1263112010-02-07 21:33:28 +0000522 return DeduceTemplateArguments(S, TemplateParams,
523 S.Context.getAsIncompleteArrayType(Param)->getElementType(),
Anders Carlsson35533d12009-06-04 04:11:30 +0000524 IncompleteArrayArg->getElementType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000525 Info, Deduced, 0);
Anders Carlsson35533d12009-06-04 04:11:30 +0000526 }
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000527
528 // T [integer-constant]
Anders Carlsson35533d12009-06-04 04:11:30 +0000529 case Type::ConstantArray: {
Mike Stump11289f42009-09-09 15:08:12 +0000530 const ConstantArrayType *ConstantArrayArg =
Chandler Carruthc1263112010-02-07 21:33:28 +0000531 S.Context.getAsConstantArrayType(Arg);
Anders Carlsson35533d12009-06-04 04:11:30 +0000532 if (!ConstantArrayArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000533 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000534
535 const ConstantArrayType *ConstantArrayParm =
Chandler Carruthc1263112010-02-07 21:33:28 +0000536 S.Context.getAsConstantArrayType(Param);
Anders Carlsson35533d12009-06-04 04:11:30 +0000537 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000538 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000539
Chandler Carruthc1263112010-02-07 21:33:28 +0000540 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlsson35533d12009-06-04 04:11:30 +0000541 ConstantArrayParm->getElementType(),
542 ConstantArrayArg->getElementType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000543 Info, Deduced, 0);
Anders Carlsson35533d12009-06-04 04:11:30 +0000544 }
545
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000546 // type [i]
547 case Type::DependentSizedArray: {
Chandler Carruthc1263112010-02-07 21:33:28 +0000548 const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000549 if (!ArrayArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000550 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000551
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000552 // Check the element type of the arrays
553 const DependentSizedArrayType *DependentArrayParm
Chandler Carruthc1263112010-02-07 21:33:28 +0000554 = S.Context.getAsDependentSizedArrayType(Param);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000555 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000556 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000557 DependentArrayParm->getElementType(),
558 ArrayArg->getElementType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000559 Info, Deduced, 0))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000560 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000561
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000562 // Determine the array bound is something we can deduce.
Mike Stump11289f42009-09-09 15:08:12 +0000563 NonTypeTemplateParmDecl *NTTP
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000564 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
565 if (!NTTP)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000566 return Sema::TDK_Success;
Mike Stump11289f42009-09-09 15:08:12 +0000567
568 // We can perform template argument deduction for the given non-type
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000569 // template parameter.
Mike Stump11289f42009-09-09 15:08:12 +0000570 assert(NTTP->getDepth() == 0 &&
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000571 "Cannot deduce non-type template argument at depth > 0");
Mike Stump11289f42009-09-09 15:08:12 +0000572 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson3a106e02009-06-16 22:44:31 +0000573 = dyn_cast<ConstantArrayType>(ArrayArg)) {
574 llvm::APSInt Size(ConstantArrayArg->getSize());
Douglas Gregor0a29a052010-03-26 05:50:28 +0000575 return DeduceNonTypeTemplateArgument(S, NTTP, Size,
576 S.Context.getSizeType(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000577 Info, Deduced);
Anders Carlsson3a106e02009-06-16 22:44:31 +0000578 }
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000579 if (const DependentSizedArrayType *DependentArrayArg
580 = dyn_cast<DependentSizedArrayType>(ArrayArg))
Chandler Carruthc1263112010-02-07 21:33:28 +0000581 return DeduceNonTypeTemplateArgument(S, NTTP,
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000582 DependentArrayArg->getSizeExpr(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000583 Info, Deduced);
Mike Stump11289f42009-09-09 15:08:12 +0000584
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000585 // Incomplete type does not match a dependently-sized array type
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000586 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000587 }
Mike Stump11289f42009-09-09 15:08:12 +0000588
589 // type(*)(T)
590 // T(*)()
591 // T(*)(T)
Anders Carlsson2128ec72009-06-08 15:19:08 +0000592 case Type::FunctionProto: {
Mike Stump11289f42009-09-09 15:08:12 +0000593 const FunctionProtoType *FunctionProtoArg =
Anders Carlsson2128ec72009-06-08 15:19:08 +0000594 dyn_cast<FunctionProtoType>(Arg);
595 if (!FunctionProtoArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000596 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000597
598 const FunctionProtoType *FunctionProtoParam =
Anders Carlsson2128ec72009-06-08 15:19:08 +0000599 cast<FunctionProtoType>(Param);
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000600
Mike Stump11289f42009-09-09 15:08:12 +0000601 if (FunctionProtoParam->getTypeQuals() !=
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000602 FunctionProtoArg->getTypeQuals())
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000603 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000604
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000605 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000606 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000607
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000608 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000609 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000610
Anders Carlsson2128ec72009-06-08 15:19:08 +0000611 // Check return types.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000612 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000613 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000614 FunctionProtoParam->getResultType(),
615 FunctionProtoArg->getResultType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000616 Info, Deduced, 0))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000617 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000618
Anders Carlsson2128ec72009-06-08 15:19:08 +0000619 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
620 // Check argument types.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000621 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000622 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000623 FunctionProtoParam->getArgType(I),
624 FunctionProtoArg->getArgType(I),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000625 Info, Deduced, 0))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000626 return Result;
Anders Carlsson2128ec72009-06-08 15:19:08 +0000627 }
Mike Stump11289f42009-09-09 15:08:12 +0000628
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000629 return Sema::TDK_Success;
Anders Carlsson2128ec72009-06-08 15:19:08 +0000630 }
Mike Stump11289f42009-09-09 15:08:12 +0000631
John McCalle78aac42010-03-10 03:28:59 +0000632 case Type::InjectedClassName: {
633 // Treat a template's injected-class-name as if the template
634 // specialization type had been used.
635 Param = cast<InjectedClassNameType>(Param)->getUnderlyingType();
636 assert(isa<TemplateSpecializationType>(Param) &&
637 "injected class name is not a template specialization type");
638 // fall through
639 }
640
Douglas Gregor705c9002009-06-26 20:57:09 +0000641 // template-name<T> (where template-name refers to a class template)
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000642 // template-name<i>
Douglas Gregoradee3e32009-11-11 23:06:43 +0000643 // TT<T>
644 // TT<i>
645 // TT<>
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000646 case Type::TemplateSpecialization: {
647 const TemplateSpecializationType *SpecParam
648 = cast<TemplateSpecializationType>(Param);
Mike Stump11289f42009-09-09 15:08:12 +0000649
Douglas Gregore81f3e72009-07-07 23:09:34 +0000650 // Try to deduce template arguments from the template-id.
651 Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000652 = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000653 Info, Deduced);
Mike Stump11289f42009-09-09 15:08:12 +0000654
Douglas Gregor42909752009-09-30 22:13:51 +0000655 if (Result && (TDF & TDF_DerivedClass)) {
Douglas Gregore81f3e72009-07-07 23:09:34 +0000656 // C++ [temp.deduct.call]p3b3:
657 // If P is a class, and P has the form template-id, then A can be a
658 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump11289f42009-09-09 15:08:12 +0000659 // class of the form template-id, A can be a pointer to a derived
Douglas Gregore81f3e72009-07-07 23:09:34 +0000660 // class pointed to by the deduced A.
661 //
662 // More importantly:
Mike Stump11289f42009-09-09 15:08:12 +0000663 // These alternatives are considered only if type deduction would
Douglas Gregore81f3e72009-07-07 23:09:34 +0000664 // otherwise fail.
Chandler Carruthc1263112010-02-07 21:33:28 +0000665 if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
666 // We cannot inspect base classes as part of deduction when the type
667 // is incomplete, so either instantiate any templates necessary to
668 // complete the type, or skip over it if it cannot be completed.
John McCallbc077cf2010-02-08 23:07:23 +0000669 if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
Chandler Carruthc1263112010-02-07 21:33:28 +0000670 return Result;
671
Douglas Gregore81f3e72009-07-07 23:09:34 +0000672 // Use data recursion to crawl through the list of base classes.
Mike Stump11289f42009-09-09 15:08:12 +0000673 // Visited contains the set of nodes we have already visited, while
Douglas Gregore81f3e72009-07-07 23:09:34 +0000674 // ToVisit is our stack of records that we still need to visit.
675 llvm::SmallPtrSet<const RecordType *, 8> Visited;
676 llvm::SmallVector<const RecordType *, 8> ToVisit;
677 ToVisit.push_back(RecordT);
678 bool Successful = false;
679 while (!ToVisit.empty()) {
680 // Retrieve the next class in the inheritance hierarchy.
681 const RecordType *NextT = ToVisit.back();
682 ToVisit.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000683
Douglas Gregore81f3e72009-07-07 23:09:34 +0000684 // If we have already seen this type, skip it.
685 if (!Visited.insert(NextT))
686 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000687
Douglas Gregore81f3e72009-07-07 23:09:34 +0000688 // If this is a base class, try to perform template argument
689 // deduction from it.
690 if (NextT != RecordT) {
691 Sema::TemplateDeductionResult BaseResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000692 = DeduceTemplateArguments(S, TemplateParams, SpecParam,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000693 QualType(NextT, 0), Info, Deduced);
Mike Stump11289f42009-09-09 15:08:12 +0000694
Douglas Gregore81f3e72009-07-07 23:09:34 +0000695 // If template argument deduction for this base was successful,
696 // note that we had some success.
697 if (BaseResult == Sema::TDK_Success)
698 Successful = true;
Douglas Gregore81f3e72009-07-07 23:09:34 +0000699 }
Mike Stump11289f42009-09-09 15:08:12 +0000700
Douglas Gregore81f3e72009-07-07 23:09:34 +0000701 // Visit base classes
702 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
703 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
704 BaseEnd = Next->bases_end();
Sebastian Redl1054fae2009-10-25 17:03:50 +0000705 Base != BaseEnd; ++Base) {
Mike Stump11289f42009-09-09 15:08:12 +0000706 assert(Base->getType()->isRecordType() &&
Douglas Gregore81f3e72009-07-07 23:09:34 +0000707 "Base class that isn't a record?");
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000708 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregore81f3e72009-07-07 23:09:34 +0000709 }
710 }
Mike Stump11289f42009-09-09 15:08:12 +0000711
Douglas Gregore81f3e72009-07-07 23:09:34 +0000712 if (Successful)
713 return Sema::TDK_Success;
714 }
Mike Stump11289f42009-09-09 15:08:12 +0000715
Douglas Gregore81f3e72009-07-07 23:09:34 +0000716 }
Mike Stump11289f42009-09-09 15:08:12 +0000717
Douglas Gregore81f3e72009-07-07 23:09:34 +0000718 return Result;
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000719 }
720
Douglas Gregor637d9982009-06-10 23:47:09 +0000721 // T type::*
722 // T T::*
723 // T (type::*)()
724 // type (T::*)()
725 // type (type::*)(T)
726 // type (T::*)(T)
727 // T (type::*)(T)
728 // T (T::*)()
729 // T (T::*)(T)
730 case Type::MemberPointer: {
731 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
732 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
733 if (!MemPtrArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000734 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637d9982009-06-10 23:47:09 +0000735
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000736 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000737 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000738 MemPtrParam->getPointeeType(),
739 MemPtrArg->getPointeeType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000740 Info, Deduced,
741 TDF & TDF_IgnoreQualifiers))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000742 return Result;
743
Chandler Carruthc1263112010-02-07 21:33:28 +0000744 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000745 QualType(MemPtrParam->getClass(), 0),
746 QualType(MemPtrArg->getClass(), 0),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000747 Info, Deduced, 0);
Douglas Gregor637d9982009-06-10 23:47:09 +0000748 }
749
Anders Carlsson15f1dd12009-06-12 22:56:54 +0000750 // (clang extension)
751 //
Mike Stump11289f42009-09-09 15:08:12 +0000752 // type(^)(T)
753 // T(^)()
754 // T(^)(T)
Anders Carlssona767eee2009-06-12 16:23:10 +0000755 case Type::BlockPointer: {
756 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
757 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump11289f42009-09-09 15:08:12 +0000758
Anders Carlssona767eee2009-06-12 16:23:10 +0000759 if (!BlockPtrArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000760 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000761
Chandler Carruthc1263112010-02-07 21:33:28 +0000762 return DeduceTemplateArguments(S, TemplateParams,
Anders Carlssona767eee2009-06-12 16:23:10 +0000763 BlockPtrParam->getPointeeType(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000764 BlockPtrArg->getPointeeType(), Info,
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000765 Deduced, 0);
Anders Carlssona767eee2009-06-12 16:23:10 +0000766 }
767
Douglas Gregor637d9982009-06-10 23:47:09 +0000768 case Type::TypeOfExpr:
769 case Type::TypeOf:
770 case Type::Typename:
771 // No template argument deduction for these types
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000772 return Sema::TDK_Success;
Douglas Gregor637d9982009-06-10 23:47:09 +0000773
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000774 default:
775 break;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000776 }
777
778 // FIXME: Many more cases to go (to go).
Douglas Gregor705c9002009-06-26 20:57:09 +0000779 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000780}
781
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000782static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000783DeduceTemplateArguments(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000784 TemplateParameterList *TemplateParams,
785 const TemplateArgument &Param,
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000786 const TemplateArgument &Arg,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000787 Sema::TemplateDeductionInfo &Info,
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000788 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000789 switch (Param.getKind()) {
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000790 case TemplateArgument::Null:
791 assert(false && "Null template argument in parameter list");
792 break;
Mike Stump11289f42009-09-09 15:08:12 +0000793
794 case TemplateArgument::Type:
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000795 if (Arg.getKind() == TemplateArgument::Type)
Chandler Carruthc1263112010-02-07 21:33:28 +0000796 return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000797 Arg.getAsType(), Info, Deduced, 0);
798 Info.FirstArg = Param;
799 Info.SecondArg = Arg;
800 return Sema::TDK_NonDeducedMismatch;
801
802 case TemplateArgument::Template:
Douglas Gregoradee3e32009-11-11 23:06:43 +0000803 if (Arg.getKind() == TemplateArgument::Template)
Chandler Carruthc1263112010-02-07 21:33:28 +0000804 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000805 Param.getAsTemplate(),
Douglas Gregoradee3e32009-11-11 23:06:43 +0000806 Arg.getAsTemplate(), Info, Deduced);
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000807 Info.FirstArg = Param;
808 Info.SecondArg = Arg;
809 return Sema::TDK_NonDeducedMismatch;
810
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000811 case TemplateArgument::Declaration:
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000812 if (Arg.getKind() == TemplateArgument::Declaration &&
813 Param.getAsDecl()->getCanonicalDecl() ==
814 Arg.getAsDecl()->getCanonicalDecl())
815 return Sema::TDK_Success;
816
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000817 Info.FirstArg = Param;
818 Info.SecondArg = Arg;
819 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000820
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000821 case TemplateArgument::Integral:
822 if (Arg.getKind() == TemplateArgument::Integral) {
Douglas Gregor0a29a052010-03-26 05:50:28 +0000823 if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral()))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000824 return Sema::TDK_Success;
825
826 Info.FirstArg = Param;
827 Info.SecondArg = Arg;
828 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000829 }
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000830
831 if (Arg.getKind() == TemplateArgument::Expression) {
832 Info.FirstArg = Param;
833 Info.SecondArg = Arg;
834 return Sema::TDK_NonDeducedMismatch;
835 }
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000836
837 assert(false && "Type/value mismatch");
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000838 Info.FirstArg = Param;
839 Info.SecondArg = Arg;
840 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000841
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000842 case TemplateArgument::Expression: {
Mike Stump11289f42009-09-09 15:08:12 +0000843 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000844 = getDeducedParameterFromExpr(Param.getAsExpr())) {
845 if (Arg.getKind() == TemplateArgument::Integral)
Chandler Carruthc1263112010-02-07 21:33:28 +0000846 return DeduceNonTypeTemplateArgument(S, NTTP,
Mike Stump11289f42009-09-09 15:08:12 +0000847 *Arg.getAsIntegral(),
Douglas Gregor0a29a052010-03-26 05:50:28 +0000848 Arg.getIntegralType(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000849 Info, Deduced);
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000850 if (Arg.getKind() == TemplateArgument::Expression)
Chandler Carruthc1263112010-02-07 21:33:28 +0000851 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000852 Info, Deduced);
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000853 if (Arg.getKind() == TemplateArgument::Declaration)
Chandler Carruthc1263112010-02-07 21:33:28 +0000854 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000855 Info, Deduced);
856
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000857 assert(false && "Type/value mismatch");
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000858 Info.FirstArg = Param;
859 Info.SecondArg = Arg;
860 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000861 }
Mike Stump11289f42009-09-09 15:08:12 +0000862
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000863 // Can't deduce anything, but that's okay.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000864 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000865 }
Anders Carlssonbc343912009-06-15 17:04:53 +0000866 case TemplateArgument::Pack:
867 assert(0 && "FIXME: Implement!");
868 break;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000869 }
Mike Stump11289f42009-09-09 15:08:12 +0000870
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000871 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000872}
873
Mike Stump11289f42009-09-09 15:08:12 +0000874static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000875DeduceTemplateArguments(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000876 TemplateParameterList *TemplateParams,
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000877 const TemplateArgumentList &ParamList,
878 const TemplateArgumentList &ArgList,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000879 Sema::TemplateDeductionInfo &Info,
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000880 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
881 assert(ParamList.size() == ArgList.size());
882 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000883 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000884 = DeduceTemplateArguments(S, TemplateParams,
Mike Stump11289f42009-09-09 15:08:12 +0000885 ParamList[I], ArgList[I],
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000886 Info, Deduced))
887 return Result;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000888 }
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000889 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000890}
891
Douglas Gregor705c9002009-06-26 20:57:09 +0000892/// \brief Determine whether two template arguments are the same.
Mike Stump11289f42009-09-09 15:08:12 +0000893static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregor705c9002009-06-26 20:57:09 +0000894 const TemplateArgument &X,
895 const TemplateArgument &Y) {
896 if (X.getKind() != Y.getKind())
897 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000898
Douglas Gregor705c9002009-06-26 20:57:09 +0000899 switch (X.getKind()) {
900 case TemplateArgument::Null:
901 assert(false && "Comparing NULL template argument");
902 break;
Mike Stump11289f42009-09-09 15:08:12 +0000903
Douglas Gregor705c9002009-06-26 20:57:09 +0000904 case TemplateArgument::Type:
905 return Context.getCanonicalType(X.getAsType()) ==
906 Context.getCanonicalType(Y.getAsType());
Mike Stump11289f42009-09-09 15:08:12 +0000907
Douglas Gregor705c9002009-06-26 20:57:09 +0000908 case TemplateArgument::Declaration:
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +0000909 return X.getAsDecl()->getCanonicalDecl() ==
910 Y.getAsDecl()->getCanonicalDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000911
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000912 case TemplateArgument::Template:
913 return Context.getCanonicalTemplateName(X.getAsTemplate())
914 .getAsVoidPointer() ==
915 Context.getCanonicalTemplateName(Y.getAsTemplate())
916 .getAsVoidPointer();
917
Douglas Gregor705c9002009-06-26 20:57:09 +0000918 case TemplateArgument::Integral:
919 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump11289f42009-09-09 15:08:12 +0000920
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000921 case TemplateArgument::Expression: {
922 llvm::FoldingSetNodeID XID, YID;
923 X.getAsExpr()->Profile(XID, Context, true);
924 Y.getAsExpr()->Profile(YID, Context, true);
925 return XID == YID;
926 }
Mike Stump11289f42009-09-09 15:08:12 +0000927
Douglas Gregor705c9002009-06-26 20:57:09 +0000928 case TemplateArgument::Pack:
929 if (X.pack_size() != Y.pack_size())
930 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000931
932 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
933 XPEnd = X.pack_end(),
Douglas Gregor705c9002009-06-26 20:57:09 +0000934 YP = Y.pack_begin();
Mike Stump11289f42009-09-09 15:08:12 +0000935 XP != XPEnd; ++XP, ++YP)
Douglas Gregor705c9002009-06-26 20:57:09 +0000936 if (!isSameTemplateArg(Context, *XP, *YP))
937 return false;
938
939 return true;
940 }
941
942 return false;
943}
944
945/// \brief Helper function to build a TemplateParameter when we don't
946/// know its type statically.
947static TemplateParameter makeTemplateParameter(Decl *D) {
948 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
949 return TemplateParameter(TTP);
950 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
951 return TemplateParameter(NTTP);
Mike Stump11289f42009-09-09 15:08:12 +0000952
Douglas Gregor705c9002009-06-26 20:57:09 +0000953 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
954}
955
Douglas Gregor170bc422009-06-12 22:31:52 +0000956/// \brief Perform template argument deduction to determine whether
957/// the given template arguments match the given class template
958/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000959Sema::TemplateDeductionResult
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000960Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000961 const TemplateArgumentList &TemplateArgs,
962 TemplateDeductionInfo &Info) {
Douglas Gregor170bc422009-06-12 22:31:52 +0000963 // C++ [temp.class.spec.match]p2:
964 // A partial specialization matches a given actual template
965 // argument list if the template arguments of the partial
966 // specialization can be deduced from the actual template argument
967 // list (14.8.2).
Douglas Gregore1416332009-06-14 08:02:22 +0000968 SFINAETrap Trap(*this);
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000969 llvm::SmallVector<TemplateArgument, 4> Deduced;
970 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000971 if (TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000972 = ::DeduceTemplateArguments(*this,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000973 Partial->getTemplateParameters(),
Mike Stump11289f42009-09-09 15:08:12 +0000974 Partial->getTemplateArgs(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000975 TemplateArgs, Info, Deduced))
976 return Result;
Douglas Gregor637d9982009-06-10 23:47:09 +0000977
Douglas Gregor637d9982009-06-10 23:47:09 +0000978 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
979 Deduced.data(), Deduced.size());
980 if (Inst)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000981 return TDK_InstantiationDepth;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000982
Douglas Gregor74eba0b2009-06-11 18:10:32 +0000983 // C++ [temp.deduct.type]p2:
984 // [...] or if any template argument remains neither deduced nor
985 // explicitly specified, template argument deduction fails.
Anders Carlsson5947ddf2009-06-23 01:26:57 +0000986 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
987 Deduced.size());
Douglas Gregor74eba0b2009-06-11 18:10:32 +0000988 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000989 if (Deduced[I].isNull()) {
Mike Stump11289f42009-09-09 15:08:12 +0000990 Decl *Param
Douglas Gregorbe999392009-09-15 16:23:51 +0000991 = const_cast<NamedDecl *>(
992 Partial->getTemplateParameters()->getParam(I));
Douglas Gregorda61afa2010-03-25 15:38:42 +0000993 Info.Param = makeTemplateParameter(Param);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000994 return TDK_Incomplete;
995 }
Douglas Gregor74eba0b2009-06-11 18:10:32 +0000996
Anders Carlsson5947ddf2009-06-23 01:26:57 +0000997 Builder.Append(Deduced[I]);
Douglas Gregor74eba0b2009-06-11 18:10:32 +0000998 }
999
1000 // Form the template argument list from the deduced template arguments.
Mike Stump11289f42009-09-09 15:08:12 +00001001 TemplateArgumentList *DeducedArgumentList
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001002 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001003 Info.reset(DeducedArgumentList);
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001004
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001005 // Substitute the deduced template arguments into the template
1006 // arguments of the class template partial specialization, and
1007 // verify that the instantiated template arguments are both valid
1008 // and are equivalent to the template arguments originally provided
Mike Stump11289f42009-09-09 15:08:12 +00001009 // to the class template.
Douglas Gregorda61afa2010-03-25 15:38:42 +00001010 Sema::LocalInstantiationScope InstScope(*this);
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001011 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
John McCall0ad16662009-10-29 08:12:44 +00001012 const TemplateArgumentLoc *PartialTemplateArgs
1013 = Partial->getTemplateArgsAsWritten();
1014 unsigned N = Partial->getNumTemplateArgsAsWritten();
John McCall6b51f282009-11-23 01:53:49 +00001015
1016 // Note that we don't provide the langle and rangle locations.
1017 TemplateArgumentListInfo InstArgs;
1018
John McCall0ad16662009-10-29 08:12:44 +00001019 for (unsigned I = 0; I != N; ++I) {
Douglas Gregorbe999392009-09-15 16:23:51 +00001020 Decl *Param = const_cast<NamedDecl *>(
Douglas Gregor4f024b22009-06-13 00:59:32 +00001021 ClassTemplate->getTemplateParameters()->getParam(I));
John McCall6b51f282009-11-23 01:53:49 +00001022 TemplateArgumentLoc InstArg;
1023 if (Subst(PartialTemplateArgs[I], InstArg,
John McCall0ad16662009-10-29 08:12:44 +00001024 MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
Douglas Gregor705c9002009-06-26 20:57:09 +00001025 Info.Param = makeTemplateParameter(Param);
John McCall0ad16662009-10-29 08:12:44 +00001026 Info.FirstArg = PartialTemplateArgs[I].getArgument();
Mike Stump11289f42009-09-09 15:08:12 +00001027 return TDK_SubstitutionFailure;
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001028 }
John McCall6b51f282009-11-23 01:53:49 +00001029 InstArgs.addArgument(InstArg);
John McCall0ad16662009-10-29 08:12:44 +00001030 }
1031
1032 TemplateArgumentListBuilder ConvertedInstArgs(
1033 ClassTemplate->getTemplateParameters(), N);
1034
1035 if (CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
John McCall6b51f282009-11-23 01:53:49 +00001036 InstArgs, false, ConvertedInstArgs)) {
John McCall0ad16662009-10-29 08:12:44 +00001037 // FIXME: fail with more useful information?
1038 return TDK_SubstitutionFailure;
1039 }
1040
1041 for (unsigned I = 0, E = ConvertedInstArgs.flatSize(); I != E; ++I) {
John McCall6b51f282009-11-23 01:53:49 +00001042 TemplateArgument InstArg = ConvertedInstArgs.getFlatArguments()[I];
John McCall0ad16662009-10-29 08:12:44 +00001043
1044 Decl *Param = const_cast<NamedDecl *>(
1045 ClassTemplate->getTemplateParameters()->getParam(I));
Mike Stump11289f42009-09-09 15:08:12 +00001046
Douglas Gregor705c9002009-06-26 20:57:09 +00001047 if (InstArg.getKind() == TemplateArgument::Expression) {
Mike Stump11289f42009-09-09 15:08:12 +00001048 // When the argument is an expression, check the expression result
Douglas Gregor705c9002009-06-26 20:57:09 +00001049 // against the actual template parameter to get down to the canonical
1050 // template argument.
1051 Expr *InstExpr = InstArg.getAsExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001052 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor705c9002009-06-26 20:57:09 +00001053 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1054 if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
1055 Info.Param = makeTemplateParameter(Param);
John McCall0ad16662009-10-29 08:12:44 +00001056 Info.FirstArg = Partial->getTemplateArgs()[I];
Mike Stump11289f42009-09-09 15:08:12 +00001057 return TDK_SubstitutionFailure;
Douglas Gregor705c9002009-06-26 20:57:09 +00001058 }
Douglas Gregor705c9002009-06-26 20:57:09 +00001059 }
1060 }
Mike Stump11289f42009-09-09 15:08:12 +00001061
Douglas Gregor705c9002009-06-26 20:57:09 +00001062 if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) {
1063 Info.Param = makeTemplateParameter(Param);
1064 Info.FirstArg = TemplateArgs[I];
1065 Info.SecondArg = InstArg;
1066 return TDK_NonDeducedMismatch;
1067 }
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001068 }
1069
Douglas Gregore1416332009-06-14 08:02:22 +00001070 if (Trap.hasErrorOccurred())
1071 return TDK_SubstitutionFailure;
1072
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001073 return TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001074}
Douglas Gregor91772d12009-06-13 00:26:55 +00001075
Douglas Gregorfc516c92009-06-26 23:27:24 +00001076/// \brief Determine whether the given type T is a simple-template-id type.
1077static bool isSimpleTemplateIdType(QualType T) {
Mike Stump11289f42009-09-09 15:08:12 +00001078 if (const TemplateSpecializationType *Spec
John McCall9dd450b2009-09-21 23:43:11 +00001079 = T->getAs<TemplateSpecializationType>())
Douglas Gregorfc516c92009-06-26 23:27:24 +00001080 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump11289f42009-09-09 15:08:12 +00001081
Douglas Gregorfc516c92009-06-26 23:27:24 +00001082 return false;
1083}
Douglas Gregor9b146582009-07-08 20:55:45 +00001084
1085/// \brief Substitute the explicitly-provided template arguments into the
1086/// given function template according to C++ [temp.arg.explicit].
1087///
1088/// \param FunctionTemplate the function template into which the explicit
1089/// template arguments will be substituted.
1090///
Mike Stump11289f42009-09-09 15:08:12 +00001091/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor9b146582009-07-08 20:55:45 +00001092/// arguments.
1093///
Mike Stump11289f42009-09-09 15:08:12 +00001094/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor9b146582009-07-08 20:55:45 +00001095/// with the converted and checked explicit template arguments.
1096///
Mike Stump11289f42009-09-09 15:08:12 +00001097/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor9b146582009-07-08 20:55:45 +00001098/// parameters.
1099///
1100/// \param FunctionType if non-NULL, the result type of the function template
1101/// will also be instantiated and the pointed-to value will be updated with
1102/// the instantiated function type.
1103///
1104/// \param Info if substitution fails for any reason, this object will be
1105/// populated with more information about the failure.
1106///
1107/// \returns TDK_Success if substitution was successful, or some failure
1108/// condition.
1109Sema::TemplateDeductionResult
1110Sema::SubstituteExplicitTemplateArguments(
1111 FunctionTemplateDecl *FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001112 const TemplateArgumentListInfo &ExplicitTemplateArgs,
Douglas Gregor9b146582009-07-08 20:55:45 +00001113 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1114 llvm::SmallVectorImpl<QualType> &ParamTypes,
1115 QualType *FunctionType,
1116 TemplateDeductionInfo &Info) {
1117 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1118 TemplateParameterList *TemplateParams
1119 = FunctionTemplate->getTemplateParameters();
1120
John McCall6b51f282009-11-23 01:53:49 +00001121 if (ExplicitTemplateArgs.size() == 0) {
Douglas Gregor9b146582009-07-08 20:55:45 +00001122 // No arguments to substitute; just copy over the parameter types and
1123 // fill in the function type.
1124 for (FunctionDecl::param_iterator P = Function->param_begin(),
1125 PEnd = Function->param_end();
1126 P != PEnd;
1127 ++P)
1128 ParamTypes.push_back((*P)->getType());
Mike Stump11289f42009-09-09 15:08:12 +00001129
Douglas Gregor9b146582009-07-08 20:55:45 +00001130 if (FunctionType)
1131 *FunctionType = Function->getType();
1132 return TDK_Success;
1133 }
Mike Stump11289f42009-09-09 15:08:12 +00001134
Douglas Gregor9b146582009-07-08 20:55:45 +00001135 // Substitution of the explicit template arguments into a function template
1136 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump11289f42009-09-09 15:08:12 +00001137 SFINAETrap Trap(*this);
1138
Douglas Gregor9b146582009-07-08 20:55:45 +00001139 // C++ [temp.arg.explicit]p3:
Mike Stump11289f42009-09-09 15:08:12 +00001140 // Template arguments that are present shall be specified in the
1141 // declaration order of their corresponding template-parameters. The
Douglas Gregor9b146582009-07-08 20:55:45 +00001142 // template argument list shall not specify more template-arguments than
Mike Stump11289f42009-09-09 15:08:12 +00001143 // there are corresponding template-parameters.
1144 TemplateArgumentListBuilder Builder(TemplateParams,
John McCall6b51f282009-11-23 01:53:49 +00001145 ExplicitTemplateArgs.size());
Mike Stump11289f42009-09-09 15:08:12 +00001146
1147 // Enter a new template instantiation context where we check the
Douglas Gregor9b146582009-07-08 20:55:45 +00001148 // explicitly-specified template arguments against this function template,
1149 // and then substitute them into the function parameter types.
Mike Stump11289f42009-09-09 15:08:12 +00001150 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor9b146582009-07-08 20:55:45 +00001151 FunctionTemplate, Deduced.data(), Deduced.size(),
1152 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution);
1153 if (Inst)
1154 return TDK_InstantiationDepth;
Mike Stump11289f42009-09-09 15:08:12 +00001155
Douglas Gregor9b146582009-07-08 20:55:45 +00001156 if (CheckTemplateArgumentList(FunctionTemplate,
Douglas Gregor9b146582009-07-08 20:55:45 +00001157 SourceLocation(),
John McCall6b51f282009-11-23 01:53:49 +00001158 ExplicitTemplateArgs,
Douglas Gregor9b146582009-07-08 20:55:45 +00001159 true,
1160 Builder) || Trap.hasErrorOccurred())
1161 return TDK_InvalidExplicitArguments;
Mike Stump11289f42009-09-09 15:08:12 +00001162
Douglas Gregor9b146582009-07-08 20:55:45 +00001163 // Form the template argument list from the explicitly-specified
1164 // template arguments.
Mike Stump11289f42009-09-09 15:08:12 +00001165 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor9b146582009-07-08 20:55:45 +00001166 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1167 Info.reset(ExplicitArgumentList);
Mike Stump11289f42009-09-09 15:08:12 +00001168
Douglas Gregor9b146582009-07-08 20:55:45 +00001169 // Instantiate the types of each of the function parameters given the
1170 // explicitly-specified template arguments.
1171 for (FunctionDecl::param_iterator P = Function->param_begin(),
1172 PEnd = Function->param_end();
1173 P != PEnd;
1174 ++P) {
Mike Stump11289f42009-09-09 15:08:12 +00001175 QualType ParamType
1176 = SubstType((*P)->getType(),
Douglas Gregor39cacdb2009-08-28 20:50:45 +00001177 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1178 (*P)->getLocation(), (*P)->getDeclName());
Douglas Gregor9b146582009-07-08 20:55:45 +00001179 if (ParamType.isNull() || Trap.hasErrorOccurred())
1180 return TDK_SubstitutionFailure;
Mike Stump11289f42009-09-09 15:08:12 +00001181
Douglas Gregor9b146582009-07-08 20:55:45 +00001182 ParamTypes.push_back(ParamType);
1183 }
1184
1185 // If the caller wants a full function type back, instantiate the return
1186 // type and form that function type.
1187 if (FunctionType) {
1188 // FIXME: exception-specifications?
Mike Stump11289f42009-09-09 15:08:12 +00001189 const FunctionProtoType *Proto
John McCall9dd450b2009-09-21 23:43:11 +00001190 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor9b146582009-07-08 20:55:45 +00001191 assert(Proto && "Function template does not have a prototype?");
Mike Stump11289f42009-09-09 15:08:12 +00001192
1193 QualType ResultType
Douglas Gregor39cacdb2009-08-28 20:50:45 +00001194 = SubstType(Proto->getResultType(),
1195 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1196 Function->getTypeSpecStartLoc(),
1197 Function->getDeclName());
Douglas Gregor9b146582009-07-08 20:55:45 +00001198 if (ResultType.isNull() || Trap.hasErrorOccurred())
1199 return TDK_SubstitutionFailure;
Mike Stump11289f42009-09-09 15:08:12 +00001200
1201 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor9b146582009-07-08 20:55:45 +00001202 ParamTypes.data(), ParamTypes.size(),
1203 Proto->isVariadic(),
1204 Proto->getTypeQuals(),
1205 Function->getLocation(),
1206 Function->getDeclName());
1207 if (FunctionType->isNull() || Trap.hasErrorOccurred())
1208 return TDK_SubstitutionFailure;
1209 }
Mike Stump11289f42009-09-09 15:08:12 +00001210
Douglas Gregor9b146582009-07-08 20:55:45 +00001211 // C++ [temp.arg.explicit]p2:
Mike Stump11289f42009-09-09 15:08:12 +00001212 // Trailing template arguments that can be deduced (14.8.2) may be
1213 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor9b146582009-07-08 20:55:45 +00001214 // template arguments can be deduced, they may all be omitted; in this
1215 // case, the empty template argument list <> itself may also be omitted.
1216 //
1217 // Take all of the explicitly-specified arguments and put them into the
Mike Stump11289f42009-09-09 15:08:12 +00001218 // set of deduced template arguments.
Douglas Gregor9b146582009-07-08 20:55:45 +00001219 Deduced.reserve(TemplateParams->size());
1220 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001221 Deduced.push_back(ExplicitArgumentList->get(I));
1222
Douglas Gregor9b146582009-07-08 20:55:45 +00001223 return TDK_Success;
1224}
1225
Mike Stump11289f42009-09-09 15:08:12 +00001226/// \brief Finish template argument deduction for a function template,
Douglas Gregor9b146582009-07-08 20:55:45 +00001227/// checking the deduced template arguments for completeness and forming
1228/// the function template specialization.
Mike Stump11289f42009-09-09 15:08:12 +00001229Sema::TemplateDeductionResult
Douglas Gregor9b146582009-07-08 20:55:45 +00001230Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
1231 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1232 FunctionDecl *&Specialization,
1233 TemplateDeductionInfo &Info) {
1234 TemplateParameterList *TemplateParams
1235 = FunctionTemplate->getTemplateParameters();
Mike Stump11289f42009-09-09 15:08:12 +00001236
Douglas Gregor9b146582009-07-08 20:55:45 +00001237 // Template argument deduction for function templates in a SFINAE context.
1238 // Trap any errors that might occur.
Mike Stump11289f42009-09-09 15:08:12 +00001239 SFINAETrap Trap(*this);
1240
Douglas Gregor9b146582009-07-08 20:55:45 +00001241 // Enter a new template instantiation context while we instantiate the
1242 // actual function declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001243 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor9b146582009-07-08 20:55:45 +00001244 FunctionTemplate, Deduced.data(), Deduced.size(),
1245 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
1246 if (Inst)
Mike Stump11289f42009-09-09 15:08:12 +00001247 return TDK_InstantiationDepth;
1248
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001249 // C++ [temp.deduct.type]p2:
1250 // [...] or if any template argument remains neither deduced nor
1251 // explicitly specified, template argument deduction fails.
1252 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
1253 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
1254 if (!Deduced[I].isNull()) {
1255 Builder.Append(Deduced[I]);
1256 continue;
1257 }
1258
1259 // Substitute into the default template argument, if available.
1260 NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I);
1261 TemplateArgumentLoc DefArg
1262 = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
1263 FunctionTemplate->getLocation(),
1264 FunctionTemplate->getSourceRange().getEnd(),
1265 Param,
1266 Builder);
1267
1268 // If there was no default argument, deduction is incomplete.
1269 if (DefArg.getArgument().isNull()) {
1270 Info.Param = makeTemplateParameter(
1271 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1272 return TDK_Incomplete;
1273 }
1274
1275 // Check whether we can actually use the default argument.
1276 if (CheckTemplateArgument(Param, DefArg,
1277 FunctionTemplate,
1278 FunctionTemplate->getLocation(),
1279 FunctionTemplate->getSourceRange().getEnd(),
1280 Builder)) {
1281 Info.Param = makeTemplateParameter(
1282 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1283 return TDK_SubstitutionFailure;
1284 }
1285
1286 // If we get here, we successfully used the default template argument.
1287 }
1288
1289 // Form the template argument list from the deduced template arguments.
1290 TemplateArgumentList *DeducedArgumentList
1291 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1292 Info.reset(DeducedArgumentList);
1293
Mike Stump11289f42009-09-09 15:08:12 +00001294 // Substitute the deduced template arguments into the function template
Douglas Gregor9b146582009-07-08 20:55:45 +00001295 // declaration to produce the function template specialization.
1296 Specialization = cast_or_null<FunctionDecl>(
John McCall76d824f2009-08-25 22:02:44 +00001297 SubstDecl(FunctionTemplate->getTemplatedDecl(),
1298 FunctionTemplate->getDeclContext(),
Douglas Gregor39cacdb2009-08-28 20:50:45 +00001299 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor9b146582009-07-08 20:55:45 +00001300 if (!Specialization)
1301 return TDK_SubstitutionFailure;
Mike Stump11289f42009-09-09 15:08:12 +00001302
Douglas Gregor31fae892009-09-15 18:26:13 +00001303 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
1304 FunctionTemplate->getCanonicalDecl());
1305
Mike Stump11289f42009-09-09 15:08:12 +00001306 // If the template argument list is owned by the function template
Douglas Gregor9b146582009-07-08 20:55:45 +00001307 // specialization, release it.
1308 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList)
1309 Info.take();
Mike Stump11289f42009-09-09 15:08:12 +00001310
Douglas Gregor9b146582009-07-08 20:55:45 +00001311 // There may have been an error that did not prevent us from constructing a
1312 // declaration. Mark the declaration invalid and return with a substitution
1313 // failure.
1314 if (Trap.hasErrorOccurred()) {
1315 Specialization->setInvalidDecl(true);
1316 return TDK_SubstitutionFailure;
1317 }
Mike Stump11289f42009-09-09 15:08:12 +00001318
1319 return TDK_Success;
Douglas Gregor9b146582009-07-08 20:55:45 +00001320}
1321
John McCallc1f69982010-02-02 02:21:27 +00001322static QualType GetTypeOfFunction(ASTContext &Context,
1323 bool isAddressOfOperand,
1324 FunctionDecl *Fn) {
1325 if (!isAddressOfOperand) return Fn->getType();
1326 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
1327 if (Method->isInstance())
1328 return Context.getMemberPointerType(Fn->getType(),
1329 Context.getTypeDeclType(Method->getParent()).getTypePtr());
1330 return Context.getPointerType(Fn->getType());
1331}
1332
1333/// Apply the deduction rules for overload sets.
1334///
1335/// \return the null type if this argument should be treated as an
1336/// undeduced context
1337static QualType
1338ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
1339 Expr *Arg, QualType ParamType) {
John McCall1acbbb52010-02-02 06:20:04 +00001340 llvm::PointerIntPair<OverloadExpr*,1> R = OverloadExpr::find(Arg);
John McCallc1f69982010-02-02 02:21:27 +00001341
John McCall1acbbb52010-02-02 06:20:04 +00001342 bool isAddressOfOperand = bool(R.getInt());
1343 OverloadExpr *Ovl = R.getPointer();
John McCallc1f69982010-02-02 02:21:27 +00001344
1345 // If there were explicit template arguments, we can only find
1346 // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
1347 // unambiguously name a full specialization.
John McCall1acbbb52010-02-02 06:20:04 +00001348 if (Ovl->hasExplicitTemplateArgs()) {
John McCallc1f69982010-02-02 02:21:27 +00001349 // But we can still look for an explicit specialization.
1350 if (FunctionDecl *ExplicitSpec
John McCall1acbbb52010-02-02 06:20:04 +00001351 = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
1352 return GetTypeOfFunction(S.Context, isAddressOfOperand, ExplicitSpec);
John McCallc1f69982010-02-02 02:21:27 +00001353 return QualType();
1354 }
1355
1356 // C++0x [temp.deduct.call]p6:
1357 // When P is a function type, pointer to function type, or pointer
1358 // to member function type:
1359
1360 if (!ParamType->isFunctionType() &&
1361 !ParamType->isFunctionPointerType() &&
1362 !ParamType->isMemberFunctionPointerType())
1363 return QualType();
1364
1365 QualType Match;
John McCall1acbbb52010-02-02 06:20:04 +00001366 for (UnresolvedSetIterator I = Ovl->decls_begin(),
1367 E = Ovl->decls_end(); I != E; ++I) {
John McCallc1f69982010-02-02 02:21:27 +00001368 NamedDecl *D = (*I)->getUnderlyingDecl();
1369
1370 // - If the argument is an overload set containing one or more
1371 // function templates, the parameter is treated as a
1372 // non-deduced context.
1373 if (isa<FunctionTemplateDecl>(D))
1374 return QualType();
1375
1376 FunctionDecl *Fn = cast<FunctionDecl>(D);
1377 QualType ArgType = GetTypeOfFunction(S.Context, isAddressOfOperand, Fn);
1378
1379 // - If the argument is an overload set (not containing function
1380 // templates), trial argument deduction is attempted using each
1381 // of the members of the set. If deduction succeeds for only one
1382 // of the overload set members, that member is used as the
1383 // argument value for the deduction. If deduction succeeds for
1384 // more than one member of the overload set the parameter is
1385 // treated as a non-deduced context.
1386
1387 // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
1388 // Type deduction is done independently for each P/A pair, and
1389 // the deduced template argument values are then combined.
1390 // So we do not reject deductions which were made elsewhere.
1391 llvm::SmallVector<TemplateArgument, 8> Deduced(TemplateParams->size());
John McCallbc077cf2010-02-08 23:07:23 +00001392 Sema::TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
John McCallc1f69982010-02-02 02:21:27 +00001393 unsigned TDF = 0;
1394
1395 Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +00001396 = DeduceTemplateArguments(S, TemplateParams,
John McCallc1f69982010-02-02 02:21:27 +00001397 ParamType, ArgType,
1398 Info, Deduced, TDF);
1399 if (Result) continue;
1400 if (!Match.isNull()) return QualType();
1401 Match = ArgType;
1402 }
1403
1404 return Match;
1405}
1406
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001407/// \brief Perform template argument deduction from a function call
1408/// (C++ [temp.deduct.call]).
1409///
1410/// \param FunctionTemplate the function template for which we are performing
1411/// template argument deduction.
1412///
Douglas Gregorea0a0a92010-01-11 18:40:55 +00001413/// \param ExplicitTemplateArguments the explicit template arguments provided
1414/// for this call.
Douglas Gregor89026b52009-06-30 23:57:56 +00001415///
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001416/// \param Args the function call arguments
1417///
1418/// \param NumArgs the number of arguments in Args
1419///
Douglas Gregorea0a0a92010-01-11 18:40:55 +00001420/// \param Name the name of the function being called. This is only significant
1421/// when the function template is a conversion function template, in which
1422/// case this routine will also perform template argument deduction based on
1423/// the function to which
1424///
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001425/// \param Specialization if template argument deduction was successful,
Mike Stump11289f42009-09-09 15:08:12 +00001426/// this will be set to the function template specialization produced by
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001427/// template argument deduction.
1428///
1429/// \param Info the argument will be updated to provide additional information
1430/// about template argument deduction.
1431///
1432/// \returns the result of template argument deduction.
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001433Sema::TemplateDeductionResult
1434Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregorea0a0a92010-01-11 18:40:55 +00001435 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001436 Expr **Args, unsigned NumArgs,
1437 FunctionDecl *&Specialization,
1438 TemplateDeductionInfo &Info) {
1439 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor89026b52009-06-30 23:57:56 +00001440
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001441 // C++ [temp.deduct.call]p1:
1442 // Template argument deduction is done by comparing each function template
1443 // parameter type (call it P) with the type of the corresponding argument
1444 // of the call (call it A) as described below.
1445 unsigned CheckArgs = NumArgs;
Douglas Gregor89026b52009-06-30 23:57:56 +00001446 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001447 return TDK_TooFewArguments;
1448 else if (NumArgs > Function->getNumParams()) {
Mike Stump11289f42009-09-09 15:08:12 +00001449 const FunctionProtoType *Proto
John McCall9dd450b2009-09-21 23:43:11 +00001450 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001451 if (!Proto->isVariadic())
1452 return TDK_TooManyArguments;
Mike Stump11289f42009-09-09 15:08:12 +00001453
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001454 CheckArgs = Function->getNumParams();
1455 }
Mike Stump11289f42009-09-09 15:08:12 +00001456
Douglas Gregor89026b52009-06-30 23:57:56 +00001457 // The types of the parameters from which we will perform template argument
1458 // deduction.
Douglas Gregorda61afa2010-03-25 15:38:42 +00001459 Sema::LocalInstantiationScope InstScope(*this);
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001460 TemplateParameterList *TemplateParams
1461 = FunctionTemplate->getTemplateParameters();
Douglas Gregor89026b52009-06-30 23:57:56 +00001462 llvm::SmallVector<TemplateArgument, 4> Deduced;
1463 llvm::SmallVector<QualType, 4> ParamTypes;
John McCall6b51f282009-11-23 01:53:49 +00001464 if (ExplicitTemplateArgs) {
Douglas Gregor9b146582009-07-08 20:55:45 +00001465 TemplateDeductionResult Result =
1466 SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001467 *ExplicitTemplateArgs,
Douglas Gregor9b146582009-07-08 20:55:45 +00001468 Deduced,
1469 ParamTypes,
1470 0,
1471 Info);
1472 if (Result)
1473 return Result;
Douglas Gregor89026b52009-06-30 23:57:56 +00001474 } else {
1475 // Just fill in the parameter types from the function declaration.
1476 for (unsigned I = 0; I != CheckArgs; ++I)
1477 ParamTypes.push_back(Function->getParamDecl(I)->getType());
1478 }
Mike Stump11289f42009-09-09 15:08:12 +00001479
Douglas Gregor89026b52009-06-30 23:57:56 +00001480 // Deduce template arguments from the function parameters.
Mike Stump11289f42009-09-09 15:08:12 +00001481 Deduced.resize(TemplateParams->size());
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001482 for (unsigned I = 0; I != CheckArgs; ++I) {
Douglas Gregor89026b52009-06-30 23:57:56 +00001483 QualType ParamType = ParamTypes[I];
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001484 QualType ArgType = Args[I]->getType();
Mike Stump11289f42009-09-09 15:08:12 +00001485
John McCallc1f69982010-02-02 02:21:27 +00001486 // Overload sets usually make this parameter an undeduced
1487 // context, but there are sometimes special circumstances.
1488 if (ArgType == Context.OverloadTy) {
1489 ArgType = ResolveOverloadForDeduction(*this, TemplateParams,
1490 Args[I], ParamType);
1491 if (ArgType.isNull())
1492 continue;
1493 }
1494
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001495 // C++ [temp.deduct.call]p2:
1496 // If P is not a reference type:
1497 QualType CanonParamType = Context.getCanonicalType(ParamType);
Douglas Gregorcceb9752009-06-26 18:27:22 +00001498 bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1499 if (!ParamWasReference) {
Mike Stump11289f42009-09-09 15:08:12 +00001500 // - If A is an array type, the pointer type produced by the
1501 // array-to-pointer standard conversion (4.2) is used in place of
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001502 // A for type deduction; otherwise,
1503 if (ArgType->isArrayType())
1504 ArgType = Context.getArrayDecayedType(ArgType);
Mike Stump11289f42009-09-09 15:08:12 +00001505 // - If A is a function type, the pointer type produced by the
1506 // function-to-pointer standard conversion (4.3) is used in place
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001507 // of A for type deduction; otherwise,
1508 else if (ArgType->isFunctionType())
1509 ArgType = Context.getPointerType(ArgType);
1510 else {
1511 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1512 // type are ignored for type deduction.
1513 QualType CanonArgType = Context.getCanonicalType(ArgType);
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001514 if (CanonArgType.getLocalCVRQualifiers())
1515 ArgType = CanonArgType.getLocalUnqualifiedType();
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001516 }
1517 }
Mike Stump11289f42009-09-09 15:08:12 +00001518
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001519 // C++0x [temp.deduct.call]p3:
1520 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
Mike Stump11289f42009-09-09 15:08:12 +00001521 // are ignored for type deduction.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001522 if (CanonParamType.getLocalCVRQualifiers())
1523 ParamType = CanonParamType.getLocalUnqualifiedType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001524 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Mike Stump11289f42009-09-09 15:08:12 +00001525 // [...] If P is a reference type, the type referred to by P is used
1526 // for type deduction.
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001527 ParamType = ParamRefType->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +00001528
1529 // [...] If P is of the form T&&, where T is a template parameter, and
1530 // the argument is an lvalue, the type A& is used in place of A for
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001531 // type deduction.
1532 if (isa<RValueReferenceType>(ParamRefType) &&
John McCall9dd450b2009-09-21 23:43:11 +00001533 ParamRefType->getAs<TemplateTypeParmType>() &&
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001534 Args[I]->isLvalue(Context) == Expr::LV_Valid)
1535 ArgType = Context.getLValueReferenceType(ArgType);
1536 }
Mike Stump11289f42009-09-09 15:08:12 +00001537
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001538 // C++0x [temp.deduct.call]p4:
1539 // In general, the deduction process attempts to find template argument
1540 // values that will make the deduced A identical to A (after the type A
1541 // is transformed as described above). [...]
Douglas Gregor406f6342009-09-14 20:00:47 +00001542 unsigned TDF = TDF_SkipNonDependent;
Mike Stump11289f42009-09-09 15:08:12 +00001543
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001544 // - If the original P is a reference type, the deduced A (i.e., the
1545 // type referred to by the reference) can be more cv-qualified than
1546 // the transformed A.
1547 if (ParamWasReference)
1548 TDF |= TDF_ParamWithReferenceType;
Mike Stump11289f42009-09-09 15:08:12 +00001549 // - The transformed A can be another pointer or pointer to member
1550 // type that can be converted to the deduced A via a qualification
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001551 // conversion (4.4).
1552 if (ArgType->isPointerType() || ArgType->isMemberPointerType())
1553 TDF |= TDF_IgnoreQualifiers;
Mike Stump11289f42009-09-09 15:08:12 +00001554 // - If P is a class and P has the form simple-template-id, then the
Douglas Gregorfc516c92009-06-26 23:27:24 +00001555 // transformed A can be a derived class of the deduced A. Likewise,
1556 // if P is a pointer to a class of the form simple-template-id, the
1557 // transformed A can be a pointer to a derived class pointed to by
1558 // the deduced A.
1559 if (isSimpleTemplateIdType(ParamType) ||
Mike Stump11289f42009-09-09 15:08:12 +00001560 (isa<PointerType>(ParamType) &&
Douglas Gregorfc516c92009-06-26 23:27:24 +00001561 isSimpleTemplateIdType(
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001562 ParamType->getAs<PointerType>()->getPointeeType())))
Douglas Gregorfc516c92009-06-26 23:27:24 +00001563 TDF |= TDF_DerivedClass;
Mike Stump11289f42009-09-09 15:08:12 +00001564
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001565 if (TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +00001566 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregorcceb9752009-06-26 18:27:22 +00001567 ParamType, ArgType, Info, Deduced,
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001568 TDF))
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001569 return Result;
Mike Stump11289f42009-09-09 15:08:12 +00001570
Douglas Gregor05155d82009-08-21 23:19:43 +00001571 // FIXME: we need to check that the deduced A is the same as A,
1572 // modulo the various allowed differences.
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001573 }
Douglas Gregor05155d82009-08-21 23:19:43 +00001574
Mike Stump11289f42009-09-09 15:08:12 +00001575 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor9b146582009-07-08 20:55:45 +00001576 Specialization, Info);
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001577}
1578
Douglas Gregor9b146582009-07-08 20:55:45 +00001579/// \brief Deduce template arguments when taking the address of a function
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001580/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
1581/// a template.
Douglas Gregor9b146582009-07-08 20:55:45 +00001582///
1583/// \param FunctionTemplate the function template for which we are performing
1584/// template argument deduction.
1585///
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001586/// \param ExplicitTemplateArguments the explicitly-specified template
1587/// arguments.
Douglas Gregor9b146582009-07-08 20:55:45 +00001588///
1589/// \param ArgFunctionType the function type that will be used as the
1590/// "argument" type (A) when performing template argument deduction from the
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001591/// function template's function type. This type may be NULL, if there is no
1592/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
Douglas Gregor9b146582009-07-08 20:55:45 +00001593///
1594/// \param Specialization if template argument deduction was successful,
Mike Stump11289f42009-09-09 15:08:12 +00001595/// this will be set to the function template specialization produced by
Douglas Gregor9b146582009-07-08 20:55:45 +00001596/// template argument deduction.
1597///
1598/// \param Info the argument will be updated to provide additional information
1599/// about template argument deduction.
1600///
1601/// \returns the result of template argument deduction.
1602Sema::TemplateDeductionResult
1603Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001604 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor9b146582009-07-08 20:55:45 +00001605 QualType ArgFunctionType,
1606 FunctionDecl *&Specialization,
1607 TemplateDeductionInfo &Info) {
1608 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1609 TemplateParameterList *TemplateParams
1610 = FunctionTemplate->getTemplateParameters();
1611 QualType FunctionType = Function->getType();
Mike Stump11289f42009-09-09 15:08:12 +00001612
Douglas Gregor9b146582009-07-08 20:55:45 +00001613 // Substitute any explicit template arguments.
Douglas Gregorda61afa2010-03-25 15:38:42 +00001614 Sema::LocalInstantiationScope InstScope(*this);
Douglas Gregor9b146582009-07-08 20:55:45 +00001615 llvm::SmallVector<TemplateArgument, 4> Deduced;
1616 llvm::SmallVector<QualType, 4> ParamTypes;
John McCall6b51f282009-11-23 01:53:49 +00001617 if (ExplicitTemplateArgs) {
Mike Stump11289f42009-09-09 15:08:12 +00001618 if (TemplateDeductionResult Result
1619 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001620 *ExplicitTemplateArgs,
Mike Stump11289f42009-09-09 15:08:12 +00001621 Deduced, ParamTypes,
Douglas Gregor9b146582009-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 Stump11289f42009-09-09 15:08:12 +00001628 SFINAETrap Trap(*this);
1629
John McCallc1f69982010-02-02 02:21:27 +00001630 Deduced.resize(TemplateParams->size());
1631
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001632 if (!ArgFunctionType.isNull()) {
1633 // Deduce template arguments from the function type.
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001634 if (TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +00001635 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001636 FunctionType, ArgFunctionType, Info,
1637 Deduced, 0))
1638 return Result;
1639 }
1640
Mike Stump11289f42009-09-09 15:08:12 +00001641 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor9b146582009-07-08 20:55:45 +00001642 Specialization, Info);
1643}
1644
Douglas Gregor05155d82009-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 Stump11289f42009-09-09 15:08:12 +00001653 CXXConversionDecl *Conv
Douglas Gregor05155d82009-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 Stump11289f42009-09-09 15:08:12 +00001674 // If A is not a reference type:
Douglas Gregor05155d82009-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 Stump11289f42009-09-09 15:08:12 +00001679 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor05155d82009-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 Stump11289f42009-09-09 15:08:12 +00001701 SFINAETrap Trap(*this);
Douglas Gregor05155d82009-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 Stump11289f42009-09-09 15:08:12 +00001711 Deduced.resize(TemplateParams->size());
Douglas Gregor05155d82009-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 Carruthc1263112010-02-07 21:33:28 +00001734 = ::DeduceTemplateArguments(*this, TemplateParams,
Douglas Gregor05155d82009-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 Stump11289f42009-09-09 15:08:12 +00001740
Douglas Gregor05155d82009-08-21 23:19:43 +00001741 // Finish template argument deduction.
Douglas Gregorda61afa2010-03-25 15:38:42 +00001742 Sema::LocalInstantiationScope InstScope(*this);
Douglas Gregor05155d82009-08-21 23:19:43 +00001743 FunctionDecl *Spec = 0;
1744 TemplateDeductionResult Result
1745 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, Spec, Info);
1746 Specialization = cast_or_null<CXXConversionDecl>(Spec);
1747 return Result;
1748}
1749
Douglas Gregor8364e6b2009-12-21 23:17:24 +00001750/// \brief Deduce template arguments for a function template when there is
1751/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
1752///
1753/// \param FunctionTemplate the function template for which we are performing
1754/// template argument deduction.
1755///
1756/// \param ExplicitTemplateArguments the explicitly-specified template
1757/// arguments.
1758///
1759/// \param Specialization if template argument deduction was successful,
1760/// this will be set to the function template specialization produced by
1761/// template argument deduction.
1762///
1763/// \param Info the argument will be updated to provide additional information
1764/// about template argument deduction.
1765///
1766/// \returns the result of template argument deduction.
1767Sema::TemplateDeductionResult
1768Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1769 const TemplateArgumentListInfo *ExplicitTemplateArgs,
1770 FunctionDecl *&Specialization,
1771 TemplateDeductionInfo &Info) {
1772 return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
1773 QualType(), Specialization, Info);
1774}
1775
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001776/// \brief Stores the result of comparing the qualifiers of two types.
1777enum DeductionQualifierComparison {
1778 NeitherMoreQualified = 0,
1779 ParamMoreQualified,
1780 ArgMoreQualified
1781};
1782
1783/// \brief Deduce the template arguments during partial ordering by comparing
1784/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
1785///
Chandler Carruthc1263112010-02-07 21:33:28 +00001786/// \param S the semantic analysis object within which we are deducing
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001787///
1788/// \param TemplateParams the template parameters that we are deducing
1789///
1790/// \param ParamIn the parameter type
1791///
1792/// \param ArgIn the argument type
1793///
1794/// \param Info information about the template argument deduction itself
1795///
1796/// \param Deduced the deduced template arguments
1797///
1798/// \returns the result of template argument deduction so far. Note that a
1799/// "success" result means that template argument deduction has not yet failed,
1800/// but it may still fail, later, for other reasons.
1801static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +00001802DeduceTemplateArgumentsDuringPartialOrdering(Sema &S,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001803 TemplateParameterList *TemplateParams,
1804 QualType ParamIn, QualType ArgIn,
1805 Sema::TemplateDeductionInfo &Info,
1806 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1807 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
Chandler Carruthc1263112010-02-07 21:33:28 +00001808 CanQualType Param = S.Context.getCanonicalType(ParamIn);
1809 CanQualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001810
1811 // C++0x [temp.deduct.partial]p5:
1812 // Before the partial ordering is done, certain transformations are
1813 // performed on the types used for partial ordering:
1814 // - If P is a reference type, P is replaced by the type referred to.
1815 CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
John McCall48f2d582009-10-23 23:03:21 +00001816 if (!ParamRef.isNull())
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001817 Param = ParamRef->getPointeeType();
1818
1819 // - If A is a reference type, A is replaced by the type referred to.
1820 CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
John McCall48f2d582009-10-23 23:03:21 +00001821 if (!ArgRef.isNull())
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001822 Arg = ArgRef->getPointeeType();
1823
John McCall48f2d582009-10-23 23:03:21 +00001824 if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001825 // C++0x [temp.deduct.partial]p6:
1826 // If both P and A were reference types (before being replaced with the
1827 // type referred to above), determine which of the two types (if any) is
1828 // more cv-qualified than the other; otherwise the types are considered to
1829 // be equally cv-qualified for partial ordering purposes. The result of this
1830 // determination will be used below.
1831 //
1832 // We save this information for later, using it only when deduction
1833 // succeeds in both directions.
1834 DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
1835 if (Param.isMoreQualifiedThan(Arg))
1836 QualifierResult = ParamMoreQualified;
1837 else if (Arg.isMoreQualifiedThan(Param))
1838 QualifierResult = ArgMoreQualified;
1839 QualifierComparisons->push_back(QualifierResult);
1840 }
1841
1842 // C++0x [temp.deduct.partial]p7:
1843 // Remove any top-level cv-qualifiers:
1844 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
1845 // version of P.
1846 Param = Param.getUnqualifiedType();
1847 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
1848 // version of A.
1849 Arg = Arg.getUnqualifiedType();
1850
1851 // C++0x [temp.deduct.partial]p8:
1852 // Using the resulting types P and A the deduction is then done as
1853 // described in 14.9.2.5. If deduction succeeds for a given type, the type
1854 // from the argument template is considered to be at least as specialized
1855 // as the type from the parameter template.
Chandler Carruthc1263112010-02-07 21:33:28 +00001856 return DeduceTemplateArguments(S, TemplateParams, Param, Arg, Info,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001857 Deduced, TDF_None);
1858}
1859
1860static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00001861MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
1862 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00001863 unsigned Level,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00001864 llvm::SmallVectorImpl<bool> &Deduced);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001865
1866/// \brief Determine whether the function template \p FT1 is at least as
1867/// specialized as \p FT2.
1868static bool isAtLeastAsSpecializedAs(Sema &S,
John McCallbc077cf2010-02-08 23:07:23 +00001869 SourceLocation Loc,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001870 FunctionTemplateDecl *FT1,
1871 FunctionTemplateDecl *FT2,
1872 TemplatePartialOrderingContext TPOC,
1873 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1874 FunctionDecl *FD1 = FT1->getTemplatedDecl();
1875 FunctionDecl *FD2 = FT2->getTemplatedDecl();
1876 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
1877 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
1878
1879 assert(Proto1 && Proto2 && "Function templates must have prototypes");
1880 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
1881 llvm::SmallVector<TemplateArgument, 4> Deduced;
1882 Deduced.resize(TemplateParams->size());
1883
1884 // C++0x [temp.deduct.partial]p3:
1885 // The types used to determine the ordering depend on the context in which
1886 // the partial ordering is done:
John McCallbc077cf2010-02-08 23:07:23 +00001887 Sema::TemplateDeductionInfo Info(S.Context, Loc);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001888 switch (TPOC) {
1889 case TPOC_Call: {
1890 // - In the context of a function call, the function parameter types are
1891 // used.
1892 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
1893 for (unsigned I = 0; I != NumParams; ++I)
Chandler Carruthc1263112010-02-07 21:33:28 +00001894 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001895 TemplateParams,
1896 Proto2->getArgType(I),
1897 Proto1->getArgType(I),
1898 Info,
1899 Deduced,
1900 QualifierComparisons))
1901 return false;
1902
1903 break;
1904 }
1905
1906 case TPOC_Conversion:
1907 // - In the context of a call to a conversion operator, the return types
1908 // of the conversion function templates are used.
Chandler Carruthc1263112010-02-07 21:33:28 +00001909 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001910 TemplateParams,
1911 Proto2->getResultType(),
1912 Proto1->getResultType(),
1913 Info,
1914 Deduced,
1915 QualifierComparisons))
1916 return false;
1917 break;
1918
1919 case TPOC_Other:
1920 // - In other contexts (14.6.6.2) the function template’s function type
1921 // is used.
Chandler Carruthc1263112010-02-07 21:33:28 +00001922 if (DeduceTemplateArgumentsDuringPartialOrdering(S,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001923 TemplateParams,
1924 FD2->getType(),
1925 FD1->getType(),
1926 Info,
1927 Deduced,
1928 QualifierComparisons))
1929 return false;
1930 break;
1931 }
1932
1933 // C++0x [temp.deduct.partial]p11:
1934 // In most cases, all template parameters must have values in order for
1935 // deduction to succeed, but for partial ordering purposes a template
1936 // parameter may remain without a value provided it is not used in the
1937 // types being used for partial ordering. [ Note: a template parameter used
1938 // in a non-deduced context is considered used. -end note]
1939 unsigned ArgIdx = 0, NumArgs = Deduced.size();
1940 for (; ArgIdx != NumArgs; ++ArgIdx)
1941 if (Deduced[ArgIdx].isNull())
1942 break;
1943
1944 if (ArgIdx == NumArgs) {
1945 // All template arguments were deduced. FT1 is at least as specialized
1946 // as FT2.
1947 return true;
1948 }
1949
Douglas Gregore1d2ef32009-09-14 21:25:05 +00001950 // Figure out which template parameters were used.
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001951 llvm::SmallVector<bool, 4> UsedParameters;
1952 UsedParameters.resize(TemplateParams->size());
1953 switch (TPOC) {
1954 case TPOC_Call: {
1955 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
1956 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor21610382009-10-29 00:04:11 +00001957 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
1958 TemplateParams->getDepth(),
Douglas Gregore1d2ef32009-09-14 21:25:05 +00001959 UsedParameters);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001960 break;
1961 }
1962
1963 case TPOC_Conversion:
Douglas Gregor21610382009-10-29 00:04:11 +00001964 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
1965 TemplateParams->getDepth(),
Douglas Gregore1d2ef32009-09-14 21:25:05 +00001966 UsedParameters);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001967 break;
1968
1969 case TPOC_Other:
Douglas Gregor21610382009-10-29 00:04:11 +00001970 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
1971 TemplateParams->getDepth(),
1972 UsedParameters);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001973 break;
1974 }
1975
1976 for (; ArgIdx != NumArgs; ++ArgIdx)
1977 // If this argument had no value deduced but was used in one of the types
1978 // used for partial ordering, then deduction fails.
1979 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
1980 return false;
1981
1982 return true;
1983}
1984
1985
Douglas Gregorbe999392009-09-15 16:23:51 +00001986/// \brief Returns the more specialized function template according
Douglas Gregor05155d82009-08-21 23:19:43 +00001987/// to the rules of function template partial ordering (C++ [temp.func.order]).
1988///
1989/// \param FT1 the first function template
1990///
1991/// \param FT2 the second function template
1992///
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001993/// \param TPOC the context in which we are performing partial ordering of
1994/// function templates.
Mike Stump11289f42009-09-09 15:08:12 +00001995///
Douglas Gregorbe999392009-09-15 16:23:51 +00001996/// \returns the more specialized function template. If neither
Douglas Gregor05155d82009-08-21 23:19:43 +00001997/// template is more specialized, returns NULL.
1998FunctionTemplateDecl *
1999Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
2000 FunctionTemplateDecl *FT2,
John McCallbc077cf2010-02-08 23:07:23 +00002001 SourceLocation Loc,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002002 TemplatePartialOrderingContext TPOC) {
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002003 llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
John McCallbc077cf2010-02-08 23:07:23 +00002004 bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, 0);
2005 bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002006 &QualifierComparisons);
2007
2008 if (Better1 != Better2) // We have a clear winner
2009 return Better1? FT1 : FT2;
2010
2011 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor05155d82009-08-21 23:19:43 +00002012 return 0;
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002013
2014
2015 // C++0x [temp.deduct.partial]p10:
2016 // If for each type being considered a given template is at least as
2017 // specialized for all types and more specialized for some set of types and
2018 // the other template is not more specialized for any types or is not at
2019 // least as specialized for any types, then the given template is more
2020 // specialized than the other template. Otherwise, neither template is more
2021 // specialized than the other.
2022 Better1 = false;
2023 Better2 = false;
2024 for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
2025 // C++0x [temp.deduct.partial]p9:
2026 // If, for a given type, deduction succeeds in both directions (i.e., the
2027 // types are identical after the transformations above) and if the type
2028 // from the argument template is more cv-qualified than the type from the
2029 // parameter template (as described above) that type is considered to be
2030 // more specialized than the other. If neither type is more cv-qualified
2031 // than the other then neither type is more specialized than the other.
2032 switch (QualifierComparisons[I]) {
2033 case NeitherMoreQualified:
2034 break;
2035
2036 case ParamMoreQualified:
2037 Better1 = true;
2038 if (Better2)
2039 return 0;
2040 break;
2041
2042 case ArgMoreQualified:
2043 Better2 = true;
2044 if (Better1)
2045 return 0;
2046 break;
2047 }
2048 }
2049
2050 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor05155d82009-08-21 23:19:43 +00002051 if (Better1)
2052 return FT1;
Douglas Gregor0ff7d922009-09-14 18:39:43 +00002053 else if (Better2)
2054 return FT2;
2055 else
2056 return 0;
Douglas Gregor05155d82009-08-21 23:19:43 +00002057}
Douglas Gregor9b146582009-07-08 20:55:45 +00002058
Douglas Gregor450f00842009-09-25 18:43:00 +00002059/// \brief Determine if the two templates are equivalent.
2060static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
2061 if (T1 == T2)
2062 return true;
2063
2064 if (!T1 || !T2)
2065 return false;
2066
2067 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
2068}
2069
2070/// \brief Retrieve the most specialized of the given function template
2071/// specializations.
2072///
John McCall58cc69d2010-01-27 01:50:18 +00002073/// \param SpecBegin the start iterator of the function template
2074/// specializations that we will be comparing.
Douglas Gregor450f00842009-09-25 18:43:00 +00002075///
John McCall58cc69d2010-01-27 01:50:18 +00002076/// \param SpecEnd the end iterator of the function template
2077/// specializations, paired with \p SpecBegin.
Douglas Gregor450f00842009-09-25 18:43:00 +00002078///
2079/// \param TPOC the partial ordering context to use to compare the function
2080/// template specializations.
2081///
2082/// \param Loc the location where the ambiguity or no-specializations
2083/// diagnostic should occur.
2084///
2085/// \param NoneDiag partial diagnostic used to diagnose cases where there are
2086/// no matching candidates.
2087///
2088/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
2089/// occurs.
2090///
2091/// \param CandidateDiag partial diagnostic used for each function template
2092/// specialization that is a candidate in the ambiguous ordering. One parameter
2093/// in this diagnostic should be unbound, which will correspond to the string
2094/// describing the template arguments for the function template specialization.
2095///
2096/// \param Index if non-NULL and the result of this function is non-nULL,
2097/// receives the index corresponding to the resulting function template
2098/// specialization.
2099///
2100/// \returns the most specialized function template specialization, if
John McCall58cc69d2010-01-27 01:50:18 +00002101/// found. Otherwise, returns SpecEnd.
Douglas Gregor450f00842009-09-25 18:43:00 +00002102///
2103/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
2104/// template argument deduction.
John McCall58cc69d2010-01-27 01:50:18 +00002105UnresolvedSetIterator
2106Sema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
2107 UnresolvedSetIterator SpecEnd,
2108 TemplatePartialOrderingContext TPOC,
2109 SourceLocation Loc,
2110 const PartialDiagnostic &NoneDiag,
2111 const PartialDiagnostic &AmbigDiag,
2112 const PartialDiagnostic &CandidateDiag) {
2113 if (SpecBegin == SpecEnd) {
Douglas Gregor450f00842009-09-25 18:43:00 +00002114 Diag(Loc, NoneDiag);
John McCall58cc69d2010-01-27 01:50:18 +00002115 return SpecEnd;
Douglas Gregor450f00842009-09-25 18:43:00 +00002116 }
2117
John McCall58cc69d2010-01-27 01:50:18 +00002118 if (SpecBegin + 1 == SpecEnd)
2119 return SpecBegin;
Douglas Gregor450f00842009-09-25 18:43:00 +00002120
2121 // Find the function template that is better than all of the templates it
2122 // has been compared to.
John McCall58cc69d2010-01-27 01:50:18 +00002123 UnresolvedSetIterator Best = SpecBegin;
Douglas Gregor450f00842009-09-25 18:43:00 +00002124 FunctionTemplateDecl *BestTemplate
John McCall58cc69d2010-01-27 01:50:18 +00002125 = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
Douglas Gregor450f00842009-09-25 18:43:00 +00002126 assert(BestTemplate && "Not a function template specialization?");
John McCall58cc69d2010-01-27 01:50:18 +00002127 for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
2128 FunctionTemplateDecl *Challenger
2129 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregor450f00842009-09-25 18:43:00 +00002130 assert(Challenger && "Not a function template specialization?");
John McCall58cc69d2010-01-27 01:50:18 +00002131 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCallbc077cf2010-02-08 23:07:23 +00002132 Loc, TPOC),
Douglas Gregor450f00842009-09-25 18:43:00 +00002133 Challenger)) {
2134 Best = I;
2135 BestTemplate = Challenger;
2136 }
2137 }
2138
2139 // Make sure that the "best" function template is more specialized than all
2140 // of the others.
2141 bool Ambiguous = false;
John McCall58cc69d2010-01-27 01:50:18 +00002142 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
2143 FunctionTemplateDecl *Challenger
2144 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregor450f00842009-09-25 18:43:00 +00002145 if (I != Best &&
2146 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
John McCallbc077cf2010-02-08 23:07:23 +00002147 Loc, TPOC),
Douglas Gregor450f00842009-09-25 18:43:00 +00002148 BestTemplate)) {
2149 Ambiguous = true;
2150 break;
2151 }
2152 }
2153
2154 if (!Ambiguous) {
2155 // We found an answer. Return it.
John McCall58cc69d2010-01-27 01:50:18 +00002156 return Best;
Douglas Gregor450f00842009-09-25 18:43:00 +00002157 }
2158
2159 // Diagnose the ambiguity.
2160 Diag(Loc, AmbigDiag);
2161
2162 // FIXME: Can we order the candidates in some sane way?
John McCall58cc69d2010-01-27 01:50:18 +00002163 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
2164 Diag((*I)->getLocation(), CandidateDiag)
Douglas Gregor450f00842009-09-25 18:43:00 +00002165 << getTemplateArgumentBindingsText(
John McCall58cc69d2010-01-27 01:50:18 +00002166 cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
2167 *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
Douglas Gregor450f00842009-09-25 18:43:00 +00002168
John McCall58cc69d2010-01-27 01:50:18 +00002169 return SpecEnd;
Douglas Gregor450f00842009-09-25 18:43:00 +00002170}
2171
Douglas Gregorbe999392009-09-15 16:23:51 +00002172/// \brief Returns the more specialized class template partial specialization
2173/// according to the rules of partial ordering of class template partial
2174/// specializations (C++ [temp.class.order]).
2175///
2176/// \param PS1 the first class template partial specialization
2177///
2178/// \param PS2 the second class template partial specialization
2179///
2180/// \returns the more specialized class template partial specialization. If
2181/// neither partial specialization is more specialized, returns NULL.
2182ClassTemplatePartialSpecializationDecl *
2183Sema::getMoreSpecializedPartialSpecialization(
2184 ClassTemplatePartialSpecializationDecl *PS1,
John McCallbc077cf2010-02-08 23:07:23 +00002185 ClassTemplatePartialSpecializationDecl *PS2,
2186 SourceLocation Loc) {
Douglas Gregorbe999392009-09-15 16:23:51 +00002187 // C++ [temp.class.order]p1:
2188 // For two class template partial specializations, the first is at least as
2189 // specialized as the second if, given the following rewrite to two
2190 // function templates, the first function template is at least as
2191 // specialized as the second according to the ordering rules for function
2192 // templates (14.6.6.2):
2193 // - the first function template has the same template parameters as the
2194 // first partial specialization and has a single function parameter
2195 // whose type is a class template specialization with the template
2196 // arguments of the first partial specialization, and
2197 // - the second function template has the same template parameters as the
2198 // second partial specialization and has a single function parameter
2199 // whose type is a class template specialization with the template
2200 // arguments of the second partial specialization.
2201 //
2202 // Rather than synthesize function templates, we merely perform the
2203 // equivalent partial ordering by performing deduction directly on the
2204 // template arguments of the class template partial specializations. This
2205 // computation is slightly simpler than the general problem of function
2206 // template partial ordering, because class template partial specializations
2207 // are more constrained. We know that every template parameter is deduc
2208 llvm::SmallVector<TemplateArgument, 4> Deduced;
John McCallbc077cf2010-02-08 23:07:23 +00002209 Sema::TemplateDeductionInfo Info(Context, Loc);
Douglas Gregorbe999392009-09-15 16:23:51 +00002210
2211 // Determine whether PS1 is at least as specialized as PS2
2212 Deduced.resize(PS2->getTemplateParameters()->size());
Chandler Carruthc1263112010-02-07 21:33:28 +00002213 bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbe999392009-09-15 16:23:51 +00002214 PS2->getTemplateParameters(),
2215 Context.getTypeDeclType(PS2),
2216 Context.getTypeDeclType(PS1),
2217 Info,
2218 Deduced,
2219 0);
2220
2221 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregoradee3e32009-11-11 23:06:43 +00002222 Deduced.clear();
Douglas Gregorbe999392009-09-15 16:23:51 +00002223 Deduced.resize(PS1->getTemplateParameters()->size());
Chandler Carruthc1263112010-02-07 21:33:28 +00002224 bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
Douglas Gregorbe999392009-09-15 16:23:51 +00002225 PS1->getTemplateParameters(),
2226 Context.getTypeDeclType(PS1),
2227 Context.getTypeDeclType(PS2),
2228 Info,
2229 Deduced,
2230 0);
2231
2232 if (Better1 == Better2)
2233 return 0;
2234
2235 return Better1? PS1 : PS2;
2236}
2237
Mike Stump11289f42009-09-09 15:08:12 +00002238static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002239MarkUsedTemplateParameters(Sema &SemaRef,
2240 const TemplateArgument &TemplateArg,
2241 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002242 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002243 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002244
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002245/// \brief Mark the template parameters that are used by the given
Douglas Gregor91772d12009-06-13 00:26:55 +00002246/// expression.
Mike Stump11289f42009-09-09 15:08:12 +00002247static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002248MarkUsedTemplateParameters(Sema &SemaRef,
2249 const Expr *E,
2250 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002251 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002252 llvm::SmallVectorImpl<bool> &Used) {
2253 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2254 // find other occurrences of template parameters.
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00002255 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregor04b11522010-01-14 18:13:22 +00002256 if (!DRE)
Douglas Gregor91772d12009-06-13 00:26:55 +00002257 return;
2258
Mike Stump11289f42009-09-09 15:08:12 +00002259 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor91772d12009-06-13 00:26:55 +00002260 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2261 if (!NTTP)
2262 return;
2263
Douglas Gregor21610382009-10-29 00:04:11 +00002264 if (NTTP->getDepth() == Depth)
2265 Used[NTTP->getIndex()] = true;
Douglas Gregor91772d12009-06-13 00:26:55 +00002266}
2267
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002268/// \brief Mark the template parameters that are used by the given
2269/// nested name specifier.
2270static void
2271MarkUsedTemplateParameters(Sema &SemaRef,
2272 NestedNameSpecifier *NNS,
2273 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002274 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002275 llvm::SmallVectorImpl<bool> &Used) {
2276 if (!NNS)
2277 return;
2278
Douglas Gregor21610382009-10-29 00:04:11 +00002279 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2280 Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002281 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregor21610382009-10-29 00:04:11 +00002282 OnlyDeduced, Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002283}
2284
2285/// \brief Mark the template parameters that are used by the given
2286/// template name.
2287static void
2288MarkUsedTemplateParameters(Sema &SemaRef,
2289 TemplateName Name,
2290 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002291 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002292 llvm::SmallVectorImpl<bool> &Used) {
2293 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2294 if (TemplateTemplateParmDecl *TTP
Douglas Gregor21610382009-10-29 00:04:11 +00002295 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2296 if (TTP->getDepth() == Depth)
2297 Used[TTP->getIndex()] = true;
2298 }
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002299 return;
2300 }
2301
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002302 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2303 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2304 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002305 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
Douglas Gregor21610382009-10-29 00:04:11 +00002306 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2307 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002308}
2309
2310/// \brief Mark the template parameters that are used by the given
Douglas Gregor91772d12009-06-13 00:26:55 +00002311/// type.
Mike Stump11289f42009-09-09 15:08:12 +00002312static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002313MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2314 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002315 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002316 llvm::SmallVectorImpl<bool> &Used) {
2317 if (T.isNull())
2318 return;
2319
Douglas Gregor91772d12009-06-13 00:26:55 +00002320 // Non-dependent types have nothing deducible
2321 if (!T->isDependentType())
2322 return;
2323
2324 T = SemaRef.Context.getCanonicalType(T);
2325 switch (T->getTypeClass()) {
Douglas Gregor91772d12009-06-13 00:26:55 +00002326 case Type::Pointer:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002327 MarkUsedTemplateParameters(SemaRef,
2328 cast<PointerType>(T)->getPointeeType(),
2329 OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002330 Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002331 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002332 break;
2333
2334 case Type::BlockPointer:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002335 MarkUsedTemplateParameters(SemaRef,
2336 cast<BlockPointerType>(T)->getPointeeType(),
2337 OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002338 Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002339 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002340 break;
2341
2342 case Type::LValueReference:
2343 case Type::RValueReference:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002344 MarkUsedTemplateParameters(SemaRef,
2345 cast<ReferenceType>(T)->getPointeeType(),
2346 OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002347 Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002348 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002349 break;
2350
2351 case Type::MemberPointer: {
2352 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002353 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002354 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002355 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregor21610382009-10-29 00:04:11 +00002356 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002357 break;
2358 }
2359
2360 case Type::DependentSizedArray:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002361 MarkUsedTemplateParameters(SemaRef,
2362 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregor21610382009-10-29 00:04:11 +00002363 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002364 // Fall through to check the element type
2365
2366 case Type::ConstantArray:
2367 case Type::IncompleteArray:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002368 MarkUsedTemplateParameters(SemaRef,
2369 cast<ArrayType>(T)->getElementType(),
Douglas Gregor21610382009-10-29 00:04:11 +00002370 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002371 break;
2372
2373 case Type::Vector:
2374 case Type::ExtVector:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002375 MarkUsedTemplateParameters(SemaRef,
2376 cast<VectorType>(T)->getElementType(),
Douglas Gregor21610382009-10-29 00:04:11 +00002377 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002378 break;
2379
Douglas Gregor758a8692009-06-17 21:51:59 +00002380 case Type::DependentSizedExtVector: {
2381 const DependentSizedExtVectorType *VecType
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00002382 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002383 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002384 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002385 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002386 Depth, Used);
Douglas Gregor758a8692009-06-17 21:51:59 +00002387 break;
2388 }
2389
Douglas Gregor91772d12009-06-13 00:26:55 +00002390 case Type::FunctionProto: {
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00002391 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002392 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002393 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002394 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002395 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002396 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002397 break;
2398 }
2399
Douglas Gregor21610382009-10-29 00:04:11 +00002400 case Type::TemplateTypeParm: {
2401 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2402 if (TTP->getDepth() == Depth)
2403 Used[TTP->getIndex()] = true;
Douglas Gregor91772d12009-06-13 00:26:55 +00002404 break;
Douglas Gregor21610382009-10-29 00:04:11 +00002405 }
Douglas Gregor91772d12009-06-13 00:26:55 +00002406
2407 case Type::TemplateSpecialization: {
Mike Stump11289f42009-09-09 15:08:12 +00002408 const TemplateSpecializationType *Spec
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00002409 = cast<TemplateSpecializationType>(T);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002410 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002411 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002412 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregor21610382009-10-29 00:04:11 +00002413 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2414 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002415 break;
2416 }
2417
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002418 case Type::Complex:
2419 if (!OnlyDeduced)
2420 MarkUsedTemplateParameters(SemaRef,
2421 cast<ComplexType>(T)->getElementType(),
Douglas Gregor21610382009-10-29 00:04:11 +00002422 OnlyDeduced, Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002423 break;
2424
2425 case Type::Typename:
2426 if (!OnlyDeduced)
2427 MarkUsedTemplateParameters(SemaRef,
2428 cast<TypenameType>(T)->getQualifier(),
Douglas Gregor21610382009-10-29 00:04:11 +00002429 OnlyDeduced, Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002430 break;
2431
John McCallbd8d9bd2010-03-01 23:49:17 +00002432 case Type::TypeOf:
2433 if (!OnlyDeduced)
2434 MarkUsedTemplateParameters(SemaRef,
2435 cast<TypeOfType>(T)->getUnderlyingType(),
2436 OnlyDeduced, Depth, Used);
2437 break;
2438
2439 case Type::TypeOfExpr:
2440 if (!OnlyDeduced)
2441 MarkUsedTemplateParameters(SemaRef,
2442 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
2443 OnlyDeduced, Depth, Used);
2444 break;
2445
2446 case Type::Decltype:
2447 if (!OnlyDeduced)
2448 MarkUsedTemplateParameters(SemaRef,
2449 cast<DecltypeType>(T)->getUnderlyingExpr(),
2450 OnlyDeduced, Depth, Used);
2451 break;
2452
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002453 // None of these types have any template parameters in them.
Douglas Gregor91772d12009-06-13 00:26:55 +00002454 case Type::Builtin:
Douglas Gregor91772d12009-06-13 00:26:55 +00002455 case Type::VariableArray:
2456 case Type::FunctionNoProto:
2457 case Type::Record:
2458 case Type::Enum:
Douglas Gregor91772d12009-06-13 00:26:55 +00002459 case Type::ObjCInterface:
Steve Narofffb4330f2009-06-17 22:40:22 +00002460 case Type::ObjCObjectPointer:
John McCallb96ec562009-12-04 22:46:56 +00002461 case Type::UnresolvedUsing:
Douglas Gregor91772d12009-06-13 00:26:55 +00002462#define TYPE(Class, Base)
2463#define ABSTRACT_TYPE(Class, Base)
2464#define DEPENDENT_TYPE(Class, Base)
2465#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2466#include "clang/AST/TypeNodes.def"
2467 break;
2468 }
2469}
2470
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002471/// \brief Mark the template parameters that are used by this
Douglas Gregor91772d12009-06-13 00:26:55 +00002472/// template argument.
Mike Stump11289f42009-09-09 15:08:12 +00002473static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002474MarkUsedTemplateParameters(Sema &SemaRef,
2475 const TemplateArgument &TemplateArg,
2476 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002477 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002478 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor91772d12009-06-13 00:26:55 +00002479 switch (TemplateArg.getKind()) {
2480 case TemplateArgument::Null:
2481 case TemplateArgument::Integral:
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002482 case TemplateArgument::Declaration:
Douglas Gregor91772d12009-06-13 00:26:55 +00002483 break;
Mike Stump11289f42009-09-09 15:08:12 +00002484
Douglas Gregor91772d12009-06-13 00:26:55 +00002485 case TemplateArgument::Type:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002486 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002487 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002488 break;
2489
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002490 case TemplateArgument::Template:
2491 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2492 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002493 break;
2494
2495 case TemplateArgument::Expression:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002496 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002497 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002498 break;
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002499
Anders Carlssonbc343912009-06-15 17:04:53 +00002500 case TemplateArgument::Pack:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002501 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2502 PEnd = TemplateArg.pack_end();
2503 P != PEnd; ++P)
Douglas Gregor21610382009-10-29 00:04:11 +00002504 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssonbc343912009-06-15 17:04:53 +00002505 break;
Douglas Gregor91772d12009-06-13 00:26:55 +00002506 }
2507}
2508
2509/// \brief Mark the template parameters can be deduced by the given
2510/// template argument list.
2511///
2512/// \param TemplateArgs the template argument list from which template
2513/// parameters will be deduced.
2514///
2515/// \param Deduced a bit vector whose elements will be set to \c true
2516/// to indicate when the corresponding template parameter will be
2517/// deduced.
Mike Stump11289f42009-09-09 15:08:12 +00002518void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002519Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregor21610382009-10-29 00:04:11 +00002520 bool OnlyDeduced, unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002521 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor91772d12009-06-13 00:26:55 +00002522 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
Douglas Gregor21610382009-10-29 00:04:11 +00002523 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2524 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002525}
Douglas Gregorce23bae2009-09-18 23:21:38 +00002526
2527/// \brief Marks all of the template parameters that will be deduced by a
2528/// call to the given function template.
2529void Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
2530 llvm::SmallVectorImpl<bool> &Deduced) {
2531 TemplateParameterList *TemplateParams
2532 = FunctionTemplate->getTemplateParameters();
2533 Deduced.clear();
2534 Deduced.resize(TemplateParams->size());
2535
2536 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2537 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
2538 ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
Douglas Gregor21610382009-10-29 00:04:11 +00002539 true, TemplateParams->getDepth(), Deduced);
Douglas Gregorce23bae2009-09-18 23:21:38 +00002540}