clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name SemaTemplateDeduction.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -relaxed-aliasing -fmath-errno -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20211110111138+cffbfd01e37b/build-llvm -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D CLANG_ROUND_TRIP_CC1_ARGS=ON -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I tools/clang/lib/Sema -I /build/llvm-toolchain-snapshot-14~++20211110111138+cffbfd01e37b/clang/lib/Sema -I /build/llvm-toolchain-snapshot-14~++20211110111138+cffbfd01e37b/clang/include -I tools/clang/include -I include -I /build/llvm-toolchain-snapshot-14~++20211110111138+cffbfd01e37b/llvm/include -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-command-line-argument -Wno-unknown-warning-option -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20211110111138+cffbfd01e37b/build-llvm -ferror-limit 19 -fvisibility-inlines-hidden -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2021-11-10-160236-22541-1 -x c++ /build/llvm-toolchain-snapshot-14~++20211110111138+cffbfd01e37b/clang/lib/Sema/SemaTemplateDeduction.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
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 | |
63 | |
64 | |
65 | enum TemplateDeductionFlags { |
66 | |
67 | |
68 | |
69 | TDF_None = 0, |
70 | |
71 | |
72 | |
73 | |
74 | TDF_ParamWithReferenceType = 0x1, |
75 | |
76 | |
77 | |
78 | TDF_IgnoreQualifiers = 0x02, |
79 | |
80 | |
81 | |
82 | |
83 | TDF_DerivedClass = 0x04, |
84 | |
85 | |
86 | |
87 | |
88 | TDF_SkipNonDependent = 0x08, |
89 | |
90 | |
91 | |
92 | TDF_TopLevelParameterTypeList = 0x10, |
93 | |
94 | |
95 | |
96 | |
97 | |
98 | |
99 | |
100 | |
101 | TDF_AllowCompatibleFunctionType = 0x20, |
102 | |
103 | |
104 | |
105 | |
106 | TDF_ArgWithReferenceType = 0x40, |
107 | }; |
108 | } |
109 | |
110 | using namespace clang; |
111 | using namespace sema; |
112 | |
113 | |
114 | |
115 | static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) { |
116 | if (Y.getBitWidth() > X.getBitWidth()) |
117 | X = X.extend(Y.getBitWidth()); |
118 | else if (Y.getBitWidth() < X.getBitWidth()) |
119 | Y = Y.extend(X.getBitWidth()); |
120 | |
121 | |
122 | if (X.isSigned() != Y.isSigned()) { |
123 | |
124 | if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative())) |
125 | return false; |
126 | |
127 | Y.setIsSigned(true); |
128 | X.setIsSigned(true); |
129 | } |
130 | |
131 | return X == Y; |
132 | } |
133 | |
134 | static Sema::TemplateDeductionResult |
135 | DeduceTemplateArguments(Sema &S, |
136 | TemplateParameterList *TemplateParams, |
137 | const TemplateArgument &Param, |
138 | TemplateArgument Arg, |
139 | TemplateDeductionInfo &Info, |
140 | SmallVectorImpl<DeducedTemplateArgument> &Deduced); |
141 | |
142 | static Sema::TemplateDeductionResult |
143 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
144 | TemplateParameterList *TemplateParams, |
145 | QualType Param, |
146 | QualType Arg, |
147 | TemplateDeductionInfo &Info, |
148 | SmallVectorImpl<DeducedTemplateArgument> & |
149 | Deduced, |
150 | unsigned TDF, |
151 | bool PartialOrdering = false, |
152 | bool DeducedFromArrayBound = false); |
153 | |
154 | static Sema::TemplateDeductionResult |
155 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
156 | ArrayRef<TemplateArgument> Params, |
157 | ArrayRef<TemplateArgument> Args, |
158 | TemplateDeductionInfo &Info, |
159 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
160 | bool NumberOfArgumentsMustMatch); |
161 | |
162 | static void MarkUsedTemplateParameters(ASTContext &Ctx, |
163 | const TemplateArgument &TemplateArg, |
164 | bool OnlyDeduced, unsigned Depth, |
165 | llvm::SmallBitVector &Used); |
166 | |
167 | static void MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
168 | bool OnlyDeduced, unsigned Level, |
169 | llvm::SmallBitVector &Deduced); |
170 | |
171 | |
172 | |
173 | |
174 | static const NonTypeTemplateParmDecl * |
175 | getDeducedParameterFromExpr(const Expr *E, unsigned Depth) { |
176 | |
177 | |
178 | while (true) { |
179 | if (const auto *IC = dyn_cast<ImplicitCastExpr>(E)) |
180 | E = IC->getSubExpr(); |
181 | else if (const auto *CE = dyn_cast<ConstantExpr>(E)) |
182 | E = CE->getSubExpr(); |
183 | else if (const auto *Subst = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
184 | E = Subst->getReplacement(); |
185 | else if (const auto *CCE = dyn_cast<CXXConstructExpr>(E)) { |
186 | |
187 | if (CCE->getParenOrBraceRange().isValid()) |
188 | break; |
189 | |
190 | assert(CCE->getNumArgs() >= 1 && "implicit construct expr should have 1 arg"); |
191 | E = CCE->getArg(0); |
192 | } else |
193 | break; |
194 | } |
195 | |
196 | if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) |
197 | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) |
198 | if (NTTP->getDepth() == Depth) |
199 | return NTTP; |
200 | |
201 | return nullptr; |
202 | } |
203 | |
204 | static const NonTypeTemplateParmDecl * |
205 | getDeducedParameterFromExpr(TemplateDeductionInfo &Info, Expr *E) { |
206 | return getDeducedParameterFromExpr(E, Info.getDeducedDepth()); |
207 | } |
208 | |
209 | |
210 | |
211 | static bool isSameDeclaration(Decl *X, Decl *Y) { |
212 | if (NamedDecl *NX = dyn_cast<NamedDecl>(X)) |
213 | X = NX->getUnderlyingDecl(); |
214 | if (NamedDecl *NY = dyn_cast<NamedDecl>(Y)) |
215 | Y = NY->getUnderlyingDecl(); |
216 | |
217 | return X->getCanonicalDecl() == Y->getCanonicalDecl(); |
218 | } |
219 | |
220 | |
221 | |
222 | |
223 | |
224 | static DeducedTemplateArgument |
225 | checkDeducedTemplateArguments(ASTContext &Context, |
226 | const DeducedTemplateArgument &X, |
227 | const DeducedTemplateArgument &Y) { |
228 | |
229 | if (X.isNull()) |
230 | return Y; |
231 | if (Y.isNull()) |
232 | return X; |
233 | |
234 | |
235 | |
236 | |
237 | |
238 | |
239 | if (!X.wasDeducedFromArrayBound() && !Y.wasDeducedFromArrayBound()) { |
240 | QualType XType = X.getNonTypeTemplateArgumentType(); |
241 | if (!XType.isNull()) { |
242 | QualType YType = Y.getNonTypeTemplateArgumentType(); |
243 | if (YType.isNull() || !Context.hasSameType(XType, YType)) |
244 | return DeducedTemplateArgument(); |
245 | } |
246 | } |
247 | |
248 | switch (X.getKind()) { |
249 | case TemplateArgument::Null: |
250 | llvm_unreachable("Non-deduced template arguments handled above"); |
251 | |
252 | case TemplateArgument::Type: |
253 | |
254 | if (Y.getKind() == TemplateArgument::Type && |
255 | Context.hasSameType(X.getAsType(), Y.getAsType())) |
256 | return X; |
257 | |
258 | |
259 | |
260 | if (X.wasDeducedFromArrayBound() != Y.wasDeducedFromArrayBound()) |
261 | return X.wasDeducedFromArrayBound() ? Y : X; |
262 | |
263 | |
264 | return DeducedTemplateArgument(); |
265 | |
266 | case TemplateArgument::Integral: |
267 | |
268 | |
269 | |
270 | if (Y.getKind() == TemplateArgument::Expression || |
271 | Y.getKind() == TemplateArgument::Declaration || |
272 | (Y.getKind() == TemplateArgument::Integral && |
273 | hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()))) |
274 | return X.wasDeducedFromArrayBound() ? Y : X; |
275 | |
276 | |
277 | return DeducedTemplateArgument(); |
278 | |
279 | case TemplateArgument::Template: |
280 | if (Y.getKind() == TemplateArgument::Template && |
281 | Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate())) |
282 | return X; |
283 | |
284 | |
285 | return DeducedTemplateArgument(); |
286 | |
287 | case TemplateArgument::TemplateExpansion: |
288 | if (Y.getKind() == TemplateArgument::TemplateExpansion && |
289 | Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(), |
290 | Y.getAsTemplateOrTemplatePattern())) |
291 | return X; |
292 | |
293 | |
294 | return DeducedTemplateArgument(); |
295 | |
296 | case TemplateArgument::Expression: { |
297 | if (Y.getKind() != TemplateArgument::Expression) |
298 | return checkDeducedTemplateArguments(Context, Y, X); |
299 | |
300 | |
301 | llvm::FoldingSetNodeID ID1, ID2; |
302 | X.getAsExpr()->Profile(ID1, Context, true); |
303 | Y.getAsExpr()->Profile(ID2, Context, true); |
304 | if (ID1 == ID2) |
305 | return X.wasDeducedFromArrayBound() ? Y : X; |
306 | |
307 | |
308 | return DeducedTemplateArgument(); |
309 | } |
310 | |
311 | case TemplateArgument::Declaration: |
312 | assert(!X.wasDeducedFromArrayBound()); |
313 | |
314 | |
315 | |
316 | if (Y.getKind() == TemplateArgument::Expression) |
317 | return X; |
318 | |
319 | |
320 | |
321 | |
322 | if (Y.getKind() == TemplateArgument::Integral) { |
323 | if (Y.wasDeducedFromArrayBound()) |
324 | return TemplateArgument(Context, Y.getAsIntegral(), |
325 | X.getParamTypeForDecl()); |
326 | return Y; |
327 | } |
328 | |
329 | |
330 | |
331 | if (Y.getKind() == TemplateArgument::Declaration && |
332 | isSameDeclaration(X.getAsDecl(), Y.getAsDecl())) |
333 | return X; |
334 | |
335 | |
336 | return DeducedTemplateArgument(); |
337 | |
338 | case TemplateArgument::NullPtr: |
339 | |
340 | |
341 | if (Y.getKind() == TemplateArgument::Expression) |
342 | return X; |
343 | |
344 | |
345 | |
346 | if (Y.getKind() == TemplateArgument::Integral) |
347 | return Y; |
348 | |
349 | |
350 | if (Y.getKind() == TemplateArgument::NullPtr) |
351 | return X; |
352 | |
353 | |
354 | return DeducedTemplateArgument(); |
355 | |
356 | case TemplateArgument::Pack: { |
357 | if (Y.getKind() != TemplateArgument::Pack || |
358 | X.pack_size() != Y.pack_size()) |
359 | return DeducedTemplateArgument(); |
360 | |
361 | llvm::SmallVector<TemplateArgument, 8> NewPack; |
362 | for (TemplateArgument::pack_iterator XA = X.pack_begin(), |
363 | XAEnd = X.pack_end(), |
364 | YA = Y.pack_begin(); |
365 | XA != XAEnd; ++XA, ++YA) { |
366 | TemplateArgument Merged = checkDeducedTemplateArguments( |
367 | Context, DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()), |
368 | DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound())); |
369 | if (Merged.isNull() && !(XA->isNull() && YA->isNull())) |
370 | return DeducedTemplateArgument(); |
371 | NewPack.push_back(Merged); |
372 | } |
373 | |
374 | return DeducedTemplateArgument( |
375 | TemplateArgument::CreatePackCopy(Context, NewPack), |
376 | X.wasDeducedFromArrayBound() && Y.wasDeducedFromArrayBound()); |
377 | } |
378 | } |
379 | |
380 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
381 | } |
382 | |
383 | |
384 | |
385 | |
386 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
387 | Sema &S, TemplateParameterList *TemplateParams, |
388 | const NonTypeTemplateParmDecl *NTTP, const DeducedTemplateArgument &NewDeduced, |
389 | QualType ValueType, TemplateDeductionInfo &Info, |
390 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
391 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
392 | "deducing non-type template argument with wrong depth"); |
393 | |
394 | DeducedTemplateArgument Result = checkDeducedTemplateArguments( |
395 | S.Context, Deduced[NTTP->getIndex()], NewDeduced); |
396 | if (Result.isNull()) { |
397 | Info.Param = const_cast<NonTypeTemplateParmDecl*>(NTTP); |
398 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
399 | Info.SecondArg = NewDeduced; |
400 | return Sema::TDK_Inconsistent; |
401 | } |
402 | |
403 | Deduced[NTTP->getIndex()] = Result; |
404 | if (!S.getLangOpts().CPlusPlus17) |
405 | return Sema::TDK_Success; |
406 | |
407 | if (NTTP->isExpandedParameterPack()) |
408 | |
409 | |
410 | |
411 | |
412 | return Sema::TDK_Success; |
413 | |
414 | |
415 | |
416 | QualType ParamType = S.Context.getAdjustedParameterType(NTTP->getType()); |
417 | if (auto *Expansion = dyn_cast<PackExpansionType>(ParamType)) |
418 | ParamType = Expansion->getPattern(); |
419 | |
420 | |
421 | |
422 | |
423 | |
424 | ValueType = ValueType.getNonReferenceType(); |
425 | if (ParamType->isReferenceType()) |
426 | ParamType = ParamType.getNonReferenceType(); |
427 | else |
428 | |
429 | ValueType = ValueType.getUnqualifiedType(); |
430 | |
431 | return DeduceTemplateArgumentsByTypeMatch( |
432 | S, TemplateParams, ParamType, ValueType, Info, Deduced, |
433 | TDF_SkipNonDependent, false, |
434 | NewDeduced.wasDeducedFromArrayBound()); |
435 | } |
436 | |
437 | |
438 | |
439 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
440 | Sema &S, TemplateParameterList *TemplateParams, |
441 | const NonTypeTemplateParmDecl *NTTP, const llvm::APSInt &Value, |
442 | QualType ValueType, bool DeducedFromArrayBound, TemplateDeductionInfo &Info, |
443 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
444 | return DeduceNonTypeTemplateArgument( |
445 | S, TemplateParams, NTTP, |
446 | DeducedTemplateArgument(S.Context, Value, ValueType, |
447 | DeducedFromArrayBound), |
448 | ValueType, Info, Deduced); |
449 | } |
450 | |
451 | |
452 | |
453 | static Sema::TemplateDeductionResult DeduceNullPtrTemplateArgument( |
454 | Sema &S, TemplateParameterList *TemplateParams, |
455 | const NonTypeTemplateParmDecl *NTTP, QualType NullPtrType, |
456 | TemplateDeductionInfo &Info, |
457 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
458 | Expr *Value = S.ImpCastExprToType( |
459 | new (S.Context) CXXNullPtrLiteralExpr(S.Context.NullPtrTy, |
460 | NTTP->getLocation()), |
461 | NullPtrType, |
462 | NullPtrType->isMemberPointerType() ? CK_NullToMemberPointer |
463 | : CK_NullToPointer) |
464 | .get(); |
465 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
466 | DeducedTemplateArgument(Value), |
467 | Value->getType(), Info, Deduced); |
468 | } |
469 | |
470 | |
471 | |
472 | |
473 | |
474 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
475 | Sema &S, TemplateParameterList *TemplateParams, |
476 | const NonTypeTemplateParmDecl *NTTP, Expr *Value, TemplateDeductionInfo &Info, |
477 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
478 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
479 | DeducedTemplateArgument(Value), |
480 | Value->getType(), Info, Deduced); |
481 | } |
482 | |
483 | |
484 | |
485 | |
486 | |
487 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
488 | Sema &S, TemplateParameterList *TemplateParams, |
489 | const NonTypeTemplateParmDecl *NTTP, ValueDecl *D, QualType T, |
490 | TemplateDeductionInfo &Info, |
491 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
492 | D = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
493 | TemplateArgument New(D, T); |
494 | return DeduceNonTypeTemplateArgument( |
495 | S, TemplateParams, NTTP, DeducedTemplateArgument(New), T, Info, Deduced); |
496 | } |
497 | |
498 | static Sema::TemplateDeductionResult |
499 | DeduceTemplateArguments(Sema &S, |
500 | TemplateParameterList *TemplateParams, |
501 | TemplateName Param, |
502 | TemplateName Arg, |
503 | TemplateDeductionInfo &Info, |
504 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
505 | TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); |
506 | if (!ParamDecl) { |
507 | |
508 | |
509 | return Sema::TDK_Success; |
510 | } |
511 | |
512 | if (TemplateTemplateParmDecl *TempParam |
513 | = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { |
514 | |
515 | if (TempParam->getDepth() != Info.getDeducedDepth()) |
516 | return Sema::TDK_Success; |
517 | |
518 | DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg)); |
519 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
520 | Deduced[TempParam->getIndex()], |
521 | NewDeduced); |
522 | if (Result.isNull()) { |
523 | Info.Param = TempParam; |
524 | Info.FirstArg = Deduced[TempParam->getIndex()]; |
525 | Info.SecondArg = NewDeduced; |
526 | return Sema::TDK_Inconsistent; |
527 | } |
528 | |
529 | Deduced[TempParam->getIndex()] = Result; |
530 | return Sema::TDK_Success; |
531 | } |
532 | |
533 | |
534 | if (S.Context.hasSameTemplateName(Param, Arg)) |
535 | return Sema::TDK_Success; |
536 | |
537 | |
538 | Info.FirstArg = TemplateArgument(Param); |
539 | Info.SecondArg = TemplateArgument(Arg); |
540 | return Sema::TDK_NonDeducedMismatch; |
541 | } |
542 | |
543 | |
544 | |
545 | |
546 | |
547 | |
548 | |
549 | |
550 | |
551 | |
552 | |
553 | |
554 | |
555 | |
556 | |
557 | |
558 | |
559 | |
560 | |
561 | static Sema::TemplateDeductionResult |
562 | DeduceTemplateArguments(Sema &S, |
563 | TemplateParameterList *TemplateParams, |
564 | const TemplateSpecializationType *Param, |
565 | QualType Arg, |
566 | TemplateDeductionInfo &Info, |
567 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
568 | assert(Arg.isCanonical() && "Argument type must be canonical"); |
569 | |
570 | |
571 | if (auto *Injected = dyn_cast<InjectedClassNameType>(Arg)) |
572 | Arg = Injected->getInjectedSpecializationType(); |
573 | |
574 | |
575 | if (const TemplateSpecializationType *SpecArg |
576 | = dyn_cast<TemplateSpecializationType>(Arg)) { |
577 | |
578 | if (Sema::TemplateDeductionResult Result |
579 | = DeduceTemplateArguments(S, TemplateParams, |
580 | Param->getTemplateName(), |
581 | SpecArg->getTemplateName(), |
582 | Info, Deduced)) |
583 | return Result; |
584 | |
585 | |
586 | |
587 | |
588 | |
589 | return DeduceTemplateArguments(S, TemplateParams, |
590 | Param->template_arguments(), |
591 | SpecArg->template_arguments(), Info, Deduced, |
592 | false); |
593 | } |
594 | |
595 | |
596 | |
597 | |
598 | const RecordType *RecordArg = dyn_cast<RecordType>(Arg); |
599 | if (!RecordArg) { |
600 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
601 | Info.SecondArg = TemplateArgument(Arg); |
602 | return Sema::TDK_NonDeducedMismatch; |
603 | } |
604 | |
605 | ClassTemplateSpecializationDecl *SpecArg |
606 | = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); |
607 | if (!SpecArg) { |
608 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
609 | Info.SecondArg = TemplateArgument(Arg); |
610 | return Sema::TDK_NonDeducedMismatch; |
611 | } |
612 | |
613 | |
614 | if (Sema::TemplateDeductionResult Result |
615 | = DeduceTemplateArguments(S, |
616 | TemplateParams, |
617 | Param->getTemplateName(), |
618 | TemplateName(SpecArg->getSpecializedTemplate()), |
619 | Info, Deduced)) |
620 | return Result; |
621 | |
622 | |
623 | return DeduceTemplateArguments(S, TemplateParams, Param->template_arguments(), |
624 | SpecArg->getTemplateArgs().asArray(), Info, |
625 | Deduced, true); |
626 | } |
627 | |
628 | |
629 | |
630 | static bool IsPossiblyOpaquelyQualifiedType(QualType T) { |
631 | switch (T->getTypeClass()) { |
632 | case Type::TypeOfExpr: |
633 | case Type::TypeOf: |
634 | case Type::DependentName: |
635 | case Type::Decltype: |
636 | case Type::UnresolvedUsing: |
637 | case Type::TemplateTypeParm: |
638 | return true; |
639 | |
640 | case Type::ConstantArray: |
641 | case Type::IncompleteArray: |
642 | case Type::VariableArray: |
643 | case Type::DependentSizedArray: |
644 | return IsPossiblyOpaquelyQualifiedType( |
645 | cast<ArrayType>(T)->getElementType()); |
646 | |
647 | default: |
648 | return false; |
649 | } |
650 | } |
651 | |
652 | |
653 | |
654 | static TemplateParameter makeTemplateParameter(Decl *D) { |
655 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
656 | return TemplateParameter(TTP); |
657 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
658 | return TemplateParameter(NTTP); |
659 | |
660 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
661 | } |
662 | |
663 | |
664 | struct clang::DeducedPack { |
665 | |
666 | unsigned Index; |
667 | |
668 | |
669 | DeducedTemplateArgument Saved; |
670 | |
671 | |
672 | |
673 | DeducedTemplateArgument DeferredDeduction; |
674 | |
675 | |
676 | SmallVector<DeducedTemplateArgument, 4> New; |
677 | |
678 | |
679 | DeducedPack *Outer = nullptr; |
680 | |
681 | DeducedPack(unsigned Index) : Index(Index) {} |
682 | }; |
683 | |
684 | namespace { |
685 | |
686 | |
687 | class PackDeductionScope { |
688 | public: |
689 | |
690 | PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, |
691 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
692 | TemplateDeductionInfo &Info, TemplateArgument Pattern) |
693 | : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { |
694 | unsigned NumNamedPacks = addPacks(Pattern); |
695 | finishConstruction(NumNamedPacks); |
696 | } |
697 | |
698 | |
699 | PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, |
700 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
701 | TemplateDeductionInfo &Info, unsigned Index) |
702 | : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { |
703 | addPack(Index); |
704 | finishConstruction(1); |
705 | } |
706 | |
707 | private: |
708 | void addPack(unsigned Index) { |
709 | |
710 | |
711 | DeducedPack Pack(Index); |
712 | Pack.Saved = Deduced[Index]; |
713 | Deduced[Index] = TemplateArgument(); |
714 | |
715 | |
716 | |
717 | |
718 | if (Optional<unsigned> ExpandedPackExpansions = |
719 | getExpandedPackSize(TemplateParams->getParam(Index))) |
720 | FixedNumExpansions = ExpandedPackExpansions; |
721 | |
722 | Packs.push_back(Pack); |
723 | } |
724 | |
725 | unsigned addPacks(TemplateArgument Pattern) { |
726 | |
727 | |
728 | llvm::SmallBitVector SawIndices(TemplateParams->size()); |
729 | llvm::SmallVector<TemplateArgument, 4> ExtraDeductions; |
730 | |
731 | auto AddPack = [&](unsigned Index) { |
732 | if (SawIndices[Index]) |
733 | return; |
734 | SawIndices[Index] = true; |
735 | addPack(Index); |
736 | |
737 | |
738 | |
739 | |
740 | |
741 | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>( |
742 | TemplateParams->getParam(Index))) { |
743 | if (!NTTP->isExpandedParameterPack()) |
744 | if (auto *Expansion = dyn_cast<PackExpansionType>(NTTP->getType())) |
745 | ExtraDeductions.push_back(Expansion->getPattern()); |
746 | } |
747 | |
748 | |
749 | }; |
750 | |
751 | auto Collect = [&](TemplateArgument Pattern) { |
752 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
753 | S.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
754 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
755 | unsigned Depth, Index; |
756 | std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
757 | if (Depth == Info.getDeducedDepth()) |
758 | AddPack(Index); |
759 | } |
760 | }; |
761 | |
762 | |
763 | Collect(Pattern); |
764 | assert(!Packs.empty() && "Pack expansion without unexpanded packs?"); |
765 | |
766 | unsigned NumNamedPacks = Packs.size(); |
767 | |
768 | |
769 | |
770 | while (!ExtraDeductions.empty()) |
771 | Collect(ExtraDeductions.pop_back_val()); |
772 | |
773 | return NumNamedPacks; |
774 | } |
775 | |
776 | void finishConstruction(unsigned NumNamedPacks) { |
777 | |
778 | const TemplateArgument *PartialPackArgs = nullptr; |
779 | unsigned NumPartialPackArgs = 0; |
780 | std::pair<unsigned, unsigned> PartialPackDepthIndex(-1u, -1u); |
781 | if (auto *Scope = S.CurrentInstantiationScope) |
782 | if (auto *Partial = Scope->getPartiallySubstitutedPack( |
783 | &PartialPackArgs, &NumPartialPackArgs)) |
784 | PartialPackDepthIndex = getDepthAndIndex(Partial); |
785 | |
786 | |
787 | |
788 | |
789 | bool IsExpanded = true; |
790 | for (unsigned I = 0; I != NumNamedPacks; ++I) { |
791 | if (Packs[I].Index >= Info.getNumExplicitArgs()) { |
792 | IsExpanded = false; |
793 | IsPartiallyExpanded = false; |
794 | break; |
795 | } |
796 | if (PartialPackDepthIndex == |
797 | std::make_pair(Info.getDeducedDepth(), Packs[I].Index)) { |
798 | IsPartiallyExpanded = true; |
799 | } |
800 | } |
801 | |
802 | |
803 | |
804 | if (IsPartiallyExpanded) |
805 | PackElements += NumPartialPackArgs; |
806 | else if (IsExpanded) |
807 | PackElements += *FixedNumExpansions; |
808 | |
809 | for (auto &Pack : Packs) { |
810 | if (Info.PendingDeducedPacks.size() > Pack.Index) |
811 | Pack.Outer = Info.PendingDeducedPacks[Pack.Index]; |
812 | else |
813 | Info.PendingDeducedPacks.resize(Pack.Index + 1); |
814 | Info.PendingDeducedPacks[Pack.Index] = &Pack; |
815 | |
816 | if (PartialPackDepthIndex == |
817 | std::make_pair(Info.getDeducedDepth(), Pack.Index)) { |
818 | Pack.New.append(PartialPackArgs, PartialPackArgs + NumPartialPackArgs); |
819 | |
820 | |
821 | |
822 | |
823 | |
824 | |
825 | |
826 | |
827 | if (!IsPartiallyExpanded) |
828 | Deduced[Pack.Index] = Pack.New[PackElements]; |
829 | } |
830 | } |
831 | } |
832 | |
833 | public: |
834 | ~PackDeductionScope() { |
835 | for (auto &Pack : Packs) |
836 | Info.PendingDeducedPacks[Pack.Index] = Pack.Outer; |
837 | } |
838 | |
839 | |
840 | |
841 | bool isPartiallyExpanded() { return IsPartiallyExpanded; } |
842 | |
843 | |
844 | |
845 | |
846 | bool hasFixedArity() { return FixedNumExpansions.hasValue(); } |
847 | |
848 | |
849 | |
850 | |
851 | bool hasNextElement() { |
852 | return !FixedNumExpansions || *FixedNumExpansions > PackElements; |
853 | } |
854 | |
855 | |
856 | void nextPackElement() { |
857 | |
858 | |
859 | |
860 | for (auto &Pack : Packs) { |
861 | DeducedTemplateArgument &DeducedArg = Deduced[Pack.Index]; |
862 | if (!Pack.New.empty() || !DeducedArg.isNull()) { |
863 | while (Pack.New.size() < PackElements) |
864 | Pack.New.push_back(DeducedTemplateArgument()); |
865 | if (Pack.New.size() == PackElements) |
866 | Pack.New.push_back(DeducedArg); |
867 | else |
868 | Pack.New[PackElements] = DeducedArg; |
869 | DeducedArg = Pack.New.size() > PackElements + 1 |
870 | ? Pack.New[PackElements + 1] |
871 | : DeducedTemplateArgument(); |
872 | } |
873 | } |
874 | ++PackElements; |
875 | } |
876 | |
877 | |
878 | |
879 | |
880 | Sema::TemplateDeductionResult finish() { |
881 | |
882 | |
883 | for (auto &Pack : Packs) { |
884 | |
885 | Deduced[Pack.Index] = Pack.Saved; |
886 | |
887 | |
888 | |
889 | |
890 | |
891 | |
892 | |
893 | |
894 | |
895 | |
896 | |
897 | |
898 | |
899 | |
900 | Pack.New.resize(PackElements); |
901 | |
902 | |
903 | DeducedTemplateArgument NewPack; |
904 | if (Pack.New.empty()) { |
905 | |
906 | NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack()); |
907 | } else { |
908 | TemplateArgument *ArgumentPack = |
909 | new (S.Context) TemplateArgument[Pack.New.size()]; |
910 | std::copy(Pack.New.begin(), Pack.New.end(), ArgumentPack); |
911 | NewPack = DeducedTemplateArgument( |
912 | TemplateArgument(llvm::makeArrayRef(ArgumentPack, Pack.New.size())), |
913 | |
914 | |
915 | |
916 | |
917 | |
918 | Pack.New[0].wasDeducedFromArrayBound()); |
919 | } |
920 | |
921 | |
922 | DeducedTemplateArgument *Loc; |
923 | if (Pack.Outer) { |
924 | if (Pack.Outer->DeferredDeduction.isNull()) { |
925 | |
926 | |
927 | Pack.Outer->DeferredDeduction = NewPack; |
928 | continue; |
929 | } |
930 | Loc = &Pack.Outer->DeferredDeduction; |
931 | } else { |
932 | Loc = &Deduced[Pack.Index]; |
933 | } |
934 | |
935 | |
936 | DeducedTemplateArgument OldPack = *Loc; |
937 | DeducedTemplateArgument Result = |
938 | checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
939 | |
940 | |
941 | if (!Result.isNull() && !Pack.DeferredDeduction.isNull()) { |
942 | OldPack = Result; |
943 | NewPack = Pack.DeferredDeduction; |
944 | Result = checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
945 | } |
946 | |
947 | NamedDecl *Param = TemplateParams->getParam(Pack.Index); |
948 | if (Result.isNull()) { |
949 | Info.Param = makeTemplateParameter(Param); |
950 | Info.FirstArg = OldPack; |
951 | Info.SecondArg = NewPack; |
952 | return Sema::TDK_Inconsistent; |
953 | } |
954 | |
955 | |
956 | |
957 | if (Optional<unsigned> Expansions = getExpandedPackSize(Param)) { |
958 | if (*Expansions != PackElements) { |
959 | Info.Param = makeTemplateParameter(Param); |
960 | Info.FirstArg = Result; |
961 | return Sema::TDK_IncompletePack; |
962 | } |
963 | } |
964 | |
965 | *Loc = Result; |
966 | } |
967 | |
968 | return Sema::TDK_Success; |
969 | } |
970 | |
971 | private: |
972 | Sema &S; |
973 | TemplateParameterList *TemplateParams; |
974 | SmallVectorImpl<DeducedTemplateArgument> &Deduced; |
975 | TemplateDeductionInfo &Info; |
976 | unsigned PackElements = 0; |
977 | bool IsPartiallyExpanded = false; |
978 | |
979 | Optional<unsigned> FixedNumExpansions; |
980 | |
981 | SmallVector<DeducedPack, 2> Packs; |
982 | }; |
983 | |
984 | } |
985 | |
986 | |
987 | |
988 | |
989 | |
990 | |
991 | |
992 | |
993 | |
994 | |
995 | |
996 | |
997 | |
998 | |
999 | |
1000 | |
1001 | |
1002 | |
1003 | |
1004 | |
1005 | |
1006 | |
1007 | |
1008 | |
1009 | |
1010 | |
1011 | |
1012 | |
1013 | |
1014 | |
1015 | |
1016 | static Sema::TemplateDeductionResult |
1017 | DeduceTemplateArguments(Sema &S, |
1018 | TemplateParameterList *TemplateParams, |
1019 | const QualType *Params, unsigned NumParams, |
1020 | const QualType *Args, unsigned NumArgs, |
1021 | TemplateDeductionInfo &Info, |
1022 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
1023 | unsigned TDF, |
1024 | bool PartialOrdering = false) { |
1025 | |
1026 | |
1027 | |
1028 | |
1029 | |
1030 | unsigned ArgIdx = 0, ParamIdx = 0; |
1031 | for (; ParamIdx != NumParams; ++ParamIdx) { |
1032 | |
1033 | const PackExpansionType *Expansion |
1034 | = dyn_cast<PackExpansionType>(Params[ParamIdx]); |
1035 | if (!Expansion) { |
1036 | |
1037 | |
1038 | |
1039 | if (ArgIdx >= NumArgs) |
1040 | return Sema::TDK_MiscellaneousDeductionFailure; |
1041 | |
1042 | if (isa<PackExpansionType>(Args[ArgIdx])) { |
1043 | |
1044 | |
1045 | |
1046 | |
1047 | return Sema::TDK_MiscellaneousDeductionFailure; |
1048 | } |
1049 | |
1050 | if (Sema::TemplateDeductionResult Result |
1051 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
1052 | Params[ParamIdx], Args[ArgIdx], |
1053 | Info, Deduced, TDF, |
1054 | PartialOrdering)) |
1055 | return Result; |
1056 | |
1057 | ++ArgIdx; |
1058 | continue; |
1059 | } |
1060 | |
1061 | |
1062 | |
1063 | |
1064 | |
1065 | |
1066 | |
1067 | |
1068 | QualType Pattern = Expansion->getPattern(); |
1069 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
1070 | |
1071 | |
1072 | |
1073 | if (ParamIdx + 1 == NumParams || PackScope.hasFixedArity()) { |
1074 | for (; ArgIdx < NumArgs && PackScope.hasNextElement(); ++ArgIdx) { |
1075 | |
1076 | if (Sema::TemplateDeductionResult Result |
1077 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, Pattern, |
1078 | Args[ArgIdx], Info, Deduced, |
1079 | TDF, PartialOrdering)) |
1080 | return Result; |
1081 | |
1082 | PackScope.nextPackElement(); |
1083 | } |
1084 | } else { |
1085 | |
1086 | |
1087 | |
1088 | |
1089 | |
1090 | |
1091 | |
1092 | |
1093 | |
1094 | |
1095 | |
1096 | |
1097 | |
1098 | |
1099 | |
1100 | |
1101 | |
1102 | Optional<unsigned> NumExpansions = Expansion->getNumExpansions(); |
1103 | if (NumExpansions && !PackScope.isPartiallyExpanded()) { |
1104 | for (unsigned I = 0; I != *NumExpansions && ArgIdx < NumArgs; |
1105 | ++I, ++ArgIdx) |
1106 | PackScope.nextPackElement(); |
1107 | } |
1108 | } |
1109 | |
1110 | |
1111 | |
1112 | if (auto Result = PackScope.finish()) |
1113 | return Result; |
1114 | } |
1115 | |
1116 | |
1117 | if (ArgIdx < NumArgs) |
1118 | return Sema::TDK_MiscellaneousDeductionFailure; |
1119 | |
1120 | return Sema::TDK_Success; |
1121 | } |
1122 | |
1123 | |
1124 | |
1125 | |
1126 | |
1127 | static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType, |
1128 | QualType ArgType) { |
1129 | Qualifiers ParamQs = ParamType.getQualifiers(); |
1130 | Qualifiers ArgQs = ArgType.getQualifiers(); |
1131 | |
1132 | if (ParamQs == ArgQs) |
1133 | return false; |
1134 | |
1135 | |
1136 | if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() && |
1137 | ParamQs.hasObjCGCAttr()) |
1138 | return true; |
1139 | |
1140 | |
1141 | if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() && |
1142 | ParamQs.hasAddressSpace()) |
1143 | return true; |
1144 | |
1145 | |
1146 | if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() && |
1147 | ParamQs.hasObjCLifetime()) |
1148 | return true; |
1149 | |
1150 | |
1151 | return (ParamQs.getCVRQualifiers() & ~ArgQs.getCVRQualifiers()) != 0; |
1152 | } |
1153 | |
1154 | |
1155 | |
1156 | |
1157 | |
1158 | |
1159 | |
1160 | |
1161 | bool Sema::isSameOrCompatibleFunctionType(CanQualType Param, |
1162 | CanQualType Arg) { |
1163 | const FunctionType *ParamFunction = Param->getAs<FunctionType>(), |
1164 | *ArgFunction = Arg->getAs<FunctionType>(); |
1165 | |
1166 | |
1167 | if (!ParamFunction || !ArgFunction) |
1168 | return Param == Arg; |
1169 | |
1170 | |
1171 | QualType AdjustedParam; |
1172 | if (IsFunctionConversion(Param, Arg, AdjustedParam)) |
1173 | return Arg == Context.getCanonicalType(AdjustedParam); |
1174 | |
1175 | |
1176 | |
1177 | return Param == Arg; |
1178 | } |
1179 | |
1180 | |
1181 | |
1182 | |
1183 | |
1184 | static unsigned getFirstInnerIndex(FunctionTemplateDecl *FTD) { |
1185 | auto *Guide = dyn_cast<CXXDeductionGuideDecl>(FTD->getTemplatedDecl()); |
1186 | if (!Guide || !Guide->isImplicit()) |
1187 | return 0; |
1188 | return Guide->getDeducedTemplate()->getTemplateParameters()->size(); |
1189 | } |
1190 | |
1191 | |
1192 | static bool isForwardingReference(QualType Param, unsigned FirstInnerIndex) { |
1193 | |
1194 | |
1195 | |
1196 | |
1197 | if (auto *ParamRef = Param->getAs<RValueReferenceType>()) { |
1198 | if (ParamRef->getPointeeType().getQualifiers()) |
1199 | return false; |
1200 | auto *TypeParm = ParamRef->getPointeeType()->getAs<TemplateTypeParmType>(); |
1201 | return TypeParm && TypeParm->getIndex() >= FirstInnerIndex; |
1202 | } |
1203 | return false; |
1204 | } |
1205 | |
1206 | |
1207 | |
1208 | |
1209 | |
1210 | |
1211 | |
1212 | |
1213 | |
1214 | |
1215 | |
1216 | |
1217 | |
1218 | |
1219 | |
1220 | |
1221 | |
1222 | |
1223 | |
1224 | static Sema::TemplateDeductionResult DeduceTemplateBases( |
1225 | Sema &S, const RecordType *RecordT, TemplateParameterList *TemplateParams, |
1226 | const TemplateSpecializationType *SpecParam, TemplateDeductionInfo &Info, |
1227 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
1228 | |
1229 | |
1230 | |
1231 | |
1232 | |
1233 | |
1234 | |
1235 | |
1236 | |
1237 | |
1238 | |
1239 | |
1240 | |
1241 | |
1242 | |
1243 | |
1244 | |
1245 | |
1246 | |
1247 | llvm::SmallPtrSet<const RecordType *, 8> Visited; |
1248 | SmallVector<const RecordType *, 8> ToVisit; |
1249 | |
1250 | |
1251 | llvm::MapVector<const RecordType *, SmallVector<DeducedTemplateArgument, 8>> |
1252 | Matches; |
1253 | |
1254 | auto AddBases = [&Visited, &ToVisit](const RecordType *RT) { |
1255 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
1256 | for (const auto &Base : RD->bases()) { |
1257 | assert(Base.getType()->isRecordType() && |
1258 | "Base class that isn't a record?"); |
1259 | const RecordType *RT = Base.getType()->getAs<RecordType>(); |
1260 | if (Visited.insert(RT).second) |
1261 | ToVisit.push_back(Base.getType()->getAs<RecordType>()); |
1262 | } |
1263 | }; |
1264 | |
1265 | |
1266 | AddBases(RecordT); |
1267 | |
1268 | |
1269 | |
1270 | while (!ToVisit.empty()) { |
1271 | const RecordType *NextT = ToVisit.pop_back_val(); |
1272 | |
1273 | SmallVector<DeducedTemplateArgument, 8> DeducedCopy(Deduced.begin(), |
1274 | Deduced.end()); |
1275 | TemplateDeductionInfo BaseInfo(TemplateDeductionInfo::ForBase, Info); |
1276 | Sema::TemplateDeductionResult BaseResult = |
1277 | DeduceTemplateArguments(S, TemplateParams, SpecParam, |
1278 | QualType(NextT, 0), BaseInfo, DeducedCopy); |
1279 | |
1280 | |
1281 | |
1282 | if (BaseResult == Sema::TDK_Success) |
1283 | Matches.insert({NextT, DeducedCopy}); |
1284 | else |
1285 | AddBases(NextT); |
1286 | } |
1287 | |
1288 | |
1289 | |
1290 | |
1291 | |
1292 | |
1293 | |
1294 | if (Matches.size() > 1) { |
1295 | Visited.clear(); |
1296 | for (const auto &Match : Matches) |
1297 | AddBases(Match.first); |
1298 | |
1299 | |
1300 | |
1301 | while (Matches.size() > 1 && !ToVisit.empty()) { |
1302 | const RecordType *NextT = ToVisit.pop_back_val(); |
1303 | Matches.erase(NextT); |
1304 | |
1305 | |
1306 | |
1307 | AddBases(NextT); |
1308 | } |
1309 | } |
1310 | |
1311 | if (Matches.empty()) |
1312 | return Sema::TDK_Invalid; |
1313 | if (Matches.size() > 1) |
1314 | return Sema::TDK_MiscellaneousDeductionFailure; |
1315 | |
1316 | std::swap(Matches.front().second, Deduced); |
1317 | return Sema::TDK_Success; |
1318 | } |
1319 | |
1320 | |
1321 | |
1322 | |
1323 | |
1324 | |
1325 | |
1326 | |
1327 | |
1328 | |
1329 | |
1330 | |
1331 | |
1332 | |
1333 | |
1334 | |
1335 | |
1336 | |
1337 | |
1338 | |
1339 | |
1340 | |
1341 | |
1342 | |
1343 | |
1344 | static Sema::TemplateDeductionResult |
1345 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
1346 | TemplateParameterList *TemplateParams, |
1347 | QualType ParamIn, QualType ArgIn, |
1348 | TemplateDeductionInfo &Info, |
1349 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
1350 | unsigned TDF, |
1351 | bool PartialOrdering, |
1352 | bool DeducedFromArrayBound) { |
1353 | |
1354 | |
1355 | QualType Param = S.Context.getCanonicalType(ParamIn); |
1356 | QualType Arg = S.Context.getCanonicalType(ArgIn); |
1357 | |
1358 | |
1359 | |
1360 | if (const PackExpansionType *ArgExpansion |
| |
1361 | = dyn_cast<PackExpansionType>(Arg)) |
1362 | Arg = ArgExpansion->getPattern(); |
1363 | |
1364 | if (PartialOrdering) { |
| 2 | | Assuming 'PartialOrdering' is false | |
|
| |
1365 | |
1366 | |
1367 | |
1368 | |
1369 | const ReferenceType *ParamRef = Param->getAs<ReferenceType>(); |
1370 | if (ParamRef) |
1371 | Param = ParamRef->getPointeeType(); |
1372 | |
1373 | |
1374 | const ReferenceType *ArgRef = Arg->getAs<ReferenceType>(); |
1375 | if (ArgRef) |
1376 | Arg = ArgRef->getPointeeType(); |
1377 | |
1378 | if (ParamRef && ArgRef && S.Context.hasSameUnqualifiedType(Param, Arg)) { |
1379 | |
1380 | |
1381 | |
1382 | |
1383 | |
1384 | |
1385 | |
1386 | |
1387 | |
1388 | |
1389 | |
1390 | |
1391 | |
1392 | |
1393 | |
1394 | |
1395 | |
1396 | |
1397 | |
1398 | Qualifiers ParamQuals = Param.getQualifiers(); |
1399 | Qualifiers ArgQuals = Arg.getQualifiers(); |
1400 | if ((ParamRef->isLValueReferenceType() && |
1401 | !ArgRef->isLValueReferenceType()) || |
1402 | ParamQuals.isStrictSupersetOf(ArgQuals) || |
1403 | (ParamQuals.hasNonTrivialObjCLifetime() && |
1404 | ArgQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone && |
1405 | ParamQuals.withoutObjCLifetime() == |
1406 | ArgQuals.withoutObjCLifetime())) { |
1407 | Info.FirstArg = TemplateArgument(ParamIn); |
1408 | Info.SecondArg = TemplateArgument(ArgIn); |
1409 | return Sema::TDK_NonDeducedMismatch; |
1410 | } |
1411 | } |
1412 | |
1413 | |
1414 | |
1415 | |
1416 | |
1417 | Param = Param.getUnqualifiedType(); |
1418 | |
1419 | |
1420 | Arg = Arg.getUnqualifiedType(); |
1421 | } else { |
1422 | |
1423 | |
1424 | |
1425 | |
1426 | if (TDF & TDF_ParamWithReferenceType) { |
| 4 | | Assuming the condition is false | |
|
1427 | Qualifiers Quals; |
1428 | QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals); |
1429 | Quals.setCVRQualifiers(Quals.getCVRQualifiers() & |
1430 | Arg.getCVRQualifiers()); |
1431 | Param = S.Context.getQualifiedType(UnqualParam, Quals); |
1432 | } |
1433 | |
1434 | if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) { |
| 5 | | Assuming the condition is false | |
|
1435 | |
1436 | |
1437 | |
1438 | |
1439 | |
1440 | |
1441 | |
1442 | |
1443 | |
1444 | |
1445 | |
1446 | TDF &= ~TDF_TopLevelParameterTypeList; |
1447 | if (isForwardingReference(Param, 0) && Arg->isLValueReferenceType()) |
1448 | Param = Param->getPointeeType(); |
1449 | } |
1450 | } |
1451 | |
1452 | |
1453 | |
1454 | |
1455 | |
1456 | |
1457 | |
1458 | |
1459 | if (const TemplateTypeParmType *TemplateTypeParm |
| |
1460 | = Param->getAs<TemplateTypeParmType>()) { |
| 6 | | Assuming the object is not a 'TemplateTypeParmType' | |
|
1461 | |
1462 | |
1463 | if (Arg->isPlaceholderType() || |
1464 | Info.getDeducedDepth() != TemplateTypeParm->getDepth()) |
1465 | return Sema::TDK_Success; |
1466 | |
1467 | unsigned Index = TemplateTypeParm->getIndex(); |
1468 | bool RecanonicalizeArg = false; |
1469 | |
1470 | |
1471 | |
1472 | if (isa<ArrayType>(Arg)) { |
1473 | Qualifiers Quals; |
1474 | Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); |
1475 | if (Quals) { |
1476 | Arg = S.Context.getQualifiedType(Arg, Quals); |
1477 | RecanonicalizeArg = true; |
1478 | } |
1479 | } |
1480 | |
1481 | |
1482 | |
1483 | if (!(TDF & TDF_IgnoreQualifiers) && |
1484 | hasInconsistentOrSupersetQualifiersOf(Param, Arg)) { |
1485 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
1486 | Info.FirstArg = TemplateArgument(Param); |
1487 | Info.SecondArg = TemplateArgument(Arg); |
1488 | return Sema::TDK_Underqualified; |
1489 | } |
1490 | |
1491 | |
1492 | |
1493 | if (Arg->isFunctionType() && Param.hasQualifiers()) { |
1494 | return Sema::TDK_NonDeducedMismatch; |
1495 | } |
1496 | |
1497 | assert(TemplateTypeParm->getDepth() == Info.getDeducedDepth() && |
1498 | "saw template type parameter with wrong depth"); |
1499 | assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function"); |
1500 | QualType DeducedType = Arg; |
1501 | |
1502 | |
1503 | |
1504 | Qualifiers DeducedQs = DeducedType.getQualifiers(); |
1505 | Qualifiers ParamQs = Param.getQualifiers(); |
1506 | DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers()); |
1507 | if (ParamQs.hasObjCGCAttr()) |
1508 | DeducedQs.removeObjCGCAttr(); |
1509 | if (ParamQs.hasAddressSpace()) |
1510 | DeducedQs.removeAddressSpace(); |
1511 | if (ParamQs.hasObjCLifetime()) |
1512 | DeducedQs.removeObjCLifetime(); |
1513 | |
1514 | |
1515 | |
1516 | |
1517 | if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType() && |
1518 | !DeducedType->isDependentType()) { |
1519 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
1520 | Info.FirstArg = TemplateArgument(Param); |
1521 | Info.SecondArg = TemplateArgument(Arg); |
1522 | return Sema::TDK_Underqualified; |
1523 | } |
1524 | |
1525 | |
1526 | |
1527 | |
1528 | if (S.getLangOpts().ObjCAutoRefCount && |
1529 | DeducedType->isObjCLifetimeType() && |
1530 | !DeducedQs.hasObjCLifetime()) |
1531 | DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong); |
1532 | |
1533 | DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(), |
1534 | DeducedQs); |
1535 | |
1536 | if (RecanonicalizeArg) |
1537 | DeducedType = S.Context.getCanonicalType(DeducedType); |
1538 | |
1539 | DeducedTemplateArgument NewDeduced(DeducedType, DeducedFromArrayBound); |
1540 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
1541 | Deduced[Index], |
1542 | NewDeduced); |
1543 | if (Result.isNull()) { |
1544 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
1545 | Info.FirstArg = Deduced[Index]; |
1546 | Info.SecondArg = NewDeduced; |
1547 | return Sema::TDK_Inconsistent; |
1548 | } |
1549 | |
1550 | Deduced[Index] = Result; |
1551 | return Sema::TDK_Success; |
1552 | } |
1553 | |
1554 | |
1555 | Info.FirstArg = TemplateArgument(ParamIn); |
1556 | Info.SecondArg = TemplateArgument(ArgIn); |
1557 | |
1558 | |
1559 | |
1560 | |
1561 | |
1562 | if (isa<SubstTemplateTypeParmPackType>(Param)) |
| 8 | | Assuming 'Param' is not a 'SubstTemplateTypeParmPackType' | |
|
| |
1563 | return Sema::TDK_Success; |
1564 | |
1565 | |
1566 | CanQualType CanParam = S.Context.getCanonicalType(Param); |
1567 | CanQualType CanArg = S.Context.getCanonicalType(Arg); |
1568 | if (!(TDF & TDF_IgnoreQualifiers)) { |
| 10 | | Assuming the condition is true | |
|
| |
1569 | if (TDF & TDF_ParamWithReferenceType) { |
| |
1570 | if (hasInconsistentOrSupersetQualifiersOf(Param, Arg)) |
1571 | return Sema::TDK_NonDeducedMismatch; |
1572 | } else if (TDF & TDF_ArgWithReferenceType) { |
| 13 | | Assuming the condition is false | |
|
| |
1573 | |
1574 | |
1575 | |
1576 | if (!Arg.getQualifiers().compatiblyIncludes(Param.getQualifiers())) |
1577 | return Sema::TDK_NonDeducedMismatch; |
1578 | |
1579 | |
1580 | |
1581 | Qualifiers Quals; |
1582 | Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); |
1583 | Arg = S.Context.getQualifiedType(Arg, Param.getQualifiers()); |
1584 | } else if (!IsPossiblyOpaquelyQualifiedType(Param)) { |
| |
1585 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
| 16 | | Assuming the condition is false | |
|
| |
1586 | return Sema::TDK_NonDeducedMismatch; |
1587 | } |
1588 | |
1589 | |
1590 | if (!Param->isDependentType()) { |
| 18 | | Assuming the condition is false | |
|
| |
1591 | if (!(TDF & TDF_SkipNonDependent)) { |
1592 | bool NonDeduced = |
1593 | (TDF & TDF_AllowCompatibleFunctionType) |
1594 | ? !S.isSameOrCompatibleFunctionType(CanParam, CanArg) |
1595 | : Param != Arg; |
1596 | if (NonDeduced) { |
1597 | return Sema::TDK_NonDeducedMismatch; |
1598 | } |
1599 | } |
1600 | return Sema::TDK_Success; |
1601 | } |
1602 | } else if (!Param->isDependentType()) { |
1603 | if (!(TDF & TDF_SkipNonDependent)) { |
1604 | CanQualType ParamUnqualType = CanParam.getUnqualifiedType(), |
1605 | ArgUnqualType = CanArg.getUnqualifiedType(); |
1606 | bool Success = |
1607 | (TDF & TDF_AllowCompatibleFunctionType) |
1608 | ? S.isSameOrCompatibleFunctionType(ParamUnqualType, ArgUnqualType) |
1609 | : ParamUnqualType == ArgUnqualType; |
1610 | if (Success) |
1611 | return Sema::TDK_Success; |
1612 | } else { |
1613 | return Sema::TDK_Success; |
1614 | } |
1615 | } |
1616 | |
1617 | switch (Param->getTypeClass()) { |
| 20 | | Control jumps to 'case DependentSizedMatrix:' at line 2137 | |
|
1618 | |
1619 | #define NON_CANONICAL_TYPE(Class, Base) \ |
1620 | case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class); |
1621 | #define TYPE(Class, Base) |
1622 | #include "clang/AST/TypeNodes.inc" |
1623 | |
1624 | case Type::TemplateTypeParm: |
1625 | case Type::SubstTemplateTypeParmPack: |
1626 | llvm_unreachable("Type nodes handled above"); |
1627 | |
1628 | |
1629 | |
1630 | case Type::Builtin: |
1631 | case Type::VariableArray: |
1632 | case Type::Vector: |
1633 | case Type::FunctionNoProto: |
1634 | case Type::Record: |
1635 | case Type::Enum: |
1636 | case Type::ObjCObject: |
1637 | case Type::ObjCInterface: |
1638 | case Type::ObjCObjectPointer: |
1639 | case Type::ExtInt: |
1640 | if (TDF & TDF_SkipNonDependent) |
1641 | return Sema::TDK_Success; |
1642 | |
1643 | if (TDF & TDF_IgnoreQualifiers) { |
1644 | Param = Param.getUnqualifiedType(); |
1645 | Arg = Arg.getUnqualifiedType(); |
1646 | } |
1647 | |
1648 | return Param == Arg? Sema::TDK_Success : Sema::TDK_NonDeducedMismatch; |
1649 | |
1650 | |
1651 | case Type::Complex: |
1652 | if (const ComplexType *ComplexArg = Arg->getAs<ComplexType>()) |
1653 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
1654 | cast<ComplexType>(Param)->getElementType(), |
1655 | ComplexArg->getElementType(), |
1656 | Info, Deduced, TDF); |
1657 | |
1658 | return Sema::TDK_NonDeducedMismatch; |
1659 | |
1660 | |
1661 | case Type::Atomic: |
1662 | if (const AtomicType *AtomicArg = Arg->getAs<AtomicType>()) |
1663 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
1664 | cast<AtomicType>(Param)->getValueType(), |
1665 | AtomicArg->getValueType(), |
1666 | Info, Deduced, TDF); |
1667 | |
1668 | return Sema::TDK_NonDeducedMismatch; |
1669 | |
1670 | |
1671 | case Type::Pointer: { |
1672 | QualType PointeeType; |
1673 | if (const PointerType *PointerArg = Arg->getAs<PointerType>()) { |
1674 | PointeeType = PointerArg->getPointeeType(); |
1675 | } else if (const ObjCObjectPointerType *PointerArg |
1676 | = Arg->getAs<ObjCObjectPointerType>()) { |
1677 | PointeeType = PointerArg->getPointeeType(); |
1678 | } else { |
1679 | return Sema::TDK_NonDeducedMismatch; |
1680 | } |
1681 | |
1682 | unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); |
1683 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
1684 | cast<PointerType>(Param)->getPointeeType(), |
1685 | PointeeType, |
1686 | Info, Deduced, SubTDF); |
1687 | } |
1688 | |
1689 | |
1690 | case Type::LValueReference: { |
1691 | const LValueReferenceType *ReferenceArg = |
1692 | Arg->getAs<LValueReferenceType>(); |
1693 | if (!ReferenceArg) |
1694 | return Sema::TDK_NonDeducedMismatch; |
1695 | |
1696 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
1697 | cast<LValueReferenceType>(Param)->getPointeeType(), |
1698 | ReferenceArg->getPointeeType(), Info, Deduced, 0); |
1699 | } |
1700 | |
1701 | |
1702 | case Type::RValueReference: { |
1703 | const RValueReferenceType *ReferenceArg = |
1704 | Arg->getAs<RValueReferenceType>(); |
1705 | if (!ReferenceArg) |
1706 | return Sema::TDK_NonDeducedMismatch; |
1707 | |
1708 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
1709 | cast<RValueReferenceType>(Param)->getPointeeType(), |
1710 | ReferenceArg->getPointeeType(), |
1711 | Info, Deduced, 0); |
1712 | } |
1713 | |
1714 | |
1715 | case Type::IncompleteArray: { |
1716 | const IncompleteArrayType *IncompleteArrayArg = |
1717 | S.Context.getAsIncompleteArrayType(Arg); |
1718 | if (!IncompleteArrayArg) |
1719 | return Sema::TDK_NonDeducedMismatch; |
1720 | |
1721 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
1722 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
1723 | S.Context.getAsIncompleteArrayType(Param)->getElementType(), |
1724 | IncompleteArrayArg->getElementType(), |
1725 | Info, Deduced, SubTDF); |
1726 | } |
1727 | |
1728 | |
1729 | case Type::ConstantArray: { |
1730 | const ConstantArrayType *ConstantArrayArg = |
1731 | S.Context.getAsConstantArrayType(Arg); |
1732 | if (!ConstantArrayArg) |
1733 | return Sema::TDK_NonDeducedMismatch; |
1734 | |
1735 | const ConstantArrayType *ConstantArrayParm = |
1736 | S.Context.getAsConstantArrayType(Param); |
1737 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
1738 | return Sema::TDK_NonDeducedMismatch; |
1739 | |
1740 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
1741 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
1742 | ConstantArrayParm->getElementType(), |
1743 | ConstantArrayArg->getElementType(), |
1744 | Info, Deduced, SubTDF); |
1745 | } |
1746 | |
1747 | |
1748 | case Type::DependentSizedArray: { |
1749 | const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg); |
1750 | if (!ArrayArg) |
1751 | return Sema::TDK_NonDeducedMismatch; |
1752 | |
1753 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
1754 | |
1755 | |
1756 | const DependentSizedArrayType *DependentArrayParm |
1757 | = S.Context.getAsDependentSizedArrayType(Param); |
1758 | if (Sema::TemplateDeductionResult Result |
1759 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
1760 | DependentArrayParm->getElementType(), |
1761 | ArrayArg->getElementType(), |
1762 | Info, Deduced, SubTDF)) |
1763 | return Result; |
1764 | |
1765 | |
1766 | const NonTypeTemplateParmDecl *NTTP |
1767 | = getDeducedParameterFromExpr(Info, DependentArrayParm->getSizeExpr()); |
1768 | if (!NTTP) |
1769 | return Sema::TDK_Success; |
1770 | |
1771 | |
1772 | |
1773 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
1774 | "saw non-type template parameter with wrong depth"); |
1775 | if (const ConstantArrayType *ConstantArrayArg |
1776 | = dyn_cast<ConstantArrayType>(ArrayArg)) { |
1777 | llvm::APSInt Size(ConstantArrayArg->getSize()); |
1778 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, Size, |
1779 | S.Context.getSizeType(), |
1780 | true, |
1781 | Info, Deduced); |
1782 | } |
1783 | if (const DependentSizedArrayType *DependentArrayArg |
1784 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
1785 | if (DependentArrayArg->getSizeExpr()) |
1786 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
1787 | DependentArrayArg->getSizeExpr(), |
1788 | Info, Deduced); |
1789 | |
1790 | |
1791 | return Sema::TDK_NonDeducedMismatch; |
1792 | } |
1793 | |
1794 | |
1795 | |
1796 | |
1797 | case Type::FunctionProto: { |
1798 | unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList; |
1799 | const FunctionProtoType *FunctionProtoArg = |
1800 | dyn_cast<FunctionProtoType>(Arg); |
1801 | if (!FunctionProtoArg) |
1802 | return Sema::TDK_NonDeducedMismatch; |
1803 | |
1804 | const FunctionProtoType *FunctionProtoParam = |
1805 | cast<FunctionProtoType>(Param); |
1806 | |
1807 | if (FunctionProtoParam->getMethodQuals() |
1808 | != FunctionProtoArg->getMethodQuals() || |
1809 | FunctionProtoParam->getRefQualifier() |
1810 | != FunctionProtoArg->getRefQualifier() || |
1811 | FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
1812 | return Sema::TDK_NonDeducedMismatch; |
1813 | |
1814 | |
1815 | if (auto Result = DeduceTemplateArgumentsByTypeMatch( |
1816 | S, TemplateParams, FunctionProtoParam->getReturnType(), |
1817 | FunctionProtoArg->getReturnType(), Info, Deduced, 0)) |
1818 | return Result; |
1819 | |
1820 | |
1821 | if (auto Result = DeduceTemplateArguments( |
1822 | S, TemplateParams, FunctionProtoParam->param_type_begin(), |
1823 | FunctionProtoParam->getNumParams(), |
1824 | FunctionProtoArg->param_type_begin(), |
1825 | FunctionProtoArg->getNumParams(), Info, Deduced, SubTDF)) |
1826 | return Result; |
1827 | |
1828 | if (TDF & TDF_AllowCompatibleFunctionType) |
1829 | return Sema::TDK_Success; |
1830 | |
1831 | |
1832 | |
1833 | |
1834 | Expr *NoexceptExpr = FunctionProtoParam->getNoexceptExpr(); |
1835 | if (const NonTypeTemplateParmDecl *NTTP = |
1836 | NoexceptExpr ? getDeducedParameterFromExpr(Info, NoexceptExpr) |
1837 | : nullptr) { |
1838 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
1839 | "saw non-type template parameter with wrong depth"); |
1840 | |
1841 | llvm::APSInt Noexcept(1); |
1842 | switch (FunctionProtoArg->canThrow()) { |
1843 | case CT_Cannot: |
1844 | Noexcept = 1; |
1845 | LLVM_FALLTHROUGH; |
1846 | |
1847 | case CT_Can: |
1848 | |
1849 | |
1850 | return DeduceNonTypeTemplateArgument( |
1851 | S, TemplateParams, NTTP, Noexcept, S.Context.BoolTy, |
1852 | true, Info, Deduced); |
1853 | |
1854 | case CT_Dependent: |
1855 | if (Expr *ArgNoexceptExpr = FunctionProtoArg->getNoexceptExpr()) |
1856 | return DeduceNonTypeTemplateArgument( |
1857 | S, TemplateParams, NTTP, ArgNoexceptExpr, Info, Deduced); |
1858 | |
1859 | break; |
1860 | } |
1861 | } |
1862 | |
1863 | |
1864 | |
1865 | |
1866 | |
1867 | return Sema::TDK_Success; |
1868 | } |
1869 | |
1870 | case Type::InjectedClassName: |
1871 | |
1872 | |
1873 | Param = cast<InjectedClassNameType>(Param) |
1874 | ->getInjectedSpecializationType(); |
1875 | assert(isa<TemplateSpecializationType>(Param) && |
1876 | "injected class name is not a template specialization type"); |
1877 | LLVM_FALLTHROUGH; |
1878 | |
1879 | |
1880 | |
1881 | |
1882 | |
1883 | |
1884 | case Type::TemplateSpecialization: { |
1885 | const TemplateSpecializationType *SpecParam = |
1886 | cast<TemplateSpecializationType>(Param); |
1887 | |
1888 | |
1889 | |
1890 | const RecordType *RecordT = Arg->getAs<RecordType>(); |
1891 | if (!(TDF & TDF_DerivedClass) || !RecordT) |
1892 | return DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg, Info, |
1893 | Deduced); |
1894 | |
1895 | SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(), |
1896 | Deduced.end()); |
1897 | |
1898 | Sema::TemplateDeductionResult Result = DeduceTemplateArguments( |
1899 | S, TemplateParams, SpecParam, Arg, Info, Deduced); |
1900 | |
1901 | if (Result == Sema::TDK_Success) |
1902 | return Result; |
1903 | |
1904 | |
1905 | |
1906 | |
1907 | if (!S.isCompleteType(Info.getLocation(), Arg)) |
1908 | return Result; |
1909 | |
1910 | |
1911 | Deduced = DeducedOrig; |
1912 | |
1913 | |
1914 | Sema::TemplateDeductionResult BaseResult = DeduceTemplateBases( |
1915 | S, RecordT, TemplateParams, SpecParam, Info, Deduced); |
1916 | |
1917 | if (BaseResult != Sema::TDK_Invalid) |
1918 | return BaseResult; |
1919 | return Result; |
1920 | } |
1921 | |
1922 | |
1923 | |
1924 | |
1925 | |
1926 | |
1927 | |
1928 | |
1929 | |
1930 | |
1931 | case Type::MemberPointer: { |
1932 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
1933 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
1934 | if (!MemPtrArg) |
1935 | return Sema::TDK_NonDeducedMismatch; |
1936 | |
1937 | QualType ParamPointeeType = MemPtrParam->getPointeeType(); |
1938 | if (ParamPointeeType->isFunctionType()) |
1939 | S.adjustMemberFunctionCC(ParamPointeeType, true, |
1940 | false, Info.getLocation()); |
1941 | QualType ArgPointeeType = MemPtrArg->getPointeeType(); |
1942 | if (ArgPointeeType->isFunctionType()) |
1943 | S.adjustMemberFunctionCC(ArgPointeeType, true, |
1944 | false, Info.getLocation()); |
1945 | |
1946 | if (Sema::TemplateDeductionResult Result |
1947 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
1948 | ParamPointeeType, |
1949 | ArgPointeeType, |
1950 | Info, Deduced, |
1951 | TDF & TDF_IgnoreQualifiers)) |
1952 | return Result; |
1953 | |
1954 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
1955 | QualType(MemPtrParam->getClass(), 0), |
1956 | QualType(MemPtrArg->getClass(), 0), |
1957 | Info, Deduced, |
1958 | TDF & TDF_IgnoreQualifiers); |
1959 | } |
1960 | |
1961 | |
1962 | |
1963 | |
1964 | |
1965 | |
1966 | case Type::BlockPointer: { |
1967 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
1968 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
1969 | |
1970 | if (!BlockPtrArg) |
1971 | return Sema::TDK_NonDeducedMismatch; |
1972 | |
1973 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
1974 | BlockPtrParam->getPointeeType(), |
1975 | BlockPtrArg->getPointeeType(), |
1976 | Info, Deduced, 0); |
1977 | } |
1978 | |
1979 | |
1980 | |
1981 | |
1982 | case Type::ExtVector: { |
1983 | const ExtVectorType *VectorParam = cast<ExtVectorType>(Param); |
1984 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
1985 | |
1986 | if (VectorParam->getNumElements() != VectorArg->getNumElements()) |
1987 | return Sema::TDK_NonDeducedMismatch; |
1988 | |
1989 | |
1990 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
1991 | VectorParam->getElementType(), |
1992 | VectorArg->getElementType(), |
1993 | Info, Deduced, TDF); |
1994 | } |
1995 | |
1996 | if (const DependentSizedExtVectorType *VectorArg |
1997 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
1998 | |
1999 | |
2000 | |
2001 | |
2002 | |
2003 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
2004 | VectorParam->getElementType(), |
2005 | VectorArg->getElementType(), |
2006 | Info, Deduced, TDF); |
2007 | } |
2008 | |
2009 | return Sema::TDK_NonDeducedMismatch; |
2010 | } |
2011 | |
2012 | case Type::DependentVector: { |
2013 | const auto *VectorParam = cast<DependentVectorType>(Param); |
2014 | |
2015 | if (const auto *VectorArg = dyn_cast<VectorType>(Arg)) { |
2016 | |
2017 | if (Sema::TemplateDeductionResult Result = |
2018 | DeduceTemplateArgumentsByTypeMatch( |
2019 | S, TemplateParams, VectorParam->getElementType(), |
2020 | VectorArg->getElementType(), Info, Deduced, TDF)) |
2021 | return Result; |
2022 | |
2023 | |
2024 | const NonTypeTemplateParmDecl *NTTP = |
2025 | getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); |
2026 | if (!NTTP) |
2027 | return Sema::TDK_Success; |
2028 | |
2029 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
2030 | ArgSize = VectorArg->getNumElements(); |
2031 | |
2032 | |
2033 | |
2034 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, |
2035 | S.Context.UnsignedIntTy, true, |
2036 | Info, Deduced); |
2037 | } |
2038 | |
2039 | if (const auto *VectorArg = dyn_cast<DependentVectorType>(Arg)) { |
2040 | |
2041 | if (Sema::TemplateDeductionResult Result = |
2042 | DeduceTemplateArgumentsByTypeMatch( |
2043 | S, TemplateParams, VectorParam->getElementType(), |
2044 | VectorArg->getElementType(), Info, Deduced, TDF)) |
2045 | return Result; |
2046 | |
2047 | |
2048 | const NonTypeTemplateParmDecl *NTTP = getDeducedParameterFromExpr( |
2049 | Info, VectorParam->getSizeExpr()); |
2050 | if (!NTTP) |
2051 | return Sema::TDK_Success; |
2052 | |
2053 | return DeduceNonTypeTemplateArgument( |
2054 | S, TemplateParams, NTTP, VectorArg->getSizeExpr(), Info, Deduced); |
2055 | } |
2056 | |
2057 | return Sema::TDK_NonDeducedMismatch; |
2058 | } |
2059 | |
2060 | |
2061 | |
2062 | |
2063 | case Type::DependentSizedExtVector: { |
2064 | const DependentSizedExtVectorType *VectorParam |
2065 | = cast<DependentSizedExtVectorType>(Param); |
2066 | |
2067 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
2068 | |
2069 | if (Sema::TemplateDeductionResult Result |
2070 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
2071 | VectorParam->getElementType(), |
2072 | VectorArg->getElementType(), |
2073 | Info, Deduced, TDF)) |
2074 | return Result; |
2075 | |
2076 | |
2077 | const NonTypeTemplateParmDecl *NTTP = |
2078 | getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); |
2079 | if (!NTTP) |
2080 | return Sema::TDK_Success; |
2081 | |
2082 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
2083 | ArgSize = VectorArg->getNumElements(); |
2084 | |
2085 | |
2086 | |
2087 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, |
2088 | S.Context.IntTy, true, Info, |
2089 | Deduced); |
2090 | } |
2091 | |
2092 | if (const DependentSizedExtVectorType *VectorArg |
2093 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
2094 | |
2095 | if (Sema::TemplateDeductionResult Result |
2096 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
2097 | VectorParam->getElementType(), |
2098 | VectorArg->getElementType(), |
2099 | Info, Deduced, TDF)) |
2100 | return Result; |
2101 | |
2102 | |
2103 | const NonTypeTemplateParmDecl *NTTP = |
2104 | getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); |
2105 | if (!NTTP) |
2106 | return Sema::TDK_Success; |
2107 | |
2108 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
2109 | VectorArg->getSizeExpr(), |
2110 | Info, Deduced); |
2111 | } |
2112 | |
2113 | return Sema::TDK_NonDeducedMismatch; |
2114 | } |
2115 | |
2116 | |
2117 | |
2118 | |
2119 | |
2120 | case Type::ConstantMatrix: { |
2121 | const ConstantMatrixType *MatrixArg = dyn_cast<ConstantMatrixType>(Arg); |
2122 | if (!MatrixArg) |
2123 | return Sema::TDK_NonDeducedMismatch; |
2124 | |
2125 | const ConstantMatrixType *MatrixParam = cast<ConstantMatrixType>(Param); |
2126 | |
2127 | if (MatrixParam->getNumRows() != MatrixArg->getNumRows() || |
2128 | MatrixParam->getNumColumns() != MatrixArg->getNumColumns()) { |
2129 | return Sema::TDK_NonDeducedMismatch; |
2130 | } |
2131 | |
2132 | return DeduceTemplateArgumentsByTypeMatch( |
2133 | S, TemplateParams, MatrixParam->getElementType(), |
2134 | MatrixArg->getElementType(), Info, Deduced, TDF); |
2135 | } |
2136 | |
2137 | case Type::DependentSizedMatrix: { |
2138 | const MatrixType *MatrixArg = dyn_cast<MatrixType>(Arg); |
2139 | if (!MatrixArg) |
| 21 | | Assuming 'MatrixArg' is non-null | |
|
| |
2140 | return Sema::TDK_NonDeducedMismatch; |
2141 | |
2142 | |
2143 | const DependentSizedMatrixType *MatrixParam = |
2144 | cast<DependentSizedMatrixType>(Param); |
2145 | if (Sema::TemplateDeductionResult Result = |
| |
| |
2146 | DeduceTemplateArgumentsByTypeMatch( |
2147 | S, TemplateParams, MatrixParam->getElementType(), |
2148 | MatrixArg->getElementType(), Info, Deduced, TDF)) |
2149 | return Result; |
2150 | |
2151 | |
2152 | auto DeduceMatrixArg = |
2153 | [&S, &Info, &Deduced, &TemplateParams]( |
2154 | Expr *ParamExpr, const MatrixType *Arg, |
2155 | unsigned (ConstantMatrixType::*GetArgDimension)() const, |
2156 | Expr *(DependentSizedMatrixType::*GetArgDimensionExpr)() const) { |
2157 | const auto *ArgConstMatrix = dyn_cast<ConstantMatrixType>(Arg); |
| 26 | | Assuming 'Arg' is not a 'ConstantMatrixType' | |
|
2158 | const auto *ArgDepMatrix = dyn_cast<DependentSizedMatrixType>(Arg); |
| 27 | | Assuming 'Arg' is not a 'DependentSizedMatrixType' | |
|
| 28 | | 'ArgDepMatrix' initialized to a null pointer value | |
|
2159 | if (!ParamExpr->isValueDependent()) { |
| 29 | | Assuming the condition is true | |
|
| |
2160 | Optional<llvm::APSInt> ParamConst = |
2161 | ParamExpr->getIntegerConstantExpr(S.Context); |
2162 | if (!ParamConst) |
| 31 | | Assuming the condition is false | |
|
| |
2163 | return Sema::TDK_NonDeducedMismatch; |
2164 | |
2165 | if (ArgConstMatrix) { |
| |
2166 | if ((ArgConstMatrix->*GetArgDimension)() == *ParamConst) |
2167 | return Sema::TDK_Success; |
2168 | return Sema::TDK_NonDeducedMismatch; |
2169 | } |
2170 | |
2171 | Expr *ArgExpr = (ArgDepMatrix->*GetArgDimensionExpr)(); |
| 34 | | Called C++ object pointer is null |
|
2172 | if (Optional<llvm::APSInt> ArgConst = |
2173 | ArgExpr->getIntegerConstantExpr(S.Context)) |
2174 | if (*ArgConst == *ParamConst) |
2175 | return Sema::TDK_Success; |
2176 | return Sema::TDK_NonDeducedMismatch; |
2177 | } |
2178 | |
2179 | const NonTypeTemplateParmDecl *NTTP = |
2180 | getDeducedParameterFromExpr(Info, ParamExpr); |
2181 | if (!NTTP) |
2182 | return Sema::TDK_Success; |
2183 | |
2184 | if (ArgConstMatrix) { |
2185 | llvm::APSInt ArgConst( |
2186 | S.Context.getTypeSize(S.Context.getSizeType())); |
2187 | ArgConst = (ArgConstMatrix->*GetArgDimension)(); |
2188 | return DeduceNonTypeTemplateArgument( |
2189 | S, TemplateParams, NTTP, ArgConst, S.Context.getSizeType(), |
2190 | true, Info, Deduced); |
2191 | } |
2192 | |
2193 | return DeduceNonTypeTemplateArgument( |
2194 | S, TemplateParams, NTTP, (ArgDepMatrix->*GetArgDimensionExpr)(), |
2195 | Info, Deduced); |
2196 | }; |
2197 | |
2198 | auto Result = DeduceMatrixArg(MatrixParam->getRowExpr(), MatrixArg, |
| |
2199 | &ConstantMatrixType::getNumRows, |
2200 | &DependentSizedMatrixType::getRowExpr); |
2201 | if (Result) |
2202 | return Result; |
2203 | |
2204 | return DeduceMatrixArg(MatrixParam->getColumnExpr(), MatrixArg, |
2205 | &ConstantMatrixType::getNumColumns, |
2206 | &DependentSizedMatrixType::getColumnExpr); |
2207 | } |
2208 | |
2209 | |
2210 | |
2211 | |
2212 | case Type::DependentAddressSpace: { |
2213 | const DependentAddressSpaceType *AddressSpaceParam = |
2214 | cast<DependentAddressSpaceType>(Param); |
2215 | |
2216 | if (const DependentAddressSpaceType *AddressSpaceArg = |
2217 | dyn_cast<DependentAddressSpaceType>(Arg)) { |
2218 | |
2219 | if (Sema::TemplateDeductionResult Result = |
2220 | DeduceTemplateArgumentsByTypeMatch( |
2221 | S, TemplateParams, AddressSpaceParam->getPointeeType(), |
2222 | AddressSpaceArg->getPointeeType(), Info, Deduced, TDF)) |
2223 | return Result; |
2224 | |
2225 | |
2226 | const NonTypeTemplateParmDecl *NTTP = getDeducedParameterFromExpr( |
2227 | Info, AddressSpaceParam->getAddrSpaceExpr()); |
2228 | if (!NTTP) |
2229 | return Sema::TDK_Success; |
2230 | |
2231 | return DeduceNonTypeTemplateArgument( |
2232 | S, TemplateParams, NTTP, AddressSpaceArg->getAddrSpaceExpr(), Info, |
2233 | Deduced); |
2234 | } |
2235 | |
2236 | if (isTargetAddressSpace(Arg.getAddressSpace())) { |
2237 | llvm::APSInt ArgAddressSpace(S.Context.getTypeSize(S.Context.IntTy), |
2238 | false); |
2239 | ArgAddressSpace = toTargetAddressSpace(Arg.getAddressSpace()); |
2240 | |
2241 | |
2242 | if (Sema::TemplateDeductionResult Result = |
2243 | DeduceTemplateArgumentsByTypeMatch( |
2244 | S, TemplateParams, AddressSpaceParam->getPointeeType(), |
2245 | S.Context.removeAddrSpaceQualType(Arg), Info, Deduced, TDF)) |
2246 | return Result; |
2247 | |
2248 | |
2249 | const NonTypeTemplateParmDecl *NTTP = getDeducedParameterFromExpr( |
2250 | Info, AddressSpaceParam->getAddrSpaceExpr()); |
2251 | if (!NTTP) |
2252 | return Sema::TDK_Success; |
2253 | |
2254 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
2255 | ArgAddressSpace, S.Context.IntTy, |
2256 | true, Info, Deduced); |
2257 | } |
2258 | |
2259 | return Sema::TDK_NonDeducedMismatch; |
2260 | } |
2261 | case Type::DependentExtInt: { |
2262 | const auto *IntParam = cast<DependentExtIntType>(Param); |
2263 | |
2264 | if (const auto *IntArg = dyn_cast<ExtIntType>(Arg)){ |
2265 | if (IntParam->isUnsigned() != IntArg->isUnsigned()) |
2266 | return Sema::TDK_NonDeducedMismatch; |
2267 | |
2268 | const NonTypeTemplateParmDecl *NTTP = |
2269 | getDeducedParameterFromExpr(Info, IntParam->getNumBitsExpr()); |
2270 | if (!NTTP) |
2271 | return Sema::TDK_Success; |
2272 | |
2273 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
2274 | ArgSize = IntArg->getNumBits(); |
2275 | |
2276 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, |
2277 | S.Context.IntTy, true, Info, |
2278 | Deduced); |
2279 | } |
2280 | |
2281 | if (const auto *IntArg = dyn_cast<DependentExtIntType>(Arg)) { |
2282 | if (IntParam->isUnsigned() != IntArg->isUnsigned()) |
2283 | return Sema::TDK_NonDeducedMismatch; |
2284 | return Sema::TDK_Success; |
2285 | } |
2286 | return Sema::TDK_NonDeducedMismatch; |
2287 | } |
2288 | |
2289 | case Type::TypeOfExpr: |
2290 | case Type::TypeOf: |
2291 | case Type::DependentName: |
2292 | case Type::UnresolvedUsing: |
2293 | case Type::Decltype: |
2294 | case Type::UnaryTransform: |
2295 | case Type::Auto: |
2296 | case Type::DeducedTemplateSpecialization: |
2297 | case Type::DependentTemplateSpecialization: |
2298 | case Type::PackExpansion: |
2299 | case Type::Pipe: |
2300 | |
2301 | return Sema::TDK_Success; |
2302 | } |
2303 | |
2304 | llvm_unreachable("Invalid Type Class!"); |
2305 | } |
2306 | |
2307 | static Sema::TemplateDeductionResult |
2308 | DeduceTemplateArguments(Sema &S, |
2309 | TemplateParameterList *TemplateParams, |
2310 | const TemplateArgument &Param, |
2311 | TemplateArgument Arg, |
2312 | TemplateDeductionInfo &Info, |
2313 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
2314 | |
2315 | |
2316 | |
2317 | if (Arg.isPackExpansion()) |
2318 | Arg = Arg.getPackExpansionPattern(); |
2319 | |
2320 | switch (Param.getKind()) { |
2321 | case TemplateArgument::Null: |
2322 | llvm_unreachable("Null template argument in parameter list"); |
2323 | |
2324 | case TemplateArgument::Type: |
2325 | if (Arg.getKind() == TemplateArgument::Type) |
2326 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
2327 | Param.getAsType(), |
2328 | Arg.getAsType(), |
2329 | Info, Deduced, 0); |
2330 | Info.FirstArg = Param; |
2331 | Info.SecondArg = Arg; |
2332 | return Sema::TDK_NonDeducedMismatch; |
2333 | |
2334 | case TemplateArgument::Template: |
2335 | if (Arg.getKind() == TemplateArgument::Template) |
2336 | return DeduceTemplateArguments(S, TemplateParams, |
2337 | Param.getAsTemplate(), |
2338 | Arg.getAsTemplate(), Info, Deduced); |
2339 | Info.FirstArg = Param; |
2340 | Info.SecondArg = Arg; |
2341 | return Sema::TDK_NonDeducedMismatch; |
2342 | |
2343 | case TemplateArgument::TemplateExpansion: |
2344 | llvm_unreachable("caller should handle pack expansions"); |
2345 | |
2346 | case TemplateArgument::Declaration: |
2347 | if (Arg.getKind() == TemplateArgument::Declaration && |
2348 | isSameDeclaration(Param.getAsDecl(), Arg.getAsDecl())) |
2349 | return Sema::TDK_Success; |
2350 | |
2351 | Info.FirstArg = Param; |
2352 | Info.SecondArg = Arg; |
2353 | return Sema::TDK_NonDeducedMismatch; |
2354 | |
2355 | case TemplateArgument::NullPtr: |
2356 | if (Arg.getKind() == TemplateArgument::NullPtr && |
2357 | S.Context.hasSameType(Param.getNullPtrType(), Arg.getNullPtrType())) |
2358 | return Sema::TDK_Success; |
2359 | |
2360 | Info.FirstArg = Param; |
2361 | Info.SecondArg = Arg; |
2362 | return Sema::TDK_NonDeducedMismatch; |
2363 | |
2364 | case TemplateArgument::Integral: |
2365 | if (Arg.getKind() == TemplateArgument::Integral) { |
2366 | if (hasSameExtendedValue(Param.getAsIntegral(), Arg.getAsIntegral())) |
2367 | return Sema::TDK_Success; |
2368 | |
2369 | Info.FirstArg = Param; |
2370 | Info.SecondArg = Arg; |
2371 | return Sema::TDK_NonDeducedMismatch; |
2372 | } |
2373 | |
2374 | if (Arg.getKind() == TemplateArgument::Expression) { |
2375 | Info.FirstArg = Param; |
2376 | Info.SecondArg = Arg; |
2377 | return Sema::TDK_NonDeducedMismatch; |
2378 | } |
2379 | |
2380 | Info.FirstArg = Param; |
2381 | Info.SecondArg = Arg; |
2382 | return Sema::TDK_NonDeducedMismatch; |
2383 | |
2384 | case TemplateArgument::Expression: |
2385 | if (const NonTypeTemplateParmDecl *NTTP = |
2386 | getDeducedParameterFromExpr(Info, Param.getAsExpr())) { |
2387 | if (Arg.getKind() == TemplateArgument::Integral) |
2388 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
2389 | Arg.getAsIntegral(), |
2390 | Arg.getIntegralType(), |
2391 | false, |
2392 | Info, Deduced); |
2393 | if (Arg.getKind() == TemplateArgument::NullPtr) |
2394 | return DeduceNullPtrTemplateArgument(S, TemplateParams, NTTP, |
2395 | Arg.getNullPtrType(), |
2396 | Info, Deduced); |
2397 | if (Arg.getKind() == TemplateArgument::Expression) |
2398 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
2399 | Arg.getAsExpr(), Info, Deduced); |
2400 | if (Arg.getKind() == TemplateArgument::Declaration) |
2401 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
2402 | Arg.getAsDecl(), |
2403 | Arg.getParamTypeForDecl(), |
2404 | Info, Deduced); |
2405 | |
2406 | Info.FirstArg = Param; |
2407 | Info.SecondArg = Arg; |
2408 | return Sema::TDK_NonDeducedMismatch; |
2409 | } |
2410 | |
2411 | |
2412 | return Sema::TDK_Success; |
2413 | |
2414 | case TemplateArgument::Pack: |
2415 | llvm_unreachable("Argument packs should be expanded by the caller!"); |
2416 | } |
2417 | |
2418 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
2419 | } |
2420 | |
2421 | |
2422 | |
2423 | |
2424 | |
2425 | |
2426 | |
2427 | |
2428 | |
2429 | static bool hasTemplateArgumentForDeduction(ArrayRef<TemplateArgument> &Args, |
2430 | unsigned &ArgIdx) { |
2431 | if (ArgIdx == Args.size()) |
2432 | return false; |
2433 | |
2434 | const TemplateArgument &Arg = Args[ArgIdx]; |
2435 | if (Arg.getKind() != TemplateArgument::Pack) |
2436 | return true; |
2437 | |
2438 | assert(ArgIdx == Args.size() - 1 && "Pack not at the end of argument list?"); |
2439 | Args = Arg.pack_elements(); |
2440 | ArgIdx = 0; |
2441 | return ArgIdx < Args.size(); |
2442 | } |
2443 | |
2444 | |
2445 | |
2446 | static bool hasPackExpansionBeforeEnd(ArrayRef<TemplateArgument> Args) { |
2447 | bool FoundPackExpansion = false; |
2448 | for (const auto &A : Args) { |
2449 | if (FoundPackExpansion) |
2450 | return true; |
2451 | |
2452 | if (A.getKind() == TemplateArgument::Pack) |
2453 | return hasPackExpansionBeforeEnd(A.pack_elements()); |
2454 | |
2455 | |
2456 | |
2457 | if (A.isPackExpansion()) |
2458 | FoundPackExpansion = true; |
2459 | } |
2460 | |
2461 | return false; |
2462 | } |
2463 | |
2464 | static Sema::TemplateDeductionResult |
2465 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
2466 | ArrayRef<TemplateArgument> Params, |
2467 | ArrayRef<TemplateArgument> Args, |
2468 | TemplateDeductionInfo &Info, |
2469 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
2470 | bool NumberOfArgumentsMustMatch) { |
2471 | |
2472 | |
2473 | |
2474 | |
2475 | if (hasPackExpansionBeforeEnd(Params)) |
2476 | return Sema::TDK_Success; |
2477 | |
2478 | |
2479 | |
2480 | |
2481 | |
2482 | unsigned ArgIdx = 0, ParamIdx = 0; |
2483 | for (; hasTemplateArgumentForDeduction(Params, ParamIdx); ++ParamIdx) { |
2484 | if (!Params[ParamIdx].isPackExpansion()) { |
2485 | |
2486 | |
2487 | |
2488 | if (!hasTemplateArgumentForDeduction(Args, ArgIdx)) |
2489 | return NumberOfArgumentsMustMatch |
2490 | ? Sema::TDK_MiscellaneousDeductionFailure |
2491 | : Sema::TDK_Success; |
2492 | |
2493 | |
2494 | |
2495 | |
2496 | if (Args[ArgIdx].isPackExpansion()) |
2497 | return Sema::TDK_MiscellaneousDeductionFailure; |
2498 | |
2499 | |
2500 | if (Sema::TemplateDeductionResult Result |
2501 | = DeduceTemplateArguments(S, TemplateParams, |
2502 | Params[ParamIdx], Args[ArgIdx], |
2503 | Info, Deduced)) |
2504 | return Result; |
2505 | |
2506 | |
2507 | ++ArgIdx; |
2508 | continue; |
2509 | } |
2510 | |
2511 | |
2512 | |
2513 | |
2514 | |
2515 | |
2516 | |
2517 | |
2518 | TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern(); |
2519 | |
2520 | |
2521 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
2522 | |
2523 | |
2524 | |
2525 | |
2526 | for (; hasTemplateArgumentForDeduction(Args, ArgIdx) && |
2527 | PackScope.hasNextElement(); |
2528 | ++ArgIdx) { |
2529 | |
2530 | if (Sema::TemplateDeductionResult Result |
2531 | = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx], |
2532 | Info, Deduced)) |
2533 | return Result; |
2534 | |
2535 | PackScope.nextPackElement(); |
2536 | } |
2537 | |
2538 | |
2539 | |
2540 | if (auto Result = PackScope.finish()) |
2541 | return Result; |
2542 | } |
2543 | |
2544 | return Sema::TDK_Success; |
2545 | } |
2546 | |
2547 | static Sema::TemplateDeductionResult |
2548 | DeduceTemplateArguments(Sema &S, |
2549 | TemplateParameterList *TemplateParams, |
2550 | const TemplateArgumentList &ParamList, |
2551 | const TemplateArgumentList &ArgList, |
2552 | TemplateDeductionInfo &Info, |
2553 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
2554 | return DeduceTemplateArguments(S, TemplateParams, ParamList.asArray(), |
2555 | ArgList.asArray(), Info, Deduced, |
2556 | false); |
2557 | } |
2558 | |
2559 | |
2560 | static bool isSameTemplateArg(ASTContext &Context, |
2561 | TemplateArgument X, |
2562 | const TemplateArgument &Y, |
2563 | bool PackExpansionMatchesPack = false) { |
2564 | |
2565 | |
2566 | if (PackExpansionMatchesPack && X.isPackExpansion() && !Y.isPackExpansion()) |
2567 | X = X.getPackExpansionPattern(); |
2568 | |
2569 | if (X.getKind() != Y.getKind()) |
2570 | return false; |
2571 | |
2572 | switch (X.getKind()) { |
2573 | case TemplateArgument::Null: |
2574 | llvm_unreachable("Comparing NULL template argument"); |
2575 | |
2576 | case TemplateArgument::Type: |
2577 | return Context.getCanonicalType(X.getAsType()) == |
2578 | Context.getCanonicalType(Y.getAsType()); |
2579 | |
2580 | case TemplateArgument::Declaration: |
2581 | return isSameDeclaration(X.getAsDecl(), Y.getAsDecl()); |
2582 | |
2583 | case TemplateArgument::NullPtr: |
2584 | return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()); |
2585 | |
2586 | case TemplateArgument::Template: |
2587 | case TemplateArgument::TemplateExpansion: |
2588 | return Context.getCanonicalTemplateName( |
2589 | X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == |
2590 | Context.getCanonicalTemplateName( |
2591 | Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); |
2592 | |
2593 | case TemplateArgument::Integral: |
2594 | return hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()); |
2595 | |
2596 | case TemplateArgument::Expression: { |
2597 | llvm::FoldingSetNodeID XID, YID; |
2598 | X.getAsExpr()->Profile(XID, Context, true); |
2599 | Y.getAsExpr()->Profile(YID, Context, true); |
2600 | return XID == YID; |
2601 | } |
2602 | |
2603 | case TemplateArgument::Pack: |
2604 | if (X.pack_size() != Y.pack_size()) |
2605 | return false; |
2606 | |
2607 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
2608 | XPEnd = X.pack_end(), |
2609 | YP = Y.pack_begin(); |
2610 | XP != XPEnd; ++XP, ++YP) |
2611 | if (!isSameTemplateArg(Context, *XP, *YP, PackExpansionMatchesPack)) |
2612 | return false; |
2613 | |
2614 | return true; |
2615 | } |
2616 | |
2617 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
2618 | } |
2619 | |
2620 | |
2621 | |
2622 | |
2623 | |
2624 | |
2625 | |
2626 | |
2627 | |
2628 | |
2629 | |
2630 | |
2631 | |
2632 | |
2633 | TemplateArgumentLoc |
2634 | Sema::getTrivialTemplateArgumentLoc(const TemplateArgument &Arg, |
2635 | QualType NTTPType, SourceLocation Loc) { |
2636 | switch (Arg.getKind()) { |
2637 | case TemplateArgument::Null: |
2638 | llvm_unreachable("Can't get a NULL template argument here"); |
2639 | |
2640 | case TemplateArgument::Type: |
2641 | return TemplateArgumentLoc( |
2642 | Arg, Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); |
2643 | |
2644 | case TemplateArgument::Declaration: { |
2645 | if (NTTPType.isNull()) |
2646 | NTTPType = Arg.getParamTypeForDecl(); |
2647 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
2648 | .getAs<Expr>(); |
2649 | return TemplateArgumentLoc(TemplateArgument(E), E); |
2650 | } |
2651 | |
2652 | case TemplateArgument::NullPtr: { |
2653 | if (NTTPType.isNull()) |
2654 | NTTPType = Arg.getNullPtrType(); |
2655 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
2656 | .getAs<Expr>(); |
2657 | return TemplateArgumentLoc(TemplateArgument(NTTPType, true), |
2658 | E); |
2659 | } |
2660 | |
2661 | case TemplateArgument::Integral: { |
2662 | Expr *E = |
2663 | BuildExpressionFromIntegralTemplateArgument(Arg, Loc).getAs<Expr>(); |
2664 | return TemplateArgumentLoc(TemplateArgument(E), E); |
2665 | } |
2666 | |
2667 | case TemplateArgument::Template: |
2668 | case TemplateArgument::TemplateExpansion: { |
2669 | NestedNameSpecifierLocBuilder Builder; |
2670 | TemplateName Template = Arg.getAsTemplateOrTemplatePattern(); |
2671 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) |
2672 | Builder.MakeTrivial(Context, DTN->getQualifier(), Loc); |
2673 | else if (QualifiedTemplateName *QTN = |
2674 | Template.getAsQualifiedTemplateName()) |
2675 | Builder.MakeTrivial(Context, QTN->getQualifier(), Loc); |
2676 | |
2677 | if (Arg.getKind() == TemplateArgument::Template) |
2678 | return TemplateArgumentLoc(Context, Arg, |
2679 | Builder.getWithLocInContext(Context), Loc); |
2680 | |
2681 | return TemplateArgumentLoc( |
2682 | Context, Arg, Builder.getWithLocInContext(Context), Loc, Loc); |
2683 | } |
2684 | |
2685 | case TemplateArgument::Expression: |
2686 | return TemplateArgumentLoc(Arg, Arg.getAsExpr()); |
2687 | |
2688 | case TemplateArgument::Pack: |
2689 | return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); |
2690 | } |
2691 | |
2692 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
2693 | } |
2694 | |
2695 | TemplateArgumentLoc |
2696 | Sema::getIdentityTemplateArgumentLoc(NamedDecl *TemplateParm, |
2697 | SourceLocation Location) { |
2698 | return getTrivialTemplateArgumentLoc( |
2699 | Context.getInjectedTemplateArg(TemplateParm), QualType(), Location); |
2700 | } |
2701 | |
2702 | |
2703 | |
2704 | static bool |
2705 | ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param, |
2706 | DeducedTemplateArgument Arg, |
2707 | NamedDecl *Template, |
2708 | TemplateDeductionInfo &Info, |
2709 | bool IsDeduced, |
2710 | SmallVectorImpl<TemplateArgument> &Output) { |
2711 | auto ConvertArg = [&](DeducedTemplateArgument Arg, |
2712 | unsigned ArgumentPackIndex) { |
2713 | |
2714 | |
2715 | |
2716 | TemplateArgumentLoc ArgLoc = |
2717 | S.getTrivialTemplateArgumentLoc(Arg, QualType(), Info.getLocation()); |
2718 | |
2719 | |
2720 | return S.CheckTemplateArgument( |
2721 | Param, ArgLoc, Template, Template->getLocation(), |
2722 | Template->getSourceRange().getEnd(), ArgumentPackIndex, Output, |
2723 | IsDeduced |
2724 | ? (Arg.wasDeducedFromArrayBound() ? Sema::CTAK_DeducedFromArrayBound |
2725 | : Sema::CTAK_Deduced) |
2726 | : Sema::CTAK_Specified); |
2727 | }; |
2728 | |
2729 | if (Arg.getKind() == TemplateArgument::Pack) { |
2730 | |
2731 | |
2732 | SmallVector<TemplateArgument, 2> PackedArgsBuilder; |
2733 | for (const auto &P : Arg.pack_elements()) { |
2734 | |
2735 | |
2736 | |
2737 | DeducedTemplateArgument InnerArg(P); |
2738 | InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); |
2739 | assert(InnerArg.getKind() != TemplateArgument::Pack && |
2740 | "deduced nested pack"); |
2741 | if (P.isNull()) { |
2742 | |
2743 | |
2744 | |
2745 | |
2746 | S.Diag(Param->getLocation(), |
2747 | diag::err_template_arg_deduced_incomplete_pack) |
2748 | << Arg << Param; |
2749 | return true; |
2750 | } |
2751 | if (ConvertArg(InnerArg, PackedArgsBuilder.size())) |
2752 | return true; |
2753 | |
2754 | |
2755 | PackedArgsBuilder.push_back(Output.pop_back_val()); |
2756 | } |
2757 | |
2758 | |
2759 | |
2760 | if (PackedArgsBuilder.empty()) { |
2761 | LocalInstantiationScope Scope(S); |
2762 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Output); |
2763 | MultiLevelTemplateArgumentList Args(TemplateArgs); |
2764 | |
2765 | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
2766 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
2767 | NTTP, Output, |
2768 | Template->getSourceRange()); |
2769 | if (Inst.isInvalid() || |
2770 | S.SubstType(NTTP->getType(), Args, NTTP->getLocation(), |
2771 | NTTP->getDeclName()).isNull()) |
2772 | return true; |
2773 | } else if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
2774 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
2775 | TTP, Output, |
2776 | Template->getSourceRange()); |
2777 | if (Inst.isInvalid() || !S.SubstDecl(TTP, S.CurContext, Args)) |
2778 | return true; |
2779 | } |
2780 | |
2781 | } |
2782 | |
2783 | |
2784 | Output.push_back( |
2785 | TemplateArgument::CreatePackCopy(S.Context, PackedArgsBuilder)); |
2786 | return false; |
2787 | } |
2788 | |
2789 | return ConvertArg(Arg, 0); |
2790 | } |
2791 | |
2792 | |
2793 | |
2794 | |
2795 | template<typename TemplateDeclT> |
2796 | static Sema::TemplateDeductionResult ConvertDeducedTemplateArguments( |
2797 | Sema &S, TemplateDeclT *Template, bool IsDeduced, |
2798 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
2799 | TemplateDeductionInfo &Info, SmallVectorImpl<TemplateArgument> &Builder, |
2800 | LocalInstantiationScope *CurrentInstantiationScope = nullptr, |
2801 | unsigned NumAlreadyConverted = 0, bool PartialOverloading = false) { |
2802 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
2803 | |
2804 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
2805 | NamedDecl *Param = TemplateParams->getParam(I); |
2806 | |
2807 | |
2808 | |
2809 | |
2810 | |
2811 | if (Deduced[I].isNull() && Param->isTemplateParameterPack()) { |
2812 | if (auto Result = |
2813 | PackDeductionScope(S, TemplateParams, Deduced, Info, I).finish()) |
2814 | return Result; |
2815 | } |
2816 | |
2817 | if (!Deduced[I].isNull()) { |
2818 | if (I < NumAlreadyConverted) { |
2819 | |
2820 | |
2821 | |
2822 | if (Param->isParameterPack() && CurrentInstantiationScope && |
2823 | CurrentInstantiationScope->getPartiallySubstitutedPack() == Param) { |
2824 | |
2825 | |
2826 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
2827 | |
2828 | |
2829 | } else { |
2830 | |
2831 | |
2832 | |
2833 | Builder.push_back(Deduced[I]); |
2834 | continue; |
2835 | } |
2836 | } |
2837 | |
2838 | |
2839 | |
2840 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Template, Info, |
2841 | IsDeduced, Builder)) { |
2842 | Info.Param = makeTemplateParameter(Param); |
2843 | |
2844 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
2845 | return Sema::TDK_SubstitutionFailure; |
2846 | } |
2847 | |
2848 | continue; |
2849 | } |
2850 | |
2851 | |
2852 | bool HasDefaultArg = false; |
2853 | TemplateDecl *TD = dyn_cast<TemplateDecl>(Template); |
2854 | if (!TD) { |
2855 | assert(isa<ClassTemplatePartialSpecializationDecl>(Template) || |
2856 | isa<VarTemplatePartialSpecializationDecl>(Template)); |
2857 | return Sema::TDK_Incomplete; |
2858 | } |
2859 | |
2860 | TemplateArgumentLoc DefArg; |
2861 | { |
2862 | Qualifiers ThisTypeQuals; |
2863 | CXXRecordDecl *ThisContext = nullptr; |
2864 | if (auto *Rec = dyn_cast<CXXRecordDecl>(TD->getDeclContext())) |
2865 | if (Rec->isLambda()) |
2866 | if (auto *Method = dyn_cast<CXXMethodDecl>(Rec->getDeclContext())) { |
2867 | ThisContext = Method->getParent(); |
2868 | ThisTypeQuals = Method->getMethodQualifiers(); |
2869 | } |
2870 | |
2871 | Sema::CXXThisScopeRAII ThisScope(S, ThisContext, ThisTypeQuals, |
2872 | S.getLangOpts().CPlusPlus17); |
2873 | |
2874 | DefArg = S.SubstDefaultTemplateArgumentIfAvailable( |
2875 | TD, TD->getLocation(), TD->getSourceRange().getEnd(), Param, Builder, |
2876 | HasDefaultArg); |
2877 | } |
2878 | |
2879 | |
2880 | if (DefArg.getArgument().isNull()) { |
2881 | Info.Param = makeTemplateParameter( |
2882 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
2883 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
2884 | if (PartialOverloading) break; |
2885 | |
2886 | return HasDefaultArg ? Sema::TDK_SubstitutionFailure |
2887 | : Sema::TDK_Incomplete; |
2888 | } |
2889 | |
2890 | |
2891 | if (S.CheckTemplateArgument(Param, DefArg, TD, TD->getLocation(), |
2892 | TD->getSourceRange().getEnd(), 0, Builder, |
2893 | Sema::CTAK_Specified)) { |
2894 | Info.Param = makeTemplateParameter( |
2895 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
2896 | |
2897 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
2898 | return Sema::TDK_SubstitutionFailure; |
2899 | } |
2900 | |
2901 | |
2902 | } |
2903 | |
2904 | return Sema::TDK_Success; |
2905 | } |
2906 | |
2907 | static DeclContext *getAsDeclContextOrEnclosing(Decl *D) { |
2908 | if (auto *DC = dyn_cast<DeclContext>(D)) |
2909 | return DC; |
2910 | return D->getDeclContext(); |
2911 | } |
2912 | |
2913 | template<typename T> struct IsPartialSpecialization { |
2914 | static constexpr bool value = false; |
2915 | }; |
2916 | template<> |
2917 | struct IsPartialSpecialization<ClassTemplatePartialSpecializationDecl> { |
2918 | static constexpr bool value = true; |
2919 | }; |
2920 | template<> |
2921 | struct IsPartialSpecialization<VarTemplatePartialSpecializationDecl> { |
2922 | static constexpr bool value = true; |
2923 | }; |
2924 | |
2925 | template<typename TemplateDeclT> |
2926 | static Sema::TemplateDeductionResult |
2927 | CheckDeducedArgumentConstraints(Sema& S, TemplateDeclT *Template, |
2928 | ArrayRef<TemplateArgument> DeducedArgs, |
2929 | TemplateDeductionInfo& Info) { |
2930 | llvm::SmallVector<const Expr *, 3> AssociatedConstraints; |
2931 | Template->getAssociatedConstraints(AssociatedConstraints); |
2932 | if (S.CheckConstraintSatisfaction(Template, AssociatedConstraints, |
2933 | DeducedArgs, Info.getLocation(), |
2934 | Info.AssociatedConstraintsSatisfaction) || |
2935 | !Info.AssociatedConstraintsSatisfaction.IsSatisfied) { |
2936 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, DeducedArgs)); |
2937 | return Sema::TDK_ConstraintsNotSatisfied; |
2938 | } |
2939 | return Sema::TDK_Success; |
2940 | } |
2941 | |
2942 | |
2943 | template <typename T> |
2944 | static std::enable_if_t<IsPartialSpecialization<T>::value, |
2945 | Sema::TemplateDeductionResult> |
2946 | FinishTemplateArgumentDeduction( |
2947 | Sema &S, T *Partial, bool IsPartialOrdering, |
2948 | const TemplateArgumentList &TemplateArgs, |
2949 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
2950 | TemplateDeductionInfo &Info) { |
2951 | |
2952 | EnterExpressionEvaluationContext Unevaluated( |
2953 | S, Sema::ExpressionEvaluationContext::Unevaluated); |
2954 | Sema::SFINAETrap Trap(S); |
2955 | |
2956 | Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Partial)); |
2957 | |
2958 | |
2959 | |
2960 | |
2961 | SmallVector<TemplateArgument, 4> Builder; |
2962 | if (auto Result = ConvertDeducedTemplateArguments( |
2963 | S, Partial, IsPartialOrdering, Deduced, Info, Builder)) |
2964 | return Result; |
2965 | |
2966 | |
2967 | TemplateArgumentList *DeducedArgumentList |
2968 | = TemplateArgumentList::CreateCopy(S.Context, Builder); |
2969 | |
2970 | Info.reset(DeducedArgumentList); |
2971 | |
2972 | |
2973 | |
2974 | |
2975 | |
2976 | |
2977 | LocalInstantiationScope InstScope(S); |
2978 | auto *Template = Partial->getSpecializedTemplate(); |
2979 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo = |
2980 | Partial->getTemplateArgsAsWritten(); |
2981 | |
2982 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
2983 | PartialTemplArgInfo->RAngleLoc); |
2984 | |
2985 | if (S.SubstTemplateArguments( |
2986 | PartialTemplArgInfo->arguments(), |
2987 | MultiLevelTemplateArgumentList(*DeducedArgumentList), InstArgs)) { |
2988 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
2989 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
2990 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
2991 | |
2992 | Decl *Param = const_cast<NamedDecl *>( |
2993 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
2994 | Info.Param = makeTemplateParameter(Param); |
2995 | Info.FirstArg = (*PartialTemplArgInfo)[ArgIdx].getArgument(); |
2996 | return Sema::TDK_SubstitutionFailure; |
2997 | } |
2998 | |
2999 | bool ConstraintsNotSatisfied; |
3000 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
3001 | if (S.CheckTemplateArgumentList(Template, Partial->getLocation(), InstArgs, |
3002 | false, ConvertedInstArgs, |
3003 | true, |
3004 | &ConstraintsNotSatisfied)) |
3005 | return ConstraintsNotSatisfied ? Sema::TDK_ConstraintsNotSatisfied : |
3006 | Sema::TDK_SubstitutionFailure; |
3007 | |
3008 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
3009 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
3010 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
3011 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
3012 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
3013 | Info.FirstArg = TemplateArgs[I]; |
3014 | Info.SecondArg = InstArg; |
3015 | return Sema::TDK_NonDeducedMismatch; |
3016 | } |
3017 | } |
3018 | |
3019 | if (Trap.hasErrorOccurred()) |
3020 | return Sema::TDK_SubstitutionFailure; |
3021 | |
3022 | if (auto Result = CheckDeducedArgumentConstraints(S, Partial, Builder, Info)) |
3023 | return Result; |
3024 | |
3025 | return Sema::TDK_Success; |
3026 | } |
3027 | |
3028 | |
3029 | |
3030 | |
3031 | static Sema::TemplateDeductionResult FinishTemplateArgumentDeduction( |
3032 | Sema &S, TemplateDecl *Template, bool PartialOrdering, |
3033 | const TemplateArgumentList &TemplateArgs, |
3034 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
3035 | TemplateDeductionInfo &Info) { |
3036 | |
3037 | EnterExpressionEvaluationContext Unevaluated( |
3038 | S, Sema::ExpressionEvaluationContext::Unevaluated); |
3039 | Sema::SFINAETrap Trap(S); |
3040 | |
3041 | Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Template)); |
3042 | |
3043 | |
3044 | |
3045 | |
3046 | SmallVector<TemplateArgument, 4> Builder; |
3047 | if (auto Result = ConvertDeducedTemplateArguments( |
3048 | S, Template, PartialOrdering, Deduced, Info, Builder)) |
3049 | return Result; |
3050 | |
3051 | |
3052 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
3053 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
3054 | TemplateArgument InstArg = Builder[I]; |
3055 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg, |
3056 | true)) { |
3057 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
3058 | Info.FirstArg = TemplateArgs[I]; |
3059 | Info.SecondArg = InstArg; |
3060 | return Sema::TDK_NonDeducedMismatch; |
3061 | } |
3062 | } |
3063 | |
3064 | if (Trap.hasErrorOccurred()) |
3065 | return Sema::TDK_SubstitutionFailure; |
3066 | |
3067 | if (auto Result = CheckDeducedArgumentConstraints(S, Template, Builder, |
3068 | Info)) |
3069 | return Result; |
3070 | |
3071 | return Sema::TDK_Success; |
3072 | } |
3073 | |
3074 | |
3075 | |
3076 | |
3077 | Sema::TemplateDeductionResult |
3078 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
3079 | const TemplateArgumentList &TemplateArgs, |
3080 | TemplateDeductionInfo &Info) { |
3081 | if (Partial->isInvalidDecl()) |
3082 | return TDK_Invalid; |
3083 | |
3084 | |
3085 | |
3086 | |
3087 | |
3088 | |
3089 | |
3090 | |
3091 | EnterExpressionEvaluationContext Unevaluated( |
3092 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
3093 | SFINAETrap Trap(*this); |
3094 | |
3095 | |
3096 | |
3097 | LocalInstantiationScope InstantiationScope(*this); |
3098 | |
3099 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
3100 | Deduced.resize(Partial->getTemplateParameters()->size()); |
3101 | if (TemplateDeductionResult Result |
3102 | = ::DeduceTemplateArguments(*this, |
3103 | Partial->getTemplateParameters(), |
3104 | Partial->getTemplateArgs(), |
3105 | TemplateArgs, Info, Deduced)) |
3106 | return Result; |
3107 | |
3108 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
3109 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
3110 | Info); |
3111 | if (Inst.isInvalid()) |
3112 | return TDK_InstantiationDepth; |
3113 | |
3114 | if (Trap.hasErrorOccurred()) |
3115 | return Sema::TDK_SubstitutionFailure; |
3116 | |
3117 | TemplateDeductionResult Result; |
3118 | runWithSufficientStackSpace(Info.getLocation(), [&] { |
3119 | Result = ::FinishTemplateArgumentDeduction(*this, Partial, |
3120 | false, |
3121 | TemplateArgs, Deduced, Info); |
3122 | }); |
3123 | return Result; |
3124 | } |
3125 | |
3126 | |
3127 | |
3128 | |
3129 | Sema::TemplateDeductionResult |
3130 | Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial, |
3131 | const TemplateArgumentList &TemplateArgs, |
3132 | TemplateDeductionInfo &Info) { |
3133 | if (Partial->isInvalidDecl()) |
3134 | return TDK_Invalid; |
3135 | |
3136 | |
3137 | |
3138 | |
3139 | |
3140 | |
3141 | |
3142 | |
3143 | EnterExpressionEvaluationContext Unevaluated( |
3144 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
3145 | SFINAETrap Trap(*this); |
3146 | |
3147 | |
3148 | |
3149 | LocalInstantiationScope InstantiationScope(*this); |
3150 | |
3151 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
3152 | Deduced.resize(Partial->getTemplateParameters()->size()); |
3153 | if (TemplateDeductionResult Result = ::DeduceTemplateArguments( |
3154 | *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(), |
3155 | TemplateArgs, Info, Deduced)) |
3156 | return Result; |
3157 | |
3158 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
3159 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
3160 | Info); |
3161 | if (Inst.isInvalid()) |
3162 | return TDK_InstantiationDepth; |
3163 | |
3164 | if (Trap.hasErrorOccurred()) |
3165 | return Sema::TDK_SubstitutionFailure; |
3166 | |
3167 | TemplateDeductionResult Result; |
3168 | runWithSufficientStackSpace(Info.getLocation(), [&] { |
3169 | Result = ::FinishTemplateArgumentDeduction(*this, Partial, |
3170 | false, |
3171 | TemplateArgs, Deduced, Info); |
3172 | }); |
3173 | return Result; |
3174 | } |
3175 | |
3176 | |
3177 | static bool isSimpleTemplateIdType(QualType T) { |
3178 | if (const TemplateSpecializationType *Spec |
3179 | = T->getAs<TemplateSpecializationType>()) |
3180 | return Spec->getTemplateName().getAsTemplateDecl() != nullptr; |
3181 | |
3182 | |
3183 | |
3184 | |
3185 | |
3186 | |
3187 | |
3188 | |
3189 | |
3190 | if (T->getAs<InjectedClassNameType>()) |
3191 | return true; |
3192 | |
3193 | return false; |
3194 | } |
3195 | |
3196 | |
3197 | |
3198 | |
3199 | |
3200 | |
3201 | |
3202 | |
3203 | |
3204 | |
3205 | |
3206 | |
3207 | |
3208 | |
3209 | |
3210 | |
3211 | |
3212 | |
3213 | |
3214 | |
3215 | |
3216 | |
3217 | |
3218 | |
3219 | |
3220 | Sema::TemplateDeductionResult |
3221 | Sema::SubstituteExplicitTemplateArguments( |
3222 | FunctionTemplateDecl *FunctionTemplate, |
3223 | TemplateArgumentListInfo &ExplicitTemplateArgs, |
3224 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
3225 | SmallVectorImpl<QualType> &ParamTypes, |
3226 | QualType *FunctionType, |
3227 | TemplateDeductionInfo &Info) { |
3228 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
3229 | TemplateParameterList *TemplateParams |
3230 | = FunctionTemplate->getTemplateParameters(); |
3231 | |
3232 | if (ExplicitTemplateArgs.size() == 0) { |
3233 | |
3234 | |
3235 | for (auto P : Function->parameters()) |
3236 | ParamTypes.push_back(P->getType()); |
3237 | |
3238 | if (FunctionType) |
3239 | *FunctionType = Function->getType(); |
3240 | return TDK_Success; |
3241 | } |
3242 | |
3243 | |
3244 | EnterExpressionEvaluationContext Unevaluated( |
3245 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
3246 | SFINAETrap Trap(*this); |
3247 | |
3248 | |
3249 | |
3250 | |
3251 | |
3252 | |
3253 | SmallVector<TemplateArgument, 4> Builder; |
3254 | |
3255 | |
3256 | |
3257 | |
3258 | SmallVector<TemplateArgument, 4> DeducedArgs; |
3259 | InstantiatingTemplate Inst( |
3260 | *this, Info.getLocation(), FunctionTemplate, DeducedArgs, |
3261 | CodeSynthesisContext::ExplicitTemplateArgumentSubstitution, Info); |
3262 | if (Inst.isInvalid()) |
3263 | return TDK_InstantiationDepth; |
3264 | |
3265 | if (CheckTemplateArgumentList(FunctionTemplate, SourceLocation(), |
3266 | ExplicitTemplateArgs, true, Builder, false) || |
3267 | Trap.hasErrorOccurred()) { |
3268 | unsigned Index = Builder.size(); |
3269 | if (Index >= TemplateParams->size()) |
3270 | return TDK_SubstitutionFailure; |
3271 | Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); |
3272 | return TDK_InvalidExplicitArguments; |
3273 | } |
3274 | |
3275 | |
3276 | |
3277 | TemplateArgumentList *ExplicitArgumentList |
3278 | = TemplateArgumentList::CreateCopy(Context, Builder); |
3279 | Info.setExplicitArgs(ExplicitArgumentList); |
3280 | |
3281 | |
3282 | |
3283 | |
3284 | |
3285 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
3286 | |
3287 | |
3288 | |
3289 | |
3290 | |
3291 | unsigned PartiallySubstitutedPackIndex = -1u; |
3292 | if (!Builder.empty()) { |
3293 | const TemplateArgument &Arg = Builder.back(); |
3294 | if (Arg.getKind() == TemplateArgument::Pack) { |
3295 | auto *Param = TemplateParams->getParam(Builder.size() - 1); |
3296 | |
3297 | |
3298 | Optional<unsigned> Expansions = getExpandedPackSize(Param); |
3299 | if (!Expansions || Arg.pack_size() < *Expansions) { |
3300 | PartiallySubstitutedPackIndex = Builder.size() - 1; |
3301 | CurrentInstantiationScope->SetPartiallySubstitutedPack( |
3302 | Param, Arg.pack_begin(), Arg.pack_size()); |
3303 | } |
3304 | } |
3305 | } |
3306 | |
3307 | const FunctionProtoType *Proto |
3308 | = Function->getType()->getAs<FunctionProtoType>(); |
3309 | assert(Proto && "Function template does not have a prototype?"); |
3310 | |
3311 | |
3312 | LocalInstantiationScope InstScope(*this, true); |
3313 | |
3314 | ExtParameterInfoBuilder ExtParamInfos; |
3315 | |
3316 | |
3317 | |
3318 | |
3319 | |
3320 | if (Proto->hasTrailingReturn()) { |
3321 | if (SubstParmTypes(Function->getLocation(), Function->parameters(), |
3322 | Proto->getExtParameterInfosOrNull(), |
3323 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
3324 | ParamTypes, nullptr, ExtParamInfos)) |
3325 | return TDK_SubstitutionFailure; |
3326 | } |
3327 | |
3328 | |
3329 | QualType ResultType; |
3330 | { |
3331 | |
3332 | |
3333 | |
3334 | |
3335 | |
3336 | |
3337 | Qualifiers ThisTypeQuals; |
3338 | CXXRecordDecl *ThisContext = nullptr; |
3339 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
3340 | ThisContext = Method->getParent(); |
3341 | ThisTypeQuals = Method->getMethodQualifiers(); |
3342 | } |
3343 | |
3344 | CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals, |
3345 | getLangOpts().CPlusPlus11); |
3346 | |
3347 | ResultType = |
3348 | SubstType(Proto->getReturnType(), |
3349 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
3350 | Function->getTypeSpecStartLoc(), Function->getDeclName()); |
3351 | if (ResultType.isNull() || Trap.hasErrorOccurred()) |
3352 | return TDK_SubstitutionFailure; |
3353 | |
3354 | if (getLangOpts().CUDA) |
3355 | if (Function->hasAttr<CUDAGlobalAttr>() && !ResultType->isVoidType()) { |
3356 | Diag(Function->getLocation(), diag::err_kern_type_not_void_return) |
3357 | << Function->getType() << Function->getSourceRange(); |
3358 | return TDK_SubstitutionFailure; |
3359 | } |
3360 | } |
3361 | |
3362 | |
3363 | |
3364 | if (!Proto->hasTrailingReturn() && |
3365 | SubstParmTypes(Function->getLocation(), Function->parameters(), |
3366 | Proto->getExtParameterInfosOrNull(), |
3367 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
3368 | ParamTypes, nullptr, ExtParamInfos)) |
3369 | return TDK_SubstitutionFailure; |
3370 | |
3371 | if (FunctionType) { |
3372 | auto EPI = Proto->getExtProtoInfo(); |
3373 | EPI.ExtParameterInfos = ExtParamInfos.getPointerOrNull(ParamTypes.size()); |
3374 | |
3375 | |
3376 | |
3377 | |
3378 | SmallVector<QualType, 4> ExceptionStorage; |
3379 | if (getLangOpts().CPlusPlus17 && |
3380 | SubstExceptionSpec( |
3381 | Function->getLocation(), EPI.ExceptionSpec, ExceptionStorage, |
3382 | MultiLevelTemplateArgumentList(*ExplicitArgumentList))) |
3383 | return TDK_SubstitutionFailure; |
3384 | |
3385 | *FunctionType = BuildFunctionType(ResultType, ParamTypes, |
3386 | Function->getLocation(), |
3387 | Function->getDeclName(), |
3388 | EPI); |
3389 | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
3390 | return TDK_SubstitutionFailure; |
3391 | } |
3392 | |
3393 | |
3394 | |
3395 | |
3396 | |
3397 | |
3398 | |
3399 | |
3400 | |
3401 | |
3402 | |
3403 | Deduced.reserve(TemplateParams->size()); |
3404 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) { |
3405 | const TemplateArgument &Arg = ExplicitArgumentList->get(I); |
3406 | if (I == PartiallySubstitutedPackIndex) |
3407 | Deduced.push_back(DeducedTemplateArgument()); |
3408 | else |
3409 | Deduced.push_back(Arg); |
3410 | } |
3411 | |
3412 | return TDK_Success; |
3413 | } |
3414 | |
3415 | |
3416 | |
3417 | static Sema::TemplateDeductionResult |
3418 | CheckOriginalCallArgDeduction(Sema &S, TemplateDeductionInfo &Info, |
3419 | Sema::OriginalCallArg OriginalArg, |
3420 | QualType DeducedA) { |
3421 | ASTContext &Context = S.Context; |
3422 | |
3423 | auto Failed = [&]() -> Sema::TemplateDeductionResult { |
3424 | Info.FirstArg = TemplateArgument(DeducedA); |
3425 | Info.SecondArg = TemplateArgument(OriginalArg.OriginalArgType); |
3426 | Info.CallArgIndex = OriginalArg.ArgIdx; |
3427 | return OriginalArg.DecomposedParam ? Sema::TDK_DeducedMismatchNested |
3428 | : Sema::TDK_DeducedMismatch; |
3429 | }; |
3430 | |
3431 | QualType A = OriginalArg.OriginalArgType; |
3432 | QualType OriginalParamType = OriginalArg.OriginalParamType; |
3433 | |
3434 | |
3435 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
3436 | return Sema::TDK_Success; |
3437 | |
3438 | |
3439 | |
3440 | if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>()) |
3441 | DeducedA = DeducedARef->getPointeeType(); |
3442 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
3443 | A = ARef->getPointeeType(); |
3444 | |
3445 | |
3446 | |
3447 | |
3448 | |
3449 | |
3450 | if (const ReferenceType *OriginalParamRef |
3451 | = OriginalParamType->getAs<ReferenceType>()) { |
3452 | |
3453 | OriginalParamType = OriginalParamRef->getPointeeType(); |
3454 | |
3455 | |
3456 | |
3457 | |
3458 | QualType Tmp; |
3459 | if (A->isFunctionType() && S.IsFunctionConversion(A, DeducedA, Tmp)) |
3460 | return Sema::TDK_Success; |
3461 | |
3462 | Qualifiers AQuals = A.getQualifiers(); |
3463 | Qualifiers DeducedAQuals = DeducedA.getQualifiers(); |
3464 | |
3465 | |
3466 | |
3467 | |
3468 | |
3469 | if (S.getLangOpts().ObjCAutoRefCount && |
3470 | ((DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong && |
3471 | AQuals.getObjCLifetime() == Qualifiers::OCL_None) || |
3472 | (DeducedAQuals.hasConst() && |
3473 | DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone))) { |
3474 | AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime()); |
3475 | } |
3476 | |
3477 | if (AQuals == DeducedAQuals) { |
3478 | |
3479 | } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) { |
3480 | return Failed(); |
3481 | } else { |
3482 | |
3483 | |
3484 | |
3485 | A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals); |
3486 | } |
3487 | } |
3488 | |
3489 | |
3490 | |
3491 | |
3492 | |
3493 | |
3494 | |
3495 | bool ObjCLifetimeConversion = false; |
3496 | QualType ResultTy; |
3497 | if ((A->isAnyPointerType() || A->isMemberPointerType()) && |
3498 | (S.IsQualificationConversion(A, DeducedA, false, |
3499 | ObjCLifetimeConversion) || |
3500 | S.IsFunctionConversion(A, DeducedA, ResultTy))) |
3501 | return Sema::TDK_Success; |
3502 | |
3503 | |
3504 | |
3505 | |
3506 | |
3507 | |
3508 | if (const PointerType *OriginalParamPtr |
3509 | = OriginalParamType->getAs<PointerType>()) { |
3510 | if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) { |
3511 | if (const PointerType *APtr = A->getAs<PointerType>()) { |
3512 | if (A->getPointeeType()->isRecordType()) { |
3513 | OriginalParamType = OriginalParamPtr->getPointeeType(); |
3514 | DeducedA = DeducedAPtr->getPointeeType(); |
3515 | A = APtr->getPointeeType(); |
3516 | } |
3517 | } |
3518 | } |
3519 | } |
3520 | |
3521 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
3522 | return Sema::TDK_Success; |
3523 | |
3524 | if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) && |
3525 | S.IsDerivedFrom(Info.getLocation(), A, DeducedA)) |
3526 | return Sema::TDK_Success; |
3527 | |
3528 | return Failed(); |
3529 | } |
3530 | |
3531 | |
3532 | |
3533 | |
3534 | |
3535 | |
3536 | |
3537 | |
3538 | |
3539 | static unsigned getPackIndexForParam(Sema &S, |
3540 | FunctionTemplateDecl *FunctionTemplate, |
3541 | const MultiLevelTemplateArgumentList &Args, |
3542 | unsigned ParamIdx) { |
3543 | unsigned Idx = 0; |
3544 | for (auto *PD : FunctionTemplate->getTemplatedDecl()->parameters()) { |
3545 | if (PD->isParameterPack()) { |
3546 | unsigned NumExpansions = |
3547 | S.getNumArgumentsInExpansion(PD->getType(), Args).getValueOr(1); |
3548 | if (Idx + NumExpansions > ParamIdx) |
3549 | return ParamIdx - Idx; |
3550 | Idx += NumExpansions; |
3551 | } else { |
3552 | if (Idx == ParamIdx) |
3553 | return -1; |
3554 | ++Idx; |
3555 | } |
3556 | } |
3557 | |
3558 | llvm_unreachable("parameter index would not be produced from template"); |
3559 | } |
3560 | |
3561 | |
3562 | |
3563 | |
3564 | |
3565 | |
3566 | |
3567 | Sema::TemplateDeductionResult Sema::FinishTemplateArgumentDeduction( |
3568 | FunctionTemplateDecl *FunctionTemplate, |
3569 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
3570 | unsigned NumExplicitlySpecified, FunctionDecl *&Specialization, |
3571 | TemplateDeductionInfo &Info, |
3572 | SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs, |
3573 | bool PartialOverloading, llvm::function_ref<bool()> CheckNonDependent) { |
3574 | |
3575 | EnterExpressionEvaluationContext Unevaluated( |
3576 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
3577 | SFINAETrap Trap(*this); |
3578 | |
3579 | |
3580 | |
3581 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
3582 | InstantiatingTemplate Inst( |
3583 | *this, Info.getLocation(), FunctionTemplate, DeducedArgs, |
3584 | CodeSynthesisContext::DeducedTemplateArgumentSubstitution, Info); |
3585 | if (Inst.isInvalid()) |
3586 | return TDK_InstantiationDepth; |
3587 | |
3588 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
3589 | |
3590 | |
3591 | |
3592 | |
3593 | SmallVector<TemplateArgument, 4> Builder; |
3594 | if (auto Result = ConvertDeducedTemplateArguments( |
3595 | *this, FunctionTemplate, true, Deduced, Info, Builder, |
3596 | CurrentInstantiationScope, NumExplicitlySpecified, |
3597 | PartialOverloading)) |
3598 | return Result; |
3599 | |
3600 | |
3601 | |
3602 | |
3603 | |
3604 | |
3605 | |
3606 | |
3607 | |
3608 | |
3609 | if (CheckNonDependent()) |
3610 | return TDK_NonDependentConversionFailure; |
3611 | |
3612 | |
3613 | TemplateArgumentList *DeducedArgumentList |
3614 | = TemplateArgumentList::CreateCopy(Context, Builder); |
3615 | Info.reset(DeducedArgumentList); |
3616 | |
3617 | |
3618 | |
3619 | DeclContext *Owner = FunctionTemplate->getDeclContext(); |
3620 | if (FunctionTemplate->getFriendObjectKind()) |
3621 | Owner = FunctionTemplate->getLexicalDeclContext(); |
3622 | MultiLevelTemplateArgumentList SubstArgs(*DeducedArgumentList); |
3623 | Specialization = cast_or_null<FunctionDecl>( |
3624 | SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, SubstArgs)); |
3625 | if (!Specialization || Specialization->isInvalidDecl()) |
3626 | return TDK_SubstitutionFailure; |
3627 | |
3628 | assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == |
3629 | FunctionTemplate->getCanonicalDecl()); |
3630 | |
3631 | |
3632 | |
3633 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList && |
3634 | !Trap.hasErrorOccurred()) |
3635 | Info.take(); |
3636 | |
3637 | |
3638 | |
3639 | |
3640 | if (Trap.hasErrorOccurred()) { |
3641 | Specialization->setInvalidDecl(true); |
3642 | return TDK_SubstitutionFailure; |
3643 | } |
3644 | |
3645 | |
3646 | |
3647 | |
3648 | |
3649 | |
3650 | |
3651 | |
3652 | |
3653 | if (!PartialOverloading || |
3654 | (Builder.size() == FunctionTemplate->getTemplateParameters()->size())) { |
3655 | if (CheckInstantiatedFunctionTemplateConstraints(Info.getLocation(), |
3656 | Specialization, Builder, Info.AssociatedConstraintsSatisfaction)) |
3657 | return TDK_MiscellaneousDeductionFailure; |
3658 | |
3659 | if (!Info.AssociatedConstraintsSatisfaction.IsSatisfied) { |
3660 | Info.reset(TemplateArgumentList::CreateCopy(Context, Builder)); |
3661 | return TDK_ConstraintsNotSatisfied; |
3662 | } |
3663 | } |
3664 | |
3665 | if (OriginalCallArgs) { |
3666 | |
3667 | |
3668 | |
3669 | |
3670 | llvm::SmallDenseMap<std::pair<unsigned, QualType>, QualType> DeducedATypes; |
3671 | for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) { |
3672 | OriginalCallArg OriginalArg = (*OriginalCallArgs)[I]; |
3673 | |
3674 | auto ParamIdx = OriginalArg.ArgIdx; |
3675 | if (ParamIdx >= Specialization->getNumParams()) |
3676 | |
3677 | |
3678 | |
3679 | continue; |
3680 | |
3681 | QualType DeducedA; |
3682 | if (!OriginalArg.DecomposedParam) { |
3683 | |
3684 | |
3685 | DeducedA = Specialization->getParamDecl(ParamIdx)->getType(); |
3686 | } else { |
3687 | |
3688 | |
3689 | |
3690 | QualType &CacheEntry = |
3691 | DeducedATypes[{ParamIdx, OriginalArg.OriginalParamType}]; |
3692 | if (CacheEntry.isNull()) { |
3693 | ArgumentPackSubstitutionIndexRAII PackIndex( |
3694 | *this, getPackIndexForParam(*this, FunctionTemplate, SubstArgs, |
3695 | ParamIdx)); |
3696 | CacheEntry = |
3697 | SubstType(OriginalArg.OriginalParamType, SubstArgs, |
3698 | Specialization->getTypeSpecStartLoc(), |
3699 | Specialization->getDeclName()); |
3700 | } |
3701 | DeducedA = CacheEntry; |
3702 | } |
3703 | |
3704 | if (auto TDK = |
3705 | CheckOriginalCallArgDeduction(*this, Info, OriginalArg, DeducedA)) |
3706 | return TDK; |
3707 | } |
3708 | } |
3709 | |
3710 | |
3711 | |
3712 | |
3713 | |
3714 | if (Info.diag_begin() != Info.diag_end()) { |
3715 | SuppressedDiagnosticsMap::iterator |
3716 | Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); |
3717 | if (Pos == SuppressedDiagnostics.end()) |
3718 | SuppressedDiagnostics[Specialization->getCanonicalDecl()] |
3719 | .append(Info.diag_begin(), Info.diag_end()); |
3720 | } |
3721 | |
3722 | return TDK_Success; |
3723 | } |
3724 | |
3725 | |
3726 | |
3727 | static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R, |
3728 | FunctionDecl *Fn) { |
3729 | |
3730 | if (S.getLangOpts().CPlusPlus14 && Fn->getReturnType()->isUndeducedType() && |
3731 | S.DeduceReturnType(Fn, R.Expression->getExprLoc(), false)) |
3732 | return {}; |
3733 | |
3734 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
3735 | if (Method->isInstance()) { |
3736 | |
3737 | |
3738 | if (!R.HasFormOfMemberPointer) |
3739 | return {}; |
3740 | |
3741 | return S.Context.getMemberPointerType(Fn->getType(), |
3742 | S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); |
3743 | } |
3744 | |
3745 | if (!R.IsAddressOfOperand) return Fn->getType(); |
3746 | return S.Context.getPointerType(Fn->getType()); |
3747 | } |
3748 | |
3749 | |
3750 | |
3751 | |
3752 | |
3753 | static QualType |
3754 | ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, |
3755 | Expr *Arg, QualType ParamType, |
3756 | bool ParamWasReference) { |
3757 | |
3758 | OverloadExpr::FindResult R = OverloadExpr::find(Arg); |
3759 | |
3760 | OverloadExpr *Ovl = R.Expression; |
3761 | |
3762 | |
3763 | unsigned TDF = 0; |
3764 | if (ParamWasReference) |
3765 | TDF |= TDF_ParamWithReferenceType; |
3766 | if (R.IsAddressOfOperand) |
3767 | TDF |= TDF_IgnoreQualifiers; |
3768 | |
3769 | |
3770 | |
3771 | |
3772 | |
3773 | if (!ParamType->isFunctionType() && |
3774 | !ParamType->isFunctionPointerType() && |
3775 | !ParamType->isMemberFunctionPointerType()) { |
3776 | if (Ovl->hasExplicitTemplateArgs()) { |
3777 | |
3778 | if (FunctionDecl *ExplicitSpec |
3779 | = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) |
3780 | return GetTypeOfFunction(S, R, ExplicitSpec); |
3781 | } |
3782 | |
3783 | DeclAccessPair DAP; |
3784 | if (FunctionDecl *Viable = |
3785 | S.resolveAddressOfSingleOverloadCandidate(Arg, DAP)) |
3786 | return GetTypeOfFunction(S, R, Viable); |
3787 | |
3788 | return {}; |
3789 | } |
3790 | |
3791 | |
3792 | TemplateArgumentListInfo ExplicitTemplateArgs; |
3793 | if (Ovl->hasExplicitTemplateArgs()) |
3794 | Ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
3795 | QualType Match; |
3796 | for (UnresolvedSetIterator I = Ovl->decls_begin(), |
3797 | E = Ovl->decls_end(); I != E; ++I) { |
3798 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
3799 | |
3800 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) { |
3801 | |
3802 | |
3803 | |
3804 | if (!Ovl->hasExplicitTemplateArgs()) |
3805 | return {}; |
3806 | |
3807 | |
3808 | FunctionDecl *Specialization = nullptr; |
3809 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
3810 | if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs, |
3811 | Specialization, Info)) |
3812 | continue; |
3813 | |
3814 | D = Specialization; |
3815 | } |
3816 | |
3817 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
3818 | QualType ArgType = GetTypeOfFunction(S, R, Fn); |
3819 | if (ArgType.isNull()) continue; |
3820 | |
3821 | |
3822 | if (!ParamWasReference && ParamType->isPointerType() && |
3823 | ArgType->isFunctionType()) |
3824 | ArgType = S.Context.getPointerType(ArgType); |
3825 | |
3826 | |
3827 | |
3828 | |
3829 | |
3830 | |
3831 | |
3832 | |
3833 | |
3834 | |
3835 | |
3836 | |
3837 | |
3838 | SmallVector<DeducedTemplateArgument, 8> |
3839 | Deduced(TemplateParams->size()); |
3840 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
3841 | Sema::TemplateDeductionResult Result |
3842 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
3843 | ArgType, Info, Deduced, TDF); |
3844 | if (Result) continue; |
3845 | if (!Match.isNull()) |
3846 | return {}; |
3847 | Match = ArgType; |
3848 | } |
3849 | |
3850 | return Match; |
3851 | } |
3852 | |
3853 | |
3854 | |
3855 | |
3856 | |
3857 | |
3858 | |
3859 | static bool AdjustFunctionParmAndArgTypesForDeduction( |
3860 | Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, |
3861 | QualType &ParamType, QualType &ArgType, Expr *Arg, unsigned &TDF) { |
3862 | |
3863 | |
3864 | |
3865 | if (ParamType.hasQualifiers()) |
3866 | ParamType = ParamType.getUnqualifiedType(); |
3867 | |
3868 | |
3869 | |
3870 | const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); |
3871 | if (ParamRefType) |
3872 | ParamType = ParamRefType->getPointeeType(); |
3873 | |
3874 | |
3875 | |
3876 | |
3877 | if (ArgType == S.Context.OverloadTy) { |
3878 | ArgType = ResolveOverloadForDeduction(S, TemplateParams, |
3879 | Arg, ParamType, |
3880 | ParamRefType != nullptr); |
3881 | if (ArgType.isNull()) |
3882 | return true; |
3883 | } |
3884 | |
3885 | if (ParamRefType) { |
3886 | |
3887 | if (ArgType->isIncompleteArrayType()) |
3888 | ArgType = S.getCompletedType(Arg); |
3889 | |
3890 | |
3891 | |
3892 | |
3893 | if (isForwardingReference(QualType(ParamRefType, 0), FirstInnerIndex) && |
3894 | Arg->isLValue()) { |
3895 | if (S.getLangOpts().OpenCL && !ArgType.hasAddressSpace()) |
3896 | ArgType = S.Context.getAddrSpaceQualType( |
3897 | ArgType, S.Context.getDefaultOpenCLPointeeAddrSpace()); |
3898 | ArgType = S.Context.getLValueReferenceType(ArgType); |
3899 | } |
3900 | } else { |
3901 | |
3902 | |
3903 | |
3904 | |
3905 | |
3906 | if (ArgType->isArrayType()) |
3907 | ArgType = S.Context.getArrayDecayedType(ArgType); |
3908 | |
3909 | |
3910 | |
3911 | else if (ArgType->isFunctionType()) |
3912 | ArgType = S.Context.getPointerType(ArgType); |
3913 | else { |
3914 | |
3915 | |
3916 | ArgType = ArgType.getUnqualifiedType(); |
3917 | } |
3918 | } |
3919 | |
3920 | |
3921 | |
3922 | |
3923 | |
3924 | TDF = TDF_SkipNonDependent; |
3925 | |
3926 | |
3927 | |
3928 | |
3929 | if (ParamRefType) |
3930 | TDF |= TDF_ParamWithReferenceType; |
3931 | |
3932 | |
3933 | |
3934 | if (ArgType->isPointerType() || ArgType->isMemberPointerType() || |
3935 | ArgType->isObjCObjectPointerType()) |
3936 | TDF |= TDF_IgnoreQualifiers; |
3937 | |
3938 | |
3939 | |
3940 | |
3941 | |
3942 | if (isSimpleTemplateIdType(ParamType) || |
3943 | (isa<PointerType>(ParamType) && |
3944 | isSimpleTemplateIdType( |
3945 | ParamType->castAs<PointerType>()->getPointeeType()))) |
3946 | TDF |= TDF_DerivedClass; |
3947 | |
3948 | return false; |
3949 | } |
3950 | |
3951 | static bool |
3952 | hasDeducibleTemplateParameters(Sema &S, FunctionTemplateDecl *FunctionTemplate, |
3953 | QualType T); |
3954 | |
3955 | static Sema::TemplateDeductionResult DeduceTemplateArgumentsFromCallArgument( |
3956 | Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, |
3957 | QualType ParamType, Expr *Arg, TemplateDeductionInfo &Info, |
3958 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
3959 | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, |
3960 | bool DecomposedParam, unsigned ArgIdx, unsigned TDF); |
3961 | |
3962 | |
3963 | |
3964 | static Sema::TemplateDeductionResult DeduceFromInitializerList( |
3965 | Sema &S, TemplateParameterList *TemplateParams, QualType AdjustedParamType, |
3966 | InitListExpr *ILE, TemplateDeductionInfo &Info, |
3967 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
3968 | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, unsigned ArgIdx, |
3969 | unsigned TDF) { |
3970 | |
3971 | |
3972 | |
3973 | |
3974 | |
3975 | |
3976 | |
3977 | |
3978 | if (!ILE->getNumInits()) |
3979 | return Sema::TDK_Success; |
3980 | |
3981 | QualType ElTy; |
3982 | auto *ArrTy = S.Context.getAsArrayType(AdjustedParamType); |
3983 | if (ArrTy) |
3984 | ElTy = ArrTy->getElementType(); |
3985 | else if (!S.isStdInitializerList(AdjustedParamType, &ElTy)) { |
3986 | |
3987 | |
3988 | return Sema::TDK_Success; |
3989 | } |
3990 | |
3991 | |
3992 | |
3993 | for (Expr *E : ILE->inits()) |
3994 | if (isa<DesignatedInitExpr>(E)) |
3995 | return Sema::TDK_Success; |
3996 | |
3997 | |
3998 | if (ElTy->isDependentType()) { |
3999 | for (Expr *E : ILE->inits()) { |
4000 | if (auto Result = DeduceTemplateArgumentsFromCallArgument( |
4001 | S, TemplateParams, 0, ElTy, E, Info, Deduced, OriginalCallArgs, true, |
4002 | ArgIdx, TDF)) |
4003 | return Result; |
4004 | } |
4005 | } |
4006 | |
4007 | |
4008 | |
4009 | if (auto *DependentArrTy = dyn_cast_or_null<DependentSizedArrayType>(ArrTy)) { |
4010 | |
4011 | if (const NonTypeTemplateParmDecl *NTTP = |
4012 | getDeducedParameterFromExpr(Info, DependentArrTy->getSizeExpr())) { |
4013 | |
4014 | |
4015 | |
4016 | |
4017 | QualType T = S.Context.getSizeType(); |
4018 | llvm::APInt Size(S.Context.getIntWidth(T), ILE->getNumInits()); |
4019 | if (auto Result = DeduceNonTypeTemplateArgument( |
4020 | S, TemplateParams, NTTP, llvm::APSInt(Size), T, |
4021 | true, Info, Deduced)) |
4022 | return Result; |
4023 | } |
4024 | } |
4025 | |
4026 | return Sema::TDK_Success; |
4027 | } |
4028 | |
4029 | |
4030 | |
4031 | static Sema::TemplateDeductionResult DeduceTemplateArgumentsFromCallArgument( |
4032 | Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, |
4033 | QualType ParamType, Expr *Arg, TemplateDeductionInfo &Info, |
4034 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
4035 | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, |
4036 | bool DecomposedParam, unsigned ArgIdx, unsigned TDF) { |
4037 | QualType ArgType = Arg->getType(); |
4038 | QualType OrigParamType = ParamType; |
4039 | |
4040 | |
4041 | |
4042 | if (AdjustFunctionParmAndArgTypesForDeduction( |
4043 | S, TemplateParams, FirstInnerIndex, ParamType, ArgType, Arg, TDF)) |
4044 | return Sema::TDK_Success; |
4045 | |
4046 | |
4047 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) |
4048 | return DeduceFromInitializerList(S, TemplateParams, ParamType, ILE, Info, |
4049 | Deduced, OriginalCallArgs, ArgIdx, TDF); |
4050 | |
4051 | |
4052 | |
4053 | |
4054 | |
4055 | |
4056 | OriginalCallArgs.push_back( |
4057 | Sema::OriginalCallArg(OrigParamType, DecomposedParam, ArgIdx, ArgType)); |
4058 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
4059 | ArgType, Info, Deduced, TDF); |
4060 | } |
4061 | |
4062 | |
4063 | |
4064 | |
4065 | |
4066 | |
4067 | |
4068 | |
4069 | |
4070 | |
4071 | |
4072 | |
4073 | |
4074 | |
4075 | |
4076 | |
4077 | |
4078 | |
4079 | |
4080 | |
4081 | |
4082 | |
4083 | |
4084 | |
4085 | |
4086 | |
4087 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
4088 | FunctionTemplateDecl *FunctionTemplate, |
4089 | TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args, |
4090 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
4091 | bool PartialOverloading, |
4092 | llvm::function_ref<bool(ArrayRef<QualType>)> CheckNonDependent) { |
4093 | if (FunctionTemplate->isInvalidDecl()) |
4094 | return TDK_Invalid; |
4095 | |
4096 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
4097 | unsigned NumParams = Function->getNumParams(); |
4098 | |
4099 | unsigned FirstInnerIndex = getFirstInnerIndex(FunctionTemplate); |
4100 | |
4101 | |
4102 | |
4103 | |
4104 | |
4105 | if (Args.size() < Function->getMinRequiredArguments() && !PartialOverloading) |
4106 | return TDK_TooFewArguments; |
4107 | else if (TooManyArguments(NumParams, Args.size(), PartialOverloading)) { |
4108 | const auto *Proto = Function->getType()->castAs<FunctionProtoType>(); |
4109 | if (Proto->isTemplateVariadic()) |
4110 | ; |
4111 | else if (!Proto->isVariadic()) |
4112 | return TDK_TooManyArguments; |
4113 | } |
4114 | |
4115 | |
4116 | |
4117 | LocalInstantiationScope InstScope(*this); |
4118 | TemplateParameterList *TemplateParams |
4119 | = FunctionTemplate->getTemplateParameters(); |
4120 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
4121 | SmallVector<QualType, 8> ParamTypes; |
4122 | unsigned NumExplicitlySpecified = 0; |
4123 | if (ExplicitTemplateArgs) { |
4124 | TemplateDeductionResult Result; |
4125 | runWithSufficientStackSpace(Info.getLocation(), [&] { |
4126 | Result = SubstituteExplicitTemplateArguments( |
4127 | FunctionTemplate, *ExplicitTemplateArgs, Deduced, ParamTypes, nullptr, |
4128 | Info); |
4129 | }); |
4130 | if (Result) |
4131 | return Result; |
4132 | |
4133 | NumExplicitlySpecified = Deduced.size(); |
4134 | } else { |
4135 | |
4136 | for (unsigned I = 0; I != NumParams; ++I) |
4137 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
4138 | } |
4139 | |
4140 | SmallVector<OriginalCallArg, 8> OriginalCallArgs; |
4141 | |
4142 | |
4143 | auto DeduceCallArgument = [&](QualType ParamType, unsigned ArgIdx) { |
4144 | |
4145 | |
4146 | |
4147 | |
4148 | if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
4149 | return Sema::TDK_Success; |
4150 | |
4151 | |
4152 | return DeduceTemplateArgumentsFromCallArgument( |
4153 | *this, TemplateParams, FirstInnerIndex, ParamType, Args[ArgIdx], Info, Deduced, |
4154 | OriginalCallArgs, false, ArgIdx, 0); |
4155 | }; |
4156 | |
4157 | |
4158 | Deduced.resize(TemplateParams->size()); |
4159 | SmallVector<QualType, 8> ParamTypesForArgChecking; |
4160 | for (unsigned ParamIdx = 0, NumParamTypes = ParamTypes.size(), ArgIdx = 0; |
4161 | ParamIdx != NumParamTypes; ++ParamIdx) { |
4162 | QualType ParamType = ParamTypes[ParamIdx]; |
4163 | |
4164 | const PackExpansionType *ParamExpansion = |
4165 | dyn_cast<PackExpansionType>(ParamType); |
4166 | if (!ParamExpansion) { |
4167 | |
4168 | if (ArgIdx >= Args.size()) |
4169 | break; |
4170 | |
4171 | ParamTypesForArgChecking.push_back(ParamType); |
4172 | if (auto Result = DeduceCallArgument(ParamType, ArgIdx++)) |
4173 | return Result; |
4174 | |
4175 | continue; |
4176 | } |
4177 | |
4178 | QualType ParamPattern = ParamExpansion->getPattern(); |
4179 | PackDeductionScope PackScope(*this, TemplateParams, Deduced, Info, |
4180 | ParamPattern); |
4181 | |
4182 | |
4183 | |
4184 | |
4185 | |
4186 | |
4187 | |
4188 | |
4189 | |
4190 | |
4191 | |
4192 | |
4193 | |
4194 | |
4195 | |
4196 | |
4197 | |
4198 | if (ParamIdx + 1 == NumParamTypes || PackScope.hasFixedArity()) { |
4199 | for (; ArgIdx < Args.size() && PackScope.hasNextElement(); |
4200 | PackScope.nextPackElement(), ++ArgIdx) { |
4201 | ParamTypesForArgChecking.push_back(ParamPattern); |
4202 | if (auto Result = DeduceCallArgument(ParamPattern, ArgIdx)) |
4203 | return Result; |
4204 | } |
4205 | } else { |
4206 | |
4207 | |
4208 | |
4209 | Optional<unsigned> NumExpansions = ParamExpansion->getNumExpansions(); |
4210 | if (NumExpansions && !PackScope.isPartiallyExpanded()) { |
4211 | for (unsigned I = 0; I != *NumExpansions && ArgIdx < Args.size(); |
4212 | ++I, ++ArgIdx) { |
4213 | ParamTypesForArgChecking.push_back(ParamPattern); |
4214 | |
4215 | |
4216 | PackScope.nextPackElement(); |
4217 | } |
4218 | } |
4219 | } |
4220 | |
4221 | |
4222 | |
4223 | if (auto Result = PackScope.finish()) |
4224 | return Result; |
4225 | } |
4226 | |
4227 | |
4228 | |
4229 | DeclContext *CallingCtx = CurContext; |
4230 | |
4231 | TemplateDeductionResult Result; |
4232 | runWithSufficientStackSpace(Info.getLocation(), [&] { |
4233 | Result = FinishTemplateArgumentDeduction( |
4234 | FunctionTemplate, Deduced, NumExplicitlySpecified, Specialization, Info, |
4235 | &OriginalCallArgs, PartialOverloading, [&, CallingCtx]() { |
4236 | ContextRAII SavedContext(*this, CallingCtx); |
4237 | return CheckNonDependent(ParamTypesForArgChecking); |
4238 | }); |
4239 | }); |
4240 | return Result; |
4241 | } |
4242 | |
4243 | QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType, |
4244 | QualType FunctionType, |
4245 | bool AdjustExceptionSpec) { |
4246 | if (ArgFunctionType.isNull()) |
4247 | return ArgFunctionType; |
4248 | |
4249 | const auto *FunctionTypeP = FunctionType->castAs<FunctionProtoType>(); |
4250 | const auto *ArgFunctionTypeP = ArgFunctionType->castAs<FunctionProtoType>(); |
4251 | FunctionProtoType::ExtProtoInfo EPI = ArgFunctionTypeP->getExtProtoInfo(); |
4252 | bool Rebuild = false; |
4253 | |
4254 | CallingConv CC = FunctionTypeP->getCallConv(); |
4255 | if (EPI.ExtInfo.getCC() != CC) { |
4256 | EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC); |
4257 | Rebuild = true; |
4258 | } |
4259 | |
4260 | bool NoReturn = FunctionTypeP->getNoReturnAttr(); |
4261 | if (EPI.ExtInfo.getNoReturn() != NoReturn) { |
4262 | EPI.ExtInfo = EPI.ExtInfo.withNoReturn(NoReturn); |
4263 | Rebuild = true; |
4264 | } |
4265 | |
4266 | if (AdjustExceptionSpec && (FunctionTypeP->hasExceptionSpec() || |
4267 | ArgFunctionTypeP->hasExceptionSpec())) { |
4268 | EPI.ExceptionSpec = FunctionTypeP->getExtProtoInfo().ExceptionSpec; |
4269 | Rebuild = true; |
4270 | } |
4271 | |
4272 | if (!Rebuild) |
4273 | return ArgFunctionType; |
4274 | |
4275 | return Context.getFunctionType(ArgFunctionTypeP->getReturnType(), |
4276 | ArgFunctionTypeP->getParamTypes(), EPI); |
4277 | } |
4278 | |
4279 | |
4280 | |
4281 | |
4282 | |
4283 | |
4284 | |
4285 | |
4286 | |
4287 | |
4288 | |
4289 | |
4290 | |
4291 | |
4292 | |
4293 | |
4294 | |
4295 | |
4296 | |
4297 | |
4298 | |
4299 | |
4300 | |
4301 | |
4302 | |
4303 | |
4304 | |
4305 | |
4306 | |
4307 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
4308 | FunctionTemplateDecl *FunctionTemplate, |
4309 | TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ArgFunctionType, |
4310 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
4311 | bool IsAddressOfFunction) { |
4312 | if (FunctionTemplate->isInvalidDecl()) |
4313 | return TDK_Invalid; |
4314 | |
4315 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
4316 | TemplateParameterList *TemplateParams |
4317 | = FunctionTemplate->getTemplateParameters(); |
4318 | QualType FunctionType = Function->getType(); |
4319 | |
4320 | |
4321 | LocalInstantiationScope InstScope(*this); |
4322 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
4323 | unsigned NumExplicitlySpecified = 0; |
4324 | SmallVector<QualType, 4> ParamTypes; |
4325 | if (ExplicitTemplateArgs) { |
4326 | TemplateDeductionResult Result; |
4327 | runWithSufficientStackSpace(Info.getLocation(), [&] { |
4328 | Result = SubstituteExplicitTemplateArguments( |
4329 | FunctionTemplate, *ExplicitTemplateArgs, Deduced, ParamTypes, |
4330 | &FunctionType, Info); |
4331 | }); |
4332 | if (Result) |
4333 | return Result; |
4334 | |
4335 | NumExplicitlySpecified = Deduced.size(); |
4336 | } |
4337 | |
4338 | |
4339 | |
4340 | |
4341 | if (!IsAddressOfFunction) |
4342 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType, |
4343 | false); |
4344 | |
4345 | |
4346 | EnterExpressionEvaluationContext Unevaluated( |
4347 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
4348 | SFINAETrap Trap(*this); |
4349 | |
4350 | Deduced.resize(TemplateParams->size()); |
4351 | |
4352 | |
4353 | |
4354 | |
4355 | |
4356 | bool HasDeducedReturnType = false; |
4357 | if (getLangOpts().CPlusPlus14 && IsAddressOfFunction && |
4358 | Function->getReturnType()->getContainedAutoType()) { |
4359 | FunctionType = SubstAutoType(FunctionType, Context.DependentTy); |
4360 | HasDeducedReturnType = true; |
4361 | } |
4362 | |
4363 | if (!ArgFunctionType.isNull() && !FunctionType.isNull()) { |
4364 | unsigned TDF = |
4365 | TDF_TopLevelParameterTypeList | TDF_AllowCompatibleFunctionType; |
4366 | |
4367 | if (TemplateDeductionResult Result |
4368 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
4369 | FunctionType, ArgFunctionType, |
4370 | Info, Deduced, TDF)) |
4371 | return Result; |
4372 | } |
4373 | |
4374 | TemplateDeductionResult Result; |
4375 | runWithSufficientStackSpace(Info.getLocation(), [&] { |
4376 | Result = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
4377 | NumExplicitlySpecified, |
4378 | Specialization, Info); |
4379 | }); |
4380 | if (Result) |
4381 | return Result; |
4382 | |
4383 | |
4384 | |
4385 | if (HasDeducedReturnType && |
4386 | Specialization->getReturnType()->isUndeducedType() && |
4387 | DeduceReturnType(Specialization, Info.getLocation(), false)) |
4388 | return TDK_MiscellaneousDeductionFailure; |
4389 | |
4390 | |
4391 | |
4392 | auto *SpecializationFPT = |
4393 | Specialization->getType()->castAs<FunctionProtoType>(); |
4394 | if (getLangOpts().CPlusPlus17 && |
4395 | isUnresolvedExceptionSpec(SpecializationFPT->getExceptionSpecType()) && |
4396 | !ResolveExceptionSpec(Info.getLocation(), SpecializationFPT)) |
4397 | return TDK_MiscellaneousDeductionFailure; |
4398 | |
4399 | |
4400 | |
4401 | |
4402 | |
4403 | QualType SpecializationType = Specialization->getType(); |
4404 | if (!IsAddressOfFunction) |
4405 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, SpecializationType, |
4406 | true); |
4407 | |
4408 | |
4409 | |
4410 | |
4411 | if (!ArgFunctionType.isNull()) { |
4412 | if (IsAddressOfFunction && |
4413 | !isSameOrCompatibleFunctionType( |
4414 | Context.getCanonicalType(SpecializationType), |
4415 | Context.getCanonicalType(ArgFunctionType))) |
4416 | return TDK_MiscellaneousDeductionFailure; |
4417 | |
4418 | if (!IsAddressOfFunction && |
4419 | !Context.hasSameType(SpecializationType, ArgFunctionType)) |
4420 | return TDK_MiscellaneousDeductionFailure; |
4421 | } |
4422 | |
4423 | return TDK_Success; |
4424 | } |
4425 | |
4426 | |
4427 | |
4428 | |
4429 | Sema::TemplateDeductionResult |
4430 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *ConversionTemplate, |
4431 | QualType ToType, |
4432 | CXXConversionDecl *&Specialization, |
4433 | TemplateDeductionInfo &Info) { |
4434 | if (ConversionTemplate->isInvalidDecl()) |
4435 | return TDK_Invalid; |
4436 | |
4437 | CXXConversionDecl *ConversionGeneric |
4438 | = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl()); |
4439 | |
4440 | QualType FromType = ConversionGeneric->getConversionType(); |
4441 | |
4442 | |
4443 | QualType P = Context.getCanonicalType(FromType); |
4444 | QualType A = Context.getCanonicalType(ToType); |
4445 | |
4446 | |
4447 | |
4448 | |
4449 | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
4450 | P = PRef->getPointeeType(); |
4451 | |
4452 | |
4453 | |
4454 | |
4455 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) { |
4456 | A = ARef->getPointeeType(); |
4457 | |
4458 | |
4459 | |
4460 | if (!FromType->getAs<ReferenceType>()) { |
4461 | A = A.getUnqualifiedType(); |
4462 | P = P.getUnqualifiedType(); |
4463 | } |
4464 | |
4465 | |
4466 | |
4467 | |
4468 | } else { |
4469 | assert(!A->isReferenceType() && "Reference types were handled above"); |
4470 | |
4471 | |
4472 | |
4473 | |
4474 | if (P->isArrayType()) |
4475 | P = Context.getArrayDecayedType(P); |
4476 | |
4477 | |
4478 | |
4479 | else if (P->isFunctionType()) |
4480 | P = Context.getPointerType(P); |
4481 | |
4482 | |
4483 | else |
4484 | P = P.getUnqualifiedType(); |
4485 | |
4486 | |
4487 | |
4488 | |
4489 | |
4490 | A = A.getUnqualifiedType(); |
4491 | } |
4492 | |
4493 | |
4494 | EnterExpressionEvaluationContext Unevaluated( |
4495 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
4496 | SFINAETrap Trap(*this); |
4497 | |
4498 | |
4499 | |
4500 | |
4501 | |
4502 | |
4503 | TemplateParameterList *TemplateParams |
4504 | = ConversionTemplate->getTemplateParameters(); |
4505 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
4506 | Deduced.resize(TemplateParams->size()); |
4507 | |
4508 | |
4509 | |
4510 | |
4511 | |
4512 | unsigned TDF = 0; |
4513 | |
4514 | |
4515 | |
4516 | if (ToType->isReferenceType()) |
4517 | TDF |= TDF_ArgWithReferenceType; |
4518 | |
4519 | |
4520 | |
4521 | |
4522 | |
4523 | |
4524 | |
4525 | if ((P->isPointerType() && A->isPointerType()) || |
4526 | (P->isMemberPointerType() && A->isMemberPointerType())) |
4527 | TDF |= TDF_IgnoreQualifiers; |
4528 | if (TemplateDeductionResult Result |
4529 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
4530 | P, A, Info, Deduced, TDF)) |
4531 | return Result; |
4532 | |
4533 | |
4534 | LocalInstantiationScope InstScope(*this); |
4535 | |
4536 | FunctionDecl *ConversionSpecialized = nullptr; |
4537 | TemplateDeductionResult Result; |
4538 | runWithSufficientStackSpace(Info.getLocation(), [&] { |
4539 | Result = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0, |
4540 | ConversionSpecialized, Info); |
4541 | }); |
4542 | Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized); |
4543 | return Result; |
4544 | } |
4545 | |
4546 | |
4547 | |
4548 | |
4549 | |
4550 | |
4551 | |
4552 | |
4553 | |
4554 | |
4555 | |
4556 | |
4557 | |
4558 | |
4559 | |
4560 | |
4561 | |
4562 | |
4563 | |
4564 | |
4565 | |
4566 | |
4567 | |
4568 | |
4569 | |
4570 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
4571 | FunctionTemplateDecl *FunctionTemplate, |
4572 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
4573 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
4574 | bool IsAddressOfFunction) { |
4575 | return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
4576 | QualType(), Specialization, Info, |
4577 | IsAddressOfFunction); |
4578 | } |
4579 | |
4580 | namespace { |
4581 | struct DependentAuto { bool IsPack; }; |
4582 | |
4583 | |
4584 | |
4585 | class SubstituteDeducedTypeTransform : |
4586 | public TreeTransform<SubstituteDeducedTypeTransform> { |
4587 | QualType Replacement; |
4588 | bool ReplacementIsPack; |
4589 | bool UseTypeSugar; |
4590 | |
4591 | public: |
4592 | SubstituteDeducedTypeTransform(Sema &SemaRef, DependentAuto DA) |
4593 | : TreeTransform<SubstituteDeducedTypeTransform>(SemaRef), Replacement(), |
4594 | ReplacementIsPack(DA.IsPack), UseTypeSugar(true) {} |
4595 | |
4596 | SubstituteDeducedTypeTransform(Sema &SemaRef, QualType Replacement, |
4597 | bool UseTypeSugar = true) |
4598 | : TreeTransform<SubstituteDeducedTypeTransform>(SemaRef), |
4599 | Replacement(Replacement), ReplacementIsPack(false), |
4600 | UseTypeSugar(UseTypeSugar) {} |
4601 | |
4602 | QualType TransformDesugared(TypeLocBuilder &TLB, DeducedTypeLoc TL) { |
4603 | assert(isa<TemplateTypeParmType>(Replacement) && |
4604 | "unexpected unsugared replacement kind"); |
4605 | QualType Result = Replacement; |
4606 | TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); |
4607 | NewTL.setNameLoc(TL.getNameLoc()); |
4608 | return Result; |
4609 | } |
4610 | |
4611 | QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) { |
4612 | |
4613 | |
4614 | |
4615 | |
4616 | |
4617 | |
4618 | |
4619 | |
4620 | |
4621 | if (!UseTypeSugar) |
4622 | return TransformDesugared(TLB, TL); |
4623 | |
4624 | QualType Result = SemaRef.Context.getAutoType( |
4625 | Replacement, TL.getTypePtr()->getKeyword(), Replacement.isNull(), |
4626 | ReplacementIsPack, TL.getTypePtr()->getTypeConstraintConcept(), |
4627 | TL.getTypePtr()->getTypeConstraintArguments()); |
4628 | auto NewTL = TLB.push<AutoTypeLoc>(Result); |
4629 | NewTL.copy(TL); |
4630 | return Result; |
4631 | } |
4632 | |
4633 | QualType TransformDeducedTemplateSpecializationType( |
4634 | TypeLocBuilder &TLB, DeducedTemplateSpecializationTypeLoc TL) { |
4635 | if (!UseTypeSugar) |
4636 | return TransformDesugared(TLB, TL); |
4637 | |
4638 | QualType Result = SemaRef.Context.getDeducedTemplateSpecializationType( |
4639 | TL.getTypePtr()->getTemplateName(), |
4640 | Replacement, Replacement.isNull()); |
4641 | auto NewTL = TLB.push<DeducedTemplateSpecializationTypeLoc>(Result); |
4642 | NewTL.setNameLoc(TL.getNameLoc()); |
4643 | return Result; |
4644 | } |
4645 | |
4646 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
4647 | |
4648 | return E; |
4649 | } |
4650 | |
4651 | QualType Apply(TypeLoc TL) { |
4652 | |
4653 | |
4654 | TypeLocBuilder TLB; |
4655 | TLB.reserve(TL.getFullDataSize()); |
4656 | return TransformType(TLB, TL); |
4657 | } |
4658 | }; |
4659 | |
4660 | } |
4661 | |
4662 | Sema::DeduceAutoResult |
4663 | Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *&Init, QualType &Result, |
4664 | Optional<unsigned> DependentDeductionDepth, |
4665 | bool IgnoreConstraints) { |
4666 | return DeduceAutoType(Type->getTypeLoc(), Init, Result, |
4667 | DependentDeductionDepth, IgnoreConstraints); |
4668 | } |
4669 | |
4670 | |
4671 | |
4672 | |
4673 | static bool diagnoseAutoDeductionFailure(Sema &S, |
4674 | Sema::TemplateDeductionResult TDK, |
4675 | TemplateDeductionInfo &Info, |
4676 | ArrayRef<SourceRange> Ranges) { |
4677 | switch (TDK) { |
4678 | case Sema::TDK_Inconsistent: { |
4679 | |
4680 | auto D = S.Diag(Info.getLocation(), diag::err_auto_inconsistent_deduction); |
4681 | D << Info.FirstArg << Info.SecondArg; |
4682 | for (auto R : Ranges) |
4683 | D << R; |
4684 | return true; |
4685 | } |
4686 | |
4687 | |
4688 | |
4689 | |
4690 | default: |
4691 | return false; |
4692 | } |
4693 | } |
4694 | |
4695 | static Sema::DeduceAutoResult |
4696 | CheckDeducedPlaceholderConstraints(Sema &S, const AutoType &Type, |
4697 | AutoTypeLoc TypeLoc, QualType Deduced) { |
4698 | ConstraintSatisfaction Satisfaction; |
4699 | ConceptDecl *Concept = Type.getTypeConstraintConcept(); |
4700 | TemplateArgumentListInfo TemplateArgs(TypeLoc.getLAngleLoc(), |
4701 | TypeLoc.getRAngleLoc()); |
4702 | TemplateArgs.addArgument( |
4703 | TemplateArgumentLoc(TemplateArgument(Deduced), |
4704 | S.Context.getTrivialTypeSourceInfo( |
4705 | Deduced, TypeLoc.getNameLoc()))); |
4706 | for (unsigned I = 0, C = TypeLoc.getNumArgs(); I != C; ++I) |
4707 | TemplateArgs.addArgument(TypeLoc.getArgLoc(I)); |
4708 | |
4709 | llvm::SmallVector<TemplateArgument, 4> Converted; |
4710 | if (S.CheckTemplateArgumentList(Concept, SourceLocation(), TemplateArgs, |
4711 | false, Converted)) |
4712 | return Sema::DAR_FailedAlreadyDiagnosed; |
4713 | if (S.CheckConstraintSatisfaction(Concept, {Concept->getConstraintExpr()}, |
4714 | Converted, TypeLoc.getLocalSourceRange(), |
4715 | Satisfaction)) |
4716 | return Sema::DAR_FailedAlreadyDiagnosed; |
4717 | if (!Satisfaction.IsSatisfied) { |
4718 | std::string Buf; |
4719 | llvm::raw_string_ostream OS(Buf); |
4720 | OS << "'" << Concept->getName(); |
4721 | if (TypeLoc.hasExplicitTemplateArgs()) { |
4722 | printTemplateArgumentList( |
4723 | OS, Type.getTypeConstraintArguments(), S.getPrintingPolicy(), |
4724 | Type.getTypeConstraintConcept()->getTemplateParameters()); |
4725 | } |
4726 | OS << "'"; |
4727 | OS.flush(); |
4728 | S.Diag(TypeLoc.getConceptNameLoc(), |
4729 | diag::err_placeholder_constraints_not_satisfied) |
4730 | << Deduced << Buf << TypeLoc.getLocalSourceRange(); |
4731 | S.DiagnoseUnsatisfiedConstraint(Satisfaction); |
4732 | return Sema::DAR_FailedAlreadyDiagnosed; |
4733 | } |
4734 | return Sema::DAR_Succeeded; |
4735 | } |
4736 | |
4737 | |
4738 | |
4739 | |
4740 | |
4741 | |
4742 | |
4743 | |
4744 | |
4745 | |
4746 | |
4747 | |
4748 | |
4749 | |
4750 | |
4751 | |
4752 | |
4753 | Sema::DeduceAutoResult |
4754 | Sema::DeduceAutoType(TypeLoc Type, Expr *&Init, QualType &Result, |
4755 | Optional<unsigned> DependentDeductionDepth, |
4756 | bool IgnoreConstraints) { |
4757 | if (Init->containsErrors()) |
4758 | return DAR_FailedAlreadyDiagnosed; |
4759 | if (Init->getType()->isNonOverloadPlaceholderType()) { |
4760 | ExprResult NonPlaceholder = CheckPlaceholderExpr(Init); |
4761 | if (NonPlaceholder.isInvalid()) |
4762 | return DAR_FailedAlreadyDiagnosed; |
4763 | Init = NonPlaceholder.get(); |
4764 | } |
4765 | |
4766 | DependentAuto DependentResult = { |
4767 | (bool)Type.getAs<PackExpansionTypeLoc>()}; |
4768 | |
4769 | if (!DependentDeductionDepth && |
4770 | (Type.getType()->isDependentType() || Init->isTypeDependent() || |
4771 | Init->containsUnexpandedParameterPack())) { |
4772 | Result = SubstituteDeducedTypeTransform(*this, DependentResult).Apply(Type); |
4773 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
4774 | return DAR_Succeeded; |
4775 | } |
4776 | |
4777 | |
4778 | unsigned Depth = DependentDeductionDepth.getValueOr(0); |
4779 | |
4780 | |
4781 | |
4782 | |
4783 | if (const AutoType *AT = Type.getType()->getAs<AutoType>()) { |
4784 | if (AT->isDecltypeAuto()) { |
4785 | if (isa<InitListExpr>(Init)) { |
4786 | Diag(Init->getBeginLoc(), diag::err_decltype_auto_initializer_list); |
4787 | return DAR_FailedAlreadyDiagnosed; |
4788 | } |
4789 | |
4790 | ExprResult ER = CheckPlaceholderExpr(Init); |
4791 | if (ER.isInvalid()) |
4792 | return DAR_FailedAlreadyDiagnosed; |
4793 | Init = ER.get(); |
4794 | QualType Deduced = BuildDecltypeType(Init, Init->getBeginLoc(), false); |
4795 | if (Deduced.isNull()) |
4796 | return DAR_FailedAlreadyDiagnosed; |
4797 | |
4798 | Deduced = Context.getCanonicalType(Deduced); |
4799 | if (AT->isConstrained() && !IgnoreConstraints) { |
4800 | auto ConstraintsResult = |
4801 | CheckDeducedPlaceholderConstraints(*this, *AT, |
4802 | Type.getContainedAutoTypeLoc(), |
4803 | Deduced); |
4804 | if (ConstraintsResult != DAR_Succeeded) |
4805 | return ConstraintsResult; |
4806 | } |
4807 | Result = SubstituteDeducedTypeTransform(*this, Deduced).Apply(Type); |
4808 | if (Result.isNull()) |
4809 | return DAR_FailedAlreadyDiagnosed; |
4810 | return DAR_Succeeded; |
4811 | } else if (!getLangOpts().CPlusPlus) { |
4812 | if (isa<InitListExpr>(Init)) { |
4813 | Diag(Init->getBeginLoc(), diag::err_auto_init_list_from_c); |
4814 | return DAR_FailedAlreadyDiagnosed; |
4815 | } |
4816 | } |
4817 | } |
4818 | |
4819 | SourceLocation Loc = Init->getExprLoc(); |
4820 | |
4821 | LocalInstantiationScope InstScope(*this); |
4822 | |
4823 | |
4824 | TemplateTypeParmDecl *TemplParam = TemplateTypeParmDecl::Create( |
4825 | Context, nullptr, SourceLocation(), Loc, Depth, 0, nullptr, false, false, |
4826 | false); |
4827 | QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0); |
4828 | NamedDecl *TemplParamPtr = TemplParam; |
4829 | FixedSizeTemplateParameterListStorage<1, false> TemplateParamsSt( |
4830 | Context, Loc, Loc, TemplParamPtr, Loc, nullptr); |
4831 | |
4832 | QualType FuncParam = |
4833 | SubstituteDeducedTypeTransform(*this, TemplArg, false) |
4834 | .Apply(Type); |
4835 | assert(!FuncParam.isNull() && |
4836 | "substituting template parameter for 'auto' failed"); |
4837 | |
4838 | |
4839 | SmallVector<DeducedTemplateArgument, 1> Deduced; |
4840 | Deduced.resize(1); |
4841 | |
4842 | TemplateDeductionInfo Info(Loc, Depth); |
4843 | |
4844 | |
4845 | |
4846 | auto DeductionFailed = [&](TemplateDeductionResult TDK, |
4847 | ArrayRef<SourceRange> Ranges) -> DeduceAutoResult { |
4848 | if (Init->isTypeDependent()) { |
4849 | Result = |
4850 | SubstituteDeducedTypeTransform(*this, DependentResult).Apply(Type); |
4851 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
4852 | return DAR_Succeeded; |
4853 | } |
4854 | if (diagnoseAutoDeductionFailure(*this, TDK, Info, Ranges)) |
4855 | return DAR_FailedAlreadyDiagnosed; |
4856 | return DAR_Failed; |
4857 | }; |
4858 | |
4859 | SmallVector<OriginalCallArg, 4> OriginalCallArgs; |
4860 | |
4861 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
4862 | if (InitList) { |
4863 | |
4864 | |
4865 | |
4866 | if (!Type.getType().getNonReferenceType()->getAs<AutoType>()) |
4867 | return DAR_Failed; |
4868 | |
4869 | |
4870 | |
4871 | for (Expr *E : InitList->inits()) |
4872 | if (isa<DesignatedInitExpr>(E)) |
4873 | return DAR_Failed; |
4874 | |
4875 | SourceRange DeducedFromInitRange; |
4876 | for (unsigned i = 0, e = InitList->getNumInits(); i < e; ++i) { |
4877 | Expr *Init = InitList->getInit(i); |
4878 | |
4879 | if (auto TDK = DeduceTemplateArgumentsFromCallArgument( |
4880 | *this, TemplateParamsSt.get(), 0, TemplArg, Init, |
4881 | Info, Deduced, OriginalCallArgs, true, |
4882 | 0, 0)) |
4883 | return DeductionFailed(TDK, {DeducedFromInitRange, |
4884 | Init->getSourceRange()}); |
4885 | |
4886 | if (DeducedFromInitRange.isInvalid() && |
4887 | Deduced[0].getKind() != TemplateArgument::Null) |
4888 | DeducedFromInitRange = Init->getSourceRange(); |
4889 | } |
4890 | } else { |
4891 | if (!getLangOpts().CPlusPlus && Init->refersToBitField()) { |
4892 | Diag(Loc, diag::err_auto_bitfield); |
4893 | return DAR_FailedAlreadyDiagnosed; |
4894 | } |
4895 | |
4896 | if (auto TDK = DeduceTemplateArgumentsFromCallArgument( |
4897 | *this, TemplateParamsSt.get(), 0, FuncParam, Init, Info, Deduced, |
4898 | OriginalCallArgs, false, 0, 0)) |
4899 | return DeductionFailed(TDK, {}); |
4900 | } |
4901 | |
4902 | |
4903 | if (Deduced[0].getKind() != TemplateArgument::Type) |
4904 | return DeductionFailed(TDK_Incomplete, {}); |
4905 | |
4906 | QualType DeducedType = Deduced[0].getAsType(); |
4907 | |
4908 | if (InitList) { |
4909 | DeducedType = BuildStdInitializerList(DeducedType, Loc); |
4910 | if (DeducedType.isNull()) |
4911 | return DAR_FailedAlreadyDiagnosed; |
4912 | } |
4913 | |
4914 | if (const auto *AT = Type.getType()->getAs<AutoType>()) { |
4915 | if (AT->isConstrained() && !IgnoreConstraints) { |
4916 | auto ConstraintsResult = |
4917 | CheckDeducedPlaceholderConstraints(*this, *AT, |
4918 | Type.getContainedAutoTypeLoc(), |
4919 | DeducedType); |
4920 | if (ConstraintsResult != DAR_Succeeded) |
4921 | return ConstraintsResult; |
4922 | } |
4923 | } |
4924 | |
4925 | Result = SubstituteDeducedTypeTransform(*this, DeducedType).Apply(Type); |
4926 | if (Result.isNull()) |
4927 | return DAR_FailedAlreadyDiagnosed; |
4928 | |
4929 | |
4930 | |
4931 | QualType DeducedA = InitList ? Deduced[0].getAsType() : Result; |
4932 | for (const OriginalCallArg &OriginalArg : OriginalCallArgs) { |
4933 | assert((bool)InitList == OriginalArg.DecomposedParam && |
4934 | "decomposed non-init-list in auto deduction?"); |
4935 | if (auto TDK = |
4936 | CheckOriginalCallArgDeduction(*this, Info, OriginalArg, DeducedA)) { |
4937 | Result = QualType(); |
4938 | return DeductionFailed(TDK, {}); |
4939 | } |
4940 | } |
4941 | |
4942 | return DAR_Succeeded; |
4943 | } |
4944 | |
4945 | QualType Sema::SubstAutoType(QualType TypeWithAuto, |
4946 | QualType TypeToReplaceAuto) { |
4947 | if (TypeToReplaceAuto->isDependentType()) |
4948 | return SubstituteDeducedTypeTransform( |
4949 | *this, DependentAuto{ |
4950 | TypeToReplaceAuto->containsUnexpandedParameterPack()}) |
4951 | .TransformType(TypeWithAuto); |
4952 | return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto) |
4953 | .TransformType(TypeWithAuto); |
4954 | } |
4955 | |
4956 | TypeSourceInfo *Sema::SubstAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, |
4957 | QualType TypeToReplaceAuto) { |
4958 | if (TypeToReplaceAuto->isDependentType()) |
4959 | return SubstituteDeducedTypeTransform( |
4960 | *this, |
4961 | DependentAuto{ |
4962 | TypeToReplaceAuto->containsUnexpandedParameterPack()}) |
4963 | .TransformType(TypeWithAuto); |
4964 | return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto) |
4965 | .TransformType(TypeWithAuto); |
4966 | } |
4967 | |
4968 | QualType Sema::ReplaceAutoType(QualType TypeWithAuto, |
4969 | QualType TypeToReplaceAuto) { |
4970 | return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto, |
4971 | false) |
4972 | .TransformType(TypeWithAuto); |
4973 | } |
4974 | |
4975 | TypeSourceInfo *Sema::ReplaceAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, |
4976 | QualType TypeToReplaceAuto) { |
4977 | return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto, |
4978 | false) |
4979 | .TransformType(TypeWithAuto); |
4980 | } |
4981 | |
4982 | void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) { |
4983 | if (isa<InitListExpr>(Init)) |
4984 | Diag(VDecl->getLocation(), |
4985 | VDecl->isInitCapture() |
4986 | ? diag::err_init_capture_deduction_failure_from_init_list |
4987 | : diag::err_auto_var_deduction_failure_from_init_list) |
4988 | << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange(); |
4989 | else |
4990 | Diag(VDecl->getLocation(), |
4991 | VDecl->isInitCapture() ? diag::err_init_capture_deduction_failure |
4992 | : diag::err_auto_var_deduction_failure) |
4993 | << VDecl->getDeclName() << VDecl->getType() << Init->getType() |
4994 | << Init->getSourceRange(); |
4995 | } |
4996 | |
4997 | bool Sema::DeduceReturnType(FunctionDecl *FD, SourceLocation Loc, |
4998 | bool Diagnose) { |
4999 | assert(FD->getReturnType()->isUndeducedType()); |
5000 | |
5001 | |
5002 | |
5003 | if (isLambdaConversionOperator(FD)) { |
5004 | CXXRecordDecl *Lambda = cast<CXXMethodDecl>(FD)->getParent(); |
5005 | FunctionDecl *CallOp = Lambda->getLambdaCallOperator(); |
5006 | |
5007 | |
5008 | if (auto *Args = FD->getTemplateSpecializationArgs()) { |
5009 | CallOp = InstantiateFunctionDeclaration( |
5010 | CallOp->getDescribedFunctionTemplate(), Args, Loc); |
5011 | if (!CallOp || CallOp->isInvalidDecl()) |
5012 | return true; |
5013 | |
5014 | |
5015 | |
5016 | if (CallOp->getReturnType()->isUndeducedType()) { |
5017 | runWithSufficientStackSpace(Loc, [&] { |
5018 | InstantiateFunctionDefinition(Loc, CallOp); |
5019 | }); |
5020 | } |
5021 | } |
5022 | |
5023 | if (CallOp->isInvalidDecl()) |
5024 | return true; |
5025 | assert(!CallOp->getReturnType()->isUndeducedType() && |
5026 | "failed to deduce lambda return type"); |
5027 | |
5028 | |
5029 | CallingConv RetTyCC = FD->getReturnType() |
5030 | ->getPointeeType() |
5031 | ->castAs<FunctionType>() |
5032 | ->getCallConv(); |
5033 | QualType RetType = getLambdaConversionFunctionResultType( |
5034 | CallOp->getType()->castAs<FunctionProtoType>(), RetTyCC); |
5035 | if (FD->getReturnType()->getAs<PointerType>()) |
5036 | RetType = Context.getPointerType(RetType); |
5037 | else { |
5038 | assert(FD->getReturnType()->getAs<BlockPointerType>()); |
5039 | RetType = Context.getBlockPointerType(RetType); |
5040 | } |
5041 | Context.adjustDeducedFunctionResultType(FD, RetType); |
5042 | return false; |
5043 | } |
5044 | |
5045 | if (FD->getTemplateInstantiationPattern()) { |
5046 | runWithSufficientStackSpace(Loc, [&] { |
5047 | InstantiateFunctionDefinition(Loc, FD); |
5048 | }); |
5049 | } |
5050 | |
5051 | bool StillUndeduced = FD->getReturnType()->isUndeducedType(); |
5052 | if (StillUndeduced && Diagnose && !FD->isInvalidDecl()) { |
5053 | Diag(Loc, diag::err_auto_fn_used_before_defined) << FD; |
5054 | Diag(FD->getLocation(), diag::note_callee_decl) << FD; |
5055 | } |
5056 | |
5057 | return StillUndeduced; |
5058 | } |
5059 | |
5060 | |
5061 | static void |
5062 | AddImplicitObjectParameterType(ASTContext &Context, |
5063 | CXXMethodDecl *Method, |
5064 | SmallVectorImpl<QualType> &ArgTypes) { |
5065 | |
5066 | |
5067 | |
5068 | |
5069 | |
5070 | |
5071 | |
5072 | QualType ArgTy = Context.getTypeDeclType(Method->getParent()); |
5073 | ArgTy = Context.getQualifiedType(ArgTy, Method->getMethodQualifiers()); |
5074 | if (Method->getRefQualifier() == RQ_RValue) |
5075 | ArgTy = Context.getRValueReferenceType(ArgTy); |
5076 | else |
5077 | ArgTy = Context.getLValueReferenceType(ArgTy); |
5078 | ArgTypes.push_back(ArgTy); |
5079 | } |
5080 | |
5081 | |
5082 | |
5083 | static bool isAtLeastAsSpecializedAs(Sema &S, |
5084 | SourceLocation Loc, |
5085 | FunctionTemplateDecl *FT1, |
5086 | FunctionTemplateDecl *FT2, |
5087 | TemplatePartialOrderingContext TPOC, |
5088 | unsigned NumCallArguments1, |
5089 | bool Reversed) { |
5090 | assert(!Reversed || TPOC == TPOC_Call); |
5091 | |
5092 | FunctionDecl *FD1 = FT1->getTemplatedDecl(); |
5093 | FunctionDecl *FD2 = FT2->getTemplatedDecl(); |
5094 | const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); |
5095 | const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); |
5096 | |
5097 | assert(Proto1 && Proto2 && "Function templates must have prototypes"); |
5098 | TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); |
5099 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
5100 | Deduced.resize(TemplateParams->size()); |
5101 | |
5102 | |
5103 | |
5104 | |
5105 | TemplateDeductionInfo Info(Loc); |
5106 | SmallVector<QualType, 4> Args2; |
5107 | switch (TPOC) { |
5108 | case TPOC_Call: { |
5109 | |
5110 | |
5111 | CXXMethodDecl *Method1 = dyn_cast<CXXMethodDecl>(FD1); |
5112 | CXXMethodDecl *Method2 = dyn_cast<CXXMethodDecl>(FD2); |
5113 | |
5114 | |
5115 | |
5116 | |
5117 | |
5118 | |
5119 | |
5120 | |
5121 | |
5122 | |
5123 | |
5124 | |
5125 | |
5126 | |
5127 | |
5128 | |
5129 | SmallVector<QualType, 4> Args1; |
5130 | |
5131 | unsigned NumComparedArguments = NumCallArguments1; |
5132 | |
5133 | if (!Method2 && Method1 && !Method1->isStatic()) { |
5134 | |
5135 | AddImplicitObjectParameterType(S.Context, Method1, Args1); |
5136 | ++NumComparedArguments; |
5137 | } else if (!Method1 && Method2 && !Method2->isStatic()) { |
5138 | |
5139 | AddImplicitObjectParameterType(S.Context, Method2, Args2); |
5140 | } else if (Method1 && Method2 && Reversed) { |
5141 | |
5142 | |
5143 | AddImplicitObjectParameterType(S.Context, Method1, Args1); |
5144 | AddImplicitObjectParameterType(S.Context, Method2, Args2); |
5145 | ++NumComparedArguments; |
5146 | } |
5147 | |
5148 | Args1.insert(Args1.end(), Proto1->param_type_begin(), |
5149 | Proto1->param_type_end()); |
5150 | Args2.insert(Args2.end(), Proto2->param_type_begin(), |
5151 | Proto2->param_type_end()); |
5152 | |
5153 | |
5154 | |
5155 | |
5156 | if (Args1.size() > NumComparedArguments) |
5157 | Args1.resize(NumComparedArguments); |
5158 | if (Args2.size() > NumComparedArguments) |
5159 | Args2.resize(NumComparedArguments); |
5160 | if (Reversed) |
5161 | std::reverse(Args2.begin(), Args2.end()); |
5162 | if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(), |
5163 | Args1.data(), Args1.size(), Info, Deduced, |
5164 | TDF_None, true)) |
5165 | return false; |
5166 | |
5167 | break; |
5168 | } |
5169 | |
5170 | case TPOC_Conversion: |
5171 | |
5172 | |
5173 | if (DeduceTemplateArgumentsByTypeMatch( |
5174 | S, TemplateParams, Proto2->getReturnType(), Proto1->getReturnType(), |
5175 | Info, Deduced, TDF_None, |
5176 | true)) |
5177 | return false; |
5178 | break; |
5179 | |
5180 | case TPOC_Other: |
5181 | |
5182 | |
5183 | if (DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
5184 | FD2->getType(), FD1->getType(), |
5185 | Info, Deduced, TDF_None, |
5186 | true)) |
5187 | return false; |
5188 | break; |
5189 | } |
5190 | |
5191 | |
5192 | |
5193 | |
5194 | |
5195 | |
5196 | |
5197 | unsigned ArgIdx = 0, NumArgs = Deduced.size(); |
5198 | for (; ArgIdx != NumArgs; ++ArgIdx) |
5199 | if (Deduced[ArgIdx].isNull()) |
5200 | break; |
5201 | |
5202 | |
5203 | |
5204 | |
5205 | |
5206 | if (ArgIdx == NumArgs) { |
5207 | |
5208 | |
5209 | return true; |
5210 | } |
5211 | |
5212 | |
5213 | llvm::SmallBitVector UsedParameters(TemplateParams->size()); |
5214 | switch (TPOC) { |
5215 | case TPOC_Call: |
5216 | for (unsigned I = 0, N = Args2.size(); I != N; ++I) |
5217 | ::MarkUsedTemplateParameters(S.Context, Args2[I], false, |
5218 | TemplateParams->getDepth(), |
5219 | UsedParameters); |
5220 | break; |
5221 | |
5222 | case TPOC_Conversion: |
5223 | ::MarkUsedTemplateParameters(S.Context, Proto2->getReturnType(), false, |
5224 | TemplateParams->getDepth(), UsedParameters); |
5225 | break; |
5226 | |
5227 | case TPOC_Other: |
5228 | ::MarkUsedTemplateParameters(S.Context, FD2->getType(), false, |
5229 | TemplateParams->getDepth(), |
5230 | UsedParameters); |
5231 | break; |
5232 | } |
5233 | |
5234 | for (; ArgIdx != NumArgs; ++ArgIdx) |
5235 | |
5236 | |
5237 | if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) |
5238 | return false; |
5239 | |
5240 | return true; |
5241 | } |
5242 | |
5243 | |
5244 | |
5245 | static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) { |
5246 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
5247 | unsigned NumParams = Function->getNumParams(); |
5248 | if (NumParams == 0) |
5249 | return false; |
5250 | |
5251 | ParmVarDecl *Last = Function->getParamDecl(NumParams - 1); |
5252 | if (!Last->isParameterPack()) |
5253 | return false; |
5254 | |
5255 | |
5256 | while (--NumParams > 0) { |
5257 | if (Function->getParamDecl(NumParams - 1)->isParameterPack()) |
5258 | return false; |
5259 | } |
5260 | |
5261 | return true; |
5262 | } |
5263 | |
5264 | |
5265 | |
5266 | |
5267 | |
5268 | |
5269 | |
5270 | |
5271 | |
5272 | |
5273 | |
5274 | |
5275 | |
5276 | |
5277 | |
5278 | |
5279 | |
5280 | |
5281 | |
5282 | |
5283 | |
5284 | |
5285 | |
5286 | FunctionTemplateDecl * |
5287 | Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, |
5288 | FunctionTemplateDecl *FT2, |
5289 | SourceLocation Loc, |
5290 | TemplatePartialOrderingContext TPOC, |
5291 | unsigned NumCallArguments1, |
5292 | unsigned NumCallArguments2, |
5293 | bool Reversed) { |
5294 | |
5295 | auto JudgeByConstraints = [&] () -> FunctionTemplateDecl * { |
5296 | llvm::SmallVector<const Expr *, 3> AC1, AC2; |
5297 | FT1->getAssociatedConstraints(AC1); |
5298 | FT2->getAssociatedConstraints(AC2); |
5299 | bool AtLeastAsConstrained1, AtLeastAsConstrained2; |
5300 | if (IsAtLeastAsConstrained(FT1, AC1, FT2, AC2, AtLeastAsConstrained1)) |
5301 | return nullptr; |
5302 | if (IsAtLeastAsConstrained(FT2, AC2, FT1, AC1, AtLeastAsConstrained2)) |
5303 | return nullptr; |
5304 | if (AtLeastAsConstrained1 == AtLeastAsConstrained2) |
5305 | return nullptr; |
5306 | return AtLeastAsConstrained1 ? FT1 : FT2; |
5307 | }; |
5308 | |
5309 | bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, |
5310 | NumCallArguments1, Reversed); |
5311 | bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, |
5312 | NumCallArguments2, Reversed); |
5313 | |
5314 | if (Better1 != Better2) |
5315 | return Better1 ? FT1 : FT2; |
5316 | |
5317 | if (!Better1 && !Better2) |
5318 | return JudgeByConstraints(); |
5319 | |
5320 | |
5321 | |
5322 | |
5323 | bool Variadic1 = isVariadicFunctionTemplate(FT1); |
5324 | bool Variadic2 = isVariadicFunctionTemplate(FT2); |
5325 | if (Variadic1 != Variadic2) |
5326 | return Variadic1? FT2 : FT1; |
5327 | |
5328 | return JudgeByConstraints(); |
5329 | } |
5330 | |
5331 | |
5332 | static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { |
5333 | if (T1 == T2) |
5334 | return true; |
5335 | |
5336 | if (!T1 || !T2) |
5337 | return false; |
5338 | |
5339 | return T1->getCanonicalDecl() == T2->getCanonicalDecl(); |
5340 | } |
5341 | |
5342 | |
5343 | |
5344 | |
5345 | |
5346 | |
5347 | |
5348 | |
5349 | |
5350 | |
5351 | |
5352 | |
5353 | |
5354 | |
5355 | |
5356 | |
5357 | |
5358 | |
5359 | |
5360 | |
5361 | |
5362 | |
5363 | |
5364 | |
5365 | |
5366 | |
5367 | UnresolvedSetIterator Sema::getMostSpecialized( |
5368 | UnresolvedSetIterator SpecBegin, UnresolvedSetIterator SpecEnd, |
5369 | TemplateSpecCandidateSet &FailedCandidates, |
5370 | SourceLocation Loc, const PartialDiagnostic &NoneDiag, |
5371 | const PartialDiagnostic &AmbigDiag, const PartialDiagnostic &CandidateDiag, |
5372 | bool Complain, QualType TargetType) { |
5373 | if (SpecBegin == SpecEnd) { |
5374 | if (Complain) { |
5375 | Diag(Loc, NoneDiag); |
5376 | FailedCandidates.NoteCandidates(*this, Loc); |
5377 | } |
5378 | return SpecEnd; |
5379 | } |
5380 | |
5381 | if (SpecBegin + 1 == SpecEnd) |
5382 | return SpecBegin; |
5383 | |
5384 | |
5385 | |
5386 | UnresolvedSetIterator Best = SpecBegin; |
5387 | FunctionTemplateDecl *BestTemplate |
5388 | = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); |
5389 | assert(BestTemplate && "Not a function template specialization?"); |
5390 | for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { |
5391 | FunctionTemplateDecl *Challenger |
5392 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
5393 | assert(Challenger && "Not a function template specialization?"); |
5394 | if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
5395 | Loc, TPOC_Other, 0, 0), |
5396 | Challenger)) { |
5397 | Best = I; |
5398 | BestTemplate = Challenger; |
5399 | } |
5400 | } |
5401 | |
5402 | |
5403 | |
5404 | bool Ambiguous = false; |
5405 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
5406 | FunctionTemplateDecl *Challenger |
5407 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
5408 | if (I != Best && |
5409 | !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
5410 | Loc, TPOC_Other, 0, 0), |
5411 | BestTemplate)) { |
5412 | Ambiguous = true; |
5413 | break; |
5414 | } |
5415 | } |
5416 | |
5417 | if (!Ambiguous) { |
5418 | |
5419 | return Best; |
5420 | } |
5421 | |
5422 | |
5423 | if (Complain) { |
5424 | Diag(Loc, AmbigDiag); |
5425 | |
5426 | |
5427 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
5428 | PartialDiagnostic PD = CandidateDiag; |
5429 | const auto *FD = cast<FunctionDecl>(*I); |
5430 | PD << FD << getTemplateArgumentBindingsText( |
5431 | FD->getPrimaryTemplate()->getTemplateParameters(), |
5432 | *FD->getTemplateSpecializationArgs()); |
5433 | if (!TargetType.isNull()) |
5434 | HandleFunctionTypeMismatch(PD, FD->getType(), TargetType); |
5435 | Diag((*I)->getLocation(), PD); |
5436 | } |
5437 | } |
5438 | |
5439 | return SpecEnd; |
5440 | } |
5441 | |
5442 | |
5443 | |
5444 | |
5445 | |
5446 | |
5447 | |
5448 | |
5449 | template<typename TemplateLikeDecl> |
5450 | static bool isAtLeastAsSpecializedAs(Sema &S, QualType T1, QualType T2, |
5451 | TemplateLikeDecl *P2, |
5452 | TemplateDeductionInfo &Info) { |
5453 | |
5454 | |
5455 | |
5456 | |
5457 | |
5458 | |
5459 | |
5460 | |
5461 | |
5462 | |
5463 | |
5464 | |
5465 | |
5466 | |
5467 | |
5468 | |
5469 | |
5470 | |
5471 | |
5472 | |
5473 | |
5474 | |
5475 | |
5476 | |
5477 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
5478 | |
5479 | |
5480 | Deduced.resize(P2->getTemplateParameters()->size()); |
5481 | if (DeduceTemplateArgumentsByTypeMatch(S, P2->getTemplateParameters(), |
5482 | T2, T1, Info, Deduced, TDF_None, |
5483 | true)) |
5484 | return false; |
5485 | |
5486 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
5487 | Deduced.end()); |
5488 | Sema::InstantiatingTemplate Inst(S, Info.getLocation(), P2, DeducedArgs, |
5489 | Info); |
5490 | if (Inst.isInvalid()) |
5491 | return false; |
5492 | |
5493 | auto *TST1 = T1->castAs<TemplateSpecializationType>(); |
5494 | bool AtLeastAsSpecialized; |
5495 | S.runWithSufficientStackSpace(Info.getLocation(), [&] { |
5496 | AtLeastAsSpecialized = !FinishTemplateArgumentDeduction( |
5497 | S, P2, true, |
5498 | TemplateArgumentList(TemplateArgumentList::OnStack, |
5499 | TST1->template_arguments()), |
5500 | Deduced, Info); |
5501 | }); |
5502 | return AtLeastAsSpecialized; |
5503 | } |
5504 | |
5505 | |
5506 | |
5507 | |
5508 | |
5509 | |
5510 | |
5511 | |
5512 | |
5513 | |
5514 | |
5515 | ClassTemplatePartialSpecializationDecl * |
5516 | Sema::getMoreSpecializedPartialSpecialization( |
5517 | ClassTemplatePartialSpecializationDecl *PS1, |
5518 | ClassTemplatePartialSpecializationDecl *PS2, |
5519 | SourceLocation Loc) { |
5520 | QualType PT1 = PS1->getInjectedSpecializationType(); |
5521 | QualType PT2 = PS2->getInjectedSpecializationType(); |
5522 | |
5523 | TemplateDeductionInfo Info(Loc); |
5524 | bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Info); |
5525 | bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Info); |
5526 | |
5527 | if (!Better1 && !Better2) |
5528 | return nullptr; |
5529 | if (Better1 && Better2) { |
5530 | llvm::SmallVector<const Expr *, 3> AC1, AC2; |
5531 | PS1->getAssociatedConstraints(AC1); |
5532 | PS2->getAssociatedConstraints(AC2); |
5533 | bool AtLeastAsConstrained1, AtLeastAsConstrained2; |
5534 | if (IsAtLeastAsConstrained(PS1, AC1, PS2, AC2, AtLeastAsConstrained1)) |
5535 | return nullptr; |
5536 | if (IsAtLeastAsConstrained(PS2, AC2, PS1, AC1, AtLeastAsConstrained2)) |
5537 | return nullptr; |
5538 | if (AtLeastAsConstrained1 == AtLeastAsConstrained2) |
5539 | return nullptr; |
5540 | return AtLeastAsConstrained1 ? PS1 : PS2; |
5541 | } |
5542 | |
5543 | return Better1 ? PS1 : PS2; |
5544 | } |
5545 | |
5546 | bool Sema::isMoreSpecializedThanPrimary( |
5547 | ClassTemplatePartialSpecializationDecl *Spec, TemplateDeductionInfo &Info) { |
5548 | ClassTemplateDecl *Primary = Spec->getSpecializedTemplate(); |
5549 | QualType PrimaryT = Primary->getInjectedClassNameSpecialization(); |
5550 | QualType PartialT = Spec->getInjectedSpecializationType(); |
5551 | if (!isAtLeastAsSpecializedAs(*this, PartialT, PrimaryT, Primary, Info)) |
5552 | return false; |
5553 | if (!isAtLeastAsSpecializedAs(*this, PrimaryT, PartialT, Spec, Info)) |
5554 | return true; |
5555 | Info.clearSFINAEDiagnostic(); |
5556 | llvm::SmallVector<const Expr *, 3> PrimaryAC, SpecAC; |
5557 | Primary->getAssociatedConstraints(PrimaryAC); |
5558 | Spec->getAssociatedConstraints(SpecAC); |
5559 | bool AtLeastAsConstrainedPrimary, AtLeastAsConstrainedSpec; |
5560 | if (IsAtLeastAsConstrained(Spec, SpecAC, Primary, PrimaryAC, |
5561 | AtLeastAsConstrainedSpec)) |
5562 | return false; |
5563 | if (!AtLeastAsConstrainedSpec) |
5564 | return false; |
5565 | if (IsAtLeastAsConstrained(Primary, PrimaryAC, Spec, SpecAC, |
5566 | AtLeastAsConstrainedPrimary)) |
5567 | return false; |
5568 | return !AtLeastAsConstrainedPrimary; |
5569 | } |
5570 | |
5571 | VarTemplatePartialSpecializationDecl * |
5572 | Sema::getMoreSpecializedPartialSpecialization( |
5573 | VarTemplatePartialSpecializationDecl *PS1, |
5574 | VarTemplatePartialSpecializationDecl *PS2, SourceLocation Loc) { |
5575 | |
5576 | |
5577 | assert(PS1->getSpecializedTemplate() == PS2->getSpecializedTemplate() && |
5578 | "the partial specializations being compared should specialize" |
5579 | " the same template."); |
5580 | TemplateName Name(PS1->getSpecializedTemplate()); |
5581 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
5582 | QualType PT1 = Context.getTemplateSpecializationType( |
5583 | CanonTemplate, PS1->getTemplateArgs().asArray()); |
5584 | QualType PT2 = Context.getTemplateSpecializationType( |
5585 | CanonTemplate, PS2->getTemplateArgs().asArray()); |
5586 | |
5587 | TemplateDeductionInfo Info(Loc); |
5588 | bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Info); |
5589 | bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Info); |
5590 | |
5591 | if (!Better1 && !Better2) |
5592 | return nullptr; |
5593 | if (Better1 && Better2) { |
5594 | llvm::SmallVector<const Expr *, 3> AC1, AC2; |
5595 | PS1->getAssociatedConstraints(AC1); |
5596 | PS2->getAssociatedConstraints(AC2); |
5597 | bool AtLeastAsConstrained1, AtLeastAsConstrained2; |
5598 | if (IsAtLeastAsConstrained(PS1, AC1, PS2, AC2, AtLeastAsConstrained1)) |
5599 | return nullptr; |
5600 | if (IsAtLeastAsConstrained(PS2, AC2, PS1, AC1, AtLeastAsConstrained2)) |
5601 | return nullptr; |
5602 | if (AtLeastAsConstrained1 == AtLeastAsConstrained2) |
5603 | return nullptr; |
5604 | return AtLeastAsConstrained1 ? PS1 : PS2; |
5605 | } |
5606 | |
5607 | return Better1 ? PS1 : PS2; |
5608 | } |
5609 | |
5610 | bool Sema::isMoreSpecializedThanPrimary( |
5611 | VarTemplatePartialSpecializationDecl *Spec, TemplateDeductionInfo &Info) { |
5612 | TemplateDecl *Primary = Spec->getSpecializedTemplate(); |
5613 | |
5614 | |
5615 | SmallVector<TemplateArgument, 8> PrimaryArgs; |
5616 | Context.getInjectedTemplateArgs(Primary->getTemplateParameters(), |
5617 | PrimaryArgs); |
5618 | |
5619 | TemplateName CanonTemplate = |
5620 | Context.getCanonicalTemplateName(TemplateName(Primary)); |
5621 | QualType PrimaryT = Context.getTemplateSpecializationType( |
5622 | CanonTemplate, PrimaryArgs); |
5623 | QualType PartialT = Context.getTemplateSpecializationType( |
5624 | CanonTemplate, Spec->getTemplateArgs().asArray()); |
5625 | |
5626 | if (!isAtLeastAsSpecializedAs(*this, PartialT, PrimaryT, Primary, Info)) |
5627 | return false; |
5628 | if (!isAtLeastAsSpecializedAs(*this, PrimaryT, PartialT, Spec, Info)) |
5629 | return true; |
5630 | Info.clearSFINAEDiagnostic(); |
5631 | llvm::SmallVector<const Expr *, 3> PrimaryAC, SpecAC; |
5632 | Primary->getAssociatedConstraints(PrimaryAC); |
5633 | Spec->getAssociatedConstraints(SpecAC); |
5634 | bool AtLeastAsConstrainedPrimary, AtLeastAsConstrainedSpec; |
5635 | if (IsAtLeastAsConstrained(Spec, SpecAC, Primary, PrimaryAC, |
5636 | AtLeastAsConstrainedSpec)) |
5637 | return false; |
5638 | if (!AtLeastAsConstrainedSpec) |
5639 | return false; |
5640 | if (IsAtLeastAsConstrained(Primary, PrimaryAC, Spec, SpecAC, |
5641 | AtLeastAsConstrainedPrimary)) |
5642 | return false; |
5643 | return !AtLeastAsConstrainedPrimary; |
5644 | } |
5645 | |
5646 | bool Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs( |
5647 | TemplateParameterList *P, TemplateDecl *AArg, SourceLocation Loc) { |
5648 | |
5649 | |
5650 | |
5651 | |
5652 | |
5653 | |
5654 | |
5655 | |
5656 | |
5657 | |
5658 | |
5659 | TemplateName X = Context.getCanonicalTemplateName(TemplateName(AArg)); |
5660 | TemplateParameterList *A = AArg->getTemplateParameters(); |
5661 | |
5662 | |
5663 | |
5664 | |
5665 | SmallVector<TemplateArgument, 8> AArgs; |
5666 | Context.getInjectedTemplateArgs(A, AArgs); |
5667 | |
5668 | |
5669 | |
5670 | |
5671 | |
5672 | SmallVector<TemplateArgument, 4> PArgs; |
5673 | { |
5674 | SFINAETrap Trap(*this); |
5675 | |
5676 | Context.getInjectedTemplateArgs(P, PArgs); |
5677 | TemplateArgumentListInfo PArgList(P->getLAngleLoc(), |
5678 | P->getRAngleLoc()); |
5679 | for (unsigned I = 0, N = P->size(); I != N; ++I) { |
5680 | |
5681 | |
5682 | TemplateArgument Arg = PArgs[I]; |
5683 | if (Arg.getKind() == TemplateArgument::Pack) { |
5684 | assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion()); |
5685 | Arg = *Arg.pack_begin(); |
5686 | } |
5687 | PArgList.addArgument(getTrivialTemplateArgumentLoc( |
5688 | Arg, QualType(), P->getParam(I)->getLocation())); |
5689 | } |
5690 | PArgs.clear(); |
5691 | |
5692 | |
5693 | |
5694 | |
5695 | if (CheckTemplateArgumentList(AArg, Loc, PArgList, false, PArgs) || |
5696 | Trap.hasErrorOccurred()) |
5697 | return false; |
5698 | } |
5699 | |
5700 | QualType AType = Context.getTemplateSpecializationType(X, AArgs); |
5701 | QualType PType = Context.getTemplateSpecializationType(X, PArgs); |
5702 | |
5703 | |
5704 | |
5705 | |
5706 | TemplateDeductionInfo Info(Loc, A->getDepth()); |
5707 | return isAtLeastAsSpecializedAs(*this, PType, AType, AArg, Info); |
5708 | } |
5709 | |
5710 | namespace { |
5711 | struct MarkUsedTemplateParameterVisitor : |
5712 | RecursiveASTVisitor<MarkUsedTemplateParameterVisitor> { |
5713 | llvm::SmallBitVector &Used; |
5714 | unsigned Depth; |
5715 | |
5716 | MarkUsedTemplateParameterVisitor(llvm::SmallBitVector &Used, |
5717 | unsigned Depth) |
5718 | : Used(Used), Depth(Depth) { } |
5719 | |
5720 | bool VisitTemplateTypeParmType(TemplateTypeParmType *T) { |
5721 | if (T->getDepth() == Depth) |
5722 | Used[T->getIndex()] = true; |
5723 | return true; |
5724 | } |
5725 | |
5726 | bool TraverseTemplateName(TemplateName Template) { |
5727 | if (auto *TTP = |
5728 | dyn_cast<TemplateTemplateParmDecl>(Template.getAsTemplateDecl())) |
5729 | if (TTP->getDepth() == Depth) |
5730 | Used[TTP->getIndex()] = true; |
5731 | RecursiveASTVisitor<MarkUsedTemplateParameterVisitor>:: |
5732 | TraverseTemplateName(Template); |
5733 | return true; |
5734 | } |
5735 | |
5736 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
5737 | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
5738 | if (NTTP->getDepth() == Depth) |
5739 | Used[NTTP->getIndex()] = true; |
5740 | return true; |
5741 | } |
5742 | }; |
5743 | } |
5744 | |
5745 | |
5746 | |
5747 | static void |
5748 | MarkUsedTemplateParameters(ASTContext &Ctx, |
5749 | const Expr *E, |
5750 | bool OnlyDeduced, |
5751 | unsigned Depth, |
5752 | llvm::SmallBitVector &Used) { |
5753 | if (!OnlyDeduced) { |
5754 | MarkUsedTemplateParameterVisitor(Used, Depth) |
5755 | .TraverseStmt(const_cast<Expr *>(E)); |
5756 | return; |
5757 | } |
5758 | |
5759 | |
5760 | if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E)) |
5761 | E = Expansion->getPattern(); |
5762 | |
5763 | const NonTypeTemplateParmDecl *NTTP = getDeducedParameterFromExpr(E, Depth); |
5764 | if (!NTTP) |
5765 | return; |
5766 | |
5767 | if (NTTP->getDepth() == Depth) |
5768 | Used[NTTP->getIndex()] = true; |
5769 | |
5770 | |
5771 | |
5772 | if (Ctx.getLangOpts().CPlusPlus17) |
5773 | MarkUsedTemplateParameters(Ctx, NTTP->getType(), OnlyDeduced, Depth, Used); |
5774 | } |
5775 | |
5776 | |
5777 | |
5778 | static void |
5779 | MarkUsedTemplateParameters(ASTContext &Ctx, |
5780 | NestedNameSpecifier *NNS, |
5781 | bool OnlyDeduced, |
5782 | unsigned Depth, |
5783 | llvm::SmallBitVector &Used) { |
5784 | if (!NNS) |
5785 | return; |
5786 | |
5787 | MarkUsedTemplateParameters(Ctx, NNS->getPrefix(), OnlyDeduced, Depth, |
5788 | Used); |
5789 | MarkUsedTemplateParameters(Ctx, QualType(NNS->getAsType(), 0), |
5790 | OnlyDeduced, Depth, Used); |
5791 | } |
5792 | |
5793 | |
5794 | |
5795 | static void |
5796 | MarkUsedTemplateParameters(ASTContext &Ctx, |
5797 | TemplateName Name, |
5798 | bool OnlyDeduced, |
5799 | unsigned Depth, |
5800 | llvm::SmallBitVector &Used) { |
5801 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
5802 | if (TemplateTemplateParmDecl *TTP |
5803 | = dyn_cast<TemplateTemplateParmDecl>(Template)) { |
5804 | if (TTP->getDepth() == Depth) |
5805 | Used[TTP->getIndex()] = true; |
5806 | } |
5807 | return; |
5808 | } |
5809 | |
5810 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) |
5811 | MarkUsedTemplateParameters(Ctx, QTN->getQualifier(), OnlyDeduced, |
5812 | Depth, Used); |
5813 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) |
5814 | MarkUsedTemplateParameters(Ctx, DTN->getQualifier(), OnlyDeduced, |
5815 | Depth, Used); |
5816 | } |
5817 | |
5818 | |
5819 | |
5820 | static void |
5821 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
5822 | bool OnlyDeduced, |
5823 | unsigned Depth, |
5824 | llvm::SmallBitVector &Used) { |
5825 | if (T.isNull()) |
5826 | return; |
5827 | |
5828 | |
5829 | if (!T->isDependentType()) |
5830 | return; |
5831 | |
5832 | T = Ctx.getCanonicalType(T); |
5833 | switch (T->getTypeClass()) { |
5834 | case Type::Pointer: |
5835 | MarkUsedTemplateParameters(Ctx, |
5836 | cast<PointerType>(T)->getPointeeType(), |
5837 | OnlyDeduced, |
5838 | Depth, |
5839 | Used); |
5840 | break; |
5841 | |
5842 | case Type::BlockPointer: |
5843 | MarkUsedTemplateParameters(Ctx, |
5844 | cast<BlockPointerType>(T)->getPointeeType(), |
5845 | OnlyDeduced, |
5846 | Depth, |
5847 | Used); |
5848 | break; |
5849 | |
5850 | case Type::LValueReference: |
5851 | case Type::RValueReference: |
5852 | MarkUsedTemplateParameters(Ctx, |
5853 | cast<ReferenceType>(T)->getPointeeType(), |
5854 | OnlyDeduced, |
5855 | Depth, |
5856 | Used); |
5857 | break; |
5858 | |
5859 | case Type::MemberPointer: { |
5860 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
5861 | MarkUsedTemplateParameters(Ctx, MemPtr->getPointeeType(), OnlyDeduced, |
5862 | Depth, Used); |
5863 | MarkUsedTemplateParameters(Ctx, QualType(MemPtr->getClass(), 0), |
5864 | OnlyDeduced, Depth, Used); |
5865 | break; |
5866 | } |
5867 | |
5868 | case Type::DependentSizedArray: |
5869 | MarkUsedTemplateParameters(Ctx, |
5870 | cast<DependentSizedArrayType>(T)->getSizeExpr(), |
5871 | OnlyDeduced, Depth, Used); |
5872 | |
5873 | LLVM_FALLTHROUGH; |
5874 | |
5875 | case Type::ConstantArray: |
5876 | case Type::IncompleteArray: |
5877 | MarkUsedTemplateParameters(Ctx, |
5878 | cast<ArrayType>(T)->getElementType(), |
5879 | OnlyDeduced, Depth, Used); |
5880 | break; |
5881 | |
5882 | case Type::Vector: |
5883 | case Type::ExtVector: |
5884 | MarkUsedTemplateParameters(Ctx, |
5885 | cast<VectorType>(T)->getElementType(), |
5886 | OnlyDeduced, Depth, Used); |
5887 | break; |
5888 | |
5889 | case Type::DependentVector: { |
5890 | const auto *VecType = cast<DependentVectorType>(T); |
5891 | MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, |
5892 | Depth, Used); |
5893 | MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, Depth, |
5894 | Used); |
5895 | break; |
5896 | } |
5897 | case Type::DependentSizedExtVector: { |
5898 | const DependentSizedExtVectorType *VecType |
5899 | = cast<DependentSizedExtVectorType>(T); |
5900 | MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, |
5901 | Depth, Used); |
5902 | MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, |
5903 | Depth, Used); |
5904 | break; |
5905 | } |
5906 | |
5907 | case Type::DependentAddressSpace: { |
5908 | const DependentAddressSpaceType *DependentASType = |
5909 | cast<DependentAddressSpaceType>(T); |
5910 | MarkUsedTemplateParameters(Ctx, DependentASType->getPointeeType(), |
5911 | OnlyDeduced, Depth, Used); |
5912 | MarkUsedTemplateParameters(Ctx, |
5913 | DependentASType->getAddrSpaceExpr(), |
5914 | OnlyDeduced, Depth, Used); |
5915 | break; |
5916 | } |
5917 | |
5918 | case Type::ConstantMatrix: { |
5919 | const ConstantMatrixType *MatType = cast<ConstantMatrixType>(T); |
5920 | MarkUsedTemplateParameters(Ctx, MatType->getElementType(), OnlyDeduced, |
5921 | Depth, Used); |
5922 | break; |
5923 | } |
5924 | |
5925 | case Type::DependentSizedMatrix: { |
5926 | const DependentSizedMatrixType *MatType = cast<DependentSizedMatrixType>(T); |
5927 | MarkUsedTemplateParameters(Ctx, MatType->getElementType(), OnlyDeduced, |
5928 | Depth, Used); |
5929 | MarkUsedTemplateParameters(Ctx, MatType->getRowExpr(), OnlyDeduced, Depth, |
5930 | Used); |
5931 | MarkUsedTemplateParameters(Ctx, MatType->getColumnExpr(), OnlyDeduced, |
5932 | Depth, Used); |
5933 | break; |
5934 | } |
5935 | |
5936 | case Type::FunctionProto: { |
5937 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
5938 | MarkUsedTemplateParameters(Ctx, Proto->getReturnType(), OnlyDeduced, Depth, |
5939 | Used); |
5940 | for (unsigned I = 0, N = Proto->getNumParams(); I != N; ++I) { |
5941 | |
5942 | |
5943 | |
5944 | |
5945 | if (!OnlyDeduced || I + 1 == N || |
5946 | !Proto->getParamType(I)->getAs<PackExpansionType>()) { |
5947 | MarkUsedTemplateParameters(Ctx, Proto->getParamType(I), OnlyDeduced, |
5948 | Depth, Used); |
5949 | } else { |
5950 | |
5951 | |
5952 | |
5953 | |
5954 | |
5955 | |
5956 | } |
5957 | } |
5958 | if (auto *E = Proto->getNoexceptExpr()) |
5959 | MarkUsedTemplateParameters(Ctx, E, OnlyDeduced, Depth, Used); |
5960 | break; |
5961 | } |
5962 | |
5963 | case Type::TemplateTypeParm: { |
5964 | const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); |
5965 | if (TTP->getDepth() == Depth) |
5966 | Used[TTP->getIndex()] = true; |
5967 | break; |
5968 | } |
5969 | |
5970 | case Type::SubstTemplateTypeParmPack: { |
5971 | const SubstTemplateTypeParmPackType *Subst |
5972 | = cast<SubstTemplateTypeParmPackType>(T); |
5973 | MarkUsedTemplateParameters(Ctx, |
5974 | QualType(Subst->getReplacedParameter(), 0), |
5975 | OnlyDeduced, Depth, Used); |
5976 | MarkUsedTemplateParameters(Ctx, Subst->getArgumentPack(), |
5977 | OnlyDeduced, Depth, Used); |
5978 | break; |
5979 | } |
5980 | |
5981 | case Type::InjectedClassName: |
5982 | T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); |
5983 | LLVM_FALLTHROUGH; |
5984 | |
5985 | case Type::TemplateSpecialization: { |
5986 | const TemplateSpecializationType *Spec |
5987 | = cast<TemplateSpecializationType>(T); |
5988 | MarkUsedTemplateParameters(Ctx, Spec->getTemplateName(), OnlyDeduced, |
5989 | Depth, Used); |
5990 | |
5991 | |
5992 | |
5993 | |
5994 | |
5995 | if (OnlyDeduced && |
5996 | hasPackExpansionBeforeEnd(Spec->template_arguments())) |
5997 | break; |
5998 | |
5999 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
6000 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
6001 | Used); |
6002 | break; |
6003 | } |
6004 | |
6005 | case Type::Complex: |
6006 | if (!OnlyDeduced) |
6007 | MarkUsedTemplateParameters(Ctx, |
6008 | cast<ComplexType>(T)->getElementType(), |
6009 | OnlyDeduced, Depth, Used); |
6010 | break; |
6011 | |
6012 | case Type::Atomic: |
6013 | if (!OnlyDeduced) |
6014 | MarkUsedTemplateParameters(Ctx, |
6015 | cast<AtomicType>(T)->getValueType(), |
6016 | OnlyDeduced, Depth, Used); |
6017 | break; |
6018 | |
6019 | case Type::DependentName: |
6020 | if (!OnlyDeduced) |
6021 | MarkUsedTemplateParameters(Ctx, |
6022 | cast<DependentNameType>(T)->getQualifier(), |
6023 | OnlyDeduced, Depth, Used); |
6024 | break; |
6025 | |
6026 | case Type::DependentTemplateSpecialization: { |
6027 | |
6028 | |
6029 | |
6030 | |
6031 | |
6032 | |
6033 | |
6034 | |
6035 | |
6036 | if (OnlyDeduced) |
6037 | break; |
6038 | |
6039 | const DependentTemplateSpecializationType *Spec |
6040 | = cast<DependentTemplateSpecializationType>(T); |
6041 | |
6042 | MarkUsedTemplateParameters(Ctx, Spec->getQualifier(), |
6043 | OnlyDeduced, Depth, Used); |
6044 | |
6045 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
6046 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
6047 | Used); |
6048 | break; |
6049 | } |
6050 | |
6051 | case Type::TypeOf: |
6052 | if (!OnlyDeduced) |
6053 | MarkUsedTemplateParameters(Ctx, |
6054 | cast<TypeOfType>(T)->getUnderlyingType(), |
6055 | OnlyDeduced, Depth, Used); |
6056 | break; |
6057 | |
6058 | case Type::TypeOfExpr: |
6059 | if (!OnlyDeduced) |
6060 | MarkUsedTemplateParameters(Ctx, |
6061 | cast<TypeOfExprType>(T)->getUnderlyingExpr(), |
6062 | OnlyDeduced, Depth, Used); |
6063 | break; |
6064 | |
6065 | case Type::Decltype: |
6066 | if (!OnlyDeduced) |
6067 | MarkUsedTemplateParameters(Ctx, |
6068 | cast<DecltypeType>(T)->getUnderlyingExpr(), |
6069 | OnlyDeduced, Depth, Used); |
6070 | break; |
6071 | |
6072 | case Type::UnaryTransform: |
6073 | if (!OnlyDeduced) |
6074 | MarkUsedTemplateParameters(Ctx, |
6075 | cast<UnaryTransformType>(T)->getUnderlyingType(), |
6076 | OnlyDeduced, Depth, Used); |
6077 | break; |
6078 | |
6079 | case Type::PackExpansion: |
6080 | MarkUsedTemplateParameters(Ctx, |
6081 | cast<PackExpansionType>(T)->getPattern(), |
6082 | OnlyDeduced, Depth, Used); |
6083 | break; |
6084 | |
6085 | case Type::Auto: |
6086 | case Type::DeducedTemplateSpecialization: |
6087 | MarkUsedTemplateParameters(Ctx, |
6088 | cast<DeducedType>(T)->getDeducedType(), |
6089 | OnlyDeduced, Depth, Used); |
6090 | break; |
6091 | case Type::DependentExtInt: |
6092 | MarkUsedTemplateParameters(Ctx, |
6093 | cast<DependentExtIntType>(T)->getNumBitsExpr(), |
6094 | OnlyDeduced, Depth, Used); |
6095 | break; |
6096 | |
6097 | |
6098 | case Type::Builtin: |
6099 | case Type::VariableArray: |
6100 | case Type::FunctionNoProto: |
6101 | case Type::Record: |
6102 | case Type::Enum: |
6103 | case Type::ObjCInterface: |
6104 | case Type::ObjCObject: |
6105 | case Type::ObjCObjectPointer: |
6106 | case Type::UnresolvedUsing: |
6107 | case Type::Pipe: |
6108 | case Type::ExtInt: |
6109 | #define TYPE(Class, Base) |
6110 | #define ABSTRACT_TYPE(Class, Base) |
6111 | #define DEPENDENT_TYPE(Class, Base) |
6112 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
6113 | #include "clang/AST/TypeNodes.inc" |
6114 | break; |
6115 | } |
6116 | } |
6117 | |
6118 | |
6119 | |
6120 | static void |
6121 | MarkUsedTemplateParameters(ASTContext &Ctx, |
6122 | const TemplateArgument &TemplateArg, |
6123 | bool OnlyDeduced, |
6124 | unsigned Depth, |
6125 | llvm::SmallBitVector &Used) { |
6126 | switch (TemplateArg.getKind()) { |
6127 | case TemplateArgument::Null: |
6128 | case TemplateArgument::Integral: |
6129 | case TemplateArgument::Declaration: |
6130 | break; |
6131 | |
6132 | case TemplateArgument::NullPtr: |
6133 | MarkUsedTemplateParameters(Ctx, TemplateArg.getNullPtrType(), OnlyDeduced, |
6134 | Depth, Used); |
6135 | break; |
6136 | |
6137 | case TemplateArgument::Type: |
6138 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsType(), OnlyDeduced, |
6139 | Depth, Used); |
6140 | break; |
6141 | |
6142 | case TemplateArgument::Template: |
6143 | case TemplateArgument::TemplateExpansion: |
6144 | MarkUsedTemplateParameters(Ctx, |
6145 | TemplateArg.getAsTemplateOrTemplatePattern(), |
6146 | OnlyDeduced, Depth, Used); |
6147 | break; |
6148 | |
6149 | case TemplateArgument::Expression: |
6150 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsExpr(), OnlyDeduced, |
6151 | Depth, Used); |
6152 | break; |
6153 | |
6154 | case TemplateArgument::Pack: |
6155 | for (const auto &P : TemplateArg.pack_elements()) |
6156 | MarkUsedTemplateParameters(Ctx, P, OnlyDeduced, Depth, Used); |
6157 | break; |
6158 | } |
6159 | } |
6160 | |
6161 | |
6162 | |
6163 | |
6164 | |
6165 | |
6166 | |
6167 | |
6168 | void |
6169 | Sema::MarkUsedTemplateParameters(const Expr *E, bool OnlyDeduced, |
6170 | unsigned Depth, |
6171 | llvm::SmallBitVector &Used) { |
6172 | ::MarkUsedTemplateParameters(Context, E, OnlyDeduced, Depth, Used); |
6173 | } |
6174 | |
6175 | |
6176 | |
6177 | |
6178 | |
6179 | |
6180 | |
6181 | |
6182 | |
6183 | |
6184 | void |
6185 | Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
6186 | bool OnlyDeduced, unsigned Depth, |
6187 | llvm::SmallBitVector &Used) { |
6188 | |
6189 | |
6190 | |
6191 | |
6192 | if (OnlyDeduced && |
6193 | hasPackExpansionBeforeEnd(TemplateArgs.asArray())) |
6194 | return; |
6195 | |
6196 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
6197 | ::MarkUsedTemplateParameters(Context, TemplateArgs[I], OnlyDeduced, |
6198 | Depth, Used); |
6199 | } |
6200 | |
6201 | |
6202 | |
6203 | void Sema::MarkDeducedTemplateParameters( |
6204 | ASTContext &Ctx, const FunctionTemplateDecl *FunctionTemplate, |
6205 | llvm::SmallBitVector &Deduced) { |
6206 | TemplateParameterList *TemplateParams |
6207 | = FunctionTemplate->getTemplateParameters(); |
6208 | Deduced.clear(); |
6209 | Deduced.resize(TemplateParams->size()); |
6210 | |
6211 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
6212 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) |
6213 | ::MarkUsedTemplateParameters(Ctx, Function->getParamDecl(I)->getType(), |
6214 | true, TemplateParams->getDepth(), Deduced); |
6215 | } |
6216 | |
6217 | bool hasDeducibleTemplateParameters(Sema &S, |
6218 | FunctionTemplateDecl *FunctionTemplate, |
6219 | QualType T) { |
6220 | if (!T->isDependentType()) |
6221 | return false; |
6222 | |
6223 | TemplateParameterList *TemplateParams |
6224 | = FunctionTemplate->getTemplateParameters(); |
6225 | llvm::SmallBitVector Deduced(TemplateParams->size()); |
6226 | ::MarkUsedTemplateParameters(S.Context, T, true, TemplateParams->getDepth(), |
6227 | Deduced); |
6228 | |
6229 | return Deduced.any(); |
6230 | } |