/src/llvm-project/clang/lib/Sema/SemaTemplateInstantiate.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/ |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | //===----------------------------------------------------------------------===/ |
7 | | // |
8 | | // This file implements C++ template instantiation. |
9 | | // |
10 | | //===----------------------------------------------------------------------===/ |
11 | | |
12 | | #include "TreeTransform.h" |
13 | | #include "clang/AST/ASTConcept.h" |
14 | | #include "clang/AST/ASTConsumer.h" |
15 | | #include "clang/AST/ASTContext.h" |
16 | | #include "clang/AST/ASTLambda.h" |
17 | | #include "clang/AST/ASTMutationListener.h" |
18 | | #include "clang/AST/DeclBase.h" |
19 | | #include "clang/AST/DeclTemplate.h" |
20 | | #include "clang/AST/Expr.h" |
21 | | #include "clang/AST/ExprConcepts.h" |
22 | | #include "clang/AST/PrettyDeclStackTrace.h" |
23 | | #include "clang/AST/Type.h" |
24 | | #include "clang/AST/TypeVisitor.h" |
25 | | #include "clang/Basic/LangOptions.h" |
26 | | #include "clang/Basic/Stack.h" |
27 | | #include "clang/Basic/TargetInfo.h" |
28 | | #include "clang/Sema/DeclSpec.h" |
29 | | #include "clang/Sema/EnterExpressionEvaluationContext.h" |
30 | | #include "clang/Sema/Initialization.h" |
31 | | #include "clang/Sema/Lookup.h" |
32 | | #include "clang/Sema/Sema.h" |
33 | | #include "clang/Sema/SemaConcept.h" |
34 | | #include "clang/Sema/SemaInternal.h" |
35 | | #include "clang/Sema/Template.h" |
36 | | #include "clang/Sema/TemplateDeduction.h" |
37 | | #include "clang/Sema/TemplateInstCallback.h" |
38 | | #include "llvm/ADT/StringExtras.h" |
39 | | #include "llvm/Support/ErrorHandling.h" |
40 | | #include "llvm/Support/TimeProfiler.h" |
41 | | #include <optional> |
42 | | |
43 | | using namespace clang; |
44 | | using namespace sema; |
45 | | |
46 | | //===----------------------------------------------------------------------===/ |
47 | | // Template Instantiation Support |
48 | | //===----------------------------------------------------------------------===/ |
49 | | |
50 | | namespace { |
51 | | namespace TemplateInstArgsHelpers { |
52 | | struct Response { |
53 | | const Decl *NextDecl = nullptr; |
54 | | bool IsDone = false; |
55 | | bool ClearRelativeToPrimary = true; |
56 | 0 | static Response Done() { |
57 | 0 | Response R; |
58 | 0 | R.IsDone = true; |
59 | 0 | return R; |
60 | 0 | } |
61 | 0 | static Response ChangeDecl(const Decl *ND) { |
62 | 0 | Response R; |
63 | 0 | R.NextDecl = ND; |
64 | 0 | return R; |
65 | 0 | } |
66 | 0 | static Response ChangeDecl(const DeclContext *Ctx) { |
67 | 0 | Response R; |
68 | 0 | R.NextDecl = Decl::castFromDeclContext(Ctx); |
69 | 0 | return R; |
70 | 0 | } |
71 | | |
72 | 0 | static Response UseNextDecl(const Decl *CurDecl) { |
73 | 0 | return ChangeDecl(CurDecl->getDeclContext()); |
74 | 0 | } |
75 | | |
76 | 0 | static Response DontClearRelativeToPrimaryNextDecl(const Decl *CurDecl) { |
77 | 0 | Response R = Response::UseNextDecl(CurDecl); |
78 | 0 | R.ClearRelativeToPrimary = false; |
79 | 0 | return R; |
80 | 0 | } |
81 | | }; |
82 | | // Add template arguments from a variable template instantiation. |
83 | | Response |
84 | | HandleVarTemplateSpec(const VarTemplateSpecializationDecl *VarTemplSpec, |
85 | | MultiLevelTemplateArgumentList &Result, |
86 | 0 | bool SkipForSpecialization) { |
87 | | // For a class-scope explicit specialization, there are no template arguments |
88 | | // at this level, but there may be enclosing template arguments. |
89 | 0 | if (VarTemplSpec->isClassScopeExplicitSpecialization()) |
90 | 0 | return Response::DontClearRelativeToPrimaryNextDecl(VarTemplSpec); |
91 | | |
92 | | // We're done when we hit an explicit specialization. |
93 | 0 | if (VarTemplSpec->getSpecializationKind() == TSK_ExplicitSpecialization && |
94 | 0 | !isa<VarTemplatePartialSpecializationDecl>(VarTemplSpec)) |
95 | 0 | return Response::Done(); |
96 | | |
97 | | // If this variable template specialization was instantiated from a |
98 | | // specialized member that is a variable template, we're done. |
99 | 0 | assert(VarTemplSpec->getSpecializedTemplate() && "No variable template?"); |
100 | 0 | llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *> |
101 | 0 | Specialized = VarTemplSpec->getSpecializedTemplateOrPartial(); |
102 | 0 | if (VarTemplatePartialSpecializationDecl *Partial = |
103 | 0 | Specialized.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
104 | 0 | if (!SkipForSpecialization) |
105 | 0 | Result.addOuterTemplateArguments( |
106 | 0 | Partial, VarTemplSpec->getTemplateInstantiationArgs().asArray(), |
107 | 0 | /*Final=*/false); |
108 | 0 | if (Partial->isMemberSpecialization()) |
109 | 0 | return Response::Done(); |
110 | 0 | } else { |
111 | 0 | VarTemplateDecl *Tmpl = Specialized.get<VarTemplateDecl *>(); |
112 | 0 | if (!SkipForSpecialization) |
113 | 0 | Result.addOuterTemplateArguments( |
114 | 0 | Tmpl, VarTemplSpec->getTemplateInstantiationArgs().asArray(), |
115 | 0 | /*Final=*/false); |
116 | 0 | if (Tmpl->isMemberSpecialization()) |
117 | 0 | return Response::Done(); |
118 | 0 | } |
119 | 0 | return Response::DontClearRelativeToPrimaryNextDecl(VarTemplSpec); |
120 | 0 | } |
121 | | |
122 | | // If we have a template template parameter with translation unit context, |
123 | | // then we're performing substitution into a default template argument of |
124 | | // this template template parameter before we've constructed the template |
125 | | // that will own this template template parameter. In this case, we |
126 | | // use empty template parameter lists for all of the outer templates |
127 | | // to avoid performing any substitutions. |
128 | | Response |
129 | | HandleDefaultTempArgIntoTempTempParam(const TemplateTemplateParmDecl *TTP, |
130 | 0 | MultiLevelTemplateArgumentList &Result) { |
131 | 0 | for (unsigned I = 0, N = TTP->getDepth() + 1; I != N; ++I) |
132 | 0 | Result.addOuterTemplateArguments(std::nullopt); |
133 | 0 | return Response::Done(); |
134 | 0 | } |
135 | | |
136 | | Response HandlePartialClassTemplateSpec( |
137 | | const ClassTemplatePartialSpecializationDecl *PartialClassTemplSpec, |
138 | 0 | MultiLevelTemplateArgumentList &Result, bool SkipForSpecialization) { |
139 | 0 | if (!SkipForSpecialization) |
140 | 0 | Result.addOuterRetainedLevels(PartialClassTemplSpec->getTemplateDepth()); |
141 | 0 | return Response::Done(); |
142 | 0 | } |
143 | | |
144 | | // Add template arguments from a class template instantiation. |
145 | | Response |
146 | | HandleClassTemplateSpec(const ClassTemplateSpecializationDecl *ClassTemplSpec, |
147 | | MultiLevelTemplateArgumentList &Result, |
148 | 0 | bool SkipForSpecialization) { |
149 | 0 | if (!ClassTemplSpec->isClassScopeExplicitSpecialization()) { |
150 | | // We're done when we hit an explicit specialization. |
151 | 0 | if (ClassTemplSpec->getSpecializationKind() == TSK_ExplicitSpecialization && |
152 | 0 | !isa<ClassTemplatePartialSpecializationDecl>(ClassTemplSpec)) |
153 | 0 | return Response::Done(); |
154 | | |
155 | 0 | if (!SkipForSpecialization) |
156 | 0 | Result.addOuterTemplateArguments( |
157 | 0 | const_cast<ClassTemplateSpecializationDecl *>(ClassTemplSpec), |
158 | 0 | ClassTemplSpec->getTemplateInstantiationArgs().asArray(), |
159 | 0 | /*Final=*/false); |
160 | | |
161 | | // If this class template specialization was instantiated from a |
162 | | // specialized member that is a class template, we're done. |
163 | 0 | assert(ClassTemplSpec->getSpecializedTemplate() && "No class template?"); |
164 | 0 | if (ClassTemplSpec->getSpecializedTemplate()->isMemberSpecialization()) |
165 | 0 | return Response::Done(); |
166 | | |
167 | | // If this was instantiated from a partial template specialization, we need |
168 | | // to get the next level of declaration context from the partial |
169 | | // specialization, as the ClassTemplateSpecializationDecl's |
170 | | // DeclContext/LexicalDeclContext will be for the primary template. |
171 | 0 | if (auto *InstFromPartialTempl = ClassTemplSpec->getSpecializedTemplateOrPartial() |
172 | 0 | .dyn_cast<ClassTemplatePartialSpecializationDecl *>()) |
173 | 0 | return Response::ChangeDecl(InstFromPartialTempl->getLexicalDeclContext()); |
174 | 0 | } |
175 | 0 | return Response::UseNextDecl(ClassTemplSpec); |
176 | 0 | } |
177 | | |
178 | | Response HandleFunction(const FunctionDecl *Function, |
179 | | MultiLevelTemplateArgumentList &Result, |
180 | | const FunctionDecl *Pattern, bool RelativeToPrimary, |
181 | 0 | bool ForConstraintInstantiation) { |
182 | | // Add template arguments from a function template specialization. |
183 | 0 | if (!RelativeToPrimary && |
184 | 0 | Function->getTemplateSpecializationKindForInstantiation() == |
185 | 0 | TSK_ExplicitSpecialization) |
186 | 0 | return Response::Done(); |
187 | | |
188 | 0 | if (!RelativeToPrimary && |
189 | 0 | Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) { |
190 | | // This is an implicit instantiation of an explicit specialization. We |
191 | | // don't get any template arguments from this function but might get |
192 | | // some from an enclosing template. |
193 | 0 | return Response::UseNextDecl(Function); |
194 | 0 | } else if (const TemplateArgumentList *TemplateArgs = |
195 | 0 | Function->getTemplateSpecializationArgs()) { |
196 | | // Add the template arguments for this specialization. |
197 | 0 | Result.addOuterTemplateArguments(const_cast<FunctionDecl *>(Function), |
198 | 0 | TemplateArgs->asArray(), |
199 | 0 | /*Final=*/false); |
200 | | |
201 | | // If this function was instantiated from a specialized member that is |
202 | | // a function template, we're done. |
203 | 0 | assert(Function->getPrimaryTemplate() && "No function template?"); |
204 | 0 | if (Function->getPrimaryTemplate()->isMemberSpecialization()) |
205 | 0 | return Response::Done(); |
206 | | |
207 | | // If this function is a generic lambda specialization, we are done. |
208 | 0 | if (!ForConstraintInstantiation && |
209 | 0 | isGenericLambdaCallOperatorOrStaticInvokerSpecialization(Function)) |
210 | 0 | return Response::Done(); |
211 | |
|
212 | 0 | } else if (Function->getDescribedFunctionTemplate()) { |
213 | 0 | assert( |
214 | 0 | (ForConstraintInstantiation || Result.getNumSubstitutedLevels() == 0) && |
215 | 0 | "Outer template not instantiated?"); |
216 | 0 | } |
217 | | // If this is a friend or local declaration and it declares an entity at |
218 | | // namespace scope, take arguments from its lexical parent |
219 | | // instead of its semantic parent, unless of course the pattern we're |
220 | | // instantiating actually comes from the file's context! |
221 | 0 | if ((Function->getFriendObjectKind() || Function->isLocalExternDecl()) && |
222 | 0 | Function->getNonTransparentDeclContext()->isFileContext() && |
223 | 0 | (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) { |
224 | 0 | return Response::ChangeDecl(Function->getLexicalDeclContext()); |
225 | 0 | } |
226 | | |
227 | 0 | if (ForConstraintInstantiation && Function->getFriendObjectKind()) |
228 | 0 | return Response::ChangeDecl(Function->getLexicalDeclContext()); |
229 | 0 | return Response::UseNextDecl(Function); |
230 | 0 | } |
231 | | |
232 | | Response HandleFunctionTemplateDecl(const FunctionTemplateDecl *FTD, |
233 | 0 | MultiLevelTemplateArgumentList &Result) { |
234 | 0 | if (!isa<ClassTemplateSpecializationDecl>(FTD->getDeclContext())) { |
235 | 0 | Result.addOuterTemplateArguments( |
236 | 0 | const_cast<FunctionTemplateDecl *>(FTD), |
237 | 0 | const_cast<FunctionTemplateDecl *>(FTD)->getInjectedTemplateArgs(), |
238 | 0 | /*Final=*/false); |
239 | |
|
240 | 0 | NestedNameSpecifier *NNS = FTD->getTemplatedDecl()->getQualifier(); |
241 | |
|
242 | 0 | while (const Type *Ty = NNS ? NNS->getAsType() : nullptr) { |
243 | 0 | if (NNS->isInstantiationDependent()) { |
244 | 0 | if (const auto *TSTy = Ty->getAs<TemplateSpecializationType>()) |
245 | 0 | Result.addOuterTemplateArguments( |
246 | 0 | const_cast<FunctionTemplateDecl *>(FTD), TSTy->template_arguments(), |
247 | 0 | /*Final=*/false); |
248 | 0 | } |
249 | |
|
250 | 0 | NNS = NNS->getPrefix(); |
251 | 0 | } |
252 | 0 | } |
253 | |
|
254 | 0 | return Response::ChangeDecl(FTD->getLexicalDeclContext()); |
255 | 0 | } |
256 | | |
257 | | Response HandleRecordDecl(const CXXRecordDecl *Rec, |
258 | | MultiLevelTemplateArgumentList &Result, |
259 | | ASTContext &Context, |
260 | 0 | bool ForConstraintInstantiation) { |
261 | 0 | if (ClassTemplateDecl *ClassTemplate = Rec->getDescribedClassTemplate()) { |
262 | 0 | assert( |
263 | 0 | (ForConstraintInstantiation || Result.getNumSubstitutedLevels() == 0) && |
264 | 0 | "Outer template not instantiated?"); |
265 | 0 | if (ClassTemplate->isMemberSpecialization()) |
266 | 0 | return Response::Done(); |
267 | 0 | if (ForConstraintInstantiation) |
268 | 0 | Result.addOuterTemplateArguments(const_cast<CXXRecordDecl *>(Rec), |
269 | 0 | ClassTemplate->getInjectedTemplateArgs(), |
270 | 0 | /*Final=*/false); |
271 | 0 | } |
272 | | |
273 | 0 | if (const MemberSpecializationInfo *MSInfo = |
274 | 0 | Rec->getMemberSpecializationInfo()) |
275 | 0 | if (MSInfo->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
276 | 0 | return Response::Done(); |
277 | | |
278 | 0 | bool IsFriend = Rec->getFriendObjectKind() || |
279 | 0 | (Rec->getDescribedClassTemplate() && |
280 | 0 | Rec->getDescribedClassTemplate()->getFriendObjectKind()); |
281 | 0 | if (ForConstraintInstantiation && IsFriend && |
282 | 0 | Rec->getNonTransparentDeclContext()->isFileContext()) { |
283 | 0 | return Response::ChangeDecl(Rec->getLexicalDeclContext()); |
284 | 0 | } |
285 | | |
286 | | // This is to make sure we pick up the VarTemplateSpecializationDecl that this |
287 | | // lambda is defined inside of. |
288 | 0 | if (Rec->isLambda()) |
289 | 0 | if (const Decl *LCD = Rec->getLambdaContextDecl()) |
290 | 0 | return Response::ChangeDecl(LCD); |
291 | | |
292 | 0 | return Response::UseNextDecl(Rec); |
293 | 0 | } |
294 | | |
295 | | Response HandleImplicitConceptSpecializationDecl( |
296 | | const ImplicitConceptSpecializationDecl *CSD, |
297 | 0 | MultiLevelTemplateArgumentList &Result) { |
298 | 0 | Result.addOuterTemplateArguments( |
299 | 0 | const_cast<ImplicitConceptSpecializationDecl *>(CSD), |
300 | 0 | CSD->getTemplateArguments(), |
301 | 0 | /*Final=*/false); |
302 | 0 | return Response::UseNextDecl(CSD); |
303 | 0 | } |
304 | | |
305 | 0 | Response HandleGenericDeclContext(const Decl *CurDecl) { |
306 | 0 | return Response::UseNextDecl(CurDecl); |
307 | 0 | } |
308 | | } // namespace TemplateInstArgsHelpers |
309 | | } // namespace |
310 | | |
311 | | /// Retrieve the template argument list(s) that should be used to |
312 | | /// instantiate the definition of the given declaration. |
313 | | /// |
314 | | /// \param ND the declaration for which we are computing template instantiation |
315 | | /// arguments. |
316 | | /// |
317 | | /// \param DC In the event we don't HAVE a declaration yet, we instead provide |
318 | | /// the decl context where it will be created. In this case, the `Innermost` |
319 | | /// should likely be provided. If ND is non-null, this is ignored. |
320 | | /// |
321 | | /// \param Innermost if non-NULL, specifies a template argument list for the |
322 | | /// template declaration passed as ND. |
323 | | /// |
324 | | /// \param RelativeToPrimary true if we should get the template |
325 | | /// arguments relative to the primary template, even when we're |
326 | | /// dealing with a specialization. This is only relevant for function |
327 | | /// template specializations. |
328 | | /// |
329 | | /// \param Pattern If non-NULL, indicates the pattern from which we will be |
330 | | /// instantiating the definition of the given declaration, \p ND. This is |
331 | | /// used to determine the proper set of template instantiation arguments for |
332 | | /// friend function template specializations. |
333 | | /// |
334 | | /// \param ForConstraintInstantiation when collecting arguments, |
335 | | /// ForConstraintInstantiation indicates we should continue looking when |
336 | | /// encountering a lambda generic call operator, and continue looking for |
337 | | /// arguments on an enclosing class template. |
338 | | |
339 | | MultiLevelTemplateArgumentList Sema::getTemplateInstantiationArgs( |
340 | | const NamedDecl *ND, const DeclContext *DC, bool Final, |
341 | | const TemplateArgumentList *Innermost, bool RelativeToPrimary, |
342 | | const FunctionDecl *Pattern, bool ForConstraintInstantiation, |
343 | 0 | bool SkipForSpecialization) { |
344 | 0 | assert((ND || DC) && "Can't find arguments for a decl if one isn't provided"); |
345 | | // Accumulate the set of template argument lists in this structure. |
346 | 0 | MultiLevelTemplateArgumentList Result; |
347 | |
|
348 | 0 | using namespace TemplateInstArgsHelpers; |
349 | 0 | const Decl *CurDecl = ND; |
350 | |
|
351 | 0 | if (!CurDecl) |
352 | 0 | CurDecl = Decl::castFromDeclContext(DC); |
353 | |
|
354 | 0 | if (Innermost) { |
355 | 0 | Result.addOuterTemplateArguments(const_cast<NamedDecl *>(ND), |
356 | 0 | Innermost->asArray(), Final); |
357 | | // Populate placeholder template arguments for TemplateTemplateParmDecls. |
358 | | // This is essential for the case e.g. |
359 | | // |
360 | | // template <class> concept Concept = false; |
361 | | // template <template <Concept C> class T> void foo(T<int>) |
362 | | // |
363 | | // where parameter C has a depth of 1 but the substituting argument `int` |
364 | | // has a depth of 0. |
365 | 0 | if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(CurDecl)) |
366 | 0 | HandleDefaultTempArgIntoTempTempParam(TTP, Result); |
367 | 0 | CurDecl = Response::UseNextDecl(CurDecl).NextDecl; |
368 | 0 | } |
369 | |
|
370 | 0 | while (!CurDecl->isFileContextDecl()) { |
371 | 0 | Response R; |
372 | 0 | if (const auto *VarTemplSpec = |
373 | 0 | dyn_cast<VarTemplateSpecializationDecl>(CurDecl)) { |
374 | 0 | R = HandleVarTemplateSpec(VarTemplSpec, Result, SkipForSpecialization); |
375 | 0 | } else if (const auto *PartialClassTemplSpec = |
376 | 0 | dyn_cast<ClassTemplatePartialSpecializationDecl>(CurDecl)) { |
377 | 0 | R = HandlePartialClassTemplateSpec(PartialClassTemplSpec, Result, |
378 | 0 | SkipForSpecialization); |
379 | 0 | } else if (const auto *ClassTemplSpec = |
380 | 0 | dyn_cast<ClassTemplateSpecializationDecl>(CurDecl)) { |
381 | 0 | R = HandleClassTemplateSpec(ClassTemplSpec, Result, |
382 | 0 | SkipForSpecialization); |
383 | 0 | } else if (const auto *Function = dyn_cast<FunctionDecl>(CurDecl)) { |
384 | 0 | R = HandleFunction(Function, Result, Pattern, RelativeToPrimary, |
385 | 0 | ForConstraintInstantiation); |
386 | 0 | } else if (const auto *Rec = dyn_cast<CXXRecordDecl>(CurDecl)) { |
387 | 0 | R = HandleRecordDecl(Rec, Result, Context, ForConstraintInstantiation); |
388 | 0 | } else if (const auto *CSD = |
389 | 0 | dyn_cast<ImplicitConceptSpecializationDecl>(CurDecl)) { |
390 | 0 | R = HandleImplicitConceptSpecializationDecl(CSD, Result); |
391 | 0 | } else if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(CurDecl)) { |
392 | 0 | R = HandleFunctionTemplateDecl(FTD, Result); |
393 | 0 | } else if (const auto *CTD = dyn_cast<ClassTemplateDecl>(CurDecl)) { |
394 | 0 | R = Response::ChangeDecl(CTD->getLexicalDeclContext()); |
395 | 0 | } else if (!isa<DeclContext>(CurDecl)) { |
396 | 0 | R = Response::DontClearRelativeToPrimaryNextDecl(CurDecl); |
397 | 0 | if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(CurDecl)) { |
398 | 0 | R = HandleDefaultTempArgIntoTempTempParam(TTP, Result); |
399 | 0 | } |
400 | 0 | } else { |
401 | 0 | R = HandleGenericDeclContext(CurDecl); |
402 | 0 | } |
403 | |
|
404 | 0 | if (R.IsDone) |
405 | 0 | return Result; |
406 | 0 | if (R.ClearRelativeToPrimary) |
407 | 0 | RelativeToPrimary = false; |
408 | 0 | assert(R.NextDecl); |
409 | 0 | CurDecl = R.NextDecl; |
410 | 0 | } |
411 | | |
412 | 0 | return Result; |
413 | 0 | } |
414 | | |
415 | 0 | bool Sema::CodeSynthesisContext::isInstantiationRecord() const { |
416 | 0 | switch (Kind) { |
417 | 0 | case TemplateInstantiation: |
418 | 0 | case ExceptionSpecInstantiation: |
419 | 0 | case DefaultTemplateArgumentInstantiation: |
420 | 0 | case DefaultFunctionArgumentInstantiation: |
421 | 0 | case ExplicitTemplateArgumentSubstitution: |
422 | 0 | case DeducedTemplateArgumentSubstitution: |
423 | 0 | case PriorTemplateArgumentSubstitution: |
424 | 0 | case ConstraintsCheck: |
425 | 0 | case NestedRequirementConstraintsCheck: |
426 | 0 | return true; |
427 | | |
428 | 0 | case RequirementInstantiation: |
429 | 0 | case RequirementParameterInstantiation: |
430 | 0 | case DefaultTemplateArgumentChecking: |
431 | 0 | case DeclaringSpecialMember: |
432 | 0 | case DeclaringImplicitEqualityComparison: |
433 | 0 | case DefiningSynthesizedFunction: |
434 | 0 | case ExceptionSpecEvaluation: |
435 | 0 | case ConstraintSubstitution: |
436 | 0 | case ParameterMappingSubstitution: |
437 | 0 | case ConstraintNormalization: |
438 | 0 | case RewritingOperatorAsSpaceship: |
439 | 0 | case InitializingStructuredBinding: |
440 | 0 | case MarkingClassDllexported: |
441 | 0 | case BuildingBuiltinDumpStructCall: |
442 | 0 | case LambdaExpressionSubstitution: |
443 | 0 | case BuildingDeductionGuides: |
444 | 0 | return false; |
445 | | |
446 | | // This function should never be called when Kind's value is Memoization. |
447 | 0 | case Memoization: |
448 | 0 | break; |
449 | 0 | } |
450 | | |
451 | 0 | llvm_unreachable("Invalid SynthesisKind!"); |
452 | 0 | } |
453 | | |
454 | | Sema::InstantiatingTemplate::InstantiatingTemplate( |
455 | | Sema &SemaRef, CodeSynthesisContext::SynthesisKind Kind, |
456 | | SourceLocation PointOfInstantiation, SourceRange InstantiationRange, |
457 | | Decl *Entity, NamedDecl *Template, ArrayRef<TemplateArgument> TemplateArgs, |
458 | | sema::TemplateDeductionInfo *DeductionInfo) |
459 | 0 | : SemaRef(SemaRef) { |
460 | | // Don't allow further instantiation if a fatal error and an uncompilable |
461 | | // error have occurred. Any diagnostics we might have raised will not be |
462 | | // visible, and we do not need to construct a correct AST. |
463 | 0 | if (SemaRef.Diags.hasFatalErrorOccurred() && |
464 | 0 | SemaRef.hasUncompilableErrorOccurred()) { |
465 | 0 | Invalid = true; |
466 | 0 | return; |
467 | 0 | } |
468 | 0 | Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange); |
469 | 0 | if (!Invalid) { |
470 | 0 | CodeSynthesisContext Inst; |
471 | 0 | Inst.Kind = Kind; |
472 | 0 | Inst.PointOfInstantiation = PointOfInstantiation; |
473 | 0 | Inst.Entity = Entity; |
474 | 0 | Inst.Template = Template; |
475 | 0 | Inst.TemplateArgs = TemplateArgs.data(); |
476 | 0 | Inst.NumTemplateArgs = TemplateArgs.size(); |
477 | 0 | Inst.DeductionInfo = DeductionInfo; |
478 | 0 | Inst.InstantiationRange = InstantiationRange; |
479 | 0 | SemaRef.pushCodeSynthesisContext(Inst); |
480 | |
|
481 | 0 | AlreadyInstantiating = !Inst.Entity ? false : |
482 | 0 | !SemaRef.InstantiatingSpecializations |
483 | 0 | .insert({Inst.Entity->getCanonicalDecl(), Inst.Kind}) |
484 | 0 | .second; |
485 | 0 | atTemplateBegin(SemaRef.TemplateInstCallbacks, SemaRef, Inst); |
486 | 0 | } |
487 | 0 | } |
488 | | |
489 | | Sema::InstantiatingTemplate::InstantiatingTemplate( |
490 | | Sema &SemaRef, SourceLocation PointOfInstantiation, Decl *Entity, |
491 | | SourceRange InstantiationRange) |
492 | | : InstantiatingTemplate(SemaRef, |
493 | | CodeSynthesisContext::TemplateInstantiation, |
494 | 0 | PointOfInstantiation, InstantiationRange, Entity) {} |
495 | | |
496 | | Sema::InstantiatingTemplate::InstantiatingTemplate( |
497 | | Sema &SemaRef, SourceLocation PointOfInstantiation, FunctionDecl *Entity, |
498 | | ExceptionSpecification, SourceRange InstantiationRange) |
499 | | : InstantiatingTemplate( |
500 | | SemaRef, CodeSynthesisContext::ExceptionSpecInstantiation, |
501 | 0 | PointOfInstantiation, InstantiationRange, Entity) {} |
502 | | |
503 | | Sema::InstantiatingTemplate::InstantiatingTemplate( |
504 | | Sema &SemaRef, SourceLocation PointOfInstantiation, TemplateParameter Param, |
505 | | TemplateDecl *Template, ArrayRef<TemplateArgument> TemplateArgs, |
506 | | SourceRange InstantiationRange) |
507 | | : InstantiatingTemplate( |
508 | | SemaRef, |
509 | | CodeSynthesisContext::DefaultTemplateArgumentInstantiation, |
510 | | PointOfInstantiation, InstantiationRange, getAsNamedDecl(Param), |
511 | 0 | Template, TemplateArgs) {} |
512 | | |
513 | | Sema::InstantiatingTemplate::InstantiatingTemplate( |
514 | | Sema &SemaRef, SourceLocation PointOfInstantiation, |
515 | | FunctionTemplateDecl *FunctionTemplate, |
516 | | ArrayRef<TemplateArgument> TemplateArgs, |
517 | | CodeSynthesisContext::SynthesisKind Kind, |
518 | | sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange) |
519 | | : InstantiatingTemplate(SemaRef, Kind, PointOfInstantiation, |
520 | | InstantiationRange, FunctionTemplate, nullptr, |
521 | 0 | TemplateArgs, &DeductionInfo) { |
522 | 0 | assert( |
523 | 0 | Kind == CodeSynthesisContext::ExplicitTemplateArgumentSubstitution || |
524 | 0 | Kind == CodeSynthesisContext::DeducedTemplateArgumentSubstitution); |
525 | 0 | } |
526 | | |
527 | | Sema::InstantiatingTemplate::InstantiatingTemplate( |
528 | | Sema &SemaRef, SourceLocation PointOfInstantiation, |
529 | | TemplateDecl *Template, |
530 | | ArrayRef<TemplateArgument> TemplateArgs, |
531 | | sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange) |
532 | | : InstantiatingTemplate( |
533 | | SemaRef, |
534 | | CodeSynthesisContext::DeducedTemplateArgumentSubstitution, |
535 | | PointOfInstantiation, InstantiationRange, Template, nullptr, |
536 | 0 | TemplateArgs, &DeductionInfo) {} |
537 | | |
538 | | Sema::InstantiatingTemplate::InstantiatingTemplate( |
539 | | Sema &SemaRef, SourceLocation PointOfInstantiation, |
540 | | ClassTemplatePartialSpecializationDecl *PartialSpec, |
541 | | ArrayRef<TemplateArgument> TemplateArgs, |
542 | | sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange) |
543 | | : InstantiatingTemplate( |
544 | | SemaRef, |
545 | | CodeSynthesisContext::DeducedTemplateArgumentSubstitution, |
546 | | PointOfInstantiation, InstantiationRange, PartialSpec, nullptr, |
547 | 0 | TemplateArgs, &DeductionInfo) {} |
548 | | |
549 | | Sema::InstantiatingTemplate::InstantiatingTemplate( |
550 | | Sema &SemaRef, SourceLocation PointOfInstantiation, |
551 | | VarTemplatePartialSpecializationDecl *PartialSpec, |
552 | | ArrayRef<TemplateArgument> TemplateArgs, |
553 | | sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange) |
554 | | : InstantiatingTemplate( |
555 | | SemaRef, |
556 | | CodeSynthesisContext::DeducedTemplateArgumentSubstitution, |
557 | | PointOfInstantiation, InstantiationRange, PartialSpec, nullptr, |
558 | 0 | TemplateArgs, &DeductionInfo) {} |
559 | | |
560 | | Sema::InstantiatingTemplate::InstantiatingTemplate( |
561 | | Sema &SemaRef, SourceLocation PointOfInstantiation, ParmVarDecl *Param, |
562 | | ArrayRef<TemplateArgument> TemplateArgs, SourceRange InstantiationRange) |
563 | | : InstantiatingTemplate( |
564 | | SemaRef, |
565 | | CodeSynthesisContext::DefaultFunctionArgumentInstantiation, |
566 | | PointOfInstantiation, InstantiationRange, Param, nullptr, |
567 | 0 | TemplateArgs) {} |
568 | | |
569 | | Sema::InstantiatingTemplate::InstantiatingTemplate( |
570 | | Sema &SemaRef, SourceLocation PointOfInstantiation, NamedDecl *Template, |
571 | | NonTypeTemplateParmDecl *Param, ArrayRef<TemplateArgument> TemplateArgs, |
572 | | SourceRange InstantiationRange) |
573 | | : InstantiatingTemplate( |
574 | | SemaRef, |
575 | | CodeSynthesisContext::PriorTemplateArgumentSubstitution, |
576 | | PointOfInstantiation, InstantiationRange, Param, Template, |
577 | 0 | TemplateArgs) {} |
578 | | |
579 | | Sema::InstantiatingTemplate::InstantiatingTemplate( |
580 | | Sema &SemaRef, SourceLocation PointOfInstantiation, NamedDecl *Template, |
581 | | TemplateTemplateParmDecl *Param, ArrayRef<TemplateArgument> TemplateArgs, |
582 | | SourceRange InstantiationRange) |
583 | | : InstantiatingTemplate( |
584 | | SemaRef, |
585 | | CodeSynthesisContext::PriorTemplateArgumentSubstitution, |
586 | | PointOfInstantiation, InstantiationRange, Param, Template, |
587 | 0 | TemplateArgs) {} |
588 | | |
589 | | Sema::InstantiatingTemplate::InstantiatingTemplate( |
590 | | Sema &SemaRef, SourceLocation PointOfInstantiation, TemplateDecl *Template, |
591 | | NamedDecl *Param, ArrayRef<TemplateArgument> TemplateArgs, |
592 | | SourceRange InstantiationRange) |
593 | | : InstantiatingTemplate( |
594 | | SemaRef, CodeSynthesisContext::DefaultTemplateArgumentChecking, |
595 | | PointOfInstantiation, InstantiationRange, Param, Template, |
596 | 0 | TemplateArgs) {} |
597 | | |
598 | | Sema::InstantiatingTemplate::InstantiatingTemplate( |
599 | | Sema &SemaRef, SourceLocation PointOfInstantiation, |
600 | | concepts::Requirement *Req, sema::TemplateDeductionInfo &DeductionInfo, |
601 | | SourceRange InstantiationRange) |
602 | | : InstantiatingTemplate( |
603 | | SemaRef, CodeSynthesisContext::RequirementInstantiation, |
604 | | PointOfInstantiation, InstantiationRange, /*Entity=*/nullptr, |
605 | 0 | /*Template=*/nullptr, /*TemplateArgs=*/std::nullopt, &DeductionInfo) { |
606 | 0 | } |
607 | | |
608 | | Sema::InstantiatingTemplate::InstantiatingTemplate( |
609 | | Sema &SemaRef, SourceLocation PointOfInstantiation, |
610 | | concepts::NestedRequirement *Req, ConstraintsCheck, |
611 | | SourceRange InstantiationRange) |
612 | | : InstantiatingTemplate( |
613 | | SemaRef, CodeSynthesisContext::NestedRequirementConstraintsCheck, |
614 | | PointOfInstantiation, InstantiationRange, /*Entity=*/nullptr, |
615 | 0 | /*Template=*/nullptr, /*TemplateArgs=*/std::nullopt) {} |
616 | | |
617 | | Sema::InstantiatingTemplate::InstantiatingTemplate( |
618 | | Sema &SemaRef, SourceLocation PointOfInstantiation, const RequiresExpr *RE, |
619 | | sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange) |
620 | | : InstantiatingTemplate( |
621 | | SemaRef, CodeSynthesisContext::RequirementParameterInstantiation, |
622 | | PointOfInstantiation, InstantiationRange, /*Entity=*/nullptr, |
623 | 0 | /*Template=*/nullptr, /*TemplateArgs=*/std::nullopt, &DeductionInfo) { |
624 | 0 | } |
625 | | |
626 | | Sema::InstantiatingTemplate::InstantiatingTemplate( |
627 | | Sema &SemaRef, SourceLocation PointOfInstantiation, |
628 | | ConstraintsCheck, NamedDecl *Template, |
629 | | ArrayRef<TemplateArgument> TemplateArgs, SourceRange InstantiationRange) |
630 | | : InstantiatingTemplate( |
631 | | SemaRef, CodeSynthesisContext::ConstraintsCheck, |
632 | | PointOfInstantiation, InstantiationRange, Template, nullptr, |
633 | 0 | TemplateArgs) {} |
634 | | |
635 | | Sema::InstantiatingTemplate::InstantiatingTemplate( |
636 | | Sema &SemaRef, SourceLocation PointOfInstantiation, |
637 | | ConstraintSubstitution, NamedDecl *Template, |
638 | | sema::TemplateDeductionInfo &DeductionInfo, SourceRange InstantiationRange) |
639 | | : InstantiatingTemplate( |
640 | | SemaRef, CodeSynthesisContext::ConstraintSubstitution, |
641 | | PointOfInstantiation, InstantiationRange, Template, nullptr, |
642 | 0 | {}, &DeductionInfo) {} |
643 | | |
644 | | Sema::InstantiatingTemplate::InstantiatingTemplate( |
645 | | Sema &SemaRef, SourceLocation PointOfInstantiation, |
646 | | ConstraintNormalization, NamedDecl *Template, |
647 | | SourceRange InstantiationRange) |
648 | | : InstantiatingTemplate( |
649 | | SemaRef, CodeSynthesisContext::ConstraintNormalization, |
650 | 0 | PointOfInstantiation, InstantiationRange, Template) {} |
651 | | |
652 | | Sema::InstantiatingTemplate::InstantiatingTemplate( |
653 | | Sema &SemaRef, SourceLocation PointOfInstantiation, |
654 | | ParameterMappingSubstitution, NamedDecl *Template, |
655 | | SourceRange InstantiationRange) |
656 | | : InstantiatingTemplate( |
657 | | SemaRef, CodeSynthesisContext::ParameterMappingSubstitution, |
658 | 0 | PointOfInstantiation, InstantiationRange, Template) {} |
659 | | |
660 | | Sema::InstantiatingTemplate::InstantiatingTemplate( |
661 | | Sema &SemaRef, SourceLocation PointOfInstantiation, TemplateDecl *Entity, |
662 | | BuildingDeductionGuidesTag, SourceRange InstantiationRange) |
663 | | : InstantiatingTemplate( |
664 | | SemaRef, CodeSynthesisContext::BuildingDeductionGuides, |
665 | 0 | PointOfInstantiation, InstantiationRange, Entity) {} |
666 | | |
667 | | |
668 | 0 | void Sema::pushCodeSynthesisContext(CodeSynthesisContext Ctx) { |
669 | 0 | Ctx.SavedInNonInstantiationSFINAEContext = InNonInstantiationSFINAEContext; |
670 | 0 | InNonInstantiationSFINAEContext = false; |
671 | |
|
672 | 0 | CodeSynthesisContexts.push_back(Ctx); |
673 | |
|
674 | 0 | if (!Ctx.isInstantiationRecord()) |
675 | 0 | ++NonInstantiationEntries; |
676 | | |
677 | | // Check to see if we're low on stack space. We can't do anything about this |
678 | | // from here, but we can at least warn the user. |
679 | 0 | if (isStackNearlyExhausted()) |
680 | 0 | warnStackExhausted(Ctx.PointOfInstantiation); |
681 | 0 | } |
682 | | |
683 | 0 | void Sema::popCodeSynthesisContext() { |
684 | 0 | auto &Active = CodeSynthesisContexts.back(); |
685 | 0 | if (!Active.isInstantiationRecord()) { |
686 | 0 | assert(NonInstantiationEntries > 0); |
687 | 0 | --NonInstantiationEntries; |
688 | 0 | } |
689 | | |
690 | 0 | InNonInstantiationSFINAEContext = Active.SavedInNonInstantiationSFINAEContext; |
691 | | |
692 | | // Name lookup no longer looks in this template's defining module. |
693 | 0 | assert(CodeSynthesisContexts.size() >= |
694 | 0 | CodeSynthesisContextLookupModules.size() && |
695 | 0 | "forgot to remove a lookup module for a template instantiation"); |
696 | 0 | if (CodeSynthesisContexts.size() == |
697 | 0 | CodeSynthesisContextLookupModules.size()) { |
698 | 0 | if (Module *M = CodeSynthesisContextLookupModules.back()) |
699 | 0 | LookupModulesCache.erase(M); |
700 | 0 | CodeSynthesisContextLookupModules.pop_back(); |
701 | 0 | } |
702 | | |
703 | | // If we've left the code synthesis context for the current context stack, |
704 | | // stop remembering that we've emitted that stack. |
705 | 0 | if (CodeSynthesisContexts.size() == |
706 | 0 | LastEmittedCodeSynthesisContextDepth) |
707 | 0 | LastEmittedCodeSynthesisContextDepth = 0; |
708 | |
|
709 | 0 | CodeSynthesisContexts.pop_back(); |
710 | 0 | } |
711 | | |
712 | 0 | void Sema::InstantiatingTemplate::Clear() { |
713 | 0 | if (!Invalid) { |
714 | 0 | if (!AlreadyInstantiating) { |
715 | 0 | auto &Active = SemaRef.CodeSynthesisContexts.back(); |
716 | 0 | if (Active.Entity) |
717 | 0 | SemaRef.InstantiatingSpecializations.erase( |
718 | 0 | {Active.Entity->getCanonicalDecl(), Active.Kind}); |
719 | 0 | } |
720 | |
|
721 | 0 | atTemplateEnd(SemaRef.TemplateInstCallbacks, SemaRef, |
722 | 0 | SemaRef.CodeSynthesisContexts.back()); |
723 | |
|
724 | 0 | SemaRef.popCodeSynthesisContext(); |
725 | 0 | Invalid = true; |
726 | 0 | } |
727 | 0 | } |
728 | | |
729 | | static std::string convertCallArgsToString(Sema &S, |
730 | 0 | llvm::ArrayRef<const Expr *> Args) { |
731 | 0 | std::string Result; |
732 | 0 | llvm::raw_string_ostream OS(Result); |
733 | 0 | llvm::ListSeparator Comma; |
734 | 0 | for (const Expr *Arg : Args) { |
735 | 0 | OS << Comma; |
736 | 0 | Arg->IgnoreParens()->printPretty(OS, nullptr, |
737 | 0 | S.Context.getPrintingPolicy()); |
738 | 0 | } |
739 | 0 | return Result; |
740 | 0 | } |
741 | | |
742 | | bool Sema::InstantiatingTemplate::CheckInstantiationDepth( |
743 | | SourceLocation PointOfInstantiation, |
744 | 0 | SourceRange InstantiationRange) { |
745 | 0 | assert(SemaRef.NonInstantiationEntries <= |
746 | 0 | SemaRef.CodeSynthesisContexts.size()); |
747 | 0 | if ((SemaRef.CodeSynthesisContexts.size() - |
748 | 0 | SemaRef.NonInstantiationEntries) |
749 | 0 | <= SemaRef.getLangOpts().InstantiationDepth) |
750 | 0 | return false; |
751 | | |
752 | 0 | SemaRef.Diag(PointOfInstantiation, |
753 | 0 | diag::err_template_recursion_depth_exceeded) |
754 | 0 | << SemaRef.getLangOpts().InstantiationDepth |
755 | 0 | << InstantiationRange; |
756 | 0 | SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth) |
757 | 0 | << SemaRef.getLangOpts().InstantiationDepth; |
758 | 0 | return true; |
759 | 0 | } |
760 | | |
761 | | /// Prints the current instantiation stack through a series of |
762 | | /// notes. |
763 | 0 | void Sema::PrintInstantiationStack() { |
764 | | // Determine which template instantiations to skip, if any. |
765 | 0 | unsigned SkipStart = CodeSynthesisContexts.size(), SkipEnd = SkipStart; |
766 | 0 | unsigned Limit = Diags.getTemplateBacktraceLimit(); |
767 | 0 | if (Limit && Limit < CodeSynthesisContexts.size()) { |
768 | 0 | SkipStart = Limit / 2 + Limit % 2; |
769 | 0 | SkipEnd = CodeSynthesisContexts.size() - Limit / 2; |
770 | 0 | } |
771 | | |
772 | | // FIXME: In all of these cases, we need to show the template arguments |
773 | 0 | unsigned InstantiationIdx = 0; |
774 | 0 | for (SmallVectorImpl<CodeSynthesisContext>::reverse_iterator |
775 | 0 | Active = CodeSynthesisContexts.rbegin(), |
776 | 0 | ActiveEnd = CodeSynthesisContexts.rend(); |
777 | 0 | Active != ActiveEnd; |
778 | 0 | ++Active, ++InstantiationIdx) { |
779 | | // Skip this instantiation? |
780 | 0 | if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) { |
781 | 0 | if (InstantiationIdx == SkipStart) { |
782 | | // Note that we're skipping instantiations. |
783 | 0 | Diags.Report(Active->PointOfInstantiation, |
784 | 0 | diag::note_instantiation_contexts_suppressed) |
785 | 0 | << unsigned(CodeSynthesisContexts.size() - Limit); |
786 | 0 | } |
787 | 0 | continue; |
788 | 0 | } |
789 | | |
790 | 0 | switch (Active->Kind) { |
791 | 0 | case CodeSynthesisContext::TemplateInstantiation: { |
792 | 0 | Decl *D = Active->Entity; |
793 | 0 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
794 | 0 | unsigned DiagID = diag::note_template_member_class_here; |
795 | 0 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
796 | 0 | DiagID = diag::note_template_class_instantiation_here; |
797 | 0 | Diags.Report(Active->PointOfInstantiation, DiagID) |
798 | 0 | << Record << Active->InstantiationRange; |
799 | 0 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
800 | 0 | unsigned DiagID; |
801 | 0 | if (Function->getPrimaryTemplate()) |
802 | 0 | DiagID = diag::note_function_template_spec_here; |
803 | 0 | else |
804 | 0 | DiagID = diag::note_template_member_function_here; |
805 | 0 | Diags.Report(Active->PointOfInstantiation, DiagID) |
806 | 0 | << Function |
807 | 0 | << Active->InstantiationRange; |
808 | 0 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
809 | 0 | Diags.Report(Active->PointOfInstantiation, |
810 | 0 | VD->isStaticDataMember()? |
811 | 0 | diag::note_template_static_data_member_def_here |
812 | 0 | : diag::note_template_variable_def_here) |
813 | 0 | << VD |
814 | 0 | << Active->InstantiationRange; |
815 | 0 | } else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
816 | 0 | Diags.Report(Active->PointOfInstantiation, |
817 | 0 | diag::note_template_enum_def_here) |
818 | 0 | << ED |
819 | 0 | << Active->InstantiationRange; |
820 | 0 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
821 | 0 | Diags.Report(Active->PointOfInstantiation, |
822 | 0 | diag::note_template_nsdmi_here) |
823 | 0 | << FD << Active->InstantiationRange; |
824 | 0 | } else if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(D)) { |
825 | 0 | Diags.Report(Active->PointOfInstantiation, |
826 | 0 | diag::note_template_class_instantiation_here) |
827 | 0 | << CTD << Active->InstantiationRange; |
828 | 0 | } else { |
829 | 0 | Diags.Report(Active->PointOfInstantiation, |
830 | 0 | diag::note_template_type_alias_instantiation_here) |
831 | 0 | << cast<TypeAliasTemplateDecl>(D) |
832 | 0 | << Active->InstantiationRange; |
833 | 0 | } |
834 | 0 | break; |
835 | 0 | } |
836 | | |
837 | 0 | case CodeSynthesisContext::DefaultTemplateArgumentInstantiation: { |
838 | 0 | TemplateDecl *Template = cast<TemplateDecl>(Active->Template); |
839 | 0 | SmallString<128> TemplateArgsStr; |
840 | 0 | llvm::raw_svector_ostream OS(TemplateArgsStr); |
841 | 0 | Template->printName(OS, getPrintingPolicy()); |
842 | 0 | printTemplateArgumentList(OS, Active->template_arguments(), |
843 | 0 | getPrintingPolicy()); |
844 | 0 | Diags.Report(Active->PointOfInstantiation, |
845 | 0 | diag::note_default_arg_instantiation_here) |
846 | 0 | << OS.str() |
847 | 0 | << Active->InstantiationRange; |
848 | 0 | break; |
849 | 0 | } |
850 | | |
851 | 0 | case CodeSynthesisContext::ExplicitTemplateArgumentSubstitution: { |
852 | 0 | FunctionTemplateDecl *FnTmpl = cast<FunctionTemplateDecl>(Active->Entity); |
853 | 0 | Diags.Report(Active->PointOfInstantiation, |
854 | 0 | diag::note_explicit_template_arg_substitution_here) |
855 | 0 | << FnTmpl |
856 | 0 | << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(), |
857 | 0 | Active->TemplateArgs, |
858 | 0 | Active->NumTemplateArgs) |
859 | 0 | << Active->InstantiationRange; |
860 | 0 | break; |
861 | 0 | } |
862 | | |
863 | 0 | case CodeSynthesisContext::DeducedTemplateArgumentSubstitution: { |
864 | 0 | if (FunctionTemplateDecl *FnTmpl = |
865 | 0 | dyn_cast<FunctionTemplateDecl>(Active->Entity)) { |
866 | 0 | Diags.Report(Active->PointOfInstantiation, |
867 | 0 | diag::note_function_template_deduction_instantiation_here) |
868 | 0 | << FnTmpl |
869 | 0 | << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(), |
870 | 0 | Active->TemplateArgs, |
871 | 0 | Active->NumTemplateArgs) |
872 | 0 | << Active->InstantiationRange; |
873 | 0 | } else { |
874 | 0 | bool IsVar = isa<VarTemplateDecl>(Active->Entity) || |
875 | 0 | isa<VarTemplateSpecializationDecl>(Active->Entity); |
876 | 0 | bool IsTemplate = false; |
877 | 0 | TemplateParameterList *Params; |
878 | 0 | if (auto *D = dyn_cast<TemplateDecl>(Active->Entity)) { |
879 | 0 | IsTemplate = true; |
880 | 0 | Params = D->getTemplateParameters(); |
881 | 0 | } else if (auto *D = dyn_cast<ClassTemplatePartialSpecializationDecl>( |
882 | 0 | Active->Entity)) { |
883 | 0 | Params = D->getTemplateParameters(); |
884 | 0 | } else if (auto *D = dyn_cast<VarTemplatePartialSpecializationDecl>( |
885 | 0 | Active->Entity)) { |
886 | 0 | Params = D->getTemplateParameters(); |
887 | 0 | } else { |
888 | 0 | llvm_unreachable("unexpected template kind"); |
889 | 0 | } |
890 | |
|
891 | 0 | Diags.Report(Active->PointOfInstantiation, |
892 | 0 | diag::note_deduced_template_arg_substitution_here) |
893 | 0 | << IsVar << IsTemplate << cast<NamedDecl>(Active->Entity) |
894 | 0 | << getTemplateArgumentBindingsText(Params, Active->TemplateArgs, |
895 | 0 | Active->NumTemplateArgs) |
896 | 0 | << Active->InstantiationRange; |
897 | 0 | } |
898 | 0 | break; |
899 | 0 | } |
900 | | |
901 | 0 | case CodeSynthesisContext::DefaultFunctionArgumentInstantiation: { |
902 | 0 | ParmVarDecl *Param = cast<ParmVarDecl>(Active->Entity); |
903 | 0 | FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext()); |
904 | |
|
905 | 0 | SmallString<128> TemplateArgsStr; |
906 | 0 | llvm::raw_svector_ostream OS(TemplateArgsStr); |
907 | 0 | FD->printName(OS, getPrintingPolicy()); |
908 | 0 | printTemplateArgumentList(OS, Active->template_arguments(), |
909 | 0 | getPrintingPolicy()); |
910 | 0 | Diags.Report(Active->PointOfInstantiation, |
911 | 0 | diag::note_default_function_arg_instantiation_here) |
912 | 0 | << OS.str() |
913 | 0 | << Active->InstantiationRange; |
914 | 0 | break; |
915 | 0 | } |
916 | | |
917 | 0 | case CodeSynthesisContext::PriorTemplateArgumentSubstitution: { |
918 | 0 | NamedDecl *Parm = cast<NamedDecl>(Active->Entity); |
919 | 0 | std::string Name; |
920 | 0 | if (!Parm->getName().empty()) |
921 | 0 | Name = std::string(" '") + Parm->getName().str() + "'"; |
922 | |
|
923 | 0 | TemplateParameterList *TemplateParams = nullptr; |
924 | 0 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template)) |
925 | 0 | TemplateParams = Template->getTemplateParameters(); |
926 | 0 | else |
927 | 0 | TemplateParams = |
928 | 0 | cast<ClassTemplatePartialSpecializationDecl>(Active->Template) |
929 | 0 | ->getTemplateParameters(); |
930 | 0 | Diags.Report(Active->PointOfInstantiation, |
931 | 0 | diag::note_prior_template_arg_substitution) |
932 | 0 | << isa<TemplateTemplateParmDecl>(Parm) |
933 | 0 | << Name |
934 | 0 | << getTemplateArgumentBindingsText(TemplateParams, |
935 | 0 | Active->TemplateArgs, |
936 | 0 | Active->NumTemplateArgs) |
937 | 0 | << Active->InstantiationRange; |
938 | 0 | break; |
939 | 0 | } |
940 | | |
941 | 0 | case CodeSynthesisContext::DefaultTemplateArgumentChecking: { |
942 | 0 | TemplateParameterList *TemplateParams = nullptr; |
943 | 0 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template)) |
944 | 0 | TemplateParams = Template->getTemplateParameters(); |
945 | 0 | else |
946 | 0 | TemplateParams = |
947 | 0 | cast<ClassTemplatePartialSpecializationDecl>(Active->Template) |
948 | 0 | ->getTemplateParameters(); |
949 | |
|
950 | 0 | Diags.Report(Active->PointOfInstantiation, |
951 | 0 | diag::note_template_default_arg_checking) |
952 | 0 | << getTemplateArgumentBindingsText(TemplateParams, |
953 | 0 | Active->TemplateArgs, |
954 | 0 | Active->NumTemplateArgs) |
955 | 0 | << Active->InstantiationRange; |
956 | 0 | break; |
957 | 0 | } |
958 | | |
959 | 0 | case CodeSynthesisContext::ExceptionSpecEvaluation: |
960 | 0 | Diags.Report(Active->PointOfInstantiation, |
961 | 0 | diag::note_evaluating_exception_spec_here) |
962 | 0 | << cast<FunctionDecl>(Active->Entity); |
963 | 0 | break; |
964 | | |
965 | 0 | case CodeSynthesisContext::ExceptionSpecInstantiation: |
966 | 0 | Diags.Report(Active->PointOfInstantiation, |
967 | 0 | diag::note_template_exception_spec_instantiation_here) |
968 | 0 | << cast<FunctionDecl>(Active->Entity) |
969 | 0 | << Active->InstantiationRange; |
970 | 0 | break; |
971 | | |
972 | 0 | case CodeSynthesisContext::RequirementInstantiation: |
973 | 0 | Diags.Report(Active->PointOfInstantiation, |
974 | 0 | diag::note_template_requirement_instantiation_here) |
975 | 0 | << Active->InstantiationRange; |
976 | 0 | break; |
977 | 0 | case CodeSynthesisContext::RequirementParameterInstantiation: |
978 | 0 | Diags.Report(Active->PointOfInstantiation, |
979 | 0 | diag::note_template_requirement_params_instantiation_here) |
980 | 0 | << Active->InstantiationRange; |
981 | 0 | break; |
982 | | |
983 | 0 | case CodeSynthesisContext::NestedRequirementConstraintsCheck: |
984 | 0 | Diags.Report(Active->PointOfInstantiation, |
985 | 0 | diag::note_nested_requirement_here) |
986 | 0 | << Active->InstantiationRange; |
987 | 0 | break; |
988 | | |
989 | 0 | case CodeSynthesisContext::DeclaringSpecialMember: |
990 | 0 | Diags.Report(Active->PointOfInstantiation, |
991 | 0 | diag::note_in_declaration_of_implicit_special_member) |
992 | 0 | << cast<CXXRecordDecl>(Active->Entity) << Active->SpecialMember; |
993 | 0 | break; |
994 | | |
995 | 0 | case CodeSynthesisContext::DeclaringImplicitEqualityComparison: |
996 | 0 | Diags.Report(Active->Entity->getLocation(), |
997 | 0 | diag::note_in_declaration_of_implicit_equality_comparison); |
998 | 0 | break; |
999 | | |
1000 | 0 | case CodeSynthesisContext::DefiningSynthesizedFunction: { |
1001 | | // FIXME: For synthesized functions that are not defaulted, |
1002 | | // produce a note. |
1003 | 0 | auto *FD = dyn_cast<FunctionDecl>(Active->Entity); |
1004 | 0 | DefaultedFunctionKind DFK = |
1005 | 0 | FD ? getDefaultedFunctionKind(FD) : DefaultedFunctionKind(); |
1006 | 0 | if (DFK.isSpecialMember()) { |
1007 | 0 | auto *MD = cast<CXXMethodDecl>(FD); |
1008 | 0 | Diags.Report(Active->PointOfInstantiation, |
1009 | 0 | diag::note_member_synthesized_at) |
1010 | 0 | << MD->isExplicitlyDefaulted() << DFK.asSpecialMember() |
1011 | 0 | << Context.getTagDeclType(MD->getParent()); |
1012 | 0 | } else if (DFK.isComparison()) { |
1013 | 0 | QualType RecordType = FD->getParamDecl(0) |
1014 | 0 | ->getType() |
1015 | 0 | .getNonReferenceType() |
1016 | 0 | .getUnqualifiedType(); |
1017 | 0 | Diags.Report(Active->PointOfInstantiation, |
1018 | 0 | diag::note_comparison_synthesized_at) |
1019 | 0 | << (int)DFK.asComparison() << RecordType; |
1020 | 0 | } |
1021 | 0 | break; |
1022 | 0 | } |
1023 | | |
1024 | 0 | case CodeSynthesisContext::RewritingOperatorAsSpaceship: |
1025 | 0 | Diags.Report(Active->Entity->getLocation(), |
1026 | 0 | diag::note_rewriting_operator_as_spaceship); |
1027 | 0 | break; |
1028 | | |
1029 | 0 | case CodeSynthesisContext::InitializingStructuredBinding: |
1030 | 0 | Diags.Report(Active->PointOfInstantiation, |
1031 | 0 | diag::note_in_binding_decl_init) |
1032 | 0 | << cast<BindingDecl>(Active->Entity); |
1033 | 0 | break; |
1034 | | |
1035 | 0 | case CodeSynthesisContext::MarkingClassDllexported: |
1036 | 0 | Diags.Report(Active->PointOfInstantiation, |
1037 | 0 | diag::note_due_to_dllexported_class) |
1038 | 0 | << cast<CXXRecordDecl>(Active->Entity) << !getLangOpts().CPlusPlus11; |
1039 | 0 | break; |
1040 | | |
1041 | 0 | case CodeSynthesisContext::BuildingBuiltinDumpStructCall: |
1042 | 0 | Diags.Report(Active->PointOfInstantiation, |
1043 | 0 | diag::note_building_builtin_dump_struct_call) |
1044 | 0 | << convertCallArgsToString( |
1045 | 0 | *this, llvm::ArrayRef(Active->CallArgs, Active->NumCallArgs)); |
1046 | 0 | break; |
1047 | | |
1048 | 0 | case CodeSynthesisContext::Memoization: |
1049 | 0 | break; |
1050 | | |
1051 | 0 | case CodeSynthesisContext::LambdaExpressionSubstitution: |
1052 | 0 | Diags.Report(Active->PointOfInstantiation, |
1053 | 0 | diag::note_lambda_substitution_here); |
1054 | 0 | break; |
1055 | 0 | case CodeSynthesisContext::ConstraintsCheck: { |
1056 | 0 | unsigned DiagID = 0; |
1057 | 0 | if (!Active->Entity) { |
1058 | 0 | Diags.Report(Active->PointOfInstantiation, |
1059 | 0 | diag::note_nested_requirement_here) |
1060 | 0 | << Active->InstantiationRange; |
1061 | 0 | break; |
1062 | 0 | } |
1063 | 0 | if (isa<ConceptDecl>(Active->Entity)) |
1064 | 0 | DiagID = diag::note_concept_specialization_here; |
1065 | 0 | else if (isa<TemplateDecl>(Active->Entity)) |
1066 | 0 | DiagID = diag::note_checking_constraints_for_template_id_here; |
1067 | 0 | else if (isa<VarTemplatePartialSpecializationDecl>(Active->Entity)) |
1068 | 0 | DiagID = diag::note_checking_constraints_for_var_spec_id_here; |
1069 | 0 | else if (isa<ClassTemplatePartialSpecializationDecl>(Active->Entity)) |
1070 | 0 | DiagID = diag::note_checking_constraints_for_class_spec_id_here; |
1071 | 0 | else { |
1072 | 0 | assert(isa<FunctionDecl>(Active->Entity)); |
1073 | 0 | DiagID = diag::note_checking_constraints_for_function_here; |
1074 | 0 | } |
1075 | 0 | SmallString<128> TemplateArgsStr; |
1076 | 0 | llvm::raw_svector_ostream OS(TemplateArgsStr); |
1077 | 0 | cast<NamedDecl>(Active->Entity)->printName(OS, getPrintingPolicy()); |
1078 | 0 | if (!isa<FunctionDecl>(Active->Entity)) { |
1079 | 0 | printTemplateArgumentList(OS, Active->template_arguments(), |
1080 | 0 | getPrintingPolicy()); |
1081 | 0 | } |
1082 | 0 | Diags.Report(Active->PointOfInstantiation, DiagID) << OS.str() |
1083 | 0 | << Active->InstantiationRange; |
1084 | 0 | break; |
1085 | 0 | } |
1086 | 0 | case CodeSynthesisContext::ConstraintSubstitution: |
1087 | 0 | Diags.Report(Active->PointOfInstantiation, |
1088 | 0 | diag::note_constraint_substitution_here) |
1089 | 0 | << Active->InstantiationRange; |
1090 | 0 | break; |
1091 | 0 | case CodeSynthesisContext::ConstraintNormalization: |
1092 | 0 | Diags.Report(Active->PointOfInstantiation, |
1093 | 0 | diag::note_constraint_normalization_here) |
1094 | 0 | << cast<NamedDecl>(Active->Entity)->getName() |
1095 | 0 | << Active->InstantiationRange; |
1096 | 0 | break; |
1097 | 0 | case CodeSynthesisContext::ParameterMappingSubstitution: |
1098 | 0 | Diags.Report(Active->PointOfInstantiation, |
1099 | 0 | diag::note_parameter_mapping_substitution_here) |
1100 | 0 | << Active->InstantiationRange; |
1101 | 0 | break; |
1102 | 0 | case CodeSynthesisContext::BuildingDeductionGuides: |
1103 | 0 | Diags.Report(Active->PointOfInstantiation, |
1104 | 0 | diag::note_building_deduction_guide_here); |
1105 | 0 | break; |
1106 | 0 | } |
1107 | 0 | } |
1108 | 0 | } |
1109 | | |
1110 | 12.8k | std::optional<TemplateDeductionInfo *> Sema::isSFINAEContext() const { |
1111 | 12.8k | if (InNonInstantiationSFINAEContext) |
1112 | 0 | return std::optional<TemplateDeductionInfo *>(nullptr); |
1113 | | |
1114 | 12.8k | for (SmallVectorImpl<CodeSynthesisContext>::const_reverse_iterator |
1115 | 12.8k | Active = CodeSynthesisContexts.rbegin(), |
1116 | 12.8k | ActiveEnd = CodeSynthesisContexts.rend(); |
1117 | 12.8k | Active != ActiveEnd; |
1118 | 12.8k | ++Active) |
1119 | 0 | { |
1120 | 0 | switch (Active->Kind) { |
1121 | 0 | case CodeSynthesisContext::TemplateInstantiation: |
1122 | | // An instantiation of an alias template may or may not be a SFINAE |
1123 | | // context, depending on what else is on the stack. |
1124 | 0 | if (isa<TypeAliasTemplateDecl>(Active->Entity)) |
1125 | 0 | break; |
1126 | 0 | [[fallthrough]]; |
1127 | 0 | case CodeSynthesisContext::DefaultFunctionArgumentInstantiation: |
1128 | 0 | case CodeSynthesisContext::ExceptionSpecInstantiation: |
1129 | 0 | case CodeSynthesisContext::ConstraintsCheck: |
1130 | 0 | case CodeSynthesisContext::ParameterMappingSubstitution: |
1131 | 0 | case CodeSynthesisContext::ConstraintNormalization: |
1132 | 0 | case CodeSynthesisContext::NestedRequirementConstraintsCheck: |
1133 | | // This is a template instantiation, so there is no SFINAE. |
1134 | 0 | return std::nullopt; |
1135 | 0 | case CodeSynthesisContext::LambdaExpressionSubstitution: |
1136 | | // [temp.deduct]p9 |
1137 | | // A lambda-expression appearing in a function type or a template |
1138 | | // parameter is not considered part of the immediate context for the |
1139 | | // purposes of template argument deduction. |
1140 | | // CWG2672: A lambda-expression body is never in the immediate context. |
1141 | 0 | return std::nullopt; |
1142 | | |
1143 | 0 | case CodeSynthesisContext::DefaultTemplateArgumentInstantiation: |
1144 | 0 | case CodeSynthesisContext::PriorTemplateArgumentSubstitution: |
1145 | 0 | case CodeSynthesisContext::DefaultTemplateArgumentChecking: |
1146 | 0 | case CodeSynthesisContext::RewritingOperatorAsSpaceship: |
1147 | | // A default template argument instantiation and substitution into |
1148 | | // template parameters with arguments for prior parameters may or may |
1149 | | // not be a SFINAE context; look further up the stack. |
1150 | 0 | break; |
1151 | | |
1152 | 0 | case CodeSynthesisContext::ExplicitTemplateArgumentSubstitution: |
1153 | 0 | case CodeSynthesisContext::DeducedTemplateArgumentSubstitution: |
1154 | | // We're either substituting explicitly-specified template arguments, |
1155 | | // deduced template arguments. SFINAE applies unless we are in a lambda |
1156 | | // body, see [temp.deduct]p9. |
1157 | 0 | case CodeSynthesisContext::ConstraintSubstitution: |
1158 | 0 | case CodeSynthesisContext::RequirementInstantiation: |
1159 | 0 | case CodeSynthesisContext::RequirementParameterInstantiation: |
1160 | | // SFINAE always applies in a constraint expression or a requirement |
1161 | | // in a requires expression. |
1162 | 0 | assert(Active->DeductionInfo && "Missing deduction info pointer"); |
1163 | 0 | return Active->DeductionInfo; |
1164 | | |
1165 | 0 | case CodeSynthesisContext::DeclaringSpecialMember: |
1166 | 0 | case CodeSynthesisContext::DeclaringImplicitEqualityComparison: |
1167 | 0 | case CodeSynthesisContext::DefiningSynthesizedFunction: |
1168 | 0 | case CodeSynthesisContext::InitializingStructuredBinding: |
1169 | 0 | case CodeSynthesisContext::MarkingClassDllexported: |
1170 | 0 | case CodeSynthesisContext::BuildingBuiltinDumpStructCall: |
1171 | 0 | case CodeSynthesisContext::BuildingDeductionGuides: |
1172 | | // This happens in a context unrelated to template instantiation, so |
1173 | | // there is no SFINAE. |
1174 | 0 | return std::nullopt; |
1175 | | |
1176 | 0 | case CodeSynthesisContext::ExceptionSpecEvaluation: |
1177 | | // FIXME: This should not be treated as a SFINAE context, because |
1178 | | // we will cache an incorrect exception specification. However, clang |
1179 | | // bootstrap relies this! See PR31692. |
1180 | 0 | break; |
1181 | | |
1182 | 0 | case CodeSynthesisContext::Memoization: |
1183 | 0 | break; |
1184 | 0 | } |
1185 | | |
1186 | | // The inner context was transparent for SFINAE. If it occurred within a |
1187 | | // non-instantiation SFINAE context, then SFINAE applies. |
1188 | 0 | if (Active->SavedInNonInstantiationSFINAEContext) |
1189 | 0 | return std::optional<TemplateDeductionInfo *>(nullptr); |
1190 | 0 | } |
1191 | | |
1192 | 12.8k | return std::nullopt; |
1193 | 12.8k | } |
1194 | | |
1195 | | //===----------------------------------------------------------------------===/ |
1196 | | // Template Instantiation for Types |
1197 | | //===----------------------------------------------------------------------===/ |
1198 | | namespace { |
1199 | | class TemplateInstantiator : public TreeTransform<TemplateInstantiator> { |
1200 | | const MultiLevelTemplateArgumentList &TemplateArgs; |
1201 | | SourceLocation Loc; |
1202 | | DeclarationName Entity; |
1203 | | // Whether to evaluate the C++20 constraints or simply substitute into them. |
1204 | | bool EvaluateConstraints = true; |
1205 | | |
1206 | | public: |
1207 | | typedef TreeTransform<TemplateInstantiator> inherited; |
1208 | | |
1209 | | TemplateInstantiator(Sema &SemaRef, |
1210 | | const MultiLevelTemplateArgumentList &TemplateArgs, |
1211 | | SourceLocation Loc, DeclarationName Entity) |
1212 | | : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc), |
1213 | 0 | Entity(Entity) {} |
1214 | | |
1215 | 0 | void setEvaluateConstraints(bool B) { |
1216 | 0 | EvaluateConstraints = B; |
1217 | 0 | } |
1218 | 0 | bool getEvaluateConstraints() { |
1219 | 0 | return EvaluateConstraints; |
1220 | 0 | } |
1221 | | |
1222 | | /// Determine whether the given type \p T has already been |
1223 | | /// transformed. |
1224 | | /// |
1225 | | /// For the purposes of template instantiation, a type has already been |
1226 | | /// transformed if it is NULL or if it is not dependent. |
1227 | | bool AlreadyTransformed(QualType T); |
1228 | | |
1229 | | /// Returns the location of the entity being instantiated, if known. |
1230 | 0 | SourceLocation getBaseLocation() { return Loc; } |
1231 | | |
1232 | | /// Returns the name of the entity being instantiated, if any. |
1233 | 0 | DeclarationName getBaseEntity() { return Entity; } |
1234 | | |
1235 | | /// Sets the "base" location and entity when that |
1236 | | /// information is known based on another transformation. |
1237 | 0 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
1238 | 0 | this->Loc = Loc; |
1239 | 0 | this->Entity = Entity; |
1240 | 0 | } |
1241 | | |
1242 | 0 | unsigned TransformTemplateDepth(unsigned Depth) { |
1243 | 0 | return TemplateArgs.getNewDepth(Depth); |
1244 | 0 | } |
1245 | | |
1246 | 0 | std::optional<unsigned> getPackIndex(TemplateArgument Pack) { |
1247 | 0 | int Index = getSema().ArgumentPackSubstitutionIndex; |
1248 | 0 | if (Index == -1) |
1249 | 0 | return std::nullopt; |
1250 | 0 | return Pack.pack_size() - 1 - Index; |
1251 | 0 | } |
1252 | | |
1253 | | bool TryExpandParameterPacks(SourceLocation EllipsisLoc, |
1254 | | SourceRange PatternRange, |
1255 | | ArrayRef<UnexpandedParameterPack> Unexpanded, |
1256 | | bool &ShouldExpand, bool &RetainExpansion, |
1257 | 0 | std::optional<unsigned> &NumExpansions) { |
1258 | 0 | return getSema().CheckParameterPacksForExpansion(EllipsisLoc, |
1259 | 0 | PatternRange, Unexpanded, |
1260 | 0 | TemplateArgs, |
1261 | 0 | ShouldExpand, |
1262 | 0 | RetainExpansion, |
1263 | 0 | NumExpansions); |
1264 | 0 | } |
1265 | | |
1266 | 0 | void ExpandingFunctionParameterPack(ParmVarDecl *Pack) { |
1267 | 0 | SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Pack); |
1268 | 0 | } |
1269 | | |
1270 | 0 | TemplateArgument ForgetPartiallySubstitutedPack() { |
1271 | 0 | TemplateArgument Result; |
1272 | 0 | if (NamedDecl *PartialPack |
1273 | 0 | = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){ |
1274 | 0 | MultiLevelTemplateArgumentList &TemplateArgs |
1275 | 0 | = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs); |
1276 | 0 | unsigned Depth, Index; |
1277 | 0 | std::tie(Depth, Index) = getDepthAndIndex(PartialPack); |
1278 | 0 | if (TemplateArgs.hasTemplateArgument(Depth, Index)) { |
1279 | 0 | Result = TemplateArgs(Depth, Index); |
1280 | 0 | TemplateArgs.setArgument(Depth, Index, TemplateArgument()); |
1281 | 0 | } |
1282 | 0 | } |
1283 | |
|
1284 | 0 | return Result; |
1285 | 0 | } |
1286 | | |
1287 | 0 | void RememberPartiallySubstitutedPack(TemplateArgument Arg) { |
1288 | 0 | if (Arg.isNull()) |
1289 | 0 | return; |
1290 | | |
1291 | 0 | if (NamedDecl *PartialPack |
1292 | 0 | = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){ |
1293 | 0 | MultiLevelTemplateArgumentList &TemplateArgs |
1294 | 0 | = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs); |
1295 | 0 | unsigned Depth, Index; |
1296 | 0 | std::tie(Depth, Index) = getDepthAndIndex(PartialPack); |
1297 | 0 | TemplateArgs.setArgument(Depth, Index, Arg); |
1298 | 0 | } |
1299 | 0 | } |
1300 | | |
1301 | | /// Transform the given declaration by instantiating a reference to |
1302 | | /// this declaration. |
1303 | | Decl *TransformDecl(SourceLocation Loc, Decl *D); |
1304 | | |
1305 | 0 | void transformAttrs(Decl *Old, Decl *New) { |
1306 | 0 | SemaRef.InstantiateAttrs(TemplateArgs, Old, New); |
1307 | 0 | } |
1308 | | |
1309 | 0 | void transformedLocalDecl(Decl *Old, ArrayRef<Decl *> NewDecls) { |
1310 | 0 | if (Old->isParameterPack()) { |
1311 | 0 | SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Old); |
1312 | 0 | for (auto *New : NewDecls) |
1313 | 0 | SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg( |
1314 | 0 | Old, cast<VarDecl>(New)); |
1315 | 0 | return; |
1316 | 0 | } |
1317 | | |
1318 | 0 | assert(NewDecls.size() == 1 && |
1319 | 0 | "should only have multiple expansions for a pack"); |
1320 | 0 | Decl *New = NewDecls.front(); |
1321 | | |
1322 | | // If we've instantiated the call operator of a lambda or the call |
1323 | | // operator template of a generic lambda, update the "instantiation of" |
1324 | | // information. |
1325 | 0 | auto *NewMD = dyn_cast<CXXMethodDecl>(New); |
1326 | 0 | if (NewMD && isLambdaCallOperator(NewMD)) { |
1327 | 0 | auto *OldMD = dyn_cast<CXXMethodDecl>(Old); |
1328 | 0 | if (auto *NewTD = NewMD->getDescribedFunctionTemplate()) |
1329 | 0 | NewTD->setInstantiatedFromMemberTemplate( |
1330 | 0 | OldMD->getDescribedFunctionTemplate()); |
1331 | 0 | else |
1332 | 0 | NewMD->setInstantiationOfMemberFunction(OldMD, |
1333 | 0 | TSK_ImplicitInstantiation); |
1334 | 0 | } |
1335 | |
|
1336 | 0 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(Old, New); |
1337 | | |
1338 | | // We recreated a local declaration, but not by instantiating it. There |
1339 | | // may be pending dependent diagnostics to produce. |
1340 | 0 | if (auto *DC = dyn_cast<DeclContext>(Old); |
1341 | 0 | DC && DC->isDependentContext() && DC->isFunctionOrMethod()) |
1342 | 0 | SemaRef.PerformDependentDiagnostics(DC, TemplateArgs); |
1343 | 0 | } |
1344 | | |
1345 | | /// Transform the definition of the given declaration by |
1346 | | /// instantiating it. |
1347 | | Decl *TransformDefinition(SourceLocation Loc, Decl *D); |
1348 | | |
1349 | | /// Transform the first qualifier within a scope by instantiating the |
1350 | | /// declaration. |
1351 | | NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc); |
1352 | | |
1353 | | bool TransformExceptionSpec(SourceLocation Loc, |
1354 | | FunctionProtoType::ExceptionSpecInfo &ESI, |
1355 | | SmallVectorImpl<QualType> &Exceptions, |
1356 | | bool &Changed); |
1357 | | |
1358 | | /// Rebuild the exception declaration and register the declaration |
1359 | | /// as an instantiated local. |
1360 | | VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, |
1361 | | TypeSourceInfo *Declarator, |
1362 | | SourceLocation StartLoc, |
1363 | | SourceLocation NameLoc, |
1364 | | IdentifierInfo *Name); |
1365 | | |
1366 | | /// Rebuild the Objective-C exception declaration and register the |
1367 | | /// declaration as an instantiated local. |
1368 | | VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl, |
1369 | | TypeSourceInfo *TSInfo, QualType T); |
1370 | | |
1371 | | /// Check for tag mismatches when instantiating an |
1372 | | /// elaborated type. |
1373 | | QualType RebuildElaboratedType(SourceLocation KeywordLoc, |
1374 | | ElaboratedTypeKeyword Keyword, |
1375 | | NestedNameSpecifierLoc QualifierLoc, |
1376 | | QualType T); |
1377 | | |
1378 | | TemplateName |
1379 | | TransformTemplateName(CXXScopeSpec &SS, TemplateName Name, |
1380 | | SourceLocation NameLoc, |
1381 | | QualType ObjectType = QualType(), |
1382 | | NamedDecl *FirstQualifierInScope = nullptr, |
1383 | | bool AllowInjectedClassName = false); |
1384 | | |
1385 | | const LoopHintAttr *TransformLoopHintAttr(const LoopHintAttr *LH); |
1386 | | const NoInlineAttr *TransformStmtNoInlineAttr(const Stmt *OrigS, |
1387 | | const Stmt *InstS, |
1388 | | const NoInlineAttr *A); |
1389 | | const AlwaysInlineAttr * |
1390 | | TransformStmtAlwaysInlineAttr(const Stmt *OrigS, const Stmt *InstS, |
1391 | | const AlwaysInlineAttr *A); |
1392 | | const CodeAlignAttr *TransformCodeAlignAttr(const CodeAlignAttr *CA); |
1393 | | ExprResult TransformPredefinedExpr(PredefinedExpr *E); |
1394 | | ExprResult TransformDeclRefExpr(DeclRefExpr *E); |
1395 | | ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E); |
1396 | | |
1397 | | ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E, |
1398 | | NonTypeTemplateParmDecl *D); |
1399 | | ExprResult TransformSubstNonTypeTemplateParmPackExpr( |
1400 | | SubstNonTypeTemplateParmPackExpr *E); |
1401 | | ExprResult TransformSubstNonTypeTemplateParmExpr( |
1402 | | SubstNonTypeTemplateParmExpr *E); |
1403 | | |
1404 | | /// Rebuild a DeclRefExpr for a VarDecl reference. |
1405 | | ExprResult RebuildVarDeclRefExpr(VarDecl *PD, SourceLocation Loc); |
1406 | | |
1407 | | /// Transform a reference to a function or init-capture parameter pack. |
1408 | | ExprResult TransformFunctionParmPackRefExpr(DeclRefExpr *E, VarDecl *PD); |
1409 | | |
1410 | | /// Transform a FunctionParmPackExpr which was built when we couldn't |
1411 | | /// expand a function parameter pack reference which refers to an expanded |
1412 | | /// pack. |
1413 | | ExprResult TransformFunctionParmPackExpr(FunctionParmPackExpr *E); |
1414 | | |
1415 | | QualType TransformFunctionProtoType(TypeLocBuilder &TLB, |
1416 | 0 | FunctionProtoTypeLoc TL) { |
1417 | | // Call the base version; it will forward to our overridden version below. |
1418 | 0 | return inherited::TransformFunctionProtoType(TLB, TL); |
1419 | 0 | } |
1420 | | |
1421 | | template<typename Fn> |
1422 | | QualType TransformFunctionProtoType(TypeLocBuilder &TLB, |
1423 | | FunctionProtoTypeLoc TL, |
1424 | | CXXRecordDecl *ThisContext, |
1425 | | Qualifiers ThisTypeQuals, |
1426 | | Fn TransformExceptionSpec); |
1427 | | |
1428 | | ParmVarDecl * |
1429 | | TransformFunctionTypeParam(ParmVarDecl *OldParm, int indexAdjustment, |
1430 | | std::optional<unsigned> NumExpansions, |
1431 | | bool ExpectParameterPack); |
1432 | | |
1433 | | using inherited::TransformTemplateTypeParmType; |
1434 | | /// Transforms a template type parameter type by performing |
1435 | | /// substitution of the corresponding template type argument. |
1436 | | QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
1437 | | TemplateTypeParmTypeLoc TL, |
1438 | | bool SuppressObjCLifetime); |
1439 | | |
1440 | | QualType BuildSubstTemplateTypeParmType( |
1441 | | TypeLocBuilder &TLB, bool SuppressObjCLifetime, bool Final, |
1442 | | Decl *AssociatedDecl, unsigned Index, std::optional<unsigned> PackIndex, |
1443 | | TemplateArgument Arg, SourceLocation NameLoc); |
1444 | | |
1445 | | /// Transforms an already-substituted template type parameter pack |
1446 | | /// into either itself (if we aren't substituting into its pack expansion) |
1447 | | /// or the appropriate substituted argument. |
1448 | | using inherited::TransformSubstTemplateTypeParmPackType; |
1449 | | QualType |
1450 | | TransformSubstTemplateTypeParmPackType(TypeLocBuilder &TLB, |
1451 | | SubstTemplateTypeParmPackTypeLoc TL, |
1452 | | bool SuppressObjCLifetime); |
1453 | | |
1454 | 0 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
1455 | 0 | LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true); |
1456 | 0 | Sema::ConstraintEvalRAII<TemplateInstantiator> RAII(*this); |
1457 | |
|
1458 | 0 | ExprResult Result = inherited::TransformLambdaExpr(E); |
1459 | 0 | if (Result.isInvalid()) |
1460 | 0 | return Result; |
1461 | | |
1462 | 0 | CXXMethodDecl *MD = Result.getAs<LambdaExpr>()->getCallOperator(); |
1463 | 0 | for (ParmVarDecl *PVD : MD->parameters()) { |
1464 | 0 | assert(PVD && "null in a parameter list"); |
1465 | 0 | if (!PVD->hasDefaultArg()) |
1466 | 0 | continue; |
1467 | 0 | Expr *UninstExpr = PVD->getUninstantiatedDefaultArg(); |
1468 | | // FIXME: Obtain the source location for the '=' token. |
1469 | 0 | SourceLocation EqualLoc = UninstExpr->getBeginLoc(); |
1470 | 0 | if (SemaRef.SubstDefaultArgument(EqualLoc, PVD, TemplateArgs)) { |
1471 | | // If substitution fails, the default argument is set to a |
1472 | | // RecoveryExpr that wraps the uninstantiated default argument so |
1473 | | // that downstream diagnostics are omitted. |
1474 | 0 | ExprResult ErrorResult = SemaRef.CreateRecoveryExpr( |
1475 | 0 | UninstExpr->getBeginLoc(), UninstExpr->getEndLoc(), |
1476 | 0 | { UninstExpr }, UninstExpr->getType()); |
1477 | 0 | if (ErrorResult.isUsable()) |
1478 | 0 | PVD->setDefaultArg(ErrorResult.get()); |
1479 | 0 | } |
1480 | 0 | } |
1481 | |
|
1482 | 0 | return Result; |
1483 | 0 | } |
1484 | | |
1485 | 0 | StmtResult TransformLambdaBody(LambdaExpr *E, Stmt *Body) { |
1486 | | // Currently, we instantiate the body when instantiating the lambda |
1487 | | // expression. However, `EvaluateConstraints` is disabled during the |
1488 | | // instantiation of the lambda expression, causing the instantiation |
1489 | | // failure of the return type requirement in the body. If p0588r1 is fully |
1490 | | // implemented, the body will be lazily instantiated, and this problem |
1491 | | // will not occur. Here, `EvaluateConstraints` is temporarily set to |
1492 | | // `true` to temporarily fix this issue. |
1493 | | // FIXME: This temporary fix can be removed after fully implementing |
1494 | | // p0588r1. |
1495 | 0 | bool Prev = EvaluateConstraints; |
1496 | 0 | EvaluateConstraints = true; |
1497 | 0 | StmtResult Stmt = inherited::TransformLambdaBody(E, Body); |
1498 | 0 | EvaluateConstraints = Prev; |
1499 | 0 | return Stmt; |
1500 | 0 | } |
1501 | | |
1502 | 0 | ExprResult TransformRequiresExpr(RequiresExpr *E) { |
1503 | 0 | LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true); |
1504 | 0 | ExprResult TransReq = inherited::TransformRequiresExpr(E); |
1505 | 0 | if (TransReq.isInvalid()) |
1506 | 0 | return TransReq; |
1507 | 0 | assert(TransReq.get() != E && |
1508 | 0 | "Do not change value of isSatisfied for the existing expression. " |
1509 | 0 | "Create a new expression instead."); |
1510 | 0 | if (E->getBody()->isDependentContext()) { |
1511 | 0 | Sema::SFINAETrap Trap(SemaRef); |
1512 | | // We recreate the RequiresExpr body, but not by instantiating it. |
1513 | | // Produce pending diagnostics for dependent access check. |
1514 | 0 | SemaRef.PerformDependentDiagnostics(E->getBody(), TemplateArgs); |
1515 | | // FIXME: Store SFINAE diagnostics in RequiresExpr for diagnosis. |
1516 | 0 | if (Trap.hasErrorOccurred()) |
1517 | 0 | TransReq.getAs<RequiresExpr>()->setSatisfied(false); |
1518 | 0 | } |
1519 | 0 | return TransReq; |
1520 | 0 | } |
1521 | | |
1522 | | bool TransformRequiresExprRequirements( |
1523 | | ArrayRef<concepts::Requirement *> Reqs, |
1524 | 0 | SmallVectorImpl<concepts::Requirement *> &Transformed) { |
1525 | 0 | bool SatisfactionDetermined = false; |
1526 | 0 | for (concepts::Requirement *Req : Reqs) { |
1527 | 0 | concepts::Requirement *TransReq = nullptr; |
1528 | 0 | if (!SatisfactionDetermined) { |
1529 | 0 | if (auto *TypeReq = dyn_cast<concepts::TypeRequirement>(Req)) |
1530 | 0 | TransReq = TransformTypeRequirement(TypeReq); |
1531 | 0 | else if (auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Req)) |
1532 | 0 | TransReq = TransformExprRequirement(ExprReq); |
1533 | 0 | else |
1534 | 0 | TransReq = TransformNestedRequirement( |
1535 | 0 | cast<concepts::NestedRequirement>(Req)); |
1536 | 0 | if (!TransReq) |
1537 | 0 | return true; |
1538 | 0 | if (!TransReq->isDependent() && !TransReq->isSatisfied()) |
1539 | | // [expr.prim.req]p6 |
1540 | | // [...] The substitution and semantic constraint checking |
1541 | | // proceeds in lexical order and stops when a condition that |
1542 | | // determines the result of the requires-expression is |
1543 | | // encountered. [..] |
1544 | 0 | SatisfactionDetermined = true; |
1545 | 0 | } else |
1546 | 0 | TransReq = Req; |
1547 | 0 | Transformed.push_back(TransReq); |
1548 | 0 | } |
1549 | 0 | return false; |
1550 | 0 | } |
1551 | | |
1552 | | TemplateParameterList *TransformTemplateParameterList( |
1553 | 0 | TemplateParameterList *OrigTPL) { |
1554 | 0 | if (!OrigTPL || !OrigTPL->size()) return OrigTPL; |
1555 | | |
1556 | 0 | DeclContext *Owner = OrigTPL->getParam(0)->getDeclContext(); |
1557 | 0 | TemplateDeclInstantiator DeclInstantiator(getSema(), |
1558 | 0 | /* DeclContext *Owner */ Owner, TemplateArgs); |
1559 | 0 | DeclInstantiator.setEvaluateConstraints(EvaluateConstraints); |
1560 | 0 | return DeclInstantiator.SubstTemplateParams(OrigTPL); |
1561 | 0 | } |
1562 | | |
1563 | | concepts::TypeRequirement * |
1564 | | TransformTypeRequirement(concepts::TypeRequirement *Req); |
1565 | | concepts::ExprRequirement * |
1566 | | TransformExprRequirement(concepts::ExprRequirement *Req); |
1567 | | concepts::NestedRequirement * |
1568 | | TransformNestedRequirement(concepts::NestedRequirement *Req); |
1569 | | ExprResult TransformRequiresTypeParams( |
1570 | | SourceLocation KWLoc, SourceLocation RBraceLoc, const RequiresExpr *RE, |
1571 | | RequiresExprBodyDecl *Body, ArrayRef<ParmVarDecl *> Params, |
1572 | | SmallVectorImpl<QualType> &PTypes, |
1573 | | SmallVectorImpl<ParmVarDecl *> &TransParams, |
1574 | | Sema::ExtParameterInfoBuilder &PInfos); |
1575 | | |
1576 | | private: |
1577 | | ExprResult |
1578 | | transformNonTypeTemplateParmRef(Decl *AssociatedDecl, |
1579 | | const NonTypeTemplateParmDecl *parm, |
1580 | | SourceLocation loc, TemplateArgument arg, |
1581 | | std::optional<unsigned> PackIndex); |
1582 | | }; |
1583 | | } |
1584 | | |
1585 | 0 | bool TemplateInstantiator::AlreadyTransformed(QualType T) { |
1586 | 0 | if (T.isNull()) |
1587 | 0 | return true; |
1588 | | |
1589 | 0 | if (T->isInstantiationDependentType() || T->isVariablyModifiedType()) |
1590 | 0 | return false; |
1591 | | |
1592 | 0 | getSema().MarkDeclarationsReferencedInType(Loc, T); |
1593 | 0 | return true; |
1594 | 0 | } |
1595 | | |
1596 | | static TemplateArgument |
1597 | 0 | getPackSubstitutedTemplateArgument(Sema &S, TemplateArgument Arg) { |
1598 | 0 | assert(S.ArgumentPackSubstitutionIndex >= 0); |
1599 | 0 | assert(S.ArgumentPackSubstitutionIndex < (int)Arg.pack_size()); |
1600 | 0 | Arg = Arg.pack_begin()[S.ArgumentPackSubstitutionIndex]; |
1601 | 0 | if (Arg.isPackExpansion()) |
1602 | 0 | Arg = Arg.getPackExpansionPattern(); |
1603 | 0 | return Arg; |
1604 | 0 | } |
1605 | | |
1606 | 0 | Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) { |
1607 | 0 | if (!D) |
1608 | 0 | return nullptr; |
1609 | | |
1610 | 0 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) { |
1611 | 0 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
1612 | | // If the corresponding template argument is NULL or non-existent, it's |
1613 | | // because we are performing instantiation from explicitly-specified |
1614 | | // template arguments in a function template, but there were some |
1615 | | // arguments left unspecified. |
1616 | 0 | if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(), |
1617 | 0 | TTP->getPosition())) |
1618 | 0 | return D; |
1619 | | |
1620 | 0 | TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition()); |
1621 | |
|
1622 | 0 | if (TTP->isParameterPack()) { |
1623 | 0 | assert(Arg.getKind() == TemplateArgument::Pack && |
1624 | 0 | "Missing argument pack"); |
1625 | 0 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
1626 | 0 | } |
1627 | | |
1628 | 0 | TemplateName Template = Arg.getAsTemplate().getNameToSubstitute(); |
1629 | 0 | assert(!Template.isNull() && Template.getAsTemplateDecl() && |
1630 | 0 | "Wrong kind of template template argument"); |
1631 | 0 | return Template.getAsTemplateDecl(); |
1632 | 0 | } |
1633 | | |
1634 | | // Fall through to find the instantiated declaration for this template |
1635 | | // template parameter. |
1636 | 0 | } |
1637 | | |
1638 | 0 | return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs); |
1639 | 0 | } |
1640 | | |
1641 | 0 | Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) { |
1642 | 0 | Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs); |
1643 | 0 | if (!Inst) |
1644 | 0 | return nullptr; |
1645 | | |
1646 | 0 | getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
1647 | 0 | return Inst; |
1648 | 0 | } |
1649 | | |
1650 | | bool TemplateInstantiator::TransformExceptionSpec( |
1651 | | SourceLocation Loc, FunctionProtoType::ExceptionSpecInfo &ESI, |
1652 | 0 | SmallVectorImpl<QualType> &Exceptions, bool &Changed) { |
1653 | 0 | if (ESI.Type == EST_Uninstantiated) { |
1654 | 0 | ESI.instantiate(); |
1655 | 0 | Changed = true; |
1656 | 0 | } |
1657 | 0 | return inherited::TransformExceptionSpec(Loc, ESI, Exceptions, Changed); |
1658 | 0 | } |
1659 | | |
1660 | | NamedDecl * |
1661 | | TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D, |
1662 | 0 | SourceLocation Loc) { |
1663 | | // If the first part of the nested-name-specifier was a template type |
1664 | | // parameter, instantiate that type parameter down to a tag type. |
1665 | 0 | if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) { |
1666 | 0 | const TemplateTypeParmType *TTP |
1667 | 0 | = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD)); |
1668 | |
|
1669 | 0 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
1670 | | // FIXME: This needs testing w/ member access expressions. |
1671 | 0 | TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex()); |
1672 | |
|
1673 | 0 | if (TTP->isParameterPack()) { |
1674 | 0 | assert(Arg.getKind() == TemplateArgument::Pack && |
1675 | 0 | "Missing argument pack"); |
1676 | | |
1677 | 0 | if (getSema().ArgumentPackSubstitutionIndex == -1) |
1678 | 0 | return nullptr; |
1679 | | |
1680 | 0 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
1681 | 0 | } |
1682 | | |
1683 | 0 | QualType T = Arg.getAsType(); |
1684 | 0 | if (T.isNull()) |
1685 | 0 | return cast_or_null<NamedDecl>(TransformDecl(Loc, D)); |
1686 | | |
1687 | 0 | if (const TagType *Tag = T->getAs<TagType>()) |
1688 | 0 | return Tag->getDecl(); |
1689 | | |
1690 | | // The resulting type is not a tag; complain. |
1691 | 0 | getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T; |
1692 | 0 | return nullptr; |
1693 | 0 | } |
1694 | 0 | } |
1695 | | |
1696 | 0 | return cast_or_null<NamedDecl>(TransformDecl(Loc, D)); |
1697 | 0 | } |
1698 | | |
1699 | | VarDecl * |
1700 | | TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl, |
1701 | | TypeSourceInfo *Declarator, |
1702 | | SourceLocation StartLoc, |
1703 | | SourceLocation NameLoc, |
1704 | 0 | IdentifierInfo *Name) { |
1705 | 0 | VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator, |
1706 | 0 | StartLoc, NameLoc, Name); |
1707 | 0 | if (Var) |
1708 | 0 | getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); |
1709 | 0 | return Var; |
1710 | 0 | } |
1711 | | |
1712 | | VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl, |
1713 | | TypeSourceInfo *TSInfo, |
1714 | 0 | QualType T) { |
1715 | 0 | VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T); |
1716 | 0 | if (Var) |
1717 | 0 | getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); |
1718 | 0 | return Var; |
1719 | 0 | } |
1720 | | |
1721 | | QualType |
1722 | | TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc, |
1723 | | ElaboratedTypeKeyword Keyword, |
1724 | | NestedNameSpecifierLoc QualifierLoc, |
1725 | 0 | QualType T) { |
1726 | 0 | if (const TagType *TT = T->getAs<TagType>()) { |
1727 | 0 | TagDecl* TD = TT->getDecl(); |
1728 | |
|
1729 | 0 | SourceLocation TagLocation = KeywordLoc; |
1730 | |
|
1731 | 0 | IdentifierInfo *Id = TD->getIdentifier(); |
1732 | | |
1733 | | // TODO: should we even warn on struct/class mismatches for this? Seems |
1734 | | // like it's likely to produce a lot of spurious errors. |
1735 | 0 | if (Id && Keyword != ElaboratedTypeKeyword::None && |
1736 | 0 | Keyword != ElaboratedTypeKeyword::Typename) { |
1737 | 0 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword); |
1738 | 0 | if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, /*isDefinition*/false, |
1739 | 0 | TagLocation, Id)) { |
1740 | 0 | SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag) |
1741 | 0 | << Id |
1742 | 0 | << FixItHint::CreateReplacement(SourceRange(TagLocation), |
1743 | 0 | TD->getKindName()); |
1744 | 0 | SemaRef.Diag(TD->getLocation(), diag::note_previous_use); |
1745 | 0 | } |
1746 | 0 | } |
1747 | 0 | } |
1748 | |
|
1749 | 0 | return inherited::RebuildElaboratedType(KeywordLoc, Keyword, QualifierLoc, T); |
1750 | 0 | } |
1751 | | |
1752 | | TemplateName TemplateInstantiator::TransformTemplateName( |
1753 | | CXXScopeSpec &SS, TemplateName Name, SourceLocation NameLoc, |
1754 | | QualType ObjectType, NamedDecl *FirstQualifierInScope, |
1755 | 0 | bool AllowInjectedClassName) { |
1756 | 0 | if (TemplateTemplateParmDecl *TTP |
1757 | 0 | = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())) { |
1758 | 0 | if (TTP->getDepth() < TemplateArgs.getNumLevels()) { |
1759 | | // If the corresponding template argument is NULL or non-existent, it's |
1760 | | // because we are performing instantiation from explicitly-specified |
1761 | | // template arguments in a function template, but there were some |
1762 | | // arguments left unspecified. |
1763 | 0 | if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(), |
1764 | 0 | TTP->getPosition())) |
1765 | 0 | return Name; |
1766 | | |
1767 | 0 | TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition()); |
1768 | |
|
1769 | 0 | if (TemplateArgs.isRewrite()) { |
1770 | | // We're rewriting the template parameter as a reference to another |
1771 | | // template parameter. |
1772 | 0 | if (Arg.getKind() == TemplateArgument::Pack) { |
1773 | 0 | assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion() && |
1774 | 0 | "unexpected pack arguments in template rewrite"); |
1775 | 0 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
1776 | 0 | } |
1777 | 0 | assert(Arg.getKind() == TemplateArgument::Template && |
1778 | 0 | "unexpected nontype template argument kind in template rewrite"); |
1779 | 0 | return Arg.getAsTemplate(); |
1780 | 0 | } |
1781 | | |
1782 | 0 | auto [AssociatedDecl, Final] = |
1783 | 0 | TemplateArgs.getAssociatedDecl(TTP->getDepth()); |
1784 | 0 | std::optional<unsigned> PackIndex; |
1785 | 0 | if (TTP->isParameterPack()) { |
1786 | 0 | assert(Arg.getKind() == TemplateArgument::Pack && |
1787 | 0 | "Missing argument pack"); |
1788 | | |
1789 | 0 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
1790 | | // We have the template argument pack to substitute, but we're not |
1791 | | // actually expanding the enclosing pack expansion yet. So, just |
1792 | | // keep the entire argument pack. |
1793 | 0 | return getSema().Context.getSubstTemplateTemplateParmPack( |
1794 | 0 | Arg, AssociatedDecl, TTP->getIndex(), Final); |
1795 | 0 | } |
1796 | | |
1797 | 0 | PackIndex = getPackIndex(Arg); |
1798 | 0 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
1799 | 0 | } |
1800 | | |
1801 | 0 | TemplateName Template = Arg.getAsTemplate().getNameToSubstitute(); |
1802 | 0 | assert(!Template.isNull() && "Null template template argument"); |
1803 | 0 | assert(!Template.getAsQualifiedTemplateName() && |
1804 | 0 | "template decl to substitute is qualified?"); |
1805 | | |
1806 | 0 | if (Final) |
1807 | 0 | return Template; |
1808 | 0 | return getSema().Context.getSubstTemplateTemplateParm( |
1809 | 0 | Template, AssociatedDecl, TTP->getIndex(), PackIndex); |
1810 | 0 | } |
1811 | 0 | } |
1812 | | |
1813 | 0 | if (SubstTemplateTemplateParmPackStorage *SubstPack |
1814 | 0 | = Name.getAsSubstTemplateTemplateParmPack()) { |
1815 | 0 | if (getSema().ArgumentPackSubstitutionIndex == -1) |
1816 | 0 | return Name; |
1817 | | |
1818 | 0 | TemplateArgument Pack = SubstPack->getArgumentPack(); |
1819 | 0 | TemplateName Template = |
1820 | 0 | getPackSubstitutedTemplateArgument(getSema(), Pack).getAsTemplate(); |
1821 | 0 | if (SubstPack->getFinal()) |
1822 | 0 | return Template; |
1823 | 0 | return getSema().Context.getSubstTemplateTemplateParm( |
1824 | 0 | Template.getNameToSubstitute(), SubstPack->getAssociatedDecl(), |
1825 | 0 | SubstPack->getIndex(), getPackIndex(Pack)); |
1826 | 0 | } |
1827 | | |
1828 | 0 | return inherited::TransformTemplateName(SS, Name, NameLoc, ObjectType, |
1829 | 0 | FirstQualifierInScope, |
1830 | 0 | AllowInjectedClassName); |
1831 | 0 | } |
1832 | | |
1833 | | ExprResult |
1834 | 0 | TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) { |
1835 | 0 | if (!E->isTypeDependent()) |
1836 | 0 | return E; |
1837 | | |
1838 | 0 | return getSema().BuildPredefinedExpr(E->getLocation(), E->getIdentKind()); |
1839 | 0 | } |
1840 | | |
1841 | | ExprResult |
1842 | | TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E, |
1843 | 0 | NonTypeTemplateParmDecl *NTTP) { |
1844 | | // If the corresponding template argument is NULL or non-existent, it's |
1845 | | // because we are performing instantiation from explicitly-specified |
1846 | | // template arguments in a function template, but there were some |
1847 | | // arguments left unspecified. |
1848 | 0 | if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(), |
1849 | 0 | NTTP->getPosition())) |
1850 | 0 | return E; |
1851 | | |
1852 | 0 | TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition()); |
1853 | |
|
1854 | 0 | if (TemplateArgs.isRewrite()) { |
1855 | | // We're rewriting the template parameter as a reference to another |
1856 | | // template parameter. |
1857 | 0 | if (Arg.getKind() == TemplateArgument::Pack) { |
1858 | 0 | assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion() && |
1859 | 0 | "unexpected pack arguments in template rewrite"); |
1860 | 0 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
1861 | 0 | } |
1862 | 0 | assert(Arg.getKind() == TemplateArgument::Expression && |
1863 | 0 | "unexpected nontype template argument kind in template rewrite"); |
1864 | | // FIXME: This can lead to the same subexpression appearing multiple times |
1865 | | // in a complete expression. |
1866 | 0 | return Arg.getAsExpr(); |
1867 | 0 | } |
1868 | | |
1869 | 0 | auto [AssociatedDecl, _] = TemplateArgs.getAssociatedDecl(NTTP->getDepth()); |
1870 | 0 | std::optional<unsigned> PackIndex; |
1871 | 0 | if (NTTP->isParameterPack()) { |
1872 | 0 | assert(Arg.getKind() == TemplateArgument::Pack && |
1873 | 0 | "Missing argument pack"); |
1874 | | |
1875 | 0 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
1876 | | // We have an argument pack, but we can't select a particular argument |
1877 | | // out of it yet. Therefore, we'll build an expression to hold on to that |
1878 | | // argument pack. |
1879 | 0 | QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs, |
1880 | 0 | E->getLocation(), |
1881 | 0 | NTTP->getDeclName()); |
1882 | 0 | if (TargetType.isNull()) |
1883 | 0 | return ExprError(); |
1884 | | |
1885 | 0 | QualType ExprType = TargetType.getNonLValueExprType(SemaRef.Context); |
1886 | 0 | if (TargetType->isRecordType()) |
1887 | 0 | ExprType.addConst(); |
1888 | | // FIXME: Pass in Final. |
1889 | 0 | return new (SemaRef.Context) SubstNonTypeTemplateParmPackExpr( |
1890 | 0 | ExprType, TargetType->isReferenceType() ? VK_LValue : VK_PRValue, |
1891 | 0 | E->getLocation(), Arg, AssociatedDecl, NTTP->getPosition()); |
1892 | 0 | } |
1893 | 0 | PackIndex = getPackIndex(Arg); |
1894 | 0 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
1895 | 0 | } |
1896 | | // FIXME: Don't put subst node on Final replacement. |
1897 | 0 | return transformNonTypeTemplateParmRef(AssociatedDecl, NTTP, E->getLocation(), |
1898 | 0 | Arg, PackIndex); |
1899 | 0 | } |
1900 | | |
1901 | | const LoopHintAttr * |
1902 | 0 | TemplateInstantiator::TransformLoopHintAttr(const LoopHintAttr *LH) { |
1903 | 0 | Expr *TransformedExpr = getDerived().TransformExpr(LH->getValue()).get(); |
1904 | |
|
1905 | 0 | if (TransformedExpr == LH->getValue()) |
1906 | 0 | return LH; |
1907 | | |
1908 | | // Generate error if there is a problem with the value. |
1909 | 0 | if (getSema().CheckLoopHintExpr(TransformedExpr, LH->getLocation())) |
1910 | 0 | return LH; |
1911 | | |
1912 | | // Create new LoopHintValueAttr with integral expression in place of the |
1913 | | // non-type template parameter. |
1914 | 0 | return LoopHintAttr::CreateImplicit(getSema().Context, LH->getOption(), |
1915 | 0 | LH->getState(), TransformedExpr, *LH); |
1916 | 0 | } |
1917 | | const NoInlineAttr *TemplateInstantiator::TransformStmtNoInlineAttr( |
1918 | 0 | const Stmt *OrigS, const Stmt *InstS, const NoInlineAttr *A) { |
1919 | 0 | if (!A || getSema().CheckNoInlineAttr(OrigS, InstS, *A)) |
1920 | 0 | return nullptr; |
1921 | | |
1922 | 0 | return A; |
1923 | 0 | } |
1924 | | const AlwaysInlineAttr *TemplateInstantiator::TransformStmtAlwaysInlineAttr( |
1925 | 0 | const Stmt *OrigS, const Stmt *InstS, const AlwaysInlineAttr *A) { |
1926 | 0 | if (!A || getSema().CheckAlwaysInlineAttr(OrigS, InstS, *A)) |
1927 | 0 | return nullptr; |
1928 | | |
1929 | 0 | return A; |
1930 | 0 | } |
1931 | | |
1932 | | const CodeAlignAttr * |
1933 | 0 | TemplateInstantiator::TransformCodeAlignAttr(const CodeAlignAttr *CA) { |
1934 | 0 | Expr *TransformedExpr = getDerived().TransformExpr(CA->getAlignment()).get(); |
1935 | 0 | return getSema().BuildCodeAlignAttr(*CA, TransformedExpr); |
1936 | 0 | } |
1937 | | |
1938 | | ExprResult TemplateInstantiator::transformNonTypeTemplateParmRef( |
1939 | | Decl *AssociatedDecl, const NonTypeTemplateParmDecl *parm, |
1940 | | SourceLocation loc, TemplateArgument arg, |
1941 | 0 | std::optional<unsigned> PackIndex) { |
1942 | 0 | ExprResult result; |
1943 | | |
1944 | | // Determine the substituted parameter type. We can usually infer this from |
1945 | | // the template argument, but not always. |
1946 | 0 | auto SubstParamType = [&] { |
1947 | 0 | QualType T; |
1948 | 0 | if (parm->isExpandedParameterPack()) |
1949 | 0 | T = parm->getExpansionType(SemaRef.ArgumentPackSubstitutionIndex); |
1950 | 0 | else |
1951 | 0 | T = parm->getType(); |
1952 | 0 | if (parm->isParameterPack() && isa<PackExpansionType>(T)) |
1953 | 0 | T = cast<PackExpansionType>(T)->getPattern(); |
1954 | 0 | return SemaRef.SubstType(T, TemplateArgs, loc, parm->getDeclName()); |
1955 | 0 | }; |
1956 | |
|
1957 | 0 | bool refParam = false; |
1958 | | |
1959 | | // The template argument itself might be an expression, in which case we just |
1960 | | // return that expression. This happens when substituting into an alias |
1961 | | // template. |
1962 | 0 | if (arg.getKind() == TemplateArgument::Expression) { |
1963 | 0 | Expr *argExpr = arg.getAsExpr(); |
1964 | 0 | result = argExpr; |
1965 | 0 | if (argExpr->isLValue()) { |
1966 | 0 | if (argExpr->getType()->isRecordType()) { |
1967 | | // Check whether the parameter was actually a reference. |
1968 | 0 | QualType paramType = SubstParamType(); |
1969 | 0 | if (paramType.isNull()) |
1970 | 0 | return ExprError(); |
1971 | 0 | refParam = paramType->isReferenceType(); |
1972 | 0 | } else { |
1973 | 0 | refParam = true; |
1974 | 0 | } |
1975 | 0 | } |
1976 | 0 | } else if (arg.getKind() == TemplateArgument::Declaration || |
1977 | 0 | arg.getKind() == TemplateArgument::NullPtr) { |
1978 | 0 | ValueDecl *VD; |
1979 | 0 | if (arg.getKind() == TemplateArgument::Declaration) { |
1980 | 0 | VD = arg.getAsDecl(); |
1981 | | |
1982 | | // Find the instantiation of the template argument. This is |
1983 | | // required for nested templates. |
1984 | 0 | VD = cast_or_null<ValueDecl>( |
1985 | 0 | getSema().FindInstantiatedDecl(loc, VD, TemplateArgs)); |
1986 | 0 | if (!VD) |
1987 | 0 | return ExprError(); |
1988 | 0 | } else { |
1989 | | // Propagate NULL template argument. |
1990 | 0 | VD = nullptr; |
1991 | 0 | } |
1992 | | |
1993 | 0 | QualType paramType = VD ? arg.getParamTypeForDecl() : arg.getNullPtrType(); |
1994 | 0 | assert(!paramType.isNull() && "type substitution failed for param type"); |
1995 | 0 | assert(!paramType->isDependentType() && "param type still dependent"); |
1996 | 0 | result = SemaRef.BuildExpressionFromDeclTemplateArgument(arg, paramType, loc); |
1997 | 0 | refParam = paramType->isReferenceType(); |
1998 | 0 | } else { |
1999 | 0 | result = SemaRef.BuildExpressionFromIntegralTemplateArgument(arg, loc); |
2000 | 0 | assert(result.isInvalid() || |
2001 | 0 | SemaRef.Context.hasSameType(result.get()->getType(), |
2002 | 0 | arg.getIntegralType())); |
2003 | 0 | } |
2004 | | |
2005 | 0 | if (result.isInvalid()) |
2006 | 0 | return ExprError(); |
2007 | | |
2008 | 0 | Expr *resultExpr = result.get(); |
2009 | | // FIXME: Don't put subst node on final replacement. |
2010 | 0 | return new (SemaRef.Context) SubstNonTypeTemplateParmExpr( |
2011 | 0 | resultExpr->getType(), resultExpr->getValueKind(), loc, resultExpr, |
2012 | 0 | AssociatedDecl, parm->getIndex(), PackIndex, refParam); |
2013 | 0 | } |
2014 | | |
2015 | | ExprResult |
2016 | | TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr( |
2017 | 0 | SubstNonTypeTemplateParmPackExpr *E) { |
2018 | 0 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
2019 | | // We aren't expanding the parameter pack, so just return ourselves. |
2020 | 0 | return E; |
2021 | 0 | } |
2022 | | |
2023 | 0 | TemplateArgument Pack = E->getArgumentPack(); |
2024 | 0 | TemplateArgument Arg = getPackSubstitutedTemplateArgument(getSema(), Pack); |
2025 | | // FIXME: Don't put subst node on final replacement. |
2026 | 0 | return transformNonTypeTemplateParmRef( |
2027 | 0 | E->getAssociatedDecl(), E->getParameterPack(), |
2028 | 0 | E->getParameterPackLocation(), Arg, getPackIndex(Pack)); |
2029 | 0 | } |
2030 | | |
2031 | | ExprResult |
2032 | | TemplateInstantiator::TransformSubstNonTypeTemplateParmExpr( |
2033 | 0 | SubstNonTypeTemplateParmExpr *E) { |
2034 | 0 | ExprResult SubstReplacement = E->getReplacement(); |
2035 | 0 | if (!isa<ConstantExpr>(SubstReplacement.get())) |
2036 | 0 | SubstReplacement = TransformExpr(E->getReplacement()); |
2037 | 0 | if (SubstReplacement.isInvalid()) |
2038 | 0 | return true; |
2039 | 0 | QualType SubstType = TransformType(E->getParameterType(getSema().Context)); |
2040 | 0 | if (SubstType.isNull()) |
2041 | 0 | return true; |
2042 | | // The type may have been previously dependent and not now, which means we |
2043 | | // might have to implicit cast the argument to the new type, for example: |
2044 | | // template<auto T, decltype(T) U> |
2045 | | // concept C = sizeof(U) == 4; |
2046 | | // void foo() requires C<2, 'a'> { } |
2047 | | // When normalizing foo(), we first form the normalized constraints of C: |
2048 | | // AtomicExpr(sizeof(U) == 4, |
2049 | | // U=SubstNonTypeTemplateParmExpr(Param=U, |
2050 | | // Expr=DeclRef(U), |
2051 | | // Type=decltype(T))) |
2052 | | // Then we substitute T = 2, U = 'a' into the parameter mapping, and need to |
2053 | | // produce: |
2054 | | // AtomicExpr(sizeof(U) == 4, |
2055 | | // U=SubstNonTypeTemplateParmExpr(Param=U, |
2056 | | // Expr=ImpCast( |
2057 | | // decltype(2), |
2058 | | // SubstNTTPE(Param=U, Expr='a', |
2059 | | // Type=char)), |
2060 | | // Type=decltype(2))) |
2061 | | // The call to CheckTemplateArgument here produces the ImpCast. |
2062 | 0 | TemplateArgument SugaredConverted, CanonicalConverted; |
2063 | 0 | if (SemaRef |
2064 | 0 | .CheckTemplateArgument(E->getParameter(), SubstType, |
2065 | 0 | SubstReplacement.get(), SugaredConverted, |
2066 | 0 | CanonicalConverted, Sema::CTAK_Specified) |
2067 | 0 | .isInvalid()) |
2068 | 0 | return true; |
2069 | 0 | return transformNonTypeTemplateParmRef(E->getAssociatedDecl(), |
2070 | 0 | E->getParameter(), E->getExprLoc(), |
2071 | 0 | SugaredConverted, E->getPackIndex()); |
2072 | 0 | } |
2073 | | |
2074 | | ExprResult TemplateInstantiator::RebuildVarDeclRefExpr(VarDecl *PD, |
2075 | 0 | SourceLocation Loc) { |
2076 | 0 | DeclarationNameInfo NameInfo(PD->getDeclName(), Loc); |
2077 | 0 | return getSema().BuildDeclarationNameExpr(CXXScopeSpec(), NameInfo, PD); |
2078 | 0 | } |
2079 | | |
2080 | | ExprResult |
2081 | 0 | TemplateInstantiator::TransformFunctionParmPackExpr(FunctionParmPackExpr *E) { |
2082 | 0 | if (getSema().ArgumentPackSubstitutionIndex != -1) { |
2083 | | // We can expand this parameter pack now. |
2084 | 0 | VarDecl *D = E->getExpansion(getSema().ArgumentPackSubstitutionIndex); |
2085 | 0 | VarDecl *VD = cast_or_null<VarDecl>(TransformDecl(E->getExprLoc(), D)); |
2086 | 0 | if (!VD) |
2087 | 0 | return ExprError(); |
2088 | 0 | return RebuildVarDeclRefExpr(VD, E->getExprLoc()); |
2089 | 0 | } |
2090 | | |
2091 | 0 | QualType T = TransformType(E->getType()); |
2092 | 0 | if (T.isNull()) |
2093 | 0 | return ExprError(); |
2094 | | |
2095 | | // Transform each of the parameter expansions into the corresponding |
2096 | | // parameters in the instantiation of the function decl. |
2097 | 0 | SmallVector<VarDecl *, 8> Vars; |
2098 | 0 | Vars.reserve(E->getNumExpansions()); |
2099 | 0 | for (FunctionParmPackExpr::iterator I = E->begin(), End = E->end(); |
2100 | 0 | I != End; ++I) { |
2101 | 0 | VarDecl *D = cast_or_null<VarDecl>(TransformDecl(E->getExprLoc(), *I)); |
2102 | 0 | if (!D) |
2103 | 0 | return ExprError(); |
2104 | 0 | Vars.push_back(D); |
2105 | 0 | } |
2106 | | |
2107 | 0 | auto *PackExpr = |
2108 | 0 | FunctionParmPackExpr::Create(getSema().Context, T, E->getParameterPack(), |
2109 | 0 | E->getParameterPackLocation(), Vars); |
2110 | 0 | getSema().MarkFunctionParmPackReferenced(PackExpr); |
2111 | 0 | return PackExpr; |
2112 | 0 | } |
2113 | | |
2114 | | ExprResult |
2115 | | TemplateInstantiator::TransformFunctionParmPackRefExpr(DeclRefExpr *E, |
2116 | 0 | VarDecl *PD) { |
2117 | 0 | typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; |
2118 | 0 | llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found |
2119 | 0 | = getSema().CurrentInstantiationScope->findInstantiationOf(PD); |
2120 | 0 | assert(Found && "no instantiation for parameter pack"); |
2121 | | |
2122 | 0 | Decl *TransformedDecl; |
2123 | 0 | if (DeclArgumentPack *Pack = Found->dyn_cast<DeclArgumentPack *>()) { |
2124 | | // If this is a reference to a function parameter pack which we can |
2125 | | // substitute but can't yet expand, build a FunctionParmPackExpr for it. |
2126 | 0 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
2127 | 0 | QualType T = TransformType(E->getType()); |
2128 | 0 | if (T.isNull()) |
2129 | 0 | return ExprError(); |
2130 | 0 | auto *PackExpr = FunctionParmPackExpr::Create(getSema().Context, T, PD, |
2131 | 0 | E->getExprLoc(), *Pack); |
2132 | 0 | getSema().MarkFunctionParmPackReferenced(PackExpr); |
2133 | 0 | return PackExpr; |
2134 | 0 | } |
2135 | | |
2136 | 0 | TransformedDecl = (*Pack)[getSema().ArgumentPackSubstitutionIndex]; |
2137 | 0 | } else { |
2138 | 0 | TransformedDecl = Found->get<Decl*>(); |
2139 | 0 | } |
2140 | | |
2141 | | // We have either an unexpanded pack or a specific expansion. |
2142 | 0 | return RebuildVarDeclRefExpr(cast<VarDecl>(TransformedDecl), E->getExprLoc()); |
2143 | 0 | } |
2144 | | |
2145 | | ExprResult |
2146 | 0 | TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) { |
2147 | 0 | NamedDecl *D = E->getDecl(); |
2148 | | |
2149 | | // Handle references to non-type template parameters and non-type template |
2150 | | // parameter packs. |
2151 | 0 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) { |
2152 | 0 | if (NTTP->getDepth() < TemplateArgs.getNumLevels()) |
2153 | 0 | return TransformTemplateParmRefExpr(E, NTTP); |
2154 | | |
2155 | | // We have a non-type template parameter that isn't fully substituted; |
2156 | | // FindInstantiatedDecl will find it in the local instantiation scope. |
2157 | 0 | } |
2158 | | |
2159 | | // Handle references to function parameter packs. |
2160 | 0 | if (VarDecl *PD = dyn_cast<VarDecl>(D)) |
2161 | 0 | if (PD->isParameterPack()) |
2162 | 0 | return TransformFunctionParmPackRefExpr(E, PD); |
2163 | | |
2164 | 0 | return inherited::TransformDeclRefExpr(E); |
2165 | 0 | } |
2166 | | |
2167 | | ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr( |
2168 | 0 | CXXDefaultArgExpr *E) { |
2169 | 0 | assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())-> |
2170 | 0 | getDescribedFunctionTemplate() && |
2171 | 0 | "Default arg expressions are never formed in dependent cases."); |
2172 | 0 | return SemaRef.BuildCXXDefaultArgExpr( |
2173 | 0 | E->getUsedLocation(), cast<FunctionDecl>(E->getParam()->getDeclContext()), |
2174 | 0 | E->getParam()); |
2175 | 0 | } |
2176 | | |
2177 | | template<typename Fn> |
2178 | | QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB, |
2179 | | FunctionProtoTypeLoc TL, |
2180 | | CXXRecordDecl *ThisContext, |
2181 | | Qualifiers ThisTypeQuals, |
2182 | 0 | Fn TransformExceptionSpec) { |
2183 | | // We need a local instantiation scope for this function prototype. |
2184 | 0 | LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true); |
2185 | 0 | return inherited::TransformFunctionProtoType( |
2186 | 0 | TLB, TL, ThisContext, ThisTypeQuals, TransformExceptionSpec); |
2187 | 0 | } Unexecuted instantiation: SemaTemplateInstantiate.cpp:clang::QualType (anonymous namespace)::TemplateInstantiator::TransformFunctionProtoType<clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformFunctionProtoType(clang::TypeLocBuilder&, clang::FunctionProtoTypeLoc)::{lambda(clang::FunctionProtoType::ExceptionSpecInfo&, bool&)#1}>(clang::TypeLocBuilder&, clang::FunctionProtoTypeLoc, clang::CXXRecordDecl*, clang::Qualifiers, clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformFunctionProtoType(clang::TypeLocBuilder&, clang::FunctionProtoTypeLoc)::{lambda(clang::FunctionProtoType::ExceptionSpecInfo&, bool&)#1}) Unexecuted instantiation: SemaTemplateInstantiate.cpp:clang::QualType (anonymous namespace)::TemplateInstantiator::TransformFunctionProtoType<clang::Sema::SubstFunctionDeclType(clang::TypeSourceInfo*, clang::MultiLevelTemplateArgumentList const&, clang::SourceLocation, clang::DeclarationName, clang::CXXRecordDecl*, clang::Qualifiers, bool)::$_0>(clang::TypeLocBuilder&, clang::FunctionProtoTypeLoc, clang::CXXRecordDecl*, clang::Qualifiers, clang::Sema::SubstFunctionDeclType(clang::TypeSourceInfo*, clang::MultiLevelTemplateArgumentList const&, clang::SourceLocation, clang::DeclarationName, clang::CXXRecordDecl*, clang::Qualifiers, bool)::$_0) |
2188 | | |
2189 | | ParmVarDecl *TemplateInstantiator::TransformFunctionTypeParam( |
2190 | | ParmVarDecl *OldParm, int indexAdjustment, |
2191 | 0 | std::optional<unsigned> NumExpansions, bool ExpectParameterPack) { |
2192 | 0 | auto NewParm = SemaRef.SubstParmVarDecl( |
2193 | 0 | OldParm, TemplateArgs, indexAdjustment, NumExpansions, |
2194 | 0 | ExpectParameterPack, EvaluateConstraints); |
2195 | 0 | if (NewParm && SemaRef.getLangOpts().OpenCL) |
2196 | 0 | SemaRef.deduceOpenCLAddressSpace(NewParm); |
2197 | 0 | return NewParm; |
2198 | 0 | } |
2199 | | |
2200 | | QualType TemplateInstantiator::BuildSubstTemplateTypeParmType( |
2201 | | TypeLocBuilder &TLB, bool SuppressObjCLifetime, bool Final, |
2202 | | Decl *AssociatedDecl, unsigned Index, std::optional<unsigned> PackIndex, |
2203 | 0 | TemplateArgument Arg, SourceLocation NameLoc) { |
2204 | 0 | QualType Replacement = Arg.getAsType(); |
2205 | | |
2206 | | // If the template parameter had ObjC lifetime qualifiers, |
2207 | | // then any such qualifiers on the replacement type are ignored. |
2208 | 0 | if (SuppressObjCLifetime) { |
2209 | 0 | Qualifiers RQs; |
2210 | 0 | RQs = Replacement.getQualifiers(); |
2211 | 0 | RQs.removeObjCLifetime(); |
2212 | 0 | Replacement = |
2213 | 0 | SemaRef.Context.getQualifiedType(Replacement.getUnqualifiedType(), RQs); |
2214 | 0 | } |
2215 | |
|
2216 | 0 | if (Final) { |
2217 | 0 | TLB.pushTrivial(SemaRef.Context, Replacement, NameLoc); |
2218 | 0 | return Replacement; |
2219 | 0 | } |
2220 | | // TODO: only do this uniquing once, at the start of instantiation. |
2221 | 0 | QualType Result = getSema().Context.getSubstTemplateTypeParmType( |
2222 | 0 | Replacement, AssociatedDecl, Index, PackIndex); |
2223 | 0 | SubstTemplateTypeParmTypeLoc NewTL = |
2224 | 0 | TLB.push<SubstTemplateTypeParmTypeLoc>(Result); |
2225 | 0 | NewTL.setNameLoc(NameLoc); |
2226 | 0 | return Result; |
2227 | 0 | } |
2228 | | |
2229 | | QualType |
2230 | | TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB, |
2231 | | TemplateTypeParmTypeLoc TL, |
2232 | 0 | bool SuppressObjCLifetime) { |
2233 | 0 | const TemplateTypeParmType *T = TL.getTypePtr(); |
2234 | 0 | if (T->getDepth() < TemplateArgs.getNumLevels()) { |
2235 | | // Replace the template type parameter with its corresponding |
2236 | | // template argument. |
2237 | | |
2238 | | // If the corresponding template argument is NULL or doesn't exist, it's |
2239 | | // because we are performing instantiation from explicitly-specified |
2240 | | // template arguments in a function template class, but there were some |
2241 | | // arguments left unspecified. |
2242 | 0 | if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) { |
2243 | 0 | TemplateTypeParmTypeLoc NewTL |
2244 | 0 | = TLB.push<TemplateTypeParmTypeLoc>(TL.getType()); |
2245 | 0 | NewTL.setNameLoc(TL.getNameLoc()); |
2246 | 0 | return TL.getType(); |
2247 | 0 | } |
2248 | | |
2249 | 0 | TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex()); |
2250 | |
|
2251 | 0 | if (TemplateArgs.isRewrite()) { |
2252 | | // We're rewriting the template parameter as a reference to another |
2253 | | // template parameter. |
2254 | 0 | if (Arg.getKind() == TemplateArgument::Pack) { |
2255 | 0 | assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion() && |
2256 | 0 | "unexpected pack arguments in template rewrite"); |
2257 | 0 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
2258 | 0 | } |
2259 | 0 | assert(Arg.getKind() == TemplateArgument::Type && |
2260 | 0 | "unexpected nontype template argument kind in template rewrite"); |
2261 | 0 | QualType NewT = Arg.getAsType(); |
2262 | 0 | assert(isa<TemplateTypeParmType>(NewT) && |
2263 | 0 | "type parm not rewritten to type parm"); |
2264 | 0 | auto NewTL = TLB.push<TemplateTypeParmTypeLoc>(NewT); |
2265 | 0 | NewTL.setNameLoc(TL.getNameLoc()); |
2266 | 0 | return NewT; |
2267 | 0 | } |
2268 | | |
2269 | 0 | auto [AssociatedDecl, Final] = |
2270 | 0 | TemplateArgs.getAssociatedDecl(T->getDepth()); |
2271 | 0 | std::optional<unsigned> PackIndex; |
2272 | 0 | if (T->isParameterPack()) { |
2273 | 0 | assert(Arg.getKind() == TemplateArgument::Pack && |
2274 | 0 | "Missing argument pack"); |
2275 | | |
2276 | 0 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
2277 | | // We have the template argument pack, but we're not expanding the |
2278 | | // enclosing pack expansion yet. Just save the template argument |
2279 | | // pack for later substitution. |
2280 | 0 | QualType Result = getSema().Context.getSubstTemplateTypeParmPackType( |
2281 | 0 | AssociatedDecl, T->getIndex(), Final, Arg); |
2282 | 0 | SubstTemplateTypeParmPackTypeLoc NewTL |
2283 | 0 | = TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result); |
2284 | 0 | NewTL.setNameLoc(TL.getNameLoc()); |
2285 | 0 | return Result; |
2286 | 0 | } |
2287 | | |
2288 | | // PackIndex starts from last element. |
2289 | 0 | PackIndex = getPackIndex(Arg); |
2290 | 0 | Arg = getPackSubstitutedTemplateArgument(getSema(), Arg); |
2291 | 0 | } |
2292 | | |
2293 | 0 | assert(Arg.getKind() == TemplateArgument::Type && |
2294 | 0 | "Template argument kind mismatch"); |
2295 | | |
2296 | 0 | return BuildSubstTemplateTypeParmType(TLB, SuppressObjCLifetime, Final, |
2297 | 0 | AssociatedDecl, T->getIndex(), |
2298 | 0 | PackIndex, Arg, TL.getNameLoc()); |
2299 | 0 | } |
2300 | | |
2301 | | // The template type parameter comes from an inner template (e.g., |
2302 | | // the template parameter list of a member template inside the |
2303 | | // template we are instantiating). Create a new template type |
2304 | | // parameter with the template "level" reduced by one. |
2305 | 0 | TemplateTypeParmDecl *NewTTPDecl = nullptr; |
2306 | 0 | if (TemplateTypeParmDecl *OldTTPDecl = T->getDecl()) |
2307 | 0 | NewTTPDecl = cast_or_null<TemplateTypeParmDecl>( |
2308 | 0 | TransformDecl(TL.getNameLoc(), OldTTPDecl)); |
2309 | 0 | QualType Result = getSema().Context.getTemplateTypeParmType( |
2310 | 0 | T->getDepth() - TemplateArgs.getNumSubstitutedLevels(), T->getIndex(), |
2311 | 0 | T->isParameterPack(), NewTTPDecl); |
2312 | 0 | TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); |
2313 | 0 | NewTL.setNameLoc(TL.getNameLoc()); |
2314 | 0 | return Result; |
2315 | 0 | } |
2316 | | |
2317 | | QualType TemplateInstantiator::TransformSubstTemplateTypeParmPackType( |
2318 | | TypeLocBuilder &TLB, SubstTemplateTypeParmPackTypeLoc TL, |
2319 | 0 | bool SuppressObjCLifetime) { |
2320 | 0 | const SubstTemplateTypeParmPackType *T = TL.getTypePtr(); |
2321 | |
|
2322 | 0 | Decl *NewReplaced = TransformDecl(TL.getNameLoc(), T->getAssociatedDecl()); |
2323 | |
|
2324 | 0 | if (getSema().ArgumentPackSubstitutionIndex == -1) { |
2325 | | // We aren't expanding the parameter pack, so just return ourselves. |
2326 | 0 | QualType Result = TL.getType(); |
2327 | 0 | if (NewReplaced != T->getAssociatedDecl()) |
2328 | 0 | Result = getSema().Context.getSubstTemplateTypeParmPackType( |
2329 | 0 | NewReplaced, T->getIndex(), T->getFinal(), T->getArgumentPack()); |
2330 | 0 | SubstTemplateTypeParmPackTypeLoc NewTL = |
2331 | 0 | TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result); |
2332 | 0 | NewTL.setNameLoc(TL.getNameLoc()); |
2333 | 0 | return Result; |
2334 | 0 | } |
2335 | | |
2336 | 0 | TemplateArgument Pack = T->getArgumentPack(); |
2337 | 0 | TemplateArgument Arg = getPackSubstitutedTemplateArgument(getSema(), Pack); |
2338 | 0 | return BuildSubstTemplateTypeParmType( |
2339 | 0 | TLB, SuppressObjCLifetime, T->getFinal(), NewReplaced, T->getIndex(), |
2340 | 0 | getPackIndex(Pack), Arg, TL.getNameLoc()); |
2341 | 0 | } |
2342 | | |
2343 | | static concepts::Requirement::SubstitutionDiagnostic * |
2344 | | createSubstDiag(Sema &S, TemplateDeductionInfo &Info, |
2345 | 0 | concepts::EntityPrinter Printer) { |
2346 | 0 | SmallString<128> Message; |
2347 | 0 | SourceLocation ErrorLoc; |
2348 | 0 | if (Info.hasSFINAEDiagnostic()) { |
2349 | 0 | PartialDiagnosticAt PDA(SourceLocation(), |
2350 | 0 | PartialDiagnostic::NullDiagnostic{}); |
2351 | 0 | Info.takeSFINAEDiagnostic(PDA); |
2352 | 0 | PDA.second.EmitToString(S.getDiagnostics(), Message); |
2353 | 0 | ErrorLoc = PDA.first; |
2354 | 0 | } else { |
2355 | 0 | ErrorLoc = Info.getLocation(); |
2356 | 0 | } |
2357 | 0 | char *MessageBuf = new (S.Context) char[Message.size()]; |
2358 | 0 | std::copy(Message.begin(), Message.end(), MessageBuf); |
2359 | 0 | SmallString<128> Entity; |
2360 | 0 | llvm::raw_svector_ostream OS(Entity); |
2361 | 0 | Printer(OS); |
2362 | 0 | char *EntityBuf = new (S.Context) char[Entity.size()]; |
2363 | 0 | std::copy(Entity.begin(), Entity.end(), EntityBuf); |
2364 | 0 | return new (S.Context) concepts::Requirement::SubstitutionDiagnostic{ |
2365 | 0 | StringRef(EntityBuf, Entity.size()), ErrorLoc, |
2366 | 0 | StringRef(MessageBuf, Message.size())}; |
2367 | 0 | } |
2368 | | |
2369 | | concepts::Requirement::SubstitutionDiagnostic * |
2370 | | concepts::createSubstDiagAt(Sema &S, SourceLocation Location, |
2371 | 0 | EntityPrinter Printer) { |
2372 | 0 | SmallString<128> Entity; |
2373 | 0 | llvm::raw_svector_ostream OS(Entity); |
2374 | 0 | Printer(OS); |
2375 | 0 | char *EntityBuf = new (S.Context) char[Entity.size()]; |
2376 | 0 | llvm::copy(Entity, EntityBuf); |
2377 | 0 | return new (S.Context) concepts::Requirement::SubstitutionDiagnostic{ |
2378 | 0 | /*SubstitutedEntity=*/StringRef(EntityBuf, Entity.size()), |
2379 | 0 | /*DiagLoc=*/Location, /*DiagMessage=*/StringRef()}; |
2380 | 0 | } |
2381 | | |
2382 | | ExprResult TemplateInstantiator::TransformRequiresTypeParams( |
2383 | | SourceLocation KWLoc, SourceLocation RBraceLoc, const RequiresExpr *RE, |
2384 | | RequiresExprBodyDecl *Body, ArrayRef<ParmVarDecl *> Params, |
2385 | | SmallVectorImpl<QualType> &PTypes, |
2386 | | SmallVectorImpl<ParmVarDecl *> &TransParams, |
2387 | 0 | Sema::ExtParameterInfoBuilder &PInfos) { |
2388 | |
|
2389 | 0 | TemplateDeductionInfo Info(KWLoc); |
2390 | 0 | Sema::InstantiatingTemplate TypeInst(SemaRef, KWLoc, |
2391 | 0 | RE, Info, |
2392 | 0 | SourceRange{KWLoc, RBraceLoc}); |
2393 | 0 | Sema::SFINAETrap Trap(SemaRef); |
2394 | |
|
2395 | 0 | unsigned ErrorIdx; |
2396 | 0 | if (getDerived().TransformFunctionTypeParams( |
2397 | 0 | KWLoc, Params, /*ParamTypes=*/nullptr, /*ParamInfos=*/nullptr, PTypes, |
2398 | 0 | &TransParams, PInfos, &ErrorIdx) || |
2399 | 0 | Trap.hasErrorOccurred()) { |
2400 | 0 | SmallVector<concepts::Requirement *, 4> TransReqs; |
2401 | 0 | ParmVarDecl *FailedDecl = Params[ErrorIdx]; |
2402 | | // Add a 'failed' Requirement to contain the error that caused the failure |
2403 | | // here. |
2404 | 0 | TransReqs.push_back(RebuildTypeRequirement(createSubstDiag( |
2405 | 0 | SemaRef, Info, [&](llvm::raw_ostream &OS) { OS << *FailedDecl; }))); |
2406 | 0 | return getDerived().RebuildRequiresExpr(KWLoc, Body, RE->getLParenLoc(), |
2407 | 0 | TransParams, RE->getRParenLoc(), |
2408 | 0 | TransReqs, RBraceLoc); |
2409 | 0 | } |
2410 | | |
2411 | 0 | return ExprResult{}; |
2412 | 0 | } |
2413 | | |
2414 | | concepts::TypeRequirement * |
2415 | 0 | TemplateInstantiator::TransformTypeRequirement(concepts::TypeRequirement *Req) { |
2416 | 0 | if (!Req->isDependent() && !AlwaysRebuild()) |
2417 | 0 | return Req; |
2418 | 0 | if (Req->isSubstitutionFailure()) { |
2419 | 0 | if (AlwaysRebuild()) |
2420 | 0 | return RebuildTypeRequirement( |
2421 | 0 | Req->getSubstitutionDiagnostic()); |
2422 | 0 | return Req; |
2423 | 0 | } |
2424 | | |
2425 | 0 | Sema::SFINAETrap Trap(SemaRef); |
2426 | 0 | TemplateDeductionInfo Info(Req->getType()->getTypeLoc().getBeginLoc()); |
2427 | 0 | Sema::InstantiatingTemplate TypeInst(SemaRef, |
2428 | 0 | Req->getType()->getTypeLoc().getBeginLoc(), Req, Info, |
2429 | 0 | Req->getType()->getTypeLoc().getSourceRange()); |
2430 | 0 | if (TypeInst.isInvalid()) |
2431 | 0 | return nullptr; |
2432 | 0 | TypeSourceInfo *TransType = TransformType(Req->getType()); |
2433 | 0 | if (!TransType || Trap.hasErrorOccurred()) |
2434 | 0 | return RebuildTypeRequirement(createSubstDiag(SemaRef, Info, |
2435 | 0 | [&] (llvm::raw_ostream& OS) { |
2436 | 0 | Req->getType()->getType().print(OS, SemaRef.getPrintingPolicy()); |
2437 | 0 | })); |
2438 | 0 | return RebuildTypeRequirement(TransType); |
2439 | 0 | } |
2440 | | |
2441 | | concepts::ExprRequirement * |
2442 | 0 | TemplateInstantiator::TransformExprRequirement(concepts::ExprRequirement *Req) { |
2443 | 0 | if (!Req->isDependent() && !AlwaysRebuild()) |
2444 | 0 | return Req; |
2445 | | |
2446 | 0 | Sema::SFINAETrap Trap(SemaRef); |
2447 | |
|
2448 | 0 | llvm::PointerUnion<Expr *, concepts::Requirement::SubstitutionDiagnostic *> |
2449 | 0 | TransExpr; |
2450 | 0 | if (Req->isExprSubstitutionFailure()) |
2451 | 0 | TransExpr = Req->getExprSubstitutionDiagnostic(); |
2452 | 0 | else { |
2453 | 0 | Expr *E = Req->getExpr(); |
2454 | 0 | TemplateDeductionInfo Info(E->getBeginLoc()); |
2455 | 0 | Sema::InstantiatingTemplate ExprInst(SemaRef, E->getBeginLoc(), Req, Info, |
2456 | 0 | E->getSourceRange()); |
2457 | 0 | if (ExprInst.isInvalid()) |
2458 | 0 | return nullptr; |
2459 | 0 | ExprResult TransExprRes = TransformExpr(E); |
2460 | 0 | if (!TransExprRes.isInvalid() && !Trap.hasErrorOccurred() && |
2461 | 0 | TransExprRes.get()->hasPlaceholderType()) |
2462 | 0 | TransExprRes = SemaRef.CheckPlaceholderExpr(TransExprRes.get()); |
2463 | 0 | if (TransExprRes.isInvalid() || Trap.hasErrorOccurred()) |
2464 | 0 | TransExpr = createSubstDiag(SemaRef, Info, [&](llvm::raw_ostream &OS) { |
2465 | 0 | E->printPretty(OS, nullptr, SemaRef.getPrintingPolicy()); |
2466 | 0 | }); |
2467 | 0 | else |
2468 | 0 | TransExpr = TransExprRes.get(); |
2469 | 0 | } |
2470 | | |
2471 | 0 | std::optional<concepts::ExprRequirement::ReturnTypeRequirement> TransRetReq; |
2472 | 0 | const auto &RetReq = Req->getReturnTypeRequirement(); |
2473 | 0 | if (RetReq.isEmpty()) |
2474 | 0 | TransRetReq.emplace(); |
2475 | 0 | else if (RetReq.isSubstitutionFailure()) |
2476 | 0 | TransRetReq.emplace(RetReq.getSubstitutionDiagnostic()); |
2477 | 0 | else if (RetReq.isTypeConstraint()) { |
2478 | 0 | TemplateParameterList *OrigTPL = |
2479 | 0 | RetReq.getTypeConstraintTemplateParameterList(); |
2480 | 0 | TemplateDeductionInfo Info(OrigTPL->getTemplateLoc()); |
2481 | 0 | Sema::InstantiatingTemplate TPLInst(SemaRef, OrigTPL->getTemplateLoc(), |
2482 | 0 | Req, Info, OrigTPL->getSourceRange()); |
2483 | 0 | if (TPLInst.isInvalid()) |
2484 | 0 | return nullptr; |
2485 | 0 | TemplateParameterList *TPL = TransformTemplateParameterList(OrigTPL); |
2486 | 0 | if (!TPL) |
2487 | 0 | TransRetReq.emplace(createSubstDiag(SemaRef, Info, |
2488 | 0 | [&] (llvm::raw_ostream& OS) { |
2489 | 0 | RetReq.getTypeConstraint()->getImmediatelyDeclaredConstraint() |
2490 | 0 | ->printPretty(OS, nullptr, SemaRef.getPrintingPolicy()); |
2491 | 0 | })); |
2492 | 0 | else { |
2493 | 0 | TPLInst.Clear(); |
2494 | 0 | TransRetReq.emplace(TPL); |
2495 | 0 | } |
2496 | 0 | } |
2497 | 0 | assert(TransRetReq && "All code paths leading here must set TransRetReq"); |
2498 | 0 | if (Expr *E = TransExpr.dyn_cast<Expr *>()) |
2499 | 0 | return RebuildExprRequirement(E, Req->isSimple(), Req->getNoexceptLoc(), |
2500 | 0 | std::move(*TransRetReq)); |
2501 | 0 | return RebuildExprRequirement( |
2502 | 0 | TransExpr.get<concepts::Requirement::SubstitutionDiagnostic *>(), |
2503 | 0 | Req->isSimple(), Req->getNoexceptLoc(), std::move(*TransRetReq)); |
2504 | 0 | } |
2505 | | |
2506 | | concepts::NestedRequirement * |
2507 | | TemplateInstantiator::TransformNestedRequirement( |
2508 | 0 | concepts::NestedRequirement *Req) { |
2509 | 0 | if (!Req->isDependent() && !AlwaysRebuild()) |
2510 | 0 | return Req; |
2511 | 0 | if (Req->hasInvalidConstraint()) { |
2512 | 0 | if (AlwaysRebuild()) |
2513 | 0 | return RebuildNestedRequirement(Req->getInvalidConstraintEntity(), |
2514 | 0 | Req->getConstraintSatisfaction()); |
2515 | 0 | return Req; |
2516 | 0 | } |
2517 | 0 | Sema::InstantiatingTemplate ReqInst(SemaRef, |
2518 | 0 | Req->getConstraintExpr()->getBeginLoc(), Req, |
2519 | 0 | Sema::InstantiatingTemplate::ConstraintsCheck{}, |
2520 | 0 | Req->getConstraintExpr()->getSourceRange()); |
2521 | 0 | if (!getEvaluateConstraints()) { |
2522 | 0 | ExprResult TransConstraint = TransformExpr(Req->getConstraintExpr()); |
2523 | 0 | if (TransConstraint.isInvalid() || !TransConstraint.get()) |
2524 | 0 | return nullptr; |
2525 | 0 | if (TransConstraint.get()->isInstantiationDependent()) |
2526 | 0 | return new (SemaRef.Context) |
2527 | 0 | concepts::NestedRequirement(TransConstraint.get()); |
2528 | 0 | ConstraintSatisfaction Satisfaction; |
2529 | 0 | return new (SemaRef.Context) concepts::NestedRequirement( |
2530 | 0 | SemaRef.Context, TransConstraint.get(), Satisfaction); |
2531 | 0 | } |
2532 | | |
2533 | 0 | ExprResult TransConstraint; |
2534 | 0 | ConstraintSatisfaction Satisfaction; |
2535 | 0 | TemplateDeductionInfo Info(Req->getConstraintExpr()->getBeginLoc()); |
2536 | 0 | { |
2537 | 0 | EnterExpressionEvaluationContext ContextRAII( |
2538 | 0 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
2539 | 0 | Sema::SFINAETrap Trap(SemaRef); |
2540 | 0 | Sema::InstantiatingTemplate ConstrInst(SemaRef, |
2541 | 0 | Req->getConstraintExpr()->getBeginLoc(), Req, Info, |
2542 | 0 | Req->getConstraintExpr()->getSourceRange()); |
2543 | 0 | if (ConstrInst.isInvalid()) |
2544 | 0 | return nullptr; |
2545 | 0 | llvm::SmallVector<Expr *> Result; |
2546 | 0 | if (!SemaRef.CheckConstraintSatisfaction( |
2547 | 0 | nullptr, {Req->getConstraintExpr()}, Result, TemplateArgs, |
2548 | 0 | Req->getConstraintExpr()->getSourceRange(), Satisfaction) && |
2549 | 0 | !Result.empty()) |
2550 | 0 | TransConstraint = Result[0]; |
2551 | 0 | assert(!Trap.hasErrorOccurred() && "Substitution failures must be handled " |
2552 | 0 | "by CheckConstraintSatisfaction."); |
2553 | 0 | } |
2554 | 0 | if (TransConstraint.isUsable() && |
2555 | 0 | TransConstraint.get()->isInstantiationDependent()) |
2556 | 0 | return new (SemaRef.Context) |
2557 | 0 | concepts::NestedRequirement(TransConstraint.get()); |
2558 | 0 | if (TransConstraint.isInvalid() || !TransConstraint.get() || |
2559 | 0 | Satisfaction.HasSubstitutionFailure()) { |
2560 | 0 | SmallString<128> Entity; |
2561 | 0 | llvm::raw_svector_ostream OS(Entity); |
2562 | 0 | Req->getConstraintExpr()->printPretty(OS, nullptr, |
2563 | 0 | SemaRef.getPrintingPolicy()); |
2564 | 0 | char *EntityBuf = new (SemaRef.Context) char[Entity.size()]; |
2565 | 0 | std::copy(Entity.begin(), Entity.end(), EntityBuf); |
2566 | 0 | return new (SemaRef.Context) concepts::NestedRequirement( |
2567 | 0 | SemaRef.Context, StringRef(EntityBuf, Entity.size()), Satisfaction); |
2568 | 0 | } |
2569 | 0 | return new (SemaRef.Context) concepts::NestedRequirement( |
2570 | 0 | SemaRef.Context, TransConstraint.get(), Satisfaction); |
2571 | 0 | } |
2572 | | |
2573 | | |
2574 | | /// Perform substitution on the type T with a given set of template |
2575 | | /// arguments. |
2576 | | /// |
2577 | | /// This routine substitutes the given template arguments into the |
2578 | | /// type T and produces the instantiated type. |
2579 | | /// |
2580 | | /// \param T the type into which the template arguments will be |
2581 | | /// substituted. If this type is not dependent, it will be returned |
2582 | | /// immediately. |
2583 | | /// |
2584 | | /// \param Args the template arguments that will be |
2585 | | /// substituted for the top-level template parameters within T. |
2586 | | /// |
2587 | | /// \param Loc the location in the source code where this substitution |
2588 | | /// is being performed. It will typically be the location of the |
2589 | | /// declarator (if we're instantiating the type of some declaration) |
2590 | | /// or the location of the type in the source code (if, e.g., we're |
2591 | | /// instantiating the type of a cast expression). |
2592 | | /// |
2593 | | /// \param Entity the name of the entity associated with a declaration |
2594 | | /// being instantiated (if any). May be empty to indicate that there |
2595 | | /// is no such entity (if, e.g., this is a type that occurs as part of |
2596 | | /// a cast expression) or that the entity has no name (e.g., an |
2597 | | /// unnamed function parameter). |
2598 | | /// |
2599 | | /// \param AllowDeducedTST Whether a DeducedTemplateSpecializationType is |
2600 | | /// acceptable as the top level type of the result. |
2601 | | /// |
2602 | | /// \returns If the instantiation succeeds, the instantiated |
2603 | | /// type. Otherwise, produces diagnostics and returns a NULL type. |
2604 | | TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T, |
2605 | | const MultiLevelTemplateArgumentList &Args, |
2606 | | SourceLocation Loc, |
2607 | | DeclarationName Entity, |
2608 | 0 | bool AllowDeducedTST) { |
2609 | 0 | assert(!CodeSynthesisContexts.empty() && |
2610 | 0 | "Cannot perform an instantiation without some context on the " |
2611 | 0 | "instantiation stack"); |
2612 | | |
2613 | 0 | if (!T->getType()->isInstantiationDependentType() && |
2614 | 0 | !T->getType()->isVariablyModifiedType()) |
2615 | 0 | return T; |
2616 | | |
2617 | 0 | TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
2618 | 0 | return AllowDeducedTST ? Instantiator.TransformTypeWithDeducedTST(T) |
2619 | 0 | : Instantiator.TransformType(T); |
2620 | 0 | } |
2621 | | |
2622 | | TypeSourceInfo *Sema::SubstType(TypeLoc TL, |
2623 | | const MultiLevelTemplateArgumentList &Args, |
2624 | | SourceLocation Loc, |
2625 | 0 | DeclarationName Entity) { |
2626 | 0 | assert(!CodeSynthesisContexts.empty() && |
2627 | 0 | "Cannot perform an instantiation without some context on the " |
2628 | 0 | "instantiation stack"); |
2629 | | |
2630 | 0 | if (TL.getType().isNull()) |
2631 | 0 | return nullptr; |
2632 | | |
2633 | 0 | if (!TL.getType()->isInstantiationDependentType() && |
2634 | 0 | !TL.getType()->isVariablyModifiedType()) { |
2635 | | // FIXME: Make a copy of the TypeLoc data here, so that we can |
2636 | | // return a new TypeSourceInfo. Inefficient! |
2637 | 0 | TypeLocBuilder TLB; |
2638 | 0 | TLB.pushFullCopy(TL); |
2639 | 0 | return TLB.getTypeSourceInfo(Context, TL.getType()); |
2640 | 0 | } |
2641 | | |
2642 | 0 | TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
2643 | 0 | TypeLocBuilder TLB; |
2644 | 0 | TLB.reserve(TL.getFullDataSize()); |
2645 | 0 | QualType Result = Instantiator.TransformType(TLB, TL); |
2646 | 0 | if (Result.isNull()) |
2647 | 0 | return nullptr; |
2648 | | |
2649 | 0 | return TLB.getTypeSourceInfo(Context, Result); |
2650 | 0 | } |
2651 | | |
2652 | | /// Deprecated form of the above. |
2653 | | QualType Sema::SubstType(QualType T, |
2654 | | const MultiLevelTemplateArgumentList &TemplateArgs, |
2655 | 0 | SourceLocation Loc, DeclarationName Entity) { |
2656 | 0 | assert(!CodeSynthesisContexts.empty() && |
2657 | 0 | "Cannot perform an instantiation without some context on the " |
2658 | 0 | "instantiation stack"); |
2659 | | |
2660 | | // If T is not a dependent type or a variably-modified type, there |
2661 | | // is nothing to do. |
2662 | 0 | if (!T->isInstantiationDependentType() && !T->isVariablyModifiedType()) |
2663 | 0 | return T; |
2664 | | |
2665 | 0 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity); |
2666 | 0 | return Instantiator.TransformType(T); |
2667 | 0 | } |
2668 | | |
2669 | 0 | static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) { |
2670 | 0 | if (T->getType()->isInstantiationDependentType() || |
2671 | 0 | T->getType()->isVariablyModifiedType()) |
2672 | 0 | return true; |
2673 | | |
2674 | 0 | TypeLoc TL = T->getTypeLoc().IgnoreParens(); |
2675 | 0 | if (!TL.getAs<FunctionProtoTypeLoc>()) |
2676 | 0 | return false; |
2677 | | |
2678 | 0 | FunctionProtoTypeLoc FP = TL.castAs<FunctionProtoTypeLoc>(); |
2679 | 0 | for (ParmVarDecl *P : FP.getParams()) { |
2680 | | // This must be synthesized from a typedef. |
2681 | 0 | if (!P) continue; |
2682 | | |
2683 | | // If there are any parameters, a new TypeSourceInfo that refers to the |
2684 | | // instantiated parameters must be built. |
2685 | 0 | return true; |
2686 | 0 | } |
2687 | | |
2688 | 0 | return false; |
2689 | 0 | } |
2690 | | |
2691 | | /// A form of SubstType intended specifically for instantiating the |
2692 | | /// type of a FunctionDecl. Its purpose is solely to force the |
2693 | | /// instantiation of default-argument expressions and to avoid |
2694 | | /// instantiating an exception-specification. |
2695 | | TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T, |
2696 | | const MultiLevelTemplateArgumentList &Args, |
2697 | | SourceLocation Loc, |
2698 | | DeclarationName Entity, |
2699 | | CXXRecordDecl *ThisContext, |
2700 | | Qualifiers ThisTypeQuals, |
2701 | 0 | bool EvaluateConstraints) { |
2702 | 0 | assert(!CodeSynthesisContexts.empty() && |
2703 | 0 | "Cannot perform an instantiation without some context on the " |
2704 | 0 | "instantiation stack"); |
2705 | | |
2706 | 0 | if (!NeedsInstantiationAsFunctionType(T)) |
2707 | 0 | return T; |
2708 | | |
2709 | 0 | TemplateInstantiator Instantiator(*this, Args, Loc, Entity); |
2710 | 0 | Instantiator.setEvaluateConstraints(EvaluateConstraints); |
2711 | |
|
2712 | 0 | TypeLocBuilder TLB; |
2713 | |
|
2714 | 0 | TypeLoc TL = T->getTypeLoc(); |
2715 | 0 | TLB.reserve(TL.getFullDataSize()); |
2716 | |
|
2717 | 0 | QualType Result; |
2718 | |
|
2719 | 0 | if (FunctionProtoTypeLoc Proto = |
2720 | 0 | TL.IgnoreParens().getAs<FunctionProtoTypeLoc>()) { |
2721 | | // Instantiate the type, other than its exception specification. The |
2722 | | // exception specification is instantiated in InitFunctionInstantiation |
2723 | | // once we've built the FunctionDecl. |
2724 | | // FIXME: Set the exception specification to EST_Uninstantiated here, |
2725 | | // instead of rebuilding the function type again later. |
2726 | 0 | Result = Instantiator.TransformFunctionProtoType( |
2727 | 0 | TLB, Proto, ThisContext, ThisTypeQuals, |
2728 | 0 | [](FunctionProtoType::ExceptionSpecInfo &ESI, |
2729 | 0 | bool &Changed) { return false; }); |
2730 | 0 | } else { |
2731 | 0 | Result = Instantiator.TransformType(TLB, TL); |
2732 | 0 | } |
2733 | | // When there are errors resolving types, clang may use IntTy as a fallback, |
2734 | | // breaking our assumption that function declarations have function types. |
2735 | 0 | if (Result.isNull() || !Result->isFunctionType()) |
2736 | 0 | return nullptr; |
2737 | | |
2738 | 0 | return TLB.getTypeSourceInfo(Context, Result); |
2739 | 0 | } |
2740 | | |
2741 | | bool Sema::SubstExceptionSpec(SourceLocation Loc, |
2742 | | FunctionProtoType::ExceptionSpecInfo &ESI, |
2743 | | SmallVectorImpl<QualType> &ExceptionStorage, |
2744 | 0 | const MultiLevelTemplateArgumentList &Args) { |
2745 | 0 | bool Changed = false; |
2746 | 0 | TemplateInstantiator Instantiator(*this, Args, Loc, DeclarationName()); |
2747 | 0 | return Instantiator.TransformExceptionSpec(Loc, ESI, ExceptionStorage, |
2748 | 0 | Changed); |
2749 | 0 | } |
2750 | | |
2751 | | void Sema::SubstExceptionSpec(FunctionDecl *New, const FunctionProtoType *Proto, |
2752 | 0 | const MultiLevelTemplateArgumentList &Args) { |
2753 | 0 | FunctionProtoType::ExceptionSpecInfo ESI = |
2754 | 0 | Proto->getExtProtoInfo().ExceptionSpec; |
2755 | |
|
2756 | 0 | SmallVector<QualType, 4> ExceptionStorage; |
2757 | 0 | if (SubstExceptionSpec(New->getTypeSourceInfo()->getTypeLoc().getEndLoc(), |
2758 | 0 | ESI, ExceptionStorage, Args)) |
2759 | | // On error, recover by dropping the exception specification. |
2760 | 0 | ESI.Type = EST_None; |
2761 | |
|
2762 | 0 | UpdateExceptionSpec(New, ESI); |
2763 | 0 | } |
2764 | | |
2765 | | namespace { |
2766 | | |
2767 | | struct GetContainedInventedTypeParmVisitor : |
2768 | | public TypeVisitor<GetContainedInventedTypeParmVisitor, |
2769 | | TemplateTypeParmDecl *> { |
2770 | | using TypeVisitor<GetContainedInventedTypeParmVisitor, |
2771 | | TemplateTypeParmDecl *>::Visit; |
2772 | | |
2773 | 0 | TemplateTypeParmDecl *Visit(QualType T) { |
2774 | 0 | if (T.isNull()) |
2775 | 0 | return nullptr; |
2776 | 0 | return Visit(T.getTypePtr()); |
2777 | 0 | } |
2778 | | // The deduced type itself. |
2779 | | TemplateTypeParmDecl *VisitTemplateTypeParmType( |
2780 | 0 | const TemplateTypeParmType *T) { |
2781 | 0 | if (!T->getDecl() || !T->getDecl()->isImplicit()) |
2782 | 0 | return nullptr; |
2783 | 0 | return T->getDecl(); |
2784 | 0 | } |
2785 | | |
2786 | | // Only these types can contain 'auto' types, and subsequently be replaced |
2787 | | // by references to invented parameters. |
2788 | | |
2789 | 0 | TemplateTypeParmDecl *VisitElaboratedType(const ElaboratedType *T) { |
2790 | 0 | return Visit(T->getNamedType()); |
2791 | 0 | } |
2792 | | |
2793 | 0 | TemplateTypeParmDecl *VisitPointerType(const PointerType *T) { |
2794 | 0 | return Visit(T->getPointeeType()); |
2795 | 0 | } |
2796 | | |
2797 | 0 | TemplateTypeParmDecl *VisitBlockPointerType(const BlockPointerType *T) { |
2798 | 0 | return Visit(T->getPointeeType()); |
2799 | 0 | } |
2800 | | |
2801 | 0 | TemplateTypeParmDecl *VisitReferenceType(const ReferenceType *T) { |
2802 | 0 | return Visit(T->getPointeeTypeAsWritten()); |
2803 | 0 | } |
2804 | | |
2805 | 0 | TemplateTypeParmDecl *VisitMemberPointerType(const MemberPointerType *T) { |
2806 | 0 | return Visit(T->getPointeeType()); |
2807 | 0 | } |
2808 | | |
2809 | 0 | TemplateTypeParmDecl *VisitArrayType(const ArrayType *T) { |
2810 | 0 | return Visit(T->getElementType()); |
2811 | 0 | } |
2812 | | |
2813 | | TemplateTypeParmDecl *VisitDependentSizedExtVectorType( |
2814 | 0 | const DependentSizedExtVectorType *T) { |
2815 | 0 | return Visit(T->getElementType()); |
2816 | 0 | } |
2817 | | |
2818 | 0 | TemplateTypeParmDecl *VisitVectorType(const VectorType *T) { |
2819 | 0 | return Visit(T->getElementType()); |
2820 | 0 | } |
2821 | | |
2822 | 0 | TemplateTypeParmDecl *VisitFunctionProtoType(const FunctionProtoType *T) { |
2823 | 0 | return VisitFunctionType(T); |
2824 | 0 | } |
2825 | | |
2826 | 0 | TemplateTypeParmDecl *VisitFunctionType(const FunctionType *T) { |
2827 | 0 | return Visit(T->getReturnType()); |
2828 | 0 | } |
2829 | | |
2830 | 0 | TemplateTypeParmDecl *VisitParenType(const ParenType *T) { |
2831 | 0 | return Visit(T->getInnerType()); |
2832 | 0 | } |
2833 | | |
2834 | 0 | TemplateTypeParmDecl *VisitAttributedType(const AttributedType *T) { |
2835 | 0 | return Visit(T->getModifiedType()); |
2836 | 0 | } |
2837 | | |
2838 | 0 | TemplateTypeParmDecl *VisitMacroQualifiedType(const MacroQualifiedType *T) { |
2839 | 0 | return Visit(T->getUnderlyingType()); |
2840 | 0 | } |
2841 | | |
2842 | 0 | TemplateTypeParmDecl *VisitAdjustedType(const AdjustedType *T) { |
2843 | 0 | return Visit(T->getOriginalType()); |
2844 | 0 | } |
2845 | | |
2846 | 0 | TemplateTypeParmDecl *VisitPackExpansionType(const PackExpansionType *T) { |
2847 | 0 | return Visit(T->getPattern()); |
2848 | 0 | } |
2849 | | }; |
2850 | | |
2851 | | } // namespace |
2852 | | |
2853 | | bool Sema::SubstTypeConstraint( |
2854 | | TemplateTypeParmDecl *Inst, const TypeConstraint *TC, |
2855 | | const MultiLevelTemplateArgumentList &TemplateArgs, |
2856 | 0 | bool EvaluateConstraints) { |
2857 | 0 | const ASTTemplateArgumentListInfo *TemplArgInfo = |
2858 | 0 | TC->getTemplateArgsAsWritten(); |
2859 | |
|
2860 | 0 | if (!EvaluateConstraints) { |
2861 | 0 | Inst->setTypeConstraint(TC->getConceptReference(), |
2862 | 0 | TC->getImmediatelyDeclaredConstraint()); |
2863 | 0 | return false; |
2864 | 0 | } |
2865 | | |
2866 | 0 | TemplateArgumentListInfo InstArgs; |
2867 | |
|
2868 | 0 | if (TemplArgInfo) { |
2869 | 0 | InstArgs.setLAngleLoc(TemplArgInfo->LAngleLoc); |
2870 | 0 | InstArgs.setRAngleLoc(TemplArgInfo->RAngleLoc); |
2871 | 0 | if (SubstTemplateArguments(TemplArgInfo->arguments(), TemplateArgs, |
2872 | 0 | InstArgs)) |
2873 | 0 | return true; |
2874 | 0 | } |
2875 | 0 | return AttachTypeConstraint( |
2876 | 0 | TC->getNestedNameSpecifierLoc(), TC->getConceptNameInfo(), |
2877 | 0 | TC->getNamedConcept(), &InstArgs, Inst, |
2878 | 0 | Inst->isParameterPack() |
2879 | 0 | ? cast<CXXFoldExpr>(TC->getImmediatelyDeclaredConstraint()) |
2880 | 0 | ->getEllipsisLoc() |
2881 | 0 | : SourceLocation()); |
2882 | 0 | } |
2883 | | |
2884 | | ParmVarDecl *Sema::SubstParmVarDecl( |
2885 | | ParmVarDecl *OldParm, const MultiLevelTemplateArgumentList &TemplateArgs, |
2886 | | int indexAdjustment, std::optional<unsigned> NumExpansions, |
2887 | 0 | bool ExpectParameterPack, bool EvaluateConstraint) { |
2888 | 0 | TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo(); |
2889 | 0 | TypeSourceInfo *NewDI = nullptr; |
2890 | |
|
2891 | 0 | TypeLoc OldTL = OldDI->getTypeLoc(); |
2892 | 0 | if (PackExpansionTypeLoc ExpansionTL = OldTL.getAs<PackExpansionTypeLoc>()) { |
2893 | | |
2894 | | // We have a function parameter pack. Substitute into the pattern of the |
2895 | | // expansion. |
2896 | 0 | NewDI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs, |
2897 | 0 | OldParm->getLocation(), OldParm->getDeclName()); |
2898 | 0 | if (!NewDI) |
2899 | 0 | return nullptr; |
2900 | | |
2901 | 0 | if (NewDI->getType()->containsUnexpandedParameterPack()) { |
2902 | | // We still have unexpanded parameter packs, which means that |
2903 | | // our function parameter is still a function parameter pack. |
2904 | | // Therefore, make its type a pack expansion type. |
2905 | 0 | NewDI = CheckPackExpansion(NewDI, ExpansionTL.getEllipsisLoc(), |
2906 | 0 | NumExpansions); |
2907 | 0 | } else if (ExpectParameterPack) { |
2908 | | // We expected to get a parameter pack but didn't (because the type |
2909 | | // itself is not a pack expansion type), so complain. This can occur when |
2910 | | // the substitution goes through an alias template that "loses" the |
2911 | | // pack expansion. |
2912 | 0 | Diag(OldParm->getLocation(), |
2913 | 0 | diag::err_function_parameter_pack_without_parameter_packs) |
2914 | 0 | << NewDI->getType(); |
2915 | 0 | return nullptr; |
2916 | 0 | } |
2917 | 0 | } else { |
2918 | 0 | NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(), |
2919 | 0 | OldParm->getDeclName()); |
2920 | 0 | } |
2921 | | |
2922 | 0 | if (!NewDI) |
2923 | 0 | return nullptr; |
2924 | | |
2925 | 0 | if (NewDI->getType()->isVoidType()) { |
2926 | 0 | Diag(OldParm->getLocation(), diag::err_param_with_void_type); |
2927 | 0 | return nullptr; |
2928 | 0 | } |
2929 | | |
2930 | | // In abbreviated templates, TemplateTypeParmDecls with possible |
2931 | | // TypeConstraints are created when the parameter list is originally parsed. |
2932 | | // The TypeConstraints can therefore reference other functions parameters in |
2933 | | // the abbreviated function template, which is why we must instantiate them |
2934 | | // here, when the instantiated versions of those referenced parameters are in |
2935 | | // scope. |
2936 | 0 | if (TemplateTypeParmDecl *TTP = |
2937 | 0 | GetContainedInventedTypeParmVisitor().Visit(OldDI->getType())) { |
2938 | 0 | if (const TypeConstraint *TC = TTP->getTypeConstraint()) { |
2939 | 0 | auto *Inst = cast_or_null<TemplateTypeParmDecl>( |
2940 | 0 | FindInstantiatedDecl(TTP->getLocation(), TTP, TemplateArgs)); |
2941 | | // We will first get here when instantiating the abbreviated function |
2942 | | // template's described function, but we might also get here later. |
2943 | | // Make sure we do not instantiate the TypeConstraint more than once. |
2944 | 0 | if (Inst && !Inst->getTypeConstraint()) { |
2945 | 0 | if (SubstTypeConstraint(Inst, TC, TemplateArgs, EvaluateConstraint)) |
2946 | 0 | return nullptr; |
2947 | 0 | } |
2948 | 0 | } |
2949 | 0 | } |
2950 | | |
2951 | 0 | ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(), |
2952 | 0 | OldParm->getInnerLocStart(), |
2953 | 0 | OldParm->getLocation(), |
2954 | 0 | OldParm->getIdentifier(), |
2955 | 0 | NewDI->getType(), NewDI, |
2956 | 0 | OldParm->getStorageClass()); |
2957 | 0 | if (!NewParm) |
2958 | 0 | return nullptr; |
2959 | | |
2960 | | // Mark the (new) default argument as uninstantiated (if any). |
2961 | 0 | if (OldParm->hasUninstantiatedDefaultArg()) { |
2962 | 0 | Expr *Arg = OldParm->getUninstantiatedDefaultArg(); |
2963 | 0 | NewParm->setUninstantiatedDefaultArg(Arg); |
2964 | 0 | } else if (OldParm->hasUnparsedDefaultArg()) { |
2965 | 0 | NewParm->setUnparsedDefaultArg(); |
2966 | 0 | UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm); |
2967 | 0 | } else if (Expr *Arg = OldParm->getDefaultArg()) { |
2968 | | // Default arguments cannot be substituted until the declaration context |
2969 | | // for the associated function or lambda capture class is available. |
2970 | | // This is necessary for cases like the following where construction of |
2971 | | // the lambda capture class for the outer lambda is dependent on the |
2972 | | // parameter types but where the default argument is dependent on the |
2973 | | // outer lambda's declaration context. |
2974 | | // template <typename T> |
2975 | | // auto f() { |
2976 | | // return [](T = []{ return T{}; }()) { return 0; }; |
2977 | | // } |
2978 | 0 | NewParm->setUninstantiatedDefaultArg(Arg); |
2979 | 0 | } |
2980 | |
|
2981 | 0 | NewParm->setExplicitObjectParameterLoc( |
2982 | 0 | OldParm->getExplicitObjectParamThisLoc()); |
2983 | 0 | NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg()); |
2984 | |
|
2985 | 0 | if (OldParm->isParameterPack() && !NewParm->isParameterPack()) { |
2986 | | // Add the new parameter to the instantiated parameter pack. |
2987 | 0 | CurrentInstantiationScope->InstantiatedLocalPackArg(OldParm, NewParm); |
2988 | 0 | } else { |
2989 | | // Introduce an Old -> New mapping |
2990 | 0 | CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm); |
2991 | 0 | } |
2992 | | |
2993 | | // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext |
2994 | | // can be anything, is this right ? |
2995 | 0 | NewParm->setDeclContext(CurContext); |
2996 | |
|
2997 | 0 | NewParm->setScopeInfo(OldParm->getFunctionScopeDepth(), |
2998 | 0 | OldParm->getFunctionScopeIndex() + indexAdjustment); |
2999 | |
|
3000 | 0 | InstantiateAttrs(TemplateArgs, OldParm, NewParm); |
3001 | |
|
3002 | 0 | return NewParm; |
3003 | 0 | } |
3004 | | |
3005 | | /// Substitute the given template arguments into the given set of |
3006 | | /// parameters, producing the set of parameter types that would be generated |
3007 | | /// from such a substitution. |
3008 | | bool Sema::SubstParmTypes( |
3009 | | SourceLocation Loc, ArrayRef<ParmVarDecl *> Params, |
3010 | | const FunctionProtoType::ExtParameterInfo *ExtParamInfos, |
3011 | | const MultiLevelTemplateArgumentList &TemplateArgs, |
3012 | | SmallVectorImpl<QualType> &ParamTypes, |
3013 | | SmallVectorImpl<ParmVarDecl *> *OutParams, |
3014 | 0 | ExtParameterInfoBuilder &ParamInfos) { |
3015 | 0 | assert(!CodeSynthesisContexts.empty() && |
3016 | 0 | "Cannot perform an instantiation without some context on the " |
3017 | 0 | "instantiation stack"); |
3018 | | |
3019 | 0 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, |
3020 | 0 | DeclarationName()); |
3021 | 0 | return Instantiator.TransformFunctionTypeParams( |
3022 | 0 | Loc, Params, nullptr, ExtParamInfos, ParamTypes, OutParams, ParamInfos); |
3023 | 0 | } |
3024 | | |
3025 | | /// Substitute the given template arguments into the default argument. |
3026 | | bool Sema::SubstDefaultArgument( |
3027 | | SourceLocation Loc, |
3028 | | ParmVarDecl *Param, |
3029 | | const MultiLevelTemplateArgumentList &TemplateArgs, |
3030 | 0 | bool ForCallExpr) { |
3031 | 0 | FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext()); |
3032 | 0 | Expr *PatternExpr = Param->getUninstantiatedDefaultArg(); |
3033 | |
|
3034 | 0 | EnterExpressionEvaluationContext EvalContext( |
3035 | 0 | *this, ExpressionEvaluationContext::PotentiallyEvaluated, Param); |
3036 | |
|
3037 | 0 | InstantiatingTemplate Inst(*this, Loc, Param, TemplateArgs.getInnermost()); |
3038 | 0 | if (Inst.isInvalid()) |
3039 | 0 | return true; |
3040 | 0 | if (Inst.isAlreadyInstantiating()) { |
3041 | 0 | Diag(Param->getBeginLoc(), diag::err_recursive_default_argument) << FD; |
3042 | 0 | Param->setInvalidDecl(); |
3043 | 0 | return true; |
3044 | 0 | } |
3045 | | |
3046 | 0 | ExprResult Result; |
3047 | 0 | { |
3048 | | // C++ [dcl.fct.default]p5: |
3049 | | // The names in the [default argument] expression are bound, and |
3050 | | // the semantic constraints are checked, at the point where the |
3051 | | // default argument expression appears. |
3052 | 0 | ContextRAII SavedContext(*this, FD); |
3053 | 0 | std::unique_ptr<LocalInstantiationScope> LIS; |
3054 | |
|
3055 | 0 | if (ForCallExpr) { |
3056 | | // When instantiating a default argument due to use in a call expression, |
3057 | | // an instantiation scope that includes the parameters of the callee is |
3058 | | // required to satisfy references from the default argument. For example: |
3059 | | // template<typename T> void f(T a, int = decltype(a)()); |
3060 | | // void g() { f(0); } |
3061 | 0 | LIS = std::make_unique<LocalInstantiationScope>(*this); |
3062 | 0 | FunctionDecl *PatternFD = FD->getTemplateInstantiationPattern( |
3063 | 0 | /*ForDefinition*/ false); |
3064 | 0 | if (addInstantiatedParametersToScope(FD, PatternFD, *LIS, TemplateArgs)) |
3065 | 0 | return true; |
3066 | 0 | } |
3067 | | |
3068 | 0 | runWithSufficientStackSpace(Loc, [&] { |
3069 | 0 | Result = SubstInitializer(PatternExpr, TemplateArgs, |
3070 | 0 | /*DirectInit*/false); |
3071 | 0 | }); |
3072 | 0 | } |
3073 | 0 | if (Result.isInvalid()) |
3074 | 0 | return true; |
3075 | | |
3076 | 0 | if (ForCallExpr) { |
3077 | | // Check the expression as an initializer for the parameter. |
3078 | 0 | InitializedEntity Entity |
3079 | 0 | = InitializedEntity::InitializeParameter(Context, Param); |
3080 | 0 | InitializationKind Kind = InitializationKind::CreateCopy( |
3081 | 0 | Param->getLocation(), |
3082 | 0 | /*FIXME:EqualLoc*/ PatternExpr->getBeginLoc()); |
3083 | 0 | Expr *ResultE = Result.getAs<Expr>(); |
3084 | |
|
3085 | 0 | InitializationSequence InitSeq(*this, Entity, Kind, ResultE); |
3086 | 0 | Result = InitSeq.Perform(*this, Entity, Kind, ResultE); |
3087 | 0 | if (Result.isInvalid()) |
3088 | 0 | return true; |
3089 | | |
3090 | 0 | Result = |
3091 | 0 | ActOnFinishFullExpr(Result.getAs<Expr>(), Param->getOuterLocStart(), |
3092 | 0 | /*DiscardedValue*/ false); |
3093 | 0 | } else { |
3094 | | // FIXME: Obtain the source location for the '=' token. |
3095 | 0 | SourceLocation EqualLoc = PatternExpr->getBeginLoc(); |
3096 | 0 | Result = ConvertParamDefaultArgument(Param, Result.getAs<Expr>(), EqualLoc); |
3097 | 0 | } |
3098 | 0 | if (Result.isInvalid()) |
3099 | 0 | return true; |
3100 | | |
3101 | | // Remember the instantiated default argument. |
3102 | 0 | Param->setDefaultArg(Result.getAs<Expr>()); |
3103 | |
|
3104 | 0 | return false; |
3105 | 0 | } |
3106 | | |
3107 | | /// Perform substitution on the base class specifiers of the |
3108 | | /// given class template specialization. |
3109 | | /// |
3110 | | /// Produces a diagnostic and returns true on error, returns false and |
3111 | | /// attaches the instantiated base classes to the class template |
3112 | | /// specialization if successful. |
3113 | | bool |
3114 | | Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation, |
3115 | | CXXRecordDecl *Pattern, |
3116 | 0 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
3117 | 0 | bool Invalid = false; |
3118 | 0 | SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases; |
3119 | 0 | for (const auto &Base : Pattern->bases()) { |
3120 | 0 | if (!Base.getType()->isDependentType()) { |
3121 | 0 | if (const CXXRecordDecl *RD = Base.getType()->getAsCXXRecordDecl()) { |
3122 | 0 | if (RD->isInvalidDecl()) |
3123 | 0 | Instantiation->setInvalidDecl(); |
3124 | 0 | } |
3125 | 0 | InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(Base)); |
3126 | 0 | continue; |
3127 | 0 | } |
3128 | | |
3129 | 0 | SourceLocation EllipsisLoc; |
3130 | 0 | TypeSourceInfo *BaseTypeLoc; |
3131 | 0 | if (Base.isPackExpansion()) { |
3132 | | // This is a pack expansion. See whether we should expand it now, or |
3133 | | // wait until later. |
3134 | 0 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
3135 | 0 | collectUnexpandedParameterPacks(Base.getTypeSourceInfo()->getTypeLoc(), |
3136 | 0 | Unexpanded); |
3137 | 0 | bool ShouldExpand = false; |
3138 | 0 | bool RetainExpansion = false; |
3139 | 0 | std::optional<unsigned> NumExpansions; |
3140 | 0 | if (CheckParameterPacksForExpansion(Base.getEllipsisLoc(), |
3141 | 0 | Base.getSourceRange(), |
3142 | 0 | Unexpanded, |
3143 | 0 | TemplateArgs, ShouldExpand, |
3144 | 0 | RetainExpansion, |
3145 | 0 | NumExpansions)) { |
3146 | 0 | Invalid = true; |
3147 | 0 | continue; |
3148 | 0 | } |
3149 | | |
3150 | | // If we should expand this pack expansion now, do so. |
3151 | 0 | if (ShouldExpand) { |
3152 | 0 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
3153 | 0 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I); |
3154 | |
|
3155 | 0 | TypeSourceInfo *BaseTypeLoc = SubstType(Base.getTypeSourceInfo(), |
3156 | 0 | TemplateArgs, |
3157 | 0 | Base.getSourceRange().getBegin(), |
3158 | 0 | DeclarationName()); |
3159 | 0 | if (!BaseTypeLoc) { |
3160 | 0 | Invalid = true; |
3161 | 0 | continue; |
3162 | 0 | } |
3163 | | |
3164 | 0 | if (CXXBaseSpecifier *InstantiatedBase |
3165 | 0 | = CheckBaseSpecifier(Instantiation, |
3166 | 0 | Base.getSourceRange(), |
3167 | 0 | Base.isVirtual(), |
3168 | 0 | Base.getAccessSpecifierAsWritten(), |
3169 | 0 | BaseTypeLoc, |
3170 | 0 | SourceLocation())) |
3171 | 0 | InstantiatedBases.push_back(InstantiatedBase); |
3172 | 0 | else |
3173 | 0 | Invalid = true; |
3174 | 0 | } |
3175 | |
|
3176 | 0 | continue; |
3177 | 0 | } |
3178 | | |
3179 | | // The resulting base specifier will (still) be a pack expansion. |
3180 | 0 | EllipsisLoc = Base.getEllipsisLoc(); |
3181 | 0 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1); |
3182 | 0 | BaseTypeLoc = SubstType(Base.getTypeSourceInfo(), |
3183 | 0 | TemplateArgs, |
3184 | 0 | Base.getSourceRange().getBegin(), |
3185 | 0 | DeclarationName()); |
3186 | 0 | } else { |
3187 | 0 | BaseTypeLoc = SubstType(Base.getTypeSourceInfo(), |
3188 | 0 | TemplateArgs, |
3189 | 0 | Base.getSourceRange().getBegin(), |
3190 | 0 | DeclarationName()); |
3191 | 0 | } |
3192 | | |
3193 | 0 | if (!BaseTypeLoc) { |
3194 | 0 | Invalid = true; |
3195 | 0 | continue; |
3196 | 0 | } |
3197 | | |
3198 | 0 | if (CXXBaseSpecifier *InstantiatedBase |
3199 | 0 | = CheckBaseSpecifier(Instantiation, |
3200 | 0 | Base.getSourceRange(), |
3201 | 0 | Base.isVirtual(), |
3202 | 0 | Base.getAccessSpecifierAsWritten(), |
3203 | 0 | BaseTypeLoc, |
3204 | 0 | EllipsisLoc)) |
3205 | 0 | InstantiatedBases.push_back(InstantiatedBase); |
3206 | 0 | else |
3207 | 0 | Invalid = true; |
3208 | 0 | } |
3209 | |
|
3210 | 0 | if (!Invalid && AttachBaseSpecifiers(Instantiation, InstantiatedBases)) |
3211 | 0 | Invalid = true; |
3212 | |
|
3213 | 0 | return Invalid; |
3214 | 0 | } |
3215 | | |
3216 | | // Defined via #include from SemaTemplateInstantiateDecl.cpp |
3217 | | namespace clang { |
3218 | | namespace sema { |
3219 | | Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, Sema &S, |
3220 | | const MultiLevelTemplateArgumentList &TemplateArgs); |
3221 | | Attr *instantiateTemplateAttributeForDecl( |
3222 | | const Attr *At, ASTContext &C, Sema &S, |
3223 | | const MultiLevelTemplateArgumentList &TemplateArgs); |
3224 | | } |
3225 | | } |
3226 | | |
3227 | | /// Instantiate the definition of a class from a given pattern. |
3228 | | /// |
3229 | | /// \param PointOfInstantiation The point of instantiation within the |
3230 | | /// source code. |
3231 | | /// |
3232 | | /// \param Instantiation is the declaration whose definition is being |
3233 | | /// instantiated. This will be either a class template specialization |
3234 | | /// or a member class of a class template specialization. |
3235 | | /// |
3236 | | /// \param Pattern is the pattern from which the instantiation |
3237 | | /// occurs. This will be either the declaration of a class template or |
3238 | | /// the declaration of a member class of a class template. |
3239 | | /// |
3240 | | /// \param TemplateArgs The template arguments to be substituted into |
3241 | | /// the pattern. |
3242 | | /// |
3243 | | /// \param TSK the kind of implicit or explicit instantiation to perform. |
3244 | | /// |
3245 | | /// \param Complain whether to complain if the class cannot be instantiated due |
3246 | | /// to the lack of a definition. |
3247 | | /// |
3248 | | /// \returns true if an error occurred, false otherwise. |
3249 | | bool |
3250 | | Sema::InstantiateClass(SourceLocation PointOfInstantiation, |
3251 | | CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern, |
3252 | | const MultiLevelTemplateArgumentList &TemplateArgs, |
3253 | | TemplateSpecializationKind TSK, |
3254 | 0 | bool Complain) { |
3255 | 0 | CXXRecordDecl *PatternDef |
3256 | 0 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
3257 | 0 | if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Instantiation, |
3258 | 0 | Instantiation->getInstantiatedFromMemberClass(), |
3259 | 0 | Pattern, PatternDef, TSK, Complain)) |
3260 | 0 | return true; |
3261 | | |
3262 | 0 | llvm::TimeTraceScope TimeScope("InstantiateClass", [&]() { |
3263 | 0 | std::string Name; |
3264 | 0 | llvm::raw_string_ostream OS(Name); |
3265 | 0 | Instantiation->getNameForDiagnostic(OS, getPrintingPolicy(), |
3266 | 0 | /*Qualified=*/true); |
3267 | 0 | return Name; |
3268 | 0 | }); |
3269 | |
|
3270 | 0 | Pattern = PatternDef; |
3271 | | |
3272 | | // Record the point of instantiation. |
3273 | 0 | if (MemberSpecializationInfo *MSInfo |
3274 | 0 | = Instantiation->getMemberSpecializationInfo()) { |
3275 | 0 | MSInfo->setTemplateSpecializationKind(TSK); |
3276 | 0 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
3277 | 0 | } else if (ClassTemplateSpecializationDecl *Spec |
3278 | 0 | = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) { |
3279 | 0 | Spec->setTemplateSpecializationKind(TSK); |
3280 | 0 | Spec->setPointOfInstantiation(PointOfInstantiation); |
3281 | 0 | } |
3282 | |
|
3283 | 0 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
3284 | 0 | if (Inst.isInvalid()) |
3285 | 0 | return true; |
3286 | 0 | assert(!Inst.isAlreadyInstantiating() && "should have been caught by caller"); |
3287 | 0 | PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(), |
3288 | 0 | "instantiating class definition"); |
3289 | | |
3290 | | // Enter the scope of this instantiation. We don't use |
3291 | | // PushDeclContext because we don't have a scope. |
3292 | 0 | ContextRAII SavedContext(*this, Instantiation); |
3293 | 0 | EnterExpressionEvaluationContext EvalContext( |
3294 | 0 | *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
3295 | | |
3296 | | // If this is an instantiation of a local class, merge this local |
3297 | | // instantiation scope with the enclosing scope. Otherwise, every |
3298 | | // instantiation of a class has its own local instantiation scope. |
3299 | 0 | bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod(); |
3300 | 0 | LocalInstantiationScope Scope(*this, MergeWithParentScope); |
3301 | | |
3302 | | // Some class state isn't processed immediately but delayed till class |
3303 | | // instantiation completes. We may not be ready to handle any delayed state |
3304 | | // already on the stack as it might correspond to a different class, so save |
3305 | | // it now and put it back later. |
3306 | 0 | SavePendingParsedClassStateRAII SavedPendingParsedClassState(*this); |
3307 | | |
3308 | | // Pull attributes from the pattern onto the instantiation. |
3309 | 0 | InstantiateAttrs(TemplateArgs, Pattern, Instantiation); |
3310 | | |
3311 | | // Start the definition of this instantiation. |
3312 | 0 | Instantiation->startDefinition(); |
3313 | | |
3314 | | // The instantiation is visible here, even if it was first declared in an |
3315 | | // unimported module. |
3316 | 0 | Instantiation->setVisibleDespiteOwningModule(); |
3317 | | |
3318 | | // FIXME: This loses the as-written tag kind for an explicit instantiation. |
3319 | 0 | Instantiation->setTagKind(Pattern->getTagKind()); |
3320 | | |
3321 | | // Do substitution on the base class specifiers. |
3322 | 0 | if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs)) |
3323 | 0 | Instantiation->setInvalidDecl(); |
3324 | |
|
3325 | 0 | TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs); |
3326 | 0 | Instantiator.setEvaluateConstraints(false); |
3327 | 0 | SmallVector<Decl*, 4> Fields; |
3328 | | // Delay instantiation of late parsed attributes. |
3329 | 0 | LateInstantiatedAttrVec LateAttrs; |
3330 | 0 | Instantiator.enableLateAttributeInstantiation(&LateAttrs); |
3331 | |
|
3332 | 0 | bool MightHaveConstexprVirtualFunctions = false; |
3333 | 0 | for (auto *Member : Pattern->decls()) { |
3334 | | // Don't instantiate members not belonging in this semantic context. |
3335 | | // e.g. for: |
3336 | | // @code |
3337 | | // template <int i> class A { |
3338 | | // class B *g; |
3339 | | // }; |
3340 | | // @endcode |
3341 | | // 'class B' has the template as lexical context but semantically it is |
3342 | | // introduced in namespace scope. |
3343 | 0 | if (Member->getDeclContext() != Pattern) |
3344 | 0 | continue; |
3345 | | |
3346 | | // BlockDecls can appear in a default-member-initializer. They must be the |
3347 | | // child of a BlockExpr, so we only know how to instantiate them from there. |
3348 | | // Similarly, lambda closure types are recreated when instantiating the |
3349 | | // corresponding LambdaExpr. |
3350 | 0 | if (isa<BlockDecl>(Member) || |
3351 | 0 | (isa<CXXRecordDecl>(Member) && cast<CXXRecordDecl>(Member)->isLambda())) |
3352 | 0 | continue; |
3353 | | |
3354 | 0 | if (Member->isInvalidDecl()) { |
3355 | 0 | Instantiation->setInvalidDecl(); |
3356 | 0 | continue; |
3357 | 0 | } |
3358 | | |
3359 | 0 | Decl *NewMember = Instantiator.Visit(Member); |
3360 | 0 | if (NewMember) { |
3361 | 0 | if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) { |
3362 | 0 | Fields.push_back(Field); |
3363 | 0 | } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(NewMember)) { |
3364 | | // C++11 [temp.inst]p1: The implicit instantiation of a class template |
3365 | | // specialization causes the implicit instantiation of the definitions |
3366 | | // of unscoped member enumerations. |
3367 | | // Record a point of instantiation for this implicit instantiation. |
3368 | 0 | if (TSK == TSK_ImplicitInstantiation && !Enum->isScoped() && |
3369 | 0 | Enum->isCompleteDefinition()) { |
3370 | 0 | MemberSpecializationInfo *MSInfo =Enum->getMemberSpecializationInfo(); |
3371 | 0 | assert(MSInfo && "no spec info for member enum specialization"); |
3372 | 0 | MSInfo->setTemplateSpecializationKind(TSK_ImplicitInstantiation); |
3373 | 0 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
3374 | 0 | } |
3375 | 0 | } else if (StaticAssertDecl *SA = dyn_cast<StaticAssertDecl>(NewMember)) { |
3376 | 0 | if (SA->isFailed()) { |
3377 | | // A static_assert failed. Bail out; instantiating this |
3378 | | // class is probably not meaningful. |
3379 | 0 | Instantiation->setInvalidDecl(); |
3380 | 0 | break; |
3381 | 0 | } |
3382 | 0 | } else if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewMember)) { |
3383 | 0 | if (MD->isConstexpr() && !MD->getFriendObjectKind() && |
3384 | 0 | (MD->isVirtualAsWritten() || Instantiation->getNumBases())) |
3385 | 0 | MightHaveConstexprVirtualFunctions = true; |
3386 | 0 | } |
3387 | | |
3388 | 0 | if (NewMember->isInvalidDecl()) |
3389 | 0 | Instantiation->setInvalidDecl(); |
3390 | 0 | } else { |
3391 | | // FIXME: Eventually, a NULL return will mean that one of the |
3392 | | // instantiations was a semantic disaster, and we'll want to mark the |
3393 | | // declaration invalid. |
3394 | | // For now, we expect to skip some members that we can't yet handle. |
3395 | 0 | } |
3396 | 0 | } |
3397 | | |
3398 | | // Finish checking fields. |
3399 | 0 | ActOnFields(nullptr, Instantiation->getLocation(), Instantiation, Fields, |
3400 | 0 | SourceLocation(), SourceLocation(), ParsedAttributesView()); |
3401 | 0 | CheckCompletedCXXClass(nullptr, Instantiation); |
3402 | | |
3403 | | // Default arguments are parsed, if not instantiated. We can go instantiate |
3404 | | // default arg exprs for default constructors if necessary now. Unless we're |
3405 | | // parsing a class, in which case wait until that's finished. |
3406 | 0 | if (ParsingClassDepth == 0) |
3407 | 0 | ActOnFinishCXXNonNestedClass(); |
3408 | | |
3409 | | // Instantiate late parsed attributes, and attach them to their decls. |
3410 | | // See Sema::InstantiateAttrs |
3411 | 0 | for (LateInstantiatedAttrVec::iterator I = LateAttrs.begin(), |
3412 | 0 | E = LateAttrs.end(); I != E; ++I) { |
3413 | 0 | assert(CurrentInstantiationScope == Instantiator.getStartingScope()); |
3414 | 0 | CurrentInstantiationScope = I->Scope; |
3415 | | |
3416 | | // Allow 'this' within late-parsed attributes. |
3417 | 0 | auto *ND = cast<NamedDecl>(I->NewDecl); |
3418 | 0 | auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()); |
3419 | 0 | CXXThisScopeRAII ThisScope(*this, ThisContext, Qualifiers(), |
3420 | 0 | ND->isCXXInstanceMember()); |
3421 | |
|
3422 | 0 | Attr *NewAttr = |
3423 | 0 | instantiateTemplateAttribute(I->TmplAttr, Context, *this, TemplateArgs); |
3424 | 0 | if (NewAttr) |
3425 | 0 | I->NewDecl->addAttr(NewAttr); |
3426 | 0 | LocalInstantiationScope::deleteScopes(I->Scope, |
3427 | 0 | Instantiator.getStartingScope()); |
3428 | 0 | } |
3429 | 0 | Instantiator.disableLateAttributeInstantiation(); |
3430 | 0 | LateAttrs.clear(); |
3431 | |
|
3432 | 0 | ActOnFinishDelayedMemberInitializers(Instantiation); |
3433 | | |
3434 | | // FIXME: We should do something similar for explicit instantiations so they |
3435 | | // end up in the right module. |
3436 | 0 | if (TSK == TSK_ImplicitInstantiation) { |
3437 | 0 | Instantiation->setLocation(Pattern->getLocation()); |
3438 | 0 | Instantiation->setLocStart(Pattern->getInnerLocStart()); |
3439 | 0 | Instantiation->setBraceRange(Pattern->getBraceRange()); |
3440 | 0 | } |
3441 | |
|
3442 | 0 | if (!Instantiation->isInvalidDecl()) { |
3443 | | // Perform any dependent diagnostics from the pattern. |
3444 | 0 | if (Pattern->isDependentContext()) |
3445 | 0 | PerformDependentDiagnostics(Pattern, TemplateArgs); |
3446 | | |
3447 | | // Instantiate any out-of-line class template partial |
3448 | | // specializations now. |
3449 | 0 | for (TemplateDeclInstantiator::delayed_partial_spec_iterator |
3450 | 0 | P = Instantiator.delayed_partial_spec_begin(), |
3451 | 0 | PEnd = Instantiator.delayed_partial_spec_end(); |
3452 | 0 | P != PEnd; ++P) { |
3453 | 0 | if (!Instantiator.InstantiateClassTemplatePartialSpecialization( |
3454 | 0 | P->first, P->second)) { |
3455 | 0 | Instantiation->setInvalidDecl(); |
3456 | 0 | break; |
3457 | 0 | } |
3458 | 0 | } |
3459 | | |
3460 | | // Instantiate any out-of-line variable template partial |
3461 | | // specializations now. |
3462 | 0 | for (TemplateDeclInstantiator::delayed_var_partial_spec_iterator |
3463 | 0 | P = Instantiator.delayed_var_partial_spec_begin(), |
3464 | 0 | PEnd = Instantiator.delayed_var_partial_spec_end(); |
3465 | 0 | P != PEnd; ++P) { |
3466 | 0 | if (!Instantiator.InstantiateVarTemplatePartialSpecialization( |
3467 | 0 | P->first, P->second)) { |
3468 | 0 | Instantiation->setInvalidDecl(); |
3469 | 0 | break; |
3470 | 0 | } |
3471 | 0 | } |
3472 | 0 | } |
3473 | | |
3474 | | // Exit the scope of this instantiation. |
3475 | 0 | SavedContext.pop(); |
3476 | |
|
3477 | 0 | if (!Instantiation->isInvalidDecl()) { |
3478 | | // Always emit the vtable for an explicit instantiation definition |
3479 | | // of a polymorphic class template specialization. Otherwise, eagerly |
3480 | | // instantiate only constexpr virtual functions in preparation for their use |
3481 | | // in constant evaluation. |
3482 | 0 | if (TSK == TSK_ExplicitInstantiationDefinition) |
3483 | 0 | MarkVTableUsed(PointOfInstantiation, Instantiation, true); |
3484 | 0 | else if (MightHaveConstexprVirtualFunctions) |
3485 | 0 | MarkVirtualMembersReferenced(PointOfInstantiation, Instantiation, |
3486 | 0 | /*ConstexprOnly*/ true); |
3487 | 0 | } |
3488 | |
|
3489 | 0 | Consumer.HandleTagDeclDefinition(Instantiation); |
3490 | |
|
3491 | 0 | return Instantiation->isInvalidDecl(); |
3492 | 0 | } |
3493 | | |
3494 | | /// Instantiate the definition of an enum from a given pattern. |
3495 | | /// |
3496 | | /// \param PointOfInstantiation The point of instantiation within the |
3497 | | /// source code. |
3498 | | /// \param Instantiation is the declaration whose definition is being |
3499 | | /// instantiated. This will be a member enumeration of a class |
3500 | | /// temploid specialization, or a local enumeration within a |
3501 | | /// function temploid specialization. |
3502 | | /// \param Pattern The templated declaration from which the instantiation |
3503 | | /// occurs. |
3504 | | /// \param TemplateArgs The template arguments to be substituted into |
3505 | | /// the pattern. |
3506 | | /// \param TSK The kind of implicit or explicit instantiation to perform. |
3507 | | /// |
3508 | | /// \return \c true if an error occurred, \c false otherwise. |
3509 | | bool Sema::InstantiateEnum(SourceLocation PointOfInstantiation, |
3510 | | EnumDecl *Instantiation, EnumDecl *Pattern, |
3511 | | const MultiLevelTemplateArgumentList &TemplateArgs, |
3512 | 0 | TemplateSpecializationKind TSK) { |
3513 | 0 | EnumDecl *PatternDef = Pattern->getDefinition(); |
3514 | 0 | if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Instantiation, |
3515 | 0 | Instantiation->getInstantiatedFromMemberEnum(), |
3516 | 0 | Pattern, PatternDef, TSK,/*Complain*/true)) |
3517 | 0 | return true; |
3518 | 0 | Pattern = PatternDef; |
3519 | | |
3520 | | // Record the point of instantiation. |
3521 | 0 | if (MemberSpecializationInfo *MSInfo |
3522 | 0 | = Instantiation->getMemberSpecializationInfo()) { |
3523 | 0 | MSInfo->setTemplateSpecializationKind(TSK); |
3524 | 0 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
3525 | 0 | } |
3526 | |
|
3527 | 0 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
3528 | 0 | if (Inst.isInvalid()) |
3529 | 0 | return true; |
3530 | 0 | if (Inst.isAlreadyInstantiating()) |
3531 | 0 | return false; |
3532 | 0 | PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(), |
3533 | 0 | "instantiating enum definition"); |
3534 | | |
3535 | | // The instantiation is visible here, even if it was first declared in an |
3536 | | // unimported module. |
3537 | 0 | Instantiation->setVisibleDespiteOwningModule(); |
3538 | | |
3539 | | // Enter the scope of this instantiation. We don't use |
3540 | | // PushDeclContext because we don't have a scope. |
3541 | 0 | ContextRAII SavedContext(*this, Instantiation); |
3542 | 0 | EnterExpressionEvaluationContext EvalContext( |
3543 | 0 | *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
3544 | |
|
3545 | 0 | LocalInstantiationScope Scope(*this, /*MergeWithParentScope*/true); |
3546 | | |
3547 | | // Pull attributes from the pattern onto the instantiation. |
3548 | 0 | InstantiateAttrs(TemplateArgs, Pattern, Instantiation); |
3549 | |
|
3550 | 0 | TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs); |
3551 | 0 | Instantiator.InstantiateEnumDefinition(Instantiation, Pattern); |
3552 | | |
3553 | | // Exit the scope of this instantiation. |
3554 | 0 | SavedContext.pop(); |
3555 | |
|
3556 | 0 | return Instantiation->isInvalidDecl(); |
3557 | 0 | } |
3558 | | |
3559 | | |
3560 | | /// Instantiate the definition of a field from the given pattern. |
3561 | | /// |
3562 | | /// \param PointOfInstantiation The point of instantiation within the |
3563 | | /// source code. |
3564 | | /// \param Instantiation is the declaration whose definition is being |
3565 | | /// instantiated. This will be a class of a class temploid |
3566 | | /// specialization, or a local enumeration within a function temploid |
3567 | | /// specialization. |
3568 | | /// \param Pattern The templated declaration from which the instantiation |
3569 | | /// occurs. |
3570 | | /// \param TemplateArgs The template arguments to be substituted into |
3571 | | /// the pattern. |
3572 | | /// |
3573 | | /// \return \c true if an error occurred, \c false otherwise. |
3574 | | bool Sema::InstantiateInClassInitializer( |
3575 | | SourceLocation PointOfInstantiation, FieldDecl *Instantiation, |
3576 | 0 | FieldDecl *Pattern, const MultiLevelTemplateArgumentList &TemplateArgs) { |
3577 | | // If there is no initializer, we don't need to do anything. |
3578 | 0 | if (!Pattern->hasInClassInitializer()) |
3579 | 0 | return false; |
3580 | | |
3581 | 0 | assert(Instantiation->getInClassInitStyle() == |
3582 | 0 | Pattern->getInClassInitStyle() && |
3583 | 0 | "pattern and instantiation disagree about init style"); |
3584 | | |
3585 | | // Error out if we haven't parsed the initializer of the pattern yet because |
3586 | | // we are waiting for the closing brace of the outer class. |
3587 | 0 | Expr *OldInit = Pattern->getInClassInitializer(); |
3588 | 0 | if (!OldInit) { |
3589 | 0 | RecordDecl *PatternRD = Pattern->getParent(); |
3590 | 0 | RecordDecl *OutermostClass = PatternRD->getOuterLexicalRecordContext(); |
3591 | 0 | Diag(PointOfInstantiation, |
3592 | 0 | diag::err_default_member_initializer_not_yet_parsed) |
3593 | 0 | << OutermostClass << Pattern; |
3594 | 0 | Diag(Pattern->getEndLoc(), |
3595 | 0 | diag::note_default_member_initializer_not_yet_parsed); |
3596 | 0 | Instantiation->setInvalidDecl(); |
3597 | 0 | return true; |
3598 | 0 | } |
3599 | | |
3600 | 0 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
3601 | 0 | if (Inst.isInvalid()) |
3602 | 0 | return true; |
3603 | 0 | if (Inst.isAlreadyInstantiating()) { |
3604 | | // Error out if we hit an instantiation cycle for this initializer. |
3605 | 0 | Diag(PointOfInstantiation, diag::err_default_member_initializer_cycle) |
3606 | 0 | << Instantiation; |
3607 | 0 | return true; |
3608 | 0 | } |
3609 | 0 | PrettyDeclStackTraceEntry CrashInfo(Context, Instantiation, SourceLocation(), |
3610 | 0 | "instantiating default member init"); |
3611 | | |
3612 | | // Enter the scope of this instantiation. We don't use PushDeclContext because |
3613 | | // we don't have a scope. |
3614 | 0 | ContextRAII SavedContext(*this, Instantiation->getParent()); |
3615 | 0 | EnterExpressionEvaluationContext EvalContext( |
3616 | 0 | *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
3617 | 0 | ExprEvalContexts.back().DelayedDefaultInitializationContext = { |
3618 | 0 | PointOfInstantiation, Instantiation, CurContext}; |
3619 | |
|
3620 | 0 | LocalInstantiationScope Scope(*this, true); |
3621 | | |
3622 | | // Instantiate the initializer. |
3623 | 0 | ActOnStartCXXInClassMemberInitializer(); |
3624 | 0 | CXXThisScopeRAII ThisScope(*this, Instantiation->getParent(), Qualifiers()); |
3625 | |
|
3626 | 0 | ExprResult NewInit = SubstInitializer(OldInit, TemplateArgs, |
3627 | 0 | /*CXXDirectInit=*/false); |
3628 | 0 | Expr *Init = NewInit.get(); |
3629 | 0 | assert((!Init || !isa<ParenListExpr>(Init)) && "call-style init in class"); |
3630 | 0 | ActOnFinishCXXInClassMemberInitializer( |
3631 | 0 | Instantiation, Init ? Init->getBeginLoc() : SourceLocation(), Init); |
3632 | |
|
3633 | 0 | if (auto *L = getASTMutationListener()) |
3634 | 0 | L->DefaultMemberInitializerInstantiated(Instantiation); |
3635 | | |
3636 | | // Return true if the in-class initializer is still missing. |
3637 | 0 | return !Instantiation->getInClassInitializer(); |
3638 | 0 | } |
3639 | | |
3640 | | namespace { |
3641 | | /// A partial specialization whose template arguments have matched |
3642 | | /// a given template-id. |
3643 | | struct PartialSpecMatchResult { |
3644 | | ClassTemplatePartialSpecializationDecl *Partial; |
3645 | | TemplateArgumentList *Args; |
3646 | | }; |
3647 | | } |
3648 | | |
3649 | | bool Sema::usesPartialOrExplicitSpecialization( |
3650 | 0 | SourceLocation Loc, ClassTemplateSpecializationDecl *ClassTemplateSpec) { |
3651 | 0 | if (ClassTemplateSpec->getTemplateSpecializationKind() == |
3652 | 0 | TSK_ExplicitSpecialization) |
3653 | 0 | return true; |
3654 | | |
3655 | 0 | SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
3656 | 0 | ClassTemplateSpec->getSpecializedTemplate() |
3657 | 0 | ->getPartialSpecializations(PartialSpecs); |
3658 | 0 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
3659 | 0 | TemplateDeductionInfo Info(Loc); |
3660 | 0 | if (!DeduceTemplateArguments(PartialSpecs[I], |
3661 | 0 | ClassTemplateSpec->getTemplateArgs(), Info)) |
3662 | 0 | return true; |
3663 | 0 | } |
3664 | | |
3665 | 0 | return false; |
3666 | 0 | } |
3667 | | |
3668 | | /// Get the instantiation pattern to use to instantiate the definition of a |
3669 | | /// given ClassTemplateSpecializationDecl (either the pattern of the primary |
3670 | | /// template or of a partial specialization). |
3671 | | static ActionResult<CXXRecordDecl *> |
3672 | | getPatternForClassTemplateSpecialization( |
3673 | | Sema &S, SourceLocation PointOfInstantiation, |
3674 | | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
3675 | 0 | TemplateSpecializationKind TSK) { |
3676 | 0 | Sema::InstantiatingTemplate Inst(S, PointOfInstantiation, ClassTemplateSpec); |
3677 | 0 | if (Inst.isInvalid()) |
3678 | 0 | return {/*Invalid=*/true}; |
3679 | 0 | if (Inst.isAlreadyInstantiating()) |
3680 | 0 | return {/*Invalid=*/false}; |
3681 | | |
3682 | 0 | llvm::PointerUnion<ClassTemplateDecl *, |
3683 | 0 | ClassTemplatePartialSpecializationDecl *> |
3684 | 0 | Specialized = ClassTemplateSpec->getSpecializedTemplateOrPartial(); |
3685 | 0 | if (!Specialized.is<ClassTemplatePartialSpecializationDecl *>()) { |
3686 | | // Find best matching specialization. |
3687 | 0 | ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
3688 | | |
3689 | | // C++ [temp.class.spec.match]p1: |
3690 | | // When a class template is used in a context that requires an |
3691 | | // instantiation of the class, it is necessary to determine |
3692 | | // whether the instantiation is to be generated using the primary |
3693 | | // template or one of the partial specializations. This is done by |
3694 | | // matching the template arguments of the class template |
3695 | | // specialization with the template argument lists of the partial |
3696 | | // specializations. |
3697 | 0 | typedef PartialSpecMatchResult MatchResult; |
3698 | 0 | SmallVector<MatchResult, 4> Matched; |
3699 | 0 | SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
3700 | 0 | Template->getPartialSpecializations(PartialSpecs); |
3701 | 0 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation); |
3702 | 0 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
3703 | 0 | ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
3704 | 0 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
3705 | 0 | if (Sema::TemplateDeductionResult Result = S.DeduceTemplateArguments( |
3706 | 0 | Partial, ClassTemplateSpec->getTemplateArgs(), Info)) { |
3707 | | // Store the failed-deduction information for use in diagnostics, later. |
3708 | | // TODO: Actually use the failed-deduction info? |
3709 | 0 | FailedCandidates.addCandidate().set( |
3710 | 0 | DeclAccessPair::make(Template, AS_public), Partial, |
3711 | 0 | MakeDeductionFailureInfo(S.Context, Result, Info)); |
3712 | 0 | (void)Result; |
3713 | 0 | } else { |
3714 | 0 | Matched.push_back(PartialSpecMatchResult()); |
3715 | 0 | Matched.back().Partial = Partial; |
3716 | 0 | Matched.back().Args = Info.takeCanonical(); |
3717 | 0 | } |
3718 | 0 | } |
3719 | | |
3720 | | // If we're dealing with a member template where the template parameters |
3721 | | // have been instantiated, this provides the original template parameters |
3722 | | // from which the member template's parameters were instantiated. |
3723 | |
|
3724 | 0 | if (Matched.size() >= 1) { |
3725 | 0 | SmallVectorImpl<MatchResult>::iterator Best = Matched.begin(); |
3726 | 0 | if (Matched.size() == 1) { |
3727 | | // -- If exactly one matching specialization is found, the |
3728 | | // instantiation is generated from that specialization. |
3729 | | // We don't need to do anything for this. |
3730 | 0 | } else { |
3731 | | // -- If more than one matching specialization is found, the |
3732 | | // partial order rules (14.5.4.2) are used to determine |
3733 | | // whether one of the specializations is more specialized |
3734 | | // than the others. If none of the specializations is more |
3735 | | // specialized than all of the other matching |
3736 | | // specializations, then the use of the class template is |
3737 | | // ambiguous and the program is ill-formed. |
3738 | 0 | for (SmallVectorImpl<MatchResult>::iterator P = Best + 1, |
3739 | 0 | PEnd = Matched.end(); |
3740 | 0 | P != PEnd; ++P) { |
3741 | 0 | if (S.getMoreSpecializedPartialSpecialization( |
3742 | 0 | P->Partial, Best->Partial, PointOfInstantiation) == |
3743 | 0 | P->Partial) |
3744 | 0 | Best = P; |
3745 | 0 | } |
3746 | | |
3747 | | // Determine if the best partial specialization is more specialized than |
3748 | | // the others. |
3749 | 0 | bool Ambiguous = false; |
3750 | 0 | for (SmallVectorImpl<MatchResult>::iterator P = Matched.begin(), |
3751 | 0 | PEnd = Matched.end(); |
3752 | 0 | P != PEnd; ++P) { |
3753 | 0 | if (P != Best && S.getMoreSpecializedPartialSpecialization( |
3754 | 0 | P->Partial, Best->Partial, |
3755 | 0 | PointOfInstantiation) != Best->Partial) { |
3756 | 0 | Ambiguous = true; |
3757 | 0 | break; |
3758 | 0 | } |
3759 | 0 | } |
3760 | |
|
3761 | 0 | if (Ambiguous) { |
3762 | | // Partial ordering did not produce a clear winner. Complain. |
3763 | 0 | Inst.Clear(); |
3764 | 0 | ClassTemplateSpec->setInvalidDecl(); |
3765 | 0 | S.Diag(PointOfInstantiation, |
3766 | 0 | diag::err_partial_spec_ordering_ambiguous) |
3767 | 0 | << ClassTemplateSpec; |
3768 | | |
3769 | | // Print the matching partial specializations. |
3770 | 0 | for (SmallVectorImpl<MatchResult>::iterator P = Matched.begin(), |
3771 | 0 | PEnd = Matched.end(); |
3772 | 0 | P != PEnd; ++P) |
3773 | 0 | S.Diag(P->Partial->getLocation(), diag::note_partial_spec_match) |
3774 | 0 | << S.getTemplateArgumentBindingsText( |
3775 | 0 | P->Partial->getTemplateParameters(), *P->Args); |
3776 | |
|
3777 | 0 | return {/*Invalid=*/true}; |
3778 | 0 | } |
3779 | 0 | } |
3780 | | |
3781 | 0 | ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args); |
3782 | 0 | } else { |
3783 | | // -- If no matches are found, the instantiation is generated |
3784 | | // from the primary template. |
3785 | 0 | } |
3786 | 0 | } |
3787 | | |
3788 | 0 | CXXRecordDecl *Pattern = nullptr; |
3789 | 0 | Specialized = ClassTemplateSpec->getSpecializedTemplateOrPartial(); |
3790 | 0 | if (auto *PartialSpec = |
3791 | 0 | Specialized.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
3792 | | // Instantiate using the best class template partial specialization. |
3793 | 0 | while (PartialSpec->getInstantiatedFromMember()) { |
3794 | | // If we've found an explicit specialization of this class template, |
3795 | | // stop here and use that as the pattern. |
3796 | 0 | if (PartialSpec->isMemberSpecialization()) |
3797 | 0 | break; |
3798 | | |
3799 | 0 | PartialSpec = PartialSpec->getInstantiatedFromMember(); |
3800 | 0 | } |
3801 | 0 | Pattern = PartialSpec; |
3802 | 0 | } else { |
3803 | 0 | ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
3804 | 0 | while (Template->getInstantiatedFromMemberTemplate()) { |
3805 | | // If we've found an explicit specialization of this class template, |
3806 | | // stop here and use that as the pattern. |
3807 | 0 | if (Template->isMemberSpecialization()) |
3808 | 0 | break; |
3809 | | |
3810 | 0 | Template = Template->getInstantiatedFromMemberTemplate(); |
3811 | 0 | } |
3812 | 0 | Pattern = Template->getTemplatedDecl(); |
3813 | 0 | } |
3814 | |
|
3815 | 0 | return Pattern; |
3816 | 0 | } |
3817 | | |
3818 | | bool Sema::InstantiateClassTemplateSpecialization( |
3819 | | SourceLocation PointOfInstantiation, |
3820 | | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
3821 | 0 | TemplateSpecializationKind TSK, bool Complain) { |
3822 | | // Perform the actual instantiation on the canonical declaration. |
3823 | 0 | ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( |
3824 | 0 | ClassTemplateSpec->getCanonicalDecl()); |
3825 | 0 | if (ClassTemplateSpec->isInvalidDecl()) |
3826 | 0 | return true; |
3827 | | |
3828 | 0 | ActionResult<CXXRecordDecl *> Pattern = |
3829 | 0 | getPatternForClassTemplateSpecialization(*this, PointOfInstantiation, |
3830 | 0 | ClassTemplateSpec, TSK); |
3831 | 0 | if (!Pattern.isUsable()) |
3832 | 0 | return Pattern.isInvalid(); |
3833 | | |
3834 | 0 | return InstantiateClass( |
3835 | 0 | PointOfInstantiation, ClassTemplateSpec, Pattern.get(), |
3836 | 0 | getTemplateInstantiationArgs(ClassTemplateSpec), TSK, Complain); |
3837 | 0 | } |
3838 | | |
3839 | | /// Instantiates the definitions of all of the member |
3840 | | /// of the given class, which is an instantiation of a class template |
3841 | | /// or a member class of a template. |
3842 | | void |
3843 | | Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation, |
3844 | | CXXRecordDecl *Instantiation, |
3845 | | const MultiLevelTemplateArgumentList &TemplateArgs, |
3846 | 0 | TemplateSpecializationKind TSK) { |
3847 | | // FIXME: We need to notify the ASTMutationListener that we did all of these |
3848 | | // things, in case we have an explicit instantiation definition in a PCM, a |
3849 | | // module, or preamble, and the declaration is in an imported AST. |
3850 | 0 | assert( |
3851 | 0 | (TSK == TSK_ExplicitInstantiationDefinition || |
3852 | 0 | TSK == TSK_ExplicitInstantiationDeclaration || |
3853 | 0 | (TSK == TSK_ImplicitInstantiation && Instantiation->isLocalClass())) && |
3854 | 0 | "Unexpected template specialization kind!"); |
3855 | 0 | for (auto *D : Instantiation->decls()) { |
3856 | 0 | bool SuppressNew = false; |
3857 | 0 | if (auto *Function = dyn_cast<FunctionDecl>(D)) { |
3858 | 0 | if (FunctionDecl *Pattern = |
3859 | 0 | Function->getInstantiatedFromMemberFunction()) { |
3860 | |
|
3861 | 0 | if (Function->isIneligibleOrNotSelected()) |
3862 | 0 | continue; |
3863 | | |
3864 | 0 | if (Function->getTrailingRequiresClause()) { |
3865 | 0 | ConstraintSatisfaction Satisfaction; |
3866 | 0 | if (CheckFunctionConstraints(Function, Satisfaction) || |
3867 | 0 | !Satisfaction.IsSatisfied) { |
3868 | 0 | continue; |
3869 | 0 | } |
3870 | 0 | } |
3871 | | |
3872 | 0 | if (Function->hasAttr<ExcludeFromExplicitInstantiationAttr>()) |
3873 | 0 | continue; |
3874 | | |
3875 | 0 | MemberSpecializationInfo *MSInfo = |
3876 | 0 | Function->getMemberSpecializationInfo(); |
3877 | 0 | assert(MSInfo && "No member specialization information?"); |
3878 | 0 | if (MSInfo->getTemplateSpecializationKind() |
3879 | 0 | == TSK_ExplicitSpecialization) |
3880 | 0 | continue; |
3881 | | |
3882 | 0 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
3883 | 0 | Function, |
3884 | 0 | MSInfo->getTemplateSpecializationKind(), |
3885 | 0 | MSInfo->getPointOfInstantiation(), |
3886 | 0 | SuppressNew) || |
3887 | 0 | SuppressNew) |
3888 | 0 | continue; |
3889 | | |
3890 | | // C++11 [temp.explicit]p8: |
3891 | | // An explicit instantiation definition that names a class template |
3892 | | // specialization explicitly instantiates the class template |
3893 | | // specialization and is only an explicit instantiation definition |
3894 | | // of members whose definition is visible at the point of |
3895 | | // instantiation. |
3896 | 0 | if (TSK == TSK_ExplicitInstantiationDefinition && !Pattern->isDefined()) |
3897 | 0 | continue; |
3898 | | |
3899 | 0 | Function->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
3900 | |
|
3901 | 0 | if (Function->isDefined()) { |
3902 | | // Let the ASTConsumer know that this function has been explicitly |
3903 | | // instantiated now, and its linkage might have changed. |
3904 | 0 | Consumer.HandleTopLevelDecl(DeclGroupRef(Function)); |
3905 | 0 | } else if (TSK == TSK_ExplicitInstantiationDefinition) { |
3906 | 0 | InstantiateFunctionDefinition(PointOfInstantiation, Function); |
3907 | 0 | } else if (TSK == TSK_ImplicitInstantiation) { |
3908 | 0 | PendingLocalImplicitInstantiations.push_back( |
3909 | 0 | std::make_pair(Function, PointOfInstantiation)); |
3910 | 0 | } |
3911 | 0 | } |
3912 | 0 | } else if (auto *Var = dyn_cast<VarDecl>(D)) { |
3913 | 0 | if (isa<VarTemplateSpecializationDecl>(Var)) |
3914 | 0 | continue; |
3915 | | |
3916 | 0 | if (Var->isStaticDataMember()) { |
3917 | 0 | if (Var->hasAttr<ExcludeFromExplicitInstantiationAttr>()) |
3918 | 0 | continue; |
3919 | | |
3920 | 0 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
3921 | 0 | assert(MSInfo && "No member specialization information?"); |
3922 | 0 | if (MSInfo->getTemplateSpecializationKind() |
3923 | 0 | == TSK_ExplicitSpecialization) |
3924 | 0 | continue; |
3925 | | |
3926 | 0 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
3927 | 0 | Var, |
3928 | 0 | MSInfo->getTemplateSpecializationKind(), |
3929 | 0 | MSInfo->getPointOfInstantiation(), |
3930 | 0 | SuppressNew) || |
3931 | 0 | SuppressNew) |
3932 | 0 | continue; |
3933 | | |
3934 | 0 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
3935 | | // C++0x [temp.explicit]p8: |
3936 | | // An explicit instantiation definition that names a class template |
3937 | | // specialization explicitly instantiates the class template |
3938 | | // specialization and is only an explicit instantiation definition |
3939 | | // of members whose definition is visible at the point of |
3940 | | // instantiation. |
3941 | 0 | if (!Var->getInstantiatedFromStaticDataMember()->getDefinition()) |
3942 | 0 | continue; |
3943 | | |
3944 | 0 | Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
3945 | 0 | InstantiateVariableDefinition(PointOfInstantiation, Var); |
3946 | 0 | } else { |
3947 | 0 | Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); |
3948 | 0 | } |
3949 | 0 | } |
3950 | 0 | } else if (auto *Record = dyn_cast<CXXRecordDecl>(D)) { |
3951 | 0 | if (Record->hasAttr<ExcludeFromExplicitInstantiationAttr>()) |
3952 | 0 | continue; |
3953 | | |
3954 | | // Always skip the injected-class-name, along with any |
3955 | | // redeclarations of nested classes, since both would cause us |
3956 | | // to try to instantiate the members of a class twice. |
3957 | | // Skip closure types; they'll get instantiated when we instantiate |
3958 | | // the corresponding lambda-expression. |
3959 | 0 | if (Record->isInjectedClassName() || Record->getPreviousDecl() || |
3960 | 0 | Record->isLambda()) |
3961 | 0 | continue; |
3962 | | |
3963 | 0 | MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo(); |
3964 | 0 | assert(MSInfo && "No member specialization information?"); |
3965 | | |
3966 | 0 | if (MSInfo->getTemplateSpecializationKind() |
3967 | 0 | == TSK_ExplicitSpecialization) |
3968 | 0 | continue; |
3969 | | |
3970 | 0 | if (Context.getTargetInfo().getTriple().isOSWindows() && |
3971 | 0 | TSK == TSK_ExplicitInstantiationDeclaration) { |
3972 | | // On Windows, explicit instantiation decl of the outer class doesn't |
3973 | | // affect the inner class. Typically extern template declarations are |
3974 | | // used in combination with dll import/export annotations, but those |
3975 | | // are not propagated from the outer class templates to inner classes. |
3976 | | // Therefore, do not instantiate inner classes on this platform, so |
3977 | | // that users don't end up with undefined symbols during linking. |
3978 | 0 | continue; |
3979 | 0 | } |
3980 | | |
3981 | 0 | if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, |
3982 | 0 | Record, |
3983 | 0 | MSInfo->getTemplateSpecializationKind(), |
3984 | 0 | MSInfo->getPointOfInstantiation(), |
3985 | 0 | SuppressNew) || |
3986 | 0 | SuppressNew) |
3987 | 0 | continue; |
3988 | | |
3989 | 0 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
3990 | 0 | assert(Pattern && "Missing instantiated-from-template information"); |
3991 | | |
3992 | 0 | if (!Record->getDefinition()) { |
3993 | 0 | if (!Pattern->getDefinition()) { |
3994 | | // C++0x [temp.explicit]p8: |
3995 | | // An explicit instantiation definition that names a class template |
3996 | | // specialization explicitly instantiates the class template |
3997 | | // specialization and is only an explicit instantiation definition |
3998 | | // of members whose definition is visible at the point of |
3999 | | // instantiation. |
4000 | 0 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
4001 | 0 | MSInfo->setTemplateSpecializationKind(TSK); |
4002 | 0 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
4003 | 0 | } |
4004 | |
|
4005 | 0 | continue; |
4006 | 0 | } |
4007 | | |
4008 | 0 | InstantiateClass(PointOfInstantiation, Record, Pattern, |
4009 | 0 | TemplateArgs, |
4010 | 0 | TSK); |
4011 | 0 | } else { |
4012 | 0 | if (TSK == TSK_ExplicitInstantiationDefinition && |
4013 | 0 | Record->getTemplateSpecializationKind() == |
4014 | 0 | TSK_ExplicitInstantiationDeclaration) { |
4015 | 0 | Record->setTemplateSpecializationKind(TSK); |
4016 | 0 | MarkVTableUsed(PointOfInstantiation, Record, true); |
4017 | 0 | } |
4018 | 0 | } |
4019 | | |
4020 | 0 | Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
4021 | 0 | if (Pattern) |
4022 | 0 | InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs, |
4023 | 0 | TSK); |
4024 | 0 | } else if (auto *Enum = dyn_cast<EnumDecl>(D)) { |
4025 | 0 | MemberSpecializationInfo *MSInfo = Enum->getMemberSpecializationInfo(); |
4026 | 0 | assert(MSInfo && "No member specialization information?"); |
4027 | | |
4028 | 0 | if (MSInfo->getTemplateSpecializationKind() |
4029 | 0 | == TSK_ExplicitSpecialization) |
4030 | 0 | continue; |
4031 | | |
4032 | 0 | if (CheckSpecializationInstantiationRedecl( |
4033 | 0 | PointOfInstantiation, TSK, Enum, |
4034 | 0 | MSInfo->getTemplateSpecializationKind(), |
4035 | 0 | MSInfo->getPointOfInstantiation(), SuppressNew) || |
4036 | 0 | SuppressNew) |
4037 | 0 | continue; |
4038 | | |
4039 | 0 | if (Enum->getDefinition()) |
4040 | 0 | continue; |
4041 | | |
4042 | 0 | EnumDecl *Pattern = Enum->getTemplateInstantiationPattern(); |
4043 | 0 | assert(Pattern && "Missing instantiated-from-template information"); |
4044 | | |
4045 | 0 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
4046 | 0 | if (!Pattern->getDefinition()) |
4047 | 0 | continue; |
4048 | | |
4049 | 0 | InstantiateEnum(PointOfInstantiation, Enum, Pattern, TemplateArgs, TSK); |
4050 | 0 | } else { |
4051 | 0 | MSInfo->setTemplateSpecializationKind(TSK); |
4052 | 0 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
4053 | 0 | } |
4054 | 0 | } else if (auto *Field = dyn_cast<FieldDecl>(D)) { |
4055 | | // No need to instantiate in-class initializers during explicit |
4056 | | // instantiation. |
4057 | 0 | if (Field->hasInClassInitializer() && TSK == TSK_ImplicitInstantiation) { |
4058 | 0 | CXXRecordDecl *ClassPattern = |
4059 | 0 | Instantiation->getTemplateInstantiationPattern(); |
4060 | 0 | DeclContext::lookup_result Lookup = |
4061 | 0 | ClassPattern->lookup(Field->getDeclName()); |
4062 | 0 | FieldDecl *Pattern = Lookup.find_first<FieldDecl>(); |
4063 | 0 | assert(Pattern); |
4064 | 0 | InstantiateInClassInitializer(PointOfInstantiation, Field, Pattern, |
4065 | 0 | TemplateArgs); |
4066 | 0 | } |
4067 | 0 | } |
4068 | 0 | } |
4069 | 0 | } |
4070 | | |
4071 | | /// Instantiate the definitions of all of the members of the |
4072 | | /// given class template specialization, which was named as part of an |
4073 | | /// explicit instantiation. |
4074 | | void |
4075 | | Sema::InstantiateClassTemplateSpecializationMembers( |
4076 | | SourceLocation PointOfInstantiation, |
4077 | | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
4078 | 0 | TemplateSpecializationKind TSK) { |
4079 | | // C++0x [temp.explicit]p7: |
4080 | | // An explicit instantiation that names a class template |
4081 | | // specialization is an explicit instantion of the same kind |
4082 | | // (declaration or definition) of each of its members (not |
4083 | | // including members inherited from base classes) that has not |
4084 | | // been previously explicitly specialized in the translation unit |
4085 | | // containing the explicit instantiation, except as described |
4086 | | // below. |
4087 | 0 | InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec, |
4088 | 0 | getTemplateInstantiationArgs(ClassTemplateSpec), |
4089 | 0 | TSK); |
4090 | 0 | } |
4091 | | |
4092 | | StmtResult |
4093 | 0 | Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) { |
4094 | 0 | if (!S) |
4095 | 0 | return S; |
4096 | | |
4097 | 0 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
4098 | 0 | SourceLocation(), |
4099 | 0 | DeclarationName()); |
4100 | 0 | return Instantiator.TransformStmt(S); |
4101 | 0 | } |
4102 | | |
4103 | | bool Sema::SubstTemplateArguments( |
4104 | | ArrayRef<TemplateArgumentLoc> Args, |
4105 | | const MultiLevelTemplateArgumentList &TemplateArgs, |
4106 | 0 | TemplateArgumentListInfo &Out) { |
4107 | 0 | TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), |
4108 | 0 | DeclarationName()); |
4109 | 0 | return Instantiator.TransformTemplateArguments(Args.begin(), Args.end(), Out); |
4110 | 0 | } |
4111 | | |
4112 | | ExprResult |
4113 | 0 | Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) { |
4114 | 0 | if (!E) |
4115 | 0 | return E; |
4116 | | |
4117 | 0 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
4118 | 0 | SourceLocation(), |
4119 | 0 | DeclarationName()); |
4120 | 0 | return Instantiator.TransformExpr(E); |
4121 | 0 | } |
4122 | | |
4123 | | ExprResult |
4124 | | Sema::SubstConstraintExpr(Expr *E, |
4125 | 0 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
4126 | | // FIXME: should call SubstExpr directly if this function is equivalent or |
4127 | | // should it be different? |
4128 | 0 | return SubstExpr(E, TemplateArgs); |
4129 | 0 | } |
4130 | | |
4131 | | ExprResult Sema::SubstConstraintExprWithoutSatisfaction( |
4132 | 0 | Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) { |
4133 | 0 | if (!E) |
4134 | 0 | return E; |
4135 | | |
4136 | 0 | TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), |
4137 | 0 | DeclarationName()); |
4138 | 0 | Instantiator.setEvaluateConstraints(false); |
4139 | 0 | return Instantiator.TransformExpr(E); |
4140 | 0 | } |
4141 | | |
4142 | | ExprResult Sema::SubstInitializer(Expr *Init, |
4143 | | const MultiLevelTemplateArgumentList &TemplateArgs, |
4144 | 0 | bool CXXDirectInit) { |
4145 | 0 | TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), |
4146 | 0 | DeclarationName()); |
4147 | 0 | return Instantiator.TransformInitializer(Init, CXXDirectInit); |
4148 | 0 | } |
4149 | | |
4150 | | bool Sema::SubstExprs(ArrayRef<Expr *> Exprs, bool IsCall, |
4151 | | const MultiLevelTemplateArgumentList &TemplateArgs, |
4152 | 0 | SmallVectorImpl<Expr *> &Outputs) { |
4153 | 0 | if (Exprs.empty()) |
4154 | 0 | return false; |
4155 | | |
4156 | 0 | TemplateInstantiator Instantiator(*this, TemplateArgs, |
4157 | 0 | SourceLocation(), |
4158 | 0 | DeclarationName()); |
4159 | 0 | return Instantiator.TransformExprs(Exprs.data(), Exprs.size(), |
4160 | 0 | IsCall, Outputs); |
4161 | 0 | } |
4162 | | |
4163 | | NestedNameSpecifierLoc |
4164 | | Sema::SubstNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS, |
4165 | 0 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
4166 | 0 | if (!NNS) |
4167 | 0 | return NestedNameSpecifierLoc(); |
4168 | | |
4169 | 0 | TemplateInstantiator Instantiator(*this, TemplateArgs, NNS.getBeginLoc(), |
4170 | 0 | DeclarationName()); |
4171 | 0 | return Instantiator.TransformNestedNameSpecifierLoc(NNS); |
4172 | 0 | } |
4173 | | |
4174 | | /// Do template substitution on declaration name info. |
4175 | | DeclarationNameInfo |
4176 | | Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo, |
4177 | 0 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
4178 | 0 | TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(), |
4179 | 0 | NameInfo.getName()); |
4180 | 0 | return Instantiator.TransformDeclarationNameInfo(NameInfo); |
4181 | 0 | } |
4182 | | |
4183 | | TemplateName |
4184 | | Sema::SubstTemplateName(NestedNameSpecifierLoc QualifierLoc, |
4185 | | TemplateName Name, SourceLocation Loc, |
4186 | 0 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
4187 | 0 | TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, |
4188 | 0 | DeclarationName()); |
4189 | 0 | CXXScopeSpec SS; |
4190 | 0 | SS.Adopt(QualifierLoc); |
4191 | 0 | return Instantiator.TransformTemplateName(SS, Name, Loc); |
4192 | 0 | } |
4193 | | |
4194 | 0 | static const Decl *getCanonicalParmVarDecl(const Decl *D) { |
4195 | | // When storing ParmVarDecls in the local instantiation scope, we always |
4196 | | // want to use the ParmVarDecl from the canonical function declaration, |
4197 | | // since the map is then valid for any redeclaration or definition of that |
4198 | | // function. |
4199 | 0 | if (const ParmVarDecl *PV = dyn_cast<ParmVarDecl>(D)) { |
4200 | 0 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(PV->getDeclContext())) { |
4201 | 0 | unsigned i = PV->getFunctionScopeIndex(); |
4202 | | // This parameter might be from a freestanding function type within the |
4203 | | // function and isn't necessarily referring to one of FD's parameters. |
4204 | 0 | if (i < FD->getNumParams() && FD->getParamDecl(i) == PV) |
4205 | 0 | return FD->getCanonicalDecl()->getParamDecl(i); |
4206 | 0 | } |
4207 | 0 | } |
4208 | 0 | return D; |
4209 | 0 | } |
4210 | | |
4211 | | |
4212 | | llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> * |
4213 | 0 | LocalInstantiationScope::findInstantiationOf(const Decl *D) { |
4214 | 0 | D = getCanonicalParmVarDecl(D); |
4215 | 0 | for (LocalInstantiationScope *Current = this; Current; |
4216 | 0 | Current = Current->Outer) { |
4217 | | |
4218 | | // Check if we found something within this scope. |
4219 | 0 | const Decl *CheckD = D; |
4220 | 0 | do { |
4221 | 0 | LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD); |
4222 | 0 | if (Found != Current->LocalDecls.end()) |
4223 | 0 | return &Found->second; |
4224 | | |
4225 | | // If this is a tag declaration, it's possible that we need to look for |
4226 | | // a previous declaration. |
4227 | 0 | if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD)) |
4228 | 0 | CheckD = Tag->getPreviousDecl(); |
4229 | 0 | else |
4230 | 0 | CheckD = nullptr; |
4231 | 0 | } while (CheckD); |
4232 | | |
4233 | | // If we aren't combined with our outer scope, we're done. |
4234 | 0 | if (!Current->CombineWithOuterScope) |
4235 | 0 | break; |
4236 | 0 | } |
4237 | | |
4238 | | // If we're performing a partial substitution during template argument |
4239 | | // deduction, we may not have values for template parameters yet. |
4240 | 0 | if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) || |
4241 | 0 | isa<TemplateTemplateParmDecl>(D)) |
4242 | 0 | return nullptr; |
4243 | | |
4244 | | // Local types referenced prior to definition may require instantiation. |
4245 | 0 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
4246 | 0 | if (RD->isLocalClass()) |
4247 | 0 | return nullptr; |
4248 | | |
4249 | | // Enumeration types referenced prior to definition may appear as a result of |
4250 | | // error recovery. |
4251 | 0 | if (isa<EnumDecl>(D)) |
4252 | 0 | return nullptr; |
4253 | | |
4254 | | // Materialized typedefs/type alias for implicit deduction guides may require |
4255 | | // instantiation. |
4256 | 0 | if (isa<TypedefNameDecl>(D) && |
4257 | 0 | isa<CXXDeductionGuideDecl>(D->getDeclContext())) |
4258 | 0 | return nullptr; |
4259 | | |
4260 | | // If we didn't find the decl, then we either have a sema bug, or we have a |
4261 | | // forward reference to a label declaration. Return null to indicate that |
4262 | | // we have an uninstantiated label. |
4263 | 0 | assert(isa<LabelDecl>(D) && "declaration not instantiated in this scope"); |
4264 | 0 | return nullptr; |
4265 | 0 | } |
4266 | | |
4267 | 0 | void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) { |
4268 | 0 | D = getCanonicalParmVarDecl(D); |
4269 | 0 | llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D]; |
4270 | 0 | if (Stored.isNull()) { |
4271 | 0 | #ifndef NDEBUG |
4272 | | // It should not be present in any surrounding scope either. |
4273 | 0 | LocalInstantiationScope *Current = this; |
4274 | 0 | while (Current->CombineWithOuterScope && Current->Outer) { |
4275 | 0 | Current = Current->Outer; |
4276 | 0 | assert(!Current->LocalDecls.contains(D) && |
4277 | 0 | "Instantiated local in inner and outer scopes"); |
4278 | 0 | } |
4279 | 0 | #endif |
4280 | 0 | Stored = Inst; |
4281 | 0 | } else if (DeclArgumentPack *Pack = Stored.dyn_cast<DeclArgumentPack *>()) { |
4282 | 0 | Pack->push_back(cast<VarDecl>(Inst)); |
4283 | 0 | } else { |
4284 | 0 | assert(Stored.get<Decl *>() == Inst && "Already instantiated this local"); |
4285 | 0 | } |
4286 | 0 | } |
4287 | | |
4288 | | void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D, |
4289 | 0 | VarDecl *Inst) { |
4290 | 0 | D = getCanonicalParmVarDecl(D); |
4291 | 0 | DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>(); |
4292 | 0 | Pack->push_back(Inst); |
4293 | 0 | } |
4294 | | |
4295 | 0 | void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) { |
4296 | 0 | #ifndef NDEBUG |
4297 | | // This should be the first time we've been told about this decl. |
4298 | 0 | for (LocalInstantiationScope *Current = this; |
4299 | 0 | Current && Current->CombineWithOuterScope; Current = Current->Outer) |
4300 | 0 | assert(!Current->LocalDecls.contains(D) && |
4301 | 0 | "Creating local pack after instantiation of local"); |
4302 | 0 | #endif |
4303 | |
|
4304 | 0 | D = getCanonicalParmVarDecl(D); |
4305 | 0 | llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D]; |
4306 | 0 | DeclArgumentPack *Pack = new DeclArgumentPack; |
4307 | 0 | Stored = Pack; |
4308 | 0 | ArgumentPacks.push_back(Pack); |
4309 | 0 | } |
4310 | | |
4311 | 0 | bool LocalInstantiationScope::isLocalPackExpansion(const Decl *D) { |
4312 | 0 | for (DeclArgumentPack *Pack : ArgumentPacks) |
4313 | 0 | if (llvm::is_contained(*Pack, D)) |
4314 | 0 | return true; |
4315 | 0 | return false; |
4316 | 0 | } |
4317 | | |
4318 | | void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack, |
4319 | | const TemplateArgument *ExplicitArgs, |
4320 | 0 | unsigned NumExplicitArgs) { |
4321 | 0 | assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) && |
4322 | 0 | "Already have a partially-substituted pack"); |
4323 | 0 | assert((!PartiallySubstitutedPack |
4324 | 0 | || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) && |
4325 | 0 | "Wrong number of arguments in partially-substituted pack"); |
4326 | 0 | PartiallySubstitutedPack = Pack; |
4327 | 0 | ArgsInPartiallySubstitutedPack = ExplicitArgs; |
4328 | 0 | NumArgsInPartiallySubstitutedPack = NumExplicitArgs; |
4329 | 0 | } |
4330 | | |
4331 | | NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack( |
4332 | | const TemplateArgument **ExplicitArgs, |
4333 | 0 | unsigned *NumExplicitArgs) const { |
4334 | 0 | if (ExplicitArgs) |
4335 | 0 | *ExplicitArgs = nullptr; |
4336 | 0 | if (NumExplicitArgs) |
4337 | 0 | *NumExplicitArgs = 0; |
4338 | |
|
4339 | 0 | for (const LocalInstantiationScope *Current = this; Current; |
4340 | 0 | Current = Current->Outer) { |
4341 | 0 | if (Current->PartiallySubstitutedPack) { |
4342 | 0 | if (ExplicitArgs) |
4343 | 0 | *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack; |
4344 | 0 | if (NumExplicitArgs) |
4345 | 0 | *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack; |
4346 | |
|
4347 | 0 | return Current->PartiallySubstitutedPack; |
4348 | 0 | } |
4349 | | |
4350 | 0 | if (!Current->CombineWithOuterScope) |
4351 | 0 | break; |
4352 | 0 | } |
4353 | | |
4354 | 0 | return nullptr; |
4355 | 0 | } |