blob: 5434beaef1259a58d386fb6b14684d342217f28d [file] [log] [blame]
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001//===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template argument deduction.
10//
11//===----------------------------------------------------------------------===/
12
13#include "Sema.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/DeclTemplate.h"
16#include "clang/AST/StmtVisitor.h"
17#include "clang/AST/Expr.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/Parse/DeclSpec.h"
Douglas Gregor8a514912009-09-14 18:39:43 +000020#include <algorithm>
Douglas Gregor508f1c82009-06-26 23:10:12 +000021
22namespace clang {
23 /// \brief Various flags that control template argument deduction.
24 ///
25 /// These flags can be bitwise-OR'd together.
26 enum TemplateDeductionFlags {
27 /// \brief No template argument deduction flags, which indicates the
28 /// strictest results for template argument deduction (as used for, e.g.,
29 /// matching class template partial specializations).
30 TDF_None = 0,
31 /// \brief Within template argument deduction from a function call, we are
32 /// matching with a parameter type for which the original parameter was
33 /// a reference.
34 TDF_ParamWithReferenceType = 0x1,
35 /// \brief Within template argument deduction from a function call, we
36 /// are matching in a case where we ignore cv-qualifiers.
37 TDF_IgnoreQualifiers = 0x02,
38 /// \brief Within template argument deduction from a function call,
39 /// we are matching in a case where we can perform template argument
Douglas Gregor41128772009-06-26 23:27:24 +000040 /// deduction from a template-id of a derived class of the argument type.
Douglas Gregor12820292009-09-14 20:00:47 +000041 TDF_DerivedClass = 0x04,
42 /// \brief Allow non-dependent types to differ, e.g., when performing
43 /// template argument deduction from a function call where conversions
44 /// may apply.
45 TDF_SkipNonDependent = 0x08
Douglas Gregor508f1c82009-06-26 23:10:12 +000046 };
47}
48
Douglas Gregor0b9247f2009-06-04 00:03:07 +000049using namespace clang;
50
Douglas Gregorf67875d2009-06-12 18:26:56 +000051static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +000052DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +000053 TemplateParameterList *TemplateParams,
54 const TemplateArgument &Param,
Douglas Gregord708c722009-06-09 16:35:58 +000055 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +000056 Sema::TemplateDeductionInfo &Info,
Douglas Gregord708c722009-06-09 16:35:58 +000057 llvm::SmallVectorImpl<TemplateArgument> &Deduced);
58
Douglas Gregor199d9912009-06-05 00:53:49 +000059/// \brief If the given expression is of a form that permits the deduction
60/// of a non-type template parameter, return the declaration of that
61/// non-type template parameter.
62static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
63 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
64 E = IC->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +000065
Douglas Gregor199d9912009-06-05 00:53:49 +000066 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
67 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +000068
Douglas Gregor199d9912009-06-05 00:53:49 +000069 return 0;
70}
71
Mike Stump1eb44332009-09-09 15:08:12 +000072/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +000073/// from the given constant.
Douglas Gregorf67875d2009-06-12 18:26:56 +000074static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +000075DeduceNonTypeTemplateArgument(ASTContext &Context,
76 NonTypeTemplateParmDecl *NTTP,
Anders Carlsson335e24a2009-06-16 22:44:31 +000077 llvm::APSInt Value,
Douglas Gregorf67875d2009-06-12 18:26:56 +000078 Sema::TemplateDeductionInfo &Info,
79 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +000080 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +000081 "Cannot deduce non-type template argument with depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +000082
Douglas Gregor199d9912009-06-05 00:53:49 +000083 if (Deduced[NTTP->getIndex()].isNull()) {
Anders Carlsson25af1ed2009-06-16 23:08:29 +000084 QualType T = NTTP->getType();
Mike Stump1eb44332009-09-09 15:08:12 +000085
Anders Carlsson25af1ed2009-06-16 23:08:29 +000086 // FIXME: Make sure we didn't overflow our data type!
87 unsigned AllowedBits = Context.getTypeSize(T);
88 if (Value.getBitWidth() != AllowedBits)
89 Value.extOrTrunc(AllowedBits);
90 Value.setIsSigned(T->isSignedIntegerType());
91
John McCall833ca992009-10-29 08:12:44 +000092 Deduced[NTTP->getIndex()] = TemplateArgument(Value, T);
Douglas Gregorf67875d2009-06-12 18:26:56 +000093 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +000094 }
Mike Stump1eb44332009-09-09 15:08:12 +000095
Douglas Gregorf67875d2009-06-12 18:26:56 +000096 assert(Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral);
Mike Stump1eb44332009-09-09 15:08:12 +000097
98 // If the template argument was previously deduced to a negative value,
Douglas Gregor199d9912009-06-05 00:53:49 +000099 // then our deduction fails.
100 const llvm::APSInt *PrevValuePtr = Deduced[NTTP->getIndex()].getAsIntegral();
Anders Carlsson335e24a2009-06-16 22:44:31 +0000101 if (PrevValuePtr->isNegative()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000102 Info.Param = NTTP;
103 Info.FirstArg = Deduced[NTTP->getIndex()];
John McCall833ca992009-10-29 08:12:44 +0000104 Info.SecondArg = TemplateArgument(Value, NTTP->getType());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000105 return Sema::TDK_Inconsistent;
106 }
107
Anders Carlsson335e24a2009-06-16 22:44:31 +0000108 llvm::APSInt PrevValue = *PrevValuePtr;
Douglas Gregor199d9912009-06-05 00:53:49 +0000109 if (Value.getBitWidth() > PrevValue.getBitWidth())
110 PrevValue.zext(Value.getBitWidth());
111 else if (Value.getBitWidth() < PrevValue.getBitWidth())
112 Value.zext(PrevValue.getBitWidth());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000113
114 if (Value != PrevValue) {
115 Info.Param = NTTP;
116 Info.FirstArg = Deduced[NTTP->getIndex()];
John McCall833ca992009-10-29 08:12:44 +0000117 Info.SecondArg = TemplateArgument(Value, NTTP->getType());
Douglas Gregorf67875d2009-06-12 18:26:56 +0000118 return Sema::TDK_Inconsistent;
119 }
120
121 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000122}
123
Mike Stump1eb44332009-09-09 15:08:12 +0000124/// \brief Deduce the value of the given non-type template parameter
Douglas Gregor199d9912009-06-05 00:53:49 +0000125/// from the given type- or value-dependent expression.
126///
127/// \returns true if deduction succeeded, false otherwise.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000128static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +0000129DeduceNonTypeTemplateArgument(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000130 NonTypeTemplateParmDecl *NTTP,
131 Expr *Value,
132 Sema::TemplateDeductionInfo &Info,
133 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Mike Stump1eb44332009-09-09 15:08:12 +0000134 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000135 "Cannot deduce non-type template argument with depth > 0");
136 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
137 "Expression template argument must be type- or value-dependent.");
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Douglas Gregor199d9912009-06-05 00:53:49 +0000139 if (Deduced[NTTP->getIndex()].isNull()) {
140 // FIXME: Clone the Value?
141 Deduced[NTTP->getIndex()] = TemplateArgument(Value);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000142 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000143 }
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Douglas Gregor199d9912009-06-05 00:53:49 +0000145 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
Mike Stump1eb44332009-09-09 15:08:12 +0000146 // Okay, we deduced a constant in one case and a dependent expression
147 // in another case. FIXME: Later, we will check that instantiating the
Douglas Gregor199d9912009-06-05 00:53:49 +0000148 // dependent expression gives us the constant value.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000149 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000150 }
Mike Stump1eb44332009-09-09 15:08:12 +0000151
Douglas Gregor9eea08b2009-09-15 16:51:42 +0000152 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
153 // Compare the expressions for equality
154 llvm::FoldingSetNodeID ID1, ID2;
155 Deduced[NTTP->getIndex()].getAsExpr()->Profile(ID1, Context, true);
156 Value->Profile(ID2, Context, true);
157 if (ID1 == ID2)
158 return Sema::TDK_Success;
159
160 // FIXME: Fill in argument mismatch information
161 return Sema::TDK_NonDeducedMismatch;
162 }
163
Douglas Gregorf67875d2009-06-12 18:26:56 +0000164 return Sema::TDK_Success;
Douglas Gregor199d9912009-06-05 00:53:49 +0000165}
166
Douglas Gregor15755cb2009-11-13 23:45:44 +0000167/// \brief Deduce the value of the given non-type template parameter
168/// from the given declaration.
169///
170/// \returns true if deduction succeeded, false otherwise.
171static Sema::TemplateDeductionResult
172DeduceNonTypeTemplateArgument(ASTContext &Context,
173 NonTypeTemplateParmDecl *NTTP,
174 Decl *D,
175 Sema::TemplateDeductionInfo &Info,
176 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
177 assert(NTTP->getDepth() == 0 &&
178 "Cannot deduce non-type template argument with depth > 0");
179
180 if (Deduced[NTTP->getIndex()].isNull()) {
181 Deduced[NTTP->getIndex()] = TemplateArgument(D->getCanonicalDecl());
182 return Sema::TDK_Success;
183 }
184
185 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
186 // Okay, we deduced a declaration in one case and a dependent expression
187 // in another case.
188 return Sema::TDK_Success;
189 }
190
191 if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Declaration) {
192 // Compare the declarations for equality
193 if (Deduced[NTTP->getIndex()].getAsDecl()->getCanonicalDecl() ==
194 D->getCanonicalDecl())
195 return Sema::TDK_Success;
196
197 // FIXME: Fill in argument mismatch information
198 return Sema::TDK_NonDeducedMismatch;
199 }
200
201 return Sema::TDK_Success;
202}
203
Douglas Gregorf67875d2009-06-12 18:26:56 +0000204static Sema::TemplateDeductionResult
205DeduceTemplateArguments(ASTContext &Context,
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000206 TemplateParameterList *TemplateParams,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000207 TemplateName Param,
208 TemplateName Arg,
209 Sema::TemplateDeductionInfo &Info,
210 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregord708c722009-06-09 16:35:58 +0000211 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000212 if (!ParamDecl) {
213 // The parameter type is dependent and is not a template template parameter,
214 // so there is nothing that we can deduce.
215 return Sema::TDK_Success;
216 }
217
218 if (TemplateTemplateParmDecl *TempParam
219 = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) {
220 // Bind the template template parameter to the given template name.
221 TemplateArgument &ExistingArg = Deduced[TempParam->getIndex()];
222 if (ExistingArg.isNull()) {
223 // This is the first deduction for this template template parameter.
224 ExistingArg = TemplateArgument(Context.getCanonicalTemplateName(Arg));
225 return Sema::TDK_Success;
226 }
227
228 // Verify that the previous binding matches this deduction.
229 assert(ExistingArg.getKind() == TemplateArgument::Template);
230 if (Context.hasSameTemplateName(ExistingArg.getAsTemplate(), Arg))
231 return Sema::TDK_Success;
232
233 // Inconsistent deduction.
234 Info.Param = TempParam;
235 Info.FirstArg = ExistingArg;
236 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000237 return Sema::TDK_Inconsistent;
238 }
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000239
240 // Verify that the two template names are equivalent.
241 if (Context.hasSameTemplateName(Param, Arg))
242 return Sema::TDK_Success;
243
244 // Mismatch of non-dependent template parameter to argument.
245 Info.FirstArg = TemplateArgument(Param);
246 Info.SecondArg = TemplateArgument(Arg);
247 return Sema::TDK_NonDeducedMismatch;
Douglas Gregord708c722009-06-09 16:35:58 +0000248}
249
Mike Stump1eb44332009-09-09 15:08:12 +0000250/// \brief Deduce the template arguments by comparing the template parameter
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000251/// type (which is a template-id) with the template argument type.
252///
253/// \param Context the AST context in which this deduction occurs.
254///
255/// \param TemplateParams the template parameters that we are deducing
256///
257/// \param Param the parameter type
258///
259/// \param Arg the argument type
260///
261/// \param Info information about the template argument deduction itself
262///
263/// \param Deduced the deduced template arguments
264///
265/// \returns the result of template argument deduction so far. Note that a
266/// "success" result means that template argument deduction has not yet failed,
267/// but it may still fail, later, for other reasons.
268static Sema::TemplateDeductionResult
269DeduceTemplateArguments(ASTContext &Context,
270 TemplateParameterList *TemplateParams,
271 const TemplateSpecializationType *Param,
272 QualType Arg,
273 Sema::TemplateDeductionInfo &Info,
274 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
John McCall467b27b2009-10-22 20:10:53 +0000275 assert(Arg.isCanonical() && "Argument type must be canonical");
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000277 // Check whether the template argument is a dependent template-id.
Mike Stump1eb44332009-09-09 15:08:12 +0000278 if (const TemplateSpecializationType *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000279 = dyn_cast<TemplateSpecializationType>(Arg)) {
280 // Perform template argument deduction for the template name.
281 if (Sema::TemplateDeductionResult Result
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000282 = DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000283 Param->getTemplateName(),
284 SpecArg->getTemplateName(),
285 Info, Deduced))
286 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000289 // Perform template argument deduction on each template
290 // argument.
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000291 unsigned NumArgs = std::min(SpecArg->getNumArgs(), Param->getNumArgs());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000292 for (unsigned I = 0; I != NumArgs; ++I)
293 if (Sema::TemplateDeductionResult Result
294 = DeduceTemplateArguments(Context, TemplateParams,
295 Param->getArg(I),
296 SpecArg->getArg(I),
297 Info, Deduced))
298 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000300 return Sema::TDK_Success;
301 }
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000303 // If the argument type is a class template specialization, we
304 // perform template argument deduction using its template
305 // arguments.
306 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
307 if (!RecordArg)
308 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000309
310 ClassTemplateSpecializationDecl *SpecArg
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000311 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
312 if (!SpecArg)
313 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000315 // Perform template argument deduction for the template name.
316 if (Sema::TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000317 = DeduceTemplateArguments(Context,
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000318 TemplateParams,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000319 Param->getTemplateName(),
320 TemplateName(SpecArg->getSpecializedTemplate()),
321 Info, Deduced))
322 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000324 unsigned NumArgs = Param->getNumArgs();
325 const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
326 if (NumArgs != ArgArgs.size())
327 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000329 for (unsigned I = 0; I != NumArgs; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +0000330 if (Sema::TemplateDeductionResult Result
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000331 = DeduceTemplateArguments(Context, TemplateParams,
332 Param->getArg(I),
333 ArgArgs.get(I),
334 Info, Deduced))
335 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000337 return Sema::TDK_Success;
338}
339
Mike Stump1eb44332009-09-09 15:08:12 +0000340/// \brief Returns a completely-unqualified array type, capturing the
John McCall0953e762009-09-24 19:53:00 +0000341/// qualifiers in Quals.
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000342///
343/// \param Context the AST context in which the array type was built.
344///
345/// \param T a canonical type that may be an array type.
346///
John McCall0953e762009-09-24 19:53:00 +0000347/// \param Quals will receive the full set of qualifiers that were
348/// applied to the element type of the array.
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000349///
350/// \returns if \p T is an array type, the completely unqualified array type
351/// that corresponds to T. Otherwise, returns T.
352static QualType getUnqualifiedArrayType(ASTContext &Context, QualType T,
John McCall0953e762009-09-24 19:53:00 +0000353 Qualifiers &Quals) {
John McCall467b27b2009-10-22 20:10:53 +0000354 assert(T.isCanonical() && "Only operates on canonical types");
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000355 if (!isa<ArrayType>(T)) {
Douglas Gregora4923eb2009-11-16 21:35:15 +0000356 Quals = T.getLocalQualifiers();
357 return T.getLocalUnqualifiedType();
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000358 }
Mike Stump1eb44332009-09-09 15:08:12 +0000359
John McCall0953e762009-09-24 19:53:00 +0000360 assert(!T.hasQualifiers() && "canonical array type has qualifiers!");
361
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000362 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) {
363 QualType Elt = getUnqualifiedArrayType(Context, CAT->getElementType(),
John McCall0953e762009-09-24 19:53:00 +0000364 Quals);
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000365 if (Elt == CAT->getElementType())
366 return T;
367
Mike Stump1eb44332009-09-09 15:08:12 +0000368 return Context.getConstantArrayType(Elt, CAT->getSize(),
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000369 CAT->getSizeModifier(), 0);
370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000372 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) {
373 QualType Elt = getUnqualifiedArrayType(Context, IAT->getElementType(),
John McCall0953e762009-09-24 19:53:00 +0000374 Quals);
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000375 if (Elt == IAT->getElementType())
376 return T;
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000378 return Context.getIncompleteArrayType(Elt, IAT->getSizeModifier(), 0);
379 }
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000381 const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T);
382 QualType Elt = getUnqualifiedArrayType(Context, DSAT->getElementType(),
John McCall0953e762009-09-24 19:53:00 +0000383 Quals);
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000384 if (Elt == DSAT->getElementType())
385 return T;
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Anders Carlssond4972062009-08-08 02:50:17 +0000387 return Context.getDependentSizedArrayType(Elt, DSAT->getSizeExpr()->Retain(),
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000388 DSAT->getSizeModifier(), 0,
389 SourceRange());
390}
391
Douglas Gregor500d3312009-06-26 18:27:22 +0000392/// \brief Deduce the template arguments by comparing the parameter type and
393/// the argument type (C++ [temp.deduct.type]).
394///
395/// \param Context the AST context in which this deduction occurs.
396///
397/// \param TemplateParams the template parameters that we are deducing
398///
399/// \param ParamIn the parameter type
400///
401/// \param ArgIn the argument type
402///
403/// \param Info information about the template argument deduction itself
404///
405/// \param Deduced the deduced template arguments
406///
Douglas Gregor508f1c82009-06-26 23:10:12 +0000407/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
Mike Stump1eb44332009-09-09 15:08:12 +0000408/// how template argument deduction is performed.
Douglas Gregor500d3312009-06-26 18:27:22 +0000409///
410/// \returns the result of template argument deduction so far. Note that a
411/// "success" result means that template argument deduction has not yet failed,
412/// but it may still fail, later, for other reasons.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000413static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +0000414DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000415 TemplateParameterList *TemplateParams,
416 QualType ParamIn, QualType ArgIn,
417 Sema::TemplateDeductionInfo &Info,
Douglas Gregor500d3312009-06-26 18:27:22 +0000418 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000419 unsigned TDF) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000420 // We only want to look at the canonical types, since typedefs and
421 // sugar are not part of template argument deduction.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000422 QualType Param = Context.getCanonicalType(ParamIn);
423 QualType Arg = Context.getCanonicalType(ArgIn);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000424
Douglas Gregor500d3312009-06-26 18:27:22 +0000425 // C++0x [temp.deduct.call]p4 bullet 1:
426 // - If the original P is a reference type, the deduced A (i.e., the type
Mike Stump1eb44332009-09-09 15:08:12 +0000427 // referred to by the reference) can be more cv-qualified than the
Douglas Gregor500d3312009-06-26 18:27:22 +0000428 // transformed A.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000429 if (TDF & TDF_ParamWithReferenceType) {
John McCall0953e762009-09-24 19:53:00 +0000430 Qualifiers Quals = Param.getQualifiers();
431 Quals.setCVRQualifiers(Quals.getCVRQualifiers() & Arg.getCVRQualifiers());
432 Param = Context.getQualifiedType(Param.getUnqualifiedType(), Quals);
Douglas Gregor500d3312009-06-26 18:27:22 +0000433 }
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000435 // If the parameter type is not dependent, there is nothing to deduce.
Douglas Gregor12820292009-09-14 20:00:47 +0000436 if (!Param->isDependentType()) {
437 if (!(TDF & TDF_SkipNonDependent) && Param != Arg) {
438
439 return Sema::TDK_NonDeducedMismatch;
440 }
441
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000442 return Sema::TDK_Success;
Douglas Gregor12820292009-09-14 20:00:47 +0000443 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000444
Douglas Gregor199d9912009-06-05 00:53:49 +0000445 // C++ [temp.deduct.type]p9:
Mike Stump1eb44332009-09-09 15:08:12 +0000446 // A template type argument T, a template template argument TT or a
447 // template non-type argument i can be deduced if P and A have one of
Douglas Gregor199d9912009-06-05 00:53:49 +0000448 // the following forms:
449 //
450 // T
451 // cv-list T
Mike Stump1eb44332009-09-09 15:08:12 +0000452 if (const TemplateTypeParmType *TemplateTypeParm
John McCall183700f2009-09-21 23:43:11 +0000453 = Param->getAs<TemplateTypeParmType>()) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000454 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000455 bool RecanonicalizeArg = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Douglas Gregor9e9fae42009-07-22 20:02:25 +0000457 // If the argument type is an array type, move the qualifiers up to the
458 // top level, so they can be matched with the qualifiers on the parameter.
459 // FIXME: address spaces, ObjC GC qualifiers
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000460 if (isa<ArrayType>(Arg)) {
John McCall0953e762009-09-24 19:53:00 +0000461 Qualifiers Quals;
462 Arg = getUnqualifiedArrayType(Context, Arg, Quals);
463 if (Quals) {
464 Arg = Context.getQualifiedType(Arg, Quals);
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000465 RecanonicalizeArg = true;
466 }
467 }
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000469 // The argument type can not be less qualified than the parameter
470 // type.
Douglas Gregor508f1c82009-06-26 23:10:12 +0000471 if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000472 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
473 Info.FirstArg = Deduced[Index];
John McCall833ca992009-10-29 08:12:44 +0000474 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000475 return Sema::TDK_InconsistentQuals;
476 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000477
478 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
Douglas Gregorc78a69d2009-12-21 21:27:38 +0000479 assert(Arg != Context.OverloadTy && "Unresolved overloaded function");
John McCall0953e762009-09-24 19:53:00 +0000480 QualType DeducedType = Arg;
481 DeducedType.removeCVRQualifiers(Param.getCVRQualifiers());
Douglas Gregorf290e0d2009-07-22 21:30:48 +0000482 if (RecanonicalizeArg)
483 DeducedType = Context.getCanonicalType(DeducedType);
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000485 if (Deduced[Index].isNull())
John McCall833ca992009-10-29 08:12:44 +0000486 Deduced[Index] = TemplateArgument(DeducedType);
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000487 else {
Mike Stump1eb44332009-09-09 15:08:12 +0000488 // C++ [temp.deduct.type]p2:
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000489 // [...] If type deduction cannot be done for any P/A pair, or if for
Mike Stump1eb44332009-09-09 15:08:12 +0000490 // any pair the deduction leads to more than one possible set of
491 // deduced values, or if different pairs yield different deduced
492 // values, or if any template argument remains neither deduced nor
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000493 // explicitly specified, template argument deduction fails.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000494 if (Deduced[Index].getAsType() != DeducedType) {
Mike Stump1eb44332009-09-09 15:08:12 +0000495 Info.Param
Douglas Gregorf67875d2009-06-12 18:26:56 +0000496 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
497 Info.FirstArg = Deduced[Index];
John McCall833ca992009-10-29 08:12:44 +0000498 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000499 return Sema::TDK_Inconsistent;
500 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000501 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000502 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000503 }
504
Douglas Gregorf67875d2009-06-12 18:26:56 +0000505 // Set up the template argument deduction information for a failure.
John McCall833ca992009-10-29 08:12:44 +0000506 Info.FirstArg = TemplateArgument(ParamIn);
507 Info.SecondArg = TemplateArgument(ArgIn);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000508
Douglas Gregor508f1c82009-06-26 23:10:12 +0000509 // Check the cv-qualifiers on the parameter and argument types.
510 if (!(TDF & TDF_IgnoreQualifiers)) {
511 if (TDF & TDF_ParamWithReferenceType) {
512 if (Param.isMoreQualifiedThan(Arg))
513 return Sema::TDK_NonDeducedMismatch;
514 } else {
515 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Mike Stump1eb44332009-09-09 15:08:12 +0000516 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor508f1c82009-06-26 23:10:12 +0000517 }
518 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000519
Douglas Gregord560d502009-06-04 00:21:18 +0000520 switch (Param->getTypeClass()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000521 // No deduction possible for these types
522 case Type::Builtin:
Douglas Gregorf67875d2009-06-12 18:26:56 +0000523 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Douglas Gregor199d9912009-06-05 00:53:49 +0000525 // T *
Douglas Gregord560d502009-06-04 00:21:18 +0000526 case Type::Pointer: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000527 const PointerType *PointerArg = Arg->getAs<PointerType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000528 if (!PointerArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000529 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Douglas Gregor41128772009-06-26 23:27:24 +0000531 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000532 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000533 cast<PointerType>(Param)->getPointeeType(),
534 PointerArg->getPointeeType(),
Douglas Gregor41128772009-06-26 23:27:24 +0000535 Info, Deduced, SubTDF);
Douglas Gregord560d502009-06-04 00:21:18 +0000536 }
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Douglas Gregor199d9912009-06-05 00:53:49 +0000538 // T &
Douglas Gregord560d502009-06-04 00:21:18 +0000539 case Type::LValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000540 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000541 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000542 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Douglas Gregorf67875d2009-06-12 18:26:56 +0000544 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000545 cast<LValueReferenceType>(Param)->getPointeeType(),
546 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000547 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000548 }
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000549
Douglas Gregor199d9912009-06-05 00:53:49 +0000550 // T && [C++0x]
Douglas Gregord560d502009-06-04 00:21:18 +0000551 case Type::RValueReference: {
Ted Kremenek6217b802009-07-29 21:53:49 +0000552 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregord560d502009-06-04 00:21:18 +0000553 if (!ReferenceArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000554 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Douglas Gregorf67875d2009-06-12 18:26:56 +0000556 return DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregord560d502009-06-04 00:21:18 +0000557 cast<RValueReferenceType>(Param)->getPointeeType(),
558 ReferenceArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000559 Info, Deduced, 0);
Douglas Gregord560d502009-06-04 00:21:18 +0000560 }
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Douglas Gregor199d9912009-06-05 00:53:49 +0000562 // T [] (implied, but not stated explicitly)
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000563 case Type::IncompleteArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000564 const IncompleteArrayType *IncompleteArrayArg =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000565 Context.getAsIncompleteArrayType(Arg);
566 if (!IncompleteArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000567 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Douglas Gregorf67875d2009-06-12 18:26:56 +0000569 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000570 Context.getAsIncompleteArrayType(Param)->getElementType(),
571 IncompleteArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000572 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000573 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000574
575 // T [integer-constant]
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000576 case Type::ConstantArray: {
Mike Stump1eb44332009-09-09 15:08:12 +0000577 const ConstantArrayType *ConstantArrayArg =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000578 Context.getAsConstantArrayType(Arg);
579 if (!ConstantArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000580 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000581
582 const ConstantArrayType *ConstantArrayParm =
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000583 Context.getAsConstantArrayType(Param);
584 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000585 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Douglas Gregorf67875d2009-06-12 18:26:56 +0000587 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000588 ConstantArrayParm->getElementType(),
589 ConstantArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000590 Info, Deduced, 0);
Anders Carlsson4d6fb502009-06-04 04:11:30 +0000591 }
592
Douglas Gregor199d9912009-06-05 00:53:49 +0000593 // type [i]
594 case Type::DependentSizedArray: {
595 const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg);
596 if (!ArrayArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000597 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Douglas Gregor199d9912009-06-05 00:53:49 +0000599 // Check the element type of the arrays
600 const DependentSizedArrayType *DependentArrayParm
601 = cast<DependentSizedArrayType>(Param);
Douglas Gregorf67875d2009-06-12 18:26:56 +0000602 if (Sema::TemplateDeductionResult Result
603 = DeduceTemplateArguments(Context, TemplateParams,
604 DependentArrayParm->getElementType(),
605 ArrayArg->getElementType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000606 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000607 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Douglas Gregor199d9912009-06-05 00:53:49 +0000609 // Determine the array bound is something we can deduce.
Mike Stump1eb44332009-09-09 15:08:12 +0000610 NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000611 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
612 if (!NTTP)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000613 return Sema::TDK_Success;
Mike Stump1eb44332009-09-09 15:08:12 +0000614
615 // We can perform template argument deduction for the given non-type
Douglas Gregor199d9912009-06-05 00:53:49 +0000616 // template parameter.
Mike Stump1eb44332009-09-09 15:08:12 +0000617 assert(NTTP->getDepth() == 0 &&
Douglas Gregor199d9912009-06-05 00:53:49 +0000618 "Cannot deduce non-type template argument at depth > 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000619 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson335e24a2009-06-16 22:44:31 +0000620 = dyn_cast<ConstantArrayType>(ArrayArg)) {
621 llvm::APSInt Size(ConstantArrayArg->getSize());
622 return DeduceNonTypeTemplateArgument(Context, NTTP, Size,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000623 Info, Deduced);
Anders Carlsson335e24a2009-06-16 22:44:31 +0000624 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000625 if (const DependentSizedArrayType *DependentArrayArg
626 = dyn_cast<DependentSizedArrayType>(ArrayArg))
627 return DeduceNonTypeTemplateArgument(Context, NTTP,
628 DependentArrayArg->getSizeExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000629 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Douglas Gregor199d9912009-06-05 00:53:49 +0000631 // Incomplete type does not match a dependently-sized array type
Douglas Gregorf67875d2009-06-12 18:26:56 +0000632 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
635 // type(*)(T)
636 // T(*)()
637 // T(*)(T)
Anders Carlssona27fad52009-06-08 15:19:08 +0000638 case Type::FunctionProto: {
Mike Stump1eb44332009-09-09 15:08:12 +0000639 const FunctionProtoType *FunctionProtoArg =
Anders Carlssona27fad52009-06-08 15:19:08 +0000640 dyn_cast<FunctionProtoType>(Arg);
641 if (!FunctionProtoArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000642 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000643
644 const FunctionProtoType *FunctionProtoParam =
Anders Carlssona27fad52009-06-08 15:19:08 +0000645 cast<FunctionProtoType>(Param);
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000646
Mike Stump1eb44332009-09-09 15:08:12 +0000647 if (FunctionProtoParam->getTypeQuals() !=
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000648 FunctionProtoArg->getTypeQuals())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000649 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000651 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000652 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000654 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregorf67875d2009-06-12 18:26:56 +0000655 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson994b6cb2009-06-08 19:22:23 +0000656
Anders Carlssona27fad52009-06-08 15:19:08 +0000657 // Check return types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000658 if (Sema::TemplateDeductionResult Result
659 = DeduceTemplateArguments(Context, TemplateParams,
660 FunctionProtoParam->getResultType(),
661 FunctionProtoArg->getResultType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000662 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000663 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Anders Carlssona27fad52009-06-08 15:19:08 +0000665 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
666 // Check argument types.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000667 if (Sema::TemplateDeductionResult Result
668 = DeduceTemplateArguments(Context, TemplateParams,
669 FunctionProtoParam->getArgType(I),
670 FunctionProtoArg->getArgType(I),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000671 Info, Deduced, 0))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000672 return Result;
Anders Carlssona27fad52009-06-08 15:19:08 +0000673 }
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Douglas Gregorf67875d2009-06-12 18:26:56 +0000675 return Sema::TDK_Success;
Anders Carlssona27fad52009-06-08 15:19:08 +0000676 }
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000678 // template-name<T> (where template-name refers to a class template)
Douglas Gregord708c722009-06-09 16:35:58 +0000679 // template-name<i>
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000680 // TT<T>
681 // TT<i>
682 // TT<>
Douglas Gregord708c722009-06-09 16:35:58 +0000683 case Type::TemplateSpecialization: {
684 const TemplateSpecializationType *SpecParam
685 = cast<TemplateSpecializationType>(Param);
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000687 // Try to deduce template arguments from the template-id.
688 Sema::TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +0000689 = DeduceTemplateArguments(Context, TemplateParams, SpecParam, Arg,
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000690 Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Douglas Gregor4a5c15f2009-09-30 22:13:51 +0000692 if (Result && (TDF & TDF_DerivedClass)) {
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000693 // C++ [temp.deduct.call]p3b3:
694 // If P is a class, and P has the form template-id, then A can be a
695 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump1eb44332009-09-09 15:08:12 +0000696 // class of the form template-id, A can be a pointer to a derived
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000697 // class pointed to by the deduced A.
698 //
699 // More importantly:
Mike Stump1eb44332009-09-09 15:08:12 +0000700 // These alternatives are considered only if type deduction would
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000701 // otherwise fail.
702 if (const RecordType *RecordT = dyn_cast<RecordType>(Arg)) {
703 // Use data recursion to crawl through the list of base classes.
Mike Stump1eb44332009-09-09 15:08:12 +0000704 // Visited contains the set of nodes we have already visited, while
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000705 // ToVisit is our stack of records that we still need to visit.
706 llvm::SmallPtrSet<const RecordType *, 8> Visited;
707 llvm::SmallVector<const RecordType *, 8> ToVisit;
708 ToVisit.push_back(RecordT);
709 bool Successful = false;
710 while (!ToVisit.empty()) {
711 // Retrieve the next class in the inheritance hierarchy.
712 const RecordType *NextT = ToVisit.back();
713 ToVisit.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000715 // If we have already seen this type, skip it.
716 if (!Visited.insert(NextT))
717 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000719 // If this is a base class, try to perform template argument
720 // deduction from it.
721 if (NextT != RecordT) {
722 Sema::TemplateDeductionResult BaseResult
723 = DeduceTemplateArguments(Context, TemplateParams, SpecParam,
724 QualType(NextT, 0), Info, Deduced);
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000726 // If template argument deduction for this base was successful,
727 // note that we had some success.
728 if (BaseResult == Sema::TDK_Success)
729 Successful = true;
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000730 }
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000732 // Visit base classes
733 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
734 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
735 BaseEnd = Next->bases_end();
Sebastian Redl9994a342009-10-25 17:03:50 +0000736 Base != BaseEnd; ++Base) {
Mike Stump1eb44332009-09-09 15:08:12 +0000737 assert(Base->getType()->isRecordType() &&
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000738 "Base class that isn't a record?");
Ted Kremenek6217b802009-07-29 21:53:49 +0000739 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000740 }
741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000743 if (Successful)
744 return Sema::TDK_Success;
745 }
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000747 }
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Douglas Gregorde0cb8b2009-07-07 23:09:34 +0000749 return Result;
Douglas Gregord708c722009-06-09 16:35:58 +0000750 }
751
Douglas Gregor637a4092009-06-10 23:47:09 +0000752 // T type::*
753 // T T::*
754 // T (type::*)()
755 // type (T::*)()
756 // type (type::*)(T)
757 // type (T::*)(T)
758 // T (type::*)(T)
759 // T (T::*)()
760 // T (T::*)(T)
761 case Type::MemberPointer: {
762 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
763 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
764 if (!MemPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000765 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637a4092009-06-10 23:47:09 +0000766
Douglas Gregorf67875d2009-06-12 18:26:56 +0000767 if (Sema::TemplateDeductionResult Result
768 = DeduceTemplateArguments(Context, TemplateParams,
769 MemPtrParam->getPointeeType(),
770 MemPtrArg->getPointeeType(),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000771 Info, Deduced,
772 TDF & TDF_IgnoreQualifiers))
Douglas Gregorf67875d2009-06-12 18:26:56 +0000773 return Result;
774
775 return DeduceTemplateArguments(Context, TemplateParams,
776 QualType(MemPtrParam->getClass(), 0),
777 QualType(MemPtrArg->getClass(), 0),
Douglas Gregor508f1c82009-06-26 23:10:12 +0000778 Info, Deduced, 0);
Douglas Gregor637a4092009-06-10 23:47:09 +0000779 }
780
Anders Carlsson9a917e42009-06-12 22:56:54 +0000781 // (clang extension)
782 //
Mike Stump1eb44332009-09-09 15:08:12 +0000783 // type(^)(T)
784 // T(^)()
785 // T(^)(T)
Anders Carlsson859ba502009-06-12 16:23:10 +0000786 case Type::BlockPointer: {
787 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
788 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Anders Carlsson859ba502009-06-12 16:23:10 +0000790 if (!BlockPtrArg)
Douglas Gregorf67875d2009-06-12 18:26:56 +0000791 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Douglas Gregorf67875d2009-06-12 18:26:56 +0000793 return DeduceTemplateArguments(Context, TemplateParams,
Anders Carlsson859ba502009-06-12 16:23:10 +0000794 BlockPtrParam->getPointeeType(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000795 BlockPtrArg->getPointeeType(), Info,
Douglas Gregor508f1c82009-06-26 23:10:12 +0000796 Deduced, 0);
Anders Carlsson859ba502009-06-12 16:23:10 +0000797 }
798
Douglas Gregor637a4092009-06-10 23:47:09 +0000799 case Type::TypeOfExpr:
800 case Type::TypeOf:
801 case Type::Typename:
802 // No template argument deduction for these types
Douglas Gregorf67875d2009-06-12 18:26:56 +0000803 return Sema::TDK_Success;
Douglas Gregor637a4092009-06-10 23:47:09 +0000804
Douglas Gregord560d502009-06-04 00:21:18 +0000805 default:
806 break;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000807 }
808
809 // FIXME: Many more cases to go (to go).
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000810 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000811}
812
Douglas Gregorf67875d2009-06-12 18:26:56 +0000813static Sema::TemplateDeductionResult
Mike Stump1eb44332009-09-09 15:08:12 +0000814DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000815 TemplateParameterList *TemplateParams,
816 const TemplateArgument &Param,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000817 const TemplateArgument &Arg,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000818 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000819 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000820 switch (Param.getKind()) {
Douglas Gregor199d9912009-06-05 00:53:49 +0000821 case TemplateArgument::Null:
822 assert(false && "Null template argument in parameter list");
823 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000824
825 case TemplateArgument::Type:
Douglas Gregor788cd062009-11-11 01:00:40 +0000826 if (Arg.getKind() == TemplateArgument::Type)
827 return DeduceTemplateArguments(Context, TemplateParams, Param.getAsType(),
828 Arg.getAsType(), Info, Deduced, 0);
829 Info.FirstArg = Param;
830 Info.SecondArg = Arg;
831 return Sema::TDK_NonDeducedMismatch;
832
833 case TemplateArgument::Template:
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000834 if (Arg.getKind() == TemplateArgument::Template)
Douglas Gregor788cd062009-11-11 01:00:40 +0000835 return DeduceTemplateArguments(Context, TemplateParams,
836 Param.getAsTemplate(),
Douglas Gregordb0d4b72009-11-11 23:06:43 +0000837 Arg.getAsTemplate(), Info, Deduced);
Douglas Gregor788cd062009-11-11 01:00:40 +0000838 Info.FirstArg = Param;
839 Info.SecondArg = Arg;
840 return Sema::TDK_NonDeducedMismatch;
841
Douglas Gregor199d9912009-06-05 00:53:49 +0000842 case TemplateArgument::Declaration:
Douglas Gregor788cd062009-11-11 01:00:40 +0000843 if (Arg.getKind() == TemplateArgument::Declaration &&
844 Param.getAsDecl()->getCanonicalDecl() ==
845 Arg.getAsDecl()->getCanonicalDecl())
846 return Sema::TDK_Success;
847
Douglas Gregorf67875d2009-06-12 18:26:56 +0000848 Info.FirstArg = Param;
849 Info.SecondArg = Arg;
850 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Douglas Gregor199d9912009-06-05 00:53:49 +0000852 case TemplateArgument::Integral:
853 if (Arg.getKind() == TemplateArgument::Integral) {
854 // FIXME: Zero extension + sign checking here?
Douglas Gregorf67875d2009-06-12 18:26:56 +0000855 if (*Param.getAsIntegral() == *Arg.getAsIntegral())
856 return Sema::TDK_Success;
857
858 Info.FirstArg = Param;
859 Info.SecondArg = Arg;
860 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000861 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000862
863 if (Arg.getKind() == TemplateArgument::Expression) {
864 Info.FirstArg = Param;
865 Info.SecondArg = Arg;
866 return Sema::TDK_NonDeducedMismatch;
867 }
Douglas Gregor199d9912009-06-05 00:53:49 +0000868
869 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000870 Info.FirstArg = Param;
871 Info.SecondArg = Arg;
872 return Sema::TDK_NonDeducedMismatch;
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Douglas Gregor199d9912009-06-05 00:53:49 +0000874 case TemplateArgument::Expression: {
Mike Stump1eb44332009-09-09 15:08:12 +0000875 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor199d9912009-06-05 00:53:49 +0000876 = getDeducedParameterFromExpr(Param.getAsExpr())) {
877 if (Arg.getKind() == TemplateArgument::Integral)
878 // FIXME: Sign problems here
Mike Stump1eb44332009-09-09 15:08:12 +0000879 return DeduceNonTypeTemplateArgument(Context, NTTP,
880 *Arg.getAsIntegral(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000881 Info, Deduced);
Douglas Gregor199d9912009-06-05 00:53:49 +0000882 if (Arg.getKind() == TemplateArgument::Expression)
883 return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(),
Douglas Gregorf67875d2009-06-12 18:26:56 +0000884 Info, Deduced);
Douglas Gregor15755cb2009-11-13 23:45:44 +0000885 if (Arg.getKind() == TemplateArgument::Declaration)
886 return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsDecl(),
887 Info, Deduced);
888
Douglas Gregor199d9912009-06-05 00:53:49 +0000889 assert(false && "Type/value mismatch");
Douglas Gregorf67875d2009-06-12 18:26:56 +0000890 Info.FirstArg = Param;
891 Info.SecondArg = Arg;
892 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor199d9912009-06-05 00:53:49 +0000893 }
Mike Stump1eb44332009-09-09 15:08:12 +0000894
Douglas Gregor199d9912009-06-05 00:53:49 +0000895 // Can't deduce anything, but that's okay.
Douglas Gregorf67875d2009-06-12 18:26:56 +0000896 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000897 }
Anders Carlssond01b1da2009-06-15 17:04:53 +0000898 case TemplateArgument::Pack:
899 assert(0 && "FIXME: Implement!");
900 break;
Douglas Gregor199d9912009-06-05 00:53:49 +0000901 }
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Douglas Gregorf67875d2009-06-12 18:26:56 +0000903 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000904}
905
Mike Stump1eb44332009-09-09 15:08:12 +0000906static Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000907DeduceTemplateArguments(ASTContext &Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000908 TemplateParameterList *TemplateParams,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000909 const TemplateArgumentList &ParamList,
910 const TemplateArgumentList &ArgList,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000911 Sema::TemplateDeductionInfo &Info,
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000912 llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
913 assert(ParamList.size() == ArgList.size());
914 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +0000915 if (Sema::TemplateDeductionResult Result
916 = DeduceTemplateArguments(Context, TemplateParams,
Mike Stump1eb44332009-09-09 15:08:12 +0000917 ParamList[I], ArgList[I],
Douglas Gregorf67875d2009-06-12 18:26:56 +0000918 Info, Deduced))
919 return Result;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000920 }
Douglas Gregorf67875d2009-06-12 18:26:56 +0000921 return Sema::TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000922}
923
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000924/// \brief Determine whether two template arguments are the same.
Mike Stump1eb44332009-09-09 15:08:12 +0000925static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000926 const TemplateArgument &X,
927 const TemplateArgument &Y) {
928 if (X.getKind() != Y.getKind())
929 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000931 switch (X.getKind()) {
932 case TemplateArgument::Null:
933 assert(false && "Comparing NULL template argument");
934 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000936 case TemplateArgument::Type:
937 return Context.getCanonicalType(X.getAsType()) ==
938 Context.getCanonicalType(Y.getAsType());
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000940 case TemplateArgument::Declaration:
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +0000941 return X.getAsDecl()->getCanonicalDecl() ==
942 Y.getAsDecl()->getCanonicalDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Douglas Gregor788cd062009-11-11 01:00:40 +0000944 case TemplateArgument::Template:
945 return Context.getCanonicalTemplateName(X.getAsTemplate())
946 .getAsVoidPointer() ==
947 Context.getCanonicalTemplateName(Y.getAsTemplate())
948 .getAsVoidPointer();
949
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000950 case TemplateArgument::Integral:
951 return *X.getAsIntegral() == *Y.getAsIntegral();
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Douglas Gregor788cd062009-11-11 01:00:40 +0000953 case TemplateArgument::Expression: {
954 llvm::FoldingSetNodeID XID, YID;
955 X.getAsExpr()->Profile(XID, Context, true);
956 Y.getAsExpr()->Profile(YID, Context, true);
957 return XID == YID;
958 }
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000960 case TemplateArgument::Pack:
961 if (X.pack_size() != Y.pack_size())
962 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000963
964 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
965 XPEnd = X.pack_end(),
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000966 YP = Y.pack_begin();
Mike Stump1eb44332009-09-09 15:08:12 +0000967 XP != XPEnd; ++XP, ++YP)
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000968 if (!isSameTemplateArg(Context, *XP, *YP))
969 return false;
970
971 return true;
972 }
973
974 return false;
975}
976
977/// \brief Helper function to build a TemplateParameter when we don't
978/// know its type statically.
979static TemplateParameter makeTemplateParameter(Decl *D) {
980 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
981 return TemplateParameter(TTP);
982 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
983 return TemplateParameter(NTTP);
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Douglas Gregorf670c8c2009-06-26 20:57:09 +0000985 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
986}
987
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000988/// \brief Perform template argument deduction to determine whether
989/// the given template arguments match the given class template
990/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregorf67875d2009-06-12 18:26:56 +0000991Sema::TemplateDeductionResult
Douglas Gregor0b9247f2009-06-04 00:03:07 +0000992Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregorf67875d2009-06-12 18:26:56 +0000993 const TemplateArgumentList &TemplateArgs,
994 TemplateDeductionInfo &Info) {
Douglas Gregorc1efb3f2009-06-12 22:31:52 +0000995 // C++ [temp.class.spec.match]p2:
996 // A partial specialization matches a given actual template
997 // argument list if the template arguments of the partial
998 // specialization can be deduced from the actual template argument
999 // list (14.8.2).
Douglas Gregorbb260412009-06-14 08:02:22 +00001000 SFINAETrap Trap(*this);
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001001 llvm::SmallVector<TemplateArgument, 4> Deduced;
1002 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregorf67875d2009-06-12 18:26:56 +00001003 if (TemplateDeductionResult Result
Mike Stump1eb44332009-09-09 15:08:12 +00001004 = ::DeduceTemplateArguments(Context,
Douglas Gregorf67875d2009-06-12 18:26:56 +00001005 Partial->getTemplateParameters(),
Mike Stump1eb44332009-09-09 15:08:12 +00001006 Partial->getTemplateArgs(),
Douglas Gregorf67875d2009-06-12 18:26:56 +00001007 TemplateArgs, Info, Deduced))
1008 return Result;
Douglas Gregor637a4092009-06-10 23:47:09 +00001009
Douglas Gregor637a4092009-06-10 23:47:09 +00001010 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
1011 Deduced.data(), Deduced.size());
1012 if (Inst)
Douglas Gregorf67875d2009-06-12 18:26:56 +00001013 return TDK_InstantiationDepth;
Douglas Gregor199d9912009-06-05 00:53:49 +00001014
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001015 // C++ [temp.deduct.type]p2:
1016 // [...] or if any template argument remains neither deduced nor
1017 // explicitly specified, template argument deduction fails.
Anders Carlssonfb250522009-06-23 01:26:57 +00001018 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
1019 Deduced.size());
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001020 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
Douglas Gregorf67875d2009-06-12 18:26:56 +00001021 if (Deduced[I].isNull()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001022 Decl *Param
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001023 = const_cast<NamedDecl *>(
1024 Partial->getTemplateParameters()->getParam(I));
Douglas Gregorf67875d2009-06-12 18:26:56 +00001025 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
1026 Info.Param = TTP;
Mike Stump1eb44332009-09-09 15:08:12 +00001027 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorf67875d2009-06-12 18:26:56 +00001028 = dyn_cast<NonTypeTemplateParmDecl>(Param))
1029 Info.Param = NTTP;
1030 else
1031 Info.Param = cast<TemplateTemplateParmDecl>(Param);
1032 return TDK_Incomplete;
1033 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001034
Anders Carlssonfb250522009-06-23 01:26:57 +00001035 Builder.Append(Deduced[I]);
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001036 }
1037
1038 // Form the template argument list from the deduced template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001039 TemplateArgumentList *DeducedArgumentList
Anders Carlssonfb250522009-06-23 01:26:57 +00001040 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
Douglas Gregorf67875d2009-06-12 18:26:56 +00001041 Info.reset(DeducedArgumentList);
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001042
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001043 // Substitute the deduced template arguments into the template
1044 // arguments of the class template partial specialization, and
1045 // verify that the instantiated template arguments are both valid
1046 // and are equivalent to the template arguments originally provided
Mike Stump1eb44332009-09-09 15:08:12 +00001047 // to the class template.
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001048 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
John McCall833ca992009-10-29 08:12:44 +00001049 const TemplateArgumentLoc *PartialTemplateArgs
1050 = Partial->getTemplateArgsAsWritten();
1051 unsigned N = Partial->getNumTemplateArgsAsWritten();
John McCalld5532b62009-11-23 01:53:49 +00001052
1053 // Note that we don't provide the langle and rangle locations.
1054 TemplateArgumentListInfo InstArgs;
1055
John McCall833ca992009-10-29 08:12:44 +00001056 for (unsigned I = 0; I != N; ++I) {
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001057 Decl *Param = const_cast<NamedDecl *>(
Douglas Gregorc9e5d252009-06-13 00:59:32 +00001058 ClassTemplate->getTemplateParameters()->getParam(I));
John McCalld5532b62009-11-23 01:53:49 +00001059 TemplateArgumentLoc InstArg;
1060 if (Subst(PartialTemplateArgs[I], InstArg,
John McCall833ca992009-10-29 08:12:44 +00001061 MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001062 Info.Param = makeTemplateParameter(Param);
John McCall833ca992009-10-29 08:12:44 +00001063 Info.FirstArg = PartialTemplateArgs[I].getArgument();
Mike Stump1eb44332009-09-09 15:08:12 +00001064 return TDK_SubstitutionFailure;
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001065 }
John McCalld5532b62009-11-23 01:53:49 +00001066 InstArgs.addArgument(InstArg);
John McCall833ca992009-10-29 08:12:44 +00001067 }
1068
1069 TemplateArgumentListBuilder ConvertedInstArgs(
1070 ClassTemplate->getTemplateParameters(), N);
1071
1072 if (CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001073 InstArgs, false, ConvertedInstArgs)) {
John McCall833ca992009-10-29 08:12:44 +00001074 // FIXME: fail with more useful information?
1075 return TDK_SubstitutionFailure;
1076 }
1077
1078 for (unsigned I = 0, E = ConvertedInstArgs.flatSize(); I != E; ++I) {
John McCalld5532b62009-11-23 01:53:49 +00001079 TemplateArgument InstArg = ConvertedInstArgs.getFlatArguments()[I];
John McCall833ca992009-10-29 08:12:44 +00001080
1081 Decl *Param = const_cast<NamedDecl *>(
1082 ClassTemplate->getTemplateParameters()->getParam(I));
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001084 if (InstArg.getKind() == TemplateArgument::Expression) {
Mike Stump1eb44332009-09-09 15:08:12 +00001085 // When the argument is an expression, check the expression result
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001086 // against the actual template parameter to get down to the canonical
1087 // template argument.
1088 Expr *InstExpr = InstArg.getAsExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001089 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001090 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1091 if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
1092 Info.Param = makeTemplateParameter(Param);
John McCall833ca992009-10-29 08:12:44 +00001093 Info.FirstArg = Partial->getTemplateArgs()[I];
Mike Stump1eb44332009-09-09 15:08:12 +00001094 return TDK_SubstitutionFailure;
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001095 }
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001096 }
1097 }
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Douglas Gregorf670c8c2009-06-26 20:57:09 +00001099 if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) {
1100 Info.Param = makeTemplateParameter(Param);
1101 Info.FirstArg = TemplateArgs[I];
1102 Info.SecondArg = InstArg;
1103 return TDK_NonDeducedMismatch;
1104 }
Douglas Gregor02cbbd22009-06-11 18:10:32 +00001105 }
1106
Douglas Gregorbb260412009-06-14 08:02:22 +00001107 if (Trap.hasErrorOccurred())
1108 return TDK_SubstitutionFailure;
1109
Douglas Gregorf67875d2009-06-12 18:26:56 +00001110 return TDK_Success;
Douglas Gregor0b9247f2009-06-04 00:03:07 +00001111}
Douglas Gregor031a5882009-06-13 00:26:55 +00001112
Douglas Gregor41128772009-06-26 23:27:24 +00001113/// \brief Determine whether the given type T is a simple-template-id type.
1114static bool isSimpleTemplateIdType(QualType T) {
Mike Stump1eb44332009-09-09 15:08:12 +00001115 if (const TemplateSpecializationType *Spec
John McCall183700f2009-09-21 23:43:11 +00001116 = T->getAs<TemplateSpecializationType>())
Douglas Gregor41128772009-06-26 23:27:24 +00001117 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Douglas Gregor41128772009-06-26 23:27:24 +00001119 return false;
1120}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001121
1122/// \brief Substitute the explicitly-provided template arguments into the
1123/// given function template according to C++ [temp.arg.explicit].
1124///
1125/// \param FunctionTemplate the function template into which the explicit
1126/// template arguments will be substituted.
1127///
Mike Stump1eb44332009-09-09 15:08:12 +00001128/// \param ExplicitTemplateArguments the explicitly-specified template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001129/// arguments.
1130///
Mike Stump1eb44332009-09-09 15:08:12 +00001131/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor83314aa2009-07-08 20:55:45 +00001132/// with the converted and checked explicit template arguments.
1133///
Mike Stump1eb44332009-09-09 15:08:12 +00001134/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor83314aa2009-07-08 20:55:45 +00001135/// parameters.
1136///
1137/// \param FunctionType if non-NULL, the result type of the function template
1138/// will also be instantiated and the pointed-to value will be updated with
1139/// the instantiated function type.
1140///
1141/// \param Info if substitution fails for any reason, this object will be
1142/// populated with more information about the failure.
1143///
1144/// \returns TDK_Success if substitution was successful, or some failure
1145/// condition.
1146Sema::TemplateDeductionResult
1147Sema::SubstituteExplicitTemplateArguments(
1148 FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001149 const TemplateArgumentListInfo &ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001150 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1151 llvm::SmallVectorImpl<QualType> &ParamTypes,
1152 QualType *FunctionType,
1153 TemplateDeductionInfo &Info) {
1154 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1155 TemplateParameterList *TemplateParams
1156 = FunctionTemplate->getTemplateParameters();
1157
John McCalld5532b62009-11-23 01:53:49 +00001158 if (ExplicitTemplateArgs.size() == 0) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001159 // No arguments to substitute; just copy over the parameter types and
1160 // fill in the function type.
1161 for (FunctionDecl::param_iterator P = Function->param_begin(),
1162 PEnd = Function->param_end();
1163 P != PEnd;
1164 ++P)
1165 ParamTypes.push_back((*P)->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Douglas Gregor83314aa2009-07-08 20:55:45 +00001167 if (FunctionType)
1168 *FunctionType = Function->getType();
1169 return TDK_Success;
1170 }
Mike Stump1eb44332009-09-09 15:08:12 +00001171
Douglas Gregor83314aa2009-07-08 20:55:45 +00001172 // Substitution of the explicit template arguments into a function template
1173 /// is a SFINAE context. Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001174 SFINAETrap Trap(*this);
1175
Douglas Gregor83314aa2009-07-08 20:55:45 +00001176 // C++ [temp.arg.explicit]p3:
Mike Stump1eb44332009-09-09 15:08:12 +00001177 // Template arguments that are present shall be specified in the
1178 // declaration order of their corresponding template-parameters. The
Douglas Gregor83314aa2009-07-08 20:55:45 +00001179 // template argument list shall not specify more template-arguments than
Mike Stump1eb44332009-09-09 15:08:12 +00001180 // there are corresponding template-parameters.
1181 TemplateArgumentListBuilder Builder(TemplateParams,
John McCalld5532b62009-11-23 01:53:49 +00001182 ExplicitTemplateArgs.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001183
1184 // Enter a new template instantiation context where we check the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001185 // explicitly-specified template arguments against this function template,
1186 // and then substitute them into the function parameter types.
Mike Stump1eb44332009-09-09 15:08:12 +00001187 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001188 FunctionTemplate, Deduced.data(), Deduced.size(),
1189 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution);
1190 if (Inst)
1191 return TDK_InstantiationDepth;
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Douglas Gregor83314aa2009-07-08 20:55:45 +00001193 if (CheckTemplateArgumentList(FunctionTemplate,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001194 SourceLocation(),
John McCalld5532b62009-11-23 01:53:49 +00001195 ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001196 true,
1197 Builder) || Trap.hasErrorOccurred())
1198 return TDK_InvalidExplicitArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Douglas Gregor83314aa2009-07-08 20:55:45 +00001200 // Form the template argument list from the explicitly-specified
1201 // template arguments.
Mike Stump1eb44332009-09-09 15:08:12 +00001202 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor83314aa2009-07-08 20:55:45 +00001203 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1204 Info.reset(ExplicitArgumentList);
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Douglas Gregor83314aa2009-07-08 20:55:45 +00001206 // Instantiate the types of each of the function parameters given the
1207 // explicitly-specified template arguments.
1208 for (FunctionDecl::param_iterator P = Function->param_begin(),
1209 PEnd = Function->param_end();
1210 P != PEnd;
1211 ++P) {
Mike Stump1eb44332009-09-09 15:08:12 +00001212 QualType ParamType
1213 = SubstType((*P)->getType(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001214 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1215 (*P)->getLocation(), (*P)->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001216 if (ParamType.isNull() || Trap.hasErrorOccurred())
1217 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Douglas Gregor83314aa2009-07-08 20:55:45 +00001219 ParamTypes.push_back(ParamType);
1220 }
1221
1222 // If the caller wants a full function type back, instantiate the return
1223 // type and form that function type.
1224 if (FunctionType) {
1225 // FIXME: exception-specifications?
Mike Stump1eb44332009-09-09 15:08:12 +00001226 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001227 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor83314aa2009-07-08 20:55:45 +00001228 assert(Proto && "Function template does not have a prototype?");
Mike Stump1eb44332009-09-09 15:08:12 +00001229
1230 QualType ResultType
Douglas Gregor357bbd02009-08-28 20:50:45 +00001231 = SubstType(Proto->getResultType(),
1232 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1233 Function->getTypeSpecStartLoc(),
1234 Function->getDeclName());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001235 if (ResultType.isNull() || Trap.hasErrorOccurred())
1236 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001237
1238 *FunctionType = BuildFunctionType(ResultType,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001239 ParamTypes.data(), ParamTypes.size(),
1240 Proto->isVariadic(),
1241 Proto->getTypeQuals(),
1242 Function->getLocation(),
1243 Function->getDeclName());
1244 if (FunctionType->isNull() || Trap.hasErrorOccurred())
1245 return TDK_SubstitutionFailure;
1246 }
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Douglas Gregor83314aa2009-07-08 20:55:45 +00001248 // C++ [temp.arg.explicit]p2:
Mike Stump1eb44332009-09-09 15:08:12 +00001249 // Trailing template arguments that can be deduced (14.8.2) may be
1250 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor83314aa2009-07-08 20:55:45 +00001251 // template arguments can be deduced, they may all be omitted; in this
1252 // case, the empty template argument list <> itself may also be omitted.
1253 //
1254 // Take all of the explicitly-specified arguments and put them into the
Mike Stump1eb44332009-09-09 15:08:12 +00001255 // set of deduced template arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001256 Deduced.reserve(TemplateParams->size());
1257 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001258 Deduced.push_back(ExplicitArgumentList->get(I));
1259
Douglas Gregor83314aa2009-07-08 20:55:45 +00001260 return TDK_Success;
1261}
1262
Mike Stump1eb44332009-09-09 15:08:12 +00001263/// \brief Finish template argument deduction for a function template,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001264/// checking the deduced template arguments for completeness and forming
1265/// the function template specialization.
Mike Stump1eb44332009-09-09 15:08:12 +00001266Sema::TemplateDeductionResult
Douglas Gregor83314aa2009-07-08 20:55:45 +00001267Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
1268 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1269 FunctionDecl *&Specialization,
1270 TemplateDeductionInfo &Info) {
1271 TemplateParameterList *TemplateParams
1272 = FunctionTemplate->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Douglas Gregor83314aa2009-07-08 20:55:45 +00001274 // Template argument deduction for function templates in a SFINAE context.
1275 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001276 SFINAETrap Trap(*this);
1277
Douglas Gregor83314aa2009-07-08 20:55:45 +00001278 // Enter a new template instantiation context while we instantiate the
1279 // actual function declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001280 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
Douglas Gregor83314aa2009-07-08 20:55:45 +00001281 FunctionTemplate, Deduced.data(), Deduced.size(),
1282 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
1283 if (Inst)
Mike Stump1eb44332009-09-09 15:08:12 +00001284 return TDK_InstantiationDepth;
1285
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00001286 // C++ [temp.deduct.type]p2:
1287 // [...] or if any template argument remains neither deduced nor
1288 // explicitly specified, template argument deduction fails.
1289 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
1290 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
1291 if (!Deduced[I].isNull()) {
1292 Builder.Append(Deduced[I]);
1293 continue;
1294 }
1295
1296 // Substitute into the default template argument, if available.
1297 NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I);
1298 TemplateArgumentLoc DefArg
1299 = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
1300 FunctionTemplate->getLocation(),
1301 FunctionTemplate->getSourceRange().getEnd(),
1302 Param,
1303 Builder);
1304
1305 // If there was no default argument, deduction is incomplete.
1306 if (DefArg.getArgument().isNull()) {
1307 Info.Param = makeTemplateParameter(
1308 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1309 return TDK_Incomplete;
1310 }
1311
1312 // Check whether we can actually use the default argument.
1313 if (CheckTemplateArgument(Param, DefArg,
1314 FunctionTemplate,
1315 FunctionTemplate->getLocation(),
1316 FunctionTemplate->getSourceRange().getEnd(),
1317 Builder)) {
1318 Info.Param = makeTemplateParameter(
1319 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1320 return TDK_SubstitutionFailure;
1321 }
1322
1323 // If we get here, we successfully used the default template argument.
1324 }
1325
1326 // Form the template argument list from the deduced template arguments.
1327 TemplateArgumentList *DeducedArgumentList
1328 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1329 Info.reset(DeducedArgumentList);
1330
Mike Stump1eb44332009-09-09 15:08:12 +00001331 // Substitute the deduced template arguments into the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001332 // declaration to produce the function template specialization.
1333 Specialization = cast_or_null<FunctionDecl>(
John McCallce3ff2b2009-08-25 22:02:44 +00001334 SubstDecl(FunctionTemplate->getTemplatedDecl(),
1335 FunctionTemplate->getDeclContext(),
Douglas Gregor357bbd02009-08-28 20:50:45 +00001336 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregor83314aa2009-07-08 20:55:45 +00001337 if (!Specialization)
1338 return TDK_SubstitutionFailure;
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Douglas Gregorf8825742009-09-15 18:26:13 +00001340 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
1341 FunctionTemplate->getCanonicalDecl());
1342
Mike Stump1eb44332009-09-09 15:08:12 +00001343 // If the template argument list is owned by the function template
Douglas Gregor83314aa2009-07-08 20:55:45 +00001344 // specialization, release it.
1345 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList)
1346 Info.take();
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Douglas Gregor83314aa2009-07-08 20:55:45 +00001348 // There may have been an error that did not prevent us from constructing a
1349 // declaration. Mark the declaration invalid and return with a substitution
1350 // failure.
1351 if (Trap.hasErrorOccurred()) {
1352 Specialization->setInvalidDecl(true);
1353 return TDK_SubstitutionFailure;
1354 }
Mike Stump1eb44332009-09-09 15:08:12 +00001355
1356 return TDK_Success;
Douglas Gregor83314aa2009-07-08 20:55:45 +00001357}
1358
Douglas Gregore53060f2009-06-25 22:08:12 +00001359/// \brief Perform template argument deduction from a function call
1360/// (C++ [temp.deduct.call]).
1361///
1362/// \param FunctionTemplate the function template for which we are performing
1363/// template argument deduction.
1364///
Mike Stump1eb44332009-09-09 15:08:12 +00001365/// \param HasExplicitTemplateArgs whether any template arguments were
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001366/// explicitly specified.
1367///
1368/// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
1369/// the explicitly-specified template arguments.
1370///
1371/// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
Mike Stump1eb44332009-09-09 15:08:12 +00001372/// the number of explicitly-specified template arguments in
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001373/// @p ExplicitTemplateArguments. This value may be zero.
1374///
Douglas Gregore53060f2009-06-25 22:08:12 +00001375/// \param Args the function call arguments
1376///
1377/// \param NumArgs the number of arguments in Args
1378///
1379/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001380/// this will be set to the function template specialization produced by
Douglas Gregore53060f2009-06-25 22:08:12 +00001381/// template argument deduction.
1382///
1383/// \param Info the argument will be updated to provide additional information
1384/// about template argument deduction.
1385///
1386/// \returns the result of template argument deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001387Sema::TemplateDeductionResult
1388Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001389 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregore53060f2009-06-25 22:08:12 +00001390 Expr **Args, unsigned NumArgs,
1391 FunctionDecl *&Specialization,
1392 TemplateDeductionInfo &Info) {
1393 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001394
Douglas Gregore53060f2009-06-25 22:08:12 +00001395 // C++ [temp.deduct.call]p1:
1396 // Template argument deduction is done by comparing each function template
1397 // parameter type (call it P) with the type of the corresponding argument
1398 // of the call (call it A) as described below.
1399 unsigned CheckArgs = NumArgs;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001400 if (NumArgs < Function->getMinRequiredArguments())
Douglas Gregore53060f2009-06-25 22:08:12 +00001401 return TDK_TooFewArguments;
1402 else if (NumArgs > Function->getNumParams()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001403 const FunctionProtoType *Proto
John McCall183700f2009-09-21 23:43:11 +00001404 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregore53060f2009-06-25 22:08:12 +00001405 if (!Proto->isVariadic())
1406 return TDK_TooManyArguments;
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Douglas Gregore53060f2009-06-25 22:08:12 +00001408 CheckArgs = Function->getNumParams();
1409 }
Mike Stump1eb44332009-09-09 15:08:12 +00001410
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001411 // The types of the parameters from which we will perform template argument
1412 // deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001413 TemplateParameterList *TemplateParams
1414 = FunctionTemplate->getTemplateParameters();
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001415 llvm::SmallVector<TemplateArgument, 4> Deduced;
1416 llvm::SmallVector<QualType, 4> ParamTypes;
John McCalld5532b62009-11-23 01:53:49 +00001417 if (ExplicitTemplateArgs) {
Douglas Gregor83314aa2009-07-08 20:55:45 +00001418 TemplateDeductionResult Result =
1419 SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001420 *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001421 Deduced,
1422 ParamTypes,
1423 0,
1424 Info);
1425 if (Result)
1426 return Result;
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001427 } else {
1428 // Just fill in the parameter types from the function declaration.
1429 for (unsigned I = 0; I != CheckArgs; ++I)
1430 ParamTypes.push_back(Function->getParamDecl(I)->getType());
1431 }
Mike Stump1eb44332009-09-09 15:08:12 +00001432
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001433 // Deduce template arguments from the function parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001434 Deduced.resize(TemplateParams->size());
Douglas Gregore53060f2009-06-25 22:08:12 +00001435 for (unsigned I = 0; I != CheckArgs; ++I) {
Douglas Gregor6db8ed42009-06-30 23:57:56 +00001436 QualType ParamType = ParamTypes[I];
Douglas Gregore53060f2009-06-25 22:08:12 +00001437 QualType ArgType = Args[I]->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Douglas Gregore53060f2009-06-25 22:08:12 +00001439 // C++ [temp.deduct.call]p2:
1440 // If P is not a reference type:
1441 QualType CanonParamType = Context.getCanonicalType(ParamType);
Douglas Gregor500d3312009-06-26 18:27:22 +00001442 bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1443 if (!ParamWasReference) {
Mike Stump1eb44332009-09-09 15:08:12 +00001444 // - If A is an array type, the pointer type produced by the
1445 // array-to-pointer standard conversion (4.2) is used in place of
Douglas Gregore53060f2009-06-25 22:08:12 +00001446 // A for type deduction; otherwise,
1447 if (ArgType->isArrayType())
1448 ArgType = Context.getArrayDecayedType(ArgType);
Mike Stump1eb44332009-09-09 15:08:12 +00001449 // - If A is a function type, the pointer type produced by the
1450 // function-to-pointer standard conversion (4.3) is used in place
Douglas Gregore53060f2009-06-25 22:08:12 +00001451 // of A for type deduction; otherwise,
1452 else if (ArgType->isFunctionType())
1453 ArgType = Context.getPointerType(ArgType);
1454 else {
1455 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1456 // type are ignored for type deduction.
1457 QualType CanonArgType = Context.getCanonicalType(ArgType);
Douglas Gregora4923eb2009-11-16 21:35:15 +00001458 if (CanonArgType.getLocalCVRQualifiers())
1459 ArgType = CanonArgType.getLocalUnqualifiedType();
Douglas Gregore53060f2009-06-25 22:08:12 +00001460 }
1461 }
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Douglas Gregore53060f2009-06-25 22:08:12 +00001463 // C++0x [temp.deduct.call]p3:
1464 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type
Mike Stump1eb44332009-09-09 15:08:12 +00001465 // are ignored for type deduction.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001466 if (CanonParamType.getLocalCVRQualifiers())
1467 ParamType = CanonParamType.getLocalUnqualifiedType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001468 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001469 // [...] If P is a reference type, the type referred to by P is used
1470 // for type deduction.
Douglas Gregore53060f2009-06-25 22:08:12 +00001471 ParamType = ParamRefType->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +00001472
1473 // [...] If P is of the form T&&, where T is a template parameter, and
1474 // the argument is an lvalue, the type A& is used in place of A for
Douglas Gregore53060f2009-06-25 22:08:12 +00001475 // type deduction.
1476 if (isa<RValueReferenceType>(ParamRefType) &&
John McCall183700f2009-09-21 23:43:11 +00001477 ParamRefType->getAs<TemplateTypeParmType>() &&
Douglas Gregore53060f2009-06-25 22:08:12 +00001478 Args[I]->isLvalue(Context) == Expr::LV_Valid)
1479 ArgType = Context.getLValueReferenceType(ArgType);
1480 }
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Douglas Gregore53060f2009-06-25 22:08:12 +00001482 // C++0x [temp.deduct.call]p4:
1483 // In general, the deduction process attempts to find template argument
1484 // values that will make the deduced A identical to A (after the type A
1485 // is transformed as described above). [...]
Douglas Gregor12820292009-09-14 20:00:47 +00001486 unsigned TDF = TDF_SkipNonDependent;
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Douglas Gregor508f1c82009-06-26 23:10:12 +00001488 // - If the original P is a reference type, the deduced A (i.e., the
1489 // type referred to by the reference) can be more cv-qualified than
1490 // the transformed A.
1491 if (ParamWasReference)
1492 TDF |= TDF_ParamWithReferenceType;
Mike Stump1eb44332009-09-09 15:08:12 +00001493 // - The transformed A can be another pointer or pointer to member
1494 // type that can be converted to the deduced A via a qualification
Douglas Gregor508f1c82009-06-26 23:10:12 +00001495 // conversion (4.4).
1496 if (ArgType->isPointerType() || ArgType->isMemberPointerType())
1497 TDF |= TDF_IgnoreQualifiers;
Mike Stump1eb44332009-09-09 15:08:12 +00001498 // - If P is a class and P has the form simple-template-id, then the
Douglas Gregor41128772009-06-26 23:27:24 +00001499 // transformed A can be a derived class of the deduced A. Likewise,
1500 // if P is a pointer to a class of the form simple-template-id, the
1501 // transformed A can be a pointer to a derived class pointed to by
1502 // the deduced A.
1503 if (isSimpleTemplateIdType(ParamType) ||
Mike Stump1eb44332009-09-09 15:08:12 +00001504 (isa<PointerType>(ParamType) &&
Douglas Gregor41128772009-06-26 23:27:24 +00001505 isSimpleTemplateIdType(
Ted Kremenek6217b802009-07-29 21:53:49 +00001506 ParamType->getAs<PointerType>()->getPointeeType())))
Douglas Gregor41128772009-06-26 23:27:24 +00001507 TDF |= TDF_DerivedClass;
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Douglas Gregore53060f2009-06-25 22:08:12 +00001509 if (TemplateDeductionResult Result
1510 = ::DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor500d3312009-06-26 18:27:22 +00001511 ParamType, ArgType, Info, Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +00001512 TDF))
Douglas Gregore53060f2009-06-25 22:08:12 +00001513 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Douglas Gregor8fdc3c42009-07-07 23:12:18 +00001515 // FIXME: C++0x [temp.deduct.call] paragraphs 6-9 deal with function
Mike Stump1eb44332009-09-09 15:08:12 +00001516 // pointer parameters.
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001517
1518 // FIXME: we need to check that the deduced A is the same as A,
1519 // modulo the various allowed differences.
Douglas Gregore53060f2009-06-25 22:08:12 +00001520 }
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001521
Mike Stump1eb44332009-09-09 15:08:12 +00001522 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001523 Specialization, Info);
Douglas Gregore53060f2009-06-25 22:08:12 +00001524}
1525
Douglas Gregor83314aa2009-07-08 20:55:45 +00001526/// \brief Deduce template arguments when taking the address of a function
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001527/// template (C++ [temp.deduct.funcaddr]) or matching a
Douglas Gregor83314aa2009-07-08 20:55:45 +00001528///
1529/// \param FunctionTemplate the function template for which we are performing
1530/// template argument deduction.
1531///
Mike Stump1eb44332009-09-09 15:08:12 +00001532/// \param HasExplicitTemplateArgs whether any template arguments were
Douglas Gregor83314aa2009-07-08 20:55:45 +00001533/// explicitly specified.
1534///
1535/// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
1536/// the explicitly-specified template arguments.
1537///
1538/// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true,
Mike Stump1eb44332009-09-09 15:08:12 +00001539/// the number of explicitly-specified template arguments in
Douglas Gregor83314aa2009-07-08 20:55:45 +00001540/// @p ExplicitTemplateArguments. This value may be zero.
1541///
1542/// \param ArgFunctionType the function type that will be used as the
1543/// "argument" type (A) when performing template argument deduction from the
1544/// function template's function type.
1545///
1546/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001547/// this will be set to the function template specialization produced by
Douglas Gregor83314aa2009-07-08 20:55:45 +00001548/// template argument deduction.
1549///
1550/// \param Info the argument will be updated to provide additional information
1551/// about template argument deduction.
1552///
1553/// \returns the result of template argument deduction.
1554Sema::TemplateDeductionResult
1555Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001556 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001557 QualType ArgFunctionType,
1558 FunctionDecl *&Specialization,
1559 TemplateDeductionInfo &Info) {
1560 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1561 TemplateParameterList *TemplateParams
1562 = FunctionTemplate->getTemplateParameters();
1563 QualType FunctionType = Function->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Douglas Gregor83314aa2009-07-08 20:55:45 +00001565 // Substitute any explicit template arguments.
1566 llvm::SmallVector<TemplateArgument, 4> Deduced;
1567 llvm::SmallVector<QualType, 4> ParamTypes;
John McCalld5532b62009-11-23 01:53:49 +00001568 if (ExplicitTemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001569 if (TemplateDeductionResult Result
1570 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001571 *ExplicitTemplateArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00001572 Deduced, ParamTypes,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001573 &FunctionType, Info))
1574 return Result;
1575 }
1576
1577 // Template argument deduction for function templates in a SFINAE context.
1578 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001579 SFINAETrap Trap(*this);
1580
Douglas Gregor83314aa2009-07-08 20:55:45 +00001581 // Deduce template arguments from the function type.
Mike Stump1eb44332009-09-09 15:08:12 +00001582 Deduced.resize(TemplateParams->size());
Douglas Gregor83314aa2009-07-08 20:55:45 +00001583 if (TemplateDeductionResult Result
1584 = ::DeduceTemplateArguments(Context, TemplateParams,
Mike Stump1eb44332009-09-09 15:08:12 +00001585 FunctionType, ArgFunctionType, Info,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001586 Deduced, 0))
1587 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001588
1589 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001590 Specialization, Info);
1591}
1592
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001593/// \brief Deduce template arguments for a templated conversion
1594/// function (C++ [temp.deduct.conv]) and, if successful, produce a
1595/// conversion function template specialization.
1596Sema::TemplateDeductionResult
1597Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1598 QualType ToType,
1599 CXXConversionDecl *&Specialization,
1600 TemplateDeductionInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00001601 CXXConversionDecl *Conv
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001602 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
1603 QualType FromType = Conv->getConversionType();
1604
1605 // Canonicalize the types for deduction.
1606 QualType P = Context.getCanonicalType(FromType);
1607 QualType A = Context.getCanonicalType(ToType);
1608
1609 // C++0x [temp.deduct.conv]p3:
1610 // If P is a reference type, the type referred to by P is used for
1611 // type deduction.
1612 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
1613 P = PRef->getPointeeType();
1614
1615 // C++0x [temp.deduct.conv]p3:
1616 // If A is a reference type, the type referred to by A is used
1617 // for type deduction.
1618 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
1619 A = ARef->getPointeeType();
1620 // C++ [temp.deduct.conv]p2:
1621 //
Mike Stump1eb44332009-09-09 15:08:12 +00001622 // If A is not a reference type:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001623 else {
1624 assert(!A->isReferenceType() && "Reference types were handled above");
1625
1626 // - If P is an array type, the pointer type produced by the
Mike Stump1eb44332009-09-09 15:08:12 +00001627 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001628 // of P for type deduction; otherwise,
1629 if (P->isArrayType())
1630 P = Context.getArrayDecayedType(P);
1631 // - If P is a function type, the pointer type produced by the
1632 // function-to-pointer standard conversion (4.3) is used in
1633 // place of P for type deduction; otherwise,
1634 else if (P->isFunctionType())
1635 P = Context.getPointerType(P);
1636 // - If P is a cv-qualified type, the top level cv-qualifiers of
1637 // P’s type are ignored for type deduction.
1638 else
1639 P = P.getUnqualifiedType();
1640
1641 // C++0x [temp.deduct.conv]p3:
1642 // If A is a cv-qualified type, the top level cv-qualifiers of A’s
1643 // type are ignored for type deduction.
1644 A = A.getUnqualifiedType();
1645 }
1646
1647 // Template argument deduction for function templates in a SFINAE context.
1648 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001649 SFINAETrap Trap(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001650
1651 // C++ [temp.deduct.conv]p1:
1652 // Template argument deduction is done by comparing the return
1653 // type of the template conversion function (call it P) with the
1654 // type that is required as the result of the conversion (call it
1655 // A) as described in 14.8.2.4.
1656 TemplateParameterList *TemplateParams
1657 = FunctionTemplate->getTemplateParameters();
1658 llvm::SmallVector<TemplateArgument, 4> Deduced;
Mike Stump1eb44332009-09-09 15:08:12 +00001659 Deduced.resize(TemplateParams->size());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001660
1661 // C++0x [temp.deduct.conv]p4:
1662 // In general, the deduction process attempts to find template
1663 // argument values that will make the deduced A identical to
1664 // A. However, there are two cases that allow a difference:
1665 unsigned TDF = 0;
1666 // - If the original A is a reference type, A can be more
1667 // cv-qualified than the deduced A (i.e., the type referred to
1668 // by the reference)
1669 if (ToType->isReferenceType())
1670 TDF |= TDF_ParamWithReferenceType;
1671 // - The deduced A can be another pointer or pointer to member
1672 // type that can be converted to A via a qualification
1673 // conversion.
1674 //
1675 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
1676 // both P and A are pointers or member pointers. In this case, we
1677 // just ignore cv-qualifiers completely).
1678 if ((P->isPointerType() && A->isPointerType()) ||
1679 (P->isMemberPointerType() && P->isMemberPointerType()))
1680 TDF |= TDF_IgnoreQualifiers;
1681 if (TemplateDeductionResult Result
1682 = ::DeduceTemplateArguments(Context, TemplateParams,
1683 P, A, Info, Deduced, TDF))
1684 return Result;
1685
1686 // FIXME: we need to check that the deduced A is the same as A,
1687 // modulo the various allowed differences.
Mike Stump1eb44332009-09-09 15:08:12 +00001688
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001689 // Finish template argument deduction.
1690 FunctionDecl *Spec = 0;
1691 TemplateDeductionResult Result
1692 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, Spec, Info);
1693 Specialization = cast_or_null<CXXConversionDecl>(Spec);
1694 return Result;
1695}
1696
Douglas Gregor8a514912009-09-14 18:39:43 +00001697/// \brief Stores the result of comparing the qualifiers of two types.
1698enum DeductionQualifierComparison {
1699 NeitherMoreQualified = 0,
1700 ParamMoreQualified,
1701 ArgMoreQualified
1702};
1703
1704/// \brief Deduce the template arguments during partial ordering by comparing
1705/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
1706///
1707/// \param Context the AST context in which this deduction occurs.
1708///
1709/// \param TemplateParams the template parameters that we are deducing
1710///
1711/// \param ParamIn the parameter type
1712///
1713/// \param ArgIn the argument type
1714///
1715/// \param Info information about the template argument deduction itself
1716///
1717/// \param Deduced the deduced template arguments
1718///
1719/// \returns the result of template argument deduction so far. Note that a
1720/// "success" result means that template argument deduction has not yet failed,
1721/// but it may still fail, later, for other reasons.
1722static Sema::TemplateDeductionResult
1723DeduceTemplateArgumentsDuringPartialOrdering(ASTContext &Context,
1724 TemplateParameterList *TemplateParams,
1725 QualType ParamIn, QualType ArgIn,
1726 Sema::TemplateDeductionInfo &Info,
1727 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1728 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1729 CanQualType Param = Context.getCanonicalType(ParamIn);
1730 CanQualType Arg = Context.getCanonicalType(ArgIn);
1731
1732 // C++0x [temp.deduct.partial]p5:
1733 // Before the partial ordering is done, certain transformations are
1734 // performed on the types used for partial ordering:
1735 // - If P is a reference type, P is replaced by the type referred to.
1736 CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00001737 if (!ParamRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00001738 Param = ParamRef->getPointeeType();
1739
1740 // - If A is a reference type, A is replaced by the type referred to.
1741 CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00001742 if (!ArgRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00001743 Arg = ArgRef->getPointeeType();
1744
John McCalle27ec8a2009-10-23 23:03:21 +00001745 if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
Douglas Gregor8a514912009-09-14 18:39:43 +00001746 // C++0x [temp.deduct.partial]p6:
1747 // If both P and A were reference types (before being replaced with the
1748 // type referred to above), determine which of the two types (if any) is
1749 // more cv-qualified than the other; otherwise the types are considered to
1750 // be equally cv-qualified for partial ordering purposes. The result of this
1751 // determination will be used below.
1752 //
1753 // We save this information for later, using it only when deduction
1754 // succeeds in both directions.
1755 DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
1756 if (Param.isMoreQualifiedThan(Arg))
1757 QualifierResult = ParamMoreQualified;
1758 else if (Arg.isMoreQualifiedThan(Param))
1759 QualifierResult = ArgMoreQualified;
1760 QualifierComparisons->push_back(QualifierResult);
1761 }
1762
1763 // C++0x [temp.deduct.partial]p7:
1764 // Remove any top-level cv-qualifiers:
1765 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
1766 // version of P.
1767 Param = Param.getUnqualifiedType();
1768 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
1769 // version of A.
1770 Arg = Arg.getUnqualifiedType();
1771
1772 // C++0x [temp.deduct.partial]p8:
1773 // Using the resulting types P and A the deduction is then done as
1774 // described in 14.9.2.5. If deduction succeeds for a given type, the type
1775 // from the argument template is considered to be at least as specialized
1776 // as the type from the parameter template.
1777 return DeduceTemplateArguments(Context, TemplateParams, Param, Arg, Info,
1778 Deduced, TDF_None);
1779}
1780
1781static void
Douglas Gregore73bb602009-09-14 21:25:05 +00001782MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
1783 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001784 unsigned Level,
Douglas Gregore73bb602009-09-14 21:25:05 +00001785 llvm::SmallVectorImpl<bool> &Deduced);
Douglas Gregor8a514912009-09-14 18:39:43 +00001786
1787/// \brief Determine whether the function template \p FT1 is at least as
1788/// specialized as \p FT2.
1789static bool isAtLeastAsSpecializedAs(Sema &S,
1790 FunctionTemplateDecl *FT1,
1791 FunctionTemplateDecl *FT2,
1792 TemplatePartialOrderingContext TPOC,
1793 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1794 FunctionDecl *FD1 = FT1->getTemplatedDecl();
1795 FunctionDecl *FD2 = FT2->getTemplatedDecl();
1796 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
1797 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
1798
1799 assert(Proto1 && Proto2 && "Function templates must have prototypes");
1800 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
1801 llvm::SmallVector<TemplateArgument, 4> Deduced;
1802 Deduced.resize(TemplateParams->size());
1803
1804 // C++0x [temp.deduct.partial]p3:
1805 // The types used to determine the ordering depend on the context in which
1806 // the partial ordering is done:
1807 Sema::TemplateDeductionInfo Info(S.Context);
1808 switch (TPOC) {
1809 case TPOC_Call: {
1810 // - In the context of a function call, the function parameter types are
1811 // used.
1812 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
1813 for (unsigned I = 0; I != NumParams; ++I)
1814 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1815 TemplateParams,
1816 Proto2->getArgType(I),
1817 Proto1->getArgType(I),
1818 Info,
1819 Deduced,
1820 QualifierComparisons))
1821 return false;
1822
1823 break;
1824 }
1825
1826 case TPOC_Conversion:
1827 // - In the context of a call to a conversion operator, the return types
1828 // of the conversion function templates are used.
1829 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1830 TemplateParams,
1831 Proto2->getResultType(),
1832 Proto1->getResultType(),
1833 Info,
1834 Deduced,
1835 QualifierComparisons))
1836 return false;
1837 break;
1838
1839 case TPOC_Other:
1840 // - In other contexts (14.6.6.2) the function template’s function type
1841 // is used.
1842 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1843 TemplateParams,
1844 FD2->getType(),
1845 FD1->getType(),
1846 Info,
1847 Deduced,
1848 QualifierComparisons))
1849 return false;
1850 break;
1851 }
1852
1853 // C++0x [temp.deduct.partial]p11:
1854 // In most cases, all template parameters must have values in order for
1855 // deduction to succeed, but for partial ordering purposes a template
1856 // parameter may remain without a value provided it is not used in the
1857 // types being used for partial ordering. [ Note: a template parameter used
1858 // in a non-deduced context is considered used. -end note]
1859 unsigned ArgIdx = 0, NumArgs = Deduced.size();
1860 for (; ArgIdx != NumArgs; ++ArgIdx)
1861 if (Deduced[ArgIdx].isNull())
1862 break;
1863
1864 if (ArgIdx == NumArgs) {
1865 // All template arguments were deduced. FT1 is at least as specialized
1866 // as FT2.
1867 return true;
1868 }
1869
Douglas Gregore73bb602009-09-14 21:25:05 +00001870 // Figure out which template parameters were used.
Douglas Gregor8a514912009-09-14 18:39:43 +00001871 llvm::SmallVector<bool, 4> UsedParameters;
1872 UsedParameters.resize(TemplateParams->size());
1873 switch (TPOC) {
1874 case TPOC_Call: {
1875 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
1876 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00001877 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
1878 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00001879 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00001880 break;
1881 }
1882
1883 case TPOC_Conversion:
Douglas Gregored9c0f92009-10-29 00:04:11 +00001884 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
1885 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00001886 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00001887 break;
1888
1889 case TPOC_Other:
Douglas Gregored9c0f92009-10-29 00:04:11 +00001890 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
1891 TemplateParams->getDepth(),
1892 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00001893 break;
1894 }
1895
1896 for (; ArgIdx != NumArgs; ++ArgIdx)
1897 // If this argument had no value deduced but was used in one of the types
1898 // used for partial ordering, then deduction fails.
1899 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
1900 return false;
1901
1902 return true;
1903}
1904
1905
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001906/// \brief Returns the more specialized function template according
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001907/// to the rules of function template partial ordering (C++ [temp.func.order]).
1908///
1909/// \param FT1 the first function template
1910///
1911/// \param FT2 the second function template
1912///
Douglas Gregor8a514912009-09-14 18:39:43 +00001913/// \param TPOC the context in which we are performing partial ordering of
1914/// function templates.
Mike Stump1eb44332009-09-09 15:08:12 +00001915///
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001916/// \returns the more specialized function template. If neither
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001917/// template is more specialized, returns NULL.
1918FunctionTemplateDecl *
1919Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
1920 FunctionTemplateDecl *FT2,
Douglas Gregor8a514912009-09-14 18:39:43 +00001921 TemplatePartialOrderingContext TPOC) {
Douglas Gregor8a514912009-09-14 18:39:43 +00001922 llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
1923 bool Better1 = isAtLeastAsSpecializedAs(*this, FT1, FT2, TPOC, 0);
1924 bool Better2 = isAtLeastAsSpecializedAs(*this, FT2, FT1, TPOC,
1925 &QualifierComparisons);
1926
1927 if (Better1 != Better2) // We have a clear winner
1928 return Better1? FT1 : FT2;
1929
1930 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001931 return 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00001932
1933
1934 // C++0x [temp.deduct.partial]p10:
1935 // If for each type being considered a given template is at least as
1936 // specialized for all types and more specialized for some set of types and
1937 // the other template is not more specialized for any types or is not at
1938 // least as specialized for any types, then the given template is more
1939 // specialized than the other template. Otherwise, neither template is more
1940 // specialized than the other.
1941 Better1 = false;
1942 Better2 = false;
1943 for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
1944 // C++0x [temp.deduct.partial]p9:
1945 // If, for a given type, deduction succeeds in both directions (i.e., the
1946 // types are identical after the transformations above) and if the type
1947 // from the argument template is more cv-qualified than the type from the
1948 // parameter template (as described above) that type is considered to be
1949 // more specialized than the other. If neither type is more cv-qualified
1950 // than the other then neither type is more specialized than the other.
1951 switch (QualifierComparisons[I]) {
1952 case NeitherMoreQualified:
1953 break;
1954
1955 case ParamMoreQualified:
1956 Better1 = true;
1957 if (Better2)
1958 return 0;
1959 break;
1960
1961 case ArgMoreQualified:
1962 Better2 = true;
1963 if (Better1)
1964 return 0;
1965 break;
1966 }
1967 }
1968
1969 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001970 if (Better1)
1971 return FT1;
Douglas Gregor8a514912009-09-14 18:39:43 +00001972 else if (Better2)
1973 return FT2;
1974 else
1975 return 0;
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001976}
Douglas Gregor83314aa2009-07-08 20:55:45 +00001977
Douglas Gregord5a423b2009-09-25 18:43:00 +00001978/// \brief Determine if the two templates are equivalent.
1979static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
1980 if (T1 == T2)
1981 return true;
1982
1983 if (!T1 || !T2)
1984 return false;
1985
1986 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
1987}
1988
1989/// \brief Retrieve the most specialized of the given function template
1990/// specializations.
1991///
1992/// \param Specializations the set of function template specializations that
1993/// we will be comparing.
1994///
1995/// \param NumSpecializations the number of function template specializations in
1996/// \p Specializations
1997///
1998/// \param TPOC the partial ordering context to use to compare the function
1999/// template specializations.
2000///
2001/// \param Loc the location where the ambiguity or no-specializations
2002/// diagnostic should occur.
2003///
2004/// \param NoneDiag partial diagnostic used to diagnose cases where there are
2005/// no matching candidates.
2006///
2007/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
2008/// occurs.
2009///
2010/// \param CandidateDiag partial diagnostic used for each function template
2011/// specialization that is a candidate in the ambiguous ordering. One parameter
2012/// in this diagnostic should be unbound, which will correspond to the string
2013/// describing the template arguments for the function template specialization.
2014///
2015/// \param Index if non-NULL and the result of this function is non-nULL,
2016/// receives the index corresponding to the resulting function template
2017/// specialization.
2018///
2019/// \returns the most specialized function template specialization, if
2020/// found. Otherwise, returns NULL.
2021///
2022/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
2023/// template argument deduction.
2024FunctionDecl *Sema::getMostSpecialized(FunctionDecl **Specializations,
2025 unsigned NumSpecializations,
2026 TemplatePartialOrderingContext TPOC,
2027 SourceLocation Loc,
2028 const PartialDiagnostic &NoneDiag,
2029 const PartialDiagnostic &AmbigDiag,
2030 const PartialDiagnostic &CandidateDiag,
2031 unsigned *Index) {
2032 if (NumSpecializations == 0) {
2033 Diag(Loc, NoneDiag);
2034 return 0;
2035 }
2036
2037 if (NumSpecializations == 1) {
2038 if (Index)
2039 *Index = 0;
2040
2041 return Specializations[0];
2042 }
2043
2044
2045 // Find the function template that is better than all of the templates it
2046 // has been compared to.
2047 unsigned Best = 0;
2048 FunctionTemplateDecl *BestTemplate
2049 = Specializations[Best]->getPrimaryTemplate();
2050 assert(BestTemplate && "Not a function template specialization?");
2051 for (unsigned I = 1; I != NumSpecializations; ++I) {
2052 FunctionTemplateDecl *Challenger = Specializations[I]->getPrimaryTemplate();
2053 assert(Challenger && "Not a function template specialization?");
2054 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
2055 TPOC),
2056 Challenger)) {
2057 Best = I;
2058 BestTemplate = Challenger;
2059 }
2060 }
2061
2062 // Make sure that the "best" function template is more specialized than all
2063 // of the others.
2064 bool Ambiguous = false;
2065 for (unsigned I = 0; I != NumSpecializations; ++I) {
2066 FunctionTemplateDecl *Challenger = Specializations[I]->getPrimaryTemplate();
2067 if (I != Best &&
2068 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
2069 TPOC),
2070 BestTemplate)) {
2071 Ambiguous = true;
2072 break;
2073 }
2074 }
2075
2076 if (!Ambiguous) {
2077 // We found an answer. Return it.
2078 if (Index)
2079 *Index = Best;
2080 return Specializations[Best];
2081 }
2082
2083 // Diagnose the ambiguity.
2084 Diag(Loc, AmbigDiag);
2085
2086 // FIXME: Can we order the candidates in some sane way?
2087 for (unsigned I = 0; I != NumSpecializations; ++I)
2088 Diag(Specializations[I]->getLocation(), CandidateDiag)
2089 << getTemplateArgumentBindingsText(
2090 Specializations[I]->getPrimaryTemplate()->getTemplateParameters(),
2091 *Specializations[I]->getTemplateSpecializationArgs());
2092
2093 return 0;
2094}
2095
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002096/// \brief Returns the more specialized class template partial specialization
2097/// according to the rules of partial ordering of class template partial
2098/// specializations (C++ [temp.class.order]).
2099///
2100/// \param PS1 the first class template partial specialization
2101///
2102/// \param PS2 the second class template partial specialization
2103///
2104/// \returns the more specialized class template partial specialization. If
2105/// neither partial specialization is more specialized, returns NULL.
2106ClassTemplatePartialSpecializationDecl *
2107Sema::getMoreSpecializedPartialSpecialization(
2108 ClassTemplatePartialSpecializationDecl *PS1,
2109 ClassTemplatePartialSpecializationDecl *PS2) {
2110 // C++ [temp.class.order]p1:
2111 // For two class template partial specializations, the first is at least as
2112 // specialized as the second if, given the following rewrite to two
2113 // function templates, the first function template is at least as
2114 // specialized as the second according to the ordering rules for function
2115 // templates (14.6.6.2):
2116 // - the first function template has the same template parameters as the
2117 // first partial specialization and has a single function parameter
2118 // whose type is a class template specialization with the template
2119 // arguments of the first partial specialization, and
2120 // - the second function template has the same template parameters as the
2121 // second partial specialization and has a single function parameter
2122 // whose type is a class template specialization with the template
2123 // arguments of the second partial specialization.
2124 //
2125 // Rather than synthesize function templates, we merely perform the
2126 // equivalent partial ordering by performing deduction directly on the
2127 // template arguments of the class template partial specializations. This
2128 // computation is slightly simpler than the general problem of function
2129 // template partial ordering, because class template partial specializations
2130 // are more constrained. We know that every template parameter is deduc
2131 llvm::SmallVector<TemplateArgument, 4> Deduced;
2132 Sema::TemplateDeductionInfo Info(Context);
2133
2134 // Determine whether PS1 is at least as specialized as PS2
2135 Deduced.resize(PS2->getTemplateParameters()->size());
2136 bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(Context,
2137 PS2->getTemplateParameters(),
2138 Context.getTypeDeclType(PS2),
2139 Context.getTypeDeclType(PS1),
2140 Info,
2141 Deduced,
2142 0);
2143
2144 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregordb0d4b72009-11-11 23:06:43 +00002145 Deduced.clear();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002146 Deduced.resize(PS1->getTemplateParameters()->size());
2147 bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(Context,
2148 PS1->getTemplateParameters(),
2149 Context.getTypeDeclType(PS1),
2150 Context.getTypeDeclType(PS2),
2151 Info,
2152 Deduced,
2153 0);
2154
2155 if (Better1 == Better2)
2156 return 0;
2157
2158 return Better1? PS1 : PS2;
2159}
2160
Mike Stump1eb44332009-09-09 15:08:12 +00002161static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002162MarkUsedTemplateParameters(Sema &SemaRef,
2163 const TemplateArgument &TemplateArg,
2164 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002165 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002166 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002167
Douglas Gregore73bb602009-09-14 21:25:05 +00002168/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002169/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00002170static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002171MarkUsedTemplateParameters(Sema &SemaRef,
2172 const Expr *E,
2173 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002174 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002175 llvm::SmallVectorImpl<bool> &Used) {
2176 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2177 // find other occurrences of template parameters.
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002178 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregor031a5882009-06-13 00:26:55 +00002179 if (!E)
2180 return;
2181
Mike Stump1eb44332009-09-09 15:08:12 +00002182 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00002183 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2184 if (!NTTP)
2185 return;
2186
Douglas Gregored9c0f92009-10-29 00:04:11 +00002187 if (NTTP->getDepth() == Depth)
2188 Used[NTTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002189}
2190
Douglas Gregore73bb602009-09-14 21:25:05 +00002191/// \brief Mark the template parameters that are used by the given
2192/// nested name specifier.
2193static void
2194MarkUsedTemplateParameters(Sema &SemaRef,
2195 NestedNameSpecifier *NNS,
2196 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002197 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002198 llvm::SmallVectorImpl<bool> &Used) {
2199 if (!NNS)
2200 return;
2201
Douglas Gregored9c0f92009-10-29 00:04:11 +00002202 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2203 Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002204 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002205 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002206}
2207
2208/// \brief Mark the template parameters that are used by the given
2209/// template name.
2210static void
2211MarkUsedTemplateParameters(Sema &SemaRef,
2212 TemplateName Name,
2213 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002214 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002215 llvm::SmallVectorImpl<bool> &Used) {
2216 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2217 if (TemplateTemplateParmDecl *TTP
Douglas Gregored9c0f92009-10-29 00:04:11 +00002218 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2219 if (TTP->getDepth() == Depth)
2220 Used[TTP->getIndex()] = true;
2221 }
Douglas Gregore73bb602009-09-14 21:25:05 +00002222 return;
2223 }
2224
Douglas Gregor788cd062009-11-11 01:00:40 +00002225 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2226 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2227 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002228 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
Douglas Gregored9c0f92009-10-29 00:04:11 +00002229 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2230 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002231}
2232
2233/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002234/// type.
Mike Stump1eb44332009-09-09 15:08:12 +00002235static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002236MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2237 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002238 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002239 llvm::SmallVectorImpl<bool> &Used) {
2240 if (T.isNull())
2241 return;
2242
Douglas Gregor031a5882009-06-13 00:26:55 +00002243 // Non-dependent types have nothing deducible
2244 if (!T->isDependentType())
2245 return;
2246
2247 T = SemaRef.Context.getCanonicalType(T);
2248 switch (T->getTypeClass()) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002249 case Type::Pointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002250 MarkUsedTemplateParameters(SemaRef,
2251 cast<PointerType>(T)->getPointeeType(),
2252 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002253 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002254 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002255 break;
2256
2257 case Type::BlockPointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002258 MarkUsedTemplateParameters(SemaRef,
2259 cast<BlockPointerType>(T)->getPointeeType(),
2260 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002261 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002262 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002263 break;
2264
2265 case Type::LValueReference:
2266 case Type::RValueReference:
Douglas Gregore73bb602009-09-14 21:25:05 +00002267 MarkUsedTemplateParameters(SemaRef,
2268 cast<ReferenceType>(T)->getPointeeType(),
2269 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002270 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002271 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002272 break;
2273
2274 case Type::MemberPointer: {
2275 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore73bb602009-09-14 21:25:05 +00002276 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002277 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002278 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002279 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002280 break;
2281 }
2282
2283 case Type::DependentSizedArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002284 MarkUsedTemplateParameters(SemaRef,
2285 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002286 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002287 // Fall through to check the element type
2288
2289 case Type::ConstantArray:
2290 case Type::IncompleteArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002291 MarkUsedTemplateParameters(SemaRef,
2292 cast<ArrayType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002293 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002294 break;
2295
2296 case Type::Vector:
2297 case Type::ExtVector:
Douglas Gregore73bb602009-09-14 21:25:05 +00002298 MarkUsedTemplateParameters(SemaRef,
2299 cast<VectorType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002300 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002301 break;
2302
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002303 case Type::DependentSizedExtVector: {
2304 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002305 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002306 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002307 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002308 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002309 Depth, Used);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002310 break;
2311 }
2312
Douglas Gregor031a5882009-06-13 00:26:55 +00002313 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002314 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002315 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002316 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002317 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore73bb602009-09-14 21:25:05 +00002318 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002319 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002320 break;
2321 }
2322
Douglas Gregored9c0f92009-10-29 00:04:11 +00002323 case Type::TemplateTypeParm: {
2324 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2325 if (TTP->getDepth() == Depth)
2326 Used[TTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002327 break;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002328 }
Douglas Gregor031a5882009-06-13 00:26:55 +00002329
2330 case Type::TemplateSpecialization: {
Mike Stump1eb44332009-09-09 15:08:12 +00002331 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002332 = cast<TemplateSpecializationType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002333 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002334 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002335 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002336 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2337 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002338 break;
2339 }
2340
Douglas Gregore73bb602009-09-14 21:25:05 +00002341 case Type::Complex:
2342 if (!OnlyDeduced)
2343 MarkUsedTemplateParameters(SemaRef,
2344 cast<ComplexType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002345 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002346 break;
2347
2348 case Type::Typename:
2349 if (!OnlyDeduced)
2350 MarkUsedTemplateParameters(SemaRef,
2351 cast<TypenameType>(T)->getQualifier(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002352 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002353 break;
2354
2355 // None of these types have any template parameters in them.
Douglas Gregor031a5882009-06-13 00:26:55 +00002356 case Type::Builtin:
2357 case Type::FixedWidthInt:
Douglas Gregor031a5882009-06-13 00:26:55 +00002358 case Type::VariableArray:
2359 case Type::FunctionNoProto:
2360 case Type::Record:
2361 case Type::Enum:
Douglas Gregor031a5882009-06-13 00:26:55 +00002362 case Type::ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002363 case Type::ObjCObjectPointer:
John McCalled976492009-12-04 22:46:56 +00002364 case Type::UnresolvedUsing:
Douglas Gregor031a5882009-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 Gregore73bb602009-09-14 21:25:05 +00002374/// \brief Mark the template parameters that are used by this
Douglas Gregor031a5882009-06-13 00:26:55 +00002375/// template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00002376static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002377MarkUsedTemplateParameters(Sema &SemaRef,
2378 const TemplateArgument &TemplateArg,
2379 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002380 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002381 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002382 switch (TemplateArg.getKind()) {
2383 case TemplateArgument::Null:
2384 case TemplateArgument::Integral:
Douglas Gregor788cd062009-11-11 01:00:40 +00002385 case TemplateArgument::Declaration:
Douglas Gregor031a5882009-06-13 00:26:55 +00002386 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002387
Douglas Gregor031a5882009-06-13 00:26:55 +00002388 case TemplateArgument::Type:
Douglas Gregore73bb602009-09-14 21:25:05 +00002389 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002390 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002391 break;
2392
Douglas Gregor788cd062009-11-11 01:00:40 +00002393 case TemplateArgument::Template:
2394 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2395 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002396 break;
2397
2398 case TemplateArgument::Expression:
Douglas Gregore73bb602009-09-14 21:25:05 +00002399 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002400 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002401 break;
Douglas Gregore73bb602009-09-14 21:25:05 +00002402
Anders Carlssond01b1da2009-06-15 17:04:53 +00002403 case TemplateArgument::Pack:
Douglas Gregore73bb602009-09-14 21:25:05 +00002404 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2405 PEnd = TemplateArg.pack_end();
2406 P != PEnd; ++P)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002407 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssond01b1da2009-06-15 17:04:53 +00002408 break;
Douglas Gregor031a5882009-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 Stump1eb44332009-09-09 15:08:12 +00002421void
Douglas Gregore73bb602009-09-14 21:25:05 +00002422Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002423 bool OnlyDeduced, unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002424 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002425 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002426 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2427 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002428}
Douglas Gregor63f07c52009-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 Gregored9c0f92009-10-29 00:04:11 +00002442 true, TemplateParams->getDepth(), Deduced);
Douglas Gregor63f07c52009-09-18 23:21:38 +00002443}