/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaTemplateDeduction.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===- SemaTemplateDeduction.cpp - Template Argument Deduction ------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file implements C++ template argument deduction. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "clang/Sema/TemplateDeduction.h" |
14 | | #include "TreeTransform.h" |
15 | | #include "TypeLocBuilder.h" |
16 | | #include "clang/AST/ASTContext.h" |
17 | | #include "clang/AST/ASTLambda.h" |
18 | | #include "clang/AST/Decl.h" |
19 | | #include "clang/AST/DeclAccessPair.h" |
20 | | #include "clang/AST/DeclBase.h" |
21 | | #include "clang/AST/DeclCXX.h" |
22 | | #include "clang/AST/DeclTemplate.h" |
23 | | #include "clang/AST/DeclarationName.h" |
24 | | #include "clang/AST/Expr.h" |
25 | | #include "clang/AST/ExprCXX.h" |
26 | | #include "clang/AST/NestedNameSpecifier.h" |
27 | | #include "clang/AST/RecursiveASTVisitor.h" |
28 | | #include "clang/AST/TemplateBase.h" |
29 | | #include "clang/AST/TemplateName.h" |
30 | | #include "clang/AST/Type.h" |
31 | | #include "clang/AST/TypeLoc.h" |
32 | | #include "clang/AST/UnresolvedSet.h" |
33 | | #include "clang/Basic/AddressSpaces.h" |
34 | | #include "clang/Basic/ExceptionSpecificationType.h" |
35 | | #include "clang/Basic/LLVM.h" |
36 | | #include "clang/Basic/LangOptions.h" |
37 | | #include "clang/Basic/PartialDiagnostic.h" |
38 | | #include "clang/Basic/SourceLocation.h" |
39 | | #include "clang/Basic/Specifiers.h" |
40 | | #include "clang/Sema/Ownership.h" |
41 | | #include "clang/Sema/Sema.h" |
42 | | #include "clang/Sema/Template.h" |
43 | | #include "llvm/ADT/APInt.h" |
44 | | #include "llvm/ADT/APSInt.h" |
45 | | #include "llvm/ADT/ArrayRef.h" |
46 | | #include "llvm/ADT/DenseMap.h" |
47 | | #include "llvm/ADT/FoldingSet.h" |
48 | | #include "llvm/ADT/Optional.h" |
49 | | #include "llvm/ADT/SmallBitVector.h" |
50 | | #include "llvm/ADT/SmallPtrSet.h" |
51 | | #include "llvm/ADT/SmallVector.h" |
52 | | #include "llvm/Support/Casting.h" |
53 | | #include "llvm/Support/Compiler.h" |
54 | | #include "llvm/Support/ErrorHandling.h" |
55 | | #include <algorithm> |
56 | | #include <cassert> |
57 | | #include <tuple> |
58 | | #include <utility> |
59 | | |
60 | | namespace clang { |
61 | | |
62 | | /// Various flags that control template argument deduction. |
63 | | /// |
64 | | /// These flags can be bitwise-OR'd together. |
65 | | enum TemplateDeductionFlags { |
66 | | /// No template argument deduction flags, which indicates the |
67 | | /// strictest results for template argument deduction (as used for, e.g., |
68 | | /// matching class template partial specializations). |
69 | | TDF_None = 0, |
70 | | |
71 | | /// Within template argument deduction from a function call, we are |
72 | | /// matching with a parameter type for which the original parameter was |
73 | | /// a reference. |
74 | | TDF_ParamWithReferenceType = 0x1, |
75 | | |
76 | | /// Within template argument deduction from a function call, we |
77 | | /// are matching in a case where we ignore cv-qualifiers. |
78 | | TDF_IgnoreQualifiers = 0x02, |
79 | | |
80 | | /// Within template argument deduction from a function call, |
81 | | /// we are matching in a case where we can perform template argument |
82 | | /// deduction from a template-id of a derived class of the argument type. |
83 | | TDF_DerivedClass = 0x04, |
84 | | |
85 | | /// Allow non-dependent types to differ, e.g., when performing |
86 | | /// template argument deduction from a function call where conversions |
87 | | /// may apply. |
88 | | TDF_SkipNonDependent = 0x08, |
89 | | |
90 | | /// Whether we are performing template argument deduction for |
91 | | /// parameters and arguments in a top-level template argument |
92 | | TDF_TopLevelParameterTypeList = 0x10, |
93 | | |
94 | | /// Within template argument deduction from overload resolution per |
95 | | /// C++ [over.over] allow matching function types that are compatible in |
96 | | /// terms of noreturn and default calling convention adjustments, or |
97 | | /// similarly matching a declared template specialization against a |
98 | | /// possible template, per C++ [temp.deduct.decl]. In either case, permit |
99 | | /// deduction where the parameter is a function type that can be converted |
100 | | /// to the argument type. |
101 | | TDF_AllowCompatibleFunctionType = 0x20, |
102 | | |
103 | | /// Within template argument deduction for a conversion function, we are |
104 | | /// matching with an argument type for which the original argument was |
105 | | /// a reference. |
106 | | TDF_ArgWithReferenceType = 0x40, |
107 | | }; |
108 | | } |
109 | | |
110 | | using namespace clang; |
111 | | using namespace sema; |
112 | | |
113 | | /// Compare two APSInts, extending and switching the sign as |
114 | | /// necessary to compare their values regardless of underlying type. |
115 | 510k | static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) { |
116 | 510k | if (Y.getBitWidth() > X.getBitWidth()) |
117 | 12 | X = X.extend(Y.getBitWidth()); |
118 | 510k | else if (Y.getBitWidth() < X.getBitWidth()) |
119 | 12 | Y = Y.extend(X.getBitWidth()); |
120 | | |
121 | | // If there is a signedness mismatch, correct it. |
122 | 510k | if (X.isSigned() != Y.isSigned()) { |
123 | | // If the signed value is negative, then the values cannot be the same. |
124 | 16 | if ((Y.isSigned() && Y.isNegative()9 ) || (X.isSigned() && X.isNegative()7 )) |
125 | 0 | return false; |
126 | | |
127 | 16 | Y.setIsSigned(true); |
128 | 16 | X.setIsSigned(true); |
129 | 16 | } |
130 | | |
131 | 510k | return X == Y; |
132 | 510k | } |
133 | | |
134 | | static Sema::TemplateDeductionResult DeduceTemplateArgumentsByTypeMatch( |
135 | | Sema &S, TemplateParameterList *TemplateParams, QualType Param, |
136 | | QualType Arg, TemplateDeductionInfo &Info, |
137 | | SmallVectorImpl<DeducedTemplateArgument> &Deduced, unsigned TDF, |
138 | | bool PartialOrdering = false, bool DeducedFromArrayBound = false); |
139 | | |
140 | | static Sema::TemplateDeductionResult |
141 | | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
142 | | ArrayRef<TemplateArgument> Ps, |
143 | | ArrayRef<TemplateArgument> As, |
144 | | TemplateDeductionInfo &Info, |
145 | | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
146 | | bool NumberOfArgumentsMustMatch); |
147 | | |
148 | | static void MarkUsedTemplateParameters(ASTContext &Ctx, |
149 | | const TemplateArgument &TemplateArg, |
150 | | bool OnlyDeduced, unsigned Depth, |
151 | | llvm::SmallBitVector &Used); |
152 | | |
153 | | static void MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
154 | | bool OnlyDeduced, unsigned Level, |
155 | | llvm::SmallBitVector &Deduced); |
156 | | |
157 | | /// If the given expression is of a form that permits the deduction |
158 | | /// of a non-type template parameter, return the declaration of that |
159 | | /// non-type template parameter. |
160 | | static const NonTypeTemplateParmDecl * |
161 | 380k | getDeducedParameterFromExpr(const Expr *E, unsigned Depth) { |
162 | | // If we are within an alias template, the expression may have undergone |
163 | | // any number of parameter substitutions already. |
164 | 391k | while (true) { |
165 | 391k | if (const auto *IC = dyn_cast<ImplicitCastExpr>(E)) |
166 | 10.5k | E = IC->getSubExpr(); |
167 | 380k | else if (const auto *CE = dyn_cast<ConstantExpr>(E)) |
168 | 0 | E = CE->getSubExpr(); |
169 | 380k | else if (const auto *Subst = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
170 | 223 | E = Subst->getReplacement(); |
171 | 380k | else if (const auto *CCE = dyn_cast<CXXConstructExpr>(E)) { |
172 | | // Look through implicit copy construction from an lvalue of the same type. |
173 | 0 | if (CCE->getParenOrBraceRange().isValid()) |
174 | 0 | break; |
175 | | // Note, there could be default arguments. |
176 | 0 | assert(CCE->getNumArgs() >= 1 && "implicit construct expr should have 1 arg"); |
177 | 0 | E = CCE->getArg(0); |
178 | 0 | } else |
179 | 380k | break; |
180 | 391k | } |
181 | | |
182 | 380k | if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) |
183 | 370k | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) |
184 | 370k | if (NTTP->getDepth() == Depth) |
185 | 370k | return NTTP; |
186 | | |
187 | 10.4k | return nullptr; |
188 | 380k | } |
189 | | |
190 | | static const NonTypeTemplateParmDecl * |
191 | 257k | getDeducedParameterFromExpr(TemplateDeductionInfo &Info, Expr *E) { |
192 | 257k | return getDeducedParameterFromExpr(E, Info.getDeducedDepth()); |
193 | 257k | } |
194 | | |
195 | | /// Determine whether two declaration pointers refer to the same |
196 | | /// declaration. |
197 | 97 | static bool isSameDeclaration(Decl *X, Decl *Y) { |
198 | 97 | if (NamedDecl *NX = dyn_cast<NamedDecl>(X)) |
199 | 97 | X = NX->getUnderlyingDecl(); |
200 | 97 | if (NamedDecl *NY = dyn_cast<NamedDecl>(Y)) |
201 | 97 | Y = NY->getUnderlyingDecl(); |
202 | | |
203 | 97 | return X->getCanonicalDecl() == Y->getCanonicalDecl(); |
204 | 97 | } |
205 | | |
206 | | /// Verify that the given, deduced template arguments are compatible. |
207 | | /// |
208 | | /// \returns The deduced template argument, or a NULL template argument if |
209 | | /// the deduced template arguments were incompatible. |
210 | | static DeducedTemplateArgument |
211 | | checkDeducedTemplateArguments(ASTContext &Context, |
212 | | const DeducedTemplateArgument &X, |
213 | 2.01M | const DeducedTemplateArgument &Y) { |
214 | | // We have no deduction for one or both of the arguments; they're compatible. |
215 | 2.01M | if (X.isNull()) |
216 | 1.92M | return Y; |
217 | 87.0k | if (Y.isNull()) |
218 | 58 | return X; |
219 | | |
220 | | // If we have two non-type template argument values deduced for the same |
221 | | // parameter, they must both match the type of the parameter, and thus must |
222 | | // match each other's type. As we're only keeping one of them, we must check |
223 | | // for that now. The exception is that if either was deduced from an array |
224 | | // bound, the type is permitted to differ. |
225 | 86.9k | if (!X.wasDeducedFromArrayBound() && !Y.wasDeducedFromArrayBound()86.8k ) { |
226 | 86.7k | QualType XType = X.getNonTypeTemplateArgumentType(); |
227 | 86.7k | if (!XType.isNull()) { |
228 | 1.02k | QualType YType = Y.getNonTypeTemplateArgumentType(); |
229 | 1.02k | if (YType.isNull() || !Context.hasSameType(XType, YType)) |
230 | 24 | return DeducedTemplateArgument(); |
231 | 1.02k | } |
232 | 86.7k | } |
233 | | |
234 | 86.9k | switch (X.getKind()) { |
235 | 0 | case TemplateArgument::Null: |
236 | 0 | llvm_unreachable("Non-deduced template arguments handled above"); |
237 | |
|
238 | 85.6k | case TemplateArgument::Type: |
239 | | // If two template type arguments have the same type, they're compatible. |
240 | 85.6k | if (Y.getKind() == TemplateArgument::Type && |
241 | 85.6k | Context.hasSameType(X.getAsType(), Y.getAsType())) |
242 | 59.0k | return X; |
243 | | |
244 | | // If one of the two arguments was deduced from an array bound, the other |
245 | | // supersedes it. |
246 | 26.5k | if (X.wasDeducedFromArrayBound() != Y.wasDeducedFromArrayBound()) |
247 | 25 | return X.wasDeducedFromArrayBound() ? Y3 : X22 ; |
248 | | |
249 | | // The arguments are not compatible. |
250 | 26.5k | return DeducedTemplateArgument(); |
251 | | |
252 | 227 | case TemplateArgument::Integral: |
253 | | // If we deduced a constant in one case and either a dependent expression or |
254 | | // declaration in another case, keep the integral constant. |
255 | | // If both are integral constants with the same value, keep that value. |
256 | 227 | if (Y.getKind() == TemplateArgument::Expression || |
257 | 227 | Y.getKind() == TemplateArgument::Declaration || |
258 | 227 | (Y.getKind() == TemplateArgument::Integral && |
259 | 227 | hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()))) |
260 | 208 | return X.wasDeducedFromArrayBound() ? Y131 : X77 ; |
261 | | |
262 | | // All other combinations are incompatible. |
263 | 19 | return DeducedTemplateArgument(); |
264 | | |
265 | 26 | case TemplateArgument::Template: |
266 | 26 | if (Y.getKind() == TemplateArgument::Template && |
267 | 26 | Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate())) |
268 | 26 | return X; |
269 | | |
270 | | // All other combinations are incompatible. |
271 | 0 | return DeducedTemplateArgument(); |
272 | | |
273 | 0 | case TemplateArgument::TemplateExpansion: |
274 | 0 | if (Y.getKind() == TemplateArgument::TemplateExpansion && |
275 | 0 | Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(), |
276 | 0 | Y.getAsTemplateOrTemplatePattern())) |
277 | 0 | return X; |
278 | | |
279 | | // All other combinations are incompatible. |
280 | 0 | return DeducedTemplateArgument(); |
281 | | |
282 | 918 | case TemplateArgument::Expression: { |
283 | 918 | if (Y.getKind() != TemplateArgument::Expression) |
284 | 0 | return checkDeducedTemplateArguments(Context, Y, X); |
285 | | |
286 | | // Compare the expressions for equality |
287 | 918 | llvm::FoldingSetNodeID ID1, ID2; |
288 | 918 | X.getAsExpr()->Profile(ID1, Context, true); |
289 | 918 | Y.getAsExpr()->Profile(ID2, Context, true); |
290 | 918 | if (ID1 == ID2) |
291 | 0 | return X.wasDeducedFromArrayBound() ? Y : X; |
292 | | |
293 | | // Differing dependent expressions are incompatible. |
294 | 918 | return DeducedTemplateArgument(); |
295 | 918 | } |
296 | | |
297 | 9 | case TemplateArgument::Declaration: |
298 | 9 | assert(!X.wasDeducedFromArrayBound()); |
299 | | |
300 | | // If we deduced a declaration and a dependent expression, keep the |
301 | | // declaration. |
302 | 9 | if (Y.getKind() == TemplateArgument::Expression) |
303 | 0 | return X; |
304 | | |
305 | | // If we deduced a declaration and an integral constant, keep the |
306 | | // integral constant and whichever type did not come from an array |
307 | | // bound. |
308 | 9 | if (Y.getKind() == TemplateArgument::Integral) { |
309 | 0 | if (Y.wasDeducedFromArrayBound()) |
310 | 0 | return TemplateArgument(Context, Y.getAsIntegral(), |
311 | 0 | X.getParamTypeForDecl()); |
312 | 0 | return Y; |
313 | 0 | } |
314 | | |
315 | | // If we deduced two declarations, make sure that they refer to the |
316 | | // same declaration. |
317 | 9 | if (Y.getKind() == TemplateArgument::Declaration && |
318 | 9 | isSameDeclaration(X.getAsDecl(), Y.getAsDecl())) |
319 | 6 | return X; |
320 | | |
321 | | // All other combinations are incompatible. |
322 | 3 | return DeducedTemplateArgument(); |
323 | | |
324 | 0 | case TemplateArgument::NullPtr: |
325 | | // If we deduced a null pointer and a dependent expression, keep the |
326 | | // null pointer. |
327 | 0 | if (Y.getKind() == TemplateArgument::Expression) |
328 | 0 | return X; |
329 | | |
330 | | // If we deduced a null pointer and an integral constant, keep the |
331 | | // integral constant. |
332 | 0 | if (Y.getKind() == TemplateArgument::Integral) |
333 | 0 | return Y; |
334 | | |
335 | | // If we deduced two null pointers, they are the same. |
336 | 0 | if (Y.getKind() == TemplateArgument::NullPtr) |
337 | 0 | return X; |
338 | | |
339 | | // All other combinations are incompatible. |
340 | 0 | return DeducedTemplateArgument(); |
341 | | |
342 | 129 | case TemplateArgument::Pack: { |
343 | 129 | if (Y.getKind() != TemplateArgument::Pack || |
344 | 129 | X.pack_size() != Y.pack_size()) |
345 | 36 | return DeducedTemplateArgument(); |
346 | | |
347 | 93 | llvm::SmallVector<TemplateArgument, 8> NewPack; |
348 | 93 | for (TemplateArgument::pack_iterator XA = X.pack_begin(), |
349 | 93 | XAEnd = X.pack_end(), |
350 | 93 | YA = Y.pack_begin(); |
351 | 233 | XA != XAEnd; ++XA, ++YA140 ) { |
352 | 159 | TemplateArgument Merged = checkDeducedTemplateArguments( |
353 | 159 | Context, DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()), |
354 | 159 | DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound())); |
355 | 159 | if (Merged.isNull() && !(23 XA->isNull()23 && YA->isNull()4 )) |
356 | 19 | return DeducedTemplateArgument(); |
357 | 140 | NewPack.push_back(Merged); |
358 | 140 | } |
359 | | |
360 | 74 | return DeducedTemplateArgument( |
361 | 74 | TemplateArgument::CreatePackCopy(Context, NewPack), |
362 | 74 | X.wasDeducedFromArrayBound() && Y.wasDeducedFromArrayBound()2 ); |
363 | 93 | } |
364 | 86.9k | } |
365 | | |
366 | 0 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
367 | 0 | } |
368 | | |
369 | | /// Deduce the value of the given non-type template parameter |
370 | | /// as the given deduced template argument. All non-type template parameter |
371 | | /// deduction is funneled through here. |
372 | | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
373 | | Sema &S, TemplateParameterList *TemplateParams, |
374 | | const NonTypeTemplateParmDecl *NTTP, const DeducedTemplateArgument &NewDeduced, |
375 | | QualType ValueType, TemplateDeductionInfo &Info, |
376 | 257k | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
377 | 257k | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
378 | 257k | "deducing non-type template argument with wrong depth"); |
379 | | |
380 | 0 | DeducedTemplateArgument Result = checkDeducedTemplateArguments( |
381 | 257k | S.Context, Deduced[NTTP->getIndex()], NewDeduced); |
382 | 257k | if (Result.isNull()) { |
383 | 963 | Info.Param = const_cast<NonTypeTemplateParmDecl*>(NTTP); |
384 | 963 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
385 | 963 | Info.SecondArg = NewDeduced; |
386 | 963 | return Sema::TDK_Inconsistent; |
387 | 963 | } |
388 | | |
389 | 256k | Deduced[NTTP->getIndex()] = Result; |
390 | 256k | if (!S.getLangOpts().CPlusPlus17) |
391 | 244k | return Sema::TDK_Success; |
392 | | |
393 | 11.8k | if (NTTP->isExpandedParameterPack()) |
394 | | // FIXME: We may still need to deduce parts of the type here! But we |
395 | | // don't have any way to find which slice of the type to use, and the |
396 | | // type stored on the NTTP itself is nonsense. Perhaps the type of an |
397 | | // expanded NTTP should be a pack expansion type? |
398 | 24 | return Sema::TDK_Success; |
399 | | |
400 | | // Get the type of the parameter for deduction. If it's a (dependent) array |
401 | | // or function type, we will not have decayed it yet, so do that now. |
402 | 11.7k | QualType ParamType = S.Context.getAdjustedParameterType(NTTP->getType()); |
403 | 11.7k | if (auto *Expansion = dyn_cast<PackExpansionType>(ParamType)) |
404 | 39 | ParamType = Expansion->getPattern(); |
405 | | |
406 | | // FIXME: It's not clear how deduction of a parameter of reference |
407 | | // type from an argument (of non-reference type) should be performed. |
408 | | // For now, we just remove reference types from both sides and let |
409 | | // the final check for matching types sort out the mess. |
410 | 11.7k | ValueType = ValueType.getNonReferenceType(); |
411 | 11.7k | if (ParamType->isReferenceType()) |
412 | 11 | ParamType = ParamType.getNonReferenceType(); |
413 | 11.7k | else |
414 | | // Top-level cv-qualifiers are irrelevant for a non-reference type. |
415 | 11.7k | ValueType = ValueType.getUnqualifiedType(); |
416 | | |
417 | 11.7k | return DeduceTemplateArgumentsByTypeMatch( |
418 | 11.7k | S, TemplateParams, ParamType, ValueType, Info, Deduced, |
419 | 11.7k | TDF_SkipNonDependent, /*PartialOrdering=*/false, |
420 | 11.7k | /*ArrayBound=*/NewDeduced.wasDeducedFromArrayBound()); |
421 | 11.8k | } |
422 | | |
423 | | /// Deduce the value of the given non-type template parameter |
424 | | /// from the given integral constant. |
425 | | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
426 | | Sema &S, TemplateParameterList *TemplateParams, |
427 | | const NonTypeTemplateParmDecl *NTTP, const llvm::APSInt &Value, |
428 | | QualType ValueType, bool DeducedFromArrayBound, TemplateDeductionInfo &Info, |
429 | 179k | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
430 | 179k | return DeduceNonTypeTemplateArgument( |
431 | 179k | S, TemplateParams, NTTP, |
432 | 179k | DeducedTemplateArgument(S.Context, Value, ValueType, |
433 | 179k | DeducedFromArrayBound), |
434 | 179k | ValueType, Info, Deduced); |
435 | 179k | } |
436 | | |
437 | | /// Deduce the value of the given non-type template parameter |
438 | | /// from the given null pointer template argument type. |
439 | | static Sema::TemplateDeductionResult DeduceNullPtrTemplateArgument( |
440 | | Sema &S, TemplateParameterList *TemplateParams, |
441 | | const NonTypeTemplateParmDecl *NTTP, QualType NullPtrType, |
442 | | TemplateDeductionInfo &Info, |
443 | 39 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
444 | 39 | Expr *Value = S.ImpCastExprToType( |
445 | 39 | new (S.Context) CXXNullPtrLiteralExpr(S.Context.NullPtrTy, |
446 | 39 | NTTP->getLocation()), |
447 | 39 | NullPtrType, |
448 | 39 | NullPtrType->isMemberPointerType() ? CK_NullToMemberPointer4 |
449 | 39 | : CK_NullToPointer35 ) |
450 | 39 | .get(); |
451 | 39 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
452 | 39 | DeducedTemplateArgument(Value), |
453 | 39 | Value->getType(), Info, Deduced); |
454 | 39 | } |
455 | | |
456 | | /// Deduce the value of the given non-type template parameter |
457 | | /// from the given type- or value-dependent expression. |
458 | | /// |
459 | | /// \returns true if deduction succeeded, false otherwise. |
460 | | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
461 | | Sema &S, TemplateParameterList *TemplateParams, |
462 | | const NonTypeTemplateParmDecl *NTTP, Expr *Value, TemplateDeductionInfo &Info, |
463 | 77.0k | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
464 | 77.0k | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
465 | 77.0k | DeducedTemplateArgument(Value), |
466 | 77.0k | Value->getType(), Info, Deduced); |
467 | 77.0k | } |
468 | | |
469 | | /// Deduce the value of the given non-type template parameter |
470 | | /// from the given declaration. |
471 | | /// |
472 | | /// \returns true if deduction succeeded, false otherwise. |
473 | | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
474 | | Sema &S, TemplateParameterList *TemplateParams, |
475 | | const NonTypeTemplateParmDecl *NTTP, ValueDecl *D, QualType T, |
476 | | TemplateDeductionInfo &Info, |
477 | 1.19k | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
478 | 1.19k | D = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr0 ; |
479 | 1.19k | TemplateArgument New(D, T); |
480 | 1.19k | return DeduceNonTypeTemplateArgument( |
481 | 1.19k | S, TemplateParams, NTTP, DeducedTemplateArgument(New), T, Info, Deduced); |
482 | 1.19k | } |
483 | | |
484 | | static Sema::TemplateDeductionResult |
485 | | DeduceTemplateArguments(Sema &S, |
486 | | TemplateParameterList *TemplateParams, |
487 | | TemplateName Param, |
488 | | TemplateName Arg, |
489 | | TemplateDeductionInfo &Info, |
490 | 769k | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
491 | 769k | TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); |
492 | 769k | if (!ParamDecl) { |
493 | | // The parameter type is dependent and is not a template template parameter, |
494 | | // so there is nothing that we can deduce. |
495 | 12 | return Sema::TDK_Success; |
496 | 12 | } |
497 | | |
498 | 769k | if (TemplateTemplateParmDecl *TempParam |
499 | 769k | = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { |
500 | | // If we're not deducing at this depth, there's nothing to deduce. |
501 | 4.14k | if (TempParam->getDepth() != Info.getDeducedDepth()) |
502 | 0 | return Sema::TDK_Success; |
503 | | |
504 | 4.14k | DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg)); |
505 | 4.14k | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
506 | 4.14k | Deduced[TempParam->getIndex()], |
507 | 4.14k | NewDeduced); |
508 | 4.14k | if (Result.isNull()) { |
509 | 0 | Info.Param = TempParam; |
510 | 0 | Info.FirstArg = Deduced[TempParam->getIndex()]; |
511 | 0 | Info.SecondArg = NewDeduced; |
512 | 0 | return Sema::TDK_Inconsistent; |
513 | 0 | } |
514 | | |
515 | 4.14k | Deduced[TempParam->getIndex()] = Result; |
516 | 4.14k | return Sema::TDK_Success; |
517 | 4.14k | } |
518 | | |
519 | | // Verify that the two template names are equivalent. |
520 | 765k | if (S.Context.hasSameTemplateName(Param, Arg)) |
521 | 593k | return Sema::TDK_Success; |
522 | | |
523 | | // Mismatch of non-dependent template parameter to argument. |
524 | 171k | Info.FirstArg = TemplateArgument(Param); |
525 | 171k | Info.SecondArg = TemplateArgument(Arg); |
526 | 171k | return Sema::TDK_NonDeducedMismatch; |
527 | 765k | } |
528 | | |
529 | | /// Deduce the template arguments by comparing the template parameter |
530 | | /// type (which is a template-id) with the template argument type. |
531 | | /// |
532 | | /// \param S the Sema |
533 | | /// |
534 | | /// \param TemplateParams the template parameters that we are deducing |
535 | | /// |
536 | | /// \param P the parameter type |
537 | | /// |
538 | | /// \param A the argument type |
539 | | /// |
540 | | /// \param Info information about the template argument deduction itself |
541 | | /// |
542 | | /// \param Deduced the deduced template arguments |
543 | | /// |
544 | | /// \returns the result of template argument deduction so far. Note that a |
545 | | /// "success" result means that template argument deduction has not yet failed, |
546 | | /// but it may still fail, later, for other reasons. |
547 | | static Sema::TemplateDeductionResult |
548 | | DeduceTemplateSpecArguments(Sema &S, TemplateParameterList *TemplateParams, |
549 | | const QualType P, QualType A, |
550 | | TemplateDeductionInfo &Info, |
551 | 1.48M | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
552 | 1.48M | QualType UP = P; |
553 | 1.48M | if (const auto *IP = P->getAs<InjectedClassNameType>()) |
554 | 423 | UP = IP->getInjectedSpecializationType(); |
555 | | // FIXME: Try to preserve type sugar here, which is hard |
556 | | // because of the unresolved template arguments. |
557 | 1.48M | const auto *TP = UP.getCanonicalType()->castAs<TemplateSpecializationType>(); |
558 | 1.48M | ArrayRef<TemplateArgument> PResolved = TP->template_arguments(); |
559 | | |
560 | 1.48M | QualType UA = A; |
561 | | // Treat an injected-class-name as its underlying template-id. |
562 | 1.48M | if (const auto *Injected = A->getAs<InjectedClassNameType>()) |
563 | 14 | UA = Injected->getInjectedSpecializationType(); |
564 | | |
565 | | // Check whether the template argument is a dependent template-id. |
566 | | // FIXME: Should not lose sugar here. |
567 | 1.48M | if (const auto *SA = |
568 | 1.48M | dyn_cast<TemplateSpecializationType>(UA.getCanonicalType())) { |
569 | | // Perform template argument deduction for the template name. |
570 | 415k | if (auto Result = |
571 | 415k | DeduceTemplateArguments(S, TemplateParams, TP->getTemplateName(), |
572 | 415k | SA->getTemplateName(), Info, Deduced)) |
573 | 6 | return Result; |
574 | | // Perform template argument deduction on each template |
575 | | // argument. Ignore any missing/extra arguments, since they could be |
576 | | // filled in by default arguments. |
577 | 415k | return DeduceTemplateArguments(S, TemplateParams, PResolved, |
578 | 415k | SA->template_arguments(), Info, Deduced, |
579 | 415k | /*NumberOfArgumentsMustMatch=*/false); |
580 | 415k | } |
581 | | |
582 | | // If the argument type is a class template specialization, we |
583 | | // perform template argument deduction using its template |
584 | | // arguments. |
585 | 1.07M | const auto *RA = UA->getAs<RecordType>(); |
586 | 1.07M | const auto *SA = |
587 | 1.07M | RA ? dyn_cast<ClassTemplateSpecializationDecl>(RA->getDecl())498k : nullptr571k ; |
588 | 1.07M | if (!SA) { |
589 | 717k | Info.FirstArg = TemplateArgument(P); |
590 | 717k | Info.SecondArg = TemplateArgument(A); |
591 | 717k | return Sema::TDK_NonDeducedMismatch; |
592 | 717k | } |
593 | | |
594 | | // Perform template argument deduction for the template name. |
595 | 353k | if (auto Result = DeduceTemplateArguments( |
596 | 353k | S, TemplateParams, TP->getTemplateName(), |
597 | 353k | TemplateName(SA->getSpecializedTemplate()), Info, Deduced)) |
598 | 171k | return Result; |
599 | | |
600 | | // Perform template argument deduction for the template arguments. |
601 | 181k | return DeduceTemplateArguments(S, TemplateParams, PResolved, |
602 | 181k | SA->getTemplateArgs().asArray(), Info, Deduced, |
603 | 181k | /*NumberOfArgumentsMustMatch=*/true); |
604 | 353k | } |
605 | | |
606 | 1.49M | static bool IsPossiblyOpaquelyQualifiedTypeInternal(const Type *T) { |
607 | 1.49M | assert(T->isCanonicalUnqualified()); |
608 | | |
609 | 0 | switch (T->getTypeClass()) { |
610 | 0 | case Type::TypeOfExpr: |
611 | 0 | case Type::TypeOf: |
612 | 104k | case Type::DependentName: |
613 | 112k | case Type::Decltype: |
614 | 112k | case Type::UnresolvedUsing: |
615 | 222k | case Type::TemplateTypeParm: |
616 | 222k | return true; |
617 | | |
618 | 23 | case Type::ConstantArray: |
619 | 56.7k | case Type::IncompleteArray: |
620 | 56.7k | case Type::VariableArray: |
621 | 109k | case Type::DependentSizedArray: |
622 | 109k | return IsPossiblyOpaquelyQualifiedTypeInternal( |
623 | 109k | cast<ArrayType>(T)->getElementType().getTypePtr()); |
624 | | |
625 | 1.15M | default: |
626 | 1.15M | return false; |
627 | 1.49M | } |
628 | 1.49M | } |
629 | | |
630 | | /// Determines whether the given type is an opaque type that |
631 | | /// might be more qualified when instantiated. |
632 | 1.38M | static bool IsPossiblyOpaquelyQualifiedType(QualType T) { |
633 | 1.38M | return IsPossiblyOpaquelyQualifiedTypeInternal( |
634 | 1.38M | T->getCanonicalTypeInternal().getTypePtr()); |
635 | 1.38M | } |
636 | | |
637 | | /// Helper function to build a TemplateParameter when we don't |
638 | | /// know its type statically. |
639 | 106k | static TemplateParameter makeTemplateParameter(Decl *D) { |
640 | 106k | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
641 | 80.4k | return TemplateParameter(TTP); |
642 | 26.0k | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
643 | 26.0k | return TemplateParameter(NTTP); |
644 | | |
645 | 27 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
646 | 26.0k | } |
647 | | |
648 | | /// A pack that we're currently deducing. |
649 | | struct clang::DeducedPack { |
650 | | // The index of the pack. |
651 | | unsigned Index; |
652 | | |
653 | | // The old value of the pack before we started deducing it. |
654 | | DeducedTemplateArgument Saved; |
655 | | |
656 | | // A deferred value of this pack from an inner deduction, that couldn't be |
657 | | // deduced because this deduction hadn't happened yet. |
658 | | DeducedTemplateArgument DeferredDeduction; |
659 | | |
660 | | // The new value of the pack. |
661 | | SmallVector<DeducedTemplateArgument, 4> New; |
662 | | |
663 | | // The outer deduction for this pack, if any. |
664 | | DeducedPack *Outer = nullptr; |
665 | | |
666 | 70.8k | DeducedPack(unsigned Index) : Index(Index) {} |
667 | | }; |
668 | | |
669 | | namespace { |
670 | | |
671 | | /// A scope in which we're performing pack deduction. |
672 | | class PackDeductionScope { |
673 | | public: |
674 | | /// Prepare to deduce the packs named within Pattern. |
675 | | PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, |
676 | | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
677 | | TemplateDeductionInfo &Info, TemplateArgument Pattern) |
678 | 45.5k | : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { |
679 | 45.5k | unsigned NumNamedPacks = addPacks(Pattern); |
680 | 45.5k | finishConstruction(NumNamedPacks); |
681 | 45.5k | } |
682 | | |
683 | | /// Prepare to directly deduce arguments of the parameter with index \p Index. |
684 | | PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, |
685 | | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
686 | | TemplateDeductionInfo &Info, unsigned Index) |
687 | 25.2k | : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { |
688 | 25.2k | addPack(Index); |
689 | 25.2k | finishConstruction(1); |
690 | 25.2k | } |
691 | | |
692 | | private: |
693 | 70.8k | void addPack(unsigned Index) { |
694 | | // Save the deduced template argument for the parameter pack expanded |
695 | | // by this pack expansion, then clear out the deduction. |
696 | 70.8k | DeducedPack Pack(Index); |
697 | 70.8k | Pack.Saved = Deduced[Index]; |
698 | 70.8k | Deduced[Index] = TemplateArgument(); |
699 | | |
700 | | // FIXME: What if we encounter multiple packs with different numbers of |
701 | | // pre-expanded expansions? (This should already have been diagnosed |
702 | | // during substitution.) |
703 | 70.8k | if (Optional<unsigned> ExpandedPackExpansions = |
704 | 70.8k | getExpandedPackSize(TemplateParams->getParam(Index))) |
705 | 27 | FixedNumExpansions = ExpandedPackExpansions; |
706 | | |
707 | 70.8k | Packs.push_back(Pack); |
708 | 70.8k | } |
709 | | |
710 | 45.5k | unsigned addPacks(TemplateArgument Pattern) { |
711 | | // Compute the set of template parameter indices that correspond to |
712 | | // parameter packs expanded by the pack expansion. |
713 | 45.5k | llvm::SmallBitVector SawIndices(TemplateParams->size()); |
714 | 45.5k | llvm::SmallVector<TemplateArgument, 4> ExtraDeductions; |
715 | | |
716 | 45.6k | auto AddPack = [&](unsigned Index) { |
717 | 45.6k | if (SawIndices[Index]) |
718 | 7 | return; |
719 | 45.6k | SawIndices[Index] = true; |
720 | 45.6k | addPack(Index); |
721 | | |
722 | | // Deducing a parameter pack that is a pack expansion also constrains the |
723 | | // packs appearing in that parameter to have the same deduced arity. Also, |
724 | | // in C++17 onwards, deducing a non-type template parameter deduces its |
725 | | // type, so we need to collect the pending deduced values for those packs. |
726 | 45.6k | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>( |
727 | 45.6k | TemplateParams->getParam(Index))) { |
728 | 3.11k | if (!NTTP->isExpandedParameterPack()) |
729 | 3.11k | if (auto *Expansion = dyn_cast<PackExpansionType>(NTTP->getType())) |
730 | 21 | ExtraDeductions.push_back(Expansion->getPattern()); |
731 | 3.11k | } |
732 | | // FIXME: Also collect the unexpanded packs in any type and template |
733 | | // parameter packs that are pack expansions. |
734 | 45.6k | }; |
735 | | |
736 | 45.5k | auto Collect = [&](TemplateArgument Pattern) { |
737 | 45.5k | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
738 | 45.5k | S.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
739 | 91.2k | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I45.6k ) { |
740 | 45.6k | unsigned Depth, Index; |
741 | 45.6k | std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
742 | 45.6k | if (Depth == Info.getDeducedDepth()) |
743 | 45.6k | AddPack(Index); |
744 | 45.6k | } |
745 | 45.5k | }; |
746 | | |
747 | | // Look for unexpanded packs in the pattern. |
748 | 45.5k | Collect(Pattern); |
749 | 45.5k | assert(!Packs.empty() && "Pack expansion without unexpanded packs?"); |
750 | | |
751 | 0 | unsigned NumNamedPacks = Packs.size(); |
752 | | |
753 | | // Also look for unexpanded packs that are indirectly deduced by deducing |
754 | | // the sizes of the packs in this pattern. |
755 | 45.5k | while (!ExtraDeductions.empty()) |
756 | 21 | Collect(ExtraDeductions.pop_back_val()); |
757 | | |
758 | 45.5k | return NumNamedPacks; |
759 | 45.5k | } |
760 | | |
761 | 70.8k | void finishConstruction(unsigned NumNamedPacks) { |
762 | | // Dig out the partially-substituted pack, if there is one. |
763 | 70.8k | const TemplateArgument *PartialPackArgs = nullptr; |
764 | 70.8k | unsigned NumPartialPackArgs = 0; |
765 | 70.8k | std::pair<unsigned, unsigned> PartialPackDepthIndex(-1u, -1u); |
766 | 70.8k | if (auto *Scope = S.CurrentInstantiationScope) |
767 | 60.6k | if (auto *Partial = Scope->getPartiallySubstitutedPack( |
768 | 60.6k | &PartialPackArgs, &NumPartialPackArgs)) |
769 | 25.5k | PartialPackDepthIndex = getDepthAndIndex(Partial); |
770 | | |
771 | | // This pack expansion will have been partially or fully expanded if |
772 | | // it only names explicitly-specified parameter packs (including the |
773 | | // partially-substituted one, if any). |
774 | 70.8k | bool IsExpanded = true; |
775 | 96.2k | for (unsigned I = 0; I != NumNamedPacks; ++I25.4k ) { |
776 | 70.8k | if (Packs[I].Index >= Info.getNumExplicitArgs()) { |
777 | 45.3k | IsExpanded = false; |
778 | 45.3k | IsPartiallyExpanded = false; |
779 | 45.3k | break; |
780 | 45.3k | } |
781 | 25.4k | if (PartialPackDepthIndex == |
782 | 25.4k | std::make_pair(Info.getDeducedDepth(), Packs[I].Index)) { |
783 | 25.4k | IsPartiallyExpanded = true; |
784 | 25.4k | } |
785 | 25.4k | } |
786 | | |
787 | | // Skip over the pack elements that were expanded into separate arguments. |
788 | | // If we partially expanded, this is the number of partial arguments. |
789 | 70.8k | if (IsPartiallyExpanded) |
790 | 25.4k | PackElements += NumPartialPackArgs; |
791 | 45.3k | else if (IsExpanded) |
792 | 0 | PackElements += *FixedNumExpansions; |
793 | | |
794 | 70.8k | for (auto &Pack : Packs) { |
795 | 70.8k | if (Info.PendingDeducedPacks.size() > Pack.Index) |
796 | 632 | Pack.Outer = Info.PendingDeducedPacks[Pack.Index]; |
797 | 70.2k | else |
798 | 70.2k | Info.PendingDeducedPacks.resize(Pack.Index + 1); |
799 | 70.8k | Info.PendingDeducedPacks[Pack.Index] = &Pack; |
800 | | |
801 | 70.8k | if (PartialPackDepthIndex == |
802 | 70.8k | std::make_pair(Info.getDeducedDepth(), Pack.Index)) { |
803 | 25.4k | Pack.New.append(PartialPackArgs, PartialPackArgs + NumPartialPackArgs); |
804 | | // We pre-populate the deduced value of the partially-substituted |
805 | | // pack with the specified value. This is not entirely correct: the |
806 | | // value is supposed to have been substituted, not deduced, but the |
807 | | // cases where this is observable require an exact type match anyway. |
808 | | // |
809 | | // FIXME: If we could represent a "depth i, index j, pack elem k" |
810 | | // parameter, we could substitute the partially-substituted pack |
811 | | // everywhere and avoid this. |
812 | 25.4k | if (!IsPartiallyExpanded) |
813 | 0 | Deduced[Pack.Index] = Pack.New[PackElements]; |
814 | 25.4k | } |
815 | 70.8k | } |
816 | 70.8k | } |
817 | | |
818 | | public: |
819 | 70.8k | ~PackDeductionScope() { |
820 | 70.8k | for (auto &Pack : Packs) |
821 | 70.8k | Info.PendingDeducedPacks[Pack.Index] = Pack.Outer; |
822 | 70.8k | } |
823 | | |
824 | | /// Determine whether this pack has already been partially expanded into a |
825 | | /// sequence of (prior) function parameters / template arguments. |
826 | 12 | bool isPartiallyExpanded() { return IsPartiallyExpanded; } |
827 | | |
828 | | /// Determine whether this pack expansion scope has a known, fixed arity. |
829 | | /// This happens if it involves a pack from an outer template that has |
830 | | /// (notionally) already been expanded. |
831 | 173 | bool hasFixedArity() { return FixedNumExpansions.has_value(); } |
832 | | |
833 | | /// Determine whether the next element of the argument is still part of this |
834 | | /// pack. This is the case unless the pack is already expanded to a fixed |
835 | | /// length. |
836 | 142k | bool hasNextElement() { |
837 | 142k | return !FixedNumExpansions || *FixedNumExpansions > PackElements34 ; |
838 | 142k | } |
839 | | |
840 | | /// Move to deducing the next element in each pack that is being deduced. |
841 | 142k | void nextPackElement() { |
842 | | // Capture the deduced template arguments for each parameter pack expanded |
843 | | // by this pack expansion, add them to the list of arguments we've deduced |
844 | | // for that pack, then clear out the deduced argument. |
845 | 142k | for (auto &Pack : Packs) { |
846 | 142k | DeducedTemplateArgument &DeducedArg = Deduced[Pack.Index]; |
847 | 142k | if (!Pack.New.empty() || !DeducedArg.isNull()34.7k ) { |
848 | 142k | while (Pack.New.size() < PackElements) |
849 | 44 | Pack.New.push_back(DeducedTemplateArgument()); |
850 | 142k | if (Pack.New.size() == PackElements) |
851 | 142k | Pack.New.push_back(DeducedArg); |
852 | 0 | else |
853 | 0 | Pack.New[PackElements] = DeducedArg; |
854 | 142k | DeducedArg = Pack.New.size() > PackElements + 1 |
855 | 142k | ? Pack.New[PackElements + 1]0 |
856 | 142k | : DeducedTemplateArgument(); |
857 | 142k | } |
858 | 142k | } |
859 | 142k | ++PackElements; |
860 | 142k | } |
861 | | |
862 | | /// Finish template argument deduction for a set of argument packs, |
863 | | /// producing the argument packs and checking for consistency with prior |
864 | | /// deductions. |
865 | 70.8k | Sema::TemplateDeductionResult finish() { |
866 | | // Build argument packs for each of the parameter packs expanded by this |
867 | | // pack expansion. |
868 | 70.8k | for (auto &Pack : Packs) { |
869 | | // Put back the old value for this pack. |
870 | 70.8k | Deduced[Pack.Index] = Pack.Saved; |
871 | | |
872 | | // Always make sure the size of this pack is correct, even if we didn't |
873 | | // deduce any values for it. |
874 | | // |
875 | | // FIXME: This isn't required by the normative wording, but substitution |
876 | | // and post-substitution checking will always fail if the arity of any |
877 | | // pack is not equal to the number of elements we processed. (Either that |
878 | | // or something else has gone *very* wrong.) We're permitted to skip any |
879 | | // hard errors from those follow-on steps by the intent (but not the |
880 | | // wording) of C++ [temp.inst]p8: |
881 | | // |
882 | | // If the function selected by overload resolution can be determined |
883 | | // without instantiating a class template definition, it is unspecified |
884 | | // whether that instantiation actually takes place |
885 | 70.8k | Pack.New.resize(PackElements); |
886 | | |
887 | | // Build or find a new value for this pack. |
888 | 70.8k | DeducedTemplateArgument NewPack; |
889 | 70.8k | if (Pack.New.empty()) { |
890 | | // If we deduced an empty argument pack, create it now. |
891 | 10.7k | NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack()); |
892 | 60.1k | } else { |
893 | 60.1k | TemplateArgument *ArgumentPack = |
894 | 60.1k | new (S.Context) TemplateArgument[Pack.New.size()]; |
895 | 60.1k | std::copy(Pack.New.begin(), Pack.New.end(), ArgumentPack); |
896 | 60.1k | NewPack = DeducedTemplateArgument( |
897 | 60.1k | TemplateArgument(llvm::makeArrayRef(ArgumentPack, Pack.New.size())), |
898 | | // FIXME: This is wrong, it's possible that some pack elements are |
899 | | // deduced from an array bound and others are not: |
900 | | // template<typename ...T, T ...V> void g(const T (&...p)[V]); |
901 | | // g({1, 2, 3}, {{}, {}}); |
902 | | // ... should deduce T = {int, size_t (from array bound)}. |
903 | 60.1k | Pack.New[0].wasDeducedFromArrayBound()); |
904 | 60.1k | } |
905 | | |
906 | | // Pick where we're going to put the merged pack. |
907 | 70.8k | DeducedTemplateArgument *Loc; |
908 | 70.8k | if (Pack.Outer) { |
909 | 17 | if (Pack.Outer->DeferredDeduction.isNull()) { |
910 | | // Defer checking this pack until we have a complete pack to compare |
911 | | // it against. |
912 | 8 | Pack.Outer->DeferredDeduction = NewPack; |
913 | 8 | continue; |
914 | 8 | } |
915 | 9 | Loc = &Pack.Outer->DeferredDeduction; |
916 | 70.8k | } else { |
917 | 70.8k | Loc = &Deduced[Pack.Index]; |
918 | 70.8k | } |
919 | | |
920 | | // Check the new pack matches any previous value. |
921 | 70.8k | DeducedTemplateArgument OldPack = *Loc; |
922 | 70.8k | DeducedTemplateArgument Result = |
923 | 70.8k | checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
924 | | |
925 | | // If we deferred a deduction of this pack, check that one now too. |
926 | 70.8k | if (!Result.isNull() && !Pack.DeferredDeduction.isNull()70.7k ) { |
927 | 5 | OldPack = Result; |
928 | 5 | NewPack = Pack.DeferredDeduction; |
929 | 5 | Result = checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
930 | 5 | } |
931 | | |
932 | 70.8k | NamedDecl *Param = TemplateParams->getParam(Pack.Index); |
933 | 70.8k | if (Result.isNull()) { |
934 | 55 | Info.Param = makeTemplateParameter(Param); |
935 | 55 | Info.FirstArg = OldPack; |
936 | 55 | Info.SecondArg = NewPack; |
937 | 55 | return Sema::TDK_Inconsistent; |
938 | 55 | } |
939 | | |
940 | | // If we have a pre-expanded pack and we didn't deduce enough elements |
941 | | // for it, fail deduction. |
942 | 70.7k | if (Optional<unsigned> Expansions = getExpandedPackSize(Param)) { |
943 | 27 | if (*Expansions != PackElements) { |
944 | 9 | Info.Param = makeTemplateParameter(Param); |
945 | 9 | Info.FirstArg = Result; |
946 | 9 | return Sema::TDK_IncompletePack; |
947 | 9 | } |
948 | 27 | } |
949 | | |
950 | 70.7k | *Loc = Result; |
951 | 70.7k | } |
952 | | |
953 | 70.7k | return Sema::TDK_Success; |
954 | 70.8k | } |
955 | | |
956 | | private: |
957 | | Sema &S; |
958 | | TemplateParameterList *TemplateParams; |
959 | | SmallVectorImpl<DeducedTemplateArgument> &Deduced; |
960 | | TemplateDeductionInfo &Info; |
961 | | unsigned PackElements = 0; |
962 | | bool IsPartiallyExpanded = false; |
963 | | /// The number of expansions, if we have a fully-expanded pack in this scope. |
964 | | Optional<unsigned> FixedNumExpansions; |
965 | | |
966 | | SmallVector<DeducedPack, 2> Packs; |
967 | | }; |
968 | | |
969 | | } // namespace |
970 | | |
971 | | /// Deduce the template arguments by comparing the list of parameter |
972 | | /// types to the list of argument types, as in the parameter-type-lists of |
973 | | /// function types (C++ [temp.deduct.type]p10). |
974 | | /// |
975 | | /// \param S The semantic analysis object within which we are deducing |
976 | | /// |
977 | | /// \param TemplateParams The template parameters that we are deducing |
978 | | /// |
979 | | /// \param Params The list of parameter types |
980 | | /// |
981 | | /// \param NumParams The number of types in \c Params |
982 | | /// |
983 | | /// \param Args The list of argument types |
984 | | /// |
985 | | /// \param NumArgs The number of types in \c Args |
986 | | /// |
987 | | /// \param Info information about the template argument deduction itself |
988 | | /// |
989 | | /// \param Deduced the deduced template arguments |
990 | | /// |
991 | | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
992 | | /// how template argument deduction is performed. |
993 | | /// |
994 | | /// \param PartialOrdering If true, we are performing template argument |
995 | | /// deduction for during partial ordering for a call |
996 | | /// (C++0x [temp.deduct.partial]). |
997 | | /// |
998 | | /// \returns the result of template argument deduction so far. Note that a |
999 | | /// "success" result means that template argument deduction has not yet failed, |
1000 | | /// but it may still fail, later, for other reasons. |
1001 | | static Sema::TemplateDeductionResult |
1002 | | DeduceTemplateArguments(Sema &S, |
1003 | | TemplateParameterList *TemplateParams, |
1004 | | const QualType *Params, unsigned NumParams, |
1005 | | const QualType *Args, unsigned NumArgs, |
1006 | | TemplateDeductionInfo &Info, |
1007 | | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
1008 | | unsigned TDF, |
1009 | 111k | bool PartialOrdering = false) { |
1010 | | // C++0x [temp.deduct.type]p10: |
1011 | | // Similarly, if P has a form that contains (T), then each parameter type |
1012 | | // Pi of the respective parameter-type- list of P is compared with the |
1013 | | // corresponding parameter type Ai of the corresponding parameter-type-list |
1014 | | // of A. [...] |
1015 | 111k | unsigned ArgIdx = 0, ParamIdx = 0; |
1016 | 214k | for (; ParamIdx != NumParams; ++ParamIdx102k ) { |
1017 | | // Check argument types. |
1018 | 186k | const PackExpansionType *Expansion |
1019 | 186k | = dyn_cast<PackExpansionType>(Params[ParamIdx]); |
1020 | 186k | if (!Expansion) { |
1021 | | // Simple case: compare the parameter and argument types at this point. |
1022 | | |
1023 | | // Make sure we have an argument. |
1024 | 185k | if (ArgIdx >= NumArgs) |
1025 | 6.60k | return Sema::TDK_MiscellaneousDeductionFailure; |
1026 | | |
1027 | 178k | if (isa<PackExpansionType>(Args[ArgIdx])) { |
1028 | | // C++0x [temp.deduct.type]p22: |
1029 | | // If the original function parameter associated with A is a function |
1030 | | // parameter pack and the function parameter associated with P is not |
1031 | | // a function parameter pack, then template argument deduction fails. |
1032 | 448 | return Sema::TDK_MiscellaneousDeductionFailure; |
1033 | 448 | } |
1034 | | |
1035 | 178k | if (Sema::TemplateDeductionResult Result = |
1036 | 178k | DeduceTemplateArgumentsByTypeMatch( |
1037 | 178k | S, TemplateParams, Params[ParamIdx].getUnqualifiedType(), |
1038 | 178k | Args[ArgIdx].getUnqualifiedType(), Info, Deduced, TDF, |
1039 | 178k | PartialOrdering, |
1040 | 178k | /*DeducedFromArrayBound=*/false)) |
1041 | 76.6k | return Result; |
1042 | | |
1043 | 101k | ++ArgIdx; |
1044 | 101k | continue; |
1045 | 178k | } |
1046 | | |
1047 | | // C++0x [temp.deduct.type]p10: |
1048 | | // If the parameter-declaration corresponding to Pi is a function |
1049 | | // parameter pack, then the type of its declarator- id is compared with |
1050 | | // each remaining parameter type in the parameter-type-list of A. Each |
1051 | | // comparison deduces template arguments for subsequent positions in the |
1052 | | // template parameter packs expanded by the function parameter pack. |
1053 | | |
1054 | 1.01k | QualType Pattern = Expansion->getPattern(); |
1055 | 1.01k | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
1056 | | |
1057 | | // A pack scope with fixed arity is not really a pack any more, so is not |
1058 | | // a non-deduced context. |
1059 | 1.01k | if (ParamIdx + 1 == NumParams || PackScope.hasFixedArity()9 ) { |
1060 | 2.68k | for (; ArgIdx < NumArgs && PackScope.hasNextElement()1.68k ; ++ArgIdx1.67k ) { |
1061 | | // Deduce template arguments from the pattern. |
1062 | 1.68k | if (Sema::TemplateDeductionResult Result = |
1063 | 1.68k | DeduceTemplateArgumentsByTypeMatch( |
1064 | 1.68k | S, TemplateParams, Pattern.getUnqualifiedType(), |
1065 | 1.68k | Args[ArgIdx].getUnqualifiedType(), Info, Deduced, TDF, |
1066 | 1.68k | PartialOrdering, /*DeducedFromArrayBound=*/false)) |
1067 | 5 | return Result; |
1068 | | |
1069 | 1.67k | PackScope.nextPackElement(); |
1070 | 1.67k | } |
1071 | 1.00k | } else { |
1072 | | // C++0x [temp.deduct.type]p5: |
1073 | | // The non-deduced contexts are: |
1074 | | // - A function parameter pack that does not occur at the end of the |
1075 | | // parameter-declaration-clause. |
1076 | | // |
1077 | | // FIXME: There is no wording to say what we should do in this case. We |
1078 | | // choose to resolve this by applying the same rule that is applied for a |
1079 | | // function call: that is, deduce all contained packs to their |
1080 | | // explicitly-specified values (or to <> if there is no such value). |
1081 | | // |
1082 | | // This is seemingly-arbitrarily different from the case of a template-id |
1083 | | // with a non-trailing pack-expansion in its arguments, which renders the |
1084 | | // entire template-argument-list a non-deduced context. |
1085 | | |
1086 | | // If the parameter type contains an explicitly-specified pack that we |
1087 | | // could not expand, skip the number of parameters notionally created |
1088 | | // by the expansion. |
1089 | 9 | Optional<unsigned> NumExpansions = Expansion->getNumExpansions(); |
1090 | 9 | if (NumExpansions && !PackScope.isPartiallyExpanded()0 ) { |
1091 | 0 | for (unsigned I = 0; I != *NumExpansions && ArgIdx < NumArgs; |
1092 | 0 | ++I, ++ArgIdx) |
1093 | 0 | PackScope.nextPackElement(); |
1094 | 0 | } |
1095 | 9 | } |
1096 | | |
1097 | | // Build argument packs for each of the parameter packs expanded by this |
1098 | | // pack expansion. |
1099 | 1.00k | if (auto Result = PackScope.finish()) |
1100 | 2 | return Result; |
1101 | 1.00k | } |
1102 | | |
1103 | | // Make sure we don't have any extra arguments. |
1104 | 28.2k | if (ArgIdx < NumArgs) |
1105 | 4.60k | return Sema::TDK_MiscellaneousDeductionFailure; |
1106 | | |
1107 | 23.6k | return Sema::TDK_Success; |
1108 | 28.2k | } |
1109 | | |
1110 | | /// Determine whether the parameter has qualifiers that the argument |
1111 | | /// lacks. Put another way, determine whether there is no way to add |
1112 | | /// a deduced set of qualifiers to the ParamType that would result in |
1113 | | /// its qualifiers matching those of the ArgType. |
1114 | | static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType, |
1115 | 2.41M | QualType ArgType) { |
1116 | 2.41M | Qualifiers ParamQs = ParamType.getQualifiers(); |
1117 | 2.41M | Qualifiers ArgQs = ArgType.getQualifiers(); |
1118 | | |
1119 | 2.41M | if (ParamQs == ArgQs) |
1120 | 2.27M | return false; |
1121 | | |
1122 | | // Mismatched (but not missing) Objective-C GC attributes. |
1123 | 143k | if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() && |
1124 | 143k | ParamQs.hasObjCGCAttr()0 ) |
1125 | 0 | return true; |
1126 | | |
1127 | | // Mismatched (but not missing) address spaces. |
1128 | 143k | if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() && |
1129 | 143k | ParamQs.hasAddressSpace()232 ) |
1130 | 145 | return true; |
1131 | | |
1132 | | // Mismatched (but not missing) Objective-C lifetime qualifiers. |
1133 | 143k | if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() && |
1134 | 143k | ParamQs.hasObjCLifetime()139 ) |
1135 | 85 | return true; |
1136 | | |
1137 | | // CVR qualifiers inconsistent or a superset. |
1138 | 142k | return (ParamQs.getCVRQualifiers() & ~ArgQs.getCVRQualifiers()) != 0; |
1139 | 143k | } |
1140 | | |
1141 | | /// Compare types for equality with respect to possibly compatible |
1142 | | /// function types (noreturn adjustment, implicit calling conventions). If any |
1143 | | /// of parameter and argument is not a function, just perform type comparison. |
1144 | | /// |
1145 | | /// \param P the template parameter type. |
1146 | | /// |
1147 | | /// \param A the argument type. |
1148 | 18.6k | bool Sema::isSameOrCompatibleFunctionType(QualType P, QualType A) { |
1149 | 18.6k | const FunctionType *PF = P->getAs<FunctionType>(), |
1150 | 18.6k | *AF = A->getAs<FunctionType>(); |
1151 | | |
1152 | | // Just compare if not functions. |
1153 | 18.6k | if (!PF || !AF) |
1154 | 0 | return Context.hasSameType(P, A); |
1155 | | |
1156 | | // Noreturn and noexcept adjustment. |
1157 | 18.6k | QualType AdjustedParam; |
1158 | 18.6k | if (IsFunctionConversion(P, A, AdjustedParam)) |
1159 | 270 | return Context.hasSameType(AdjustedParam, A); |
1160 | | |
1161 | | // FIXME: Compatible calling conventions. |
1162 | | |
1163 | 18.3k | return Context.hasSameType(P, A); |
1164 | 18.6k | } |
1165 | | |
1166 | | /// Get the index of the first template parameter that was originally from the |
1167 | | /// innermost template-parameter-list. This is 0 except when we concatenate |
1168 | | /// the template parameter lists of a class template and a constructor template |
1169 | | /// when forming an implicit deduction guide. |
1170 | 1.74M | static unsigned getFirstInnerIndex(FunctionTemplateDecl *FTD) { |
1171 | 1.74M | auto *Guide = dyn_cast<CXXDeductionGuideDecl>(FTD->getTemplatedDecl()); |
1172 | 1.74M | if (!Guide || !Guide->isImplicit()1.44k ) |
1173 | 1.74M | return 0; |
1174 | 1.20k | return Guide->getDeducedTemplate()->getTemplateParameters()->size(); |
1175 | 1.74M | } |
1176 | | |
1177 | | /// Determine whether a type denotes a forwarding reference. |
1178 | 1.13M | static bool isForwardingReference(QualType Param, unsigned FirstInnerIndex) { |
1179 | | // C++1z [temp.deduct.call]p3: |
1180 | | // A forwarding reference is an rvalue reference to a cv-unqualified |
1181 | | // template parameter that does not represent a template parameter of a |
1182 | | // class template. |
1183 | 1.13M | if (auto *ParamRef = Param->getAs<RValueReferenceType>()) { |
1184 | 138k | if (ParamRef->getPointeeType().getQualifiers()) |
1185 | 4.12k | return false; |
1186 | 134k | auto *TypeParm = ParamRef->getPointeeType()->getAs<TemplateTypeParmType>(); |
1187 | 134k | return TypeParm && TypeParm->getIndex() >= FirstInnerIndex100k ; |
1188 | 138k | } |
1189 | 996k | return false; |
1190 | 1.13M | } |
1191 | | |
1192 | 308k | static CXXRecordDecl *getCanonicalRD(QualType T) { |
1193 | 308k | return cast<CXXRecordDecl>( |
1194 | 308k | T->castAs<RecordType>()->getDecl()->getCanonicalDecl()); |
1195 | 308k | } |
1196 | | |
1197 | | /// Attempt to deduce the template arguments by checking the base types |
1198 | | /// according to (C++20 [temp.deduct.call] p4b3. |
1199 | | /// |
1200 | | /// \param S the semantic analysis object within which we are deducing. |
1201 | | /// |
1202 | | /// \param RD the top level record object we are deducing against. |
1203 | | /// |
1204 | | /// \param TemplateParams the template parameters that we are deducing. |
1205 | | /// |
1206 | | /// \param P the template specialization parameter type. |
1207 | | /// |
1208 | | /// \param Info information about the template argument deduction itself. |
1209 | | /// |
1210 | | /// \param Deduced the deduced template arguments. |
1211 | | /// |
1212 | | /// \returns the result of template argument deduction with the bases. "invalid" |
1213 | | /// means no matches, "success" found a single item, and the |
1214 | | /// "MiscellaneousDeductionFailure" result happens when the match is ambiguous. |
1215 | | static Sema::TemplateDeductionResult |
1216 | | DeduceTemplateBases(Sema &S, const CXXRecordDecl *RD, |
1217 | | TemplateParameterList *TemplateParams, QualType P, |
1218 | | TemplateDeductionInfo &Info, |
1219 | 267k | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
1220 | | // C++14 [temp.deduct.call] p4b3: |
1221 | | // If P is a class and P has the form simple-template-id, then the |
1222 | | // transformed A can be a derived class of the deduced A. Likewise if |
1223 | | // P is a pointer to a class of the form simple-template-id, the |
1224 | | // transformed A can be a pointer to a derived class pointed to by the |
1225 | | // deduced A. However, if there is a class C that is a (direct or |
1226 | | // indirect) base class of D and derived (directly or indirectly) from a |
1227 | | // class B and that would be a valid deduced A, the deduced A cannot be |
1228 | | // B or pointer to B, respectively. |
1229 | | // |
1230 | | // These alternatives are considered only if type deduction would |
1231 | | // otherwise fail. If they yield more than one possible deduced A, the |
1232 | | // type deduction fails. |
1233 | | |
1234 | | // Use a breadth-first search through the bases to collect the set of |
1235 | | // successful matches. Visited contains the set of nodes we have already |
1236 | | // visited, while ToVisit is our stack of records that we still need to |
1237 | | // visit. Matches contains a list of matches that have yet to be |
1238 | | // disqualified. |
1239 | 267k | llvm::SmallPtrSet<const CXXRecordDecl *, 8> Visited; |
1240 | 267k | SmallVector<QualType, 8> ToVisit; |
1241 | | // We iterate over this later, so we have to use MapVector to ensure |
1242 | | // determinism. |
1243 | 267k | llvm::MapVector<const CXXRecordDecl *, |
1244 | 267k | SmallVector<DeducedTemplateArgument, 8>> |
1245 | 267k | Matches; |
1246 | | |
1247 | 279k | auto AddBases = [&Visited, &ToVisit](const CXXRecordDecl *RD) { |
1248 | 279k | for (const auto &Base : RD->bases()) { |
1249 | 20.2k | QualType T = Base.getType(); |
1250 | 20.2k | assert(T->isRecordType() && "Base class that isn't a record?"); |
1251 | 20.2k | if (Visited.insert(::getCanonicalRD(T)).second) |
1252 | 20.2k | ToVisit.push_back(T); |
1253 | 20.2k | } |
1254 | 279k | }; |
1255 | | |
1256 | | // Set up the loop by adding all the bases. |
1257 | 267k | AddBases(RD); |
1258 | | |
1259 | | // Search each path of bases until we either run into a successful match |
1260 | | // (where all bases of it are invalid), or we run out of bases. |
1261 | 288k | while (!ToVisit.empty()) { |
1262 | 20.2k | QualType NextT = ToVisit.pop_back_val(); |
1263 | | |
1264 | 20.2k | SmallVector<DeducedTemplateArgument, 8> DeducedCopy(Deduced.begin(), |
1265 | 20.2k | Deduced.end()); |
1266 | 20.2k | TemplateDeductionInfo BaseInfo(TemplateDeductionInfo::ForBase, Info); |
1267 | 20.2k | Sema::TemplateDeductionResult BaseResult = DeduceTemplateSpecArguments( |
1268 | 20.2k | S, TemplateParams, P, NextT, BaseInfo, DeducedCopy); |
1269 | | |
1270 | | // If this was a successful deduction, add it to the list of matches, |
1271 | | // otherwise we need to continue searching its bases. |
1272 | 20.2k | const CXXRecordDecl *RD = ::getCanonicalRD(NextT); |
1273 | 20.2k | if (BaseResult == Sema::TDK_Success) |
1274 | 8.86k | Matches.insert({RD, DeducedCopy}); |
1275 | 11.3k | else |
1276 | 11.3k | AddBases(RD); |
1277 | 20.2k | } |
1278 | | |
1279 | | // At this point, 'Matches' contains a list of seemingly valid bases, however |
1280 | | // in the event that we have more than 1 match, it is possible that the base |
1281 | | // of one of the matches might be disqualified for being a base of another |
1282 | | // valid match. We can count on cyclical instantiations being invalid to |
1283 | | // simplify the disqualifications. That is, if A & B are both matches, and B |
1284 | | // inherits from A (disqualifying A), we know that A cannot inherit from B. |
1285 | 267k | if (Matches.size() > 1) { |
1286 | 10 | Visited.clear(); |
1287 | 10 | for (const auto &Match : Matches) |
1288 | 20 | AddBases(Match.first); |
1289 | | |
1290 | | // We can give up once we have a single item (or have run out of things to |
1291 | | // search) since cyclical inheritance isn't valid. |
1292 | 22 | while (Matches.size() > 1 && !ToVisit.empty()14 ) { |
1293 | 12 | const CXXRecordDecl *RD = ::getCanonicalRD(ToVisit.pop_back_val()); |
1294 | 12 | Matches.erase(RD); |
1295 | | |
1296 | | // Always add all bases, since the inheritance tree can contain |
1297 | | // disqualifications for multiple matches. |
1298 | 12 | AddBases(RD); |
1299 | 12 | } |
1300 | 10 | } |
1301 | | |
1302 | 267k | if (Matches.empty()) |
1303 | 259k | return Sema::TDK_Invalid; |
1304 | 8.85k | if (Matches.size() > 1) |
1305 | 2 | return Sema::TDK_MiscellaneousDeductionFailure; |
1306 | | |
1307 | 8.85k | std::swap(Matches.front().second, Deduced); |
1308 | 8.85k | return Sema::TDK_Success; |
1309 | 8.85k | } |
1310 | | |
1311 | | /// Deduce the template arguments by comparing the parameter type and |
1312 | | /// the argument type (C++ [temp.deduct.type]). |
1313 | | /// |
1314 | | /// \param S the semantic analysis object within which we are deducing |
1315 | | /// |
1316 | | /// \param TemplateParams the template parameters that we are deducing |
1317 | | /// |
1318 | | /// \param P the parameter type |
1319 | | /// |
1320 | | /// \param A the argument type |
1321 | | /// |
1322 | | /// \param Info information about the template argument deduction itself |
1323 | | /// |
1324 | | /// \param Deduced the deduced template arguments |
1325 | | /// |
1326 | | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
1327 | | /// how template argument deduction is performed. |
1328 | | /// |
1329 | | /// \param PartialOrdering Whether we're performing template argument deduction |
1330 | | /// in the context of partial ordering (C++0x [temp.deduct.partial]). |
1331 | | /// |
1332 | | /// \returns the result of template argument deduction so far. Note that a |
1333 | | /// "success" result means that template argument deduction has not yet failed, |
1334 | | /// but it may still fail, later, for other reasons. |
1335 | | static Sema::TemplateDeductionResult DeduceTemplateArgumentsByTypeMatch( |
1336 | | Sema &S, TemplateParameterList *TemplateParams, QualType P, QualType A, |
1337 | | TemplateDeductionInfo &Info, |
1338 | | SmallVectorImpl<DeducedTemplateArgument> &Deduced, unsigned TDF, |
1339 | 4.01M | bool PartialOrdering, bool DeducedFromArrayBound) { |
1340 | | |
1341 | | // If the argument type is a pack expansion, look at its pattern. |
1342 | | // This isn't explicitly called out |
1343 | 4.01M | if (const auto *AExp = dyn_cast<PackExpansionType>(A)) |
1344 | 48 | A = AExp->getPattern(); |
1345 | 4.01M | assert(!isa<PackExpansionType>(A.getCanonicalType())); |
1346 | | |
1347 | 4.01M | if (PartialOrdering) { |
1348 | | // C++11 [temp.deduct.partial]p5: |
1349 | | // Before the partial ordering is done, certain transformations are |
1350 | | // performed on the types used for partial ordering: |
1351 | | // - If P is a reference type, P is replaced by the type referred to. |
1352 | 432k | const ReferenceType *PRef = P->getAs<ReferenceType>(); |
1353 | 432k | if (PRef) |
1354 | 10.0k | P = PRef->getPointeeType(); |
1355 | | |
1356 | | // - If A is a reference type, A is replaced by the type referred to. |
1357 | 432k | const ReferenceType *ARef = A->getAs<ReferenceType>(); |
1358 | 432k | if (ARef) |
1359 | 10.0k | A = A->getPointeeType(); |
1360 | | |
1361 | 432k | if (PRef && ARef10.0k && S.Context.hasSameUnqualifiedType(P, A)9.49k ) { |
1362 | | // C++11 [temp.deduct.partial]p9: |
1363 | | // If, for a given type, deduction succeeds in both directions (i.e., |
1364 | | // the types are identical after the transformations above) and both |
1365 | | // P and A were reference types [...]: |
1366 | | // - if [one type] was an lvalue reference and [the other type] was |
1367 | | // not, [the other type] is not considered to be at least as |
1368 | | // specialized as [the first type] |
1369 | | // - if [one type] is more cv-qualified than [the other type], |
1370 | | // [the other type] is not considered to be at least as specialized |
1371 | | // as [the first type] |
1372 | | // Objective-C ARC adds: |
1373 | | // - [one type] has non-trivial lifetime, [the other type] has |
1374 | | // __unsafe_unretained lifetime, and the types are otherwise |
1375 | | // identical |
1376 | | // |
1377 | | // A is "considered to be at least as specialized" as P iff deduction |
1378 | | // succeeds, so we model this as a deduction failure. Note that |
1379 | | // [the first type] is P and [the other type] is A here; the standard |
1380 | | // gets this backwards. |
1381 | 5.12k | Qualifiers PQuals = P.getQualifiers(), AQuals = A.getQualifiers(); |
1382 | 5.12k | if ((PRef->isLValueReferenceType() && !ARef->isLValueReferenceType()3.66k ) || |
1383 | 5.12k | PQuals.isStrictSupersetOf(AQuals)5.11k || |
1384 | 5.12k | (5.04k PQuals.hasNonTrivialObjCLifetime()5.04k && |
1385 | 5.04k | AQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone4 && |
1386 | 5.04k | PQuals.withoutObjCLifetime() == AQuals.withoutObjCLifetime()4 )) { |
1387 | 84 | Info.FirstArg = TemplateArgument(P); |
1388 | 84 | Info.SecondArg = TemplateArgument(A); |
1389 | 84 | return Sema::TDK_NonDeducedMismatch; |
1390 | 84 | } |
1391 | 5.12k | } |
1392 | 432k | Qualifiers DiscardedQuals; |
1393 | | // C++11 [temp.deduct.partial]p7: |
1394 | | // Remove any top-level cv-qualifiers: |
1395 | | // - If P is a cv-qualified type, P is replaced by the cv-unqualified |
1396 | | // version of P. |
1397 | 432k | P = S.Context.getUnqualifiedArrayType(P, DiscardedQuals); |
1398 | | // - If A is a cv-qualified type, A is replaced by the cv-unqualified |
1399 | | // version of A. |
1400 | 432k | A = S.Context.getUnqualifiedArrayType(A, DiscardedQuals); |
1401 | 3.58M | } else { |
1402 | | // C++0x [temp.deduct.call]p4 bullet 1: |
1403 | | // - If the original P is a reference type, the deduced A (i.e., the type |
1404 | | // referred to by the reference) can be more cv-qualified than the |
1405 | | // transformed A. |
1406 | 3.58M | if (TDF & TDF_ParamWithReferenceType) { |
1407 | 983k | Qualifiers Quals; |
1408 | 983k | QualType UnqualP = S.Context.getUnqualifiedArrayType(P, Quals); |
1409 | 983k | Quals.setCVRQualifiers(Quals.getCVRQualifiers() & A.getCVRQualifiers()); |
1410 | 983k | P = S.Context.getQualifiedType(UnqualP, Quals); |
1411 | 983k | } |
1412 | | |
1413 | 3.58M | if ((TDF & TDF_TopLevelParameterTypeList) && !P->isFunctionType()291k ) { |
1414 | | // C++0x [temp.deduct.type]p10: |
1415 | | // If P and A are function types that originated from deduction when |
1416 | | // taking the address of a function template (14.8.2.2) or when deducing |
1417 | | // template arguments from a function declaration (14.8.2.6) and Pi and |
1418 | | // Ai are parameters of the top-level parameter-type-list of P and A, |
1419 | | // respectively, Pi is adjusted if it is a forwarding reference and Ai |
1420 | | // is an lvalue reference, in |
1421 | | // which case the type of Pi is changed to be the template parameter |
1422 | | // type (i.e., T&& is changed to simply T). [ Note: As a result, when |
1423 | | // Pi is T&& and Ai is X&, the adjusted Pi will be T, causing T to be |
1424 | | // deduced as X&. - end note ] |
1425 | 151k | TDF &= ~TDF_TopLevelParameterTypeList; |
1426 | 151k | if (isForwardingReference(P, /*FirstInnerIndex=*/0) && |
1427 | 151k | A->isLValueReferenceType()3 ) |
1428 | 3 | P = P->getPointeeType(); |
1429 | 151k | } |
1430 | 3.58M | } |
1431 | | |
1432 | | // C++ [temp.deduct.type]p9: |
1433 | | // A template type argument T, a template template argument TT or a |
1434 | | // template non-type argument i can be deduced if P and A have one of |
1435 | | // the following forms: |
1436 | | // |
1437 | | // T |
1438 | | // cv-list T |
1439 | 4.01M | if (const auto *TTP = P->getAs<TemplateTypeParmType>()) { |
1440 | | // Just skip any attempts to deduce from a placeholder type or a parameter |
1441 | | // at a different depth. |
1442 | 1.77M | if (A->isPlaceholderType() || Info.getDeducedDepth() != TTP->getDepth()) |
1443 | 4 | return Sema::TDK_Success; |
1444 | | |
1445 | 1.77M | unsigned Index = TTP->getIndex(); |
1446 | | |
1447 | | // If the argument type is an array type, move the qualifiers up to the |
1448 | | // top level, so they can be matched with the qualifiers on the parameter. |
1449 | 1.77M | if (A->isArrayType()) { |
1450 | 11.1k | Qualifiers Quals; |
1451 | 11.1k | A = S.Context.getUnqualifiedArrayType(A, Quals); |
1452 | 11.1k | if (Quals) |
1453 | 584 | A = S.Context.getQualifiedType(A, Quals); |
1454 | 11.1k | } |
1455 | | |
1456 | | // The argument type can not be less qualified than the parameter |
1457 | | // type. |
1458 | 1.77M | if (!(TDF & TDF_IgnoreQualifiers) && |
1459 | 1.77M | hasInconsistentOrSupersetQualifiersOf(P, A)1.65M ) { |
1460 | 92.1k | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
1461 | 92.1k | Info.FirstArg = TemplateArgument(P); |
1462 | 92.1k | Info.SecondArg = TemplateArgument(A); |
1463 | 92.1k | return Sema::TDK_Underqualified; |
1464 | 92.1k | } |
1465 | | |
1466 | | // Do not match a function type with a cv-qualified type. |
1467 | | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1584 |
1468 | 1.67M | if (A->isFunctionType() && P.hasQualifiers()9.33k ) |
1469 | 8 | return Sema::TDK_NonDeducedMismatch; |
1470 | | |
1471 | 1.67M | assert(TTP->getDepth() == Info.getDeducedDepth() && |
1472 | 1.67M | "saw template type parameter with wrong depth"); |
1473 | 0 | assert(A->getCanonicalTypeInternal() != S.Context.OverloadTy && |
1474 | 1.67M | "Unresolved overloaded function"); |
1475 | 0 | QualType DeducedType = A; |
1476 | | |
1477 | | // Remove any qualifiers on the parameter from the deduced type. |
1478 | | // We checked the qualifiers for consistency above. |
1479 | 1.67M | Qualifiers DeducedQs = DeducedType.getQualifiers(); |
1480 | 1.67M | Qualifiers ParamQs = P.getQualifiers(); |
1481 | 1.67M | DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers()); |
1482 | 1.67M | if (ParamQs.hasObjCGCAttr()) |
1483 | 0 | DeducedQs.removeObjCGCAttr(); |
1484 | 1.67M | if (ParamQs.hasAddressSpace()) |
1485 | 21 | DeducedQs.removeAddressSpace(); |
1486 | 1.67M | if (ParamQs.hasObjCLifetime()) |
1487 | 63 | DeducedQs.removeObjCLifetime(); |
1488 | | |
1489 | | // Objective-C ARC: |
1490 | | // If template deduction would produce a lifetime qualifier on a type |
1491 | | // that is not a lifetime type, template argument deduction fails. |
1492 | 1.67M | if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType()63 && |
1493 | 1.67M | !DeducedType->isDependentType()2 ) { |
1494 | 2 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
1495 | 2 | Info.FirstArg = TemplateArgument(P); |
1496 | 2 | Info.SecondArg = TemplateArgument(A); |
1497 | 2 | return Sema::TDK_Underqualified; |
1498 | 2 | } |
1499 | | |
1500 | | // Objective-C ARC: |
1501 | | // If template deduction would produce an argument type with lifetime type |
1502 | | // but no lifetime qualifier, the __strong lifetime qualifier is inferred. |
1503 | 1.67M | if (S.getLangOpts().ObjCAutoRefCount && DeducedType->isObjCLifetimeType()395 && |
1504 | 1.67M | !DeducedQs.hasObjCLifetime()178 ) |
1505 | 124 | DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong); |
1506 | | |
1507 | 1.67M | DeducedType = |
1508 | 1.67M | S.Context.getQualifiedType(DeducedType.getUnqualifiedType(), DeducedQs); |
1509 | | |
1510 | 1.67M | DeducedTemplateArgument NewDeduced(DeducedType, DeducedFromArrayBound); |
1511 | 1.67M | DeducedTemplateArgument Result = |
1512 | 1.67M | checkDeducedTemplateArguments(S.Context, Deduced[Index], NewDeduced); |
1513 | 1.67M | if (Result.isNull()) { |
1514 | 26.5k | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
1515 | 26.5k | Info.FirstArg = Deduced[Index]; |
1516 | 26.5k | Info.SecondArg = NewDeduced; |
1517 | 26.5k | return Sema::TDK_Inconsistent; |
1518 | 26.5k | } |
1519 | | |
1520 | 1.65M | Deduced[Index] = Result; |
1521 | 1.65M | return Sema::TDK_Success; |
1522 | 1.67M | } |
1523 | | |
1524 | | // Set up the template argument deduction information for a failure. |
1525 | 2.24M | Info.FirstArg = TemplateArgument(P); |
1526 | 2.24M | Info.SecondArg = TemplateArgument(A); |
1527 | | |
1528 | | // If the parameter is an already-substituted template parameter |
1529 | | // pack, do nothing: we don't know which of its arguments to look |
1530 | | // at, so we have to wait until all of the parameter packs in this |
1531 | | // expansion have arguments. |
1532 | 2.24M | if (P->getAs<SubstTemplateTypeParmPackType>()) |
1533 | 46 | return Sema::TDK_Success; |
1534 | | |
1535 | | // Check the cv-qualifiers on the parameter and argument types. |
1536 | 2.24M | if (!(TDF & TDF_IgnoreQualifiers)) { |
1537 | 2.14M | if (TDF & TDF_ParamWithReferenceType) { |
1538 | 760k | if (hasInconsistentOrSupersetQualifiersOf(P, A)) |
1539 | 0 | return Sema::TDK_NonDeducedMismatch; |
1540 | 1.38M | } else if (TDF & TDF_ArgWithReferenceType) { |
1541 | | // C++ [temp.deduct.conv]p4: |
1542 | | // If the original A is a reference type, A can be more cv-qualified |
1543 | | // than the deduced A |
1544 | 53 | if (!A.getQualifiers().compatiblyIncludes(P.getQualifiers())) |
1545 | 0 | return Sema::TDK_NonDeducedMismatch; |
1546 | | |
1547 | | // Strip out all extra qualifiers from the argument to figure out the |
1548 | | // type we're converting to, prior to the qualification conversion. |
1549 | 53 | Qualifiers Quals; |
1550 | 53 | A = S.Context.getUnqualifiedArrayType(A, Quals); |
1551 | 53 | A = S.Context.getQualifiedType(A, P.getQualifiers()); |
1552 | 1.38M | } else if (!IsPossiblyOpaquelyQualifiedType(P)) { |
1553 | 1.15M | if (P.getCVRQualifiers() != A.getCVRQualifiers()) |
1554 | 25.7k | return Sema::TDK_NonDeducedMismatch; |
1555 | 1.15M | } |
1556 | 2.14M | } |
1557 | | |
1558 | | // If the parameter type is not dependent, there is nothing to deduce. |
1559 | 2.22M | if (!P->isDependentType()) { |
1560 | 148k | if (TDF & TDF_SkipNonDependent) |
1561 | 11.4k | return Sema::TDK_Success; |
1562 | 137k | if ((TDF & TDF_IgnoreQualifiers) ? S.Context.hasSameUnqualifiedType(P, A)73 |
1563 | 137k | : S.Context.hasSameType(P, A)137k ) |
1564 | 84.7k | return Sema::TDK_Success; |
1565 | 52.5k | if (TDF & TDF_AllowCompatibleFunctionType && |
1566 | 52.5k | S.isSameOrCompatibleFunctionType(P, A)13.5k ) |
1567 | 18 | return Sema::TDK_Success; |
1568 | 52.5k | if (!(TDF & TDF_IgnoreQualifiers)) |
1569 | 52.5k | return Sema::TDK_NonDeducedMismatch; |
1570 | | // Otherwise, when ignoring qualifiers, the types not having the same |
1571 | | // unqualified type does not mean they do not match, so in this case we |
1572 | | // must keep going and analyze with a non-dependent parameter type. |
1573 | 52.5k | } |
1574 | | |
1575 | 2.07M | switch (P.getCanonicalType()->getTypeClass()) { |
1576 | | // Non-canonical types cannot appear here. |
1577 | 0 | #define NON_CANONICAL_TYPE(Class, Base) \ |
1578 | 0 | case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class); |
1579 | 0 | #define TYPE(Class, Base) |
1580 | 0 | #include "clang/AST/TypeNodes.inc" |
1581 | |
|
1582 | 0 | case Type::TemplateTypeParm: |
1583 | 0 | case Type::SubstTemplateTypeParmPack: |
1584 | 0 | llvm_unreachable("Type nodes handled above"); |
1585 | |
|
1586 | 1.16k | case Type::Auto: |
1587 | | // FIXME: Implement deduction in dependent case. |
1588 | 1.16k | if (P->isDependentType()) |
1589 | 1.16k | return Sema::TDK_Success; |
1590 | 1.16k | LLVM_FALLTHROUGH0 ;0 |
1591 | 2 | case Type::Builtin: |
1592 | 2 | case Type::VariableArray: |
1593 | 2 | case Type::Vector: |
1594 | 2 | case Type::FunctionNoProto: |
1595 | 2 | case Type::Record: |
1596 | 2 | case Type::Enum: |
1597 | 2 | case Type::ObjCObject: |
1598 | 2 | case Type::ObjCInterface: |
1599 | 2 | case Type::ObjCObjectPointer: |
1600 | 2 | case Type::BitInt: |
1601 | 2 | return (TDF & TDF_SkipNonDependent) || |
1602 | 2 | ((TDF & TDF_IgnoreQualifiers) |
1603 | 2 | ? S.Context.hasSameUnqualifiedType(P, A) |
1604 | 2 | : S.Context.hasSameType(P, A)0 ) |
1605 | 2 | ? Sema::TDK_Success0 |
1606 | 2 | : Sema::TDK_NonDeducedMismatch; |
1607 | | |
1608 | | // _Complex T [placeholder extension] |
1609 | 0 | case Type::Complex: { |
1610 | 0 | const auto *CP = P->castAs<ComplexType>(), *CA = A->getAs<ComplexType>(); |
1611 | 0 | if (!CA) |
1612 | 0 | return Sema::TDK_NonDeducedMismatch; |
1613 | 0 | return DeduceTemplateArgumentsByTypeMatch( |
1614 | 0 | S, TemplateParams, CP->getElementType(), CA->getElementType(), Info, |
1615 | 0 | Deduced, TDF); |
1616 | 0 | } |
1617 | | |
1618 | | // _Atomic T [extension] |
1619 | 0 | case Type::Atomic: { |
1620 | 0 | const auto *PA = P->castAs<AtomicType>(), *AA = A->getAs<AtomicType>(); |
1621 | 0 | if (!AA) |
1622 | 0 | return Sema::TDK_NonDeducedMismatch; |
1623 | 0 | return DeduceTemplateArgumentsByTypeMatch( |
1624 | 0 | S, TemplateParams, PA->getValueType(), AA->getValueType(), Info, |
1625 | 0 | Deduced, TDF); |
1626 | 0 | } |
1627 | | |
1628 | | // T * |
1629 | 87.3k | case Type::Pointer: { |
1630 | 87.3k | QualType PointeeType; |
1631 | 87.3k | if (const auto *PA = A->getAs<PointerType>()) { |
1632 | 53.8k | PointeeType = PA->getPointeeType(); |
1633 | 53.8k | } else if (const auto *33.4k PA33.4k = A->getAs<ObjCObjectPointerType>()) { |
1634 | 44 | PointeeType = PA->getPointeeType(); |
1635 | 33.4k | } else { |
1636 | 33.4k | return Sema::TDK_NonDeducedMismatch; |
1637 | 33.4k | } |
1638 | 53.9k | return DeduceTemplateArgumentsByTypeMatch( |
1639 | 53.9k | S, TemplateParams, P->castAs<PointerType>()->getPointeeType(), |
1640 | 53.9k | PointeeType, Info, Deduced, |
1641 | 53.9k | TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass)); |
1642 | 87.3k | } |
1643 | | |
1644 | | // T & |
1645 | 108k | case Type::LValueReference: { |
1646 | 108k | const auto *RP = P->castAs<LValueReferenceType>(), |
1647 | 108k | *RA = A->getAs<LValueReferenceType>(); |
1648 | 108k | if (!RA) |
1649 | 67.6k | return Sema::TDK_NonDeducedMismatch; |
1650 | | |
1651 | 40.8k | return DeduceTemplateArgumentsByTypeMatch( |
1652 | 40.8k | S, TemplateParams, RP->getPointeeType(), RA->getPointeeType(), Info, |
1653 | 40.8k | Deduced, 0); |
1654 | 108k | } |
1655 | | |
1656 | | // T && [C++0x] |
1657 | 44.5k | case Type::RValueReference: { |
1658 | 44.5k | const auto *RP = P->castAs<RValueReferenceType>(), |
1659 | 44.5k | *RA = A->getAs<RValueReferenceType>(); |
1660 | 44.5k | if (!RA) |
1661 | 43.2k | return Sema::TDK_NonDeducedMismatch; |
1662 | | |
1663 | 1.32k | return DeduceTemplateArgumentsByTypeMatch( |
1664 | 1.32k | S, TemplateParams, RP->getPointeeType(), RA->getPointeeType(), Info, |
1665 | 1.32k | Deduced, 0); |
1666 | 44.5k | } |
1667 | | |
1668 | | // T [] (implied, but not stated explicitly) |
1669 | 57.0k | case Type::IncompleteArray: { |
1670 | 57.0k | const auto *IAA = S.Context.getAsIncompleteArrayType(A); |
1671 | 57.0k | if (!IAA) |
1672 | 56.7k | return Sema::TDK_NonDeducedMismatch; |
1673 | | |
1674 | 281 | return DeduceTemplateArgumentsByTypeMatch( |
1675 | 281 | S, TemplateParams, |
1676 | 281 | S.Context.getAsIncompleteArrayType(P)->getElementType(), |
1677 | 281 | IAA->getElementType(), Info, Deduced, TDF & TDF_IgnoreQualifiers); |
1678 | 57.0k | } |
1679 | | |
1680 | | // T [integer-constant] |
1681 | 56 | case Type::ConstantArray: { |
1682 | 56 | const auto *CAA = S.Context.getAsConstantArrayType(A), |
1683 | 56 | *CAP = S.Context.getAsConstantArrayType(P); |
1684 | 56 | assert(CAP); |
1685 | 56 | if (!CAA || CAA->getSize() != CAP->getSize()45 ) |
1686 | 15 | return Sema::TDK_NonDeducedMismatch; |
1687 | | |
1688 | 41 | return DeduceTemplateArgumentsByTypeMatch( |
1689 | 41 | S, TemplateParams, CAP->getElementType(), CAA->getElementType(), Info, |
1690 | 41 | Deduced, TDF & TDF_IgnoreQualifiers); |
1691 | 56 | } |
1692 | | |
1693 | | // type [i] |
1694 | 57.3k | case Type::DependentSizedArray: { |
1695 | 57.3k | const auto *AA = S.Context.getAsArrayType(A); |
1696 | 57.3k | if (!AA) |
1697 | 56.6k | return Sema::TDK_NonDeducedMismatch; |
1698 | | |
1699 | | // Check the element type of the arrays |
1700 | 682 | const auto *DAP = S.Context.getAsDependentSizedArrayType(P); |
1701 | 682 | assert(DAP); |
1702 | 682 | if (auto Result = DeduceTemplateArgumentsByTypeMatch( |
1703 | 682 | S, TemplateParams, DAP->getElementType(), AA->getElementType(), |
1704 | 682 | Info, Deduced, TDF & TDF_IgnoreQualifiers)) |
1705 | 42 | return Result; |
1706 | | |
1707 | | // Determine the array bound is something we can deduce. |
1708 | 640 | const NonTypeTemplateParmDecl *NTTP = |
1709 | 640 | getDeducedParameterFromExpr(Info, DAP->getSizeExpr()); |
1710 | 640 | if (!NTTP) |
1711 | 4 | return Sema::TDK_Success; |
1712 | | |
1713 | | // We can perform template argument deduction for the given non-type |
1714 | | // template parameter. |
1715 | 636 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
1716 | 636 | "saw non-type template parameter with wrong depth"); |
1717 | 636 | if (const auto *CAA = dyn_cast<ConstantArrayType>(AA)) { |
1718 | 545 | llvm::APSInt Size(CAA->getSize()); |
1719 | 545 | return DeduceNonTypeTemplateArgument( |
1720 | 545 | S, TemplateParams, NTTP, Size, S.Context.getSizeType(), |
1721 | 545 | /*ArrayBound=*/true, Info, Deduced); |
1722 | 545 | } |
1723 | 91 | if (const auto *DAA = dyn_cast<DependentSizedArrayType>(AA)) |
1724 | 34 | if (DAA->getSizeExpr()) |
1725 | 34 | return DeduceNonTypeTemplateArgument( |
1726 | 34 | S, TemplateParams, NTTP, DAA->getSizeExpr(), Info, Deduced); |
1727 | | |
1728 | | // Incomplete type does not match a dependently-sized array type |
1729 | 57 | return Sema::TDK_NonDeducedMismatch; |
1730 | 91 | } |
1731 | | |
1732 | | // type(*)(T) |
1733 | | // T(*)() |
1734 | | // T(*)(T) |
1735 | 117k | case Type::FunctionProto: { |
1736 | 117k | const auto *FPP = P->castAs<FunctionProtoType>(), |
1737 | 117k | *FPA = A->getAs<FunctionProtoType>(); |
1738 | 117k | if (!FPA) |
1739 | 8.88k | return Sema::TDK_NonDeducedMismatch; |
1740 | | |
1741 | 108k | if (FPP->getMethodQuals() != FPA->getMethodQuals() || |
1742 | 108k | FPP->getRefQualifier() != FPA->getRefQualifier()108k || |
1743 | 108k | FPP->isVariadic() != FPA->isVariadic()108k ) |
1744 | 109 | return Sema::TDK_NonDeducedMismatch; |
1745 | | |
1746 | | // Check return types. |
1747 | 108k | if (auto Result = DeduceTemplateArgumentsByTypeMatch( |
1748 | 108k | S, TemplateParams, FPP->getReturnType(), FPA->getReturnType(), |
1749 | 108k | Info, Deduced, 0, |
1750 | 108k | /*PartialOrdering=*/false, |
1751 | 108k | /*DeducedFromArrayBound=*/false)) |
1752 | 11.9k | return Result; |
1753 | | |
1754 | | // Check parameter types. |
1755 | 96.2k | if (auto Result = DeduceTemplateArguments( |
1756 | 96.2k | S, TemplateParams, FPP->param_type_begin(), FPP->getNumParams(), |
1757 | 96.2k | FPA->param_type_begin(), FPA->getNumParams(), Info, Deduced, |
1758 | 96.2k | TDF & TDF_TopLevelParameterTypeList)) |
1759 | 81.7k | return Result; |
1760 | | |
1761 | 14.4k | if (TDF & TDF_AllowCompatibleFunctionType) |
1762 | 12.4k | return Sema::TDK_Success; |
1763 | | |
1764 | | // FIXME: Per core-2016/10/1019 (no corresponding core issue yet), permit |
1765 | | // deducing through the noexcept-specifier if it's part of the canonical |
1766 | | // type. libstdc++ relies on this. |
1767 | 1.99k | Expr *NoexceptExpr = FPP->getNoexceptExpr(); |
1768 | 1.99k | if (const NonTypeTemplateParmDecl *NTTP = |
1769 | 1.99k | NoexceptExpr ? getDeducedParameterFromExpr(Info, NoexceptExpr) |
1770 | 1.99k | : nullptr) { |
1771 | 21 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
1772 | 21 | "saw non-type template parameter with wrong depth"); |
1773 | | |
1774 | 0 | llvm::APSInt Noexcept(1); |
1775 | 21 | switch (FPA->canThrow()) { |
1776 | 8 | case CT_Cannot: |
1777 | 8 | Noexcept = 1; |
1778 | 8 | LLVM_FALLTHROUGH; |
1779 | | |
1780 | 17 | case CT_Can: |
1781 | | // We give E in noexcept(E) the "deduced from array bound" treatment. |
1782 | | // FIXME: Should we? |
1783 | 17 | return DeduceNonTypeTemplateArgument( |
1784 | 17 | S, TemplateParams, NTTP, Noexcept, S.Context.BoolTy, |
1785 | 17 | /*DeducedFromArrayBound=*/true, Info, Deduced); |
1786 | | |
1787 | 4 | case CT_Dependent: |
1788 | 4 | if (Expr *ArgNoexceptExpr = FPA->getNoexceptExpr()) |
1789 | 0 | return DeduceNonTypeTemplateArgument( |
1790 | 0 | S, TemplateParams, NTTP, ArgNoexceptExpr, Info, Deduced); |
1791 | | // Can't deduce anything from throw(T...). |
1792 | 4 | break; |
1793 | 21 | } |
1794 | 21 | } |
1795 | | // FIXME: Detect non-deduced exception specification mismatches? |
1796 | | // |
1797 | | // Careful about [temp.deduct.call] and [temp.deduct.conv], which allow |
1798 | | // top-level differences in noexcept-specifications. |
1799 | | |
1800 | 1.97k | return Sema::TDK_Success; |
1801 | 1.99k | } |
1802 | | |
1803 | 418 | case Type::InjectedClassName: |
1804 | | // Treat a template's injected-class-name as if the template |
1805 | | // specialization type had been used. |
1806 | | |
1807 | | // template-name<T> (where template-name refers to a class template) |
1808 | | // template-name<i> |
1809 | | // TT<T> |
1810 | | // TT<i> |
1811 | | // TT<> |
1812 | 1.46M | case Type::TemplateSpecialization: { |
1813 | | // When Arg cannot be a derived class, we can just try to deduce template |
1814 | | // arguments from the template-id. |
1815 | 1.46M | if (!(TDF & TDF_DerivedClass) || !A->isRecordType()875k ) |
1816 | 1.09M | return DeduceTemplateSpecArguments(S, TemplateParams, P, A, Info, |
1817 | 1.09M | Deduced); |
1818 | | |
1819 | 369k | SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(), |
1820 | 369k | Deduced.end()); |
1821 | | |
1822 | 369k | auto Result = |
1823 | 369k | DeduceTemplateSpecArguments(S, TemplateParams, P, A, Info, Deduced); |
1824 | 369k | if (Result == Sema::TDK_Success) |
1825 | 101k | return Result; |
1826 | | |
1827 | | // We cannot inspect base classes as part of deduction when the type |
1828 | | // is incomplete, so either instantiate any templates necessary to |
1829 | | // complete the type, or skip over it if it cannot be completed. |
1830 | 268k | if (!S.isCompleteType(Info.getLocation(), A)) |
1831 | 9 | return Result; |
1832 | | |
1833 | | // Reset the incorrectly deduced argument from above. |
1834 | 267k | Deduced = DeducedOrig; |
1835 | | |
1836 | | // Check bases according to C++14 [temp.deduct.call] p4b3: |
1837 | 267k | auto BaseResult = DeduceTemplateBases(S, getCanonicalRD(A), |
1838 | 267k | TemplateParams, P, Info, Deduced); |
1839 | 267k | return BaseResult != Sema::TDK_Invalid ? BaseResult8.85k : Result259k ; |
1840 | 268k | } |
1841 | | |
1842 | | // T type::* |
1843 | | // T T::* |
1844 | | // T (type::*)() |
1845 | | // type (T::*)() |
1846 | | // type (type::*)(T) |
1847 | | // type (T::*)(T) |
1848 | | // T (type::*)(T) |
1849 | | // T (T::*)() |
1850 | | // T (T::*)(T) |
1851 | 20.2k | case Type::MemberPointer: { |
1852 | 20.2k | const auto *MPP = P->castAs<MemberPointerType>(), |
1853 | 20.2k | *MPA = A->getAs<MemberPointerType>(); |
1854 | 20.2k | if (!MPA) |
1855 | 19.9k | return Sema::TDK_NonDeducedMismatch; |
1856 | | |
1857 | 277 | QualType PPT = MPP->getPointeeType(); |
1858 | 277 | if (PPT->isFunctionType()) |
1859 | 161 | S.adjustMemberFunctionCC(PPT, /*IsStatic=*/true, |
1860 | 161 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
1861 | 277 | QualType APT = MPA->getPointeeType(); |
1862 | 277 | if (APT->isFunctionType()) |
1863 | 176 | S.adjustMemberFunctionCC(APT, /*IsStatic=*/true, |
1864 | 176 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
1865 | | |
1866 | 277 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
1867 | 277 | if (auto Result = DeduceTemplateArgumentsByTypeMatch( |
1868 | 277 | S, TemplateParams, PPT, APT, Info, Deduced, SubTDF)) |
1869 | 85 | return Result; |
1870 | 192 | return DeduceTemplateArgumentsByTypeMatch( |
1871 | 192 | S, TemplateParams, QualType(MPP->getClass(), 0), |
1872 | 192 | QualType(MPA->getClass(), 0), Info, Deduced, SubTDF); |
1873 | 277 | } |
1874 | | |
1875 | | // (clang extension) |
1876 | | // |
1877 | | // type(^)(T) |
1878 | | // T(^)() |
1879 | | // T(^)(T) |
1880 | 430 | case Type::BlockPointer: { |
1881 | 430 | const auto *BPP = P->castAs<BlockPointerType>(), |
1882 | 430 | *BPA = A->getAs<BlockPointerType>(); |
1883 | 430 | if (!BPA) |
1884 | 418 | return Sema::TDK_NonDeducedMismatch; |
1885 | 12 | return DeduceTemplateArgumentsByTypeMatch( |
1886 | 12 | S, TemplateParams, BPP->getPointeeType(), BPA->getPointeeType(), Info, |
1887 | 12 | Deduced, 0); |
1888 | 430 | } |
1889 | | |
1890 | | // (clang extension) |
1891 | | // |
1892 | | // T __attribute__(((ext_vector_type(<integral constant>)))) |
1893 | 6 | case Type::ExtVector: { |
1894 | 6 | const auto *VP = P->castAs<ExtVectorType>(); |
1895 | 6 | QualType ElementType; |
1896 | 6 | if (const auto *VA = A->getAs<ExtVectorType>()) { |
1897 | | // Make sure that the vectors have the same number of elements. |
1898 | 3 | if (VP->getNumElements() != VA->getNumElements()) |
1899 | 2 | return Sema::TDK_NonDeducedMismatch; |
1900 | 1 | ElementType = VA->getElementType(); |
1901 | 3 | } else if (const auto *VA = A->getAs<DependentSizedExtVectorType>()) { |
1902 | | // We can't check the number of elements, since the argument has a |
1903 | | // dependent number of elements. This can only occur during partial |
1904 | | // ordering. |
1905 | 2 | ElementType = VA->getElementType(); |
1906 | 2 | } else { |
1907 | 1 | return Sema::TDK_NonDeducedMismatch; |
1908 | 1 | } |
1909 | | // Perform deduction on the element types. |
1910 | 3 | return DeduceTemplateArgumentsByTypeMatch( |
1911 | 3 | S, TemplateParams, VP->getElementType(), ElementType, Info, Deduced, |
1912 | 3 | TDF); |
1913 | 6 | } |
1914 | | |
1915 | 0 | case Type::DependentVector: { |
1916 | 0 | const auto *VP = P->castAs<DependentVectorType>(); |
1917 | |
|
1918 | 0 | if (const auto *VA = A->getAs<VectorType>()) { |
1919 | | // Perform deduction on the element types. |
1920 | 0 | if (auto Result = DeduceTemplateArgumentsByTypeMatch( |
1921 | 0 | S, TemplateParams, VP->getElementType(), VA->getElementType(), |
1922 | 0 | Info, Deduced, TDF)) |
1923 | 0 | return Result; |
1924 | | |
1925 | | // Perform deduction on the vector size, if we can. |
1926 | 0 | const NonTypeTemplateParmDecl *NTTP = |
1927 | 0 | getDeducedParameterFromExpr(Info, VP->getSizeExpr()); |
1928 | 0 | if (!NTTP) |
1929 | 0 | return Sema::TDK_Success; |
1930 | | |
1931 | 0 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
1932 | 0 | ArgSize = VA->getNumElements(); |
1933 | | // Note that we use the "array bound" rules here; just like in that |
1934 | | // case, we don't have any particular type for the vector size, but |
1935 | | // we can provide one if necessary. |
1936 | 0 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, |
1937 | 0 | S.Context.UnsignedIntTy, true, |
1938 | 0 | Info, Deduced); |
1939 | 0 | } |
1940 | | |
1941 | 0 | if (const auto *VA = A->getAs<DependentVectorType>()) { |
1942 | | // Perform deduction on the element types. |
1943 | 0 | if (auto Result = DeduceTemplateArgumentsByTypeMatch( |
1944 | 0 | S, TemplateParams, VP->getElementType(), VA->getElementType(), |
1945 | 0 | Info, Deduced, TDF)) |
1946 | 0 | return Result; |
1947 | | |
1948 | | // Perform deduction on the vector size, if we can. |
1949 | 0 | const NonTypeTemplateParmDecl *NTTP = |
1950 | 0 | getDeducedParameterFromExpr(Info, VP->getSizeExpr()); |
1951 | 0 | if (!NTTP) |
1952 | 0 | return Sema::TDK_Success; |
1953 | | |
1954 | 0 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
1955 | 0 | VA->getSizeExpr(), Info, Deduced); |
1956 | 0 | } |
1957 | | |
1958 | 0 | return Sema::TDK_NonDeducedMismatch; |
1959 | 0 | } |
1960 | | |
1961 | | // (clang extension) |
1962 | | // |
1963 | | // T __attribute__(((ext_vector_type(N)))) |
1964 | 14 | case Type::DependentSizedExtVector: { |
1965 | 14 | const auto *VP = P->castAs<DependentSizedExtVectorType>(); |
1966 | | |
1967 | 14 | if (const auto *VA = A->getAs<ExtVectorType>()) { |
1968 | | // Perform deduction on the element types. |
1969 | 8 | if (auto Result = DeduceTemplateArgumentsByTypeMatch( |
1970 | 8 | S, TemplateParams, VP->getElementType(), VA->getElementType(), |
1971 | 8 | Info, Deduced, TDF)) |
1972 | 2 | return Result; |
1973 | | |
1974 | | // Perform deduction on the vector size, if we can. |
1975 | 6 | const NonTypeTemplateParmDecl *NTTP = |
1976 | 6 | getDeducedParameterFromExpr(Info, VP->getSizeExpr()); |
1977 | 6 | if (!NTTP) |
1978 | 0 | return Sema::TDK_Success; |
1979 | | |
1980 | 6 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
1981 | 6 | ArgSize = VA->getNumElements(); |
1982 | | // Note that we use the "array bound" rules here; just like in that |
1983 | | // case, we don't have any particular type for the vector size, but |
1984 | | // we can provide one if necessary. |
1985 | 6 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, |
1986 | 6 | S.Context.IntTy, true, Info, |
1987 | 6 | Deduced); |
1988 | 6 | } |
1989 | | |
1990 | 6 | if (const auto *VA = A->getAs<DependentSizedExtVectorType>()) { |
1991 | | // Perform deduction on the element types. |
1992 | 4 | if (auto Result = DeduceTemplateArgumentsByTypeMatch( |
1993 | 4 | S, TemplateParams, VP->getElementType(), VA->getElementType(), |
1994 | 4 | Info, Deduced, TDF)) |
1995 | 2 | return Result; |
1996 | | |
1997 | | // Perform deduction on the vector size, if we can. |
1998 | 2 | const NonTypeTemplateParmDecl *NTTP = |
1999 | 2 | getDeducedParameterFromExpr(Info, VP->getSizeExpr()); |
2000 | 2 | if (!NTTP) |
2001 | 0 | return Sema::TDK_Success; |
2002 | | |
2003 | 2 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
2004 | 2 | VA->getSizeExpr(), Info, Deduced); |
2005 | 2 | } |
2006 | | |
2007 | 2 | return Sema::TDK_NonDeducedMismatch; |
2008 | 6 | } |
2009 | | |
2010 | | // (clang extension) |
2011 | | // |
2012 | | // T __attribute__((matrix_type(<integral constant>, |
2013 | | // <integral constant>))) |
2014 | 17 | case Type::ConstantMatrix: { |
2015 | 17 | const auto *MP = P->castAs<ConstantMatrixType>(), |
2016 | 17 | *MA = A->getAs<ConstantMatrixType>(); |
2017 | 17 | if (!MA) |
2018 | 9 | return Sema::TDK_NonDeducedMismatch; |
2019 | | |
2020 | | // Check that the dimensions are the same |
2021 | 8 | if (MP->getNumRows() != MA->getNumRows() || |
2022 | 8 | MP->getNumColumns() != MA->getNumColumns()3 ) { |
2023 | 6 | return Sema::TDK_NonDeducedMismatch; |
2024 | 6 | } |
2025 | | // Perform deduction on element types. |
2026 | 2 | return DeduceTemplateArgumentsByTypeMatch( |
2027 | 2 | S, TemplateParams, MP->getElementType(), MA->getElementType(), Info, |
2028 | 2 | Deduced, TDF); |
2029 | 8 | } |
2030 | | |
2031 | 338 | case Type::DependentSizedMatrix: { |
2032 | 338 | const auto *MP = P->castAs<DependentSizedMatrixType>(); |
2033 | 338 | const auto *MA = A->getAs<MatrixType>(); |
2034 | 338 | if (!MA) |
2035 | 248 | return Sema::TDK_NonDeducedMismatch; |
2036 | | |
2037 | | // Check the element type of the matrixes. |
2038 | 90 | if (auto Result = DeduceTemplateArgumentsByTypeMatch( |
2039 | 90 | S, TemplateParams, MP->getElementType(), MA->getElementType(), |
2040 | 90 | Info, Deduced, TDF)) |
2041 | 7 | return Result; |
2042 | | |
2043 | | // Try to deduce a matrix dimension. |
2044 | 83 | auto DeduceMatrixArg = |
2045 | 83 | [&S, &Info, &Deduced, &TemplateParams]( |
2046 | 83 | Expr *ParamExpr, const MatrixType *A, |
2047 | 83 | unsigned (ConstantMatrixType::*GetArgDimension)() const, |
2048 | 157 | Expr *(DependentSizedMatrixType::*GetArgDimensionExpr)() const) { |
2049 | 157 | const auto *ACM = dyn_cast<ConstantMatrixType>(A); |
2050 | 157 | const auto *ADM = dyn_cast<DependentSizedMatrixType>(A); |
2051 | 157 | if (!ParamExpr->isValueDependent()) { |
2052 | 42 | Optional<llvm::APSInt> ParamConst = |
2053 | 42 | ParamExpr->getIntegerConstantExpr(S.Context); |
2054 | 42 | if (!ParamConst) |
2055 | 0 | return Sema::TDK_NonDeducedMismatch; |
2056 | | |
2057 | 42 | if (ACM) { |
2058 | 26 | if ((ACM->*GetArgDimension)() == *ParamConst) |
2059 | 19 | return Sema::TDK_Success; |
2060 | 7 | return Sema::TDK_NonDeducedMismatch; |
2061 | 26 | } |
2062 | | |
2063 | 16 | Expr *ArgExpr = (ADM->*GetArgDimensionExpr)(); |
2064 | 16 | if (Optional<llvm::APSInt> ArgConst = |
2065 | 16 | ArgExpr->getIntegerConstantExpr(S.Context)) |
2066 | 2 | if (*ArgConst == *ParamConst) |
2067 | 2 | return Sema::TDK_Success; |
2068 | 14 | return Sema::TDK_NonDeducedMismatch; |
2069 | 16 | } |
2070 | | |
2071 | 115 | const NonTypeTemplateParmDecl *NTTP = |
2072 | 115 | getDeducedParameterFromExpr(Info, ParamExpr); |
2073 | 115 | if (!NTTP) |
2074 | 9 | return Sema::TDK_Success; |
2075 | | |
2076 | 106 | if (ACM) { |
2077 | 80 | llvm::APSInt ArgConst( |
2078 | 80 | S.Context.getTypeSize(S.Context.getSizeType())); |
2079 | 80 | ArgConst = (ACM->*GetArgDimension)(); |
2080 | 80 | return DeduceNonTypeTemplateArgument( |
2081 | 80 | S, TemplateParams, NTTP, ArgConst, S.Context.getSizeType(), |
2082 | 80 | /*ArrayBound=*/true, Info, Deduced); |
2083 | 80 | } |
2084 | | |
2085 | 26 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
2086 | 26 | (ADM->*GetArgDimensionExpr)(), |
2087 | 26 | Info, Deduced); |
2088 | 106 | }; |
2089 | | |
2090 | 83 | if (auto Result = DeduceMatrixArg(MP->getRowExpr(), MA, |
2091 | 83 | &ConstantMatrixType::getNumRows, |
2092 | 83 | &DependentSizedMatrixType::getRowExpr)) |
2093 | 9 | return Result; |
2094 | | |
2095 | 74 | return DeduceMatrixArg(MP->getColumnExpr(), MA, |
2096 | 74 | &ConstantMatrixType::getNumColumns, |
2097 | 74 | &DependentSizedMatrixType::getColumnExpr); |
2098 | 83 | } |
2099 | | |
2100 | | // (clang extension) |
2101 | | // |
2102 | | // T __attribute__(((address_space(N)))) |
2103 | 2 | case Type::DependentAddressSpace: { |
2104 | 2 | const auto *ASP = P->castAs<DependentAddressSpaceType>(); |
2105 | | |
2106 | 2 | if (const auto *ASA = A->getAs<DependentAddressSpaceType>()) { |
2107 | | // Perform deduction on the pointer type. |
2108 | 0 | if (auto Result = DeduceTemplateArgumentsByTypeMatch( |
2109 | 0 | S, TemplateParams, ASP->getPointeeType(), ASA->getPointeeType(), |
2110 | 0 | Info, Deduced, TDF)) |
2111 | 0 | return Result; |
2112 | | |
2113 | | // Perform deduction on the address space, if we can. |
2114 | 0 | const NonTypeTemplateParmDecl *NTTP = |
2115 | 0 | getDeducedParameterFromExpr(Info, ASP->getAddrSpaceExpr()); |
2116 | 0 | if (!NTTP) |
2117 | 0 | return Sema::TDK_Success; |
2118 | | |
2119 | 0 | return DeduceNonTypeTemplateArgument( |
2120 | 0 | S, TemplateParams, NTTP, ASA->getAddrSpaceExpr(), Info, Deduced); |
2121 | 0 | } |
2122 | | |
2123 | 2 | if (isTargetAddressSpace(A.getAddressSpace())) { |
2124 | 2 | llvm::APSInt ArgAddressSpace(S.Context.getTypeSize(S.Context.IntTy), |
2125 | 2 | false); |
2126 | 2 | ArgAddressSpace = toTargetAddressSpace(A.getAddressSpace()); |
2127 | | |
2128 | | // Perform deduction on the pointer types. |
2129 | 2 | if (auto Result = DeduceTemplateArgumentsByTypeMatch( |
2130 | 2 | S, TemplateParams, ASP->getPointeeType(), |
2131 | 2 | S.Context.removeAddrSpaceQualType(A), Info, Deduced, TDF)) |
2132 | 0 | return Result; |
2133 | | |
2134 | | // Perform deduction on the address space, if we can. |
2135 | 2 | const NonTypeTemplateParmDecl *NTTP = |
2136 | 2 | getDeducedParameterFromExpr(Info, ASP->getAddrSpaceExpr()); |
2137 | 2 | if (!NTTP) |
2138 | 0 | return Sema::TDK_Success; |
2139 | | |
2140 | 2 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
2141 | 2 | ArgAddressSpace, S.Context.IntTy, |
2142 | 2 | true, Info, Deduced); |
2143 | 2 | } |
2144 | | |
2145 | 0 | return Sema::TDK_NonDeducedMismatch; |
2146 | 2 | } |
2147 | 1 | case Type::DependentBitInt: { |
2148 | 1 | const auto *IP = P->castAs<DependentBitIntType>(); |
2149 | | |
2150 | 1 | if (const auto *IA = A->getAs<BitIntType>()) { |
2151 | 1 | if (IP->isUnsigned() != IA->isUnsigned()) |
2152 | 0 | return Sema::TDK_NonDeducedMismatch; |
2153 | | |
2154 | 1 | const NonTypeTemplateParmDecl *NTTP = |
2155 | 1 | getDeducedParameterFromExpr(Info, IP->getNumBitsExpr()); |
2156 | 1 | if (!NTTP) |
2157 | 0 | return Sema::TDK_Success; |
2158 | | |
2159 | 1 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
2160 | 1 | ArgSize = IA->getNumBits(); |
2161 | | |
2162 | 1 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, |
2163 | 1 | S.Context.IntTy, true, Info, |
2164 | 1 | Deduced); |
2165 | 1 | } |
2166 | | |
2167 | 0 | if (const auto *IA = A->getAs<DependentBitIntType>()) { |
2168 | 0 | if (IP->isUnsigned() != IA->isUnsigned()) |
2169 | 0 | return Sema::TDK_NonDeducedMismatch; |
2170 | 0 | return Sema::TDK_Success; |
2171 | 0 | } |
2172 | | |
2173 | 0 | return Sema::TDK_NonDeducedMismatch; |
2174 | 0 | } |
2175 | | |
2176 | 0 | case Type::TypeOfExpr: |
2177 | 0 | case Type::TypeOf: |
2178 | 104k | case Type::DependentName: |
2179 | 104k | case Type::UnresolvedUsing: |
2180 | 112k | case Type::Decltype: |
2181 | 112k | case Type::UnaryTransform: |
2182 | 112k | case Type::DeducedTemplateSpecialization: |
2183 | 112k | case Type::DependentTemplateSpecialization: |
2184 | 112k | case Type::PackExpansion: |
2185 | 112k | case Type::Pipe: |
2186 | | // No template argument deduction for these types |
2187 | 112k | return Sema::TDK_Success; |
2188 | 2.07M | } |
2189 | | |
2190 | 0 | llvm_unreachable("Invalid Type Class!"); |
2191 | 0 | } |
2192 | | |
2193 | | static Sema::TemplateDeductionResult |
2194 | | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
2195 | | const TemplateArgument &P, TemplateArgument A, |
2196 | | TemplateDeductionInfo &Info, |
2197 | 2.36M | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
2198 | | // If the template argument is a pack expansion, perform template argument |
2199 | | // deduction against the pattern of that expansion. This only occurs during |
2200 | | // partial ordering. |
2201 | 2.36M | if (A.isPackExpansion()) |
2202 | 5.97k | A = A.getPackExpansionPattern(); |
2203 | | |
2204 | 2.36M | switch (P.getKind()) { |
2205 | 0 | case TemplateArgument::Null: |
2206 | 0 | llvm_unreachable("Null template argument in parameter list"); |
2207 | |
|
2208 | 1.79M | case TemplateArgument::Type: |
2209 | 1.79M | if (A.getKind() == TemplateArgument::Type) |
2210 | 1.79M | return DeduceTemplateArgumentsByTypeMatch( |
2211 | 1.79M | S, TemplateParams, P.getAsType(), A.getAsType(), Info, Deduced, 0); |
2212 | 3 | Info.FirstArg = P; |
2213 | 3 | Info.SecondArg = A; |
2214 | 3 | return Sema::TDK_NonDeducedMismatch; |
2215 | | |
2216 | 1.42k | case TemplateArgument::Template: |
2217 | 1.42k | if (A.getKind() == TemplateArgument::Template) |
2218 | 1.42k | return DeduceTemplateArguments(S, TemplateParams, P.getAsTemplate(), |
2219 | 1.42k | A.getAsTemplate(), Info, Deduced); |
2220 | 0 | Info.FirstArg = P; |
2221 | 0 | Info.SecondArg = A; |
2222 | 0 | return Sema::TDK_NonDeducedMismatch; |
2223 | | |
2224 | 0 | case TemplateArgument::TemplateExpansion: |
2225 | 0 | llvm_unreachable("caller should handle pack expansions"); |
2226 | |
|
2227 | 29 | case TemplateArgument::Declaration: |
2228 | 29 | if (A.getKind() == TemplateArgument::Declaration && |
2229 | 29 | isSameDeclaration(P.getAsDecl(), A.getAsDecl())6 ) |
2230 | 6 | return Sema::TDK_Success; |
2231 | | |
2232 | 23 | Info.FirstArg = P; |
2233 | 23 | Info.SecondArg = A; |
2234 | 23 | return Sema::TDK_NonDeducedMismatch; |
2235 | | |
2236 | 6 | case TemplateArgument::NullPtr: |
2237 | 6 | if (A.getKind() == TemplateArgument::NullPtr && |
2238 | 6 | S.Context.hasSameType(P.getNullPtrType(), A.getNullPtrType())5 ) |
2239 | 5 | return Sema::TDK_Success; |
2240 | | |
2241 | 1 | Info.FirstArg = P; |
2242 | 1 | Info.SecondArg = A; |
2243 | 1 | return Sema::TDK_NonDeducedMismatch; |
2244 | | |
2245 | 307k | case TemplateArgument::Integral: |
2246 | 307k | if (A.getKind() == TemplateArgument::Integral) { |
2247 | 249k | if (hasSameExtendedValue(P.getAsIntegral(), A.getAsIntegral())) |
2248 | 138k | return Sema::TDK_Success; |
2249 | 249k | } |
2250 | 168k | Info.FirstArg = P; |
2251 | 168k | Info.SecondArg = A; |
2252 | 168k | return Sema::TDK_NonDeducedMismatch; |
2253 | | |
2254 | 256k | case TemplateArgument::Expression: |
2255 | 256k | if (const NonTypeTemplateParmDecl *NTTP = |
2256 | 256k | getDeducedParameterFromExpr(Info, P.getAsExpr())) { |
2257 | 256k | if (A.getKind() == TemplateArgument::Integral) |
2258 | 178k | return DeduceNonTypeTemplateArgument( |
2259 | 178k | S, TemplateParams, NTTP, A.getAsIntegral(), A.getIntegralType(), |
2260 | 178k | /*ArrayBound=*/false, Info, Deduced); |
2261 | 78.2k | if (A.getKind() == TemplateArgument::NullPtr) |
2262 | 39 | return DeduceNullPtrTemplateArgument(S, TemplateParams, NTTP, |
2263 | 39 | A.getNullPtrType(), Info, Deduced); |
2264 | 78.2k | if (A.getKind() == TemplateArgument::Expression) |
2265 | 77.0k | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
2266 | 77.0k | A.getAsExpr(), Info, Deduced); |
2267 | 1.19k | if (A.getKind() == TemplateArgument::Declaration) |
2268 | 1.19k | return DeduceNonTypeTemplateArgument( |
2269 | 1.19k | S, TemplateParams, NTTP, A.getAsDecl(), A.getParamTypeForDecl(), |
2270 | 1.19k | Info, Deduced); |
2271 | | |
2272 | 6 | Info.FirstArg = P; |
2273 | 6 | Info.SecondArg = A; |
2274 | 6 | return Sema::TDK_NonDeducedMismatch; |
2275 | 1.19k | } |
2276 | | |
2277 | | // Can't deduce anything, but that's okay. |
2278 | 276 | return Sema::TDK_Success; |
2279 | 0 | case TemplateArgument::Pack: |
2280 | 0 | llvm_unreachable("Argument packs should be expanded by the caller!"); |
2281 | 2.36M | } |
2282 | | |
2283 | 0 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
2284 | 0 | } |
2285 | | |
2286 | | /// Determine whether there is a template argument to be used for |
2287 | | /// deduction. |
2288 | | /// |
2289 | | /// This routine "expands" argument packs in-place, overriding its input |
2290 | | /// parameters so that \c Args[ArgIdx] will be the available template argument. |
2291 | | /// |
2292 | | /// \returns true if there is another template argument (which will be at |
2293 | | /// \c Args[ArgIdx]), false otherwise. |
2294 | | static bool hasTemplateArgumentForDeduction(ArrayRef<TemplateArgument> &Args, |
2295 | 5.34M | unsigned &ArgIdx) { |
2296 | 5.34M | if (ArgIdx == Args.size()) |
2297 | 678k | return false; |
2298 | | |
2299 | 4.66M | const TemplateArgument &Arg = Args[ArgIdx]; |
2300 | 4.66M | if (Arg.getKind() != TemplateArgument::Pack) |
2301 | 4.47M | return true; |
2302 | | |
2303 | 187k | assert(ArgIdx == Args.size() - 1 && "Pack not at the end of argument list?"); |
2304 | 0 | Args = Arg.pack_elements(); |
2305 | 187k | ArgIdx = 0; |
2306 | 187k | return ArgIdx < Args.size(); |
2307 | 4.66M | } |
2308 | | |
2309 | | /// Determine whether the given set of template arguments has a pack |
2310 | | /// expansion that is not the last template argument. |
2311 | 2.60M | static bool hasPackExpansionBeforeEnd(ArrayRef<TemplateArgument> Args) { |
2312 | 2.60M | bool FoundPackExpansion = false; |
2313 | 4.77M | for (const auto &A : Args) { |
2314 | 4.77M | if (FoundPackExpansion) |
2315 | 12 | return true; |
2316 | | |
2317 | 4.77M | if (A.getKind() == TemplateArgument::Pack) |
2318 | 184k | return hasPackExpansionBeforeEnd(A.pack_elements()); |
2319 | | |
2320 | | // FIXME: If this is a fixed-arity pack expansion from an outer level of |
2321 | | // templates, it should not be treated as a pack expansion. |
2322 | 4.58M | if (A.isPackExpansion()) |
2323 | 119k | FoundPackExpansion = true; |
2324 | 4.58M | } |
2325 | | |
2326 | 2.41M | return false; |
2327 | 2.60M | } |
2328 | | |
2329 | | static Sema::TemplateDeductionResult |
2330 | | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
2331 | | ArrayRef<TemplateArgument> Ps, |
2332 | | ArrayRef<TemplateArgument> As, |
2333 | | TemplateDeductionInfo &Info, |
2334 | | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
2335 | 1.29M | bool NumberOfArgumentsMustMatch) { |
2336 | | // C++0x [temp.deduct.type]p9: |
2337 | | // If the template argument list of P contains a pack expansion that is not |
2338 | | // the last template argument, the entire template argument list is a |
2339 | | // non-deduced context. |
2340 | 1.29M | if (hasPackExpansionBeforeEnd(Ps)) |
2341 | 2 | return Sema::TDK_Success; |
2342 | | |
2343 | | // C++0x [temp.deduct.type]p9: |
2344 | | // If P has a form that contains <T> or <i>, then each argument Pi of the |
2345 | | // respective template argument list P is compared with the corresponding |
2346 | | // argument Ai of the corresponding template argument list of A. |
2347 | 1.29M | unsigned ArgIdx = 0, ParamIdx = 0; |
2348 | 2.92M | for (; hasTemplateArgumentForDeduction(Ps, ParamIdx); ++ParamIdx1.62M ) { |
2349 | 2.28M | const TemplateArgument &P = Ps[ParamIdx]; |
2350 | 2.28M | if (!P.isPackExpansion()) { |
2351 | | // The simple case: deduce template arguments by matching Pi and Ai. |
2352 | | |
2353 | | // Check whether we have enough arguments. |
2354 | 2.25M | if (!hasTemplateArgumentForDeduction(As, ArgIdx)) |
2355 | 13.4k | return NumberOfArgumentsMustMatch |
2356 | 13.4k | ? Sema::TDK_MiscellaneousDeductionFailure528 |
2357 | 13.4k | : Sema::TDK_Success12.9k ; |
2358 | | |
2359 | | // C++1z [temp.deduct.type]p9: |
2360 | | // During partial ordering, if Ai was originally a pack expansion [and] |
2361 | | // Pi is not a pack expansion, template argument deduction fails. |
2362 | 2.24M | if (As[ArgIdx].isPackExpansion()) |
2363 | 4.85k | return Sema::TDK_MiscellaneousDeductionFailure; |
2364 | | |
2365 | | // Perform deduction for this Pi/Ai pair. |
2366 | 2.24M | if (auto Result = DeduceTemplateArguments(S, TemplateParams, P, |
2367 | 2.24M | As[ArgIdx], Info, Deduced)) |
2368 | 641k | return Result; |
2369 | | |
2370 | | // Move to the next argument. |
2371 | 1.60M | ++ArgIdx; |
2372 | 1.60M | continue; |
2373 | 2.24M | } |
2374 | | |
2375 | | // The parameter is a pack expansion. |
2376 | | |
2377 | | // C++0x [temp.deduct.type]p9: |
2378 | | // If Pi is a pack expansion, then the pattern of Pi is compared with |
2379 | | // each remaining argument in the template argument list of A. Each |
2380 | | // comparison deduces template arguments for subsequent positions in the |
2381 | | // template parameter packs expanded by Pi. |
2382 | 29.2k | TemplateArgument Pattern = P.getPackExpansionPattern(); |
2383 | | |
2384 | | // Prepare to deduce the packs within the pattern. |
2385 | 29.2k | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
2386 | | |
2387 | | // Keep track of the deduced template arguments for each parameter pack |
2388 | | // expanded by this pack expansion (the outer index) and for each |
2389 | | // template argument (the inner SmallVectors). |
2390 | 152k | for (; hasTemplateArgumentForDeduction(As, ArgIdx) && |
2391 | 152k | PackScope.hasNextElement()123k ; |
2392 | 123k | ++ArgIdx123k ) { |
2393 | | // Deduce template arguments from the pattern. |
2394 | 123k | if (auto Result = DeduceTemplateArguments(S, TemplateParams, Pattern, |
2395 | 123k | As[ArgIdx], Info, Deduced)) |
2396 | 21 | return Result; |
2397 | | |
2398 | 123k | PackScope.nextPackElement(); |
2399 | 123k | } |
2400 | | |
2401 | | // Build argument packs for each of the parameter packs expanded by this |
2402 | | // pack expansion. |
2403 | 29.2k | if (auto Result = PackScope.finish()) |
2404 | 14 | return Result; |
2405 | 29.2k | } |
2406 | | |
2407 | 639k | return Sema::TDK_Success; |
2408 | 1.29M | } |
2409 | | |
2410 | | static Sema::TemplateDeductionResult |
2411 | | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
2412 | | const TemplateArgumentList &ParamList, |
2413 | | const TemplateArgumentList &ArgList, |
2414 | | TemplateDeductionInfo &Info, |
2415 | 702k | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
2416 | 702k | return DeduceTemplateArguments(S, TemplateParams, ParamList.asArray(), |
2417 | 702k | ArgList.asArray(), Info, Deduced, |
2418 | 702k | /*NumberOfArgumentsMustMatch=*/false); |
2419 | 702k | } |
2420 | | |
2421 | | /// Determine whether two template arguments are the same. |
2422 | | static bool isSameTemplateArg(ASTContext &Context, |
2423 | | TemplateArgument X, |
2424 | | const TemplateArgument &Y, |
2425 | 994k | bool PackExpansionMatchesPack = false) { |
2426 | | // If we're checking deduced arguments (X) against original arguments (Y), |
2427 | | // we will have flattened packs to non-expansions in X. |
2428 | 994k | if (PackExpansionMatchesPack && X.isPackExpansion()416k && !Y.isPackExpansion()5.38k ) |
2429 | 5.38k | X = X.getPackExpansionPattern(); |
2430 | | |
2431 | 994k | if (X.getKind() != Y.getKind()) |
2432 | 0 | return false; |
2433 | | |
2434 | 994k | switch (X.getKind()) { |
2435 | 0 | case TemplateArgument::Null: |
2436 | 0 | llvm_unreachable("Comparing NULL template argument"); |
2437 | |
|
2438 | 645k | case TemplateArgument::Type: |
2439 | 645k | return Context.getCanonicalType(X.getAsType()) == |
2440 | 645k | Context.getCanonicalType(Y.getAsType()); |
2441 | | |
2442 | 82 | case TemplateArgument::Declaration: |
2443 | 82 | return isSameDeclaration(X.getAsDecl(), Y.getAsDecl()); |
2444 | | |
2445 | 6 | case TemplateArgument::NullPtr: |
2446 | 6 | return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()); |
2447 | | |
2448 | 727 | case TemplateArgument::Template: |
2449 | 727 | case TemplateArgument::TemplateExpansion: |
2450 | 727 | return Context.getCanonicalTemplateName( |
2451 | 727 | X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == |
2452 | 727 | Context.getCanonicalTemplateName( |
2453 | 727 | Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); |
2454 | | |
2455 | 261k | case TemplateArgument::Integral: |
2456 | 261k | return hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()); |
2457 | | |
2458 | 45.3k | case TemplateArgument::Expression: { |
2459 | 45.3k | llvm::FoldingSetNodeID XID, YID; |
2460 | 45.3k | X.getAsExpr()->Profile(XID, Context, true); |
2461 | 45.3k | Y.getAsExpr()->Profile(YID, Context, true); |
2462 | 45.3k | return XID == YID; |
2463 | 727 | } |
2464 | | |
2465 | 41.7k | case TemplateArgument::Pack: |
2466 | 41.7k | if (X.pack_size() != Y.pack_size()) |
2467 | 8.48k | return false; |
2468 | | |
2469 | 33.2k | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
2470 | 33.2k | XPEnd = X.pack_end(), |
2471 | 33.2k | YP = Y.pack_begin(); |
2472 | 92.4k | XP != XPEnd; ++XP, ++YP59.1k ) |
2473 | 59.1k | if (!isSameTemplateArg(Context, *XP, *YP, PackExpansionMatchesPack)) |
2474 | 0 | return false; |
2475 | | |
2476 | 33.2k | return true; |
2477 | 994k | } |
2478 | | |
2479 | 0 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
2480 | 0 | } |
2481 | | |
2482 | | /// Allocate a TemplateArgumentLoc where all locations have |
2483 | | /// been initialized to the given location. |
2484 | | /// |
2485 | | /// \param Arg The template argument we are producing template argument |
2486 | | /// location information for. |
2487 | | /// |
2488 | | /// \param NTTPType For a declaration template argument, the type of |
2489 | | /// the non-type template parameter that corresponds to this template |
2490 | | /// argument. Can be null if no type sugar is available to add to the |
2491 | | /// type from the template argument. |
2492 | | /// |
2493 | | /// \param Loc The source location to use for the resulting template |
2494 | | /// argument. |
2495 | | TemplateArgumentLoc |
2496 | | Sema::getTrivialTemplateArgumentLoc(const TemplateArgument &Arg, |
2497 | 1.52M | QualType NTTPType, SourceLocation Loc) { |
2498 | 1.52M | switch (Arg.getKind()) { |
2499 | 0 | case TemplateArgument::Null: |
2500 | 0 | llvm_unreachable("Can't get a NULL template argument here"); |
2501 | |
|
2502 | 1.32M | case TemplateArgument::Type: |
2503 | 1.32M | return TemplateArgumentLoc( |
2504 | 1.32M | Arg, Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); |
2505 | | |
2506 | 127 | case TemplateArgument::Declaration: { |
2507 | 127 | if (NTTPType.isNull()) |
2508 | 127 | NTTPType = Arg.getParamTypeForDecl(); |
2509 | 127 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
2510 | 127 | .getAs<Expr>(); |
2511 | 127 | return TemplateArgumentLoc(TemplateArgument(E), E); |
2512 | 0 | } |
2513 | | |
2514 | 1 | case TemplateArgument::NullPtr: { |
2515 | 1 | if (NTTPType.isNull()) |
2516 | 1 | NTTPType = Arg.getNullPtrType(); |
2517 | 1 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
2518 | 1 | .getAs<Expr>(); |
2519 | 1 | return TemplateArgumentLoc(TemplateArgument(NTTPType, /*isNullPtr*/true), |
2520 | 1 | E); |
2521 | 0 | } |
2522 | | |
2523 | 154k | case TemplateArgument::Integral: { |
2524 | 154k | Expr *E = |
2525 | 154k | BuildExpressionFromIntegralTemplateArgument(Arg, Loc).getAs<Expr>(); |
2526 | 154k | return TemplateArgumentLoc(TemplateArgument(E), E); |
2527 | 0 | } |
2528 | | |
2529 | 2.28k | case TemplateArgument::Template: |
2530 | 2.29k | case TemplateArgument::TemplateExpansion: { |
2531 | 2.29k | NestedNameSpecifierLocBuilder Builder; |
2532 | 2.29k | TemplateName Template = Arg.getAsTemplateOrTemplatePattern(); |
2533 | 2.29k | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) |
2534 | 0 | Builder.MakeTrivial(Context, DTN->getQualifier(), Loc); |
2535 | 2.29k | else if (QualifiedTemplateName *QTN = |
2536 | 2.29k | Template.getAsQualifiedTemplateName()) |
2537 | 0 | Builder.MakeTrivial(Context, QTN->getQualifier(), Loc); |
2538 | | |
2539 | 2.29k | if (Arg.getKind() == TemplateArgument::Template) |
2540 | 2.28k | return TemplateArgumentLoc(Context, Arg, |
2541 | 2.28k | Builder.getWithLocInContext(Context), Loc); |
2542 | | |
2543 | 6 | return TemplateArgumentLoc( |
2544 | 6 | Context, Arg, Builder.getWithLocInContext(Context), Loc, Loc); |
2545 | 2.29k | } |
2546 | | |
2547 | 45.6k | case TemplateArgument::Expression: |
2548 | 45.6k | return TemplateArgumentLoc(Arg, Arg.getAsExpr()); |
2549 | | |
2550 | 10 | case TemplateArgument::Pack: |
2551 | 10 | return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); |
2552 | 1.52M | } |
2553 | | |
2554 | 0 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
2555 | 0 | } |
2556 | | |
2557 | | TemplateArgumentLoc |
2558 | | Sema::getIdentityTemplateArgumentLoc(NamedDecl *TemplateParm, |
2559 | 164 | SourceLocation Location) { |
2560 | 164 | return getTrivialTemplateArgumentLoc( |
2561 | 164 | Context.getInjectedTemplateArg(TemplateParm), QualType(), Location); |
2562 | 164 | } |
2563 | | |
2564 | | /// Convert the given deduced template argument and add it to the set of |
2565 | | /// fully-converted template arguments. |
2566 | | static bool |
2567 | | ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param, |
2568 | | DeducedTemplateArgument Arg, |
2569 | | NamedDecl *Template, |
2570 | | TemplateDeductionInfo &Info, |
2571 | | bool IsDeduced, |
2572 | 1.37M | SmallVectorImpl<TemplateArgument> &Output) { |
2573 | 1.37M | auto ConvertArg = [&](DeducedTemplateArgument Arg, |
2574 | 1.49M | unsigned ArgumentPackIndex) { |
2575 | | // Convert the deduced template argument into a template |
2576 | | // argument that we can check, almost as if the user had written |
2577 | | // the template argument explicitly. |
2578 | 1.49M | TemplateArgumentLoc ArgLoc = |
2579 | 1.49M | S.getTrivialTemplateArgumentLoc(Arg, QualType(), Info.getLocation()); |
2580 | | |
2581 | | // Check the template argument, converting it as necessary. |
2582 | 1.49M | return S.CheckTemplateArgument( |
2583 | 1.49M | Param, ArgLoc, Template, Template->getLocation(), |
2584 | 1.49M | Template->getSourceRange().getEnd(), ArgumentPackIndex, Output, |
2585 | 1.49M | IsDeduced |
2586 | 1.49M | ? (1.02M Arg.wasDeducedFromArrayBound()1.02M ? Sema::CTAK_DeducedFromArrayBound646 |
2587 | 1.02M | : Sema::CTAK_Deduced1.02M ) |
2588 | 1.49M | : Sema::CTAK_Specified468k ); |
2589 | 1.49M | }; |
2590 | | |
2591 | 1.37M | if (Arg.getKind() == TemplateArgument::Pack) { |
2592 | | // This is a template argument pack, so check each of its arguments against |
2593 | | // the template parameter. |
2594 | 67.2k | SmallVector<TemplateArgument, 2> PackedArgsBuilder; |
2595 | 184k | for (const auto &P : Arg.pack_elements()) { |
2596 | | // When converting the deduced template argument, append it to the |
2597 | | // general output list. We need to do this so that the template argument |
2598 | | // checking logic has all of the prior template arguments available. |
2599 | 184k | DeducedTemplateArgument InnerArg(P); |
2600 | 184k | InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); |
2601 | 184k | assert(InnerArg.getKind() != TemplateArgument::Pack && |
2602 | 184k | "deduced nested pack"); |
2603 | 184k | if (P.isNull()) { |
2604 | | // We deduced arguments for some elements of this pack, but not for |
2605 | | // all of them. This happens if we get a conditionally-non-deduced |
2606 | | // context in a pack expansion (such as an overload set in one of the |
2607 | | // arguments). |
2608 | 25 | S.Diag(Param->getLocation(), |
2609 | 25 | diag::err_template_arg_deduced_incomplete_pack) |
2610 | 25 | << Arg << Param; |
2611 | 25 | return true; |
2612 | 25 | } |
2613 | 184k | if (ConvertArg(InnerArg, PackedArgsBuilder.size())) |
2614 | 2 | return true; |
2615 | | |
2616 | | // Move the converted template argument into our argument pack. |
2617 | 184k | PackedArgsBuilder.push_back(Output.pop_back_val()); |
2618 | 184k | } |
2619 | | |
2620 | | // If the pack is empty, we still need to substitute into the parameter |
2621 | | // itself, in case that substitution fails. |
2622 | 67.1k | if (PackedArgsBuilder.empty()) { |
2623 | 9.21k | LocalInstantiationScope Scope(S); |
2624 | 9.21k | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Output); |
2625 | 9.21k | MultiLevelTemplateArgumentList Args(TemplateArgs); |
2626 | | |
2627 | 9.21k | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
2628 | 1.28k | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
2629 | 1.28k | NTTP, Output, |
2630 | 1.28k | Template->getSourceRange()); |
2631 | 1.28k | if (Inst.isInvalid() || |
2632 | 1.28k | S.SubstType(NTTP->getType(), Args, NTTP->getLocation(), |
2633 | 1.28k | NTTP->getDeclName()).isNull()) |
2634 | 2 | return true; |
2635 | 7.93k | } else if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
2636 | 14 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
2637 | 14 | TTP, Output, |
2638 | 14 | Template->getSourceRange()); |
2639 | 14 | if (Inst.isInvalid() || !S.SubstDecl(TTP, S.CurContext, Args)) |
2640 | 4 | return true; |
2641 | 14 | } |
2642 | | // For type parameters, no substitution is ever required. |
2643 | 9.21k | } |
2644 | | |
2645 | | // Create the resulting argument pack. |
2646 | 67.1k | Output.push_back( |
2647 | 67.1k | TemplateArgument::CreatePackCopy(S.Context, PackedArgsBuilder)); |
2648 | 67.1k | return false; |
2649 | 67.1k | } |
2650 | | |
2651 | 1.30M | return ConvertArg(Arg, 0); |
2652 | 1.37M | } |
2653 | | |
2654 | | // FIXME: This should not be a template, but |
2655 | | // ClassTemplatePartialSpecializationDecl sadly does not derive from |
2656 | | // TemplateDecl. |
2657 | | template<typename TemplateDeclT> |
2658 | | static Sema::TemplateDeductionResult ConvertDeducedTemplateArguments( |
2659 | | Sema &S, TemplateDeclT *Template, bool IsDeduced, |
2660 | | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
2661 | | TemplateDeductionInfo &Info, SmallVectorImpl<TemplateArgument> &Builder, |
2662 | | LocalInstantiationScope *CurrentInstantiationScope = nullptr, |
2663 | 1.19M | unsigned NumAlreadyConverted = 0, bool PartialOverloading = false) { |
2664 | 1.19M | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
2665 | | |
2666 | 3.05M | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I1.85M ) { |
2667 | 1.93M | NamedDecl *Param = TemplateParams->getParam(I); |
2668 | | |
2669 | | // C++0x [temp.arg.explicit]p3: |
2670 | | // A trailing template parameter pack (14.5.3) not otherwise deduced will |
2671 | | // be deduced to an empty sequence of template arguments. |
2672 | | // FIXME: Where did the word "trailing" come from? |
2673 | 1.93M | if (Deduced[I].isNull() && Param->isTemplateParameterPack()214k ) { |
2674 | 25.2k | if (auto Result = |
2675 | 25.2k | PackDeductionScope(S, TemplateParams, Deduced, Info, I).finish()) |
2676 | 8 | return Result; |
2677 | 25.2k | } |
2678 | | |
2679 | 1.93M | if (!Deduced[I].isNull()) { |
2680 | 1.74M | if (I < NumAlreadyConverted) { |
2681 | | // We may have had explicitly-specified template arguments for a |
2682 | | // template parameter pack (that may or may not have been extended |
2683 | | // via additional deduced arguments). |
2684 | 400k | if (Param->isParameterPack() && CurrentInstantiationScope25.4k && |
2685 | 400k | CurrentInstantiationScope->getPartiallySubstitutedPack() == Param25.4k ) { |
2686 | | // Forget the partially-substituted pack; its substitution is now |
2687 | | // complete. |
2688 | 25.4k | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
2689 | | // We still need to check the argument in case it was extended by |
2690 | | // deduction. |
2691 | 375k | } else { |
2692 | | // We have already fully type-checked and converted this |
2693 | | // argument, because it was explicitly-specified. Just record the |
2694 | | // presence of this argument. |
2695 | 375k | Builder.push_back(Deduced[I]); |
2696 | 375k | continue; |
2697 | 375k | } |
2698 | 400k | } |
2699 | | |
2700 | | // We may have deduced this argument, so it still needs to be |
2701 | | // checked and converted. |
2702 | 1.37M | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Template, Info, |
2703 | 1.37M | IsDeduced, Builder)) { |
2704 | 90 | Info.Param = makeTemplateParameter(Param); |
2705 | | // FIXME: These template arguments are temporary. Free them! |
2706 | 90 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
2707 | 90 | return Sema::TDK_SubstitutionFailure; |
2708 | 90 | } |
2709 | | |
2710 | 1.37M | continue; |
2711 | 1.37M | } |
2712 | | |
2713 | | // Substitute into the default template argument, if available. |
2714 | 189k | bool HasDefaultArg = false; |
2715 | 189k | TemplateDecl *TD = dyn_cast<TemplateDecl>(Template); |
2716 | 189k | if (!TD) { |
2717 | 12.9k | assert(isa<ClassTemplatePartialSpecializationDecl>(Template) || |
2718 | 12.9k | isa<VarTemplatePartialSpecializationDecl>(Template)); |
2719 | 0 | return Sema::TDK_Incomplete; |
2720 | 12.9k | } |
2721 | | |
2722 | 176k | TemplateArgumentLoc DefArg; |
2723 | 176k | { |
2724 | 176k | Qualifiers ThisTypeQuals; |
2725 | 176k | CXXRecordDecl *ThisContext = nullptr; |
2726 | 176k | if (auto *Rec = dyn_cast<CXXRecordDecl>(TD->getDeclContext())) |
2727 | 144k | if (Rec->isLambda()) |
2728 | 20 | if (auto *Method = dyn_cast<CXXMethodDecl>(Rec->getDeclContext())) { |
2729 | 6 | ThisContext = Method->getParent(); |
2730 | 6 | ThisTypeQuals = Method->getMethodQualifiers(); |
2731 | 6 | } |
2732 | | |
2733 | 176k | Sema::CXXThisScopeRAII ThisScope(S, ThisContext, ThisTypeQuals, |
2734 | 176k | S.getLangOpts().CPlusPlus17); |
2735 | | |
2736 | 176k | DefArg = S.SubstDefaultTemplateArgumentIfAvailable( |
2737 | 176k | TD, TD->getLocation(), TD->getSourceRange().getEnd(), Param, Builder, |
2738 | 176k | HasDefaultArg); |
2739 | 176k | } |
2740 | | |
2741 | | // If there was no default argument, deduction is incomplete. |
2742 | 176k | if (DefArg.getArgument().isNull()) { |
2743 | 46.3k | Info.Param = makeTemplateParameter( |
2744 | 46.3k | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
2745 | 46.3k | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
2746 | 46.3k | if (PartialOverloading) break66 ; |
2747 | | |
2748 | 46.2k | return HasDefaultArg ? Sema::TDK_SubstitutionFailure44.7k |
2749 | 46.2k | : Sema::TDK_Incomplete1.54k ; |
2750 | 46.3k | } |
2751 | | |
2752 | | // Check whether we can actually use the default argument. |
2753 | 130k | if (S.CheckTemplateArgument(Param, DefArg, TD, TD->getLocation(), |
2754 | 130k | TD->getSourceRange().getEnd(), 0, Builder, |
2755 | 130k | Sema::CTAK_Specified)) { |
2756 | 23.8k | Info.Param = makeTemplateParameter( |
2757 | 23.8k | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
2758 | | // FIXME: These template arguments are temporary. Free them! |
2759 | 23.8k | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
2760 | 23.8k | return Sema::TDK_SubstitutionFailure; |
2761 | 23.8k | } |
2762 | | |
2763 | | // If we get here, we successfully used the default template argument. |
2764 | 130k | } |
2765 | | |
2766 | 1.11M | return Sema::TDK_Success; |
2767 | 1.19M | } SemaTemplateDeduction.cpp:clang::Sema::TemplateDeductionResult ConvertDeducedTemplateArguments<clang::ClassTemplatePartialSpecializationDecl>(clang::Sema&, clang::ClassTemplatePartialSpecializationDecl*, bool, llvm::SmallVectorImpl<clang::DeducedTemplateArgument>&, clang::sema::TemplateDeductionInfo&, llvm::SmallVectorImpl<clang::TemplateArgument>&, clang::LocalInstantiationScope*, unsigned int, bool) Line | Count | Source | 2663 | 283k | unsigned NumAlreadyConverted = 0, bool PartialOverloading = false) { | 2664 | 283k | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); | 2665 | | | 2666 | 719k | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I436k ) { | 2667 | 449k | NamedDecl *Param = TemplateParams->getParam(I); | 2668 | | | 2669 | | // C++0x [temp.arg.explicit]p3: | 2670 | | // A trailing template parameter pack (14.5.3) not otherwise deduced will | 2671 | | // be deduced to an empty sequence of template arguments. | 2672 | | // FIXME: Where did the word "trailing" come from? | 2673 | 449k | if (Deduced[I].isNull() && Param->isTemplateParameterPack()12.9k ) { | 2674 | 6 | if (auto Result = | 2675 | 6 | PackDeductionScope(S, TemplateParams, Deduced, Info, I).finish()) | 2676 | 0 | return Result; | 2677 | 6 | } | 2678 | | | 2679 | 449k | if (!Deduced[I].isNull()) { | 2680 | 436k | if (I < NumAlreadyConverted) { | 2681 | | // We may have had explicitly-specified template arguments for a | 2682 | | // template parameter pack (that may or may not have been extended | 2683 | | // via additional deduced arguments). | 2684 | 0 | if (Param->isParameterPack() && CurrentInstantiationScope && | 2685 | 0 | CurrentInstantiationScope->getPartiallySubstitutedPack() == Param) { | 2686 | | // Forget the partially-substituted pack; its substitution is now | 2687 | | // complete. | 2688 | 0 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); | 2689 | | // We still need to check the argument in case it was extended by | 2690 | | // deduction. | 2691 | 0 | } else { | 2692 | | // We have already fully type-checked and converted this | 2693 | | // argument, because it was explicitly-specified. Just record the | 2694 | | // presence of this argument. | 2695 | 0 | Builder.push_back(Deduced[I]); | 2696 | 0 | continue; | 2697 | 0 | } | 2698 | 0 | } | 2699 | | | 2700 | | // We may have deduced this argument, so it still needs to be | 2701 | | // checked and converted. | 2702 | 436k | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Template, Info, | 2703 | 436k | IsDeduced, Builder)) { | 2704 | 45 | Info.Param = makeTemplateParameter(Param); | 2705 | | // FIXME: These template arguments are temporary. Free them! | 2706 | 45 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); | 2707 | 45 | return Sema::TDK_SubstitutionFailure; | 2708 | 45 | } | 2709 | | | 2710 | 436k | continue; | 2711 | 436k | } | 2712 | | | 2713 | | // Substitute into the default template argument, if available. | 2714 | 12.9k | bool HasDefaultArg = false; | 2715 | 12.9k | TemplateDecl *TD = dyn_cast<TemplateDecl>(Template); | 2716 | 12.9k | if (!TD) { | 2717 | 12.9k | assert(isa<ClassTemplatePartialSpecializationDecl>(Template) || | 2718 | 12.9k | isa<VarTemplatePartialSpecializationDecl>(Template)); | 2719 | 0 | return Sema::TDK_Incomplete; | 2720 | 12.9k | } | 2721 | | | 2722 | 0 | TemplateArgumentLoc DefArg; | 2723 | 0 | { | 2724 | 0 | Qualifiers ThisTypeQuals; | 2725 | 0 | CXXRecordDecl *ThisContext = nullptr; | 2726 | 0 | if (auto *Rec = dyn_cast<CXXRecordDecl>(TD->getDeclContext())) | 2727 | 0 | if (Rec->isLambda()) | 2728 | 0 | if (auto *Method = dyn_cast<CXXMethodDecl>(Rec->getDeclContext())) { | 2729 | 0 | ThisContext = Method->getParent(); | 2730 | 0 | ThisTypeQuals = Method->getMethodQualifiers(); | 2731 | 0 | } | 2732 | |
| 2733 | 0 | Sema::CXXThisScopeRAII ThisScope(S, ThisContext, ThisTypeQuals, | 2734 | 0 | S.getLangOpts().CPlusPlus17); | 2735 | |
| 2736 | 0 | DefArg = S.SubstDefaultTemplateArgumentIfAvailable( | 2737 | 0 | TD, TD->getLocation(), TD->getSourceRange().getEnd(), Param, Builder, | 2738 | 0 | HasDefaultArg); | 2739 | 0 | } | 2740 | | | 2741 | | // If there was no default argument, deduction is incomplete. | 2742 | 0 | if (DefArg.getArgument().isNull()) { | 2743 | 0 | Info.Param = makeTemplateParameter( | 2744 | 0 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); | 2745 | 0 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); | 2746 | 0 | if (PartialOverloading) break; | 2747 | | | 2748 | 0 | return HasDefaultArg ? Sema::TDK_SubstitutionFailure | 2749 | 0 | : Sema::TDK_Incomplete; | 2750 | 0 | } | 2751 | | | 2752 | | // Check whether we can actually use the default argument. | 2753 | 0 | if (S.CheckTemplateArgument(Param, DefArg, TD, TD->getLocation(), | 2754 | 0 | TD->getSourceRange().getEnd(), 0, Builder, | 2755 | 0 | Sema::CTAK_Specified)) { | 2756 | 0 | Info.Param = makeTemplateParameter( | 2757 | 0 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); | 2758 | | // FIXME: These template arguments are temporary. Free them! | 2759 | 0 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); | 2760 | 0 | return Sema::TDK_SubstitutionFailure; | 2761 | 0 | } | 2762 | | | 2763 | | // If we get here, we successfully used the default template argument. | 2764 | 0 | } | 2765 | | | 2766 | 270k | return Sema::TDK_Success; | 2767 | 283k | } |
SemaTemplateDeduction.cpp:clang::Sema::TemplateDeductionResult ConvertDeducedTemplateArguments<clang::VarTemplatePartialSpecializationDecl>(clang::Sema&, clang::VarTemplatePartialSpecializationDecl*, bool, llvm::SmallVectorImpl<clang::DeducedTemplateArgument>&, clang::sema::TemplateDeductionInfo&, llvm::SmallVectorImpl<clang::TemplateArgument>&, clang::LocalInstantiationScope*, unsigned int, bool) Line | Count | Source | 2663 | 927 | unsigned NumAlreadyConverted = 0, bool PartialOverloading = false) { | 2664 | 927 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); | 2665 | | | 2666 | 1.92k | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I996 ) { | 2667 | 997 | NamedDecl *Param = TemplateParams->getParam(I); | 2668 | | | 2669 | | // C++0x [temp.arg.explicit]p3: | 2670 | | // A trailing template parameter pack (14.5.3) not otherwise deduced will | 2671 | | // be deduced to an empty sequence of template arguments. | 2672 | | // FIXME: Where did the word "trailing" come from? | 2673 | 997 | if (Deduced[I].isNull() && Param->isTemplateParameterPack()1 ) { | 2674 | 0 | if (auto Result = | 2675 | 0 | PackDeductionScope(S, TemplateParams, Deduced, Info, I).finish()) | 2676 | 0 | return Result; | 2677 | 0 | } | 2678 | | | 2679 | 997 | if (!Deduced[I].isNull()) { | 2680 | 996 | if (I < NumAlreadyConverted) { | 2681 | | // We may have had explicitly-specified template arguments for a | 2682 | | // template parameter pack (that may or may not have been extended | 2683 | | // via additional deduced arguments). | 2684 | 0 | if (Param->isParameterPack() && CurrentInstantiationScope && | 2685 | 0 | CurrentInstantiationScope->getPartiallySubstitutedPack() == Param) { | 2686 | | // Forget the partially-substituted pack; its substitution is now | 2687 | | // complete. | 2688 | 0 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); | 2689 | | // We still need to check the argument in case it was extended by | 2690 | | // deduction. | 2691 | 0 | } else { | 2692 | | // We have already fully type-checked and converted this | 2693 | | // argument, because it was explicitly-specified. Just record the | 2694 | | // presence of this argument. | 2695 | 0 | Builder.push_back(Deduced[I]); | 2696 | 0 | continue; | 2697 | 0 | } | 2698 | 0 | } | 2699 | | | 2700 | | // We may have deduced this argument, so it still needs to be | 2701 | | // checked and converted. | 2702 | 996 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Template, Info, | 2703 | 996 | IsDeduced, Builder)) { | 2704 | 0 | Info.Param = makeTemplateParameter(Param); | 2705 | | // FIXME: These template arguments are temporary. Free them! | 2706 | 0 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); | 2707 | 0 | return Sema::TDK_SubstitutionFailure; | 2708 | 0 | } | 2709 | | | 2710 | 996 | continue; | 2711 | 996 | } | 2712 | | | 2713 | | // Substitute into the default template argument, if available. | 2714 | 1 | bool HasDefaultArg = false; | 2715 | 1 | TemplateDecl *TD = dyn_cast<TemplateDecl>(Template); | 2716 | 1 | if (!TD) { | 2717 | 1 | assert(isa<ClassTemplatePartialSpecializationDecl>(Template) || | 2718 | 1 | isa<VarTemplatePartialSpecializationDecl>(Template)); | 2719 | 0 | return Sema::TDK_Incomplete; | 2720 | 1 | } | 2721 | | | 2722 | 0 | TemplateArgumentLoc DefArg; | 2723 | 0 | { | 2724 | 0 | Qualifiers ThisTypeQuals; | 2725 | 0 | CXXRecordDecl *ThisContext = nullptr; | 2726 | 0 | if (auto *Rec = dyn_cast<CXXRecordDecl>(TD->getDeclContext())) | 2727 | 0 | if (Rec->isLambda()) | 2728 | 0 | if (auto *Method = dyn_cast<CXXMethodDecl>(Rec->getDeclContext())) { | 2729 | 0 | ThisContext = Method->getParent(); | 2730 | 0 | ThisTypeQuals = Method->getMethodQualifiers(); | 2731 | 0 | } | 2732 | |
| 2733 | 0 | Sema::CXXThisScopeRAII ThisScope(S, ThisContext, ThisTypeQuals, | 2734 | 0 | S.getLangOpts().CPlusPlus17); | 2735 | |
| 2736 | 0 | DefArg = S.SubstDefaultTemplateArgumentIfAvailable( | 2737 | 0 | TD, TD->getLocation(), TD->getSourceRange().getEnd(), Param, Builder, | 2738 | 0 | HasDefaultArg); | 2739 | 0 | } | 2740 | | | 2741 | | // If there was no default argument, deduction is incomplete. | 2742 | 0 | if (DefArg.getArgument().isNull()) { | 2743 | 0 | Info.Param = makeTemplateParameter( | 2744 | 0 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); | 2745 | 0 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); | 2746 | 0 | if (PartialOverloading) break; | 2747 | | | 2748 | 0 | return HasDefaultArg ? Sema::TDK_SubstitutionFailure | 2749 | 0 | : Sema::TDK_Incomplete; | 2750 | 0 | } | 2751 | | | 2752 | | // Check whether we can actually use the default argument. | 2753 | 0 | if (S.CheckTemplateArgument(Param, DefArg, TD, TD->getLocation(), | 2754 | 0 | TD->getSourceRange().getEnd(), 0, Builder, | 2755 | 0 | Sema::CTAK_Specified)) { | 2756 | 0 | Info.Param = makeTemplateParameter( | 2757 | 0 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); | 2758 | | // FIXME: These template arguments are temporary. Free them! | 2759 | 0 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); | 2760 | 0 | return Sema::TDK_SubstitutionFailure; | 2761 | 0 | } | 2762 | | | 2763 | | // If we get here, we successfully used the default template argument. | 2764 | 0 | } | 2765 | | | 2766 | 926 | return Sema::TDK_Success; | 2767 | 927 | } |
SemaTemplateDeduction.cpp:clang::Sema::TemplateDeductionResult ConvertDeducedTemplateArguments<clang::FunctionTemplateDecl>(clang::Sema&, clang::FunctionTemplateDecl*, bool, llvm::SmallVectorImpl<clang::DeducedTemplateArgument>&, clang::sema::TemplateDeductionInfo&, llvm::SmallVectorImpl<clang::TemplateArgument>&, clang::LocalInstantiationScope*, unsigned int, bool) Line | Count | Source | 2663 | 727k | unsigned NumAlreadyConverted = 0, bool PartialOverloading = false) { | 2664 | 727k | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); | 2665 | | | 2666 | 1.74M | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I1.01M ) { | 2667 | 1.08M | NamedDecl *Param = TemplateParams->getParam(I); | 2668 | | | 2669 | | // C++0x [temp.arg.explicit]p3: | 2670 | | // A trailing template parameter pack (14.5.3) not otherwise deduced will | 2671 | | // be deduced to an empty sequence of template arguments. | 2672 | | // FIXME: Where did the word "trailing" come from? | 2673 | 1.08M | if (Deduced[I].isNull() && Param->isTemplateParameterPack()201k ) { | 2674 | 25.2k | if (auto Result = | 2675 | 25.2k | PackDeductionScope(S, TemplateParams, Deduced, Info, I).finish()) | 2676 | 8 | return Result; | 2677 | 25.2k | } | 2678 | | | 2679 | 1.08M | if (!Deduced[I].isNull()) { | 2680 | 908k | if (I < NumAlreadyConverted) { | 2681 | | // We may have had explicitly-specified template arguments for a | 2682 | | // template parameter pack (that may or may not have been extended | 2683 | | // via additional deduced arguments). | 2684 | 400k | if (Param->isParameterPack() && CurrentInstantiationScope25.4k && | 2685 | 400k | CurrentInstantiationScope->getPartiallySubstitutedPack() == Param25.4k ) { | 2686 | | // Forget the partially-substituted pack; its substitution is now | 2687 | | // complete. | 2688 | 25.4k | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); | 2689 | | // We still need to check the argument in case it was extended by | 2690 | | // deduction. | 2691 | 375k | } else { | 2692 | | // We have already fully type-checked and converted this | 2693 | | // argument, because it was explicitly-specified. Just record the | 2694 | | // presence of this argument. | 2695 | 375k | Builder.push_back(Deduced[I]); | 2696 | 375k | continue; | 2697 | 375k | } | 2698 | 400k | } | 2699 | | | 2700 | | // We may have deduced this argument, so it still needs to be | 2701 | | // checked and converted. | 2702 | 533k | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Template, Info, | 2703 | 533k | IsDeduced, Builder)) { | 2704 | 42 | Info.Param = makeTemplateParameter(Param); | 2705 | | // FIXME: These template arguments are temporary. Free them! | 2706 | 42 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); | 2707 | 42 | return Sema::TDK_SubstitutionFailure; | 2708 | 42 | } | 2709 | | | 2710 | 533k | continue; | 2711 | 533k | } | 2712 | | | 2713 | | // Substitute into the default template argument, if available. | 2714 | 176k | bool HasDefaultArg = false; | 2715 | 176k | TemplateDecl *TD = dyn_cast<TemplateDecl>(Template); | 2716 | 176k | if (!TD) { | 2717 | 0 | assert(isa<ClassTemplatePartialSpecializationDecl>(Template) || | 2718 | 0 | isa<VarTemplatePartialSpecializationDecl>(Template)); | 2719 | 0 | return Sema::TDK_Incomplete; | 2720 | 0 | } | 2721 | | | 2722 | 176k | TemplateArgumentLoc DefArg; | 2723 | 176k | { | 2724 | 176k | Qualifiers ThisTypeQuals; | 2725 | 176k | CXXRecordDecl *ThisContext = nullptr; | 2726 | 176k | if (auto *Rec = dyn_cast<CXXRecordDecl>(TD->getDeclContext())) | 2727 | 144k | if (Rec->isLambda()) | 2728 | 20 | if (auto *Method = dyn_cast<CXXMethodDecl>(Rec->getDeclContext())) { | 2729 | 6 | ThisContext = Method->getParent(); | 2730 | 6 | ThisTypeQuals = Method->getMethodQualifiers(); | 2731 | 6 | } | 2732 | | | 2733 | 176k | Sema::CXXThisScopeRAII ThisScope(S, ThisContext, ThisTypeQuals, | 2734 | 176k | S.getLangOpts().CPlusPlus17); | 2735 | | | 2736 | 176k | DefArg = S.SubstDefaultTemplateArgumentIfAvailable( | 2737 | 176k | TD, TD->getLocation(), TD->getSourceRange().getEnd(), Param, Builder, | 2738 | 176k | HasDefaultArg); | 2739 | 176k | } | 2740 | | | 2741 | | // If there was no default argument, deduction is incomplete. | 2742 | 176k | if (DefArg.getArgument().isNull()) { | 2743 | 46.3k | Info.Param = makeTemplateParameter( | 2744 | 46.3k | const_cast<NamedDecl *>(TemplateParams->getParam(I))); | 2745 | 46.3k | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); | 2746 | 46.3k | if (PartialOverloading) break66 ; | 2747 | | | 2748 | 46.2k | return HasDefaultArg ? Sema::TDK_SubstitutionFailure44.7k | 2749 | 46.2k | : Sema::TDK_Incomplete1.54k ; | 2750 | 46.3k | } | 2751 | | | 2752 | | // Check whether we can actually use the default argument. | 2753 | 130k | if (S.CheckTemplateArgument(Param, DefArg, TD, TD->getLocation(), | 2754 | 130k | TD->getSourceRange().getEnd(), 0, Builder, | 2755 | 130k | Sema::CTAK_Specified)) { | 2756 | 23.8k | Info.Param = makeTemplateParameter( | 2757 | 23.8k | const_cast<NamedDecl *>(TemplateParams->getParam(I))); | 2758 | | // FIXME: These template arguments are temporary. Free them! | 2759 | 23.8k | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); | 2760 | 23.8k | return Sema::TDK_SubstitutionFailure; | 2761 | 23.8k | } | 2762 | | | 2763 | | // If we get here, we successfully used the default template argument. | 2764 | 130k | } | 2765 | | | 2766 | 657k | return Sema::TDK_Success; | 2767 | 727k | } |
SemaTemplateDeduction.cpp:clang::Sema::TemplateDeductionResult ConvertDeducedTemplateArguments<clang::TemplateDecl>(clang::Sema&, clang::TemplateDecl*, bool, llvm::SmallVectorImpl<clang::DeducedTemplateArgument>&, clang::sema::TemplateDeductionInfo&, llvm::SmallVectorImpl<clang::TemplateArgument>&, clang::LocalInstantiationScope*, unsigned int, bool) Line | Count | Source | 2663 | 187k | unsigned NumAlreadyConverted = 0, bool PartialOverloading = false) { | 2664 | 187k | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); | 2665 | | | 2666 | 589k | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I402k ) { | 2667 | 402k | NamedDecl *Param = TemplateParams->getParam(I); | 2668 | | | 2669 | | // C++0x [temp.arg.explicit]p3: | 2670 | | // A trailing template parameter pack (14.5.3) not otherwise deduced will | 2671 | | // be deduced to an empty sequence of template arguments. | 2672 | | // FIXME: Where did the word "trailing" come from? | 2673 | 402k | if (Deduced[I].isNull() && Param->isTemplateParameterPack()0 ) { | 2674 | 0 | if (auto Result = | 2675 | 0 | PackDeductionScope(S, TemplateParams, Deduced, Info, I).finish()) | 2676 | 0 | return Result; | 2677 | 0 | } | 2678 | | | 2679 | 402k | if (!Deduced[I].isNull()) { | 2680 | 402k | if (I < NumAlreadyConverted) { | 2681 | | // We may have had explicitly-specified template arguments for a | 2682 | | // template parameter pack (that may or may not have been extended | 2683 | | // via additional deduced arguments). | 2684 | 0 | if (Param->isParameterPack() && CurrentInstantiationScope && | 2685 | 0 | CurrentInstantiationScope->getPartiallySubstitutedPack() == Param) { | 2686 | | // Forget the partially-substituted pack; its substitution is now | 2687 | | // complete. | 2688 | 0 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); | 2689 | | // We still need to check the argument in case it was extended by | 2690 | | // deduction. | 2691 | 0 | } else { | 2692 | | // We have already fully type-checked and converted this | 2693 | | // argument, because it was explicitly-specified. Just record the | 2694 | | // presence of this argument. | 2695 | 0 | Builder.push_back(Deduced[I]); | 2696 | 0 | continue; | 2697 | 0 | } | 2698 | 0 | } | 2699 | | | 2700 | | // We may have deduced this argument, so it still needs to be | 2701 | | // checked and converted. | 2702 | 402k | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Template, Info, | 2703 | 402k | IsDeduced, Builder)) { | 2704 | 3 | Info.Param = makeTemplateParameter(Param); | 2705 | | // FIXME: These template arguments are temporary. Free them! | 2706 | 3 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); | 2707 | 3 | return Sema::TDK_SubstitutionFailure; | 2708 | 3 | } | 2709 | | | 2710 | 402k | continue; | 2711 | 402k | } | 2712 | | | 2713 | | // Substitute into the default template argument, if available. | 2714 | 0 | bool HasDefaultArg = false; | 2715 | 0 | TemplateDecl *TD = dyn_cast<TemplateDecl>(Template); | 2716 | 0 | if (!TD) { | 2717 | 0 | assert(isa<ClassTemplatePartialSpecializationDecl>(Template) || | 2718 | 0 | isa<VarTemplatePartialSpecializationDecl>(Template)); | 2719 | 0 | return Sema::TDK_Incomplete; | 2720 | 0 | } | 2721 | | | 2722 | 0 | TemplateArgumentLoc DefArg; | 2723 | 0 | { | 2724 | 0 | Qualifiers ThisTypeQuals; | 2725 | 0 | CXXRecordDecl *ThisContext = nullptr; | 2726 | 0 | if (auto *Rec = dyn_cast<CXXRecordDecl>(TD->getDeclContext())) | 2727 | 0 | if (Rec->isLambda()) | 2728 | 0 | if (auto *Method = dyn_cast<CXXMethodDecl>(Rec->getDeclContext())) { | 2729 | 0 | ThisContext = Method->getParent(); | 2730 | 0 | ThisTypeQuals = Method->getMethodQualifiers(); | 2731 | 0 | } | 2732 | |
| 2733 | 0 | Sema::CXXThisScopeRAII ThisScope(S, ThisContext, ThisTypeQuals, | 2734 | 0 | S.getLangOpts().CPlusPlus17); | 2735 | |
| 2736 | 0 | DefArg = S.SubstDefaultTemplateArgumentIfAvailable( | 2737 | 0 | TD, TD->getLocation(), TD->getSourceRange().getEnd(), Param, Builder, | 2738 | 0 | HasDefaultArg); | 2739 | 0 | } | 2740 | | | 2741 | | // If there was no default argument, deduction is incomplete. | 2742 | 0 | if (DefArg.getArgument().isNull()) { | 2743 | 0 | Info.Param = makeTemplateParameter( | 2744 | 0 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); | 2745 | 0 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); | 2746 | 0 | if (PartialOverloading) break; | 2747 | | | 2748 | 0 | return HasDefaultArg ? Sema::TDK_SubstitutionFailure | 2749 | 0 | : Sema::TDK_Incomplete; | 2750 | 0 | } | 2751 | | | 2752 | | // Check whether we can actually use the default argument. | 2753 | 0 | if (S.CheckTemplateArgument(Param, DefArg, TD, TD->getLocation(), | 2754 | 0 | TD->getSourceRange().getEnd(), 0, Builder, | 2755 | 0 | Sema::CTAK_Specified)) { | 2756 | 0 | Info.Param = makeTemplateParameter( | 2757 | 0 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); | 2758 | | // FIXME: These template arguments are temporary. Free them! | 2759 | 0 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); | 2760 | 0 | return Sema::TDK_SubstitutionFailure; | 2761 | 0 | } | 2762 | | | 2763 | | // If we get here, we successfully used the default template argument. | 2764 | 0 | } | 2765 | | | 2766 | 187k | return Sema::TDK_Success; | 2767 | 187k | } |
|
2768 | | |
2769 | 472k | static DeclContext *getAsDeclContextOrEnclosing(Decl *D) { |
2770 | 472k | if (auto *DC = dyn_cast<DeclContext>(D)) |
2771 | 283k | return DC; |
2772 | 188k | return D->getDeclContext(); |
2773 | 472k | } |
2774 | | |
2775 | | template<typename T> struct IsPartialSpecialization { |
2776 | | static constexpr bool value = false; |
2777 | | }; |
2778 | | template<> |
2779 | | struct IsPartialSpecialization<ClassTemplatePartialSpecializationDecl> { |
2780 | | static constexpr bool value = true; |
2781 | | }; |
2782 | | template<> |
2783 | | struct IsPartialSpecialization<VarTemplatePartialSpecializationDecl> { |
2784 | | static constexpr bool value = true; |
2785 | | }; |
2786 | | |
2787 | | template<typename TemplateDeclT> |
2788 | | static Sema::TemplateDeductionResult |
2789 | | CheckDeducedArgumentConstraints(Sema& S, TemplateDeclT *Template, |
2790 | | ArrayRef<TemplateArgument> DeducedArgs, |
2791 | 424k | TemplateDeductionInfo& Info) { |
2792 | 424k | llvm::SmallVector<const Expr *, 3> AssociatedConstraints; |
2793 | 424k | Template->getAssociatedConstraints(AssociatedConstraints); |
2794 | 424k | if (S.CheckConstraintSatisfaction(Template, AssociatedConstraints, |
2795 | 424k | DeducedArgs, Info.getLocation(), |
2796 | 424k | Info.AssociatedConstraintsSatisfaction) || |
2797 | 424k | !Info.AssociatedConstraintsSatisfaction.IsSatisfied424k ) { |
2798 | 105 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, DeducedArgs)); |
2799 | 105 | return Sema::TDK_ConstraintsNotSatisfied; |
2800 | 105 | } |
2801 | 424k | return Sema::TDK_Success; |
2802 | 424k | } SemaTemplateDeduction.cpp:clang::Sema::TemplateDeductionResult CheckDeducedArgumentConstraints<clang::ClassTemplatePartialSpecializationDecl>(clang::Sema&, clang::ClassTemplatePartialSpecializationDecl*, llvm::ArrayRef<clang::TemplateArgument>, clang::sema::TemplateDeductionInfo&) Line | Count | Source | 2791 | 236k | TemplateDeductionInfo& Info) { | 2792 | 236k | llvm::SmallVector<const Expr *, 3> AssociatedConstraints; | 2793 | 236k | Template->getAssociatedConstraints(AssociatedConstraints); | 2794 | 236k | if (S.CheckConstraintSatisfaction(Template, AssociatedConstraints, | 2795 | 236k | DeducedArgs, Info.getLocation(), | 2796 | 236k | Info.AssociatedConstraintsSatisfaction) || | 2797 | 236k | !Info.AssociatedConstraintsSatisfaction.IsSatisfied236k ) { | 2798 | 94 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, DeducedArgs)); | 2799 | 94 | return Sema::TDK_ConstraintsNotSatisfied; | 2800 | 94 | } | 2801 | 236k | return Sema::TDK_Success; | 2802 | 236k | } |
SemaTemplateDeduction.cpp:clang::Sema::TemplateDeductionResult CheckDeducedArgumentConstraints<clang::VarTemplatePartialSpecializationDecl>(clang::Sema&, clang::VarTemplatePartialSpecializationDecl*, llvm::ArrayRef<clang::TemplateArgument>, clang::sema::TemplateDeductionInfo&) Line | Count | Source | 2791 | 922 | TemplateDeductionInfo& Info) { | 2792 | 922 | llvm::SmallVector<const Expr *, 3> AssociatedConstraints; | 2793 | 922 | Template->getAssociatedConstraints(AssociatedConstraints); | 2794 | 922 | if (S.CheckConstraintSatisfaction(Template, AssociatedConstraints, | 2795 | 922 | DeducedArgs, Info.getLocation(), | 2796 | 922 | Info.AssociatedConstraintsSatisfaction) || | 2797 | 922 | !Info.AssociatedConstraintsSatisfaction.IsSatisfied918 ) { | 2798 | 11 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, DeducedArgs)); | 2799 | 11 | return Sema::TDK_ConstraintsNotSatisfied; | 2800 | 11 | } | 2801 | 911 | return Sema::TDK_Success; | 2802 | 922 | } |
SemaTemplateDeduction.cpp:clang::Sema::TemplateDeductionResult CheckDeducedArgumentConstraints<clang::TemplateDecl>(clang::Sema&, clang::TemplateDecl*, llvm::ArrayRef<clang::TemplateArgument>, clang::sema::TemplateDeductionInfo&) Line | Count | Source | 2791 | 187k | TemplateDeductionInfo& Info) { | 2792 | 187k | llvm::SmallVector<const Expr *, 3> AssociatedConstraints; | 2793 | 187k | Template->getAssociatedConstraints(AssociatedConstraints); | 2794 | 187k | if (S.CheckConstraintSatisfaction(Template, AssociatedConstraints, | 2795 | 187k | DeducedArgs, Info.getLocation(), | 2796 | 187k | Info.AssociatedConstraintsSatisfaction) || | 2797 | 187k | !Info.AssociatedConstraintsSatisfaction.IsSatisfied) { | 2798 | 0 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, DeducedArgs)); | 2799 | 0 | return Sema::TDK_ConstraintsNotSatisfied; | 2800 | 0 | } | 2801 | 187k | return Sema::TDK_Success; | 2802 | 187k | } |
|
2803 | | |
2804 | | /// Complete template argument deduction for a partial specialization. |
2805 | | template <typename T> |
2806 | | static std::enable_if_t<IsPartialSpecialization<T>::value, |
2807 | | Sema::TemplateDeductionResult> |
2808 | | FinishTemplateArgumentDeduction( |
2809 | | Sema &S, T *Partial, bool IsPartialOrdering, |
2810 | | const TemplateArgumentList &TemplateArgs, |
2811 | | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
2812 | 284k | TemplateDeductionInfo &Info) { |
2813 | | // Unevaluated SFINAE context. |
2814 | 284k | EnterExpressionEvaluationContext Unevaluated( |
2815 | 284k | S, Sema::ExpressionEvaluationContext::Unevaluated); |
2816 | 284k | Sema::SFINAETrap Trap(S); |
2817 | | |
2818 | 284k | Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Partial)); |
2819 | | |
2820 | | // C++ [temp.deduct.type]p2: |
2821 | | // [...] or if any template argument remains neither deduced nor |
2822 | | // explicitly specified, template argument deduction fails. |
2823 | 284k | SmallVector<TemplateArgument, 4> Builder; |
2824 | 284k | if (auto Result = ConvertDeducedTemplateArguments( |
2825 | 284k | S, Partial, IsPartialOrdering, Deduced, Info, Builder)) |
2826 | 12.9k | return Result; |
2827 | | |
2828 | | // Form the template argument list from the deduced template arguments. |
2829 | 271k | TemplateArgumentList *DeducedArgumentList |
2830 | 271k | = TemplateArgumentList::CreateCopy(S.Context, Builder); |
2831 | | |
2832 | 271k | Info.reset(DeducedArgumentList); |
2833 | | |
2834 | | // Substitute the deduced template arguments into the template |
2835 | | // arguments of the class template partial specialization, and |
2836 | | // verify that the instantiated template arguments are both valid |
2837 | | // and are equivalent to the template arguments originally provided |
2838 | | // to the class template. |
2839 | 271k | LocalInstantiationScope InstScope(S); |
2840 | 271k | auto *Template = Partial->getSpecializedTemplate(); |
2841 | 271k | const ASTTemplateArgumentListInfo *PartialTemplArgInfo = |
2842 | 271k | Partial->getTemplateArgsAsWritten(); |
2843 | | |
2844 | 271k | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
2845 | 271k | PartialTemplArgInfo->RAngleLoc); |
2846 | | |
2847 | 271k | if (S.SubstTemplateArguments( |
2848 | 271k | PartialTemplArgInfo->arguments(), |
2849 | 271k | MultiLevelTemplateArgumentList(*DeducedArgumentList), InstArgs)) { |
2850 | 12.8k | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
2851 | 12.8k | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
2852 | 12.5k | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
2853 | | |
2854 | 12.8k | Decl *Param = const_cast<NamedDecl *>( |
2855 | 12.8k | Partial->getTemplateParameters()->getParam(ParamIdx)); |
2856 | 12.8k | Info.Param = makeTemplateParameter(Param); |
2857 | 12.8k | Info.FirstArg = (*PartialTemplArgInfo)[ArgIdx].getArgument(); |
2858 | 12.8k | return Sema::TDK_SubstitutionFailure; |
2859 | 12.8k | } |
2860 | | |
2861 | 259k | bool ConstraintsNotSatisfied; |
2862 | 259k | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
2863 | 259k | if (S.CheckTemplateArgumentList(Template, Partial->getLocation(), InstArgs, |
2864 | 259k | false, ConvertedInstArgs, |
2865 | 259k | /*UpdateArgsWithConversions=*/true, |
2866 | 259k | &ConstraintsNotSatisfied)) |
2867 | 2 | return ConstraintsNotSatisfied ? Sema::TDK_ConstraintsNotSatisfied0 : |
2868 | 2 | Sema::TDK_SubstitutionFailure; |
2869 | | |
2870 | 259k | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
2871 | 771k | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I512k ) { |
2872 | 533k | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
2873 | 533k | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
2874 | 21.5k | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
2875 | 21.5k | Info.FirstArg = TemplateArgs[I]; |
2876 | 21.5k | Info.SecondArg = InstArg; |
2877 | 21.5k | return Sema::TDK_NonDeducedMismatch; |
2878 | 21.5k | } |
2879 | 533k | } |
2880 | | |
2881 | 237k | if (Trap.hasErrorOccurred()) |
2882 | 0 | return Sema::TDK_SubstitutionFailure; |
2883 | | |
2884 | 237k | if (auto Result = CheckDeducedArgumentConstraints(S, Partial, Builder, Info)) |
2885 | 105 | return Result; |
2886 | | |
2887 | 237k | return Sema::TDK_Success; |
2888 | 237k | } SemaTemplateDeduction.cpp:std::__1::enable_if<IsPartialSpecialization<clang::ClassTemplatePartialSpecializationDecl>::value, clang::Sema::TemplateDeductionResult>::type FinishTemplateArgumentDeduction<clang::ClassTemplatePartialSpecializationDecl>(clang::Sema&, clang::ClassTemplatePartialSpecializationDecl*, bool, clang::TemplateArgumentList const&, llvm::SmallVectorImpl<clang::DeducedTemplateArgument>&, clang::sema::TemplateDeductionInfo&) Line | Count | Source | 2812 | 283k | TemplateDeductionInfo &Info) { | 2813 | | // Unevaluated SFINAE context. | 2814 | 283k | EnterExpressionEvaluationContext Unevaluated( | 2815 | 283k | S, Sema::ExpressionEvaluationContext::Unevaluated); | 2816 | 283k | Sema::SFINAETrap Trap(S); | 2817 | | | 2818 | 283k | Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Partial)); | 2819 | | | 2820 | | // C++ [temp.deduct.type]p2: | 2821 | | // [...] or if any template argument remains neither deduced nor | 2822 | | // explicitly specified, template argument deduction fails. | 2823 | 283k | SmallVector<TemplateArgument, 4> Builder; | 2824 | 283k | if (auto Result = ConvertDeducedTemplateArguments( | 2825 | 283k | S, Partial, IsPartialOrdering, Deduced, Info, Builder)) | 2826 | 12.9k | return Result; | 2827 | | | 2828 | | // Form the template argument list from the deduced template arguments. | 2829 | 270k | TemplateArgumentList *DeducedArgumentList | 2830 | 270k | = TemplateArgumentList::CreateCopy(S.Context, Builder); | 2831 | | | 2832 | 270k | Info.reset(DeducedArgumentList); | 2833 | | | 2834 | | // Substitute the deduced template arguments into the template | 2835 | | // arguments of the class template partial specialization, and | 2836 | | // verify that the instantiated template arguments are both valid | 2837 | | // and are equivalent to the template arguments originally provided | 2838 | | // to the class template. | 2839 | 270k | LocalInstantiationScope InstScope(S); | 2840 | 270k | auto *Template = Partial->getSpecializedTemplate(); | 2841 | 270k | const ASTTemplateArgumentListInfo *PartialTemplArgInfo = | 2842 | 270k | Partial->getTemplateArgsAsWritten(); | 2843 | | | 2844 | 270k | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, | 2845 | 270k | PartialTemplArgInfo->RAngleLoc); | 2846 | | | 2847 | 270k | if (S.SubstTemplateArguments( | 2848 | 270k | PartialTemplArgInfo->arguments(), | 2849 | 270k | MultiLevelTemplateArgumentList(*DeducedArgumentList), InstArgs)) { | 2850 | 12.8k | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; | 2851 | 12.8k | if (ParamIdx >= Partial->getTemplateParameters()->size()) | 2852 | 12.5k | ParamIdx = Partial->getTemplateParameters()->size() - 1; | 2853 | | | 2854 | 12.8k | Decl *Param = const_cast<NamedDecl *>( | 2855 | 12.8k | Partial->getTemplateParameters()->getParam(ParamIdx)); | 2856 | 12.8k | Info.Param = makeTemplateParameter(Param); | 2857 | 12.8k | Info.FirstArg = (*PartialTemplArgInfo)[ArgIdx].getArgument(); | 2858 | 12.8k | return Sema::TDK_SubstitutionFailure; | 2859 | 12.8k | } | 2860 | | | 2861 | 258k | bool ConstraintsNotSatisfied; | 2862 | 258k | SmallVector<TemplateArgument, 4> ConvertedInstArgs; | 2863 | 258k | if (S.CheckTemplateArgumentList(Template, Partial->getLocation(), InstArgs, | 2864 | 258k | false, ConvertedInstArgs, | 2865 | 258k | /*UpdateArgsWithConversions=*/true, | 2866 | 258k | &ConstraintsNotSatisfied)) | 2867 | 2 | return ConstraintsNotSatisfied ? Sema::TDK_ConstraintsNotSatisfied0 : | 2868 | 2 | Sema::TDK_SubstitutionFailure; | 2869 | | | 2870 | 258k | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); | 2871 | 768k | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I510k ) { | 2872 | 531k | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; | 2873 | 531k | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { | 2874 | 21.5k | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); | 2875 | 21.5k | Info.FirstArg = TemplateArgs[I]; | 2876 | 21.5k | Info.SecondArg = InstArg; | 2877 | 21.5k | return Sema::TDK_NonDeducedMismatch; | 2878 | 21.5k | } | 2879 | 531k | } | 2880 | | | 2881 | 236k | if (Trap.hasErrorOccurred()) | 2882 | 0 | return Sema::TDK_SubstitutionFailure; | 2883 | | | 2884 | 236k | if (auto Result = CheckDeducedArgumentConstraints(S, Partial, Builder, Info)) | 2885 | 94 | return Result; | 2886 | | | 2887 | 236k | return Sema::TDK_Success; | 2888 | 236k | } |
SemaTemplateDeduction.cpp:std::__1::enable_if<IsPartialSpecialization<clang::VarTemplatePartialSpecializationDecl>::value, clang::Sema::TemplateDeductionResult>::type FinishTemplateArgumentDeduction<clang::VarTemplatePartialSpecializationDecl>(clang::Sema&, clang::VarTemplatePartialSpecializationDecl*, bool, clang::TemplateArgumentList const&, llvm::SmallVectorImpl<clang::DeducedTemplateArgument>&, clang::sema::TemplateDeductionInfo&) Line | Count | Source | 2812 | 927 | TemplateDeductionInfo &Info) { | 2813 | | // Unevaluated SFINAE context. | 2814 | 927 | EnterExpressionEvaluationContext Unevaluated( | 2815 | 927 | S, Sema::ExpressionEvaluationContext::Unevaluated); | 2816 | 927 | Sema::SFINAETrap Trap(S); | 2817 | | | 2818 | 927 | Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Partial)); | 2819 | | | 2820 | | // C++ [temp.deduct.type]p2: | 2821 | | // [...] or if any template argument remains neither deduced nor | 2822 | | // explicitly specified, template argument deduction fails. | 2823 | 927 | SmallVector<TemplateArgument, 4> Builder; | 2824 | 927 | if (auto Result = ConvertDeducedTemplateArguments( | 2825 | 927 | S, Partial, IsPartialOrdering, Deduced, Info, Builder)) | 2826 | 1 | return Result; | 2827 | | | 2828 | | // Form the template argument list from the deduced template arguments. | 2829 | 926 | TemplateArgumentList *DeducedArgumentList | 2830 | 926 | = TemplateArgumentList::CreateCopy(S.Context, Builder); | 2831 | | | 2832 | 926 | Info.reset(DeducedArgumentList); | 2833 | | | 2834 | | // Substitute the deduced template arguments into the template | 2835 | | // arguments of the class template partial specialization, and | 2836 | | // verify that the instantiated template arguments are both valid | 2837 | | // and are equivalent to the template arguments originally provided | 2838 | | // to the class template. | 2839 | 926 | LocalInstantiationScope InstScope(S); | 2840 | 926 | auto *Template = Partial->getSpecializedTemplate(); | 2841 | 926 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo = | 2842 | 926 | Partial->getTemplateArgsAsWritten(); | 2843 | | | 2844 | 926 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, | 2845 | 926 | PartialTemplArgInfo->RAngleLoc); | 2846 | | | 2847 | 926 | if (S.SubstTemplateArguments( | 2848 | 926 | PartialTemplArgInfo->arguments(), | 2849 | 926 | MultiLevelTemplateArgumentList(*DeducedArgumentList), InstArgs)) { | 2850 | 2 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; | 2851 | 2 | if (ParamIdx >= Partial->getTemplateParameters()->size()) | 2852 | 2 | ParamIdx = Partial->getTemplateParameters()->size() - 1; | 2853 | | | 2854 | 2 | Decl *Param = const_cast<NamedDecl *>( | 2855 | 2 | Partial->getTemplateParameters()->getParam(ParamIdx)); | 2856 | 2 | Info.Param = makeTemplateParameter(Param); | 2857 | 2 | Info.FirstArg = (*PartialTemplArgInfo)[ArgIdx].getArgument(); | 2858 | 2 | return Sema::TDK_SubstitutionFailure; | 2859 | 2 | } | 2860 | | | 2861 | 924 | bool ConstraintsNotSatisfied; | 2862 | 924 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; | 2863 | 924 | if (S.CheckTemplateArgumentList(Template, Partial->getLocation(), InstArgs, | 2864 | 924 | false, ConvertedInstArgs, | 2865 | 924 | /*UpdateArgsWithConversions=*/true, | 2866 | 924 | &ConstraintsNotSatisfied)) | 2867 | 0 | return ConstraintsNotSatisfied ? Sema::TDK_ConstraintsNotSatisfied : | 2868 | 0 | Sema::TDK_SubstitutionFailure; | 2869 | | | 2870 | 924 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); | 2871 | 2.63k | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I1.71k ) { | 2872 | 1.71k | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; | 2873 | 1.71k | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { | 2874 | 2 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); | 2875 | 2 | Info.FirstArg = TemplateArgs[I]; | 2876 | 2 | Info.SecondArg = InstArg; | 2877 | 2 | return Sema::TDK_NonDeducedMismatch; | 2878 | 2 | } | 2879 | 1.71k | } | 2880 | | | 2881 | 922 | if (Trap.hasErrorOccurred()) | 2882 | 0 | return Sema::TDK_SubstitutionFailure; | 2883 | | | 2884 | 922 | if (auto Result = CheckDeducedArgumentConstraints(S, Partial, Builder, Info)) | 2885 | 11 | return Result; | 2886 | | | 2887 | 911 | return Sema::TDK_Success; | 2888 | 922 | } |
|
2889 | | |
2890 | | /// Complete template argument deduction for a class or variable template, |
2891 | | /// when partial ordering against a partial specialization. |
2892 | | // FIXME: Factor out duplication with partial specialization version above. |
2893 | | static Sema::TemplateDeductionResult FinishTemplateArgumentDeduction( |
2894 | | Sema &S, TemplateDecl *Template, bool PartialOrdering, |
2895 | | const TemplateArgumentList &TemplateArgs, |
2896 | | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
2897 | 187k | TemplateDeductionInfo &Info) { |
2898 | | // Unevaluated SFINAE context. |
2899 | 187k | EnterExpressionEvaluationContext Unevaluated( |
2900 | 187k | S, Sema::ExpressionEvaluationContext::Unevaluated); |
2901 | 187k | Sema::SFINAETrap Trap(S); |
2902 | | |
2903 | 187k | Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Template)); |
2904 | | |
2905 | | // C++ [temp.deduct.type]p2: |
2906 | | // [...] or if any template argument remains neither deduced nor |
2907 | | // explicitly specified, template argument deduction fails. |
2908 | 187k | SmallVector<TemplateArgument, 4> Builder; |
2909 | 187k | if (auto Result = ConvertDeducedTemplateArguments( |
2910 | 187k | S, Template, /*IsDeduced*/PartialOrdering, Deduced, Info, Builder)) |
2911 | 3 | return Result; |
2912 | | |
2913 | | // Check that we produced the correct argument list. |
2914 | 187k | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
2915 | 589k | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I402k ) { |
2916 | 402k | TemplateArgument InstArg = Builder[I]; |
2917 | 402k | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg, |
2918 | 402k | /*PackExpansionMatchesPack*/true)) { |
2919 | 0 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
2920 | 0 | Info.FirstArg = TemplateArgs[I]; |
2921 | 0 | Info.SecondArg = InstArg; |
2922 | 0 | return Sema::TDK_NonDeducedMismatch; |
2923 | 0 | } |
2924 | 402k | } |
2925 | | |
2926 | 187k | if (Trap.hasErrorOccurred()) |
2927 | 0 | return Sema::TDK_SubstitutionFailure; |
2928 | | |
2929 | 187k | if (auto Result = CheckDeducedArgumentConstraints(S, Template, Builder, |
2930 | 187k | Info)) |
2931 | 0 | return Result; |
2932 | | |
2933 | 187k | return Sema::TDK_Success; |
2934 | 187k | } |
2935 | | |
2936 | | /// Perform template argument deduction to determine whether |
2937 | | /// the given template arguments match the given class template |
2938 | | /// partial specialization per C++ [temp.class.spec.match]. |
2939 | | Sema::TemplateDeductionResult |
2940 | | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
2941 | | const TemplateArgumentList &TemplateArgs, |
2942 | 701k | TemplateDeductionInfo &Info) { |
2943 | 701k | if (Partial->isInvalidDecl()) |
2944 | 4 | return TDK_Invalid; |
2945 | | |
2946 | | // C++ [temp.class.spec.match]p2: |
2947 | | // A partial specialization matches a given actual template |
2948 | | // argument list if the template arguments of the partial |
2949 | | // specialization can be deduced from the actual template argument |
2950 | | // list (14.8.2). |
2951 | | |
2952 | | // Unevaluated SFINAE context. |
2953 | 701k | EnterExpressionEvaluationContext Unevaluated( |
2954 | 701k | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
2955 | 701k | SFINAETrap Trap(*this); |
2956 | | |
2957 | | // This deduction has no relation to any outer instantiation we might be |
2958 | | // performing. |
2959 | 701k | LocalInstantiationScope InstantiationScope(*this); |
2960 | | |
2961 | 701k | SmallVector<DeducedTemplateArgument, 4> Deduced; |
2962 | 701k | Deduced.resize(Partial->getTemplateParameters()->size()); |
2963 | 701k | if (TemplateDeductionResult Result |
2964 | 701k | = ::DeduceTemplateArguments(*this, |
2965 | 701k | Partial->getTemplateParameters(), |
2966 | 701k | Partial->getTemplateArgs(), |
2967 | 701k | TemplateArgs, Info, Deduced)) |
2968 | 446k | return Result; |
2969 | | |
2970 | 254k | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
2971 | 254k | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
2972 | 254k | Info); |
2973 | 254k | if (Inst.isInvalid()) |
2974 | 0 | return TDK_InstantiationDepth; |
2975 | | |
2976 | 254k | if (Trap.hasErrorOccurred()) |
2977 | 0 | return Sema::TDK_SubstitutionFailure; |
2978 | | |
2979 | 254k | TemplateDeductionResult Result; |
2980 | 254k | runWithSufficientStackSpace(Info.getLocation(), [&] { |
2981 | 254k | Result = ::FinishTemplateArgumentDeduction(*this, Partial, |
2982 | 254k | /*IsPartialOrdering=*/false, |
2983 | 254k | TemplateArgs, Deduced, Info); |
2984 | 254k | }); |
2985 | 254k | return Result; |
2986 | 254k | } |
2987 | | |
2988 | | /// Perform template argument deduction to determine whether |
2989 | | /// the given template arguments match the given variable template |
2990 | | /// partial specialization per C++ [temp.class.spec.match]. |
2991 | | Sema::TemplateDeductionResult |
2992 | | Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial, |
2993 | | const TemplateArgumentList &TemplateArgs, |
2994 | 1.13k | TemplateDeductionInfo &Info) { |
2995 | 1.13k | if (Partial->isInvalidDecl()) |
2996 | 0 | return TDK_Invalid; |
2997 | | |
2998 | | // C++ [temp.class.spec.match]p2: |
2999 | | // A partial specialization matches a given actual template |
3000 | | // argument list if the template arguments of the partial |
3001 | | // specialization can be deduced from the actual template argument |
3002 | | // list (14.8.2). |
3003 | | |
3004 | | // Unevaluated SFINAE context. |
3005 | 1.13k | EnterExpressionEvaluationContext Unevaluated( |
3006 | 1.13k | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
3007 | 1.13k | SFINAETrap Trap(*this); |
3008 | | |
3009 | | // This deduction has no relation to any outer instantiation we might be |
3010 | | // performing. |
3011 | 1.13k | LocalInstantiationScope InstantiationScope(*this); |
3012 | | |
3013 | 1.13k | SmallVector<DeducedTemplateArgument, 4> Deduced; |
3014 | 1.13k | Deduced.resize(Partial->getTemplateParameters()->size()); |
3015 | 1.13k | if (TemplateDeductionResult Result = ::DeduceTemplateArguments( |
3016 | 1.13k | *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(), |
3017 | 1.13k | TemplateArgs, Info, Deduced)) |
3018 | 273 | return Result; |
3019 | | |
3020 | 864 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
3021 | 864 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
3022 | 864 | Info); |
3023 | 864 | if (Inst.isInvalid()) |
3024 | 0 | return TDK_InstantiationDepth; |
3025 | | |
3026 | 864 | if (Trap.hasErrorOccurred()) |
3027 | 0 | return Sema::TDK_SubstitutionFailure; |
3028 | | |
3029 | 864 | TemplateDeductionResult Result; |
3030 | 864 | runWithSufficientStackSpace(Info.getLocation(), [&] { |
3031 | 864 | Result = ::FinishTemplateArgumentDeduction(*this, Partial, |
3032 | 864 | /*IsPartialOrdering=*/false, |
3033 | 864 | TemplateArgs, Deduced, Info); |
3034 | 864 | }); |
3035 | 864 | return Result; |
3036 | 864 | } |
3037 | | |
3038 | | /// Determine whether the given type T is a simple-template-id type. |
3039 | 1.33M | static bool isSimpleTemplateIdType(QualType T) { |
3040 | 1.33M | if (const TemplateSpecializationType *Spec |
3041 | 1.33M | = T->getAs<TemplateSpecializationType>()) |
3042 | 882k | return Spec->getTemplateName().getAsTemplateDecl() != nullptr; |
3043 | | |
3044 | | // C++17 [temp.local]p2: |
3045 | | // the injected-class-name [...] is equivalent to the template-name followed |
3046 | | // by the template-arguments of the class template specialization or partial |
3047 | | // specialization enclosed in <> |
3048 | | // ... which means it's equivalent to a simple-template-id. |
3049 | | // |
3050 | | // This only arises during class template argument deduction for a copy |
3051 | | // deduction candidate, where it permits slicing. |
3052 | 454k | if (T->getAs<InjectedClassNameType>()) |
3053 | 369 | return true; |
3054 | | |
3055 | 453k | return false; |
3056 | 454k | } |
3057 | | |
3058 | | /// Substitute the explicitly-provided template arguments into the |
3059 | | /// given function template according to C++ [temp.arg.explicit]. |
3060 | | /// |
3061 | | /// \param FunctionTemplate the function template into which the explicit |
3062 | | /// template arguments will be substituted. |
3063 | | /// |
3064 | | /// \param ExplicitTemplateArgs the explicitly-specified template |
3065 | | /// arguments. |
3066 | | /// |
3067 | | /// \param Deduced the deduced template arguments, which will be populated |
3068 | | /// with the converted and checked explicit template arguments. |
3069 | | /// |
3070 | | /// \param ParamTypes will be populated with the instantiated function |
3071 | | /// parameters. |
3072 | | /// |
3073 | | /// \param FunctionType if non-NULL, the result type of the function template |
3074 | | /// will also be instantiated and the pointed-to value will be updated with |
3075 | | /// the instantiated function type. |
3076 | | /// |
3077 | | /// \param Info if substitution fails for any reason, this object will be |
3078 | | /// populated with more information about the failure. |
3079 | | /// |
3080 | | /// \returns TDK_Success if substitution was successful, or some failure |
3081 | | /// condition. |
3082 | | Sema::TemplateDeductionResult |
3083 | | Sema::SubstituteExplicitTemplateArguments( |
3084 | | FunctionTemplateDecl *FunctionTemplate, |
3085 | | TemplateArgumentListInfo &ExplicitTemplateArgs, |
3086 | | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
3087 | | SmallVectorImpl<QualType> &ParamTypes, |
3088 | | QualType *FunctionType, |
3089 | 395k | TemplateDeductionInfo &Info) { |
3090 | 395k | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
3091 | 395k | TemplateParameterList *TemplateParams |
3092 | 395k | = FunctionTemplate->getTemplateParameters(); |
3093 | | |
3094 | 395k | if (ExplicitTemplateArgs.size() == 0) { |
3095 | | // No arguments to substitute; just copy over the parameter types and |
3096 | | // fill in the function type. |
3097 | 34.4k | for (auto P : Function->parameters()) |
3098 | 65.2k | ParamTypes.push_back(P->getType()); |
3099 | | |
3100 | 34.4k | if (FunctionType) |
3101 | 34.3k | *FunctionType = Function->getType(); |
3102 | 34.4k | return TDK_Success; |
3103 | 34.4k | } |
3104 | | |
3105 | | // Unevaluated SFINAE context. |
3106 | 361k | EnterExpressionEvaluationContext Unevaluated( |
3107 | 361k | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
3108 | 361k | SFINAETrap Trap(*this); |
3109 | | |
3110 | | // C++ [temp.arg.explicit]p3: |
3111 | | // Template arguments that are present shall be specified in the |
3112 | | // declaration order of their corresponding template-parameters. The |
3113 | | // template argument list shall not specify more template-arguments than |
3114 | | // there are corresponding template-parameters. |
3115 | 361k | SmallVector<TemplateArgument, 4> Builder; |
3116 | | |
3117 | | // Enter a new template instantiation context where we check the |
3118 | | // explicitly-specified template arguments against this function template, |
3119 | | // and then substitute them into the function parameter types. |
3120 | 361k | SmallVector<TemplateArgument, 4> DeducedArgs; |
3121 | 361k | InstantiatingTemplate Inst( |
3122 | 361k | *this, Info.getLocation(), FunctionTemplate, DeducedArgs, |
3123 | 361k | CodeSynthesisContext::ExplicitTemplateArgumentSubstitution, Info); |
3124 | 361k | if (Inst.isInvalid()) |
3125 | 12 | return TDK_InstantiationDepth; |
3126 | | |
3127 | 361k | if (CheckTemplateArgumentList(FunctionTemplate, SourceLocation(), |
3128 | 361k | ExplicitTemplateArgs, true, Builder, false) || |
3129 | 361k | Trap.hasErrorOccurred()358k ) { |
3130 | 2.92k | unsigned Index = Builder.size(); |
3131 | 2.92k | if (Index >= TemplateParams->size()) |
3132 | 1.04k | return TDK_SubstitutionFailure; |
3133 | 1.88k | Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); |
3134 | 1.88k | return TDK_InvalidExplicitArguments; |
3135 | 2.92k | } |
3136 | | |
3137 | | // Form the template argument list from the explicitly-specified |
3138 | | // template arguments. |
3139 | 358k | TemplateArgumentList *ExplicitArgumentList |
3140 | 358k | = TemplateArgumentList::CreateCopy(Context, Builder); |
3141 | 358k | Info.setExplicitArgs(ExplicitArgumentList); |
3142 | | |
3143 | | // Template argument deduction and the final substitution should be |
3144 | | // done in the context of the templated declaration. Explicit |
3145 | | // argument substitution, on the other hand, needs to happen in the |
3146 | | // calling context. |
3147 | 358k | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
3148 | | |
3149 | | // If we deduced template arguments for a template parameter pack, |
3150 | | // note that the template argument pack is partially substituted and record |
3151 | | // the explicit template arguments. They'll be used as part of deduction |
3152 | | // for this template parameter pack. |
3153 | 358k | unsigned PartiallySubstitutedPackIndex = -1u; |
3154 | 358k | if (!Builder.empty()) { |
3155 | 358k | const TemplateArgument &Arg = Builder.back(); |
3156 | 358k | if (Arg.getKind() == TemplateArgument::Pack) { |
3157 | 31.0k | auto *Param = TemplateParams->getParam(Builder.size() - 1); |
3158 | | // If this is a fully-saturated fixed-size pack, it should be |
3159 | | // fully-substituted, not partially-substituted. |
3160 | 31.0k | Optional<unsigned> Expansions = getExpandedPackSize(Param); |
3161 | 31.0k | if (!Expansions || Arg.pack_size() < *Expansions16 ) { |
3162 | 31.0k | PartiallySubstitutedPackIndex = Builder.size() - 1; |
3163 | 31.0k | CurrentInstantiationScope->SetPartiallySubstitutedPack( |
3164 | 31.0k | Param, Arg.pack_begin(), Arg.pack_size()); |
3165 | 31.0k | } |
3166 | 31.0k | } |
3167 | 358k | } |
3168 | | |
3169 | 358k | const FunctionProtoType *Proto |
3170 | 358k | = Function->getType()->getAs<FunctionProtoType>(); |
3171 | 358k | assert(Proto && "Function template does not have a prototype?"); |
3172 | | |
3173 | | // Isolate our substituted parameters from our caller. |
3174 | 0 | LocalInstantiationScope InstScope(*this, /*MergeWithOuterScope*/true); |
3175 | | |
3176 | 358k | ExtParameterInfoBuilder ExtParamInfos; |
3177 | | |
3178 | | // Instantiate the types of each of the function parameters given the |
3179 | | // explicitly-specified template arguments. If the function has a trailing |
3180 | | // return type, substitute it after the arguments to ensure we substitute |
3181 | | // in lexical order. |
3182 | 358k | if (Proto->hasTrailingReturn()) { |
3183 | 2.91k | if (SubstParmTypes(Function->getLocation(), Function->parameters(), |
3184 | 2.91k | Proto->getExtParameterInfosOrNull(), |
3185 | 2.91k | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
3186 | 2.91k | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
3187 | 4 | return TDK_SubstitutionFailure; |
3188 | 2.91k | } |
3189 | | |
3190 | | // Instantiate the return type. |
3191 | 358k | QualType ResultType; |
3192 | 358k | { |
3193 | | // C++11 [expr.prim.general]p3: |
3194 | | // If a declaration declares a member function or member function |
3195 | | // template of a class X, the expression this is a prvalue of type |
3196 | | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
3197 | | // and the end of the function-definition, member-declarator, or |
3198 | | // declarator. |
3199 | 358k | Qualifiers ThisTypeQuals; |
3200 | 358k | CXXRecordDecl *ThisContext = nullptr; |
3201 | 358k | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
3202 | 122k | ThisContext = Method->getParent(); |
3203 | 122k | ThisTypeQuals = Method->getMethodQualifiers(); |
3204 | 122k | } |
3205 | | |
3206 | 358k | CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals, |
3207 | 358k | getLangOpts().CPlusPlus11); |
3208 | | |
3209 | 358k | ResultType = |
3210 | 358k | SubstType(Proto->getReturnType(), |
3211 | 358k | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
3212 | 358k | Function->getTypeSpecStartLoc(), Function->getDeclName()); |
3213 | 358k | if (ResultType.isNull() || Trap.hasErrorOccurred()352k ) |
3214 | 6.40k | return TDK_SubstitutionFailure; |
3215 | | // CUDA: Kernel function must have 'void' return type. |
3216 | 352k | if (getLangOpts().CUDA) |
3217 | 246 | if (Function->hasAttr<CUDAGlobalAttr>() && !ResultType->isVoidType()56 ) { |
3218 | 1 | Diag(Function->getLocation(), diag::err_kern_type_not_void_return) |
3219 | 1 | << Function->getType() << Function->getSourceRange(); |
3220 | 1 | return TDK_SubstitutionFailure; |
3221 | 1 | } |
3222 | 352k | } |
3223 | | |
3224 | | // Instantiate the types of each of the function parameters given the |
3225 | | // explicitly-specified template arguments if we didn't do so earlier. |
3226 | 352k | if (!Proto->hasTrailingReturn() && |
3227 | 352k | SubstParmTypes(Function->getLocation(), Function->parameters(), |
3228 | 349k | Proto->getExtParameterInfosOrNull(), |
3229 | 349k | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
3230 | 349k | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
3231 | 458 | return TDK_SubstitutionFailure; |
3232 | | |
3233 | 351k | if (FunctionType) { |
3234 | 27.3k | auto EPI = Proto->getExtProtoInfo(); |
3235 | 27.3k | EPI.ExtParameterInfos = ExtParamInfos.getPointerOrNull(ParamTypes.size()); |
3236 | | |
3237 | | // In C++1z onwards, exception specifications are part of the function type, |
3238 | | // so substitution into the type must also substitute into the exception |
3239 | | // specification. |
3240 | 27.3k | SmallVector<QualType, 4> ExceptionStorage; |
3241 | 27.3k | if (getLangOpts().CPlusPlus17 && |
3242 | 27.3k | SubstExceptionSpec( |
3243 | 2.09k | Function->getLocation(), EPI.ExceptionSpec, ExceptionStorage, |
3244 | 2.09k | MultiLevelTemplateArgumentList(*ExplicitArgumentList))) |
3245 | 2 | return TDK_SubstitutionFailure; |
3246 | | |
3247 | 27.3k | *FunctionType = BuildFunctionType(ResultType, ParamTypes, |
3248 | 27.3k | Function->getLocation(), |
3249 | 27.3k | Function->getDeclName(), |
3250 | 27.3k | EPI); |
3251 | 27.3k | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
3252 | 1 | return TDK_SubstitutionFailure; |
3253 | 27.3k | } |
3254 | | |
3255 | | // C++ [temp.arg.explicit]p2: |
3256 | | // Trailing template arguments that can be deduced (14.8.2) may be |
3257 | | // omitted from the list of explicit template-arguments. If all of the |
3258 | | // template arguments can be deduced, they may all be omitted; in this |
3259 | | // case, the empty template argument list <> itself may also be omitted. |
3260 | | // |
3261 | | // Take all of the explicitly-specified arguments and put them into |
3262 | | // the set of deduced template arguments. The partially-substituted |
3263 | | // parameter pack, however, will be set to NULL since the deduction |
3264 | | // mechanism handles the partially-substituted argument pack directly. |
3265 | 351k | Deduced.reserve(TemplateParams->size()); |
3266 | 774k | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I422k ) { |
3267 | 422k | const TemplateArgument &Arg = ExplicitArgumentList->get(I); |
3268 | 422k | if (I == PartiallySubstitutedPackIndex) |
3269 | 25.4k | Deduced.push_back(DeducedTemplateArgument()); |
3270 | 397k | else |
3271 | 397k | Deduced.push_back(Arg); |
3272 | 422k | } |
3273 | | |
3274 | 351k | return TDK_Success; |
3275 | 351k | } |
3276 | | |
3277 | | /// Check whether the deduced argument type for a call to a function |
3278 | | /// template matches the actual argument type per C++ [temp.deduct.call]p4. |
3279 | | static Sema::TemplateDeductionResult |
3280 | | CheckOriginalCallArgDeduction(Sema &S, TemplateDeductionInfo &Info, |
3281 | | Sema::OriginalCallArg OriginalArg, |
3282 | 336k | QualType DeducedA) { |
3283 | 336k | ASTContext &Context = S.Context; |
3284 | | |
3285 | 336k | auto Failed = [&]() -> Sema::TemplateDeductionResult { |
3286 | 998 | Info.FirstArg = TemplateArgument(DeducedA); |
3287 | 998 | Info.SecondArg = TemplateArgument(OriginalArg.OriginalArgType); |
3288 | 998 | Info.CallArgIndex = OriginalArg.ArgIdx; |
3289 | 998 | return OriginalArg.DecomposedParam ? Sema::TDK_DeducedMismatchNested1 |
3290 | 998 | : Sema::TDK_DeducedMismatch997 ; |
3291 | 998 | }; |
3292 | | |
3293 | 336k | QualType A = OriginalArg.OriginalArgType; |
3294 | 336k | QualType OriginalParamType = OriginalArg.OriginalParamType; |
3295 | | |
3296 | | // Check for type equality (top-level cv-qualifiers are ignored). |
3297 | 336k | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
3298 | 167k | return Sema::TDK_Success; |
3299 | | |
3300 | | // Strip off references on the argument types; they aren't needed for |
3301 | | // the following checks. |
3302 | 169k | if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>()) |
3303 | 161k | DeducedA = DeducedARef->getPointeeType(); |
3304 | 169k | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
3305 | 0 | A = ARef->getPointeeType(); |
3306 | | |
3307 | | // C++ [temp.deduct.call]p4: |
3308 | | // [...] However, there are three cases that allow a difference: |
3309 | | // - If the original P is a reference type, the deduced A (i.e., the |
3310 | | // type referred to by the reference) can be more cv-qualified than |
3311 | | // the transformed A. |
3312 | 169k | if (const ReferenceType *OriginalParamRef |
3313 | 169k | = OriginalParamType->getAs<ReferenceType>()) { |
3314 | | // We don't want to keep the reference around any more. |
3315 | 161k | OriginalParamType = OriginalParamRef->getPointeeType(); |
3316 | | |
3317 | | // FIXME: Resolve core issue (no number yet): if the original P is a |
3318 | | // reference type and the transformed A is function type "noexcept F", |
3319 | | // the deduced A can be F. |
3320 | 161k | QualType Tmp; |
3321 | 161k | if (A->isFunctionType() && S.IsFunctionConversion(A, DeducedA, Tmp)56 ) |
3322 | 1 | return Sema::TDK_Success; |
3323 | | |
3324 | 161k | Qualifiers AQuals = A.getQualifiers(); |
3325 | 161k | Qualifiers DeducedAQuals = DeducedA.getQualifiers(); |
3326 | | |
3327 | | // Under Objective-C++ ARC, the deduced type may have implicitly |
3328 | | // been given strong or (when dealing with a const reference) |
3329 | | // unsafe_unretained lifetime. If so, update the original |
3330 | | // qualifiers to include this lifetime. |
3331 | 161k | if (S.getLangOpts().ObjCAutoRefCount && |
3332 | 161k | (87 (87 DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong87 && |
3333 | 87 | AQuals.getObjCLifetime() == Qualifiers::OCL_None20 ) || |
3334 | 87 | (86 DeducedAQuals.hasConst()86 && |
3335 | 86 | DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone16 ))) { |
3336 | 6 | AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime()); |
3337 | 6 | } |
3338 | | |
3339 | 161k | if (AQuals == DeducedAQuals) { |
3340 | | // Qualifiers match; there's nothing to do. |
3341 | 89.3k | } else if (72.0k !DeducedAQuals.compatiblyIncludes(AQuals)72.0k ) { |
3342 | 962 | return Failed(); |
3343 | 71.0k | } else { |
3344 | | // Qualifiers are compatible, so have the argument type adopt the |
3345 | | // deduced argument type's qualifiers as if we had performed the |
3346 | | // qualification conversion. |
3347 | 71.0k | A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals); |
3348 | 71.0k | } |
3349 | 161k | } |
3350 | | |
3351 | | // - The transformed A can be another pointer or pointer to member |
3352 | | // type that can be converted to the deduced A via a function pointer |
3353 | | // conversion and/or a qualification conversion. |
3354 | | // |
3355 | | // Also allow conversions which merely strip __attribute__((noreturn)) from |
3356 | | // function types (recursively). |
3357 | 168k | bool ObjCLifetimeConversion = false; |
3358 | 168k | QualType ResultTy; |
3359 | 168k | if ((A->isAnyPointerType() || A->isMemberPointerType()141k ) && |
3360 | 168k | (27.4k S.IsQualificationConversion(A, DeducedA, false, |
3361 | 27.4k | ObjCLifetimeConversion) || |
3362 | 27.4k | S.IsFunctionConversion(A, DeducedA, ResultTy)27.1k )) |
3363 | 336 | return Sema::TDK_Success; |
3364 | | |
3365 | | // - If P is a class and P has the form simple-template-id, then the |
3366 | | // transformed A can be a derived class of the deduced A. [...] |
3367 | | // [...] Likewise, if P is a pointer to a class of the form |
3368 | | // simple-template-id, the transformed A can be a pointer to a |
3369 | | // derived class pointed to by the deduced A. |
3370 | 168k | if (const PointerType *OriginalParamPtr |
3371 | 168k | = OriginalParamType->getAs<PointerType>()) { |
3372 | 8.42k | if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) { |
3373 | 8.42k | if (const PointerType *APtr = A->getAs<PointerType>()) { |
3374 | 8.42k | if (A->getPointeeType()->isRecordType()) { |
3375 | 7.94k | OriginalParamType = OriginalParamPtr->getPointeeType(); |
3376 | 7.94k | DeducedA = DeducedAPtr->getPointeeType(); |
3377 | 7.94k | A = APtr->getPointeeType(); |
3378 | 7.94k | } |
3379 | 8.42k | } |
3380 | 8.42k | } |
3381 | 8.42k | } |
3382 | | |
3383 | 168k | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
3384 | 160k | return Sema::TDK_Success; |
3385 | | |
3386 | 8.12k | if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType)8.09k && |
3387 | 8.12k | S.IsDerivedFrom(Info.getLocation(), A, DeducedA)8.09k ) |
3388 | 8.08k | return Sema::TDK_Success; |
3389 | | |
3390 | 36 | return Failed(); |
3391 | 8.12k | } |
3392 | | |
3393 | | /// Find the pack index for a particular parameter index in an instantiation of |
3394 | | /// a function template with specific arguments. |
3395 | | /// |
3396 | | /// \return The pack index for whichever pack produced this parameter, or -1 |
3397 | | /// if this was not produced by a parameter. Intended to be used as the |
3398 | | /// ArgumentPackSubstitutionIndex for further substitutions. |
3399 | | // FIXME: We should track this in OriginalCallArgs so we don't need to |
3400 | | // reconstruct it here. |
3401 | | static unsigned getPackIndexForParam(Sema &S, |
3402 | | FunctionTemplateDecl *FunctionTemplate, |
3403 | | const MultiLevelTemplateArgumentList &Args, |
3404 | 111 | unsigned ParamIdx) { |
3405 | 111 | unsigned Idx = 0; |
3406 | 114 | for (auto *PD : FunctionTemplate->getTemplatedDecl()->parameters()) { |
3407 | 114 | if (PD->isParameterPack()) { |
3408 | 15 | unsigned NumExpansions = |
3409 | 15 | S.getNumArgumentsInExpansion(PD->getType(), Args).value_or(1); |
3410 | 15 | if (Idx + NumExpansions > ParamIdx) |
3411 | 15 | return ParamIdx - Idx; |
3412 | 0 | Idx += NumExpansions; |
3413 | 99 | } else { |
3414 | 99 | if (Idx == ParamIdx) |
3415 | 96 | return -1; // Not a pack expansion |
3416 | 3 | ++Idx; |
3417 | 3 | } |
3418 | 114 | } |
3419 | | |
3420 | 0 | llvm_unreachable("parameter index would not be produced from template"); |
3421 | 0 | } |
3422 | | |
3423 | | /// Finish template argument deduction for a function template, |
3424 | | /// checking the deduced template arguments for completeness and forming |
3425 | | /// the function template specialization. |
3426 | | /// |
3427 | | /// \param OriginalCallArgs If non-NULL, the original call arguments against |
3428 | | /// which the deduced argument types should be compared. |
3429 | | Sema::TemplateDeductionResult Sema::FinishTemplateArgumentDeduction( |
3430 | | FunctionTemplateDecl *FunctionTemplate, |
3431 | | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
3432 | | unsigned NumExplicitlySpecified, FunctionDecl *&Specialization, |
3433 | | TemplateDeductionInfo &Info, |
3434 | | SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs, |
3435 | 729k | bool PartialOverloading, llvm::function_ref<bool()> CheckNonDependent) { |
3436 | | // Unevaluated SFINAE context. |
3437 | 729k | EnterExpressionEvaluationContext Unevaluated( |
3438 | 729k | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
3439 | 729k | SFINAETrap Trap(*this); |
3440 | | |
3441 | | // Enter a new template instantiation context while we instantiate the |
3442 | | // actual function declaration. |
3443 | 729k | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
3444 | 729k | InstantiatingTemplate Inst( |
3445 | 729k | *this, Info.getLocation(), FunctionTemplate, DeducedArgs, |
3446 | 729k | CodeSynthesisContext::DeducedTemplateArgumentSubstitution, Info); |
3447 | 729k | if (Inst.isInvalid()) |
3448 | 2.23k | return TDK_InstantiationDepth; |
3449 | | |
3450 | 727k | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
3451 | | |
3452 | | // C++ [temp.deduct.type]p2: |
3453 | | // [...] or if any template argument remains neither deduced nor |
3454 | | // explicitly specified, template argument deduction fails. |
3455 | 727k | SmallVector<TemplateArgument, 4> Builder; |
3456 | 727k | if (auto Result = ConvertDeducedTemplateArguments( |
3457 | 727k | *this, FunctionTemplate, /*IsDeduced*/true, Deduced, Info, Builder, |
3458 | 727k | CurrentInstantiationScope, NumExplicitlySpecified, |
3459 | 727k | PartialOverloading)) |
3460 | 70.1k | return Result; |
3461 | | |
3462 | | // C++ [temp.deduct.call]p10: [DR1391] |
3463 | | // If deduction succeeds for all parameters that contain |
3464 | | // template-parameters that participate in template argument deduction, |
3465 | | // and all template arguments are explicitly specified, deduced, or |
3466 | | // obtained from default template arguments, remaining parameters are then |
3467 | | // compared with the corresponding arguments. For each remaining parameter |
3468 | | // P with a type that was non-dependent before substitution of any |
3469 | | // explicitly-specified template arguments, if the corresponding argument |
3470 | | // A cannot be implicitly converted to P, deduction fails. |
3471 | 657k | if (CheckNonDependent()) |
3472 | 38.6k | return TDK_NonDependentConversionFailure; |
3473 | | |
3474 | | // Form the template argument list from the deduced template arguments. |
3475 | 618k | TemplateArgumentList *DeducedArgumentList |
3476 | 618k | = TemplateArgumentList::CreateCopy(Context, Builder); |
3477 | 618k | Info.reset(DeducedArgumentList); |
3478 | | |
3479 | | // Substitute the deduced template arguments into the function template |
3480 | | // declaration to produce the function template specialization. |
3481 | 618k | DeclContext *Owner = FunctionTemplate->getDeclContext(); |
3482 | 618k | if (FunctionTemplate->getFriendObjectKind()) |
3483 | 55 | Owner = FunctionTemplate->getLexicalDeclContext(); |
3484 | 618k | MultiLevelTemplateArgumentList SubstArgs(*DeducedArgumentList); |
3485 | 618k | Specialization = cast_or_null<FunctionDecl>( |
3486 | 618k | SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, SubstArgs)); |
3487 | 618k | if (!Specialization || Specialization->isInvalidDecl()560k ) |
3488 | 58.5k | return TDK_SubstitutionFailure; |
3489 | | |
3490 | 560k | assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == |
3491 | 560k | FunctionTemplate->getCanonicalDecl()); |
3492 | | |
3493 | | // If the template argument list is owned by the function template |
3494 | | // specialization, release it. |
3495 | 560k | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList && |
3496 | 560k | !Trap.hasErrorOccurred()0 ) |
3497 | 0 | Info.take(); |
3498 | | |
3499 | | // There may have been an error that did not prevent us from constructing a |
3500 | | // declaration. Mark the declaration invalid and return with a substitution |
3501 | | // failure. |
3502 | 560k | if (Trap.hasErrorOccurred()) { |
3503 | 112 | Specialization->setInvalidDecl(true); |
3504 | 112 | return TDK_SubstitutionFailure; |
3505 | 112 | } |
3506 | | |
3507 | | // C++2a [temp.deduct]p5 |
3508 | | // [...] When all template arguments have been deduced [...] all uses of |
3509 | | // template parameters [...] are replaced with the corresponding deduced |
3510 | | // or default argument values. |
3511 | | // [...] If the function template has associated constraints |
3512 | | // ([temp.constr.decl]), those constraints are checked for satisfaction |
3513 | | // ([temp.constr.constr]). If the constraints are not satisfied, type |
3514 | | // deduction fails. |
3515 | 560k | if (!PartialOverloading || |
3516 | 560k | (Builder.size() == FunctionTemplate->getTemplateParameters()->size())87 ) { |
3517 | 560k | if (CheckInstantiatedFunctionTemplateConstraints(Info.getLocation(), |
3518 | 560k | Specialization, Builder, Info.AssociatedConstraintsSatisfaction)) |
3519 | 1 | return TDK_MiscellaneousDeductionFailure; |
3520 | | |
3521 | 560k | if (!Info.AssociatedConstraintsSatisfaction.IsSatisfied) { |
3522 | 1.39k | Info.reset(TemplateArgumentList::CreateCopy(Context, Builder)); |
3523 | 1.39k | return TDK_ConstraintsNotSatisfied; |
3524 | 1.39k | } |
3525 | 560k | } |
3526 | | |
3527 | 558k | if (OriginalCallArgs) { |
3528 | | // C++ [temp.deduct.call]p4: |
3529 | | // In general, the deduction process attempts to find template argument |
3530 | | // values that will make the deduced A identical to A (after the type A |
3531 | | // is transformed as described above). [...] |
3532 | 529k | llvm::SmallDenseMap<std::pair<unsigned, QualType>, QualType> DeducedATypes; |
3533 | 833k | for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I303k ) { |
3534 | 304k | OriginalCallArg OriginalArg = (*OriginalCallArgs)[I]; |
3535 | | |
3536 | 304k | auto ParamIdx = OriginalArg.ArgIdx; |
3537 | 304k | if (ParamIdx >= Specialization->getNumParams()) |
3538 | | // FIXME: This presumably means a pack ended up smaller than we |
3539 | | // expected while deducing. Should this not result in deduction |
3540 | | // failure? Can it even happen? |
3541 | 0 | continue; |
3542 | | |
3543 | 304k | QualType DeducedA; |
3544 | 304k | if (!OriginalArg.DecomposedParam) { |
3545 | | // P is one of the function parameters, just look up its substituted |
3546 | | // type. |
3547 | 304k | DeducedA = Specialization->getParamDecl(ParamIdx)->getType(); |
3548 | 304k | } else { |
3549 | | // P is a decomposed element of a parameter corresponding to a |
3550 | | // braced-init-list argument. Substitute back into P to find the |
3551 | | // deduced A. |
3552 | 389 | QualType &CacheEntry = |
3553 | 389 | DeducedATypes[{ParamIdx, OriginalArg.OriginalParamType}]; |
3554 | 389 | if (CacheEntry.isNull()) { |
3555 | 111 | ArgumentPackSubstitutionIndexRAII PackIndex( |
3556 | 111 | *this, getPackIndexForParam(*this, FunctionTemplate, SubstArgs, |
3557 | 111 | ParamIdx)); |
3558 | 111 | CacheEntry = |
3559 | 111 | SubstType(OriginalArg.OriginalParamType, SubstArgs, |
3560 | 111 | Specialization->getTypeSpecStartLoc(), |
3561 | 111 | Specialization->getDeclName()); |
3562 | 111 | } |
3563 | 389 | DeducedA = CacheEntry; |
3564 | 389 | } |
3565 | | |
3566 | 304k | if (auto TDK = |
3567 | 304k | CheckOriginalCallArgDeduction(*this, Info, OriginalArg, DeducedA)) |
3568 | 993 | return TDK; |
3569 | 304k | } |
3570 | 529k | } |
3571 | | |
3572 | | // If we suppressed any diagnostics while performing template argument |
3573 | | // deduction, and if we haven't already instantiated this declaration, |
3574 | | // keep track of these diagnostics. They'll be emitted if this specialization |
3575 | | // is actually used. |
3576 | 557k | if (Info.diag_begin() != Info.diag_end()) { |
3577 | 30.0k | SuppressedDiagnosticsMap::iterator |
3578 | 30.0k | Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); |
3579 | 30.0k | if (Pos == SuppressedDiagnostics.end()) |
3580 | 9.84k | SuppressedDiagnostics[Specialization->getCanonicalDecl()] |
3581 | 9.84k | .append(Info.diag_begin(), Info.diag_end()); |
3582 | 30.0k | } |
3583 | | |
3584 | 557k | return TDK_Success; |
3585 | 558k | } |
3586 | | |
3587 | | /// Gets the type of a function for template-argument-deducton |
3588 | | /// purposes when it's considered as part of an overload set. |
3589 | | static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R, |
3590 | 478 | FunctionDecl *Fn) { |
3591 | | // We may need to deduce the return type of the function now. |
3592 | 478 | if (S.getLangOpts().CPlusPlus14 && Fn->getReturnType()->isUndeducedType()304 && |
3593 | 478 | S.DeduceReturnType(Fn, R.Expression->getExprLoc(), /*Diagnose*/ false)36 ) |
3594 | 0 | return {}; |
3595 | | |
3596 | 478 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
3597 | 111 | if (Method->isInstance()) { |
3598 | | // An instance method that's referenced in a form that doesn't |
3599 | | // look like a member pointer is just invalid. |
3600 | 45 | if (!R.HasFormOfMemberPointer) |
3601 | 18 | return {}; |
3602 | | |
3603 | 27 | return S.Context.getMemberPointerType(Fn->getType(), |
3604 | 27 | S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); |
3605 | 45 | } |
3606 | | |
3607 | 433 | if (!R.IsAddressOfOperand) return Fn->getType()310 ; |
3608 | 123 | return S.Context.getPointerType(Fn->getType()); |
3609 | 433 | } |
3610 | | |
3611 | | /// Apply the deduction rules for overload sets. |
3612 | | /// |
3613 | | /// \return the null type if this argument should be treated as an |
3614 | | /// undeduced context |
3615 | | static QualType |
3616 | | ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, |
3617 | | Expr *Arg, QualType ParamType, |
3618 | 1.07k | bool ParamWasReference) { |
3619 | | |
3620 | 1.07k | OverloadExpr::FindResult R = OverloadExpr::find(Arg); |
3621 | | |
3622 | 1.07k | OverloadExpr *Ovl = R.Expression; |
3623 | | |
3624 | | // C++0x [temp.deduct.call]p4 |
3625 | 1.07k | unsigned TDF = 0; |
3626 | 1.07k | if (ParamWasReference) |
3627 | 378 | TDF |= TDF_ParamWithReferenceType; |
3628 | 1.07k | if (R.IsAddressOfOperand) |
3629 | 197 | TDF |= TDF_IgnoreQualifiers; |
3630 | | |
3631 | | // C++0x [temp.deduct.call]p6: |
3632 | | // When P is a function type, pointer to function type, or pointer |
3633 | | // to member function type: |
3634 | | |
3635 | 1.07k | if (!ParamType->isFunctionType() && |
3636 | 1.07k | !ParamType->isFunctionPointerType()1.06k && |
3637 | 1.07k | !ParamType->isMemberFunctionPointerType()784 ) { |
3638 | 764 | if (Ovl->hasExplicitTemplateArgs()) { |
3639 | | // But we can still look for an explicit specialization. |
3640 | 87 | if (FunctionDecl *ExplicitSpec |
3641 | 87 | = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) |
3642 | 83 | return GetTypeOfFunction(S, R, ExplicitSpec); |
3643 | 87 | } |
3644 | | |
3645 | 681 | DeclAccessPair DAP; |
3646 | 681 | if (FunctionDecl *Viable = |
3647 | 681 | S.resolveAddressOfSingleOverloadCandidate(Arg, DAP)) |
3648 | 6 | return GetTypeOfFunction(S, R, Viable); |
3649 | | |
3650 | 675 | return {}; |
3651 | 681 | } |
3652 | | |
3653 | | // Gather the explicit template arguments, if any. |
3654 | 306 | TemplateArgumentListInfo ExplicitTemplateArgs; |
3655 | 306 | if (Ovl->hasExplicitTemplateArgs()) |
3656 | 97 | Ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
3657 | 306 | QualType Match; |
3658 | 306 | for (UnresolvedSetIterator I = Ovl->decls_begin(), |
3659 | 631 | E = Ovl->decls_end(); I != E; ++I325 ) { |
3660 | 469 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
3661 | | |
3662 | 469 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) { |
3663 | | // - If the argument is an overload set containing one or more |
3664 | | // function templates, the parameter is treated as a |
3665 | | // non-deduced context. |
3666 | 177 | if (!Ovl->hasExplicitTemplateArgs()) |
3667 | 70 | return {}; |
3668 | | |
3669 | | // Otherwise, see if we can resolve a function type |
3670 | 107 | FunctionDecl *Specialization = nullptr; |
3671 | 107 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
3672 | 107 | if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs, |
3673 | 107 | Specialization, Info)) |
3674 | 10 | continue; |
3675 | | |
3676 | 97 | D = Specialization; |
3677 | 97 | } |
3678 | | |
3679 | 389 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
3680 | 389 | QualType ArgType = GetTypeOfFunction(S, R, Fn); |
3681 | 389 | if (ArgType.isNull()) continue18 ; |
3682 | | |
3683 | | // Function-to-pointer conversion. |
3684 | 371 | if (!ParamWasReference && ParamType->isPointerType()363 && |
3685 | 371 | ArgType->isFunctionType()332 ) |
3686 | 253 | ArgType = S.Context.getPointerType(ArgType); |
3687 | | |
3688 | | // - If the argument is an overload set (not containing function |
3689 | | // templates), trial argument deduction is attempted using each |
3690 | | // of the members of the set. If deduction succeeds for only one |
3691 | | // of the overload set members, that member is used as the |
3692 | | // argument value for the deduction. If deduction succeeds for |
3693 | | // more than one member of the overload set the parameter is |
3694 | | // treated as a non-deduced context. |
3695 | | |
3696 | | // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: |
3697 | | // Type deduction is done independently for each P/A pair, and |
3698 | | // the deduced template argument values are then combined. |
3699 | | // So we do not reject deductions which were made elsewhere. |
3700 | 371 | SmallVector<DeducedTemplateArgument, 8> |
3701 | 371 | Deduced(TemplateParams->size()); |
3702 | 371 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
3703 | 371 | Sema::TemplateDeductionResult Result |
3704 | 371 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
3705 | 371 | ArgType, Info, Deduced, TDF); |
3706 | 371 | if (Result) continue89 ; |
3707 | 282 | if (!Match.isNull()) |
3708 | 74 | return {}; |
3709 | 208 | Match = ArgType; |
3710 | 208 | } |
3711 | | |
3712 | 162 | return Match; |
3713 | 306 | } |
3714 | | |
3715 | | /// Perform the adjustments to the parameter and argument types |
3716 | | /// described in C++ [temp.deduct.call]. |
3717 | | /// |
3718 | | /// \returns true if the caller should not attempt to perform any template |
3719 | | /// argument deduction based on this P/A pair because the argument is an |
3720 | | /// overloaded function set that could not be resolved. |
3721 | | static bool AdjustFunctionParmAndArgTypesForDeduction( |
3722 | | Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, |
3723 | 1.27M | QualType &ParamType, QualType &ArgType, Expr *Arg, unsigned &TDF) { |
3724 | | // C++0x [temp.deduct.call]p3: |
3725 | | // If P is a cv-qualified type, the top level cv-qualifiers of P's type |
3726 | | // are ignored for type deduction. |
3727 | 1.27M | if (ParamType.hasQualifiers()) |
3728 | 5.07k | ParamType = ParamType.getUnqualifiedType(); |
3729 | | |
3730 | | // [...] If P is a reference type, the type referred to by P is |
3731 | | // used for type deduction. |
3732 | 1.27M | const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); |
3733 | 1.27M | if (ParamRefType) |
3734 | 983k | ParamType = ParamRefType->getPointeeType(); |
3735 | | |
3736 | | // Overload sets usually make this parameter an undeduced context, |
3737 | | // but there are sometimes special circumstances. Typically |
3738 | | // involving a template-id-expr. |
3739 | 1.27M | if (ArgType == S.Context.OverloadTy) { |
3740 | 1.07k | ArgType = ResolveOverloadForDeduction(S, TemplateParams, |
3741 | 1.07k | Arg, ParamType, |
3742 | 1.07k | ParamRefType != nullptr); |
3743 | 1.07k | if (ArgType.isNull()) |
3744 | 853 | return true; |
3745 | 1.07k | } |
3746 | | |
3747 | 1.27M | if (ParamRefType) { |
3748 | | // If the argument has incomplete array type, try to complete its type. |
3749 | 983k | if (ArgType->isIncompleteArrayType()) |
3750 | 12 | ArgType = S.getCompletedType(Arg); |
3751 | | |
3752 | | // C++1z [temp.deduct.call]p3: |
3753 | | // If P is a forwarding reference and the argument is an lvalue, the type |
3754 | | // "lvalue reference to A" is used in place of A for type deduction. |
3755 | 983k | if (isForwardingReference(QualType(ParamRefType, 0), FirstInnerIndex) && |
3756 | 983k | Arg->isLValue()100k ) { |
3757 | 42.0k | if (S.getLangOpts().OpenCL && !ArgType.hasAddressSpace()2 ) |
3758 | 1 | ArgType = S.Context.getAddrSpaceQualType( |
3759 | 1 | ArgType, S.Context.getDefaultOpenCLPointeeAddrSpace()); |
3760 | 42.0k | ArgType = S.Context.getLValueReferenceType(ArgType); |
3761 | 42.0k | } |
3762 | 983k | } else { |
3763 | | // C++ [temp.deduct.call]p2: |
3764 | | // If P is not a reference type: |
3765 | | // - If A is an array type, the pointer type produced by the |
3766 | | // array-to-pointer standard conversion (4.2) is used in place of |
3767 | | // A for type deduction; otherwise, |
3768 | 287k | if (ArgType->isArrayType()) |
3769 | 4.09k | ArgType = S.Context.getArrayDecayedType(ArgType); |
3770 | | // - If A is a function type, the pointer type produced by the |
3771 | | // function-to-pointer standard conversion (4.3) is used in place |
3772 | | // of A for type deduction; otherwise, |
3773 | 283k | else if (ArgType->isFunctionType()) |
3774 | 336 | ArgType = S.Context.getPointerType(ArgType); |
3775 | 282k | else { |
3776 | | // - If A is a cv-qualified type, the top level cv-qualifiers of A's |
3777 | | // type are ignored for type deduction. |
3778 | 282k | ArgType = ArgType.getUnqualifiedType(); |
3779 | 282k | } |
3780 | 287k | } |
3781 | | |
3782 | | // C++0x [temp.deduct.call]p4: |
3783 | | // In general, the deduction process attempts to find template argument |
3784 | | // values that will make the deduced A identical to A (after the type A |
3785 | | // is transformed as described above). [...] |
3786 | 1.27M | TDF = TDF_SkipNonDependent; |
3787 | | |
3788 | | // - If the original P is a reference type, the deduced A (i.e., the |
3789 | | // type referred to by the reference) can be more cv-qualified than |
3790 | | // the transformed A. |
3791 | 1.27M | if (ParamRefType) |
3792 | 983k | TDF |= TDF_ParamWithReferenceType; |
3793 | | // - The transformed A can be another pointer or pointer to member |
3794 | | // type that can be converted to the deduced A via a qualification |
3795 | | // conversion (4.4). |
3796 | 1.27M | if (ArgType->isPointerType() || ArgType->isMemberPointerType()1.09M || |
3797 | 1.27M | ArgType->isObjCObjectPointerType()1.09M ) |
3798 | 175k | TDF |= TDF_IgnoreQualifiers; |
3799 | | // - If P is a class and P has the form simple-template-id, then the |
3800 | | // transformed A can be a derived class of the deduced A. Likewise, |
3801 | | // if P is a pointer to a class of the form simple-template-id, the |
3802 | | // transformed A can be a pointer to a derived class pointed to by |
3803 | | // the deduced A. |
3804 | 1.27M | if (isSimpleTemplateIdType(ParamType) || |
3805 | 1.27M | (403k isa<PointerType>(ParamType)403k && |
3806 | 403k | isSimpleTemplateIdType( |
3807 | 58.5k | ParamType->castAs<PointerType>()->getPointeeType()))) |
3808 | 875k | TDF |= TDF_DerivedClass; |
3809 | | |
3810 | 1.27M | return false; |
3811 | 1.27M | } |
3812 | | |
3813 | | static bool |
3814 | | hasDeducibleTemplateParameters(Sema &S, FunctionTemplateDecl *FunctionTemplate, |
3815 | | QualType T); |
3816 | | |
3817 | | static Sema::TemplateDeductionResult DeduceTemplateArgumentsFromCallArgument( |
3818 | | Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, |
3819 | | QualType ParamType, Expr *Arg, TemplateDeductionInfo &Info, |
3820 | | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
3821 | | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, |
3822 | | bool DecomposedParam, unsigned ArgIdx, unsigned TDF); |
3823 | | |
3824 | | /// Attempt template argument deduction from an initializer list |
3825 | | /// deemed to be an argument in a function call. |
3826 | | static Sema::TemplateDeductionResult DeduceFromInitializerList( |
3827 | | Sema &S, TemplateParameterList *TemplateParams, QualType AdjustedParamType, |
3828 | | InitListExpr *ILE, TemplateDeductionInfo &Info, |
3829 | | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
3830 | | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, unsigned ArgIdx, |
3831 | 837 | unsigned TDF) { |
3832 | | // C++ [temp.deduct.call]p1: (CWG 1591) |
3833 | | // If removing references and cv-qualifiers from P gives |
3834 | | // std::initializer_list<P0> or P0[N] for some P0 and N and the argument is |
3835 | | // a non-empty initializer list, then deduction is performed instead for |
3836 | | // each element of the initializer list, taking P0 as a function template |
3837 | | // parameter type and the initializer element as its argument |
3838 | | // |
3839 | | // We've already removed references and cv-qualifiers here. |
3840 | 837 | if (!ILE->getNumInits()) |
3841 | 33 | return Sema::TDK_Success; |
3842 | | |
3843 | 804 | QualType ElTy; |
3844 | 804 | auto *ArrTy = S.Context.getAsArrayType(AdjustedParamType); |
3845 | 804 | if (ArrTy) |
3846 | 400 | ElTy = ArrTy->getElementType(); |
3847 | 404 | else if (!S.isStdInitializerList(AdjustedParamType, &ElTy)) { |
3848 | | // Otherwise, an initializer list argument causes the parameter to be |
3849 | | // considered a non-deduced context |
3850 | 313 | return Sema::TDK_Success; |
3851 | 313 | } |
3852 | | |
3853 | | // Resolving a core issue: a braced-init-list containing any designators is |
3854 | | // a non-deduced context. |
3855 | 491 | for (Expr *E : ILE->inits()) |
3856 | 1.10k | if (isa<DesignatedInitExpr>(E)) |
3857 | 4 | return Sema::TDK_Success; |
3858 | | |
3859 | | // Deduction only needs to be done for dependent types. |
3860 | 487 | if (ElTy->isDependentType()) { |
3861 | 966 | for (Expr *E : ILE->inits()) { |
3862 | 966 | if (auto Result = DeduceTemplateArgumentsFromCallArgument( |
3863 | 966 | S, TemplateParams, 0, ElTy, E, Info, Deduced, OriginalCallArgs, true, |
3864 | 966 | ArgIdx, TDF)) |
3865 | 72 | return Result; |
3866 | 966 | } |
3867 | 461 | } |
3868 | | |
3869 | | // in the P0[N] case, if N is a non-type template parameter, N is deduced |
3870 | | // from the length of the initializer list. |
3871 | 415 | if (auto *DependentArrTy = dyn_cast_or_null<DependentSizedArrayType>(ArrTy)) { |
3872 | | // Determine the array bound is something we can deduce. |
3873 | 315 | if (const NonTypeTemplateParmDecl *NTTP = |
3874 | 315 | getDeducedParameterFromExpr(Info, DependentArrTy->getSizeExpr())) { |
3875 | | // We can perform template argument deduction for the given non-type |
3876 | | // template parameter. |
3877 | | // C++ [temp.deduct.type]p13: |
3878 | | // The type of N in the type T[N] is std::size_t. |
3879 | 315 | QualType T = S.Context.getSizeType(); |
3880 | 315 | llvm::APInt Size(S.Context.getIntWidth(T), ILE->getNumInits()); |
3881 | 315 | if (auto Result = DeduceNonTypeTemplateArgument( |
3882 | 315 | S, TemplateParams, NTTP, llvm::APSInt(Size), T, |
3883 | 315 | /*ArrayBound=*/true, Info, Deduced)) |
3884 | 11 | return Result; |
3885 | 315 | } |
3886 | 315 | } |
3887 | | |
3888 | 404 | return Sema::TDK_Success; |
3889 | 415 | } |
3890 | | |
3891 | | /// Perform template argument deduction per [temp.deduct.call] for a |
3892 | | /// single parameter / argument pair. |
3893 | | static Sema::TemplateDeductionResult DeduceTemplateArgumentsFromCallArgument( |
3894 | | Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, |
3895 | | QualType ParamType, Expr *Arg, TemplateDeductionInfo &Info, |
3896 | | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
3897 | | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, |
3898 | 1.27M | bool DecomposedParam, unsigned ArgIdx, unsigned TDF) { |
3899 | 1.27M | QualType ArgType = Arg->getType(); |
3900 | 1.27M | QualType OrigParamType = ParamType; |
3901 | | |
3902 | | // If P is a reference type [...] |
3903 | | // If P is a cv-qualified type [...] |
3904 | 1.27M | if (AdjustFunctionParmAndArgTypesForDeduction( |
3905 | 1.27M | S, TemplateParams, FirstInnerIndex, ParamType, ArgType, Arg, TDF)) |
3906 | 853 | return Sema::TDK_Success; |
3907 | | |
3908 | | // If [...] the argument is a non-empty initializer list [...] |
3909 | 1.27M | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) |
3910 | 837 | return DeduceFromInitializerList(S, TemplateParams, ParamType, ILE, Info, |
3911 | 837 | Deduced, OriginalCallArgs, ArgIdx, TDF); |
3912 | | |
3913 | | // [...] the deduction process attempts to find template argument values |
3914 | | // that will make the deduced A identical to A |
3915 | | // |
3916 | | // Keep track of the argument type and corresponding parameter index, |
3917 | | // so we can check for compatibility between the deduced A and A. |
3918 | 1.26M | OriginalCallArgs.push_back( |
3919 | 1.26M | Sema::OriginalCallArg(OrigParamType, DecomposedParam, ArgIdx, ArgType)); |
3920 | 1.26M | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
3921 | 1.26M | ArgType, Info, Deduced, TDF); |
3922 | 1.27M | } |
3923 | | |
3924 | | /// Perform template argument deduction from a function call |
3925 | | /// (C++ [temp.deduct.call]). |
3926 | | /// |
3927 | | /// \param FunctionTemplate the function template for which we are performing |
3928 | | /// template argument deduction. |
3929 | | /// |
3930 | | /// \param ExplicitTemplateArgs the explicit template arguments provided |
3931 | | /// for this call. |
3932 | | /// |
3933 | | /// \param Args the function call arguments |
3934 | | /// |
3935 | | /// \param Specialization if template argument deduction was successful, |
3936 | | /// this will be set to the function template specialization produced by |
3937 | | /// template argument deduction. |
3938 | | /// |
3939 | | /// \param Info the argument will be updated to provide additional information |
3940 | | /// about template argument deduction. |
3941 | | /// |
3942 | | /// \param CheckNonDependent A callback to invoke to check conversions for |
3943 | | /// non-dependent parameters, between deduction and substitution, per DR1391. |
3944 | | /// If this returns true, substitution will be skipped and we return |
3945 | | /// TDK_NonDependentConversionFailure. The callback is passed the parameter |
3946 | | /// types (after substituting explicit template arguments). |
3947 | | /// |
3948 | | /// \returns the result of template argument deduction. |
3949 | | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
3950 | | FunctionTemplateDecl *FunctionTemplate, |
3951 | | TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args, |
3952 | | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
3953 | | bool PartialOverloading, |
3954 | 1.74M | llvm::function_ref<bool(ArrayRef<QualType>)> CheckNonDependent) { |
3955 | 1.74M | if (FunctionTemplate->isInvalidDecl()) |
3956 | 12 | return TDK_Invalid; |
3957 | | |
3958 | 1.74M | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
3959 | 1.74M | unsigned NumParams = Function->getNumParams(); |
3960 | | |
3961 | 1.74M | unsigned FirstInnerIndex = getFirstInnerIndex(FunctionTemplate); |
3962 | | |
3963 | | // C++ [temp.deduct.call]p1: |
3964 | | // Template argument deduction is done by comparing each function template |
3965 | | // parameter type (call it P) with the type of the corresponding argument |
3966 | | // of the call (call it A) as described below. |
3967 | 1.74M | if (Args.size() < Function->getMinRequiredArguments() && !PartialOverloading193k ) |
3968 | 193k | return TDK_TooFewArguments; |
3969 | 1.55M | else if (TooManyArguments(NumParams, Args.size(), PartialOverloading)) { |
3970 | 112k | const auto *Proto = Function->getType()->castAs<FunctionProtoType>(); |
3971 | 112k | if (Proto->isTemplateVariadic()) |
3972 | 2.95k | /* Do nothing */; |
3973 | 109k | else if (!Proto->isVariadic()) |
3974 | 62.0k | return TDK_TooManyArguments; |
3975 | 112k | } |
3976 | | |
3977 | | // The types of the parameters from which we will perform template argument |
3978 | | // deduction. |
3979 | 1.48M | LocalInstantiationScope InstScope(*this); |
3980 | 1.48M | TemplateParameterList *TemplateParams |
3981 | 1.48M | = FunctionTemplate->getTemplateParameters(); |
3982 | 1.48M | SmallVector<DeducedTemplateArgument, 4> Deduced; |
3983 | 1.48M | SmallVector<QualType, 8> ParamTypes; |
3984 | 1.48M | unsigned NumExplicitlySpecified = 0; |
3985 | 1.48M | if (ExplicitTemplateArgs) { |
3986 | 332k | TemplateDeductionResult Result; |
3987 | 332k | runWithSufficientStackSpace(Info.getLocation(), [&] { |
3988 | 332k | Result = SubstituteExplicitTemplateArguments( |
3989 | 332k | FunctionTemplate, *ExplicitTemplateArgs, Deduced, ParamTypes, nullptr, |
3990 | 332k | Info); |
3991 | 332k | }); |
3992 | 332k | if (Result) |
3993 | 7.71k | return Result; |
3994 | | |
3995 | 324k | NumExplicitlySpecified = Deduced.size(); |
3996 | 1.15M | } else { |
3997 | | // Just fill in the parameter types from the function declaration. |
3998 | 3.28M | for (unsigned I = 0; I != NumParams; ++I2.13M ) |
3999 | 2.13M | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
4000 | 1.15M | } |
4001 | | |
4002 | 1.48M | SmallVector<OriginalCallArg, 8> OriginalCallArgs; |
4003 | | |
4004 | | // Deduce an argument of type ParamType from an expression with index ArgIdx. |
4005 | 1.60M | auto DeduceCallArgument = [&](QualType ParamType, unsigned ArgIdx) { |
4006 | | // C++ [demp.deduct.call]p1: (DR1391) |
4007 | | // Template argument deduction is done by comparing each function template |
4008 | | // parameter that contains template-parameters that participate in |
4009 | | // template argument deduction ... |
4010 | 1.60M | if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
4011 | 365k | return Sema::TDK_Success; |
4012 | | |
4013 | | // ... with the type of the corresponding argument |
4014 | 1.23M | return DeduceTemplateArgumentsFromCallArgument( |
4015 | 1.23M | *this, TemplateParams, FirstInnerIndex, ParamType, Args[ArgIdx], Info, Deduced, |
4016 | 1.23M | OriginalCallArgs, /*Decomposed*/false, ArgIdx, /*TDF*/ 0); |
4017 | 1.60M | }; |
4018 | | |
4019 | | // Deduce template arguments from the function parameters. |
4020 | 1.48M | Deduced.resize(TemplateParams->size()); |
4021 | 1.48M | SmallVector<QualType, 8> ParamTypesForArgChecking; |
4022 | 1.48M | for (unsigned ParamIdx = 0, NumParamTypes = ParamTypes.size(), ArgIdx = 0; |
4023 | 2.29M | ParamIdx != NumParamTypes; ++ParamIdx814k ) { |
4024 | 1.67M | QualType ParamType = ParamTypes[ParamIdx]; |
4025 | | |
4026 | 1.67M | const PackExpansionType *ParamExpansion = |
4027 | 1.67M | dyn_cast<PackExpansionType>(ParamType); |
4028 | 1.67M | if (!ParamExpansion) { |
4029 | | // Simple case: matching a function parameter to a function argument. |
4030 | 1.65M | if (ArgIdx >= Args.size()) |
4031 | 71.4k | break; |
4032 | | |
4033 | 1.58M | ParamTypesForArgChecking.push_back(ParamType); |
4034 | 1.58M | if (auto Result = DeduceCallArgument(ParamType, ArgIdx++)) |
4035 | 787k | return Result; |
4036 | | |
4037 | 799k | continue; |
4038 | 1.58M | } |
4039 | | |
4040 | 15.2k | QualType ParamPattern = ParamExpansion->getPattern(); |
4041 | 15.2k | PackDeductionScope PackScope(*this, TemplateParams, Deduced, Info, |
4042 | 15.2k | ParamPattern); |
4043 | | |
4044 | | // C++0x [temp.deduct.call]p1: |
4045 | | // For a function parameter pack that occurs at the end of the |
4046 | | // parameter-declaration-list, the type A of each remaining argument of |
4047 | | // the call is compared with the type P of the declarator-id of the |
4048 | | // function parameter pack. Each comparison deduces template arguments |
4049 | | // for subsequent positions in the template parameter packs expanded by |
4050 | | // the function parameter pack. When a function parameter pack appears |
4051 | | // in a non-deduced context [not at the end of the list], the type of |
4052 | | // that parameter pack is never deduced. |
4053 | | // |
4054 | | // FIXME: The above rule allows the size of the parameter pack to change |
4055 | | // after we skip it (in the non-deduced case). That makes no sense, so |
4056 | | // we instead notionally deduce the pack against N arguments, where N is |
4057 | | // the length of the explicitly-specified pack if it's expanded by the |
4058 | | // parameter pack and 0 otherwise, and we treat each deduction as a |
4059 | | // non-deduced context. |
4060 | 15.2k | if (ParamIdx + 1 == NumParamTypes || PackScope.hasFixedArity()164 ) { |
4061 | 32.1k | for (; ArgIdx < Args.size() && PackScope.hasNextElement()16.9k ; |
4062 | 16.9k | PackScope.nextPackElement(), ++ArgIdx16.9k ) { |
4063 | 16.9k | ParamTypesForArgChecking.push_back(ParamPattern); |
4064 | 16.9k | if (auto Result = DeduceCallArgument(ParamPattern, ArgIdx)) |
4065 | 10 | return Result; |
4066 | 16.9k | } |
4067 | 15.1k | } else { |
4068 | | // If the parameter type contains an explicitly-specified pack that we |
4069 | | // could not expand, skip the number of parameters notionally created |
4070 | | // by the expansion. |
4071 | 162 | Optional<unsigned> NumExpansions = ParamExpansion->getNumExpansions(); |
4072 | 162 | if (NumExpansions && !PackScope.isPartiallyExpanded()12 ) { |
4073 | 36 | for (unsigned I = 0; I != *NumExpansions && ArgIdx < Args.size()24 ; |
4074 | 24 | ++I, ++ArgIdx) { |
4075 | 24 | ParamTypesForArgChecking.push_back(ParamPattern); |
4076 | | // FIXME: Should we add OriginalCallArgs for these? What if the |
4077 | | // corresponding argument is a list? |
4078 | 24 | PackScope.nextPackElement(); |
4079 | 24 | } |
4080 | 12 | } |
4081 | 162 | } |
4082 | | |
4083 | | // Build argument packs for each of the parameter packs expanded by this |
4084 | | // pack expansion. |
4085 | 15.2k | if (auto Result = PackScope.finish()) |
4086 | 40 | return Result; |
4087 | 15.2k | } |
4088 | | |
4089 | | // Capture the context in which the function call is made. This is the context |
4090 | | // that is needed when the accessibility of template arguments is checked. |
4091 | 694k | DeclContext *CallingCtx = CurContext; |
4092 | | |
4093 | 694k | TemplateDeductionResult Result; |
4094 | 694k | runWithSufficientStackSpace(Info.getLocation(), [&] { |
4095 | 694k | Result = FinishTemplateArgumentDeduction( |
4096 | 694k | FunctionTemplate, Deduced, NumExplicitlySpecified, Specialization, Info, |
4097 | 694k | &OriginalCallArgs, PartialOverloading, [&, CallingCtx]() { |
4098 | 625k | ContextRAII SavedContext(*this, CallingCtx); |
4099 | 625k | return CheckNonDependent(ParamTypesForArgChecking); |
4100 | 625k | }); |
4101 | 694k | }); |
4102 | 694k | return Result; |
4103 | 1.48M | } |
4104 | | |
4105 | | QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType, |
4106 | | QualType FunctionType, |
4107 | 339k | bool AdjustExceptionSpec) { |
4108 | 339k | if (ArgFunctionType.isNull()) |
4109 | 204 | return ArgFunctionType; |
4110 | | |
4111 | 339k | const auto *FunctionTypeP = FunctionType->castAs<FunctionProtoType>(); |
4112 | 339k | const auto *ArgFunctionTypeP = ArgFunctionType->castAs<FunctionProtoType>(); |
4113 | 339k | FunctionProtoType::ExtProtoInfo EPI = ArgFunctionTypeP->getExtProtoInfo(); |
4114 | 339k | bool Rebuild = false; |
4115 | | |
4116 | 339k | CallingConv CC = FunctionTypeP->getCallConv(); |
4117 | 339k | if (EPI.ExtInfo.getCC() != CC) { |
4118 | 182 | EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC); |
4119 | 182 | Rebuild = true; |
4120 | 182 | } |
4121 | | |
4122 | 339k | bool NoReturn = FunctionTypeP->getNoReturnAttr(); |
4123 | 339k | if (EPI.ExtInfo.getNoReturn() != NoReturn) { |
4124 | 1 | EPI.ExtInfo = EPI.ExtInfo.withNoReturn(NoReturn); |
4125 | 1 | Rebuild = true; |
4126 | 1 | } |
4127 | | |
4128 | 339k | if (AdjustExceptionSpec && (201k FunctionTypeP->hasExceptionSpec()201k || |
4129 | 201k | ArgFunctionTypeP->hasExceptionSpec()164k )) { |
4130 | 43.5k | EPI.ExceptionSpec = FunctionTypeP->getExtProtoInfo().ExceptionSpec; |
4131 | 43.5k | Rebuild = true; |
4132 | 43.5k | } |
4133 | | |
4134 | 339k | if (!Rebuild) |
4135 | 295k | return ArgFunctionType; |
4136 | | |
4137 | 43.7k | return Context.getFunctionType(ArgFunctionTypeP->getReturnType(), |
4138 | 43.7k | ArgFunctionTypeP->getParamTypes(), EPI); |
4139 | 339k | } |
4140 | | |
4141 | | /// Deduce template arguments when taking the address of a function |
4142 | | /// template (C++ [temp.deduct.funcaddr]) or matching a specialization to |
4143 | | /// a template. |
4144 | | /// |
4145 | | /// \param FunctionTemplate the function template for which we are performing |
4146 | | /// template argument deduction. |
4147 | | /// |
4148 | | /// \param ExplicitTemplateArgs the explicitly-specified template |
4149 | | /// arguments. |
4150 | | /// |
4151 | | /// \param ArgFunctionType the function type that will be used as the |
4152 | | /// "argument" type (A) when performing template argument deduction from the |
4153 | | /// function template's function type. This type may be NULL, if there is no |
4154 | | /// argument type to compare against, in C++0x [temp.arg.explicit]p3. |
4155 | | /// |
4156 | | /// \param Specialization if template argument deduction was successful, |
4157 | | /// this will be set to the function template specialization produced by |
4158 | | /// template argument deduction. |
4159 | | /// |
4160 | | /// \param Info the argument will be updated to provide additional information |
4161 | | /// about template argument deduction. |
4162 | | /// |
4163 | | /// \param IsAddressOfFunction If \c true, we are deducing as part of taking |
4164 | | /// the address of a function template per [temp.deduct.funcaddr] and |
4165 | | /// [over.over]. If \c false, we are looking up a function template |
4166 | | /// specialization based on its signature, per [temp.deduct.decl]. |
4167 | | /// |
4168 | | /// \returns the result of template argument deduction. |
4169 | | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
4170 | | FunctionTemplateDecl *FunctionTemplate, |
4171 | | TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ArgFunctionType, |
4172 | | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
4173 | 142k | bool IsAddressOfFunction) { |
4174 | 142k | if (FunctionTemplate->isInvalidDecl()) |
4175 | 0 | return TDK_Invalid; |
4176 | | |
4177 | 142k | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
4178 | 142k | TemplateParameterList *TemplateParams |
4179 | 142k | = FunctionTemplate->getTemplateParameters(); |
4180 | 142k | QualType FunctionType = Function->getType(); |
4181 | | |
4182 | | // Substitute any explicit template arguments. |
4183 | 142k | LocalInstantiationScope InstScope(*this); |
4184 | 142k | SmallVector<DeducedTemplateArgument, 4> Deduced; |
4185 | 142k | unsigned NumExplicitlySpecified = 0; |
4186 | 142k | SmallVector<QualType, 4> ParamTypes; |
4187 | 142k | if (ExplicitTemplateArgs) { |
4188 | 63.8k | TemplateDeductionResult Result; |
4189 | 63.8k | runWithSufficientStackSpace(Info.getLocation(), [&] { |
4190 | 63.8k | Result = SubstituteExplicitTemplateArguments( |
4191 | 63.8k | FunctionTemplate, *ExplicitTemplateArgs, Deduced, ParamTypes, |
4192 | 63.8k | &FunctionType, Info); |
4193 | 63.8k | }); |
4194 | 63.8k | if (Result) |
4195 | 2.08k | return Result; |
4196 | | |
4197 | 61.7k | NumExplicitlySpecified = Deduced.size(); |
4198 | 61.7k | } |
4199 | | |
4200 | | // When taking the address of a function, we require convertibility of |
4201 | | // the resulting function type. Otherwise, we allow arbitrary mismatches |
4202 | | // of calling convention and noreturn. |
4203 | 140k | if (!IsAddressOfFunction) |
4204 | 134k | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType, |
4205 | 134k | /*AdjustExceptionSpec*/false); |
4206 | | |
4207 | | // Unevaluated SFINAE context. |
4208 | 140k | EnterExpressionEvaluationContext Unevaluated( |
4209 | 140k | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
4210 | 140k | SFINAETrap Trap(*this); |
4211 | | |
4212 | 140k | Deduced.resize(TemplateParams->size()); |
4213 | | |
4214 | | // If the function has a deduced return type, substitute it for a dependent |
4215 | | // type so that we treat it as a non-deduced context in what follows. If we |
4216 | | // are looking up by signature, the signature type should also have a deduced |
4217 | | // return type, which we instead expect to exactly match. |
4218 | 140k | bool HasDeducedReturnType = false; |
4219 | 140k | if (getLangOpts().CPlusPlus14 && IsAddressOfFunction16.1k && |
4220 | 140k | Function->getReturnType()->getContainedAutoType()2.54k ) { |
4221 | 722 | FunctionType = SubstAutoTypeDependent(FunctionType); |
4222 | 722 | HasDeducedReturnType = true; |
4223 | 722 | } |
4224 | | |
4225 | 140k | if (!ArgFunctionType.isNull() && !FunctionType.isNull()139k ) { |
4226 | 139k | unsigned TDF = |
4227 | 139k | TDF_TopLevelParameterTypeList | TDF_AllowCompatibleFunctionType; |
4228 | | // Deduce template arguments from the function type. |
4229 | 139k | if (TemplateDeductionResult Result |
4230 | 139k | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
4231 | 139k | FunctionType, ArgFunctionType, |
4232 | 139k | Info, Deduced, TDF)) |
4233 | 108k | return Result; |
4234 | 139k | } |
4235 | | |
4236 | 31.8k | TemplateDeductionResult Result; |
4237 | 31.8k | runWithSufficientStackSpace(Info.getLocation(), [&] { |
4238 | 31.8k | Result = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
4239 | 31.8k | NumExplicitlySpecified, |
4240 | 31.8k | Specialization, Info); |
4241 | 31.8k | }); |
4242 | 31.8k | if (Result) |
4243 | 6.09k | return Result; |
4244 | | |
4245 | | // If the function has a deduced return type, deduce it now, so we can check |
4246 | | // that the deduced function type matches the requested type. |
4247 | 25.7k | if (HasDeducedReturnType && |
4248 | 25.7k | Specialization->getReturnType()->isUndeducedType()348 && |
4249 | 25.7k | DeduceReturnType(Specialization, Info.getLocation(), false)60 ) |
4250 | 12 | return TDK_MiscellaneousDeductionFailure; |
4251 | | |
4252 | | // If the function has a dependent exception specification, resolve it now, |
4253 | | // so we can check that the exception specification matches. |
4254 | 25.7k | auto *SpecializationFPT = |
4255 | 25.7k | Specialization->getType()->castAs<FunctionProtoType>(); |
4256 | 25.7k | if (getLangOpts().CPlusPlus17 && |
4257 | 25.7k | isUnresolvedExceptionSpec(SpecializationFPT->getExceptionSpecType())2.86k && |
4258 | 25.7k | !ResolveExceptionSpec(Info.getLocation(), SpecializationFPT)10 ) |
4259 | 0 | return TDK_MiscellaneousDeductionFailure; |
4260 | | |
4261 | | // Adjust the exception specification of the argument to match the |
4262 | | // substituted and resolved type we just formed. (Calling convention and |
4263 | | // noreturn can't be dependent, so we don't actually need this for them |
4264 | | // right now.) |
4265 | 25.7k | QualType SpecializationType = Specialization->getType(); |
4266 | 25.7k | if (!IsAddressOfFunction) |
4267 | 22.5k | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, SpecializationType, |
4268 | 22.5k | /*AdjustExceptionSpec*/true); |
4269 | | |
4270 | | // If the requested function type does not match the actual type of the |
4271 | | // specialization with respect to arguments of compatible pointer to function |
4272 | | // types, template argument deduction fails. |
4273 | 25.7k | if (!ArgFunctionType.isNull()) { |
4274 | 25.0k | if (IsAddressOfFunction && |
4275 | 25.0k | !isSameOrCompatibleFunctionType( |
4276 | 2.55k | Context.getCanonicalType(SpecializationType), |
4277 | 2.55k | Context.getCanonicalType(ArgFunctionType))) |
4278 | 23 | return TDK_MiscellaneousDeductionFailure; |
4279 | | |
4280 | 25.0k | if (!IsAddressOfFunction && |
4281 | 25.0k | !Context.hasSameType(SpecializationType, ArgFunctionType)22.5k ) |
4282 | 36 | return TDK_MiscellaneousDeductionFailure; |
4283 | 25.0k | } |
4284 | | |
4285 | 25.6k | return TDK_Success; |
4286 | 25.7k | } |
4287 | | |
4288 | | /// Deduce template arguments for a templated conversion |
4289 | | /// function (C++ [temp.deduct.conv]) and, if successful, produce a |
4290 | | /// conversion function template specialization. |
4291 | | Sema::TemplateDeductionResult |
4292 | | Sema::DeduceTemplateArguments(FunctionTemplateDecl *ConversionTemplate, |
4293 | | QualType ToType, |
4294 | | CXXConversionDecl *&Specialization, |
4295 | 4.40k | TemplateDeductionInfo &Info) { |
4296 | 4.40k | if (ConversionTemplate->isInvalidDecl()) |
4297 | 0 | return TDK_Invalid; |
4298 | | |
4299 | 4.40k | CXXConversionDecl *ConversionGeneric |
4300 | 4.40k | = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl()); |
4301 | | |
4302 | 4.40k | QualType FromType = ConversionGeneric->getConversionType(); |
4303 | | |
4304 | | // Canonicalize the types for deduction. |
4305 | 4.40k | QualType P = Context.getCanonicalType(FromType); |
4306 | 4.40k | QualType A = Context.getCanonicalType(ToType); |
4307 | | |
4308 | | // C++0x [temp.deduct.conv]p2: |
4309 | | // If P is a reference type, the type referred to by P is used for |
4310 | | // type deduction. |
4311 | 4.40k | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
4312 | 89 | P = PRef->getPointeeType(); |
4313 | | |
4314 | | // C++0x [temp.deduct.conv]p4: |
4315 | | // [...] If A is a reference type, the type referred to by A is used |
4316 | | // for type deduction. |
4317 | 4.40k | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) { |
4318 | 190 | A = ARef->getPointeeType(); |
4319 | | // We work around a defect in the standard here: cv-qualifiers are also |
4320 | | // removed from P and A in this case, unless P was a reference type. This |
4321 | | // seems to mostly match what other compilers are doing. |
4322 | 190 | if (!FromType->getAs<ReferenceType>()) { |
4323 | 123 | A = A.getUnqualifiedType(); |
4324 | 123 | P = P.getUnqualifiedType(); |
4325 | 123 | } |
4326 | | |
4327 | | // C++ [temp.deduct.conv]p3: |
4328 | | // |
4329 | | // If A is not a reference type: |
4330 | 4.21k | } else { |
4331 | 4.21k | assert(!A->isReferenceType() && "Reference types were handled above"); |
4332 | | |
4333 | | // - If P is an array type, the pointer type produced by the |
4334 | | // array-to-pointer standard conversion (4.2) is used in place |
4335 | | // of P for type deduction; otherwise, |
4336 | 4.21k | if (P->isArrayType()) |
4337 | 1 | P = Context.getArrayDecayedType(P); |
4338 | | // - If P is a function type, the pointer type produced by the |
4339 | | // function-to-pointer standard conversion (4.3) is used in |
4340 | | // place of P for type deduction; otherwise, |
4341 | 4.21k | else if (P->isFunctionType()) |
4342 | 0 | P = Context.getPointerType(P); |
4343 | | // - If P is a cv-qualified type, the top level cv-qualifiers of |
4344 | | // P's type are ignored for type deduction. |
4345 | 4.21k | else |
4346 | 4.21k | P = P.getUnqualifiedType(); |
4347 | | |
4348 | | // C++0x [temp.deduct.conv]p4: |
4349 | | // If A is a cv-qualified type, the top level cv-qualifiers of A's |
4350 | | // type are ignored for type deduction. If A is a reference type, the type |
4351 | | // referred to by A is used for type deduction. |
4352 | 4.21k | A = A.getUnqualifiedType(); |
4353 | 4.21k | } |
4354 | | |
4355 | | // Unevaluated SFINAE context. |
4356 | 0 | EnterExpressionEvaluationContext Unevaluated( |
4357 | 4.40k | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
4358 | 4.40k | SFINAETrap Trap(*this); |
4359 | | |
4360 | | // C++ [temp.deduct.conv]p1: |
4361 | | // Template argument deduction is done by comparing the return |
4362 | | // type of the template conversion function (call it P) with the |
4363 | | // type that is required as the result of the conversion (call it |
4364 | | // A) as described in 14.8.2.4. |
4365 | 4.40k | TemplateParameterList *TemplateParams |
4366 | 4.40k | = ConversionTemplate->getTemplateParameters(); |
4367 | 4.40k | SmallVector<DeducedTemplateArgument, 4> Deduced; |
4368 | 4.40k | Deduced.resize(TemplateParams->size()); |
4369 | | |
4370 | | // C++0x [temp.deduct.conv]p4: |
4371 | | // In general, the deduction process attempts to find template |
4372 | | // argument values that will make the deduced A identical to |
4373 | | // A. However, there are two cases that allow a difference: |
4374 | 4.40k | unsigned TDF = 0; |
4375 | | // - If the original A is a reference type, A can be more |
4376 | | // cv-qualified than the deduced A (i.e., the type referred to |
4377 | | // by the reference) |
4378 | 4.40k | if (ToType->isReferenceType()) |
4379 | 190 | TDF |= TDF_ArgWithReferenceType; |
4380 | | // - The deduced A can be another pointer or pointer to member |
4381 | | // type that can be converted to A via a qualification |
4382 | | // conversion. |
4383 | | // |
4384 | | // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when |
4385 | | // both P and A are pointers or member pointers. In this case, we |
4386 | | // just ignore cv-qualifiers completely). |
4387 | 4.40k | if ((P->isPointerType() && A->isPointerType()1.19k ) || |
4388 | 4.40k | (3.41k P->isMemberPointerType()3.41k && A->isMemberPointerType()20 )) |
4389 | 1.01k | TDF |= TDF_IgnoreQualifiers; |
4390 | 4.40k | if (TemplateDeductionResult Result |
4391 | 4.40k | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
4392 | 4.40k | P, A, Info, Deduced, TDF)) |
4393 | 542 | return Result; |
4394 | | |
4395 | | // Create an Instantiation Scope for finalizing the operator. |
4396 | 3.86k | LocalInstantiationScope InstScope(*this); |
4397 | | // Finish template argument deduction. |
4398 | 3.86k | FunctionDecl *ConversionSpecialized = nullptr; |
4399 | 3.86k | TemplateDeductionResult Result; |
4400 | 3.86k | runWithSufficientStackSpace(Info.getLocation(), [&] { |
4401 | 3.86k | Result = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0, |
4402 | 3.86k | ConversionSpecialized, Info); |
4403 | 3.86k | }); |
4404 | 3.86k | Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized); |
4405 | 3.86k | return Result; |
4406 | 4.40k | } |
4407 | | |
4408 | | /// Deduce template arguments for a function template when there is |
4409 | | /// nothing to deduce against (C++0x [temp.arg.explicit]p3). |
4410 | | /// |
4411 | | /// \param FunctionTemplate the function template for which we are performing |
4412 | | /// template argument deduction. |
4413 | | /// |
4414 | | /// \param ExplicitTemplateArgs the explicitly-specified template |
4415 | | /// arguments. |
4416 | | /// |
4417 | | /// \param Specialization if template argument deduction was successful, |
4418 | | /// this will be set to the function template specialization produced by |
4419 | | /// template argument deduction. |
4420 | | /// |
4421 | | /// \param Info the argument will be updated to provide additional information |
4422 | | /// about template argument deduction. |
4423 | | /// |
4424 | | /// \param IsAddressOfFunction If \c true, we are deducing as part of taking |
4425 | | /// the address of a function template in a context where we do not have a |
4426 | | /// target type, per [over.over]. If \c false, we are looking up a function |
4427 | | /// template specialization based on its signature, which only happens when |
4428 | | /// deducing a function parameter type from an argument that is a template-id |
4429 | | /// naming a function template specialization. |
4430 | | /// |
4431 | | /// \returns the result of template argument deduction. |
4432 | | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
4433 | | FunctionTemplateDecl *FunctionTemplate, |
4434 | | TemplateArgumentListInfo *ExplicitTemplateArgs, |
4435 | | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
4436 | 775 | bool IsAddressOfFunction) { |
4437 | 775 | return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
4438 | 775 | QualType(), Specialization, Info, |
4439 | 775 | IsAddressOfFunction); |
4440 | 775 | } |
4441 | | |
4442 | | namespace { |
4443 | | struct DependentAuto { bool IsPack; }; |
4444 | | |
4445 | | /// Substitute the 'auto' specifier or deduced template specialization type |
4446 | | /// specifier within a type for a given replacement type. |
4447 | | class SubstituteDeducedTypeTransform : |
4448 | | public TreeTransform<SubstituteDeducedTypeTransform> { |
4449 | | QualType Replacement; |
4450 | | bool ReplacementIsPack; |
4451 | | bool UseTypeSugar; |
4452 | | |
4453 | | public: |
4454 | | SubstituteDeducedTypeTransform(Sema &SemaRef, DependentAuto DA) |
4455 | | : TreeTransform<SubstituteDeducedTypeTransform>(SemaRef), |
4456 | 32.9k | ReplacementIsPack(DA.IsPack), UseTypeSugar(true) {} |
4457 | | |
4458 | | SubstituteDeducedTypeTransform(Sema &SemaRef, QualType Replacement, |
4459 | | bool UseTypeSugar = true) |
4460 | | : TreeTransform<SubstituteDeducedTypeTransform>(SemaRef), |
|