blob: ef325b04edf5951fbed063e39cd4f445f14eb095 [file] [log] [blame]
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001//===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//===----------------------------------------------------------------------===/
8//
9// This file implements C++ template argument deduction.
10//
11//===----------------------------------------------------------------------===/
12
John McCall19c1bfd2010-08-25 05:32:35 +000013#include "clang/Sema/TemplateDeduction.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000014#include "TreeTransform.h"
Douglas Gregor55ca8f62009-06-04 00:03:07 +000015#include "clang/AST/ASTContext.h"
Faisal Vali571df122013-09-29 08:45:24 +000016#include "clang/AST/ASTLambda.h"
John McCallde6836a2010-08-24 07:21:54 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor55ca8f62009-06-04 00:03:07 +000018#include "clang/AST/DeclTemplate.h"
Douglas Gregor55ca8f62009-06-04 00:03:07 +000019#include "clang/AST/Expr.h"
20#include "clang/AST/ExprCXX.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/AST/StmtVisitor.h"
22#include "clang/Sema/DeclSpec.h"
23#include "clang/Sema/Sema.h"
24#include "clang/Sema/Template.h"
Benjamin Kramere0513cb2012-01-30 16:17:39 +000025#include "llvm/ADT/SmallBitVector.h"
Douglas Gregor0ff7d922009-09-14 18:39:43 +000026#include <algorithm>
Douglas Gregorcf0b47d2009-06-26 23:10:12 +000027
28namespace clang {
John McCall19c1bfd2010-08-25 05:32:35 +000029 using namespace sema;
Douglas Gregorcf0b47d2009-06-26 23:10:12 +000030 /// \brief Various flags that control template argument deduction.
31 ///
32 /// These flags can be bitwise-OR'd together.
33 enum TemplateDeductionFlags {
34 /// \brief No template argument deduction flags, which indicates the
35 /// strictest results for template argument deduction (as used for, e.g.,
36 /// matching class template partial specializations).
37 TDF_None = 0,
38 /// \brief Within template argument deduction from a function call, we are
39 /// matching with a parameter type for which the original parameter was
40 /// a reference.
41 TDF_ParamWithReferenceType = 0x1,
42 /// \brief Within template argument deduction from a function call, we
43 /// are matching in a case where we ignore cv-qualifiers.
44 TDF_IgnoreQualifiers = 0x02,
45 /// \brief Within template argument deduction from a function call,
46 /// we are matching in a case where we can perform template argument
Douglas Gregorfc516c92009-06-26 23:27:24 +000047 /// deduction from a template-id of a derived class of the argument type.
Douglas Gregor406f6342009-09-14 20:00:47 +000048 TDF_DerivedClass = 0x04,
49 /// \brief Allow non-dependent types to differ, e.g., when performing
50 /// template argument deduction from a function call where conversions
51 /// may apply.
Douglas Gregor85f240c2011-01-25 17:19:08 +000052 TDF_SkipNonDependent = 0x08,
53 /// \brief Whether we are performing template argument deduction for
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +000054 /// parameters and arguments in a top-level template argument
Douglas Gregor19a41f12013-04-17 08:45:07 +000055 TDF_TopLevelParameterTypeList = 0x10,
56 /// \brief Within template argument deduction from overload resolution per
57 /// C++ [over.over] allow matching function types that are compatible in
58 /// terms of noreturn and default calling convention adjustments.
59 TDF_InOverloadResolution = 0x20
Douglas Gregorcf0b47d2009-06-26 23:10:12 +000060 };
61}
62
Douglas Gregor55ca8f62009-06-04 00:03:07 +000063using namespace clang;
64
Douglas Gregor0a29a052010-03-26 05:50:28 +000065/// \brief Compare two APSInts, extending and switching the sign as
66/// necessary to compare their values regardless of underlying type.
67static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) {
68 if (Y.getBitWidth() > X.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +000069 X = X.extend(Y.getBitWidth());
Douglas Gregor0a29a052010-03-26 05:50:28 +000070 else if (Y.getBitWidth() < X.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +000071 Y = Y.extend(X.getBitWidth());
Douglas Gregor0a29a052010-03-26 05:50:28 +000072
73 // If there is a signedness mismatch, correct it.
74 if (X.isSigned() != Y.isSigned()) {
75 // If the signed value is negative, then the values cannot be the same.
76 if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative()))
77 return false;
78
79 Y.setIsSigned(true);
80 X.setIsSigned(true);
81 }
82
83 return X == Y;
84}
85
Douglas Gregor181aa4a2009-06-12 18:26:56 +000086static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +000087DeduceTemplateArguments(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +000088 TemplateParameterList *TemplateParams,
89 const TemplateArgument &Param,
Douglas Gregor2fcb8632011-01-11 22:21:24 +000090 TemplateArgument Arg,
John McCall19c1bfd2010-08-25 05:32:35 +000091 TemplateDeductionInfo &Info,
Craig Topper79653572013-07-08 04:13:06 +000092 SmallVectorImpl<DeducedTemplateArgument> &Deduced);
Douglas Gregor4fbe3e32009-06-09 16:35:58 +000093
Douglas Gregor63814022011-01-21 17:29:42 +000094/// \brief Whether template argument deduction for two reference parameters
95/// resulted in the argument type, parameter type, or neither type being more
96/// qualified than the other.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +000097enum DeductionQualifierComparison {
98 NeitherMoreQualified = 0,
99 ParamMoreQualified,
100 ArgMoreQualified
Douglas Gregorb837ea42011-01-11 17:34:58 +0000101};
102
Douglas Gregor63814022011-01-21 17:29:42 +0000103/// \brief Stores the result of comparing two reference parameters while
104/// performing template argument deduction for partial ordering of function
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000105/// templates.
Douglas Gregor63814022011-01-21 17:29:42 +0000106struct RefParamPartialOrderingComparison {
107 /// \brief Whether the parameter type is an rvalue reference type.
108 bool ParamIsRvalueRef;
109 /// \brief Whether the argument type is an rvalue reference type.
110 bool ArgIsRvalueRef;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000111
Douglas Gregor63814022011-01-21 17:29:42 +0000112 /// \brief Whether the parameter or argument (or neither) is more qualified.
113 DeductionQualifierComparison Qualifiers;
114};
115
116
Douglas Gregorb837ea42011-01-11 17:34:58 +0000117
Douglas Gregor7baabef2010-12-22 18:17:10 +0000118static Sema::TemplateDeductionResult
Sebastian Redlfb0b1f12012-01-17 22:49:52 +0000119DeduceTemplateArgumentsByTypeMatch(Sema &S,
120 TemplateParameterList *TemplateParams,
121 QualType Param,
122 QualType Arg,
123 TemplateDeductionInfo &Info,
124 SmallVectorImpl<DeducedTemplateArgument> &
125 Deduced,
126 unsigned TDF,
127 bool PartialOrdering = false,
128 SmallVectorImpl<RefParamPartialOrderingComparison> *
Douglas Gregor63814022011-01-21 17:29:42 +0000129 RefParamComparisons = 0);
Douglas Gregor5499af42011-01-05 23:12:31 +0000130
131static Sema::TemplateDeductionResult
132DeduceTemplateArguments(Sema &S,
133 TemplateParameterList *TemplateParams,
Douglas Gregor7baabef2010-12-22 18:17:10 +0000134 const TemplateArgument *Params, unsigned NumParams,
135 const TemplateArgument *Args, unsigned NumArgs,
136 TemplateDeductionInfo &Info,
Richard Smith16b65392012-12-06 06:44:44 +0000137 SmallVectorImpl<DeducedTemplateArgument> &Deduced);
Douglas Gregor7baabef2010-12-22 18:17:10 +0000138
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000139/// \brief If the given expression is of a form that permits the deduction
140/// of a non-type template parameter, return the declaration of that
141/// non-type template parameter.
142static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
Richard Smith7ebb07c2012-07-08 04:37:51 +0000143 // If we are within an alias template, the expression may have undergone
144 // any number of parameter substitutions already.
145 while (1) {
146 if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
147 E = IC->getSubExpr();
148 else if (SubstNonTypeTemplateParmExpr *Subst =
149 dyn_cast<SubstNonTypeTemplateParmExpr>(E))
150 E = Subst->getReplacement();
151 else
152 break;
153 }
Mike Stump11289f42009-09-09 15:08:12 +0000154
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000155 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
156 return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000157
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000158 return 0;
159}
160
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000161/// \brief Determine whether two declaration pointers refer to the same
162/// declaration.
163static bool isSameDeclaration(Decl *X, Decl *Y) {
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000164 if (NamedDecl *NX = dyn_cast<NamedDecl>(X))
165 X = NX->getUnderlyingDecl();
166 if (NamedDecl *NY = dyn_cast<NamedDecl>(Y))
167 Y = NY->getUnderlyingDecl();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000168
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000169 return X->getCanonicalDecl() == Y->getCanonicalDecl();
170}
171
172/// \brief Verify that the given, deduced template arguments are compatible.
173///
174/// \returns The deduced template argument, or a NULL template argument if
175/// the deduced template arguments were incompatible.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000176static DeducedTemplateArgument
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000177checkDeducedTemplateArguments(ASTContext &Context,
178 const DeducedTemplateArgument &X,
179 const DeducedTemplateArgument &Y) {
180 // We have no deduction for one or both of the arguments; they're compatible.
181 if (X.isNull())
182 return Y;
183 if (Y.isNull())
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000184 return X;
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000185
186 switch (X.getKind()) {
187 case TemplateArgument::Null:
188 llvm_unreachable("Non-deduced template arguments handled above");
189
190 case TemplateArgument::Type:
191 // If two template type arguments have the same type, they're compatible.
192 if (Y.getKind() == TemplateArgument::Type &&
193 Context.hasSameType(X.getAsType(), Y.getAsType()))
194 return X;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000195
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000196 return DeducedTemplateArgument();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000197
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000198 case TemplateArgument::Integral:
199 // If we deduced a constant in one case and either a dependent expression or
200 // declaration in another case, keep the integral constant.
201 // If both are integral constants with the same value, keep that value.
202 if (Y.getKind() == TemplateArgument::Expression ||
203 Y.getKind() == TemplateArgument::Declaration ||
204 (Y.getKind() == TemplateArgument::Integral &&
Benjamin Kramer6003ad52012-06-07 15:09:51 +0000205 hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral())))
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000206 return DeducedTemplateArgument(X,
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000207 X.wasDeducedFromArrayBound() &&
208 Y.wasDeducedFromArrayBound());
209
210 // All other combinations are incompatible.
211 return DeducedTemplateArgument();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000212
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000213 case TemplateArgument::Template:
214 if (Y.getKind() == TemplateArgument::Template &&
215 Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate()))
216 return X;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000217
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000218 // All other combinations are incompatible.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000219 return DeducedTemplateArgument();
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000220
221 case TemplateArgument::TemplateExpansion:
222 if (Y.getKind() == TemplateArgument::TemplateExpansion &&
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000223 Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(),
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000224 Y.getAsTemplateOrTemplatePattern()))
225 return X;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000226
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000227 // All other combinations are incompatible.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000228 return DeducedTemplateArgument();
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000229
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000230 case TemplateArgument::Expression:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000231 // If we deduced a dependent expression in one case and either an integral
232 // constant or a declaration in another case, keep the integral constant
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000233 // or declaration.
234 if (Y.getKind() == TemplateArgument::Integral ||
235 Y.getKind() == TemplateArgument::Declaration)
236 return DeducedTemplateArgument(Y, X.wasDeducedFromArrayBound() &&
237 Y.wasDeducedFromArrayBound());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000238
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000239 if (Y.getKind() == TemplateArgument::Expression) {
240 // Compare the expressions for equality
241 llvm::FoldingSetNodeID ID1, ID2;
242 X.getAsExpr()->Profile(ID1, Context, true);
243 Y.getAsExpr()->Profile(ID2, Context, true);
244 if (ID1 == ID2)
245 return X;
246 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000247
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000248 // All other combinations are incompatible.
249 return DeducedTemplateArgument();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000250
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000251 case TemplateArgument::Declaration:
252 // If we deduced a declaration and a dependent expression, keep the
253 // declaration.
254 if (Y.getKind() == TemplateArgument::Expression)
255 return X;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000256
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000257 // If we deduced a declaration and an integral constant, keep the
258 // integral constant.
259 if (Y.getKind() == TemplateArgument::Integral)
260 return Y;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000261
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000262 // If we deduced two declarations, make sure they they refer to the
263 // same declaration.
264 if (Y.getKind() == TemplateArgument::Declaration &&
Eli Friedmanb826a002012-09-26 02:36:12 +0000265 isSameDeclaration(X.getAsDecl(), Y.getAsDecl()) &&
266 X.isDeclForReferenceParam() == Y.isDeclForReferenceParam())
267 return X;
268
269 // All other combinations are incompatible.
270 return DeducedTemplateArgument();
271
272 case TemplateArgument::NullPtr:
273 // If we deduced a null pointer and a dependent expression, keep the
274 // null pointer.
275 if (Y.getKind() == TemplateArgument::Expression)
276 return X;
277
278 // If we deduced a null pointer and an integral constant, keep the
279 // integral constant.
280 if (Y.getKind() == TemplateArgument::Integral)
281 return Y;
282
283 // If we deduced two null pointers, make sure they have the same type.
284 if (Y.getKind() == TemplateArgument::NullPtr &&
285 Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()))
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000286 return X;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000287
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000288 // All other combinations are incompatible.
289 return DeducedTemplateArgument();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000290
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000291 case TemplateArgument::Pack:
292 if (Y.getKind() != TemplateArgument::Pack ||
293 X.pack_size() != Y.pack_size())
294 return DeducedTemplateArgument();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000295
296 for (TemplateArgument::pack_iterator XA = X.pack_begin(),
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000297 XAEnd = X.pack_end(),
298 YA = Y.pack_begin();
299 XA != XAEnd; ++XA, ++YA) {
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000300 if (checkDeducedTemplateArguments(Context,
301 DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()),
Douglas Gregorf491ee22011-01-05 21:00:53 +0000302 DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound()))
303 .isNull())
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000304 return DeducedTemplateArgument();
305 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000306
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000307 return X;
308 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000309
David Blaikiee4d798f2012-01-20 21:50:17 +0000310 llvm_unreachable("Invalid TemplateArgument Kind!");
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000311}
312
Mike Stump11289f42009-09-09 15:08:12 +0000313/// \brief Deduce the value of the given non-type template parameter
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000314/// from the given constant.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000315static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000316DeduceNonTypeTemplateArgument(Sema &S,
Mike Stump11289f42009-09-09 15:08:12 +0000317 NonTypeTemplateParmDecl *NTTP,
Douglas Gregor0a29a052010-03-26 05:50:28 +0000318 llvm::APSInt Value, QualType ValueType,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +0000319 bool DeducedFromArrayBound,
John McCall19c1bfd2010-08-25 05:32:35 +0000320 TemplateDeductionInfo &Info,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000321 SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump11289f42009-09-09 15:08:12 +0000322 assert(NTTP->getDepth() == 0 &&
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000323 "Cannot deduce non-type template argument with depth > 0");
Mike Stump11289f42009-09-09 15:08:12 +0000324
Benjamin Kramer6003ad52012-06-07 15:09:51 +0000325 DeducedTemplateArgument NewDeduced(S.Context, Value, ValueType,
326 DeducedFromArrayBound);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000327 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000328 Deduced[NTTP->getIndex()],
329 NewDeduced);
330 if (Result.isNull()) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000331 Info.Param = NTTP;
332 Info.FirstArg = Deduced[NTTP->getIndex()];
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000333 Info.SecondArg = NewDeduced;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000334 return Sema::TDK_Inconsistent;
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000335 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000336
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000337 Deduced[NTTP->getIndex()] = Result;
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000338 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000339}
340
Mike Stump11289f42009-09-09 15:08:12 +0000341/// \brief Deduce the value of the given non-type template parameter
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000342/// from the given type- or value-dependent expression.
343///
344/// \returns true if deduction succeeded, false otherwise.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000345static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000346DeduceNonTypeTemplateArgument(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000347 NonTypeTemplateParmDecl *NTTP,
348 Expr *Value,
John McCall19c1bfd2010-08-25 05:32:35 +0000349 TemplateDeductionInfo &Info,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000350 SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Mike Stump11289f42009-09-09 15:08:12 +0000351 assert(NTTP->getDepth() == 0 &&
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000352 "Cannot deduce non-type template argument with depth > 0");
353 assert((Value->isTypeDependent() || Value->isValueDependent()) &&
354 "Expression template argument must be type- or value-dependent.");
Mike Stump11289f42009-09-09 15:08:12 +0000355
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000356 DeducedTemplateArgument NewDeduced(Value);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000357 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
358 Deduced[NTTP->getIndex()],
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000359 NewDeduced);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000360
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000361 if (Result.isNull()) {
362 Info.Param = NTTP;
363 Info.FirstArg = Deduced[NTTP->getIndex()];
364 Info.SecondArg = NewDeduced;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000365 return Sema::TDK_Inconsistent;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000366 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000367
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000368 Deduced[NTTP->getIndex()] = Result;
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000369 return Sema::TDK_Success;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +0000370}
371
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000372/// \brief Deduce the value of the given non-type template parameter
373/// from the given declaration.
374///
375/// \returns true if deduction succeeded, false otherwise.
376static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000377DeduceNonTypeTemplateArgument(Sema &S,
Craig Topperc1bbe8d2013-07-08 04:16:49 +0000378 NonTypeTemplateParmDecl *NTTP,
379 ValueDecl *D,
380 TemplateDeductionInfo &Info,
381 SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000382 assert(NTTP->getDepth() == 0 &&
383 "Cannot deduce non-type template argument with depth > 0");
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000384
Eli Friedmanb826a002012-09-26 02:36:12 +0000385 D = D ? cast<ValueDecl>(D->getCanonicalDecl()) : 0;
386 TemplateArgument New(D, NTTP->getType()->isReferenceType());
387 DeducedTemplateArgument NewDeduced(New);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000388 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000389 Deduced[NTTP->getIndex()],
390 NewDeduced);
391 if (Result.isNull()) {
392 Info.Param = NTTP;
393 Info.FirstArg = Deduced[NTTP->getIndex()];
394 Info.SecondArg = NewDeduced;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000395 return Sema::TDK_Inconsistent;
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000396 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000397
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000398 Deduced[NTTP->getIndex()] = Result;
Douglas Gregor2bb756a2009-11-13 23:45:44 +0000399 return Sema::TDK_Success;
400}
401
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000402static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000403DeduceTemplateArguments(Sema &S,
Douglas Gregoradee3e32009-11-11 23:06:43 +0000404 TemplateParameterList *TemplateParams,
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000405 TemplateName Param,
406 TemplateName Arg,
John McCall19c1bfd2010-08-25 05:32:35 +0000407 TemplateDeductionInfo &Info,
Craig Topperc1bbe8d2013-07-08 04:16:49 +0000408 SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000409 TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
Douglas Gregoradee3e32009-11-11 23:06:43 +0000410 if (!ParamDecl) {
411 // The parameter type is dependent and is not a template template parameter,
412 // so there is nothing that we can deduce.
413 return Sema::TDK_Success;
414 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000415
Douglas Gregoradee3e32009-11-11 23:06:43 +0000416 if (TemplateTemplateParmDecl *TempParam
417 = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) {
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000418 DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg));
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000419 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000420 Deduced[TempParam->getIndex()],
421 NewDeduced);
422 if (Result.isNull()) {
423 Info.Param = TempParam;
424 Info.FirstArg = Deduced[TempParam->getIndex()];
425 Info.SecondArg = NewDeduced;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000426 return Sema::TDK_Inconsistent;
Douglas Gregoradee3e32009-11-11 23:06:43 +0000427 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000428
Douglas Gregor7f8e7682010-12-22 23:09:49 +0000429 Deduced[TempParam->getIndex()] = Result;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000430 return Sema::TDK_Success;
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000431 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000432
Douglas Gregoradee3e32009-11-11 23:06:43 +0000433 // Verify that the two template names are equivalent.
Chandler Carruthc1263112010-02-07 21:33:28 +0000434 if (S.Context.hasSameTemplateName(Param, Arg))
Douglas Gregoradee3e32009-11-11 23:06:43 +0000435 return Sema::TDK_Success;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000436
Douglas Gregoradee3e32009-11-11 23:06:43 +0000437 // Mismatch of non-dependent template parameter to argument.
438 Info.FirstArg = TemplateArgument(Param);
439 Info.SecondArg = TemplateArgument(Arg);
440 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor4fbe3e32009-06-09 16:35:58 +0000441}
442
Mike Stump11289f42009-09-09 15:08:12 +0000443/// \brief Deduce the template arguments by comparing the template parameter
Douglas Gregore81f3e72009-07-07 23:09:34 +0000444/// type (which is a template-id) with the template argument type.
445///
Chandler Carruthc1263112010-02-07 21:33:28 +0000446/// \param S the Sema
Douglas Gregore81f3e72009-07-07 23:09:34 +0000447///
448/// \param TemplateParams the template parameters that we are deducing
449///
450/// \param Param the parameter type
451///
452/// \param Arg the argument type
453///
454/// \param Info information about the template argument deduction itself
455///
456/// \param Deduced the deduced template arguments
457///
458/// \returns the result of template argument deduction so far. Note that a
459/// "success" result means that template argument deduction has not yet failed,
460/// but it may still fail, later, for other reasons.
461static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +0000462DeduceTemplateArguments(Sema &S,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000463 TemplateParameterList *TemplateParams,
464 const TemplateSpecializationType *Param,
465 QualType Arg,
John McCall19c1bfd2010-08-25 05:32:35 +0000466 TemplateDeductionInfo &Info,
Craig Topper79653572013-07-08 04:13:06 +0000467 SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
John McCallb692a092009-10-22 20:10:53 +0000468 assert(Arg.isCanonical() && "Argument type must be canonical");
Mike Stump11289f42009-09-09 15:08:12 +0000469
Douglas Gregore81f3e72009-07-07 23:09:34 +0000470 // Check whether the template argument is a dependent template-id.
Mike Stump11289f42009-09-09 15:08:12 +0000471 if (const TemplateSpecializationType *SpecArg
Douglas Gregore81f3e72009-07-07 23:09:34 +0000472 = dyn_cast<TemplateSpecializationType>(Arg)) {
473 // Perform template argument deduction for the template name.
474 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000475 = DeduceTemplateArguments(S, TemplateParams,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000476 Param->getTemplateName(),
477 SpecArg->getTemplateName(),
478 Info, Deduced))
479 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000480
Mike Stump11289f42009-09-09 15:08:12 +0000481
Douglas Gregore81f3e72009-07-07 23:09:34 +0000482 // Perform template argument deduction on each template
Douglas Gregord80ea202010-12-22 18:55:49 +0000483 // argument. Ignore any missing/extra arguments, since they could be
484 // filled in by default arguments.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000485 return DeduceTemplateArguments(S, TemplateParams,
486 Param->getArgs(), Param->getNumArgs(),
Douglas Gregord80ea202010-12-22 18:55:49 +0000487 SpecArg->getArgs(), SpecArg->getNumArgs(),
Richard Smith16b65392012-12-06 06:44:44 +0000488 Info, Deduced);
Douglas Gregore81f3e72009-07-07 23:09:34 +0000489 }
Mike Stump11289f42009-09-09 15:08:12 +0000490
Douglas Gregore81f3e72009-07-07 23:09:34 +0000491 // If the argument type is a class template specialization, we
492 // perform template argument deduction using its template
493 // arguments.
494 const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
Richard Smith44ecdbd2013-01-31 05:19:49 +0000495 if (!RecordArg) {
496 Info.FirstArg = TemplateArgument(QualType(Param, 0));
497 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregore81f3e72009-07-07 23:09:34 +0000498 return Sema::TDK_NonDeducedMismatch;
Richard Smith44ecdbd2013-01-31 05:19:49 +0000499 }
Mike Stump11289f42009-09-09 15:08:12 +0000500
501 ClassTemplateSpecializationDecl *SpecArg
Douglas Gregore81f3e72009-07-07 23:09:34 +0000502 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
Richard Smith44ecdbd2013-01-31 05:19:49 +0000503 if (!SpecArg) {
504 Info.FirstArg = TemplateArgument(QualType(Param, 0));
505 Info.SecondArg = TemplateArgument(Arg);
Douglas Gregore81f3e72009-07-07 23:09:34 +0000506 return Sema::TDK_NonDeducedMismatch;
Richard Smith44ecdbd2013-01-31 05:19:49 +0000507 }
Mike Stump11289f42009-09-09 15:08:12 +0000508
Douglas Gregore81f3e72009-07-07 23:09:34 +0000509 // Perform template argument deduction for the template name.
510 if (Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +0000511 = DeduceTemplateArguments(S,
Douglas Gregoradee3e32009-11-11 23:06:43 +0000512 TemplateParams,
Douglas Gregore81f3e72009-07-07 23:09:34 +0000513 Param->getTemplateName(),
514 TemplateName(SpecArg->getSpecializedTemplate()),
515 Info, Deduced))
516 return Result;
Mike Stump11289f42009-09-09 15:08:12 +0000517
Douglas Gregor7baabef2010-12-22 18:17:10 +0000518 // Perform template argument deduction for the template arguments.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000519 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor7baabef2010-12-22 18:17:10 +0000520 Param->getArgs(), Param->getNumArgs(),
521 SpecArg->getTemplateArgs().data(),
522 SpecArg->getTemplateArgs().size(),
523 Info, Deduced);
Douglas Gregore81f3e72009-07-07 23:09:34 +0000524}
525
John McCall08569062010-08-28 22:14:41 +0000526/// \brief Determines whether the given type is an opaque type that
527/// might be more qualified when instantiated.
528static bool IsPossiblyOpaquelyQualifiedType(QualType T) {
529 switch (T->getTypeClass()) {
530 case Type::TypeOfExpr:
531 case Type::TypeOf:
532 case Type::DependentName:
533 case Type::Decltype:
534 case Type::UnresolvedUsing:
John McCall6c9dd522011-01-18 07:41:22 +0000535 case Type::TemplateTypeParm:
John McCall08569062010-08-28 22:14:41 +0000536 return true;
537
538 case Type::ConstantArray:
539 case Type::IncompleteArray:
540 case Type::VariableArray:
541 case Type::DependentSizedArray:
542 return IsPossiblyOpaquelyQualifiedType(
543 cast<ArrayType>(T)->getElementType());
544
545 default:
546 return false;
547 }
548}
549
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000550/// \brief Retrieve the depth and index of a template parameter.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000551static std::pair<unsigned, unsigned>
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000552getDepthAndIndex(NamedDecl *ND) {
Douglas Gregor5499af42011-01-05 23:12:31 +0000553 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
554 return std::make_pair(TTP->getDepth(), TTP->getIndex());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000555
Douglas Gregor5499af42011-01-05 23:12:31 +0000556 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
557 return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000558
Douglas Gregor5499af42011-01-05 23:12:31 +0000559 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
560 return std::make_pair(TTP->getDepth(), TTP->getIndex());
561}
562
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000563/// \brief Retrieve the depth and index of an unexpanded parameter pack.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000564static std::pair<unsigned, unsigned>
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000565getDepthAndIndex(UnexpandedParameterPack UPP) {
566 if (const TemplateTypeParmType *TTP
567 = UPP.first.dyn_cast<const TemplateTypeParmType *>())
568 return std::make_pair(TTP->getDepth(), TTP->getIndex());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000569
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000570 return getDepthAndIndex(UPP.first.get<NamedDecl *>());
571}
572
Douglas Gregor5499af42011-01-05 23:12:31 +0000573/// \brief Helper function to build a TemplateParameter when we don't
574/// know its type statically.
575static TemplateParameter makeTemplateParameter(Decl *D) {
576 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
577 return TemplateParameter(TTP);
Craig Topper4b482ee2013-07-08 04:24:47 +0000578 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
Douglas Gregor5499af42011-01-05 23:12:31 +0000579 return TemplateParameter(NTTP);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000580
Douglas Gregor5499af42011-01-05 23:12:31 +0000581 return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
582}
583
Craig Topper0a4e1f52013-07-08 04:44:01 +0000584typedef SmallVector<SmallVector<DeducedTemplateArgument, 4>, 2>
585 NewlyDeducedPacksType;
586
Douglas Gregora8bd0d92011-01-10 17:35:05 +0000587/// \brief Prepare to perform template argument deduction for all of the
588/// arguments in a set of argument packs.
Craig Topper0a4e1f52013-07-08 04:44:01 +0000589static void
590PrepareArgumentPackDeduction(Sema &S,
591 SmallVectorImpl<DeducedTemplateArgument> &Deduced,
592 ArrayRef<unsigned> PackIndices,
593 SmallVectorImpl<DeducedTemplateArgument> &SavedPacks,
594 NewlyDeducedPacksType &NewlyDeducedPacks) {
Douglas Gregora8bd0d92011-01-10 17:35:05 +0000595 // Save the deduced template arguments for each parameter pack expanded
596 // by this pack expansion, then clear out the deduction.
597 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
598 // Save the previously-deduced argument pack, then clear it out so that we
599 // can deduce a new argument pack.
600 SavedPacks[I] = Deduced[PackIndices[I]];
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000601 Deduced[PackIndices[I]] = TemplateArgument();
602
Richard Smith802c4b72012-08-23 06:16:52 +0000603 if (!S.CurrentInstantiationScope)
604 continue;
605
Richard Smith1fde8ec2012-09-07 02:06:42 +0000606 // If the template argument pack was explicitly specified, add that to
Douglas Gregora8bd0d92011-01-10 17:35:05 +0000607 // the set of deduced arguments.
608 const TemplateArgument *ExplicitArgs;
609 unsigned NumExplicitArgs;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000610 if (NamedDecl *PartiallySubstitutedPack
Douglas Gregora8bd0d92011-01-10 17:35:05 +0000611 = S.CurrentInstantiationScope->getPartiallySubstitutedPack(
612 &ExplicitArgs,
613 &NumExplicitArgs)) {
614 if (getDepthAndIndex(PartiallySubstitutedPack).second == PackIndices[I])
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000615 NewlyDeducedPacks[I].append(ExplicitArgs,
Douglas Gregora8bd0d92011-01-10 17:35:05 +0000616 ExplicitArgs + NumExplicitArgs);
617 }
618 }
619}
620
Douglas Gregorb94a6172011-01-10 17:53:52 +0000621/// \brief Finish template argument deduction for a set of argument packs,
622/// producing the argument packs and checking for consistency with prior
623/// deductions.
624static Sema::TemplateDeductionResult
625FinishArgumentPackDeduction(Sema &S,
Craig Topper0a4e1f52013-07-08 04:44:01 +0000626 TemplateParameterList *TemplateParams,
627 bool HasAnyArguments,
628 SmallVectorImpl<DeducedTemplateArgument> &Deduced,
629 ArrayRef<unsigned> PackIndices,
630 SmallVectorImpl<DeducedTemplateArgument> &SavedPacks,
631 NewlyDeducedPacksType &NewlyDeducedPacks,
632 TemplateDeductionInfo &Info) {
Douglas Gregorb94a6172011-01-10 17:53:52 +0000633 // Build argument packs for each of the parameter packs expanded by this
634 // pack expansion.
635 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
636 if (HasAnyArguments && NewlyDeducedPacks[I].empty()) {
637 // We were not able to deduce anything for this parameter pack,
638 // so just restore the saved argument pack.
639 Deduced[PackIndices[I]] = SavedPacks[I];
640 continue;
641 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000642
Douglas Gregorb94a6172011-01-10 17:53:52 +0000643 DeducedTemplateArgument NewPack;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000644
Douglas Gregorb94a6172011-01-10 17:53:52 +0000645 if (NewlyDeducedPacks[I].empty()) {
646 // If we deduced an empty argument pack, create it now.
Eli Friedmanb826a002012-09-26 02:36:12 +0000647 NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack());
Douglas Gregorb94a6172011-01-10 17:53:52 +0000648 } else {
649 TemplateArgument *ArgumentPack
Douglas Gregor74c6d192011-01-11 23:09:57 +0000650 = new (S.Context) TemplateArgument [NewlyDeducedPacks[I].size()];
Douglas Gregorb94a6172011-01-10 17:53:52 +0000651 std::copy(NewlyDeducedPacks[I].begin(), NewlyDeducedPacks[I].end(),
652 ArgumentPack);
653 NewPack
Douglas Gregor74c6d192011-01-11 23:09:57 +0000654 = DeducedTemplateArgument(TemplateArgument(ArgumentPack,
655 NewlyDeducedPacks[I].size()),
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000656 NewlyDeducedPacks[I][0].wasDeducedFromArrayBound());
Douglas Gregorb94a6172011-01-10 17:53:52 +0000657 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000658
Douglas Gregorb94a6172011-01-10 17:53:52 +0000659 DeducedTemplateArgument Result
660 = checkDeducedTemplateArguments(S.Context, SavedPacks[I], NewPack);
661 if (Result.isNull()) {
662 Info.Param
663 = makeTemplateParameter(TemplateParams->getParam(PackIndices[I]));
664 Info.FirstArg = SavedPacks[I];
665 Info.SecondArg = NewPack;
666 return Sema::TDK_Inconsistent;
667 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000668
Douglas Gregorb94a6172011-01-10 17:53:52 +0000669 Deduced[PackIndices[I]] = Result;
670 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000671
Douglas Gregorb94a6172011-01-10 17:53:52 +0000672 return Sema::TDK_Success;
673}
674
Douglas Gregor5499af42011-01-05 23:12:31 +0000675/// \brief Deduce the template arguments by comparing the list of parameter
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000676/// types to the list of argument types, as in the parameter-type-lists of
677/// function types (C++ [temp.deduct.type]p10).
Douglas Gregor5499af42011-01-05 23:12:31 +0000678///
679/// \param S The semantic analysis object within which we are deducing
680///
681/// \param TemplateParams The template parameters that we are deducing
682///
683/// \param Params The list of parameter types
684///
685/// \param NumParams The number of types in \c Params
686///
687/// \param Args The list of argument types
688///
689/// \param NumArgs The number of types in \c Args
690///
691/// \param Info information about the template argument deduction itself
692///
693/// \param Deduced the deduced template arguments
694///
695/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
696/// how template argument deduction is performed.
697///
Douglas Gregorb837ea42011-01-11 17:34:58 +0000698/// \param PartialOrdering If true, we are performing template argument
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000699/// deduction for during partial ordering for a call
Douglas Gregorb837ea42011-01-11 17:34:58 +0000700/// (C++0x [temp.deduct.partial]).
701///
Douglas Gregor63814022011-01-21 17:29:42 +0000702/// \param RefParamComparisons If we're performing template argument deduction
Douglas Gregorb837ea42011-01-11 17:34:58 +0000703/// in the context of partial ordering, the set of qualifier comparisons.
704///
Douglas Gregor5499af42011-01-05 23:12:31 +0000705/// \returns the result of template argument deduction so far. Note that a
706/// "success" result means that template argument deduction has not yet failed,
707/// but it may still fail, later, for other reasons.
708static Sema::TemplateDeductionResult
709DeduceTemplateArguments(Sema &S,
710 TemplateParameterList *TemplateParams,
711 const QualType *Params, unsigned NumParams,
712 const QualType *Args, unsigned NumArgs,
713 TemplateDeductionInfo &Info,
Craig Topperc1bbe8d2013-07-08 04:16:49 +0000714 SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregorb837ea42011-01-11 17:34:58 +0000715 unsigned TDF,
716 bool PartialOrdering = false,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000717 SmallVectorImpl<RefParamPartialOrderingComparison> *
Douglas Gregor63814022011-01-21 17:29:42 +0000718 RefParamComparisons = 0) {
Douglas Gregor86bea352011-01-05 23:23:17 +0000719 // Fast-path check to see if we have too many/too few arguments.
720 if (NumParams != NumArgs &&
721 !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) &&
722 !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1])))
Richard Smith44ecdbd2013-01-31 05:19:49 +0000723 return Sema::TDK_MiscellaneousDeductionFailure;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000724
Douglas Gregor5499af42011-01-05 23:12:31 +0000725 // C++0x [temp.deduct.type]p10:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000726 // Similarly, if P has a form that contains (T), then each parameter type
727 // Pi of the respective parameter-type- list of P is compared with the
728 // corresponding parameter type Ai of the corresponding parameter-type-list
729 // of A. [...]
Douglas Gregor5499af42011-01-05 23:12:31 +0000730 unsigned ArgIdx = 0, ParamIdx = 0;
731 for (; ParamIdx != NumParams; ++ParamIdx) {
732 // Check argument types.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000733 const PackExpansionType *Expansion
Douglas Gregor5499af42011-01-05 23:12:31 +0000734 = dyn_cast<PackExpansionType>(Params[ParamIdx]);
735 if (!Expansion) {
736 // Simple case: compare the parameter and argument types at this point.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000737
Douglas Gregor5499af42011-01-05 23:12:31 +0000738 // Make sure we have an argument.
739 if (ArgIdx >= NumArgs)
Richard Smith44ecdbd2013-01-31 05:19:49 +0000740 return Sema::TDK_MiscellaneousDeductionFailure;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000741
Douglas Gregor2fcb8632011-01-11 22:21:24 +0000742 if (isa<PackExpansionType>(Args[ArgIdx])) {
743 // C++0x [temp.deduct.type]p22:
744 // If the original function parameter associated with A is a function
745 // parameter pack and the function parameter associated with P is not
746 // a function parameter pack, then template argument deduction fails.
Richard Smith44ecdbd2013-01-31 05:19:49 +0000747 return Sema::TDK_MiscellaneousDeductionFailure;
Douglas Gregor2fcb8632011-01-11 22:21:24 +0000748 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000749
Douglas Gregor5499af42011-01-05 23:12:31 +0000750 if (Sema::TemplateDeductionResult Result
Sebastian Redlfb0b1f12012-01-17 22:49:52 +0000751 = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
752 Params[ParamIdx], Args[ArgIdx],
753 Info, Deduced, TDF,
754 PartialOrdering,
755 RefParamComparisons))
Douglas Gregor5499af42011-01-05 23:12:31 +0000756 return Result;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000757
Douglas Gregor5499af42011-01-05 23:12:31 +0000758 ++ArgIdx;
759 continue;
760 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000761
Douglas Gregor0dd423e2011-01-11 01:52:23 +0000762 // C++0x [temp.deduct.type]p5:
763 // The non-deduced contexts are:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000764 // - A function parameter pack that does not occur at the end of the
Douglas Gregor0dd423e2011-01-11 01:52:23 +0000765 // parameter-declaration-clause.
766 if (ParamIdx + 1 < NumParams)
767 return Sema::TDK_Success;
768
Douglas Gregor5499af42011-01-05 23:12:31 +0000769 // C++0x [temp.deduct.type]p10:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000770 // If the parameter-declaration corresponding to Pi is a function
Douglas Gregor5499af42011-01-05 23:12:31 +0000771 // parameter pack, then the type of its declarator- id is compared with
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000772 // each remaining parameter type in the parameter-type-list of A. Each
Douglas Gregor5499af42011-01-05 23:12:31 +0000773 // comparison deduces template arguments for subsequent positions in the
774 // template parameter packs expanded by the function parameter pack.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000775
Douglas Gregor5499af42011-01-05 23:12:31 +0000776 // Compute the set of template parameter indices that correspond to
777 // parameter packs expanded by the pack expansion.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000778 SmallVector<unsigned, 2> PackIndices;
Douglas Gregor5499af42011-01-05 23:12:31 +0000779 QualType Pattern = Expansion->getPattern();
780 {
Benjamin Kramere0513cb2012-01-30 16:17:39 +0000781 llvm::SmallBitVector SawIndices(TemplateParams->size());
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000782 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor5499af42011-01-05 23:12:31 +0000783 S.collectUnexpandedParameterPacks(Pattern, Unexpanded);
784 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
785 unsigned Depth, Index;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +0000786 std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
Douglas Gregor5499af42011-01-05 23:12:31 +0000787 if (Depth == 0 && !SawIndices[Index]) {
788 SawIndices[Index] = true;
789 PackIndices.push_back(Index);
790 }
791 }
792 }
793 assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
794
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000795 // Keep track of the deduced template arguments for each parameter pack
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000796 // expanded by this pack expansion (the outer index) and for each
Douglas Gregora8bac7f2011-01-10 07:32:04 +0000797 // template argument (the inner SmallVectors).
Craig Topper0a4e1f52013-07-08 04:44:01 +0000798 NewlyDeducedPacksType NewlyDeducedPacks(PackIndices.size());
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000799 SmallVector<DeducedTemplateArgument, 2>
Douglas Gregora8bd0d92011-01-10 17:35:05 +0000800 SavedPacks(PackIndices.size());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000801 PrepareArgumentPackDeduction(S, Deduced, PackIndices, SavedPacks,
Douglas Gregora8bd0d92011-01-10 17:35:05 +0000802 NewlyDeducedPacks);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000803
Douglas Gregor5499af42011-01-05 23:12:31 +0000804 bool HasAnyArguments = false;
805 for (; ArgIdx < NumArgs; ++ArgIdx) {
806 HasAnyArguments = true;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000807
Douglas Gregor5499af42011-01-05 23:12:31 +0000808 // Deduce template arguments from the pattern.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000809 if (Sema::TemplateDeductionResult Result
Sebastian Redlfb0b1f12012-01-17 22:49:52 +0000810 = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, Pattern,
811 Args[ArgIdx], Info, Deduced,
812 TDF, PartialOrdering,
813 RefParamComparisons))
Douglas Gregor5499af42011-01-05 23:12:31 +0000814 return Result;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000815
Douglas Gregor5499af42011-01-05 23:12:31 +0000816 // Capture the deduced template arguments for each parameter pack expanded
817 // by this pack expansion, add them to the list of arguments we've deduced
818 // for that pack, then clear out the deduced argument.
819 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
820 DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
821 if (!DeducedArg.isNull()) {
822 NewlyDeducedPacks[I].push_back(DeducedArg);
823 DeducedArg = DeducedTemplateArgument();
824 }
825 }
826 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000827
Douglas Gregor5499af42011-01-05 23:12:31 +0000828 // Build argument packs for each of the parameter packs expanded by this
829 // pack expansion.
Douglas Gregorb94a6172011-01-10 17:53:52 +0000830 if (Sema::TemplateDeductionResult Result
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000831 = FinishArgumentPackDeduction(S, TemplateParams, HasAnyArguments,
Douglas Gregorb94a6172011-01-10 17:53:52 +0000832 Deduced, PackIndices, SavedPacks,
833 NewlyDeducedPacks, Info))
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000834 return Result;
Douglas Gregor5499af42011-01-05 23:12:31 +0000835 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000836
Douglas Gregor5499af42011-01-05 23:12:31 +0000837 // Make sure we don't have any extra arguments.
838 if (ArgIdx < NumArgs)
Richard Smith44ecdbd2013-01-31 05:19:49 +0000839 return Sema::TDK_MiscellaneousDeductionFailure;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000840
Douglas Gregor5499af42011-01-05 23:12:31 +0000841 return Sema::TDK_Success;
842}
843
Douglas Gregor1d684c22011-04-28 00:56:09 +0000844/// \brief Determine whether the parameter has qualifiers that are either
845/// inconsistent with or a superset of the argument's qualifiers.
846static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType,
847 QualType ArgType) {
848 Qualifiers ParamQs = ParamType.getQualifiers();
849 Qualifiers ArgQs = ArgType.getQualifiers();
850
851 if (ParamQs == ArgQs)
852 return false;
853
854 // Mismatched (but not missing) Objective-C GC attributes.
855 if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() &&
856 ParamQs.hasObjCGCAttr())
857 return true;
858
859 // Mismatched (but not missing) address spaces.
860 if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() &&
861 ParamQs.hasAddressSpace())
862 return true;
863
John McCall31168b02011-06-15 23:02:42 +0000864 // Mismatched (but not missing) Objective-C lifetime qualifiers.
865 if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() &&
866 ParamQs.hasObjCLifetime())
867 return true;
868
Douglas Gregor1d684c22011-04-28 00:56:09 +0000869 // CVR qualifier superset.
870 return (ParamQs.getCVRQualifiers() != ArgQs.getCVRQualifiers()) &&
871 ((ParamQs.getCVRQualifiers() | ArgQs.getCVRQualifiers())
872 == ParamQs.getCVRQualifiers());
873}
874
Douglas Gregor19a41f12013-04-17 08:45:07 +0000875/// \brief Compare types for equality with respect to possibly compatible
876/// function types (noreturn adjustment, implicit calling conventions). If any
877/// of parameter and argument is not a function, just perform type comparison.
878///
879/// \param Param the template parameter type.
880///
881/// \param Arg the argument type.
882bool Sema::isSameOrCompatibleFunctionType(CanQualType Param,
883 CanQualType Arg) {
884 const FunctionType *ParamFunction = Param->getAs<FunctionType>(),
885 *ArgFunction = Arg->getAs<FunctionType>();
886
887 // Just compare if not functions.
888 if (!ParamFunction || !ArgFunction)
889 return Param == Arg;
890
891 // Noreturn adjustment.
892 QualType AdjustedParam;
893 if (IsNoReturnConversion(Param, Arg, AdjustedParam))
894 return Arg == Context.getCanonicalType(AdjustedParam);
895
896 // FIXME: Compatible calling conventions.
897
898 return Param == Arg;
899}
900
Douglas Gregorcceb9752009-06-26 18:27:22 +0000901/// \brief Deduce the template arguments by comparing the parameter type and
902/// the argument type (C++ [temp.deduct.type]).
903///
Chandler Carruthc1263112010-02-07 21:33:28 +0000904/// \param S the semantic analysis object within which we are deducing
Douglas Gregorcceb9752009-06-26 18:27:22 +0000905///
906/// \param TemplateParams the template parameters that we are deducing
907///
908/// \param ParamIn the parameter type
909///
910/// \param ArgIn the argument type
911///
912/// \param Info information about the template argument deduction itself
913///
914/// \param Deduced the deduced template arguments
915///
Douglas Gregorcf0b47d2009-06-26 23:10:12 +0000916/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
Mike Stump11289f42009-09-09 15:08:12 +0000917/// how template argument deduction is performed.
Douglas Gregorcceb9752009-06-26 18:27:22 +0000918///
Douglas Gregorb837ea42011-01-11 17:34:58 +0000919/// \param PartialOrdering Whether we're performing template argument deduction
920/// in the context of partial ordering (C++0x [temp.deduct.partial]).
921///
Douglas Gregor63814022011-01-21 17:29:42 +0000922/// \param RefParamComparisons If we're performing template argument deduction
Douglas Gregorb837ea42011-01-11 17:34:58 +0000923/// in the context of partial ordering, the set of qualifier comparisons.
924///
Douglas Gregorcceb9752009-06-26 18:27:22 +0000925/// \returns the result of template argument deduction so far. Note that a
926/// "success" result means that template argument deduction has not yet failed,
927/// but it may still fail, later, for other reasons.
Douglas Gregor181aa4a2009-06-12 18:26:56 +0000928static Sema::TemplateDeductionResult
Sebastian Redlfb0b1f12012-01-17 22:49:52 +0000929DeduceTemplateArgumentsByTypeMatch(Sema &S,
930 TemplateParameterList *TemplateParams,
931 QualType ParamIn, QualType ArgIn,
932 TemplateDeductionInfo &Info,
933 SmallVectorImpl<DeducedTemplateArgument> &Deduced,
934 unsigned TDF,
935 bool PartialOrdering,
936 SmallVectorImpl<RefParamPartialOrderingComparison> *
937 RefParamComparisons) {
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000938 // We only want to look at the canonical types, since typedefs and
939 // sugar are not part of template argument deduction.
Chandler Carruthc1263112010-02-07 21:33:28 +0000940 QualType Param = S.Context.getCanonicalType(ParamIn);
941 QualType Arg = S.Context.getCanonicalType(ArgIn);
Douglas Gregor55ca8f62009-06-04 00:03:07 +0000942
Douglas Gregor2fcb8632011-01-11 22:21:24 +0000943 // If the argument type is a pack expansion, look at its pattern.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000944 // This isn't explicitly called out
Douglas Gregor2fcb8632011-01-11 22:21:24 +0000945 if (const PackExpansionType *ArgExpansion
946 = dyn_cast<PackExpansionType>(Arg))
947 Arg = ArgExpansion->getPattern();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000948
Douglas Gregorb837ea42011-01-11 17:34:58 +0000949 if (PartialOrdering) {
950 // C++0x [temp.deduct.partial]p5:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000951 // Before the partial ordering is done, certain transformations are
952 // performed on the types used for partial ordering:
953 // - If P is a reference type, P is replaced by the type referred to.
Douglas Gregorb837ea42011-01-11 17:34:58 +0000954 const ReferenceType *ParamRef = Param->getAs<ReferenceType>();
955 if (ParamRef)
956 Param = ParamRef->getPointeeType();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000957
Douglas Gregorb837ea42011-01-11 17:34:58 +0000958 // - If A is a reference type, A is replaced by the type referred to.
959 const ReferenceType *ArgRef = Arg->getAs<ReferenceType>();
960 if (ArgRef)
961 Arg = ArgRef->getPointeeType();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000962
Douglas Gregor63814022011-01-21 17:29:42 +0000963 if (RefParamComparisons && ParamRef && ArgRef) {
Douglas Gregorb837ea42011-01-11 17:34:58 +0000964 // C++0x [temp.deduct.partial]p6:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000965 // If both P and A were reference types (before being replaced with the
966 // type referred to above), determine which of the two types (if any) is
Douglas Gregorb837ea42011-01-11 17:34:58 +0000967 // more cv-qualified than the other; otherwise the types are considered
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000968 // to be equally cv-qualified for partial ordering purposes. The result
Douglas Gregorb837ea42011-01-11 17:34:58 +0000969 // of this determination will be used below.
970 //
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000971 // We save this information for later, using it only when deduction
Douglas Gregorb837ea42011-01-11 17:34:58 +0000972 // succeeds in both directions.
Douglas Gregor63814022011-01-21 17:29:42 +0000973 RefParamPartialOrderingComparison Comparison;
974 Comparison.ParamIsRvalueRef = ParamRef->getAs<RValueReferenceType>();
975 Comparison.ArgIsRvalueRef = ArgRef->getAs<RValueReferenceType>();
976 Comparison.Qualifiers = NeitherMoreQualified;
Douglas Gregor85894a82011-04-30 17:07:52 +0000977
978 Qualifiers ParamQuals = Param.getQualifiers();
979 Qualifiers ArgQuals = Arg.getQualifiers();
980 if (ParamQuals.isStrictSupersetOf(ArgQuals))
Douglas Gregor63814022011-01-21 17:29:42 +0000981 Comparison.Qualifiers = ParamMoreQualified;
Douglas Gregor85894a82011-04-30 17:07:52 +0000982 else if (ArgQuals.isStrictSupersetOf(ParamQuals))
Douglas Gregor63814022011-01-21 17:29:42 +0000983 Comparison.Qualifiers = ArgMoreQualified;
Douglas Gregor6beabee2014-01-02 19:42:02 +0000984 else if (ArgQuals.getObjCLifetime() != ParamQuals.getObjCLifetime() &&
985 ArgQuals.withoutObjCLifetime()
986 == ParamQuals.withoutObjCLifetime()) {
987 // Prefer binding to non-__unsafe_autoretained parameters.
988 if (ArgQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone &&
989 ParamQuals.getObjCLifetime())
990 Comparison.Qualifiers = ParamMoreQualified;
991 else if (ParamQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone &&
992 ArgQuals.getObjCLifetime())
993 Comparison.Qualifiers = ArgMoreQualified;
994 }
Douglas Gregor63814022011-01-21 17:29:42 +0000995 RefParamComparisons->push_back(Comparison);
Douglas Gregorb837ea42011-01-11 17:34:58 +0000996 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +0000997
Douglas Gregorb837ea42011-01-11 17:34:58 +0000998 // C++0x [temp.deduct.partial]p7:
999 // Remove any top-level cv-qualifiers:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001000 // - If P is a cv-qualified type, P is replaced by the cv-unqualified
Douglas Gregorb837ea42011-01-11 17:34:58 +00001001 // version of P.
1002 Param = Param.getUnqualifiedType();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001003 // - If A is a cv-qualified type, A is replaced by the cv-unqualified
Douglas Gregorb837ea42011-01-11 17:34:58 +00001004 // version of A.
1005 Arg = Arg.getUnqualifiedType();
1006 } else {
1007 // C++0x [temp.deduct.call]p4 bullet 1:
1008 // - If the original P is a reference type, the deduced A (i.e., the type
1009 // referred to by the reference) can be more cv-qualified than the
1010 // transformed A.
1011 if (TDF & TDF_ParamWithReferenceType) {
1012 Qualifiers Quals;
1013 QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals);
1014 Quals.setCVRQualifiers(Quals.getCVRQualifiers() &
John McCall6c9dd522011-01-18 07:41:22 +00001015 Arg.getCVRQualifiers());
Douglas Gregorb837ea42011-01-11 17:34:58 +00001016 Param = S.Context.getQualifiedType(UnqualParam, Quals);
1017 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001018
Douglas Gregor85f240c2011-01-25 17:19:08 +00001019 if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) {
1020 // C++0x [temp.deduct.type]p10:
1021 // If P and A are function types that originated from deduction when
1022 // taking the address of a function template (14.8.2.2) or when deducing
1023 // template arguments from a function declaration (14.8.2.6) and Pi and
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001024 // Ai are parameters of the top-level parameter-type-list of P and A,
1025 // respectively, Pi is adjusted if it is an rvalue reference to a
1026 // cv-unqualified template parameter and Ai is an lvalue reference, in
1027 // which case the type of Pi is changed to be the template parameter
Douglas Gregor85f240c2011-01-25 17:19:08 +00001028 // type (i.e., T&& is changed to simply T). [ Note: As a result, when
1029 // Pi is T&& and Ai is X&, the adjusted Pi will be T, causing T to be
NAKAMURA Takumi7c288862011-01-27 07:09:49 +00001030 // deduced as X&. - end note ]
Douglas Gregor85f240c2011-01-25 17:19:08 +00001031 TDF &= ~TDF_TopLevelParameterTypeList;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001032
Douglas Gregor85f240c2011-01-25 17:19:08 +00001033 if (const RValueReferenceType *ParamRef
1034 = Param->getAs<RValueReferenceType>()) {
1035 if (isa<TemplateTypeParmType>(ParamRef->getPointeeType()) &&
1036 !ParamRef->getPointeeType().getQualifiers())
1037 if (Arg->isLValueReferenceType())
1038 Param = ParamRef->getPointeeType();
1039 }
1040 }
Douglas Gregorcceb9752009-06-26 18:27:22 +00001041 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001042
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001043 // C++ [temp.deduct.type]p9:
Mike Stump11289f42009-09-09 15:08:12 +00001044 // A template type argument T, a template template argument TT or a
1045 // template non-type argument i can be deduced if P and A have one of
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001046 // the following forms:
1047 //
1048 // T
1049 // cv-list T
Mike Stump11289f42009-09-09 15:08:12 +00001050 if (const TemplateTypeParmType *TemplateTypeParm
John McCall9dd450b2009-09-21 23:43:11 +00001051 = Param->getAs<TemplateTypeParmType>()) {
Douglas Gregor4ea5dec2011-09-22 15:57:07 +00001052 // Just skip any attempts to deduce from a placeholder type.
1053 if (Arg->isPlaceholderType())
1054 return Sema::TDK_Success;
1055
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001056 unsigned Index = TemplateTypeParm->getIndex();
Douglas Gregord6605db2009-07-22 21:30:48 +00001057 bool RecanonicalizeArg = false;
Mike Stump11289f42009-09-09 15:08:12 +00001058
Douglas Gregor60454822009-07-22 20:02:25 +00001059 // If the argument type is an array type, move the qualifiers up to the
1060 // top level, so they can be matched with the qualifiers on the parameter.
Douglas Gregord6605db2009-07-22 21:30:48 +00001061 if (isa<ArrayType>(Arg)) {
John McCall8ccfcb52009-09-24 19:53:00 +00001062 Qualifiers Quals;
Chandler Carruthc1263112010-02-07 21:33:28 +00001063 Arg = S.Context.getUnqualifiedArrayType(Arg, Quals);
John McCall8ccfcb52009-09-24 19:53:00 +00001064 if (Quals) {
Chandler Carruthc1263112010-02-07 21:33:28 +00001065 Arg = S.Context.getQualifiedType(Arg, Quals);
Douglas Gregord6605db2009-07-22 21:30:48 +00001066 RecanonicalizeArg = true;
1067 }
1068 }
Mike Stump11289f42009-09-09 15:08:12 +00001069
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001070 // The argument type can not be less qualified than the parameter
1071 // type.
Douglas Gregor1d684c22011-04-28 00:56:09 +00001072 if (!(TDF & TDF_IgnoreQualifiers) &&
1073 hasInconsistentOrSupersetQualifiersOf(Param, Arg)) {
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001074 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
John McCall42d7d192010-08-05 09:05:08 +00001075 Info.FirstArg = TemplateArgument(Param);
John McCall0ad16662009-10-29 08:12:44 +00001076 Info.SecondArg = TemplateArgument(Arg);
John McCall42d7d192010-08-05 09:05:08 +00001077 return Sema::TDK_Underqualified;
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001078 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001079
1080 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
Chandler Carruthc1263112010-02-07 21:33:28 +00001081 assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function");
John McCall8ccfcb52009-09-24 19:53:00 +00001082 QualType DeducedType = Arg;
John McCall717d9b02010-12-10 11:01:00 +00001083
Douglas Gregor1d684c22011-04-28 00:56:09 +00001084 // Remove any qualifiers on the parameter from the deduced type.
1085 // We checked the qualifiers for consistency above.
1086 Qualifiers DeducedQs = DeducedType.getQualifiers();
1087 Qualifiers ParamQs = Param.getQualifiers();
1088 DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers());
1089 if (ParamQs.hasObjCGCAttr())
1090 DeducedQs.removeObjCGCAttr();
1091 if (ParamQs.hasAddressSpace())
1092 DeducedQs.removeAddressSpace();
John McCall31168b02011-06-15 23:02:42 +00001093 if (ParamQs.hasObjCLifetime())
1094 DeducedQs.removeObjCLifetime();
Douglas Gregore46db902011-06-17 22:11:49 +00001095
1096 // Objective-C ARC:
Douglas Gregora4f2b432011-07-26 14:53:44 +00001097 // If template deduction would produce a lifetime qualifier on a type
1098 // that is not a lifetime type, template argument deduction fails.
1099 if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType() &&
1100 !DeducedType->isDependentType()) {
1101 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
1102 Info.FirstArg = TemplateArgument(Param);
1103 Info.SecondArg = TemplateArgument(Arg);
1104 return Sema::TDK_Underqualified;
1105 }
1106
1107 // Objective-C ARC:
Douglas Gregore46db902011-06-17 22:11:49 +00001108 // If template deduction would produce an argument type with lifetime type
1109 // but no lifetime qualifier, the __strong lifetime qualifier is inferred.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001110 if (S.getLangOpts().ObjCAutoRefCount &&
Douglas Gregore46db902011-06-17 22:11:49 +00001111 DeducedType->isObjCLifetimeType() &&
1112 !DeducedQs.hasObjCLifetime())
1113 DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong);
1114
Douglas Gregor1d684c22011-04-28 00:56:09 +00001115 DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(),
1116 DeducedQs);
1117
Douglas Gregord6605db2009-07-22 21:30:48 +00001118 if (RecanonicalizeArg)
Chandler Carruthc1263112010-02-07 21:33:28 +00001119 DeducedType = S.Context.getCanonicalType(DeducedType);
Mike Stump11289f42009-09-09 15:08:12 +00001120
Douglas Gregor7f8e7682010-12-22 23:09:49 +00001121 DeducedTemplateArgument NewDeduced(DeducedType);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001122 DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
Douglas Gregor7f8e7682010-12-22 23:09:49 +00001123 Deduced[Index],
1124 NewDeduced);
1125 if (Result.isNull()) {
1126 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
1127 Info.FirstArg = Deduced[Index];
1128 Info.SecondArg = NewDeduced;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001129 return Sema::TDK_Inconsistent;
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001130 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001131
Douglas Gregor7f8e7682010-12-22 23:09:49 +00001132 Deduced[Index] = Result;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001133 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001134 }
1135
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001136 // Set up the template argument deduction information for a failure.
John McCall0ad16662009-10-29 08:12:44 +00001137 Info.FirstArg = TemplateArgument(ParamIn);
1138 Info.SecondArg = TemplateArgument(ArgIn);
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001139
Douglas Gregorfb322d82011-01-14 05:11:40 +00001140 // If the parameter is an already-substituted template parameter
1141 // pack, do nothing: we don't know which of its arguments to look
1142 // at, so we have to wait until all of the parameter packs in this
1143 // expansion have arguments.
1144 if (isa<SubstTemplateTypeParmPackType>(Param))
1145 return Sema::TDK_Success;
1146
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001147 // Check the cv-qualifiers on the parameter and argument types.
Douglas Gregor19a41f12013-04-17 08:45:07 +00001148 CanQualType CanParam = S.Context.getCanonicalType(Param);
1149 CanQualType CanArg = S.Context.getCanonicalType(Arg);
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001150 if (!(TDF & TDF_IgnoreQualifiers)) {
1151 if (TDF & TDF_ParamWithReferenceType) {
Douglas Gregor1d684c22011-04-28 00:56:09 +00001152 if (hasInconsistentOrSupersetQualifiersOf(Param, Arg))
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001153 return Sema::TDK_NonDeducedMismatch;
John McCall08569062010-08-28 22:14:41 +00001154 } else if (!IsPossiblyOpaquelyQualifiedType(Param)) {
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001155 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
Mike Stump11289f42009-09-09 15:08:12 +00001156 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001157 }
Douglas Gregor194ea692012-03-11 03:29:50 +00001158
1159 // If the parameter type is not dependent, there is nothing to deduce.
1160 if (!Param->isDependentType()) {
Douglas Gregor19a41f12013-04-17 08:45:07 +00001161 if (!(TDF & TDF_SkipNonDependent)) {
1162 bool NonDeduced = (TDF & TDF_InOverloadResolution)?
1163 !S.isSameOrCompatibleFunctionType(CanParam, CanArg) :
1164 Param != Arg;
1165 if (NonDeduced) {
1166 return Sema::TDK_NonDeducedMismatch;
1167 }
1168 }
Douglas Gregor194ea692012-03-11 03:29:50 +00001169 return Sema::TDK_Success;
1170 }
Douglas Gregor19a41f12013-04-17 08:45:07 +00001171 } else if (!Param->isDependentType()) {
1172 CanQualType ParamUnqualType = CanParam.getUnqualifiedType(),
1173 ArgUnqualType = CanArg.getUnqualifiedType();
1174 bool Success = (TDF & TDF_InOverloadResolution)?
1175 S.isSameOrCompatibleFunctionType(ParamUnqualType,
1176 ArgUnqualType) :
1177 ParamUnqualType == ArgUnqualType;
1178 if (Success)
1179 return Sema::TDK_Success;
Douglas Gregorcf0b47d2009-06-26 23:10:12 +00001180 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001181
Douglas Gregor5cdac0a2009-06-04 00:21:18 +00001182 switch (Param->getTypeClass()) {
Douglas Gregor39c02722011-06-15 16:02:29 +00001183 // Non-canonical types cannot appear here.
1184#define NON_CANONICAL_TYPE(Class, Base) \
1185 case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class);
1186#define TYPE(Class, Base)
1187#include "clang/AST/TypeNodes.def"
1188
1189 case Type::TemplateTypeParm:
1190 case Type::SubstTemplateTypeParmPack:
1191 llvm_unreachable("Type nodes handled above");
Douglas Gregor194ea692012-03-11 03:29:50 +00001192
1193 // These types cannot be dependent, so simply check whether the types are
1194 // the same.
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001195 case Type::Builtin:
Douglas Gregor39c02722011-06-15 16:02:29 +00001196 case Type::VariableArray:
1197 case Type::Vector:
1198 case Type::FunctionNoProto:
1199 case Type::Record:
1200 case Type::Enum:
1201 case Type::ObjCObject:
1202 case Type::ObjCInterface:
Douglas Gregor194ea692012-03-11 03:29:50 +00001203 case Type::ObjCObjectPointer: {
1204 if (TDF & TDF_SkipNonDependent)
1205 return Sema::TDK_Success;
1206
1207 if (TDF & TDF_IgnoreQualifiers) {
1208 Param = Param.getUnqualifiedType();
1209 Arg = Arg.getUnqualifiedType();
1210 }
1211
1212 return Param == Arg? Sema::TDK_Success : Sema::TDK_NonDeducedMismatch;
1213 }
1214
Douglas Gregor39c02722011-06-15 16:02:29 +00001215 // _Complex T [placeholder extension]
1216 case Type::Complex:
1217 if (const ComplexType *ComplexArg = Arg->getAs<ComplexType>())
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00001218 return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
Douglas Gregor39c02722011-06-15 16:02:29 +00001219 cast<ComplexType>(Param)->getElementType(),
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00001220 ComplexArg->getElementType(),
1221 Info, Deduced, TDF);
Douglas Gregor39c02722011-06-15 16:02:29 +00001222
1223 return Sema::TDK_NonDeducedMismatch;
Eli Friedman0dfb8892011-10-06 23:00:33 +00001224
1225 // _Atomic T [extension]
1226 case Type::Atomic:
1227 if (const AtomicType *AtomicArg = Arg->getAs<AtomicType>())
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00001228 return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
Eli Friedman0dfb8892011-10-06 23:00:33 +00001229 cast<AtomicType>(Param)->getValueType(),
1230 AtomicArg->getValueType(),
1231 Info, Deduced, TDF);
1232
1233 return Sema::TDK_NonDeducedMismatch;
1234
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001235 // T *
Douglas Gregor5cdac0a2009-06-04 00:21:18 +00001236 case Type::Pointer: {
John McCallbb4ea812010-05-13 07:48:05 +00001237 QualType PointeeType;
1238 if (const PointerType *PointerArg = Arg->getAs<PointerType>()) {
1239 PointeeType = PointerArg->getPointeeType();
1240 } else if (const ObjCObjectPointerType *PointerArg
1241 = Arg->getAs<ObjCObjectPointerType>()) {
1242 PointeeType = PointerArg->getPointeeType();
1243 } else {
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001244 return Sema::TDK_NonDeducedMismatch;
John McCallbb4ea812010-05-13 07:48:05 +00001245 }
Mike Stump11289f42009-09-09 15:08:12 +00001246
Douglas Gregorfc516c92009-06-26 23:27:24 +00001247 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00001248 return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1249 cast<PointerType>(Param)->getPointeeType(),
John McCallbb4ea812010-05-13 07:48:05 +00001250 PointeeType,
Douglas Gregorfc516c92009-06-26 23:27:24 +00001251 Info, Deduced, SubTDF);
Douglas Gregor5cdac0a2009-06-04 00:21:18 +00001252 }
Mike Stump11289f42009-09-09 15:08:12 +00001253
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001254 // T &
Douglas Gregor5cdac0a2009-06-04 00:21:18 +00001255 case Type::LValueReference: {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001256 const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
Douglas Gregor5cdac0a2009-06-04 00:21:18 +00001257 if (!ReferenceArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001258 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +00001259
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00001260 return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
Douglas Gregor5cdac0a2009-06-04 00:21:18 +00001261 cast<LValueReferenceType>(Param)->getPointeeType(),
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00001262 ReferenceArg->getPointeeType(), Info, Deduced, 0);
Douglas Gregor5cdac0a2009-06-04 00:21:18 +00001263 }
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001264
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001265 // T && [C++0x]
Douglas Gregor5cdac0a2009-06-04 00:21:18 +00001266 case Type::RValueReference: {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001267 const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
Douglas Gregor5cdac0a2009-06-04 00:21:18 +00001268 if (!ReferenceArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001269 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +00001270
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00001271 return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1272 cast<RValueReferenceType>(Param)->getPointeeType(),
1273 ReferenceArg->getPointeeType(),
1274 Info, Deduced, 0);
Douglas Gregor5cdac0a2009-06-04 00:21:18 +00001275 }
Mike Stump11289f42009-09-09 15:08:12 +00001276
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001277 // T [] (implied, but not stated explicitly)
Anders Carlsson35533d12009-06-04 04:11:30 +00001278 case Type::IncompleteArray: {
Mike Stump11289f42009-09-09 15:08:12 +00001279 const IncompleteArrayType *IncompleteArrayArg =
Chandler Carruthc1263112010-02-07 21:33:28 +00001280 S.Context.getAsIncompleteArrayType(Arg);
Anders Carlsson35533d12009-06-04 04:11:30 +00001281 if (!IncompleteArrayArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001282 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +00001283
John McCallf7332682010-08-19 00:20:19 +00001284 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00001285 return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1286 S.Context.getAsIncompleteArrayType(Param)->getElementType(),
1287 IncompleteArrayArg->getElementType(),
1288 Info, Deduced, SubTDF);
Anders Carlsson35533d12009-06-04 04:11:30 +00001289 }
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001290
1291 // T [integer-constant]
Anders Carlsson35533d12009-06-04 04:11:30 +00001292 case Type::ConstantArray: {
Mike Stump11289f42009-09-09 15:08:12 +00001293 const ConstantArrayType *ConstantArrayArg =
Chandler Carruthc1263112010-02-07 21:33:28 +00001294 S.Context.getAsConstantArrayType(Arg);
Anders Carlsson35533d12009-06-04 04:11:30 +00001295 if (!ConstantArrayArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001296 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +00001297
1298 const ConstantArrayType *ConstantArrayParm =
Chandler Carruthc1263112010-02-07 21:33:28 +00001299 S.Context.getAsConstantArrayType(Param);
Anders Carlsson35533d12009-06-04 04:11:30 +00001300 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001301 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +00001302
John McCallf7332682010-08-19 00:20:19 +00001303 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00001304 return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1305 ConstantArrayParm->getElementType(),
1306 ConstantArrayArg->getElementType(),
1307 Info, Deduced, SubTDF);
Anders Carlsson35533d12009-06-04 04:11:30 +00001308 }
1309
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001310 // type [i]
1311 case Type::DependentSizedArray: {
Chandler Carruthc1263112010-02-07 21:33:28 +00001312 const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001313 if (!ArrayArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001314 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +00001315
John McCallf7332682010-08-19 00:20:19 +00001316 unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
1317
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001318 // Check the element type of the arrays
1319 const DependentSizedArrayType *DependentArrayParm
Chandler Carruthc1263112010-02-07 21:33:28 +00001320 = S.Context.getAsDependentSizedArrayType(Param);
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001321 if (Sema::TemplateDeductionResult Result
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00001322 = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1323 DependentArrayParm->getElementType(),
1324 ArrayArg->getElementType(),
1325 Info, Deduced, SubTDF))
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001326 return Result;
Mike Stump11289f42009-09-09 15:08:12 +00001327
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001328 // Determine the array bound is something we can deduce.
Mike Stump11289f42009-09-09 15:08:12 +00001329 NonTypeTemplateParmDecl *NTTP
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001330 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
1331 if (!NTTP)
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001332 return Sema::TDK_Success;
Mike Stump11289f42009-09-09 15:08:12 +00001333
1334 // We can perform template argument deduction for the given non-type
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001335 // template parameter.
Mike Stump11289f42009-09-09 15:08:12 +00001336 assert(NTTP->getDepth() == 0 &&
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001337 "Cannot deduce non-type template argument at depth > 0");
Mike Stump11289f42009-09-09 15:08:12 +00001338 if (const ConstantArrayType *ConstantArrayArg
Anders Carlsson3a106e02009-06-16 22:44:31 +00001339 = dyn_cast<ConstantArrayType>(ArrayArg)) {
1340 llvm::APSInt Size(ConstantArrayArg->getSize());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001341 return DeduceNonTypeTemplateArgument(S, NTTP, Size,
Douglas Gregor0a29a052010-03-26 05:50:28 +00001342 S.Context.getSizeType(),
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001343 /*ArrayBound=*/true,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001344 Info, Deduced);
Anders Carlsson3a106e02009-06-16 22:44:31 +00001345 }
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001346 if (const DependentSizedArrayType *DependentArrayArg
1347 = dyn_cast<DependentSizedArrayType>(ArrayArg))
Douglas Gregor7a49ead2010-12-22 23:15:38 +00001348 if (DependentArrayArg->getSizeExpr())
1349 return DeduceNonTypeTemplateArgument(S, NTTP,
1350 DependentArrayArg->getSizeExpr(),
1351 Info, Deduced);
Mike Stump11289f42009-09-09 15:08:12 +00001352
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001353 // Incomplete type does not match a dependently-sized array type
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001354 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001355 }
Mike Stump11289f42009-09-09 15:08:12 +00001356
1357 // type(*)(T)
1358 // T(*)()
1359 // T(*)(T)
Anders Carlsson2128ec72009-06-08 15:19:08 +00001360 case Type::FunctionProto: {
Douglas Gregor85f240c2011-01-25 17:19:08 +00001361 unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList;
Mike Stump11289f42009-09-09 15:08:12 +00001362 const FunctionProtoType *FunctionProtoArg =
Anders Carlsson2128ec72009-06-08 15:19:08 +00001363 dyn_cast<FunctionProtoType>(Arg);
1364 if (!FunctionProtoArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001365 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +00001366
1367 const FunctionProtoType *FunctionProtoParam =
Anders Carlsson2128ec72009-06-08 15:19:08 +00001368 cast<FunctionProtoType>(Param);
Anders Carlsson096e6ee2009-06-08 19:22:23 +00001369
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001370 if (FunctionProtoParam->getTypeQuals()
Douglas Gregor54e462a2011-01-26 16:50:54 +00001371 != FunctionProtoArg->getTypeQuals() ||
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001372 FunctionProtoParam->getRefQualifier()
Douglas Gregor54e462a2011-01-26 16:50:54 +00001373 != FunctionProtoArg->getRefQualifier() ||
1374 FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001375 return Sema::TDK_NonDeducedMismatch;
Anders Carlsson096e6ee2009-06-08 19:22:23 +00001376
Anders Carlsson2128ec72009-06-08 15:19:08 +00001377 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00001378 if (Sema::TemplateDeductionResult Result =
1379 DeduceTemplateArgumentsByTypeMatch(
1380 S, TemplateParams, FunctionProtoParam->getReturnType(),
1381 FunctionProtoArg->getReturnType(), Info, Deduced, 0))
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001382 return Result;
Mike Stump11289f42009-09-09 15:08:12 +00001383
Alp Toker9cacbab2014-01-20 20:26:09 +00001384 return DeduceTemplateArguments(
1385 S, TemplateParams, FunctionProtoParam->param_type_begin(),
1386 FunctionProtoParam->getNumParams(),
1387 FunctionProtoArg->param_type_begin(),
1388 FunctionProtoArg->getNumParams(), Info, Deduced, SubTDF);
Anders Carlsson2128ec72009-06-08 15:19:08 +00001389 }
Mike Stump11289f42009-09-09 15:08:12 +00001390
John McCalle78aac42010-03-10 03:28:59 +00001391 case Type::InjectedClassName: {
1392 // Treat a template's injected-class-name as if the template
1393 // specialization type had been used.
John McCall2408e322010-04-27 00:57:59 +00001394 Param = cast<InjectedClassNameType>(Param)
1395 ->getInjectedSpecializationType();
John McCalle78aac42010-03-10 03:28:59 +00001396 assert(isa<TemplateSpecializationType>(Param) &&
1397 "injected class name is not a template specialization type");
1398 // fall through
1399 }
1400
Douglas Gregor705c9002009-06-26 20:57:09 +00001401 // template-name<T> (where template-name refers to a class template)
Douglas Gregor4fbe3e32009-06-09 16:35:58 +00001402 // template-name<i>
Douglas Gregoradee3e32009-11-11 23:06:43 +00001403 // TT<T>
1404 // TT<i>
1405 // TT<>
Douglas Gregor4fbe3e32009-06-09 16:35:58 +00001406 case Type::TemplateSpecialization: {
1407 const TemplateSpecializationType *SpecParam
1408 = cast<TemplateSpecializationType>(Param);
Mike Stump11289f42009-09-09 15:08:12 +00001409
Douglas Gregore81f3e72009-07-07 23:09:34 +00001410 // Try to deduce template arguments from the template-id.
1411 Sema::TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +00001412 = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
Douglas Gregore81f3e72009-07-07 23:09:34 +00001413 Info, Deduced);
Mike Stump11289f42009-09-09 15:08:12 +00001414
Douglas Gregor42909752009-09-30 22:13:51 +00001415 if (Result && (TDF & TDF_DerivedClass)) {
Douglas Gregore81f3e72009-07-07 23:09:34 +00001416 // C++ [temp.deduct.call]p3b3:
1417 // If P is a class, and P has the form template-id, then A can be a
1418 // derived class of the deduced A. Likewise, if P is a pointer to a
Mike Stump11289f42009-09-09 15:08:12 +00001419 // class of the form template-id, A can be a pointer to a derived
Douglas Gregore81f3e72009-07-07 23:09:34 +00001420 // class pointed to by the deduced A.
1421 //
1422 // More importantly:
Mike Stump11289f42009-09-09 15:08:12 +00001423 // These alternatives are considered only if type deduction would
Douglas Gregore81f3e72009-07-07 23:09:34 +00001424 // otherwise fail.
Chandler Carruthc1263112010-02-07 21:33:28 +00001425 if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
1426 // We cannot inspect base classes as part of deduction when the type
1427 // is incomplete, so either instantiate any templates necessary to
1428 // complete the type, or skip over it if it cannot be completed.
John McCallbc077cf2010-02-08 23:07:23 +00001429 if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
Chandler Carruthc1263112010-02-07 21:33:28 +00001430 return Result;
1431
Douglas Gregore81f3e72009-07-07 23:09:34 +00001432 // Use data recursion to crawl through the list of base classes.
Mike Stump11289f42009-09-09 15:08:12 +00001433 // Visited contains the set of nodes we have already visited, while
Douglas Gregore81f3e72009-07-07 23:09:34 +00001434 // ToVisit is our stack of records that we still need to visit.
1435 llvm::SmallPtrSet<const RecordType *, 8> Visited;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001436 SmallVector<const RecordType *, 8> ToVisit;
Douglas Gregore81f3e72009-07-07 23:09:34 +00001437 ToVisit.push_back(RecordT);
1438 bool Successful = false;
Benjamin Kramer4d08ccb2012-01-20 16:39:18 +00001439 SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(),
1440 Deduced.end());
Douglas Gregore81f3e72009-07-07 23:09:34 +00001441 while (!ToVisit.empty()) {
1442 // Retrieve the next class in the inheritance hierarchy.
Robert Wilhelm25284cc2013-08-23 16:11:15 +00001443 const RecordType *NextT = ToVisit.pop_back_val();
Mike Stump11289f42009-09-09 15:08:12 +00001444
Douglas Gregore81f3e72009-07-07 23:09:34 +00001445 // If we have already seen this type, skip it.
1446 if (!Visited.insert(NextT))
1447 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001448
Douglas Gregore81f3e72009-07-07 23:09:34 +00001449 // If this is a base class, try to perform template argument
1450 // deduction from it.
1451 if (NextT != RecordT) {
Richard Trieu23bafad2012-11-07 21:17:13 +00001452 TemplateDeductionInfo BaseInfo(Info.getLocation());
Douglas Gregore81f3e72009-07-07 23:09:34 +00001453 Sema::TemplateDeductionResult BaseResult
Chandler Carruthc1263112010-02-07 21:33:28 +00001454 = DeduceTemplateArguments(S, TemplateParams, SpecParam,
Richard Trieu23bafad2012-11-07 21:17:13 +00001455 QualType(NextT, 0), BaseInfo,
1456 Deduced);
Mike Stump11289f42009-09-09 15:08:12 +00001457
Douglas Gregore81f3e72009-07-07 23:09:34 +00001458 // If template argument deduction for this base was successful,
Douglas Gregore0f7a8a2010-11-02 00:02:34 +00001459 // note that we had some success. Otherwise, ignore any deductions
1460 // from this base class.
1461 if (BaseResult == Sema::TDK_Success) {
Douglas Gregore81f3e72009-07-07 23:09:34 +00001462 Successful = true;
Benjamin Kramer4d08ccb2012-01-20 16:39:18 +00001463 DeducedOrig.clear();
1464 DeducedOrig.append(Deduced.begin(), Deduced.end());
Richard Trieu23bafad2012-11-07 21:17:13 +00001465 Info.Param = BaseInfo.Param;
1466 Info.FirstArg = BaseInfo.FirstArg;
1467 Info.SecondArg = BaseInfo.SecondArg;
Douglas Gregore0f7a8a2010-11-02 00:02:34 +00001468 }
1469 else
1470 Deduced = DeducedOrig;
Douglas Gregore81f3e72009-07-07 23:09:34 +00001471 }
Mike Stump11289f42009-09-09 15:08:12 +00001472
Douglas Gregore81f3e72009-07-07 23:09:34 +00001473 // Visit base classes
1474 CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
1475 for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
1476 BaseEnd = Next->bases_end();
Sebastian Redl1054fae2009-10-25 17:03:50 +00001477 Base != BaseEnd; ++Base) {
Mike Stump11289f42009-09-09 15:08:12 +00001478 assert(Base->getType()->isRecordType() &&
Douglas Gregore81f3e72009-07-07 23:09:34 +00001479 "Base class that isn't a record?");
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001480 ToVisit.push_back(Base->getType()->getAs<RecordType>());
Douglas Gregore81f3e72009-07-07 23:09:34 +00001481 }
1482 }
Mike Stump11289f42009-09-09 15:08:12 +00001483
Douglas Gregore81f3e72009-07-07 23:09:34 +00001484 if (Successful)
1485 return Sema::TDK_Success;
1486 }
Mike Stump11289f42009-09-09 15:08:12 +00001487
Douglas Gregore81f3e72009-07-07 23:09:34 +00001488 }
Mike Stump11289f42009-09-09 15:08:12 +00001489
Douglas Gregore81f3e72009-07-07 23:09:34 +00001490 return Result;
Douglas Gregor4fbe3e32009-06-09 16:35:58 +00001491 }
1492
Douglas Gregor637d9982009-06-10 23:47:09 +00001493 // T type::*
1494 // T T::*
1495 // T (type::*)()
1496 // type (T::*)()
1497 // type (type::*)(T)
1498 // type (T::*)(T)
1499 // T (type::*)(T)
1500 // T (T::*)()
1501 // T (T::*)(T)
1502 case Type::MemberPointer: {
1503 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
1504 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
1505 if (!MemPtrArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001506 return Sema::TDK_NonDeducedMismatch;
Douglas Gregor637d9982009-06-10 23:47:09 +00001507
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001508 if (Sema::TemplateDeductionResult Result
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00001509 = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1510 MemPtrParam->getPointeeType(),
1511 MemPtrArg->getPointeeType(),
1512 Info, Deduced,
1513 TDF & TDF_IgnoreQualifiers))
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001514 return Result;
1515
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00001516 return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1517 QualType(MemPtrParam->getClass(), 0),
1518 QualType(MemPtrArg->getClass(), 0),
Douglas Gregor194ea692012-03-11 03:29:50 +00001519 Info, Deduced,
1520 TDF & TDF_IgnoreQualifiers);
Douglas Gregor637d9982009-06-10 23:47:09 +00001521 }
1522
Anders Carlsson15f1dd12009-06-12 22:56:54 +00001523 // (clang extension)
1524 //
Mike Stump11289f42009-09-09 15:08:12 +00001525 // type(^)(T)
1526 // T(^)()
1527 // T(^)(T)
Anders Carlssona767eee2009-06-12 16:23:10 +00001528 case Type::BlockPointer: {
1529 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
1530 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
Mike Stump11289f42009-09-09 15:08:12 +00001531
Anders Carlssona767eee2009-06-12 16:23:10 +00001532 if (!BlockPtrArg)
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001533 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +00001534
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00001535 return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1536 BlockPtrParam->getPointeeType(),
1537 BlockPtrArg->getPointeeType(),
1538 Info, Deduced, 0);
Anders Carlssona767eee2009-06-12 16:23:10 +00001539 }
1540
Douglas Gregor39c02722011-06-15 16:02:29 +00001541 // (clang extension)
1542 //
1543 // T __attribute__(((ext_vector_type(<integral constant>))))
1544 case Type::ExtVector: {
1545 const ExtVectorType *VectorParam = cast<ExtVectorType>(Param);
1546 if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) {
1547 // Make sure that the vectors have the same number of elements.
1548 if (VectorParam->getNumElements() != VectorArg->getNumElements())
1549 return Sema::TDK_NonDeducedMismatch;
1550
1551 // Perform deduction on the element types.
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00001552 return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1553 VectorParam->getElementType(),
1554 VectorArg->getElementType(),
1555 Info, Deduced, TDF);
Douglas Gregor39c02722011-06-15 16:02:29 +00001556 }
1557
1558 if (const DependentSizedExtVectorType *VectorArg
1559 = dyn_cast<DependentSizedExtVectorType>(Arg)) {
1560 // We can't check the number of elements, since the argument has a
1561 // dependent number of elements. This can only occur during partial
1562 // ordering.
1563
1564 // Perform deduction on the element types.
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00001565 return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1566 VectorParam->getElementType(),
1567 VectorArg->getElementType(),
1568 Info, Deduced, TDF);
Douglas Gregor39c02722011-06-15 16:02:29 +00001569 }
1570
1571 return Sema::TDK_NonDeducedMismatch;
1572 }
1573
1574 // (clang extension)
1575 //
1576 // T __attribute__(((ext_vector_type(N))))
1577 case Type::DependentSizedExtVector: {
1578 const DependentSizedExtVectorType *VectorParam
1579 = cast<DependentSizedExtVectorType>(Param);
1580
1581 if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) {
1582 // Perform deduction on the element types.
1583 if (Sema::TemplateDeductionResult Result
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00001584 = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1585 VectorParam->getElementType(),
1586 VectorArg->getElementType(),
1587 Info, Deduced, TDF))
Douglas Gregor39c02722011-06-15 16:02:29 +00001588 return Result;
1589
1590 // Perform deduction on the vector size, if we can.
1591 NonTypeTemplateParmDecl *NTTP
1592 = getDeducedParameterFromExpr(VectorParam->getSizeExpr());
1593 if (!NTTP)
1594 return Sema::TDK_Success;
1595
1596 llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false);
1597 ArgSize = VectorArg->getNumElements();
1598 return DeduceNonTypeTemplateArgument(S, NTTP, ArgSize, S.Context.IntTy,
1599 false, Info, Deduced);
1600 }
1601
1602 if (const DependentSizedExtVectorType *VectorArg
1603 = dyn_cast<DependentSizedExtVectorType>(Arg)) {
1604 // Perform deduction on the element types.
1605 if (Sema::TemplateDeductionResult Result
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00001606 = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1607 VectorParam->getElementType(),
1608 VectorArg->getElementType(),
1609 Info, Deduced, TDF))
Douglas Gregor39c02722011-06-15 16:02:29 +00001610 return Result;
1611
1612 // Perform deduction on the vector size, if we can.
1613 NonTypeTemplateParmDecl *NTTP
1614 = getDeducedParameterFromExpr(VectorParam->getSizeExpr());
1615 if (!NTTP)
1616 return Sema::TDK_Success;
1617
1618 return DeduceNonTypeTemplateArgument(S, NTTP, VectorArg->getSizeExpr(),
1619 Info, Deduced);
1620 }
1621
1622 return Sema::TDK_NonDeducedMismatch;
1623 }
1624
Douglas Gregor637d9982009-06-10 23:47:09 +00001625 case Type::TypeOfExpr:
1626 case Type::TypeOf:
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00001627 case Type::DependentName:
Douglas Gregor39c02722011-06-15 16:02:29 +00001628 case Type::UnresolvedUsing:
1629 case Type::Decltype:
1630 case Type::UnaryTransform:
1631 case Type::Auto:
1632 case Type::DependentTemplateSpecialization:
1633 case Type::PackExpansion:
Douglas Gregor637d9982009-06-10 23:47:09 +00001634 // No template argument deduction for these types
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001635 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001636 }
1637
David Blaikiee4d798f2012-01-20 21:50:17 +00001638 llvm_unreachable("Invalid Type Class!");
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001639}
1640
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001641static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +00001642DeduceTemplateArguments(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001643 TemplateParameterList *TemplateParams,
1644 const TemplateArgument &Param,
Douglas Gregor2fcb8632011-01-11 22:21:24 +00001645 TemplateArgument Arg,
John McCall19c1bfd2010-08-25 05:32:35 +00001646 TemplateDeductionInfo &Info,
Craig Topper79653572013-07-08 04:13:06 +00001647 SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor2fcb8632011-01-11 22:21:24 +00001648 // If the template argument is a pack expansion, perform template argument
1649 // deduction against the pattern of that expansion. This only occurs during
1650 // partial ordering.
1651 if (Arg.isPackExpansion())
1652 Arg = Arg.getPackExpansionPattern();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001653
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001654 switch (Param.getKind()) {
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001655 case TemplateArgument::Null:
David Blaikie83d382b2011-09-23 05:06:16 +00001656 llvm_unreachable("Null template argument in parameter list");
Mike Stump11289f42009-09-09 15:08:12 +00001657
1658 case TemplateArgument::Type:
Douglas Gregor9167f8b2009-11-11 01:00:40 +00001659 if (Arg.getKind() == TemplateArgument::Type)
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00001660 return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
1661 Param.getAsType(),
1662 Arg.getAsType(),
1663 Info, Deduced, 0);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00001664 Info.FirstArg = Param;
1665 Info.SecondArg = Arg;
1666 return Sema::TDK_NonDeducedMismatch;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001667
Douglas Gregor9167f8b2009-11-11 01:00:40 +00001668 case TemplateArgument::Template:
Douglas Gregoradee3e32009-11-11 23:06:43 +00001669 if (Arg.getKind() == TemplateArgument::Template)
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001670 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor9167f8b2009-11-11 01:00:40 +00001671 Param.getAsTemplate(),
Douglas Gregoradee3e32009-11-11 23:06:43 +00001672 Arg.getAsTemplate(), Info, Deduced);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00001673 Info.FirstArg = Param;
1674 Info.SecondArg = Arg;
1675 return Sema::TDK_NonDeducedMismatch;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001676
1677 case TemplateArgument::TemplateExpansion:
1678 llvm_unreachable("caller should handle pack expansions");
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001679
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001680 case TemplateArgument::Declaration:
Douglas Gregor9167f8b2009-11-11 01:00:40 +00001681 if (Arg.getKind() == TemplateArgument::Declaration &&
Eli Friedmanb826a002012-09-26 02:36:12 +00001682 isSameDeclaration(Param.getAsDecl(), Arg.getAsDecl()) &&
1683 Param.isDeclForReferenceParam() == Arg.isDeclForReferenceParam())
1684 return Sema::TDK_Success;
1685
1686 Info.FirstArg = Param;
1687 Info.SecondArg = Arg;
1688 return Sema::TDK_NonDeducedMismatch;
1689
1690 case TemplateArgument::NullPtr:
1691 if (Arg.getKind() == TemplateArgument::NullPtr &&
1692 S.Context.hasSameType(Param.getNullPtrType(), Arg.getNullPtrType()))
Douglas Gregor9167f8b2009-11-11 01:00:40 +00001693 return Sema::TDK_Success;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001694
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001695 Info.FirstArg = Param;
1696 Info.SecondArg = Arg;
1697 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +00001698
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001699 case TemplateArgument::Integral:
1700 if (Arg.getKind() == TemplateArgument::Integral) {
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001701 if (hasSameExtendedValue(Param.getAsIntegral(), Arg.getAsIntegral()))
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001702 return Sema::TDK_Success;
1703
1704 Info.FirstArg = Param;
1705 Info.SecondArg = Arg;
1706 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001707 }
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001708
1709 if (Arg.getKind() == TemplateArgument::Expression) {
1710 Info.FirstArg = Param;
1711 Info.SecondArg = Arg;
1712 return Sema::TDK_NonDeducedMismatch;
1713 }
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001714
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001715 Info.FirstArg = Param;
1716 Info.SecondArg = Arg;
1717 return Sema::TDK_NonDeducedMismatch;
Mike Stump11289f42009-09-09 15:08:12 +00001718
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001719 case TemplateArgument::Expression: {
Mike Stump11289f42009-09-09 15:08:12 +00001720 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001721 = getDeducedParameterFromExpr(Param.getAsExpr())) {
1722 if (Arg.getKind() == TemplateArgument::Integral)
Chandler Carruthc1263112010-02-07 21:33:28 +00001723 return DeduceNonTypeTemplateArgument(S, NTTP,
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001724 Arg.getAsIntegral(),
Douglas Gregor0a29a052010-03-26 05:50:28 +00001725 Arg.getIntegralType(),
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00001726 /*ArrayBound=*/false,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001727 Info, Deduced);
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001728 if (Arg.getKind() == TemplateArgument::Expression)
Chandler Carruthc1263112010-02-07 21:33:28 +00001729 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001730 Info, Deduced);
Douglas Gregor2bb756a2009-11-13 23:45:44 +00001731 if (Arg.getKind() == TemplateArgument::Declaration)
Chandler Carruthc1263112010-02-07 21:33:28 +00001732 return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
Douglas Gregor2bb756a2009-11-13 23:45:44 +00001733 Info, Deduced);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001734
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001735 Info.FirstArg = Param;
1736 Info.SecondArg = Arg;
1737 return Sema::TDK_NonDeducedMismatch;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001738 }
Mike Stump11289f42009-09-09 15:08:12 +00001739
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001740 // Can't deduce anything, but that's okay.
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001741 return Sema::TDK_Success;
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001742 }
Anders Carlssonbc343912009-06-15 17:04:53 +00001743 case TemplateArgument::Pack:
Douglas Gregor7baabef2010-12-22 18:17:10 +00001744 llvm_unreachable("Argument packs should be expanded by the caller!");
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00001745 }
Mike Stump11289f42009-09-09 15:08:12 +00001746
David Blaikiee4d798f2012-01-20 21:50:17 +00001747 llvm_unreachable("Invalid TemplateArgument Kind!");
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001748}
1749
Douglas Gregor7baabef2010-12-22 18:17:10 +00001750/// \brief Determine whether there is a template argument to be used for
1751/// deduction.
1752///
1753/// This routine "expands" argument packs in-place, overriding its input
1754/// parameters so that \c Args[ArgIdx] will be the available template argument.
1755///
1756/// \returns true if there is another template argument (which will be at
1757/// \c Args[ArgIdx]), false otherwise.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001758static bool hasTemplateArgumentForDeduction(const TemplateArgument *&Args,
Douglas Gregor7baabef2010-12-22 18:17:10 +00001759 unsigned &ArgIdx,
1760 unsigned &NumArgs) {
1761 if (ArgIdx == NumArgs)
1762 return false;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001763
Douglas Gregor7baabef2010-12-22 18:17:10 +00001764 const TemplateArgument &Arg = Args[ArgIdx];
1765 if (Arg.getKind() != TemplateArgument::Pack)
1766 return true;
1767
1768 assert(ArgIdx == NumArgs - 1 && "Pack not at the end of argument list?");
1769 Args = Arg.pack_begin();
1770 NumArgs = Arg.pack_size();
1771 ArgIdx = 0;
1772 return ArgIdx < NumArgs;
1773}
1774
Douglas Gregord0ad2942010-12-23 01:24:45 +00001775/// \brief Determine whether the given set of template arguments has a pack
1776/// expansion that is not the last template argument.
1777static bool hasPackExpansionBeforeEnd(const TemplateArgument *Args,
1778 unsigned NumArgs) {
1779 unsigned ArgIdx = 0;
1780 while (ArgIdx < NumArgs) {
1781 const TemplateArgument &Arg = Args[ArgIdx];
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001782
Douglas Gregord0ad2942010-12-23 01:24:45 +00001783 // Unwrap argument packs.
1784 if (Args[ArgIdx].getKind() == TemplateArgument::Pack) {
1785 Args = Arg.pack_begin();
1786 NumArgs = Arg.pack_size();
1787 ArgIdx = 0;
1788 continue;
1789 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001790
Douglas Gregord0ad2942010-12-23 01:24:45 +00001791 ++ArgIdx;
1792 if (ArgIdx == NumArgs)
1793 return false;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001794
Douglas Gregord0ad2942010-12-23 01:24:45 +00001795 if (Arg.isPackExpansion())
1796 return true;
1797 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001798
Douglas Gregord0ad2942010-12-23 01:24:45 +00001799 return false;
1800}
1801
Douglas Gregor7baabef2010-12-22 18:17:10 +00001802static Sema::TemplateDeductionResult
1803DeduceTemplateArguments(Sema &S,
1804 TemplateParameterList *TemplateParams,
1805 const TemplateArgument *Params, unsigned NumParams,
1806 const TemplateArgument *Args, unsigned NumArgs,
1807 TemplateDeductionInfo &Info,
Richard Smith16b65392012-12-06 06:44:44 +00001808 SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001809 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001810 // If the template argument list of P contains a pack expansion that is not
1811 // the last template argument, the entire template argument list is a
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001812 // non-deduced context.
Douglas Gregord0ad2942010-12-23 01:24:45 +00001813 if (hasPackExpansionBeforeEnd(Params, NumParams))
1814 return Sema::TDK_Success;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001815
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001816 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001817 // If P has a form that contains <T> or <i>, then each argument Pi of the
1818 // respective template argument list P is compared with the corresponding
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001819 // argument Ai of the corresponding template argument list of A.
Douglas Gregor7baabef2010-12-22 18:17:10 +00001820 unsigned ArgIdx = 0, ParamIdx = 0;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001821 for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams);
Douglas Gregor7baabef2010-12-22 18:17:10 +00001822 ++ParamIdx) {
1823 if (!Params[ParamIdx].isPackExpansion()) {
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001824 // The simple case: deduce template arguments by matching Pi and Ai.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001825
Douglas Gregor7baabef2010-12-22 18:17:10 +00001826 // Check whether we have enough arguments.
1827 if (!hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
Richard Smith16b65392012-12-06 06:44:44 +00001828 return Sema::TDK_Success;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001829
Douglas Gregor2fcb8632011-01-11 22:21:24 +00001830 if (Args[ArgIdx].isPackExpansion()) {
1831 // FIXME: We follow the logic of C++0x [temp.deduct.type]p22 here,
1832 // but applied to pack expansions that are template arguments.
Richard Smith44ecdbd2013-01-31 05:19:49 +00001833 return Sema::TDK_MiscellaneousDeductionFailure;
Douglas Gregor2fcb8632011-01-11 22:21:24 +00001834 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001835
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001836 // Perform deduction for this Pi/Ai pair.
Douglas Gregor7baabef2010-12-22 18:17:10 +00001837 if (Sema::TemplateDeductionResult Result
Douglas Gregor2fcb8632011-01-11 22:21:24 +00001838 = DeduceTemplateArguments(S, TemplateParams,
1839 Params[ParamIdx], Args[ArgIdx],
1840 Info, Deduced))
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001841 return Result;
1842
Douglas Gregor7baabef2010-12-22 18:17:10 +00001843 // Move to the next argument.
1844 ++ArgIdx;
1845 continue;
1846 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001847
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001848 // The parameter is a pack expansion.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001849
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001850 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001851 // If Pi is a pack expansion, then the pattern of Pi is compared with
1852 // each remaining argument in the template argument list of A. Each
1853 // comparison deduces template arguments for subsequent positions in the
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001854 // template parameter packs expanded by Pi.
1855 TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001856
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001857 // Compute the set of template parameter indices that correspond to
1858 // parameter packs expanded by the pack expansion.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001859 SmallVector<unsigned, 2> PackIndices;
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001860 {
Benjamin Kramere0513cb2012-01-30 16:17:39 +00001861 llvm::SmallBitVector SawIndices(TemplateParams->size());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001862 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001863 S.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1864 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
1865 unsigned Depth, Index;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00001866 std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001867 if (Depth == 0 && !SawIndices[Index]) {
1868 SawIndices[Index] = true;
1869 PackIndices.push_back(Index);
1870 }
1871 }
1872 }
1873 assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001874
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001875 // FIXME: If there are no remaining arguments, we can bail out early
1876 // and set any deduced parameter packs to an empty argument pack.
1877 // The latter part of this is a (minor) correctness issue.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001878
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001879 // Save the deduced template arguments for each parameter pack expanded
1880 // by this pack expansion, then clear out the deduction.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001881 SmallVector<DeducedTemplateArgument, 2>
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001882 SavedPacks(PackIndices.size());
Craig Topper0a4e1f52013-07-08 04:44:01 +00001883 NewlyDeducedPacksType NewlyDeducedPacks(PackIndices.size());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001884 PrepareArgumentPackDeduction(S, Deduced, PackIndices, SavedPacks,
Douglas Gregora8bd0d92011-01-10 17:35:05 +00001885 NewlyDeducedPacks);
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001886
1887 // Keep track of the deduced template arguments for each parameter pack
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001888 // expanded by this pack expansion (the outer index) and for each
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001889 // template argument (the inner SmallVectors).
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001890 bool HasAnyArguments = false;
1891 while (hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) {
1892 HasAnyArguments = true;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001893
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001894 // Deduce template arguments from the pattern.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001895 if (Sema::TemplateDeductionResult Result
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001896 = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx],
1897 Info, Deduced))
1898 return Result;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001899
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001900 // Capture the deduced template arguments for each parameter pack expanded
1901 // by this pack expansion, add them to the list of arguments we've deduced
1902 // for that pack, then clear out the deduced argument.
1903 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
1904 DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
1905 if (!DeducedArg.isNull()) {
1906 NewlyDeducedPacks[I].push_back(DeducedArg);
1907 DeducedArg = DeducedTemplateArgument();
1908 }
1909 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001910
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001911 ++ArgIdx;
1912 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001913
Douglas Gregor0f3feb42010-12-22 21:19:48 +00001914 // Build argument packs for each of the parameter packs expanded by this
1915 // pack expansion.
Douglas Gregorb94a6172011-01-10 17:53:52 +00001916 if (Sema::TemplateDeductionResult Result
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001917 = FinishArgumentPackDeduction(S, TemplateParams, HasAnyArguments,
Douglas Gregorb94a6172011-01-10 17:53:52 +00001918 Deduced, PackIndices, SavedPacks,
1919 NewlyDeducedPacks, Info))
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001920 return Result;
Douglas Gregor7baabef2010-12-22 18:17:10 +00001921 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001922
Douglas Gregor7baabef2010-12-22 18:17:10 +00001923 return Sema::TDK_Success;
1924}
1925
Mike Stump11289f42009-09-09 15:08:12 +00001926static Sema::TemplateDeductionResult
Chandler Carruthc1263112010-02-07 21:33:28 +00001927DeduceTemplateArguments(Sema &S,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00001928 TemplateParameterList *TemplateParams,
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001929 const TemplateArgumentList &ParamList,
1930 const TemplateArgumentList &ArgList,
John McCall19c1bfd2010-08-25 05:32:35 +00001931 TemplateDeductionInfo &Info,
Craig Topper79653572013-07-08 04:13:06 +00001932 SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001933 return DeduceTemplateArguments(S, TemplateParams,
Douglas Gregor7baabef2010-12-22 18:17:10 +00001934 ParamList.data(), ParamList.size(),
1935 ArgList.data(), ArgList.size(),
1936 Info, Deduced);
Douglas Gregor55ca8f62009-06-04 00:03:07 +00001937}
1938
Douglas Gregor705c9002009-06-26 20:57:09 +00001939/// \brief Determine whether two template arguments are the same.
Mike Stump11289f42009-09-09 15:08:12 +00001940static bool isSameTemplateArg(ASTContext &Context,
Douglas Gregor705c9002009-06-26 20:57:09 +00001941 const TemplateArgument &X,
1942 const TemplateArgument &Y) {
1943 if (X.getKind() != Y.getKind())
1944 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001945
Douglas Gregor705c9002009-06-26 20:57:09 +00001946 switch (X.getKind()) {
1947 case TemplateArgument::Null:
David Blaikie83d382b2011-09-23 05:06:16 +00001948 llvm_unreachable("Comparing NULL template argument");
Mike Stump11289f42009-09-09 15:08:12 +00001949
Douglas Gregor705c9002009-06-26 20:57:09 +00001950 case TemplateArgument::Type:
1951 return Context.getCanonicalType(X.getAsType()) ==
1952 Context.getCanonicalType(Y.getAsType());
Mike Stump11289f42009-09-09 15:08:12 +00001953
Douglas Gregor705c9002009-06-26 20:57:09 +00001954 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +00001955 return isSameDeclaration(X.getAsDecl(), Y.getAsDecl()) &&
1956 X.isDeclForReferenceParam() == Y.isDeclForReferenceParam();
1957
1958 case TemplateArgument::NullPtr:
1959 return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType());
Mike Stump11289f42009-09-09 15:08:12 +00001960
Douglas Gregor9167f8b2009-11-11 01:00:40 +00001961 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001962 case TemplateArgument::TemplateExpansion:
1963 return Context.getCanonicalTemplateName(
1964 X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() ==
1965 Context.getCanonicalTemplateName(
1966 Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001967
Douglas Gregor705c9002009-06-26 20:57:09 +00001968 case TemplateArgument::Integral:
Benjamin Kramer6003ad52012-06-07 15:09:51 +00001969 return X.getAsIntegral() == Y.getAsIntegral();
Mike Stump11289f42009-09-09 15:08:12 +00001970
Douglas Gregor9167f8b2009-11-11 01:00:40 +00001971 case TemplateArgument::Expression: {
1972 llvm::FoldingSetNodeID XID, YID;
1973 X.getAsExpr()->Profile(XID, Context, true);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00001974 Y.getAsExpr()->Profile(YID, Context, true);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00001975 return XID == YID;
1976 }
Mike Stump11289f42009-09-09 15:08:12 +00001977
Douglas Gregor705c9002009-06-26 20:57:09 +00001978 case TemplateArgument::Pack:
1979 if (X.pack_size() != Y.pack_size())
1980 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001981
1982 for (TemplateArgument::pack_iterator XP = X.pack_begin(),
1983 XPEnd = X.pack_end(),
Douglas Gregor705c9002009-06-26 20:57:09 +00001984 YP = Y.pack_begin();
Mike Stump11289f42009-09-09 15:08:12 +00001985 XP != XPEnd; ++XP, ++YP)
Douglas Gregor705c9002009-06-26 20:57:09 +00001986 if (!isSameTemplateArg(Context, *XP, *YP))
1987 return false;
1988
1989 return true;
1990 }
1991
David Blaikiee4d798f2012-01-20 21:50:17 +00001992 llvm_unreachable("Invalid TemplateArgument Kind!");
Douglas Gregor705c9002009-06-26 20:57:09 +00001993}
1994
Douglas Gregorca4686d2011-01-04 23:35:54 +00001995/// \brief Allocate a TemplateArgumentLoc where all locations have
1996/// been initialized to the given location.
1997///
1998/// \param S The semantic analysis object.
1999///
James Dennett634962f2012-06-14 21:40:34 +00002000/// \param Arg The template argument we are producing template argument
Douglas Gregorca4686d2011-01-04 23:35:54 +00002001/// location information for.
2002///
2003/// \param NTTPType For a declaration template argument, the type of
2004/// the non-type template parameter that corresponds to this template
2005/// argument.
2006///
2007/// \param Loc The source location to use for the resulting template
2008/// argument.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002009static TemplateArgumentLoc
Douglas Gregorca4686d2011-01-04 23:35:54 +00002010getTrivialTemplateArgumentLoc(Sema &S,
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002011 const TemplateArgument &Arg,
Douglas Gregorca4686d2011-01-04 23:35:54 +00002012 QualType NTTPType,
2013 SourceLocation Loc) {
2014 switch (Arg.getKind()) {
2015 case TemplateArgument::Null:
2016 llvm_unreachable("Can't get a NULL template argument here");
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002017
Douglas Gregorca4686d2011-01-04 23:35:54 +00002018 case TemplateArgument::Type:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002019 return TemplateArgumentLoc(Arg,
Douglas Gregorca4686d2011-01-04 23:35:54 +00002020 S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002021
Douglas Gregorca4686d2011-01-04 23:35:54 +00002022 case TemplateArgument::Declaration: {
2023 Expr *E
Douglas Gregoreb29d182011-01-05 17:40:24 +00002024 = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
Douglas Gregor31f55dc2012-04-06 22:40:38 +00002025 .takeAs<Expr>();
Douglas Gregorca4686d2011-01-04 23:35:54 +00002026 return TemplateArgumentLoc(TemplateArgument(E), E);
2027 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002028
Eli Friedmanb826a002012-09-26 02:36:12 +00002029 case TemplateArgument::NullPtr: {
2030 Expr *E
2031 = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
2032 .takeAs<Expr>();
2033 return TemplateArgumentLoc(TemplateArgument(NTTPType, /*isNullPtr*/true),
2034 E);
2035 }
2036
Douglas Gregorca4686d2011-01-04 23:35:54 +00002037 case TemplateArgument::Integral: {
2038 Expr *E
Douglas Gregoreb29d182011-01-05 17:40:24 +00002039 = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>();
Douglas Gregorca4686d2011-01-04 23:35:54 +00002040 return TemplateArgumentLoc(TemplateArgument(E), E);
2041 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002042
Douglas Gregor9d802122011-03-02 17:09:35 +00002043 case TemplateArgument::Template:
2044 case TemplateArgument::TemplateExpansion: {
2045 NestedNameSpecifierLocBuilder Builder;
2046 TemplateName Template = Arg.getAsTemplate();
2047 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
2048 Builder.MakeTrivial(S.Context, DTN->getQualifier(), Loc);
2049 else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
2050 Builder.MakeTrivial(S.Context, QTN->getQualifier(), Loc);
2051
2052 if (Arg.getKind() == TemplateArgument::Template)
2053 return TemplateArgumentLoc(Arg,
2054 Builder.getWithLocInContext(S.Context),
2055 Loc);
2056
2057
2058 return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(S.Context),
2059 Loc, Loc);
2060 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002061
Douglas Gregorca4686d2011-01-04 23:35:54 +00002062 case TemplateArgument::Expression:
2063 return TemplateArgumentLoc(Arg, Arg.getAsExpr());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002064
Douglas Gregorca4686d2011-01-04 23:35:54 +00002065 case TemplateArgument::Pack:
2066 return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo());
2067 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002068
David Blaikiee4d798f2012-01-20 21:50:17 +00002069 llvm_unreachable("Invalid TemplateArgument Kind!");
Douglas Gregorca4686d2011-01-04 23:35:54 +00002070}
2071
2072
2073/// \brief Convert the given deduced template argument and add it to the set of
2074/// fully-converted template arguments.
Craig Topper79653572013-07-08 04:13:06 +00002075static bool
2076ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param,
2077 DeducedTemplateArgument Arg,
2078 NamedDecl *Template,
2079 QualType NTTPType,
2080 unsigned ArgumentPackIndex,
2081 TemplateDeductionInfo &Info,
2082 bool InFunctionTemplate,
2083 SmallVectorImpl<TemplateArgument> &Output) {
Douglas Gregorca4686d2011-01-04 23:35:54 +00002084 if (Arg.getKind() == TemplateArgument::Pack) {
2085 // This is a template argument pack, so check each of its arguments against
2086 // the template parameter.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002087 SmallVector<TemplateArgument, 2> PackedArgsBuilder;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002088 for (TemplateArgument::pack_iterator PA = Arg.pack_begin(),
Douglas Gregorf491ee22011-01-05 21:00:53 +00002089 PAEnd = Arg.pack_end();
Douglas Gregorca4686d2011-01-04 23:35:54 +00002090 PA != PAEnd; ++PA) {
Douglas Gregor51bc5712011-01-05 20:52:18 +00002091 // When converting the deduced template argument, append it to the
2092 // general output list. We need to do this so that the template argument
2093 // checking logic has all of the prior template arguments available.
Douglas Gregorca4686d2011-01-04 23:35:54 +00002094 DeducedTemplateArgument InnerArg(*PA);
2095 InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002096 if (ConvertDeducedTemplateArgument(S, Param, InnerArg, Template,
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002097 NTTPType, PackedArgsBuilder.size(),
2098 Info, InFunctionTemplate, Output))
Douglas Gregorca4686d2011-01-04 23:35:54 +00002099 return true;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002100
Douglas Gregor51bc5712011-01-05 20:52:18 +00002101 // Move the converted template argument into our argument pack.
Robert Wilhelm25284cc2013-08-23 16:11:15 +00002102 PackedArgsBuilder.push_back(Output.pop_back_val());
Douglas Gregorca4686d2011-01-04 23:35:54 +00002103 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002104
Douglas Gregorca4686d2011-01-04 23:35:54 +00002105 // Create the resulting argument pack.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002106 Output.push_back(TemplateArgument::CreatePackCopy(S.Context,
Douglas Gregor74c6d192011-01-11 23:09:57 +00002107 PackedArgsBuilder.data(),
2108 PackedArgsBuilder.size()));
Douglas Gregorca4686d2011-01-04 23:35:54 +00002109 return false;
2110 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002111
Douglas Gregorca4686d2011-01-04 23:35:54 +00002112 // Convert the deduced template argument into a template
2113 // argument that we can check, almost as if the user had written
2114 // the template argument explicitly.
2115 TemplateArgumentLoc ArgLoc = getTrivialTemplateArgumentLoc(S, Arg, NTTPType,
2116 Info.getLocation());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002117
Douglas Gregorca4686d2011-01-04 23:35:54 +00002118 // Check the template argument, converting it as necessary.
2119 return S.CheckTemplateArgument(Param, ArgLoc,
2120 Template,
2121 Template->getLocation(),
2122 Template->getSourceRange().getEnd(),
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002123 ArgumentPackIndex,
Douglas Gregorca4686d2011-01-04 23:35:54 +00002124 Output,
2125 InFunctionTemplate
2126 ? (Arg.wasDeducedFromArrayBound()
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002127 ? Sema::CTAK_DeducedFromArrayBound
Douglas Gregorca4686d2011-01-04 23:35:54 +00002128 : Sema::CTAK_Deduced)
2129 : Sema::CTAK_Specified);
2130}
2131
Douglas Gregor684268d2010-04-29 06:21:43 +00002132/// Complete template argument deduction for a class template partial
2133/// specialization.
2134static Sema::TemplateDeductionResult
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002135FinishTemplateArgumentDeduction(Sema &S,
Douglas Gregor684268d2010-04-29 06:21:43 +00002136 ClassTemplatePartialSpecializationDecl *Partial,
2137 const TemplateArgumentList &TemplateArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002138 SmallVectorImpl<DeducedTemplateArgument> &Deduced,
John McCall19c1bfd2010-08-25 05:32:35 +00002139 TemplateDeductionInfo &Info) {
Eli Friedman77dcc722012-02-08 03:07:05 +00002140 // Unevaluated SFINAE context.
2141 EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated);
Douglas Gregor684268d2010-04-29 06:21:43 +00002142 Sema::SFINAETrap Trap(S);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002143
Douglas Gregor684268d2010-04-29 06:21:43 +00002144 Sema::ContextRAII SavedContext(S, Partial);
2145
2146 // C++ [temp.deduct.type]p2:
2147 // [...] or if any template argument remains neither deduced nor
2148 // explicitly specified, template argument deduction fails.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002149 SmallVector<TemplateArgument, 4> Builder;
Douglas Gregoraef93f22011-01-04 22:23:38 +00002150 TemplateParameterList *PartialParams = Partial->getTemplateParameters();
2151 for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) {
Douglas Gregorca4686d2011-01-04 23:35:54 +00002152 NamedDecl *Param = PartialParams->getParam(I);
Douglas Gregor684268d2010-04-29 06:21:43 +00002153 if (Deduced[I].isNull()) {
Douglas Gregorca4686d2011-01-04 23:35:54 +00002154 Info.Param = makeTemplateParameter(Param);
Douglas Gregor684268d2010-04-29 06:21:43 +00002155 return Sema::TDK_Incomplete;
2156 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002157
Douglas Gregorca4686d2011-01-04 23:35:54 +00002158 // We have deduced this argument, so it still needs to be
2159 // checked and converted.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002160
Douglas Gregorca4686d2011-01-04 23:35:54 +00002161 // First, for a non-type template parameter type that is
2162 // initialized by a declaration, we need the type of the
2163 // corresponding non-type template parameter.
2164 QualType NTTPType;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002165 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor51bc5712011-01-05 20:52:18 +00002166 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregorca4686d2011-01-04 23:35:54 +00002167 NTTPType = NTTP->getType();
Douglas Gregor51bc5712011-01-05 20:52:18 +00002168 if (NTTPType->isDependentType()) {
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002169 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor51bc5712011-01-05 20:52:18 +00002170 Builder.data(), Builder.size());
2171 NTTPType = S.SubstType(NTTPType,
2172 MultiLevelTemplateArgumentList(TemplateArgs),
2173 NTTP->getLocation(),
2174 NTTP->getDeclName());
2175 if (NTTPType.isNull()) {
2176 Info.Param = makeTemplateParameter(Param);
2177 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002178 Info.reset(TemplateArgumentList::CreateCopy(S.Context,
2179 Builder.data(),
Douglas Gregor51bc5712011-01-05 20:52:18 +00002180 Builder.size()));
2181 return Sema::TDK_SubstitutionFailure;
2182 }
2183 }
2184 }
2185
Douglas Gregorca4686d2011-01-04 23:35:54 +00002186 if (ConvertDeducedTemplateArgument(S, Param, Deduced[I],
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002187 Partial, NTTPType, 0, Info, false,
Douglas Gregorca4686d2011-01-04 23:35:54 +00002188 Builder)) {
2189 Info.Param = makeTemplateParameter(Param);
2190 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002191 Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
2192 Builder.size()));
Douglas Gregorca4686d2011-01-04 23:35:54 +00002193 return Sema::TDK_SubstitutionFailure;
2194 }
Douglas Gregor684268d2010-04-29 06:21:43 +00002195 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002196
Douglas Gregor684268d2010-04-29 06:21:43 +00002197 // Form the template argument list from the deduced template arguments.
2198 TemplateArgumentList *DeducedArgumentList
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002199 = TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
Douglas Gregor1ccc8412010-11-07 23:05:16 +00002200 Builder.size());
2201
Douglas Gregor684268d2010-04-29 06:21:43 +00002202 Info.reset(DeducedArgumentList);
2203
2204 // Substitute the deduced template arguments into the template
2205 // arguments of the class template partial specialization, and
2206 // verify that the instantiated template arguments are both valid
2207 // and are equivalent to the template arguments originally provided
2208 // to the class template.
John McCall19c1bfd2010-08-25 05:32:35 +00002209 LocalInstantiationScope InstScope(S);
Douglas Gregor684268d2010-04-29 06:21:43 +00002210 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00002211 const ASTTemplateArgumentListInfo *PartialTemplArgInfo
Douglas Gregor684268d2010-04-29 06:21:43 +00002212 = Partial->getTemplateArgsAsWritten();
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00002213 const TemplateArgumentLoc *PartialTemplateArgs
2214 = PartialTemplArgInfo->getTemplateArgs();
Douglas Gregor684268d2010-04-29 06:21:43 +00002215
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00002216 TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc,
2217 PartialTemplArgInfo->RAngleLoc);
Douglas Gregor684268d2010-04-29 06:21:43 +00002218
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00002219 if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs,
Douglas Gregor0f3feb42010-12-22 21:19:48 +00002220 InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
2221 unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx;
2222 if (ParamIdx >= Partial->getTemplateParameters()->size())
2223 ParamIdx = Partial->getTemplateParameters()->size() - 1;
2224
2225 Decl *Param
2226 = const_cast<NamedDecl *>(
2227 Partial->getTemplateParameters()->getParam(ParamIdx));
2228 Info.Param = makeTemplateParameter(Param);
2229 Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument();
2230 return Sema::TDK_SubstitutionFailure;
Douglas Gregor684268d2010-04-29 06:21:43 +00002231 }
2232
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002233 SmallVector<TemplateArgument, 4> ConvertedInstArgs;
Douglas Gregor684268d2010-04-29 06:21:43 +00002234 if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
Douglas Gregorca4686d2011-01-04 23:35:54 +00002235 InstArgs, false, ConvertedInstArgs))
Douglas Gregor684268d2010-04-29 06:21:43 +00002236 return Sema::TDK_SubstitutionFailure;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002237
Douglas Gregorca4686d2011-01-04 23:35:54 +00002238 TemplateParameterList *TemplateParams
2239 = ClassTemplate->getTemplateParameters();
2240 for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) {
Douglas Gregor1ccc8412010-11-07 23:05:16 +00002241 TemplateArgument InstArg = ConvertedInstArgs.data()[I];
Douglas Gregor684268d2010-04-29 06:21:43 +00002242 if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
Douglas Gregor66990032011-01-05 00:13:17 +00002243 Info.Param = makeTemplateParameter(TemplateParams->getParam(I));
Douglas Gregor684268d2010-04-29 06:21:43 +00002244 Info.FirstArg = TemplateArgs[I];
2245 Info.SecondArg = InstArg;
2246 return Sema::TDK_NonDeducedMismatch;
2247 }
2248 }
2249
2250 if (Trap.hasErrorOccurred())
2251 return Sema::TDK_SubstitutionFailure;
2252
2253 return Sema::TDK_Success;
2254}
2255
Douglas Gregor170bc422009-06-12 22:31:52 +00002256/// \brief Perform template argument deduction to determine whether
Larisse Voufo833b05a2013-08-06 07:33:00 +00002257/// the given template arguments match the given class template
Douglas Gregor170bc422009-06-12 22:31:52 +00002258/// partial specialization per C++ [temp.class.spec.match].
Douglas Gregor181aa4a2009-06-12 18:26:56 +00002259Sema::TemplateDeductionResult
Douglas Gregor55ca8f62009-06-04 00:03:07 +00002260Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00002261 const TemplateArgumentList &TemplateArgs,
2262 TemplateDeductionInfo &Info) {
Douglas Gregorc5c01a62012-09-13 21:01:57 +00002263 if (Partial->isInvalidDecl())
2264 return TDK_Invalid;
2265
Douglas Gregor170bc422009-06-12 22:31:52 +00002266 // C++ [temp.class.spec.match]p2:
2267 // A partial specialization matches a given actual template
2268 // argument list if the template arguments of the partial
2269 // specialization can be deduced from the actual template argument
2270 // list (14.8.2).
Eli Friedman77dcc722012-02-08 03:07:05 +00002271
2272 // Unevaluated SFINAE context.
2273 EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated);
Douglas Gregore1416332009-06-14 08:02:22 +00002274 SFINAETrap Trap(*this);
Eli Friedman77dcc722012-02-08 03:07:05 +00002275
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002276 SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor55ca8f62009-06-04 00:03:07 +00002277 Deduced.resize(Partial->getTemplateParameters()->size());
Douglas Gregor181aa4a2009-06-12 18:26:56 +00002278 if (TemplateDeductionResult Result
Chandler Carruthc1263112010-02-07 21:33:28 +00002279 = ::DeduceTemplateArguments(*this,
Douglas Gregor181aa4a2009-06-12 18:26:56 +00002280 Partial->getTemplateParameters(),
Mike Stump11289f42009-09-09 15:08:12 +00002281 Partial->getTemplateArgs(),
Douglas Gregor181aa4a2009-06-12 18:26:56 +00002282 TemplateArgs, Info, Deduced))
2283 return Result;
Douglas Gregor637d9982009-06-10 23:47:09 +00002284
Richard Smith80934652012-07-16 01:09:10 +00002285 SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end());
Nick Lewycky56412332014-01-11 02:37:12 +00002286 InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs,
2287 Info);
Alp Tokerd4a72d52013-10-08 08:09:04 +00002288 if (Inst.isInvalid())
Douglas Gregor181aa4a2009-06-12 18:26:56 +00002289 return TDK_InstantiationDepth;
Douglas Gregorb7ae10f2009-06-05 00:53:49 +00002290
Douglas Gregore1416332009-06-14 08:02:22 +00002291 if (Trap.hasErrorOccurred())
Douglas Gregor684268d2010-04-29 06:21:43 +00002292 return Sema::TDK_SubstitutionFailure;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002293
2294 return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
Douglas Gregor684268d2010-04-29 06:21:43 +00002295 Deduced, Info);
Douglas Gregor55ca8f62009-06-04 00:03:07 +00002296}
Douglas Gregor91772d12009-06-13 00:26:55 +00002297
Larisse Voufo39a1e502013-08-06 01:03:05 +00002298/// Complete template argument deduction for a variable template partial
2299/// specialization.
Larisse Voufo30616382013-08-23 22:21:36 +00002300/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2301/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2302/// VarTemplate(Partial)SpecializationDecl with a new data
2303/// structure Template(Partial)SpecializationDecl, and
2304/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002305static Sema::TemplateDeductionResult FinishTemplateArgumentDeduction(
2306 Sema &S, VarTemplatePartialSpecializationDecl *Partial,
2307 const TemplateArgumentList &TemplateArgs,
2308 SmallVectorImpl<DeducedTemplateArgument> &Deduced,
2309 TemplateDeductionInfo &Info) {
2310 // Unevaluated SFINAE context.
2311 EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated);
2312 Sema::SFINAETrap Trap(S);
2313
2314 // C++ [temp.deduct.type]p2:
2315 // [...] or if any template argument remains neither deduced nor
2316 // explicitly specified, template argument deduction fails.
2317 SmallVector<TemplateArgument, 4> Builder;
2318 TemplateParameterList *PartialParams = Partial->getTemplateParameters();
2319 for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) {
2320 NamedDecl *Param = PartialParams->getParam(I);
2321 if (Deduced[I].isNull()) {
2322 Info.Param = makeTemplateParameter(Param);
2323 return Sema::TDK_Incomplete;
2324 }
2325
2326 // We have deduced this argument, so it still needs to be
2327 // checked and converted.
2328
2329 // First, for a non-type template parameter type that is
2330 // initialized by a declaration, we need the type of the
2331 // corresponding non-type template parameter.
2332 QualType NTTPType;
2333 if (NonTypeTemplateParmDecl *NTTP =
2334 dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2335 NTTPType = NTTP->getType();
2336 if (NTTPType->isDependentType()) {
2337 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
2338 Builder.data(), Builder.size());
2339 NTTPType =
2340 S.SubstType(NTTPType, MultiLevelTemplateArgumentList(TemplateArgs),
2341 NTTP->getLocation(), NTTP->getDeclName());
2342 if (NTTPType.isNull()) {
2343 Info.Param = makeTemplateParameter(Param);
2344 // FIXME: These template arguments are temporary. Free them!
2345 Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
2346 Builder.size()));
2347 return Sema::TDK_SubstitutionFailure;
2348 }
2349 }
2350 }
2351
2352 if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Partial, NTTPType,
2353 0, Info, false, Builder)) {
2354 Info.Param = makeTemplateParameter(Param);
2355 // FIXME: These template arguments are temporary. Free them!
2356 Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
2357 Builder.size()));
2358 return Sema::TDK_SubstitutionFailure;
2359 }
2360 }
2361
2362 // Form the template argument list from the deduced template arguments.
2363 TemplateArgumentList *DeducedArgumentList = TemplateArgumentList::CreateCopy(
2364 S.Context, Builder.data(), Builder.size());
2365
2366 Info.reset(DeducedArgumentList);
2367
2368 // Substitute the deduced template arguments into the template
2369 // arguments of the class template partial specialization, and
2370 // verify that the instantiated template arguments are both valid
2371 // and are equivalent to the template arguments originally provided
2372 // to the class template.
2373 LocalInstantiationScope InstScope(S);
2374 VarTemplateDecl *VarTemplate = Partial->getSpecializedTemplate();
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00002375 const ASTTemplateArgumentListInfo *PartialTemplArgInfo
2376 = Partial->getTemplateArgsAsWritten();
2377 const TemplateArgumentLoc *PartialTemplateArgs
2378 = PartialTemplArgInfo->getTemplateArgs();
Larisse Voufo39a1e502013-08-06 01:03:05 +00002379
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00002380 TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc,
2381 PartialTemplArgInfo->RAngleLoc);
Larisse Voufo39a1e502013-08-06 01:03:05 +00002382
Enea Zaffanella6dbe1872013-08-10 07:24:53 +00002383 if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs,
Larisse Voufo39a1e502013-08-06 01:03:05 +00002384 InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
2385 unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx;
2386 if (ParamIdx >= Partial->getTemplateParameters()->size())
2387 ParamIdx = Partial->getTemplateParameters()->size() - 1;
2388
2389 Decl *Param = const_cast<NamedDecl *>(
2390 Partial->getTemplateParameters()->getParam(ParamIdx));
2391 Info.Param = makeTemplateParameter(Param);
2392 Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument();
2393 return Sema::TDK_SubstitutionFailure;
2394 }
2395 SmallVector<TemplateArgument, 4> ConvertedInstArgs;
2396 if (S.CheckTemplateArgumentList(VarTemplate, Partial->getLocation(), InstArgs,
2397 false, ConvertedInstArgs))
2398 return Sema::TDK_SubstitutionFailure;
2399
2400 TemplateParameterList *TemplateParams = VarTemplate->getTemplateParameters();
2401 for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) {
2402 TemplateArgument InstArg = ConvertedInstArgs.data()[I];
2403 if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
2404 Info.Param = makeTemplateParameter(TemplateParams->getParam(I));
2405 Info.FirstArg = TemplateArgs[I];
2406 Info.SecondArg = InstArg;
2407 return Sema::TDK_NonDeducedMismatch;
2408 }
2409 }
2410
2411 if (Trap.hasErrorOccurred())
2412 return Sema::TDK_SubstitutionFailure;
2413
2414 return Sema::TDK_Success;
2415}
2416
2417/// \brief Perform template argument deduction to determine whether
2418/// the given template arguments match the given variable template
2419/// partial specialization per C++ [temp.class.spec.match].
Larisse Voufo30616382013-08-23 22:21:36 +00002420/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
2421/// May require unifying ClassTemplate(Partial)SpecializationDecl and
2422/// VarTemplate(Partial)SpecializationDecl with a new data
2423/// structure Template(Partial)SpecializationDecl, and
2424/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00002425Sema::TemplateDeductionResult
2426Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial,
2427 const TemplateArgumentList &TemplateArgs,
2428 TemplateDeductionInfo &Info) {
2429 if (Partial->isInvalidDecl())
2430 return TDK_Invalid;
2431
2432 // C++ [temp.class.spec.match]p2:
2433 // A partial specialization matches a given actual template
2434 // argument list if the template arguments of the partial
2435 // specialization can be deduced from the actual template argument
2436 // list (14.8.2).
2437
2438 // Unevaluated SFINAE context.
2439 EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated);
2440 SFINAETrap Trap(*this);
2441
2442 SmallVector<DeducedTemplateArgument, 4> Deduced;
2443 Deduced.resize(Partial->getTemplateParameters()->size());
2444 if (TemplateDeductionResult Result = ::DeduceTemplateArguments(
2445 *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(),
2446 TemplateArgs, Info, Deduced))
2447 return Result;
2448
2449 SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end());
Nick Lewycky56412332014-01-11 02:37:12 +00002450 InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs,
2451 Info);
Alp Tokerd4a72d52013-10-08 08:09:04 +00002452 if (Inst.isInvalid())
Larisse Voufo39a1e502013-08-06 01:03:05 +00002453 return TDK_InstantiationDepth;
2454
2455 if (Trap.hasErrorOccurred())
2456 return Sema::TDK_SubstitutionFailure;
2457
2458 return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
2459 Deduced, Info);
2460}
2461
Douglas Gregorfc516c92009-06-26 23:27:24 +00002462/// \brief Determine whether the given type T is a simple-template-id type.
2463static bool isSimpleTemplateIdType(QualType T) {
Mike Stump11289f42009-09-09 15:08:12 +00002464 if (const TemplateSpecializationType *Spec
John McCall9dd450b2009-09-21 23:43:11 +00002465 = T->getAs<TemplateSpecializationType>())
Douglas Gregorfc516c92009-06-26 23:27:24 +00002466 return Spec->getTemplateName().getAsTemplateDecl() != 0;
Mike Stump11289f42009-09-09 15:08:12 +00002467
Douglas Gregorfc516c92009-06-26 23:27:24 +00002468 return false;
2469}
Douglas Gregor9b146582009-07-08 20:55:45 +00002470
2471/// \brief Substitute the explicitly-provided template arguments into the
2472/// given function template according to C++ [temp.arg.explicit].
2473///
2474/// \param FunctionTemplate the function template into which the explicit
2475/// template arguments will be substituted.
2476///
James Dennett634962f2012-06-14 21:40:34 +00002477/// \param ExplicitTemplateArgs the explicitly-specified template
Douglas Gregor9b146582009-07-08 20:55:45 +00002478/// arguments.
2479///
Mike Stump11289f42009-09-09 15:08:12 +00002480/// \param Deduced the deduced template arguments, which will be populated
Douglas Gregor9b146582009-07-08 20:55:45 +00002481/// with the converted and checked explicit template arguments.
2482///
Mike Stump11289f42009-09-09 15:08:12 +00002483/// \param ParamTypes will be populated with the instantiated function
Douglas Gregor9b146582009-07-08 20:55:45 +00002484/// parameters.
2485///
2486/// \param FunctionType if non-NULL, the result type of the function template
2487/// will also be instantiated and the pointed-to value will be updated with
2488/// the instantiated function type.
2489///
2490/// \param Info if substitution fails for any reason, this object will be
2491/// populated with more information about the failure.
2492///
2493/// \returns TDK_Success if substitution was successful, or some failure
2494/// condition.
2495Sema::TemplateDeductionResult
2496Sema::SubstituteExplicitTemplateArguments(
2497 FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor739b107a2011-03-03 02:41:12 +00002498 TemplateArgumentListInfo &ExplicitTemplateArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002499 SmallVectorImpl<DeducedTemplateArgument> &Deduced,
2500 SmallVectorImpl<QualType> &ParamTypes,
Douglas Gregor9b146582009-07-08 20:55:45 +00002501 QualType *FunctionType,
2502 TemplateDeductionInfo &Info) {
2503 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
2504 TemplateParameterList *TemplateParams
2505 = FunctionTemplate->getTemplateParameters();
2506
John McCall6b51f282009-11-23 01:53:49 +00002507 if (ExplicitTemplateArgs.size() == 0) {
Douglas Gregor9b146582009-07-08 20:55:45 +00002508 // No arguments to substitute; just copy over the parameter types and
2509 // fill in the function type.
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00002510 for (auto P : Function->params())
2511 ParamTypes.push_back(P->getType());
Mike Stump11289f42009-09-09 15:08:12 +00002512
Douglas Gregor9b146582009-07-08 20:55:45 +00002513 if (FunctionType)
2514 *FunctionType = Function->getType();
2515 return TDK_Success;
2516 }
Mike Stump11289f42009-09-09 15:08:12 +00002517
Eli Friedman77dcc722012-02-08 03:07:05 +00002518 // Unevaluated SFINAE context.
2519 EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated);
Mike Stump11289f42009-09-09 15:08:12 +00002520 SFINAETrap Trap(*this);
2521
Douglas Gregor9b146582009-07-08 20:55:45 +00002522 // C++ [temp.arg.explicit]p3:
Mike Stump11289f42009-09-09 15:08:12 +00002523 // Template arguments that are present shall be specified in the
2524 // declaration order of their corresponding template-parameters. The
Douglas Gregor9b146582009-07-08 20:55:45 +00002525 // template argument list shall not specify more template-arguments than
Mike Stump11289f42009-09-09 15:08:12 +00002526 // there are corresponding template-parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002527 SmallVector<TemplateArgument, 4> Builder;
Mike Stump11289f42009-09-09 15:08:12 +00002528
2529 // Enter a new template instantiation context where we check the
Douglas Gregor9b146582009-07-08 20:55:45 +00002530 // explicitly-specified template arguments against this function template,
2531 // and then substitute them into the function parameter types.
Richard Smith80934652012-07-16 01:09:10 +00002532 SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end());
Nick Lewycky56412332014-01-11 02:37:12 +00002533 InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate,
2534 DeducedArgs,
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002535 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution,
2536 Info);
Alp Tokerd4a72d52013-10-08 08:09:04 +00002537 if (Inst.isInvalid())
Douglas Gregor9b146582009-07-08 20:55:45 +00002538 return TDK_InstantiationDepth;
Mike Stump11289f42009-09-09 15:08:12 +00002539
Douglas Gregor9b146582009-07-08 20:55:45 +00002540 if (CheckTemplateArgumentList(FunctionTemplate,
Douglas Gregor9b146582009-07-08 20:55:45 +00002541 SourceLocation(),
John McCall6b51f282009-11-23 01:53:49 +00002542 ExplicitTemplateArgs,
Douglas Gregor9b146582009-07-08 20:55:45 +00002543 true,
Douglas Gregor1d72edd2010-05-08 19:15:54 +00002544 Builder) || Trap.hasErrorOccurred()) {
Douglas Gregor1ccc8412010-11-07 23:05:16 +00002545 unsigned Index = Builder.size();
Douglas Gregor62c281a2010-05-09 01:26:06 +00002546 if (Index >= TemplateParams->size())
2547 Index = TemplateParams->size() - 1;
2548 Info.Param = makeTemplateParameter(TemplateParams->getParam(Index));
Douglas Gregor9b146582009-07-08 20:55:45 +00002549 return TDK_InvalidExplicitArguments;
Douglas Gregor1d72edd2010-05-08 19:15:54 +00002550 }
Mike Stump11289f42009-09-09 15:08:12 +00002551
Douglas Gregor9b146582009-07-08 20:55:45 +00002552 // Form the template argument list from the explicitly-specified
2553 // template arguments.
Mike Stump11289f42009-09-09 15:08:12 +00002554 TemplateArgumentList *ExplicitArgumentList
Douglas Gregor1ccc8412010-11-07 23:05:16 +00002555 = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
Douglas Gregor9b146582009-07-08 20:55:45 +00002556 Info.reset(ExplicitArgumentList);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002557
John McCall036855a2010-10-12 19:40:14 +00002558 // Template argument deduction and the final substitution should be
2559 // done in the context of the templated declaration. Explicit
2560 // argument substitution, on the other hand, needs to happen in the
2561 // calling context.
2562 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
2563
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002564 // If we deduced template arguments for a template parameter pack,
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002565 // note that the template argument pack is partially substituted and record
2566 // the explicit template arguments. They'll be used as part of deduction
2567 // for this template parameter pack.
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002568 for (unsigned I = 0, N = Builder.size(); I != N; ++I) {
2569 const TemplateArgument &Arg = Builder[I];
2570 if (Arg.getKind() == TemplateArgument::Pack) {
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002571 CurrentInstantiationScope->SetPartiallySubstitutedPack(
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002572 TemplateParams->getParam(I),
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002573 Arg.pack_begin(),
2574 Arg.pack_size());
2575 break;
2576 }
2577 }
2578
Richard Smith5e580292012-02-10 09:58:53 +00002579 const FunctionProtoType *Proto
2580 = Function->getType()->getAs<FunctionProtoType>();
2581 assert(Proto && "Function template does not have a prototype?");
2582
Douglas Gregor9b146582009-07-08 20:55:45 +00002583 // Instantiate the types of each of the function parameters given the
Richard Smith5e580292012-02-10 09:58:53 +00002584 // explicitly-specified template arguments. If the function has a trailing
2585 // return type, substitute it after the arguments to ensure we substitute
2586 // in lexical order.
Douglas Gregor3024f072012-04-16 07:05:22 +00002587 if (Proto->hasTrailingReturn()) {
2588 if (SubstParmTypes(Function->getLocation(),
2589 Function->param_begin(), Function->getNumParams(),
2590 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
2591 ParamTypes))
2592 return TDK_SubstitutionFailure;
2593 }
2594
Richard Smith5e580292012-02-10 09:58:53 +00002595 // Instantiate the return type.
Douglas Gregor3024f072012-04-16 07:05:22 +00002596 QualType ResultType;
2597 {
2598 // C++11 [expr.prim.general]p3:
2599 // If a declaration declares a member function or member function
2600 // template of a class X, the expression this is a prvalue of type
2601 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
2602 // and the end of the function-definition, member-declarator, or
2603 // declarator.
2604 unsigned ThisTypeQuals = 0;
2605 CXXRecordDecl *ThisContext = 0;
2606 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) {
2607 ThisContext = Method->getParent();
2608 ThisTypeQuals = Method->getTypeQualifiers();
2609 }
2610
2611 CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals,
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002612 getLangOpts().CPlusPlus11);
Alp Toker314cc812014-01-25 16:55:45 +00002613
2614 ResultType =
2615 SubstType(Proto->getReturnType(),
2616 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
2617 Function->getTypeSpecStartLoc(), Function->getDeclName());
Douglas Gregor3024f072012-04-16 07:05:22 +00002618 if (ResultType.isNull() || Trap.hasErrorOccurred())
2619 return TDK_SubstitutionFailure;
2620 }
2621
Richard Smith5e580292012-02-10 09:58:53 +00002622 // Instantiate the types of each of the function parameters given the
2623 // explicitly-specified template arguments if we didn't do so earlier.
2624 if (!Proto->hasTrailingReturn() &&
2625 SubstParmTypes(Function->getLocation(),
2626 Function->param_begin(), Function->getNumParams(),
2627 MultiLevelTemplateArgumentList(*ExplicitArgumentList),
2628 ParamTypes))
2629 return TDK_SubstitutionFailure;
2630
Douglas Gregor9b146582009-07-08 20:55:45 +00002631 if (FunctionType) {
Jordan Rose5c382722013-03-08 21:51:21 +00002632 *FunctionType = BuildFunctionType(ResultType, ParamTypes,
Douglas Gregor9b146582009-07-08 20:55:45 +00002633 Function->getLocation(),
Eli Friedmand8725a92010-08-05 02:54:05 +00002634 Function->getDeclName(),
Jordan Rosea0a86be2013-03-08 22:25:36 +00002635 Proto->getExtProtoInfo());
Douglas Gregor9b146582009-07-08 20:55:45 +00002636 if (FunctionType->isNull() || Trap.hasErrorOccurred())
2637 return TDK_SubstitutionFailure;
2638 }
Mike Stump11289f42009-09-09 15:08:12 +00002639
Douglas Gregor9b146582009-07-08 20:55:45 +00002640 // C++ [temp.arg.explicit]p2:
Mike Stump11289f42009-09-09 15:08:12 +00002641 // Trailing template arguments that can be deduced (14.8.2) may be
2642 // omitted from the list of explicit template-arguments. If all of the
Douglas Gregor9b146582009-07-08 20:55:45 +00002643 // template arguments can be deduced, they may all be omitted; in this
2644 // case, the empty template argument list <> itself may also be omitted.
2645 //
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002646 // Take all of the explicitly-specified arguments and put them into
2647 // the set of deduced template arguments. Explicitly-specified
2648 // parameter packs, however, will be set to NULL since the deduction
2649 // mechanisms handle explicitly-specified argument packs directly.
Douglas Gregor9b146582009-07-08 20:55:45 +00002650 Deduced.reserve(TemplateParams->size());
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002651 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) {
2652 const TemplateArgument &Arg = ExplicitArgumentList->get(I);
2653 if (Arg.getKind() == TemplateArgument::Pack)
2654 Deduced.push_back(DeducedTemplateArgument());
2655 else
2656 Deduced.push_back(Arg);
2657 }
Mike Stump11289f42009-09-09 15:08:12 +00002658
Douglas Gregor9b146582009-07-08 20:55:45 +00002659 return TDK_Success;
2660}
2661
Douglas Gregor2ead4c42011-06-17 05:18:17 +00002662/// \brief Check whether the deduced argument type for a call to a function
2663/// template matches the actual argument type per C++ [temp.deduct.call]p4.
2664static bool
2665CheckOriginalCallArgDeduction(Sema &S, Sema::OriginalCallArg OriginalArg,
2666 QualType DeducedA) {
2667 ASTContext &Context = S.Context;
2668
2669 QualType A = OriginalArg.OriginalArgType;
2670 QualType OriginalParamType = OriginalArg.OriginalParamType;
2671
2672 // Check for type equality (top-level cv-qualifiers are ignored).
2673 if (Context.hasSameUnqualifiedType(A, DeducedA))
2674 return false;
2675
2676 // Strip off references on the argument types; they aren't needed for
2677 // the following checks.
2678 if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>())
2679 DeducedA = DeducedARef->getPointeeType();
2680 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
2681 A = ARef->getPointeeType();
2682
2683 // C++ [temp.deduct.call]p4:
2684 // [...] However, there are three cases that allow a difference:
2685 // - If the original P is a reference type, the deduced A (i.e., the
2686 // type referred to by the reference) can be more cv-qualified than
2687 // the transformed A.
2688 if (const ReferenceType *OriginalParamRef
2689 = OriginalParamType->getAs<ReferenceType>()) {
2690 // We don't want to keep the reference around any more.
2691 OriginalParamType = OriginalParamRef->getPointeeType();
2692
2693 Qualifiers AQuals = A.getQualifiers();
2694 Qualifiers DeducedAQuals = DeducedA.getQualifiers();
Douglas Gregora906ad22012-07-18 00:14:59 +00002695
Douglas Gregorc9f019a2013-11-08 02:04:24 +00002696 // Under Objective-C++ ARC, the deduced type may have implicitly
2697 // been given strong or (when dealing with a const reference)
2698 // unsafe_unretained lifetime. If so, update the original
2699 // qualifiers to include this lifetime.
Douglas Gregora906ad22012-07-18 00:14:59 +00002700 if (S.getLangOpts().ObjCAutoRefCount &&
Douglas Gregorc9f019a2013-11-08 02:04:24 +00002701 ((DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong &&
2702 AQuals.getObjCLifetime() == Qualifiers::OCL_None) ||
2703 (DeducedAQuals.hasConst() &&
2704 DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone))) {
2705 AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime());
Douglas Gregora906ad22012-07-18 00:14:59 +00002706 }
2707
Douglas Gregor2ead4c42011-06-17 05:18:17 +00002708 if (AQuals == DeducedAQuals) {
2709 // Qualifiers match; there's nothing to do.
2710 } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) {
Douglas Gregorddaae522011-06-17 14:36:00 +00002711 return true;
Douglas Gregor2ead4c42011-06-17 05:18:17 +00002712 } else {
2713 // Qualifiers are compatible, so have the argument type adopt the
2714 // deduced argument type's qualifiers as if we had performed the
2715 // qualification conversion.
2716 A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals);
2717 }
2718 }
2719
2720 // - The transformed A can be another pointer or pointer to member
2721 // type that can be converted to the deduced A via a qualification
2722 // conversion.
Chandler Carruth53e61b02011-06-18 01:19:03 +00002723 //
2724 // Also allow conversions which merely strip [[noreturn]] from function types
2725 // (recursively) as an extension.
Douglas Gregorc9f019a2013-11-08 02:04:24 +00002726 // FIXME: Currently, this doesn't play nicely with qualification conversions.
Douglas Gregor2ead4c42011-06-17 05:18:17 +00002727 bool ObjCLifetimeConversion = false;
Chandler Carruth53e61b02011-06-18 01:19:03 +00002728 QualType ResultTy;
Douglas Gregor2ead4c42011-06-17 05:18:17 +00002729 if ((A->isAnyPointerType() || A->isMemberPointerType()) &&
Chandler Carruth53e61b02011-06-18 01:19:03 +00002730 (S.IsQualificationConversion(A, DeducedA, false,
2731 ObjCLifetimeConversion) ||
2732 S.IsNoReturnConversion(A, DeducedA, ResultTy)))
Douglas Gregor2ead4c42011-06-17 05:18:17 +00002733 return false;
2734
2735
2736 // - If P is a class and P has the form simple-template-id, then the
2737 // transformed A can be a derived class of the deduced A. [...]
2738 // [...] Likewise, if P is a pointer to a class of the form
2739 // simple-template-id, the transformed A can be a pointer to a
2740 // derived class pointed to by the deduced A.
2741 if (const PointerType *OriginalParamPtr
2742 = OriginalParamType->getAs<PointerType>()) {
2743 if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) {
2744 if (const PointerType *APtr = A->getAs<PointerType>()) {
2745 if (A->getPointeeType()->isRecordType()) {
2746 OriginalParamType = OriginalParamPtr->getPointeeType();
2747 DeducedA = DeducedAPtr->getPointeeType();
2748 A = APtr->getPointeeType();
2749 }
2750 }
2751 }
2752 }
2753
2754 if (Context.hasSameUnqualifiedType(A, DeducedA))
2755 return false;
2756
2757 if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) &&
2758 S.IsDerivedFrom(A, DeducedA))
2759 return false;
2760
2761 return true;
2762}
2763
Mike Stump11289f42009-09-09 15:08:12 +00002764/// \brief Finish template argument deduction for a function template,
Douglas Gregor9b146582009-07-08 20:55:45 +00002765/// checking the deduced template arguments for completeness and forming
2766/// the function template specialization.
Douglas Gregore65aacb2011-06-16 16:50:48 +00002767///
2768/// \param OriginalCallArgs If non-NULL, the original call arguments against
2769/// which the deduced argument types should be compared.
Mike Stump11289f42009-09-09 15:08:12 +00002770Sema::TemplateDeductionResult
Douglas Gregor9b146582009-07-08 20:55:45 +00002771Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002772 SmallVectorImpl<DeducedTemplateArgument> &Deduced,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00002773 unsigned NumExplicitlySpecified,
Douglas Gregor9b146582009-07-08 20:55:45 +00002774 FunctionDecl *&Specialization,
Douglas Gregore65aacb2011-06-16 16:50:48 +00002775 TemplateDeductionInfo &Info,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002776 SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs) {
Douglas Gregor9b146582009-07-08 20:55:45 +00002777 TemplateParameterList *TemplateParams
2778 = FunctionTemplate->getTemplateParameters();
Mike Stump11289f42009-09-09 15:08:12 +00002779
Eli Friedman77dcc722012-02-08 03:07:05 +00002780 // Unevaluated SFINAE context.
2781 EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated);
Mike Stump11289f42009-09-09 15:08:12 +00002782 SFINAETrap Trap(*this);
2783
Douglas Gregor9b146582009-07-08 20:55:45 +00002784 // Enter a new template instantiation context while we instantiate the
2785 // actual function declaration.
Richard Smith80934652012-07-16 01:09:10 +00002786 SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end());
Nick Lewycky56412332014-01-11 02:37:12 +00002787 InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate,
2788 DeducedArgs,
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002789 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution,
2790 Info);
Alp Tokerd4a72d52013-10-08 08:09:04 +00002791 if (Inst.isInvalid())
Mike Stump11289f42009-09-09 15:08:12 +00002792 return TDK_InstantiationDepth;
2793
John McCalle23b8712010-04-29 01:18:58 +00002794 ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
John McCall80e58cd2010-04-29 00:35:03 +00002795
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00002796 // C++ [temp.deduct.type]p2:
2797 // [...] or if any template argument remains neither deduced nor
2798 // explicitly specified, template argument deduction fails.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002799 SmallVector<TemplateArgument, 4> Builder;
Douglas Gregoraaa6a902011-01-04 22:13:36 +00002800 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
2801 NamedDecl *Param = TemplateParams->getParam(I);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002802
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00002803 if (!Deduced[I].isNull()) {
Douglas Gregor6e9cf632010-10-12 18:51:08 +00002804 if (I < NumExplicitlySpecified) {
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00002805 // We have already fully type-checked and converted this
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002806 // argument, because it was explicitly-specified. Just record the
Douglas Gregor6e9cf632010-10-12 18:51:08 +00002807 // presence of this argument.
Douglas Gregor1ccc8412010-11-07 23:05:16 +00002808 Builder.push_back(Deduced[I]);
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00002809 continue;
2810 }
2811
2812 // We have deduced this argument, so it still needs to be
2813 // checked and converted.
2814
2815 // First, for a non-type template parameter type that is
2816 // initialized by a declaration, we need the type of the
2817 // corresponding non-type template parameter.
2818 QualType NTTPType;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002819 if (NonTypeTemplateParmDecl *NTTP
2820 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregoraaa6a902011-01-04 22:13:36 +00002821 NTTPType = NTTP->getType();
2822 if (NTTPType->isDependentType()) {
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002823 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregoraaa6a902011-01-04 22:13:36 +00002824 Builder.data(), Builder.size());
2825 NTTPType = SubstType(NTTPType,
2826 MultiLevelTemplateArgumentList(TemplateArgs),
2827 NTTP->getLocation(),
2828 NTTP->getDeclName());
2829 if (NTTPType.isNull()) {
2830 Info.Param = makeTemplateParameter(Param);
2831 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002832 Info.reset(TemplateArgumentList::CreateCopy(Context,
2833 Builder.data(),
Douglas Gregoraaa6a902011-01-04 22:13:36 +00002834 Builder.size()));
2835 return TDK_SubstitutionFailure;
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00002836 }
2837 }
2838 }
2839
Douglas Gregoraaa6a902011-01-04 22:13:36 +00002840 if (ConvertDeducedTemplateArgument(*this, Param, Deduced[I],
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002841 FunctionTemplate, NTTPType, 0, Info,
Douglas Gregorca4686d2011-01-04 23:35:54 +00002842 true, Builder)) {
Douglas Gregoraaa6a902011-01-04 22:13:36 +00002843 Info.Param = makeTemplateParameter(Param);
Douglas Gregor1ccc8412010-11-07 23:05:16 +00002844 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002845 Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
2846 Builder.size()));
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00002847 return TDK_SubstitutionFailure;
2848 }
2849
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00002850 continue;
2851 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002852
Douglas Gregorca4d91d2010-12-23 01:52:01 +00002853 // C++0x [temp.arg.explicit]p3:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002854 // A trailing template parameter pack (14.5.3) not otherwise deduced will
Douglas Gregorca4d91d2010-12-23 01:52:01 +00002855 // be deduced to an empty sequence of template arguments.
2856 // FIXME: Where did the word "trailing" come from?
2857 if (Param->isTemplateParameterPack()) {
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002858 // We may have had explicitly-specified template arguments for this
2859 // template parameter pack. If so, our empty deduction extends the
2860 // explicitly-specified set (C++0x [temp.arg.explicit]p9).
2861 const TemplateArgument *ExplicitArgs;
2862 unsigned NumExplicitArgs;
Richard Smith802c4b72012-08-23 06:16:52 +00002863 if (CurrentInstantiationScope &&
2864 CurrentInstantiationScope->getPartiallySubstitutedPack(&ExplicitArgs,
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002865 &NumExplicitArgs)
Douglas Gregorcaddba92013-01-18 22:27:09 +00002866 == Param) {
Douglas Gregora8bac7f2011-01-10 07:32:04 +00002867 Builder.push_back(TemplateArgument(ExplicitArgs, NumExplicitArgs));
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002868
Douglas Gregorcaddba92013-01-18 22:27:09 +00002869 // Forget the partially-substituted pack; it's substitution is now
2870 // complete.
2871 CurrentInstantiationScope->ResetPartiallySubstitutedPack();
2872 } else {
2873 Builder.push_back(TemplateArgument::getEmptyPack());
2874 }
Douglas Gregorca4d91d2010-12-23 01:52:01 +00002875 continue;
2876 }
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00002877
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002878 // Substitute into the default template argument, if available.
Richard Smithc87b9382013-07-04 01:01:24 +00002879 bool HasDefaultArg = false;
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00002880 TemplateArgumentLoc DefArg
2881 = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
2882 FunctionTemplate->getLocation(),
2883 FunctionTemplate->getSourceRange().getEnd(),
2884 Param,
Richard Smithc87b9382013-07-04 01:01:24 +00002885 Builder, HasDefaultArg);
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00002886
2887 // If there was no default argument, deduction is incomplete.
2888 if (DefArg.getArgument().isNull()) {
2889 Info.Param = makeTemplateParameter(
2890 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Richard Smithc87b9382013-07-04 01:01:24 +00002891 Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
2892 Builder.size()));
2893 return HasDefaultArg ? TDK_SubstitutionFailure : TDK_Incomplete;
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00002894 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002895
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00002896 // Check whether we can actually use the default argument.
2897 if (CheckTemplateArgument(Param, DefArg,
2898 FunctionTemplate,
2899 FunctionTemplate->getLocation(),
2900 FunctionTemplate->getSourceRange().getEnd(),
Douglas Gregor0231d8d2011-01-19 20:10:05 +00002901 0, Builder,
Douglas Gregor2f157c92011-06-03 02:59:40 +00002902 CTAK_Specified)) {
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00002903 Info.Param = makeTemplateParameter(
2904 const_cast<NamedDecl *>(TemplateParams->getParam(I)));
Douglas Gregor1ccc8412010-11-07 23:05:16 +00002905 // FIXME: These template arguments are temporary. Free them!
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002906 Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
Douglas Gregor1ccc8412010-11-07 23:05:16 +00002907 Builder.size()));
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00002908 return TDK_SubstitutionFailure;
2909 }
2910
2911 // If we get here, we successfully used the default template argument.
2912 }
2913
2914 // Form the template argument list from the deduced template arguments.
2915 TemplateArgumentList *DeducedArgumentList
Douglas Gregor1ccc8412010-11-07 23:05:16 +00002916 = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
Douglas Gregor5c80a27b2009-11-25 18:55:14 +00002917 Info.reset(DeducedArgumentList);
2918
Mike Stump11289f42009-09-09 15:08:12 +00002919 // Substitute the deduced template arguments into the function template
Douglas Gregor9b146582009-07-08 20:55:45 +00002920 // declaration to produce the function template specialization.
Douglas Gregor16142372010-04-28 04:52:24 +00002921 DeclContext *Owner = FunctionTemplate->getDeclContext();
2922 if (FunctionTemplate->getFriendObjectKind())
2923 Owner = FunctionTemplate->getLexicalDeclContext();
Douglas Gregor9b146582009-07-08 20:55:45 +00002924 Specialization = cast_or_null<FunctionDecl>(
Douglas Gregor16142372010-04-28 04:52:24 +00002925 SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner,
Douglas Gregor39cacdb2009-08-28 20:50:45 +00002926 MultiLevelTemplateArgumentList(*DeducedArgumentList)));
Douglas Gregorebcfbb52011-10-12 20:35:48 +00002927 if (!Specialization || Specialization->isInvalidDecl())
Douglas Gregor9b146582009-07-08 20:55:45 +00002928 return TDK_SubstitutionFailure;
Mike Stump11289f42009-09-09 15:08:12 +00002929
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002930 assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
Douglas Gregor31fae892009-09-15 18:26:13 +00002931 FunctionTemplate->getCanonicalDecl());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002932
Mike Stump11289f42009-09-09 15:08:12 +00002933 // If the template argument list is owned by the function template
Douglas Gregor9b146582009-07-08 20:55:45 +00002934 // specialization, release it.
Douglas Gregord09efd42010-05-08 20:07:26 +00002935 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList &&
2936 !Trap.hasErrorOccurred())
Douglas Gregor9b146582009-07-08 20:55:45 +00002937 Info.take();
Mike Stump11289f42009-09-09 15:08:12 +00002938
Douglas Gregorebcfbb52011-10-12 20:35:48 +00002939 // There may have been an error that did not prevent us from constructing a
2940 // declaration. Mark the declaration invalid and return with a substitution
2941 // failure.
2942 if (Trap.hasErrorOccurred()) {
2943 Specialization->setInvalidDecl(true);
2944 return TDK_SubstitutionFailure;
2945 }
2946
Douglas Gregore65aacb2011-06-16 16:50:48 +00002947 if (OriginalCallArgs) {
2948 // C++ [temp.deduct.call]p4:
2949 // In general, the deduction process attempts to find template argument
2950 // values that will make the deduced A identical to A (after the type A
2951 // is transformed as described above). [...]
2952 for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) {
2953 OriginalCallArg OriginalArg = (*OriginalCallArgs)[I];
Douglas Gregore65aacb2011-06-16 16:50:48 +00002954 unsigned ParamIdx = OriginalArg.ArgIdx;
2955
2956 if (ParamIdx >= Specialization->getNumParams())
2957 continue;
2958
2959 QualType DeducedA = Specialization->getParamDecl(ParamIdx)->getType();
Douglas Gregor2ead4c42011-06-17 05:18:17 +00002960 if (CheckOriginalCallArgDeduction(*this, OriginalArg, DeducedA))
2961 return Sema::TDK_SubstitutionFailure;
Douglas Gregore65aacb2011-06-16 16:50:48 +00002962 }
2963 }
2964
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002965 // If we suppressed any diagnostics while performing template argument
2966 // deduction, and if we haven't already instantiated this declaration,
2967 // keep track of these diagnostics. They'll be emitted if this specialization
2968 // is actually used.
2969 if (Info.diag_begin() != Info.diag_end()) {
Craig Topper79be4cd2013-07-05 04:33:53 +00002970 SuppressedDiagnosticsMap::iterator
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002971 Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl());
2972 if (Pos == SuppressedDiagnostics.end())
2973 SuppressedDiagnostics[Specialization->getCanonicalDecl()]
2974 .append(Info.diag_begin(), Info.diag_end());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00002975 }
Douglas Gregor5bb5e4a2010-10-12 23:32:35 +00002976
Mike Stump11289f42009-09-09 15:08:12 +00002977 return TDK_Success;
Douglas Gregor9b146582009-07-08 20:55:45 +00002978}
2979
John McCall8d08b9b2010-08-27 09:08:28 +00002980/// Gets the type of a function for template-argument-deducton
2981/// purposes when it's considered as part of an overload set.
Richard Smith2a7d4812013-05-04 07:00:32 +00002982static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R,
John McCallc1f69982010-02-02 02:21:27 +00002983 FunctionDecl *Fn) {
Richard Smith2a7d4812013-05-04 07:00:32 +00002984 // We may need to deduce the return type of the function now.
Alp Toker314cc812014-01-25 16:55:45 +00002985 if (S.getLangOpts().CPlusPlus1y && Fn->getReturnType()->isUndeducedType() &&
2986 S.DeduceReturnType(Fn, R.Expression->getExprLoc(), /*Diagnose*/ false))
Richard Smith2a7d4812013-05-04 07:00:32 +00002987 return QualType();
2988
John McCallc1f69982010-02-02 02:21:27 +00002989 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
John McCall8d08b9b2010-08-27 09:08:28 +00002990 if (Method->isInstance()) {
2991 // An instance method that's referenced in a form that doesn't
2992 // look like a member pointer is just invalid.
2993 if (!R.HasFormOfMemberPointer) return QualType();
2994
Richard Smith2a7d4812013-05-04 07:00:32 +00002995 return S.Context.getMemberPointerType(Fn->getType(),
2996 S.Context.getTypeDeclType(Method->getParent()).getTypePtr());
John McCall8d08b9b2010-08-27 09:08:28 +00002997 }
2998
2999 if (!R.IsAddressOfOperand) return Fn->getType();
Richard Smith2a7d4812013-05-04 07:00:32 +00003000 return S.Context.getPointerType(Fn->getType());
John McCallc1f69982010-02-02 02:21:27 +00003001}
3002
3003/// Apply the deduction rules for overload sets.
3004///
3005/// \return the null type if this argument should be treated as an
3006/// undeduced context
3007static QualType
3008ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
Douglas Gregor66d2c8e2010-08-30 21:04:23 +00003009 Expr *Arg, QualType ParamType,
3010 bool ParamWasReference) {
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003011
John McCall8d08b9b2010-08-27 09:08:28 +00003012 OverloadExpr::FindResult R = OverloadExpr::find(Arg);
John McCallc1f69982010-02-02 02:21:27 +00003013
John McCall8d08b9b2010-08-27 09:08:28 +00003014 OverloadExpr *Ovl = R.Expression;
John McCallc1f69982010-02-02 02:21:27 +00003015
Douglas Gregor66d2c8e2010-08-30 21:04:23 +00003016 // C++0x [temp.deduct.call]p4
3017 unsigned TDF = 0;
3018 if (ParamWasReference)
3019 TDF |= TDF_ParamWithReferenceType;
3020 if (R.IsAddressOfOperand)
3021 TDF |= TDF_IgnoreQualifiers;
3022
John McCallc1f69982010-02-02 02:21:27 +00003023 // C++0x [temp.deduct.call]p6:
3024 // When P is a function type, pointer to function type, or pointer
3025 // to member function type:
3026
3027 if (!ParamType->isFunctionType() &&
3028 !ParamType->isFunctionPointerType() &&
Douglas Gregor8409ccd2012-03-12 21:09:16 +00003029 !ParamType->isMemberFunctionPointerType()) {
3030 if (Ovl->hasExplicitTemplateArgs()) {
3031 // But we can still look for an explicit specialization.
3032 if (FunctionDecl *ExplicitSpec
3033 = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
Richard Smith2a7d4812013-05-04 07:00:32 +00003034 return GetTypeOfFunction(S, R, ExplicitSpec);
Douglas Gregor8409ccd2012-03-12 21:09:16 +00003035 }
John McCallc1f69982010-02-02 02:21:27 +00003036
Douglas Gregor8409ccd2012-03-12 21:09:16 +00003037 return QualType();
3038 }
3039
3040 // Gather the explicit template arguments, if any.
3041 TemplateArgumentListInfo ExplicitTemplateArgs;
3042 if (Ovl->hasExplicitTemplateArgs())
3043 Ovl->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs);
John McCallc1f69982010-02-02 02:21:27 +00003044 QualType Match;
John McCall1acbbb52010-02-02 06:20:04 +00003045 for (UnresolvedSetIterator I = Ovl->decls_begin(),
3046 E = Ovl->decls_end(); I != E; ++I) {
John McCallc1f69982010-02-02 02:21:27 +00003047 NamedDecl *D = (*I)->getUnderlyingDecl();
3048
Douglas Gregor8409ccd2012-03-12 21:09:16 +00003049 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) {
3050 // - If the argument is an overload set containing one or more
3051 // function templates, the parameter is treated as a
3052 // non-deduced context.
3053 if (!Ovl->hasExplicitTemplateArgs())
3054 return QualType();
3055
3056 // Otherwise, see if we can resolve a function type
3057 FunctionDecl *Specialization = 0;
Craig Toppere6706e42012-09-19 02:26:47 +00003058 TemplateDeductionInfo Info(Ovl->getNameLoc());
Douglas Gregor8409ccd2012-03-12 21:09:16 +00003059 if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs,
3060 Specialization, Info))
3061 continue;
3062
3063 D = Specialization;
3064 }
John McCallc1f69982010-02-02 02:21:27 +00003065
3066 FunctionDecl *Fn = cast<FunctionDecl>(D);
Richard Smith2a7d4812013-05-04 07:00:32 +00003067 QualType ArgType = GetTypeOfFunction(S, R, Fn);
John McCall8d08b9b2010-08-27 09:08:28 +00003068 if (ArgType.isNull()) continue;
John McCallc1f69982010-02-02 02:21:27 +00003069
Douglas Gregor66d2c8e2010-08-30 21:04:23 +00003070 // Function-to-pointer conversion.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003071 if (!ParamWasReference && ParamType->isPointerType() &&
Douglas Gregor66d2c8e2010-08-30 21:04:23 +00003072 ArgType->isFunctionType())
3073 ArgType = S.Context.getPointerType(ArgType);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003074
John McCallc1f69982010-02-02 02:21:27 +00003075 // - If the argument is an overload set (not containing function
3076 // templates), trial argument deduction is attempted using each
3077 // of the members of the set. If deduction succeeds for only one
3078 // of the overload set members, that member is used as the
3079 // argument value for the deduction. If deduction succeeds for
3080 // more than one member of the overload set the parameter is
3081 // treated as a non-deduced context.
3082
3083 // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
3084 // Type deduction is done independently for each P/A pair, and
3085 // the deduced template argument values are then combined.
3086 // So we do not reject deductions which were made elsewhere.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003087 SmallVector<DeducedTemplateArgument, 8>
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00003088 Deduced(TemplateParams->size());
Craig Toppere6706e42012-09-19 02:26:47 +00003089 TemplateDeductionInfo Info(Ovl->getNameLoc());
John McCallc1f69982010-02-02 02:21:27 +00003090 Sema::TemplateDeductionResult Result
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00003091 = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType,
3092 ArgType, Info, Deduced, TDF);
John McCallc1f69982010-02-02 02:21:27 +00003093 if (Result) continue;
3094 if (!Match.isNull()) return QualType();
3095 Match = ArgType;
3096 }
3097
3098 return Match;
3099}
3100
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003101/// \brief Perform the adjustments to the parameter and argument types
Douglas Gregor7825bf32011-01-06 22:09:01 +00003102/// described in C++ [temp.deduct.call].
3103///
3104/// \returns true if the caller should not attempt to perform any template
Richard Smith8c6eeb92013-01-31 04:03:12 +00003105/// argument deduction based on this P/A pair because the argument is an
3106/// overloaded function set that could not be resolved.
Douglas Gregor7825bf32011-01-06 22:09:01 +00003107static bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S,
3108 TemplateParameterList *TemplateParams,
3109 QualType &ParamType,
3110 QualType &ArgType,
3111 Expr *Arg,
3112 unsigned &TDF) {
3113 // C++0x [temp.deduct.call]p3:
NAKAMURA Takumi7c288862011-01-27 07:09:49 +00003114 // If P is a cv-qualified type, the top level cv-qualifiers of P's type
Douglas Gregor7825bf32011-01-06 22:09:01 +00003115 // are ignored for type deduction.
Douglas Gregor17846882011-04-27 23:34:22 +00003116 if (ParamType.hasQualifiers())
3117 ParamType = ParamType.getUnqualifiedType();
Douglas Gregor7825bf32011-01-06 22:09:01 +00003118 const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>();
3119 if (ParamRefType) {
Richard Smith30482bc2011-02-20 03:19:35 +00003120 QualType PointeeType = ParamRefType->getPointeeType();
3121
Richard Smith8c6eeb92013-01-31 04:03:12 +00003122 // If the argument has incomplete array type, try to complete its type.
Douglas Gregor7bfb2d02012-05-04 16:32:21 +00003123 if (ArgType->isIncompleteArrayType() && !S.RequireCompleteExprType(Arg, 0))
Douglas Gregor57d4f972011-06-03 03:35:07 +00003124 ArgType = Arg->getType();
3125
Douglas Gregorcba72b12011-01-21 05:18:22 +00003126 // [C++0x] If P is an rvalue reference to a cv-unqualified
3127 // template parameter and the argument is an lvalue, the type
3128 // "lvalue reference to A" is used in place of A for type
3129 // deduction.
Richard Smith30482bc2011-02-20 03:19:35 +00003130 if (isa<RValueReferenceType>(ParamType)) {
3131 if (!PointeeType.getQualifiers() &&
3132 isa<TemplateTypeParmType>(PointeeType) &&
Douglas Gregor291e8ee2011-05-21 22:16:50 +00003133 Arg->Classify(S.Context).isLValue() &&
3134 Arg->getType() != S.Context.OverloadTy &&
3135 Arg->getType() != S.Context.BoundMemberTy)
Douglas Gregorcba72b12011-01-21 05:18:22 +00003136 ArgType = S.Context.getLValueReferenceType(ArgType);
3137 }
3138
Douglas Gregor7825bf32011-01-06 22:09:01 +00003139 // [...] If P is a reference type, the type referred to by P is used
3140 // for type deduction.
Richard Smith30482bc2011-02-20 03:19:35 +00003141 ParamType = PointeeType;
Douglas Gregor7825bf32011-01-06 22:09:01 +00003142 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003143
Douglas Gregor7825bf32011-01-06 22:09:01 +00003144 // Overload sets usually make this parameter an undeduced
3145 // context, but there are sometimes special circumstances.
3146 if (ArgType == S.Context.OverloadTy) {
3147 ArgType = ResolveOverloadForDeduction(S, TemplateParams,
3148 Arg, ParamType,
3149 ParamRefType != 0);
3150 if (ArgType.isNull())
3151 return true;
3152 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003153
Douglas Gregor7825bf32011-01-06 22:09:01 +00003154 if (ParamRefType) {
3155 // C++0x [temp.deduct.call]p3:
3156 // [...] If P is of the form T&&, where T is a template parameter, and
3157 // the argument is an lvalue, the type A& is used in place of A for
3158 // type deduction.
3159 if (ParamRefType->isRValueReferenceType() &&
3160 ParamRefType->getAs<TemplateTypeParmType>() &&
3161 Arg->isLValue())
3162 ArgType = S.Context.getLValueReferenceType(ArgType);
3163 } else {
3164 // C++ [temp.deduct.call]p2:
3165 // If P is not a reference type:
3166 // - If A is an array type, the pointer type produced by the
3167 // array-to-pointer standard conversion (4.2) is used in place of
3168 // A for type deduction; otherwise,
3169 if (ArgType->isArrayType())
3170 ArgType = S.Context.getArrayDecayedType(ArgType);
3171 // - If A is a function type, the pointer type produced by the
3172 // function-to-pointer standard conversion (4.3) is used in place
3173 // of A for type deduction; otherwise,
3174 else if (ArgType->isFunctionType())
3175 ArgType = S.Context.getPointerType(ArgType);
3176 else {
NAKAMURA Takumi7c288862011-01-27 07:09:49 +00003177 // - If A is a cv-qualified type, the top level cv-qualifiers of A's
Douglas Gregor7825bf32011-01-06 22:09:01 +00003178 // type are ignored for type deduction.
Douglas Gregor17846882011-04-27 23:34:22 +00003179 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor7825bf32011-01-06 22:09:01 +00003180 }
3181 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003182
Douglas Gregor7825bf32011-01-06 22:09:01 +00003183 // C++0x [temp.deduct.call]p4:
3184 // In general, the deduction process attempts to find template argument
3185 // values that will make the deduced A identical to A (after the type A
3186 // is transformed as described above). [...]
3187 TDF = TDF_SkipNonDependent;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003188
Douglas Gregor7825bf32011-01-06 22:09:01 +00003189 // - If the original P is a reference type, the deduced A (i.e., the
3190 // type referred to by the reference) can be more cv-qualified than
3191 // the transformed A.
3192 if (ParamRefType)
3193 TDF |= TDF_ParamWithReferenceType;
3194 // - The transformed A can be another pointer or pointer to member
3195 // type that can be converted to the deduced A via a qualification
3196 // conversion (4.4).
3197 if (ArgType->isPointerType() || ArgType->isMemberPointerType() ||
3198 ArgType->isObjCObjectPointerType())
3199 TDF |= TDF_IgnoreQualifiers;
3200 // - If P is a class and P has the form simple-template-id, then the
3201 // transformed A can be a derived class of the deduced A. Likewise,
3202 // if P is a pointer to a class of the form simple-template-id, the
3203 // transformed A can be a pointer to a derived class pointed to by
3204 // the deduced A.
3205 if (isSimpleTemplateIdType(ParamType) ||
3206 (isa<PointerType>(ParamType) &&
3207 isSimpleTemplateIdType(
3208 ParamType->getAs<PointerType>()->getPointeeType())))
3209 TDF |= TDF_DerivedClass;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003210
Douglas Gregor7825bf32011-01-06 22:09:01 +00003211 return false;
3212}
3213
Douglas Gregore65aacb2011-06-16 16:50:48 +00003214static bool hasDeducibleTemplateParameters(Sema &S,
3215 FunctionTemplateDecl *FunctionTemplate,
3216 QualType T);
3217
Sebastian Redl19181662012-03-15 21:40:51 +00003218/// \brief Perform template argument deduction by matching a parameter type
3219/// against a single expression, where the expression is an element of
Richard Smith8c6eeb92013-01-31 04:03:12 +00003220/// an initializer list that was originally matched against a parameter
3221/// of type \c initializer_list\<ParamType\>.
Sebastian Redl19181662012-03-15 21:40:51 +00003222static Sema::TemplateDeductionResult
3223DeduceTemplateArgumentByListElement(Sema &S,
3224 TemplateParameterList *TemplateParams,
3225 QualType ParamType, Expr *Arg,
3226 TemplateDeductionInfo &Info,
3227 SmallVectorImpl<DeducedTemplateArgument> &Deduced,
3228 unsigned TDF) {
3229 // Handle the case where an init list contains another init list as the
3230 // element.
3231 if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) {
3232 QualType X;
3233 if (!S.isStdInitializerList(ParamType.getNonReferenceType(), &X))
3234 return Sema::TDK_Success; // Just ignore this expression.
3235
3236 // Recurse down into the init list.
3237 for (unsigned i = 0, e = ILE->getNumInits(); i < e; ++i) {
3238 if (Sema::TemplateDeductionResult Result =
3239 DeduceTemplateArgumentByListElement(S, TemplateParams, X,
3240 ILE->getInit(i),
3241 Info, Deduced, TDF))
3242 return Result;
3243 }
3244 return Sema::TDK_Success;
3245 }
3246
3247 // For all other cases, just match by type.
Douglas Gregor0e60cd72012-04-04 05:10:53 +00003248 QualType ArgType = Arg->getType();
3249 if (AdjustFunctionParmAndArgTypesForDeduction(S, TemplateParams, ParamType,
Richard Smith8c6eeb92013-01-31 04:03:12 +00003250 ArgType, Arg, TDF)) {
3251 Info.Expression = Arg;
Douglas Gregor0e60cd72012-04-04 05:10:53 +00003252 return Sema::TDK_FailedOverloadResolution;
Richard Smith8c6eeb92013-01-31 04:03:12 +00003253 }
Sebastian Redl19181662012-03-15 21:40:51 +00003254 return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType,
Douglas Gregor0e60cd72012-04-04 05:10:53 +00003255 ArgType, Info, Deduced, TDF);
Sebastian Redl19181662012-03-15 21:40:51 +00003256}
3257
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00003258/// \brief Perform template argument deduction from a function call
3259/// (C++ [temp.deduct.call]).
3260///
3261/// \param FunctionTemplate the function template for which we are performing
3262/// template argument deduction.
3263///
James Dennett18348b62012-06-22 08:52:37 +00003264/// \param ExplicitTemplateArgs the explicit template arguments provided
Douglas Gregorea0a0a92010-01-11 18:40:55 +00003265/// for this call.
Douglas Gregor89026b52009-06-30 23:57:56 +00003266///
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00003267/// \param Args the function call arguments
3268///
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00003269/// \param Specialization if template argument deduction was successful,
Mike Stump11289f42009-09-09 15:08:12 +00003270/// this will be set to the function template specialization produced by
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00003271/// template argument deduction.
3272///
3273/// \param Info the argument will be updated to provide additional information
3274/// about template argument deduction.
3275///
3276/// \returns the result of template argument deduction.
Robert Wilhelm16e94b92013-08-09 18:02:13 +00003277Sema::TemplateDeductionResult Sema::DeduceTemplateArguments(
3278 FunctionTemplateDecl *FunctionTemplate,
3279 TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args,
3280 FunctionDecl *&Specialization, TemplateDeductionInfo &Info) {
Douglas Gregorc5c01a62012-09-13 21:01:57 +00003281 if (FunctionTemplate->isInvalidDecl())
3282 return TDK_Invalid;
3283
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00003284 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
Douglas Gregor89026b52009-06-30 23:57:56 +00003285
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00003286 // C++ [temp.deduct.call]p1:
3287 // Template argument deduction is done by comparing each function template
3288 // parameter type (call it P) with the type of the corresponding argument
3289 // of the call (call it A) as described below.
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00003290 unsigned CheckArgs = Args.size();
3291 if (Args.size() < Function->getMinRequiredArguments())
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00003292 return TDK_TooFewArguments;
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00003293 else if (Args.size() > Function->getNumParams()) {
Mike Stump11289f42009-09-09 15:08:12 +00003294 const FunctionProtoType *Proto
John McCall9dd450b2009-09-21 23:43:11 +00003295 = Function->getType()->getAs<FunctionProtoType>();
Douglas Gregor7825bf32011-01-06 22:09:01 +00003296 if (Proto->isTemplateVariadic())
3297 /* Do nothing */;
3298 else if (Proto->isVariadic())
3299 CheckArgs = Function->getNumParams();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003300 else
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00003301 return TDK_TooManyArguments;
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00003302 }
Mike Stump11289f42009-09-09 15:08:12 +00003303
Douglas Gregor89026b52009-06-30 23:57:56 +00003304 // The types of the parameters from which we will perform template argument
3305 // deduction.
John McCall19c1bfd2010-08-25 05:32:35 +00003306 LocalInstantiationScope InstScope(*this);
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00003307 TemplateParameterList *TemplateParams
3308 = FunctionTemplate->getTemplateParameters();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003309 SmallVector<DeducedTemplateArgument, 4> Deduced;
3310 SmallVector<QualType, 4> ParamTypes;
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00003311 unsigned NumExplicitlySpecified = 0;
John McCall6b51f282009-11-23 01:53:49 +00003312 if (ExplicitTemplateArgs) {
Douglas Gregor9b146582009-07-08 20:55:45 +00003313 TemplateDeductionResult Result =
3314 SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00003315 *ExplicitTemplateArgs,
Douglas Gregor9b146582009-07-08 20:55:45 +00003316 Deduced,
3317 ParamTypes,
3318 0,
3319 Info);
3320 if (Result)
3321 return Result;
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00003322
3323 NumExplicitlySpecified = Deduced.size();
Douglas Gregor89026b52009-06-30 23:57:56 +00003324 } else {
3325 // Just fill in the parameter types from the function declaration.
Douglas Gregor7825bf32011-01-06 22:09:01 +00003326 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
Douglas Gregor89026b52009-06-30 23:57:56 +00003327 ParamTypes.push_back(Function->getParamDecl(I)->getType());
3328 }
Mike Stump11289f42009-09-09 15:08:12 +00003329
Douglas Gregor89026b52009-06-30 23:57:56 +00003330 // Deduce template arguments from the function parameters.
Mike Stump11289f42009-09-09 15:08:12 +00003331 Deduced.resize(TemplateParams->size());
Douglas Gregor7825bf32011-01-06 22:09:01 +00003332 unsigned ArgIdx = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003333 SmallVector<OriginalCallArg, 4> OriginalCallArgs;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003334 for (unsigned ParamIdx = 0, NumParams = ParamTypes.size();
Douglas Gregor7825bf32011-01-06 22:09:01 +00003335 ParamIdx != NumParams; ++ParamIdx) {
Douglas Gregore65aacb2011-06-16 16:50:48 +00003336 QualType OrigParamType = ParamTypes[ParamIdx];
3337 QualType ParamType = OrigParamType;
3338
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003339 const PackExpansionType *ParamExpansion
Douglas Gregor7825bf32011-01-06 22:09:01 +00003340 = dyn_cast<PackExpansionType>(ParamType);
3341 if (!ParamExpansion) {
3342 // Simple case: matching a function parameter to a function argument.
3343 if (ArgIdx >= CheckArgs)
3344 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003345
Douglas Gregor7825bf32011-01-06 22:09:01 +00003346 Expr *Arg = Args[ArgIdx++];
3347 QualType ArgType = Arg->getType();
Douglas Gregore65aacb2011-06-16 16:50:48 +00003348
Douglas Gregor7825bf32011-01-06 22:09:01 +00003349 unsigned TDF = 0;
3350 if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams,
3351 ParamType, ArgType, Arg,
3352 TDF))
3353 continue;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003354
Douglas Gregor0c83c812011-10-09 22:06:46 +00003355 // If we have nothing to deduce, we're done.
3356 if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType))
3357 continue;
3358
Sebastian Redl43144e72012-01-17 22:49:58 +00003359 // If the argument is an initializer list ...
3360 if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) {
3361 // ... then the parameter is an undeduced context, unless the parameter
3362 // type is (reference to cv) std::initializer_list<P'>, in which case
3363 // deduction is done for each element of the initializer list, and the
3364 // result is the deduced type if it's the same for all elements.
3365 QualType X;
3366 // Removing references was already done.
3367 if (!isStdInitializerList(ParamType, &X))
3368 continue;
3369
3370 for (unsigned i = 0, e = ILE->getNumInits(); i < e; ++i) {
3371 if (TemplateDeductionResult Result =
Sebastian Redl19181662012-03-15 21:40:51 +00003372 DeduceTemplateArgumentByListElement(*this, TemplateParams, X,
3373 ILE->getInit(i),
3374 Info, Deduced, TDF))
Sebastian Redl43144e72012-01-17 22:49:58 +00003375 return Result;
3376 }
3377 // Don't track the argument type, since an initializer list has none.
3378 continue;
3379 }
3380
Douglas Gregore65aacb2011-06-16 16:50:48 +00003381 // Keep track of the argument type and corresponding parameter index,
3382 // so we can check for compatibility between the deduced A and A.
Douglas Gregor0c83c812011-10-09 22:06:46 +00003383 OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx-1,
3384 ArgType));
Douglas Gregore65aacb2011-06-16 16:50:48 +00003385
Douglas Gregor7825bf32011-01-06 22:09:01 +00003386 if (TemplateDeductionResult Result
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00003387 = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams,
3388 ParamType, ArgType,
3389 Info, Deduced, TDF))
Douglas Gregor7825bf32011-01-06 22:09:01 +00003390 return Result;
Mike Stump11289f42009-09-09 15:08:12 +00003391
Douglas Gregor7825bf32011-01-06 22:09:01 +00003392 continue;
Douglas Gregor66d2c8e2010-08-30 21:04:23 +00003393 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003394
Douglas Gregor7825bf32011-01-06 22:09:01 +00003395 // C++0x [temp.deduct.call]p1:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003396 // For a function parameter pack that occurs at the end of the
3397 // parameter-declaration-list, the type A of each remaining argument of
3398 // the call is compared with the type P of the declarator-id of the
3399 // function parameter pack. Each comparison deduces template arguments
3400 // for subsequent positions in the template parameter packs expanded by
Douglas Gregor0dd423e2011-01-11 01:52:23 +00003401 // the function parameter pack. For a function parameter pack that does
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003402 // not occur at the end of the parameter-declaration-list, the type of
Douglas Gregor0dd423e2011-01-11 01:52:23 +00003403 // the parameter pack is a non-deduced context.
3404 if (ParamIdx + 1 < NumParams)
3405 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003406
Douglas Gregor7825bf32011-01-06 22:09:01 +00003407 QualType ParamPattern = ParamExpansion->getPattern();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003408 SmallVector<unsigned, 2> PackIndices;
Douglas Gregor7825bf32011-01-06 22:09:01 +00003409 {
Benjamin Kramere0513cb2012-01-30 16:17:39 +00003410 llvm::SmallBitVector SawIndices(TemplateParams->size());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003411 SmallVector<UnexpandedParameterPack, 2> Unexpanded;
Douglas Gregor7825bf32011-01-06 22:09:01 +00003412 collectUnexpandedParameterPacks(ParamPattern, Unexpanded);
3413 for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
3414 unsigned Depth, Index;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00003415 std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
Douglas Gregor7825bf32011-01-06 22:09:01 +00003416 if (Depth == 0 && !SawIndices[Index]) {
3417 SawIndices[Index] = true;
3418 PackIndices.push_back(Index);
3419 }
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00003420 }
3421 }
Douglas Gregor7825bf32011-01-06 22:09:01 +00003422 assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003423
Douglas Gregor7825bf32011-01-06 22:09:01 +00003424 // Keep track of the deduced template arguments for each parameter pack
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003425 // expanded by this pack expansion (the outer index) and for each
Douglas Gregor7825bf32011-01-06 22:09:01 +00003426 // template argument (the inner SmallVectors).
Craig Topper0a4e1f52013-07-08 04:44:01 +00003427 NewlyDeducedPacksType NewlyDeducedPacks(PackIndices.size());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003428 SmallVector<DeducedTemplateArgument, 2>
Douglas Gregora8bac7f2011-01-10 07:32:04 +00003429 SavedPacks(PackIndices.size());
Douglas Gregora8bd0d92011-01-10 17:35:05 +00003430 PrepareArgumentPackDeduction(*this, Deduced, PackIndices, SavedPacks,
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003431 NewlyDeducedPacks);
Douglas Gregor7825bf32011-01-06 22:09:01 +00003432 bool HasAnyArguments = false;
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00003433 for (; ArgIdx < Args.size(); ++ArgIdx) {
Douglas Gregor7825bf32011-01-06 22:09:01 +00003434 HasAnyArguments = true;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003435
Douglas Gregore65aacb2011-06-16 16:50:48 +00003436 QualType OrigParamType = ParamPattern;
3437 ParamType = OrigParamType;
Douglas Gregor7825bf32011-01-06 22:09:01 +00003438 Expr *Arg = Args[ArgIdx];
3439 QualType ArgType = Arg->getType();
Douglas Gregore65aacb2011-06-16 16:50:48 +00003440
Douglas Gregor7825bf32011-01-06 22:09:01 +00003441 unsigned TDF = 0;
3442 if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams,
3443 ParamType, ArgType, Arg,
3444 TDF)) {
3445 // We can't actually perform any deduction for this argument, so stop
3446 // deduction at this point.
3447 ++ArgIdx;
3448 break;
3449 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003450
Sebastian Redl43144e72012-01-17 22:49:58 +00003451 // As above, initializer lists need special handling.
3452 if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) {
3453 QualType X;
3454 if (!isStdInitializerList(ParamType, &X)) {
3455 ++ArgIdx;
3456 break;
3457 }
Douglas Gregore65aacb2011-06-16 16:50:48 +00003458
Sebastian Redl43144e72012-01-17 22:49:58 +00003459 for (unsigned i = 0, e = ILE->getNumInits(); i < e; ++i) {
3460 if (TemplateDeductionResult Result =
3461 DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, X,
3462 ILE->getInit(i)->getType(),
3463 Info, Deduced, TDF))
3464 return Result;
3465 }
3466 } else {
3467
3468 // Keep track of the argument type and corresponding argument index,
3469 // so we can check for compatibility between the deduced A and A.
3470 if (hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType))
3471 OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx,
3472 ArgType));
3473
3474 if (TemplateDeductionResult Result
3475 = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams,
3476 ParamType, ArgType, Info,
3477 Deduced, TDF))
3478 return Result;
3479 }
Mike Stump11289f42009-09-09 15:08:12 +00003480
Douglas Gregor7825bf32011-01-06 22:09:01 +00003481 // Capture the deduced template arguments for each parameter pack expanded
3482 // by this pack expansion, add them to the list of arguments we've deduced
3483 // for that pack, then clear out the deduced argument.
3484 for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
3485 DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
3486 if (!DeducedArg.isNull()) {
3487 NewlyDeducedPacks[I].push_back(DeducedArg);
3488 DeducedArg = DeducedTemplateArgument();
3489 }
3490 }
3491 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003492
Douglas Gregor7825bf32011-01-06 22:09:01 +00003493 // Build argument packs for each of the parameter packs expanded by this
3494 // pack expansion.
Douglas Gregorb94a6172011-01-10 17:53:52 +00003495 if (Sema::TemplateDeductionResult Result
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003496 = FinishArgumentPackDeduction(*this, TemplateParams, HasAnyArguments,
Douglas Gregorb94a6172011-01-10 17:53:52 +00003497 Deduced, PackIndices, SavedPacks,
3498 NewlyDeducedPacks, Info))
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003499 return Result;
Mike Stump11289f42009-09-09 15:08:12 +00003500
Douglas Gregor7825bf32011-01-06 22:09:01 +00003501 // After we've matching against a parameter pack, we're done.
3502 break;
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00003503 }
Douglas Gregor05155d82009-08-21 23:19:43 +00003504
Mike Stump11289f42009-09-09 15:08:12 +00003505 return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00003506 NumExplicitlySpecified,
Douglas Gregore65aacb2011-06-16 16:50:48 +00003507 Specialization, Info, &OriginalCallArgs);
Douglas Gregorad3f2fc2009-06-25 22:08:12 +00003508}
3509
Rafael Espindola6edca7d2013-12-01 16:54:29 +00003510QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType,
3511 QualType FunctionType) {
3512 if (ArgFunctionType.isNull())
3513 return ArgFunctionType;
3514
3515 const FunctionProtoType *FunctionTypeP =
3516 FunctionType->castAs<FunctionProtoType>();
3517 CallingConv CC = FunctionTypeP->getCallConv();
3518 bool NoReturn = FunctionTypeP->getNoReturnAttr();
3519 const FunctionProtoType *ArgFunctionTypeP =
3520 ArgFunctionType->getAs<FunctionProtoType>();
3521 if (ArgFunctionTypeP->getCallConv() == CC &&
3522 ArgFunctionTypeP->getNoReturnAttr() == NoReturn)
3523 return ArgFunctionType;
3524
3525 FunctionType::ExtInfo EI = ArgFunctionTypeP->getExtInfo().withCallingConv(CC);
3526 EI = EI.withNoReturn(NoReturn);
3527 ArgFunctionTypeP =
3528 cast<FunctionProtoType>(Context.adjustFunctionType(ArgFunctionTypeP, EI));
3529 return QualType(ArgFunctionTypeP, 0);
3530}
3531
Douglas Gregor9b146582009-07-08 20:55:45 +00003532/// \brief Deduce template arguments when taking the address of a function
Douglas Gregor8364e6b2009-12-21 23:17:24 +00003533/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
3534/// a template.
Douglas Gregor9b146582009-07-08 20:55:45 +00003535///
3536/// \param FunctionTemplate the function template for which we are performing
3537/// template argument deduction.
3538///
James Dennett18348b62012-06-22 08:52:37 +00003539/// \param ExplicitTemplateArgs the explicitly-specified template
Douglas Gregor8364e6b2009-12-21 23:17:24 +00003540/// arguments.
Douglas Gregor9b146582009-07-08 20:55:45 +00003541///
3542/// \param ArgFunctionType the function type that will be used as the
3543/// "argument" type (A) when performing template argument deduction from the
Douglas Gregor8364e6b2009-12-21 23:17:24 +00003544/// function template's function type. This type may be NULL, if there is no
3545/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
Douglas Gregor9b146582009-07-08 20:55:45 +00003546///
3547/// \param Specialization if template argument deduction was successful,
Mike Stump11289f42009-09-09 15:08:12 +00003548/// this will be set to the function template specialization produced by
Douglas Gregor9b146582009-07-08 20:55:45 +00003549/// template argument deduction.
3550///
3551/// \param Info the argument will be updated to provide additional information
3552/// about template argument deduction.
3553///
3554/// \returns the result of template argument deduction.
3555Sema::TemplateDeductionResult
3556Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor739b107a2011-03-03 02:41:12 +00003557 TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor9b146582009-07-08 20:55:45 +00003558 QualType ArgFunctionType,
3559 FunctionDecl *&Specialization,
Douglas Gregor19a41f12013-04-17 08:45:07 +00003560 TemplateDeductionInfo &Info,
3561 bool InOverloadResolution) {
Douglas Gregorc5c01a62012-09-13 21:01:57 +00003562 if (FunctionTemplate->isInvalidDecl())
3563 return TDK_Invalid;
3564
Douglas Gregor9b146582009-07-08 20:55:45 +00003565 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
3566 TemplateParameterList *TemplateParams
3567 = FunctionTemplate->getTemplateParameters();
3568 QualType FunctionType = Function->getType();
Rafael Espindola6edca7d2013-12-01 16:54:29 +00003569 if (!InOverloadResolution)
3570 ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType);
Mike Stump11289f42009-09-09 15:08:12 +00003571
Douglas Gregor9b146582009-07-08 20:55:45 +00003572 // Substitute any explicit template arguments.
John McCall19c1bfd2010-08-25 05:32:35 +00003573 LocalInstantiationScope InstScope(*this);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003574 SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00003575 unsigned NumExplicitlySpecified = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003576 SmallVector<QualType, 4> ParamTypes;
John McCall6b51f282009-11-23 01:53:49 +00003577 if (ExplicitTemplateArgs) {
Mike Stump11289f42009-09-09 15:08:12 +00003578 if (TemplateDeductionResult Result
3579 = SubstituteExplicitTemplateArguments(FunctionTemplate,
John McCall6b51f282009-11-23 01:53:49 +00003580 *ExplicitTemplateArgs,
Mike Stump11289f42009-09-09 15:08:12 +00003581 Deduced, ParamTypes,
Douglas Gregor9b146582009-07-08 20:55:45 +00003582 &FunctionType, Info))
3583 return Result;
Douglas Gregord5cb1dd2010-03-28 02:42:43 +00003584
3585 NumExplicitlySpecified = Deduced.size();
Douglas Gregor9b146582009-07-08 20:55:45 +00003586 }
3587
Eli Friedman77dcc722012-02-08 03:07:05 +00003588 // Unevaluated SFINAE context.
3589 EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated);
Mike Stump11289f42009-09-09 15:08:12 +00003590 SFINAETrap Trap(*this);
3591
John McCallc1f69982010-02-02 02:21:27 +00003592 Deduced.resize(TemplateParams->size());
3593
Richard Smith2a7d4812013-05-04 07:00:32 +00003594 // If the function has a deduced return type, substitute it for a dependent
3595 // type so that we treat it as a non-deduced context in what follows.
Richard Smithc58f38f2013-08-14 20:16:31 +00003596 bool HasDeducedReturnType = false;
Richard Smith2a7d4812013-05-04 07:00:32 +00003597 if (getLangOpts().CPlusPlus1y && InOverloadResolution &&
Alp Toker314cc812014-01-25 16:55:45 +00003598 Function->getReturnType()->getContainedAutoType()) {
Richard Smith2a7d4812013-05-04 07:00:32 +00003599 FunctionType = SubstAutoType(FunctionType, Context.DependentTy);
Richard Smithc58f38f2013-08-14 20:16:31 +00003600 HasDeducedReturnType = true;
Richard Smith2a7d4812013-05-04 07:00:32 +00003601 }
3602
Douglas Gregor8364e6b2009-12-21 23:17:24 +00003603 if (!ArgFunctionType.isNull()) {
Douglas Gregor19a41f12013-04-17 08:45:07 +00003604 unsigned TDF = TDF_TopLevelParameterTypeList;
3605 if (InOverloadResolution) TDF |= TDF_InOverloadResolution;
Douglas Gregor8364e6b2009-12-21 23:17:24 +00003606 // Deduce template arguments from the function type.
Douglas Gregor8364e6b2009-12-21 23:17:24 +00003607 if (TemplateDeductionResult Result
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00003608 = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams,
Douglas Gregor19a41f12013-04-17 08:45:07 +00003609 FunctionType, ArgFunctionType,
3610 Info, Deduced, TDF))
Douglas Gregor8364e6b2009-12-21 23:17:24 +00003611 return Result;
3612 }
Douglas Gregor4ed49f32010-09-29 21:14:36 +00003613
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00003614 if (TemplateDeductionResult Result
Douglas Gregor4ed49f32010-09-29 21:14:36 +00003615 = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
3616 NumExplicitlySpecified,
3617 Specialization, Info))
3618 return Result;
3619
Richard Smith2a7d4812013-05-04 07:00:32 +00003620 // If the function has a deduced return type, deduce it now, so we can check
3621 // that the deduced function type matches the requested type.
Richard Smithc58f38f2013-08-14 20:16:31 +00003622 if (HasDeducedReturnType &&
Alp Toker314cc812014-01-25 16:55:45 +00003623 Specialization->getReturnType()->isUndeducedType() &&
Richard Smith2a7d4812013-05-04 07:00:32 +00003624 DeduceReturnType(Specialization, Info.getLocation(), false))
3625 return TDK_MiscellaneousDeductionFailure;
3626
Douglas Gregor4ed49f32010-09-29 21:14:36 +00003627 // If the requested function type does not match the actual type of the
Douglas Gregor19a41f12013-04-17 08:45:07 +00003628 // specialization with respect to arguments of compatible pointer to function
3629 // types, template argument deduction fails.
3630 if (!ArgFunctionType.isNull()) {
3631 if (InOverloadResolution && !isSameOrCompatibleFunctionType(
3632 Context.getCanonicalType(Specialization->getType()),
3633 Context.getCanonicalType(ArgFunctionType)))
3634 return TDK_MiscellaneousDeductionFailure;
3635 else if(!InOverloadResolution &&
3636 !Context.hasSameType(Specialization->getType(), ArgFunctionType))
3637 return TDK_MiscellaneousDeductionFailure;
3638 }
Douglas Gregor4ed49f32010-09-29 21:14:36 +00003639
3640 return TDK_Success;
Douglas Gregor9b146582009-07-08 20:55:45 +00003641}
3642
Faisal Vali850da1a2013-09-29 17:08:32 +00003643/// \brief Given a function declaration (e.g. a generic lambda conversion
3644/// function) that contains an 'auto' in its result type, substitute it
Faisal Vali2b3a3012013-10-24 23:40:02 +00003645/// with TypeToReplaceAutoWith. Be careful to pass in the type you want
3646/// to replace 'auto' with and not the actual result type you want
3647/// to set the function to.
Faisal Vali571df122013-09-29 08:45:24 +00003648static inline void
Faisal Vali2b3a3012013-10-24 23:40:02 +00003649SubstAutoWithinFunctionReturnType(FunctionDecl *F,
Faisal Vali571df122013-09-29 08:45:24 +00003650 QualType TypeToReplaceAutoWith, Sema &S) {
Faisal Vali2b3a3012013-10-24 23:40:02 +00003651 assert(!TypeToReplaceAutoWith->getContainedAutoType());
Alp Toker314cc812014-01-25 16:55:45 +00003652 QualType AutoResultType = F->getReturnType();
Faisal Vali850da1a2013-09-29 17:08:32 +00003653 assert(AutoResultType->getContainedAutoType());
3654 QualType DeducedResultType = S.SubstAutoType(AutoResultType,
Faisal Vali571df122013-09-29 08:45:24 +00003655 TypeToReplaceAutoWith);
3656 S.Context.adjustDeducedFunctionResultType(F, DeducedResultType);
3657}
Faisal Vali2b3a3012013-10-24 23:40:02 +00003658
3659/// \brief Given a specialized conversion operator of a generic lambda
3660/// create the corresponding specializations of the call operator and
3661/// the static-invoker. If the return type of the call operator is auto,
3662/// deduce its return type and check if that matches the
3663/// return type of the destination function ptr.
3664
3665static inline Sema::TemplateDeductionResult
3666SpecializeCorrespondingLambdaCallOperatorAndInvoker(
3667 CXXConversionDecl *ConversionSpecialized,
3668 SmallVectorImpl<DeducedTemplateArgument> &DeducedArguments,
3669 QualType ReturnTypeOfDestFunctionPtr,
3670 TemplateDeductionInfo &TDInfo,
3671 Sema &S) {
3672
3673 CXXRecordDecl *LambdaClass = ConversionSpecialized->getParent();
3674 assert(LambdaClass && LambdaClass->isGenericLambda());
3675
3676 CXXMethodDecl *CallOpGeneric = LambdaClass->getLambdaCallOperator();
Alp Toker314cc812014-01-25 16:55:45 +00003677 QualType CallOpResultType = CallOpGeneric->getReturnType();
Faisal Vali2b3a3012013-10-24 23:40:02 +00003678 const bool GenericLambdaCallOperatorHasDeducedReturnType =
3679 CallOpResultType->getContainedAutoType();
3680
3681 FunctionTemplateDecl *CallOpTemplate =
3682 CallOpGeneric->getDescribedFunctionTemplate();
3683
3684 FunctionDecl *CallOpSpecialized = 0;
3685 // Use the deduced arguments of the conversion function, to specialize our
3686 // generic lambda's call operator.
3687 if (Sema::TemplateDeductionResult Result
3688 = S.FinishTemplateArgumentDeduction(CallOpTemplate,
3689 DeducedArguments,
3690 0, CallOpSpecialized, TDInfo))
3691 return Result;
3692
3693 // If we need to deduce the return type, do so (instantiates the callop).
Alp Toker314cc812014-01-25 16:55:45 +00003694 if (GenericLambdaCallOperatorHasDeducedReturnType &&
3695 CallOpSpecialized->getReturnType()->isUndeducedType())
Faisal Vali2b3a3012013-10-24 23:40:02 +00003696 S.DeduceReturnType(CallOpSpecialized,
3697 CallOpSpecialized->getPointOfInstantiation(),
3698 /*Diagnose*/ true);
3699
3700 // Check to see if the return type of the destination ptr-to-function
3701 // matches the return type of the call operator.
Alp Toker314cc812014-01-25 16:55:45 +00003702 if (!S.Context.hasSameType(CallOpSpecialized->getReturnType(),
Faisal Vali2b3a3012013-10-24 23:40:02 +00003703 ReturnTypeOfDestFunctionPtr))
3704 return Sema::TDK_NonDeducedMismatch;
3705 // Since we have succeeded in matching the source and destination
3706 // ptr-to-functions (now including return type), and have successfully
3707 // specialized our corresponding call operator, we are ready to
3708 // specialize the static invoker with the deduced arguments of our
3709 // ptr-to-function.
3710 FunctionDecl *InvokerSpecialized = 0;
3711 FunctionTemplateDecl *InvokerTemplate = LambdaClass->
3712 getLambdaStaticInvoker()->getDescribedFunctionTemplate();
3713
3714 Sema::TemplateDeductionResult LLVM_ATTRIBUTE_UNUSED Result
3715 = S.FinishTemplateArgumentDeduction(InvokerTemplate, DeducedArguments, 0,
3716 InvokerSpecialized, TDInfo);
3717 assert(Result == Sema::TDK_Success &&
3718 "If the call operator succeeded so should the invoker!");
3719 // Set the result type to match the corresponding call operator
3720 // specialization's result type.
Alp Toker314cc812014-01-25 16:55:45 +00003721 if (GenericLambdaCallOperatorHasDeducedReturnType &&
3722 InvokerSpecialized->getReturnType()->isUndeducedType()) {
Faisal Vali2b3a3012013-10-24 23:40:02 +00003723 // Be sure to get the type to replace 'auto' with and not
3724 // the full result type of the call op specialization
3725 // to substitute into the 'auto' of the invoker and conversion
3726 // function.
3727 // For e.g.
3728 // int* (*fp)(int*) = [](auto* a) -> auto* { return a; };
3729 // We don't want to subst 'int*' into 'auto' to get int**.
3730
Alp Toker314cc812014-01-25 16:55:45 +00003731 QualType TypeToReplaceAutoWith = CallOpSpecialized->getReturnType()
3732 ->getContainedAutoType()
3733 ->getDeducedType();
Faisal Vali2b3a3012013-10-24 23:40:02 +00003734 SubstAutoWithinFunctionReturnType(InvokerSpecialized,
3735 TypeToReplaceAutoWith, S);
3736 SubstAutoWithinFunctionReturnType(ConversionSpecialized,
3737 TypeToReplaceAutoWith, S);
3738 }
3739
3740 // Ensure that static invoker doesn't have a const qualifier.
3741 // FIXME: When creating the InvokerTemplate in SemaLambda.cpp
3742 // do not use the CallOperator's TypeSourceInfo which allows
3743 // the const qualifier to leak through.
3744 const FunctionProtoType *InvokerFPT = InvokerSpecialized->
3745 getType().getTypePtr()->castAs<FunctionProtoType>();
3746 FunctionProtoType::ExtProtoInfo EPI = InvokerFPT->getExtProtoInfo();
3747 EPI.TypeQuals = 0;
3748 InvokerSpecialized->setType(S.Context.getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00003749 InvokerFPT->getReturnType(), InvokerFPT->getParamTypes(), EPI));
Faisal Vali2b3a3012013-10-24 23:40:02 +00003750 return Sema::TDK_Success;
3751}
Douglas Gregor05155d82009-08-21 23:19:43 +00003752/// \brief Deduce template arguments for a templated conversion
3753/// function (C++ [temp.deduct.conv]) and, if successful, produce a
3754/// conversion function template specialization.
3755Sema::TemplateDeductionResult
Faisal Vali571df122013-09-29 08:45:24 +00003756Sema::DeduceTemplateArguments(FunctionTemplateDecl *ConversionTemplate,
Douglas Gregor05155d82009-08-21 23:19:43 +00003757 QualType ToType,
3758 CXXConversionDecl *&Specialization,
3759 TemplateDeductionInfo &Info) {
Faisal Vali571df122013-09-29 08:45:24 +00003760 if (ConversionTemplate->isInvalidDecl())
Douglas Gregorc5c01a62012-09-13 21:01:57 +00003761 return TDK_Invalid;
3762
Faisal Vali2b3a3012013-10-24 23:40:02 +00003763 CXXConversionDecl *ConversionGeneric
Faisal Vali571df122013-09-29 08:45:24 +00003764 = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl());
3765
Faisal Vali2b3a3012013-10-24 23:40:02 +00003766 QualType FromType = ConversionGeneric->getConversionType();
Douglas Gregor05155d82009-08-21 23:19:43 +00003767
3768 // Canonicalize the types for deduction.
3769 QualType P = Context.getCanonicalType(FromType);
3770 QualType A = Context.getCanonicalType(ToType);
3771
Douglas Gregord99609a2011-03-06 09:03:20 +00003772 // C++0x [temp.deduct.conv]p2:
Douglas Gregor05155d82009-08-21 23:19:43 +00003773 // If P is a reference type, the type referred to by P is used for
3774 // type deduction.
3775 if (const ReferenceType *PRef = P->getAs<ReferenceType>())
3776 P = PRef->getPointeeType();
3777
Douglas Gregord99609a2011-03-06 09:03:20 +00003778 // C++0x [temp.deduct.conv]p4:
3779 // [...] If A is a reference type, the type referred to by A is used
Douglas Gregor05155d82009-08-21 23:19:43 +00003780 // for type deduction.
3781 if (const ReferenceType *ARef = A->getAs<ReferenceType>())
Douglas Gregord99609a2011-03-06 09:03:20 +00003782 A = ARef->getPointeeType().getUnqualifiedType();
3783 // C++ [temp.deduct.conv]p3:
Douglas Gregor05155d82009-08-21 23:19:43 +00003784 //
Mike Stump11289f42009-09-09 15:08:12 +00003785 // If A is not a reference type:
Douglas Gregor05155d82009-08-21 23:19:43 +00003786 else {
3787 assert(!A->isReferenceType() && "Reference types were handled above");
3788
3789 // - If P is an array type, the pointer type produced by the
Mike Stump11289f42009-09-09 15:08:12 +00003790 // array-to-pointer standard conversion (4.2) is used in place
Douglas Gregor05155d82009-08-21 23:19:43 +00003791 // of P for type deduction; otherwise,
3792 if (P->isArrayType())
3793 P = Context.getArrayDecayedType(P);
3794 // - If P is a function type, the pointer type produced by the
3795 // function-to-pointer standard conversion (4.3) is used in
3796 // place of P for type deduction; otherwise,
3797 else if (P->isFunctionType())
3798 P = Context.getPointerType(P);
3799 // - If P is a cv-qualified type, the top level cv-qualifiers of
NAKAMURA Takumi7c288862011-01-27 07:09:49 +00003800 // P's type are ignored for type deduction.
Douglas Gregor05155d82009-08-21 23:19:43 +00003801 else
3802 P = P.getUnqualifiedType();
3803
Douglas Gregord99609a2011-03-06 09:03:20 +00003804 // C++0x [temp.deduct.conv]p4:
NAKAMURA Takumi7c288862011-01-27 07:09:49 +00003805 // If A is a cv-qualified type, the top level cv-qualifiers of A's
Douglas Gregord99609a2011-03-06 09:03:20 +00003806 // type are ignored for type deduction. If A is a reference type, the type
3807 // referred to by A is used for type deduction.
Douglas Gregor05155d82009-08-21 23:19:43 +00003808 A = A.getUnqualifiedType();
3809 }
3810
Eli Friedman77dcc722012-02-08 03:07:05 +00003811 // Unevaluated SFINAE context.
3812 EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated);
Mike Stump11289f42009-09-09 15:08:12 +00003813 SFINAETrap Trap(*this);
Douglas Gregor05155d82009-08-21 23:19:43 +00003814
3815 // C++ [temp.deduct.conv]p1:
3816 // Template argument deduction is done by comparing the return
3817 // type of the template conversion function (call it P) with the
3818 // type that is required as the result of the conversion (call it
3819 // A) as described in 14.8.2.4.
3820 TemplateParameterList *TemplateParams
Faisal Vali571df122013-09-29 08:45:24 +00003821 = ConversionTemplate->getTemplateParameters();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003822 SmallVector<DeducedTemplateArgument, 4> Deduced;
Mike Stump11289f42009-09-09 15:08:12 +00003823 Deduced.resize(TemplateParams->size());
Douglas Gregor05155d82009-08-21 23:19:43 +00003824
3825 // C++0x [temp.deduct.conv]p4:
3826 // In general, the deduction process attempts to find template
3827 // argument values that will make the deduced A identical to
3828 // A. However, there are two cases that allow a difference:
3829 unsigned TDF = 0;
3830 // - If the original A is a reference type, A can be more
3831 // cv-qualified than the deduced A (i.e., the type referred to
3832 // by the reference)
3833 if (ToType->isReferenceType())
3834 TDF |= TDF_ParamWithReferenceType;
3835 // - The deduced A can be another pointer or pointer to member
NAKAMURA Takumi7c288862011-01-27 07:09:49 +00003836 // type that can be converted to A via a qualification
Douglas Gregor05155d82009-08-21 23:19:43 +00003837 // conversion.
3838 //
3839 // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
3840 // both P and A are pointers or member pointers. In this case, we
3841 // just ignore cv-qualifiers completely).
3842 if ((P->isPointerType() && A->isPointerType()) ||
Douglas Gregor9f05ed52011-08-30 00:37:54 +00003843 (P->isMemberPointerType() && A->isMemberPointerType()))
Douglas Gregor05155d82009-08-21 23:19:43 +00003844 TDF |= TDF_IgnoreQualifiers;
3845 if (TemplateDeductionResult Result
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00003846 = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams,
3847 P, A, Info, Deduced, TDF))
Douglas Gregor05155d82009-08-21 23:19:43 +00003848 return Result;
Faisal Vali850da1a2013-09-29 17:08:32 +00003849
3850 // Create an Instantiation Scope for finalizing the operator.
3851 LocalInstantiationScope InstScope(*this);
Douglas Gregor05155d82009-08-21 23:19:43 +00003852 // Finish template argument deduction.
Faisal Vali2b3a3012013-10-24 23:40:02 +00003853 FunctionDecl *ConversionSpecialized = 0;
Faisal Vali850da1a2013-09-29 17:08:32 +00003854 TemplateDeductionResult Result
Faisal Vali2b3a3012013-10-24 23:40:02 +00003855 = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0,
3856 ConversionSpecialized, Info);
3857 Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized);
3858
3859 // If the conversion operator is being invoked on a lambda closure to convert
3860 // to a ptr-to-function, use the deduced arguments from the conversion function
3861 // to specialize the corresponding call operator.
3862 // e.g., int (*fp)(int) = [](auto a) { return a; };
3863 if (Result == TDK_Success && isLambdaConversionOperator(ConversionGeneric)) {
3864
3865 // Get the return type of the destination ptr-to-function we are converting
3866 // to. This is necessary for matching the lambda call operator's return
3867 // type to that of the destination ptr-to-function's return type.
3868 assert(A->isPointerType() &&
3869 "Can only convert from lambda to ptr-to-function");
3870 const FunctionType *ToFunType =
3871 A->getPointeeType().getTypePtr()->getAs<FunctionType>();
Alp Toker314cc812014-01-25 16:55:45 +00003872 const QualType DestFunctionPtrReturnType = ToFunType->getReturnType();
3873
Faisal Vali2b3a3012013-10-24 23:40:02 +00003874 // Create the corresponding specializations of the call operator and
3875 // the static-invoker; and if the return type is auto,
3876 // deduce the return type and check if it matches the
3877 // DestFunctionPtrReturnType.
3878 // For instance:
3879 // auto L = [](auto a) { return f(a); };
3880 // int (*fp)(int) = L;
3881 // char (*fp2)(int) = L; <-- Not OK.
3882
3883 Result = SpecializeCorrespondingLambdaCallOperatorAndInvoker(
3884 Specialization, Deduced, DestFunctionPtrReturnType,
3885 Info, *this);
3886 }
Douglas Gregor05155d82009-08-21 23:19:43 +00003887 return Result;
3888}
3889
Douglas Gregor8364e6b2009-12-21 23:17:24 +00003890/// \brief Deduce template arguments for a function template when there is
3891/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
3892///
3893/// \param FunctionTemplate the function template for which we are performing
3894/// template argument deduction.
3895///
James Dennett18348b62012-06-22 08:52:37 +00003896/// \param ExplicitTemplateArgs the explicitly-specified template
Douglas Gregor8364e6b2009-12-21 23:17:24 +00003897/// arguments.
3898///
3899/// \param Specialization if template argument deduction was successful,
3900/// this will be set to the function template specialization produced by
3901/// template argument deduction.
3902///
3903/// \param Info the argument will be updated to provide additional information
3904/// about template argument deduction.
3905///
3906/// \returns the result of template argument deduction.
3907Sema::TemplateDeductionResult
3908Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
Douglas Gregor739b107a2011-03-03 02:41:12 +00003909 TemplateArgumentListInfo *ExplicitTemplateArgs,
Douglas Gregor8364e6b2009-12-21 23:17:24 +00003910 FunctionDecl *&Specialization,
Douglas Gregor19a41f12013-04-17 08:45:07 +00003911 TemplateDeductionInfo &Info,
3912 bool InOverloadResolution) {
Douglas Gregor8364e6b2009-12-21 23:17:24 +00003913 return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
Douglas Gregor19a41f12013-04-17 08:45:07 +00003914 QualType(), Specialization, Info,
3915 InOverloadResolution);
Douglas Gregor8364e6b2009-12-21 23:17:24 +00003916}
3917
Richard Smith30482bc2011-02-20 03:19:35 +00003918namespace {
3919 /// Substitute the 'auto' type specifier within a type for a given replacement
3920 /// type.
3921 class SubstituteAutoTransform :
3922 public TreeTransform<SubstituteAutoTransform> {
3923 QualType Replacement;
3924 public:
3925 SubstituteAutoTransform(Sema &SemaRef, QualType Replacement) :
3926 TreeTransform<SubstituteAutoTransform>(SemaRef), Replacement(Replacement) {
3927 }
3928 QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) {
3929 // If we're building the type pattern to deduce against, don't wrap the
3930 // substituted type in an AutoType. Certain template deduction rules
3931 // apply only when a template type parameter appears directly (and not if
3932 // the parameter is found through desugaring). For instance:
3933 // auto &&lref = lvalue;
3934 // must transform into "rvalue reference to T" not "rvalue reference to
3935 // auto type deduced as T" in order for [temp.deduct.call]p3 to apply.
Richard Smith2a7d4812013-05-04 07:00:32 +00003936 if (!Replacement.isNull() && isa<TemplateTypeParmType>(Replacement)) {
Richard Smith30482bc2011-02-20 03:19:35 +00003937 QualType Result = Replacement;
Richard Smith74aeef52013-04-26 16:15:35 +00003938 TemplateTypeParmTypeLoc NewTL =
3939 TLB.push<TemplateTypeParmTypeLoc>(Result);
Richard Smith30482bc2011-02-20 03:19:35 +00003940 NewTL.setNameLoc(TL.getNameLoc());
3941 return Result;
3942 } else {
Richard Smith27d807c2013-04-30 13:56:41 +00003943 bool Dependent =
3944 !Replacement.isNull() && Replacement->isDependentType();
3945 QualType Result =
3946 SemaRef.Context.getAutoType(Dependent ? QualType() : Replacement,
3947 TL.getTypePtr()->isDecltypeAuto(),
Manuel Klimek2fdbea22013-08-22 12:12:24 +00003948 Dependent);
Richard Smith30482bc2011-02-20 03:19:35 +00003949 AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result);
3950 NewTL.setNameLoc(TL.getNameLoc());
3951 return Result;
3952 }
3953 }
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00003954
3955 ExprResult TransformLambdaExpr(LambdaExpr *E) {
3956 // Lambdas never need to be transformed.
3957 return E;
3958 }
Richard Smith061f1e22013-04-30 21:23:01 +00003959
Richard Smith2a7d4812013-05-04 07:00:32 +00003960 QualType Apply(TypeLoc TL) {
3961 // Create some scratch storage for the transformed type locations.
3962 // FIXME: We're just going to throw this information away. Don't build it.
3963 TypeLocBuilder TLB;
3964 TLB.reserve(TL.getFullDataSize());
3965 return TransformType(TLB, TL);
Richard Smith061f1e22013-04-30 21:23:01 +00003966 }
Richard Smith30482bc2011-02-20 03:19:35 +00003967 };
3968}
3969
Richard Smith2a7d4812013-05-04 07:00:32 +00003970Sema::DeduceAutoResult
3971Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *&Init, QualType &Result) {
3972 return DeduceAutoType(Type->getTypeLoc(), Init, Result);
3973}
3974
Richard Smith061f1e22013-04-30 21:23:01 +00003975/// \brief Deduce the type for an auto type-specifier (C++11 [dcl.spec.auto]p6)
Richard Smith30482bc2011-02-20 03:19:35 +00003976///
3977/// \param Type the type pattern using the auto type-specifier.
Richard Smith30482bc2011-02-20 03:19:35 +00003978/// \param Init the initializer for the variable whose type is to be deduced.
Richard Smith30482bc2011-02-20 03:19:35 +00003979/// \param Result if type deduction was successful, this will be set to the
Richard Smith061f1e22013-04-30 21:23:01 +00003980/// deduced type.
Sebastian Redl09edce02012-01-23 22:09:39 +00003981Sema::DeduceAutoResult
Richard Smith2a7d4812013-05-04 07:00:32 +00003982Sema::DeduceAutoType(TypeLoc Type, Expr *&Init, QualType &Result) {
John McCalld5c98ae2011-11-15 01:35:18 +00003983 if (Init->getType()->isNonOverloadPlaceholderType()) {
Richard Smith061f1e22013-04-30 21:23:01 +00003984 ExprResult NonPlaceholder = CheckPlaceholderExpr(Init);
3985 if (NonPlaceholder.isInvalid())
3986 return DAR_FailedAlreadyDiagnosed;
3987 Init = NonPlaceholder.take();
John McCalld5c98ae2011-11-15 01:35:18 +00003988 }
3989
Richard Smith2a7d4812013-05-04 07:00:32 +00003990 if (Init->isTypeDependent() || Type.getType()->isDependentType()) {
Richard Smith061f1e22013-04-30 21:23:01 +00003991 Result = SubstituteAutoTransform(*this, Context.DependentTy).Apply(Type);
Richard Smith2a7d4812013-05-04 07:00:32 +00003992 assert(!Result.isNull() && "substituting DependentTy can't fail");
Sebastian Redl09edce02012-01-23 22:09:39 +00003993 return DAR_Succeeded;
Richard Smith30482bc2011-02-20 03:19:35 +00003994 }
3995
Richard Smith74aeef52013-04-26 16:15:35 +00003996 // If this is a 'decltype(auto)' specifier, do the decltype dance.
3997 // Since 'decltype(auto)' can only occur at the top of the type, we
3998 // don't need to go digging for it.
Richard Smith2a7d4812013-05-04 07:00:32 +00003999 if (const AutoType *AT = Type.getType()->getAs<AutoType>()) {
Richard Smith74aeef52013-04-26 16:15:35 +00004000 if (AT->isDecltypeAuto()) {
4001 if (isa<InitListExpr>(Init)) {
4002 Diag(Init->getLocStart(), diag::err_decltype_auto_initializer_list);
4003 return DAR_FailedAlreadyDiagnosed;
4004 }
4005
4006 QualType Deduced = BuildDecltypeType(Init, Init->getLocStart());
4007 // FIXME: Support a non-canonical deduced type for 'auto'.
4008 Deduced = Context.getCanonicalType(Deduced);
Richard Smith061f1e22013-04-30 21:23:01 +00004009 Result = SubstituteAutoTransform(*this, Deduced).Apply(Type);
Richard Smith2a7d4812013-05-04 07:00:32 +00004010 if (Result.isNull())
4011 return DAR_FailedAlreadyDiagnosed;
Richard Smith74aeef52013-04-26 16:15:35 +00004012 return DAR_Succeeded;
4013 }
4014 }
4015
Richard Smith30482bc2011-02-20 03:19:35 +00004016 SourceLocation Loc = Init->getExprLoc();
4017
4018 LocalInstantiationScope InstScope(*this);
4019
4020 // Build template<class TemplParam> void Func(FuncParam);
Chandler Carruth08836322011-05-01 00:51:33 +00004021 TemplateTypeParmDecl *TemplParam =
4022 TemplateTypeParmDecl::Create(Context, 0, SourceLocation(), Loc, 0, 0, 0,
4023 false, false);
4024 QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0);
4025 NamedDecl *TemplParamPtr = TemplParam;
Richard Smithb2bc2e62011-02-21 20:05:19 +00004026 FixedSizeTemplateParameterList<1> TemplateParams(Loc, Loc, &TemplParamPtr,
4027 Loc);
4028
Richard Smith061f1e22013-04-30 21:23:01 +00004029 QualType FuncParam = SubstituteAutoTransform(*this, TemplArg).Apply(Type);
4030 assert(!FuncParam.isNull() &&
4031 "substituting template parameter for 'auto' failed");
Richard Smith30482bc2011-02-20 03:19:35 +00004032
4033 // Deduce type of TemplParam in Func(Init)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004034 SmallVector<DeducedTemplateArgument, 1> Deduced;
Richard Smith30482bc2011-02-20 03:19:35 +00004035 Deduced.resize(1);
4036 QualType InitType = Init->getType();
4037 unsigned TDF = 0;
Richard Smith30482bc2011-02-20 03:19:35 +00004038
Craig Toppere6706e42012-09-19 02:26:47 +00004039 TemplateDeductionInfo Info(Loc);
Sebastian Redl42acd4a2012-01-17 22:50:08 +00004040
Richard Smith74801c82012-07-08 04:13:07 +00004041 InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
Sebastian Redl42acd4a2012-01-17 22:50:08 +00004042 if (InitList) {
4043 for (unsigned i = 0, e = InitList->getNumInits(); i < e; ++i) {
Richard Smith74801c82012-07-08 04:13:07 +00004044 if (DeduceTemplateArgumentByListElement(*this, &TemplateParams,
Douglas Gregor0e60cd72012-04-04 05:10:53 +00004045 TemplArg,
4046 InitList->getInit(i),
4047 Info, Deduced, TDF))
Sebastian Redl09edce02012-01-23 22:09:39 +00004048 return DAR_Failed;
Sebastian Redl42acd4a2012-01-17 22:50:08 +00004049 }
4050 } else {
Douglas Gregor0e60cd72012-04-04 05:10:53 +00004051 if (AdjustFunctionParmAndArgTypesForDeduction(*this, &TemplateParams,
4052 FuncParam, InitType, Init,
4053 TDF))
4054 return DAR_Failed;
Richard Smith74801c82012-07-08 04:13:07 +00004055
Sebastian Redl42acd4a2012-01-17 22:50:08 +00004056 if (DeduceTemplateArgumentsByTypeMatch(*this, &TemplateParams, FuncParam,
4057 InitType, Info, Deduced, TDF))
Sebastian Redl09edce02012-01-23 22:09:39 +00004058 return DAR_Failed;
Sebastian Redl42acd4a2012-01-17 22:50:08 +00004059 }
Richard Smith30482bc2011-02-20 03:19:35 +00004060
Eli Friedmane4310952012-11-06 23:56:42 +00004061 if (Deduced[0].getKind() != TemplateArgument::Type)
Sebastian Redl09edce02012-01-23 22:09:39 +00004062 return DAR_Failed;
Sebastian Redl42acd4a2012-01-17 22:50:08 +00004063
Eli Friedmane4310952012-11-06 23:56:42 +00004064 QualType DeducedType = Deduced[0].getAsType();
4065
Sebastian Redl42acd4a2012-01-17 22:50:08 +00004066 if (InitList) {
4067 DeducedType = BuildStdInitializerList(DeducedType, Loc);
4068 if (DeducedType.isNull())
Sebastian Redl09edce02012-01-23 22:09:39 +00004069 return DAR_FailedAlreadyDiagnosed;
Sebastian Redl42acd4a2012-01-17 22:50:08 +00004070 }
4071
Richard Smith061f1e22013-04-30 21:23:01 +00004072 Result = SubstituteAutoTransform(*this, DeducedType).Apply(Type);
Richard Smith2a7d4812013-05-04 07:00:32 +00004073 if (Result.isNull())
4074 return DAR_FailedAlreadyDiagnosed;
Sebastian Redl42acd4a2012-01-17 22:50:08 +00004075
Douglas Gregor518bc4c2011-06-17 05:31:46 +00004076 // Check that the deduced argument type is compatible with the original
4077 // argument type per C++ [temp.deduct.call]p4.
Richard Smith061f1e22013-04-30 21:23:01 +00004078 if (!InitList && !Result.isNull() &&
4079 CheckOriginalCallArgDeduction(*this,
Douglas Gregor518bc4c2011-06-17 05:31:46 +00004080 Sema::OriginalCallArg(FuncParam,0,InitType),
Richard Smith061f1e22013-04-30 21:23:01 +00004081 Result)) {
4082 Result = QualType();
Sebastian Redl09edce02012-01-23 22:09:39 +00004083 return DAR_Failed;
Douglas Gregor518bc4c2011-06-17 05:31:46 +00004084 }
4085
Sebastian Redl09edce02012-01-23 22:09:39 +00004086 return DAR_Succeeded;
Richard Smith30482bc2011-02-20 03:19:35 +00004087}
4088
Faisal Vali2b391ab2013-09-26 19:54:12 +00004089QualType Sema::SubstAutoType(QualType TypeWithAuto,
4090 QualType TypeToReplaceAuto) {
4091 return SubstituteAutoTransform(*this, TypeToReplaceAuto).
4092 TransformType(TypeWithAuto);
4093}
4094
4095TypeSourceInfo* Sema::SubstAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto,
4096 QualType TypeToReplaceAuto) {
4097 return SubstituteAutoTransform(*this, TypeToReplaceAuto).
4098 TransformType(TypeWithAuto);
Richard Smith27d807c2013-04-30 13:56:41 +00004099}
4100
Sebastian Redl42acd4a2012-01-17 22:50:08 +00004101void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) {
4102 if (isa<InitListExpr>(Init))
4103 Diag(VDecl->getLocation(),
Richard Smithbb13c9a2013-09-28 04:02:39 +00004104 VDecl->isInitCapture()
4105 ? diag::err_init_capture_deduction_failure_from_init_list
4106 : diag::err_auto_var_deduction_failure_from_init_list)
Sebastian Redl42acd4a2012-01-17 22:50:08 +00004107 << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange();
4108 else
Richard Smithbb13c9a2013-09-28 04:02:39 +00004109 Diag(VDecl->getLocation(),
4110 VDecl->isInitCapture() ? diag::err_init_capture_deduction_failure
4111 : diag::err_auto_var_deduction_failure)
Sebastian Redl42acd4a2012-01-17 22:50:08 +00004112 << VDecl->getDeclName() << VDecl->getType() << Init->getType()
4113 << Init->getSourceRange();
4114}
4115
Richard Smith2a7d4812013-05-04 07:00:32 +00004116bool Sema::DeduceReturnType(FunctionDecl *FD, SourceLocation Loc,
4117 bool Diagnose) {
Alp Toker314cc812014-01-25 16:55:45 +00004118 assert(FD->getReturnType()->isUndeducedType());
Richard Smith2a7d4812013-05-04 07:00:32 +00004119
4120 if (FD->getTemplateInstantiationPattern())
4121 InstantiateFunctionDefinition(Loc, FD);
4122
Alp Toker314cc812014-01-25 16:55:45 +00004123 bool StillUndeduced = FD->getReturnType()->isUndeducedType();
Richard Smith2a7d4812013-05-04 07:00:32 +00004124 if (StillUndeduced && Diagnose && !FD->isInvalidDecl()) {
4125 Diag(Loc, diag::err_auto_fn_used_before_defined) << FD;
4126 Diag(FD->getLocation(), diag::note_callee_decl) << FD;
4127 }
4128
4129 return StillUndeduced;
4130}
4131
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004132static void
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004133MarkUsedTemplateParameters(ASTContext &Ctx, QualType T,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004134 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00004135 unsigned Level,
Benjamin Kramere0513cb2012-01-30 16:17:39 +00004136 llvm::SmallBitVector &Deduced);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004137
4138/// \brief If this is a non-static member function,
Craig Topper79653572013-07-08 04:13:06 +00004139static void
4140AddImplicitObjectParameterType(ASTContext &Context,
4141 CXXMethodDecl *Method,
4142 SmallVectorImpl<QualType> &ArgTypes) {
Eli Friedmanee2ff1c2012-09-19 23:52:13 +00004143 // C++11 [temp.func.order]p3:
4144 // [...] The new parameter is of type "reference to cv A," where cv are
4145 // the cv-qualifiers of the function template (if any) and A is
4146 // the class of which the function template is a member.
Douglas Gregor52773dc2010-11-12 23:44:13 +00004147 //
Eli Friedmanee2ff1c2012-09-19 23:52:13 +00004148 // The standard doesn't say explicitly, but we pick the appropriate kind of
4149 // reference type based on [over.match.funcs]p4.
Douglas Gregor52773dc2010-11-12 23:44:13 +00004150 QualType ArgTy = Context.getTypeDeclType(Method->getParent());
4151 ArgTy = Context.getQualifiedType(ArgTy,
4152 Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
Eli Friedmanee2ff1c2012-09-19 23:52:13 +00004153 if (Method->getRefQualifier() == RQ_RValue)
4154 ArgTy = Context.getRValueReferenceType(ArgTy);
4155 else
4156 ArgTy = Context.getLValueReferenceType(ArgTy);
Douglas Gregor52773dc2010-11-12 23:44:13 +00004157 ArgTypes.push_back(ArgTy);
4158}
4159
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004160/// \brief Determine whether the function template \p FT1 is at least as
4161/// specialized as \p FT2.
4162static bool isAtLeastAsSpecializedAs(Sema &S,
John McCallbc077cf2010-02-08 23:07:23 +00004163 SourceLocation Loc,
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004164 FunctionTemplateDecl *FT1,
4165 FunctionTemplateDecl *FT2,
4166 TemplatePartialOrderingContext TPOC,
Richard Smithe5b52202013-09-11 00:52:39 +00004167 unsigned NumCallArguments1,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004168 SmallVectorImpl<RefParamPartialOrderingComparison> *RefParamComparisons) {
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004169 FunctionDecl *FD1 = FT1->getTemplatedDecl();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004170 FunctionDecl *FD2 = FT2->getTemplatedDecl();
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004171 const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
4172 const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004173
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004174 assert(Proto1 && Proto2 && "Function templates must have prototypes");
4175 TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004176 SmallVector<DeducedTemplateArgument, 4> Deduced;
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004177 Deduced.resize(TemplateParams->size());
4178
4179 // C++0x [temp.deduct.partial]p3:
4180 // The types used to determine the ordering depend on the context in which
4181 // the partial ordering is done:
Craig Toppere6706e42012-09-19 02:26:47 +00004182 TemplateDeductionInfo Info(Loc);
Richard Smithe5b52202013-09-11 00:52:39 +00004183 SmallVector<QualType, 4> Args2;
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004184 switch (TPOC) {
4185 case TPOC_Call: {
4186 // - In the context of a function call, the function parameter types are
4187 // used.
Richard Smithe5b52202013-09-11 00:52:39 +00004188 CXXMethodDecl *Method1 = dyn_cast<CXXMethodDecl>(FD1);
4189 CXXMethodDecl *Method2 = dyn_cast<CXXMethodDecl>(FD2);
Douglas Gregoree430a32010-11-15 15:41:16 +00004190
Eli Friedman3b5774a2012-09-19 23:27:04 +00004191 // C++11 [temp.func.order]p3:
Douglas Gregoree430a32010-11-15 15:41:16 +00004192 // [...] If only one of the function templates is a non-static
4193 // member, that function template is considered to have a new
4194 // first parameter inserted in its function parameter list. The
4195 // new parameter is of type "reference to cv A," where cv are
4196 // the cv-qualifiers of the function template (if any) and A is
4197 // the class of which the function template is a member.
4198 //
Eli Friedman3b5774a2012-09-19 23:27:04 +00004199 // Note that we interpret this to mean "if one of the function
4200 // templates is a non-static member and the other is a non-member";
4201 // otherwise, the ordering rules for static functions against non-static
4202 // functions don't make any sense.
4203 //
Douglas Gregoree430a32010-11-15 15:41:16 +00004204 // C++98/03 doesn't have this provision, so instead we drop the
Eli Friedman3b5774a2012-09-19 23:27:04 +00004205 // first argument of the free function, which seems to match
4206 // existing practice.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004207 SmallVector<QualType, 4> Args1;
Richard Smithe5b52202013-09-11 00:52:39 +00004208
4209 unsigned Skip1 = 0, Skip2 = 0;
4210 unsigned NumComparedArguments = NumCallArguments1;
4211
4212 if (!Method2 && Method1 && !Method1->isStatic()) {
4213 if (S.getLangOpts().CPlusPlus11) {
4214 // Compare 'this' from Method1 against first parameter from Method2.
4215 AddImplicitObjectParameterType(S.Context, Method1, Args1);
4216 ++NumComparedArguments;
4217 } else
4218 // Ignore first parameter from Method2.
4219 ++Skip2;
4220 } else if (!Method1 && Method2 && !Method2->isStatic()) {
4221 if (S.getLangOpts().CPlusPlus11)
4222 // Compare 'this' from Method2 against first parameter from Method1.
4223 AddImplicitObjectParameterType(S.Context, Method2, Args2);
4224 else
4225 // Ignore first parameter from Method1.
4226 ++Skip1;
4227 }
4228
Alp Toker9cacbab2014-01-20 20:26:09 +00004229 Args1.insert(Args1.end(), Proto1->param_type_begin() + Skip1,
4230 Proto1->param_type_end());
4231 Args2.insert(Args2.end(), Proto2->param_type_begin() + Skip2,
4232 Proto2->param_type_end());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004233
Douglas Gregorb837ea42011-01-11 17:34:58 +00004234 // C++ [temp.func.order]p5:
4235 // The presence of unused ellipsis and default arguments has no effect on
4236 // the partial ordering of function templates.
Richard Smithe5b52202013-09-11 00:52:39 +00004237 if (Args1.size() > NumComparedArguments)
4238 Args1.resize(NumComparedArguments);
4239 if (Args2.size() > NumComparedArguments)
4240 Args2.resize(NumComparedArguments);
Douglas Gregorb837ea42011-01-11 17:34:58 +00004241 if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(),
4242 Args1.data(), Args1.size(), Info, Deduced,
4243 TDF_None, /*PartialOrdering=*/true,
Douglas Gregor63814022011-01-21 17:29:42 +00004244 RefParamComparisons))
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004245 return false;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004246
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004247 break;
4248 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004249
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004250 case TPOC_Conversion:
4251 // - In the context of a call to a conversion operator, the return types
4252 // of the conversion function templates are used.
Alp Toker314cc812014-01-25 16:55:45 +00004253 if (DeduceTemplateArgumentsByTypeMatch(
4254 S, TemplateParams, Proto2->getReturnType(), Proto1->getReturnType(),
4255 Info, Deduced, TDF_None,
4256 /*PartialOrdering=*/true, RefParamComparisons))
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004257 return false;
4258 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004259
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004260 case TPOC_Other:
NAKAMURA Takumi7c288862011-01-27 07:09:49 +00004261 // - In other contexts (14.6.6.2) the function template's function type
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004262 // is used.
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00004263 if (DeduceTemplateArgumentsByTypeMatch(S, TemplateParams,
4264 FD2->getType(), FD1->getType(),
4265 Info, Deduced, TDF_None,
4266 /*PartialOrdering=*/true,
4267 RefParamComparisons))
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004268 return false;
4269 break;
4270 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004271
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004272 // C++0x [temp.deduct.partial]p11:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004273 // In most cases, all template parameters must have values in order for
4274 // deduction to succeed, but for partial ordering purposes a template
4275 // parameter may remain without a value provided it is not used in the
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004276 // types being used for partial ordering. [ Note: a template parameter used
4277 // in a non-deduced context is considered used. -end note]
4278 unsigned ArgIdx = 0, NumArgs = Deduced.size();
4279 for (; ArgIdx != NumArgs; ++ArgIdx)
4280 if (Deduced[ArgIdx].isNull())
4281 break;
4282
4283 if (ArgIdx == NumArgs) {
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004284 // All template arguments were deduced. FT1 is at least as specialized
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004285 // as FT2.
4286 return true;
4287 }
4288
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004289 // Figure out which template parameters were used.
Benjamin Kramere0513cb2012-01-30 16:17:39 +00004290 llvm::SmallBitVector UsedParameters(TemplateParams->size());
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004291 switch (TPOC) {
Richard Smithe5b52202013-09-11 00:52:39 +00004292 case TPOC_Call:
4293 for (unsigned I = 0, N = Args2.size(); I != N; ++I)
4294 ::MarkUsedTemplateParameters(S.Context, Args2[I], false,
Douglas Gregor21610382009-10-29 00:04:11 +00004295 TemplateParams->getDepth(),
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004296 UsedParameters);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004297 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004298
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004299 case TPOC_Conversion:
Alp Toker314cc812014-01-25 16:55:45 +00004300 ::MarkUsedTemplateParameters(S.Context, Proto2->getReturnType(), false,
4301 TemplateParams->getDepth(), UsedParameters);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004302 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004303
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004304 case TPOC_Other:
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004305 ::MarkUsedTemplateParameters(S.Context, FD2->getType(), false,
Douglas Gregor21610382009-10-29 00:04:11 +00004306 TemplateParams->getDepth(),
4307 UsedParameters);
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004308 break;
4309 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004310
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004311 for (; ArgIdx != NumArgs; ++ArgIdx)
4312 // If this argument had no value deduced but was used in one of the types
4313 // used for partial ordering, then deduction fails.
4314 if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
4315 return false;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004316
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004317 return true;
4318}
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004319
Douglas Gregorcef1a032011-01-16 16:03:23 +00004320/// \brief Determine whether this a function template whose parameter-type-list
4321/// ends with a function parameter pack.
4322static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) {
4323 FunctionDecl *Function = FunTmpl->getTemplatedDecl();
4324 unsigned NumParams = Function->getNumParams();
4325 if (NumParams == 0)
4326 return false;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004327
Douglas Gregorcef1a032011-01-16 16:03:23 +00004328 ParmVarDecl *Last = Function->getParamDecl(NumParams - 1);
4329 if (!Last->isParameterPack())
4330 return false;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004331
Douglas Gregorcef1a032011-01-16 16:03:23 +00004332 // Make sure that no previous parameter is a parameter pack.
4333 while (--NumParams > 0) {
4334 if (Function->getParamDecl(NumParams - 1)->isParameterPack())
4335 return false;
4336 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004337
Douglas Gregorcef1a032011-01-16 16:03:23 +00004338 return true;
4339}
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004340
Douglas Gregorbe999392009-09-15 16:23:51 +00004341/// \brief Returns the more specialized function template according
Douglas Gregor05155d82009-08-21 23:19:43 +00004342/// to the rules of function template partial ordering (C++ [temp.func.order]).
4343///
4344/// \param FT1 the first function template
4345///
4346/// \param FT2 the second function template
4347///
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004348/// \param TPOC the context in which we are performing partial ordering of
4349/// function templates.
Mike Stump11289f42009-09-09 15:08:12 +00004350///
Richard Smithe5b52202013-09-11 00:52:39 +00004351/// \param NumCallArguments1 The number of arguments in the call to FT1, used
4352/// only when \c TPOC is \c TPOC_Call.
4353///
4354/// \param NumCallArguments2 The number of arguments in the call to FT2, used
4355/// only when \c TPOC is \c TPOC_Call.
Douglas Gregorb837ea42011-01-11 17:34:58 +00004356///
Douglas Gregorbe999392009-09-15 16:23:51 +00004357/// \returns the more specialized function template. If neither
Douglas Gregor05155d82009-08-21 23:19:43 +00004358/// template is more specialized, returns NULL.
4359FunctionTemplateDecl *
4360Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
4361 FunctionTemplateDecl *FT2,
John McCallbc077cf2010-02-08 23:07:23 +00004362 SourceLocation Loc,
Douglas Gregorb837ea42011-01-11 17:34:58 +00004363 TemplatePartialOrderingContext TPOC,
Richard Smithe5b52202013-09-11 00:52:39 +00004364 unsigned NumCallArguments1,
4365 unsigned NumCallArguments2) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004366 SmallVector<RefParamPartialOrderingComparison, 4> RefParamComparisons;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004367 bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC,
Richard Smithe5b52202013-09-11 00:52:39 +00004368 NumCallArguments1, 0);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004369 bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
Richard Smithe5b52202013-09-11 00:52:39 +00004370 NumCallArguments2,
Douglas Gregor63814022011-01-21 17:29:42 +00004371 &RefParamComparisons);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004372
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004373 if (Better1 != Better2) // We have a clear winner
4374 return Better1? FT1 : FT2;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004375
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004376 if (!Better1 && !Better2) // Neither is better than the other
Douglas Gregor05155d82009-08-21 23:19:43 +00004377 return 0;
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004378
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004379 // C++0x [temp.deduct.partial]p10:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004380 // If for each type being considered a given template is at least as
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004381 // specialized for all types and more specialized for some set of types and
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004382 // the other template is not more specialized for any types or is not at
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004383 // least as specialized for any types, then the given template is more
4384 // specialized than the other template. Otherwise, neither template is more
4385 // specialized than the other.
4386 Better1 = false;
4387 Better2 = false;
Douglas Gregor63814022011-01-21 17:29:42 +00004388 for (unsigned I = 0, N = RefParamComparisons.size(); I != N; ++I) {
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004389 // C++0x [temp.deduct.partial]p9:
4390 // If, for a given type, deduction succeeds in both directions (i.e., the
Douglas Gregor63814022011-01-21 17:29:42 +00004391 // types are identical after the transformations above) and both P and A
4392 // were reference types (before being replaced with the type referred to
4393 // above):
4394
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004395 // -- if the type from the argument template was an lvalue reference
Douglas Gregor63814022011-01-21 17:29:42 +00004396 // and the type from the parameter template was not, the argument
4397 // type is considered to be more specialized than the other;
4398 // otherwise,
4399 if (!RefParamComparisons[I].ArgIsRvalueRef &&
4400 RefParamComparisons[I].ParamIsRvalueRef) {
4401 Better2 = true;
4402 if (Better1)
4403 return 0;
4404 continue;
4405 } else if (!RefParamComparisons[I].ParamIsRvalueRef &&
4406 RefParamComparisons[I].ArgIsRvalueRef) {
4407 Better1 = true;
4408 if (Better2)
4409 return 0;
4410 continue;
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004411 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004412
Douglas Gregor63814022011-01-21 17:29:42 +00004413 // -- if the type from the argument template is more cv-qualified than
4414 // the type from the parameter template (as described above), the
4415 // argument type is considered to be more specialized than the
4416 // other; otherwise,
4417 switch (RefParamComparisons[I].Qualifiers) {
4418 case NeitherMoreQualified:
4419 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004420
Douglas Gregor63814022011-01-21 17:29:42 +00004421 case ParamMoreQualified:
4422 Better1 = true;
4423 if (Better2)
4424 return 0;
4425 continue;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004426
Douglas Gregor63814022011-01-21 17:29:42 +00004427 case ArgMoreQualified:
4428 Better2 = true;
4429 if (Better1)
4430 return 0;
4431 continue;
4432 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004433
Douglas Gregor63814022011-01-21 17:29:42 +00004434 // -- neither type is more specialized than the other.
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004435 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004436
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004437 assert(!(Better1 && Better2) && "Should have broken out in the loop above");
Douglas Gregor05155d82009-08-21 23:19:43 +00004438 if (Better1)
4439 return FT1;
Douglas Gregor0ff7d922009-09-14 18:39:43 +00004440 else if (Better2)
4441 return FT2;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004442
Douglas Gregorcef1a032011-01-16 16:03:23 +00004443 // FIXME: This mimics what GCC implements, but doesn't match up with the
4444 // proposed resolution for core issue 692. This area needs to be sorted out,
4445 // but for now we attempt to maintain compatibility.
4446 bool Variadic1 = isVariadicFunctionTemplate(FT1);
4447 bool Variadic2 = isVariadicFunctionTemplate(FT2);
4448 if (Variadic1 != Variadic2)
4449 return Variadic1? FT2 : FT1;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004450
Douglas Gregorcef1a032011-01-16 16:03:23 +00004451 return 0;
Douglas Gregor05155d82009-08-21 23:19:43 +00004452}
Douglas Gregor9b146582009-07-08 20:55:45 +00004453
Douglas Gregor450f00842009-09-25 18:43:00 +00004454/// \brief Determine if the two templates are equivalent.
4455static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
4456 if (T1 == T2)
4457 return true;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004458
Douglas Gregor450f00842009-09-25 18:43:00 +00004459 if (!T1 || !T2)
4460 return false;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004461
Douglas Gregor450f00842009-09-25 18:43:00 +00004462 return T1->getCanonicalDecl() == T2->getCanonicalDecl();
4463}
4464
4465/// \brief Retrieve the most specialized of the given function template
4466/// specializations.
4467///
John McCall58cc69d2010-01-27 01:50:18 +00004468/// \param SpecBegin the start iterator of the function template
4469/// specializations that we will be comparing.
Douglas Gregor450f00842009-09-25 18:43:00 +00004470///
John McCall58cc69d2010-01-27 01:50:18 +00004471/// \param SpecEnd the end iterator of the function template
4472/// specializations, paired with \p SpecBegin.
Douglas Gregor450f00842009-09-25 18:43:00 +00004473///
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004474/// \param Loc the location where the ambiguity or no-specializations
Douglas Gregor450f00842009-09-25 18:43:00 +00004475/// diagnostic should occur.
4476///
4477/// \param NoneDiag partial diagnostic used to diagnose cases where there are
4478/// no matching candidates.
4479///
4480/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
4481/// occurs.
4482///
4483/// \param CandidateDiag partial diagnostic used for each function template
4484/// specialization that is a candidate in the ambiguous ordering. One parameter
4485/// in this diagnostic should be unbound, which will correspond to the string
4486/// describing the template arguments for the function template specialization.
4487///
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004488/// \returns the most specialized function template specialization, if
John McCall58cc69d2010-01-27 01:50:18 +00004489/// found. Otherwise, returns SpecEnd.
Larisse Voufo98b20f12013-07-19 23:00:19 +00004490UnresolvedSetIterator Sema::getMostSpecialized(
4491 UnresolvedSetIterator SpecBegin, UnresolvedSetIterator SpecEnd,
4492 TemplateSpecCandidateSet &FailedCandidates,
Larisse Voufo98b20f12013-07-19 23:00:19 +00004493 SourceLocation Loc, const PartialDiagnostic &NoneDiag,
4494 const PartialDiagnostic &AmbigDiag, const PartialDiagnostic &CandidateDiag,
4495 bool Complain, QualType TargetType) {
John McCall58cc69d2010-01-27 01:50:18 +00004496 if (SpecBegin == SpecEnd) {
Larisse Voufo98b20f12013-07-19 23:00:19 +00004497 if (Complain) {
Douglas Gregorb491ed32011-02-19 21:32:49 +00004498 Diag(Loc, NoneDiag);
Larisse Voufo98b20f12013-07-19 23:00:19 +00004499 FailedCandidates.NoteCandidates(*this, Loc);
4500 }
John McCall58cc69d2010-01-27 01:50:18 +00004501 return SpecEnd;
Douglas Gregor450f00842009-09-25 18:43:00 +00004502 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004503
4504 if (SpecBegin + 1 == SpecEnd)
John McCall58cc69d2010-01-27 01:50:18 +00004505 return SpecBegin;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004506
Douglas Gregor450f00842009-09-25 18:43:00 +00004507 // Find the function template that is better than all of the templates it
4508 // has been compared to.
John McCall58cc69d2010-01-27 01:50:18 +00004509 UnresolvedSetIterator Best = SpecBegin;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004510 FunctionTemplateDecl *BestTemplate
John McCall58cc69d2010-01-27 01:50:18 +00004511 = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
Douglas Gregor450f00842009-09-25 18:43:00 +00004512 assert(BestTemplate && "Not a function template specialization?");
John McCall58cc69d2010-01-27 01:50:18 +00004513 for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
4514 FunctionTemplateDecl *Challenger
4515 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregor450f00842009-09-25 18:43:00 +00004516 assert(Challenger && "Not a function template specialization?");
John McCall58cc69d2010-01-27 01:50:18 +00004517 if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
Richard Smithe5b52202013-09-11 00:52:39 +00004518 Loc, TPOC_Other, 0, 0),
Douglas Gregor450f00842009-09-25 18:43:00 +00004519 Challenger)) {
4520 Best = I;
4521 BestTemplate = Challenger;
4522 }
4523 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004524
Douglas Gregor450f00842009-09-25 18:43:00 +00004525 // Make sure that the "best" function template is more specialized than all
4526 // of the others.
4527 bool Ambiguous = false;
John McCall58cc69d2010-01-27 01:50:18 +00004528 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
4529 FunctionTemplateDecl *Challenger
4530 = cast<FunctionDecl>(*I)->getPrimaryTemplate();
Douglas Gregor450f00842009-09-25 18:43:00 +00004531 if (I != Best &&
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004532 !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
Richard Smithe5b52202013-09-11 00:52:39 +00004533 Loc, TPOC_Other, 0, 0),
Douglas Gregor450f00842009-09-25 18:43:00 +00004534 BestTemplate)) {
4535 Ambiguous = true;
4536 break;
4537 }
4538 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004539
Douglas Gregor450f00842009-09-25 18:43:00 +00004540 if (!Ambiguous) {
4541 // We found an answer. Return it.
John McCall58cc69d2010-01-27 01:50:18 +00004542 return Best;
Douglas Gregor450f00842009-09-25 18:43:00 +00004543 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004544
Douglas Gregor450f00842009-09-25 18:43:00 +00004545 // Diagnose the ambiguity.
Richard Smithb875c432013-05-04 01:51:08 +00004546 if (Complain) {
Douglas Gregorb491ed32011-02-19 21:32:49 +00004547 Diag(Loc, AmbigDiag);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004548
Richard Smithb875c432013-05-04 01:51:08 +00004549 // FIXME: Can we order the candidates in some sane way?
Richard Trieucaff2472011-11-23 22:32:32 +00004550 for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
4551 PartialDiagnostic PD = CandidateDiag;
4552 PD << getTemplateArgumentBindingsText(
Douglas Gregorb491ed32011-02-19 21:32:49 +00004553 cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
John McCall58cc69d2010-01-27 01:50:18 +00004554 *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
Richard Trieucaff2472011-11-23 22:32:32 +00004555 if (!TargetType.isNull())
4556 HandleFunctionTypeMismatch(PD, cast<FunctionDecl>(*I)->getType(),
4557 TargetType);
4558 Diag((*I)->getLocation(), PD);
4559 }
Richard Smithb875c432013-05-04 01:51:08 +00004560 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004561
John McCall58cc69d2010-01-27 01:50:18 +00004562 return SpecEnd;
Douglas Gregor450f00842009-09-25 18:43:00 +00004563}
4564
Douglas Gregorbe999392009-09-15 16:23:51 +00004565/// \brief Returns the more specialized class template partial specialization
4566/// according to the rules of partial ordering of class template partial
4567/// specializations (C++ [temp.class.order]).
4568///
4569/// \param PS1 the first class template partial specialization
4570///
4571/// \param PS2 the second class template partial specialization
4572///
4573/// \returns the more specialized class template partial specialization. If
4574/// neither partial specialization is more specialized, returns NULL.
4575ClassTemplatePartialSpecializationDecl *
4576Sema::getMoreSpecializedPartialSpecialization(
4577 ClassTemplatePartialSpecializationDecl *PS1,
John McCallbc077cf2010-02-08 23:07:23 +00004578 ClassTemplatePartialSpecializationDecl *PS2,
4579 SourceLocation Loc) {
Douglas Gregorbe999392009-09-15 16:23:51 +00004580 // C++ [temp.class.order]p1:
4581 // For two class template partial specializations, the first is at least as
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004582 // specialized as the second if, given the following rewrite to two
4583 // function templates, the first function template is at least as
4584 // specialized as the second according to the ordering rules for function
Douglas Gregorbe999392009-09-15 16:23:51 +00004585 // templates (14.6.6.2):
4586 // - the first function template has the same template parameters as the
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004587 // first partial specialization and has a single function parameter
4588 // whose type is a class template specialization with the template
Douglas Gregorbe999392009-09-15 16:23:51 +00004589 // arguments of the first partial specialization, and
4590 // - the second function template has the same template parameters as the
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004591 // second partial specialization and has a single function parameter
4592 // whose type is a class template specialization with the template
Douglas Gregorbe999392009-09-15 16:23:51 +00004593 // arguments of the second partial specialization.
4594 //
Douglas Gregor684268d2010-04-29 06:21:43 +00004595 // Rather than synthesize function templates, we merely perform the
4596 // equivalent partial ordering by performing deduction directly on
4597 // the template arguments of the class template partial
4598 // specializations. This computation is slightly simpler than the
4599 // general problem of function template partial ordering, because
4600 // class template partial specializations are more constrained. We
4601 // know that every template parameter is deducible from the class
4602 // template partial specialization's template arguments, for
4603 // example.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004604 SmallVector<DeducedTemplateArgument, 4> Deduced;
Craig Toppere6706e42012-09-19 02:26:47 +00004605 TemplateDeductionInfo Info(Loc);
John McCall2408e322010-04-27 00:57:59 +00004606
4607 QualType PT1 = PS1->getInjectedSpecializationType();
4608 QualType PT2 = PS2->getInjectedSpecializationType();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004609
Douglas Gregorbe999392009-09-15 16:23:51 +00004610 // Determine whether PS1 is at least as specialized as PS2
4611 Deduced.resize(PS2->getTemplateParameters()->size());
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00004612 bool Better1 = !DeduceTemplateArgumentsByTypeMatch(*this,
4613 PS2->getTemplateParameters(),
Douglas Gregorb837ea42011-01-11 17:34:58 +00004614 PT2, PT1, Info, Deduced, TDF_None,
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004615 /*PartialOrdering=*/true,
Douglas Gregor63814022011-01-21 17:29:42 +00004616 /*RefParamComparisons=*/0);
Argyrios Kyrtzidis7d391552010-11-05 23:25:18 +00004617 if (Better1) {
Richard Smith80934652012-07-16 01:09:10 +00004618 SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(),Deduced.end());
Nick Lewycky56412332014-01-11 02:37:12 +00004619 InstantiatingTemplate Inst(*this, Loc, PS2, DeducedArgs, Info);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004620 Better1 = !::FinishTemplateArgumentDeduction(
4621 *this, PS2, PS1->getTemplateArgs(), Deduced, Info);
4622 }
4623
4624 // Determine whether PS2 is at least as specialized as PS1
4625 Deduced.clear();
4626 Deduced.resize(PS1->getTemplateParameters()->size());
4627 bool Better2 = !DeduceTemplateArgumentsByTypeMatch(
4628 *this, PS1->getTemplateParameters(), PT1, PT2, Info, Deduced, TDF_None,
4629 /*PartialOrdering=*/true,
4630 /*RefParamComparisons=*/0);
4631 if (Better2) {
4632 SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(),
4633 Deduced.end());
Nick Lewycky56412332014-01-11 02:37:12 +00004634 InstantiatingTemplate Inst(*this, Loc, PS1, DeducedArgs, Info);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004635 Better2 = !::FinishTemplateArgumentDeduction(
4636 *this, PS1, PS2->getTemplateArgs(), Deduced, Info);
4637 }
4638
4639 if (Better1 == Better2)
4640 return 0;
4641
4642 return Better1 ? PS1 : PS2;
4643}
4644
Larisse Voufo30616382013-08-23 22:21:36 +00004645/// TODO: Unify with ClassTemplatePartialSpecializationDecl version?
4646/// May require unifying ClassTemplate(Partial)SpecializationDecl and
4647/// VarTemplate(Partial)SpecializationDecl with a new data
4648/// structure Template(Partial)SpecializationDecl, and
4649/// using Template(Partial)SpecializationDecl as input type.
Larisse Voufo39a1e502013-08-06 01:03:05 +00004650VarTemplatePartialSpecializationDecl *
4651Sema::getMoreSpecializedPartialSpecialization(
4652 VarTemplatePartialSpecializationDecl *PS1,
4653 VarTemplatePartialSpecializationDecl *PS2, SourceLocation Loc) {
4654 SmallVector<DeducedTemplateArgument, 4> Deduced;
4655 TemplateDeductionInfo Info(Loc);
4656
Richard Smithf04fd0b2013-12-12 23:14:16 +00004657 assert(PS1->getSpecializedTemplate() == PS2->getSpecializedTemplate() &&
Larisse Voufo39a1e502013-08-06 01:03:05 +00004658 "the partial specializations being compared should specialize"
4659 " the same template.");
4660 TemplateName Name(PS1->getSpecializedTemplate());
4661 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4662 QualType PT1 = Context.getTemplateSpecializationType(
4663 CanonTemplate, PS1->getTemplateArgs().data(),
4664 PS1->getTemplateArgs().size());
4665 QualType PT2 = Context.getTemplateSpecializationType(
4666 CanonTemplate, PS2->getTemplateArgs().data(),
4667 PS2->getTemplateArgs().size());
4668
4669 // Determine whether PS1 is at least as specialized as PS2
4670 Deduced.resize(PS2->getTemplateParameters()->size());
4671 bool Better1 = !DeduceTemplateArgumentsByTypeMatch(
4672 *this, PS2->getTemplateParameters(), PT2, PT1, Info, Deduced, TDF_None,
4673 /*PartialOrdering=*/true,
4674 /*RefParamComparisons=*/0);
4675 if (Better1) {
4676 SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(),
4677 Deduced.end());
Nick Lewycky56412332014-01-11 02:37:12 +00004678 InstantiatingTemplate Inst(*this, Loc, PS2, DeducedArgs, Info);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004679 Better1 = !::FinishTemplateArgumentDeduction(*this, PS2,
4680 PS1->getTemplateArgs(),
Douglas Gregor9225b022010-04-29 06:31:36 +00004681 Deduced, Info);
Argyrios Kyrtzidis7d391552010-11-05 23:25:18 +00004682 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004683
Douglas Gregorbe999392009-09-15 16:23:51 +00004684 // Determine whether PS2 is at least as specialized as PS1
Douglas Gregoradee3e32009-11-11 23:06:43 +00004685 Deduced.clear();
Douglas Gregorbe999392009-09-15 16:23:51 +00004686 Deduced.resize(PS1->getTemplateParameters()->size());
Sebastian Redlfb0b1f12012-01-17 22:49:52 +00004687 bool Better2 = !DeduceTemplateArgumentsByTypeMatch(*this,
4688 PS1->getTemplateParameters(),
Douglas Gregorb837ea42011-01-11 17:34:58 +00004689 PT1, PT2, Info, Deduced, TDF_None,
4690 /*PartialOrdering=*/true,
Douglas Gregor63814022011-01-21 17:29:42 +00004691 /*RefParamComparisons=*/0);
Argyrios Kyrtzidis7d391552010-11-05 23:25:18 +00004692 if (Better2) {
Richard Smith80934652012-07-16 01:09:10 +00004693 SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(),Deduced.end());
Nick Lewycky56412332014-01-11 02:37:12 +00004694 InstantiatingTemplate Inst(*this, Loc, PS1, DeducedArgs, Info);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004695 Better2 = !::FinishTemplateArgumentDeduction(*this, PS1,
4696 PS2->getTemplateArgs(),
Douglas Gregor9225b022010-04-29 06:31:36 +00004697 Deduced, Info);
Argyrios Kyrtzidis7d391552010-11-05 23:25:18 +00004698 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004699
Douglas Gregorbe999392009-09-15 16:23:51 +00004700 if (Better1 == Better2)
4701 return 0;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004702
Douglas Gregorbe999392009-09-15 16:23:51 +00004703 return Better1? PS1 : PS2;
4704}
4705
Mike Stump11289f42009-09-09 15:08:12 +00004706static void
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004707MarkUsedTemplateParameters(ASTContext &Ctx,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004708 const TemplateArgument &TemplateArg,
4709 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00004710 unsigned Depth,
Benjamin Kramere0513cb2012-01-30 16:17:39 +00004711 llvm::SmallBitVector &Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00004712
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004713/// \brief Mark the template parameters that are used by the given
Douglas Gregor91772d12009-06-13 00:26:55 +00004714/// expression.
Mike Stump11289f42009-09-09 15:08:12 +00004715static void
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004716MarkUsedTemplateParameters(ASTContext &Ctx,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004717 const Expr *E,
4718 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00004719 unsigned Depth,
Benjamin Kramere0513cb2012-01-30 16:17:39 +00004720 llvm::SmallBitVector &Used) {
Douglas Gregore8e9dd62011-01-03 17:17:50 +00004721 // We can deduce from a pack expansion.
4722 if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E))
4723 E = Expansion->getPattern();
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004724
Richard Smith34349002012-07-09 03:07:20 +00004725 // Skip through any implicit casts we added while type-checking, and any
4726 // substitutions performed by template alias expansion.
4727 while (1) {
4728 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
4729 E = ICE->getSubExpr();
4730 else if (const SubstNonTypeTemplateParmExpr *Subst =
4731 dyn_cast<SubstNonTypeTemplateParmExpr>(E))
4732 E = Subst->getReplacement();
4733 else
4734 break;
4735 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004736
4737 // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004738 // find other occurrences of template parameters.
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00004739 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
Douglas Gregor04b11522010-01-14 18:13:22 +00004740 if (!DRE)
Douglas Gregor91772d12009-06-13 00:26:55 +00004741 return;
4742
Mike Stump11289f42009-09-09 15:08:12 +00004743 const NonTypeTemplateParmDecl *NTTP
Douglas Gregor91772d12009-06-13 00:26:55 +00004744 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
4745 if (!NTTP)
4746 return;
4747
Douglas Gregor21610382009-10-29 00:04:11 +00004748 if (NTTP->getDepth() == Depth)
4749 Used[NTTP->getIndex()] = true;
Douglas Gregor91772d12009-06-13 00:26:55 +00004750}
4751
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004752/// \brief Mark the template parameters that are used by the given
4753/// nested name specifier.
4754static void
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004755MarkUsedTemplateParameters(ASTContext &Ctx,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004756 NestedNameSpecifier *NNS,
4757 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00004758 unsigned Depth,
Benjamin Kramere0513cb2012-01-30 16:17:39 +00004759 llvm::SmallBitVector &Used) {
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004760 if (!NNS)
4761 return;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004762
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004763 MarkUsedTemplateParameters(Ctx, NNS->getPrefix(), OnlyDeduced, Depth,
Douglas Gregor21610382009-10-29 00:04:11 +00004764 Used);
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004765 MarkUsedTemplateParameters(Ctx, QualType(NNS->getAsType(), 0),
Douglas Gregor21610382009-10-29 00:04:11 +00004766 OnlyDeduced, Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004767}
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004768
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004769/// \brief Mark the template parameters that are used by the given
4770/// template name.
4771static void
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004772MarkUsedTemplateParameters(ASTContext &Ctx,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004773 TemplateName Name,
4774 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00004775 unsigned Depth,
Benjamin Kramere0513cb2012-01-30 16:17:39 +00004776 llvm::SmallBitVector &Used) {
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004777 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
4778 if (TemplateTemplateParmDecl *TTP
Douglas Gregor21610382009-10-29 00:04:11 +00004779 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
4780 if (TTP->getDepth() == Depth)
4781 Used[TTP->getIndex()] = true;
4782 }
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004783 return;
4784 }
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004785
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004786 if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004787 MarkUsedTemplateParameters(Ctx, QTN->getQualifier(), OnlyDeduced,
Douglas Gregor9167f8b2009-11-11 01:00:40 +00004788 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004789 if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004790 MarkUsedTemplateParameters(Ctx, DTN->getQualifier(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00004791 Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004792}
4793
4794/// \brief Mark the template parameters that are used by the given
Douglas Gregor91772d12009-06-13 00:26:55 +00004795/// type.
Mike Stump11289f42009-09-09 15:08:12 +00004796static void
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004797MarkUsedTemplateParameters(ASTContext &Ctx, QualType T,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004798 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00004799 unsigned Depth,
Benjamin Kramere0513cb2012-01-30 16:17:39 +00004800 llvm::SmallBitVector &Used) {
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004801 if (T.isNull())
4802 return;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004803
Douglas Gregor91772d12009-06-13 00:26:55 +00004804 // Non-dependent types have nothing deducible
4805 if (!T->isDependentType())
4806 return;
4807
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004808 T = Ctx.getCanonicalType(T);
Douglas Gregor91772d12009-06-13 00:26:55 +00004809 switch (T->getTypeClass()) {
Douglas Gregor91772d12009-06-13 00:26:55 +00004810 case Type::Pointer:
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004811 MarkUsedTemplateParameters(Ctx,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004812 cast<PointerType>(T)->getPointeeType(),
4813 OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00004814 Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004815 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00004816 break;
4817
4818 case Type::BlockPointer:
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004819 MarkUsedTemplateParameters(Ctx,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004820 cast<BlockPointerType>(T)->getPointeeType(),
4821 OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00004822 Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004823 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00004824 break;
4825
4826 case Type::LValueReference:
4827 case Type::RValueReference:
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004828 MarkUsedTemplateParameters(Ctx,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004829 cast<ReferenceType>(T)->getPointeeType(),
4830 OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00004831 Depth,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004832 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00004833 break;
4834
4835 case Type::MemberPointer: {
4836 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004837 MarkUsedTemplateParameters(Ctx, MemPtr->getPointeeType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00004838 Depth, Used);
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004839 MarkUsedTemplateParameters(Ctx, QualType(MemPtr->getClass(), 0),
Douglas Gregor21610382009-10-29 00:04:11 +00004840 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00004841 break;
4842 }
4843
4844 case Type::DependentSizedArray:
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004845 MarkUsedTemplateParameters(Ctx,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004846 cast<DependentSizedArrayType>(T)->getSizeExpr(),
Douglas Gregor21610382009-10-29 00:04:11 +00004847 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00004848 // Fall through to check the element type
4849
4850 case Type::ConstantArray:
4851 case Type::IncompleteArray:
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004852 MarkUsedTemplateParameters(Ctx,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004853 cast<ArrayType>(T)->getElementType(),
Douglas Gregor21610382009-10-29 00:04:11 +00004854 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00004855 break;
4856
4857 case Type::Vector:
4858 case Type::ExtVector:
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004859 MarkUsedTemplateParameters(Ctx,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004860 cast<VectorType>(T)->getElementType(),
Douglas Gregor21610382009-10-29 00:04:11 +00004861 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00004862 break;
4863
Douglas Gregor758a8692009-06-17 21:51:59 +00004864 case Type::DependentSizedExtVector: {
4865 const DependentSizedExtVectorType *VecType
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00004866 = cast<DependentSizedExtVectorType>(T);
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004867 MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00004868 Depth, Used);
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004869 MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00004870 Depth, Used);
Douglas Gregor758a8692009-06-17 21:51:59 +00004871 break;
4872 }
4873
Douglas Gregor91772d12009-06-13 00:26:55 +00004874 case Type::FunctionProto: {
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00004875 const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
Alp Toker314cc812014-01-25 16:55:45 +00004876 MarkUsedTemplateParameters(Ctx, Proto->getReturnType(), OnlyDeduced, Depth,
4877 Used);
Alp Toker9cacbab2014-01-20 20:26:09 +00004878 for (unsigned I = 0, N = Proto->getNumParams(); I != N; ++I)
4879 MarkUsedTemplateParameters(Ctx, Proto->getParamType(I), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00004880 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00004881 break;
4882 }
4883
Douglas Gregor21610382009-10-29 00:04:11 +00004884 case Type::TemplateTypeParm: {
4885 const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
4886 if (TTP->getDepth() == Depth)
4887 Used[TTP->getIndex()] = true;
Douglas Gregor91772d12009-06-13 00:26:55 +00004888 break;
Douglas Gregor21610382009-10-29 00:04:11 +00004889 }
Douglas Gregor91772d12009-06-13 00:26:55 +00004890
Douglas Gregorfb322d82011-01-14 05:11:40 +00004891 case Type::SubstTemplateTypeParmPack: {
4892 const SubstTemplateTypeParmPackType *Subst
4893 = cast<SubstTemplateTypeParmPackType>(T);
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004894 MarkUsedTemplateParameters(Ctx,
Douglas Gregorfb322d82011-01-14 05:11:40 +00004895 QualType(Subst->getReplacedParameter(), 0),
4896 OnlyDeduced, Depth, Used);
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004897 MarkUsedTemplateParameters(Ctx, Subst->getArgumentPack(),
Douglas Gregorfb322d82011-01-14 05:11:40 +00004898 OnlyDeduced, Depth, Used);
4899 break;
4900 }
4901
John McCall2408e322010-04-27 00:57:59 +00004902 case Type::InjectedClassName:
4903 T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
4904 // fall through
4905
Douglas Gregor91772d12009-06-13 00:26:55 +00004906 case Type::TemplateSpecialization: {
Mike Stump11289f42009-09-09 15:08:12 +00004907 const TemplateSpecializationType *Spec
Douglas Gregor1e09bf83c2009-06-18 18:45:36 +00004908 = cast<TemplateSpecializationType>(T);
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004909 MarkUsedTemplateParameters(Ctx, Spec->getTemplateName(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00004910 Depth, Used);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004911
Douglas Gregord0ad2942010-12-23 01:24:45 +00004912 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004913 // If the template argument list of P contains a pack expansion that is not
4914 // the last template argument, the entire template argument list is a
Douglas Gregord0ad2942010-12-23 01:24:45 +00004915 // non-deduced context.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004916 if (OnlyDeduced &&
Douglas Gregord0ad2942010-12-23 01:24:45 +00004917 hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs()))
4918 break;
4919
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004920 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004921 MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth,
Douglas Gregor21610382009-10-29 00:04:11 +00004922 Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00004923 break;
4924 }
4925
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004926 case Type::Complex:
4927 if (!OnlyDeduced)
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004928 MarkUsedTemplateParameters(Ctx,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004929 cast<ComplexType>(T)->getElementType(),
Douglas Gregor21610382009-10-29 00:04:11 +00004930 OnlyDeduced, Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004931 break;
4932
Eli Friedman0dfb8892011-10-06 23:00:33 +00004933 case Type::Atomic:
4934 if (!OnlyDeduced)
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004935 MarkUsedTemplateParameters(Ctx,
Eli Friedman0dfb8892011-10-06 23:00:33 +00004936 cast<AtomicType>(T)->getValueType(),
4937 OnlyDeduced, Depth, Used);
4938 break;
4939
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00004940 case Type::DependentName:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004941 if (!OnlyDeduced)
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004942 MarkUsedTemplateParameters(Ctx,
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +00004943 cast<DependentNameType>(T)->getQualifier(),
Douglas Gregor21610382009-10-29 00:04:11 +00004944 OnlyDeduced, Depth, Used);
Douglas Gregore1d2ef32009-09-14 21:25:05 +00004945 break;
4946
John McCallc392f372010-06-11 00:33:02 +00004947 case Type::DependentTemplateSpecialization: {
4948 const DependentTemplateSpecializationType *Spec
4949 = cast<DependentTemplateSpecializationType>(T);
4950 if (!OnlyDeduced)
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004951 MarkUsedTemplateParameters(Ctx, Spec->getQualifier(),
John McCallc392f372010-06-11 00:33:02 +00004952 OnlyDeduced, Depth, Used);
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004953
Douglas Gregord0ad2942010-12-23 01:24:45 +00004954 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004955 // If the template argument list of P contains a pack expansion that is not
4956 // the last template argument, the entire template argument list is a
Douglas Gregord0ad2942010-12-23 01:24:45 +00004957 // non-deduced context.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00004958 if (OnlyDeduced &&
Douglas Gregord0ad2942010-12-23 01:24:45 +00004959 hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs()))
4960 break;
4961
John McCallc392f372010-06-11 00:33:02 +00004962 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004963 MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth,
John McCallc392f372010-06-11 00:33:02 +00004964 Used);
4965 break;
4966 }
4967
John McCallbd8d9bd2010-03-01 23:49:17 +00004968 case Type::TypeOf:
4969 if (!OnlyDeduced)
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004970 MarkUsedTemplateParameters(Ctx,
John McCallbd8d9bd2010-03-01 23:49:17 +00004971 cast<TypeOfType>(T)->getUnderlyingType(),
4972 OnlyDeduced, Depth, Used);
4973 break;
4974
4975 case Type::TypeOfExpr:
4976 if (!OnlyDeduced)
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004977 MarkUsedTemplateParameters(Ctx,
John McCallbd8d9bd2010-03-01 23:49:17 +00004978 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
4979 OnlyDeduced, Depth, Used);
4980 break;
4981
4982 case Type::Decltype:
4983 if (!OnlyDeduced)
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004984 MarkUsedTemplateParameters(Ctx,
John McCallbd8d9bd2010-03-01 23:49:17 +00004985 cast<DecltypeType>(T)->getUnderlyingExpr(),
4986 OnlyDeduced, Depth, Used);
4987 break;
4988
Alexis Hunte852b102011-05-24 22:41:36 +00004989 case Type::UnaryTransform:
4990 if (!OnlyDeduced)
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004991 MarkUsedTemplateParameters(Ctx,
Alexis Hunte852b102011-05-24 22:41:36 +00004992 cast<UnaryTransformType>(T)->getUnderlyingType(),
4993 OnlyDeduced, Depth, Used);
4994 break;
4995
Douglas Gregord2fa7662010-12-20 02:24:11 +00004996 case Type::PackExpansion:
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00004997 MarkUsedTemplateParameters(Ctx,
Douglas Gregord2fa7662010-12-20 02:24:11 +00004998 cast<PackExpansionType>(T)->getPattern(),
4999 OnlyDeduced, Depth, Used);
5000 break;
5001
Richard Smith30482bc2011-02-20 03:19:35 +00005002 case Type::Auto:
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00005003 MarkUsedTemplateParameters(Ctx,
Richard Smith30482bc2011-02-20 03:19:35 +00005004 cast<AutoType>(T)->getDeducedType(),
5005 OnlyDeduced, Depth, Used);
5006
Douglas Gregore1d2ef32009-09-14 21:25:05 +00005007 // None of these types have any template parameters in them.
Douglas Gregor91772d12009-06-13 00:26:55 +00005008 case Type::Builtin:
Douglas Gregor91772d12009-06-13 00:26:55 +00005009 case Type::VariableArray:
5010 case Type::FunctionNoProto:
5011 case Type::Record:
5012 case Type::Enum:
Douglas Gregor91772d12009-06-13 00:26:55 +00005013 case Type::ObjCInterface:
John McCall8b07ec22010-05-15 11:32:37 +00005014 case Type::ObjCObject:
Steve Narofffb4330f2009-06-17 22:40:22 +00005015 case Type::ObjCObjectPointer:
John McCallb96ec562009-12-04 22:46:56 +00005016 case Type::UnresolvedUsing:
Douglas Gregor91772d12009-06-13 00:26:55 +00005017#define TYPE(Class, Base)
5018#define ABSTRACT_TYPE(Class, Base)
5019#define DEPENDENT_TYPE(Class, Base)
5020#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
5021#include "clang/AST/TypeNodes.def"
5022 break;
5023 }
5024}
5025
Douglas Gregore1d2ef32009-09-14 21:25:05 +00005026/// \brief Mark the template parameters that are used by this
Douglas Gregor91772d12009-06-13 00:26:55 +00005027/// template argument.
Mike Stump11289f42009-09-09 15:08:12 +00005028static void
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00005029MarkUsedTemplateParameters(ASTContext &Ctx,
Douglas Gregore1d2ef32009-09-14 21:25:05 +00005030 const TemplateArgument &TemplateArg,
5031 bool OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00005032 unsigned Depth,
Benjamin Kramere0513cb2012-01-30 16:17:39 +00005033 llvm::SmallBitVector &Used) {
Douglas Gregor91772d12009-06-13 00:26:55 +00005034 switch (TemplateArg.getKind()) {
5035 case TemplateArgument::Null:
5036 case TemplateArgument::Integral:
Douglas Gregor31f55dc2012-04-06 22:40:38 +00005037 case TemplateArgument::Declaration:
Douglas Gregor91772d12009-06-13 00:26:55 +00005038 break;
Mike Stump11289f42009-09-09 15:08:12 +00005039
Eli Friedmanb826a002012-09-26 02:36:12 +00005040 case TemplateArgument::NullPtr:
5041 MarkUsedTemplateParameters(Ctx, TemplateArg.getNullPtrType(), OnlyDeduced,
5042 Depth, Used);
5043 break;
5044
Douglas Gregor91772d12009-06-13 00:26:55 +00005045 case TemplateArgument::Type:
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00005046 MarkUsedTemplateParameters(Ctx, TemplateArg.getAsType(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00005047 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00005048 break;
5049
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005050 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +00005051 case TemplateArgument::TemplateExpansion:
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00005052 MarkUsedTemplateParameters(Ctx,
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00005053 TemplateArg.getAsTemplateOrTemplatePattern(),
Douglas Gregor9167f8b2009-11-11 01:00:40 +00005054 OnlyDeduced, Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00005055 break;
5056
5057 case TemplateArgument::Expression:
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00005058 MarkUsedTemplateParameters(Ctx, TemplateArg.getAsExpr(), OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00005059 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00005060 break;
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00005061
Anders Carlssonbc343912009-06-15 17:04:53 +00005062 case TemplateArgument::Pack:
Douglas Gregore1d2ef32009-09-14 21:25:05 +00005063 for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
5064 PEnd = TemplateArg.pack_end();
5065 P != PEnd; ++P)
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00005066 MarkUsedTemplateParameters(Ctx, *P, OnlyDeduced, Depth, Used);
Anders Carlssonbc343912009-06-15 17:04:53 +00005067 break;
Douglas Gregor91772d12009-06-13 00:26:55 +00005068 }
5069}
5070
James Dennett41725122012-06-22 10:16:05 +00005071/// \brief Mark which template parameters can be deduced from a given
Douglas Gregor91772d12009-06-13 00:26:55 +00005072/// template argument list.
5073///
5074/// \param TemplateArgs the template argument list from which template
5075/// parameters will be deduced.
5076///
James Dennett41725122012-06-22 10:16:05 +00005077/// \param Used a bit vector whose elements will be set to \c true
Douglas Gregor91772d12009-06-13 00:26:55 +00005078/// to indicate when the corresponding template parameter will be
5079/// deduced.
Mike Stump11289f42009-09-09 15:08:12 +00005080void
Douglas Gregore1d2ef32009-09-14 21:25:05 +00005081Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
Douglas Gregor21610382009-10-29 00:04:11 +00005082 bool OnlyDeduced, unsigned Depth,
Benjamin Kramere0513cb2012-01-30 16:17:39 +00005083 llvm::SmallBitVector &Used) {
Douglas Gregord0ad2942010-12-23 01:24:45 +00005084 // C++0x [temp.deduct.type]p9:
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00005085 // If the template argument list of P contains a pack expansion that is not
5086 // the last template argument, the entire template argument list is a
Douglas Gregord0ad2942010-12-23 01:24:45 +00005087 // non-deduced context.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00005088 if (OnlyDeduced &&
Douglas Gregord0ad2942010-12-23 01:24:45 +00005089 hasPackExpansionBeforeEnd(TemplateArgs.data(), TemplateArgs.size()))
5090 return;
5091
Douglas Gregor91772d12009-06-13 00:26:55 +00005092 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00005093 ::MarkUsedTemplateParameters(Context, TemplateArgs[I], OnlyDeduced,
Douglas Gregor21610382009-10-29 00:04:11 +00005094 Depth, Used);
Douglas Gregor91772d12009-06-13 00:26:55 +00005095}
Douglas Gregorce23bae2009-09-18 23:21:38 +00005096
5097/// \brief Marks all of the template parameters that will be deduced by a
5098/// call to the given function template.
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00005099void
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00005100Sema::MarkDeducedTemplateParameters(ASTContext &Ctx,
Dmitri Gribenkofe0483d2013-01-23 17:21:11 +00005101 const FunctionTemplateDecl *FunctionTemplate,
Benjamin Kramere0513cb2012-01-30 16:17:39 +00005102 llvm::SmallBitVector &Deduced) {
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00005103 TemplateParameterList *TemplateParams
Douglas Gregorce23bae2009-09-18 23:21:38 +00005104 = FunctionTemplate->getTemplateParameters();
5105 Deduced.clear();
5106 Deduced.resize(TemplateParams->size());
NAKAMURA Takumif9cbcc42011-01-27 07:10:08 +00005107
Douglas Gregorce23bae2009-09-18 23:21:38 +00005108 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
5109 for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00005110 ::MarkUsedTemplateParameters(Ctx, Function->getParamDecl(I)->getType(),
Douglas Gregor21610382009-10-29 00:04:11 +00005111 true, TemplateParams->getDepth(), Deduced);
Douglas Gregorce23bae2009-09-18 23:21:38 +00005112}
Douglas Gregore65aacb2011-06-16 16:50:48 +00005113
5114bool hasDeducibleTemplateParameters(Sema &S,
5115 FunctionTemplateDecl *FunctionTemplate,
5116 QualType T) {
5117 if (!T->isDependentType())
5118 return false;
5119
5120 TemplateParameterList *TemplateParams
5121 = FunctionTemplate->getTemplateParameters();
Benjamin Kramere0513cb2012-01-30 16:17:39 +00005122 llvm::SmallBitVector Deduced(TemplateParams->size());
Argyrios Kyrtzidisf34950d2012-01-17 02:15:41 +00005123 ::MarkUsedTemplateParameters(S.Context, T, true, TemplateParams->getDepth(),
Douglas Gregore65aacb2011-06-16 16:50:48 +00005124 Deduced);
5125
Benjamin Kramere0513cb2012-01-30 16:17:39 +00005126 return Deduced.any();
Douglas Gregore65aacb2011-06-16 16:50:48 +00005127}