xref: /minix3/external/bsd/llvm/dist/clang/lib/Sema/SemaTemplateDeduction.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===/
8f4a2713aSLionel Sambuc //
9f4a2713aSLionel Sambuc //  This file implements C++ template argument deduction.
10f4a2713aSLionel Sambuc //
11f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===/
12f4a2713aSLionel Sambuc 
13f4a2713aSLionel Sambuc #include "clang/Sema/TemplateDeduction.h"
14f4a2713aSLionel Sambuc #include "TreeTransform.h"
15f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
16f4a2713aSLionel Sambuc #include "clang/AST/ASTLambda.h"
17f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h"
18f4a2713aSLionel Sambuc #include "clang/AST/DeclTemplate.h"
19f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
20f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
21f4a2713aSLionel Sambuc #include "clang/AST/StmtVisitor.h"
22f4a2713aSLionel Sambuc #include "clang/Sema/DeclSpec.h"
23f4a2713aSLionel Sambuc #include "clang/Sema/Sema.h"
24f4a2713aSLionel Sambuc #include "clang/Sema/Template.h"
25f4a2713aSLionel Sambuc #include "llvm/ADT/SmallBitVector.h"
26f4a2713aSLionel Sambuc #include <algorithm>
27f4a2713aSLionel Sambuc 
28f4a2713aSLionel Sambuc namespace clang {
29f4a2713aSLionel Sambuc   using namespace sema;
30f4a2713aSLionel Sambuc   /// \brief Various flags that control template argument deduction.
31f4a2713aSLionel Sambuc   ///
32f4a2713aSLionel Sambuc   /// These flags can be bitwise-OR'd together.
33f4a2713aSLionel Sambuc   enum TemplateDeductionFlags {
34f4a2713aSLionel Sambuc     /// \brief No template argument deduction flags, which indicates the
35f4a2713aSLionel Sambuc     /// strictest results for template argument deduction (as used for, e.g.,
36f4a2713aSLionel Sambuc     /// matching class template partial specializations).
37f4a2713aSLionel Sambuc     TDF_None = 0,
38f4a2713aSLionel Sambuc     /// \brief Within template argument deduction from a function call, we are
39f4a2713aSLionel Sambuc     /// matching with a parameter type for which the original parameter was
40f4a2713aSLionel Sambuc     /// a reference.
41f4a2713aSLionel Sambuc     TDF_ParamWithReferenceType = 0x1,
42f4a2713aSLionel Sambuc     /// \brief Within template argument deduction from a function call, we
43f4a2713aSLionel Sambuc     /// are matching in a case where we ignore cv-qualifiers.
44f4a2713aSLionel Sambuc     TDF_IgnoreQualifiers = 0x02,
45f4a2713aSLionel Sambuc     /// \brief Within template argument deduction from a function call,
46f4a2713aSLionel Sambuc     /// we are matching in a case where we can perform template argument
47f4a2713aSLionel Sambuc     /// deduction from a template-id of a derived class of the argument type.
48f4a2713aSLionel Sambuc     TDF_DerivedClass = 0x04,
49f4a2713aSLionel Sambuc     /// \brief Allow non-dependent types to differ, e.g., when performing
50f4a2713aSLionel Sambuc     /// template argument deduction from a function call where conversions
51f4a2713aSLionel Sambuc     /// may apply.
52f4a2713aSLionel Sambuc     TDF_SkipNonDependent = 0x08,
53f4a2713aSLionel Sambuc     /// \brief Whether we are performing template argument deduction for
54f4a2713aSLionel Sambuc     /// parameters and arguments in a top-level template argument
55f4a2713aSLionel Sambuc     TDF_TopLevelParameterTypeList = 0x10,
56f4a2713aSLionel Sambuc     /// \brief Within template argument deduction from overload resolution per
57f4a2713aSLionel Sambuc     /// C++ [over.over] allow matching function types that are compatible in
58f4a2713aSLionel Sambuc     /// terms of noreturn and default calling convention adjustments.
59f4a2713aSLionel Sambuc     TDF_InOverloadResolution = 0x20
60f4a2713aSLionel Sambuc   };
61f4a2713aSLionel Sambuc }
62f4a2713aSLionel Sambuc 
63f4a2713aSLionel Sambuc using namespace clang;
64f4a2713aSLionel Sambuc 
65f4a2713aSLionel Sambuc /// \brief Compare two APSInts, extending and switching the sign as
66f4a2713aSLionel Sambuc /// necessary to compare their values regardless of underlying type.
hasSameExtendedValue(llvm::APSInt X,llvm::APSInt Y)67f4a2713aSLionel Sambuc static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) {
68f4a2713aSLionel Sambuc   if (Y.getBitWidth() > X.getBitWidth())
69f4a2713aSLionel Sambuc     X = X.extend(Y.getBitWidth());
70f4a2713aSLionel Sambuc   else if (Y.getBitWidth() < X.getBitWidth())
71f4a2713aSLionel Sambuc     Y = Y.extend(X.getBitWidth());
72f4a2713aSLionel Sambuc 
73f4a2713aSLionel Sambuc   // If there is a signedness mismatch, correct it.
74f4a2713aSLionel Sambuc   if (X.isSigned() != Y.isSigned()) {
75f4a2713aSLionel Sambuc     // If the signed value is negative, then the values cannot be the same.
76f4a2713aSLionel Sambuc     if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative()))
77f4a2713aSLionel Sambuc       return false;
78f4a2713aSLionel Sambuc 
79f4a2713aSLionel Sambuc     Y.setIsSigned(true);
80f4a2713aSLionel Sambuc     X.setIsSigned(true);
81f4a2713aSLionel Sambuc   }
82f4a2713aSLionel Sambuc 
83f4a2713aSLionel Sambuc   return X == Y;
84f4a2713aSLionel Sambuc }
85f4a2713aSLionel Sambuc 
86f4a2713aSLionel Sambuc static Sema::TemplateDeductionResult
87f4a2713aSLionel Sambuc DeduceTemplateArguments(Sema &S,
88f4a2713aSLionel Sambuc                         TemplateParameterList *TemplateParams,
89f4a2713aSLionel Sambuc                         const TemplateArgument &Param,
90f4a2713aSLionel Sambuc                         TemplateArgument Arg,
91f4a2713aSLionel Sambuc                         TemplateDeductionInfo &Info,
92f4a2713aSLionel Sambuc                         SmallVectorImpl<DeducedTemplateArgument> &Deduced);
93f4a2713aSLionel Sambuc 
94f4a2713aSLionel Sambuc /// \brief Whether template argument deduction for two reference parameters
95f4a2713aSLionel Sambuc /// resulted in the argument type, parameter type, or neither type being more
96f4a2713aSLionel Sambuc /// qualified than the other.
97f4a2713aSLionel Sambuc enum DeductionQualifierComparison {
98f4a2713aSLionel Sambuc   NeitherMoreQualified = 0,
99f4a2713aSLionel Sambuc   ParamMoreQualified,
100f4a2713aSLionel Sambuc   ArgMoreQualified
101f4a2713aSLionel Sambuc };
102f4a2713aSLionel Sambuc 
103f4a2713aSLionel Sambuc /// \brief Stores the result of comparing two reference parameters while
104f4a2713aSLionel Sambuc /// performing template argument deduction for partial ordering of function
105f4a2713aSLionel Sambuc /// templates.
106f4a2713aSLionel Sambuc struct RefParamPartialOrderingComparison {
107f4a2713aSLionel Sambuc   /// \brief Whether the parameter type is an rvalue reference type.
108f4a2713aSLionel Sambuc   bool ParamIsRvalueRef;
109f4a2713aSLionel Sambuc   /// \brief Whether the argument type is an rvalue reference type.
110f4a2713aSLionel Sambuc   bool ArgIsRvalueRef;
111f4a2713aSLionel Sambuc 
112f4a2713aSLionel Sambuc   /// \brief Whether the parameter or argument (or neither) is more qualified.
113f4a2713aSLionel Sambuc   DeductionQualifierComparison Qualifiers;
114f4a2713aSLionel Sambuc };
115f4a2713aSLionel Sambuc 
116f4a2713aSLionel Sambuc 
117f4a2713aSLionel Sambuc 
118f4a2713aSLionel Sambuc static Sema::TemplateDeductionResult
119f4a2713aSLionel Sambuc DeduceTemplateArgumentsByTypeMatch(Sema &S,
120f4a2713aSLionel Sambuc                                    TemplateParameterList *TemplateParams,
121f4a2713aSLionel Sambuc                                    QualType Param,
122f4a2713aSLionel Sambuc                                    QualType Arg,
123f4a2713aSLionel Sambuc                                    TemplateDeductionInfo &Info,
124f4a2713aSLionel Sambuc                                    SmallVectorImpl<DeducedTemplateArgument> &
125f4a2713aSLionel Sambuc                                                       Deduced,
126f4a2713aSLionel Sambuc                                    unsigned TDF,
127f4a2713aSLionel Sambuc                                    bool PartialOrdering = false,
128f4a2713aSLionel Sambuc                             SmallVectorImpl<RefParamPartialOrderingComparison> *
129*0a6a1f1dSLionel Sambuc                                                  RefParamComparisons = nullptr);
130f4a2713aSLionel Sambuc 
131f4a2713aSLionel Sambuc static Sema::TemplateDeductionResult
132f4a2713aSLionel Sambuc DeduceTemplateArguments(Sema &S,
133f4a2713aSLionel Sambuc                         TemplateParameterList *TemplateParams,
134f4a2713aSLionel Sambuc                         const TemplateArgument *Params, unsigned NumParams,
135f4a2713aSLionel Sambuc                         const TemplateArgument *Args, unsigned NumArgs,
136f4a2713aSLionel Sambuc                         TemplateDeductionInfo &Info,
137f4a2713aSLionel Sambuc                         SmallVectorImpl<DeducedTemplateArgument> &Deduced);
138f4a2713aSLionel Sambuc 
139f4a2713aSLionel Sambuc /// \brief If the given expression is of a form that permits the deduction
140f4a2713aSLionel Sambuc /// of a non-type template parameter, return the declaration of that
141f4a2713aSLionel Sambuc /// non-type template parameter.
getDeducedParameterFromExpr(Expr * E)142f4a2713aSLionel Sambuc static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
143f4a2713aSLionel Sambuc   // If we are within an alias template, the expression may have undergone
144f4a2713aSLionel Sambuc   // any number of parameter substitutions already.
145f4a2713aSLionel Sambuc   while (1) {
146f4a2713aSLionel Sambuc     if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
147f4a2713aSLionel Sambuc       E = IC->getSubExpr();
148f4a2713aSLionel Sambuc     else if (SubstNonTypeTemplateParmExpr *Subst =
149f4a2713aSLionel Sambuc                dyn_cast<SubstNonTypeTemplateParmExpr>(E))
150f4a2713aSLionel Sambuc       E = Subst->getReplacement();
151f4a2713aSLionel Sambuc     else
152f4a2713aSLionel Sambuc       break;
153f4a2713aSLionel Sambuc   }
154f4a2713aSLionel Sambuc 
155f4a2713aSLionel Sambuc   if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
156f4a2713aSLionel Sambuc     return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
157f4a2713aSLionel Sambuc 
158*0a6a1f1dSLionel Sambuc   return nullptr;
159f4a2713aSLionel Sambuc }
160f4a2713aSLionel Sambuc 
161f4a2713aSLionel Sambuc /// \brief Determine whether two declaration pointers refer to the same
162f4a2713aSLionel Sambuc /// declaration.
isSameDeclaration(Decl * X,Decl * Y)163f4a2713aSLionel Sambuc static bool isSameDeclaration(Decl *X, Decl *Y) {
164f4a2713aSLionel Sambuc   if (NamedDecl *NX = dyn_cast<NamedDecl>(X))
165f4a2713aSLionel Sambuc     X = NX->getUnderlyingDecl();
166f4a2713aSLionel Sambuc   if (NamedDecl *NY = dyn_cast<NamedDecl>(Y))
167f4a2713aSLionel Sambuc     Y = NY->getUnderlyingDecl();
168f4a2713aSLionel Sambuc 
169f4a2713aSLionel Sambuc   return X->getCanonicalDecl() == Y->getCanonicalDecl();
170f4a2713aSLionel Sambuc }
171f4a2713aSLionel Sambuc 
172f4a2713aSLionel Sambuc /// \brief Verify that the given, deduced template arguments are compatible.
173f4a2713aSLionel Sambuc ///
174f4a2713aSLionel Sambuc /// \returns The deduced template argument, or a NULL template argument if
175f4a2713aSLionel Sambuc /// the deduced template arguments were incompatible.
176f4a2713aSLionel Sambuc static DeducedTemplateArgument
checkDeducedTemplateArguments(ASTContext & Context,const DeducedTemplateArgument & X,const DeducedTemplateArgument & Y)177f4a2713aSLionel Sambuc checkDeducedTemplateArguments(ASTContext &Context,
178f4a2713aSLionel Sambuc                               const DeducedTemplateArgument &X,
179f4a2713aSLionel Sambuc                               const DeducedTemplateArgument &Y) {
180f4a2713aSLionel Sambuc   // We have no deduction for one or both of the arguments; they're compatible.
181f4a2713aSLionel Sambuc   if (X.isNull())
182f4a2713aSLionel Sambuc     return Y;
183f4a2713aSLionel Sambuc   if (Y.isNull())
184f4a2713aSLionel Sambuc     return X;
185f4a2713aSLionel Sambuc 
186f4a2713aSLionel Sambuc   switch (X.getKind()) {
187f4a2713aSLionel Sambuc   case TemplateArgument::Null:
188f4a2713aSLionel Sambuc     llvm_unreachable("Non-deduced template arguments handled above");
189f4a2713aSLionel Sambuc 
190f4a2713aSLionel Sambuc   case TemplateArgument::Type:
191f4a2713aSLionel Sambuc     // If two template type arguments have the same type, they're compatible.
192f4a2713aSLionel Sambuc     if (Y.getKind() == TemplateArgument::Type &&
193f4a2713aSLionel Sambuc         Context.hasSameType(X.getAsType(), Y.getAsType()))
194f4a2713aSLionel Sambuc       return X;
195f4a2713aSLionel Sambuc 
196f4a2713aSLionel Sambuc     return DeducedTemplateArgument();
197f4a2713aSLionel Sambuc 
198f4a2713aSLionel Sambuc   case TemplateArgument::Integral:
199f4a2713aSLionel Sambuc     // If we deduced a constant in one case and either a dependent expression or
200f4a2713aSLionel Sambuc     // declaration in another case, keep the integral constant.
201f4a2713aSLionel Sambuc     // If both are integral constants with the same value, keep that value.
202f4a2713aSLionel Sambuc     if (Y.getKind() == TemplateArgument::Expression ||
203f4a2713aSLionel Sambuc         Y.getKind() == TemplateArgument::Declaration ||
204f4a2713aSLionel Sambuc         (Y.getKind() == TemplateArgument::Integral &&
205f4a2713aSLionel Sambuc          hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral())))
206f4a2713aSLionel Sambuc       return DeducedTemplateArgument(X,
207f4a2713aSLionel Sambuc                                      X.wasDeducedFromArrayBound() &&
208f4a2713aSLionel Sambuc                                      Y.wasDeducedFromArrayBound());
209f4a2713aSLionel Sambuc 
210f4a2713aSLionel Sambuc     // All other combinations are incompatible.
211f4a2713aSLionel Sambuc     return DeducedTemplateArgument();
212f4a2713aSLionel Sambuc 
213f4a2713aSLionel Sambuc   case TemplateArgument::Template:
214f4a2713aSLionel Sambuc     if (Y.getKind() == TemplateArgument::Template &&
215f4a2713aSLionel Sambuc         Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate()))
216f4a2713aSLionel Sambuc       return X;
217f4a2713aSLionel Sambuc 
218f4a2713aSLionel Sambuc     // All other combinations are incompatible.
219f4a2713aSLionel Sambuc     return DeducedTemplateArgument();
220f4a2713aSLionel Sambuc 
221f4a2713aSLionel Sambuc   case TemplateArgument::TemplateExpansion:
222f4a2713aSLionel Sambuc     if (Y.getKind() == TemplateArgument::TemplateExpansion &&
223f4a2713aSLionel Sambuc         Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(),
224f4a2713aSLionel Sambuc                                     Y.getAsTemplateOrTemplatePattern()))
225f4a2713aSLionel Sambuc       return X;
226f4a2713aSLionel Sambuc 
227f4a2713aSLionel Sambuc     // All other combinations are incompatible.
228f4a2713aSLionel Sambuc     return DeducedTemplateArgument();
229f4a2713aSLionel Sambuc 
230f4a2713aSLionel Sambuc   case TemplateArgument::Expression:
231f4a2713aSLionel Sambuc     // If we deduced a dependent expression in one case and either an integral
232f4a2713aSLionel Sambuc     // constant or a declaration in another case, keep the integral constant
233f4a2713aSLionel Sambuc     // or declaration.
234f4a2713aSLionel Sambuc     if (Y.getKind() == TemplateArgument::Integral ||
235f4a2713aSLionel Sambuc         Y.getKind() == TemplateArgument::Declaration)
236f4a2713aSLionel Sambuc       return DeducedTemplateArgument(Y, X.wasDeducedFromArrayBound() &&
237f4a2713aSLionel Sambuc                                      Y.wasDeducedFromArrayBound());
238f4a2713aSLionel Sambuc 
239f4a2713aSLionel Sambuc     if (Y.getKind() == TemplateArgument::Expression) {
240f4a2713aSLionel Sambuc       // Compare the expressions for equality
241f4a2713aSLionel Sambuc       llvm::FoldingSetNodeID ID1, ID2;
242f4a2713aSLionel Sambuc       X.getAsExpr()->Profile(ID1, Context, true);
243f4a2713aSLionel Sambuc       Y.getAsExpr()->Profile(ID2, Context, true);
244f4a2713aSLionel Sambuc       if (ID1 == ID2)
245f4a2713aSLionel Sambuc         return X;
246f4a2713aSLionel Sambuc     }
247f4a2713aSLionel Sambuc 
248f4a2713aSLionel Sambuc     // All other combinations are incompatible.
249f4a2713aSLionel Sambuc     return DeducedTemplateArgument();
250f4a2713aSLionel Sambuc 
251f4a2713aSLionel Sambuc   case TemplateArgument::Declaration:
252f4a2713aSLionel Sambuc     // If we deduced a declaration and a dependent expression, keep the
253f4a2713aSLionel Sambuc     // declaration.
254f4a2713aSLionel Sambuc     if (Y.getKind() == TemplateArgument::Expression)
255f4a2713aSLionel Sambuc       return X;
256f4a2713aSLionel Sambuc 
257f4a2713aSLionel Sambuc     // If we deduced a declaration and an integral constant, keep the
258f4a2713aSLionel Sambuc     // integral constant.
259f4a2713aSLionel Sambuc     if (Y.getKind() == TemplateArgument::Integral)
260f4a2713aSLionel Sambuc       return Y;
261f4a2713aSLionel Sambuc 
262f4a2713aSLionel Sambuc     // If we deduced two declarations, make sure they they refer to the
263f4a2713aSLionel Sambuc     // same declaration.
264f4a2713aSLionel Sambuc     if (Y.getKind() == TemplateArgument::Declaration &&
265*0a6a1f1dSLionel Sambuc         isSameDeclaration(X.getAsDecl(), Y.getAsDecl()))
266f4a2713aSLionel Sambuc       return X;
267f4a2713aSLionel Sambuc 
268f4a2713aSLionel Sambuc     // All other combinations are incompatible.
269f4a2713aSLionel Sambuc     return DeducedTemplateArgument();
270f4a2713aSLionel Sambuc 
271f4a2713aSLionel Sambuc   case TemplateArgument::NullPtr:
272f4a2713aSLionel Sambuc     // If we deduced a null pointer and a dependent expression, keep the
273f4a2713aSLionel Sambuc     // null pointer.
274f4a2713aSLionel Sambuc     if (Y.getKind() == TemplateArgument::Expression)
275f4a2713aSLionel Sambuc       return X;
276f4a2713aSLionel Sambuc 
277f4a2713aSLionel Sambuc     // If we deduced a null pointer and an integral constant, keep the
278f4a2713aSLionel Sambuc     // integral constant.
279f4a2713aSLionel Sambuc     if (Y.getKind() == TemplateArgument::Integral)
280f4a2713aSLionel Sambuc       return Y;
281f4a2713aSLionel Sambuc 
282f4a2713aSLionel Sambuc     // If we deduced two null pointers, make sure they have the same type.
283f4a2713aSLionel Sambuc     if (Y.getKind() == TemplateArgument::NullPtr &&
284f4a2713aSLionel Sambuc         Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()))
285f4a2713aSLionel Sambuc       return X;
286f4a2713aSLionel Sambuc 
287f4a2713aSLionel Sambuc     // All other combinations are incompatible.
288f4a2713aSLionel Sambuc     return DeducedTemplateArgument();
289f4a2713aSLionel Sambuc 
290f4a2713aSLionel Sambuc   case TemplateArgument::Pack:
291f4a2713aSLionel Sambuc     if (Y.getKind() != TemplateArgument::Pack ||
292f4a2713aSLionel Sambuc         X.pack_size() != Y.pack_size())
293f4a2713aSLionel Sambuc       return DeducedTemplateArgument();
294f4a2713aSLionel Sambuc 
295f4a2713aSLionel Sambuc     for (TemplateArgument::pack_iterator XA = X.pack_begin(),
296f4a2713aSLionel Sambuc                                       XAEnd = X.pack_end(),
297f4a2713aSLionel Sambuc                                          YA = Y.pack_begin();
298f4a2713aSLionel Sambuc          XA != XAEnd; ++XA, ++YA) {
299*0a6a1f1dSLionel Sambuc       // FIXME: Do we need to merge the results together here?
300f4a2713aSLionel Sambuc       if (checkDeducedTemplateArguments(Context,
301f4a2713aSLionel Sambuc                     DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()),
302f4a2713aSLionel Sambuc                     DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound()))
303f4a2713aSLionel Sambuc             .isNull())
304f4a2713aSLionel Sambuc         return DeducedTemplateArgument();
305f4a2713aSLionel Sambuc     }
306f4a2713aSLionel Sambuc 
307f4a2713aSLionel Sambuc     return X;
308f4a2713aSLionel Sambuc   }
309f4a2713aSLionel Sambuc 
310f4a2713aSLionel Sambuc   llvm_unreachable("Invalid TemplateArgument Kind!");
311f4a2713aSLionel Sambuc }
312f4a2713aSLionel Sambuc 
313f4a2713aSLionel Sambuc /// \brief Deduce the value of the given non-type template parameter
314f4a2713aSLionel Sambuc /// from the given constant.
315f4a2713aSLionel Sambuc static Sema::TemplateDeductionResult
DeduceNonTypeTemplateArgument(Sema & S,NonTypeTemplateParmDecl * NTTP,llvm::APSInt Value,QualType ValueType,bool DeducedFromArrayBound,TemplateDeductionInfo & Info,SmallVectorImpl<DeducedTemplateArgument> & Deduced)316f4a2713aSLionel Sambuc DeduceNonTypeTemplateArgument(Sema &S,
317f4a2713aSLionel Sambuc                               NonTypeTemplateParmDecl *NTTP,
318f4a2713aSLionel Sambuc                               llvm::APSInt Value, QualType ValueType,
319f4a2713aSLionel Sambuc                               bool DeducedFromArrayBound,
320f4a2713aSLionel Sambuc                               TemplateDeductionInfo &Info,
321f4a2713aSLionel Sambuc                     SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
322f4a2713aSLionel Sambuc   assert(NTTP->getDepth() == 0 &&
323f4a2713aSLionel Sambuc          "Cannot deduce non-type template argument with depth > 0");
324f4a2713aSLionel Sambuc 
325f4a2713aSLionel Sambuc   DeducedTemplateArgument NewDeduced(S.Context, Value, ValueType,
326f4a2713aSLionel Sambuc                                      DeducedFromArrayBound);
327f4a2713aSLionel Sambuc   DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
328f4a2713aSLionel Sambuc                                                      Deduced[NTTP->getIndex()],
329f4a2713aSLionel Sambuc                                                                  NewDeduced);
330f4a2713aSLionel Sambuc   if (Result.isNull()) {
331f4a2713aSLionel Sambuc     Info.Param = NTTP;
332f4a2713aSLionel Sambuc     Info.FirstArg = Deduced[NTTP->getIndex()];
333f4a2713aSLionel Sambuc     Info.SecondArg = NewDeduced;
334f4a2713aSLionel Sambuc     return Sema::TDK_Inconsistent;
335f4a2713aSLionel Sambuc   }
336f4a2713aSLionel Sambuc 
337f4a2713aSLionel Sambuc   Deduced[NTTP->getIndex()] = Result;
338f4a2713aSLionel Sambuc   return Sema::TDK_Success;
339f4a2713aSLionel Sambuc }
340f4a2713aSLionel Sambuc 
341f4a2713aSLionel Sambuc /// \brief Deduce the value of the given non-type template parameter
342f4a2713aSLionel Sambuc /// from the given type- or value-dependent expression.
343f4a2713aSLionel Sambuc ///
344f4a2713aSLionel Sambuc /// \returns true if deduction succeeded, false otherwise.
345f4a2713aSLionel Sambuc static Sema::TemplateDeductionResult
DeduceNonTypeTemplateArgument(Sema & S,NonTypeTemplateParmDecl * NTTP,Expr * Value,TemplateDeductionInfo & Info,SmallVectorImpl<DeducedTemplateArgument> & Deduced)346f4a2713aSLionel Sambuc DeduceNonTypeTemplateArgument(Sema &S,
347f4a2713aSLionel Sambuc                               NonTypeTemplateParmDecl *NTTP,
348f4a2713aSLionel Sambuc                               Expr *Value,
349f4a2713aSLionel Sambuc                               TemplateDeductionInfo &Info,
350f4a2713aSLionel Sambuc                     SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
351f4a2713aSLionel Sambuc   assert(NTTP->getDepth() == 0 &&
352f4a2713aSLionel Sambuc          "Cannot deduce non-type template argument with depth > 0");
353f4a2713aSLionel Sambuc   assert((Value->isTypeDependent() || Value->isValueDependent()) &&
354f4a2713aSLionel Sambuc          "Expression template argument must be type- or value-dependent.");
355f4a2713aSLionel Sambuc 
356f4a2713aSLionel Sambuc   DeducedTemplateArgument NewDeduced(Value);
357f4a2713aSLionel Sambuc   DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
358f4a2713aSLionel Sambuc                                                      Deduced[NTTP->getIndex()],
359f4a2713aSLionel Sambuc                                                                  NewDeduced);
360f4a2713aSLionel Sambuc 
361f4a2713aSLionel Sambuc   if (Result.isNull()) {
362f4a2713aSLionel Sambuc     Info.Param = NTTP;
363f4a2713aSLionel Sambuc     Info.FirstArg = Deduced[NTTP->getIndex()];
364f4a2713aSLionel Sambuc     Info.SecondArg = NewDeduced;
365f4a2713aSLionel Sambuc     return Sema::TDK_Inconsistent;
366f4a2713aSLionel Sambuc   }
367f4a2713aSLionel Sambuc 
368f4a2713aSLionel Sambuc   Deduced[NTTP->getIndex()] = Result;
369f4a2713aSLionel Sambuc   return Sema::TDK_Success;
370f4a2713aSLionel Sambuc }
371f4a2713aSLionel Sambuc 
372f4a2713aSLionel Sambuc /// \brief Deduce the value of the given non-type template parameter
373f4a2713aSLionel Sambuc /// from the given declaration.
374f4a2713aSLionel Sambuc ///
375f4a2713aSLionel Sambuc /// \returns true if deduction succeeded, false otherwise.
376f4a2713aSLionel Sambuc static Sema::TemplateDeductionResult
DeduceNonTypeTemplateArgument(Sema & S,NonTypeTemplateParmDecl * NTTP,ValueDecl * D,TemplateDeductionInfo & Info,SmallVectorImpl<DeducedTemplateArgument> & Deduced)377f4a2713aSLionel Sambuc DeduceNonTypeTemplateArgument(Sema &S,
378f4a2713aSLionel Sambuc                             NonTypeTemplateParmDecl *NTTP,
379f4a2713aSLionel Sambuc                             ValueDecl *D,
380f4a2713aSLionel Sambuc                             TemplateDeductionInfo &Info,
381f4a2713aSLionel Sambuc                             SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
382f4a2713aSLionel Sambuc   assert(NTTP->getDepth() == 0 &&
383f4a2713aSLionel Sambuc          "Cannot deduce non-type template argument with depth > 0");
384f4a2713aSLionel Sambuc 
385*0a6a1f1dSLionel Sambuc   D = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr;
386*0a6a1f1dSLionel Sambuc   TemplateArgument New(D, NTTP->getType());
387f4a2713aSLionel Sambuc   DeducedTemplateArgument NewDeduced(New);
388f4a2713aSLionel Sambuc   DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
389f4a2713aSLionel Sambuc                                                      Deduced[NTTP->getIndex()],
390f4a2713aSLionel Sambuc                                                                  NewDeduced);
391f4a2713aSLionel Sambuc   if (Result.isNull()) {
392f4a2713aSLionel Sambuc     Info.Param = NTTP;
393f4a2713aSLionel Sambuc     Info.FirstArg = Deduced[NTTP->getIndex()];
394f4a2713aSLionel Sambuc     Info.SecondArg = NewDeduced;
395f4a2713aSLionel Sambuc     return Sema::TDK_Inconsistent;
396f4a2713aSLionel Sambuc   }
397f4a2713aSLionel Sambuc 
398f4a2713aSLionel Sambuc   Deduced[NTTP->getIndex()] = Result;
399f4a2713aSLionel Sambuc   return Sema::TDK_Success;
400f4a2713aSLionel Sambuc }
401f4a2713aSLionel Sambuc 
402f4a2713aSLionel Sambuc static Sema::TemplateDeductionResult
DeduceTemplateArguments(Sema & S,TemplateParameterList * TemplateParams,TemplateName Param,TemplateName Arg,TemplateDeductionInfo & Info,SmallVectorImpl<DeducedTemplateArgument> & Deduced)403f4a2713aSLionel Sambuc DeduceTemplateArguments(Sema &S,
404f4a2713aSLionel Sambuc                         TemplateParameterList *TemplateParams,
405f4a2713aSLionel Sambuc                         TemplateName Param,
406f4a2713aSLionel Sambuc                         TemplateName Arg,
407f4a2713aSLionel Sambuc                         TemplateDeductionInfo &Info,
408f4a2713aSLionel Sambuc                         SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
409f4a2713aSLionel Sambuc   TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
410f4a2713aSLionel Sambuc   if (!ParamDecl) {
411f4a2713aSLionel Sambuc     // The parameter type is dependent and is not a template template parameter,
412f4a2713aSLionel Sambuc     // so there is nothing that we can deduce.
413f4a2713aSLionel Sambuc     return Sema::TDK_Success;
414f4a2713aSLionel Sambuc   }
415f4a2713aSLionel Sambuc 
416f4a2713aSLionel Sambuc   if (TemplateTemplateParmDecl *TempParam
417f4a2713aSLionel Sambuc         = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) {
418f4a2713aSLionel Sambuc     DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg));
419f4a2713aSLionel Sambuc     DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
420f4a2713aSLionel Sambuc                                                  Deduced[TempParam->getIndex()],
421f4a2713aSLionel Sambuc                                                                    NewDeduced);
422f4a2713aSLionel Sambuc     if (Result.isNull()) {
423f4a2713aSLionel Sambuc       Info.Param = TempParam;
424f4a2713aSLionel Sambuc       Info.FirstArg = Deduced[TempParam->getIndex()];
425f4a2713aSLionel Sambuc       Info.SecondArg = NewDeduced;
426f4a2713aSLionel Sambuc       return Sema::TDK_Inconsistent;
427f4a2713aSLionel Sambuc     }
428f4a2713aSLionel Sambuc 
429f4a2713aSLionel Sambuc     Deduced[TempParam->getIndex()] = Result;
430f4a2713aSLionel Sambuc     return Sema::TDK_Success;
431f4a2713aSLionel Sambuc   }
432f4a2713aSLionel Sambuc 
433f4a2713aSLionel Sambuc   // Verify that the two template names are equivalent.
434f4a2713aSLionel Sambuc   if (S.Context.hasSameTemplateName(Param, Arg))
435f4a2713aSLionel Sambuc     return Sema::TDK_Success;
436f4a2713aSLionel Sambuc 
437f4a2713aSLionel Sambuc   // Mismatch of non-dependent template parameter to argument.
438f4a2713aSLionel Sambuc   Info.FirstArg = TemplateArgument(Param);
439f4a2713aSLionel Sambuc   Info.SecondArg = TemplateArgument(Arg);
440f4a2713aSLionel Sambuc   return Sema::TDK_NonDeducedMismatch;
441f4a2713aSLionel Sambuc }
442f4a2713aSLionel Sambuc 
443f4a2713aSLionel Sambuc /// \brief Deduce the template arguments by comparing the template parameter
444f4a2713aSLionel Sambuc /// type (which is a template-id) with the template argument type.
445f4a2713aSLionel Sambuc ///
446f4a2713aSLionel Sambuc /// \param S the Sema
447f4a2713aSLionel Sambuc ///
448f4a2713aSLionel Sambuc /// \param TemplateParams the template parameters that we are deducing
449f4a2713aSLionel Sambuc ///
450f4a2713aSLionel Sambuc /// \param Param the parameter type
451f4a2713aSLionel Sambuc ///
452f4a2713aSLionel Sambuc /// \param Arg the argument type
453f4a2713aSLionel Sambuc ///
454f4a2713aSLionel Sambuc /// \param Info information about the template argument deduction itself
455f4a2713aSLionel Sambuc ///
456f4a2713aSLionel Sambuc /// \param Deduced the deduced template arguments
457f4a2713aSLionel Sambuc ///
458f4a2713aSLionel Sambuc /// \returns the result of template argument deduction so far. Note that a
459f4a2713aSLionel Sambuc /// "success" result means that template argument deduction has not yet failed,
460f4a2713aSLionel Sambuc /// but it may still fail, later, for other reasons.
461f4a2713aSLionel Sambuc static Sema::TemplateDeductionResult
DeduceTemplateArguments(Sema & S,TemplateParameterList * TemplateParams,const TemplateSpecializationType * Param,QualType Arg,TemplateDeductionInfo & Info,SmallVectorImpl<DeducedTemplateArgument> & Deduced)462f4a2713aSLionel Sambuc DeduceTemplateArguments(Sema &S,
463f4a2713aSLionel Sambuc                         TemplateParameterList *TemplateParams,
464f4a2713aSLionel Sambuc                         const TemplateSpecializationType *Param,
465f4a2713aSLionel Sambuc                         QualType Arg,
466f4a2713aSLionel Sambuc                         TemplateDeductionInfo &Info,
467f4a2713aSLionel Sambuc                         SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
468f4a2713aSLionel Sambuc   assert(Arg.isCanonical() && "Argument type must be canonical");
469f4a2713aSLionel Sambuc 
470f4a2713aSLionel Sambuc   // Check whether the template argument is a dependent template-id.
471f4a2713aSLionel Sambuc   if (const TemplateSpecializationType *SpecArg
472f4a2713aSLionel Sambuc         = dyn_cast<TemplateSpecializationType>(Arg)) {
473f4a2713aSLionel Sambuc     // Perform template argument deduction for the template name.
474f4a2713aSLionel Sambuc     if (Sema::TemplateDeductionResult Result
475f4a2713aSLionel Sambuc           = DeduceTemplateArguments(S, TemplateParams,
476f4a2713aSLionel Sambuc                                     Param->getTemplateName(),
477f4a2713aSLionel Sambuc                                     SpecArg->getTemplateName(),
478f4a2713aSLionel Sambuc                                     Info, Deduced))
479f4a2713aSLionel Sambuc       return Result;
480f4a2713aSLionel Sambuc 
481f4a2713aSLionel Sambuc 
482f4a2713aSLionel Sambuc     // Perform template argument deduction on each template
483f4a2713aSLionel Sambuc     // argument. Ignore any missing/extra arguments, since they could be
484f4a2713aSLionel Sambuc     // filled in by default arguments.
485f4a2713aSLionel Sambuc     return DeduceTemplateArguments(S, TemplateParams,
486f4a2713aSLionel Sambuc                                    Param->getArgs(), Param->getNumArgs(),
487f4a2713aSLionel Sambuc                                    SpecArg->getArgs(), SpecArg->getNumArgs(),
488f4a2713aSLionel Sambuc                                    Info, Deduced);
489f4a2713aSLionel Sambuc   }
490f4a2713aSLionel Sambuc 
491f4a2713aSLionel Sambuc   // If the argument type is a class template specialization, we
492f4a2713aSLionel Sambuc   // perform template argument deduction using its template
493f4a2713aSLionel Sambuc   // arguments.
494f4a2713aSLionel Sambuc   const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
495f4a2713aSLionel Sambuc   if (!RecordArg) {
496f4a2713aSLionel Sambuc     Info.FirstArg = TemplateArgument(QualType(Param, 0));
497f4a2713aSLionel Sambuc     Info.SecondArg = TemplateArgument(Arg);
498f4a2713aSLionel Sambuc     return Sema::TDK_NonDeducedMismatch;
499f4a2713aSLionel Sambuc   }
500f4a2713aSLionel Sambuc 
501f4a2713aSLionel Sambuc   ClassTemplateSpecializationDecl *SpecArg
502f4a2713aSLionel Sambuc     = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
503f4a2713aSLionel Sambuc   if (!SpecArg) {
504f4a2713aSLionel Sambuc     Info.FirstArg = TemplateArgument(QualType(Param, 0));
505f4a2713aSLionel Sambuc     Info.SecondArg = TemplateArgument(Arg);
506f4a2713aSLionel Sambuc     return Sema::TDK_NonDeducedMismatch;
507f4a2713aSLionel Sambuc   }
508f4a2713aSLionel Sambuc 
509f4a2713aSLionel Sambuc   // Perform template argument deduction for the template name.
510f4a2713aSLionel Sambuc   if (Sema::TemplateDeductionResult Result
511f4a2713aSLionel Sambuc         = DeduceTemplateArguments(S,
512f4a2713aSLionel Sambuc                                   TemplateParams,
513f4a2713aSLionel Sambuc                                   Param->getTemplateName(),
514f4a2713aSLionel Sambuc                                TemplateName(SpecArg->getSpecializedTemplate()),
515f4a2713aSLionel Sambuc                                   Info, Deduced))
516f4a2713aSLionel Sambuc     return Result;
517f4a2713aSLionel Sambuc 
518f4a2713aSLionel Sambuc   // Perform template argument deduction for the template arguments.
519f4a2713aSLionel Sambuc   return DeduceTemplateArguments(S, TemplateParams,
520f4a2713aSLionel Sambuc                                  Param->getArgs(), Param->getNumArgs(),
521f4a2713aSLionel Sambuc                                  SpecArg->getTemplateArgs().data(),
522f4a2713aSLionel Sambuc                                  SpecArg->getTemplateArgs().size(),
523f4a2713aSLionel Sambuc                                  Info, Deduced);
524f4a2713aSLionel Sambuc }
525f4a2713aSLionel Sambuc 
526f4a2713aSLionel Sambuc /// \brief Determines whether the given type is an opaque type that
527f4a2713aSLionel Sambuc /// might be more qualified when instantiated.
IsPossiblyOpaquelyQualifiedType(QualType T)528f4a2713aSLionel Sambuc static bool IsPossiblyOpaquelyQualifiedType(QualType T) {
529f4a2713aSLionel Sambuc   switch (T->getTypeClass()) {
530f4a2713aSLionel Sambuc   case Type::TypeOfExpr:
531f4a2713aSLionel Sambuc   case Type::TypeOf:
532f4a2713aSLionel Sambuc   case Type::DependentName:
533f4a2713aSLionel Sambuc   case Type::Decltype:
534f4a2713aSLionel Sambuc   case Type::UnresolvedUsing:
535f4a2713aSLionel Sambuc   case Type::TemplateTypeParm:
536f4a2713aSLionel Sambuc     return true;
537f4a2713aSLionel Sambuc 
538f4a2713aSLionel Sambuc   case Type::ConstantArray:
539f4a2713aSLionel Sambuc   case Type::IncompleteArray:
540f4a2713aSLionel Sambuc   case Type::VariableArray:
541f4a2713aSLionel Sambuc   case Type::DependentSizedArray:
542f4a2713aSLionel Sambuc     return IsPossiblyOpaquelyQualifiedType(
543f4a2713aSLionel Sambuc                                       cast<ArrayType>(T)->getElementType());
544f4a2713aSLionel Sambuc 
545f4a2713aSLionel Sambuc   default:
546f4a2713aSLionel Sambuc     return false;
547f4a2713aSLionel Sambuc   }
548f4a2713aSLionel Sambuc }
549f4a2713aSLionel Sambuc 
550f4a2713aSLionel Sambuc /// \brief Retrieve the depth and index of a template parameter.
551f4a2713aSLionel Sambuc static std::pair<unsigned, unsigned>
getDepthAndIndex(NamedDecl * ND)552f4a2713aSLionel Sambuc getDepthAndIndex(NamedDecl *ND) {
553f4a2713aSLionel Sambuc   if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
554f4a2713aSLionel Sambuc     return std::make_pair(TTP->getDepth(), TTP->getIndex());
555f4a2713aSLionel Sambuc 
556f4a2713aSLionel Sambuc   if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
557f4a2713aSLionel Sambuc     return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
558f4a2713aSLionel Sambuc 
559f4a2713aSLionel Sambuc   TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
560f4a2713aSLionel Sambuc   return std::make_pair(TTP->getDepth(), TTP->getIndex());
561f4a2713aSLionel Sambuc }
562f4a2713aSLionel Sambuc 
563f4a2713aSLionel Sambuc /// \brief Retrieve the depth and index of an unexpanded parameter pack.
564f4a2713aSLionel Sambuc static std::pair<unsigned, unsigned>
getDepthAndIndex(UnexpandedParameterPack UPP)565f4a2713aSLionel Sambuc getDepthAndIndex(UnexpandedParameterPack UPP) {
566f4a2713aSLionel Sambuc   if (const TemplateTypeParmType *TTP
567f4a2713aSLionel Sambuc                           = UPP.first.dyn_cast<const TemplateTypeParmType *>())
568f4a2713aSLionel Sambuc     return std::make_pair(TTP->getDepth(), TTP->getIndex());
569f4a2713aSLionel Sambuc 
570f4a2713aSLionel Sambuc   return getDepthAndIndex(UPP.first.get<NamedDecl *>());
571f4a2713aSLionel Sambuc }
572f4a2713aSLionel Sambuc 
573f4a2713aSLionel Sambuc /// \brief Helper function to build a TemplateParameter when we don't
574f4a2713aSLionel Sambuc /// know its type statically.
makeTemplateParameter(Decl * D)575f4a2713aSLionel Sambuc static TemplateParameter makeTemplateParameter(Decl *D) {
576f4a2713aSLionel Sambuc   if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
577f4a2713aSLionel Sambuc     return TemplateParameter(TTP);
578f4a2713aSLionel Sambuc   if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
579f4a2713aSLionel Sambuc     return TemplateParameter(NTTP);
580f4a2713aSLionel Sambuc 
581f4a2713aSLionel Sambuc   return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
582f4a2713aSLionel Sambuc }
583f4a2713aSLionel Sambuc 
584*0a6a1f1dSLionel Sambuc /// A pack that we're currently deducing.
585*0a6a1f1dSLionel Sambuc struct clang::DeducedPack {
DeducedPackclang::DeducedPack586*0a6a1f1dSLionel Sambuc   DeducedPack(unsigned Index) : Index(Index), Outer(nullptr) {}
587f4a2713aSLionel Sambuc 
588*0a6a1f1dSLionel Sambuc   // The index of the pack.
589*0a6a1f1dSLionel Sambuc   unsigned Index;
590*0a6a1f1dSLionel Sambuc 
591*0a6a1f1dSLionel Sambuc   // The old value of the pack before we started deducing it.
592*0a6a1f1dSLionel Sambuc   DeducedTemplateArgument Saved;
593*0a6a1f1dSLionel Sambuc 
594*0a6a1f1dSLionel Sambuc   // A deferred value of this pack from an inner deduction, that couldn't be
595*0a6a1f1dSLionel Sambuc   // deduced because this deduction hadn't happened yet.
596*0a6a1f1dSLionel Sambuc   DeducedTemplateArgument DeferredDeduction;
597*0a6a1f1dSLionel Sambuc 
598*0a6a1f1dSLionel Sambuc   // The new value of the pack.
599*0a6a1f1dSLionel Sambuc   SmallVector<DeducedTemplateArgument, 4> New;
600*0a6a1f1dSLionel Sambuc 
601*0a6a1f1dSLionel Sambuc   // The outer deduction for this pack, if any.
602*0a6a1f1dSLionel Sambuc   DeducedPack *Outer;
603*0a6a1f1dSLionel Sambuc };
604*0a6a1f1dSLionel Sambuc 
605*0a6a1f1dSLionel Sambuc /// A scope in which we're performing pack deduction.
606*0a6a1f1dSLionel Sambuc class PackDeductionScope {
607*0a6a1f1dSLionel Sambuc public:
PackDeductionScope(Sema & S,TemplateParameterList * TemplateParams,SmallVectorImpl<DeducedTemplateArgument> & Deduced,TemplateDeductionInfo & Info,TemplateArgument Pattern)608*0a6a1f1dSLionel Sambuc   PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams,
609f4a2713aSLionel Sambuc                      SmallVectorImpl<DeducedTemplateArgument> &Deduced,
610*0a6a1f1dSLionel Sambuc                      TemplateDeductionInfo &Info, TemplateArgument Pattern)
611*0a6a1f1dSLionel Sambuc       : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) {
612*0a6a1f1dSLionel Sambuc     // Compute the set of template parameter indices that correspond to
613*0a6a1f1dSLionel Sambuc     // parameter packs expanded by the pack expansion.
614*0a6a1f1dSLionel Sambuc     {
615*0a6a1f1dSLionel Sambuc       llvm::SmallBitVector SawIndices(TemplateParams->size());
616*0a6a1f1dSLionel Sambuc       SmallVector<UnexpandedParameterPack, 2> Unexpanded;
617*0a6a1f1dSLionel Sambuc       S.collectUnexpandedParameterPacks(Pattern, Unexpanded);
618*0a6a1f1dSLionel Sambuc       for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
619*0a6a1f1dSLionel Sambuc         unsigned Depth, Index;
620*0a6a1f1dSLionel Sambuc         std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
621*0a6a1f1dSLionel Sambuc         if (Depth == 0 && !SawIndices[Index]) {
622*0a6a1f1dSLionel Sambuc           SawIndices[Index] = true;
623*0a6a1f1dSLionel Sambuc 
624*0a6a1f1dSLionel Sambuc           // Save the deduced template argument for the parameter pack expanded
625f4a2713aSLionel Sambuc           // by this pack expansion, then clear out the deduction.
626*0a6a1f1dSLionel Sambuc           DeducedPack Pack(Index);
627*0a6a1f1dSLionel Sambuc           Pack.Saved = Deduced[Index];
628*0a6a1f1dSLionel Sambuc           Deduced[Index] = TemplateArgument();
629f4a2713aSLionel Sambuc 
630*0a6a1f1dSLionel Sambuc           Packs.push_back(Pack);
631*0a6a1f1dSLionel Sambuc         }
632*0a6a1f1dSLionel Sambuc       }
633*0a6a1f1dSLionel Sambuc     }
634*0a6a1f1dSLionel Sambuc     assert(!Packs.empty() && "Pack expansion without unexpanded packs?");
635f4a2713aSLionel Sambuc 
636*0a6a1f1dSLionel Sambuc     for (auto &Pack : Packs) {
637*0a6a1f1dSLionel Sambuc       if (Info.PendingDeducedPacks.size() > Pack.Index)
638*0a6a1f1dSLionel Sambuc         Pack.Outer = Info.PendingDeducedPacks[Pack.Index];
639*0a6a1f1dSLionel Sambuc       else
640*0a6a1f1dSLionel Sambuc         Info.PendingDeducedPacks.resize(Pack.Index + 1);
641*0a6a1f1dSLionel Sambuc       Info.PendingDeducedPacks[Pack.Index] = &Pack;
642*0a6a1f1dSLionel Sambuc 
643*0a6a1f1dSLionel Sambuc       if (S.CurrentInstantiationScope) {
644f4a2713aSLionel Sambuc         // If the template argument pack was explicitly specified, add that to
645f4a2713aSLionel Sambuc         // the set of deduced arguments.
646f4a2713aSLionel Sambuc         const TemplateArgument *ExplicitArgs;
647f4a2713aSLionel Sambuc         unsigned NumExplicitArgs;
648*0a6a1f1dSLionel Sambuc         NamedDecl *PartiallySubstitutedPack =
649*0a6a1f1dSLionel Sambuc             S.CurrentInstantiationScope->getPartiallySubstitutedPack(
650*0a6a1f1dSLionel Sambuc                 &ExplicitArgs, &NumExplicitArgs);
651*0a6a1f1dSLionel Sambuc         if (PartiallySubstitutedPack &&
652*0a6a1f1dSLionel Sambuc             getDepthAndIndex(PartiallySubstitutedPack).second == Pack.Index)
653*0a6a1f1dSLionel Sambuc           Pack.New.append(ExplicitArgs, ExplicitArgs + NumExplicitArgs);
654*0a6a1f1dSLionel Sambuc       }
655*0a6a1f1dSLionel Sambuc     }
656*0a6a1f1dSLionel Sambuc   }
657*0a6a1f1dSLionel Sambuc 
~PackDeductionScope()658*0a6a1f1dSLionel Sambuc   ~PackDeductionScope() {
659*0a6a1f1dSLionel Sambuc     for (auto &Pack : Packs)
660*0a6a1f1dSLionel Sambuc       Info.PendingDeducedPacks[Pack.Index] = Pack.Outer;
661*0a6a1f1dSLionel Sambuc   }
662*0a6a1f1dSLionel Sambuc 
663*0a6a1f1dSLionel Sambuc   /// Move to deducing the next element in each pack that is being deduced.
nextPackElement()664*0a6a1f1dSLionel Sambuc   void nextPackElement() {
665*0a6a1f1dSLionel Sambuc     // Capture the deduced template arguments for each parameter pack expanded
666*0a6a1f1dSLionel Sambuc     // by this pack expansion, add them to the list of arguments we've deduced
667*0a6a1f1dSLionel Sambuc     // for that pack, then clear out the deduced argument.
668*0a6a1f1dSLionel Sambuc     for (auto &Pack : Packs) {
669*0a6a1f1dSLionel Sambuc       DeducedTemplateArgument &DeducedArg = Deduced[Pack.Index];
670*0a6a1f1dSLionel Sambuc       if (!DeducedArg.isNull()) {
671*0a6a1f1dSLionel Sambuc         Pack.New.push_back(DeducedArg);
672*0a6a1f1dSLionel Sambuc         DeducedArg = DeducedTemplateArgument();
673f4a2713aSLionel Sambuc       }
674f4a2713aSLionel Sambuc     }
675f4a2713aSLionel Sambuc   }
676f4a2713aSLionel Sambuc 
677f4a2713aSLionel Sambuc   /// \brief Finish template argument deduction for a set of argument packs,
678f4a2713aSLionel Sambuc   /// producing the argument packs and checking for consistency with prior
679f4a2713aSLionel Sambuc   /// deductions.
finish(bool HasAnyArguments)680*0a6a1f1dSLionel Sambuc   Sema::TemplateDeductionResult finish(bool HasAnyArguments) {
681f4a2713aSLionel Sambuc     // Build argument packs for each of the parameter packs expanded by this
682f4a2713aSLionel Sambuc     // pack expansion.
683*0a6a1f1dSLionel Sambuc     for (auto &Pack : Packs) {
684*0a6a1f1dSLionel Sambuc       // Put back the old value for this pack.
685*0a6a1f1dSLionel Sambuc       Deduced[Pack.Index] = Pack.Saved;
686*0a6a1f1dSLionel Sambuc 
687*0a6a1f1dSLionel Sambuc       // Build or find a new value for this pack.
688*0a6a1f1dSLionel Sambuc       DeducedTemplateArgument NewPack;
689*0a6a1f1dSLionel Sambuc       if (HasAnyArguments && Pack.New.empty()) {
690*0a6a1f1dSLionel Sambuc         if (Pack.DeferredDeduction.isNull()) {
691*0a6a1f1dSLionel Sambuc           // We were not able to deduce anything for this parameter pack
692*0a6a1f1dSLionel Sambuc           // (because it only appeared in non-deduced contexts), so just
693*0a6a1f1dSLionel Sambuc           // restore the saved argument pack.
694f4a2713aSLionel Sambuc           continue;
695f4a2713aSLionel Sambuc         }
696f4a2713aSLionel Sambuc 
697*0a6a1f1dSLionel Sambuc         NewPack = Pack.DeferredDeduction;
698*0a6a1f1dSLionel Sambuc         Pack.DeferredDeduction = TemplateArgument();
699*0a6a1f1dSLionel Sambuc       } else if (Pack.New.empty()) {
700f4a2713aSLionel Sambuc         // If we deduced an empty argument pack, create it now.
701f4a2713aSLionel Sambuc         NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack());
702f4a2713aSLionel Sambuc       } else {
703*0a6a1f1dSLionel Sambuc         TemplateArgument *ArgumentPack =
704*0a6a1f1dSLionel Sambuc             new (S.Context) TemplateArgument[Pack.New.size()];
705*0a6a1f1dSLionel Sambuc         std::copy(Pack.New.begin(), Pack.New.end(), ArgumentPack);
706*0a6a1f1dSLionel Sambuc         NewPack = DeducedTemplateArgument(
707*0a6a1f1dSLionel Sambuc             TemplateArgument(ArgumentPack, Pack.New.size()),
708*0a6a1f1dSLionel Sambuc             Pack.New[0].wasDeducedFromArrayBound());
709f4a2713aSLionel Sambuc       }
710f4a2713aSLionel Sambuc 
711*0a6a1f1dSLionel Sambuc       // Pick where we're going to put the merged pack.
712*0a6a1f1dSLionel Sambuc       DeducedTemplateArgument *Loc;
713*0a6a1f1dSLionel Sambuc       if (Pack.Outer) {
714*0a6a1f1dSLionel Sambuc         if (Pack.Outer->DeferredDeduction.isNull()) {
715*0a6a1f1dSLionel Sambuc           // Defer checking this pack until we have a complete pack to compare
716*0a6a1f1dSLionel Sambuc           // it against.
717*0a6a1f1dSLionel Sambuc           Pack.Outer->DeferredDeduction = NewPack;
718*0a6a1f1dSLionel Sambuc           continue;
719*0a6a1f1dSLionel Sambuc         }
720*0a6a1f1dSLionel Sambuc         Loc = &Pack.Outer->DeferredDeduction;
721*0a6a1f1dSLionel Sambuc       } else {
722*0a6a1f1dSLionel Sambuc         Loc = &Deduced[Pack.Index];
723*0a6a1f1dSLionel Sambuc       }
724*0a6a1f1dSLionel Sambuc 
725*0a6a1f1dSLionel Sambuc       // Check the new pack matches any previous value.
726*0a6a1f1dSLionel Sambuc       DeducedTemplateArgument OldPack = *Loc;
727*0a6a1f1dSLionel Sambuc       DeducedTemplateArgument Result =
728*0a6a1f1dSLionel Sambuc           checkDeducedTemplateArguments(S.Context, OldPack, NewPack);
729*0a6a1f1dSLionel Sambuc 
730*0a6a1f1dSLionel Sambuc       // If we deferred a deduction of this pack, check that one now too.
731*0a6a1f1dSLionel Sambuc       if (!Result.isNull() && !Pack.DeferredDeduction.isNull()) {
732*0a6a1f1dSLionel Sambuc         OldPack = Result;
733*0a6a1f1dSLionel Sambuc         NewPack = Pack.DeferredDeduction;
734*0a6a1f1dSLionel Sambuc         Result = checkDeducedTemplateArguments(S.Context, OldPack, NewPack);
735*0a6a1f1dSLionel Sambuc       }
736*0a6a1f1dSLionel Sambuc 
737f4a2713aSLionel Sambuc       if (Result.isNull()) {
738*0a6a1f1dSLionel Sambuc         Info.Param =
739*0a6a1f1dSLionel Sambuc             makeTemplateParameter(TemplateParams->getParam(Pack.Index));
740*0a6a1f1dSLionel Sambuc         Info.FirstArg = OldPack;
741f4a2713aSLionel Sambuc         Info.SecondArg = NewPack;
742f4a2713aSLionel Sambuc         return Sema::TDK_Inconsistent;
743f4a2713aSLionel Sambuc       }
744f4a2713aSLionel Sambuc 
745*0a6a1f1dSLionel Sambuc       *Loc = Result;
746f4a2713aSLionel Sambuc     }
747f4a2713aSLionel Sambuc 
748f4a2713aSLionel Sambuc     return Sema::TDK_Success;
749f4a2713aSLionel Sambuc   }
750f4a2713aSLionel Sambuc 
751*0a6a1f1dSLionel Sambuc private:
752*0a6a1f1dSLionel Sambuc   Sema &S;
753*0a6a1f1dSLionel Sambuc   TemplateParameterList *TemplateParams;
754*0a6a1f1dSLionel Sambuc   SmallVectorImpl<DeducedTemplateArgument> &Deduced;
755*0a6a1f1dSLionel Sambuc   TemplateDeductionInfo &Info;
756*0a6a1f1dSLionel Sambuc 
757*0a6a1f1dSLionel Sambuc   SmallVector<DeducedPack, 2> Packs;
758*0a6a1f1dSLionel Sambuc };
759*0a6a1f1dSLionel Sambuc 
760f4a2713aSLionel Sambuc /// \brief Deduce the template arguments by comparing the list of parameter
761f4a2713aSLionel Sambuc /// types to the list of argument types, as in the parameter-type-lists of
762f4a2713aSLionel Sambuc /// function types (C++ [temp.deduct.type]p10).
763f4a2713aSLionel Sambuc ///
764f4a2713aSLionel Sambuc /// \param S The semantic analysis object within which we are deducing
765f4a2713aSLionel Sambuc ///
766f4a2713aSLionel Sambuc /// \param TemplateParams The template parameters that we are deducing
767f4a2713aSLionel Sambuc ///
768f4a2713aSLionel Sambuc /// \param Params The list of parameter types
769f4a2713aSLionel Sambuc ///
770f4a2713aSLionel Sambuc /// \param NumParams The number of types in \c Params
771f4a2713aSLionel Sambuc ///
772f4a2713aSLionel Sambuc /// \param Args The list of argument types
773f4a2713aSLionel Sambuc ///
774f4a2713aSLionel Sambuc /// \param NumArgs The number of types in \c Args
775f4a2713aSLionel Sambuc ///
776f4a2713aSLionel Sambuc /// \param Info information about the template argument deduction itself
777f4a2713aSLionel Sambuc ///
778f4a2713aSLionel Sambuc /// \param Deduced the deduced template arguments
779f4a2713aSLionel Sambuc ///
780f4a2713aSLionel Sambuc /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
781f4a2713aSLionel Sambuc /// how template argument deduction is performed.
782f4a2713aSLionel Sambuc ///
783f4a2713aSLionel Sambuc /// \param PartialOrdering If true, we are performing template argument
784f4a2713aSLionel Sambuc /// deduction for during partial ordering for a call
785f4a2713aSLionel Sambuc /// (C++0x [temp.deduct.partial]).
786f4a2713aSLionel Sambuc ///
787f4a2713aSLionel Sambuc /// \param RefParamComparisons If we're performing template argument deduction
788f4a2713aSLionel Sambuc /// in the context of partial ordering, the set of qualifier comparisons.
789f4a2713aSLionel Sambuc ///
790f4a2713aSLionel Sambuc /// \returns the result of template argument deduction so far. Note that a
791f4a2713aSLionel Sambuc /// "success" result means that template argument deduction has not yet failed,
792f4a2713aSLionel Sambuc /// but it may still fail, later, for other reasons.
793f4a2713aSLionel Sambuc static Sema::TemplateDeductionResult
DeduceTemplateArguments(Sema & S,TemplateParameterList * TemplateParams,const QualType * Params,unsigned NumParams,const QualType * Args,unsigned NumArgs,TemplateDeductionInfo & Info,SmallVectorImpl<DeducedTemplateArgument> & Deduced,unsigned TDF,bool PartialOrdering=false,SmallVectorImpl<RefParamPartialOrderingComparison> * RefParamComparisons=nullptr)794f4a2713aSLionel Sambuc DeduceTemplateArguments(Sema &S,
795f4a2713aSLionel Sambuc                         TemplateParameterList *TemplateParams,
796f4a2713aSLionel Sambuc                         const QualType *Params, unsigned NumParams,
797f4a2713aSLionel Sambuc                         const QualType *Args, unsigned NumArgs,
798f4a2713aSLionel Sambuc                         TemplateDeductionInfo &Info,
799f4a2713aSLionel Sambuc                         SmallVectorImpl<DeducedTemplateArgument> &Deduced,
800f4a2713aSLionel Sambuc                         unsigned TDF,
801f4a2713aSLionel Sambuc                         bool PartialOrdering = false,
802f4a2713aSLionel Sambuc                         SmallVectorImpl<RefParamPartialOrderingComparison> *
803*0a6a1f1dSLionel Sambuc                                                 RefParamComparisons = nullptr) {
804f4a2713aSLionel Sambuc   // Fast-path check to see if we have too many/too few arguments.
805f4a2713aSLionel Sambuc   if (NumParams != NumArgs &&
806f4a2713aSLionel Sambuc       !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) &&
807f4a2713aSLionel Sambuc       !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1])))
808f4a2713aSLionel Sambuc     return Sema::TDK_MiscellaneousDeductionFailure;
809f4a2713aSLionel Sambuc 
810f4a2713aSLionel Sambuc   // C++0x [temp.deduct.type]p10:
811f4a2713aSLionel Sambuc   //   Similarly, if P has a form that contains (T), then each parameter type
812f4a2713aSLionel Sambuc   //   Pi of the respective parameter-type- list of P is compared with the
813f4a2713aSLionel Sambuc   //   corresponding parameter type Ai of the corresponding parameter-type-list
814f4a2713aSLionel Sambuc   //   of A. [...]
815f4a2713aSLionel Sambuc   unsigned ArgIdx = 0, ParamIdx = 0;
816f4a2713aSLionel Sambuc   for (; ParamIdx != NumParams; ++ParamIdx) {
817f4a2713aSLionel Sambuc     // Check argument types.
818f4a2713aSLionel Sambuc     const PackExpansionType *Expansion
819f4a2713aSLionel Sambuc                                 = dyn_cast<PackExpansionType>(Params[ParamIdx]);
820f4a2713aSLionel Sambuc     if (!Expansion) {
821f4a2713aSLionel Sambuc       // Simple case: compare the parameter and argument types at this point.
822f4a2713aSLionel Sambuc 
823f4a2713aSLionel Sambuc       // Make sure we have an argument.
824f4a2713aSLionel Sambuc       if (ArgIdx >= NumArgs)
825f4a2713aSLionel Sambuc         return Sema::TDK_MiscellaneousDeductionFailure;
826f4a2713aSLionel Sambuc 
827f4a2713aSLionel Sambuc       if (isa<PackExpansionType>(Args[ArgIdx])) {
828f4a2713aSLionel Sambuc         // C++0x [temp.deduct.type]p22:
829f4a2713aSLionel Sambuc         //   If the original function parameter associated with A is a function
830f4a2713aSLionel Sambuc         //   parameter pack and the function parameter associated with P is not
831f4a2713aSLionel Sambuc         //   a function parameter pack, then template argument deduction fails.
832f4a2713aSLionel Sambuc         return Sema::TDK_MiscellaneousDeductionFailure;
833f4a2713aSLionel Sambuc       }
834f4a2713aSLionel Sambuc 
835f4a2713aSLionel Sambuc       if (Sema::TemplateDeductionResult Result
836f4a2713aSLionel Sambuc             = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
837f4a2713aSLionel Sambuc                                                  Params[ParamIdx], Args[ArgIdx],
838f4a2713aSLionel Sambuc                                                  Info, Deduced, TDF,
839f4a2713aSLionel Sambuc                                                  PartialOrdering,
840f4a2713aSLionel Sambuc                                                  RefParamComparisons))
841f4a2713aSLionel Sambuc         return Result;
842f4a2713aSLionel Sambuc 
843f4a2713aSLionel Sambuc       ++ArgIdx;
844f4a2713aSLionel Sambuc       continue;
845f4a2713aSLionel Sambuc     }
846f4a2713aSLionel Sambuc 
847f4a2713aSLionel Sambuc     // C++0x [temp.deduct.type]p5:
848f4a2713aSLionel Sambuc     //   The non-deduced contexts are:
849f4a2713aSLionel Sambuc     //     - A function parameter pack that does not occur at the end of the
850f4a2713aSLionel Sambuc     //       parameter-declaration-clause.
851f4a2713aSLionel Sambuc     if (ParamIdx + 1 < NumParams)
852f4a2713aSLionel Sambuc       return Sema::TDK_Success;
853f4a2713aSLionel Sambuc 
854f4a2713aSLionel Sambuc     // C++0x [temp.deduct.type]p10:
855f4a2713aSLionel Sambuc     //   If the parameter-declaration corresponding to Pi is a function
856f4a2713aSLionel Sambuc     //   parameter pack, then the type of its declarator- id is compared with
857f4a2713aSLionel Sambuc     //   each remaining parameter type in the parameter-type-list of A. Each
858f4a2713aSLionel Sambuc     //   comparison deduces template arguments for subsequent positions in the
859f4a2713aSLionel Sambuc     //   template parameter packs expanded by the function parameter pack.
860f4a2713aSLionel Sambuc 
861f4a2713aSLionel Sambuc     QualType Pattern = Expansion->getPattern();
862*0a6a1f1dSLionel Sambuc     PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern);
863f4a2713aSLionel Sambuc 
864f4a2713aSLionel Sambuc     bool HasAnyArguments = false;
865f4a2713aSLionel Sambuc     for (; ArgIdx < NumArgs; ++ArgIdx) {
866f4a2713aSLionel Sambuc       HasAnyArguments = true;
867f4a2713aSLionel Sambuc 
868f4a2713aSLionel Sambuc       // Deduce template arguments from the pattern.
869f4a2713aSLionel Sambuc       if (Sema::TemplateDeductionResult Result
870f4a2713aSLionel Sambuc             = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, Pattern,
871f4a2713aSLionel Sambuc                                                  Args[ArgIdx], Info, Deduced,
872f4a2713aSLionel Sambuc                                                  TDF, PartialOrdering,
873f4a2713aSLionel Sambuc                                                  RefParamComparisons))
874f4a2713aSLionel Sambuc         return Result;
875f4a2713aSLionel Sambuc 
876*0a6a1f1dSLionel Sambuc       PackScope.nextPackElement();
877f4a2713aSLionel Sambuc     }
878f4a2713aSLionel Sambuc 
879f4a2713aSLionel Sambuc     // Build argument packs for each of the parameter packs expanded by this
880f4a2713aSLionel Sambuc     // pack expansion.
881*0a6a1f1dSLionel Sambuc     if (auto Result = PackScope.finish(HasAnyArguments))
882f4a2713aSLionel Sambuc       return Result;
883f4a2713aSLionel Sambuc   }
884f4a2713aSLionel Sambuc 
885f4a2713aSLionel Sambuc   // Make sure we don't have any extra arguments.
886f4a2713aSLionel Sambuc   if (ArgIdx < NumArgs)
887f4a2713aSLionel Sambuc     return Sema::TDK_MiscellaneousDeductionFailure;
888f4a2713aSLionel Sambuc 
889f4a2713aSLionel Sambuc   return Sema::TDK_Success;
890f4a2713aSLionel Sambuc }
891f4a2713aSLionel Sambuc 
892f4a2713aSLionel Sambuc /// \brief Determine whether the parameter has qualifiers that are either
893f4a2713aSLionel Sambuc /// inconsistent with or a superset of the argument's qualifiers.
hasInconsistentOrSupersetQualifiersOf(QualType ParamType,QualType ArgType)894f4a2713aSLionel Sambuc static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType,
895f4a2713aSLionel Sambuc                                                   QualType ArgType) {
896f4a2713aSLionel Sambuc   Qualifiers ParamQs = ParamType.getQualifiers();
897f4a2713aSLionel Sambuc   Qualifiers ArgQs = ArgType.getQualifiers();
898f4a2713aSLionel Sambuc 
899f4a2713aSLionel Sambuc   if (ParamQs == ArgQs)
900f4a2713aSLionel Sambuc     return false;
901f4a2713aSLionel Sambuc 
902f4a2713aSLionel Sambuc   // Mismatched (but not missing) Objective-C GC attributes.
903f4a2713aSLionel Sambuc   if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() &&
904f4a2713aSLionel Sambuc       ParamQs.hasObjCGCAttr())
905f4a2713aSLionel Sambuc     return true;
906f4a2713aSLionel Sambuc 
907f4a2713aSLionel Sambuc   // Mismatched (but not missing) address spaces.
908f4a2713aSLionel Sambuc   if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() &&
909f4a2713aSLionel Sambuc       ParamQs.hasAddressSpace())
910f4a2713aSLionel Sambuc     return true;
911f4a2713aSLionel Sambuc 
912f4a2713aSLionel Sambuc   // Mismatched (but not missing) Objective-C lifetime qualifiers.
913f4a2713aSLionel Sambuc   if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() &&
914f4a2713aSLionel Sambuc       ParamQs.hasObjCLifetime())
915f4a2713aSLionel Sambuc     return true;
916f4a2713aSLionel Sambuc 
917f4a2713aSLionel Sambuc   // CVR qualifier superset.
918f4a2713aSLionel Sambuc   return (ParamQs.getCVRQualifiers() != ArgQs.getCVRQualifiers()) &&
919f4a2713aSLionel Sambuc       ((ParamQs.getCVRQualifiers() | ArgQs.getCVRQualifiers())
920f4a2713aSLionel Sambuc                                                 == ParamQs.getCVRQualifiers());
921f4a2713aSLionel Sambuc }
922f4a2713aSLionel Sambuc 
923f4a2713aSLionel Sambuc /// \brief Compare types for equality with respect to possibly compatible
924f4a2713aSLionel Sambuc /// function types (noreturn adjustment, implicit calling conventions). If any
925f4a2713aSLionel Sambuc /// of parameter and argument is not a function, just perform type comparison.
926f4a2713aSLionel Sambuc ///
927f4a2713aSLionel Sambuc /// \param Param the template parameter type.
928f4a2713aSLionel Sambuc ///
929f4a2713aSLionel Sambuc /// \param Arg the argument type.
isSameOrCompatibleFunctionType(CanQualType Param,CanQualType Arg)930f4a2713aSLionel Sambuc bool Sema::isSameOrCompatibleFunctionType(CanQualType Param,
931f4a2713aSLionel Sambuc                                           CanQualType Arg) {
932f4a2713aSLionel Sambuc   const FunctionType *ParamFunction = Param->getAs<FunctionType>(),
933f4a2713aSLionel Sambuc                      *ArgFunction   = Arg->getAs<FunctionType>();
934f4a2713aSLionel Sambuc 
935f4a2713aSLionel Sambuc   // Just compare if not functions.
936f4a2713aSLionel Sambuc   if (!ParamFunction || !ArgFunction)
937f4a2713aSLionel Sambuc     return Param == Arg;
938f4a2713aSLionel Sambuc 
939f4a2713aSLionel Sambuc   // Noreturn adjustment.
940f4a2713aSLionel Sambuc   QualType AdjustedParam;
941f4a2713aSLionel Sambuc   if (IsNoReturnConversion(Param, Arg, AdjustedParam))
942f4a2713aSLionel Sambuc     return Arg == Context.getCanonicalType(AdjustedParam);
943f4a2713aSLionel Sambuc 
944f4a2713aSLionel Sambuc   // FIXME: Compatible calling conventions.
945f4a2713aSLionel Sambuc 
946f4a2713aSLionel Sambuc   return Param == Arg;
947f4a2713aSLionel Sambuc }
948f4a2713aSLionel Sambuc 
949f4a2713aSLionel Sambuc /// \brief Deduce the template arguments by comparing the parameter type and
950f4a2713aSLionel Sambuc /// the argument type (C++ [temp.deduct.type]).
951f4a2713aSLionel Sambuc ///
952f4a2713aSLionel Sambuc /// \param S the semantic analysis object within which we are deducing
953f4a2713aSLionel Sambuc ///
954f4a2713aSLionel Sambuc /// \param TemplateParams the template parameters that we are deducing
955f4a2713aSLionel Sambuc ///
956f4a2713aSLionel Sambuc /// \param ParamIn the parameter type
957f4a2713aSLionel Sambuc ///
958f4a2713aSLionel Sambuc /// \param ArgIn the argument type
959f4a2713aSLionel Sambuc ///
960f4a2713aSLionel Sambuc /// \param Info information about the template argument deduction itself
961f4a2713aSLionel Sambuc ///
962f4a2713aSLionel Sambuc /// \param Deduced the deduced template arguments
963f4a2713aSLionel Sambuc ///
964f4a2713aSLionel Sambuc /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
965f4a2713aSLionel Sambuc /// how template argument deduction is performed.
966f4a2713aSLionel Sambuc ///
967f4a2713aSLionel Sambuc /// \param PartialOrdering Whether we're performing template argument deduction
968f4a2713aSLionel Sambuc /// in the context of partial ordering (C++0x [temp.deduct.partial]).
969f4a2713aSLionel Sambuc ///
970f4a2713aSLionel Sambuc /// \param RefParamComparisons If we're performing template argument deduction
971f4a2713aSLionel Sambuc /// in the context of partial ordering, the set of qualifier comparisons.
972f4a2713aSLionel Sambuc ///
973f4a2713aSLionel Sambuc /// \returns the result of template argument deduction so far. Note that a
974f4a2713aSLionel Sambuc /// "success" result means that template argument deduction has not yet failed,
975f4a2713aSLionel Sambuc /// but it may still fail, later, for other reasons.
976f4a2713aSLionel Sambuc static Sema::TemplateDeductionResult
DeduceTemplateArgumentsByTypeMatch(Sema & S,TemplateParameterList * TemplateParams,QualType ParamIn,QualType ArgIn,TemplateDeductionInfo & Info,SmallVectorImpl<DeducedTemplateArgument> & Deduced,unsigned TDF,bool PartialOrdering,SmallVectorImpl<RefParamPartialOrderingComparison> * RefParamComparisons)977f4a2713aSLionel Sambuc DeduceTemplateArgumentsByTypeMatch(Sema &S,
978f4a2713aSLionel Sambuc                                    TemplateParameterList *TemplateParams,
979f4a2713aSLionel Sambuc                                    QualType ParamIn, QualType ArgIn,
980f4a2713aSLionel Sambuc                                    TemplateDeductionInfo &Info,
981f4a2713aSLionel Sambuc                             SmallVectorImpl<DeducedTemplateArgument> &Deduced,
982f4a2713aSLionel Sambuc                                    unsigned TDF,
983f4a2713aSLionel Sambuc                                    bool PartialOrdering,
984f4a2713aSLionel Sambuc                             SmallVectorImpl<RefParamPartialOrderingComparison> *
985f4a2713aSLionel Sambuc                                                           RefParamComparisons) {
986f4a2713aSLionel Sambuc   // We only want to look at the canonical types, since typedefs and
987f4a2713aSLionel Sambuc   // sugar are not part of template argument deduction.
988f4a2713aSLionel Sambuc   QualType Param = S.Context.getCanonicalType(ParamIn);
989f4a2713aSLionel Sambuc   QualType Arg = S.Context.getCanonicalType(ArgIn);
990f4a2713aSLionel Sambuc 
991f4a2713aSLionel Sambuc   // If the argument type is a pack expansion, look at its pattern.
992f4a2713aSLionel Sambuc   // This isn't explicitly called out
993f4a2713aSLionel Sambuc   if (const PackExpansionType *ArgExpansion
994f4a2713aSLionel Sambuc                                             = dyn_cast<PackExpansionType>(Arg))
995f4a2713aSLionel Sambuc     Arg = ArgExpansion->getPattern();
996f4a2713aSLionel Sambuc 
997f4a2713aSLionel Sambuc   if (PartialOrdering) {
998f4a2713aSLionel Sambuc     // C++0x [temp.deduct.partial]p5:
999f4a2713aSLionel Sambuc     //   Before the partial ordering is done, certain transformations are
1000f4a2713aSLionel Sambuc     //   performed on the types used for partial ordering:
1001f4a2713aSLionel Sambuc     //     - If P is a reference type, P is replaced by the type referred to.
1002f4a2713aSLionel Sambuc     const ReferenceType *ParamRef = Param->getAs<ReferenceType>();
1003f4a2713aSLionel Sambuc     if (ParamRef)
1004f4a2713aSLionel Sambuc       Param = ParamRef->getPointeeType();
1005f4a2713aSLionel Sambuc 
1006f4a2713aSLionel Sambuc     //     - If A is a reference type, A is replaced by the type referred to.
1007f4a2713aSLionel Sambuc     const ReferenceType *ArgRef = Arg->getAs<ReferenceType>();
1008f4a2713aSLionel Sambuc     if (ArgRef)
1009f4a2713aSLionel Sambuc       Arg = ArgRef->getPointeeType();
1010f4a2713aSLionel Sambuc 
1011f4a2713aSLionel Sambuc     if (RefParamComparisons && ParamRef && ArgRef) {
1012f4a2713aSLionel Sambuc       // C++0x [temp.deduct.partial]p6:
1013f4a2713aSLionel Sambuc       //   If both P and A were reference types (before being replaced with the
1014f4a2713aSLionel Sambuc       //   type referred to above), determine which of the two types (if any) is
1015f4a2713aSLionel Sambuc       //   more cv-qualified than the other; otherwise the types are considered
1016f4a2713aSLionel Sambuc       //   to be equally cv-qualified for partial ordering purposes. The result
1017f4a2713aSLionel Sambuc       //   of this determination will be used below.
1018f4a2713aSLionel Sambuc       //
1019f4a2713aSLionel Sambuc       // We save this information for later, using it only when deduction
1020f4a2713aSLionel Sambuc       // succeeds in both directions.
1021f4a2713aSLionel Sambuc       RefParamPartialOrderingComparison Comparison;
1022f4a2713aSLionel Sambuc       Comparison.ParamIsRvalueRef = ParamRef->getAs<RValueReferenceType>();
1023f4a2713aSLionel Sambuc       Comparison.ArgIsRvalueRef = ArgRef->getAs<RValueReferenceType>();
1024f4a2713aSLionel Sambuc       Comparison.Qualifiers = NeitherMoreQualified;
1025f4a2713aSLionel Sambuc 
1026f4a2713aSLionel Sambuc       Qualifiers ParamQuals = Param.getQualifiers();
1027f4a2713aSLionel Sambuc       Qualifiers ArgQuals = Arg.getQualifiers();
1028f4a2713aSLionel Sambuc       if (ParamQuals.isStrictSupersetOf(ArgQuals))
1029f4a2713aSLionel Sambuc         Comparison.Qualifiers = ParamMoreQualified;
1030f4a2713aSLionel Sambuc       else if (ArgQuals.isStrictSupersetOf(ParamQuals))
1031f4a2713aSLionel Sambuc         Comparison.Qualifiers = ArgMoreQualified;
1032*0a6a1f1dSLionel Sambuc       else if (ArgQuals.getObjCLifetime() != ParamQuals.getObjCLifetime() &&
1033*0a6a1f1dSLionel Sambuc                ArgQuals.withoutObjCLifetime()
1034*0a6a1f1dSLionel Sambuc                  == ParamQuals.withoutObjCLifetime()) {
1035*0a6a1f1dSLionel Sambuc         // Prefer binding to non-__unsafe_autoretained parameters.
1036*0a6a1f1dSLionel Sambuc         if (ArgQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone &&
1037*0a6a1f1dSLionel Sambuc             ParamQuals.getObjCLifetime())
1038*0a6a1f1dSLionel Sambuc           Comparison.Qualifiers = ParamMoreQualified;
1039*0a6a1f1dSLionel Sambuc         else if (ParamQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone &&
1040*0a6a1f1dSLionel Sambuc                  ArgQuals.getObjCLifetime())
1041*0a6a1f1dSLionel Sambuc           Comparison.Qualifiers = ArgMoreQualified;
1042*0a6a1f1dSLionel Sambuc       }
1043f4a2713aSLionel Sambuc       RefParamComparisons->push_back(Comparison);
1044f4a2713aSLionel Sambuc     }
1045f4a2713aSLionel Sambuc 
1046f4a2713aSLionel Sambuc     // C++0x [temp.deduct.partial]p7:
1047f4a2713aSLionel Sambuc     //   Remove any top-level cv-qualifiers:
1048f4a2713aSLionel Sambuc     //     - If P is a cv-qualified type, P is replaced by the cv-unqualified
1049f4a2713aSLionel Sambuc     //       version of P.
1050f4a2713aSLionel Sambuc     Param = Param.getUnqualifiedType();
1051f4a2713aSLionel Sambuc     //     - If A is a cv-qualified type, A is replaced by the cv-unqualified
1052f4a2713aSLionel Sambuc     //       version of A.
1053f4a2713aSLionel Sambuc     Arg = Arg.getUnqualifiedType();
1054f4a2713aSLionel Sambuc   } else {
1055f4a2713aSLionel Sambuc     // C++0x [temp.deduct.call]p4 bullet 1:
1056f4a2713aSLionel Sambuc     //   - If the original P is a reference type, the deduced A (i.e., the type
1057f4a2713aSLionel Sambuc     //     referred to by the reference) can be more cv-qualified than the
1058f4a2713aSLionel Sambuc     //     transformed A.
1059f4a2713aSLionel Sambuc     if (TDF & TDF_ParamWithReferenceType) {
1060f4a2713aSLionel Sambuc       Qualifiers Quals;
1061f4a2713aSLionel Sambuc       QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals);
1062f4a2713aSLionel Sambuc       Quals.setCVRQualifiers(Quals.getCVRQualifiers() &
1063f4a2713aSLionel Sambuc                              Arg.getCVRQualifiers());
1064f4a2713aSLionel Sambuc       Param = S.Context.getQualifiedType(UnqualParam, Quals);
1065f4a2713aSLionel Sambuc     }
1066f4a2713aSLionel Sambuc 
1067f4a2713aSLionel Sambuc     if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) {
1068f4a2713aSLionel Sambuc       // C++0x [temp.deduct.type]p10:
1069f4a2713aSLionel Sambuc       //   If P and A are function types that originated from deduction when
1070f4a2713aSLionel Sambuc       //   taking the address of a function template (14.8.2.2) or when deducing
1071f4a2713aSLionel Sambuc       //   template arguments from a function declaration (14.8.2.6) and Pi and
1072f4a2713aSLionel Sambuc       //   Ai are parameters of the top-level parameter-type-list of P and A,
1073f4a2713aSLionel Sambuc       //   respectively, Pi is adjusted if it is an rvalue reference to a
1074f4a2713aSLionel Sambuc       //   cv-unqualified template parameter and Ai is an lvalue reference, in
1075f4a2713aSLionel Sambuc       //   which case the type of Pi is changed to be the template parameter
1076f4a2713aSLionel Sambuc       //   type (i.e., T&& is changed to simply T). [ Note: As a result, when
1077f4a2713aSLionel Sambuc       //   Pi is T&& and Ai is X&, the adjusted Pi will be T, causing T to be
1078f4a2713aSLionel Sambuc       //   deduced as X&. - end note ]
1079f4a2713aSLionel Sambuc       TDF &= ~TDF_TopLevelParameterTypeList;
1080f4a2713aSLionel Sambuc 
1081f4a2713aSLionel Sambuc       if (const RValueReferenceType *ParamRef
1082f4a2713aSLionel Sambuc                                         = Param->getAs<RValueReferenceType>()) {
1083f4a2713aSLionel Sambuc         if (isa<TemplateTypeParmType>(ParamRef->getPointeeType()) &&
1084f4a2713aSLionel Sambuc             !ParamRef->getPointeeType().getQualifiers())
1085f4a2713aSLionel Sambuc           if (Arg->isLValueReferenceType())
1086f4a2713aSLionel Sambuc             Param = ParamRef->getPointeeType();
1087f4a2713aSLionel Sambuc       }
1088f4a2713aSLionel Sambuc     }
1089f4a2713aSLionel Sambuc   }
1090f4a2713aSLionel Sambuc 
1091f4a2713aSLionel Sambuc   // C++ [temp.deduct.type]p9:
1092f4a2713aSLionel Sambuc   //   A template type argument T, a template template argument TT or a
1093f4a2713aSLionel Sambuc   //   template non-type argument i can be deduced if P and A have one of
1094f4a2713aSLionel Sambuc   //   the following forms:
1095f4a2713aSLionel Sambuc   //
1096f4a2713aSLionel Sambuc   //     T
1097f4a2713aSLionel Sambuc   //     cv-list T
1098f4a2713aSLionel Sambuc   if (const TemplateTypeParmType *TemplateTypeParm
1099f4a2713aSLionel Sambuc         = Param->getAs<TemplateTypeParmType>()) {
1100f4a2713aSLionel Sambuc     // Just skip any attempts to deduce from a placeholder type.
1101f4a2713aSLionel Sambuc     if (Arg->isPlaceholderType())
1102f4a2713aSLionel Sambuc       return Sema::TDK_Success;
1103f4a2713aSLionel Sambuc 
1104f4a2713aSLionel Sambuc     unsigned Index = TemplateTypeParm->getIndex();
1105f4a2713aSLionel Sambuc     bool RecanonicalizeArg = false;
1106f4a2713aSLionel Sambuc 
1107f4a2713aSLionel Sambuc     // If the argument type is an array type, move the qualifiers up to the
1108f4a2713aSLionel Sambuc     // top level, so they can be matched with the qualifiers on the parameter.
1109f4a2713aSLionel Sambuc     if (isa<ArrayType>(Arg)) {
1110f4a2713aSLionel Sambuc       Qualifiers Quals;
1111f4a2713aSLionel Sambuc       Arg = S.Context.getUnqualifiedArrayType(Arg, Quals);
1112f4a2713aSLionel Sambuc       if (Quals) {
1113f4a2713aSLionel Sambuc         Arg = S.Context.getQualifiedType(Arg, Quals);
1114f4a2713aSLionel Sambuc         RecanonicalizeArg = true;
1115f4a2713aSLionel Sambuc       }
1116f4a2713aSLionel Sambuc     }
1117f4a2713aSLionel Sambuc 
1118f4a2713aSLionel Sambuc     // The argument type can not be less qualified than the parameter
1119f4a2713aSLionel Sambuc     // type.
1120f4a2713aSLionel Sambuc     if (!(TDF & TDF_IgnoreQualifiers) &&
1121f4a2713aSLionel Sambuc         hasInconsistentOrSupersetQualifiersOf(Param, Arg)) {
1122f4a2713aSLionel Sambuc       Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
1123f4a2713aSLionel Sambuc       Info.FirstArg = TemplateArgument(Param);
1124f4a2713aSLionel Sambuc       Info.SecondArg = TemplateArgument(Arg);
1125f4a2713aSLionel Sambuc       return Sema::TDK_Underqualified;
1126f4a2713aSLionel Sambuc     }
1127f4a2713aSLionel Sambuc 
1128f4a2713aSLionel Sambuc     assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
1129f4a2713aSLionel Sambuc     assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function");
1130f4a2713aSLionel Sambuc     QualType DeducedType = Arg;
1131f4a2713aSLionel Sambuc 
1132f4a2713aSLionel Sambuc     // Remove any qualifiers on the parameter from the deduced type.
1133f4a2713aSLionel Sambuc     // We checked the qualifiers for consistency above.
1134f4a2713aSLionel Sambuc     Qualifiers DeducedQs = DeducedType.getQualifiers();
1135f4a2713aSLionel Sambuc     Qualifiers ParamQs = Param.getQualifiers();
1136f4a2713aSLionel Sambuc     DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers());
1137f4a2713aSLionel Sambuc     if (ParamQs.hasObjCGCAttr())
1138f4a2713aSLionel Sambuc       DeducedQs.removeObjCGCAttr();
1139f4a2713aSLionel Sambuc     if (ParamQs.hasAddressSpace())
1140f4a2713aSLionel Sambuc       DeducedQs.removeAddressSpace();
1141f4a2713aSLionel Sambuc     if (ParamQs.hasObjCLifetime())
1142f4a2713aSLionel Sambuc       DeducedQs.removeObjCLifetime();
1143f4a2713aSLionel Sambuc 
1144f4a2713aSLionel Sambuc     // Objective-C ARC:
1145f4a2713aSLionel Sambuc     //   If template deduction would produce a lifetime qualifier on a type
1146f4a2713aSLionel Sambuc     //   that is not a lifetime type, template argument deduction fails.
1147f4a2713aSLionel Sambuc     if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType() &&
1148f4a2713aSLionel Sambuc         !DeducedType->isDependentType()) {
1149f4a2713aSLionel Sambuc       Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
1150f4a2713aSLionel Sambuc       Info.FirstArg = TemplateArgument(Param);
1151f4a2713aSLionel Sambuc       Info.SecondArg = TemplateArgument(Arg);
1152f4a2713aSLionel Sambuc       return Sema::TDK_Underqualified;
1153f4a2713aSLionel Sambuc     }
1154f4a2713aSLionel Sambuc 
1155f4a2713aSLionel Sambuc     // Objective-C ARC:
1156f4a2713aSLionel Sambuc     //   If template deduction would produce an argument type with lifetime type
1157f4a2713aSLionel Sambuc     //   but no lifetime qualifier, the __strong lifetime qualifier is inferred.
1158f4a2713aSLionel Sambuc     if (S.getLangOpts().ObjCAutoRefCount &&
1159f4a2713aSLionel Sambuc         DeducedType->isObjCLifetimeType() &&
1160f4a2713aSLionel Sambuc         !DeducedQs.hasObjCLifetime())
1161f4a2713aSLionel Sambuc       DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong);
1162f4a2713aSLionel Sambuc 
1163f4a2713aSLionel Sambuc     DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(),
1164f4a2713aSLionel Sambuc                                              DeducedQs);
1165f4a2713aSLionel Sambuc 
1166f4a2713aSLionel Sambuc     if (RecanonicalizeArg)
1167f4a2713aSLionel Sambuc       DeducedType = S.Context.getCanonicalType(DeducedType);
1168f4a2713aSLionel Sambuc 
1169f4a2713aSLionel Sambuc     DeducedTemplateArgument NewDeduced(DeducedType);
1170f4a2713aSLionel Sambuc     DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
1171f4a2713aSLionel Sambuc                                                                  Deduced[Index],
1172f4a2713aSLionel Sambuc                                                                    NewDeduced);
1173f4a2713aSLionel Sambuc     if (Result.isNull()) {
1174f4a2713aSLionel Sambuc       Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
1175f4a2713aSLionel Sambuc       Info.FirstArg = Deduced[Index];
1176f4a2713aSLionel Sambuc       Info.SecondArg = NewDeduced;
1177f4a2713aSLionel Sambuc       return Sema::TDK_Inconsistent;
1178f4a2713aSLionel Sambuc     }
1179f4a2713aSLionel Sambuc 
1180f4a2713aSLionel Sambuc     Deduced[Index] = Result;
1181f4a2713aSLionel Sambuc     return Sema::TDK_Success;
1182f4a2713aSLionel Sambuc   }
1183f4a2713aSLionel Sambuc 
1184f4a2713aSLionel Sambuc   // Set up the template argument deduction information for a failure.
1185f4a2713aSLionel Sambuc   Info.FirstArg = TemplateArgument(ParamIn);
1186f4a2713aSLionel Sambuc   Info.SecondArg = TemplateArgument(ArgIn);
1187f4a2713aSLionel Sambuc 
1188f4a2713aSLionel Sambuc   // If the parameter is an already-substituted template parameter
1189f4a2713aSLionel Sambuc   // pack, do nothing: we don't know which of its arguments to look
1190f4a2713aSLionel Sambuc   // at, so we have to wait until all of the parameter packs in this
1191f4a2713aSLionel Sambuc   // expansion have arguments.
1192f4a2713aSLionel Sambuc   if (isa<SubstTemplateTypeParmPackType>(Param))
1193f4a2713aSLionel Sambuc     return Sema::TDK_Success;
1194f4a2713aSLionel Sambuc 
1195f4a2713aSLionel Sambuc   // Check the cv-qualifiers on the parameter and argument types.
1196f4a2713aSLionel Sambuc   CanQualType CanParam = S.Context.getCanonicalType(Param);
1197f4a2713aSLionel Sambuc   CanQualType CanArg = S.Context.getCanonicalType(Arg);
1198f4a2713aSLionel Sambuc   if (!(TDF & TDF_IgnoreQualifiers)) {
1199f4a2713aSLionel Sambuc     if (TDF & TDF_ParamWithReferenceType) {
1200f4a2713aSLionel Sambuc       if (hasInconsistentOrSupersetQualifiersOf(Param, Arg))
1201f4a2713aSLionel Sambuc         return Sema::TDK_NonDeducedMismatch;
1202f4a2713aSLionel Sambuc     } else if (!IsPossiblyOpaquelyQualifiedType(Param)) {
1203f4a2713aSLionel Sambuc       if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
1204f4a2713aSLionel Sambuc         return Sema::TDK_NonDeducedMismatch;
1205f4a2713aSLionel Sambuc     }
1206f4a2713aSLionel Sambuc 
1207f4a2713aSLionel Sambuc     // If the parameter type is not dependent, there is nothing to deduce.
1208f4a2713aSLionel Sambuc     if (!Param->isDependentType()) {
1209f4a2713aSLionel Sambuc       if (!(TDF & TDF_SkipNonDependent)) {
1210f4a2713aSLionel Sambuc         bool NonDeduced = (TDF & TDF_InOverloadResolution)?
1211f4a2713aSLionel Sambuc                           !S.isSameOrCompatibleFunctionType(CanParam, CanArg) :
1212f4a2713aSLionel Sambuc                           Param != Arg;
1213f4a2713aSLionel Sambuc         if (NonDeduced) {
1214f4a2713aSLionel Sambuc           return Sema::TDK_NonDeducedMismatch;
1215f4a2713aSLionel Sambuc         }
1216f4a2713aSLionel Sambuc       }
1217f4a2713aSLionel Sambuc       return Sema::TDK_Success;
1218f4a2713aSLionel Sambuc     }
1219f4a2713aSLionel Sambuc   } else if (!Param->isDependentType()) {
1220f4a2713aSLionel Sambuc     CanQualType ParamUnqualType = CanParam.getUnqualifiedType(),
1221f4a2713aSLionel Sambuc                 ArgUnqualType = CanArg.getUnqualifiedType();
1222f4a2713aSLionel Sambuc     bool Success = (TDF & TDF_InOverloadResolution)?
1223f4a2713aSLionel Sambuc                    S.isSameOrCompatibleFunctionType(ParamUnqualType,
1224f4a2713aSLionel Sambuc                                                     ArgUnqualType) :
1225f4a2713aSLionel Sambuc                    ParamUnqualType == ArgUnqualType;
1226f4a2713aSLionel Sambuc     if (Success)
1227f4a2713aSLionel Sambuc       return Sema::TDK_Success;
1228f4a2713aSLionel Sambuc   }
1229f4a2713aSLionel Sambuc 
1230f4a2713aSLionel Sambuc   switch (Param->getTypeClass()) {
1231f4a2713aSLionel Sambuc     // Non-canonical types cannot appear here.
1232f4a2713aSLionel Sambuc #define NON_CANONICAL_TYPE(Class, Base) \
1233f4a2713aSLionel Sambuc   case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class);
1234f4a2713aSLionel Sambuc #define TYPE(Class, Base)
1235f4a2713aSLionel Sambuc #include "clang/AST/TypeNodes.def"
1236f4a2713aSLionel Sambuc 
1237f4a2713aSLionel Sambuc     case Type::TemplateTypeParm:
1238f4a2713aSLionel Sambuc     case Type::SubstTemplateTypeParmPack:
1239f4a2713aSLionel Sambuc       llvm_unreachable("Type nodes handled above");
1240f4a2713aSLionel Sambuc 
1241f4a2713aSLionel Sambuc     // These types cannot be dependent, so simply check whether the types are
1242f4a2713aSLionel Sambuc     // the same.
1243f4a2713aSLionel Sambuc     case Type::Builtin:
1244f4a2713aSLionel Sambuc     case Type::VariableArray:
1245f4a2713aSLionel Sambuc     case Type::Vector:
1246f4a2713aSLionel Sambuc     case Type::FunctionNoProto:
1247f4a2713aSLionel Sambuc     case Type::Record:
1248f4a2713aSLionel Sambuc     case Type::Enum:
1249f4a2713aSLionel Sambuc     case Type::ObjCObject:
1250f4a2713aSLionel Sambuc     case Type::ObjCInterface:
1251f4a2713aSLionel Sambuc     case Type::ObjCObjectPointer: {
1252f4a2713aSLionel Sambuc       if (TDF & TDF_SkipNonDependent)
1253f4a2713aSLionel Sambuc         return Sema::TDK_Success;
1254f4a2713aSLionel Sambuc 
1255f4a2713aSLionel Sambuc       if (TDF & TDF_IgnoreQualifiers) {
1256f4a2713aSLionel Sambuc         Param = Param.getUnqualifiedType();
1257f4a2713aSLionel Sambuc         Arg = Arg.getUnqualifiedType();
1258f4a2713aSLionel Sambuc       }
1259f4a2713aSLionel Sambuc 
1260f4a2713aSLionel Sambuc       return Param == Arg? Sema::TDK_Success : Sema::TDK_NonDeducedMismatch;
1261f4a2713aSLionel Sambuc     }
1262f4a2713aSLionel Sambuc 
1263f4a2713aSLionel Sambuc     //     _Complex T   [placeholder extension]
1264f4a2713aSLionel Sambuc     case Type::Complex:
1265f4a2713aSLionel Sambuc       if (const ComplexType *ComplexArg = Arg->getAs<ComplexType>())
1266f4a2713aSLionel Sambuc         return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1267f4a2713aSLionel Sambuc                                     cast<ComplexType>(Param)->getElementType(),
1268f4a2713aSLionel Sambuc                                     ComplexArg->getElementType(),
1269f4a2713aSLionel Sambuc                                     Info, Deduced, TDF);
1270f4a2713aSLionel Sambuc 
1271f4a2713aSLionel Sambuc       return Sema::TDK_NonDeducedMismatch;
1272f4a2713aSLionel Sambuc 
1273f4a2713aSLionel Sambuc     //     _Atomic T   [extension]
1274f4a2713aSLionel Sambuc     case Type::Atomic:
1275f4a2713aSLionel Sambuc       if (const AtomicType *AtomicArg = Arg->getAs<AtomicType>())
1276f4a2713aSLionel Sambuc         return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1277f4a2713aSLionel Sambuc                                        cast<AtomicType>(Param)->getValueType(),
1278f4a2713aSLionel Sambuc                                        AtomicArg->getValueType(),
1279f4a2713aSLionel Sambuc                                        Info, Deduced, TDF);
1280f4a2713aSLionel Sambuc 
1281f4a2713aSLionel Sambuc       return Sema::TDK_NonDeducedMismatch;
1282f4a2713aSLionel Sambuc 
1283f4a2713aSLionel Sambuc     //     T *
1284f4a2713aSLionel Sambuc     case Type::Pointer: {
1285f4a2713aSLionel Sambuc       QualType PointeeType;
1286f4a2713aSLionel Sambuc       if (const PointerType *PointerArg = Arg->getAs<PointerType>()) {
1287f4a2713aSLionel Sambuc         PointeeType = PointerArg->getPointeeType();
1288f4a2713aSLionel Sambuc       } else if (const ObjCObjectPointerType *PointerArg
1289f4a2713aSLionel Sambuc                    = Arg->getAs<ObjCObjectPointerType>()) {
1290f4a2713aSLionel Sambuc         PointeeType = PointerArg->getPointeeType();
1291f4a2713aSLionel Sambuc       } else {
1292f4a2713aSLionel Sambuc         return Sema::TDK_NonDeducedMismatch;
1293f4a2713aSLionel Sambuc       }
1294f4a2713aSLionel Sambuc 
1295f4a2713aSLionel Sambuc       unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
1296f4a2713aSLionel Sambuc       return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1297f4a2713aSLionel Sambuc                                      cast<PointerType>(Param)->getPointeeType(),
1298f4a2713aSLionel Sambuc                                      PointeeType,
1299f4a2713aSLionel Sambuc                                      Info, Deduced, SubTDF);
1300f4a2713aSLionel Sambuc     }
1301f4a2713aSLionel Sambuc 
1302f4a2713aSLionel Sambuc     //     T &
1303f4a2713aSLionel Sambuc     case Type::LValueReference: {
1304*0a6a1f1dSLionel Sambuc       const LValueReferenceType *ReferenceArg =
1305*0a6a1f1dSLionel Sambuc           Arg->getAs<LValueReferenceType>();
1306f4a2713aSLionel Sambuc       if (!ReferenceArg)
1307f4a2713aSLionel Sambuc         return Sema::TDK_NonDeducedMismatch;
1308f4a2713aSLionel Sambuc 
1309f4a2713aSLionel Sambuc       return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1310f4a2713aSLionel Sambuc                            cast<LValueReferenceType>(Param)->getPointeeType(),
1311f4a2713aSLionel Sambuc                            ReferenceArg->getPointeeType(), Info, Deduced, 0);
1312f4a2713aSLionel Sambuc     }
1313f4a2713aSLionel Sambuc 
1314f4a2713aSLionel Sambuc     //     T && [C++0x]
1315f4a2713aSLionel Sambuc     case Type::RValueReference: {
1316*0a6a1f1dSLionel Sambuc       const RValueReferenceType *ReferenceArg =
1317*0a6a1f1dSLionel Sambuc           Arg->getAs<RValueReferenceType>();
1318f4a2713aSLionel Sambuc       if (!ReferenceArg)
1319f4a2713aSLionel Sambuc         return Sema::TDK_NonDeducedMismatch;
1320f4a2713aSLionel Sambuc 
1321f4a2713aSLionel Sambuc       return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1322f4a2713aSLionel Sambuc                              cast<RValueReferenceType>(Param)->getPointeeType(),
1323f4a2713aSLionel Sambuc                              ReferenceArg->getPointeeType(),
1324f4a2713aSLionel Sambuc                              Info, Deduced, 0);
1325f4a2713aSLionel Sambuc     }
1326f4a2713aSLionel Sambuc 
1327f4a2713aSLionel Sambuc     //     T [] (implied, but not stated explicitly)
1328f4a2713aSLionel Sambuc     case Type::IncompleteArray: {
1329f4a2713aSLionel Sambuc       const IncompleteArrayType *IncompleteArrayArg =
1330f4a2713aSLionel Sambuc         S.Context.getAsIncompleteArrayType(Arg);
1331f4a2713aSLionel Sambuc       if (!IncompleteArrayArg)
1332f4a2713aSLionel Sambuc         return Sema::TDK_NonDeducedMismatch;
1333f4a2713aSLionel Sambuc 
1334f4a2713aSLionel Sambuc       unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
1335f4a2713aSLionel Sambuc       return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1336f4a2713aSLionel Sambuc                     S.Context.getAsIncompleteArrayType(Param)->getElementType(),
1337f4a2713aSLionel Sambuc                     IncompleteArrayArg->getElementType(),
1338f4a2713aSLionel Sambuc                     Info, Deduced, SubTDF);
1339f4a2713aSLionel Sambuc     }
1340f4a2713aSLionel Sambuc 
1341f4a2713aSLionel Sambuc     //     T [integer-constant]
1342f4a2713aSLionel Sambuc     case Type::ConstantArray: {
1343f4a2713aSLionel Sambuc       const ConstantArrayType *ConstantArrayArg =
1344f4a2713aSLionel Sambuc         S.Context.getAsConstantArrayType(Arg);
1345f4a2713aSLionel Sambuc       if (!ConstantArrayArg)
1346f4a2713aSLionel Sambuc         return Sema::TDK_NonDeducedMismatch;
1347f4a2713aSLionel Sambuc 
1348f4a2713aSLionel Sambuc       const ConstantArrayType *ConstantArrayParm =
1349f4a2713aSLionel Sambuc         S.Context.getAsConstantArrayType(Param);
1350f4a2713aSLionel Sambuc       if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
1351f4a2713aSLionel Sambuc         return Sema::TDK_NonDeducedMismatch;
1352f4a2713aSLionel Sambuc 
1353f4a2713aSLionel Sambuc       unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
1354f4a2713aSLionel Sambuc       return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1355f4a2713aSLionel Sambuc                                            ConstantArrayParm->getElementType(),
1356f4a2713aSLionel Sambuc                                            ConstantArrayArg->getElementType(),
1357f4a2713aSLionel Sambuc                                            Info, Deduced, SubTDF);
1358f4a2713aSLionel Sambuc     }
1359f4a2713aSLionel Sambuc 
1360f4a2713aSLionel Sambuc     //     type [i]
1361f4a2713aSLionel Sambuc     case Type::DependentSizedArray: {
1362f4a2713aSLionel Sambuc       const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
1363f4a2713aSLionel Sambuc       if (!ArrayArg)
1364f4a2713aSLionel Sambuc         return Sema::TDK_NonDeducedMismatch;
1365f4a2713aSLionel Sambuc 
1366f4a2713aSLionel Sambuc       unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
1367f4a2713aSLionel Sambuc 
1368f4a2713aSLionel Sambuc       // Check the element type of the arrays
1369f4a2713aSLionel Sambuc       const DependentSizedArrayType *DependentArrayParm
1370f4a2713aSLionel Sambuc         = S.Context.getAsDependentSizedArrayType(Param);
1371f4a2713aSLionel Sambuc       if (Sema::TemplateDeductionResult Result
1372f4a2713aSLionel Sambuc             = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1373f4a2713aSLionel Sambuc                                           DependentArrayParm->getElementType(),
1374f4a2713aSLionel Sambuc                                           ArrayArg->getElementType(),
1375f4a2713aSLionel Sambuc                                           Info, Deduced, SubTDF))
1376f4a2713aSLionel Sambuc         return Result;
1377f4a2713aSLionel Sambuc 
1378f4a2713aSLionel Sambuc       // Determine the array bound is something we can deduce.
1379f4a2713aSLionel Sambuc       NonTypeTemplateParmDecl *NTTP
1380f4a2713aSLionel Sambuc         = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
1381f4a2713aSLionel Sambuc       if (!NTTP)
1382f4a2713aSLionel Sambuc         return Sema::TDK_Success;
1383f4a2713aSLionel Sambuc 
1384f4a2713aSLionel Sambuc       // We can perform template argument deduction for the given non-type
1385f4a2713aSLionel Sambuc       // template parameter.
1386f4a2713aSLionel Sambuc       assert(NTTP->getDepth() == 0 &&
1387f4a2713aSLionel Sambuc              "Cannot deduce non-type template argument at depth > 0");
1388f4a2713aSLionel Sambuc       if (const ConstantArrayType *ConstantArrayArg
1389f4a2713aSLionel Sambuc             = dyn_cast<ConstantArrayType>(ArrayArg)) {
1390f4a2713aSLionel Sambuc         llvm::APSInt Size(ConstantArrayArg->getSize());
1391f4a2713aSLionel Sambuc         return DeduceNonTypeTemplateArgument(S, NTTP, Size,
1392f4a2713aSLionel Sambuc                                              S.Context.getSizeType(),
1393f4a2713aSLionel Sambuc                                              /*ArrayBound=*/true,
1394f4a2713aSLionel Sambuc                                              Info, Deduced);
1395f4a2713aSLionel Sambuc       }
1396f4a2713aSLionel Sambuc       if (const DependentSizedArrayType *DependentArrayArg
1397f4a2713aSLionel Sambuc             = dyn_cast<DependentSizedArrayType>(ArrayArg))
1398f4a2713aSLionel Sambuc         if (DependentArrayArg->getSizeExpr())
1399f4a2713aSLionel Sambuc           return DeduceNonTypeTemplateArgument(S, NTTP,
1400f4a2713aSLionel Sambuc                                                DependentArrayArg->getSizeExpr(),
1401f4a2713aSLionel Sambuc                                                Info, Deduced);
1402f4a2713aSLionel Sambuc 
1403f4a2713aSLionel Sambuc       // Incomplete type does not match a dependently-sized array type
1404f4a2713aSLionel Sambuc       return Sema::TDK_NonDeducedMismatch;
1405f4a2713aSLionel Sambuc     }
1406f4a2713aSLionel Sambuc 
1407f4a2713aSLionel Sambuc     //     type(*)(T)
1408f4a2713aSLionel Sambuc     //     T(*)()
1409f4a2713aSLionel Sambuc     //     T(*)(T)
1410f4a2713aSLionel Sambuc     case Type::FunctionProto: {
1411f4a2713aSLionel Sambuc       unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList;
1412f4a2713aSLionel Sambuc       const FunctionProtoType *FunctionProtoArg =
1413f4a2713aSLionel Sambuc         dyn_cast<FunctionProtoType>(Arg);
1414f4a2713aSLionel Sambuc       if (!FunctionProtoArg)
1415f4a2713aSLionel Sambuc         return Sema::TDK_NonDeducedMismatch;
1416f4a2713aSLionel Sambuc 
1417f4a2713aSLionel Sambuc       const FunctionProtoType *FunctionProtoParam =
1418f4a2713aSLionel Sambuc         cast<FunctionProtoType>(Param);
1419f4a2713aSLionel Sambuc 
1420f4a2713aSLionel Sambuc       if (FunctionProtoParam->getTypeQuals()
1421f4a2713aSLionel Sambuc             != FunctionProtoArg->getTypeQuals() ||
1422f4a2713aSLionel Sambuc           FunctionProtoParam->getRefQualifier()
1423f4a2713aSLionel Sambuc             != FunctionProtoArg->getRefQualifier() ||
1424f4a2713aSLionel Sambuc           FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
1425f4a2713aSLionel Sambuc         return Sema::TDK_NonDeducedMismatch;
1426f4a2713aSLionel Sambuc 
1427f4a2713aSLionel Sambuc       // Check return types.
1428*0a6a1f1dSLionel Sambuc       if (Sema::TemplateDeductionResult Result =
1429*0a6a1f1dSLionel Sambuc               DeduceTemplateArgumentsByTypeMatch(
1430*0a6a1f1dSLionel Sambuc                   S, TemplateParams, FunctionProtoParam->getReturnType(),
1431*0a6a1f1dSLionel Sambuc                   FunctionProtoArg->getReturnType(), Info, Deduced, 0))
1432f4a2713aSLionel Sambuc         return Result;
1433f4a2713aSLionel Sambuc 
1434*0a6a1f1dSLionel Sambuc       return DeduceTemplateArguments(
1435*0a6a1f1dSLionel Sambuc           S, TemplateParams, FunctionProtoParam->param_type_begin(),
1436*0a6a1f1dSLionel Sambuc           FunctionProtoParam->getNumParams(),
1437*0a6a1f1dSLionel Sambuc           FunctionProtoArg->param_type_begin(),
1438*0a6a1f1dSLionel Sambuc           FunctionProtoArg->getNumParams(), Info, Deduced, SubTDF);
1439f4a2713aSLionel Sambuc     }
1440f4a2713aSLionel Sambuc 
1441f4a2713aSLionel Sambuc     case Type::InjectedClassName: {
1442f4a2713aSLionel Sambuc       // Treat a template's injected-class-name as if the template
1443f4a2713aSLionel Sambuc       // specialization type had been used.
1444f4a2713aSLionel Sambuc       Param = cast<InjectedClassNameType>(Param)
1445f4a2713aSLionel Sambuc         ->getInjectedSpecializationType();
1446f4a2713aSLionel Sambuc       assert(isa<TemplateSpecializationType>(Param) &&
1447f4a2713aSLionel Sambuc              "injected class name is not a template specialization type");
1448f4a2713aSLionel Sambuc       // fall through
1449f4a2713aSLionel Sambuc     }
1450f4a2713aSLionel Sambuc 
1451f4a2713aSLionel Sambuc     //     template-name<T> (where template-name refers to a class template)
1452f4a2713aSLionel Sambuc     //     template-name<i>
1453f4a2713aSLionel Sambuc     //     TT<T>
1454f4a2713aSLionel Sambuc     //     TT<i>
1455f4a2713aSLionel Sambuc     //     TT<>
1456f4a2713aSLionel Sambuc     case Type::TemplateSpecialization: {
1457f4a2713aSLionel Sambuc       const TemplateSpecializationType *SpecParam
1458f4a2713aSLionel Sambuc         = cast<TemplateSpecializationType>(Param);
1459f4a2713aSLionel Sambuc 
1460f4a2713aSLionel Sambuc       // Try to deduce template arguments from the template-id.
1461f4a2713aSLionel Sambuc       Sema::TemplateDeductionResult Result
1462f4a2713aSLionel Sambuc         = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
1463f4a2713aSLionel Sambuc                                   Info, Deduced);
1464f4a2713aSLionel Sambuc 
1465f4a2713aSLionel Sambuc       if (Result && (TDF & TDF_DerivedClass)) {
1466f4a2713aSLionel Sambuc         // C++ [temp.deduct.call]p3b3:
1467f4a2713aSLionel Sambuc         //   If P is a class, and P has the form template-id, then A can be a
1468f4a2713aSLionel Sambuc         //   derived class of the deduced A. Likewise, if P is a pointer to a
1469f4a2713aSLionel Sambuc         //   class of the form template-id, A can be a pointer to a derived
1470f4a2713aSLionel Sambuc         //   class pointed to by the deduced A.
1471f4a2713aSLionel Sambuc         //
1472f4a2713aSLionel Sambuc         // More importantly:
1473f4a2713aSLionel Sambuc         //   These alternatives are considered only if type deduction would
1474f4a2713aSLionel Sambuc         //   otherwise fail.
1475f4a2713aSLionel Sambuc         if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
1476f4a2713aSLionel Sambuc           // We cannot inspect base classes as part of deduction when the type
1477f4a2713aSLionel Sambuc           // is incomplete, so either instantiate any templates necessary to
1478f4a2713aSLionel Sambuc           // complete the type, or skip over it if it cannot be completed.
1479f4a2713aSLionel Sambuc           if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
1480f4a2713aSLionel Sambuc             return Result;
1481f4a2713aSLionel Sambuc 
1482f4a2713aSLionel Sambuc           // Use data recursion to crawl through the list of base classes.
1483f4a2713aSLionel Sambuc           // Visited contains the set of nodes we have already visited, while
1484f4a2713aSLionel Sambuc           // ToVisit is our stack of records that we still need to visit.
1485f4a2713aSLionel Sambuc           llvm::SmallPtrSet<const RecordType *, 8> Visited;
1486f4a2713aSLionel Sambuc           SmallVector<const RecordType *, 8> ToVisit;
1487f4a2713aSLionel Sambuc           ToVisit.push_back(RecordT);
1488f4a2713aSLionel Sambuc           bool Successful = false;
1489f4a2713aSLionel Sambuc           SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(),
1490f4a2713aSLionel Sambuc                                                               Deduced.end());
1491f4a2713aSLionel Sambuc           while (!ToVisit.empty()) {
1492f4a2713aSLionel Sambuc             // Retrieve the next class in the inheritance hierarchy.
1493f4a2713aSLionel Sambuc             const RecordType *NextT = ToVisit.pop_back_val();
1494f4a2713aSLionel Sambuc 
1495f4a2713aSLionel Sambuc             // If we have already seen this type, skip it.
1496*0a6a1f1dSLionel Sambuc             if (!Visited.insert(NextT).second)
1497f4a2713aSLionel Sambuc               continue;
1498f4a2713aSLionel Sambuc 
1499f4a2713aSLionel Sambuc             // If this is a base class, try to perform template argument
1500f4a2713aSLionel Sambuc             // deduction from it.
1501f4a2713aSLionel Sambuc             if (NextT != RecordT) {
1502f4a2713aSLionel Sambuc               TemplateDeductionInfo BaseInfo(Info.getLocation());
1503f4a2713aSLionel Sambuc               Sema::TemplateDeductionResult BaseResult
1504f4a2713aSLionel Sambuc                 = DeduceTemplateArguments(S, TemplateParams, SpecParam,
1505f4a2713aSLionel Sambuc                                           QualType(NextT, 0), BaseInfo,
1506f4a2713aSLionel Sambuc                                           Deduced);
1507f4a2713aSLionel Sambuc 
1508f4a2713aSLionel Sambuc               // If template argument deduction for this base was successful,
1509f4a2713aSLionel Sambuc               // note that we had some success. Otherwise, ignore any deductions
1510f4a2713aSLionel Sambuc               // from this base class.
1511f4a2713aSLionel Sambuc               if (BaseResult == Sema::TDK_Success) {
1512f4a2713aSLionel Sambuc                 Successful = true;
1513f4a2713aSLionel Sambuc                 DeducedOrig.clear();
1514f4a2713aSLionel Sambuc                 DeducedOrig.append(Deduced.begin(), Deduced.end());
1515f4a2713aSLionel Sambuc                 Info.Param = BaseInfo.Param;
1516f4a2713aSLionel Sambuc                 Info.FirstArg = BaseInfo.FirstArg;
1517f4a2713aSLionel Sambuc                 Info.SecondArg = BaseInfo.SecondArg;
1518f4a2713aSLionel Sambuc               }
1519f4a2713aSLionel Sambuc               else
1520f4a2713aSLionel Sambuc                 Deduced = DeducedOrig;
1521f4a2713aSLionel Sambuc             }
1522f4a2713aSLionel Sambuc 
1523f4a2713aSLionel Sambuc             // Visit base classes
1524f4a2713aSLionel Sambuc             CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
1525*0a6a1f1dSLionel Sambuc             for (const auto &Base : Next->bases()) {
1526*0a6a1f1dSLionel Sambuc               assert(Base.getType()->isRecordType() &&
1527f4a2713aSLionel Sambuc                      "Base class that isn't a record?");
1528*0a6a1f1dSLionel Sambuc               ToVisit.push_back(Base.getType()->getAs<RecordType>());
1529f4a2713aSLionel Sambuc             }
1530f4a2713aSLionel Sambuc           }
1531f4a2713aSLionel Sambuc 
1532f4a2713aSLionel Sambuc           if (Successful)
1533f4a2713aSLionel Sambuc             return Sema::TDK_Success;
1534f4a2713aSLionel Sambuc         }
1535f4a2713aSLionel Sambuc 
1536f4a2713aSLionel Sambuc       }
1537f4a2713aSLionel Sambuc 
1538f4a2713aSLionel Sambuc       return Result;
1539f4a2713aSLionel Sambuc     }
1540f4a2713aSLionel Sambuc 
1541f4a2713aSLionel Sambuc     //     T type::*
1542f4a2713aSLionel Sambuc     //     T T::*
1543f4a2713aSLionel Sambuc     //     T (type::*)()
1544f4a2713aSLionel Sambuc     //     type (T::*)()
1545f4a2713aSLionel Sambuc     //     type (type::*)(T)
1546f4a2713aSLionel Sambuc     //     type (T::*)(T)
1547f4a2713aSLionel Sambuc     //     T (type::*)(T)
1548f4a2713aSLionel Sambuc     //     T (T::*)()
1549f4a2713aSLionel Sambuc     //     T (T::*)(T)
1550f4a2713aSLionel Sambuc     case Type::MemberPointer: {
1551f4a2713aSLionel Sambuc       const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
1552f4a2713aSLionel Sambuc       const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
1553f4a2713aSLionel Sambuc       if (!MemPtrArg)
1554f4a2713aSLionel Sambuc         return Sema::TDK_NonDeducedMismatch;
1555f4a2713aSLionel Sambuc 
1556f4a2713aSLionel Sambuc       if (Sema::TemplateDeductionResult Result
1557f4a2713aSLionel Sambuc             = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1558f4a2713aSLionel Sambuc                                                  MemPtrParam->getPointeeType(),
1559f4a2713aSLionel Sambuc                                                  MemPtrArg->getPointeeType(),
1560f4a2713aSLionel Sambuc                                                  Info, Deduced,
1561f4a2713aSLionel Sambuc                                                  TDF & TDF_IgnoreQualifiers))
1562f4a2713aSLionel Sambuc         return Result;
1563f4a2713aSLionel Sambuc 
1564f4a2713aSLionel Sambuc       return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1565f4a2713aSLionel Sambuc                                            QualType(MemPtrParam->getClass(), 0),
1566f4a2713aSLionel Sambuc                                            QualType(MemPtrArg->getClass(), 0),
1567f4a2713aSLionel Sambuc                                            Info, Deduced,
1568f4a2713aSLionel Sambuc                                            TDF & TDF_IgnoreQualifiers);
1569f4a2713aSLionel Sambuc     }
1570f4a2713aSLionel Sambuc 
1571f4a2713aSLionel Sambuc     //     (clang extension)
1572f4a2713aSLionel Sambuc     //
1573f4a2713aSLionel Sambuc     //     type(^)(T)
1574f4a2713aSLionel Sambuc     //     T(^)()
1575f4a2713aSLionel Sambuc     //     T(^)(T)
1576f4a2713aSLionel Sambuc     case Type::BlockPointer: {
1577f4a2713aSLionel Sambuc       const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
1578f4a2713aSLionel Sambuc       const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
1579f4a2713aSLionel Sambuc 
1580f4a2713aSLionel Sambuc       if (!BlockPtrArg)
1581f4a2713aSLionel Sambuc         return Sema::TDK_NonDeducedMismatch;
1582f4a2713aSLionel Sambuc 
1583f4a2713aSLionel Sambuc       return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1584f4a2713aSLionel Sambuc                                                 BlockPtrParam->getPointeeType(),
1585f4a2713aSLionel Sambuc                                                 BlockPtrArg->getPointeeType(),
1586f4a2713aSLionel Sambuc                                                 Info, Deduced, 0);
1587f4a2713aSLionel Sambuc     }
1588f4a2713aSLionel Sambuc 
1589f4a2713aSLionel Sambuc     //     (clang extension)
1590f4a2713aSLionel Sambuc     //
1591f4a2713aSLionel Sambuc     //     T __attribute__(((ext_vector_type(<integral constant>))))
1592f4a2713aSLionel Sambuc     case Type::ExtVector: {
1593f4a2713aSLionel Sambuc       const ExtVectorType *VectorParam = cast<ExtVectorType>(Param);
1594f4a2713aSLionel Sambuc       if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) {
1595f4a2713aSLionel Sambuc         // Make sure that the vectors have the same number of elements.
1596f4a2713aSLionel Sambuc         if (VectorParam->getNumElements() != VectorArg->getNumElements())
1597f4a2713aSLionel Sambuc           return Sema::TDK_NonDeducedMismatch;
1598f4a2713aSLionel Sambuc 
1599f4a2713aSLionel Sambuc         // Perform deduction on the element types.
1600f4a2713aSLionel Sambuc         return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1601f4a2713aSLionel Sambuc                                                   VectorParam->getElementType(),
1602f4a2713aSLionel Sambuc                                                   VectorArg->getElementType(),
1603f4a2713aSLionel Sambuc                                                   Info, Deduced, TDF);
1604f4a2713aSLionel Sambuc       }
1605f4a2713aSLionel Sambuc 
1606f4a2713aSLionel Sambuc       if (const DependentSizedExtVectorType *VectorArg
1607f4a2713aSLionel Sambuc                                 = dyn_cast<DependentSizedExtVectorType>(Arg)) {
1608f4a2713aSLionel Sambuc         // We can't check the number of elements, since the argument has a
1609f4a2713aSLionel Sambuc         // dependent number of elements. This can only occur during partial
1610f4a2713aSLionel Sambuc         // ordering.
1611f4a2713aSLionel Sambuc 
1612f4a2713aSLionel Sambuc         // Perform deduction on the element types.
1613f4a2713aSLionel Sambuc         return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1614f4a2713aSLionel Sambuc                                                   VectorParam->getElementType(),
1615f4a2713aSLionel Sambuc                                                   VectorArg->getElementType(),
1616f4a2713aSLionel Sambuc                                                   Info, Deduced, TDF);
1617f4a2713aSLionel Sambuc       }
1618f4a2713aSLionel Sambuc 
1619f4a2713aSLionel Sambuc       return Sema::TDK_NonDeducedMismatch;
1620f4a2713aSLionel Sambuc     }
1621f4a2713aSLionel Sambuc 
1622f4a2713aSLionel Sambuc     //     (clang extension)
1623f4a2713aSLionel Sambuc     //
1624f4a2713aSLionel Sambuc     //     T __attribute__(((ext_vector_type(N))))
1625f4a2713aSLionel Sambuc     case Type::DependentSizedExtVector: {
1626f4a2713aSLionel Sambuc       const DependentSizedExtVectorType *VectorParam
1627f4a2713aSLionel Sambuc         = cast<DependentSizedExtVectorType>(Param);
1628f4a2713aSLionel Sambuc 
1629f4a2713aSLionel Sambuc       if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) {
1630f4a2713aSLionel Sambuc         // Perform deduction on the element types.
1631f4a2713aSLionel Sambuc         if (Sema::TemplateDeductionResult Result
1632f4a2713aSLionel Sambuc               = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1633f4a2713aSLionel Sambuc                                                   VectorParam->getElementType(),
1634f4a2713aSLionel Sambuc                                                    VectorArg->getElementType(),
1635f4a2713aSLionel Sambuc                                                    Info, Deduced, TDF))
1636f4a2713aSLionel Sambuc           return Result;
1637f4a2713aSLionel Sambuc 
1638f4a2713aSLionel Sambuc         // Perform deduction on the vector size, if we can.
1639f4a2713aSLionel Sambuc         NonTypeTemplateParmDecl *NTTP
1640f4a2713aSLionel Sambuc           = getDeducedParameterFromExpr(VectorParam->getSizeExpr());
1641f4a2713aSLionel Sambuc         if (!NTTP)
1642f4a2713aSLionel Sambuc           return Sema::TDK_Success;
1643f4a2713aSLionel Sambuc 
1644f4a2713aSLionel Sambuc         llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false);
1645f4a2713aSLionel Sambuc         ArgSize = VectorArg->getNumElements();
1646f4a2713aSLionel Sambuc         return DeduceNonTypeTemplateArgument(S, NTTP, ArgSize, S.Context.IntTy,
1647f4a2713aSLionel Sambuc                                              false, Info, Deduced);
1648f4a2713aSLionel Sambuc       }
1649f4a2713aSLionel Sambuc 
1650f4a2713aSLionel Sambuc       if (const DependentSizedExtVectorType *VectorArg
1651f4a2713aSLionel Sambuc                                 = dyn_cast<DependentSizedExtVectorType>(Arg)) {
1652f4a2713aSLionel Sambuc         // Perform deduction on the element types.
1653f4a2713aSLionel Sambuc         if (Sema::TemplateDeductionResult Result
1654f4a2713aSLionel Sambuc             = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1655f4a2713aSLionel Sambuc                                                  VectorParam->getElementType(),
1656f4a2713aSLionel Sambuc                                                  VectorArg->getElementType(),
1657f4a2713aSLionel Sambuc                                                  Info, Deduced, TDF))
1658f4a2713aSLionel Sambuc           return Result;
1659f4a2713aSLionel Sambuc 
1660f4a2713aSLionel Sambuc         // Perform deduction on the vector size, if we can.
1661f4a2713aSLionel Sambuc         NonTypeTemplateParmDecl *NTTP
1662f4a2713aSLionel Sambuc           = getDeducedParameterFromExpr(VectorParam->getSizeExpr());
1663f4a2713aSLionel Sambuc         if (!NTTP)
1664f4a2713aSLionel Sambuc           return Sema::TDK_Success;
1665f4a2713aSLionel Sambuc 
1666f4a2713aSLionel Sambuc         return DeduceNonTypeTemplateArgument(S, NTTP, VectorArg->getSizeExpr(),
1667f4a2713aSLionel Sambuc                                              Info, Deduced);
1668f4a2713aSLionel Sambuc       }
1669f4a2713aSLionel Sambuc 
1670f4a2713aSLionel Sambuc       return Sema::TDK_NonDeducedMismatch;
1671f4a2713aSLionel Sambuc     }
1672f4a2713aSLionel Sambuc 
1673f4a2713aSLionel Sambuc     case Type::TypeOfExpr:
1674f4a2713aSLionel Sambuc     case Type::TypeOf:
1675f4a2713aSLionel Sambuc     case Type::DependentName:
1676f4a2713aSLionel Sambuc     case Type::UnresolvedUsing:
1677f4a2713aSLionel Sambuc     case Type::Decltype:
1678f4a2713aSLionel Sambuc     case Type::UnaryTransform:
1679f4a2713aSLionel Sambuc     case Type::Auto:
1680f4a2713aSLionel Sambuc     case Type::DependentTemplateSpecialization:
1681f4a2713aSLionel Sambuc     case Type::PackExpansion:
1682f4a2713aSLionel Sambuc       // No template argument deduction for these types
1683f4a2713aSLionel Sambuc       return Sema::TDK_Success;
1684f4a2713aSLionel Sambuc   }
1685f4a2713aSLionel Sambuc 
1686f4a2713aSLionel Sambuc   llvm_unreachable("Invalid Type Class!");
1687f4a2713aSLionel Sambuc }
1688f4a2713aSLionel Sambuc 
1689f4a2713aSLionel Sambuc static Sema::TemplateDeductionResult
DeduceTemplateArguments(Sema & S,TemplateParameterList * TemplateParams,const TemplateArgument & Param,TemplateArgument Arg,TemplateDeductionInfo & Info,SmallVectorImpl<DeducedTemplateArgument> & Deduced)1690f4a2713aSLionel Sambuc DeduceTemplateArguments(Sema &S,
1691f4a2713aSLionel Sambuc                         TemplateParameterList *TemplateParams,
1692f4a2713aSLionel Sambuc                         const TemplateArgument &Param,
1693f4a2713aSLionel Sambuc                         TemplateArgument Arg,
1694f4a2713aSLionel Sambuc                         TemplateDeductionInfo &Info,
1695f4a2713aSLionel Sambuc                         SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
1696f4a2713aSLionel Sambuc   // If the template argument is a pack expansion, perform template argument
1697f4a2713aSLionel Sambuc   // deduction against the pattern of that expansion. This only occurs during
1698f4a2713aSLionel Sambuc   // partial ordering.
1699f4a2713aSLionel Sambuc   if (Arg.isPackExpansion())
1700f4a2713aSLionel Sambuc     Arg = Arg.getPackExpansionPattern();
1701f4a2713aSLionel Sambuc 
1702f4a2713aSLionel Sambuc   switch (Param.getKind()) {
1703f4a2713aSLionel Sambuc   case TemplateArgument::Null:
1704f4a2713aSLionel Sambuc     llvm_unreachable("Null template argument in parameter list");
1705f4a2713aSLionel Sambuc 
1706f4a2713aSLionel Sambuc   case TemplateArgument::Type:
1707f4a2713aSLionel Sambuc     if (Arg.getKind() == TemplateArgument::Type)
1708f4a2713aSLionel Sambuc       return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1709f4a2713aSLionel Sambuc                                                 Param.getAsType(),
1710f4a2713aSLionel Sambuc                                                 Arg.getAsType(),
1711f4a2713aSLionel Sambuc                                                 Info, Deduced, 0);
1712f4a2713aSLionel Sambuc     Info.FirstArg = Param;
1713f4a2713aSLionel Sambuc     Info.SecondArg = Arg;
1714f4a2713aSLionel Sambuc     return Sema::TDK_NonDeducedMismatch;
1715f4a2713aSLionel Sambuc 
1716f4a2713aSLionel Sambuc   case TemplateArgument::Template:
1717f4a2713aSLionel Sambuc     if (Arg.getKind() == TemplateArgument::Template)
1718f4a2713aSLionel Sambuc       return DeduceTemplateArguments(S, TemplateParams,
1719f4a2713aSLionel Sambuc                                      Param.getAsTemplate(),
1720f4a2713aSLionel Sambuc                                      Arg.getAsTemplate(), Info, Deduced);
1721f4a2713aSLionel Sambuc     Info.FirstArg = Param;
1722f4a2713aSLionel Sambuc     Info.SecondArg = Arg;
1723f4a2713aSLionel Sambuc     return Sema::TDK_NonDeducedMismatch;
1724f4a2713aSLionel Sambuc 
1725f4a2713aSLionel Sambuc   case TemplateArgument::TemplateExpansion:
1726f4a2713aSLionel Sambuc     llvm_unreachable("caller should handle pack expansions");
1727f4a2713aSLionel Sambuc 
1728f4a2713aSLionel Sambuc   case TemplateArgument::Declaration:
1729f4a2713aSLionel Sambuc     if (Arg.getKind() == TemplateArgument::Declaration &&
1730*0a6a1f1dSLionel Sambuc         isSameDeclaration(Param.getAsDecl(), Arg.getAsDecl()))
1731f4a2713aSLionel Sambuc       return Sema::TDK_Success;
1732f4a2713aSLionel Sambuc 
1733f4a2713aSLionel Sambuc     Info.FirstArg = Param;
1734f4a2713aSLionel Sambuc     Info.SecondArg = Arg;
1735f4a2713aSLionel Sambuc     return Sema::TDK_NonDeducedMismatch;
1736f4a2713aSLionel Sambuc 
1737f4a2713aSLionel Sambuc   case TemplateArgument::NullPtr:
1738f4a2713aSLionel Sambuc     if (Arg.getKind() == TemplateArgument::NullPtr &&
1739f4a2713aSLionel Sambuc         S.Context.hasSameType(Param.getNullPtrType(), Arg.getNullPtrType()))
1740f4a2713aSLionel Sambuc       return Sema::TDK_Success;
1741f4a2713aSLionel Sambuc 
1742f4a2713aSLionel Sambuc     Info.FirstArg = Param;
1743f4a2713aSLionel Sambuc     Info.SecondArg = Arg;
1744f4a2713aSLionel Sambuc     return Sema::TDK_NonDeducedMismatch;
1745f4a2713aSLionel Sambuc 
1746f4a2713aSLionel Sambuc   case TemplateArgument::Integral:
1747f4a2713aSLionel Sambuc     if (Arg.getKind() == TemplateArgument::Integral) {
1748f4a2713aSLionel Sambuc       if (hasSameExtendedValue(Param.getAsIntegral(), Arg.getAsIntegral()))
1749f4a2713aSLionel Sambuc         return Sema::TDK_Success;
1750f4a2713aSLionel Sambuc 
1751f4a2713aSLionel Sambuc       Info.FirstArg = Param;
1752f4a2713aSLionel Sambuc       Info.SecondArg = Arg;
1753f4a2713aSLionel Sambuc       return Sema::TDK_NonDeducedMismatch;
1754f4a2713aSLionel Sambuc     }
1755f4a2713aSLionel Sambuc 
1756f4a2713aSLionel Sambuc     if (Arg.getKind() == TemplateArgument::Expression) {
1757f4a2713aSLionel Sambuc       Info.FirstArg = Param;
1758f4a2713aSLionel Sambuc       Info.SecondArg = Arg;
1759f4a2713aSLionel Sambuc       return Sema::TDK_NonDeducedMismatch;
1760f4a2713aSLionel Sambuc     }
1761f4a2713aSLionel Sambuc 
1762f4a2713aSLionel Sambuc     Info.FirstArg = Param;
1763f4a2713aSLionel Sambuc     Info.SecondArg = Arg;
1764f4a2713aSLionel Sambuc     return Sema::TDK_NonDeducedMismatch;
1765f4a2713aSLionel Sambuc 
1766f4a2713aSLionel Sambuc   case TemplateArgument::Expression: {
1767f4a2713aSLionel Sambuc     if (NonTypeTemplateParmDecl *NTTP
1768f4a2713aSLionel Sambuc           = getDeducedParameterFromExpr(Param.getAsExpr())) {
1769f4a2713aSLionel Sambuc       if (Arg.getKind() == TemplateArgument::Integral)
1770f4a2713aSLionel Sambuc         return DeduceNonTypeTemplateArgument(S, NTTP,
1771f4a2713aSLionel Sambuc                                              Arg.getAsIntegral(),
1772f4a2713aSLionel Sambuc                                              Arg.getIntegralType(),
1773f4a2713aSLionel Sambuc                                              /*ArrayBound=*/false,
1774f4a2713aSLionel Sambuc                                              Info, Deduced);
1775f4a2713aSLionel Sambuc       if (Arg.getKind() == TemplateArgument::Expression)
1776f4a2713aSLionel Sambuc         return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
1777f4a2713aSLionel Sambuc                                              Info, Deduced);
1778f4a2713aSLionel Sambuc       if (Arg.getKind() == TemplateArgument::Declaration)
1779f4a2713aSLionel Sambuc         return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
1780f4a2713aSLionel Sambuc                                              Info, Deduced);
1781f4a2713aSLionel Sambuc 
1782f4a2713aSLionel Sambuc       Info.FirstArg = Param;
1783f4a2713aSLionel Sambuc       Info.SecondArg = Arg;
1784f4a2713aSLionel Sambuc       return Sema::TDK_NonDeducedMismatch;
1785f4a2713aSLionel Sambuc     }
1786f4a2713aSLionel Sambuc 
1787f4a2713aSLionel Sambuc     // Can't deduce anything, but that's okay.
1788f4a2713aSLionel Sambuc     return Sema::TDK_Success;
1789f4a2713aSLionel Sambuc   }
1790f4a2713aSLionel Sambuc   case TemplateArgument::Pack:
1791f4a2713aSLionel Sambuc     llvm_unreachable("Argument packs should be expanded by the caller!");
1792f4a2713aSLionel Sambuc   }
1793f4a2713aSLionel Sambuc 
1794f4a2713aSLionel Sambuc   llvm_unreachable("Invalid TemplateArgument Kind!");
1795f4a2713aSLionel Sambuc }
1796f4a2713aSLionel Sambuc 
1797f4a2713aSLionel Sambuc /// \brief Determine whether there is a template argument to be used for
1798f4a2713aSLionel Sambuc /// deduction.
1799f4a2713aSLionel Sambuc ///
1800f4a2713aSLionel Sambuc /// This routine "expands" argument packs in-place, overriding its input
1801f4a2713aSLionel Sambuc /// parameters so that \c Args[ArgIdx] will be the available template argument.
1802f4a2713aSLionel Sambuc ///
1803f4a2713aSLionel Sambuc /// \returns true if there is another template argument (which will be at
1804f4a2713aSLionel Sambuc /// \c Args[ArgIdx]), false otherwise.
hasTemplateArgumentForDeduction(const TemplateArgument * & Args,unsigned & ArgIdx,unsigned & NumArgs)1805f4a2713aSLionel Sambuc static bool hasTemplateArgumentForDeduction(const TemplateArgument *&Args,
1806f4a2713aSLionel Sambuc                                             unsigned &ArgIdx,
1807f4a2713aSLionel Sambuc                                             unsigned &NumArgs) {
1808f4a2713aSLionel Sambuc   if (ArgIdx == NumArgs)
1809f4a2713aSLionel Sambuc     return false;
1810f4a2713aSLionel Sambuc 
1811f4a2713aSLionel Sambuc   const TemplateArgument &Arg = Args[ArgIdx];
1812f4a2713aSLionel Sambuc   if (Arg.getKind() != TemplateArgument::Pack)
1813f4a2713aSLionel Sambuc     return true;
1814f4a2713aSLionel Sambuc 
1815f4a2713aSLionel Sambuc   assert(ArgIdx == NumArgs - 1 && "Pack not at the end of argument list?");
1816f4a2713aSLionel Sambuc   Args = Arg.pack_begin();
1817f4a2713aSLionel Sambuc   NumArgs = Arg.pack_size();
1818f4a2713aSLionel Sambuc   ArgIdx = 0;
1819f4a2713aSLionel Sambuc   return ArgIdx < NumArgs;
1820f4a2713aSLionel Sambuc }
1821f4a2713aSLionel Sambuc 
1822f4a2713aSLionel Sambuc /// \brief Determine whether the given set of template arguments has a pack
1823f4a2713aSLionel Sambuc /// expansion that is not the last template argument.
hasPackExpansionBeforeEnd(const TemplateArgument * Args,unsigned NumArgs)1824f4a2713aSLionel Sambuc static bool hasPackExpansionBeforeEnd(const TemplateArgument *Args,
1825f4a2713aSLionel Sambuc                                       unsigned NumArgs) {
1826f4a2713aSLionel Sambuc   unsigned ArgIdx = 0;
1827f4a2713aSLionel Sambuc   while (ArgIdx < NumArgs) {
1828f4a2713aSLionel Sambuc     const TemplateArgument &Arg = Args[ArgIdx];
1829f4a2713aSLionel Sambuc 
1830f4a2713aSLionel Sambuc     // Unwrap argument packs.
1831f4a2713aSLionel Sambuc     if (Args[ArgIdx].getKind() == TemplateArgument::Pack) {
1832f4a2713aSLionel Sambuc       Args = Arg.pack_begin();
1833f4a2713aSLionel Sambuc       NumArgs = Arg.pack_size();
1834f4a2713aSLionel Sambuc       ArgIdx = 0;
1835f4a2713aSLionel Sambuc       continue;
1836f4a2713aSLionel Sambuc     }
1837f4a2713aSLionel Sambuc 
1838f4a2713aSLionel Sambuc     ++ArgIdx;
1839f4a2713aSLionel Sambuc     if (ArgIdx == NumArgs)
1840f4a2713aSLionel Sambuc       return false;
1841f4a2713aSLionel Sambuc 
1842f4a2713aSLionel Sambuc     if (Arg.isPackExpansion())
1843f4a2713aSLionel Sambuc       return true;
1844f4a2713aSLionel Sambuc   }
1845f4a2713aSLionel Sambuc 
1846f4a2713aSLionel Sambuc   return false;
1847f4a2713aSLionel Sambuc }
1848f4a2713aSLionel Sambuc 
1849f4a2713aSLionel Sambuc static Sema::TemplateDeductionResult
DeduceTemplateArguments(Sema & S,TemplateParameterList * TemplateParams,const TemplateArgument * Params,unsigned NumParams,const TemplateArgument * Args,unsigned NumArgs,TemplateDeductionInfo & Info,SmallVectorImpl<DeducedTemplateArgument> & Deduced)1850f4a2713aSLionel Sambuc DeduceTemplateArguments(Sema &S,
1851f4a2713aSLionel Sambuc                         TemplateParameterList *TemplateParams,
1852f4a2713aSLionel Sambuc                         const TemplateArgument *Params, unsigned NumParams,
1853f4a2713aSLionel Sambuc                         const TemplateArgument *Args, unsigned NumArgs,
1854f4a2713aSLionel Sambuc                         TemplateDeductionInfo &Info,
1855f4a2713aSLionel Sambuc                         SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
1856f4a2713aSLionel Sambuc   // C++0x [temp.deduct.type]p9:
1857f4a2713aSLionel Sambuc   //   If the template argument list of P contains a pack expansion that is not
1858f4a2713aSLionel Sambuc   //   the last template argument, the entire template argument list is a
1859f4a2713aSLionel Sambuc   //   non-deduced context.
1860f4a2713aSLionel Sambuc   if (hasPackExpansionBeforeEnd(Params, NumParams))
1861f4a2713aSLionel Sambuc     return Sema::TDK_Success;
1862f4a2713aSLionel Sambuc 
1863f4a2713aSLionel Sambuc   // C++0x [temp.deduct.type]p9:
1864f4a2713aSLionel Sambuc   //   If P has a form that contains <T> or <i>, then each argument Pi of the
1865f4a2713aSLionel Sambuc   //   respective template argument list P is compared with the corresponding
1866f4a2713aSLionel Sambuc   //   argument Ai of the corresponding template argument list of A.
1867f4a2713aSLionel Sambuc   unsigned ArgIdx = 0, ParamIdx = 0;
1868f4a2713aSLionel Sambuc   for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams);
1869f4a2713aSLionel Sambuc        ++ParamIdx) {
1870f4a2713aSLionel Sambuc     if (!Params[ParamIdx].isPackExpansion()) {
1871f4a2713aSLionel Sambuc       // The simple case: deduce template arguments by matching Pi and Ai.
1872f4a2713aSLionel Sambuc 
1873f4a2713aSLionel Sambuc       // Check whether we have enough arguments.
1874f4a2713aSLionel Sambuc       if (!hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
1875f4a2713aSLionel Sambuc         return Sema::TDK_Success;
1876f4a2713aSLionel Sambuc 
1877f4a2713aSLionel Sambuc       if (Args[ArgIdx].isPackExpansion()) {
1878f4a2713aSLionel Sambuc         // FIXME: We follow the logic of C++0x [temp.deduct.type]p22 here,
1879f4a2713aSLionel Sambuc         // but applied to pack expansions that are template arguments.
1880f4a2713aSLionel Sambuc         return Sema::TDK_MiscellaneousDeductionFailure;
1881f4a2713aSLionel Sambuc       }
1882f4a2713aSLionel Sambuc 
1883f4a2713aSLionel Sambuc       // Perform deduction for this Pi/Ai pair.
1884f4a2713aSLionel Sambuc       if (Sema::TemplateDeductionResult Result
1885f4a2713aSLionel Sambuc             = DeduceTemplateArguments(S, TemplateParams,
1886f4a2713aSLionel Sambuc                                       Params[ParamIdx], Args[ArgIdx],
1887f4a2713aSLionel Sambuc                                       Info, Deduced))
1888f4a2713aSLionel Sambuc         return Result;
1889f4a2713aSLionel Sambuc 
1890f4a2713aSLionel Sambuc       // Move to the next argument.
1891f4a2713aSLionel Sambuc       ++ArgIdx;
1892f4a2713aSLionel Sambuc       continue;
1893f4a2713aSLionel Sambuc     }
1894f4a2713aSLionel Sambuc 
1895f4a2713aSLionel Sambuc     // The parameter is a pack expansion.
1896f4a2713aSLionel Sambuc 
1897f4a2713aSLionel Sambuc     // C++0x [temp.deduct.type]p9:
1898f4a2713aSLionel Sambuc     //   If Pi is a pack expansion, then the pattern of Pi is compared with
1899f4a2713aSLionel Sambuc     //   each remaining argument in the template argument list of A. Each
1900f4a2713aSLionel Sambuc     //   comparison deduces template arguments for subsequent positions in the
1901f4a2713aSLionel Sambuc     //   template parameter packs expanded by Pi.
1902f4a2713aSLionel Sambuc     TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern();
1903f4a2713aSLionel Sambuc 
1904f4a2713aSLionel Sambuc     // FIXME: If there are no remaining arguments, we can bail out early
1905f4a2713aSLionel Sambuc     // and set any deduced parameter packs to an empty argument pack.
1906f4a2713aSLionel Sambuc     // The latter part of this is a (minor) correctness issue.
1907f4a2713aSLionel Sambuc 
1908*0a6a1f1dSLionel Sambuc     // Prepare to deduce the packs within the pattern.
1909*0a6a1f1dSLionel Sambuc     PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern);
1910f4a2713aSLionel Sambuc 
1911f4a2713aSLionel Sambuc     // Keep track of the deduced template arguments for each parameter pack
1912f4a2713aSLionel Sambuc     // expanded by this pack expansion (the outer index) and for each
1913f4a2713aSLionel Sambuc     // template argument (the inner SmallVectors).
1914f4a2713aSLionel Sambuc     bool HasAnyArguments = false;
1915*0a6a1f1dSLionel Sambuc     for (; hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs); ++ArgIdx) {
1916f4a2713aSLionel Sambuc       HasAnyArguments = true;
1917f4a2713aSLionel Sambuc 
1918f4a2713aSLionel Sambuc       // Deduce template arguments from the pattern.
1919f4a2713aSLionel Sambuc       if (Sema::TemplateDeductionResult Result
1920f4a2713aSLionel Sambuc             = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx],
1921f4a2713aSLionel Sambuc                                       Info, Deduced))
1922f4a2713aSLionel Sambuc         return Result;
1923f4a2713aSLionel Sambuc 
1924*0a6a1f1dSLionel Sambuc       PackScope.nextPackElement();
1925f4a2713aSLionel Sambuc     }
1926f4a2713aSLionel Sambuc 
1927f4a2713aSLionel Sambuc     // Build argument packs for each of the parameter packs expanded by this
1928f4a2713aSLionel Sambuc     // pack expansion.
1929*0a6a1f1dSLionel Sambuc     if (auto Result = PackScope.finish(HasAnyArguments))
1930f4a2713aSLionel Sambuc       return Result;
1931f4a2713aSLionel Sambuc   }
1932f4a2713aSLionel Sambuc 
1933f4a2713aSLionel Sambuc   return Sema::TDK_Success;
1934f4a2713aSLionel Sambuc }
1935f4a2713aSLionel Sambuc 
1936f4a2713aSLionel Sambuc static Sema::TemplateDeductionResult
DeduceTemplateArguments(Sema & S,TemplateParameterList * TemplateParams,const TemplateArgumentList & ParamList,const TemplateArgumentList & ArgList,TemplateDeductionInfo & Info,SmallVectorImpl<DeducedTemplateArgument> & Deduced)1937f4a2713aSLionel Sambuc DeduceTemplateArguments(Sema &S,
1938f4a2713aSLionel Sambuc                         TemplateParameterList *TemplateParams,
1939f4a2713aSLionel Sambuc                         const TemplateArgumentList &ParamList,
1940f4a2713aSLionel Sambuc                         const TemplateArgumentList &ArgList,
1941f4a2713aSLionel Sambuc                         TemplateDeductionInfo &Info,
1942f4a2713aSLionel Sambuc                         SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
1943f4a2713aSLionel Sambuc   return DeduceTemplateArguments(S, TemplateParams,
1944f4a2713aSLionel Sambuc                                  ParamList.data(), ParamList.size(),
1945f4a2713aSLionel Sambuc                                  ArgList.data(), ArgList.size(),
1946f4a2713aSLionel Sambuc                                  Info, Deduced);
1947f4a2713aSLionel Sambuc }
1948f4a2713aSLionel Sambuc 
1949f4a2713aSLionel Sambuc /// \brief Determine whether two template arguments are the same.
isSameTemplateArg(ASTContext & Context,const TemplateArgument & X,const TemplateArgument & Y)1950f4a2713aSLionel Sambuc static bool isSameTemplateArg(ASTContext &Context,
1951f4a2713aSLionel Sambuc                               const TemplateArgument &X,
1952f4a2713aSLionel Sambuc                               const TemplateArgument &Y) {
1953f4a2713aSLionel Sambuc   if (X.getKind() != Y.getKind())
1954f4a2713aSLionel Sambuc     return false;
1955f4a2713aSLionel Sambuc 
1956f4a2713aSLionel Sambuc   switch (X.getKind()) {
1957f4a2713aSLionel Sambuc     case TemplateArgument::Null:
1958f4a2713aSLionel Sambuc       llvm_unreachable("Comparing NULL template argument");
1959f4a2713aSLionel Sambuc 
1960f4a2713aSLionel Sambuc     case TemplateArgument::Type:
1961f4a2713aSLionel Sambuc       return Context.getCanonicalType(X.getAsType()) ==
1962f4a2713aSLionel Sambuc              Context.getCanonicalType(Y.getAsType());
1963f4a2713aSLionel Sambuc 
1964f4a2713aSLionel Sambuc     case TemplateArgument::Declaration:
1965*0a6a1f1dSLionel Sambuc       return isSameDeclaration(X.getAsDecl(), Y.getAsDecl());
1966f4a2713aSLionel Sambuc 
1967f4a2713aSLionel Sambuc     case TemplateArgument::NullPtr:
1968f4a2713aSLionel Sambuc       return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType());
1969f4a2713aSLionel Sambuc 
1970f4a2713aSLionel Sambuc     case TemplateArgument::Template:
1971f4a2713aSLionel Sambuc     case TemplateArgument::TemplateExpansion:
1972f4a2713aSLionel Sambuc       return Context.getCanonicalTemplateName(
1973f4a2713aSLionel Sambuc                     X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() ==
1974f4a2713aSLionel Sambuc              Context.getCanonicalTemplateName(
1975f4a2713aSLionel Sambuc                     Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer();
1976f4a2713aSLionel Sambuc 
1977f4a2713aSLionel Sambuc     case TemplateArgument::Integral:
1978f4a2713aSLionel Sambuc       return X.getAsIntegral() == Y.getAsIntegral();
1979f4a2713aSLionel Sambuc 
1980f4a2713aSLionel Sambuc     case TemplateArgument::Expression: {
1981f4a2713aSLionel Sambuc       llvm::FoldingSetNodeID XID, YID;
1982f4a2713aSLionel Sambuc       X.getAsExpr()->Profile(XID, Context, true);
1983f4a2713aSLionel Sambuc       Y.getAsExpr()->Profile(YID, Context, true);
1984f4a2713aSLionel Sambuc       return XID == YID;
1985f4a2713aSLionel Sambuc     }
1986f4a2713aSLionel Sambuc 
1987f4a2713aSLionel Sambuc     case TemplateArgument::Pack:
1988f4a2713aSLionel Sambuc       if (X.pack_size() != Y.pack_size())
1989f4a2713aSLionel Sambuc         return false;
1990f4a2713aSLionel Sambuc 
1991f4a2713aSLionel Sambuc       for (TemplateArgument::pack_iterator XP = X.pack_begin(),
1992f4a2713aSLionel Sambuc                                         XPEnd = X.pack_end(),
1993f4a2713aSLionel Sambuc                                            YP = Y.pack_begin();
1994f4a2713aSLionel Sambuc            XP != XPEnd; ++XP, ++YP)
1995f4a2713aSLionel Sambuc         if (!isSameTemplateArg(Context, *XP, *YP))
1996f4a2713aSLionel Sambuc           return false;
1997f4a2713aSLionel Sambuc 
1998f4a2713aSLionel Sambuc       return true;
1999f4a2713aSLionel Sambuc   }
2000f4a2713aSLionel Sambuc 
2001f4a2713aSLionel Sambuc   llvm_unreachable("Invalid TemplateArgument Kind!");
2002f4a2713aSLionel Sambuc }
2003f4a2713aSLionel Sambuc 
2004f4a2713aSLionel Sambuc /// \brief Allocate a TemplateArgumentLoc where all locations have
2005f4a2713aSLionel Sambuc /// been initialized to the given location.
2006f4a2713aSLionel Sambuc ///
2007f4a2713aSLionel Sambuc /// \param S The semantic analysis object.
2008f4a2713aSLionel Sambuc ///
2009f4a2713aSLionel Sambuc /// \param Arg The template argument we are producing template argument
2010f4a2713aSLionel Sambuc /// location information for.
2011f4a2713aSLionel Sambuc ///
2012f4a2713aSLionel Sambuc /// \param NTTPType For a declaration template argument, the type of
2013f4a2713aSLionel Sambuc /// the non-type template parameter that corresponds to this template
2014f4a2713aSLionel Sambuc /// argument.
2015f4a2713aSLionel Sambuc ///
2016f4a2713aSLionel Sambuc /// \param Loc The source location to use for the resulting template
2017f4a2713aSLionel Sambuc /// argument.
2018f4a2713aSLionel Sambuc static TemplateArgumentLoc
getTrivialTemplateArgumentLoc(Sema & S,const TemplateArgument & Arg,QualType NTTPType,SourceLocation Loc)2019f4a2713aSLionel Sambuc getTrivialTemplateArgumentLoc(Sema &S,
2020f4a2713aSLionel Sambuc                               const TemplateArgument &Arg,
2021f4a2713aSLionel Sambuc                               QualType NTTPType,
2022f4a2713aSLionel Sambuc                               SourceLocation Loc) {
2023f4a2713aSLionel Sambuc   switch (Arg.getKind()) {
2024f4a2713aSLionel Sambuc   case TemplateArgument::Null:
2025f4a2713aSLionel Sambuc     llvm_unreachable("Can't get a NULL template argument here");
2026f4a2713aSLionel Sambuc 
2027f4a2713aSLionel Sambuc   case TemplateArgument::Type:
2028f4a2713aSLionel Sambuc     return TemplateArgumentLoc(Arg,
2029f4a2713aSLionel Sambuc                      S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
2030f4a2713aSLionel Sambuc 
2031f4a2713aSLionel Sambuc   case TemplateArgument::Declaration: {
2032f4a2713aSLionel Sambuc     Expr *E
2033f4a2713aSLionel Sambuc       = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
2034*0a6a1f1dSLionel Sambuc           .getAs<Expr>();
2035f4a2713aSLionel Sambuc     return TemplateArgumentLoc(TemplateArgument(E), E);
2036f4a2713aSLionel Sambuc   }
2037f4a2713aSLionel Sambuc 
2038f4a2713aSLionel Sambuc   case TemplateArgument::NullPtr: {
2039f4a2713aSLionel Sambuc     Expr *E
2040f4a2713aSLionel Sambuc       = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
2041*0a6a1f1dSLionel Sambuc           .getAs<Expr>();
2042f4a2713aSLionel Sambuc     return TemplateArgumentLoc(TemplateArgument(NTTPType, /*isNullPtr*/true),
2043f4a2713aSLionel Sambuc                                E);
2044f4a2713aSLionel Sambuc   }
2045f4a2713aSLionel Sambuc 
2046f4a2713aSLionel Sambuc   case TemplateArgument::Integral: {
2047f4a2713aSLionel Sambuc     Expr *E
2048*0a6a1f1dSLionel Sambuc       = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).getAs<Expr>();
2049f4a2713aSLionel Sambuc     return TemplateArgumentLoc(TemplateArgument(E), E);
2050f4a2713aSLionel Sambuc   }
2051f4a2713aSLionel Sambuc 
2052f4a2713aSLionel Sambuc     case TemplateArgument::Template:
2053f4a2713aSLionel Sambuc     case TemplateArgument::TemplateExpansion: {
2054f4a2713aSLionel Sambuc       NestedNameSpecifierLocBuilder Builder;
2055f4a2713aSLionel Sambuc       TemplateName Template = Arg.getAsTemplate();
2056f4a2713aSLionel Sambuc       if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
2057f4a2713aSLionel Sambuc         Builder.MakeTrivial(S.Context, DTN->getQualifier(), Loc);
2058*0a6a1f1dSLionel Sambuc       else if (QualifiedTemplateName *QTN =
2059*0a6a1f1dSLionel Sambuc                    Template.getAsQualifiedTemplateName())
2060f4a2713aSLionel Sambuc         Builder.MakeTrivial(S.Context, QTN->getQualifier(), Loc);
2061f4a2713aSLionel Sambuc 
2062f4a2713aSLionel Sambuc       if (Arg.getKind() == TemplateArgument::Template)
2063f4a2713aSLionel Sambuc         return TemplateArgumentLoc(Arg,
2064f4a2713aSLionel Sambuc                                    Builder.getWithLocInContext(S.Context),
2065f4a2713aSLionel Sambuc                                    Loc);
2066f4a2713aSLionel Sambuc 
2067f4a2713aSLionel Sambuc 
2068f4a2713aSLionel Sambuc       return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(S.Context),
2069f4a2713aSLionel Sambuc                                  Loc, Loc);
2070f4a2713aSLionel Sambuc     }
2071f4a2713aSLionel Sambuc 
2072f4a2713aSLionel Sambuc   case TemplateArgument::Expression:
2073f4a2713aSLionel Sambuc     return TemplateArgumentLoc(Arg, Arg.getAsExpr());
2074f4a2713aSLionel Sambuc 
2075f4a2713aSLionel Sambuc   case TemplateArgument::Pack:
2076f4a2713aSLionel Sambuc     return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo());
2077f4a2713aSLionel Sambuc   }
2078f4a2713aSLionel Sambuc 
2079f4a2713aSLionel Sambuc   llvm_unreachable("Invalid TemplateArgument Kind!");
2080f4a2713aSLionel Sambuc }
2081f4a2713aSLionel Sambuc 
2082f4a2713aSLionel Sambuc 
2083f4a2713aSLionel Sambuc /// \brief Convert the given deduced template argument and add it to the set of
2084f4a2713aSLionel Sambuc /// fully-converted template arguments.
2085f4a2713aSLionel Sambuc static bool
ConvertDeducedTemplateArgument(Sema & S,NamedDecl * Param,DeducedTemplateArgument Arg,NamedDecl * Template,QualType NTTPType,unsigned ArgumentPackIndex,TemplateDeductionInfo & Info,bool InFunctionTemplate,SmallVectorImpl<TemplateArgument> & Output)2086f4a2713aSLionel Sambuc ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param,
2087f4a2713aSLionel Sambuc                                DeducedTemplateArgument Arg,
2088f4a2713aSLionel Sambuc                                NamedDecl *Template,
2089f4a2713aSLionel Sambuc                                QualType NTTPType,
2090f4a2713aSLionel Sambuc                                unsigned ArgumentPackIndex,
2091f4a2713aSLionel Sambuc                                TemplateDeductionInfo &Info,
2092f4a2713aSLionel Sambuc                                bool InFunctionTemplate,
2093f4a2713aSLionel Sambuc                                SmallVectorImpl<TemplateArgument> &Output) {
2094f4a2713aSLionel Sambuc   if (Arg.getKind() == TemplateArgument::Pack) {
2095f4a2713aSLionel Sambuc     // This is a template argument pack, so check each of its arguments against
2096f4a2713aSLionel Sambuc     // the template parameter.
2097f4a2713aSLionel Sambuc     SmallVector<TemplateArgument, 2> PackedArgsBuilder;
2098*0a6a1f1dSLionel Sambuc     for (const auto &P : Arg.pack_elements()) {
2099f4a2713aSLionel Sambuc       // When converting the deduced template argument, append it to the
2100f4a2713aSLionel Sambuc       // general output list. We need to do this so that the template argument
2101f4a2713aSLionel Sambuc       // checking logic has all of the prior template arguments available.
2102*0a6a1f1dSLionel Sambuc       DeducedTemplateArgument InnerArg(P);
2103f4a2713aSLionel Sambuc       InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound());
2104f4a2713aSLionel Sambuc       if (ConvertDeducedTemplateArgument(S, Param, InnerArg, Template,
2105f4a2713aSLionel Sambuc                                          NTTPType, PackedArgsBuilder.size(),
2106f4a2713aSLionel Sambuc                                          Info, InFunctionTemplate, Output))
2107f4a2713aSLionel Sambuc         return true;
2108f4a2713aSLionel Sambuc 
2109f4a2713aSLionel Sambuc       // Move the converted template argument into our argument pack.
2110f4a2713aSLionel Sambuc       PackedArgsBuilder.push_back(Output.pop_back_val());
2111f4a2713aSLionel Sambuc     }
2112f4a2713aSLionel Sambuc 
2113f4a2713aSLionel Sambuc     // Create the resulting argument pack.
2114f4a2713aSLionel Sambuc     Output.push_back(TemplateArgument::CreatePackCopy(S.Context,
2115f4a2713aSLionel Sambuc                                                       PackedArgsBuilder.data(),
2116f4a2713aSLionel Sambuc                                                      PackedArgsBuilder.size()));
2117f4a2713aSLionel Sambuc     return false;
2118f4a2713aSLionel Sambuc   }
2119f4a2713aSLionel Sambuc 
2120f4a2713aSLionel Sambuc   // Convert the deduced template argument into a template
2121f4a2713aSLionel Sambuc   // argument that we can check, almost as if the user had written
2122f4a2713aSLionel Sambuc   // the template argument explicitly.
2123f4a2713aSLionel Sambuc   TemplateArgumentLoc ArgLoc = getTrivialTemplateArgumentLoc(S, Arg, NTTPType,
2124f4a2713aSLionel Sambuc                                                              Info.getLocation());
2125f4a2713aSLionel Sambuc 
2126f4a2713aSLionel Sambuc   // Check the template argument, converting it as necessary.
2127f4a2713aSLionel Sambuc   return S.CheckTemplateArgument(Param, ArgLoc,
2128f4a2713aSLionel Sambuc                                  Template,
2129f4a2713aSLionel Sambuc                                  Template->getLocation(),
2130f4a2713aSLionel Sambuc                                  Template->getSourceRange().getEnd(),
2131f4a2713aSLionel Sambuc                                  ArgumentPackIndex,
2132f4a2713aSLionel Sambuc                                  Output,
2133f4a2713aSLionel Sambuc                                  InFunctionTemplate
2134f4a2713aSLionel Sambuc                                   ? (Arg.wasDeducedFromArrayBound()
2135f4a2713aSLionel Sambuc                                        ? Sema::CTAK_DeducedFromArrayBound
2136f4a2713aSLionel Sambuc                                        : Sema::CTAK_Deduced)
2137f4a2713aSLionel Sambuc                                  : Sema::CTAK_Specified);
2138f4a2713aSLionel Sambuc }
2139f4a2713aSLionel Sambuc 
2140f4a2713aSLionel Sambuc /// Complete template argument deduction for a class template partial
2141f4a2713aSLionel Sambuc /// specialization.
2142f4a2713aSLionel Sambuc static Sema::TemplateDeductionResult
FinishTemplateArgumentDeduction(Sema & S,ClassTemplatePartialSpecializationDecl * Partial,const TemplateArgumentList & TemplateArgs,SmallVectorImpl<DeducedTemplateArgument> & Deduced,TemplateDeductionInfo & Info)2143f4a2713aSLionel Sambuc FinishTemplateArgumentDeduction(Sema &S,
2144f4a2713aSLionel Sambuc                                 ClassTemplatePartialSpecializationDecl *Partial,
2145f4a2713aSLionel Sambuc                                 const TemplateArgumentList &TemplateArgs,
2146f4a2713aSLionel Sambuc                       SmallVectorImpl<DeducedTemplateArgument> &Deduced,
2147f4a2713aSLionel Sambuc                                 TemplateDeductionInfo &Info) {
2148f4a2713aSLionel Sambuc   // Unevaluated SFINAE context.
2149f4a2713aSLionel Sambuc   EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated);
2150f4a2713aSLionel Sambuc   Sema::SFINAETrap Trap(S);
2151f4a2713aSLionel Sambuc 
2152f4a2713aSLionel Sambuc   Sema::ContextRAII SavedContext(S, Partial);
2153f4a2713aSLionel Sambuc 
2154f4a2713aSLionel Sambuc   // C++ [temp.deduct.type]p2:
2155f4a2713aSLionel Sambuc   //   [...] or if any template argument remains neither deduced nor
2156f4a2713aSLionel Sambuc   //   explicitly specified, template argument deduction fails.
2157f4a2713aSLionel Sambuc   SmallVector<TemplateArgument, 4> Builder;
2158f4a2713aSLionel Sambuc   TemplateParameterList *PartialParams = Partial->getTemplateParameters();
2159f4a2713aSLionel Sambuc   for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) {
2160f4a2713aSLionel Sambuc     NamedDecl *Param = PartialParams->getParam(I);
2161f4a2713aSLionel Sambuc     if (Deduced[I].isNull()) {
2162f4a2713aSLionel Sambuc       Info.Param = makeTemplateParameter(Param);
2163f4a2713aSLionel Sambuc       return Sema::TDK_Incomplete;
2164f4a2713aSLionel Sambuc     }
2165f4a2713aSLionel Sambuc 
2166f4a2713aSLionel Sambuc     // We have deduced this argument, so it still needs to be
2167f4a2713aSLionel Sambuc     // checked and converted.
2168f4a2713aSLionel Sambuc 
2169f4a2713aSLionel Sambuc     // First, for a non-type template parameter type that is
2170f4a2713aSLionel Sambuc     // initialized by a declaration, we need the type of the
2171f4a2713aSLionel Sambuc     // corresponding non-type template parameter.
2172f4a2713aSLionel Sambuc     QualType NTTPType;
2173f4a2713aSLionel Sambuc     if (NonTypeTemplateParmDecl *NTTP
2174f4a2713aSLionel Sambuc                                   = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2175f4a2713aSLionel Sambuc       NTTPType = NTTP->getType();
2176f4a2713aSLionel Sambuc       if (NTTPType->isDependentType()) {
2177f4a2713aSLionel Sambuc         TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
2178f4a2713aSLionel Sambuc                                           Builder.data(), Builder.size());
2179f4a2713aSLionel Sambuc         NTTPType = S.SubstType(NTTPType,
2180f4a2713aSLionel Sambuc                                MultiLevelTemplateArgumentList(TemplateArgs),
2181f4a2713aSLionel Sambuc                                NTTP->getLocation(),
2182f4a2713aSLionel Sambuc                                NTTP->getDeclName());
2183f4a2713aSLionel Sambuc         if (NTTPType.isNull()) {
2184f4a2713aSLionel Sambuc           Info.Param = makeTemplateParameter(Param);
2185f4a2713aSLionel Sambuc           // FIXME: These template arguments are temporary. Free them!
2186f4a2713aSLionel Sambuc           Info.reset(TemplateArgumentList::CreateCopy(S.Context,
2187f4a2713aSLionel Sambuc                                                       Builder.data(),
2188f4a2713aSLionel Sambuc                                                       Builder.size()));
2189f4a2713aSLionel Sambuc           return Sema::TDK_SubstitutionFailure;
2190f4a2713aSLionel Sambuc         }
2191f4a2713aSLionel Sambuc       }
2192f4a2713aSLionel Sambuc     }
2193f4a2713aSLionel Sambuc 
2194f4a2713aSLionel Sambuc     if (ConvertDeducedTemplateArgument(S, Param, Deduced[I],
2195f4a2713aSLionel Sambuc                                        Partial, NTTPType, 0, Info, false,
2196f4a2713aSLionel Sambuc                                        Builder)) {
2197f4a2713aSLionel Sambuc       Info.Param = makeTemplateParameter(Param);
2198f4a2713aSLionel Sambuc       // FIXME: These template arguments are temporary. Free them!
2199f4a2713aSLionel Sambuc       Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
2200f4a2713aSLionel Sambuc                                                   Builder.size()));
2201f4a2713aSLionel Sambuc       return Sema::TDK_SubstitutionFailure;
2202f4a2713aSLionel Sambuc     }
2203f4a2713aSLionel Sambuc   }
2204f4a2713aSLionel Sambuc 
2205f4a2713aSLionel Sambuc   // Form the template argument list from the deduced template arguments.
2206f4a2713aSLionel Sambuc   TemplateArgumentList *DeducedArgumentList
2207f4a2713aSLionel Sambuc     = TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
2208f4a2713aSLionel Sambuc                                        Builder.size());
2209f4a2713aSLionel Sambuc 
2210f4a2713aSLionel Sambuc   Info.reset(DeducedArgumentList);
2211f4a2713aSLionel Sambuc 
2212f4a2713aSLionel Sambuc   // Substitute the deduced template arguments into the template
2213f4a2713aSLionel Sambuc   // arguments of the class template partial specialization, and
2214f4a2713aSLionel Sambuc   // verify that the instantiated template arguments are both valid
2215f4a2713aSLionel Sambuc   // and are equivalent to the template arguments originally provided
2216f4a2713aSLionel Sambuc   // to the class template.
2217f4a2713aSLionel Sambuc   LocalInstantiationScope InstScope(S);
2218f4a2713aSLionel Sambuc   ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
2219f4a2713aSLionel Sambuc   const ASTTemplateArgumentListInfo *PartialTemplArgInfo
2220f4a2713aSLionel Sambuc     = Partial->getTemplateArgsAsWritten();
2221f4a2713aSLionel Sambuc   const TemplateArgumentLoc *PartialTemplateArgs
2222f4a2713aSLionel Sambuc     = PartialTemplArgInfo->getTemplateArgs();
2223f4a2713aSLionel Sambuc 
2224f4a2713aSLionel Sambuc   TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc,
2225f4a2713aSLionel Sambuc                                     PartialTemplArgInfo->RAngleLoc);
2226f4a2713aSLionel Sambuc 
2227f4a2713aSLionel Sambuc   if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs,
2228f4a2713aSLionel Sambuc               InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
2229f4a2713aSLionel Sambuc     unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx;
2230f4a2713aSLionel Sambuc     if (ParamIdx >= Partial->getTemplateParameters()->size())
2231f4a2713aSLionel Sambuc       ParamIdx = Partial->getTemplateParameters()->size() - 1;
2232f4a2713aSLionel Sambuc 
2233f4a2713aSLionel Sambuc     Decl *Param
2234f4a2713aSLionel Sambuc       = const_cast<NamedDecl *>(
2235f4a2713aSLionel Sambuc                           Partial->getTemplateParameters()->getParam(ParamIdx));
2236f4a2713aSLionel Sambuc     Info.Param = makeTemplateParameter(Param);
2237f4a2713aSLionel Sambuc     Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument();
2238f4a2713aSLionel Sambuc     return Sema::TDK_SubstitutionFailure;
2239f4a2713aSLionel Sambuc   }
2240f4a2713aSLionel Sambuc 
2241f4a2713aSLionel Sambuc   SmallVector<TemplateArgument, 4> ConvertedInstArgs;
2242f4a2713aSLionel Sambuc   if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
2243f4a2713aSLionel Sambuc                                   InstArgs, false, ConvertedInstArgs))
2244f4a2713aSLionel Sambuc     return Sema::TDK_SubstitutionFailure;
2245f4a2713aSLionel Sambuc 
2246f4a2713aSLionel Sambuc   TemplateParameterList *TemplateParams
2247f4a2713aSLionel Sambuc     = ClassTemplate->getTemplateParameters();
2248f4a2713aSLionel Sambuc   for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) {
2249f4a2713aSLionel Sambuc     TemplateArgument InstArg = ConvertedInstArgs.data()[I];
2250f4a2713aSLionel Sambuc     if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
2251f4a2713aSLionel Sambuc       Info.Param = makeTemplateParameter(TemplateParams->getParam(I));
2252f4a2713aSLionel Sambuc       Info.FirstArg = TemplateArgs[I];
2253f4a2713aSLionel Sambuc       Info.SecondArg = InstArg;
2254f4a2713aSLionel Sambuc       return Sema::TDK_NonDeducedMismatch;
2255f4a2713aSLionel Sambuc     }
2256f4a2713aSLionel Sambuc   }
2257f4a2713aSLionel Sambuc 
2258f4a2713aSLionel Sambuc   if (Trap.hasErrorOccurred())
2259f4a2713aSLionel Sambuc     return Sema::TDK_SubstitutionFailure;
2260f4a2713aSLionel Sambuc 
2261f4a2713aSLionel Sambuc   return Sema::TDK_Success;
2262f4a2713aSLionel Sambuc }
2263f4a2713aSLionel Sambuc 
2264f4a2713aSLionel Sambuc /// \brief Perform template argument deduction to determine whether
2265f4a2713aSLionel Sambuc /// the given template arguments match the given class template
2266f4a2713aSLionel Sambuc /// partial specialization per C++ [temp.class.spec.match].
2267f4a2713aSLionel Sambuc Sema::TemplateDeductionResult
DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl * Partial,const TemplateArgumentList & TemplateArgs,TemplateDeductionInfo & Info)2268f4a2713aSLionel Sambuc Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
2269f4a2713aSLionel Sambuc                               const TemplateArgumentList &TemplateArgs,
2270f4a2713aSLionel Sambuc                               TemplateDeductionInfo &Info) {
2271f4a2713aSLionel Sambuc   if (Partial->isInvalidDecl())
2272f4a2713aSLionel Sambuc     return TDK_Invalid;
2273f4a2713aSLionel Sambuc 
2274f4a2713aSLionel Sambuc   // C++ [temp.class.spec.match]p2:
2275f4a2713aSLionel Sambuc   //   A partial specialization matches a given actual template
2276f4a2713aSLionel Sambuc   //   argument list if the template arguments of the partial
2277f4a2713aSLionel Sambuc   //   specialization can be deduced from the actual template argument
2278f4a2713aSLionel Sambuc   //   list (14.8.2).
2279f4a2713aSLionel Sambuc 
2280f4a2713aSLionel Sambuc   // Unevaluated SFINAE context.
2281f4a2713aSLionel Sambuc   EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated);
2282f4a2713aSLionel Sambuc   SFINAETrap Trap(*this);
2283f4a2713aSLionel Sambuc 
2284f4a2713aSLionel Sambuc   SmallVector<DeducedTemplateArgument, 4> Deduced;
2285f4a2713aSLionel Sambuc   Deduced.resize(Partial->getTemplateParameters()->size());
2286f4a2713aSLionel Sambuc   if (TemplateDeductionResult Result
2287f4a2713aSLionel Sambuc         = ::DeduceTemplateArguments(*this,
2288f4a2713aSLionel Sambuc                                     Partial->getTemplateParameters(),
2289f4a2713aSLionel Sambuc                                     Partial->getTemplateArgs(),
2290f4a2713aSLionel Sambuc                                     TemplateArgs, Info, Deduced))
2291f4a2713aSLionel Sambuc     return Result;
2292f4a2713aSLionel Sambuc 
2293f4a2713aSLionel Sambuc   SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end());
2294*0a6a1f1dSLionel Sambuc   InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs,
2295*0a6a1f1dSLionel Sambuc                              Info);
2296f4a2713aSLionel Sambuc   if (Inst.isInvalid())
2297f4a2713aSLionel Sambuc     return TDK_InstantiationDepth;
2298f4a2713aSLionel Sambuc 
2299f4a2713aSLionel Sambuc   if (Trap.hasErrorOccurred())
2300f4a2713aSLionel Sambuc     return Sema::TDK_SubstitutionFailure;
2301f4a2713aSLionel Sambuc 
2302f4a2713aSLionel Sambuc   return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
2303f4a2713aSLionel Sambuc                                            Deduced, Info);
2304f4a2713aSLionel Sambuc }
2305f4a2713aSLionel Sambuc 
2306f4a2713aSLionel Sambuc /// Complete template argument deduction for a variable template partial
2307f4a2713aSLionel Sambuc /// specialization.
2308f4a2713aSLionel Sambuc /// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2309f4a2713aSLionel Sambuc ///       May require unifying ClassTemplate(Partial)SpecializationDecl and
2310f4a2713aSLionel Sambuc ///        VarTemplate(Partial)SpecializationDecl with a new data
2311f4a2713aSLionel Sambuc ///        structure Template(Partial)SpecializationDecl, and
2312f4a2713aSLionel Sambuc ///        using Template(Partial)SpecializationDecl as input type.
FinishTemplateArgumentDeduction(Sema & S,VarTemplatePartialSpecializationDecl * Partial,const TemplateArgumentList & TemplateArgs,SmallVectorImpl<DeducedTemplateArgument> & Deduced,TemplateDeductionInfo & Info)2313f4a2713aSLionel Sambuc static Sema::TemplateDeductionResult FinishTemplateArgumentDeduction(
2314f4a2713aSLionel Sambuc     Sema &S, VarTemplatePartialSpecializationDecl *Partial,
2315f4a2713aSLionel Sambuc     const TemplateArgumentList &TemplateArgs,
2316f4a2713aSLionel Sambuc     SmallVectorImpl<DeducedTemplateArgument> &Deduced,
2317f4a2713aSLionel Sambuc     TemplateDeductionInfo &Info) {
2318f4a2713aSLionel Sambuc   // Unevaluated SFINAE context.
2319f4a2713aSLionel Sambuc   EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated);
2320f4a2713aSLionel Sambuc   Sema::SFINAETrap Trap(S);
2321f4a2713aSLionel Sambuc 
2322f4a2713aSLionel Sambuc   // C++ [temp.deduct.type]p2:
2323f4a2713aSLionel Sambuc   //   [...] or if any template argument remains neither deduced nor
2324f4a2713aSLionel Sambuc   //   explicitly specified, template argument deduction fails.
2325f4a2713aSLionel Sambuc   SmallVector<TemplateArgument, 4> Builder;
2326f4a2713aSLionel Sambuc   TemplateParameterList *PartialParams = Partial->getTemplateParameters();
2327f4a2713aSLionel Sambuc   for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) {
2328f4a2713aSLionel Sambuc     NamedDecl *Param = PartialParams->getParam(I);
2329f4a2713aSLionel Sambuc     if (Deduced[I].isNull()) {
2330f4a2713aSLionel Sambuc       Info.Param = makeTemplateParameter(Param);
2331f4a2713aSLionel Sambuc       return Sema::TDK_Incomplete;
2332f4a2713aSLionel Sambuc     }
2333f4a2713aSLionel Sambuc 
2334f4a2713aSLionel Sambuc     // We have deduced this argument, so it still needs to be
2335f4a2713aSLionel Sambuc     // checked and converted.
2336f4a2713aSLionel Sambuc 
2337f4a2713aSLionel Sambuc     // First, for a non-type template parameter type that is
2338f4a2713aSLionel Sambuc     // initialized by a declaration, we need the type of the
2339f4a2713aSLionel Sambuc     // corresponding non-type template parameter.
2340f4a2713aSLionel Sambuc     QualType NTTPType;
2341f4a2713aSLionel Sambuc     if (NonTypeTemplateParmDecl *NTTP =
2342f4a2713aSLionel Sambuc             dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2343f4a2713aSLionel Sambuc       NTTPType = NTTP->getType();
2344f4a2713aSLionel Sambuc       if (NTTPType->isDependentType()) {
2345f4a2713aSLionel Sambuc         TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
2346f4a2713aSLionel Sambuc                                           Builder.data(), Builder.size());
2347f4a2713aSLionel Sambuc         NTTPType =
2348f4a2713aSLionel Sambuc             S.SubstType(NTTPType, MultiLevelTemplateArgumentList(TemplateArgs),
2349f4a2713aSLionel Sambuc                         NTTP->getLocation(), NTTP->getDeclName());
2350f4a2713aSLionel Sambuc         if (NTTPType.isNull()) {
2351f4a2713aSLionel Sambuc           Info.Param = makeTemplateParameter(Param);
2352f4a2713aSLionel Sambuc           // FIXME: These template arguments are temporary. Free them!
2353f4a2713aSLionel Sambuc           Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
2354f4a2713aSLionel Sambuc                                                       Builder.size()));
2355f4a2713aSLionel Sambuc           return Sema::TDK_SubstitutionFailure;
2356f4a2713aSLionel Sambuc         }
2357f4a2713aSLionel Sambuc       }
2358f4a2713aSLionel Sambuc     }
2359f4a2713aSLionel Sambuc 
2360f4a2713aSLionel Sambuc     if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Partial, NTTPType,
2361f4a2713aSLionel Sambuc                                        0, Info, false, Builder)) {
2362f4a2713aSLionel Sambuc       Info.Param = makeTemplateParameter(Param);
2363f4a2713aSLionel Sambuc       // FIXME: These template arguments are temporary. Free them!
2364f4a2713aSLionel Sambuc       Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
2365f4a2713aSLionel Sambuc                                                   Builder.size()));
2366f4a2713aSLionel Sambuc       return Sema::TDK_SubstitutionFailure;
2367f4a2713aSLionel Sambuc     }
2368f4a2713aSLionel Sambuc   }
2369f4a2713aSLionel Sambuc 
2370f4a2713aSLionel Sambuc   // Form the template argument list from the deduced template arguments.
2371f4a2713aSLionel Sambuc   TemplateArgumentList *DeducedArgumentList = TemplateArgumentList::CreateCopy(
2372f4a2713aSLionel Sambuc       S.Context, Builder.data(), Builder.size());
2373f4a2713aSLionel Sambuc 
2374f4a2713aSLionel Sambuc   Info.reset(DeducedArgumentList);
2375f4a2713aSLionel Sambuc 
2376f4a2713aSLionel Sambuc   // Substitute the deduced template arguments into the template
2377f4a2713aSLionel Sambuc   // arguments of the class template partial specialization, and
2378f4a2713aSLionel Sambuc   // verify that the instantiated template arguments are both valid
2379f4a2713aSLionel Sambuc   // and are equivalent to the template arguments originally provided
2380f4a2713aSLionel Sambuc   // to the class template.
2381f4a2713aSLionel Sambuc   LocalInstantiationScope InstScope(S);
2382f4a2713aSLionel Sambuc   VarTemplateDecl *VarTemplate = Partial->getSpecializedTemplate();
2383f4a2713aSLionel Sambuc   const ASTTemplateArgumentListInfo *PartialTemplArgInfo
2384f4a2713aSLionel Sambuc     = Partial->getTemplateArgsAsWritten();
2385f4a2713aSLionel Sambuc   const TemplateArgumentLoc *PartialTemplateArgs
2386f4a2713aSLionel Sambuc     = PartialTemplArgInfo->getTemplateArgs();
2387f4a2713aSLionel Sambuc 
2388f4a2713aSLionel Sambuc   TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc,
2389f4a2713aSLionel Sambuc                                     PartialTemplArgInfo->RAngleLoc);
2390f4a2713aSLionel Sambuc 
2391f4a2713aSLionel Sambuc   if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs,
2392f4a2713aSLionel Sambuc               InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
2393f4a2713aSLionel Sambuc     unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx;
2394f4a2713aSLionel Sambuc     if (ParamIdx >= Partial->getTemplateParameters()->size())
2395f4a2713aSLionel Sambuc       ParamIdx = Partial->getTemplateParameters()->size() - 1;
2396f4a2713aSLionel Sambuc 
2397f4a2713aSLionel Sambuc     Decl *Param = const_cast<NamedDecl *>(
2398f4a2713aSLionel Sambuc         Partial->getTemplateParameters()->getParam(ParamIdx));
2399f4a2713aSLionel Sambuc     Info.Param = makeTemplateParameter(Param);
2400f4a2713aSLionel Sambuc     Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument();
2401f4a2713aSLionel Sambuc     return Sema::TDK_SubstitutionFailure;
2402f4a2713aSLionel Sambuc   }
2403f4a2713aSLionel Sambuc   SmallVector<TemplateArgument, 4> ConvertedInstArgs;
2404f4a2713aSLionel Sambuc   if (S.CheckTemplateArgumentList(VarTemplate, Partial->getLocation(), InstArgs,
2405f4a2713aSLionel Sambuc                                   false, ConvertedInstArgs))
2406f4a2713aSLionel Sambuc     return Sema::TDK_SubstitutionFailure;
2407f4a2713aSLionel Sambuc 
2408f4a2713aSLionel Sambuc   TemplateParameterList *TemplateParams = VarTemplate->getTemplateParameters();
2409f4a2713aSLionel Sambuc   for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) {
2410f4a2713aSLionel Sambuc     TemplateArgument InstArg = ConvertedInstArgs.data()[I];
2411f4a2713aSLionel Sambuc     if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
2412f4a2713aSLionel Sambuc       Info.Param = makeTemplateParameter(TemplateParams->getParam(I));
2413f4a2713aSLionel Sambuc       Info.FirstArg = TemplateArgs[I];
2414f4a2713aSLionel Sambuc       Info.SecondArg = InstArg;
2415f4a2713aSLionel Sambuc       return Sema::TDK_NonDeducedMismatch;
2416f4a2713aSLionel Sambuc     }
2417f4a2713aSLionel Sambuc   }
2418f4a2713aSLionel Sambuc 
2419f4a2713aSLionel Sambuc   if (Trap.hasErrorOccurred())
2420f4a2713aSLionel Sambuc     return Sema::TDK_SubstitutionFailure;
2421f4a2713aSLionel Sambuc 
2422f4a2713aSLionel Sambuc   return Sema::TDK_Success;
2423f4a2713aSLionel Sambuc }
2424f4a2713aSLionel Sambuc 
2425f4a2713aSLionel Sambuc /// \brief Perform template argument deduction to determine whether
2426f4a2713aSLionel Sambuc /// the given template arguments match the given variable template
2427f4a2713aSLionel Sambuc /// partial specialization per C++ [temp.class.spec.match].
2428f4a2713aSLionel Sambuc /// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2429f4a2713aSLionel Sambuc ///       May require unifying ClassTemplate(Partial)SpecializationDecl and
2430f4a2713aSLionel Sambuc ///        VarTemplate(Partial)SpecializationDecl with a new data
2431f4a2713aSLionel Sambuc ///        structure Template(Partial)SpecializationDecl, and
2432f4a2713aSLionel Sambuc ///        using Template(Partial)SpecializationDecl as input type.
2433f4a2713aSLionel Sambuc Sema::TemplateDeductionResult
DeduceTemplateArguments(VarTemplatePartialSpecializationDecl * Partial,const TemplateArgumentList & TemplateArgs,TemplateDeductionInfo & Info)2434f4a2713aSLionel Sambuc Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial,
2435f4a2713aSLionel Sambuc                               const TemplateArgumentList &TemplateArgs,
2436f4a2713aSLionel Sambuc                               TemplateDeductionInfo &Info) {
2437f4a2713aSLionel Sambuc   if (Partial->isInvalidDecl())
2438f4a2713aSLionel Sambuc     return TDK_Invalid;
2439f4a2713aSLionel Sambuc 
2440f4a2713aSLionel Sambuc   // C++ [temp.class.spec.match]p2:
2441f4a2713aSLionel Sambuc   //   A partial specialization matches a given actual template
2442f4a2713aSLionel Sambuc   //   argument list if the template arguments of the partial
2443f4a2713aSLionel Sambuc   //   specialization can be deduced from the actual template argument
2444f4a2713aSLionel Sambuc   //   list (14.8.2).
2445f4a2713aSLionel Sambuc 
2446f4a2713aSLionel Sambuc   // Unevaluated SFINAE context.
2447f4a2713aSLionel Sambuc   EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated);
2448f4a2713aSLionel Sambuc   SFINAETrap Trap(*this);
2449f4a2713aSLionel Sambuc 
2450f4a2713aSLionel Sambuc   SmallVector<DeducedTemplateArgument, 4> Deduced;
2451f4a2713aSLionel Sambuc   Deduced.resize(Partial->getTemplateParameters()->size());
2452f4a2713aSLionel Sambuc   if (TemplateDeductionResult Result = ::DeduceTemplateArguments(
2453f4a2713aSLionel Sambuc           *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(),
2454f4a2713aSLionel Sambuc           TemplateArgs, Info, Deduced))
2455f4a2713aSLionel Sambuc     return Result;
2456f4a2713aSLionel Sambuc 
2457f4a2713aSLionel Sambuc   SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end());
2458*0a6a1f1dSLionel Sambuc   InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs,
2459*0a6a1f1dSLionel Sambuc                              Info);
2460f4a2713aSLionel Sambuc   if (Inst.isInvalid())
2461f4a2713aSLionel Sambuc     return TDK_InstantiationDepth;
2462f4a2713aSLionel Sambuc 
2463f4a2713aSLionel Sambuc   if (Trap.hasErrorOccurred())
2464f4a2713aSLionel Sambuc     return Sema::TDK_SubstitutionFailure;
2465f4a2713aSLionel Sambuc 
2466f4a2713aSLionel Sambuc   return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
2467f4a2713aSLionel Sambuc                                            Deduced, Info);
2468f4a2713aSLionel Sambuc }
2469f4a2713aSLionel Sambuc 
2470f4a2713aSLionel Sambuc /// \brief Determine whether the given type T is a simple-template-id type.
isSimpleTemplateIdType(QualType T)2471f4a2713aSLionel Sambuc static bool isSimpleTemplateIdType(QualType T) {
2472f4a2713aSLionel Sambuc   if (const TemplateSpecializationType *Spec
2473f4a2713aSLionel Sambuc         = T->getAs<TemplateSpecializationType>())
2474*0a6a1f1dSLionel Sambuc     return Spec->getTemplateName().getAsTemplateDecl() != nullptr;
2475f4a2713aSLionel Sambuc 
2476f4a2713aSLionel Sambuc   return false;
2477f4a2713aSLionel Sambuc }
2478f4a2713aSLionel Sambuc 
2479f4a2713aSLionel Sambuc /// \brief Substitute the explicitly-provided template arguments into the
2480f4a2713aSLionel Sambuc /// given function template according to C++ [temp.arg.explicit].
2481f4a2713aSLionel Sambuc ///
2482f4a2713aSLionel Sambuc /// \param FunctionTemplate the function template into which the explicit
2483f4a2713aSLionel Sambuc /// template arguments will be substituted.
2484f4a2713aSLionel Sambuc ///
2485f4a2713aSLionel Sambuc /// \param ExplicitTemplateArgs the explicitly-specified template
2486f4a2713aSLionel Sambuc /// arguments.
2487f4a2713aSLionel Sambuc ///
2488f4a2713aSLionel Sambuc /// \param Deduced the deduced template arguments, which will be populated
2489f4a2713aSLionel Sambuc /// with the converted and checked explicit template arguments.
2490f4a2713aSLionel Sambuc ///
2491f4a2713aSLionel Sambuc /// \param ParamTypes will be populated with the instantiated function
2492f4a2713aSLionel Sambuc /// parameters.
2493f4a2713aSLionel Sambuc ///
2494f4a2713aSLionel Sambuc /// \param FunctionType if non-NULL, the result type of the function template
2495f4a2713aSLionel Sambuc /// will also be instantiated and the pointed-to value will be updated with
2496f4a2713aSLionel Sambuc /// the instantiated function type.
2497f4a2713aSLionel Sambuc ///
2498f4a2713aSLionel Sambuc /// \param Info if substitution fails for any reason, this object will be
2499f4a2713aSLionel Sambuc /// populated with more information about the failure.
2500f4a2713aSLionel Sambuc ///
2501f4a2713aSLionel Sambuc /// \returns TDK_Success if substitution was successful, or some failure
2502f4a2713aSLionel Sambuc /// condition.
2503f4a2713aSLionel Sambuc Sema::TemplateDeductionResult
SubstituteExplicitTemplateArguments(FunctionTemplateDecl * FunctionTemplate,TemplateArgumentListInfo & ExplicitTemplateArgs,SmallVectorImpl<DeducedTemplateArgument> & Deduced,SmallVectorImpl<QualType> & ParamTypes,QualType * FunctionType,TemplateDeductionInfo & Info)2504f4a2713aSLionel Sambuc Sema::SubstituteExplicitTemplateArguments(
2505f4a2713aSLionel Sambuc                                       FunctionTemplateDecl *FunctionTemplate,
2506f4a2713aSLionel Sambuc                                TemplateArgumentListInfo &ExplicitTemplateArgs,
2507f4a2713aSLionel Sambuc                        SmallVectorImpl<DeducedTemplateArgument> &Deduced,
2508f4a2713aSLionel Sambuc                                  SmallVectorImpl<QualType> &ParamTypes,
2509f4a2713aSLionel Sambuc                                           QualType *FunctionType,
2510f4a2713aSLionel Sambuc                                           TemplateDeductionInfo &Info) {
2511f4a2713aSLionel Sambuc   FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2512f4a2713aSLionel Sambuc   TemplateParameterList *TemplateParams
2513f4a2713aSLionel Sambuc     = FunctionTemplate->getTemplateParameters();
2514f4a2713aSLionel Sambuc 
2515f4a2713aSLionel Sambuc   if (ExplicitTemplateArgs.size() == 0) {
2516f4a2713aSLionel Sambuc     // No arguments to substitute; just copy over the parameter types and
2517f4a2713aSLionel Sambuc     // fill in the function type.
2518*0a6a1f1dSLionel Sambuc     for (auto P : Function->params())
2519*0a6a1f1dSLionel Sambuc       ParamTypes.push_back(P->getType());
2520f4a2713aSLionel Sambuc 
2521f4a2713aSLionel Sambuc     if (FunctionType)
2522f4a2713aSLionel Sambuc       *FunctionType = Function->getType();
2523f4a2713aSLionel Sambuc     return TDK_Success;
2524f4a2713aSLionel Sambuc   }
2525f4a2713aSLionel Sambuc 
2526f4a2713aSLionel Sambuc   // Unevaluated SFINAE context.
2527f4a2713aSLionel Sambuc   EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated);
2528f4a2713aSLionel Sambuc   SFINAETrap Trap(*this);
2529f4a2713aSLionel Sambuc 
2530f4a2713aSLionel Sambuc   // C++ [temp.arg.explicit]p3:
2531f4a2713aSLionel Sambuc   //   Template arguments that are present shall be specified in the
2532f4a2713aSLionel Sambuc   //   declaration order of their corresponding template-parameters. The
2533f4a2713aSLionel Sambuc   //   template argument list shall not specify more template-arguments than
2534f4a2713aSLionel Sambuc   //   there are corresponding template-parameters.
2535f4a2713aSLionel Sambuc   SmallVector<TemplateArgument, 4> Builder;
2536f4a2713aSLionel Sambuc 
2537f4a2713aSLionel Sambuc   // Enter a new template instantiation context where we check the
2538f4a2713aSLionel Sambuc   // explicitly-specified template arguments against this function template,
2539f4a2713aSLionel Sambuc   // and then substitute them into the function parameter types.
2540f4a2713aSLionel Sambuc   SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end());
2541*0a6a1f1dSLionel Sambuc   InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate,
2542*0a6a1f1dSLionel Sambuc                              DeducedArgs,
2543f4a2713aSLionel Sambuc            ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution,
2544f4a2713aSLionel Sambuc                              Info);
2545f4a2713aSLionel Sambuc   if (Inst.isInvalid())
2546f4a2713aSLionel Sambuc     return TDK_InstantiationDepth;
2547f4a2713aSLionel Sambuc 
2548f4a2713aSLionel Sambuc   if (CheckTemplateArgumentList(FunctionTemplate,
2549f4a2713aSLionel Sambuc                                 SourceLocation(),
2550f4a2713aSLionel Sambuc                                 ExplicitTemplateArgs,
2551f4a2713aSLionel Sambuc                                 true,
2552f4a2713aSLionel Sambuc                                 Builder) || Trap.hasErrorOccurred()) {
2553f4a2713aSLionel Sambuc     unsigned Index = Builder.size();
2554f4a2713aSLionel Sambuc     if (Index >= TemplateParams->size())
2555f4a2713aSLionel Sambuc       Index = TemplateParams->size() - 1;
2556f4a2713aSLionel Sambuc     Info.Param = makeTemplateParameter(TemplateParams->getParam(Index));
2557f4a2713aSLionel Sambuc     return TDK_InvalidExplicitArguments;
2558f4a2713aSLionel Sambuc   }
2559f4a2713aSLionel Sambuc 
2560f4a2713aSLionel Sambuc   // Form the template argument list from the explicitly-specified
2561f4a2713aSLionel Sambuc   // template arguments.
2562f4a2713aSLionel Sambuc   TemplateArgumentList *ExplicitArgumentList
2563f4a2713aSLionel Sambuc     = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
2564f4a2713aSLionel Sambuc   Info.reset(ExplicitArgumentList);
2565f4a2713aSLionel Sambuc 
2566f4a2713aSLionel Sambuc   // Template argument deduction and the final substitution should be
2567f4a2713aSLionel Sambuc   // done in the context of the templated declaration.  Explicit
2568f4a2713aSLionel Sambuc   // argument substitution, on the other hand, needs to happen in the
2569f4a2713aSLionel Sambuc   // calling context.
2570f4a2713aSLionel Sambuc   ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
2571f4a2713aSLionel Sambuc 
2572f4a2713aSLionel Sambuc   // If we deduced template arguments for a template parameter pack,
2573f4a2713aSLionel Sambuc   // note that the template argument pack is partially substituted and record
2574f4a2713aSLionel Sambuc   // the explicit template arguments. They'll be used as part of deduction
2575f4a2713aSLionel Sambuc   // for this template parameter pack.
2576f4a2713aSLionel Sambuc   for (unsigned I = 0, N = Builder.size(); I != N; ++I) {
2577f4a2713aSLionel Sambuc     const TemplateArgument &Arg = Builder[I];
2578f4a2713aSLionel Sambuc     if (Arg.getKind() == TemplateArgument::Pack) {
2579f4a2713aSLionel Sambuc       CurrentInstantiationScope->SetPartiallySubstitutedPack(
2580f4a2713aSLionel Sambuc                                                  TemplateParams->getParam(I),
2581f4a2713aSLionel Sambuc                                                              Arg.pack_begin(),
2582f4a2713aSLionel Sambuc                                                              Arg.pack_size());
2583f4a2713aSLionel Sambuc       break;
2584f4a2713aSLionel Sambuc     }
2585f4a2713aSLionel Sambuc   }
2586f4a2713aSLionel Sambuc 
2587f4a2713aSLionel Sambuc   const FunctionProtoType *Proto
2588f4a2713aSLionel Sambuc     = Function->getType()->getAs<FunctionProtoType>();
2589f4a2713aSLionel Sambuc   assert(Proto && "Function template does not have a prototype?");
2590f4a2713aSLionel Sambuc 
2591*0a6a1f1dSLionel Sambuc   // Isolate our substituted parameters from our caller.
2592*0a6a1f1dSLionel Sambuc   LocalInstantiationScope InstScope(*this, /*MergeWithOuterScope*/true);
2593*0a6a1f1dSLionel Sambuc 
2594f4a2713aSLionel Sambuc   // Instantiate the types of each of the function parameters given the
2595f4a2713aSLionel Sambuc   // explicitly-specified template arguments. If the function has a trailing
2596f4a2713aSLionel Sambuc   // return type, substitute it after the arguments to ensure we substitute
2597f4a2713aSLionel Sambuc   // in lexical order.
2598f4a2713aSLionel Sambuc   if (Proto->hasTrailingReturn()) {
2599f4a2713aSLionel Sambuc     if (SubstParmTypes(Function->getLocation(),
2600f4a2713aSLionel Sambuc                        Function->param_begin(), Function->getNumParams(),
2601f4a2713aSLionel Sambuc                        MultiLevelTemplateArgumentList(*ExplicitArgumentList),
2602f4a2713aSLionel Sambuc                        ParamTypes))
2603f4a2713aSLionel Sambuc       return TDK_SubstitutionFailure;
2604f4a2713aSLionel Sambuc   }
2605f4a2713aSLionel Sambuc 
2606f4a2713aSLionel Sambuc   // Instantiate the return type.
2607f4a2713aSLionel Sambuc   QualType ResultType;
2608f4a2713aSLionel Sambuc   {
2609f4a2713aSLionel Sambuc     // C++11 [expr.prim.general]p3:
2610f4a2713aSLionel Sambuc     //   If a declaration declares a member function or member function
2611f4a2713aSLionel Sambuc     //   template of a class X, the expression this is a prvalue of type
2612f4a2713aSLionel Sambuc     //   "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
2613f4a2713aSLionel Sambuc     //   and the end of the function-definition, member-declarator, or
2614f4a2713aSLionel Sambuc     //   declarator.
2615f4a2713aSLionel Sambuc     unsigned ThisTypeQuals = 0;
2616*0a6a1f1dSLionel Sambuc     CXXRecordDecl *ThisContext = nullptr;
2617f4a2713aSLionel Sambuc     if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) {
2618f4a2713aSLionel Sambuc       ThisContext = Method->getParent();
2619f4a2713aSLionel Sambuc       ThisTypeQuals = Method->getTypeQualifiers();
2620f4a2713aSLionel Sambuc     }
2621f4a2713aSLionel Sambuc 
2622f4a2713aSLionel Sambuc     CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals,
2623f4a2713aSLionel Sambuc                                getLangOpts().CPlusPlus11);
2624f4a2713aSLionel Sambuc 
2625*0a6a1f1dSLionel Sambuc     ResultType =
2626*0a6a1f1dSLionel Sambuc         SubstType(Proto->getReturnType(),
2627f4a2713aSLionel Sambuc                   MultiLevelTemplateArgumentList(*ExplicitArgumentList),
2628*0a6a1f1dSLionel Sambuc                   Function->getTypeSpecStartLoc(), Function->getDeclName());
2629f4a2713aSLionel Sambuc     if (ResultType.isNull() || Trap.hasErrorOccurred())
2630f4a2713aSLionel Sambuc       return TDK_SubstitutionFailure;
2631f4a2713aSLionel Sambuc   }
2632f4a2713aSLionel Sambuc 
2633f4a2713aSLionel Sambuc   // Instantiate the types of each of the function parameters given the
2634f4a2713aSLionel Sambuc   // explicitly-specified template arguments if we didn't do so earlier.
2635f4a2713aSLionel Sambuc   if (!Proto->hasTrailingReturn() &&
2636f4a2713aSLionel Sambuc       SubstParmTypes(Function->getLocation(),
2637f4a2713aSLionel Sambuc                      Function->param_begin(), Function->getNumParams(),
2638f4a2713aSLionel Sambuc                      MultiLevelTemplateArgumentList(*ExplicitArgumentList),
2639f4a2713aSLionel Sambuc                      ParamTypes))
2640f4a2713aSLionel Sambuc     return TDK_SubstitutionFailure;
2641f4a2713aSLionel Sambuc 
2642f4a2713aSLionel Sambuc   if (FunctionType) {
2643f4a2713aSLionel Sambuc     *FunctionType = BuildFunctionType(ResultType, ParamTypes,
2644f4a2713aSLionel Sambuc                                       Function->getLocation(),
2645f4a2713aSLionel Sambuc                                       Function->getDeclName(),
2646f4a2713aSLionel Sambuc                                       Proto->getExtProtoInfo());
2647f4a2713aSLionel Sambuc     if (FunctionType->isNull() || Trap.hasErrorOccurred())
2648f4a2713aSLionel Sambuc       return TDK_SubstitutionFailure;
2649f4a2713aSLionel Sambuc   }
2650f4a2713aSLionel Sambuc 
2651f4a2713aSLionel Sambuc   // C++ [temp.arg.explicit]p2:
2652f4a2713aSLionel Sambuc   //   Trailing template arguments that can be deduced (14.8.2) may be
2653f4a2713aSLionel Sambuc   //   omitted from the list of explicit template-arguments. If all of the
2654f4a2713aSLionel Sambuc   //   template arguments can be deduced, they may all be omitted; in this
2655f4a2713aSLionel Sambuc   //   case, the empty template argument list <> itself may also be omitted.
2656f4a2713aSLionel Sambuc   //
2657f4a2713aSLionel Sambuc   // Take all of the explicitly-specified arguments and put them into
2658f4a2713aSLionel Sambuc   // the set of deduced template arguments. Explicitly-specified
2659f4a2713aSLionel Sambuc   // parameter packs, however, will be set to NULL since the deduction
2660f4a2713aSLionel Sambuc   // mechanisms handle explicitly-specified argument packs directly.
2661f4a2713aSLionel Sambuc   Deduced.reserve(TemplateParams->size());
2662f4a2713aSLionel Sambuc   for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) {
2663f4a2713aSLionel Sambuc     const TemplateArgument &Arg = ExplicitArgumentList->get(I);
2664f4a2713aSLionel Sambuc     if (Arg.getKind() == TemplateArgument::Pack)
2665f4a2713aSLionel Sambuc       Deduced.push_back(DeducedTemplateArgument());
2666f4a2713aSLionel Sambuc     else
2667f4a2713aSLionel Sambuc       Deduced.push_back(Arg);
2668f4a2713aSLionel Sambuc   }
2669f4a2713aSLionel Sambuc 
2670f4a2713aSLionel Sambuc   return TDK_Success;
2671f4a2713aSLionel Sambuc }
2672f4a2713aSLionel Sambuc 
2673f4a2713aSLionel Sambuc /// \brief Check whether the deduced argument type for a call to a function
2674f4a2713aSLionel Sambuc /// template matches the actual argument type per C++ [temp.deduct.call]p4.
2675f4a2713aSLionel Sambuc static bool
CheckOriginalCallArgDeduction(Sema & S,Sema::OriginalCallArg OriginalArg,QualType DeducedA)2676f4a2713aSLionel Sambuc CheckOriginalCallArgDeduction(Sema &S, Sema::OriginalCallArg OriginalArg,
2677f4a2713aSLionel Sambuc                               QualType DeducedA) {
2678f4a2713aSLionel Sambuc   ASTContext &Context = S.Context;
2679f4a2713aSLionel Sambuc 
2680f4a2713aSLionel Sambuc   QualType A = OriginalArg.OriginalArgType;
2681f4a2713aSLionel Sambuc   QualType OriginalParamType = OriginalArg.OriginalParamType;
2682f4a2713aSLionel Sambuc 
2683f4a2713aSLionel Sambuc   // Check for type equality (top-level cv-qualifiers are ignored).
2684f4a2713aSLionel Sambuc   if (Context.hasSameUnqualifiedType(A, DeducedA))
2685f4a2713aSLionel Sambuc     return false;
2686f4a2713aSLionel Sambuc 
2687f4a2713aSLionel Sambuc   // Strip off references on the argument types; they aren't needed for
2688f4a2713aSLionel Sambuc   // the following checks.
2689f4a2713aSLionel Sambuc   if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>())
2690f4a2713aSLionel Sambuc     DeducedA = DeducedARef->getPointeeType();
2691f4a2713aSLionel Sambuc   if (const ReferenceType *ARef = A->getAs<ReferenceType>())
2692f4a2713aSLionel Sambuc     A = ARef->getPointeeType();
2693f4a2713aSLionel Sambuc 
2694f4a2713aSLionel Sambuc   // C++ [temp.deduct.call]p4:
2695f4a2713aSLionel Sambuc   //   [...] However, there are three cases that allow a difference:
2696f4a2713aSLionel Sambuc   //     - If the original P is a reference type, the deduced A (i.e., the
2697f4a2713aSLionel Sambuc   //       type referred to by the reference) can be more cv-qualified than
2698f4a2713aSLionel Sambuc   //       the transformed A.
2699f4a2713aSLionel Sambuc   if (const ReferenceType *OriginalParamRef
2700f4a2713aSLionel Sambuc       = OriginalParamType->getAs<ReferenceType>()) {
2701f4a2713aSLionel Sambuc     // We don't want to keep the reference around any more.
2702f4a2713aSLionel Sambuc     OriginalParamType = OriginalParamRef->getPointeeType();
2703f4a2713aSLionel Sambuc 
2704f4a2713aSLionel Sambuc     Qualifiers AQuals = A.getQualifiers();
2705f4a2713aSLionel Sambuc     Qualifiers DeducedAQuals = DeducedA.getQualifiers();
2706f4a2713aSLionel Sambuc 
2707f4a2713aSLionel Sambuc     // Under Objective-C++ ARC, the deduced type may have implicitly
2708f4a2713aSLionel Sambuc     // been given strong or (when dealing with a const reference)
2709f4a2713aSLionel Sambuc     // unsafe_unretained lifetime. If so, update the original
2710f4a2713aSLionel Sambuc     // qualifiers to include this lifetime.
2711f4a2713aSLionel Sambuc     if (S.getLangOpts().ObjCAutoRefCount &&
2712f4a2713aSLionel Sambuc         ((DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong &&
2713f4a2713aSLionel Sambuc           AQuals.getObjCLifetime() == Qualifiers::OCL_None) ||
2714f4a2713aSLionel Sambuc          (DeducedAQuals.hasConst() &&
2715f4a2713aSLionel Sambuc           DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone))) {
2716f4a2713aSLionel Sambuc       AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime());
2717f4a2713aSLionel Sambuc     }
2718f4a2713aSLionel Sambuc 
2719f4a2713aSLionel Sambuc     if (AQuals == DeducedAQuals) {
2720f4a2713aSLionel Sambuc       // Qualifiers match; there's nothing to do.
2721f4a2713aSLionel Sambuc     } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) {
2722f4a2713aSLionel Sambuc       return true;
2723f4a2713aSLionel Sambuc     } else {
2724f4a2713aSLionel Sambuc       // Qualifiers are compatible, so have the argument type adopt the
2725f4a2713aSLionel Sambuc       // deduced argument type's qualifiers as if we had performed the
2726f4a2713aSLionel Sambuc       // qualification conversion.
2727f4a2713aSLionel Sambuc       A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals);
2728f4a2713aSLionel Sambuc     }
2729f4a2713aSLionel Sambuc   }
2730f4a2713aSLionel Sambuc 
2731f4a2713aSLionel Sambuc   //    - The transformed A can be another pointer or pointer to member
2732f4a2713aSLionel Sambuc   //      type that can be converted to the deduced A via a qualification
2733f4a2713aSLionel Sambuc   //      conversion.
2734f4a2713aSLionel Sambuc   //
2735f4a2713aSLionel Sambuc   // Also allow conversions which merely strip [[noreturn]] from function types
2736f4a2713aSLionel Sambuc   // (recursively) as an extension.
2737f4a2713aSLionel Sambuc   // FIXME: Currently, this doesn't play nicely with qualification conversions.
2738f4a2713aSLionel Sambuc   bool ObjCLifetimeConversion = false;
2739f4a2713aSLionel Sambuc   QualType ResultTy;
2740f4a2713aSLionel Sambuc   if ((A->isAnyPointerType() || A->isMemberPointerType()) &&
2741f4a2713aSLionel Sambuc       (S.IsQualificationConversion(A, DeducedA, false,
2742f4a2713aSLionel Sambuc                                    ObjCLifetimeConversion) ||
2743f4a2713aSLionel Sambuc        S.IsNoReturnConversion(A, DeducedA, ResultTy)))
2744f4a2713aSLionel Sambuc     return false;
2745f4a2713aSLionel Sambuc 
2746f4a2713aSLionel Sambuc 
2747f4a2713aSLionel Sambuc   //    - If P is a class and P has the form simple-template-id, then the
2748f4a2713aSLionel Sambuc   //      transformed A can be a derived class of the deduced A. [...]
2749f4a2713aSLionel Sambuc   //     [...] Likewise, if P is a pointer to a class of the form
2750f4a2713aSLionel Sambuc   //      simple-template-id, the transformed A can be a pointer to a
2751f4a2713aSLionel Sambuc   //      derived class pointed to by the deduced A.
2752f4a2713aSLionel Sambuc   if (const PointerType *OriginalParamPtr
2753f4a2713aSLionel Sambuc       = OriginalParamType->getAs<PointerType>()) {
2754f4a2713aSLionel Sambuc     if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) {
2755f4a2713aSLionel Sambuc       if (const PointerType *APtr = A->getAs<PointerType>()) {
2756f4a2713aSLionel Sambuc         if (A->getPointeeType()->isRecordType()) {
2757f4a2713aSLionel Sambuc           OriginalParamType = OriginalParamPtr->getPointeeType();
2758f4a2713aSLionel Sambuc           DeducedA = DeducedAPtr->getPointeeType();
2759f4a2713aSLionel Sambuc           A = APtr->getPointeeType();
2760f4a2713aSLionel Sambuc         }
2761f4a2713aSLionel Sambuc       }
2762f4a2713aSLionel Sambuc     }
2763f4a2713aSLionel Sambuc   }
2764f4a2713aSLionel Sambuc 
2765f4a2713aSLionel Sambuc   if (Context.hasSameUnqualifiedType(A, DeducedA))
2766f4a2713aSLionel Sambuc     return false;
2767f4a2713aSLionel Sambuc 
2768f4a2713aSLionel Sambuc   if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) &&
2769f4a2713aSLionel Sambuc       S.IsDerivedFrom(A, DeducedA))
2770f4a2713aSLionel Sambuc     return false;
2771f4a2713aSLionel Sambuc 
2772f4a2713aSLionel Sambuc   return true;
2773f4a2713aSLionel Sambuc }
2774f4a2713aSLionel Sambuc 
2775f4a2713aSLionel Sambuc /// \brief Finish template argument deduction for a function template,
2776f4a2713aSLionel Sambuc /// checking the deduced template arguments for completeness and forming
2777f4a2713aSLionel Sambuc /// the function template specialization.
2778f4a2713aSLionel Sambuc ///
2779f4a2713aSLionel Sambuc /// \param OriginalCallArgs If non-NULL, the original call arguments against
2780f4a2713aSLionel Sambuc /// which the deduced argument types should be compared.
2781f4a2713aSLionel Sambuc Sema::TemplateDeductionResult
FinishTemplateArgumentDeduction(FunctionTemplateDecl * FunctionTemplate,SmallVectorImpl<DeducedTemplateArgument> & Deduced,unsigned NumExplicitlySpecified,FunctionDecl * & Specialization,TemplateDeductionInfo & Info,SmallVectorImpl<OriginalCallArg> const * OriginalCallArgs)2782f4a2713aSLionel Sambuc Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
2783f4a2713aSLionel Sambuc                        SmallVectorImpl<DeducedTemplateArgument> &Deduced,
2784f4a2713aSLionel Sambuc                                       unsigned NumExplicitlySpecified,
2785f4a2713aSLionel Sambuc                                       FunctionDecl *&Specialization,
2786f4a2713aSLionel Sambuc                                       TemplateDeductionInfo &Info,
2787f4a2713aSLionel Sambuc         SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs) {
2788f4a2713aSLionel Sambuc   TemplateParameterList *TemplateParams
2789f4a2713aSLionel Sambuc     = FunctionTemplate->getTemplateParameters();
2790f4a2713aSLionel Sambuc 
2791f4a2713aSLionel Sambuc   // Unevaluated SFINAE context.
2792f4a2713aSLionel Sambuc   EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated);
2793f4a2713aSLionel Sambuc   SFINAETrap Trap(*this);
2794f4a2713aSLionel Sambuc 
2795f4a2713aSLionel Sambuc   // Enter a new template instantiation context while we instantiate the
2796f4a2713aSLionel Sambuc   // actual function declaration.
2797f4a2713aSLionel Sambuc   SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end());
2798*0a6a1f1dSLionel Sambuc   InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate,
2799*0a6a1f1dSLionel Sambuc                              DeducedArgs,
2800f4a2713aSLionel Sambuc               ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution,
2801f4a2713aSLionel Sambuc                              Info);
2802f4a2713aSLionel Sambuc   if (Inst.isInvalid())
2803f4a2713aSLionel Sambuc     return TDK_InstantiationDepth;
2804f4a2713aSLionel Sambuc 
2805f4a2713aSLionel Sambuc   ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
2806f4a2713aSLionel Sambuc 
2807f4a2713aSLionel Sambuc   // C++ [temp.deduct.type]p2:
2808f4a2713aSLionel Sambuc   //   [...] or if any template argument remains neither deduced nor
2809f4a2713aSLionel Sambuc   //   explicitly specified, template argument deduction fails.
2810f4a2713aSLionel Sambuc   SmallVector<TemplateArgument, 4> Builder;
2811f4a2713aSLionel Sambuc   for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
2812f4a2713aSLionel Sambuc     NamedDecl *Param = TemplateParams->getParam(I);
2813f4a2713aSLionel Sambuc 
2814f4a2713aSLionel Sambuc     if (!Deduced[I].isNull()) {
2815f4a2713aSLionel Sambuc       if (I < NumExplicitlySpecified) {
2816f4a2713aSLionel Sambuc         // We have already fully type-checked and converted this
2817f4a2713aSLionel Sambuc         // argument, because it was explicitly-specified. Just record the
2818f4a2713aSLionel Sambuc         // presence of this argument.
2819f4a2713aSLionel Sambuc         Builder.push_back(Deduced[I]);
2820*0a6a1f1dSLionel Sambuc         // We may have had explicitly-specified template arguments for a
2821*0a6a1f1dSLionel Sambuc         // template parameter pack (that may or may not have been extended
2822*0a6a1f1dSLionel Sambuc         // via additional deduced arguments).
2823*0a6a1f1dSLionel Sambuc         if (Param->isParameterPack() && CurrentInstantiationScope) {
2824*0a6a1f1dSLionel Sambuc           if (CurrentInstantiationScope->getPartiallySubstitutedPack() ==
2825*0a6a1f1dSLionel Sambuc               Param) {
2826*0a6a1f1dSLionel Sambuc             // Forget the partially-substituted pack; its substitution is now
2827*0a6a1f1dSLionel Sambuc             // complete.
2828*0a6a1f1dSLionel Sambuc             CurrentInstantiationScope->ResetPartiallySubstitutedPack();
2829*0a6a1f1dSLionel Sambuc           }
2830*0a6a1f1dSLionel Sambuc         }
2831f4a2713aSLionel Sambuc         continue;
2832f4a2713aSLionel Sambuc       }
2833f4a2713aSLionel Sambuc       // We have deduced this argument, so it still needs to be
2834f4a2713aSLionel Sambuc       // checked and converted.
2835f4a2713aSLionel Sambuc 
2836f4a2713aSLionel Sambuc       // First, for a non-type template parameter type that is
2837f4a2713aSLionel Sambuc       // initialized by a declaration, we need the type of the
2838f4a2713aSLionel Sambuc       // corresponding non-type template parameter.
2839f4a2713aSLionel Sambuc       QualType NTTPType;
2840f4a2713aSLionel Sambuc       if (NonTypeTemplateParmDecl *NTTP
2841f4a2713aSLionel Sambuc                                 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2842f4a2713aSLionel Sambuc         NTTPType = NTTP->getType();
2843f4a2713aSLionel Sambuc         if (NTTPType->isDependentType()) {
2844f4a2713aSLionel Sambuc           TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
2845f4a2713aSLionel Sambuc                                             Builder.data(), Builder.size());
2846f4a2713aSLionel Sambuc           NTTPType = SubstType(NTTPType,
2847f4a2713aSLionel Sambuc                                MultiLevelTemplateArgumentList(TemplateArgs),
2848f4a2713aSLionel Sambuc                                NTTP->getLocation(),
2849f4a2713aSLionel Sambuc                                NTTP->getDeclName());
2850f4a2713aSLionel Sambuc           if (NTTPType.isNull()) {
2851f4a2713aSLionel Sambuc             Info.Param = makeTemplateParameter(Param);
2852f4a2713aSLionel Sambuc             // FIXME: These template arguments are temporary. Free them!
2853f4a2713aSLionel Sambuc             Info.reset(TemplateArgumentList::CreateCopy(Context,
2854f4a2713aSLionel Sambuc                                                         Builder.data(),
2855f4a2713aSLionel Sambuc                                                         Builder.size()));
2856f4a2713aSLionel Sambuc             return TDK_SubstitutionFailure;
2857f4a2713aSLionel Sambuc           }
2858f4a2713aSLionel Sambuc         }
2859f4a2713aSLionel Sambuc       }
2860f4a2713aSLionel Sambuc 
2861f4a2713aSLionel Sambuc       if (ConvertDeducedTemplateArgument(*this, Param, Deduced[I],
2862f4a2713aSLionel Sambuc                                          FunctionTemplate, NTTPType, 0, Info,
2863f4a2713aSLionel Sambuc                                          true, Builder)) {
2864f4a2713aSLionel Sambuc         Info.Param = makeTemplateParameter(Param);
2865f4a2713aSLionel Sambuc         // FIXME: These template arguments are temporary. Free them!
2866f4a2713aSLionel Sambuc         Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
2867f4a2713aSLionel Sambuc                                                     Builder.size()));
2868f4a2713aSLionel Sambuc         return TDK_SubstitutionFailure;
2869f4a2713aSLionel Sambuc       }
2870f4a2713aSLionel Sambuc 
2871f4a2713aSLionel Sambuc       continue;
2872f4a2713aSLionel Sambuc     }
2873f4a2713aSLionel Sambuc 
2874f4a2713aSLionel Sambuc     // C++0x [temp.arg.explicit]p3:
2875f4a2713aSLionel Sambuc     //    A trailing template parameter pack (14.5.3) not otherwise deduced will
2876f4a2713aSLionel Sambuc     //    be deduced to an empty sequence of template arguments.
2877f4a2713aSLionel Sambuc     // FIXME: Where did the word "trailing" come from?
2878f4a2713aSLionel Sambuc     if (Param->isTemplateParameterPack()) {
2879f4a2713aSLionel Sambuc       // We may have had explicitly-specified template arguments for this
2880f4a2713aSLionel Sambuc       // template parameter pack. If so, our empty deduction extends the
2881f4a2713aSLionel Sambuc       // explicitly-specified set (C++0x [temp.arg.explicit]p9).
2882f4a2713aSLionel Sambuc       const TemplateArgument *ExplicitArgs;
2883f4a2713aSLionel Sambuc       unsigned NumExplicitArgs;
2884f4a2713aSLionel Sambuc       if (CurrentInstantiationScope &&
2885f4a2713aSLionel Sambuc           CurrentInstantiationScope->getPartiallySubstitutedPack(&ExplicitArgs,
2886f4a2713aSLionel Sambuc                                                              &NumExplicitArgs)
2887f4a2713aSLionel Sambuc             == Param) {
2888f4a2713aSLionel Sambuc         Builder.push_back(TemplateArgument(ExplicitArgs, NumExplicitArgs));
2889f4a2713aSLionel Sambuc 
2890f4a2713aSLionel Sambuc         // Forget the partially-substituted pack; it's substitution is now
2891f4a2713aSLionel Sambuc         // complete.
2892f4a2713aSLionel Sambuc         CurrentInstantiationScope->ResetPartiallySubstitutedPack();
2893f4a2713aSLionel Sambuc       } else {
2894f4a2713aSLionel Sambuc         Builder.push_back(TemplateArgument::getEmptyPack());
2895f4a2713aSLionel Sambuc       }
2896f4a2713aSLionel Sambuc       continue;
2897f4a2713aSLionel Sambuc     }
2898f4a2713aSLionel Sambuc 
2899f4a2713aSLionel Sambuc     // Substitute into the default template argument, if available.
2900f4a2713aSLionel Sambuc     bool HasDefaultArg = false;
2901f4a2713aSLionel Sambuc     TemplateArgumentLoc DefArg
2902f4a2713aSLionel Sambuc       = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
2903f4a2713aSLionel Sambuc                                               FunctionTemplate->getLocation(),
2904f4a2713aSLionel Sambuc                                   FunctionTemplate->getSourceRange().getEnd(),
2905f4a2713aSLionel Sambuc                                                 Param,
2906f4a2713aSLionel Sambuc                                                 Builder, HasDefaultArg);
2907f4a2713aSLionel Sambuc 
2908f4a2713aSLionel Sambuc     // If there was no default argument, deduction is incomplete.
2909f4a2713aSLionel Sambuc     if (DefArg.getArgument().isNull()) {
2910f4a2713aSLionel Sambuc       Info.Param = makeTemplateParameter(
2911f4a2713aSLionel Sambuc                          const_cast<NamedDecl *>(TemplateParams->getParam(I)));
2912f4a2713aSLionel Sambuc       Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
2913f4a2713aSLionel Sambuc                                                   Builder.size()));
2914f4a2713aSLionel Sambuc       return HasDefaultArg ? TDK_SubstitutionFailure : TDK_Incomplete;
2915f4a2713aSLionel Sambuc     }
2916f4a2713aSLionel Sambuc 
2917f4a2713aSLionel Sambuc     // Check whether we can actually use the default argument.
2918f4a2713aSLionel Sambuc     if (CheckTemplateArgument(Param, DefArg,
2919f4a2713aSLionel Sambuc                               FunctionTemplate,
2920f4a2713aSLionel Sambuc                               FunctionTemplate->getLocation(),
2921f4a2713aSLionel Sambuc                               FunctionTemplate->getSourceRange().getEnd(),
2922f4a2713aSLionel Sambuc                               0, Builder,
2923f4a2713aSLionel Sambuc                               CTAK_Specified)) {
2924f4a2713aSLionel Sambuc       Info.Param = makeTemplateParameter(
2925f4a2713aSLionel Sambuc                          const_cast<NamedDecl *>(TemplateParams->getParam(I)));
2926f4a2713aSLionel Sambuc       // FIXME: These template arguments are temporary. Free them!
2927f4a2713aSLionel Sambuc       Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
2928f4a2713aSLionel Sambuc                                                   Builder.size()));
2929f4a2713aSLionel Sambuc       return TDK_SubstitutionFailure;
2930f4a2713aSLionel Sambuc     }
2931f4a2713aSLionel Sambuc 
2932f4a2713aSLionel Sambuc     // If we get here, we successfully used the default template argument.
2933f4a2713aSLionel Sambuc   }
2934f4a2713aSLionel Sambuc 
2935f4a2713aSLionel Sambuc   // Form the template argument list from the deduced template arguments.
2936f4a2713aSLionel Sambuc   TemplateArgumentList *DeducedArgumentList
2937f4a2713aSLionel Sambuc     = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
2938f4a2713aSLionel Sambuc   Info.reset(DeducedArgumentList);
2939f4a2713aSLionel Sambuc 
2940f4a2713aSLionel Sambuc   // Substitute the deduced template arguments into the function template
2941f4a2713aSLionel Sambuc   // declaration to produce the function template specialization.
2942f4a2713aSLionel Sambuc   DeclContext *Owner = FunctionTemplate->getDeclContext();
2943f4a2713aSLionel Sambuc   if (FunctionTemplate->getFriendObjectKind())
2944f4a2713aSLionel Sambuc     Owner = FunctionTemplate->getLexicalDeclContext();
2945f4a2713aSLionel Sambuc   Specialization = cast_or_null<FunctionDecl>(
2946f4a2713aSLionel Sambuc                       SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner,
2947f4a2713aSLionel Sambuc                          MultiLevelTemplateArgumentList(*DeducedArgumentList)));
2948f4a2713aSLionel Sambuc   if (!Specialization || Specialization->isInvalidDecl())
2949f4a2713aSLionel Sambuc     return TDK_SubstitutionFailure;
2950f4a2713aSLionel Sambuc 
2951f4a2713aSLionel Sambuc   assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
2952f4a2713aSLionel Sambuc          FunctionTemplate->getCanonicalDecl());
2953f4a2713aSLionel Sambuc 
2954f4a2713aSLionel Sambuc   // If the template argument list is owned by the function template
2955f4a2713aSLionel Sambuc   // specialization, release it.
2956f4a2713aSLionel Sambuc   if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList &&
2957f4a2713aSLionel Sambuc       !Trap.hasErrorOccurred())
2958f4a2713aSLionel Sambuc     Info.take();
2959f4a2713aSLionel Sambuc 
2960f4a2713aSLionel Sambuc   // There may have been an error that did not prevent us from constructing a
2961f4a2713aSLionel Sambuc   // declaration. Mark the declaration invalid and return with a substitution
2962f4a2713aSLionel Sambuc   // failure.
2963f4a2713aSLionel Sambuc   if (Trap.hasErrorOccurred()) {
2964f4a2713aSLionel Sambuc     Specialization->setInvalidDecl(true);
2965f4a2713aSLionel Sambuc     return TDK_SubstitutionFailure;
2966f4a2713aSLionel Sambuc   }
2967f4a2713aSLionel Sambuc 
2968f4a2713aSLionel Sambuc   if (OriginalCallArgs) {
2969f4a2713aSLionel Sambuc     // C++ [temp.deduct.call]p4:
2970f4a2713aSLionel Sambuc     //   In general, the deduction process attempts to find template argument
2971f4a2713aSLionel Sambuc     //   values that will make the deduced A identical to A (after the type A
2972f4a2713aSLionel Sambuc     //   is transformed as described above). [...]
2973f4a2713aSLionel Sambuc     for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) {
2974f4a2713aSLionel Sambuc       OriginalCallArg OriginalArg = (*OriginalCallArgs)[I];
2975f4a2713aSLionel Sambuc       unsigned ParamIdx = OriginalArg.ArgIdx;
2976f4a2713aSLionel Sambuc 
2977f4a2713aSLionel Sambuc       if (ParamIdx >= Specialization->getNumParams())
2978f4a2713aSLionel Sambuc         continue;
2979f4a2713aSLionel Sambuc 
2980f4a2713aSLionel Sambuc       QualType DeducedA = Specialization->getParamDecl(ParamIdx)->getType();
2981f4a2713aSLionel Sambuc       if (CheckOriginalCallArgDeduction(*this, OriginalArg, DeducedA))
2982f4a2713aSLionel Sambuc         return Sema::TDK_SubstitutionFailure;
2983f4a2713aSLionel Sambuc     }
2984f4a2713aSLionel Sambuc   }
2985f4a2713aSLionel Sambuc 
2986f4a2713aSLionel Sambuc   // If we suppressed any diagnostics while performing template argument
2987f4a2713aSLionel Sambuc   // deduction, and if we haven't already instantiated this declaration,
2988f4a2713aSLionel Sambuc   // keep track of these diagnostics. They'll be emitted if this specialization
2989f4a2713aSLionel Sambuc   // is actually used.
2990f4a2713aSLionel Sambuc   if (Info.diag_begin() != Info.diag_end()) {
2991f4a2713aSLionel Sambuc     SuppressedDiagnosticsMap::iterator
2992f4a2713aSLionel Sambuc       Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl());
2993f4a2713aSLionel Sambuc     if (Pos == SuppressedDiagnostics.end())
2994f4a2713aSLionel Sambuc         SuppressedDiagnostics[Specialization->getCanonicalDecl()]
2995f4a2713aSLionel Sambuc           .append(Info.diag_begin(), Info.diag_end());
2996f4a2713aSLionel Sambuc   }
2997f4a2713aSLionel Sambuc 
2998f4a2713aSLionel Sambuc   return TDK_Success;
2999f4a2713aSLionel Sambuc }
3000f4a2713aSLionel Sambuc 
3001f4a2713aSLionel Sambuc /// Gets the type of a function for template-argument-deducton
3002f4a2713aSLionel Sambuc /// purposes when it's considered as part of an overload set.
GetTypeOfFunction(Sema & S,const OverloadExpr::FindResult & R,FunctionDecl * Fn)3003f4a2713aSLionel Sambuc static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R,
3004f4a2713aSLionel Sambuc                                   FunctionDecl *Fn) {
3005f4a2713aSLionel Sambuc   // We may need to deduce the return type of the function now.
3006*0a6a1f1dSLionel Sambuc   if (S.getLangOpts().CPlusPlus14 && Fn->getReturnType()->isUndeducedType() &&
3007f4a2713aSLionel Sambuc       S.DeduceReturnType(Fn, R.Expression->getExprLoc(), /*Diagnose*/ false))
3008f4a2713aSLionel Sambuc     return QualType();
3009f4a2713aSLionel Sambuc 
3010f4a2713aSLionel Sambuc   if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
3011f4a2713aSLionel Sambuc     if (Method->isInstance()) {
3012f4a2713aSLionel Sambuc       // An instance method that's referenced in a form that doesn't
3013f4a2713aSLionel Sambuc       // look like a member pointer is just invalid.
3014f4a2713aSLionel Sambuc       if (!R.HasFormOfMemberPointer) return QualType();
3015f4a2713aSLionel Sambuc 
3016f4a2713aSLionel Sambuc       return S.Context.getMemberPointerType(Fn->getType(),
3017f4a2713aSLionel Sambuc                S.Context.getTypeDeclType(Method->getParent()).getTypePtr());
3018f4a2713aSLionel Sambuc     }
3019f4a2713aSLionel Sambuc 
3020f4a2713aSLionel Sambuc   if (!R.IsAddressOfOperand) return Fn->getType();
3021f4a2713aSLionel Sambuc   return S.Context.getPointerType(Fn->getType());
3022f4a2713aSLionel Sambuc }
3023f4a2713aSLionel Sambuc 
3024f4a2713aSLionel Sambuc /// Apply the deduction rules for overload sets.
3025f4a2713aSLionel Sambuc ///
3026f4a2713aSLionel Sambuc /// \return the null type if this argument should be treated as an
3027f4a2713aSLionel Sambuc /// undeduced context
3028f4a2713aSLionel Sambuc static QualType
ResolveOverloadForDeduction(Sema & S,TemplateParameterList * TemplateParams,Expr * Arg,QualType ParamType,bool ParamWasReference)3029f4a2713aSLionel Sambuc ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
3030f4a2713aSLionel Sambuc                             Expr *Arg, QualType ParamType,
3031f4a2713aSLionel Sambuc                             bool ParamWasReference) {
3032f4a2713aSLionel Sambuc 
3033f4a2713aSLionel Sambuc   OverloadExpr::FindResult R = OverloadExpr::find(Arg);
3034f4a2713aSLionel Sambuc 
3035f4a2713aSLionel Sambuc   OverloadExpr *Ovl = R.Expression;
3036f4a2713aSLionel Sambuc 
3037f4a2713aSLionel Sambuc   // C++0x [temp.deduct.call]p4
3038f4a2713aSLionel Sambuc   unsigned TDF = 0;
3039f4a2713aSLionel Sambuc   if (ParamWasReference)
3040f4a2713aSLionel Sambuc     TDF |= TDF_ParamWithReferenceType;
3041f4a2713aSLionel Sambuc   if (R.IsAddressOfOperand)
3042f4a2713aSLionel Sambuc     TDF |= TDF_IgnoreQualifiers;
3043f4a2713aSLionel Sambuc 
3044f4a2713aSLionel Sambuc   // C++0x [temp.deduct.call]p6:
3045f4a2713aSLionel Sambuc   //   When P is a function type, pointer to function type, or pointer
3046f4a2713aSLionel Sambuc   //   to member function type:
3047f4a2713aSLionel Sambuc 
3048f4a2713aSLionel Sambuc   if (!ParamType->isFunctionType() &&
3049f4a2713aSLionel Sambuc       !ParamType->isFunctionPointerType() &&
3050f4a2713aSLionel Sambuc       !ParamType->isMemberFunctionPointerType()) {
3051f4a2713aSLionel Sambuc     if (Ovl->hasExplicitTemplateArgs()) {
3052f4a2713aSLionel Sambuc       // But we can still look for an explicit specialization.
3053f4a2713aSLionel Sambuc       if (FunctionDecl *ExplicitSpec
3054f4a2713aSLionel Sambuc             = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
3055f4a2713aSLionel Sambuc         return GetTypeOfFunction(S, R, ExplicitSpec);
3056f4a2713aSLionel Sambuc     }
3057f4a2713aSLionel Sambuc 
3058f4a2713aSLionel Sambuc     return QualType();
3059f4a2713aSLionel Sambuc   }
3060f4a2713aSLionel Sambuc 
3061f4a2713aSLionel Sambuc   // Gather the explicit template arguments, if any.
3062f4a2713aSLionel Sambuc   TemplateArgumentListInfo ExplicitTemplateArgs;
3063f4a2713aSLionel Sambuc   if (Ovl->hasExplicitTemplateArgs())
3064f4a2713aSLionel Sambuc     Ovl->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs);
3065f4a2713aSLionel Sambuc   QualType Match;
3066f4a2713aSLionel Sambuc   for (UnresolvedSetIterator I = Ovl->decls_begin(),
3067f4a2713aSLionel Sambuc          E = Ovl->decls_end(); I != E; ++I) {
3068f4a2713aSLionel Sambuc     NamedDecl *D = (*I)->getUnderlyingDecl();
3069f4a2713aSLionel Sambuc 
3070f4a2713aSLionel Sambuc     if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) {
3071f4a2713aSLionel Sambuc       //   - If the argument is an overload set containing one or more
3072f4a2713aSLionel Sambuc       //     function templates, the parameter is treated as a
3073f4a2713aSLionel Sambuc       //     non-deduced context.
3074f4a2713aSLionel Sambuc       if (!Ovl->hasExplicitTemplateArgs())
3075f4a2713aSLionel Sambuc         return QualType();
3076f4a2713aSLionel Sambuc 
3077f4a2713aSLionel Sambuc       // Otherwise, see if we can resolve a function type
3078*0a6a1f1dSLionel Sambuc       FunctionDecl *Specialization = nullptr;
3079f4a2713aSLionel Sambuc       TemplateDeductionInfo Info(Ovl->getNameLoc());
3080f4a2713aSLionel Sambuc       if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs,
3081f4a2713aSLionel Sambuc                                     Specialization, Info))
3082f4a2713aSLionel Sambuc         continue;
3083f4a2713aSLionel Sambuc 
3084f4a2713aSLionel Sambuc       D = Specialization;
3085f4a2713aSLionel Sambuc     }
3086f4a2713aSLionel Sambuc 
3087f4a2713aSLionel Sambuc     FunctionDecl *Fn = cast<FunctionDecl>(D);
3088f4a2713aSLionel Sambuc     QualType ArgType = GetTypeOfFunction(S, R, Fn);
3089f4a2713aSLionel Sambuc     if (ArgType.isNull()) continue;
3090f4a2713aSLionel Sambuc 
3091f4a2713aSLionel Sambuc     // Function-to-pointer conversion.
3092f4a2713aSLionel Sambuc     if (!ParamWasReference && ParamType->isPointerType() &&
3093f4a2713aSLionel Sambuc         ArgType->isFunctionType())
3094f4a2713aSLionel Sambuc       ArgType = S.Context.getPointerType(ArgType);
3095f4a2713aSLionel Sambuc 
3096f4a2713aSLionel Sambuc     //   - If the argument is an overload set (not containing function
3097f4a2713aSLionel Sambuc     //     templates), trial argument deduction is attempted using each
3098f4a2713aSLionel Sambuc     //     of the members of the set. If deduction succeeds for only one
3099f4a2713aSLionel Sambuc     //     of the overload set members, that member is used as the
3100f4a2713aSLionel Sambuc     //     argument value for the deduction. If deduction succeeds for
3101f4a2713aSLionel Sambuc     //     more than one member of the overload set the parameter is
3102f4a2713aSLionel Sambuc     //     treated as a non-deduced context.
3103f4a2713aSLionel Sambuc 
3104f4a2713aSLionel Sambuc     // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
3105f4a2713aSLionel Sambuc     //   Type deduction is done independently for each P/A pair, and
3106f4a2713aSLionel Sambuc     //   the deduced template argument values are then combined.
3107f4a2713aSLionel Sambuc     // So we do not reject deductions which were made elsewhere.
3108f4a2713aSLionel Sambuc     SmallVector<DeducedTemplateArgument, 8>
3109f4a2713aSLionel Sambuc       Deduced(TemplateParams->size());
3110f4a2713aSLionel Sambuc     TemplateDeductionInfo Info(Ovl->getNameLoc());
3111f4a2713aSLionel Sambuc     Sema::TemplateDeductionResult Result
3112f4a2713aSLionel Sambuc       = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType,
3113f4a2713aSLionel Sambuc                                            ArgType, Info, Deduced, TDF);
3114f4a2713aSLionel Sambuc     if (Result) continue;
3115f4a2713aSLionel Sambuc     if (!Match.isNull()) return QualType();
3116f4a2713aSLionel Sambuc     Match = ArgType;
3117f4a2713aSLionel Sambuc   }
3118f4a2713aSLionel Sambuc 
3119f4a2713aSLionel Sambuc   return Match;
3120f4a2713aSLionel Sambuc }
3121f4a2713aSLionel Sambuc 
3122f4a2713aSLionel Sambuc /// \brief Perform the adjustments to the parameter and argument types
3123f4a2713aSLionel Sambuc /// described in C++ [temp.deduct.call].
3124f4a2713aSLionel Sambuc ///
3125f4a2713aSLionel Sambuc /// \returns true if the caller should not attempt to perform any template
3126f4a2713aSLionel Sambuc /// argument deduction based on this P/A pair because the argument is an
3127f4a2713aSLionel Sambuc /// overloaded function set that could not be resolved.
AdjustFunctionParmAndArgTypesForDeduction(Sema & S,TemplateParameterList * TemplateParams,QualType & ParamType,QualType & ArgType,Expr * Arg,unsigned & TDF)3128f4a2713aSLionel Sambuc static bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S,
3129f4a2713aSLionel Sambuc                                           TemplateParameterList *TemplateParams,
3130f4a2713aSLionel Sambuc                                                       QualType &ParamType,
3131f4a2713aSLionel Sambuc                                                       QualType &ArgType,
3132f4a2713aSLionel Sambuc                                                       Expr *Arg,
3133f4a2713aSLionel Sambuc                                                       unsigned &TDF) {
3134f4a2713aSLionel Sambuc   // C++0x [temp.deduct.call]p3:
3135f4a2713aSLionel Sambuc   //   If P is a cv-qualified type, the top level cv-qualifiers of P's type
3136f4a2713aSLionel Sambuc   //   are ignored for type deduction.
3137f4a2713aSLionel Sambuc   if (ParamType.hasQualifiers())
3138f4a2713aSLionel Sambuc     ParamType = ParamType.getUnqualifiedType();
3139f4a2713aSLionel Sambuc   const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>();
3140f4a2713aSLionel Sambuc   if (ParamRefType) {
3141f4a2713aSLionel Sambuc     QualType PointeeType = ParamRefType->getPointeeType();
3142f4a2713aSLionel Sambuc 
3143f4a2713aSLionel Sambuc     // If the argument has incomplete array type, try to complete its type.
3144f4a2713aSLionel Sambuc     if (ArgType->isIncompleteArrayType() && !S.RequireCompleteExprType(Arg, 0))
3145f4a2713aSLionel Sambuc       ArgType = Arg->getType();
3146f4a2713aSLionel Sambuc 
3147f4a2713aSLionel Sambuc     //   [C++0x] If P is an rvalue reference to a cv-unqualified
3148f4a2713aSLionel Sambuc     //   template parameter and the argument is an lvalue, the type
3149f4a2713aSLionel Sambuc     //   "lvalue reference to A" is used in place of A for type
3150f4a2713aSLionel Sambuc     //   deduction.
3151f4a2713aSLionel Sambuc     if (isa<RValueReferenceType>(ParamType)) {
3152f4a2713aSLionel Sambuc       if (!PointeeType.getQualifiers() &&
3153f4a2713aSLionel Sambuc           isa<TemplateTypeParmType>(PointeeType) &&
3154f4a2713aSLionel Sambuc           Arg->Classify(S.Context).isLValue() &&
3155f4a2713aSLionel Sambuc           Arg->getType() != S.Context.OverloadTy &&
3156f4a2713aSLionel Sambuc           Arg->getType() != S.Context.BoundMemberTy)
3157f4a2713aSLionel Sambuc         ArgType = S.Context.getLValueReferenceType(ArgType);
3158f4a2713aSLionel Sambuc     }
3159f4a2713aSLionel Sambuc 
3160f4a2713aSLionel Sambuc     //   [...] If P is a reference type, the type referred to by P is used
3161f4a2713aSLionel Sambuc     //   for type deduction.
3162f4a2713aSLionel Sambuc     ParamType = PointeeType;
3163f4a2713aSLionel Sambuc   }
3164f4a2713aSLionel Sambuc 
3165f4a2713aSLionel Sambuc   // Overload sets usually make this parameter an undeduced
3166f4a2713aSLionel Sambuc   // context, but there are sometimes special circumstances.
3167f4a2713aSLionel Sambuc   if (ArgType == S.Context.OverloadTy) {
3168f4a2713aSLionel Sambuc     ArgType = ResolveOverloadForDeduction(S, TemplateParams,
3169f4a2713aSLionel Sambuc                                           Arg, ParamType,
3170*0a6a1f1dSLionel Sambuc                                           ParamRefType != nullptr);
3171f4a2713aSLionel Sambuc     if (ArgType.isNull())
3172f4a2713aSLionel Sambuc       return true;
3173f4a2713aSLionel Sambuc   }
3174f4a2713aSLionel Sambuc 
3175f4a2713aSLionel Sambuc   if (ParamRefType) {
3176f4a2713aSLionel Sambuc     // C++0x [temp.deduct.call]p3:
3177f4a2713aSLionel Sambuc     //   [...] If P is of the form T&&, where T is a template parameter, and
3178f4a2713aSLionel Sambuc     //   the argument is an lvalue, the type A& is used in place of A for
3179f4a2713aSLionel Sambuc     //   type deduction.
3180f4a2713aSLionel Sambuc     if (ParamRefType->isRValueReferenceType() &&
3181f4a2713aSLionel Sambuc         ParamRefType->getAs<TemplateTypeParmType>() &&
3182f4a2713aSLionel Sambuc         Arg->isLValue())
3183f4a2713aSLionel Sambuc       ArgType = S.Context.getLValueReferenceType(ArgType);
3184f4a2713aSLionel Sambuc   } else {
3185f4a2713aSLionel Sambuc     // C++ [temp.deduct.call]p2:
3186f4a2713aSLionel Sambuc     //   If P is not a reference type:
3187f4a2713aSLionel Sambuc     //   - If A is an array type, the pointer type produced by the
3188f4a2713aSLionel Sambuc     //     array-to-pointer standard conversion (4.2) is used in place of
3189f4a2713aSLionel Sambuc     //     A for type deduction; otherwise,
3190f4a2713aSLionel Sambuc     if (ArgType->isArrayType())
3191f4a2713aSLionel Sambuc       ArgType = S.Context.getArrayDecayedType(ArgType);
3192f4a2713aSLionel Sambuc     //   - If A is a function type, the pointer type produced by the
3193f4a2713aSLionel Sambuc     //     function-to-pointer standard conversion (4.3) is used in place
3194f4a2713aSLionel Sambuc     //     of A for type deduction; otherwise,
3195f4a2713aSLionel Sambuc     else if (ArgType->isFunctionType())
3196f4a2713aSLionel Sambuc       ArgType = S.Context.getPointerType(ArgType);
3197f4a2713aSLionel Sambuc     else {
3198f4a2713aSLionel Sambuc       // - If A is a cv-qualified type, the top level cv-qualifiers of A's
3199f4a2713aSLionel Sambuc       //   type are ignored for type deduction.
3200f4a2713aSLionel Sambuc       ArgType = ArgType.getUnqualifiedType();
3201f4a2713aSLionel Sambuc     }
3202f4a2713aSLionel Sambuc   }
3203f4a2713aSLionel Sambuc 
3204f4a2713aSLionel Sambuc   // C++0x [temp.deduct.call]p4:
3205f4a2713aSLionel Sambuc   //   In general, the deduction process attempts to find template argument
3206f4a2713aSLionel Sambuc   //   values that will make the deduced A identical to A (after the type A
3207f4a2713aSLionel Sambuc   //   is transformed as described above). [...]
3208f4a2713aSLionel Sambuc   TDF = TDF_SkipNonDependent;
3209f4a2713aSLionel Sambuc 
3210f4a2713aSLionel Sambuc   //     - If the original P is a reference type, the deduced A (i.e., the
3211f4a2713aSLionel Sambuc   //       type referred to by the reference) can be more cv-qualified than
3212f4a2713aSLionel Sambuc   //       the transformed A.
3213f4a2713aSLionel Sambuc   if (ParamRefType)
3214f4a2713aSLionel Sambuc     TDF |= TDF_ParamWithReferenceType;
3215f4a2713aSLionel Sambuc   //     - The transformed A can be another pointer or pointer to member
3216f4a2713aSLionel Sambuc   //       type that can be converted to the deduced A via a qualification
3217f4a2713aSLionel Sambuc   //       conversion (4.4).
3218f4a2713aSLionel Sambuc   if (ArgType->isPointerType() || ArgType->isMemberPointerType() ||
3219f4a2713aSLionel Sambuc       ArgType->isObjCObjectPointerType())
3220f4a2713aSLionel Sambuc     TDF |= TDF_IgnoreQualifiers;
3221f4a2713aSLionel Sambuc   //     - If P is a class and P has the form simple-template-id, then the
3222f4a2713aSLionel Sambuc   //       transformed A can be a derived class of the deduced A. Likewise,
3223f4a2713aSLionel Sambuc   //       if P is a pointer to a class of the form simple-template-id, the
3224f4a2713aSLionel Sambuc   //       transformed A can be a pointer to a derived class pointed to by
3225f4a2713aSLionel Sambuc   //       the deduced A.
3226f4a2713aSLionel Sambuc   if (isSimpleTemplateIdType(ParamType) ||
3227f4a2713aSLionel Sambuc       (isa<PointerType>(ParamType) &&
3228f4a2713aSLionel Sambuc        isSimpleTemplateIdType(
3229f4a2713aSLionel Sambuc                               ParamType->getAs<PointerType>()->getPointeeType())))
3230f4a2713aSLionel Sambuc     TDF |= TDF_DerivedClass;
3231f4a2713aSLionel Sambuc 
3232f4a2713aSLionel Sambuc   return false;
3233f4a2713aSLionel Sambuc }
3234f4a2713aSLionel Sambuc 
3235*0a6a1f1dSLionel Sambuc static bool
3236*0a6a1f1dSLionel Sambuc hasDeducibleTemplateParameters(Sema &S, FunctionTemplateDecl *FunctionTemplate,
3237f4a2713aSLionel Sambuc                                QualType T);
3238f4a2713aSLionel Sambuc 
3239f4a2713aSLionel Sambuc /// \brief Perform template argument deduction by matching a parameter type
3240f4a2713aSLionel Sambuc ///        against a single expression, where the expression is an element of
3241f4a2713aSLionel Sambuc ///        an initializer list that was originally matched against a parameter
3242f4a2713aSLionel Sambuc ///        of type \c initializer_list\<ParamType\>.
3243f4a2713aSLionel Sambuc static Sema::TemplateDeductionResult
DeduceTemplateArgumentByListElement(Sema & S,TemplateParameterList * TemplateParams,QualType ParamType,Expr * Arg,TemplateDeductionInfo & Info,SmallVectorImpl<DeducedTemplateArgument> & Deduced,unsigned TDF)3244f4a2713aSLionel Sambuc DeduceTemplateArgumentByListElement(Sema &S,
3245f4a2713aSLionel Sambuc                                     TemplateParameterList *TemplateParams,
3246f4a2713aSLionel Sambuc                                     QualType ParamType, Expr *Arg,
3247f4a2713aSLionel Sambuc                                     TemplateDeductionInfo &Info,
3248f4a2713aSLionel Sambuc                               SmallVectorImpl<DeducedTemplateArgument> &Deduced,
3249f4a2713aSLionel Sambuc                                     unsigned TDF) {
3250f4a2713aSLionel Sambuc   // Handle the case where an init list contains another init list as the
3251f4a2713aSLionel Sambuc   // element.
3252f4a2713aSLionel Sambuc   if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) {
3253f4a2713aSLionel Sambuc     QualType X;
3254f4a2713aSLionel Sambuc     if (!S.isStdInitializerList(ParamType.getNonReferenceType(), &X))
3255f4a2713aSLionel Sambuc       return Sema::TDK_Success; // Just ignore this expression.
3256f4a2713aSLionel Sambuc 
3257f4a2713aSLionel Sambuc     // Recurse down into the init list.
3258f4a2713aSLionel Sambuc     for (unsigned i = 0, e = ILE->getNumInits(); i < e; ++i) {
3259f4a2713aSLionel Sambuc       if (Sema::TemplateDeductionResult Result =
3260f4a2713aSLionel Sambuc             DeduceTemplateArgumentByListElement(S, TemplateParams, X,
3261f4a2713aSLionel Sambuc                                                  ILE->getInit(i),
3262f4a2713aSLionel Sambuc                                                  Info, Deduced, TDF))
3263f4a2713aSLionel Sambuc         return Result;
3264f4a2713aSLionel Sambuc     }
3265f4a2713aSLionel Sambuc     return Sema::TDK_Success;
3266f4a2713aSLionel Sambuc   }
3267f4a2713aSLionel Sambuc 
3268f4a2713aSLionel Sambuc   // For all other cases, just match by type.
3269f4a2713aSLionel Sambuc   QualType ArgType = Arg->getType();
3270f4a2713aSLionel Sambuc   if (AdjustFunctionParmAndArgTypesForDeduction(S, TemplateParams, ParamType,
3271f4a2713aSLionel Sambuc                                                 ArgType, Arg, TDF)) {
3272f4a2713aSLionel Sambuc     Info.Expression = Arg;
3273f4a2713aSLionel Sambuc     return Sema::TDK_FailedOverloadResolution;
3274f4a2713aSLionel Sambuc   }
3275f4a2713aSLionel Sambuc   return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType,
3276f4a2713aSLionel Sambuc                                             ArgType, Info, Deduced, TDF);
3277f4a2713aSLionel Sambuc }
3278f4a2713aSLionel Sambuc 
3279f4a2713aSLionel Sambuc /// \brief Perform template argument deduction from a function call
3280f4a2713aSLionel Sambuc /// (C++ [temp.deduct.call]).
3281f4a2713aSLionel Sambuc ///
3282f4a2713aSLionel Sambuc /// \param FunctionTemplate the function template for which we are performing
3283f4a2713aSLionel Sambuc /// template argument deduction.
3284f4a2713aSLionel Sambuc ///
3285f4a2713aSLionel Sambuc /// \param ExplicitTemplateArgs the explicit template arguments provided
3286f4a2713aSLionel Sambuc /// for this call.
3287f4a2713aSLionel Sambuc ///
3288f4a2713aSLionel Sambuc /// \param Args the function call arguments
3289f4a2713aSLionel Sambuc ///
3290f4a2713aSLionel Sambuc /// \param Specialization if template argument deduction was successful,
3291f4a2713aSLionel Sambuc /// this will be set to the function template specialization produced by
3292f4a2713aSLionel Sambuc /// template argument deduction.
3293f4a2713aSLionel Sambuc ///
3294f4a2713aSLionel Sambuc /// \param Info the argument will be updated to provide additional information
3295f4a2713aSLionel Sambuc /// about template argument deduction.
3296f4a2713aSLionel Sambuc ///
3297f4a2713aSLionel Sambuc /// \returns the result of template argument deduction.
DeduceTemplateArguments(FunctionTemplateDecl * FunctionTemplate,TemplateArgumentListInfo * ExplicitTemplateArgs,ArrayRef<Expr * > Args,FunctionDecl * & Specialization,TemplateDeductionInfo & Info)3298f4a2713aSLionel Sambuc Sema::TemplateDeductionResult Sema::DeduceTemplateArguments(
3299f4a2713aSLionel Sambuc     FunctionTemplateDecl *FunctionTemplate,
3300f4a2713aSLionel Sambuc     TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args,
3301f4a2713aSLionel Sambuc     FunctionDecl *&Specialization, TemplateDeductionInfo &Info) {
3302f4a2713aSLionel Sambuc   if (FunctionTemplate->isInvalidDecl())
3303f4a2713aSLionel Sambuc     return TDK_Invalid;
3304f4a2713aSLionel Sambuc 
3305f4a2713aSLionel Sambuc   FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
3306f4a2713aSLionel Sambuc 
3307f4a2713aSLionel Sambuc   // C++ [temp.deduct.call]p1:
3308f4a2713aSLionel Sambuc   //   Template argument deduction is done by comparing each function template
3309f4a2713aSLionel Sambuc   //   parameter type (call it P) with the type of the corresponding argument
3310f4a2713aSLionel Sambuc   //   of the call (call it A) as described below.
3311f4a2713aSLionel Sambuc   unsigned CheckArgs = Args.size();
3312f4a2713aSLionel Sambuc   if (Args.size() < Function->getMinRequiredArguments())
3313f4a2713aSLionel Sambuc     return TDK_TooFewArguments;
3314f4a2713aSLionel Sambuc   else if (Args.size() > Function->getNumParams()) {
3315f4a2713aSLionel Sambuc     const FunctionProtoType *Proto
3316f4a2713aSLionel Sambuc       = Function->getType()->getAs<FunctionProtoType>();
3317f4a2713aSLionel Sambuc     if (Proto->isTemplateVariadic())
3318f4a2713aSLionel Sambuc       /* Do nothing */;
3319f4a2713aSLionel Sambuc     else if (Proto->isVariadic())
3320f4a2713aSLionel Sambuc       CheckArgs = Function->getNumParams();
3321f4a2713aSLionel Sambuc     else
3322f4a2713aSLionel Sambuc       return TDK_TooManyArguments;
3323f4a2713aSLionel Sambuc   }
3324f4a2713aSLionel Sambuc 
3325f4a2713aSLionel Sambuc   // The types of the parameters from which we will perform template argument
3326f4a2713aSLionel Sambuc   // deduction.
3327f4a2713aSLionel Sambuc   LocalInstantiationScope InstScope(*this);
3328f4a2713aSLionel Sambuc   TemplateParameterList *TemplateParams
3329f4a2713aSLionel Sambuc     = FunctionTemplate->getTemplateParameters();
3330f4a2713aSLionel Sambuc   SmallVector<DeducedTemplateArgument, 4> Deduced;
3331f4a2713aSLionel Sambuc   SmallVector<QualType, 4> ParamTypes;
3332f4a2713aSLionel Sambuc   unsigned NumExplicitlySpecified = 0;
3333f4a2713aSLionel Sambuc   if (ExplicitTemplateArgs) {
3334f4a2713aSLionel Sambuc     TemplateDeductionResult Result =
3335f4a2713aSLionel Sambuc       SubstituteExplicitTemplateArguments(FunctionTemplate,
3336f4a2713aSLionel Sambuc                                           *ExplicitTemplateArgs,
3337f4a2713aSLionel Sambuc                                           Deduced,
3338f4a2713aSLionel Sambuc                                           ParamTypes,
3339*0a6a1f1dSLionel Sambuc                                           nullptr,
3340f4a2713aSLionel Sambuc                                           Info);
3341f4a2713aSLionel Sambuc     if (Result)
3342f4a2713aSLionel Sambuc       return Result;
3343f4a2713aSLionel Sambuc 
3344f4a2713aSLionel Sambuc     NumExplicitlySpecified = Deduced.size();
3345f4a2713aSLionel Sambuc   } else {
3346f4a2713aSLionel Sambuc     // Just fill in the parameter types from the function declaration.
3347f4a2713aSLionel Sambuc     for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
3348f4a2713aSLionel Sambuc       ParamTypes.push_back(Function->getParamDecl(I)->getType());
3349f4a2713aSLionel Sambuc   }
3350f4a2713aSLionel Sambuc 
3351f4a2713aSLionel Sambuc   // Deduce template arguments from the function parameters.
3352f4a2713aSLionel Sambuc   Deduced.resize(TemplateParams->size());
3353f4a2713aSLionel Sambuc   unsigned ArgIdx = 0;
3354f4a2713aSLionel Sambuc   SmallVector<OriginalCallArg, 4> OriginalCallArgs;
3355f4a2713aSLionel Sambuc   for (unsigned ParamIdx = 0, NumParams = ParamTypes.size();
3356f4a2713aSLionel Sambuc        ParamIdx != NumParams; ++ParamIdx) {
3357f4a2713aSLionel Sambuc     QualType OrigParamType = ParamTypes[ParamIdx];
3358f4a2713aSLionel Sambuc     QualType ParamType = OrigParamType;
3359f4a2713aSLionel Sambuc 
3360f4a2713aSLionel Sambuc     const PackExpansionType *ParamExpansion
3361f4a2713aSLionel Sambuc       = dyn_cast<PackExpansionType>(ParamType);
3362f4a2713aSLionel Sambuc     if (!ParamExpansion) {
3363f4a2713aSLionel Sambuc       // Simple case: matching a function parameter to a function argument.
3364f4a2713aSLionel Sambuc       if (ArgIdx >= CheckArgs)
3365f4a2713aSLionel Sambuc         break;
3366f4a2713aSLionel Sambuc 
3367f4a2713aSLionel Sambuc       Expr *Arg = Args[ArgIdx++];
3368f4a2713aSLionel Sambuc       QualType ArgType = Arg->getType();
3369f4a2713aSLionel Sambuc 
3370f4a2713aSLionel Sambuc       unsigned TDF = 0;
3371f4a2713aSLionel Sambuc       if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams,
3372f4a2713aSLionel Sambuc                                                     ParamType, ArgType, Arg,
3373f4a2713aSLionel Sambuc                                                     TDF))
3374f4a2713aSLionel Sambuc         continue;
3375f4a2713aSLionel Sambuc 
3376f4a2713aSLionel Sambuc       // If we have nothing to deduce, we're done.
3377f4a2713aSLionel Sambuc       if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType))
3378f4a2713aSLionel Sambuc         continue;
3379f4a2713aSLionel Sambuc 
3380f4a2713aSLionel Sambuc       // If the argument is an initializer list ...
3381f4a2713aSLionel Sambuc       if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) {
3382f4a2713aSLionel Sambuc         // ... then the parameter is an undeduced context, unless the parameter
3383f4a2713aSLionel Sambuc         // type is (reference to cv) std::initializer_list<P'>, in which case
3384f4a2713aSLionel Sambuc         // deduction is done for each element of the initializer list, and the
3385f4a2713aSLionel Sambuc         // result is the deduced type if it's the same for all elements.
3386f4a2713aSLionel Sambuc         QualType X;
3387f4a2713aSLionel Sambuc         // Removing references was already done.
3388f4a2713aSLionel Sambuc         if (!isStdInitializerList(ParamType, &X))
3389f4a2713aSLionel Sambuc           continue;
3390f4a2713aSLionel Sambuc 
3391f4a2713aSLionel Sambuc         for (unsigned i = 0, e = ILE->getNumInits(); i < e; ++i) {
3392f4a2713aSLionel Sambuc           if (TemplateDeductionResult Result =
3393f4a2713aSLionel Sambuc                 DeduceTemplateArgumentByListElement(*this, TemplateParams, X,
3394f4a2713aSLionel Sambuc                                                      ILE->getInit(i),
3395f4a2713aSLionel Sambuc                                                      Info, Deduced, TDF))
3396f4a2713aSLionel Sambuc             return Result;
3397f4a2713aSLionel Sambuc         }
3398f4a2713aSLionel Sambuc         // Don't track the argument type, since an initializer list has none.
3399f4a2713aSLionel Sambuc         continue;
3400f4a2713aSLionel Sambuc       }
3401f4a2713aSLionel Sambuc 
3402f4a2713aSLionel Sambuc       // Keep track of the argument type and corresponding parameter index,
3403f4a2713aSLionel Sambuc       // so we can check for compatibility between the deduced A and A.
3404f4a2713aSLionel Sambuc       OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx-1,
3405f4a2713aSLionel Sambuc                                                  ArgType));
3406f4a2713aSLionel Sambuc 
3407f4a2713aSLionel Sambuc       if (TemplateDeductionResult Result
3408f4a2713aSLionel Sambuc             = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams,
3409f4a2713aSLionel Sambuc                                                  ParamType, ArgType,
3410f4a2713aSLionel Sambuc                                                  Info, Deduced, TDF))
3411f4a2713aSLionel Sambuc         return Result;
3412f4a2713aSLionel Sambuc 
3413f4a2713aSLionel Sambuc       continue;
3414f4a2713aSLionel Sambuc     }
3415f4a2713aSLionel Sambuc 
3416f4a2713aSLionel Sambuc     // C++0x [temp.deduct.call]p1:
3417f4a2713aSLionel Sambuc     //   For a function parameter pack that occurs at the end of the
3418f4a2713aSLionel Sambuc     //   parameter-declaration-list, the type A of each remaining argument of
3419f4a2713aSLionel Sambuc     //   the call is compared with the type P of the declarator-id of the
3420f4a2713aSLionel Sambuc     //   function parameter pack. Each comparison deduces template arguments
3421f4a2713aSLionel Sambuc     //   for subsequent positions in the template parameter packs expanded by
3422f4a2713aSLionel Sambuc     //   the function parameter pack. For a function parameter pack that does
3423f4a2713aSLionel Sambuc     //   not occur at the end of the parameter-declaration-list, the type of
3424f4a2713aSLionel Sambuc     //   the parameter pack is a non-deduced context.
3425f4a2713aSLionel Sambuc     if (ParamIdx + 1 < NumParams)
3426f4a2713aSLionel Sambuc       break;
3427f4a2713aSLionel Sambuc 
3428f4a2713aSLionel Sambuc     QualType ParamPattern = ParamExpansion->getPattern();
3429*0a6a1f1dSLionel Sambuc     PackDeductionScope PackScope(*this, TemplateParams, Deduced, Info,
3430*0a6a1f1dSLionel Sambuc                                  ParamPattern);
3431f4a2713aSLionel Sambuc 
3432f4a2713aSLionel Sambuc     bool HasAnyArguments = false;
3433f4a2713aSLionel Sambuc     for (; ArgIdx < Args.size(); ++ArgIdx) {
3434f4a2713aSLionel Sambuc       HasAnyArguments = true;
3435f4a2713aSLionel Sambuc 
3436f4a2713aSLionel Sambuc       QualType OrigParamType = ParamPattern;
3437f4a2713aSLionel Sambuc       ParamType = OrigParamType;
3438f4a2713aSLionel Sambuc       Expr *Arg = Args[ArgIdx];
3439f4a2713aSLionel Sambuc       QualType ArgType = Arg->getType();
3440f4a2713aSLionel Sambuc 
3441f4a2713aSLionel Sambuc       unsigned TDF = 0;
3442f4a2713aSLionel Sambuc       if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams,
3443f4a2713aSLionel Sambuc                                                     ParamType, ArgType, Arg,
3444f4a2713aSLionel Sambuc                                                     TDF)) {
3445f4a2713aSLionel Sambuc         // We can't actually perform any deduction for this argument, so stop
3446f4a2713aSLionel Sambuc         // deduction at this point.
3447f4a2713aSLionel Sambuc         ++ArgIdx;
3448f4a2713aSLionel Sambuc         break;
3449f4a2713aSLionel Sambuc       }
3450f4a2713aSLionel Sambuc 
3451f4a2713aSLionel Sambuc       // As above, initializer lists need special handling.
3452f4a2713aSLionel Sambuc       if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) {
3453f4a2713aSLionel Sambuc         QualType X;
3454f4a2713aSLionel Sambuc         if (!isStdInitializerList(ParamType, &X)) {
3455f4a2713aSLionel Sambuc           ++ArgIdx;
3456f4a2713aSLionel Sambuc           break;
3457f4a2713aSLionel Sambuc         }
3458f4a2713aSLionel Sambuc 
3459f4a2713aSLionel Sambuc         for (unsigned i = 0, e = ILE->getNumInits(); i < e; ++i) {
3460f4a2713aSLionel Sambuc           if (TemplateDeductionResult Result =
3461f4a2713aSLionel Sambuc                 DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, X,
3462f4a2713aSLionel Sambuc                                                    ILE->getInit(i)->getType(),
3463f4a2713aSLionel Sambuc                                                    Info, Deduced, TDF))
3464f4a2713aSLionel Sambuc             return Result;
3465f4a2713aSLionel Sambuc         }
3466f4a2713aSLionel Sambuc       } else {
3467f4a2713aSLionel Sambuc 
3468f4a2713aSLionel Sambuc         // Keep track of the argument type and corresponding argument index,
3469f4a2713aSLionel Sambuc         // so we can check for compatibility between the deduced A and A.
3470f4a2713aSLionel Sambuc         if (hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType))
3471f4a2713aSLionel Sambuc           OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx,
3472f4a2713aSLionel Sambuc                                                      ArgType));
3473f4a2713aSLionel Sambuc 
3474f4a2713aSLionel Sambuc         if (TemplateDeductionResult Result
3475f4a2713aSLionel Sambuc             = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams,
3476f4a2713aSLionel Sambuc                                                  ParamType, ArgType, Info,
3477f4a2713aSLionel Sambuc                                                  Deduced, TDF))
3478f4a2713aSLionel Sambuc           return Result;
3479f4a2713aSLionel Sambuc       }
3480f4a2713aSLionel Sambuc 
3481*0a6a1f1dSLionel Sambuc       PackScope.nextPackElement();
3482f4a2713aSLionel Sambuc     }
3483f4a2713aSLionel Sambuc 
3484f4a2713aSLionel Sambuc     // Build argument packs for each of the parameter packs expanded by this
3485f4a2713aSLionel Sambuc     // pack expansion.
3486*0a6a1f1dSLionel Sambuc     if (auto Result = PackScope.finish(HasAnyArguments))
3487f4a2713aSLionel Sambuc       return Result;
3488f4a2713aSLionel Sambuc 
3489f4a2713aSLionel Sambuc     // After we've matching against a parameter pack, we're done.
3490f4a2713aSLionel Sambuc     break;
3491f4a2713aSLionel Sambuc   }
3492f4a2713aSLionel Sambuc 
3493f4a2713aSLionel Sambuc   return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
3494*0a6a1f1dSLionel Sambuc                                          NumExplicitlySpecified, Specialization,
3495*0a6a1f1dSLionel Sambuc                                          Info, &OriginalCallArgs);
3496*0a6a1f1dSLionel Sambuc }
3497*0a6a1f1dSLionel Sambuc 
adjustCCAndNoReturn(QualType ArgFunctionType,QualType FunctionType)3498*0a6a1f1dSLionel Sambuc QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType,
3499*0a6a1f1dSLionel Sambuc                                    QualType FunctionType) {
3500*0a6a1f1dSLionel Sambuc   if (ArgFunctionType.isNull())
3501*0a6a1f1dSLionel Sambuc     return ArgFunctionType;
3502*0a6a1f1dSLionel Sambuc 
3503*0a6a1f1dSLionel Sambuc   const FunctionProtoType *FunctionTypeP =
3504*0a6a1f1dSLionel Sambuc       FunctionType->castAs<FunctionProtoType>();
3505*0a6a1f1dSLionel Sambuc   CallingConv CC = FunctionTypeP->getCallConv();
3506*0a6a1f1dSLionel Sambuc   bool NoReturn = FunctionTypeP->getNoReturnAttr();
3507*0a6a1f1dSLionel Sambuc   const FunctionProtoType *ArgFunctionTypeP =
3508*0a6a1f1dSLionel Sambuc       ArgFunctionType->getAs<FunctionProtoType>();
3509*0a6a1f1dSLionel Sambuc   if (ArgFunctionTypeP->getCallConv() == CC &&
3510*0a6a1f1dSLionel Sambuc       ArgFunctionTypeP->getNoReturnAttr() == NoReturn)
3511*0a6a1f1dSLionel Sambuc     return ArgFunctionType;
3512*0a6a1f1dSLionel Sambuc 
3513*0a6a1f1dSLionel Sambuc   FunctionType::ExtInfo EI = ArgFunctionTypeP->getExtInfo().withCallingConv(CC);
3514*0a6a1f1dSLionel Sambuc   EI = EI.withNoReturn(NoReturn);
3515*0a6a1f1dSLionel Sambuc   ArgFunctionTypeP =
3516*0a6a1f1dSLionel Sambuc       cast<FunctionProtoType>(Context.adjustFunctionType(ArgFunctionTypeP, EI));
3517*0a6a1f1dSLionel Sambuc   return QualType(ArgFunctionTypeP, 0);
3518f4a2713aSLionel Sambuc }
3519f4a2713aSLionel Sambuc 
3520f4a2713aSLionel Sambuc /// \brief Deduce template arguments when taking the address of a function
3521f4a2713aSLionel Sambuc /// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
3522f4a2713aSLionel Sambuc /// a template.
3523f4a2713aSLionel Sambuc ///
3524f4a2713aSLionel Sambuc /// \param FunctionTemplate the function template for which we are performing
3525f4a2713aSLionel Sambuc /// template argument deduction.
3526f4a2713aSLionel Sambuc ///
3527f4a2713aSLionel Sambuc /// \param ExplicitTemplateArgs the explicitly-specified template
3528f4a2713aSLionel Sambuc /// arguments.
3529f4a2713aSLionel Sambuc ///
3530f4a2713aSLionel Sambuc /// \param ArgFunctionType the function type that will be used as the
3531f4a2713aSLionel Sambuc /// "argument" type (A) when performing template argument deduction from the
3532f4a2713aSLionel Sambuc /// function template's function type. This type may be NULL, if there is no
3533f4a2713aSLionel Sambuc /// argument type to compare against, in C++0x [temp.arg.explicit]p3.
3534f4a2713aSLionel Sambuc ///
3535f4a2713aSLionel Sambuc /// \param Specialization if template argument deduction was successful,
3536f4a2713aSLionel Sambuc /// this will be set to the function template specialization produced by
3537f4a2713aSLionel Sambuc /// template argument deduction.
3538f4a2713aSLionel Sambuc ///
3539f4a2713aSLionel Sambuc /// \param Info the argument will be updated to provide additional information
3540f4a2713aSLionel Sambuc /// about template argument deduction.
3541f4a2713aSLionel Sambuc ///
3542f4a2713aSLionel Sambuc /// \returns the result of template argument deduction.
3543f4a2713aSLionel Sambuc Sema::TemplateDeductionResult
DeduceTemplateArguments(FunctionTemplateDecl * FunctionTemplate,TemplateArgumentListInfo * ExplicitTemplateArgs,QualType ArgFunctionType,FunctionDecl * & Specialization,TemplateDeductionInfo & Info,bool InOverloadResolution)3544f4a2713aSLionel Sambuc Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
3545f4a2713aSLionel Sambuc                               TemplateArgumentListInfo *ExplicitTemplateArgs,
3546f4a2713aSLionel Sambuc                               QualType ArgFunctionType,
3547f4a2713aSLionel Sambuc                               FunctionDecl *&Specialization,
3548f4a2713aSLionel Sambuc                               TemplateDeductionInfo &Info,
3549f4a2713aSLionel Sambuc                               bool InOverloadResolution) {
3550f4a2713aSLionel Sambuc   if (FunctionTemplate->isInvalidDecl())
3551f4a2713aSLionel Sambuc     return TDK_Invalid;
3552f4a2713aSLionel Sambuc 
3553f4a2713aSLionel Sambuc   FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
3554f4a2713aSLionel Sambuc   TemplateParameterList *TemplateParams
3555f4a2713aSLionel Sambuc     = FunctionTemplate->getTemplateParameters();
3556f4a2713aSLionel Sambuc   QualType FunctionType = Function->getType();
3557*0a6a1f1dSLionel Sambuc   if (!InOverloadResolution)
3558*0a6a1f1dSLionel Sambuc     ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType);
3559f4a2713aSLionel Sambuc 
3560f4a2713aSLionel Sambuc   // Substitute any explicit template arguments.
3561f4a2713aSLionel Sambuc   LocalInstantiationScope InstScope(*this);
3562f4a2713aSLionel Sambuc   SmallVector<DeducedTemplateArgument, 4> Deduced;
3563f4a2713aSLionel Sambuc   unsigned NumExplicitlySpecified = 0;
3564f4a2713aSLionel Sambuc   SmallVector<QualType, 4> ParamTypes;
3565f4a2713aSLionel Sambuc   if (ExplicitTemplateArgs) {
3566f4a2713aSLionel Sambuc     if (TemplateDeductionResult Result
3567f4a2713aSLionel Sambuc           = SubstituteExplicitTemplateArguments(FunctionTemplate,
3568f4a2713aSLionel Sambuc                                                 *ExplicitTemplateArgs,
3569f4a2713aSLionel Sambuc                                                 Deduced, ParamTypes,
3570f4a2713aSLionel Sambuc                                                 &FunctionType, Info))
3571f4a2713aSLionel Sambuc       return Result;
3572f4a2713aSLionel Sambuc 
3573f4a2713aSLionel Sambuc     NumExplicitlySpecified = Deduced.size();
3574f4a2713aSLionel Sambuc   }
3575f4a2713aSLionel Sambuc 
3576f4a2713aSLionel Sambuc   // Unevaluated SFINAE context.
3577f4a2713aSLionel Sambuc   EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated);
3578f4a2713aSLionel Sambuc   SFINAETrap Trap(*this);
3579f4a2713aSLionel Sambuc 
3580f4a2713aSLionel Sambuc   Deduced.resize(TemplateParams->size());
3581f4a2713aSLionel Sambuc 
3582f4a2713aSLionel Sambuc   // If the function has a deduced return type, substitute it for a dependent
3583f4a2713aSLionel Sambuc   // type so that we treat it as a non-deduced context in what follows.
3584f4a2713aSLionel Sambuc   bool HasDeducedReturnType = false;
3585*0a6a1f1dSLionel Sambuc   if (getLangOpts().CPlusPlus14 && InOverloadResolution &&
3586*0a6a1f1dSLionel Sambuc       Function->getReturnType()->getContainedAutoType()) {
3587f4a2713aSLionel Sambuc     FunctionType = SubstAutoType(FunctionType, Context.DependentTy);
3588f4a2713aSLionel Sambuc     HasDeducedReturnType = true;
3589f4a2713aSLionel Sambuc   }
3590f4a2713aSLionel Sambuc 
3591f4a2713aSLionel Sambuc   if (!ArgFunctionType.isNull()) {
3592f4a2713aSLionel Sambuc     unsigned TDF = TDF_TopLevelParameterTypeList;
3593f4a2713aSLionel Sambuc     if (InOverloadResolution) TDF |= TDF_InOverloadResolution;
3594f4a2713aSLionel Sambuc     // Deduce template arguments from the function type.
3595f4a2713aSLionel Sambuc     if (TemplateDeductionResult Result
3596f4a2713aSLionel Sambuc           = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams,
3597f4a2713aSLionel Sambuc                                                FunctionType, ArgFunctionType,
3598f4a2713aSLionel Sambuc                                                Info, Deduced, TDF))
3599f4a2713aSLionel Sambuc       return Result;
3600f4a2713aSLionel Sambuc   }
3601f4a2713aSLionel Sambuc 
3602f4a2713aSLionel Sambuc   if (TemplateDeductionResult Result
3603f4a2713aSLionel Sambuc         = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
3604f4a2713aSLionel Sambuc                                           NumExplicitlySpecified,
3605f4a2713aSLionel Sambuc                                           Specialization, Info))
3606f4a2713aSLionel Sambuc     return Result;
3607f4a2713aSLionel Sambuc 
3608f4a2713aSLionel Sambuc   // If the function has a deduced return type, deduce it now, so we can check
3609f4a2713aSLionel Sambuc   // that the deduced function type matches the requested type.
3610f4a2713aSLionel Sambuc   if (HasDeducedReturnType &&
3611*0a6a1f1dSLionel Sambuc       Specialization->getReturnType()->isUndeducedType() &&
3612f4a2713aSLionel Sambuc       DeduceReturnType(Specialization, Info.getLocation(), false))
3613f4a2713aSLionel Sambuc     return TDK_MiscellaneousDeductionFailure;
3614f4a2713aSLionel Sambuc 
3615f4a2713aSLionel Sambuc   // If the requested function type does not match the actual type of the
3616f4a2713aSLionel Sambuc   // specialization with respect to arguments of compatible pointer to function
3617f4a2713aSLionel Sambuc   // types, template argument deduction fails.
3618f4a2713aSLionel Sambuc   if (!ArgFunctionType.isNull()) {
3619f4a2713aSLionel Sambuc     if (InOverloadResolution && !isSameOrCompatibleFunctionType(
3620f4a2713aSLionel Sambuc                            Context.getCanonicalType(Specialization->getType()),
3621f4a2713aSLionel Sambuc                            Context.getCanonicalType(ArgFunctionType)))
3622f4a2713aSLionel Sambuc       return TDK_MiscellaneousDeductionFailure;
3623f4a2713aSLionel Sambuc     else if(!InOverloadResolution &&
3624f4a2713aSLionel Sambuc             !Context.hasSameType(Specialization->getType(), ArgFunctionType))
3625f4a2713aSLionel Sambuc       return TDK_MiscellaneousDeductionFailure;
3626f4a2713aSLionel Sambuc   }
3627f4a2713aSLionel Sambuc 
3628f4a2713aSLionel Sambuc   return TDK_Success;
3629f4a2713aSLionel Sambuc }
3630f4a2713aSLionel Sambuc 
3631f4a2713aSLionel Sambuc /// \brief Given a function declaration (e.g. a generic lambda conversion
3632f4a2713aSLionel Sambuc ///  function) that contains an 'auto' in its result type, substitute it
3633f4a2713aSLionel Sambuc ///  with TypeToReplaceAutoWith.  Be careful to pass in the type you want
3634f4a2713aSLionel Sambuc ///  to replace 'auto' with and not the actual result type you want
3635f4a2713aSLionel Sambuc ///  to set the function to.
3636f4a2713aSLionel Sambuc static inline void
SubstAutoWithinFunctionReturnType(FunctionDecl * F,QualType TypeToReplaceAutoWith,Sema & S)3637f4a2713aSLionel Sambuc SubstAutoWithinFunctionReturnType(FunctionDecl *F,
3638f4a2713aSLionel Sambuc                                     QualType TypeToReplaceAutoWith, Sema &S) {
3639f4a2713aSLionel Sambuc   assert(!TypeToReplaceAutoWith->getContainedAutoType());
3640*0a6a1f1dSLionel Sambuc   QualType AutoResultType = F->getReturnType();
3641f4a2713aSLionel Sambuc   assert(AutoResultType->getContainedAutoType());
3642f4a2713aSLionel Sambuc   QualType DeducedResultType = S.SubstAutoType(AutoResultType,
3643f4a2713aSLionel Sambuc                                                TypeToReplaceAutoWith);
3644f4a2713aSLionel Sambuc   S.Context.adjustDeducedFunctionResultType(F, DeducedResultType);
3645f4a2713aSLionel Sambuc }
3646f4a2713aSLionel Sambuc 
3647f4a2713aSLionel Sambuc /// \brief Given a specialized conversion operator of a generic lambda
3648f4a2713aSLionel Sambuc /// create the corresponding specializations of the call operator and
3649f4a2713aSLionel Sambuc /// the static-invoker. If the return type of the call operator is auto,
3650f4a2713aSLionel Sambuc /// deduce its return type and check if that matches the
3651f4a2713aSLionel Sambuc /// return type of the destination function ptr.
3652f4a2713aSLionel Sambuc 
3653f4a2713aSLionel Sambuc static inline Sema::TemplateDeductionResult
SpecializeCorrespondingLambdaCallOperatorAndInvoker(CXXConversionDecl * ConversionSpecialized,SmallVectorImpl<DeducedTemplateArgument> & DeducedArguments,QualType ReturnTypeOfDestFunctionPtr,TemplateDeductionInfo & TDInfo,Sema & S)3654f4a2713aSLionel Sambuc SpecializeCorrespondingLambdaCallOperatorAndInvoker(
3655f4a2713aSLionel Sambuc     CXXConversionDecl *ConversionSpecialized,
3656f4a2713aSLionel Sambuc     SmallVectorImpl<DeducedTemplateArgument> &DeducedArguments,
3657f4a2713aSLionel Sambuc     QualType ReturnTypeOfDestFunctionPtr,
3658f4a2713aSLionel Sambuc     TemplateDeductionInfo &TDInfo,
3659f4a2713aSLionel Sambuc     Sema &S) {
3660f4a2713aSLionel Sambuc 
3661f4a2713aSLionel Sambuc   CXXRecordDecl *LambdaClass = ConversionSpecialized->getParent();
3662f4a2713aSLionel Sambuc   assert(LambdaClass && LambdaClass->isGenericLambda());
3663f4a2713aSLionel Sambuc 
3664f4a2713aSLionel Sambuc   CXXMethodDecl *CallOpGeneric = LambdaClass->getLambdaCallOperator();
3665*0a6a1f1dSLionel Sambuc   QualType CallOpResultType = CallOpGeneric->getReturnType();
3666f4a2713aSLionel Sambuc   const bool GenericLambdaCallOperatorHasDeducedReturnType =
3667f4a2713aSLionel Sambuc       CallOpResultType->getContainedAutoType();
3668f4a2713aSLionel Sambuc 
3669f4a2713aSLionel Sambuc   FunctionTemplateDecl *CallOpTemplate =
3670f4a2713aSLionel Sambuc       CallOpGeneric->getDescribedFunctionTemplate();
3671f4a2713aSLionel Sambuc 
3672*0a6a1f1dSLionel Sambuc   FunctionDecl *CallOpSpecialized = nullptr;
3673f4a2713aSLionel Sambuc   // Use the deduced arguments of the conversion function, to specialize our
3674f4a2713aSLionel Sambuc   // generic lambda's call operator.
3675f4a2713aSLionel Sambuc   if (Sema::TemplateDeductionResult Result
3676f4a2713aSLionel Sambuc       = S.FinishTemplateArgumentDeduction(CallOpTemplate,
3677f4a2713aSLionel Sambuc                                           DeducedArguments,
3678f4a2713aSLionel Sambuc                                           0, CallOpSpecialized, TDInfo))
3679f4a2713aSLionel Sambuc     return Result;
3680f4a2713aSLionel Sambuc 
3681f4a2713aSLionel Sambuc   // If we need to deduce the return type, do so (instantiates the callop).
3682f4a2713aSLionel Sambuc   if (GenericLambdaCallOperatorHasDeducedReturnType &&
3683*0a6a1f1dSLionel Sambuc       CallOpSpecialized->getReturnType()->isUndeducedType())
3684f4a2713aSLionel Sambuc     S.DeduceReturnType(CallOpSpecialized,
3685f4a2713aSLionel Sambuc                        CallOpSpecialized->getPointOfInstantiation(),
3686f4a2713aSLionel Sambuc                        /*Diagnose*/ true);
3687f4a2713aSLionel Sambuc 
3688f4a2713aSLionel Sambuc   // Check to see if the return type of the destination ptr-to-function
3689f4a2713aSLionel Sambuc   // matches the return type of the call operator.
3690*0a6a1f1dSLionel Sambuc   if (!S.Context.hasSameType(CallOpSpecialized->getReturnType(),
3691f4a2713aSLionel Sambuc                              ReturnTypeOfDestFunctionPtr))
3692f4a2713aSLionel Sambuc     return Sema::TDK_NonDeducedMismatch;
3693f4a2713aSLionel Sambuc   // Since we have succeeded in matching the source and destination
3694f4a2713aSLionel Sambuc   // ptr-to-functions (now including return type), and have successfully
3695f4a2713aSLionel Sambuc   // specialized our corresponding call operator, we are ready to
3696f4a2713aSLionel Sambuc   // specialize the static invoker with the deduced arguments of our
3697f4a2713aSLionel Sambuc   // ptr-to-function.
3698*0a6a1f1dSLionel Sambuc   FunctionDecl *InvokerSpecialized = nullptr;
3699f4a2713aSLionel Sambuc   FunctionTemplateDecl *InvokerTemplate = LambdaClass->
3700f4a2713aSLionel Sambuc                   getLambdaStaticInvoker()->getDescribedFunctionTemplate();
3701f4a2713aSLionel Sambuc 
3702f4a2713aSLionel Sambuc   Sema::TemplateDeductionResult LLVM_ATTRIBUTE_UNUSED Result
3703f4a2713aSLionel Sambuc     = S.FinishTemplateArgumentDeduction(InvokerTemplate, DeducedArguments, 0,
3704f4a2713aSLionel Sambuc           InvokerSpecialized, TDInfo);
3705f4a2713aSLionel Sambuc   assert(Result == Sema::TDK_Success &&
3706f4a2713aSLionel Sambuc     "If the call operator succeeded so should the invoker!");
3707f4a2713aSLionel Sambuc   // Set the result type to match the corresponding call operator
3708f4a2713aSLionel Sambuc   // specialization's result type.
3709f4a2713aSLionel Sambuc   if (GenericLambdaCallOperatorHasDeducedReturnType &&
3710*0a6a1f1dSLionel Sambuc       InvokerSpecialized->getReturnType()->isUndeducedType()) {
3711f4a2713aSLionel Sambuc     // Be sure to get the type to replace 'auto' with and not
3712f4a2713aSLionel Sambuc     // the full result type of the call op specialization
3713f4a2713aSLionel Sambuc     // to substitute into the 'auto' of the invoker and conversion
3714f4a2713aSLionel Sambuc     // function.
3715f4a2713aSLionel Sambuc     // For e.g.
3716f4a2713aSLionel Sambuc     //  int* (*fp)(int*) = [](auto* a) -> auto* { return a; };
3717f4a2713aSLionel Sambuc     // We don't want to subst 'int*' into 'auto' to get int**.
3718f4a2713aSLionel Sambuc 
3719*0a6a1f1dSLionel Sambuc     QualType TypeToReplaceAutoWith = CallOpSpecialized->getReturnType()
3720*0a6a1f1dSLionel Sambuc                                          ->getContainedAutoType()
3721*0a6a1f1dSLionel Sambuc                                          ->getDeducedType();
3722f4a2713aSLionel Sambuc     SubstAutoWithinFunctionReturnType(InvokerSpecialized,
3723f4a2713aSLionel Sambuc         TypeToReplaceAutoWith, S);
3724f4a2713aSLionel Sambuc     SubstAutoWithinFunctionReturnType(ConversionSpecialized,
3725f4a2713aSLionel Sambuc         TypeToReplaceAutoWith, S);
3726f4a2713aSLionel Sambuc   }
3727f4a2713aSLionel Sambuc 
3728f4a2713aSLionel Sambuc   // Ensure that static invoker doesn't have a const qualifier.
3729f4a2713aSLionel Sambuc   // FIXME: When creating the InvokerTemplate in SemaLambda.cpp
3730f4a2713aSLionel Sambuc   // do not use the CallOperator's TypeSourceInfo which allows
3731f4a2713aSLionel Sambuc   // the const qualifier to leak through.
3732f4a2713aSLionel Sambuc   const FunctionProtoType *InvokerFPT = InvokerSpecialized->
3733f4a2713aSLionel Sambuc                   getType().getTypePtr()->castAs<FunctionProtoType>();
3734f4a2713aSLionel Sambuc   FunctionProtoType::ExtProtoInfo EPI = InvokerFPT->getExtProtoInfo();
3735f4a2713aSLionel Sambuc   EPI.TypeQuals = 0;
3736f4a2713aSLionel Sambuc   InvokerSpecialized->setType(S.Context.getFunctionType(
3737*0a6a1f1dSLionel Sambuc       InvokerFPT->getReturnType(), InvokerFPT->getParamTypes(), EPI));
3738f4a2713aSLionel Sambuc   return Sema::TDK_Success;
3739f4a2713aSLionel Sambuc }
3740f4a2713aSLionel Sambuc /// \brief Deduce template arguments for a templated conversion
3741f4a2713aSLionel Sambuc /// function (C++ [temp.deduct.conv]) and, if successful, produce a
3742f4a2713aSLionel Sambuc /// conversion function template specialization.
3743f4a2713aSLionel Sambuc Sema::TemplateDeductionResult
DeduceTemplateArguments(FunctionTemplateDecl * ConversionTemplate,QualType ToType,CXXConversionDecl * & Specialization,TemplateDeductionInfo & Info)3744f4a2713aSLionel Sambuc Sema::DeduceTemplateArguments(FunctionTemplateDecl *ConversionTemplate,
3745f4a2713aSLionel Sambuc                               QualType ToType,
3746f4a2713aSLionel Sambuc                               CXXConversionDecl *&Specialization,
3747f4a2713aSLionel Sambuc                               TemplateDeductionInfo &Info) {
3748f4a2713aSLionel Sambuc   if (ConversionTemplate->isInvalidDecl())
3749f4a2713aSLionel Sambuc     return TDK_Invalid;
3750f4a2713aSLionel Sambuc 
3751f4a2713aSLionel Sambuc   CXXConversionDecl *ConversionGeneric
3752f4a2713aSLionel Sambuc     = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl());
3753f4a2713aSLionel Sambuc 
3754f4a2713aSLionel Sambuc   QualType FromType = ConversionGeneric->getConversionType();
3755f4a2713aSLionel Sambuc 
3756f4a2713aSLionel Sambuc   // Canonicalize the types for deduction.
3757f4a2713aSLionel Sambuc   QualType P = Context.getCanonicalType(FromType);
3758f4a2713aSLionel Sambuc   QualType A = Context.getCanonicalType(ToType);
3759f4a2713aSLionel Sambuc 
3760f4a2713aSLionel Sambuc   // C++0x [temp.deduct.conv]p2:
3761f4a2713aSLionel Sambuc   //   If P is a reference type, the type referred to by P is used for
3762f4a2713aSLionel Sambuc   //   type deduction.
3763f4a2713aSLionel Sambuc   if (const ReferenceType *PRef = P->getAs<ReferenceType>())
3764f4a2713aSLionel Sambuc     P = PRef->getPointeeType();
3765f4a2713aSLionel Sambuc 
3766f4a2713aSLionel Sambuc   // C++0x [temp.deduct.conv]p4:
3767f4a2713aSLionel Sambuc   //   [...] If A is a reference type, the type referred to by A is used
3768f4a2713aSLionel Sambuc   //   for type deduction.
3769f4a2713aSLionel Sambuc   if (const ReferenceType *ARef = A->getAs<ReferenceType>())
3770f4a2713aSLionel Sambuc     A = ARef->getPointeeType().getUnqualifiedType();
3771f4a2713aSLionel Sambuc   // C++ [temp.deduct.conv]p3:
3772f4a2713aSLionel Sambuc   //
3773f4a2713aSLionel Sambuc   //   If A is not a reference type:
3774f4a2713aSLionel Sambuc   else {
3775f4a2713aSLionel Sambuc     assert(!A->isReferenceType() && "Reference types were handled above");
3776f4a2713aSLionel Sambuc 
3777f4a2713aSLionel Sambuc     //   - If P is an array type, the pointer type produced by the
3778f4a2713aSLionel Sambuc     //     array-to-pointer standard conversion (4.2) is used in place
3779f4a2713aSLionel Sambuc     //     of P for type deduction; otherwise,
3780f4a2713aSLionel Sambuc     if (P->isArrayType())
3781f4a2713aSLionel Sambuc       P = Context.getArrayDecayedType(P);
3782f4a2713aSLionel Sambuc     //   - If P is a function type, the pointer type produced by the
3783f4a2713aSLionel Sambuc     //     function-to-pointer standard conversion (4.3) is used in
3784f4a2713aSLionel Sambuc     //     place of P for type deduction; otherwise,
3785f4a2713aSLionel Sambuc     else if (P->isFunctionType())
3786f4a2713aSLionel Sambuc       P = Context.getPointerType(P);
3787f4a2713aSLionel Sambuc     //   - If P is a cv-qualified type, the top level cv-qualifiers of
3788f4a2713aSLionel Sambuc     //     P's type are ignored for type deduction.
3789f4a2713aSLionel Sambuc     else
3790f4a2713aSLionel Sambuc       P = P.getUnqualifiedType();
3791f4a2713aSLionel Sambuc 
3792f4a2713aSLionel Sambuc     // C++0x [temp.deduct.conv]p4:
3793f4a2713aSLionel Sambuc     //   If A is a cv-qualified type, the top level cv-qualifiers of A's
3794f4a2713aSLionel Sambuc     //   type are ignored for type deduction. If A is a reference type, the type
3795f4a2713aSLionel Sambuc     //   referred to by A is used for type deduction.
3796f4a2713aSLionel Sambuc     A = A.getUnqualifiedType();
3797f4a2713aSLionel Sambuc   }
3798f4a2713aSLionel Sambuc 
3799f4a2713aSLionel Sambuc   // Unevaluated SFINAE context.
3800f4a2713aSLionel Sambuc   EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated);
3801f4a2713aSLionel Sambuc   SFINAETrap Trap(*this);
3802f4a2713aSLionel Sambuc 
3803f4a2713aSLionel Sambuc   // C++ [temp.deduct.conv]p1:
3804f4a2713aSLionel Sambuc   //   Template argument deduction is done by comparing the return
3805f4a2713aSLionel Sambuc   //   type of the template conversion function (call it P) with the
3806f4a2713aSLionel Sambuc   //   type that is required as the result of the conversion (call it
3807f4a2713aSLionel Sambuc   //   A) as described in 14.8.2.4.
3808f4a2713aSLionel Sambuc   TemplateParameterList *TemplateParams
3809f4a2713aSLionel Sambuc     = ConversionTemplate->getTemplateParameters();
3810f4a2713aSLionel Sambuc   SmallVector<DeducedTemplateArgument, 4> Deduced;
3811f4a2713aSLionel Sambuc   Deduced.resize(TemplateParams->size());
3812f4a2713aSLionel Sambuc 
3813f4a2713aSLionel Sambuc   // C++0x [temp.deduct.conv]p4:
3814f4a2713aSLionel Sambuc   //   In general, the deduction process attempts to find template
3815f4a2713aSLionel Sambuc   //   argument values that will make the deduced A identical to
3816f4a2713aSLionel Sambuc   //   A. However, there are two cases that allow a difference:
3817f4a2713aSLionel Sambuc   unsigned TDF = 0;
3818f4a2713aSLionel Sambuc   //     - If the original A is a reference type, A can be more
3819f4a2713aSLionel Sambuc   //       cv-qualified than the deduced A (i.e., the type referred to
3820f4a2713aSLionel Sambuc   //       by the reference)
3821f4a2713aSLionel Sambuc   if (ToType->isReferenceType())
3822f4a2713aSLionel Sambuc     TDF |= TDF_ParamWithReferenceType;
3823f4a2713aSLionel Sambuc   //     - The deduced A can be another pointer or pointer to member
3824f4a2713aSLionel Sambuc   //       type that can be converted to A via a qualification
3825f4a2713aSLionel Sambuc   //       conversion.
3826f4a2713aSLionel Sambuc   //
3827f4a2713aSLionel Sambuc   // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
3828f4a2713aSLionel Sambuc   // both P and A are pointers or member pointers. In this case, we
3829f4a2713aSLionel Sambuc   // just ignore cv-qualifiers completely).
3830f4a2713aSLionel Sambuc   if ((P->isPointerType() && A->isPointerType()) ||
3831f4a2713aSLionel Sambuc       (P->isMemberPointerType() && A->isMemberPointerType()))
3832f4a2713aSLionel Sambuc     TDF |= TDF_IgnoreQualifiers;
3833f4a2713aSLionel Sambuc   if (TemplateDeductionResult Result
3834f4a2713aSLionel Sambuc         = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams,
3835f4a2713aSLionel Sambuc                                              P, A, Info, Deduced, TDF))
3836f4a2713aSLionel Sambuc     return Result;
3837f4a2713aSLionel Sambuc 
3838f4a2713aSLionel Sambuc   // Create an Instantiation Scope for finalizing the operator.
3839f4a2713aSLionel Sambuc   LocalInstantiationScope InstScope(*this);
3840f4a2713aSLionel Sambuc   // Finish template argument deduction.
3841*0a6a1f1dSLionel Sambuc   FunctionDecl *ConversionSpecialized = nullptr;
3842f4a2713aSLionel Sambuc   TemplateDeductionResult Result
3843f4a2713aSLionel Sambuc       = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0,
3844f4a2713aSLionel Sambuc                                         ConversionSpecialized, Info);
3845f4a2713aSLionel Sambuc   Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized);
3846f4a2713aSLionel Sambuc 
3847f4a2713aSLionel Sambuc   // If the conversion operator is being invoked on a lambda closure to convert
3848*0a6a1f1dSLionel Sambuc   // to a ptr-to-function, use the deduced arguments from the conversion
3849*0a6a1f1dSLionel Sambuc   // function to specialize the corresponding call operator.
3850f4a2713aSLionel Sambuc   //   e.g., int (*fp)(int) = [](auto a) { return a; };
3851f4a2713aSLionel Sambuc   if (Result == TDK_Success && isLambdaConversionOperator(ConversionGeneric)) {
3852f4a2713aSLionel Sambuc 
3853f4a2713aSLionel Sambuc     // Get the return type of the destination ptr-to-function we are converting
3854f4a2713aSLionel Sambuc     // to.  This is necessary for matching the lambda call operator's return
3855f4a2713aSLionel Sambuc     // type to that of the destination ptr-to-function's return type.
3856f4a2713aSLionel Sambuc     assert(A->isPointerType() &&
3857f4a2713aSLionel Sambuc         "Can only convert from lambda to ptr-to-function");
3858f4a2713aSLionel Sambuc     const FunctionType *ToFunType =
3859f4a2713aSLionel Sambuc         A->getPointeeType().getTypePtr()->getAs<FunctionType>();
3860*0a6a1f1dSLionel Sambuc     const QualType DestFunctionPtrReturnType = ToFunType->getReturnType();
3861f4a2713aSLionel Sambuc 
3862f4a2713aSLionel Sambuc     // Create the corresponding specializations of the call operator and
3863f4a2713aSLionel Sambuc     // the static-invoker; and if the return type is auto,
3864f4a2713aSLionel Sambuc     // deduce the return type and check if it matches the
3865f4a2713aSLionel Sambuc     // DestFunctionPtrReturnType.
3866f4a2713aSLionel Sambuc     // For instance:
3867f4a2713aSLionel Sambuc     //   auto L = [](auto a) { return f(a); };
3868f4a2713aSLionel Sambuc     //   int (*fp)(int) = L;
3869f4a2713aSLionel Sambuc     //   char (*fp2)(int) = L; <-- Not OK.
3870f4a2713aSLionel Sambuc 
3871f4a2713aSLionel Sambuc     Result = SpecializeCorrespondingLambdaCallOperatorAndInvoker(
3872f4a2713aSLionel Sambuc         Specialization, Deduced, DestFunctionPtrReturnType,
3873f4a2713aSLionel Sambuc         Info, *this);
3874f4a2713aSLionel Sambuc   }
3875f4a2713aSLionel Sambuc   return Result;
3876f4a2713aSLionel Sambuc }
3877f4a2713aSLionel Sambuc 
3878f4a2713aSLionel Sambuc /// \brief Deduce template arguments for a function template when there is
3879f4a2713aSLionel Sambuc /// nothing to deduce against (C++0x [temp.arg.explicit]p3).
3880f4a2713aSLionel Sambuc ///
3881f4a2713aSLionel Sambuc /// \param FunctionTemplate the function template for which we are performing
3882f4a2713aSLionel Sambuc /// template argument deduction.
3883f4a2713aSLionel Sambuc ///
3884f4a2713aSLionel Sambuc /// \param ExplicitTemplateArgs the explicitly-specified template
3885f4a2713aSLionel Sambuc /// arguments.
3886f4a2713aSLionel Sambuc ///
3887f4a2713aSLionel Sambuc /// \param Specialization if template argument deduction was successful,
3888f4a2713aSLionel Sambuc /// this will be set to the function template specialization produced by
3889f4a2713aSLionel Sambuc /// template argument deduction.
3890f4a2713aSLionel Sambuc ///
3891f4a2713aSLionel Sambuc /// \param Info the argument will be updated to provide additional information
3892f4a2713aSLionel Sambuc /// about template argument deduction.
3893f4a2713aSLionel Sambuc ///
3894f4a2713aSLionel Sambuc /// \returns the result of template argument deduction.
3895f4a2713aSLionel Sambuc Sema::TemplateDeductionResult
DeduceTemplateArguments(FunctionTemplateDecl * FunctionTemplate,TemplateArgumentListInfo * ExplicitTemplateArgs,FunctionDecl * & Specialization,TemplateDeductionInfo & Info,bool InOverloadResolution)3896f4a2713aSLionel Sambuc Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
3897f4a2713aSLionel Sambuc                               TemplateArgumentListInfo *ExplicitTemplateArgs,
3898f4a2713aSLionel Sambuc                               FunctionDecl *&Specialization,
3899f4a2713aSLionel Sambuc                               TemplateDeductionInfo &Info,
3900f4a2713aSLionel Sambuc                               bool InOverloadResolution) {
3901f4a2713aSLionel Sambuc   return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
3902f4a2713aSLionel Sambuc                                  QualType(), Specialization, Info,
3903f4a2713aSLionel Sambuc                                  InOverloadResolution);
3904f4a2713aSLionel Sambuc }
3905f4a2713aSLionel Sambuc 
3906f4a2713aSLionel Sambuc namespace {
3907f4a2713aSLionel Sambuc   /// Substitute the 'auto' type specifier within a type for a given replacement
3908f4a2713aSLionel Sambuc   /// type.
3909f4a2713aSLionel Sambuc   class SubstituteAutoTransform :
3910f4a2713aSLionel Sambuc     public TreeTransform<SubstituteAutoTransform> {
3911f4a2713aSLionel Sambuc     QualType Replacement;
3912f4a2713aSLionel Sambuc   public:
SubstituteAutoTransform(Sema & SemaRef,QualType Replacement)3913*0a6a1f1dSLionel Sambuc     SubstituteAutoTransform(Sema &SemaRef, QualType Replacement)
3914*0a6a1f1dSLionel Sambuc         : TreeTransform<SubstituteAutoTransform>(SemaRef),
3915*0a6a1f1dSLionel Sambuc           Replacement(Replacement) {}
3916*0a6a1f1dSLionel Sambuc 
TransformAutoType(TypeLocBuilder & TLB,AutoTypeLoc TL)3917f4a2713aSLionel Sambuc     QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) {
3918f4a2713aSLionel Sambuc       // If we're building the type pattern to deduce against, don't wrap the
3919f4a2713aSLionel Sambuc       // substituted type in an AutoType. Certain template deduction rules
3920f4a2713aSLionel Sambuc       // apply only when a template type parameter appears directly (and not if
3921f4a2713aSLionel Sambuc       // the parameter is found through desugaring). For instance:
3922f4a2713aSLionel Sambuc       //   auto &&lref = lvalue;
3923f4a2713aSLionel Sambuc       // must transform into "rvalue reference to T" not "rvalue reference to
3924f4a2713aSLionel Sambuc       // auto type deduced as T" in order for [temp.deduct.call]p3 to apply.
3925f4a2713aSLionel Sambuc       if (!Replacement.isNull() && isa<TemplateTypeParmType>(Replacement)) {
3926f4a2713aSLionel Sambuc         QualType Result = Replacement;
3927f4a2713aSLionel Sambuc         TemplateTypeParmTypeLoc NewTL =
3928f4a2713aSLionel Sambuc           TLB.push<TemplateTypeParmTypeLoc>(Result);
3929f4a2713aSLionel Sambuc         NewTL.setNameLoc(TL.getNameLoc());
3930f4a2713aSLionel Sambuc         return Result;
3931f4a2713aSLionel Sambuc       } else {
3932f4a2713aSLionel Sambuc         bool Dependent =
3933f4a2713aSLionel Sambuc           !Replacement.isNull() && Replacement->isDependentType();
3934f4a2713aSLionel Sambuc         QualType Result =
3935f4a2713aSLionel Sambuc           SemaRef.Context.getAutoType(Dependent ? QualType() : Replacement,
3936f4a2713aSLionel Sambuc                                       TL.getTypePtr()->isDecltypeAuto(),
3937f4a2713aSLionel Sambuc                                       Dependent);
3938f4a2713aSLionel Sambuc         AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result);
3939f4a2713aSLionel Sambuc         NewTL.setNameLoc(TL.getNameLoc());
3940f4a2713aSLionel Sambuc         return Result;
3941f4a2713aSLionel Sambuc       }
3942f4a2713aSLionel Sambuc     }
3943f4a2713aSLionel Sambuc 
TransformLambdaExpr(LambdaExpr * E)3944f4a2713aSLionel Sambuc     ExprResult TransformLambdaExpr(LambdaExpr *E) {
3945f4a2713aSLionel Sambuc       // Lambdas never need to be transformed.
3946f4a2713aSLionel Sambuc       return E;
3947f4a2713aSLionel Sambuc     }
3948f4a2713aSLionel Sambuc 
Apply(TypeLoc TL)3949f4a2713aSLionel Sambuc     QualType Apply(TypeLoc TL) {
3950f4a2713aSLionel Sambuc       // Create some scratch storage for the transformed type locations.
3951f4a2713aSLionel Sambuc       // FIXME: We're just going to throw this information away. Don't build it.
3952f4a2713aSLionel Sambuc       TypeLocBuilder TLB;
3953f4a2713aSLionel Sambuc       TLB.reserve(TL.getFullDataSize());
3954f4a2713aSLionel Sambuc       return TransformType(TLB, TL);
3955f4a2713aSLionel Sambuc     }
3956f4a2713aSLionel Sambuc   };
3957f4a2713aSLionel Sambuc }
3958f4a2713aSLionel Sambuc 
3959f4a2713aSLionel Sambuc Sema::DeduceAutoResult
DeduceAutoType(TypeSourceInfo * Type,Expr * & Init,QualType & Result)3960f4a2713aSLionel Sambuc Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *&Init, QualType &Result) {
3961f4a2713aSLionel Sambuc   return DeduceAutoType(Type->getTypeLoc(), Init, Result);
3962f4a2713aSLionel Sambuc }
3963f4a2713aSLionel Sambuc 
3964f4a2713aSLionel Sambuc /// \brief Deduce the type for an auto type-specifier (C++11 [dcl.spec.auto]p6)
3965f4a2713aSLionel Sambuc ///
3966f4a2713aSLionel Sambuc /// \param Type the type pattern using the auto type-specifier.
3967f4a2713aSLionel Sambuc /// \param Init the initializer for the variable whose type is to be deduced.
3968f4a2713aSLionel Sambuc /// \param Result if type deduction was successful, this will be set to the
3969f4a2713aSLionel Sambuc ///        deduced type.
3970f4a2713aSLionel Sambuc Sema::DeduceAutoResult
DeduceAutoType(TypeLoc Type,Expr * & Init,QualType & Result)3971f4a2713aSLionel Sambuc Sema::DeduceAutoType(TypeLoc Type, Expr *&Init, QualType &Result) {
3972f4a2713aSLionel Sambuc   if (Init->getType()->isNonOverloadPlaceholderType()) {
3973f4a2713aSLionel Sambuc     ExprResult NonPlaceholder = CheckPlaceholderExpr(Init);
3974f4a2713aSLionel Sambuc     if (NonPlaceholder.isInvalid())
3975f4a2713aSLionel Sambuc       return DAR_FailedAlreadyDiagnosed;
3976*0a6a1f1dSLionel Sambuc     Init = NonPlaceholder.get();
3977f4a2713aSLionel Sambuc   }
3978f4a2713aSLionel Sambuc 
3979f4a2713aSLionel Sambuc   if (Init->isTypeDependent() || Type.getType()->isDependentType()) {
3980f4a2713aSLionel Sambuc     Result = SubstituteAutoTransform(*this, Context.DependentTy).Apply(Type);
3981f4a2713aSLionel Sambuc     assert(!Result.isNull() && "substituting DependentTy can't fail");
3982f4a2713aSLionel Sambuc     return DAR_Succeeded;
3983f4a2713aSLionel Sambuc   }
3984f4a2713aSLionel Sambuc 
3985f4a2713aSLionel Sambuc   // If this is a 'decltype(auto)' specifier, do the decltype dance.
3986f4a2713aSLionel Sambuc   // Since 'decltype(auto)' can only occur at the top of the type, we
3987f4a2713aSLionel Sambuc   // don't need to go digging for it.
3988f4a2713aSLionel Sambuc   if (const AutoType *AT = Type.getType()->getAs<AutoType>()) {
3989f4a2713aSLionel Sambuc     if (AT->isDecltypeAuto()) {
3990f4a2713aSLionel Sambuc       if (isa<InitListExpr>(Init)) {
3991f4a2713aSLionel Sambuc         Diag(Init->getLocStart(), diag::err_decltype_auto_initializer_list);
3992f4a2713aSLionel Sambuc         return DAR_FailedAlreadyDiagnosed;
3993f4a2713aSLionel Sambuc       }
3994f4a2713aSLionel Sambuc 
3995*0a6a1f1dSLionel Sambuc       QualType Deduced = BuildDecltypeType(Init, Init->getLocStart(), false);
3996f4a2713aSLionel Sambuc       // FIXME: Support a non-canonical deduced type for 'auto'.
3997f4a2713aSLionel Sambuc       Deduced = Context.getCanonicalType(Deduced);
3998f4a2713aSLionel Sambuc       Result = SubstituteAutoTransform(*this, Deduced).Apply(Type);
3999f4a2713aSLionel Sambuc       if (Result.isNull())
4000f4a2713aSLionel Sambuc         return DAR_FailedAlreadyDiagnosed;
4001f4a2713aSLionel Sambuc       return DAR_Succeeded;
4002f4a2713aSLionel Sambuc     }
4003f4a2713aSLionel Sambuc   }
4004f4a2713aSLionel Sambuc 
4005f4a2713aSLionel Sambuc   SourceLocation Loc = Init->getExprLoc();
4006f4a2713aSLionel Sambuc 
4007f4a2713aSLionel Sambuc   LocalInstantiationScope InstScope(*this);
4008f4a2713aSLionel Sambuc 
4009f4a2713aSLionel Sambuc   // Build template<class TemplParam> void Func(FuncParam);
4010f4a2713aSLionel Sambuc   TemplateTypeParmDecl *TemplParam =
4011*0a6a1f1dSLionel Sambuc     TemplateTypeParmDecl::Create(Context, nullptr, SourceLocation(), Loc, 0, 0,
4012*0a6a1f1dSLionel Sambuc                                  nullptr, false, false);
4013f4a2713aSLionel Sambuc   QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0);
4014f4a2713aSLionel Sambuc   NamedDecl *TemplParamPtr = TemplParam;
4015f4a2713aSLionel Sambuc   FixedSizeTemplateParameterList<1> TemplateParams(Loc, Loc, &TemplParamPtr,
4016f4a2713aSLionel Sambuc                                                    Loc);
4017f4a2713aSLionel Sambuc 
4018f4a2713aSLionel Sambuc   QualType FuncParam = SubstituteAutoTransform(*this, TemplArg).Apply(Type);
4019f4a2713aSLionel Sambuc   assert(!FuncParam.isNull() &&
4020f4a2713aSLionel Sambuc          "substituting template parameter for 'auto' failed");
4021f4a2713aSLionel Sambuc 
4022f4a2713aSLionel Sambuc   // Deduce type of TemplParam in Func(Init)
4023f4a2713aSLionel Sambuc   SmallVector<DeducedTemplateArgument, 1> Deduced;
4024f4a2713aSLionel Sambuc   Deduced.resize(1);
4025f4a2713aSLionel Sambuc   QualType InitType = Init->getType();
4026f4a2713aSLionel Sambuc   unsigned TDF = 0;
4027f4a2713aSLionel Sambuc 
4028f4a2713aSLionel Sambuc   TemplateDeductionInfo Info(Loc);
4029f4a2713aSLionel Sambuc 
4030f4a2713aSLionel Sambuc   InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
4031f4a2713aSLionel Sambuc   if (InitList) {
4032f4a2713aSLionel Sambuc     for (unsigned i = 0, e = InitList->getNumInits(); i < e; ++i) {
4033f4a2713aSLionel Sambuc       if (DeduceTemplateArgumentByListElement(*this, &TemplateParams,
4034f4a2713aSLionel Sambuc                                               TemplArg,
4035f4a2713aSLionel Sambuc                                               InitList->getInit(i),
4036f4a2713aSLionel Sambuc                                               Info, Deduced, TDF))
4037f4a2713aSLionel Sambuc         return DAR_Failed;
4038f4a2713aSLionel Sambuc     }
4039f4a2713aSLionel Sambuc   } else {
4040f4a2713aSLionel Sambuc     if (AdjustFunctionParmAndArgTypesForDeduction(*this, &TemplateParams,
4041f4a2713aSLionel Sambuc                                                   FuncParam, InitType, Init,
4042f4a2713aSLionel Sambuc                                                   TDF))
4043f4a2713aSLionel Sambuc       return DAR_Failed;
4044f4a2713aSLionel Sambuc 
4045f4a2713aSLionel Sambuc     if (DeduceTemplateArgumentsByTypeMatch(*this, &TemplateParams, FuncParam,
4046f4a2713aSLionel Sambuc                                            InitType, Info, Deduced, TDF))
4047f4a2713aSLionel Sambuc       return DAR_Failed;
4048f4a2713aSLionel Sambuc   }
4049f4a2713aSLionel Sambuc 
4050f4a2713aSLionel Sambuc   if (Deduced[0].getKind() != TemplateArgument::Type)
4051f4a2713aSLionel Sambuc     return DAR_Failed;
4052f4a2713aSLionel Sambuc 
4053f4a2713aSLionel Sambuc   QualType DeducedType = Deduced[0].getAsType();
4054f4a2713aSLionel Sambuc 
4055f4a2713aSLionel Sambuc   if (InitList) {
4056f4a2713aSLionel Sambuc     DeducedType = BuildStdInitializerList(DeducedType, Loc);
4057f4a2713aSLionel Sambuc     if (DeducedType.isNull())
4058f4a2713aSLionel Sambuc       return DAR_FailedAlreadyDiagnosed;
4059f4a2713aSLionel Sambuc   }
4060f4a2713aSLionel Sambuc 
4061f4a2713aSLionel Sambuc   Result = SubstituteAutoTransform(*this, DeducedType).Apply(Type);
4062f4a2713aSLionel Sambuc   if (Result.isNull())
4063f4a2713aSLionel Sambuc    return DAR_FailedAlreadyDiagnosed;
4064f4a2713aSLionel Sambuc 
4065f4a2713aSLionel Sambuc   // Check that the deduced argument type is compatible with the original
4066f4a2713aSLionel Sambuc   // argument type per C++ [temp.deduct.call]p4.
4067f4a2713aSLionel Sambuc   if (!InitList && !Result.isNull() &&
4068f4a2713aSLionel Sambuc       CheckOriginalCallArgDeduction(*this,
4069f4a2713aSLionel Sambuc                                     Sema::OriginalCallArg(FuncParam,0,InitType),
4070f4a2713aSLionel Sambuc                                     Result)) {
4071f4a2713aSLionel Sambuc     Result = QualType();
4072f4a2713aSLionel Sambuc     return DAR_Failed;
4073f4a2713aSLionel Sambuc   }
4074f4a2713aSLionel Sambuc 
4075f4a2713aSLionel Sambuc   return DAR_Succeeded;
4076f4a2713aSLionel Sambuc }
4077f4a2713aSLionel Sambuc 
SubstAutoType(QualType TypeWithAuto,QualType TypeToReplaceAuto)4078f4a2713aSLionel Sambuc QualType Sema::SubstAutoType(QualType TypeWithAuto,
4079f4a2713aSLionel Sambuc                              QualType TypeToReplaceAuto) {
4080f4a2713aSLionel Sambuc   return SubstituteAutoTransform(*this, TypeToReplaceAuto).
4081f4a2713aSLionel Sambuc                TransformType(TypeWithAuto);
4082f4a2713aSLionel Sambuc }
4083f4a2713aSLionel Sambuc 
SubstAutoTypeSourceInfo(TypeSourceInfo * TypeWithAuto,QualType TypeToReplaceAuto)4084f4a2713aSLionel Sambuc TypeSourceInfo* Sema::SubstAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto,
4085f4a2713aSLionel Sambuc                              QualType TypeToReplaceAuto) {
4086f4a2713aSLionel Sambuc     return SubstituteAutoTransform(*this, TypeToReplaceAuto).
4087f4a2713aSLionel Sambuc                TransformType(TypeWithAuto);
4088f4a2713aSLionel Sambuc }
4089f4a2713aSLionel Sambuc 
DiagnoseAutoDeductionFailure(VarDecl * VDecl,Expr * Init)4090f4a2713aSLionel Sambuc void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) {
4091f4a2713aSLionel Sambuc   if (isa<InitListExpr>(Init))
4092f4a2713aSLionel Sambuc     Diag(VDecl->getLocation(),
4093f4a2713aSLionel Sambuc          VDecl->isInitCapture()
4094f4a2713aSLionel Sambuc              ? diag::err_init_capture_deduction_failure_from_init_list
4095f4a2713aSLionel Sambuc              : diag::err_auto_var_deduction_failure_from_init_list)
4096f4a2713aSLionel Sambuc       << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange();
4097f4a2713aSLionel Sambuc   else
4098f4a2713aSLionel Sambuc     Diag(VDecl->getLocation(),
4099f4a2713aSLionel Sambuc          VDecl->isInitCapture() ? diag::err_init_capture_deduction_failure
4100f4a2713aSLionel Sambuc                                 : diag::err_auto_var_deduction_failure)
4101f4a2713aSLionel Sambuc       << VDecl->getDeclName() << VDecl->getType() << Init->getType()
4102f4a2713aSLionel Sambuc       << Init->getSourceRange();
4103f4a2713aSLionel Sambuc }
4104f4a2713aSLionel Sambuc 
DeduceReturnType(FunctionDecl * FD,SourceLocation Loc,bool Diagnose)4105f4a2713aSLionel Sambuc bool Sema::DeduceReturnType(FunctionDecl *FD, SourceLocation Loc,
4106f4a2713aSLionel Sambuc                             bool Diagnose) {
4107*0a6a1f1dSLionel Sambuc   assert(FD->getReturnType()->isUndeducedType());
4108f4a2713aSLionel Sambuc 
4109f4a2713aSLionel Sambuc   if (FD->getTemplateInstantiationPattern())
4110f4a2713aSLionel Sambuc     InstantiateFunctionDefinition(Loc, FD);
4111f4a2713aSLionel Sambuc 
4112*0a6a1f1dSLionel Sambuc   bool StillUndeduced = FD->getReturnType()->isUndeducedType();
4113f4a2713aSLionel Sambuc   if (StillUndeduced && Diagnose && !FD->isInvalidDecl()) {
4114f4a2713aSLionel Sambuc     Diag(Loc, diag::err_auto_fn_used_before_defined) << FD;
4115f4a2713aSLionel Sambuc     Diag(FD->getLocation(), diag::note_callee_decl) << FD;
4116f4a2713aSLionel Sambuc   }
4117f4a2713aSLionel Sambuc 
4118f4a2713aSLionel Sambuc   return StillUndeduced;
4119f4a2713aSLionel Sambuc }
4120f4a2713aSLionel Sambuc 
4121f4a2713aSLionel Sambuc static void
4122f4a2713aSLionel Sambuc MarkUsedTemplateParameters(ASTContext &Ctx, QualType T,
4123f4a2713aSLionel Sambuc                            bool OnlyDeduced,
4124f4a2713aSLionel Sambuc                            unsigned Level,
4125f4a2713aSLionel Sambuc                            llvm::SmallBitVector &Deduced);
4126f4a2713aSLionel Sambuc 
4127f4a2713aSLionel Sambuc /// \brief If this is a non-static member function,
4128f4a2713aSLionel Sambuc static void
AddImplicitObjectParameterType(ASTContext & Context,CXXMethodDecl * Method,SmallVectorImpl<QualType> & ArgTypes)4129f4a2713aSLionel Sambuc AddImplicitObjectParameterType(ASTContext &Context,
4130f4a2713aSLionel Sambuc                                CXXMethodDecl *Method,
4131f4a2713aSLionel Sambuc                                SmallVectorImpl<QualType> &ArgTypes) {
4132f4a2713aSLionel Sambuc   // C++11 [temp.func.order]p3:
4133f4a2713aSLionel Sambuc   //   [...] The new parameter is of type "reference to cv A," where cv are
4134f4a2713aSLionel Sambuc   //   the cv-qualifiers of the function template (if any) and A is
4135f4a2713aSLionel Sambuc   //   the class of which the function template is a member.
4136f4a2713aSLionel Sambuc   //
4137f4a2713aSLionel Sambuc   // The standard doesn't say explicitly, but we pick the appropriate kind of
4138f4a2713aSLionel Sambuc   // reference type based on [over.match.funcs]p4.
4139f4a2713aSLionel Sambuc   QualType ArgTy = Context.getTypeDeclType(Method->getParent());
4140f4a2713aSLionel Sambuc   ArgTy = Context.getQualifiedType(ArgTy,
4141f4a2713aSLionel Sambuc                         Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
4142f4a2713aSLionel Sambuc   if (Method->getRefQualifier() == RQ_RValue)
4143f4a2713aSLionel Sambuc     ArgTy = Context.getRValueReferenceType(ArgTy);
4144f4a2713aSLionel Sambuc   else
4145f4a2713aSLionel Sambuc     ArgTy = Context.getLValueReferenceType(ArgTy);
4146f4a2713aSLionel Sambuc   ArgTypes.push_back(ArgTy);
4147f4a2713aSLionel Sambuc }
4148f4a2713aSLionel Sambuc 
4149f4a2713aSLionel Sambuc /// \brief Determine whether the function template \p FT1 is at least as
4150f4a2713aSLionel Sambuc /// specialized as \p FT2.
isAtLeastAsSpecializedAs(Sema & S,SourceLocation Loc,FunctionTemplateDecl * FT1,FunctionTemplateDecl * FT2,TemplatePartialOrderingContext TPOC,unsigned NumCallArguments1,SmallVectorImpl<RefParamPartialOrderingComparison> * RefParamComparisons)4151f4a2713aSLionel Sambuc static bool isAtLeastAsSpecializedAs(Sema &S,
4152f4a2713aSLionel Sambuc                                      SourceLocation Loc,
4153f4a2713aSLionel Sambuc                                      FunctionTemplateDecl *FT1,
4154f4a2713aSLionel Sambuc                                      FunctionTemplateDecl *FT2,
4155f4a2713aSLionel Sambuc                                      TemplatePartialOrderingContext TPOC,
4156f4a2713aSLionel Sambuc                                      unsigned NumCallArguments1,
4157f4a2713aSLionel Sambuc     SmallVectorImpl<RefParamPartialOrderingComparison> *RefParamComparisons) {
4158f4a2713aSLionel Sambuc   FunctionDecl *FD1 = FT1->getTemplatedDecl();
4159f4a2713aSLionel Sambuc   FunctionDecl *FD2 = FT2->getTemplatedDecl();
4160f4a2713aSLionel Sambuc   const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
4161f4a2713aSLionel Sambuc   const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
4162f4a2713aSLionel Sambuc 
4163f4a2713aSLionel Sambuc   assert(Proto1 && Proto2 && "Function templates must have prototypes");
4164f4a2713aSLionel Sambuc   TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
4165f4a2713aSLionel Sambuc   SmallVector<DeducedTemplateArgument, 4> Deduced;
4166f4a2713aSLionel Sambuc   Deduced.resize(TemplateParams->size());
4167f4a2713aSLionel Sambuc 
4168f4a2713aSLionel Sambuc   // C++0x [temp.deduct.partial]p3:
4169f4a2713aSLionel Sambuc   //   The types used to determine the ordering depend on the context in which
4170f4a2713aSLionel Sambuc   //   the partial ordering is done:
4171f4a2713aSLionel Sambuc   TemplateDeductionInfo Info(Loc);
4172f4a2713aSLionel Sambuc   SmallVector<QualType, 4> Args2;
4173f4a2713aSLionel Sambuc   switch (TPOC) {
4174f4a2713aSLionel Sambuc   case TPOC_Call: {
4175f4a2713aSLionel Sambuc     //   - In the context of a function call, the function parameter types are
4176f4a2713aSLionel Sambuc     //     used.
4177f4a2713aSLionel Sambuc     CXXMethodDecl *Method1 = dyn_cast<CXXMethodDecl>(FD1);
4178f4a2713aSLionel Sambuc     CXXMethodDecl *Method2 = dyn_cast<CXXMethodDecl>(FD2);
4179f4a2713aSLionel Sambuc 
4180f4a2713aSLionel Sambuc     // C++11 [temp.func.order]p3:
4181f4a2713aSLionel Sambuc     //   [...] If only one of the function templates is a non-static
4182f4a2713aSLionel Sambuc     //   member, that function template is considered to have a new
4183f4a2713aSLionel Sambuc     //   first parameter inserted in its function parameter list. The
4184f4a2713aSLionel Sambuc     //   new parameter is of type "reference to cv A," where cv are
4185f4a2713aSLionel Sambuc     //   the cv-qualifiers of the function template (if any) and A is
4186f4a2713aSLionel Sambuc     //   the class of which the function template is a member.
4187f4a2713aSLionel Sambuc     //
4188f4a2713aSLionel Sambuc     // Note that we interpret this to mean "if one of the function
4189f4a2713aSLionel Sambuc     // templates is a non-static member and the other is a non-member";
4190f4a2713aSLionel Sambuc     // otherwise, the ordering rules for static functions against non-static
4191f4a2713aSLionel Sambuc     // functions don't make any sense.
4192f4a2713aSLionel Sambuc     //
4193*0a6a1f1dSLionel Sambuc     // C++98/03 doesn't have this provision but we've extended DR532 to cover
4194*0a6a1f1dSLionel Sambuc     // it as wording was broken prior to it.
4195f4a2713aSLionel Sambuc     SmallVector<QualType, 4> Args1;
4196f4a2713aSLionel Sambuc 
4197f4a2713aSLionel Sambuc     unsigned NumComparedArguments = NumCallArguments1;
4198f4a2713aSLionel Sambuc 
4199f4a2713aSLionel Sambuc     if (!Method2 && Method1 && !Method1->isStatic()) {
4200f4a2713aSLionel Sambuc       // Compare 'this' from Method1 against first parameter from Method2.
4201f4a2713aSLionel Sambuc       AddImplicitObjectParameterType(S.Context, Method1, Args1);
4202f4a2713aSLionel Sambuc       ++NumComparedArguments;
4203f4a2713aSLionel Sambuc     } else if (!Method1 && Method2 && !Method2->isStatic()) {
4204f4a2713aSLionel Sambuc       // Compare 'this' from Method2 against first parameter from Method1.
4205f4a2713aSLionel Sambuc       AddImplicitObjectParameterType(S.Context, Method2, Args2);
4206f4a2713aSLionel Sambuc     }
4207f4a2713aSLionel Sambuc 
4208*0a6a1f1dSLionel Sambuc     Args1.insert(Args1.end(), Proto1->param_type_begin(),
4209*0a6a1f1dSLionel Sambuc                  Proto1->param_type_end());
4210*0a6a1f1dSLionel Sambuc     Args2.insert(Args2.end(), Proto2->param_type_begin(),
4211*0a6a1f1dSLionel Sambuc                  Proto2->param_type_end());
4212f4a2713aSLionel Sambuc 
4213f4a2713aSLionel Sambuc     // C++ [temp.func.order]p5:
4214f4a2713aSLionel Sambuc     //   The presence of unused ellipsis and default arguments has no effect on
4215f4a2713aSLionel Sambuc     //   the partial ordering of function templates.
4216f4a2713aSLionel Sambuc     if (Args1.size() > NumComparedArguments)
4217f4a2713aSLionel Sambuc       Args1.resize(NumComparedArguments);
4218f4a2713aSLionel Sambuc     if (Args2.size() > NumComparedArguments)
4219f4a2713aSLionel Sambuc       Args2.resize(NumComparedArguments);
4220f4a2713aSLionel Sambuc     if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(),
4221f4a2713aSLionel Sambuc                                 Args1.data(), Args1.size(), Info, Deduced,
4222f4a2713aSLionel Sambuc                                 TDF_None, /*PartialOrdering=*/true,
4223f4a2713aSLionel Sambuc                                 RefParamComparisons))
4224f4a2713aSLionel Sambuc       return false;
4225f4a2713aSLionel Sambuc 
4226f4a2713aSLionel Sambuc     break;
4227f4a2713aSLionel Sambuc   }
4228f4a2713aSLionel Sambuc 
4229f4a2713aSLionel Sambuc   case TPOC_Conversion:
4230f4a2713aSLionel Sambuc     //   - In the context of a call to a conversion operator, the return types
4231f4a2713aSLionel Sambuc     //     of the conversion function templates are used.
4232*0a6a1f1dSLionel Sambuc     if (DeduceTemplateArgumentsByTypeMatch(
4233*0a6a1f1dSLionel Sambuc             S, TemplateParams, Proto2->getReturnType(), Proto1->getReturnType(),
4234f4a2713aSLionel Sambuc             Info, Deduced, TDF_None,
4235*0a6a1f1dSLionel Sambuc             /*PartialOrdering=*/true, RefParamComparisons))
4236f4a2713aSLionel Sambuc       return false;
4237f4a2713aSLionel Sambuc     break;
4238f4a2713aSLionel Sambuc 
4239f4a2713aSLionel Sambuc   case TPOC_Other:
4240f4a2713aSLionel Sambuc     //   - In other contexts (14.6.6.2) the function template's function type
4241f4a2713aSLionel Sambuc     //     is used.
4242f4a2713aSLionel Sambuc     if (DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
4243f4a2713aSLionel Sambuc                                            FD2->getType(), FD1->getType(),
4244f4a2713aSLionel Sambuc                                            Info, Deduced, TDF_None,
4245f4a2713aSLionel Sambuc                                            /*PartialOrdering=*/true,
4246f4a2713aSLionel Sambuc                                            RefParamComparisons))
4247f4a2713aSLionel Sambuc       return false;
4248f4a2713aSLionel Sambuc     break;
4249f4a2713aSLionel Sambuc   }
4250f4a2713aSLionel Sambuc 
4251f4a2713aSLionel Sambuc   // C++0x [temp.deduct.partial]p11:
4252f4a2713aSLionel Sambuc   //   In most cases, all template parameters must have values in order for
4253f4a2713aSLionel Sambuc   //   deduction to succeed, but for partial ordering purposes a template
4254f4a2713aSLionel Sambuc   //   parameter may remain without a value provided it is not used in the
4255f4a2713aSLionel Sambuc   //   types being used for partial ordering. [ Note: a template parameter used
4256f4a2713aSLionel Sambuc   //   in a non-deduced context is considered used. -end note]
4257f4a2713aSLionel Sambuc   unsigned ArgIdx = 0, NumArgs = Deduced.size();
4258f4a2713aSLionel Sambuc   for (; ArgIdx != NumArgs; ++ArgIdx)
4259f4a2713aSLionel Sambuc     if (Deduced[ArgIdx].isNull())
4260f4a2713aSLionel Sambuc       break;
4261f4a2713aSLionel Sambuc 
4262f4a2713aSLionel Sambuc   if (ArgIdx == NumArgs) {
4263f4a2713aSLionel Sambuc     // All template arguments were deduced. FT1 is at least as specialized
4264f4a2713aSLionel Sambuc     // as FT2.
4265f4a2713aSLionel Sambuc     return true;
4266f4a2713aSLionel Sambuc   }
4267f4a2713aSLionel Sambuc 
4268f4a2713aSLionel Sambuc   // Figure out which template parameters were used.
4269f4a2713aSLionel Sambuc   llvm::SmallBitVector UsedParameters(TemplateParams->size());
4270f4a2713aSLionel Sambuc   switch (TPOC) {
4271f4a2713aSLionel Sambuc   case TPOC_Call:
4272f4a2713aSLionel Sambuc     for (unsigned I = 0, N = Args2.size(); I != N; ++I)
4273f4a2713aSLionel Sambuc       ::MarkUsedTemplateParameters(S.Context, Args2[I], false,
4274f4a2713aSLionel Sambuc                                    TemplateParams->getDepth(),
4275f4a2713aSLionel Sambuc                                    UsedParameters);
4276f4a2713aSLionel Sambuc     break;
4277f4a2713aSLionel Sambuc 
4278f4a2713aSLionel Sambuc   case TPOC_Conversion:
4279*0a6a1f1dSLionel Sambuc     ::MarkUsedTemplateParameters(S.Context, Proto2->getReturnType(), false,
4280*0a6a1f1dSLionel Sambuc                                  TemplateParams->getDepth(), UsedParameters);
4281f4a2713aSLionel Sambuc     break;
4282f4a2713aSLionel Sambuc 
4283f4a2713aSLionel Sambuc   case TPOC_Other:
4284f4a2713aSLionel Sambuc     ::MarkUsedTemplateParameters(S.Context, FD2->getType(), false,
4285f4a2713aSLionel Sambuc                                  TemplateParams->getDepth(),
4286f4a2713aSLionel Sambuc                                  UsedParameters);
4287f4a2713aSLionel Sambuc     break;
4288f4a2713aSLionel Sambuc   }
4289f4a2713aSLionel Sambuc 
4290f4a2713aSLionel Sambuc   for (; ArgIdx != NumArgs; ++ArgIdx)
4291f4a2713aSLionel Sambuc     // If this argument had no value deduced but was used in one of the types
4292f4a2713aSLionel Sambuc     // used for partial ordering, then deduction fails.
4293f4a2713aSLionel Sambuc     if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
4294f4a2713aSLionel Sambuc       return false;
4295f4a2713aSLionel Sambuc 
4296f4a2713aSLionel Sambuc   return true;
4297f4a2713aSLionel Sambuc }
4298f4a2713aSLionel Sambuc 
4299f4a2713aSLionel Sambuc /// \brief Determine whether this a function template whose parameter-type-list
4300f4a2713aSLionel Sambuc /// ends with a function parameter pack.
isVariadicFunctionTemplate(FunctionTemplateDecl * FunTmpl)4301f4a2713aSLionel Sambuc static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) {
4302f4a2713aSLionel Sambuc   FunctionDecl *Function = FunTmpl->getTemplatedDecl();
4303f4a2713aSLionel Sambuc   unsigned NumParams = Function->getNumParams();
4304f4a2713aSLionel Sambuc   if (NumParams == 0)
4305f4a2713aSLionel Sambuc     return false;
4306f4a2713aSLionel Sambuc 
4307f4a2713aSLionel Sambuc   ParmVarDecl *Last = Function->getParamDecl(NumParams - 1);
4308f4a2713aSLionel Sambuc   if (!Last->isParameterPack())
4309f4a2713aSLionel Sambuc     return false;
4310f4a2713aSLionel Sambuc 
4311f4a2713aSLionel Sambuc   // Make sure that no previous parameter is a parameter pack.
4312f4a2713aSLionel Sambuc   while (--NumParams > 0) {
4313f4a2713aSLionel Sambuc     if (Function->getParamDecl(NumParams - 1)->isParameterPack())
4314f4a2713aSLionel Sambuc       return false;
4315f4a2713aSLionel Sambuc   }
4316f4a2713aSLionel Sambuc 
4317f4a2713aSLionel Sambuc   return true;
4318f4a2713aSLionel Sambuc }
4319f4a2713aSLionel Sambuc 
4320f4a2713aSLionel Sambuc /// \brief Returns the more specialized function template according
4321f4a2713aSLionel Sambuc /// to the rules of function template partial ordering (C++ [temp.func.order]).
4322f4a2713aSLionel Sambuc ///
4323f4a2713aSLionel Sambuc /// \param FT1 the first function template
4324f4a2713aSLionel Sambuc ///
4325f4a2713aSLionel Sambuc /// \param FT2 the second function template
4326f4a2713aSLionel Sambuc ///
4327f4a2713aSLionel Sambuc /// \param TPOC the context in which we are performing partial ordering of
4328f4a2713aSLionel Sambuc /// function templates.
4329f4a2713aSLionel Sambuc ///
4330f4a2713aSLionel Sambuc /// \param NumCallArguments1 The number of arguments in the call to FT1, used
4331f4a2713aSLionel Sambuc /// only when \c TPOC is \c TPOC_Call.
4332f4a2713aSLionel Sambuc ///
4333f4a2713aSLionel Sambuc /// \param NumCallArguments2 The number of arguments in the call to FT2, used
4334f4a2713aSLionel Sambuc /// only when \c TPOC is \c TPOC_Call.
4335f4a2713aSLionel Sambuc ///
4336f4a2713aSLionel Sambuc /// \returns the more specialized function template. If neither
4337f4a2713aSLionel Sambuc /// template is more specialized, returns NULL.
4338f4a2713aSLionel Sambuc FunctionTemplateDecl *
getMoreSpecializedTemplate(FunctionTemplateDecl * FT1,FunctionTemplateDecl * FT2,SourceLocation Loc,TemplatePartialOrderingContext TPOC,unsigned NumCallArguments1,unsigned NumCallArguments2)4339f4a2713aSLionel Sambuc Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
4340f4a2713aSLionel Sambuc                                  FunctionTemplateDecl *FT2,
4341f4a2713aSLionel Sambuc                                  SourceLocation Loc,
4342f4a2713aSLionel Sambuc                                  TemplatePartialOrderingContext TPOC,
4343f4a2713aSLionel Sambuc                                  unsigned NumCallArguments1,
4344f4a2713aSLionel Sambuc                                  unsigned NumCallArguments2) {
4345f4a2713aSLionel Sambuc   SmallVector<RefParamPartialOrderingComparison, 4> RefParamComparisons;
4346f4a2713aSLionel Sambuc   bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC,
4347*0a6a1f1dSLionel Sambuc                                           NumCallArguments1, nullptr);
4348f4a2713aSLionel Sambuc   bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
4349f4a2713aSLionel Sambuc                                           NumCallArguments2,
4350f4a2713aSLionel Sambuc                                           &RefParamComparisons);
4351f4a2713aSLionel Sambuc 
4352f4a2713aSLionel Sambuc   if (Better1 != Better2) // We have a clear winner
4353f4a2713aSLionel Sambuc     return Better1? FT1 : FT2;
4354f4a2713aSLionel Sambuc 
4355f4a2713aSLionel Sambuc   if (!Better1 && !Better2) // Neither is better than the other
4356*0a6a1f1dSLionel Sambuc     return nullptr;
4357f4a2713aSLionel Sambuc 
4358f4a2713aSLionel Sambuc   // C++0x [temp.deduct.partial]p10:
4359f4a2713aSLionel Sambuc   //   If for each type being considered a given template is at least as
4360f4a2713aSLionel Sambuc   //   specialized for all types and more specialized for some set of types and
4361f4a2713aSLionel Sambuc   //   the other template is not more specialized for any types or is not at
4362f4a2713aSLionel Sambuc   //   least as specialized for any types, then the given template is more
4363f4a2713aSLionel Sambuc   //   specialized than the other template. Otherwise, neither template is more
4364f4a2713aSLionel Sambuc   //   specialized than the other.
4365f4a2713aSLionel Sambuc   Better1 = false;
4366f4a2713aSLionel Sambuc   Better2 = false;
4367f4a2713aSLionel Sambuc   for (unsigned I = 0, N = RefParamComparisons.size(); I != N; ++I) {
4368f4a2713aSLionel Sambuc     // C++0x [temp.deduct.partial]p9:
4369f4a2713aSLionel Sambuc     //   If, for a given type, deduction succeeds in both directions (i.e., the
4370f4a2713aSLionel Sambuc     //   types are identical after the transformations above) and both P and A
4371f4a2713aSLionel Sambuc     //   were reference types (before being replaced with the type referred to
4372f4a2713aSLionel Sambuc     //   above):
4373f4a2713aSLionel Sambuc 
4374f4a2713aSLionel Sambuc     //     -- if the type from the argument template was an lvalue reference
4375f4a2713aSLionel Sambuc     //        and the type from the parameter template was not, the argument
4376f4a2713aSLionel Sambuc     //        type is considered to be more specialized than the other;
4377f4a2713aSLionel Sambuc     //        otherwise,
4378f4a2713aSLionel Sambuc     if (!RefParamComparisons[I].ArgIsRvalueRef &&
4379f4a2713aSLionel Sambuc         RefParamComparisons[I].ParamIsRvalueRef) {
4380f4a2713aSLionel Sambuc       Better2 = true;
4381f4a2713aSLionel Sambuc       if (Better1)
4382*0a6a1f1dSLionel Sambuc         return nullptr;
4383f4a2713aSLionel Sambuc       continue;
4384f4a2713aSLionel Sambuc     } else if (!RefParamComparisons[I].ParamIsRvalueRef &&
4385f4a2713aSLionel Sambuc                RefParamComparisons[I].ArgIsRvalueRef) {
4386f4a2713aSLionel Sambuc       Better1 = true;
4387f4a2713aSLionel Sambuc       if (Better2)
4388*0a6a1f1dSLionel Sambuc         return nullptr;
4389f4a2713aSLionel Sambuc       continue;
4390f4a2713aSLionel Sambuc     }
4391f4a2713aSLionel Sambuc 
4392f4a2713aSLionel Sambuc     //     -- if the type from the argument template is more cv-qualified than
4393f4a2713aSLionel Sambuc     //        the type from the parameter template (as described above), the
4394f4a2713aSLionel Sambuc     //        argument type is considered to be more specialized than the
4395f4a2713aSLionel Sambuc     //        other; otherwise,
4396f4a2713aSLionel Sambuc     switch (RefParamComparisons[I].Qualifiers) {
4397f4a2713aSLionel Sambuc     case NeitherMoreQualified:
4398f4a2713aSLionel Sambuc       break;
4399f4a2713aSLionel Sambuc 
4400f4a2713aSLionel Sambuc     case ParamMoreQualified:
4401f4a2713aSLionel Sambuc       Better1 = true;
4402f4a2713aSLionel Sambuc       if (Better2)
4403*0a6a1f1dSLionel Sambuc         return nullptr;
4404f4a2713aSLionel Sambuc       continue;
4405f4a2713aSLionel Sambuc 
4406f4a2713aSLionel Sambuc     case ArgMoreQualified:
4407f4a2713aSLionel Sambuc       Better2 = true;
4408f4a2713aSLionel Sambuc       if (Better1)
4409*0a6a1f1dSLionel Sambuc         return nullptr;
4410f4a2713aSLionel Sambuc       continue;
4411f4a2713aSLionel Sambuc     }
4412f4a2713aSLionel Sambuc 
4413f4a2713aSLionel Sambuc     //     -- neither type is more specialized than the other.
4414f4a2713aSLionel Sambuc   }
4415f4a2713aSLionel Sambuc 
4416f4a2713aSLionel Sambuc   assert(!(Better1 && Better2) && "Should have broken out in the loop above");
4417f4a2713aSLionel Sambuc   if (Better1)
4418f4a2713aSLionel Sambuc     return FT1;
4419f4a2713aSLionel Sambuc   else if (Better2)
4420f4a2713aSLionel Sambuc     return FT2;
4421f4a2713aSLionel Sambuc 
4422f4a2713aSLionel Sambuc   // FIXME: This mimics what GCC implements, but doesn't match up with the
4423f4a2713aSLionel Sambuc   // proposed resolution for core issue 692. This area needs to be sorted out,
4424f4a2713aSLionel Sambuc   // but for now we attempt to maintain compatibility.
4425f4a2713aSLionel Sambuc   bool Variadic1 = isVariadicFunctionTemplate(FT1);
4426f4a2713aSLionel Sambuc   bool Variadic2 = isVariadicFunctionTemplate(FT2);
4427f4a2713aSLionel Sambuc   if (Variadic1 != Variadic2)
4428f4a2713aSLionel Sambuc     return Variadic1? FT2 : FT1;
4429f4a2713aSLionel Sambuc 
4430*0a6a1f1dSLionel Sambuc   return nullptr;
4431f4a2713aSLionel Sambuc }
4432f4a2713aSLionel Sambuc 
4433f4a2713aSLionel Sambuc /// \brief Determine if the two templates are equivalent.
isSameTemplate(TemplateDecl * T1,TemplateDecl * T2)4434f4a2713aSLionel Sambuc static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
4435f4a2713aSLionel Sambuc   if (T1 == T2)
4436f4a2713aSLionel Sambuc     return true;
4437f4a2713aSLionel Sambuc 
4438f4a2713aSLionel Sambuc   if (!T1 || !T2)
4439f4a2713aSLionel Sambuc     return false;
4440f4a2713aSLionel Sambuc 
4441f4a2713aSLionel Sambuc   return T1->getCanonicalDecl() == T2->getCanonicalDecl();
4442f4a2713aSLionel Sambuc }
4443f4a2713aSLionel Sambuc 
4444f4a2713aSLionel Sambuc /// \brief Retrieve the most specialized of the given function template
4445f4a2713aSLionel Sambuc /// specializations.
4446f4a2713aSLionel Sambuc ///
4447f4a2713aSLionel Sambuc /// \param SpecBegin the start iterator of the function template
4448f4a2713aSLionel Sambuc /// specializations that we will be comparing.
4449f4a2713aSLionel Sambuc ///
4450f4a2713aSLionel Sambuc /// \param SpecEnd the end iterator of the function template
4451f4a2713aSLionel Sambuc /// specializations, paired with \p SpecBegin.
4452f4a2713aSLionel Sambuc ///
4453f4a2713aSLionel Sambuc /// \param Loc the location where the ambiguity or no-specializations
4454f4a2713aSLionel Sambuc /// diagnostic should occur.
4455f4a2713aSLionel Sambuc ///
4456f4a2713aSLionel Sambuc /// \param NoneDiag partial diagnostic used to diagnose cases where there are
4457f4a2713aSLionel Sambuc /// no matching candidates.
4458f4a2713aSLionel Sambuc ///
4459f4a2713aSLionel Sambuc /// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
4460f4a2713aSLionel Sambuc /// occurs.
4461f4a2713aSLionel Sambuc ///
4462f4a2713aSLionel Sambuc /// \param CandidateDiag partial diagnostic used for each function template
4463f4a2713aSLionel Sambuc /// specialization that is a candidate in the ambiguous ordering. One parameter
4464f4a2713aSLionel Sambuc /// in this diagnostic should be unbound, which will correspond to the string
4465f4a2713aSLionel Sambuc /// describing the template arguments for the function template specialization.
4466f4a2713aSLionel Sambuc ///
4467f4a2713aSLionel Sambuc /// \returns the most specialized function template specialization, if
4468f4a2713aSLionel Sambuc /// found. Otherwise, returns SpecEnd.
getMostSpecialized(UnresolvedSetIterator SpecBegin,UnresolvedSetIterator SpecEnd,TemplateSpecCandidateSet & FailedCandidates,SourceLocation Loc,const PartialDiagnostic & NoneDiag,const PartialDiagnostic & AmbigDiag,const PartialDiagnostic & CandidateDiag,bool Complain,QualType TargetType)4469f4a2713aSLionel Sambuc UnresolvedSetIterator Sema::getMostSpecialized(
4470f4a2713aSLionel Sambuc     UnresolvedSetIterator SpecBegin, UnresolvedSetIterator SpecEnd,
4471f4a2713aSLionel Sambuc     TemplateSpecCandidateSet &FailedCandidates,
4472f4a2713aSLionel Sambuc     SourceLocation Loc, const PartialDiagnostic &NoneDiag,
4473f4a2713aSLionel Sambuc     const PartialDiagnostic &AmbigDiag, const PartialDiagnostic &CandidateDiag,
4474f4a2713aSLionel Sambuc     bool Complain, QualType TargetType) {
4475f4a2713aSLionel Sambuc   if (SpecBegin == SpecEnd) {
4476f4a2713aSLionel Sambuc     if (Complain) {
4477f4a2713aSLionel Sambuc       Diag(Loc, NoneDiag);
4478f4a2713aSLionel Sambuc       FailedCandidates.NoteCandidates(*this, Loc);
4479f4a2713aSLionel Sambuc     }
4480f4a2713aSLionel Sambuc     return SpecEnd;
4481f4a2713aSLionel Sambuc   }
4482f4a2713aSLionel Sambuc 
4483f4a2713aSLionel Sambuc   if (SpecBegin + 1 == SpecEnd)
4484f4a2713aSLionel Sambuc     return SpecBegin;
4485f4a2713aSLionel Sambuc 
4486f4a2713aSLionel Sambuc   // Find the function template that is better than all of the templates it
4487f4a2713aSLionel Sambuc   // has been compared to.
4488f4a2713aSLionel Sambuc   UnresolvedSetIterator Best = SpecBegin;
4489f4a2713aSLionel Sambuc   FunctionTemplateDecl *BestTemplate
4490f4a2713aSLionel Sambuc     = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
4491f4a2713aSLionel Sambuc   assert(BestTemplate && "Not a function template specialization?");
4492f4a2713aSLionel Sambuc   for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
4493f4a2713aSLionel Sambuc     FunctionTemplateDecl *Challenger
4494f4a2713aSLionel Sambuc       = cast<FunctionDecl>(*I)->getPrimaryTemplate();
4495f4a2713aSLionel Sambuc     assert(Challenger && "Not a function template specialization?");
4496f4a2713aSLionel Sambuc     if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
4497f4a2713aSLionel Sambuc                                                   Loc, TPOC_Other, 0, 0),
4498f4a2713aSLionel Sambuc                        Challenger)) {
4499f4a2713aSLionel Sambuc       Best = I;
4500f4a2713aSLionel Sambuc       BestTemplate = Challenger;
4501f4a2713aSLionel Sambuc     }
4502f4a2713aSLionel Sambuc   }
4503f4a2713aSLionel Sambuc 
4504f4a2713aSLionel Sambuc   // Make sure that the "best" function template is more specialized than all
4505f4a2713aSLionel Sambuc   // of the others.
4506f4a2713aSLionel Sambuc   bool Ambiguous = false;
4507f4a2713aSLionel Sambuc   for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
4508f4a2713aSLionel Sambuc     FunctionTemplateDecl *Challenger
4509f4a2713aSLionel Sambuc       = cast<FunctionDecl>(*I)->getPrimaryTemplate();
4510f4a2713aSLionel Sambuc     if (I != Best &&
4511f4a2713aSLionel Sambuc         !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
4512f4a2713aSLionel Sambuc                                                    Loc, TPOC_Other, 0, 0),
4513f4a2713aSLionel Sambuc                         BestTemplate)) {
4514f4a2713aSLionel Sambuc       Ambiguous = true;
4515f4a2713aSLionel Sambuc       break;
4516f4a2713aSLionel Sambuc     }
4517f4a2713aSLionel Sambuc   }
4518f4a2713aSLionel Sambuc 
4519f4a2713aSLionel Sambuc   if (!Ambiguous) {
4520f4a2713aSLionel Sambuc     // We found an answer. Return it.
4521f4a2713aSLionel Sambuc     return Best;
4522f4a2713aSLionel Sambuc   }
4523f4a2713aSLionel Sambuc 
4524f4a2713aSLionel Sambuc   // Diagnose the ambiguity.
4525f4a2713aSLionel Sambuc   if (Complain) {
4526f4a2713aSLionel Sambuc     Diag(Loc, AmbigDiag);
4527f4a2713aSLionel Sambuc 
4528f4a2713aSLionel Sambuc     // FIXME: Can we order the candidates in some sane way?
4529f4a2713aSLionel Sambuc     for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
4530f4a2713aSLionel Sambuc       PartialDiagnostic PD = CandidateDiag;
4531f4a2713aSLionel Sambuc       PD << getTemplateArgumentBindingsText(
4532f4a2713aSLionel Sambuc           cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
4533f4a2713aSLionel Sambuc                     *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
4534f4a2713aSLionel Sambuc       if (!TargetType.isNull())
4535f4a2713aSLionel Sambuc         HandleFunctionTypeMismatch(PD, cast<FunctionDecl>(*I)->getType(),
4536f4a2713aSLionel Sambuc                                    TargetType);
4537f4a2713aSLionel Sambuc       Diag((*I)->getLocation(), PD);
4538f4a2713aSLionel Sambuc     }
4539f4a2713aSLionel Sambuc   }
4540f4a2713aSLionel Sambuc 
4541f4a2713aSLionel Sambuc   return SpecEnd;
4542f4a2713aSLionel Sambuc }
4543f4a2713aSLionel Sambuc 
4544f4a2713aSLionel Sambuc /// \brief Returns the more specialized class template partial specialization
4545f4a2713aSLionel Sambuc /// according to the rules of partial ordering of class template partial
4546f4a2713aSLionel Sambuc /// specializations (C++ [temp.class.order]).
4547f4a2713aSLionel Sambuc ///
4548f4a2713aSLionel Sambuc /// \param PS1 the first class template partial specialization
4549f4a2713aSLionel Sambuc ///
4550f4a2713aSLionel Sambuc /// \param PS2 the second class template partial specialization
4551f4a2713aSLionel Sambuc ///
4552f4a2713aSLionel Sambuc /// \returns the more specialized class template partial specialization. If
4553f4a2713aSLionel Sambuc /// neither partial specialization is more specialized, returns NULL.
4554f4a2713aSLionel Sambuc ClassTemplatePartialSpecializationDecl *
getMoreSpecializedPartialSpecialization(ClassTemplatePartialSpecializationDecl * PS1,ClassTemplatePartialSpecializationDecl * PS2,SourceLocation Loc)4555f4a2713aSLionel Sambuc Sema::getMoreSpecializedPartialSpecialization(
4556f4a2713aSLionel Sambuc                                   ClassTemplatePartialSpecializationDecl *PS1,
4557f4a2713aSLionel Sambuc                                   ClassTemplatePartialSpecializationDecl *PS2,
4558f4a2713aSLionel Sambuc                                               SourceLocation Loc) {
4559f4a2713aSLionel Sambuc   // C++ [temp.class.order]p1:
4560f4a2713aSLionel Sambuc   //   For two class template partial specializations, the first is at least as
4561f4a2713aSLionel Sambuc   //   specialized as the second if, given the following rewrite to two
4562f4a2713aSLionel Sambuc   //   function templates, the first function template is at least as
4563f4a2713aSLionel Sambuc   //   specialized as the second according to the ordering rules for function
4564f4a2713aSLionel Sambuc   //   templates (14.6.6.2):
4565f4a2713aSLionel Sambuc   //     - the first function template has the same template parameters as the
4566f4a2713aSLionel Sambuc   //       first partial specialization and has a single function parameter
4567f4a2713aSLionel Sambuc   //       whose type is a class template specialization with the template
4568f4a2713aSLionel Sambuc   //       arguments of the first partial specialization, and
4569f4a2713aSLionel Sambuc   //     - the second function template has the same template parameters as the
4570f4a2713aSLionel Sambuc   //       second partial specialization and has a single function parameter
4571f4a2713aSLionel Sambuc   //       whose type is a class template specialization with the template
4572f4a2713aSLionel Sambuc   //       arguments of the second partial specialization.
4573f4a2713aSLionel Sambuc   //
4574f4a2713aSLionel Sambuc   // Rather than synthesize function templates, we merely perform the
4575f4a2713aSLionel Sambuc   // equivalent partial ordering by performing deduction directly on
4576f4a2713aSLionel Sambuc   // the template arguments of the class template partial
4577f4a2713aSLionel Sambuc   // specializations. This computation is slightly simpler than the
4578f4a2713aSLionel Sambuc   // general problem of function template partial ordering, because
4579f4a2713aSLionel Sambuc   // class template partial specializations are more constrained. We
4580f4a2713aSLionel Sambuc   // know that every template parameter is deducible from the class
4581f4a2713aSLionel Sambuc   // template partial specialization's template arguments, for
4582f4a2713aSLionel Sambuc   // example.
4583f4a2713aSLionel Sambuc   SmallVector<DeducedTemplateArgument, 4> Deduced;
4584f4a2713aSLionel Sambuc   TemplateDeductionInfo Info(Loc);
4585f4a2713aSLionel Sambuc 
4586f4a2713aSLionel Sambuc   QualType PT1 = PS1->getInjectedSpecializationType();
4587f4a2713aSLionel Sambuc   QualType PT2 = PS2->getInjectedSpecializationType();
4588f4a2713aSLionel Sambuc 
4589f4a2713aSLionel Sambuc   // Determine whether PS1 is at least as specialized as PS2
4590f4a2713aSLionel Sambuc   Deduced.resize(PS2->getTemplateParameters()->size());
4591f4a2713aSLionel Sambuc   bool Better1 = !DeduceTemplateArgumentsByTypeMatch(*this,
4592f4a2713aSLionel Sambuc                                             PS2->getTemplateParameters(),
4593f4a2713aSLionel Sambuc                                             PT2, PT1, Info, Deduced, TDF_None,
4594f4a2713aSLionel Sambuc                                             /*PartialOrdering=*/true,
4595*0a6a1f1dSLionel Sambuc                                             /*RefParamComparisons=*/nullptr);
4596f4a2713aSLionel Sambuc   if (Better1) {
4597f4a2713aSLionel Sambuc     SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(),Deduced.end());
4598*0a6a1f1dSLionel Sambuc     InstantiatingTemplate Inst(*this, Loc, PS2, DeducedArgs, Info);
4599f4a2713aSLionel Sambuc     Better1 = !::FinishTemplateArgumentDeduction(
4600f4a2713aSLionel Sambuc         *this, PS2, PS1->getTemplateArgs(), Deduced, Info);
4601f4a2713aSLionel Sambuc   }
4602f4a2713aSLionel Sambuc 
4603f4a2713aSLionel Sambuc   // Determine whether PS2 is at least as specialized as PS1
4604f4a2713aSLionel Sambuc   Deduced.clear();
4605f4a2713aSLionel Sambuc   Deduced.resize(PS1->getTemplateParameters()->size());
4606f4a2713aSLionel Sambuc   bool Better2 = !DeduceTemplateArgumentsByTypeMatch(
4607f4a2713aSLionel Sambuc       *this, PS1->getTemplateParameters(), PT1, PT2, Info, Deduced, TDF_None,
4608f4a2713aSLionel Sambuc       /*PartialOrdering=*/true,
4609*0a6a1f1dSLionel Sambuc       /*RefParamComparisons=*/nullptr);
4610f4a2713aSLionel Sambuc   if (Better2) {
4611f4a2713aSLionel Sambuc     SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(),
4612f4a2713aSLionel Sambuc                                                  Deduced.end());
4613*0a6a1f1dSLionel Sambuc     InstantiatingTemplate Inst(*this, Loc, PS1, DeducedArgs, Info);
4614f4a2713aSLionel Sambuc     Better2 = !::FinishTemplateArgumentDeduction(
4615f4a2713aSLionel Sambuc         *this, PS1, PS2->getTemplateArgs(), Deduced, Info);
4616f4a2713aSLionel Sambuc   }
4617f4a2713aSLionel Sambuc 
4618f4a2713aSLionel Sambuc   if (Better1 == Better2)
4619*0a6a1f1dSLionel Sambuc     return nullptr;
4620f4a2713aSLionel Sambuc 
4621f4a2713aSLionel Sambuc   return Better1 ? PS1 : PS2;
4622f4a2713aSLionel Sambuc }
4623f4a2713aSLionel Sambuc 
4624f4a2713aSLionel Sambuc /// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
4625f4a2713aSLionel Sambuc ///       May require unifying ClassTemplate(Partial)SpecializationDecl and
4626f4a2713aSLionel Sambuc ///        VarTemplate(Partial)SpecializationDecl with a new data
4627f4a2713aSLionel Sambuc ///        structure Template(Partial)SpecializationDecl, and
4628f4a2713aSLionel Sambuc ///        using Template(Partial)SpecializationDecl as input type.
4629f4a2713aSLionel Sambuc VarTemplatePartialSpecializationDecl *
getMoreSpecializedPartialSpecialization(VarTemplatePartialSpecializationDecl * PS1,VarTemplatePartialSpecializationDecl * PS2,SourceLocation Loc)4630f4a2713aSLionel Sambuc Sema::getMoreSpecializedPartialSpecialization(
4631f4a2713aSLionel Sambuc     VarTemplatePartialSpecializationDecl *PS1,
4632f4a2713aSLionel Sambuc     VarTemplatePartialSpecializationDecl *PS2, SourceLocation Loc) {
4633f4a2713aSLionel Sambuc   SmallVector<DeducedTemplateArgument, 4> Deduced;
4634f4a2713aSLionel Sambuc   TemplateDeductionInfo Info(Loc);
4635f4a2713aSLionel Sambuc 
4636*0a6a1f1dSLionel Sambuc   assert(PS1->getSpecializedTemplate() == PS2->getSpecializedTemplate() &&
4637f4a2713aSLionel Sambuc          "the partial specializations being compared should specialize"
4638f4a2713aSLionel Sambuc          " the same template.");
4639f4a2713aSLionel Sambuc   TemplateName Name(PS1->getSpecializedTemplate());
4640f4a2713aSLionel Sambuc   TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4641f4a2713aSLionel Sambuc   QualType PT1 = Context.getTemplateSpecializationType(
4642f4a2713aSLionel Sambuc       CanonTemplate, PS1->getTemplateArgs().data(),
4643f4a2713aSLionel Sambuc       PS1->getTemplateArgs().size());
4644f4a2713aSLionel Sambuc   QualType PT2 = Context.getTemplateSpecializationType(
4645f4a2713aSLionel Sambuc       CanonTemplate, PS2->getTemplateArgs().data(),
4646f4a2713aSLionel Sambuc       PS2->getTemplateArgs().size());
4647f4a2713aSLionel Sambuc 
4648f4a2713aSLionel Sambuc   // Determine whether PS1 is at least as specialized as PS2
4649f4a2713aSLionel Sambuc   Deduced.resize(PS2->getTemplateParameters()->size());
4650f4a2713aSLionel Sambuc   bool Better1 = !DeduceTemplateArgumentsByTypeMatch(
4651f4a2713aSLionel Sambuc       *this, PS2->getTemplateParameters(), PT2, PT1, Info, Deduced, TDF_None,
4652f4a2713aSLionel Sambuc       /*PartialOrdering=*/true,
4653*0a6a1f1dSLionel Sambuc       /*RefParamComparisons=*/nullptr);
4654f4a2713aSLionel Sambuc   if (Better1) {
4655f4a2713aSLionel Sambuc     SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(),
4656f4a2713aSLionel Sambuc                                                  Deduced.end());
4657*0a6a1f1dSLionel Sambuc     InstantiatingTemplate Inst(*this, Loc, PS2, DeducedArgs, Info);
4658f4a2713aSLionel Sambuc     Better1 = !::FinishTemplateArgumentDeduction(*this, PS2,
4659f4a2713aSLionel Sambuc                                                  PS1->getTemplateArgs(),
4660f4a2713aSLionel Sambuc                                                  Deduced, Info);
4661f4a2713aSLionel Sambuc   }
4662f4a2713aSLionel Sambuc 
4663f4a2713aSLionel Sambuc   // Determine whether PS2 is at least as specialized as PS1
4664f4a2713aSLionel Sambuc   Deduced.clear();
4665f4a2713aSLionel Sambuc   Deduced.resize(PS1->getTemplateParameters()->size());
4666f4a2713aSLionel Sambuc   bool Better2 = !DeduceTemplateArgumentsByTypeMatch(*this,
4667f4a2713aSLionel Sambuc                                             PS1->getTemplateParameters(),
4668f4a2713aSLionel Sambuc                                             PT1, PT2, Info, Deduced, TDF_None,
4669f4a2713aSLionel Sambuc                                             /*PartialOrdering=*/true,
4670*0a6a1f1dSLionel Sambuc                                             /*RefParamComparisons=*/nullptr);
4671f4a2713aSLionel Sambuc   if (Better2) {
4672f4a2713aSLionel Sambuc     SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(),Deduced.end());
4673*0a6a1f1dSLionel Sambuc     InstantiatingTemplate Inst(*this, Loc, PS1, DeducedArgs, Info);
4674f4a2713aSLionel Sambuc     Better2 = !::FinishTemplateArgumentDeduction(*this, PS1,
4675f4a2713aSLionel Sambuc                                                  PS2->getTemplateArgs(),
4676f4a2713aSLionel Sambuc                                                  Deduced, Info);
4677f4a2713aSLionel Sambuc   }
4678f4a2713aSLionel Sambuc 
4679f4a2713aSLionel Sambuc   if (Better1 == Better2)
4680*0a6a1f1dSLionel Sambuc     return nullptr;
4681f4a2713aSLionel Sambuc 
4682f4a2713aSLionel Sambuc   return Better1? PS1 : PS2;
4683f4a2713aSLionel Sambuc }
4684f4a2713aSLionel Sambuc 
4685f4a2713aSLionel Sambuc static void
4686f4a2713aSLionel Sambuc MarkUsedTemplateParameters(ASTContext &Ctx,
4687f4a2713aSLionel Sambuc                            const TemplateArgument &TemplateArg,
4688f4a2713aSLionel Sambuc                            bool OnlyDeduced,
4689f4a2713aSLionel Sambuc                            unsigned Depth,
4690f4a2713aSLionel Sambuc                            llvm::SmallBitVector &Used);
4691f4a2713aSLionel Sambuc 
4692f4a2713aSLionel Sambuc /// \brief Mark the template parameters that are used by the given
4693f4a2713aSLionel Sambuc /// expression.
4694f4a2713aSLionel Sambuc static void
MarkUsedTemplateParameters(ASTContext & Ctx,const Expr * E,bool OnlyDeduced,unsigned Depth,llvm::SmallBitVector & Used)4695f4a2713aSLionel Sambuc MarkUsedTemplateParameters(ASTContext &Ctx,
4696f4a2713aSLionel Sambuc                            const Expr *E,
4697f4a2713aSLionel Sambuc                            bool OnlyDeduced,
4698f4a2713aSLionel Sambuc                            unsigned Depth,
4699f4a2713aSLionel Sambuc                            llvm::SmallBitVector &Used) {
4700f4a2713aSLionel Sambuc   // We can deduce from a pack expansion.
4701f4a2713aSLionel Sambuc   if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E))
4702f4a2713aSLionel Sambuc     E = Expansion->getPattern();
4703f4a2713aSLionel Sambuc 
4704f4a2713aSLionel Sambuc   // Skip through any implicit casts we added while type-checking, and any
4705f4a2713aSLionel Sambuc   // substitutions performed by template alias expansion.
4706f4a2713aSLionel Sambuc   while (1) {
4707f4a2713aSLionel Sambuc     if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
4708f4a2713aSLionel Sambuc       E = ICE->getSubExpr();
4709f4a2713aSLionel Sambuc     else if (const SubstNonTypeTemplateParmExpr *Subst =
4710f4a2713aSLionel Sambuc                dyn_cast<SubstNonTypeTemplateParmExpr>(E))
4711f4a2713aSLionel Sambuc       E = Subst->getReplacement();
4712f4a2713aSLionel Sambuc     else
4713f4a2713aSLionel Sambuc       break;
4714f4a2713aSLionel Sambuc   }
4715f4a2713aSLionel Sambuc 
4716f4a2713aSLionel Sambuc   // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
4717f4a2713aSLionel Sambuc   // find other occurrences of template parameters.
4718f4a2713aSLionel Sambuc   const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
4719f4a2713aSLionel Sambuc   if (!DRE)
4720f4a2713aSLionel Sambuc     return;
4721f4a2713aSLionel Sambuc 
4722f4a2713aSLionel Sambuc   const NonTypeTemplateParmDecl *NTTP
4723f4a2713aSLionel Sambuc     = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
4724f4a2713aSLionel Sambuc   if (!NTTP)
4725f4a2713aSLionel Sambuc     return;
4726f4a2713aSLionel Sambuc 
4727f4a2713aSLionel Sambuc   if (NTTP->getDepth() == Depth)
4728f4a2713aSLionel Sambuc     Used[NTTP->getIndex()] = true;
4729f4a2713aSLionel Sambuc }
4730f4a2713aSLionel Sambuc 
4731f4a2713aSLionel Sambuc /// \brief Mark the template parameters that are used by the given
4732f4a2713aSLionel Sambuc /// nested name specifier.
4733f4a2713aSLionel Sambuc static void
MarkUsedTemplateParameters(ASTContext & Ctx,NestedNameSpecifier * NNS,bool OnlyDeduced,unsigned Depth,llvm::SmallBitVector & Used)4734f4a2713aSLionel Sambuc MarkUsedTemplateParameters(ASTContext &Ctx,
4735f4a2713aSLionel Sambuc                            NestedNameSpecifier *NNS,
4736f4a2713aSLionel Sambuc                            bool OnlyDeduced,
4737f4a2713aSLionel Sambuc                            unsigned Depth,
4738f4a2713aSLionel Sambuc                            llvm::SmallBitVector &Used) {
4739f4a2713aSLionel Sambuc   if (!NNS)
4740f4a2713aSLionel Sambuc     return;
4741f4a2713aSLionel Sambuc 
4742f4a2713aSLionel Sambuc   MarkUsedTemplateParameters(Ctx, NNS->getPrefix(), OnlyDeduced, Depth,
4743f4a2713aSLionel Sambuc                              Used);
4744f4a2713aSLionel Sambuc   MarkUsedTemplateParameters(Ctx, QualType(NNS->getAsType(), 0),
4745f4a2713aSLionel Sambuc                              OnlyDeduced, Depth, Used);
4746f4a2713aSLionel Sambuc }
4747f4a2713aSLionel Sambuc 
4748f4a2713aSLionel Sambuc /// \brief Mark the template parameters that are used by the given
4749f4a2713aSLionel Sambuc /// template name.
4750f4a2713aSLionel Sambuc static void
MarkUsedTemplateParameters(ASTContext & Ctx,TemplateName Name,bool OnlyDeduced,unsigned Depth,llvm::SmallBitVector & Used)4751f4a2713aSLionel Sambuc MarkUsedTemplateParameters(ASTContext &Ctx,
4752f4a2713aSLionel Sambuc                            TemplateName Name,
4753f4a2713aSLionel Sambuc                            bool OnlyDeduced,
4754f4a2713aSLionel Sambuc                            unsigned Depth,
4755f4a2713aSLionel Sambuc                            llvm::SmallBitVector &Used) {
4756f4a2713aSLionel Sambuc   if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
4757f4a2713aSLionel Sambuc     if (TemplateTemplateParmDecl *TTP
4758f4a2713aSLionel Sambuc           = dyn_cast<TemplateTemplateParmDecl>(Template)) {
4759f4a2713aSLionel Sambuc       if (TTP->getDepth() == Depth)
4760f4a2713aSLionel Sambuc         Used[TTP->getIndex()] = true;
4761f4a2713aSLionel Sambuc     }
4762f4a2713aSLionel Sambuc     return;
4763f4a2713aSLionel Sambuc   }
4764f4a2713aSLionel Sambuc 
4765f4a2713aSLionel Sambuc   if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
4766f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx, QTN->getQualifier(), OnlyDeduced,
4767f4a2713aSLionel Sambuc                                Depth, Used);
4768f4a2713aSLionel Sambuc   if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
4769f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx, DTN->getQualifier(), OnlyDeduced,
4770f4a2713aSLionel Sambuc                                Depth, Used);
4771f4a2713aSLionel Sambuc }
4772f4a2713aSLionel Sambuc 
4773f4a2713aSLionel Sambuc /// \brief Mark the template parameters that are used by the given
4774f4a2713aSLionel Sambuc /// type.
4775f4a2713aSLionel Sambuc static void
MarkUsedTemplateParameters(ASTContext & Ctx,QualType T,bool OnlyDeduced,unsigned Depth,llvm::SmallBitVector & Used)4776f4a2713aSLionel Sambuc MarkUsedTemplateParameters(ASTContext &Ctx, QualType T,
4777f4a2713aSLionel Sambuc                            bool OnlyDeduced,
4778f4a2713aSLionel Sambuc                            unsigned Depth,
4779f4a2713aSLionel Sambuc                            llvm::SmallBitVector &Used) {
4780f4a2713aSLionel Sambuc   if (T.isNull())
4781f4a2713aSLionel Sambuc     return;
4782f4a2713aSLionel Sambuc 
4783f4a2713aSLionel Sambuc   // Non-dependent types have nothing deducible
4784f4a2713aSLionel Sambuc   if (!T->isDependentType())
4785f4a2713aSLionel Sambuc     return;
4786f4a2713aSLionel Sambuc 
4787f4a2713aSLionel Sambuc   T = Ctx.getCanonicalType(T);
4788f4a2713aSLionel Sambuc   switch (T->getTypeClass()) {
4789f4a2713aSLionel Sambuc   case Type::Pointer:
4790f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx,
4791f4a2713aSLionel Sambuc                                cast<PointerType>(T)->getPointeeType(),
4792f4a2713aSLionel Sambuc                                OnlyDeduced,
4793f4a2713aSLionel Sambuc                                Depth,
4794f4a2713aSLionel Sambuc                                Used);
4795f4a2713aSLionel Sambuc     break;
4796f4a2713aSLionel Sambuc 
4797f4a2713aSLionel Sambuc   case Type::BlockPointer:
4798f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx,
4799f4a2713aSLionel Sambuc                                cast<BlockPointerType>(T)->getPointeeType(),
4800f4a2713aSLionel Sambuc                                OnlyDeduced,
4801f4a2713aSLionel Sambuc                                Depth,
4802f4a2713aSLionel Sambuc                                Used);
4803f4a2713aSLionel Sambuc     break;
4804f4a2713aSLionel Sambuc 
4805f4a2713aSLionel Sambuc   case Type::LValueReference:
4806f4a2713aSLionel Sambuc   case Type::RValueReference:
4807f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx,
4808f4a2713aSLionel Sambuc                                cast<ReferenceType>(T)->getPointeeType(),
4809f4a2713aSLionel Sambuc                                OnlyDeduced,
4810f4a2713aSLionel Sambuc                                Depth,
4811f4a2713aSLionel Sambuc                                Used);
4812f4a2713aSLionel Sambuc     break;
4813f4a2713aSLionel Sambuc 
4814f4a2713aSLionel Sambuc   case Type::MemberPointer: {
4815f4a2713aSLionel Sambuc     const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
4816f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx, MemPtr->getPointeeType(), OnlyDeduced,
4817f4a2713aSLionel Sambuc                                Depth, Used);
4818f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx, QualType(MemPtr->getClass(), 0),
4819f4a2713aSLionel Sambuc                                OnlyDeduced, Depth, Used);
4820f4a2713aSLionel Sambuc     break;
4821f4a2713aSLionel Sambuc   }
4822f4a2713aSLionel Sambuc 
4823f4a2713aSLionel Sambuc   case Type::DependentSizedArray:
4824f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx,
4825f4a2713aSLionel Sambuc                                cast<DependentSizedArrayType>(T)->getSizeExpr(),
4826f4a2713aSLionel Sambuc                                OnlyDeduced, Depth, Used);
4827f4a2713aSLionel Sambuc     // Fall through to check the element type
4828f4a2713aSLionel Sambuc 
4829f4a2713aSLionel Sambuc   case Type::ConstantArray:
4830f4a2713aSLionel Sambuc   case Type::IncompleteArray:
4831f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx,
4832f4a2713aSLionel Sambuc                                cast<ArrayType>(T)->getElementType(),
4833f4a2713aSLionel Sambuc                                OnlyDeduced, Depth, Used);
4834f4a2713aSLionel Sambuc     break;
4835f4a2713aSLionel Sambuc 
4836f4a2713aSLionel Sambuc   case Type::Vector:
4837f4a2713aSLionel Sambuc   case Type::ExtVector:
4838f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx,
4839f4a2713aSLionel Sambuc                                cast<VectorType>(T)->getElementType(),
4840f4a2713aSLionel Sambuc                                OnlyDeduced, Depth, Used);
4841f4a2713aSLionel Sambuc     break;
4842f4a2713aSLionel Sambuc 
4843f4a2713aSLionel Sambuc   case Type::DependentSizedExtVector: {
4844f4a2713aSLionel Sambuc     const DependentSizedExtVectorType *VecType
4845f4a2713aSLionel Sambuc       = cast<DependentSizedExtVectorType>(T);
4846f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced,
4847f4a2713aSLionel Sambuc                                Depth, Used);
4848f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced,
4849f4a2713aSLionel Sambuc                                Depth, Used);
4850f4a2713aSLionel Sambuc     break;
4851f4a2713aSLionel Sambuc   }
4852f4a2713aSLionel Sambuc 
4853f4a2713aSLionel Sambuc   case Type::FunctionProto: {
4854f4a2713aSLionel Sambuc     const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
4855*0a6a1f1dSLionel Sambuc     MarkUsedTemplateParameters(Ctx, Proto->getReturnType(), OnlyDeduced, Depth,
4856*0a6a1f1dSLionel Sambuc                                Used);
4857*0a6a1f1dSLionel Sambuc     for (unsigned I = 0, N = Proto->getNumParams(); I != N; ++I)
4858*0a6a1f1dSLionel Sambuc       MarkUsedTemplateParameters(Ctx, Proto->getParamType(I), OnlyDeduced,
4859f4a2713aSLionel Sambuc                                  Depth, Used);
4860f4a2713aSLionel Sambuc     break;
4861f4a2713aSLionel Sambuc   }
4862f4a2713aSLionel Sambuc 
4863f4a2713aSLionel Sambuc   case Type::TemplateTypeParm: {
4864f4a2713aSLionel Sambuc     const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
4865f4a2713aSLionel Sambuc     if (TTP->getDepth() == Depth)
4866f4a2713aSLionel Sambuc       Used[TTP->getIndex()] = true;
4867f4a2713aSLionel Sambuc     break;
4868f4a2713aSLionel Sambuc   }
4869f4a2713aSLionel Sambuc 
4870f4a2713aSLionel Sambuc   case Type::SubstTemplateTypeParmPack: {
4871f4a2713aSLionel Sambuc     const SubstTemplateTypeParmPackType *Subst
4872f4a2713aSLionel Sambuc       = cast<SubstTemplateTypeParmPackType>(T);
4873f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx,
4874f4a2713aSLionel Sambuc                                QualType(Subst->getReplacedParameter(), 0),
4875f4a2713aSLionel Sambuc                                OnlyDeduced, Depth, Used);
4876f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx, Subst->getArgumentPack(),
4877f4a2713aSLionel Sambuc                                OnlyDeduced, Depth, Used);
4878f4a2713aSLionel Sambuc     break;
4879f4a2713aSLionel Sambuc   }
4880f4a2713aSLionel Sambuc 
4881f4a2713aSLionel Sambuc   case Type::InjectedClassName:
4882f4a2713aSLionel Sambuc     T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
4883f4a2713aSLionel Sambuc     // fall through
4884f4a2713aSLionel Sambuc 
4885f4a2713aSLionel Sambuc   case Type::TemplateSpecialization: {
4886f4a2713aSLionel Sambuc     const TemplateSpecializationType *Spec
4887f4a2713aSLionel Sambuc       = cast<TemplateSpecializationType>(T);
4888f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx, Spec->getTemplateName(), OnlyDeduced,
4889f4a2713aSLionel Sambuc                                Depth, Used);
4890f4a2713aSLionel Sambuc 
4891f4a2713aSLionel Sambuc     // C++0x [temp.deduct.type]p9:
4892*0a6a1f1dSLionel Sambuc     //   If the template argument list of P contains a pack expansion that is
4893*0a6a1f1dSLionel Sambuc     //   not the last template argument, the entire template argument list is a
4894f4a2713aSLionel Sambuc     //   non-deduced context.
4895f4a2713aSLionel Sambuc     if (OnlyDeduced &&
4896f4a2713aSLionel Sambuc         hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs()))
4897f4a2713aSLionel Sambuc       break;
4898f4a2713aSLionel Sambuc 
4899f4a2713aSLionel Sambuc     for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
4900f4a2713aSLionel Sambuc       MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth,
4901f4a2713aSLionel Sambuc                                  Used);
4902f4a2713aSLionel Sambuc     break;
4903f4a2713aSLionel Sambuc   }
4904f4a2713aSLionel Sambuc 
4905f4a2713aSLionel Sambuc   case Type::Complex:
4906f4a2713aSLionel Sambuc     if (!OnlyDeduced)
4907f4a2713aSLionel Sambuc       MarkUsedTemplateParameters(Ctx,
4908f4a2713aSLionel Sambuc                                  cast<ComplexType>(T)->getElementType(),
4909f4a2713aSLionel Sambuc                                  OnlyDeduced, Depth, Used);
4910f4a2713aSLionel Sambuc     break;
4911f4a2713aSLionel Sambuc 
4912f4a2713aSLionel Sambuc   case Type::Atomic:
4913f4a2713aSLionel Sambuc     if (!OnlyDeduced)
4914f4a2713aSLionel Sambuc       MarkUsedTemplateParameters(Ctx,
4915f4a2713aSLionel Sambuc                                  cast<AtomicType>(T)->getValueType(),
4916f4a2713aSLionel Sambuc                                  OnlyDeduced, Depth, Used);
4917f4a2713aSLionel Sambuc     break;
4918f4a2713aSLionel Sambuc 
4919f4a2713aSLionel Sambuc   case Type::DependentName:
4920f4a2713aSLionel Sambuc     if (!OnlyDeduced)
4921f4a2713aSLionel Sambuc       MarkUsedTemplateParameters(Ctx,
4922f4a2713aSLionel Sambuc                                  cast<DependentNameType>(T)->getQualifier(),
4923f4a2713aSLionel Sambuc                                  OnlyDeduced, Depth, Used);
4924f4a2713aSLionel Sambuc     break;
4925f4a2713aSLionel Sambuc 
4926f4a2713aSLionel Sambuc   case Type::DependentTemplateSpecialization: {
4927f4a2713aSLionel Sambuc     const DependentTemplateSpecializationType *Spec
4928f4a2713aSLionel Sambuc       = cast<DependentTemplateSpecializationType>(T);
4929f4a2713aSLionel Sambuc     if (!OnlyDeduced)
4930f4a2713aSLionel Sambuc       MarkUsedTemplateParameters(Ctx, Spec->getQualifier(),
4931f4a2713aSLionel Sambuc                                  OnlyDeduced, Depth, Used);
4932f4a2713aSLionel Sambuc 
4933f4a2713aSLionel Sambuc     // C++0x [temp.deduct.type]p9:
4934f4a2713aSLionel Sambuc     //   If the template argument list of P contains a pack expansion that is not
4935f4a2713aSLionel Sambuc     //   the last template argument, the entire template argument list is a
4936f4a2713aSLionel Sambuc     //   non-deduced context.
4937f4a2713aSLionel Sambuc     if (OnlyDeduced &&
4938f4a2713aSLionel Sambuc         hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs()))
4939f4a2713aSLionel Sambuc       break;
4940f4a2713aSLionel Sambuc 
4941f4a2713aSLionel Sambuc     for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
4942f4a2713aSLionel Sambuc       MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth,
4943f4a2713aSLionel Sambuc                                  Used);
4944f4a2713aSLionel Sambuc     break;
4945f4a2713aSLionel Sambuc   }
4946f4a2713aSLionel Sambuc 
4947f4a2713aSLionel Sambuc   case Type::TypeOf:
4948f4a2713aSLionel Sambuc     if (!OnlyDeduced)
4949f4a2713aSLionel Sambuc       MarkUsedTemplateParameters(Ctx,
4950f4a2713aSLionel Sambuc                                  cast<TypeOfType>(T)->getUnderlyingType(),
4951f4a2713aSLionel Sambuc                                  OnlyDeduced, Depth, Used);
4952f4a2713aSLionel Sambuc     break;
4953f4a2713aSLionel Sambuc 
4954f4a2713aSLionel Sambuc   case Type::TypeOfExpr:
4955f4a2713aSLionel Sambuc     if (!OnlyDeduced)
4956f4a2713aSLionel Sambuc       MarkUsedTemplateParameters(Ctx,
4957f4a2713aSLionel Sambuc                                  cast<TypeOfExprType>(T)->getUnderlyingExpr(),
4958f4a2713aSLionel Sambuc                                  OnlyDeduced, Depth, Used);
4959f4a2713aSLionel Sambuc     break;
4960f4a2713aSLionel Sambuc 
4961f4a2713aSLionel Sambuc   case Type::Decltype:
4962f4a2713aSLionel Sambuc     if (!OnlyDeduced)
4963f4a2713aSLionel Sambuc       MarkUsedTemplateParameters(Ctx,
4964f4a2713aSLionel Sambuc                                  cast<DecltypeType>(T)->getUnderlyingExpr(),
4965f4a2713aSLionel Sambuc                                  OnlyDeduced, Depth, Used);
4966f4a2713aSLionel Sambuc     break;
4967f4a2713aSLionel Sambuc 
4968f4a2713aSLionel Sambuc   case Type::UnaryTransform:
4969f4a2713aSLionel Sambuc     if (!OnlyDeduced)
4970f4a2713aSLionel Sambuc       MarkUsedTemplateParameters(Ctx,
4971f4a2713aSLionel Sambuc                                cast<UnaryTransformType>(T)->getUnderlyingType(),
4972f4a2713aSLionel Sambuc                                  OnlyDeduced, Depth, Used);
4973f4a2713aSLionel Sambuc     break;
4974f4a2713aSLionel Sambuc 
4975f4a2713aSLionel Sambuc   case Type::PackExpansion:
4976f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx,
4977f4a2713aSLionel Sambuc                                cast<PackExpansionType>(T)->getPattern(),
4978f4a2713aSLionel Sambuc                                OnlyDeduced, Depth, Used);
4979f4a2713aSLionel Sambuc     break;
4980f4a2713aSLionel Sambuc 
4981f4a2713aSLionel Sambuc   case Type::Auto:
4982f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx,
4983f4a2713aSLionel Sambuc                                cast<AutoType>(T)->getDeducedType(),
4984f4a2713aSLionel Sambuc                                OnlyDeduced, Depth, Used);
4985f4a2713aSLionel Sambuc 
4986f4a2713aSLionel Sambuc   // None of these types have any template parameters in them.
4987f4a2713aSLionel Sambuc   case Type::Builtin:
4988f4a2713aSLionel Sambuc   case Type::VariableArray:
4989f4a2713aSLionel Sambuc   case Type::FunctionNoProto:
4990f4a2713aSLionel Sambuc   case Type::Record:
4991f4a2713aSLionel Sambuc   case Type::Enum:
4992f4a2713aSLionel Sambuc   case Type::ObjCInterface:
4993f4a2713aSLionel Sambuc   case Type::ObjCObject:
4994f4a2713aSLionel Sambuc   case Type::ObjCObjectPointer:
4995f4a2713aSLionel Sambuc   case Type::UnresolvedUsing:
4996f4a2713aSLionel Sambuc #define TYPE(Class, Base)
4997f4a2713aSLionel Sambuc #define ABSTRACT_TYPE(Class, Base)
4998f4a2713aSLionel Sambuc #define DEPENDENT_TYPE(Class, Base)
4999f4a2713aSLionel Sambuc #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
5000f4a2713aSLionel Sambuc #include "clang/AST/TypeNodes.def"
5001f4a2713aSLionel Sambuc     break;
5002f4a2713aSLionel Sambuc   }
5003f4a2713aSLionel Sambuc }
5004f4a2713aSLionel Sambuc 
5005f4a2713aSLionel Sambuc /// \brief Mark the template parameters that are used by this
5006f4a2713aSLionel Sambuc /// template argument.
5007f4a2713aSLionel Sambuc static void
MarkUsedTemplateParameters(ASTContext & Ctx,const TemplateArgument & TemplateArg,bool OnlyDeduced,unsigned Depth,llvm::SmallBitVector & Used)5008f4a2713aSLionel Sambuc MarkUsedTemplateParameters(ASTContext &Ctx,
5009f4a2713aSLionel Sambuc                            const TemplateArgument &TemplateArg,
5010f4a2713aSLionel Sambuc                            bool OnlyDeduced,
5011f4a2713aSLionel Sambuc                            unsigned Depth,
5012f4a2713aSLionel Sambuc                            llvm::SmallBitVector &Used) {
5013f4a2713aSLionel Sambuc   switch (TemplateArg.getKind()) {
5014f4a2713aSLionel Sambuc   case TemplateArgument::Null:
5015f4a2713aSLionel Sambuc   case TemplateArgument::Integral:
5016f4a2713aSLionel Sambuc   case TemplateArgument::Declaration:
5017f4a2713aSLionel Sambuc     break;
5018f4a2713aSLionel Sambuc 
5019f4a2713aSLionel Sambuc   case TemplateArgument::NullPtr:
5020f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx, TemplateArg.getNullPtrType(), OnlyDeduced,
5021f4a2713aSLionel Sambuc                                Depth, Used);
5022f4a2713aSLionel Sambuc     break;
5023f4a2713aSLionel Sambuc 
5024f4a2713aSLionel Sambuc   case TemplateArgument::Type:
5025f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx, TemplateArg.getAsType(), OnlyDeduced,
5026f4a2713aSLionel Sambuc                                Depth, Used);
5027f4a2713aSLionel Sambuc     break;
5028f4a2713aSLionel Sambuc 
5029f4a2713aSLionel Sambuc   case TemplateArgument::Template:
5030f4a2713aSLionel Sambuc   case TemplateArgument::TemplateExpansion:
5031f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx,
5032f4a2713aSLionel Sambuc                                TemplateArg.getAsTemplateOrTemplatePattern(),
5033f4a2713aSLionel Sambuc                                OnlyDeduced, Depth, Used);
5034f4a2713aSLionel Sambuc     break;
5035f4a2713aSLionel Sambuc 
5036f4a2713aSLionel Sambuc   case TemplateArgument::Expression:
5037f4a2713aSLionel Sambuc     MarkUsedTemplateParameters(Ctx, TemplateArg.getAsExpr(), OnlyDeduced,
5038f4a2713aSLionel Sambuc                                Depth, Used);
5039f4a2713aSLionel Sambuc     break;
5040f4a2713aSLionel Sambuc 
5041f4a2713aSLionel Sambuc   case TemplateArgument::Pack:
5042*0a6a1f1dSLionel Sambuc     for (const auto &P : TemplateArg.pack_elements())
5043*0a6a1f1dSLionel Sambuc       MarkUsedTemplateParameters(Ctx, P, OnlyDeduced, Depth, Used);
5044f4a2713aSLionel Sambuc     break;
5045f4a2713aSLionel Sambuc   }
5046f4a2713aSLionel Sambuc }
5047f4a2713aSLionel Sambuc 
5048f4a2713aSLionel Sambuc /// \brief Mark which template parameters can be deduced from a given
5049f4a2713aSLionel Sambuc /// template argument list.
5050f4a2713aSLionel Sambuc ///
5051f4a2713aSLionel Sambuc /// \param TemplateArgs the template argument list from which template
5052f4a2713aSLionel Sambuc /// parameters will be deduced.
5053f4a2713aSLionel Sambuc ///
5054f4a2713aSLionel Sambuc /// \param Used a bit vector whose elements will be set to \c true
5055f4a2713aSLionel Sambuc /// to indicate when the corresponding template parameter will be
5056f4a2713aSLionel Sambuc /// deduced.
5057f4a2713aSLionel Sambuc void
MarkUsedTemplateParameters(const TemplateArgumentList & TemplateArgs,bool OnlyDeduced,unsigned Depth,llvm::SmallBitVector & Used)5058f4a2713aSLionel Sambuc Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
5059f4a2713aSLionel Sambuc                                  bool OnlyDeduced, unsigned Depth,
5060f4a2713aSLionel Sambuc                                  llvm::SmallBitVector &Used) {
5061f4a2713aSLionel Sambuc   // C++0x [temp.deduct.type]p9:
5062f4a2713aSLionel Sambuc   //   If the template argument list of P contains a pack expansion that is not
5063f4a2713aSLionel Sambuc   //   the last template argument, the entire template argument list is a
5064f4a2713aSLionel Sambuc   //   non-deduced context.
5065f4a2713aSLionel Sambuc   if (OnlyDeduced &&
5066f4a2713aSLionel Sambuc       hasPackExpansionBeforeEnd(TemplateArgs.data(), TemplateArgs.size()))
5067f4a2713aSLionel Sambuc     return;
5068f4a2713aSLionel Sambuc 
5069f4a2713aSLionel Sambuc   for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
5070f4a2713aSLionel Sambuc     ::MarkUsedTemplateParameters(Context, TemplateArgs[I], OnlyDeduced,
5071f4a2713aSLionel Sambuc                                  Depth, Used);
5072f4a2713aSLionel Sambuc }
5073f4a2713aSLionel Sambuc 
5074f4a2713aSLionel Sambuc /// \brief Marks all of the template parameters that will be deduced by a
5075f4a2713aSLionel Sambuc /// call to the given function template.
MarkDeducedTemplateParameters(ASTContext & Ctx,const FunctionTemplateDecl * FunctionTemplate,llvm::SmallBitVector & Deduced)5076*0a6a1f1dSLionel Sambuc void Sema::MarkDeducedTemplateParameters(
5077*0a6a1f1dSLionel Sambuc     ASTContext &Ctx, const FunctionTemplateDecl *FunctionTemplate,
5078f4a2713aSLionel Sambuc     llvm::SmallBitVector &Deduced) {
5079f4a2713aSLionel Sambuc   TemplateParameterList *TemplateParams
5080f4a2713aSLionel Sambuc     = FunctionTemplate->getTemplateParameters();
5081f4a2713aSLionel Sambuc   Deduced.clear();
5082f4a2713aSLionel Sambuc   Deduced.resize(TemplateParams->size());
5083f4a2713aSLionel Sambuc 
5084f4a2713aSLionel Sambuc   FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
5085f4a2713aSLionel Sambuc   for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
5086f4a2713aSLionel Sambuc     ::MarkUsedTemplateParameters(Ctx, Function->getParamDecl(I)->getType(),
5087f4a2713aSLionel Sambuc                                  true, TemplateParams->getDepth(), Deduced);
5088f4a2713aSLionel Sambuc }
5089f4a2713aSLionel Sambuc 
hasDeducibleTemplateParameters(Sema & S,FunctionTemplateDecl * FunctionTemplate,QualType T)5090f4a2713aSLionel Sambuc bool hasDeducibleTemplateParameters(Sema &S,
5091f4a2713aSLionel Sambuc                                     FunctionTemplateDecl *FunctionTemplate,
5092f4a2713aSLionel Sambuc                                     QualType T) {
5093f4a2713aSLionel Sambuc   if (!T->isDependentType())
5094f4a2713aSLionel Sambuc     return false;
5095f4a2713aSLionel Sambuc 
5096f4a2713aSLionel Sambuc   TemplateParameterList *TemplateParams
5097f4a2713aSLionel Sambuc     = FunctionTemplate->getTemplateParameters();
5098f4a2713aSLionel Sambuc   llvm::SmallBitVector Deduced(TemplateParams->size());
5099f4a2713aSLionel Sambuc   ::MarkUsedTemplateParameters(S.Context, T, true, TemplateParams->getDepth(),
5100f4a2713aSLionel Sambuc                                Deduced);
5101f4a2713aSLionel Sambuc 
5102f4a2713aSLionel Sambuc   return Deduced.any();
5103f4a2713aSLionel Sambuc }
5104