blob: 590a75174bf3a31b27a8d03afe85fa43a83230a2 [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 Gregor4b52e252009-12-21 23:17:24 +00001509 // FIXME: C++0x [temp.deduct.call] paragraphs 6-9 deal with function
1510 // pointer parameters.
1511
1512 if (Context.hasSameUnqualifiedType(ArgType, Context.OverloadTy)) {
1513 // We know that template argument deduction will fail if the argument is
1514 // still an overloaded function. Check whether we can resolve this
1515 // argument as a single function template specialization per
1516 // C++ [temp.arg.explicit]p3.
1517 FunctionDecl *ExplicitSpec
1518 = ResolveSingleFunctionTemplateSpecialization(Args[I]);
1519 Expr *ResolvedArg = 0;
1520 if (ExplicitSpec)
1521 ResolvedArg = FixOverloadedFunctionReference(Args[I], ExplicitSpec);
1522 if (!ExplicitSpec || !ResolvedArg) {
1523 // Template argument deduction fails if we can't resolve the overloaded
1524 // function.
1525 return TDK_FailedOverloadResolution;
1526 }
1527
1528 // Get the type of the resolved argument.
1529 ArgType = ResolvedArg->getType();
1530 if (ArgType->isPointerType() || ArgType->isMemberPointerType())
1531 TDF |= TDF_IgnoreQualifiers;
1532
1533 ResolvedArg->Destroy(Context);
1534 }
1535
Douglas Gregore53060f2009-06-25 22:08:12 +00001536 if (TemplateDeductionResult Result
1537 = ::DeduceTemplateArguments(Context, TemplateParams,
Douglas Gregor500d3312009-06-26 18:27:22 +00001538 ParamType, ArgType, Info, Deduced,
Douglas Gregor508f1c82009-06-26 23:10:12 +00001539 TDF))
Douglas Gregore53060f2009-06-25 22:08:12 +00001540 return Result;
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001542 // FIXME: we need to check that the deduced A is the same as A,
1543 // modulo the various allowed differences.
Douglas Gregore53060f2009-06-25 22:08:12 +00001544 }
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001545
Mike Stump1eb44332009-09-09 15:08:12 +00001546 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001547 Specialization, Info);
Douglas Gregore53060f2009-06-25 22:08:12 +00001548}
1549
Douglas Gregor83314aa2009-07-08 20:55:45 +00001550/// \brief Deduce template arguments when taking the address of a function
Douglas Gregor4b52e252009-12-21 23:17:24 +00001551/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
1552/// a template.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001553///
1554/// \param FunctionTemplate the function template for which we are performing
1555/// template argument deduction.
1556///
Douglas Gregor4b52e252009-12-21 23:17:24 +00001557/// \param ExplicitTemplateArguments the explicitly-specified template
1558/// arguments.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001559///
1560/// \param ArgFunctionType the function type that will be used as the
1561/// "argument" type (A) when performing template argument deduction from the
Douglas Gregor4b52e252009-12-21 23:17:24 +00001562/// function template's function type. This type may be NULL, if there is no
1563/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
Douglas Gregor83314aa2009-07-08 20:55:45 +00001564///
1565/// \param Specialization if template argument deduction was successful,
Mike Stump1eb44332009-09-09 15:08:12 +00001566/// this will be set to the function template specialization produced by
Douglas Gregor83314aa2009-07-08 20:55:45 +00001567/// template argument deduction.
1568///
1569/// \param Info the argument will be updated to provide additional information
1570/// about template argument deduction.
1571///
1572/// \returns the result of template argument deduction.
1573Sema::TemplateDeductionResult
1574Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001575 const TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001576 QualType ArgFunctionType,
1577 FunctionDecl *&Specialization,
1578 TemplateDeductionInfo &Info) {
1579 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
1580 TemplateParameterList *TemplateParams
1581 = FunctionTemplate->getTemplateParameters();
1582 QualType FunctionType = Function->getType();
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Douglas Gregor83314aa2009-07-08 20:55:45 +00001584 // Substitute any explicit template arguments.
1585 llvm::SmallVector<TemplateArgument, 4> Deduced;
1586 llvm::SmallVector<QualType, 4> ParamTypes;
John McCalld5532b62009-11-23 01:53:49 +00001587 if (ExplicitTemplateArgs) {
Mike Stump1eb44332009-09-09 15:08:12 +00001588 if (TemplateDeductionResult Result
1589 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCalld5532b62009-11-23 01:53:49 +00001590 *ExplicitTemplateArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00001591 Deduced, ParamTypes,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001592 &FunctionType, Info))
1593 return Result;
1594 }
1595
1596 // Template argument deduction for function templates in a SFINAE context.
1597 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001598 SFINAETrap Trap(*this);
1599
Douglas Gregor4b52e252009-12-21 23:17:24 +00001600 if (!ArgFunctionType.isNull()) {
1601 // Deduce template arguments from the function type.
1602 Deduced.resize(TemplateParams->size());
1603 if (TemplateDeductionResult Result
1604 = ::DeduceTemplateArguments(Context, TemplateParams,
1605 FunctionType, ArgFunctionType, Info,
1606 Deduced, 0))
1607 return Result;
1608 }
1609
Mike Stump1eb44332009-09-09 15:08:12 +00001610 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregor83314aa2009-07-08 20:55:45 +00001611 Specialization, Info);
1612}
1613
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001614/// \brief Deduce template arguments for a templated conversion
1615/// function (C++ [temp.deduct.conv]) and, if successful, produce a
1616/// conversion function template specialization.
1617Sema::TemplateDeductionResult
1618Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1619 QualType ToType,
1620 CXXConversionDecl *&Specialization,
1621 TemplateDeductionInfo &Info) {
Mike Stump1eb44332009-09-09 15:08:12 +00001622 CXXConversionDecl *Conv
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001623 = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
1624 QualType FromType = Conv->getConversionType();
1625
1626 // Canonicalize the types for deduction.
1627 QualType P = Context.getCanonicalType(FromType);
1628 QualType A = Context.getCanonicalType(ToType);
1629
1630 // C++0x [temp.deduct.conv]p3:
1631 // If P is a reference type, the type referred to by P is used for
1632 // type deduction.
1633 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
1634 P = PRef->getPointeeType();
1635
1636 // C++0x [temp.deduct.conv]p3:
1637 // If A is a reference type, the type referred to by A is used
1638 // for type deduction.
1639 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
1640 A = ARef->getPointeeType();
1641 // C++ [temp.deduct.conv]p2:
1642 //
Mike Stump1eb44332009-09-09 15:08:12 +00001643 // If A is not a reference type:
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001644 else {
1645 assert(!A->isReferenceType() && "Reference types were handled above");
1646
1647 // - If P is an array type, the pointer type produced by the
Mike Stump1eb44332009-09-09 15:08:12 +00001648 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001649 // of P for type deduction; otherwise,
1650 if (P->isArrayType())
1651 P = Context.getArrayDecayedType(P);
1652 // - If P is a function type, the pointer type produced by the
1653 // function-to-pointer standard conversion (4.3) is used in
1654 // place of P for type deduction; otherwise,
1655 else if (P->isFunctionType())
1656 P = Context.getPointerType(P);
1657 // - If P is a cv-qualified type, the top level cv-qualifiers of
1658 // P’s type are ignored for type deduction.
1659 else
1660 P = P.getUnqualifiedType();
1661
1662 // C++0x [temp.deduct.conv]p3:
1663 // If A is a cv-qualified type, the top level cv-qualifiers of A’s
1664 // type are ignored for type deduction.
1665 A = A.getUnqualifiedType();
1666 }
1667
1668 // Template argument deduction for function templates in a SFINAE context.
1669 // Trap any errors that might occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001670 SFINAETrap Trap(*this);
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001671
1672 // C++ [temp.deduct.conv]p1:
1673 // Template argument deduction is done by comparing the return
1674 // type of the template conversion function (call it P) with the
1675 // type that is required as the result of the conversion (call it
1676 // A) as described in 14.8.2.4.
1677 TemplateParameterList *TemplateParams
1678 = FunctionTemplate->getTemplateParameters();
1679 llvm::SmallVector<TemplateArgument, 4> Deduced;
Mike Stump1eb44332009-09-09 15:08:12 +00001680 Deduced.resize(TemplateParams->size());
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001681
1682 // C++0x [temp.deduct.conv]p4:
1683 // In general, the deduction process attempts to find template
1684 // argument values that will make the deduced A identical to
1685 // A. However, there are two cases that allow a difference:
1686 unsigned TDF = 0;
1687 // - If the original A is a reference type, A can be more
1688 // cv-qualified than the deduced A (i.e., the type referred to
1689 // by the reference)
1690 if (ToType->isReferenceType())
1691 TDF |= TDF_ParamWithReferenceType;
1692 // - The deduced A can be another pointer or pointer to member
1693 // type that can be converted to A via a qualification
1694 // conversion.
1695 //
1696 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
1697 // both P and A are pointers or member pointers. In this case, we
1698 // just ignore cv-qualifiers completely).
1699 if ((P->isPointerType() && A->isPointerType()) ||
1700 (P->isMemberPointerType() && P->isMemberPointerType()))
1701 TDF |= TDF_IgnoreQualifiers;
1702 if (TemplateDeductionResult Result
1703 = ::DeduceTemplateArguments(Context, TemplateParams,
1704 P, A, Info, Deduced, TDF))
1705 return Result;
1706
1707 // FIXME: we need to check that the deduced A is the same as A,
1708 // modulo the various allowed differences.
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001710 // Finish template argument deduction.
1711 FunctionDecl *Spec = 0;
1712 TemplateDeductionResult Result
1713 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, Spec, Info);
1714 Specialization = cast_or_null<CXXConversionDecl>(Spec);
1715 return Result;
1716}
1717
Douglas Gregor4b52e252009-12-21 23:17:24 +00001718/// \brief Deduce template arguments for a function template when there is
1719/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
1720///
1721/// \param FunctionTemplate the function template for which we are performing
1722/// template argument deduction.
1723///
1724/// \param ExplicitTemplateArguments the explicitly-specified template
1725/// arguments.
1726///
1727/// \param Specialization if template argument deduction was successful,
1728/// this will be set to the function template specialization produced by
1729/// template argument deduction.
1730///
1731/// \param Info the argument will be updated to provide additional information
1732/// about template argument deduction.
1733///
1734/// \returns the result of template argument deduction.
1735Sema::TemplateDeductionResult
1736Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1737 const TemplateArgumentListInfo *ExplicitTemplateArgs,
1738 FunctionDecl *&Specialization,
1739 TemplateDeductionInfo &Info) {
1740 return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
1741 QualType(), Specialization, Info);
1742}
1743
Douglas Gregor8a514912009-09-14 18:39:43 +00001744/// \brief Stores the result of comparing the qualifiers of two types.
1745enum DeductionQualifierComparison {
1746 NeitherMoreQualified = 0,
1747 ParamMoreQualified,
1748 ArgMoreQualified
1749};
1750
1751/// \brief Deduce the template arguments during partial ordering by comparing
1752/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
1753///
1754/// \param Context the AST context in which this deduction occurs.
1755///
1756/// \param TemplateParams the template parameters that we are deducing
1757///
1758/// \param ParamIn the parameter type
1759///
1760/// \param ArgIn the argument type
1761///
1762/// \param Info information about the template argument deduction itself
1763///
1764/// \param Deduced the deduced template arguments
1765///
1766/// \returns the result of template argument deduction so far. Note that a
1767/// "success" result means that template argument deduction has not yet failed,
1768/// but it may still fail, later, for other reasons.
1769static Sema::TemplateDeductionResult
1770DeduceTemplateArgumentsDuringPartialOrdering(ASTContext &Context,
1771 TemplateParameterList *TemplateParams,
1772 QualType ParamIn, QualType ArgIn,
1773 Sema::TemplateDeductionInfo &Info,
1774 llvm::SmallVectorImpl<TemplateArgument> &Deduced,
1775 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1776 CanQualType Param = Context.getCanonicalType(ParamIn);
1777 CanQualType Arg = Context.getCanonicalType(ArgIn);
1778
1779 // C++0x [temp.deduct.partial]p5:
1780 // Before the partial ordering is done, certain transformations are
1781 // performed on the types used for partial ordering:
1782 // - If P is a reference type, P is replaced by the type referred to.
1783 CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00001784 if (!ParamRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00001785 Param = ParamRef->getPointeeType();
1786
1787 // - If A is a reference type, A is replaced by the type referred to.
1788 CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
John McCalle27ec8a2009-10-23 23:03:21 +00001789 if (!ArgRef.isNull())
Douglas Gregor8a514912009-09-14 18:39:43 +00001790 Arg = ArgRef->getPointeeType();
1791
John McCalle27ec8a2009-10-23 23:03:21 +00001792 if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
Douglas Gregor8a514912009-09-14 18:39:43 +00001793 // C++0x [temp.deduct.partial]p6:
1794 // If both P and A were reference types (before being replaced with the
1795 // type referred to above), determine which of the two types (if any) is
1796 // more cv-qualified than the other; otherwise the types are considered to
1797 // be equally cv-qualified for partial ordering purposes. The result of this
1798 // determination will be used below.
1799 //
1800 // We save this information for later, using it only when deduction
1801 // succeeds in both directions.
1802 DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
1803 if (Param.isMoreQualifiedThan(Arg))
1804 QualifierResult = ParamMoreQualified;
1805 else if (Arg.isMoreQualifiedThan(Param))
1806 QualifierResult = ArgMoreQualified;
1807 QualifierComparisons->push_back(QualifierResult);
1808 }
1809
1810 // C++0x [temp.deduct.partial]p7:
1811 // Remove any top-level cv-qualifiers:
1812 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
1813 // version of P.
1814 Param = Param.getUnqualifiedType();
1815 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
1816 // version of A.
1817 Arg = Arg.getUnqualifiedType();
1818
1819 // C++0x [temp.deduct.partial]p8:
1820 // Using the resulting types P and A the deduction is then done as
1821 // described in 14.9.2.5. If deduction succeeds for a given type, the type
1822 // from the argument template is considered to be at least as specialized
1823 // as the type from the parameter template.
1824 return DeduceTemplateArguments(Context, TemplateParams, Param, Arg, Info,
1825 Deduced, TDF_None);
1826}
1827
1828static void
Douglas Gregore73bb602009-09-14 21:25:05 +00001829MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
1830 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00001831 unsigned Level,
Douglas Gregore73bb602009-09-14 21:25:05 +00001832 llvm::SmallVectorImpl<bool> &Deduced);
Douglas Gregor8a514912009-09-14 18:39:43 +00001833
1834/// \brief Determine whether the function template \p FT1 is at least as
1835/// specialized as \p FT2.
1836static bool isAtLeastAsSpecializedAs(Sema &S,
1837 FunctionTemplateDecl *FT1,
1838 FunctionTemplateDecl *FT2,
1839 TemplatePartialOrderingContext TPOC,
1840 llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1841 FunctionDecl *FD1 = FT1->getTemplatedDecl();
1842 FunctionDecl *FD2 = FT2->getTemplatedDecl();
1843 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
1844 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
1845
1846 assert(Proto1 && Proto2 && "Function templates must have prototypes");
1847 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
1848 llvm::SmallVector<TemplateArgument, 4> Deduced;
1849 Deduced.resize(TemplateParams->size());
1850
1851 // C++0x [temp.deduct.partial]p3:
1852 // The types used to determine the ordering depend on the context in which
1853 // the partial ordering is done:
1854 Sema::TemplateDeductionInfo Info(S.Context);
1855 switch (TPOC) {
1856 case TPOC_Call: {
1857 // - In the context of a function call, the function parameter types are
1858 // used.
1859 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
1860 for (unsigned I = 0; I != NumParams; ++I)
1861 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1862 TemplateParams,
1863 Proto2->getArgType(I),
1864 Proto1->getArgType(I),
1865 Info,
1866 Deduced,
1867 QualifierComparisons))
1868 return false;
1869
1870 break;
1871 }
1872
1873 case TPOC_Conversion:
1874 // - In the context of a call to a conversion operator, the return types
1875 // of the conversion function templates are used.
1876 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1877 TemplateParams,
1878 Proto2->getResultType(),
1879 Proto1->getResultType(),
1880 Info,
1881 Deduced,
1882 QualifierComparisons))
1883 return false;
1884 break;
1885
1886 case TPOC_Other:
1887 // - In other contexts (14.6.6.2) the function template’s function type
1888 // is used.
1889 if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context,
1890 TemplateParams,
1891 FD2->getType(),
1892 FD1->getType(),
1893 Info,
1894 Deduced,
1895 QualifierComparisons))
1896 return false;
1897 break;
1898 }
1899
1900 // C++0x [temp.deduct.partial]p11:
1901 // In most cases, all template parameters must have values in order for
1902 // deduction to succeed, but for partial ordering purposes a template
1903 // parameter may remain without a value provided it is not used in the
1904 // types being used for partial ordering. [ Note: a template parameter used
1905 // in a non-deduced context is considered used. -end note]
1906 unsigned ArgIdx = 0, NumArgs = Deduced.size();
1907 for (; ArgIdx != NumArgs; ++ArgIdx)
1908 if (Deduced[ArgIdx].isNull())
1909 break;
1910
1911 if (ArgIdx == NumArgs) {
1912 // All template arguments were deduced. FT1 is at least as specialized
1913 // as FT2.
1914 return true;
1915 }
1916
Douglas Gregore73bb602009-09-14 21:25:05 +00001917 // Figure out which template parameters were used.
Douglas Gregor8a514912009-09-14 18:39:43 +00001918 llvm::SmallVector<bool, 4> UsedParameters;
1919 UsedParameters.resize(TemplateParams->size());
1920 switch (TPOC) {
1921 case TPOC_Call: {
1922 unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
1923 for (unsigned I = 0; I != NumParams; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00001924 ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
1925 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00001926 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00001927 break;
1928 }
1929
1930 case TPOC_Conversion:
Douglas Gregored9c0f92009-10-29 00:04:11 +00001931 ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
1932 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00001933 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00001934 break;
1935
1936 case TPOC_Other:
Douglas Gregored9c0f92009-10-29 00:04:11 +00001937 ::MarkUsedTemplateParameters(S, FD2->getType(), false,
1938 TemplateParams->getDepth(),
1939 UsedParameters);
Douglas Gregor8a514912009-09-14 18:39:43 +00001940 break;
1941 }
1942
1943 for (; ArgIdx != NumArgs; ++ArgIdx)
1944 // If this argument had no value deduced but was used in one of the types
1945 // used for partial ordering, then deduction fails.
1946 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
1947 return false;
1948
1949 return true;
1950}
1951
1952
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001953/// \brief Returns the more specialized function template according
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001954/// to the rules of function template partial ordering (C++ [temp.func.order]).
1955///
1956/// \param FT1 the first function template
1957///
1958/// \param FT2 the second function template
1959///
Douglas Gregor8a514912009-09-14 18:39:43 +00001960/// \param TPOC the context in which we are performing partial ordering of
1961/// function templates.
Mike Stump1eb44332009-09-09 15:08:12 +00001962///
Douglas Gregorbf4ea562009-09-15 16:23:51 +00001963/// \returns the more specialized function template. If neither
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001964/// template is more specialized, returns NULL.
1965FunctionTemplateDecl *
1966Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
1967 FunctionTemplateDecl *FT2,
Douglas Gregor8a514912009-09-14 18:39:43 +00001968 TemplatePartialOrderingContext TPOC) {
Douglas Gregor8a514912009-09-14 18:39:43 +00001969 llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
1970 bool Better1 = isAtLeastAsSpecializedAs(*this, FT1, FT2, TPOC, 0);
1971 bool Better2 = isAtLeastAsSpecializedAs(*this, FT2, FT1, TPOC,
1972 &QualifierComparisons);
1973
1974 if (Better1 != Better2) // We have a clear winner
1975 return Better1? FT1 : FT2;
1976
1977 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00001978 return 0;
Douglas Gregor8a514912009-09-14 18:39:43 +00001979
1980
1981 // C++0x [temp.deduct.partial]p10:
1982 // If for each type being considered a given template is at least as
1983 // specialized for all types and more specialized for some set of types and
1984 // the other template is not more specialized for any types or is not at
1985 // least as specialized for any types, then the given template is more
1986 // specialized than the other template. Otherwise, neither template is more
1987 // specialized than the other.
1988 Better1 = false;
1989 Better2 = false;
1990 for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
1991 // C++0x [temp.deduct.partial]p9:
1992 // If, for a given type, deduction succeeds in both directions (i.e., the
1993 // types are identical after the transformations above) and if the type
1994 // from the argument template is more cv-qualified than the type from the
1995 // parameter template (as described above) that type is considered to be
1996 // more specialized than the other. If neither type is more cv-qualified
1997 // than the other then neither type is more specialized than the other.
1998 switch (QualifierComparisons[I]) {
1999 case NeitherMoreQualified:
2000 break;
2001
2002 case ParamMoreQualified:
2003 Better1 = true;
2004 if (Better2)
2005 return 0;
2006 break;
2007
2008 case ArgMoreQualified:
2009 Better2 = true;
2010 if (Better1)
2011 return 0;
2012 break;
2013 }
2014 }
2015
2016 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002017 if (Better1)
2018 return FT1;
Douglas Gregor8a514912009-09-14 18:39:43 +00002019 else if (Better2)
2020 return FT2;
2021 else
2022 return 0;
Douglas Gregor65ec1fd2009-08-21 23:19:43 +00002023}
Douglas Gregor83314aa2009-07-08 20:55:45 +00002024
Douglas Gregord5a423b2009-09-25 18:43:00 +00002025/// \brief Determine if the two templates are equivalent.
2026static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
2027 if (T1 == T2)
2028 return true;
2029
2030 if (!T1 || !T2)
2031 return false;
2032
2033 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
2034}
2035
2036/// \brief Retrieve the most specialized of the given function template
2037/// specializations.
2038///
2039/// \param Specializations the set of function template specializations that
2040/// we will be comparing.
2041///
2042/// \param NumSpecializations the number of function template specializations in
2043/// \p Specializations
2044///
2045/// \param TPOC the partial ordering context to use to compare the function
2046/// template specializations.
2047///
2048/// \param Loc the location where the ambiguity or no-specializations
2049/// diagnostic should occur.
2050///
2051/// \param NoneDiag partial diagnostic used to diagnose cases where there are
2052/// no matching candidates.
2053///
2054/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
2055/// occurs.
2056///
2057/// \param CandidateDiag partial diagnostic used for each function template
2058/// specialization that is a candidate in the ambiguous ordering. One parameter
2059/// in this diagnostic should be unbound, which will correspond to the string
2060/// describing the template arguments for the function template specialization.
2061///
2062/// \param Index if non-NULL and the result of this function is non-nULL,
2063/// receives the index corresponding to the resulting function template
2064/// specialization.
2065///
2066/// \returns the most specialized function template specialization, if
2067/// found. Otherwise, returns NULL.
2068///
2069/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
2070/// template argument deduction.
2071FunctionDecl *Sema::getMostSpecialized(FunctionDecl **Specializations,
2072 unsigned NumSpecializations,
2073 TemplatePartialOrderingContext TPOC,
2074 SourceLocation Loc,
2075 const PartialDiagnostic &NoneDiag,
2076 const PartialDiagnostic &AmbigDiag,
2077 const PartialDiagnostic &CandidateDiag,
2078 unsigned *Index) {
2079 if (NumSpecializations == 0) {
2080 Diag(Loc, NoneDiag);
2081 return 0;
2082 }
2083
2084 if (NumSpecializations == 1) {
2085 if (Index)
2086 *Index = 0;
2087
2088 return Specializations[0];
2089 }
2090
2091
2092 // Find the function template that is better than all of the templates it
2093 // has been compared to.
2094 unsigned Best = 0;
2095 FunctionTemplateDecl *BestTemplate
2096 = Specializations[Best]->getPrimaryTemplate();
2097 assert(BestTemplate && "Not a function template specialization?");
2098 for (unsigned I = 1; I != NumSpecializations; ++I) {
2099 FunctionTemplateDecl *Challenger = Specializations[I]->getPrimaryTemplate();
2100 assert(Challenger && "Not a function template specialization?");
2101 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
2102 TPOC),
2103 Challenger)) {
2104 Best = I;
2105 BestTemplate = Challenger;
2106 }
2107 }
2108
2109 // Make sure that the "best" function template is more specialized than all
2110 // of the others.
2111 bool Ambiguous = false;
2112 for (unsigned I = 0; I != NumSpecializations; ++I) {
2113 FunctionTemplateDecl *Challenger = Specializations[I]->getPrimaryTemplate();
2114 if (I != Best &&
2115 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
2116 TPOC),
2117 BestTemplate)) {
2118 Ambiguous = true;
2119 break;
2120 }
2121 }
2122
2123 if (!Ambiguous) {
2124 // We found an answer. Return it.
2125 if (Index)
2126 *Index = Best;
2127 return Specializations[Best];
2128 }
2129
2130 // Diagnose the ambiguity.
2131 Diag(Loc, AmbigDiag);
2132
2133 // FIXME: Can we order the candidates in some sane way?
2134 for (unsigned I = 0; I != NumSpecializations; ++I)
2135 Diag(Specializations[I]->getLocation(), CandidateDiag)
2136 << getTemplateArgumentBindingsText(
2137 Specializations[I]->getPrimaryTemplate()->getTemplateParameters(),
2138 *Specializations[I]->getTemplateSpecializationArgs());
2139
2140 return 0;
2141}
2142
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002143/// \brief Returns the more specialized class template partial specialization
2144/// according to the rules of partial ordering of class template partial
2145/// specializations (C++ [temp.class.order]).
2146///
2147/// \param PS1 the first class template partial specialization
2148///
2149/// \param PS2 the second class template partial specialization
2150///
2151/// \returns the more specialized class template partial specialization. If
2152/// neither partial specialization is more specialized, returns NULL.
2153ClassTemplatePartialSpecializationDecl *
2154Sema::getMoreSpecializedPartialSpecialization(
2155 ClassTemplatePartialSpecializationDecl *PS1,
2156 ClassTemplatePartialSpecializationDecl *PS2) {
2157 // C++ [temp.class.order]p1:
2158 // For two class template partial specializations, the first is at least as
2159 // specialized as the second if, given the following rewrite to two
2160 // function templates, the first function template is at least as
2161 // specialized as the second according to the ordering rules for function
2162 // templates (14.6.6.2):
2163 // - the first function template has the same template parameters as the
2164 // first partial specialization and has a single function parameter
2165 // whose type is a class template specialization with the template
2166 // arguments of the first partial specialization, and
2167 // - the second function template has the same template parameters as the
2168 // second partial specialization and has a single function parameter
2169 // whose type is a class template specialization with the template
2170 // arguments of the second partial specialization.
2171 //
2172 // Rather than synthesize function templates, we merely perform the
2173 // equivalent partial ordering by performing deduction directly on the
2174 // template arguments of the class template partial specializations. This
2175 // computation is slightly simpler than the general problem of function
2176 // template partial ordering, because class template partial specializations
2177 // are more constrained. We know that every template parameter is deduc
2178 llvm::SmallVector<TemplateArgument, 4> Deduced;
2179 Sema::TemplateDeductionInfo Info(Context);
2180
2181 // Determine whether PS1 is at least as specialized as PS2
2182 Deduced.resize(PS2->getTemplateParameters()->size());
2183 bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(Context,
2184 PS2->getTemplateParameters(),
2185 Context.getTypeDeclType(PS2),
2186 Context.getTypeDeclType(PS1),
2187 Info,
2188 Deduced,
2189 0);
2190
2191 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregordb0d4b72009-11-11 23:06:43 +00002192 Deduced.clear();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00002193 Deduced.resize(PS1->getTemplateParameters()->size());
2194 bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(Context,
2195 PS1->getTemplateParameters(),
2196 Context.getTypeDeclType(PS1),
2197 Context.getTypeDeclType(PS2),
2198 Info,
2199 Deduced,
2200 0);
2201
2202 if (Better1 == Better2)
2203 return 0;
2204
2205 return Better1? PS1 : PS2;
2206}
2207
Mike Stump1eb44332009-09-09 15:08:12 +00002208static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002209MarkUsedTemplateParameters(Sema &SemaRef,
2210 const TemplateArgument &TemplateArg,
2211 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002212 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002213 llvm::SmallVectorImpl<bool> &Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002214
Douglas Gregore73bb602009-09-14 21:25:05 +00002215/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002216/// expression.
Mike Stump1eb44332009-09-09 15:08:12 +00002217static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002218MarkUsedTemplateParameters(Sema &SemaRef,
2219 const Expr *E,
2220 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002221 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002222 llvm::SmallVectorImpl<bool> &Used) {
2223 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2224 // find other occurrences of template parameters.
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002225 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregor031a5882009-06-13 00:26:55 +00002226 if (!E)
2227 return;
2228
Mike Stump1eb44332009-09-09 15:08:12 +00002229 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor031a5882009-06-13 00:26:55 +00002230 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2231 if (!NTTP)
2232 return;
2233
Douglas Gregored9c0f92009-10-29 00:04:11 +00002234 if (NTTP->getDepth() == Depth)
2235 Used[NTTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002236}
2237
Douglas Gregore73bb602009-09-14 21:25:05 +00002238/// \brief Mark the template parameters that are used by the given
2239/// nested name specifier.
2240static void
2241MarkUsedTemplateParameters(Sema &SemaRef,
2242 NestedNameSpecifier *NNS,
2243 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002244 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002245 llvm::SmallVectorImpl<bool> &Used) {
2246 if (!NNS)
2247 return;
2248
Douglas Gregored9c0f92009-10-29 00:04:11 +00002249 MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2250 Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002251 MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002252 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002253}
2254
2255/// \brief Mark the template parameters that are used by the given
2256/// template name.
2257static void
2258MarkUsedTemplateParameters(Sema &SemaRef,
2259 TemplateName Name,
2260 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002261 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002262 llvm::SmallVectorImpl<bool> &Used) {
2263 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2264 if (TemplateTemplateParmDecl *TTP
Douglas Gregored9c0f92009-10-29 00:04:11 +00002265 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2266 if (TTP->getDepth() == Depth)
2267 Used[TTP->getIndex()] = true;
2268 }
Douglas Gregore73bb602009-09-14 21:25:05 +00002269 return;
2270 }
2271
Douglas Gregor788cd062009-11-11 01:00:40 +00002272 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2273 MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2274 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002275 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
Douglas Gregored9c0f92009-10-29 00:04:11 +00002276 MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2277 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002278}
2279
2280/// \brief Mark the template parameters that are used by the given
Douglas Gregor031a5882009-06-13 00:26:55 +00002281/// type.
Mike Stump1eb44332009-09-09 15:08:12 +00002282static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002283MarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2284 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002285 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002286 llvm::SmallVectorImpl<bool> &Used) {
2287 if (T.isNull())
2288 return;
2289
Douglas Gregor031a5882009-06-13 00:26:55 +00002290 // Non-dependent types have nothing deducible
2291 if (!T->isDependentType())
2292 return;
2293
2294 T = SemaRef.Context.getCanonicalType(T);
2295 switch (T->getTypeClass()) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002296 case Type::Pointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002297 MarkUsedTemplateParameters(SemaRef,
2298 cast<PointerType>(T)->getPointeeType(),
2299 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002300 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002301 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002302 break;
2303
2304 case Type::BlockPointer:
Douglas Gregore73bb602009-09-14 21:25:05 +00002305 MarkUsedTemplateParameters(SemaRef,
2306 cast<BlockPointerType>(T)->getPointeeType(),
2307 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002308 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002309 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002310 break;
2311
2312 case Type::LValueReference:
2313 case Type::RValueReference:
Douglas Gregore73bb602009-09-14 21:25:05 +00002314 MarkUsedTemplateParameters(SemaRef,
2315 cast<ReferenceType>(T)->getPointeeType(),
2316 OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002317 Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002318 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002319 break;
2320
2321 case Type::MemberPointer: {
2322 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Douglas Gregore73bb602009-09-14 21:25:05 +00002323 MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002324 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002325 MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002326 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002327 break;
2328 }
2329
2330 case Type::DependentSizedArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002331 MarkUsedTemplateParameters(SemaRef,
2332 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002333 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002334 // Fall through to check the element type
2335
2336 case Type::ConstantArray:
2337 case Type::IncompleteArray:
Douglas Gregore73bb602009-09-14 21:25:05 +00002338 MarkUsedTemplateParameters(SemaRef,
2339 cast<ArrayType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002340 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002341 break;
2342
2343 case Type::Vector:
2344 case Type::ExtVector:
Douglas Gregore73bb602009-09-14 21:25:05 +00002345 MarkUsedTemplateParameters(SemaRef,
2346 cast<VectorType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002347 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002348 break;
2349
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002350 case Type::DependentSizedExtVector: {
2351 const DependentSizedExtVectorType *VecType
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002352 = cast<DependentSizedExtVectorType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002353 MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002354 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002355 MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002356 Depth, Used);
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002357 break;
2358 }
2359
Douglas Gregor031a5882009-06-13 00:26:55 +00002360 case Type::FunctionProto: {
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002361 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002362 MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002363 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002364 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
Douglas Gregore73bb602009-09-14 21:25:05 +00002365 MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002366 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002367 break;
2368 }
2369
Douglas Gregored9c0f92009-10-29 00:04:11 +00002370 case Type::TemplateTypeParm: {
2371 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2372 if (TTP->getDepth() == Depth)
2373 Used[TTP->getIndex()] = true;
Douglas Gregor031a5882009-06-13 00:26:55 +00002374 break;
Douglas Gregored9c0f92009-10-29 00:04:11 +00002375 }
Douglas Gregor031a5882009-06-13 00:26:55 +00002376
2377 case Type::TemplateSpecialization: {
Mike Stump1eb44332009-09-09 15:08:12 +00002378 const TemplateSpecializationType *Spec
Douglas Gregorf6ddb732009-06-18 18:45:36 +00002379 = cast<TemplateSpecializationType>(T);
Douglas Gregore73bb602009-09-14 21:25:05 +00002380 MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002381 Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002382 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002383 MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2384 Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002385 break;
2386 }
2387
Douglas Gregore73bb602009-09-14 21:25:05 +00002388 case Type::Complex:
2389 if (!OnlyDeduced)
2390 MarkUsedTemplateParameters(SemaRef,
2391 cast<ComplexType>(T)->getElementType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002392 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002393 break;
2394
2395 case Type::Typename:
2396 if (!OnlyDeduced)
2397 MarkUsedTemplateParameters(SemaRef,
2398 cast<TypenameType>(T)->getQualifier(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002399 OnlyDeduced, Depth, Used);
Douglas Gregore73bb602009-09-14 21:25:05 +00002400 break;
2401
2402 // None of these types have any template parameters in them.
Douglas Gregor031a5882009-06-13 00:26:55 +00002403 case Type::Builtin:
Douglas Gregor031a5882009-06-13 00:26:55 +00002404 case Type::VariableArray:
2405 case Type::FunctionNoProto:
2406 case Type::Record:
2407 case Type::Enum:
Douglas Gregor031a5882009-06-13 00:26:55 +00002408 case Type::ObjCInterface:
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00002409 case Type::ObjCObjectPointer:
John McCalled976492009-12-04 22:46:56 +00002410 case Type::UnresolvedUsing:
Douglas Gregor031a5882009-06-13 00:26:55 +00002411#define TYPE(Class, Base)
2412#define ABSTRACT_TYPE(Class, Base)
2413#define DEPENDENT_TYPE(Class, Base)
2414#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2415#include "clang/AST/TypeNodes.def"
2416 break;
2417 }
2418}
2419
Douglas Gregore73bb602009-09-14 21:25:05 +00002420/// \brief Mark the template parameters that are used by this
Douglas Gregor031a5882009-06-13 00:26:55 +00002421/// template argument.
Mike Stump1eb44332009-09-09 15:08:12 +00002422static void
Douglas Gregore73bb602009-09-14 21:25:05 +00002423MarkUsedTemplateParameters(Sema &SemaRef,
2424 const TemplateArgument &TemplateArg,
2425 bool OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002426 unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002427 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002428 switch (TemplateArg.getKind()) {
2429 case TemplateArgument::Null:
2430 case TemplateArgument::Integral:
Douglas Gregor788cd062009-11-11 01:00:40 +00002431 case TemplateArgument::Declaration:
Douglas Gregor031a5882009-06-13 00:26:55 +00002432 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002433
Douglas Gregor031a5882009-06-13 00:26:55 +00002434 case TemplateArgument::Type:
Douglas Gregore73bb602009-09-14 21:25:05 +00002435 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002436 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002437 break;
2438
Douglas Gregor788cd062009-11-11 01:00:40 +00002439 case TemplateArgument::Template:
2440 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2441 OnlyDeduced, Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002442 break;
2443
2444 case TemplateArgument::Expression:
Douglas Gregore73bb602009-09-14 21:25:05 +00002445 MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002446 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002447 break;
Douglas Gregore73bb602009-09-14 21:25:05 +00002448
Anders Carlssond01b1da2009-06-15 17:04:53 +00002449 case TemplateArgument::Pack:
Douglas Gregore73bb602009-09-14 21:25:05 +00002450 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2451 PEnd = TemplateArg.pack_end();
2452 P != PEnd; ++P)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002453 MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
Anders Carlssond01b1da2009-06-15 17:04:53 +00002454 break;
Douglas Gregor031a5882009-06-13 00:26:55 +00002455 }
2456}
2457
2458/// \brief Mark the template parameters can be deduced by the given
2459/// template argument list.
2460///
2461/// \param TemplateArgs the template argument list from which template
2462/// parameters will be deduced.
2463///
2464/// \param Deduced a bit vector whose elements will be set to \c true
2465/// to indicate when the corresponding template parameter will be
2466/// deduced.
Mike Stump1eb44332009-09-09 15:08:12 +00002467void
Douglas Gregore73bb602009-09-14 21:25:05 +00002468Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregored9c0f92009-10-29 00:04:11 +00002469 bool OnlyDeduced, unsigned Depth,
Douglas Gregore73bb602009-09-14 21:25:05 +00002470 llvm::SmallVectorImpl<bool> &Used) {
Douglas Gregor031a5882009-06-13 00:26:55 +00002471 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
Douglas Gregored9c0f92009-10-29 00:04:11 +00002472 ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2473 Depth, Used);
Douglas Gregor031a5882009-06-13 00:26:55 +00002474}
Douglas Gregor63f07c52009-09-18 23:21:38 +00002475
2476/// \brief Marks all of the template parameters that will be deduced by a
2477/// call to the given function template.
2478void Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
2479 llvm::SmallVectorImpl<bool> &Deduced) {
2480 TemplateParameterList *TemplateParams
2481 = FunctionTemplate->getTemplateParameters();
2482 Deduced.clear();
2483 Deduced.resize(TemplateParams->size());
2484
2485 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2486 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
2487 ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
Douglas Gregored9c0f92009-10-29 00:04:11 +00002488 true, TemplateParams->getDepth(), Deduced);
Douglas Gregor63f07c52009-09-18 23:21:38 +00002489}