blob: 06b2dec590b2297f52b5d862d8266ccf76ee85cb [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"
20#include "llvm/Support/Compiler.h"
Douglas Gregor0ff7d922009-09-14 18:39:43 +000021#include <algorithm>
Douglas Gregorcf0b47d2009-06-26 23:10:12 +000022
23namespace clang {
24 /// \brief Various flags that control template argument deduction.
25 ///
26 /// These flags can be bitwise-OR'd together.
27 enum TemplateDeductionFlags {
28 /// \brief No template argument deduction flags, which indicates the
29 /// strictest results for template argument deduction (as used for, e.g.,
30 /// matching class template partial specializations).
31 TDF_None = 0,
32 /// \brief Within template argument deduction from a function call, we are
33 /// matching with a parameter type for which the original parameter was
34 /// a reference.
35 TDF_ParamWithReferenceType = 0x1,
36 /// \brief Within template argument deduction from a function call, we
37 /// are matching in a case where we ignore cv-qualifiers.
38 TDF_IgnoreQualifiers = 0x02,
39 /// \brief Within template argument deduction from a function call,
40 /// we are matching in a case where we can perform template argument
Douglas Gregorfc516c92009-06-26 23:27:24 +000041 /// deduction from a template-id of a derived class of the argument type.
Douglas Gregor406f6342009-09-14 20:00:47 +000042 TDF_DerivedClass = 0x04,
43 /// \brief Allow non-dependent types to differ, e.g., when performing
44 /// template argument deduction from a function call where conversions
45 /// may apply.
46 TDF_SkipNonDependent = 0x08
Douglas Gregorcf0b47d2009-06-26 23:10:12 +000047 };
48}
49
Douglas Gregor55ca8f62009-06-04 00:03:07 +000050using namespace clang;
51
Douglas Gregor181aa4a2009-06-12 18:26:56 +000052static Sema::TemplateDeductionResult
Mike Stump11289f42009-09-09 15:08:12 +000053DeduceTemplateArguments(ASTContext &Context,
Douglas Gregor181aa4a2009-06-12 18:26:56 +000054 TemplateParameterList *TemplateParams,
55 const TemplateArgument &Param,
Douglas Gregor4fbe3e32009-06-09 16:35:58 +000056 const TemplateArgument &Arg,
Douglas Gregor181aa4a2009-06-12 18:26:56 +000057 Sema::TemplateDeductionInfo &Info,
Douglas Gregor4fbe3e32009-06-09 16:35:58 +000058 llvm::SmallVectorImpl<TemplateArgument> &Deduced);
59
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000060/// \brief If the given expression is of a form that permits the deduction
61/// of a non-type template parameter, return the declaration of that
62/// non-type template parameter.
63static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
64 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
65 E = IC->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +000066
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000067 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
68 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
Mike Stump11289f42009-09-09 15:08:12 +000069
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000070 return 0;
71}
72
Mike Stump11289f42009-09-09 15:08:12 +000073/// \brief Deduce the value of the given non-type template parameter
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000074/// from the given constant.
Douglas Gregor181aa4a2009-06-12 18:26:56 +000075static Sema::TemplateDeductionResult
Mike Stump11289f42009-09-09 15:08:12 +000076DeduceNonTypeTemplateArgument(ASTContext &Context,
77 NonTypeTemplateParmDecl *NTTP,
Anders Carlsson3a106e02009-06-16 22:44:31 +000078 llvm::APSInt Value,
Douglas Gregor181aa4a2009-06-12 18:26:56 +000079 Sema::TemplateDeductionInfo &Info,
80 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Mike Stump11289f42009-09-09 15:08:12 +000081 assert(NTTP->getDepth() == 0 &&
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000082 "Cannot deduce non-type template argument with depth > 0");
Mike Stump11289f42009-09-09 15:08:12 +000083
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000084 if (Deduced[NTTP->getIndex()].isNull()) {
Anders Carlsson655908a2009-06-16 23:08:29 +000085 QualType T = NTTP->getType();
Mike Stump11289f42009-09-09 15:08:12 +000086
Anders Carlsson655908a2009-06-16 23:08:29 +000087 // FIXME: Make sure we didn't overflow our data type!
88 unsigned AllowedBits = Context.getTypeSize(T);
89 if (Value.getBitWidth() != AllowedBits)
90 Value.extOrTrunc(AllowedBits);
91 Value.setIsSigned(T->isSignedIntegerType());
92
John McCall0ad16662009-10-29 08:12:44 +000093 Deduced[NTTP->getIndex()] = TemplateArgument(Value, T);
Douglas Gregor181aa4a2009-06-12 18:26:56 +000094 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +000095 }
Mike Stump11289f42009-09-09 15:08:12 +000096
Douglas Gregor181aa4a2009-06-12 18:26:56 +000097 assert(Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral);
Mike Stump11289f42009-09-09 15:08:12 +000098
99 // If the template argument was previously deduced to a negative value,
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000100 // then our deduction fails.
101 const llvm::APSInt *PrevValuePtr = Deduced[NTTP->getIndex()].getAsIntegral();
Anders Carlsson3a106e02009-06-16 22:44:31 +0000102 if (PrevValuePtr->isNegative()) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000103 Info.Param = NTTP;
104 Info.FirstArg = Deduced[NTTP->getIndex()];
John McCall0ad16662009-10-29 08:12:44 +0000105 Info.SecondArg = TemplateArgument(Value, NTTP->getType());
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000106 return Sema::TDK_Inconsistent;
107 }
108
Anders Carlsson3a106e02009-06-16 22:44:31 +0000109 llvm::APSInt PrevValue = *PrevValuePtr;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000110 if (Value.getBitWidth() > PrevValue.getBitWidth())
111 PrevValue.zext(Value.getBitWidth());
112 else if (Value.getBitWidth() < PrevValue.getBitWidth())
113 Value.zext(PrevValue.getBitWidth());
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000114
115 if (Value != PrevValue) {
116 Info.Param = NTTP;
117 Info.FirstArg = Deduced[NTTP->getIndex()];
John McCall0ad16662009-10-29 08:12:44 +0000118 Info.SecondArg = TemplateArgument(Value, NTTP->getType());
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000119 return Sema::TDK_Inconsistent;
120 }
121
122 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000123}
124
Mike Stump11289f42009-09-09 15:08:12 +0000125/// \brief Deduce the value of the given non-type template parameter
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000126/// from the given type- or value-dependent expression.
127///
128/// \returns true if deduction succeeded, false otherwise.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000129static Sema::TemplateDeductionResult
Mike Stump11289f42009-09-09 15:08:12 +0000130DeduceNonTypeTemplateArgument(ASTContext &Context,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000131 NonTypeTemplateParmDecl *NTTP,
132 Expr *Value,
133 Sema::TemplateDeductionInfo &Info,
134 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Mike Stump11289f42009-09-09 15:08:12 +0000135 assert(NTTP->getDepth() == 0 &&
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000136 "Cannot deduce non-type template argument with depth > 0");
137 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
138 "Expression template argument must be type- or value-dependent.");
Mike Stump11289f42009-09-09 15:08:12 +0000139
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000140 if (Deduced[NTTP->getIndex()].isNull()) {
141 // FIXME: Clone the Value?
142 Deduced[NTTP->getIndex()] = TemplateArgument(Value);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000143 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000144 }
Mike Stump11289f42009-09-09 15:08:12 +0000145
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000146 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
Mike Stump11289f42009-09-09 15:08:12 +0000147 // Okay, we deduced a constant in one case and a dependent expression
148 // in another case. FIXME: Later, we will check that instantiating the
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000149 // dependent expression gives us the constant value.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000150 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000151 }
Mike Stump11289f42009-09-09 15:08:12 +0000152
Douglas Gregor00a511f2009-09-15 16:51:42 +0000153 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
154 // Compare the expressions for equality
155 llvm::FoldingSetNodeID ID1, ID2;
156 Deduced[NTTP->getIndex()].getAsExpr()->Profile(ID1, Context, true);
157 Value->Profile(ID2, Context, true);
158 if (ID1 == ID2)
159 return Sema::TDK_Success;
160
161 // FIXME: Fill in argument mismatch information
162 return Sema::TDK_NonDeducedMismatch;
163 }
164
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000165 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000166}
167
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000168/// \brief Deduce the value of the given non-type template parameter
169/// from the given declaration.
170///
171/// \returns true if deduction succeeded, false otherwise.
172static Sema::TemplateDeductionResult
173DeduceNonTypeTemplateArgument(ASTContext &Context,
174 NonTypeTemplateParmDecl *NTTP,
175 Decl *D,
176 Sema::TemplateDeductionInfo &Info,
177 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
178 assert(NTTP->getDepth() == 0 &&
179 "Cannot deduce non-type template argument with depth > 0");
180
181 if (Deduced[NTTP->getIndex()].isNull()) {
182 Deduced[NTTP->getIndex()] = TemplateArgument(D->getCanonicalDecl());
183 return Sema::TDK_Success;
184 }
185
186 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
187 // Okay, we deduced a declaration in one case and a dependent expression
188 // in another case.
189 return Sema::TDK_Success;
190 }
191
192 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Declaration) {
193 // Compare the declarations for equality
194 if (Deduced[NTTP->getIndex()].getAsDecl()->getCanonicalDecl() ==
195 D->getCanonicalDecl())
196 return Sema::TDK_Success;
197
198 // FIXME: Fill in argument mismatch information
199 return Sema::TDK_NonDeducedMismatch;
200 }
201
202 return Sema::TDK_Success;
203}
204
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000205static Sema::TemplateDeductionResult
206DeduceTemplateArguments(ASTContext &Context,
Douglas Gregoradee3e32009-11-11 23:06:43 +0000207 TemplateParameterList *TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000208 TemplateName Param,
209 TemplateName Arg,
210 Sema::TemplateDeductionInfo &Info,
211 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000212 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
Douglas Gregoradee3e32009-11-11 23:06:43 +0000213 if (!ParamDecl) {
214 // The parameter type is dependent and is not a template template parameter,
215 // so there is nothing that we can deduce.
216 return Sema::TDK_Success;
217 }
218
219 if (TemplateTemplateParmDecl *TempParam
220 = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) {
221 // Bind the template template parameter to the given template name.
222 TemplateArgument &ExistingArg = Deduced[TempParam->getIndex()];
223 if (ExistingArg.isNull()) {
224 // This is the first deduction for this template template parameter.
225 ExistingArg = TemplateArgument(Context.getCanonicalTemplateName(Arg));
226 return Sema::TDK_Success;
227 }
228
229 // Verify that the previous binding matches this deduction.
230 assert(ExistingArg.getKind() == TemplateArgument::Template);
231 if (Context.hasSameTemplateName(ExistingArg.getAsTemplate(), Arg))
232 return Sema::TDK_Success;
233
234 // Inconsistent deduction.
235 Info.Param = TempParam;
236 Info.FirstArg = ExistingArg;
237 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000238 return Sema::TDK_Inconsistent;
239 }
Douglas Gregoradee3e32009-11-11 23:06:43 +0000240
241 // Verify that the two template names are equivalent.
242 if (Context.hasSameTemplateName(Param, Arg))
243 return Sema::TDK_Success;
244
245 // Mismatch of non-dependent template parameter to argument.
246 Info.FirstArg = TemplateArgument(Param);
247 Info.SecondArg = TemplateArgument(Arg);
248 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000249}
250
Mike Stump11289f42009-09-09 15:08:12 +0000251/// \brief Deduce the template arguments by comparing the template parameter
Douglas Gregore81f3e72009-07-07 23:09:34 +0000252/// type (which is a template-id) with the template argument type.
253///
254/// \param Context the AST context in which this deduction occurs.
255///
256/// \param TemplateParams the template parameters that we are deducing
257///
258/// \param Param the parameter type
259///
260/// \param Arg the argument type
261///
262/// \param Info information about the template argument deduction itself
263///
264/// \param Deduced the deduced template arguments
265///
266/// \returns the result of template argument deduction so far. Note that a
267/// "success" result means that template argument deduction has not yet failed,
268/// but it may still fail, later, for other reasons.
269static Sema::TemplateDeductionResult
270DeduceTemplateArguments(ASTContext &Context,
271 TemplateParameterList *TemplateParams,
272 const TemplateSpecializationType *Param,
273 QualType Arg,
274 Sema::TemplateDeductionInfo &Info,
275 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
John McCallb692a092009-10-22 20:10:53 +0000276 assert(Arg.isCanonical() && "Argument type must be canonical");
Mike Stump11289f42009-09-09 15:08:12 +0000277
Douglas Gregore81f3e72009-07-07 23:09:34 +0000278 // Check whether the template argument is a dependent template-id.
Mike Stump11289f42009-09-09 15:08:12 +0000279 if (const TemplateSpecializationType *SpecArg
Douglas Gregore81f3e72009-07-07 23:09:34 +0000280 = dyn_cast<TemplateSpecializationType>(Arg)) {
281 // Perform template argument deduction for the template name.
282 if (Sema::TemplateDeductionResult Result
Douglas Gregoradee3e32009-11-11 23:06:43 +0000283 = DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000284 Param->getTemplateName(),
285 SpecArg->getTemplateName(),
286 Info, Deduced))
287 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000288
Mike Stump11289f42009-09-09 15:08:12 +0000289
Douglas Gregore81f3e72009-07-07 23:09:34 +0000290 // Perform template argument deduction on each template
291 // argument.
Douglas Gregoradee3e32009-11-11 23:06:43 +0000292 unsigned NumArgs = std::min(SpecArg->getNumArgs(), Param->getNumArgs());
Douglas Gregore81f3e72009-07-07 23:09:34 +0000293 for (unsigned I = 0; I != NumArgs; ++I)
294 if (Sema::TemplateDeductionResult Result
295 = DeduceTemplateArguments(Context, TemplateParams,
296 Param->getArg(I),
297 SpecArg->getArg(I),
298 Info, Deduced))
299 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000300
Douglas Gregore81f3e72009-07-07 23:09:34 +0000301 return Sema::TDK_Success;
302 }
Mike Stump11289f42009-09-09 15:08:12 +0000303
Douglas Gregore81f3e72009-07-07 23:09:34 +0000304 // If the argument type is a class template specialization, we
305 // perform template argument deduction using its template
306 // arguments.
307 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
308 if (!RecordArg)
309 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000310
311 ClassTemplateSpecializationDecl *SpecArg
Douglas Gregore81f3e72009-07-07 23:09:34 +0000312 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
313 if (!SpecArg)
314 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000315
Douglas Gregore81f3e72009-07-07 23:09:34 +0000316 // Perform template argument deduction for the template name.
317 if (Sema::TemplateDeductionResult Result
Mike Stump11289f42009-09-09 15:08:12 +0000318 = DeduceTemplateArguments(Context,
Douglas Gregoradee3e32009-11-11 23:06:43 +0000319 TemplateParams,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000320 Param->getTemplateName(),
321 TemplateName(SpecArg->getSpecializedTemplate()),
322 Info, Deduced))
323 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000324
Douglas Gregore81f3e72009-07-07 23:09:34 +0000325 unsigned NumArgs = Param->getNumArgs();
326 const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
327 if (NumArgs != ArgArgs.size())
328 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000329
Douglas Gregore81f3e72009-07-07 23:09:34 +0000330 for (unsigned I = 0; I != NumArgs; ++I)
Mike Stump11289f42009-09-09 15:08:12 +0000331 if (Sema::TemplateDeductionResult Result
Douglas Gregore81f3e72009-07-07 23:09:34 +0000332 = DeduceTemplateArguments(Context, TemplateParams,
333 Param->getArg(I),
334 ArgArgs.get(I),
335 Info, Deduced))
336 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000337
Douglas Gregore81f3e72009-07-07 23:09:34 +0000338 return Sema::TDK_Success;
339}
340
Mike Stump11289f42009-09-09 15:08:12 +0000341/// \brief Returns a completely-unqualified array type, capturing the
John McCall8ccfcb52009-09-24 19:53:00 +0000342/// qualifiers in Quals.
Douglas Gregord6605db2009-07-22 21:30:48 +0000343///
344/// \param Context the AST context in which the array type was built.
345///
346/// \param T a canonical type that may be an array type.
347///
John McCall8ccfcb52009-09-24 19:53:00 +0000348/// \param Quals will receive the full set of qualifiers that were
349/// applied to the element type of the array.
Douglas Gregord6605db2009-07-22 21:30:48 +0000350///
351/// \returns if \p T is an array type, the completely unqualified array type
352/// that corresponds to T. Otherwise, returns T.
353static QualType getUnqualifiedArrayType(ASTContext &Context, QualType T,
John McCall8ccfcb52009-09-24 19:53:00 +0000354 Qualifiers &Quals) {
John McCallb692a092009-10-22 20:10:53 +0000355 assert(T.isCanonical() && "Only operates on canonical types");
Douglas Gregord6605db2009-07-22 21:30:48 +0000356 if (!isa<ArrayType>(T)) {
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +0000357 Quals = T.getLocalQualifiers();
358 return T.getLocalUnqualifiedType();
Douglas Gregord6605db2009-07-22 21:30:48 +0000359 }
Mike Stump11289f42009-09-09 15:08:12 +0000360
John McCall8ccfcb52009-09-24 19:53:00 +0000361 assert(!T.hasQualifiers() && "canonical array type has qualifiers!");
362
Douglas Gregord6605db2009-07-22 21:30:48 +0000363 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) {
364 QualType Elt = getUnqualifiedArrayType(Context, CAT->getElementType(),
John McCall8ccfcb52009-09-24 19:53:00 +0000365 Quals);
Douglas Gregord6605db2009-07-22 21:30:48 +0000366 if (Elt == CAT->getElementType())
367 return T;
368
Mike Stump11289f42009-09-09 15:08:12 +0000369 return Context.getConstantArrayType(Elt, CAT->getSize(),
Douglas Gregord6605db2009-07-22 21:30:48 +0000370 CAT->getSizeModifier(), 0);
371 }
Mike Stump11289f42009-09-09 15:08:12 +0000372
Douglas Gregord6605db2009-07-22 21:30:48 +0000373 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) {
374 QualType Elt = getUnqualifiedArrayType(Context, IAT->getElementType(),
John McCall8ccfcb52009-09-24 19:53:00 +0000375 Quals);
Douglas Gregord6605db2009-07-22 21:30:48 +0000376 if (Elt == IAT->getElementType())
377 return T;
Mike Stump11289f42009-09-09 15:08:12 +0000378
Douglas Gregord6605db2009-07-22 21:30:48 +0000379 return Context.getIncompleteArrayType(Elt, IAT->getSizeModifier(), 0);
380 }
Mike Stump11289f42009-09-09 15:08:12 +0000381
Douglas Gregord6605db2009-07-22 21:30:48 +0000382 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T);
383 QualType Elt = getUnqualifiedArrayType(Context, DSAT->getElementType(),
John McCall8ccfcb52009-09-24 19:53:00 +0000384 Quals);
Douglas Gregord6605db2009-07-22 21:30:48 +0000385 if (Elt == DSAT->getElementType())
386 return T;
Mike Stump11289f42009-09-09 15:08:12 +0000387
Anders Carlssonc5c57c32009-08-08 02:50:17 +0000388 return Context.getDependentSizedArrayType(Elt, DSAT->getSizeExpr()->Retain(),
Douglas Gregord6605db2009-07-22 21:30:48 +0000389 DSAT->getSizeModifier(), 0,
390 SourceRange());
391}
392
Douglas Gregorcceb9752009-06-26 18:27:22 +0000393/// \brief Deduce the template arguments by comparing the parameter type and
394/// the argument type (C++ [temp.deduct.type]).
395///
396/// \param Context the AST context in which this deduction occurs.
397///
398/// \param TemplateParams the template parameters that we are deducing
399///
400/// \param ParamIn the parameter type
401///
402/// \param ArgIn the argument type
403///
404/// \param Info information about the template argument deduction itself
405///
406/// \param Deduced the deduced template arguments
407///
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000408/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
Mike Stump11289f42009-09-09 15:08:12 +0000409/// how template argument deduction is performed.
Douglas Gregorcceb9752009-06-26 18:27:22 +0000410///
411/// \returns the result of template argument deduction so far. Note that a
412/// "success" result means that template argument deduction has not yet failed,
413/// but it may still fail, later, for other reasons.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000414static Sema::TemplateDeductionResult
Mike Stump11289f42009-09-09 15:08:12 +0000415DeduceTemplateArguments(ASTContext &Context,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000416 TemplateParameterList *TemplateParams,
417 QualType ParamIn, QualType ArgIn,
418 Sema::TemplateDeductionInfo &Info,
Douglas Gregorcceb9752009-06-26 18:27:22 +0000419 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000420 unsigned TDF) {
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000421 // We only want to look at the canonical types, since typedefs and
422 // sugar are not part of template argument deduction.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000423 QualType Param = Context.getCanonicalType(ParamIn);
424 QualType Arg = Context.getCanonicalType(ArgIn);
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000425
Douglas Gregorcceb9752009-06-26 18:27:22 +0000426 // C++0x [temp.deduct.call]p4 bullet 1:
427 // - If the original P is a reference type, the deduced A (i.e., the type
Mike Stump11289f42009-09-09 15:08:12 +0000428 // referred to by the reference) can be more cv-qualified than the
Douglas Gregorcceb9752009-06-26 18:27:22 +0000429 // transformed A.
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000430 if (TDF & TDF_ParamWithReferenceType) {
John McCall8ccfcb52009-09-24 19:53:00 +0000431 Qualifiers Quals = Param.getQualifiers();
432 Quals.setCVRQualifiers(Quals.getCVRQualifiers() & Arg.getCVRQualifiers());
433 Param = Context.getQualifiedType(Param.getUnqualifiedType(), Quals);
Douglas Gregorcceb9752009-06-26 18:27:22 +0000434 }
Mike Stump11289f42009-09-09 15:08:12 +0000435
Douglas Gregor705c9002009-06-26 20:57:09 +0000436 // If the parameter type is not dependent, there is nothing to deduce.
Douglas Gregor406f6342009-09-14 20:00:47 +0000437 if (!Param->isDependentType()) {
438 if (!(TDF & TDF_SkipNonDependent) && Param != Arg) {
439
440 return Sema::TDK_NonDeducedMismatch;
441 }
442
Douglas Gregor705c9002009-06-26 20:57:09 +0000443 return Sema::TDK_Success;
Douglas Gregor406f6342009-09-14 20:00:47 +0000444 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000445
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000446 // C++ [temp.deduct.type]p9:
Mike Stump11289f42009-09-09 15:08:12 +0000447 // A template type argument T, a template template argument TT or a
448 // template non-type argument i can be deduced if P and A have one of
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000449 // the following forms:
450 //
451 // T
452 // cv-list T
Mike Stump11289f42009-09-09 15:08:12 +0000453 if (const TemplateTypeParmType *TemplateTypeParm
John McCall9dd450b2009-09-21 23:43:11 +0000454 = Param->getAs<TemplateTypeParmType>()) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000455 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregord6605db2009-07-22 21:30:48 +0000456 bool RecanonicalizeArg = false;
Mike Stump11289f42009-09-09 15:08:12 +0000457
Douglas Gregor60454822009-07-22 20:02:25 +0000458 // If the argument type is an array type, move the qualifiers up to the
459 // top level, so they can be matched with the qualifiers on the parameter.
460 // FIXME: address spaces, ObjC GC qualifiers
Douglas Gregord6605db2009-07-22 21:30:48 +0000461 if (isa<ArrayType>(Arg)) {
John McCall8ccfcb52009-09-24 19:53:00 +0000462 Qualifiers Quals;
463 Arg = getUnqualifiedArrayType(Context, Arg, Quals);
464 if (Quals) {
465 Arg = Context.getQualifiedType(Arg, Quals);
Douglas Gregord6605db2009-07-22 21:30:48 +0000466 RecanonicalizeArg = true;
467 }
468 }
Mike Stump11289f42009-09-09 15:08:12 +0000469
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000470 // The argument type can not be less qualified than the parameter
471 // type.
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000472 if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000473 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
474 Info.FirstArg = Deduced[Index];
John McCall0ad16662009-10-29 08:12:44 +0000475 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000476 return Sema::TDK_InconsistentQuals;
477 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000478
479 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
Mike Stump11289f42009-09-09 15:08:12 +0000480
John McCall8ccfcb52009-09-24 19:53:00 +0000481 QualType DeducedType = Arg;
482 DeducedType.removeCVRQualifiers(Param.getCVRQualifiers());
Douglas Gregord6605db2009-07-22 21:30:48 +0000483 if (RecanonicalizeArg)
484 DeducedType = Context.getCanonicalType(DeducedType);
Mike Stump11289f42009-09-09 15:08:12 +0000485
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000486 if (Deduced[Index].isNull())
John McCall0ad16662009-10-29 08:12:44 +0000487 Deduced[Index] = TemplateArgument(DeducedType);
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000488 else {
Mike Stump11289f42009-09-09 15:08:12 +0000489 // C++ [temp.deduct.type]p2:
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000490 // [...] If type deduction cannot be done for any P/A pair, or if for
Mike Stump11289f42009-09-09 15:08:12 +0000491 // any pair the deduction leads to more than one possible set of
492 // deduced values, or if different pairs yield different deduced
493 // values, or if any template argument remains neither deduced nor
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000494 // explicitly specified, template argument deduction fails.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000495 if (Deduced[Index].getAsType() != DeducedType) {
Mike Stump11289f42009-09-09 15:08:12 +0000496 Info.Param
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000497 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
498 Info.FirstArg = Deduced[Index];
John McCall0ad16662009-10-29 08:12:44 +0000499 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000500 return Sema::TDK_Inconsistent;
501 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000502 }
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000503 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000504 }
505
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000506 // Set up the template argument deduction information for a failure.
John McCall0ad16662009-10-29 08:12:44 +0000507 Info.FirstArg = TemplateArgument(ParamIn);
508 Info.SecondArg = TemplateArgument(ArgIn);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000509
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000510 // Check the cv-qualifiers on the parameter and argument types.
511 if (!(TDF & TDF_IgnoreQualifiers)) {
512 if (TDF & TDF_ParamWithReferenceType) {
513 if (Param.isMoreQualifiedThan(Arg))
514 return Sema::TDK_NonDeducedMismatch;
515 } else {
516 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Mike Stump11289f42009-09-09 15:08:12 +0000517 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000518 }
519 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000520
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000521 switch (Param->getTypeClass()) {
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000522 // No deduction possible for these types
523 case Type::Builtin:
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000524 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000525
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000526 // T *
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000527 case Type::Pointer: {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000528 const PointerType *PointerArg = Arg->getAs<PointerType>();
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000529 if (!PointerArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000530 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000531
Douglas Gregorfc516c92009-06-26 23:27:24 +0000532 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000533 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000534 cast<PointerType>(Param)->getPointeeType(),
535 PointerArg->getPointeeType(),
Douglas Gregorfc516c92009-06-26 23:27:24 +0000536 Info, Deduced, SubTDF);
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000537 }
Mike Stump11289f42009-09-09 15:08:12 +0000538
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000539 // T &
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000540 case Type::LValueReference: {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000541 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000542 if (!ReferenceArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000543 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000544
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000545 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000546 cast<LValueReferenceType>(Param)->getPointeeType(),
547 ReferenceArg->getPointeeType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000548 Info, Deduced, 0);
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000549 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000550
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000551 // T && [C++0x]
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000552 case Type::RValueReference: {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000553 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000554 if (!ReferenceArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000555 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000556
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000557 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000558 cast<RValueReferenceType>(Param)->getPointeeType(),
559 ReferenceArg->getPointeeType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000560 Info, Deduced, 0);
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000561 }
Mike Stump11289f42009-09-09 15:08:12 +0000562
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000563 // T [] (implied, but not stated explicitly)
Anders Carlsson35533d12009-06-04 04:11:30 +0000564 case Type::IncompleteArray: {
Mike Stump11289f42009-09-09 15:08:12 +0000565 const IncompleteArrayType *IncompleteArrayArg =
Anders Carlsson35533d12009-06-04 04:11:30 +0000566 Context.getAsIncompleteArrayType(Arg);
567 if (!IncompleteArrayArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000568 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000569
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000570 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson35533d12009-06-04 04:11:30 +0000571 Context.getAsIncompleteArrayType(Param)->getElementType(),
572 IncompleteArrayArg->getElementType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000573 Info, Deduced, 0);
Anders Carlsson35533d12009-06-04 04:11:30 +0000574 }
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000575
576 // T [integer-constant]
Anders Carlsson35533d12009-06-04 04:11:30 +0000577 case Type::ConstantArray: {
Mike Stump11289f42009-09-09 15:08:12 +0000578 const ConstantArrayType *ConstantArrayArg =
Anders Carlsson35533d12009-06-04 04:11:30 +0000579 Context.getAsConstantArrayType(Arg);
580 if (!ConstantArrayArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000581 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000582
583 const ConstantArrayType *ConstantArrayParm =
Anders Carlsson35533d12009-06-04 04:11:30 +0000584 Context.getAsConstantArrayType(Param);
585 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000586 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000587
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000588 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson35533d12009-06-04 04:11:30 +0000589 ConstantArrayParm->getElementType(),
590 ConstantArrayArg->getElementType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000591 Info, Deduced, 0);
Anders Carlsson35533d12009-06-04 04:11:30 +0000592 }
593
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000594 // type [i]
595 case Type::DependentSizedArray: {
596 const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg);
597 if (!ArrayArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000598 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000599
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000600 // Check the element type of the arrays
601 const DependentSizedArrayType *DependentArrayParm
602 = cast<DependentSizedArrayType>(Param);
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000603 if (Sema::TemplateDeductionResult Result
604 = DeduceTemplateArguments(Context, TemplateParams,
605 DependentArrayParm->getElementType(),
606 ArrayArg->getElementType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000607 Info, Deduced, 0))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000608 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000609
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000610 // Determine the array bound is something we can deduce.
Mike Stump11289f42009-09-09 15:08:12 +0000611 NonTypeTemplateParmDecl *NTTP
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000612 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
613 if (!NTTP)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000614 return Sema::TDK_Success;
Mike Stump11289f42009-09-09 15:08:12 +0000615
616 // We can perform template argument deduction for the given non-type
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000617 // template parameter.
Mike Stump11289f42009-09-09 15:08:12 +0000618 assert(NTTP->getDepth() == 0 &&
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000619 "Cannot deduce non-type template argument at depth > 0");
Mike Stump11289f42009-09-09 15:08:12 +0000620 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson3a106e02009-06-16 22:44:31 +0000621 = dyn_cast<ConstantArrayType>(ArrayArg)) {
622 llvm::APSInt Size(ConstantArrayArg->getSize());
623 return DeduceNonTypeTemplateArgument(Context, NTTP, Size,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000624 Info, Deduced);
Anders Carlsson3a106e02009-06-16 22:44:31 +0000625 }
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000626 if (const DependentSizedArrayType *DependentArrayArg
627 = dyn_cast<DependentSizedArrayType>(ArrayArg))
628 return DeduceNonTypeTemplateArgument(Context, NTTP,
629 DependentArrayArg->getSizeExpr(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000630 Info, Deduced);
Mike Stump11289f42009-09-09 15:08:12 +0000631
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000632 // Incomplete type does not match a dependently-sized array type
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000633 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000634 }
Mike Stump11289f42009-09-09 15:08:12 +0000635
636 // type(*)(T)
637 // T(*)()
638 // T(*)(T)
Anders Carlsson2128ec72009-06-08 15:19:08 +0000639 case Type::FunctionProto: {
Mike Stump11289f42009-09-09 15:08:12 +0000640 const FunctionProtoType *FunctionProtoArg =
Anders Carlsson2128ec72009-06-08 15:19:08 +0000641 dyn_cast<FunctionProtoType>(Arg);
642 if (!FunctionProtoArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000643 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000644
645 const FunctionProtoType *FunctionProtoParam =
Anders Carlsson2128ec72009-06-08 15:19:08 +0000646 cast<FunctionProtoType>(Param);
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000647
Mike Stump11289f42009-09-09 15:08:12 +0000648 if (FunctionProtoParam->getTypeQuals() !=
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000649 FunctionProtoArg->getTypeQuals())
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000650 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000651
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000652 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000653 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000654
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000655 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000656 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson096e6ee2009-06-08 19:22:23 +0000657
Anders Carlsson2128ec72009-06-08 15:19:08 +0000658 // Check return types.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000659 if (Sema::TemplateDeductionResult Result
660 = DeduceTemplateArguments(Context, TemplateParams,
661 FunctionProtoParam->getResultType(),
662 FunctionProtoArg->getResultType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000663 Info, Deduced, 0))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000664 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000665
Anders Carlsson2128ec72009-06-08 15:19:08 +0000666 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
667 // Check argument types.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000668 if (Sema::TemplateDeductionResult Result
669 = DeduceTemplateArguments(Context, TemplateParams,
670 FunctionProtoParam->getArgType(I),
671 FunctionProtoArg->getArgType(I),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000672 Info, Deduced, 0))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000673 return Result;
Anders Carlsson2128ec72009-06-08 15:19:08 +0000674 }
Mike Stump11289f42009-09-09 15:08:12 +0000675
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000676 return Sema::TDK_Success;
Anders Carlsson2128ec72009-06-08 15:19:08 +0000677 }
Mike Stump11289f42009-09-09 15:08:12 +0000678
Douglas Gregor705c9002009-06-26 20:57:09 +0000679 // template-name<T> (where template-name refers to a class template)
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000680 // template-name<i>
Douglas Gregoradee3e32009-11-11 23:06:43 +0000681 // TT<T>
682 // TT<i>
683 // TT<>
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000684 case Type::TemplateSpecialization: {
685 const TemplateSpecializationType *SpecParam
686 = cast<TemplateSpecializationType>(Param);
Mike Stump11289f42009-09-09 15:08:12 +0000687
Douglas Gregore81f3e72009-07-07 23:09:34 +0000688 // Try to deduce template arguments from the template-id.
689 Sema::TemplateDeductionResult Result
Mike Stump11289f42009-09-09 15:08:12 +0000690 = DeduceTemplateArguments(Context, TemplateParams, SpecParam, Arg,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000691 Info, Deduced);
Mike Stump11289f42009-09-09 15:08:12 +0000692
Douglas Gregor42909752009-09-30 22:13:51 +0000693 if (Result && (TDF & TDF_DerivedClass)) {
Douglas Gregore81f3e72009-07-07 23:09:34 +0000694 // C++ [temp.deduct.call]p3b3:
695 // If P is a class, and P has the form template-id, then A can be a
696 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump11289f42009-09-09 15:08:12 +0000697 // class of the form template-id, A can be a pointer to a derived
Douglas Gregore81f3e72009-07-07 23:09:34 +0000698 // class pointed to by the deduced A.
699 //
700 // More importantly:
Mike Stump11289f42009-09-09 15:08:12 +0000701 // These alternatives are considered only if type deduction would
Douglas Gregore81f3e72009-07-07 23:09:34 +0000702 // otherwise fail.
703 if (const RecordType *RecordT = dyn_cast<RecordType>(Arg)) {
704 // Use data recursion to crawl through the list of base classes.
Mike Stump11289f42009-09-09 15:08:12 +0000705 // Visited contains the set of nodes we have already visited, while
Douglas Gregore81f3e72009-07-07 23:09:34 +0000706 // ToVisit is our stack of records that we still need to visit.
707 llvm::SmallPtrSet<const RecordType *, 8> Visited;
708 llvm::SmallVector<const RecordType *, 8> ToVisit;
709 ToVisit.push_back(RecordT);
710 bool Successful = false;
711 while (!ToVisit.empty()) {
712 // Retrieve the next class in the inheritance hierarchy.
713 const RecordType *NextT = ToVisit.back();
714 ToVisit.pop_back();
Mike Stump11289f42009-09-09 15:08:12 +0000715
Douglas Gregore81f3e72009-07-07 23:09:34 +0000716 // If we have already seen this type, skip it.
717 if (!Visited.insert(NextT))
718 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000719
Douglas Gregore81f3e72009-07-07 23:09:34 +0000720 // If this is a base class, try to perform template argument
721 // deduction from it.
722 if (NextT != RecordT) {
723 Sema::TemplateDeductionResult BaseResult
724 = DeduceTemplateArguments(Context, TemplateParams, SpecParam,
725 QualType(NextT, 0), Info, Deduced);
Mike Stump11289f42009-09-09 15:08:12 +0000726
Douglas Gregore81f3e72009-07-07 23:09:34 +0000727 // If template argument deduction for this base was successful,
728 // note that we had some success.
729 if (BaseResult == Sema::TDK_Success)
730 Successful = true;
Douglas Gregore81f3e72009-07-07 23:09:34 +0000731 }
Mike Stump11289f42009-09-09 15:08:12 +0000732
Douglas Gregore81f3e72009-07-07 23:09:34 +0000733 // Visit base classes
734 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
735 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
736 BaseEnd = Next->bases_end();
Sebastian Redl1054fae2009-10-25 17:03:50 +0000737 Base != BaseEnd; ++Base) {
Mike Stump11289f42009-09-09 15:08:12 +0000738 assert(Base->getType()->isRecordType() &&
Douglas Gregore81f3e72009-07-07 23:09:34 +0000739 "Base class that isn't a record?");
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000740 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregore81f3e72009-07-07 23:09:34 +0000741 }
742 }
Mike Stump11289f42009-09-09 15:08:12 +0000743
Douglas Gregore81f3e72009-07-07 23:09:34 +0000744 if (Successful)
745 return Sema::TDK_Success;
746 }
Mike Stump11289f42009-09-09 15:08:12 +0000747
Douglas Gregore81f3e72009-07-07 23:09:34 +0000748 }
Mike Stump11289f42009-09-09 15:08:12 +0000749
Douglas Gregore81f3e72009-07-07 23:09:34 +0000750 return Result;
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000751 }
752
Douglas Gregor637d9982009-06-10 23:47:09 +0000753 // T type::*
754 // T T::*
755 // T (type::*)()
756 // type (T::*)()
757 // type (type::*)(T)
758 // type (T::*)(T)
759 // T (type::*)(T)
760 // T (T::*)()
761 // T (T::*)(T)
762 case Type::MemberPointer: {
763 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
764 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
765 if (!MemPtrArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000766 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637d9982009-06-10 23:47:09 +0000767
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000768 if (Sema::TemplateDeductionResult Result
769 = DeduceTemplateArguments(Context, TemplateParams,
770 MemPtrParam->getPointeeType(),
771 MemPtrArg->getPointeeType(),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000772 Info, Deduced,
773 TDF & TDF_IgnoreQualifiers))
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000774 return Result;
775
776 return DeduceTemplateArguments(Context, TemplateParams,
777 QualType(MemPtrParam->getClass(), 0),
778 QualType(MemPtrArg->getClass(), 0),
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000779 Info, Deduced, 0);
Douglas Gregor637d9982009-06-10 23:47:09 +0000780 }
781
Anders Carlsson15f1dd12009-06-12 22:56:54 +0000782 // (clang extension)
783 //
Mike Stump11289f42009-09-09 15:08:12 +0000784 // type(^)(T)
785 // T(^)()
786 // T(^)(T)
Anders Carlssona767eee2009-06-12 16:23:10 +0000787 case Type::BlockPointer: {
788 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
789 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump11289f42009-09-09 15:08:12 +0000790
Anders Carlssona767eee2009-06-12 16:23:10 +0000791 if (!BlockPtrArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000792 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000793
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000794 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlssona767eee2009-06-12 16:23:10 +0000795 BlockPtrParam->getPointeeType(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000796 BlockPtrArg->getPointeeType(), Info,
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000797 Deduced, 0);
Anders Carlssona767eee2009-06-12 16:23:10 +0000798 }
799
Douglas Gregor637d9982009-06-10 23:47:09 +0000800 case Type::TypeOfExpr:
801 case Type::TypeOf:
802 case Type::Typename:
803 // No template argument deduction for these types
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000804 return Sema::TDK_Success;
Douglas Gregor637d9982009-06-10 23:47:09 +0000805
Douglas Gregor5cdac0a2009-06-04 00:21:18 +0000806 default:
807 break;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000808 }
809
810 // FIXME: Many more cases to go (to go).
Douglas Gregor705c9002009-06-26 20:57:09 +0000811 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000812}
813
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000814static Sema::TemplateDeductionResult
Mike Stump11289f42009-09-09 15:08:12 +0000815DeduceTemplateArguments(ASTContext &Context,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000816 TemplateParameterList *TemplateParams,
817 const TemplateArgument &Param,
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000818 const TemplateArgument &Arg,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000819 Sema::TemplateDeductionInfo &Info,
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000820 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000821 switch (Param.getKind()) {
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000822 case TemplateArgument::Null:
823 assert(false && "Null template argument in parameter list");
824 break;
Mike Stump11289f42009-09-09 15:08:12 +0000825
826 case TemplateArgument::Type:
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000827 if (Arg.getKind() == TemplateArgument::Type)
828 return DeduceTemplateArguments(Context, TemplateParams, Param.getAsType(),
829 Arg.getAsType(), Info, Deduced, 0);
830 Info.FirstArg = Param;
831 Info.SecondArg = Arg;
832 return Sema::TDK_NonDeducedMismatch;
833
834 case TemplateArgument::Template:
Douglas Gregoradee3e32009-11-11 23:06:43 +0000835 if (Arg.getKind() == TemplateArgument::Template)
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000836 return DeduceTemplateArguments(Context, TemplateParams,
837 Param.getAsTemplate(),
Douglas Gregoradee3e32009-11-11 23:06:43 +0000838 Arg.getAsTemplate(), Info, Deduced);
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000839 Info.FirstArg = Param;
840 Info.SecondArg = Arg;
841 return Sema::TDK_NonDeducedMismatch;
842
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000843 case TemplateArgument::Declaration:
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000844 if (Arg.getKind() == TemplateArgument::Declaration &&
845 Param.getAsDecl()->getCanonicalDecl() ==
846 Arg.getAsDecl()->getCanonicalDecl())
847 return Sema::TDK_Success;
848
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000849 Info.FirstArg = Param;
850 Info.SecondArg = Arg;
851 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000852
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000853 case TemplateArgument::Integral:
854 if (Arg.getKind() == TemplateArgument::Integral) {
855 // FIXME: Zero extension + sign checking here?
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000856 if (*Param.getAsIntegral() == *Arg.getAsIntegral())
857 return Sema::TDK_Success;
858
859 Info.FirstArg = Param;
860 Info.SecondArg = Arg;
861 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000862 }
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000863
864 if (Arg.getKind() == TemplateArgument::Expression) {
865 Info.FirstArg = Param;
866 Info.SecondArg = Arg;
867 return Sema::TDK_NonDeducedMismatch;
868 }
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000869
870 assert(false && "Type/value mismatch");
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000871 Info.FirstArg = Param;
872 Info.SecondArg = Arg;
873 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +0000874
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000875 case TemplateArgument::Expression: {
Mike Stump11289f42009-09-09 15:08:12 +0000876 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000877 = getDeducedParameterFromExpr(Param.getAsExpr())) {
878 if (Arg.getKind() == TemplateArgument::Integral)
879 // FIXME: Sign problems here
Mike Stump11289f42009-09-09 15:08:12 +0000880 return DeduceNonTypeTemplateArgument(Context, NTTP,
881 *Arg.getAsIntegral(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000882 Info, Deduced);
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000883 if (Arg.getKind() == TemplateArgument::Expression)
884 return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000885 Info, Deduced);
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000886 if (Arg.getKind() == TemplateArgument::Declaration)
887 return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsDecl(),
888 Info, Deduced);
889
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000890 assert(false && "Type/value mismatch");
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000891 Info.FirstArg = Param;
892 Info.SecondArg = Arg;
893 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000894 }
Mike Stump11289f42009-09-09 15:08:12 +0000895
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000896 // Can't deduce anything, but that's okay.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000897 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000898 }
Anders Carlssonbc343912009-06-15 17:04:53 +0000899 case TemplateArgument::Pack:
900 assert(0 && "FIXME: Implement!");
901 break;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000902 }
Mike Stump11289f42009-09-09 15:08:12 +0000903
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000904 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000905}
906
Mike Stump11289f42009-09-09 15:08:12 +0000907static Sema::TemplateDeductionResult
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000908DeduceTemplateArguments(ASTContext &Context,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000909 TemplateParameterList *TemplateParams,
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000910 const TemplateArgumentList &ParamList,
911 const TemplateArgumentList &ArgList,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000912 Sema::TemplateDeductionInfo &Info,
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000913 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
914 assert(ParamList.size() == ArgList.size());
915 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000916 if (Sema::TemplateDeductionResult Result
917 = DeduceTemplateArguments(Context, TemplateParams,
Mike Stump11289f42009-09-09 15:08:12 +0000918 ParamList[I], ArgList[I],
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000919 Info, Deduced))
920 return Result;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000921 }
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000922 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000923}
924
Douglas Gregor705c9002009-06-26 20:57:09 +0000925/// \brief Determine whether two template arguments are the same.
Mike Stump11289f42009-09-09 15:08:12 +0000926static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregor705c9002009-06-26 20:57:09 +0000927 const TemplateArgument &X,
928 const TemplateArgument &Y) {
929 if (X.getKind() != Y.getKind())
930 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000931
Douglas Gregor705c9002009-06-26 20:57:09 +0000932 switch (X.getKind()) {
933 case TemplateArgument::Null:
934 assert(false && "Comparing NULL template argument");
935 break;
Mike Stump11289f42009-09-09 15:08:12 +0000936
Douglas Gregor705c9002009-06-26 20:57:09 +0000937 case TemplateArgument::Type:
938 return Context.getCanonicalType(X.getAsType()) ==
939 Context.getCanonicalType(Y.getAsType());
Mike Stump11289f42009-09-09 15:08:12 +0000940
Douglas Gregor705c9002009-06-26 20:57:09 +0000941 case TemplateArgument::Declaration:
Argyrios Kyrtzidis6b7e3762009-07-18 00:34:25 +0000942 return X.getAsDecl()->getCanonicalDecl() ==
943 Y.getAsDecl()->getCanonicalDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000944
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000945 case TemplateArgument::Template:
946 return Context.getCanonicalTemplateName(X.getAsTemplate())
947 .getAsVoidPointer() ==
948 Context.getCanonicalTemplateName(Y.getAsTemplate())
949 .getAsVoidPointer();
950
Douglas Gregor705c9002009-06-26 20:57:09 +0000951 case TemplateArgument::Integral:
952 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump11289f42009-09-09 15:08:12 +0000953
Douglas Gregor9167f8b2009-11-11 01:00:40 +0000954 case TemplateArgument::Expression: {
955 llvm::FoldingSetNodeID XID, YID;
956 X.getAsExpr()->Profile(XID, Context, true);
957 Y.getAsExpr()->Profile(YID, Context, true);
958 return XID == YID;
959 }
Mike Stump11289f42009-09-09 15:08:12 +0000960
Douglas Gregor705c9002009-06-26 20:57:09 +0000961 case TemplateArgument::Pack:
962 if (X.pack_size() != Y.pack_size())
963 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000964
965 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
966 XPEnd = X.pack_end(),
Douglas Gregor705c9002009-06-26 20:57:09 +0000967 YP = Y.pack_begin();
Mike Stump11289f42009-09-09 15:08:12 +0000968 XP != XPEnd; ++XP, ++YP)
Douglas Gregor705c9002009-06-26 20:57:09 +0000969 if (!isSameTemplateArg(Context, *XP, *YP))
970 return false;
971
972 return true;
973 }
974
975 return false;
976}
977
978/// \brief Helper function to build a TemplateParameter when we don't
979/// know its type statically.
980static TemplateParameter makeTemplateParameter(Decl *D) {
981 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
982 return TemplateParameter(TTP);
983 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
984 return TemplateParameter(NTTP);
Mike Stump11289f42009-09-09 15:08:12 +0000985
Douglas Gregor705c9002009-06-26 20:57:09 +0000986 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
987}
988
Douglas Gregor170bc422009-06-12 22:31:52 +0000989/// \brief Perform template argument deduction to determine whether
990/// the given template arguments match the given class template
991/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000992Sema::TemplateDeductionResult
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000993Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000994 const TemplateArgumentList &TemplateArgs,
995 TemplateDeductionInfo &Info) {
Douglas Gregor170bc422009-06-12 22:31:52 +0000996 // C++ [temp.class.spec.match]p2:
997 // A partial specialization matches a given actual template
998 // argument list if the template arguments of the partial
999 // specialization can be deduced from the actual template argument
1000 // list (14.8.2).
Douglas Gregore1416332009-06-14 08:02:22 +00001001 SFINAETrap Trap(*this);
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001002 llvm::SmallVector<TemplateArgument, 4> Deduced;
1003 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001004 if (TemplateDeductionResult Result
Mike Stump11289f42009-09-09 15:08:12 +00001005 = ::DeduceTemplateArguments(Context,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001006 Partial->getTemplateParameters(),
Mike Stump11289f42009-09-09 15:08:12 +00001007 Partial->getTemplateArgs(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001008 TemplateArgs, Info, Deduced))
1009 return Result;
Douglas Gregor637d9982009-06-10 23:47:09 +00001010
Douglas Gregor637d9982009-06-10 23:47:09 +00001011 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
1012 Deduced.data(), Deduced.size());
1013 if (Inst)
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001014 return TDK_InstantiationDepth;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001015
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001016 // C++ [temp.deduct.type]p2:
1017 // [...] or if any template argument remains neither deduced nor
1018 // explicitly specified, template argument deduction fails.
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001019 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
1020 Deduced.size());
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001021 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001022 if (Deduced[I].isNull()) {
Mike Stump11289f42009-09-09 15:08:12 +00001023 Decl *Param
Douglas Gregorbe999392009-09-15 16:23:51 +00001024 = const_cast<NamedDecl *>(
1025 Partial->getTemplateParameters()->getParam(I));
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001026 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
1027 Info.Param = TTP;
Mike Stump11289f42009-09-09 15:08:12 +00001028 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001029 = dyn_cast<NonTypeTemplateParmDecl>(Param))
1030 Info.Param = NTTP;
1031 else
1032 Info.Param = cast<TemplateTemplateParmDecl>(Param);
1033 return TDK_Incomplete;
1034 }
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001035
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001036 Builder.Append(Deduced[I]);
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001037 }
1038
1039 // Form the template argument list from the deduced template arguments.
Mike Stump11289f42009-09-09 15:08:12 +00001040 TemplateArgumentList *DeducedArgumentList
Anders Carlsson5947ddf2009-06-23 01:26:57 +00001041 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001042 Info.reset(DeducedArgumentList);
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001043
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001044 // Substitute the deduced template arguments into the template
1045 // arguments of the class template partial specialization, and
1046 // verify that the instantiated template arguments are both valid
1047 // and are equivalent to the template arguments originally provided
Mike Stump11289f42009-09-09 15:08:12 +00001048 // to the class template.
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001049 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
John McCall0ad16662009-10-29 08:12:44 +00001050 const TemplateArgumentLoc *PartialTemplateArgs
1051 = Partial->getTemplateArgsAsWritten();
1052 unsigned N = Partial->getNumTemplateArgsAsWritten();
John McCall6b51f282009-11-23 01:53:49 +00001053
1054 // Note that we don't provide the langle and rangle locations.
1055 TemplateArgumentListInfo InstArgs;
1056
John McCall0ad16662009-10-29 08:12:44 +00001057 for (unsigned I = 0; I != N; ++I) {
Douglas Gregorbe999392009-09-15 16:23:51 +00001058 Decl *Param = const_cast<NamedDecl *>(
Douglas Gregor4f024b22009-06-13 00:59:32 +00001059 ClassTemplate->getTemplateParameters()->getParam(I));
John McCall6b51f282009-11-23 01:53:49 +00001060 TemplateArgumentLoc InstArg;
1061 if (Subst(PartialTemplateArgs[I], InstArg,
John McCall0ad16662009-10-29 08:12:44 +00001062 MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
Douglas Gregor705c9002009-06-26 20:57:09 +00001063 Info.Param = makeTemplateParameter(Param);
John McCall0ad16662009-10-29 08:12:44 +00001064 Info.FirstArg = PartialTemplateArgs[I].getArgument();
Mike Stump11289f42009-09-09 15:08:12 +00001065 return TDK_SubstitutionFailure;
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001066 }
John McCall6b51f282009-11-23 01:53:49 +00001067 InstArgs.addArgument(InstArg);
John McCall0ad16662009-10-29 08:12:44 +00001068 }
1069
1070 TemplateArgumentListBuilder ConvertedInstArgs(
1071 ClassTemplate->getTemplateParameters(), N);
1072
1073 if (CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
John McCall6b51f282009-11-23 01:53:49 +00001074 InstArgs, false, ConvertedInstArgs)) {
John McCall0ad16662009-10-29 08:12:44 +00001075 // FIXME: fail with more useful information?
1076 return TDK_SubstitutionFailure;
1077 }
1078
1079 for (unsigned I = 0, E = ConvertedInstArgs.flatSize(); I != E; ++I) {
John McCall6b51f282009-11-23 01:53:49 +00001080 TemplateArgument InstArg = ConvertedInstArgs.getFlatArguments()[I];
John McCall0ad16662009-10-29 08:12:44 +00001081
1082 Decl *Param = const_cast<NamedDecl *>(
1083 ClassTemplate->getTemplateParameters()->getParam(I));
Mike Stump11289f42009-09-09 15:08:12 +00001084
Douglas Gregor705c9002009-06-26 20:57:09 +00001085 if (InstArg.getKind() == TemplateArgument::Expression) {
Mike Stump11289f42009-09-09 15:08:12 +00001086 // When the argument is an expression, check the expression result
Douglas Gregor705c9002009-06-26 20:57:09 +00001087 // against the actual template parameter to get down to the canonical
1088 // template argument.
1089 Expr *InstExpr = InstArg.getAsExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001090 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor705c9002009-06-26 20:57:09 +00001091 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1092 if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
1093 Info.Param = makeTemplateParameter(Param);
John McCall0ad16662009-10-29 08:12:44 +00001094 Info.FirstArg = Partial->getTemplateArgs()[I];
Mike Stump11289f42009-09-09 15:08:12 +00001095 return TDK_SubstitutionFailure;
Douglas Gregor705c9002009-06-26 20:57:09 +00001096 }
Douglas Gregor705c9002009-06-26 20:57:09 +00001097 }
1098 }
Mike Stump11289f42009-09-09 15:08:12 +00001099
Douglas Gregor705c9002009-06-26 20:57:09 +00001100 if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) {
1101 Info.Param = makeTemplateParameter(Param);
1102 Info.FirstArg = TemplateArgs[I];
1103 Info.SecondArg = InstArg;
1104 return TDK_NonDeducedMismatch;
1105 }
Douglas Gregor74eba0b2009-06-11 18:10:32 +00001106 }
1107
Douglas Gregore1416332009-06-14 08:02:22 +00001108 if (Trap.hasErrorOccurred())
1109 return TDK_SubstitutionFailure;
1110
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001111 return TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001112}
Douglas Gregor91772d12009-06-13 00:26:55 +00001113
Douglas Gregorfc516c92009-06-26 23:27:24 +00001114/// \brief Determine whether the given type T is a simple-template-id type.
1115static bool isSimpleTemplateIdType(QualType T) {
Mike Stump11289f42009-09-09 15:08:12 +00001116 if (const TemplateSpecializationType *Spec
John McCall9dd450b2009-09-21 23:43:11 +00001117 = T->getAs<TemplateSpecializationType>())
Douglas Gregorfc516c92009-06-26 23:27:24 +00001118 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump11289f42009-09-09 15:08:12 +00001119
Douglas Gregorfc516c92009-06-26 23:27:24 +00001120 return false;
1121}
Douglas Gregor9b146582009-07-08 20:55:45 +00001122
1123/// \brief Substitute the explicitly-provided template arguments into the
1124/// given function template according to C++ [temp.arg.explicit].
1125///
1126/// \param FunctionTemplate the function template into which the explicit
1127/// template arguments will be substituted.
1128///
Mike Stump11289f42009-09-09 15:08:12 +00001129/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor9b146582009-07-08 20:55:45 +00001130/// arguments.
1131///
Mike Stump11289f42009-09-09 15:08:12 +00001132/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor9b146582009-07-08 20:55:45 +00001133/// with the converted and checked explicit template arguments.
1134///
Mike Stump11289f42009-09-09 15:08:12 +00001135/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor9b146582009-07-08 20:55:45 +00001136/// parameters.
1137///
1138/// \param FunctionType if non-NULL, the result type of the function template
1139/// will also be instantiated and the pointed-to value will be updated with
1140/// the instantiated function type.
1141///
1142/// \param Info if substitution fails for any reason, this object will be
1143/// populated with more information about the failure.
1144///
1145/// \returns TDK_Success if substitution was successful, or some failure
1146/// condition.
1147Sema::TemplateDeductionResult
1148Sema::SubstituteExplicitTemplateArguments(
1149 FunctionTemplateDecl *FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001150 const TemplateArgumentListInfo &ExplicitTemplateArgs,
Douglas Gregor9b146582009-07-08 20:55:45 +00001151 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1152 llvm::SmallVectorImpl<QualType> &ParamTypes,
1153 QualType *FunctionType,
1154 TemplateDeductionInfo &Info) {
1155 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1156 TemplateParameterList *TemplateParams
1157 = FunctionTemplate->getTemplateParameters();
1158
John McCall6b51f282009-11-23 01:53:49 +00001159 if (ExplicitTemplateArgs.size() == 0) {
Douglas Gregor9b146582009-07-08 20:55:45 +00001160 // No arguments to substitute; just copy over the parameter types and
1161 // fill in the function type.
1162 for (FunctionDecl::param_iterator P = Function->param_begin(),
1163 PEnd = Function->param_end();
1164 P != PEnd;
1165 ++P)
1166 ParamTypes.push_back((*P)->getType());
Mike Stump11289f42009-09-09 15:08:12 +00001167
Douglas Gregor9b146582009-07-08 20:55:45 +00001168 if (FunctionType)
1169 *FunctionType = Function->getType();
1170 return TDK_Success;
1171 }
Mike Stump11289f42009-09-09 15:08:12 +00001172
Douglas Gregor9b146582009-07-08 20:55:45 +00001173 // Substitution of the explicit template arguments into a function template
1174 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump11289f42009-09-09 15:08:12 +00001175 SFINAETrap Trap(*this);
1176
Douglas Gregor9b146582009-07-08 20:55:45 +00001177 // C++ [temp.arg.explicit]p3:
Mike Stump11289f42009-09-09 15:08:12 +00001178 // Template arguments that are present shall be specified in the
1179 // declaration order of their corresponding template-parameters. The
Douglas Gregor9b146582009-07-08 20:55:45 +00001180 // template argument list shall not specify more template-arguments than
Mike Stump11289f42009-09-09 15:08:12 +00001181 // there are corresponding template-parameters.
1182 TemplateArgumentListBuilder Builder(TemplateParams,
John McCall6b51f282009-11-23 01:53:49 +00001183 ExplicitTemplateArgs.size());
Mike Stump11289f42009-09-09 15:08:12 +00001184
1185 // Enter a new template instantiation context where we check the
Douglas Gregor9b146582009-07-08 20:55:45 +00001186 // explicitly-specified template arguments against this function template,
1187 // and then substitute them into the function parameter types.
Mike Stump11289f42009-09-09 15:08:12 +00001188 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor9b146582009-07-08 20:55:45 +00001189 FunctionTemplate, Deduced.data(), Deduced.size(),
1190 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution);
1191 if (Inst)
1192 return TDK_InstantiationDepth;
Mike Stump11289f42009-09-09 15:08:12 +00001193
Douglas Gregor9b146582009-07-08 20:55:45 +00001194 if (CheckTemplateArgumentList(FunctionTemplate,
Douglas Gregor9b146582009-07-08 20:55:45 +00001195 SourceLocation(),
John McCall6b51f282009-11-23 01:53:49 +00001196 ExplicitTemplateArgs,
Douglas Gregor9b146582009-07-08 20:55:45 +00001197 true,
1198 Builder) || Trap.hasErrorOccurred())
1199 return TDK_InvalidExplicitArguments;
Mike Stump11289f42009-09-09 15:08:12 +00001200
Douglas Gregor9b146582009-07-08 20:55:45 +00001201 // Form the template argument list from the explicitly-specified
1202 // template arguments.
Mike Stump11289f42009-09-09 15:08:12 +00001203 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor9b146582009-07-08 20:55:45 +00001204 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1205 Info.reset(ExplicitArgumentList);
Mike Stump11289f42009-09-09 15:08:12 +00001206
Douglas Gregor9b146582009-07-08 20:55:45 +00001207 // Instantiate the types of each of the function parameters given the
1208 // explicitly-specified template arguments.
1209 for (FunctionDecl::param_iterator P = Function->param_begin(),
1210 PEnd = Function->param_end();
1211 P != PEnd;
1212 ++P) {
Mike Stump11289f42009-09-09 15:08:12 +00001213 QualType ParamType
1214 = SubstType((*P)->getType(),
Douglas Gregor39cacdb2009-08-28 20:50:45 +00001215 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1216 (*P)->getLocation(), (*P)->getDeclName());
Douglas Gregor9b146582009-07-08 20:55:45 +00001217 if (ParamType.isNull() || Trap.hasErrorOccurred())
1218 return TDK_SubstitutionFailure;
Mike Stump11289f42009-09-09 15:08:12 +00001219
Douglas Gregor9b146582009-07-08 20:55:45 +00001220 ParamTypes.push_back(ParamType);
1221 }
1222
1223 // If the caller wants a full function type back, instantiate the return
1224 // type and form that function type.
1225 if (FunctionType) {
1226 // FIXME: exception-specifications?
Mike Stump11289f42009-09-09 15:08:12 +00001227 const FunctionProtoType *Proto
John McCall9dd450b2009-09-21 23:43:11 +00001228 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor9b146582009-07-08 20:55:45 +00001229 assert(Proto && "Function template does not have a prototype?");
Mike Stump11289f42009-09-09 15:08:12 +00001230
1231 QualType ResultType
Douglas Gregor39cacdb2009-08-28 20:50:45 +00001232 = SubstType(Proto->getResultType(),
1233 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1234 Function->getTypeSpecStartLoc(),
1235 Function->getDeclName());
Douglas Gregor9b146582009-07-08 20:55:45 +00001236 if (ResultType.isNull() || Trap.hasErrorOccurred())
1237 return TDK_SubstitutionFailure;
Mike Stump11289f42009-09-09 15:08:12 +00001238
1239 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor9b146582009-07-08 20:55:45 +00001240 ParamTypes.data(), ParamTypes.size(),
1241 Proto->isVariadic(),
1242 Proto->getTypeQuals(),
1243 Function->getLocation(),
1244 Function->getDeclName());
1245 if (FunctionType->isNull() || Trap.hasErrorOccurred())
1246 return TDK_SubstitutionFailure;
1247 }
Mike Stump11289f42009-09-09 15:08:12 +00001248
Douglas Gregor9b146582009-07-08 20:55:45 +00001249 // C++ [temp.arg.explicit]p2:
Mike Stump11289f42009-09-09 15:08:12 +00001250 // Trailing template arguments that can be deduced (14.8.2) may be
1251 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor9b146582009-07-08 20:55:45 +00001252 // template arguments can be deduced, they may all be omitted; in this
1253 // case, the empty template argument list <> itself may also be omitted.
1254 //
1255 // Take all of the explicitly-specified arguments and put them into the
Mike Stump11289f42009-09-09 15:08:12 +00001256 // set of deduced template arguments.
Douglas Gregor9b146582009-07-08 20:55:45 +00001257 Deduced.reserve(TemplateParams->size());
1258 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001259 Deduced.push_back(ExplicitArgumentList->get(I));
1260
Douglas Gregor9b146582009-07-08 20:55:45 +00001261 return TDK_Success;
1262}
1263
Mike Stump11289f42009-09-09 15:08:12 +00001264/// \brief Finish template argument deduction for a function template,
Douglas Gregor9b146582009-07-08 20:55:45 +00001265/// checking the deduced template arguments for completeness and forming
1266/// the function template specialization.
Mike Stump11289f42009-09-09 15:08:12 +00001267Sema::TemplateDeductionResult
Douglas Gregor9b146582009-07-08 20:55:45 +00001268Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
1269 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1270 FunctionDecl *&Specialization,
1271 TemplateDeductionInfo &Info) {
1272 TemplateParameterList *TemplateParams
1273 = FunctionTemplate->getTemplateParameters();
Mike Stump11289f42009-09-09 15:08:12 +00001274
Douglas Gregor9b146582009-07-08 20:55:45 +00001275 // Template argument deduction for function templates in a SFINAE context.
1276 // Trap any errors that might occur.
Mike Stump11289f42009-09-09 15:08:12 +00001277 SFINAETrap Trap(*this);
1278
Douglas Gregor9b146582009-07-08 20:55:45 +00001279 // Enter a new template instantiation context while we instantiate the
1280 // actual function declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001281 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor9b146582009-07-08 20:55:45 +00001282 FunctionTemplate, Deduced.data(), Deduced.size(),
1283 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
1284 if (Inst)
Mike Stump11289f42009-09-09 15:08:12 +00001285 return TDK_InstantiationDepth;
1286
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00001287 // C++ [temp.deduct.type]p2:
1288 // [...] or if any template argument remains neither deduced nor
1289 // explicitly specified, template argument deduction fails.
1290 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
1291 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
1292 if (!Deduced[I].isNull()) {
1293 Builder.Append(Deduced[I]);
1294 continue;
1295 }
1296
1297 // Substitute into the default template argument, if available.
1298 NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I);
1299 TemplateArgumentLoc DefArg
1300 = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
1301 FunctionTemplate->getLocation(),
1302 FunctionTemplate->getSourceRange().getEnd(),
1303 Param,
1304 Builder);
1305
1306 // If there was no default argument, deduction is incomplete.
1307 if (DefArg.getArgument().isNull()) {
1308 Info.Param = makeTemplateParameter(
1309 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1310 return TDK_Incomplete;
1311 }
1312
1313 // Check whether we can actually use the default argument.
1314 if (CheckTemplateArgument(Param, DefArg,
1315 FunctionTemplate,
1316 FunctionTemplate->getLocation(),
1317 FunctionTemplate->getSourceRange().getEnd(),
1318 Builder)) {
1319 Info.Param = makeTemplateParameter(
1320 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1321 return TDK_SubstitutionFailure;
1322 }
1323
1324 // If we get here, we successfully used the default template argument.
1325 }
1326
1327 // Form the template argument list from the deduced template arguments.
1328 TemplateArgumentList *DeducedArgumentList
1329 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1330 Info.reset(DeducedArgumentList);
1331
Mike Stump11289f42009-09-09 15:08:12 +00001332 // Substitute the deduced template arguments into the function template
Douglas Gregor9b146582009-07-08 20:55:45 +00001333 // declaration to produce the function template specialization.
1334 Specialization = cast_or_null<FunctionDecl>(
John McCall76d824f2009-08-25 22:02:44 +00001335 SubstDecl(FunctionTemplate->getTemplatedDecl(),
1336 FunctionTemplate->getDeclContext(),
Douglas Gregor39cacdb2009-08-28 20:50:45 +00001337 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor9b146582009-07-08 20:55:45 +00001338 if (!Specialization)
1339 return TDK_SubstitutionFailure;
Mike Stump11289f42009-09-09 15:08:12 +00001340
Douglas Gregor31fae892009-09-15 18:26:13 +00001341 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
1342 FunctionTemplate->getCanonicalDecl());
1343
Mike Stump11289f42009-09-09 15:08:12 +00001344 // If the template argument list is owned by the function template
Douglas Gregor9b146582009-07-08 20:55:45 +00001345 // specialization, release it.
1346 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList)
1347 Info.take();
Mike Stump11289f42009-09-09 15:08:12 +00001348
Douglas Gregor9b146582009-07-08 20:55:45 +00001349 // There may have been an error that did not prevent us from constructing a
1350 // declaration. Mark the declaration invalid and return with a substitution
1351 // failure.
1352 if (Trap.hasErrorOccurred()) {
1353 Specialization->setInvalidDecl(true);
1354 return TDK_SubstitutionFailure;
1355 }
Mike Stump11289f42009-09-09 15:08:12 +00001356
1357 return TDK_Success;
Douglas Gregor9b146582009-07-08 20:55:45 +00001358}
1359
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001360/// \brief Perform template argument deduction from a function call
1361/// (C++ [temp.deduct.call]).
1362///
1363/// \param FunctionTemplate the function template for which we are performing
1364/// template argument deduction.
1365///
Mike Stump11289f42009-09-09 15:08:12 +00001366/// \param HasExplicitTemplateArgs whether any template arguments were
Douglas Gregor89026b52009-06-30 23:57:56 +00001367/// explicitly specified.
1368///
1369/// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
1370/// the explicitly-specified template arguments.
1371///
1372/// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
Mike Stump11289f42009-09-09 15:08:12 +00001373/// the number of explicitly-specified template arguments in
Douglas Gregor89026b52009-06-30 23:57:56 +00001374/// @p ExplicitTemplateArguments. This value may be zero.
1375///
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001376/// \param Args the function call arguments
1377///
1378/// \param NumArgs the number of arguments in Args
1379///
1380/// \param Specialization if template argument deduction was successful,
Mike Stump11289f42009-09-09 15:08:12 +00001381/// this will be set to the function template specialization produced by
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001382/// template argument deduction.
1383///
1384/// \param Info the argument will be updated to provide additional information
1385/// about template argument deduction.
1386///
1387/// \returns the result of template argument deduction.
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001388Sema::TemplateDeductionResult
1389Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001390 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001391 Expr **Args, unsigned NumArgs,
1392 FunctionDecl *&Specialization,
1393 TemplateDeductionInfo &Info) {
1394 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor89026b52009-06-30 23:57:56 +00001395
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001396 // C++ [temp.deduct.call]p1:
1397 // Template argument deduction is done by comparing each function template
1398 // parameter type (call it P) with the type of the corresponding argument
1399 // of the call (call it A) as described below.
1400 unsigned CheckArgs = NumArgs;
Douglas Gregor89026b52009-06-30 23:57:56 +00001401 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001402 return TDK_TooFewArguments;
1403 else if (NumArgs > Function->getNumParams()) {
Mike Stump11289f42009-09-09 15:08:12 +00001404 const FunctionProtoType *Proto
John McCall9dd450b2009-09-21 23:43:11 +00001405 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001406 if (!Proto->isVariadic())
1407 return TDK_TooManyArguments;
Mike Stump11289f42009-09-09 15:08:12 +00001408
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001409 CheckArgs = Function->getNumParams();
1410 }
Mike Stump11289f42009-09-09 15:08:12 +00001411
Douglas Gregor89026b52009-06-30 23:57:56 +00001412 // The types of the parameters from which we will perform template argument
1413 // deduction.
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001414 TemplateParameterList *TemplateParams
1415 = FunctionTemplate->getTemplateParameters();
Douglas Gregor89026b52009-06-30 23:57:56 +00001416 llvm::SmallVector<TemplateArgument, 4> Deduced;
1417 llvm::SmallVector<QualType, 4> ParamTypes;
John McCall6b51f282009-11-23 01:53:49 +00001418 if (ExplicitTemplateArgs) {
Douglas Gregor9b146582009-07-08 20:55:45 +00001419 TemplateDeductionResult Result =
1420 SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001421 *ExplicitTemplateArgs,
Douglas Gregor9b146582009-07-08 20:55:45 +00001422 Deduced,
1423 ParamTypes,
1424 0,
1425 Info);
1426 if (Result)
1427 return Result;
Douglas Gregor89026b52009-06-30 23:57:56 +00001428 } else {
1429 // Just fill in the parameter types from the function declaration.
1430 for (unsigned I = 0; I != CheckArgs; ++I)
1431 ParamTypes.push_back(Function->getParamDecl(I)->getType());
1432 }
Mike Stump11289f42009-09-09 15:08:12 +00001433
Douglas Gregor89026b52009-06-30 23:57:56 +00001434 // Deduce template arguments from the function parameters.
Mike Stump11289f42009-09-09 15:08:12 +00001435 Deduced.resize(TemplateParams->size());
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001436 for (unsigned I = 0; I != CheckArgs; ++I) {
Douglas Gregor89026b52009-06-30 23:57:56 +00001437 QualType ParamType = ParamTypes[I];
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001438 QualType ArgType = Args[I]->getType();
Mike Stump11289f42009-09-09 15:08:12 +00001439
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001440 // C++ [temp.deduct.call]p2:
1441 // If P is not a reference type:
1442 QualType CanonParamType = Context.getCanonicalType(ParamType);
Douglas Gregorcceb9752009-06-26 18:27:22 +00001443 bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1444 if (!ParamWasReference) {
Mike Stump11289f42009-09-09 15:08:12 +00001445 // - If A is an array type, the pointer type produced by the
1446 // array-to-pointer standard conversion (4.2) is used in place of
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001447 // A for type deduction; otherwise,
1448 if (ArgType->isArrayType())
1449 ArgType = Context.getArrayDecayedType(ArgType);
Mike Stump11289f42009-09-09 15:08:12 +00001450 // - If A is a function type, the pointer type produced by the
1451 // function-to-pointer standard conversion (4.3) is used in place
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001452 // of A for type deduction; otherwise,
1453 else if (ArgType->isFunctionType())
1454 ArgType = Context.getPointerType(ArgType);
1455 else {
1456 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1457 // type are ignored for type deduction.
1458 QualType CanonArgType = Context.getCanonicalType(ArgType);
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001459 if (CanonArgType.getLocalCVRQualifiers())
1460 ArgType = CanonArgType.getLocalUnqualifiedType();
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001461 }
1462 }
Mike Stump11289f42009-09-09 15:08:12 +00001463
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001464 // C++0x [temp.deduct.call]p3:
1465 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
Mike Stump11289f42009-09-09 15:08:12 +00001466 // are ignored for type deduction.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001467 if (CanonParamType.getLocalCVRQualifiers())
1468 ParamType = CanonParamType.getLocalUnqualifiedType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001469 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Mike Stump11289f42009-09-09 15:08:12 +00001470 // [...] If P is a reference type, the type referred to by P is used
1471 // for type deduction.
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001472 ParamType = ParamRefType->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +00001473
1474 // [...] If P is of the form T&&, where T is a template parameter, and
1475 // the argument is an lvalue, the type A& is used in place of A for
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001476 // type deduction.
1477 if (isa<RValueReferenceType>(ParamRefType) &&
John McCall9dd450b2009-09-21 23:43:11 +00001478 ParamRefType->getAs<TemplateTypeParmType>() &&
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001479 Args[I]->isLvalue(Context) == Expr::LV_Valid)
1480 ArgType = Context.getLValueReferenceType(ArgType);
1481 }
Mike Stump11289f42009-09-09 15:08:12 +00001482
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001483 // C++0x [temp.deduct.call]p4:
1484 // In general, the deduction process attempts to find template argument
1485 // values that will make the deduced A identical to A (after the type A
1486 // is transformed as described above). [...]
Douglas Gregor406f6342009-09-14 20:00:47 +00001487 unsigned TDF = TDF_SkipNonDependent;
Mike Stump11289f42009-09-09 15:08:12 +00001488
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001489 // - If the original P is a reference type, the deduced A (i.e., the
1490 // type referred to by the reference) can be more cv-qualified than
1491 // the transformed A.
1492 if (ParamWasReference)
1493 TDF |= TDF_ParamWithReferenceType;
Mike Stump11289f42009-09-09 15:08:12 +00001494 // - The transformed A can be another pointer or pointer to member
1495 // type that can be converted to the deduced A via a qualification
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001496 // conversion (4.4).
1497 if (ArgType->isPointerType() || ArgType->isMemberPointerType())
1498 TDF |= TDF_IgnoreQualifiers;
Mike Stump11289f42009-09-09 15:08:12 +00001499 // - If P is a class and P has the form simple-template-id, then the
Douglas Gregorfc516c92009-06-26 23:27:24 +00001500 // transformed A can be a derived class of the deduced A. Likewise,
1501 // if P is a pointer to a class of the form simple-template-id, the
1502 // transformed A can be a pointer to a derived class pointed to by
1503 // the deduced A.
1504 if (isSimpleTemplateIdType(ParamType) ||
Mike Stump11289f42009-09-09 15:08:12 +00001505 (isa<PointerType>(ParamType) &&
Douglas Gregorfc516c92009-06-26 23:27:24 +00001506 isSimpleTemplateIdType(
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001507 ParamType->getAs<PointerType>()->getPointeeType())))
Douglas Gregorfc516c92009-06-26 23:27:24 +00001508 TDF |= TDF_DerivedClass;
Mike Stump11289f42009-09-09 15:08:12 +00001509
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001510 if (TemplateDeductionResult Result
1511 = ::DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregorcceb9752009-06-26 18:27:22 +00001512 ParamType, ArgType, Info, Deduced,
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001513 TDF))
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001514 return Result;
Mike Stump11289f42009-09-09 15:08:12 +00001515
Douglas Gregor9fc60972009-07-07 23:12:18 +00001516 // FIXME: C++0x [temp.deduct.call] paragraphs 6-9 deal with function
Mike Stump11289f42009-09-09 15:08:12 +00001517 // pointer parameters.
Douglas Gregor05155d82009-08-21 23:19:43 +00001518
1519 // FIXME: we need to check that the deduced A is the same as A,
1520 // modulo the various allowed differences.
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001521 }
Douglas Gregor05155d82009-08-21 23:19:43 +00001522
Mike Stump11289f42009-09-09 15:08:12 +00001523 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor9b146582009-07-08 20:55:45 +00001524 Specialization, Info);
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00001525}
1526
Douglas Gregor9b146582009-07-08 20:55:45 +00001527/// \brief Deduce template arguments when taking the address of a function
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001528/// template (C++ [temp.deduct.funcaddr]) or matching a
Douglas Gregor9b146582009-07-08 20:55:45 +00001529///
1530/// \param FunctionTemplate the function template for which we are performing
1531/// template argument deduction.
1532///
Mike Stump11289f42009-09-09 15:08:12 +00001533/// \param HasExplicitTemplateArgs whether any template arguments were
Douglas Gregor9b146582009-07-08 20:55:45 +00001534/// explicitly specified.
1535///
1536/// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
1537/// the explicitly-specified template arguments.
1538///
1539/// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
Mike Stump11289f42009-09-09 15:08:12 +00001540/// the number of explicitly-specified template arguments in
Douglas Gregor9b146582009-07-08 20:55:45 +00001541/// @p ExplicitTemplateArguments. This value may be zero.
1542///
1543/// \param ArgFunctionType the function type that will be used as the
1544/// "argument" type (A) when performing template argument deduction from the
1545/// function template's function type.
1546///
1547/// \param Specialization if template argument deduction was successful,
Mike Stump11289f42009-09-09 15:08:12 +00001548/// this will be set to the function template specialization produced by
Douglas Gregor9b146582009-07-08 20:55:45 +00001549/// template argument deduction.
1550///
1551/// \param Info the argument will be updated to provide additional information
1552/// about template argument deduction.
1553///
1554/// \returns the result of template argument deduction.
1555Sema::TemplateDeductionResult
1556Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001557 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor9b146582009-07-08 20:55:45 +00001558 QualType ArgFunctionType,
1559 FunctionDecl *&Specialization,
1560 TemplateDeductionInfo &Info) {
1561 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1562 TemplateParameterList *TemplateParams
1563 = FunctionTemplate->getTemplateParameters();
1564 QualType FunctionType = Function->getType();
Mike Stump11289f42009-09-09 15:08:12 +00001565
Douglas Gregor9b146582009-07-08 20:55:45 +00001566 // Substitute any explicit template arguments.
1567 llvm::SmallVector<TemplateArgument, 4> Deduced;
1568 llvm::SmallVector<QualType, 4> ParamTypes;
John McCall6b51f282009-11-23 01:53:49 +00001569 if (ExplicitTemplateArgs) {
Mike Stump11289f42009-09-09 15:08:12 +00001570 if (TemplateDeductionResult Result
1571 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00001572 *ExplicitTemplateArgs,
Mike Stump11289f42009-09-09 15:08:12 +00001573 Deduced, ParamTypes,
Douglas Gregor9b146582009-07-08 20:55:45 +00001574 &FunctionType, Info))
1575 return Result;
1576 }
1577
1578 // Template argument deduction for function templates in a SFINAE context.
1579 // Trap any errors that might occur.
Mike Stump11289f42009-09-09 15:08:12 +00001580 SFINAETrap Trap(*this);
1581
Douglas Gregor9b146582009-07-08 20:55:45 +00001582 // Deduce template arguments from the function type.
Mike Stump11289f42009-09-09 15:08:12 +00001583 Deduced.resize(TemplateParams->size());
Douglas Gregor9b146582009-07-08 20:55:45 +00001584 if (TemplateDeductionResult Result
1585 = ::DeduceTemplateArguments(Context, TemplateParams,
Mike Stump11289f42009-09-09 15:08:12 +00001586 FunctionType, ArgFunctionType, Info,
Douglas Gregor9b146582009-07-08 20:55:45 +00001587 Deduced, 0))
1588 return Result;
Mike Stump11289f42009-09-09 15:08:12 +00001589
1590 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor9b146582009-07-08 20:55:45 +00001591 Specialization, Info);
1592}
1593
Douglas Gregor05155d82009-08-21 23:19:43 +00001594/// \brief Deduce template arguments for a templated conversion
1595/// function (C++ [temp.deduct.conv]) and, if successful, produce a
1596/// conversion function template specialization.
1597Sema::TemplateDeductionResult
1598Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1599 QualType ToType,
1600 CXXConversionDecl *&Specialization,
1601 TemplateDeductionInfo &Info) {
Mike Stump11289f42009-09-09 15:08:12 +00001602 CXXConversionDecl *Conv
Douglas Gregor05155d82009-08-21 23:19:43 +00001603 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
1604 QualType FromType = Conv->getConversionType();
1605
1606 // Canonicalize the types for deduction.
1607 QualType P = Context.getCanonicalType(FromType);
1608 QualType A = Context.getCanonicalType(ToType);
1609
1610 // C++0x [temp.deduct.conv]p3:
1611 // If P is a reference type, the type referred to by P is used for
1612 // type deduction.
1613 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
1614 P = PRef->getPointeeType();
1615
1616 // C++0x [temp.deduct.conv]p3:
1617 // If A is a reference type, the type referred to by A is used
1618 // for type deduction.
1619 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
1620 A = ARef->getPointeeType();
1621 // C++ [temp.deduct.conv]p2:
1622 //
Mike Stump11289f42009-09-09 15:08:12 +00001623 // If A is not a reference type:
Douglas Gregor05155d82009-08-21 23:19:43 +00001624 else {
1625 assert(!A->isReferenceType() && "Reference types were handled above");
1626
1627 // - If P is an array type, the pointer type produced by the
Mike Stump11289f42009-09-09 15:08:12 +00001628 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor05155d82009-08-21 23:19:43 +00001629 // of P for type deduction; otherwise,
1630 if (P->isArrayType())
1631 P = Context.getArrayDecayedType(P);
1632 // - If P is a function type, the pointer type produced by the
1633 // function-to-pointer standard conversion (4.3) is used in
1634 // place of P for type deduction; otherwise,
1635 else if (P->isFunctionType())
1636 P = Context.getPointerType(P);
1637 // - If P is a cv-qualified type, the top level cv-qualifiers of
1638 // P’s type are ignored for type deduction.
1639 else
1640 P = P.getUnqualifiedType();
1641
1642 // C++0x [temp.deduct.conv]p3:
1643 // If A is a cv-qualified type, the top level cv-qualifiers of A’s
1644 // type are ignored for type deduction.
1645 A = A.getUnqualifiedType();
1646 }
1647
1648 // Template argument deduction for function templates in a SFINAE context.
1649 // Trap any errors that might occur.
Mike Stump11289f42009-09-09 15:08:12 +00001650 SFINAETrap Trap(*this);
Douglas Gregor05155d82009-08-21 23:19:43 +00001651
1652 // C++ [temp.deduct.conv]p1:
1653 // Template argument deduction is done by comparing the return
1654 // type of the template conversion function (call it P) with the
1655 // type that is required as the result of the conversion (call it
1656 // A) as described in 14.8.2.4.
1657 TemplateParameterList *TemplateParams
1658 = FunctionTemplate->getTemplateParameters();
1659 llvm::SmallVector<TemplateArgument, 4> Deduced;
Mike Stump11289f42009-09-09 15:08:12 +00001660 Deduced.resize(TemplateParams->size());
Douglas Gregor05155d82009-08-21 23:19:43 +00001661
1662 // C++0x [temp.deduct.conv]p4:
1663 // In general, the deduction process attempts to find template
1664 // argument values that will make the deduced A identical to
1665 // A. However, there are two cases that allow a difference:
1666 unsigned TDF = 0;
1667 // - If the original A is a reference type, A can be more
1668 // cv-qualified than the deduced A (i.e., the type referred to
1669 // by the reference)
1670 if (ToType->isReferenceType())
1671 TDF |= TDF_ParamWithReferenceType;
1672 // - The deduced A can be another pointer or pointer to member
1673 // type that can be converted to A via a qualification
1674 // conversion.
1675 //
1676 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
1677 // both P and A are pointers or member pointers. In this case, we
1678 // just ignore cv-qualifiers completely).
1679 if ((P->isPointerType() && A->isPointerType()) ||
1680 (P->isMemberPointerType() && P->isMemberPointerType()))
1681 TDF |= TDF_IgnoreQualifiers;
1682 if (TemplateDeductionResult Result
1683 = ::DeduceTemplateArguments(Context, TemplateParams,
1684 P, A, Info, Deduced, TDF))
1685 return Result;
1686
1687 // FIXME: we need to check that the deduced A is the same as A,
1688 // modulo the various allowed differences.
Mike Stump11289f42009-09-09 15:08:12 +00001689
Douglas Gregor05155d82009-08-21 23:19:43 +00001690 // Finish template argument deduction.
1691 FunctionDecl *Spec = 0;
1692 TemplateDeductionResult Result
1693 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, Spec, Info);
1694 Specialization = cast_or_null<CXXConversionDecl>(Spec);
1695 return Result;
1696}
1697
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001698/// \brief Stores the result of comparing the qualifiers of two types.
1699enum DeductionQualifierComparison {
1700 NeitherMoreQualified = 0,
1701 ParamMoreQualified,
1702 ArgMoreQualified
1703};
1704
1705/// \brief Deduce the template arguments during partial ordering by comparing
1706/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
1707///
1708/// \param Context the AST context in which this deduction occurs.
1709///
1710/// \param TemplateParams the template parameters that we are deducing
1711///
1712/// \param ParamIn the parameter type
1713///
1714/// \param ArgIn the argument type
1715///
1716/// \param Info information about the template argument deduction itself
1717///
1718/// \param Deduced the deduced template arguments
1719///
1720/// \returns the result of template argument deduction so far. Note that a
1721/// "success" result means that template argument deduction has not yet failed,
1722/// but it may still fail, later, for other reasons.
1723static Sema::TemplateDeductionResult
1724DeduceTemplateArgumentsDuringPartialOrdering(ASTContext &Context,
1725 TemplateParameterList *TemplateParams,
1726 QualType ParamIn, QualType ArgIn,
1727 Sema::TemplateDeductionInfo &Info,
1728 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1729 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1730 CanQualType Param = Context.getCanonicalType(ParamIn);
1731 CanQualType Arg = Context.getCanonicalType(ArgIn);
1732
1733 // C++0x [temp.deduct.partial]p5:
1734 // Before the partial ordering is done, certain transformations are
1735 // performed on the types used for partial ordering:
1736 // - If P is a reference type, P is replaced by the type referred to.
1737 CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
John McCall48f2d582009-10-23 23:03:21 +00001738 if (!ParamRef.isNull())
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001739 Param = ParamRef->getPointeeType();
1740
1741 // - If A is a reference type, A is replaced by the type referred to.
1742 CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
John McCall48f2d582009-10-23 23:03:21 +00001743 if (!ArgRef.isNull())
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001744 Arg = ArgRef->getPointeeType();
1745
John McCall48f2d582009-10-23 23:03:21 +00001746 if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001747 // C++0x [temp.deduct.partial]p6:
1748 // If both P and A were reference types (before being replaced with the
1749 // type referred to above), determine which of the two types (if any) is
1750 // more cv-qualified than the other; otherwise the types are considered to
1751 // be equally cv-qualified for partial ordering purposes. The result of this
1752 // determination will be used below.
1753 //
1754 // We save this information for later, using it only when deduction
1755 // succeeds in both directions.
1756 DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
1757 if (Param.isMoreQualifiedThan(Arg))
1758 QualifierResult = ParamMoreQualified;
1759 else if (Arg.isMoreQualifiedThan(Param))
1760 QualifierResult = ArgMoreQualified;
1761 QualifierComparisons->push_back(QualifierResult);
1762 }
1763
1764 // C++0x [temp.deduct.partial]p7:
1765 // Remove any top-level cv-qualifiers:
1766 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
1767 // version of P.
1768 Param = Param.getUnqualifiedType();
1769 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
1770 // version of A.
1771 Arg = Arg.getUnqualifiedType();
1772
1773 // C++0x [temp.deduct.partial]p8:
1774 // Using the resulting types P and A the deduction is then done as
1775 // described in 14.9.2.5. If deduction succeeds for a given type, the type
1776 // from the argument template is considered to be at least as specialized
1777 // as the type from the parameter template.
1778 return DeduceTemplateArguments(Context, TemplateParams, Param, Arg, Info,
1779 Deduced, TDF_None);
1780}
1781
1782static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00001783MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
1784 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00001785 unsigned Level,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00001786 llvm::SmallVectorImpl<bool> &Deduced);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001787
1788/// \brief Determine whether the function template \p FT1 is at least as
1789/// specialized as \p FT2.
1790static bool isAtLeastAsSpecializedAs(Sema &S,
1791 FunctionTemplateDecl *FT1,
1792 FunctionTemplateDecl *FT2,
1793 TemplatePartialOrderingContext TPOC,
1794 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1795 FunctionDecl *FD1 = FT1->getTemplatedDecl();
1796 FunctionDecl *FD2 = FT2->getTemplatedDecl();
1797 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
1798 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
1799
1800 assert(Proto1 && Proto2 && "Function templates must have prototypes");
1801 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
1802 llvm::SmallVector<TemplateArgument, 4> Deduced;
1803 Deduced.resize(TemplateParams->size());
1804
1805 // C++0x [temp.deduct.partial]p3:
1806 // The types used to determine the ordering depend on the context in which
1807 // the partial ordering is done:
1808 Sema::TemplateDeductionInfo Info(S.Context);
1809 switch (TPOC) {
1810 case TPOC_Call: {
1811 // - In the context of a function call, the function parameter types are
1812 // used.
1813 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
1814 for (unsigned I = 0; I != NumParams; ++I)
1815 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1816 TemplateParams,
1817 Proto2->getArgType(I),
1818 Proto1->getArgType(I),
1819 Info,
1820 Deduced,
1821 QualifierComparisons))
1822 return false;
1823
1824 break;
1825 }
1826
1827 case TPOC_Conversion:
1828 // - In the context of a call to a conversion operator, the return types
1829 // of the conversion function templates are used.
1830 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1831 TemplateParams,
1832 Proto2->getResultType(),
1833 Proto1->getResultType(),
1834 Info,
1835 Deduced,
1836 QualifierComparisons))
1837 return false;
1838 break;
1839
1840 case TPOC_Other:
1841 // - In other contexts (14.6.6.2) the function template’s function type
1842 // is used.
1843 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1844 TemplateParams,
1845 FD2->getType(),
1846 FD1->getType(),
1847 Info,
1848 Deduced,
1849 QualifierComparisons))
1850 return false;
1851 break;
1852 }
1853
1854 // C++0x [temp.deduct.partial]p11:
1855 // In most cases, all template parameters must have values in order for
1856 // deduction to succeed, but for partial ordering purposes a template
1857 // parameter may remain without a value provided it is not used in the
1858 // types being used for partial ordering. [ Note: a template parameter used
1859 // in a non-deduced context is considered used. -end note]
1860 unsigned ArgIdx = 0, NumArgs = Deduced.size();
1861 for (; ArgIdx != NumArgs; ++ArgIdx)
1862 if (Deduced[ArgIdx].isNull())
1863 break;
1864
1865 if (ArgIdx == NumArgs) {
1866 // All template arguments were deduced. FT1 is at least as specialized
1867 // as FT2.
1868 return true;
1869 }
1870
Douglas Gregore1d2ef32009-09-14 21:25:05 +00001871 // Figure out which template parameters were used.
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001872 llvm::SmallVector<bool, 4> UsedParameters;
1873 UsedParameters.resize(TemplateParams->size());
1874 switch (TPOC) {
1875 case TPOC_Call: {
1876 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
1877 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregor21610382009-10-29 00:04:11 +00001878 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
1879 TemplateParams->getDepth(),
Douglas Gregore1d2ef32009-09-14 21:25:05 +00001880 UsedParameters);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001881 break;
1882 }
1883
1884 case TPOC_Conversion:
Douglas Gregor21610382009-10-29 00:04:11 +00001885 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
1886 TemplateParams->getDepth(),
Douglas Gregore1d2ef32009-09-14 21:25:05 +00001887 UsedParameters);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001888 break;
1889
1890 case TPOC_Other:
Douglas Gregor21610382009-10-29 00:04:11 +00001891 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
1892 TemplateParams->getDepth(),
1893 UsedParameters);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001894 break;
1895 }
1896
1897 for (; ArgIdx != NumArgs; ++ArgIdx)
1898 // If this argument had no value deduced but was used in one of the types
1899 // used for partial ordering, then deduction fails.
1900 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
1901 return false;
1902
1903 return true;
1904}
1905
1906
Douglas Gregorbe999392009-09-15 16:23:51 +00001907/// \brief Returns the more specialized function template according
Douglas Gregor05155d82009-08-21 23:19:43 +00001908/// to the rules of function template partial ordering (C++ [temp.func.order]).
1909///
1910/// \param FT1 the first function template
1911///
1912/// \param FT2 the second function template
1913///
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001914/// \param TPOC the context in which we are performing partial ordering of
1915/// function templates.
Mike Stump11289f42009-09-09 15:08:12 +00001916///
Douglas Gregorbe999392009-09-15 16:23:51 +00001917/// \returns the more specialized function template. If neither
Douglas Gregor05155d82009-08-21 23:19:43 +00001918/// template is more specialized, returns NULL.
1919FunctionTemplateDecl *
1920Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
1921 FunctionTemplateDecl *FT2,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001922 TemplatePartialOrderingContext TPOC) {
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001923 llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
1924 bool Better1 = isAtLeastAsSpecializedAs(*this, FT1, FT2, TPOC, 0);
1925 bool Better2 = isAtLeastAsSpecializedAs(*this, FT2, FT1, TPOC,
1926 &QualifierComparisons);
1927
1928 if (Better1 != Better2) // We have a clear winner
1929 return Better1? FT1 : FT2;
1930
1931 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor05155d82009-08-21 23:19:43 +00001932 return 0;
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001933
1934
1935 // C++0x [temp.deduct.partial]p10:
1936 // If for each type being considered a given template is at least as
1937 // specialized for all types and more specialized for some set of types and
1938 // the other template is not more specialized for any types or is not at
1939 // least as specialized for any types, then the given template is more
1940 // specialized than the other template. Otherwise, neither template is more
1941 // specialized than the other.
1942 Better1 = false;
1943 Better2 = false;
1944 for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
1945 // C++0x [temp.deduct.partial]p9:
1946 // If, for a given type, deduction succeeds in both directions (i.e., the
1947 // types are identical after the transformations above) and if the type
1948 // from the argument template is more cv-qualified than the type from the
1949 // parameter template (as described above) that type is considered to be
1950 // more specialized than the other. If neither type is more cv-qualified
1951 // than the other then neither type is more specialized than the other.
1952 switch (QualifierComparisons[I]) {
1953 case NeitherMoreQualified:
1954 break;
1955
1956 case ParamMoreQualified:
1957 Better1 = true;
1958 if (Better2)
1959 return 0;
1960 break;
1961
1962 case ArgMoreQualified:
1963 Better2 = true;
1964 if (Better1)
1965 return 0;
1966 break;
1967 }
1968 }
1969
1970 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor05155d82009-08-21 23:19:43 +00001971 if (Better1)
1972 return FT1;
Douglas Gregor0ff7d922009-09-14 18:39:43 +00001973 else if (Better2)
1974 return FT2;
1975 else
1976 return 0;
Douglas Gregor05155d82009-08-21 23:19:43 +00001977}
Douglas Gregor9b146582009-07-08 20:55:45 +00001978
Douglas Gregor450f00842009-09-25 18:43:00 +00001979/// \brief Determine if the two templates are equivalent.
1980static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
1981 if (T1 == T2)
1982 return true;
1983
1984 if (!T1 || !T2)
1985 return false;
1986
1987 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
1988}
1989
1990/// \brief Retrieve the most specialized of the given function template
1991/// specializations.
1992///
1993/// \param Specializations the set of function template specializations that
1994/// we will be comparing.
1995///
1996/// \param NumSpecializations the number of function template specializations in
1997/// \p Specializations
1998///
1999/// \param TPOC the partial ordering context to use to compare the function
2000/// template specializations.
2001///
2002/// \param Loc the location where the ambiguity or no-specializations
2003/// diagnostic should occur.
2004///
2005/// \param NoneDiag partial diagnostic used to diagnose cases where there are
2006/// no matching candidates.
2007///
2008/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
2009/// occurs.
2010///
2011/// \param CandidateDiag partial diagnostic used for each function template
2012/// specialization that is a candidate in the ambiguous ordering. One parameter
2013/// in this diagnostic should be unbound, which will correspond to the string
2014/// describing the template arguments for the function template specialization.
2015///
2016/// \param Index if non-NULL and the result of this function is non-nULL,
2017/// receives the index corresponding to the resulting function template
2018/// specialization.
2019///
2020/// \returns the most specialized function template specialization, if
2021/// found. Otherwise, returns NULL.
2022///
2023/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
2024/// template argument deduction.
2025FunctionDecl *Sema::getMostSpecialized(FunctionDecl **Specializations,
2026 unsigned NumSpecializations,
2027 TemplatePartialOrderingContext TPOC,
2028 SourceLocation Loc,
2029 const PartialDiagnostic &NoneDiag,
2030 const PartialDiagnostic &AmbigDiag,
2031 const PartialDiagnostic &CandidateDiag,
2032 unsigned *Index) {
2033 if (NumSpecializations == 0) {
2034 Diag(Loc, NoneDiag);
2035 return 0;
2036 }
2037
2038 if (NumSpecializations == 1) {
2039 if (Index)
2040 *Index = 0;
2041
2042 return Specializations[0];
2043 }
2044
2045
2046 // Find the function template that is better than all of the templates it
2047 // has been compared to.
2048 unsigned Best = 0;
2049 FunctionTemplateDecl *BestTemplate
2050 = Specializations[Best]->getPrimaryTemplate();
2051 assert(BestTemplate && "Not a function template specialization?");
2052 for (unsigned I = 1; I != NumSpecializations; ++I) {
2053 FunctionTemplateDecl *Challenger = Specializations[I]->getPrimaryTemplate();
2054 assert(Challenger && "Not a function template specialization?");
2055 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
2056 TPOC),
2057 Challenger)) {
2058 Best = I;
2059 BestTemplate = Challenger;
2060 }
2061 }
2062
2063 // Make sure that the "best" function template is more specialized than all
2064 // of the others.
2065 bool Ambiguous = false;
2066 for (unsigned I = 0; I != NumSpecializations; ++I) {
2067 FunctionTemplateDecl *Challenger = Specializations[I]->getPrimaryTemplate();
2068 if (I != Best &&
2069 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
2070 TPOC),
2071 BestTemplate)) {
2072 Ambiguous = true;
2073 break;
2074 }
2075 }
2076
2077 if (!Ambiguous) {
2078 // We found an answer. Return it.
2079 if (Index)
2080 *Index = Best;
2081 return Specializations[Best];
2082 }
2083
2084 // Diagnose the ambiguity.
2085 Diag(Loc, AmbigDiag);
2086
2087 // FIXME: Can we order the candidates in some sane way?
2088 for (unsigned I = 0; I != NumSpecializations; ++I)
2089 Diag(Specializations[I]->getLocation(), CandidateDiag)
2090 << getTemplateArgumentBindingsText(
2091 Specializations[I]->getPrimaryTemplate()->getTemplateParameters(),
2092 *Specializations[I]->getTemplateSpecializationArgs());
2093
2094 return 0;
2095}
2096
Douglas Gregorbe999392009-09-15 16:23:51 +00002097/// \brief Returns the more specialized class template partial specialization
2098/// according to the rules of partial ordering of class template partial
2099/// specializations (C++ [temp.class.order]).
2100///
2101/// \param PS1 the first class template partial specialization
2102///
2103/// \param PS2 the second class template partial specialization
2104///
2105/// \returns the more specialized class template partial specialization. If
2106/// neither partial specialization is more specialized, returns NULL.
2107ClassTemplatePartialSpecializationDecl *
2108Sema::getMoreSpecializedPartialSpecialization(
2109 ClassTemplatePartialSpecializationDecl *PS1,
2110 ClassTemplatePartialSpecializationDecl *PS2) {
2111 // C++ [temp.class.order]p1:
2112 // For two class template partial specializations, the first is at least as
2113 // specialized as the second if, given the following rewrite to two
2114 // function templates, the first function template is at least as
2115 // specialized as the second according to the ordering rules for function
2116 // templates (14.6.6.2):
2117 // - the first function template has the same template parameters as the
2118 // first partial specialization and has a single function parameter
2119 // whose type is a class template specialization with the template
2120 // arguments of the first partial specialization, and
2121 // - the second function template has the same template parameters as the
2122 // second partial specialization and has a single function parameter
2123 // whose type is a class template specialization with the template
2124 // arguments of the second partial specialization.
2125 //
2126 // Rather than synthesize function templates, we merely perform the
2127 // equivalent partial ordering by performing deduction directly on the
2128 // template arguments of the class template partial specializations. This
2129 // computation is slightly simpler than the general problem of function
2130 // template partial ordering, because class template partial specializations
2131 // are more constrained. We know that every template parameter is deduc
2132 llvm::SmallVector<TemplateArgument, 4> Deduced;
2133 Sema::TemplateDeductionInfo Info(Context);
2134
2135 // Determine whether PS1 is at least as specialized as PS2
2136 Deduced.resize(PS2->getTemplateParameters()->size());
2137 bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(Context,
2138 PS2->getTemplateParameters(),
2139 Context.getTypeDeclType(PS2),
2140 Context.getTypeDeclType(PS1),
2141 Info,
2142 Deduced,
2143 0);
2144
2145 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregoradee3e32009-11-11 23:06:43 +00002146 Deduced.clear();
Douglas Gregorbe999392009-09-15 16:23:51 +00002147 Deduced.resize(PS1->getTemplateParameters()->size());
2148 bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(Context,
2149 PS1->getTemplateParameters(),
2150 Context.getTypeDeclType(PS1),
2151 Context.getTypeDeclType(PS2),
2152 Info,
2153 Deduced,
2154 0);
2155
2156 if (Better1 == Better2)
2157 return 0;
2158
2159 return Better1? PS1 : PS2;
2160}
2161
Mike Stump11289f42009-09-09 15:08:12 +00002162static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002163MarkUsedTemplateParameters(Sema &SemaRef,
2164 const TemplateArgument &TemplateArg,
2165 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002166 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002167 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002168
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002169/// \brief Mark the template parameters that are used by the given
Douglas Gregor91772d12009-06-13 00:26:55 +00002170/// expression.
Mike Stump11289f42009-09-09 15:08:12 +00002171static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002172MarkUsedTemplateParameters(Sema &SemaRef,
2173 const Expr *E,
2174 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002175 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002176 llvm::SmallVectorImpl<bool> &Used) {
2177 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2178 // find other occurrences of template parameters.
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00002179 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregor91772d12009-06-13 00:26:55 +00002180 if (!E)
2181 return;
2182
Mike Stump11289f42009-09-09 15:08:12 +00002183 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor91772d12009-06-13 00:26:55 +00002184 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2185 if (!NTTP)
2186 return;
2187
Douglas Gregor21610382009-10-29 00:04:11 +00002188 if (NTTP->getDepth() == Depth)
2189 Used[NTTP->getIndex()] = true;
Douglas Gregor91772d12009-06-13 00:26:55 +00002190}
2191
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002192/// \brief Mark the template parameters that are used by the given
2193/// nested name specifier.
2194static void
2195MarkUsedTemplateParameters(Sema &SemaRef,
2196 NestedNameSpecifier *NNS,
2197 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002198 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002199 llvm::SmallVectorImpl<bool> &Used) {
2200 if (!NNS)
2201 return;
2202
Douglas Gregor21610382009-10-29 00:04:11 +00002203 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2204 Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002205 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregor21610382009-10-29 00:04:11 +00002206 OnlyDeduced, Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002207}
2208
2209/// \brief Mark the template parameters that are used by the given
2210/// template name.
2211static void
2212MarkUsedTemplateParameters(Sema &SemaRef,
2213 TemplateName Name,
2214 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002215 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002216 llvm::SmallVectorImpl<bool> &Used) {
2217 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2218 if (TemplateTemplateParmDecl *TTP
Douglas Gregor21610382009-10-29 00:04:11 +00002219 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2220 if (TTP->getDepth() == Depth)
2221 Used[TTP->getIndex()] = true;
2222 }
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002223 return;
2224 }
2225
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002226 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2227 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2228 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002229 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
Douglas Gregor21610382009-10-29 00:04:11 +00002230 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2231 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002232}
2233
2234/// \brief Mark the template parameters that are used by the given
Douglas Gregor91772d12009-06-13 00:26:55 +00002235/// type.
Mike Stump11289f42009-09-09 15:08:12 +00002236static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002237MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2238 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002239 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002240 llvm::SmallVectorImpl<bool> &Used) {
2241 if (T.isNull())
2242 return;
2243
Douglas Gregor91772d12009-06-13 00:26:55 +00002244 // Non-dependent types have nothing deducible
2245 if (!T->isDependentType())
2246 return;
2247
2248 T = SemaRef.Context.getCanonicalType(T);
2249 switch (T->getTypeClass()) {
Douglas Gregor91772d12009-06-13 00:26:55 +00002250 case Type::Pointer:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002251 MarkUsedTemplateParameters(SemaRef,
2252 cast<PointerType>(T)->getPointeeType(),
2253 OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002254 Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002255 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002256 break;
2257
2258 case Type::BlockPointer:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002259 MarkUsedTemplateParameters(SemaRef,
2260 cast<BlockPointerType>(T)->getPointeeType(),
2261 OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002262 Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002263 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002264 break;
2265
2266 case Type::LValueReference:
2267 case Type::RValueReference:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002268 MarkUsedTemplateParameters(SemaRef,
2269 cast<ReferenceType>(T)->getPointeeType(),
2270 OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002271 Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002272 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002273 break;
2274
2275 case Type::MemberPointer: {
2276 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002277 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002278 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002279 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregor21610382009-10-29 00:04:11 +00002280 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002281 break;
2282 }
2283
2284 case Type::DependentSizedArray:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002285 MarkUsedTemplateParameters(SemaRef,
2286 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregor21610382009-10-29 00:04:11 +00002287 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002288 // Fall through to check the element type
2289
2290 case Type::ConstantArray:
2291 case Type::IncompleteArray:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002292 MarkUsedTemplateParameters(SemaRef,
2293 cast<ArrayType>(T)->getElementType(),
Douglas Gregor21610382009-10-29 00:04:11 +00002294 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002295 break;
2296
2297 case Type::Vector:
2298 case Type::ExtVector:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002299 MarkUsedTemplateParameters(SemaRef,
2300 cast<VectorType>(T)->getElementType(),
Douglas Gregor21610382009-10-29 00:04:11 +00002301 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002302 break;
2303
Douglas Gregor758a8692009-06-17 21:51:59 +00002304 case Type::DependentSizedExtVector: {
2305 const DependentSizedExtVectorType *VecType
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00002306 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002307 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002308 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002309 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002310 Depth, Used);
Douglas Gregor758a8692009-06-17 21:51:59 +00002311 break;
2312 }
2313
Douglas Gregor91772d12009-06-13 00:26:55 +00002314 case Type::FunctionProto: {
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00002315 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002316 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002317 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002318 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002319 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002320 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002321 break;
2322 }
2323
Douglas Gregor21610382009-10-29 00:04:11 +00002324 case Type::TemplateTypeParm: {
2325 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2326 if (TTP->getDepth() == Depth)
2327 Used[TTP->getIndex()] = true;
Douglas Gregor91772d12009-06-13 00:26:55 +00002328 break;
Douglas Gregor21610382009-10-29 00:04:11 +00002329 }
Douglas Gregor91772d12009-06-13 00:26:55 +00002330
2331 case Type::TemplateSpecialization: {
Mike Stump11289f42009-09-09 15:08:12 +00002332 const TemplateSpecializationType *Spec
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00002333 = cast<TemplateSpecializationType>(T);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002334 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002335 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002336 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregor21610382009-10-29 00:04:11 +00002337 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2338 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002339 break;
2340 }
2341
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002342 case Type::Complex:
2343 if (!OnlyDeduced)
2344 MarkUsedTemplateParameters(SemaRef,
2345 cast<ComplexType>(T)->getElementType(),
Douglas Gregor21610382009-10-29 00:04:11 +00002346 OnlyDeduced, Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002347 break;
2348
2349 case Type::Typename:
2350 if (!OnlyDeduced)
2351 MarkUsedTemplateParameters(SemaRef,
2352 cast<TypenameType>(T)->getQualifier(),
Douglas Gregor21610382009-10-29 00:04:11 +00002353 OnlyDeduced, Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002354 break;
2355
2356 // None of these types have any template parameters in them.
Douglas Gregor91772d12009-06-13 00:26:55 +00002357 case Type::Builtin:
2358 case Type::FixedWidthInt:
Douglas Gregor91772d12009-06-13 00:26:55 +00002359 case Type::VariableArray:
2360 case Type::FunctionNoProto:
2361 case Type::Record:
2362 case Type::Enum:
Douglas Gregor91772d12009-06-13 00:26:55 +00002363 case Type::ObjCInterface:
Steve Narofffb4330f2009-06-17 22:40:22 +00002364 case Type::ObjCObjectPointer:
Douglas Gregor91772d12009-06-13 00:26:55 +00002365#define TYPE(Class, Base)
2366#define ABSTRACT_TYPE(Class, Base)
2367#define DEPENDENT_TYPE(Class, Base)
2368#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2369#include "clang/AST/TypeNodes.def"
2370 break;
2371 }
2372}
2373
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002374/// \brief Mark the template parameters that are used by this
Douglas Gregor91772d12009-06-13 00:26:55 +00002375/// template argument.
Mike Stump11289f42009-09-09 15:08:12 +00002376static void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002377MarkUsedTemplateParameters(Sema &SemaRef,
2378 const TemplateArgument &TemplateArg,
2379 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002380 unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002381 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor91772d12009-06-13 00:26:55 +00002382 switch (TemplateArg.getKind()) {
2383 case TemplateArgument::Null:
2384 case TemplateArgument::Integral:
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002385 case TemplateArgument::Declaration:
Douglas Gregor91772d12009-06-13 00:26:55 +00002386 break;
Mike Stump11289f42009-09-09 15:08:12 +00002387
Douglas Gregor91772d12009-06-13 00:26:55 +00002388 case TemplateArgument::Type:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002389 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002390 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002391 break;
2392
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002393 case TemplateArgument::Template:
2394 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2395 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002396 break;
2397
2398 case TemplateArgument::Expression:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002399 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00002400 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002401 break;
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002402
Anders Carlssonbc343912009-06-15 17:04:53 +00002403 case TemplateArgument::Pack:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002404 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2405 PEnd = TemplateArg.pack_end();
2406 P != PEnd; ++P)
Douglas Gregor21610382009-10-29 00:04:11 +00002407 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssonbc343912009-06-15 17:04:53 +00002408 break;
Douglas Gregor91772d12009-06-13 00:26:55 +00002409 }
2410}
2411
2412/// \brief Mark the template parameters can be deduced by the given
2413/// template argument list.
2414///
2415/// \param TemplateArgs the template argument list from which template
2416/// parameters will be deduced.
2417///
2418/// \param Deduced a bit vector whose elements will be set to \c true
2419/// to indicate when the corresponding template parameter will be
2420/// deduced.
Mike Stump11289f42009-09-09 15:08:12 +00002421void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002422Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregor21610382009-10-29 00:04:11 +00002423 bool OnlyDeduced, unsigned Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00002424 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor91772d12009-06-13 00:26:55 +00002425 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
Douglas Gregor21610382009-10-29 00:04:11 +00002426 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2427 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00002428}
Douglas Gregorce23bae2009-09-18 23:21:38 +00002429
2430/// \brief Marks all of the template parameters that will be deduced by a
2431/// call to the given function template.
2432void Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
2433 llvm::SmallVectorImpl<bool> &Deduced) {
2434 TemplateParameterList *TemplateParams
2435 = FunctionTemplate->getTemplateParameters();
2436 Deduced.clear();
2437 Deduced.resize(TemplateParams->size());
2438
2439 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2440 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
2441 ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
Douglas Gregor21610382009-10-29 00:04:11 +00002442 true, TemplateParams->getDepth(), Deduced);
Douglas Gregorce23bae2009-09-18 23:21:38 +00002443}