1 | //===- DeclTemplate.h - Classes for representing C++ templates --*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | /// \file |
10 | /// Defines the C++ template declaration subclasses. |
11 | // |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #ifndef LLVM_CLANG_AST_DECLTEMPLATE_H |
15 | #define LLVM_CLANG_AST_DECLTEMPLATE_H |
16 | |
17 | #include "clang/AST/ASTConcept.h" |
18 | #include "clang/AST/ASTContext.h" |
19 | #include "clang/AST/Decl.h" |
20 | #include "clang/AST/DeclBase.h" |
21 | #include "clang/AST/DeclCXX.h" |
22 | #include "clang/AST/DeclarationName.h" |
23 | #include "clang/AST/Redeclarable.h" |
24 | #include "clang/AST/TemplateBase.h" |
25 | #include "clang/AST/Type.h" |
26 | #include "clang/Basic/LLVM.h" |
27 | #include "clang/Basic/SourceLocation.h" |
28 | #include "clang/Basic/Specifiers.h" |
29 | #include "llvm/ADT/ArrayRef.h" |
30 | #include "llvm/ADT/FoldingSet.h" |
31 | #include "llvm/ADT/PointerIntPair.h" |
32 | #include "llvm/ADT/PointerUnion.h" |
33 | #include "llvm/ADT/iterator.h" |
34 | #include "llvm/ADT/iterator_range.h" |
35 | #include "llvm/Support/Casting.h" |
36 | #include "llvm/Support/Compiler.h" |
37 | #include "llvm/Support/TrailingObjects.h" |
38 | #include <cassert> |
39 | #include <cstddef> |
40 | #include <cstdint> |
41 | #include <iterator> |
42 | #include <optional> |
43 | #include <utility> |
44 | |
45 | namespace clang { |
46 | |
47 | enum BuiltinTemplateKind : int; |
48 | class ClassTemplateDecl; |
49 | class ClassTemplatePartialSpecializationDecl; |
50 | class Expr; |
51 | class FunctionTemplateDecl; |
52 | class IdentifierInfo; |
53 | class NonTypeTemplateParmDecl; |
54 | class TemplateDecl; |
55 | class TemplateTemplateParmDecl; |
56 | class TemplateTypeParmDecl; |
57 | class ConceptDecl; |
58 | class UnresolvedSetImpl; |
59 | class VarTemplateDecl; |
60 | class VarTemplatePartialSpecializationDecl; |
61 | |
62 | /// Stores a template parameter of any kind. |
63 | using TemplateParameter = |
64 | llvm::PointerUnion<TemplateTypeParmDecl *, NonTypeTemplateParmDecl *, |
65 | TemplateTemplateParmDecl *>; |
66 | |
67 | NamedDecl *getAsNamedDecl(TemplateParameter P); |
68 | |
69 | /// Stores a list of template parameters for a TemplateDecl and its |
70 | /// derived classes. |
71 | class TemplateParameterList final |
72 | : private llvm::TrailingObjects<TemplateParameterList, NamedDecl *, |
73 | Expr *> { |
74 | /// The location of the 'template' keyword. |
75 | SourceLocation TemplateLoc; |
76 | |
77 | /// The locations of the '<' and '>' angle brackets. |
78 | SourceLocation LAngleLoc, RAngleLoc; |
79 | |
80 | /// The number of template parameters in this template |
81 | /// parameter list. |
82 | unsigned NumParams : 29; |
83 | |
84 | /// Whether this template parameter list contains an unexpanded parameter |
85 | /// pack. |
86 | LLVM_PREFERRED_TYPE(bool) |
87 | unsigned ContainsUnexpandedParameterPack : 1; |
88 | |
89 | /// Whether this template parameter list has a requires clause. |
90 | LLVM_PREFERRED_TYPE(bool) |
91 | unsigned HasRequiresClause : 1; |
92 | |
93 | /// Whether any of the template parameters has constrained-parameter |
94 | /// constraint-expression. |
95 | LLVM_PREFERRED_TYPE(bool) |
96 | unsigned HasConstrainedParameters : 1; |
97 | |
98 | protected: |
99 | TemplateParameterList(const ASTContext& C, SourceLocation TemplateLoc, |
100 | SourceLocation LAngleLoc, ArrayRef<NamedDecl *> Params, |
101 | SourceLocation RAngleLoc, Expr *RequiresClause); |
102 | |
103 | size_t numTrailingObjects(OverloadToken<NamedDecl *>) const { |
104 | return NumParams; |
105 | } |
106 | |
107 | size_t numTrailingObjects(OverloadToken<Expr *>) const { |
108 | return HasRequiresClause ? 1 : 0; |
109 | } |
110 | |
111 | public: |
112 | template <size_t N, bool HasRequiresClause> |
113 | friend class FixedSizeTemplateParameterListStorage; |
114 | friend TrailingObjects; |
115 | |
116 | static TemplateParameterList *Create(const ASTContext &C, |
117 | SourceLocation TemplateLoc, |
118 | SourceLocation LAngleLoc, |
119 | ArrayRef<NamedDecl *> Params, |
120 | SourceLocation RAngleLoc, |
121 | Expr *RequiresClause); |
122 | |
123 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &C) const; |
124 | |
125 | /// Iterates through the template parameters in this list. |
126 | using iterator = NamedDecl **; |
127 | |
128 | /// Iterates through the template parameters in this list. |
129 | using const_iterator = NamedDecl * const *; |
130 | |
131 | iterator begin() { return getTrailingObjects<NamedDecl *>(); } |
132 | const_iterator begin() const { return getTrailingObjects<NamedDecl *>(); } |
133 | iterator end() { return begin() + NumParams; } |
134 | const_iterator end() const { return begin() + NumParams; } |
135 | |
136 | unsigned size() const { return NumParams; } |
137 | bool empty() const { return NumParams == 0; } |
138 | |
139 | ArrayRef<NamedDecl *> asArray() { return llvm::ArrayRef(begin(), end()); } |
140 | ArrayRef<const NamedDecl*> asArray() const { |
141 | return llvm::ArrayRef(begin(), size()); |
142 | } |
143 | |
144 | NamedDecl* getParam(unsigned Idx) { |
145 | assert(Idx < size() && "Template parameter index out-of-range" ); |
146 | return begin()[Idx]; |
147 | } |
148 | const NamedDecl* getParam(unsigned Idx) const { |
149 | assert(Idx < size() && "Template parameter index out-of-range" ); |
150 | return begin()[Idx]; |
151 | } |
152 | |
153 | /// Returns the minimum number of arguments needed to form a |
154 | /// template specialization. |
155 | /// |
156 | /// This may be fewer than the number of template parameters, if some of |
157 | /// the parameters have default arguments or if there is a parameter pack. |
158 | unsigned getMinRequiredArguments() const; |
159 | |
160 | /// Get the depth of this template parameter list in the set of |
161 | /// template parameter lists. |
162 | /// |
163 | /// The first template parameter list in a declaration will have depth 0, |
164 | /// the second template parameter list will have depth 1, etc. |
165 | unsigned getDepth() const; |
166 | |
167 | /// Determine whether this template parameter list contains an |
168 | /// unexpanded parameter pack. |
169 | bool containsUnexpandedParameterPack() const; |
170 | |
171 | /// Determine whether this template parameter list contains a parameter pack. |
172 | bool hasParameterPack() const { |
173 | for (const NamedDecl *P : asArray()) |
174 | if (P->isParameterPack()) |
175 | return true; |
176 | return false; |
177 | } |
178 | |
179 | /// The constraint-expression of the associated requires-clause. |
180 | Expr *getRequiresClause() { |
181 | return HasRequiresClause ? getTrailingObjects<Expr *>()[0] : nullptr; |
182 | } |
183 | |
184 | /// The constraint-expression of the associated requires-clause. |
185 | const Expr *getRequiresClause() const { |
186 | return HasRequiresClause ? getTrailingObjects<Expr *>()[0] : nullptr; |
187 | } |
188 | |
189 | /// \brief All associated constraints derived from this template parameter |
190 | /// list, including the requires clause and any constraints derived from |
191 | /// constrained-parameters. |
192 | /// |
193 | /// The constraints in the resulting list are to be treated as if in a |
194 | /// conjunction ("and"). |
195 | void getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const; |
196 | |
197 | bool hasAssociatedConstraints() const; |
198 | |
199 | SourceLocation getTemplateLoc() const { return TemplateLoc; } |
200 | SourceLocation getLAngleLoc() const { return LAngleLoc; } |
201 | SourceLocation getRAngleLoc() const { return RAngleLoc; } |
202 | |
203 | SourceRange getSourceRange() const LLVM_READONLY { |
204 | return SourceRange(TemplateLoc, RAngleLoc); |
205 | } |
206 | |
207 | void print(raw_ostream &Out, const ASTContext &Context, |
208 | bool OmitTemplateKW = false) const; |
209 | void print(raw_ostream &Out, const ASTContext &Context, |
210 | const PrintingPolicy &Policy, bool OmitTemplateKW = false) const; |
211 | |
212 | static bool shouldIncludeTypeForArgument(const PrintingPolicy &Policy, |
213 | const TemplateParameterList *TPL, |
214 | unsigned Idx); |
215 | }; |
216 | |
217 | /// Stores a list of template parameters and the associated |
218 | /// requires-clause (if any) for a TemplateDecl and its derived classes. |
219 | /// Suitable for creating on the stack. |
220 | template <size_t N, bool HasRequiresClause> |
221 | class FixedSizeTemplateParameterListStorage |
222 | : public TemplateParameterList::FixedSizeStorageOwner { |
223 | typename TemplateParameterList::FixedSizeStorage< |
224 | NamedDecl *, Expr *>::with_counts< |
225 | N, HasRequiresClause ? 1u : 0u |
226 | >::type storage; |
227 | |
228 | public: |
229 | FixedSizeTemplateParameterListStorage(const ASTContext &C, |
230 | SourceLocation TemplateLoc, |
231 | SourceLocation LAngleLoc, |
232 | ArrayRef<NamedDecl *> Params, |
233 | SourceLocation RAngleLoc, |
234 | Expr *RequiresClause) |
235 | : FixedSizeStorageOwner( |
236 | (assert(N == Params.size()), |
237 | assert(HasRequiresClause == (RequiresClause != nullptr)), |
238 | new (static_cast<void *>(&storage)) TemplateParameterList(C, |
239 | TemplateLoc, LAngleLoc, Params, RAngleLoc, RequiresClause))) {} |
240 | }; |
241 | |
242 | /// A template argument list. |
243 | class TemplateArgumentList final |
244 | : private llvm::TrailingObjects<TemplateArgumentList, TemplateArgument> { |
245 | /// The number of template arguments in this template |
246 | /// argument list. |
247 | unsigned NumArguments; |
248 | |
249 | // Constructs an instance with an internal Argument list, containing |
250 | // a copy of the Args array. (Called by CreateCopy) |
251 | TemplateArgumentList(ArrayRef<TemplateArgument> Args); |
252 | |
253 | public: |
254 | friend TrailingObjects; |
255 | |
256 | TemplateArgumentList(const TemplateArgumentList &) = delete; |
257 | TemplateArgumentList &operator=(const TemplateArgumentList &) = delete; |
258 | |
259 | /// Create a new template argument list that copies the given set of |
260 | /// template arguments. |
261 | static TemplateArgumentList *CreateCopy(ASTContext &Context, |
262 | ArrayRef<TemplateArgument> Args); |
263 | |
264 | /// Retrieve the template argument at a given index. |
265 | const TemplateArgument &get(unsigned Idx) const { |
266 | assert(Idx < NumArguments && "Invalid template argument index" ); |
267 | return data()[Idx]; |
268 | } |
269 | |
270 | /// Retrieve the template argument at a given index. |
271 | const TemplateArgument &operator[](unsigned Idx) const { return get(Idx); } |
272 | |
273 | /// Produce this as an array ref. |
274 | ArrayRef<TemplateArgument> asArray() const { |
275 | return llvm::ArrayRef(data(), size()); |
276 | } |
277 | |
278 | /// Retrieve the number of template arguments in this |
279 | /// template argument list. |
280 | unsigned size() const { return NumArguments; } |
281 | |
282 | /// Retrieve a pointer to the template argument list. |
283 | const TemplateArgument *data() const { |
284 | return getTrailingObjects<TemplateArgument>(); |
285 | } |
286 | }; |
287 | |
288 | void *allocateDefaultArgStorageChain(const ASTContext &C); |
289 | |
290 | /// Storage for a default argument. This is conceptually either empty, or an |
291 | /// argument value, or a pointer to a previous declaration that had a default |
292 | /// argument. |
293 | /// |
294 | /// However, this is complicated by modules: while we require all the default |
295 | /// arguments for a template to be equivalent, there may be more than one, and |
296 | /// we need to track all the originating parameters to determine if the default |
297 | /// argument is visible. |
298 | template<typename ParmDecl, typename ArgType> |
299 | class DefaultArgStorage { |
300 | /// Storage for both the value *and* another parameter from which we inherit |
301 | /// the default argument. This is used when multiple default arguments for a |
302 | /// parameter are merged together from different modules. |
303 | struct Chain { |
304 | ParmDecl *PrevDeclWithDefaultArg; |
305 | ArgType Value; |
306 | }; |
307 | static_assert(sizeof(Chain) == sizeof(void *) * 2, |
308 | "non-pointer argument type?" ); |
309 | |
310 | llvm::PointerUnion<ArgType, ParmDecl*, Chain*> ValueOrInherited; |
311 | |
312 | static ParmDecl *getParmOwningDefaultArg(ParmDecl *Parm) { |
313 | const DefaultArgStorage &Storage = Parm->getDefaultArgStorage(); |
314 | if (auto *Prev = Storage.ValueOrInherited.template dyn_cast<ParmDecl *>()) |
315 | Parm = Prev; |
316 | assert(!Parm->getDefaultArgStorage() |
317 | .ValueOrInherited.template is<ParmDecl *>() && |
318 | "should only be one level of indirection" ); |
319 | return Parm; |
320 | } |
321 | |
322 | public: |
323 | DefaultArgStorage() : ValueOrInherited(ArgType()) {} |
324 | |
325 | /// Determine whether there is a default argument for this parameter. |
326 | bool isSet() const { return !ValueOrInherited.isNull(); } |
327 | |
328 | /// Determine whether the default argument for this parameter was inherited |
329 | /// from a previous declaration of the same entity. |
330 | bool isInherited() const { return ValueOrInherited.template is<ParmDecl*>(); } |
331 | |
332 | /// Get the default argument's value. This does not consider whether the |
333 | /// default argument is visible. |
334 | ArgType get() const { |
335 | const DefaultArgStorage *Storage = this; |
336 | if (const auto *Prev = ValueOrInherited.template dyn_cast<ParmDecl *>()) |
337 | Storage = &Prev->getDefaultArgStorage(); |
338 | if (const auto *C = Storage->ValueOrInherited.template dyn_cast<Chain *>()) |
339 | return C->Value; |
340 | return Storage->ValueOrInherited.template get<ArgType>(); |
341 | } |
342 | |
343 | /// Get the parameter from which we inherit the default argument, if any. |
344 | /// This is the parameter on which the default argument was actually written. |
345 | const ParmDecl *getInheritedFrom() const { |
346 | if (const auto *D = ValueOrInherited.template dyn_cast<ParmDecl *>()) |
347 | return D; |
348 | if (const auto *C = ValueOrInherited.template dyn_cast<Chain *>()) |
349 | return C->PrevDeclWithDefaultArg; |
350 | return nullptr; |
351 | } |
352 | |
353 | /// Set the default argument. |
354 | void set(ArgType Arg) { |
355 | assert(!isSet() && "default argument already set" ); |
356 | ValueOrInherited = Arg; |
357 | } |
358 | |
359 | /// Set that the default argument was inherited from another parameter. |
360 | void setInherited(const ASTContext &C, ParmDecl *InheritedFrom) { |
361 | InheritedFrom = getParmOwningDefaultArg(Parm: InheritedFrom); |
362 | if (!isSet()) |
363 | ValueOrInherited = InheritedFrom; |
364 | else if ([[maybe_unused]] auto *D = |
365 | ValueOrInherited.template dyn_cast<ParmDecl *>()) { |
366 | assert(C.isSameDefaultTemplateArgument(D, InheritedFrom)); |
367 | ValueOrInherited = |
368 | new (allocateDefaultArgStorageChain(C)) Chain{InheritedFrom, get()}; |
369 | } else if (auto *Inherited = |
370 | ValueOrInherited.template dyn_cast<Chain *>()) { |
371 | assert(C.isSameDefaultTemplateArgument(Inherited->PrevDeclWithDefaultArg, |
372 | InheritedFrom)); |
373 | Inherited->PrevDeclWithDefaultArg = InheritedFrom; |
374 | } else |
375 | ValueOrInherited = new (allocateDefaultArgStorageChain(C)) |
376 | Chain{InheritedFrom, ValueOrInherited.template get<ArgType>()}; |
377 | } |
378 | |
379 | /// Remove the default argument, even if it was inherited. |
380 | void clear() { |
381 | ValueOrInherited = ArgType(); |
382 | } |
383 | }; |
384 | |
385 | //===----------------------------------------------------------------------===// |
386 | // Kinds of Templates |
387 | //===----------------------------------------------------------------------===// |
388 | |
389 | /// \brief The base class of all kinds of template declarations (e.g., |
390 | /// class, function, etc.). |
391 | /// |
392 | /// The TemplateDecl class stores the list of template parameters and a |
393 | /// reference to the templated scoped declaration: the underlying AST node. |
394 | class TemplateDecl : public NamedDecl { |
395 | void anchor() override; |
396 | |
397 | protected: |
398 | // Construct a template decl with name, parameters, and templated element. |
399 | TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName Name, |
400 | TemplateParameterList *Params, NamedDecl *Decl); |
401 | |
402 | // Construct a template decl with the given name and parameters. |
403 | // Used when there is no templated element (e.g., for tt-params). |
404 | TemplateDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName Name, |
405 | TemplateParameterList *Params) |
406 | : TemplateDecl(DK, DC, L, Name, Params, nullptr) {} |
407 | |
408 | public: |
409 | friend class ASTDeclReader; |
410 | friend class ASTDeclWriter; |
411 | |
412 | /// Get the list of template parameters |
413 | TemplateParameterList *getTemplateParameters() const { |
414 | return TemplateParams; |
415 | } |
416 | |
417 | /// \brief Get the total constraint-expression associated with this template, |
418 | /// including constraint-expressions derived from the requires-clause, |
419 | /// trailing requires-clause (for functions and methods) and constrained |
420 | /// template parameters. |
421 | void getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const; |
422 | |
423 | bool hasAssociatedConstraints() const; |
424 | |
425 | /// Get the underlying, templated declaration. |
426 | NamedDecl *getTemplatedDecl() const { return TemplatedDecl; } |
427 | |
428 | // Should a specialization behave like an alias for another type. |
429 | bool isTypeAlias() const; |
430 | |
431 | // Implement isa/cast/dyncast/etc. |
432 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
433 | |
434 | static bool classofKind(Kind K) { |
435 | return K >= firstTemplate && K <= lastTemplate; |
436 | } |
437 | |
438 | SourceRange getSourceRange() const override LLVM_READONLY { |
439 | return SourceRange(getTemplateParameters()->getTemplateLoc(), |
440 | TemplatedDecl->getSourceRange().getEnd()); |
441 | } |
442 | |
443 | protected: |
444 | NamedDecl *TemplatedDecl; |
445 | TemplateParameterList *TemplateParams; |
446 | |
447 | public: |
448 | void setTemplateParameters(TemplateParameterList *TParams) { |
449 | TemplateParams = TParams; |
450 | } |
451 | |
452 | /// Initialize the underlying templated declaration. |
453 | void init(NamedDecl *NewTemplatedDecl) { |
454 | if (TemplatedDecl) |
455 | assert(TemplatedDecl == NewTemplatedDecl && "Inconsistent TemplatedDecl" ); |
456 | else |
457 | TemplatedDecl = NewTemplatedDecl; |
458 | } |
459 | }; |
460 | |
461 | /// Provides information about a function template specialization, |
462 | /// which is a FunctionDecl that has been explicitly specialization or |
463 | /// instantiated from a function template. |
464 | class FunctionTemplateSpecializationInfo final |
465 | : public llvm::FoldingSetNode, |
466 | private llvm::TrailingObjects<FunctionTemplateSpecializationInfo, |
467 | MemberSpecializationInfo *> { |
468 | /// The function template specialization that this structure describes and a |
469 | /// flag indicating if the function is a member specialization. |
470 | llvm::PointerIntPair<FunctionDecl *, 1, bool> Function; |
471 | |
472 | /// The function template from which this function template |
473 | /// specialization was generated. |
474 | /// |
475 | /// The two bits contain the top 4 values of TemplateSpecializationKind. |
476 | llvm::PointerIntPair<FunctionTemplateDecl *, 2> Template; |
477 | |
478 | public: |
479 | /// The template arguments used to produce the function template |
480 | /// specialization from the function template. |
481 | const TemplateArgumentList *TemplateArguments; |
482 | |
483 | /// The template arguments as written in the sources, if provided. |
484 | /// FIXME: Normally null; tail-allocate this. |
485 | const ASTTemplateArgumentListInfo *TemplateArgumentsAsWritten; |
486 | |
487 | /// The point at which this function template specialization was |
488 | /// first instantiated. |
489 | SourceLocation PointOfInstantiation; |
490 | |
491 | private: |
492 | FunctionTemplateSpecializationInfo( |
493 | FunctionDecl *FD, FunctionTemplateDecl *Template, |
494 | TemplateSpecializationKind TSK, const TemplateArgumentList *TemplateArgs, |
495 | const ASTTemplateArgumentListInfo *TemplateArgsAsWritten, |
496 | SourceLocation POI, MemberSpecializationInfo *MSInfo) |
497 | : Function(FD, MSInfo ? true : false), Template(Template, TSK - 1), |
498 | TemplateArguments(TemplateArgs), |
499 | TemplateArgumentsAsWritten(TemplateArgsAsWritten), |
500 | PointOfInstantiation(POI) { |
501 | if (MSInfo) |
502 | getTrailingObjects<MemberSpecializationInfo *>()[0] = MSInfo; |
503 | } |
504 | |
505 | size_t numTrailingObjects(OverloadToken<MemberSpecializationInfo*>) const { |
506 | return Function.getInt(); |
507 | } |
508 | |
509 | public: |
510 | friend TrailingObjects; |
511 | |
512 | static FunctionTemplateSpecializationInfo * |
513 | Create(ASTContext &C, FunctionDecl *FD, FunctionTemplateDecl *Template, |
514 | TemplateSpecializationKind TSK, |
515 | const TemplateArgumentList *TemplateArgs, |
516 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
517 | SourceLocation POI, MemberSpecializationInfo *MSInfo); |
518 | |
519 | /// Retrieve the declaration of the function template specialization. |
520 | FunctionDecl *getFunction() const { return Function.getPointer(); } |
521 | |
522 | /// Retrieve the template from which this function was specialized. |
523 | FunctionTemplateDecl *getTemplate() const { return Template.getPointer(); } |
524 | |
525 | /// Determine what kind of template specialization this is. |
526 | TemplateSpecializationKind getTemplateSpecializationKind() const { |
527 | return (TemplateSpecializationKind)(Template.getInt() + 1); |
528 | } |
529 | |
530 | bool isExplicitSpecialization() const { |
531 | return getTemplateSpecializationKind() == TSK_ExplicitSpecialization; |
532 | } |
533 | |
534 | /// True if this declaration is an explicit specialization, |
535 | /// explicit instantiation declaration, or explicit instantiation |
536 | /// definition. |
537 | bool isExplicitInstantiationOrSpecialization() const { |
538 | return isTemplateExplicitInstantiationOrSpecialization( |
539 | Kind: getTemplateSpecializationKind()); |
540 | } |
541 | |
542 | /// Set the template specialization kind. |
543 | void setTemplateSpecializationKind(TemplateSpecializationKind TSK) { |
544 | assert(TSK != TSK_Undeclared && |
545 | "Cannot encode TSK_Undeclared for a function template specialization" ); |
546 | Template.setInt(TSK - 1); |
547 | } |
548 | |
549 | /// Retrieve the first point of instantiation of this function |
550 | /// template specialization. |
551 | /// |
552 | /// The point of instantiation may be an invalid source location if this |
553 | /// function has yet to be instantiated. |
554 | SourceLocation getPointOfInstantiation() const { |
555 | return PointOfInstantiation; |
556 | } |
557 | |
558 | /// Set the (first) point of instantiation of this function template |
559 | /// specialization. |
560 | void setPointOfInstantiation(SourceLocation POI) { |
561 | PointOfInstantiation = POI; |
562 | } |
563 | |
564 | /// Get the specialization info if this function template specialization is |
565 | /// also a member specialization: |
566 | /// |
567 | /// \code |
568 | /// template<typename> struct A { |
569 | /// template<typename> void f(); |
570 | /// template<> void f<int>(); |
571 | /// }; |
572 | /// \endcode |
573 | /// |
574 | /// Here, A<int>::f<int> is a function template specialization that is |
575 | /// an explicit specialization of A<int>::f, but it's also a member |
576 | /// specialization (an implicit instantiation in this case) of A::f<int>. |
577 | /// Further: |
578 | /// |
579 | /// \code |
580 | /// template<> template<> void A<int>::f<int>() {} |
581 | /// \endcode |
582 | /// |
583 | /// ... declares a function template specialization that is an explicit |
584 | /// specialization of A<int>::f, and is also an explicit member |
585 | /// specialization of A::f<int>. |
586 | /// |
587 | /// Note that the TemplateSpecializationKind of the MemberSpecializationInfo |
588 | /// need not be the same as that returned by getTemplateSpecializationKind(), |
589 | /// and represents the relationship between the function and the class-scope |
590 | /// explicit specialization in the original templated class -- whereas our |
591 | /// TemplateSpecializationKind represents the relationship between the |
592 | /// function and the function template, and should always be |
593 | /// TSK_ExplicitSpecialization whenever we have MemberSpecializationInfo. |
594 | MemberSpecializationInfo *getMemberSpecializationInfo() const { |
595 | return numTrailingObjects(OverloadToken<MemberSpecializationInfo *>()) |
596 | ? getTrailingObjects<MemberSpecializationInfo *>()[0] |
597 | : nullptr; |
598 | } |
599 | |
600 | void Profile(llvm::FoldingSetNodeID &ID) { |
601 | Profile(ID, TemplateArguments->asArray(), getFunction()->getASTContext()); |
602 | } |
603 | |
604 | static void |
605 | Profile(llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs, |
606 | const ASTContext &Context) { |
607 | ID.AddInteger(I: TemplateArgs.size()); |
608 | for (const TemplateArgument &TemplateArg : TemplateArgs) |
609 | TemplateArg.Profile(ID, Context); |
610 | } |
611 | }; |
612 | |
613 | /// Provides information a specialization of a member of a class |
614 | /// template, which may be a member function, static data member, |
615 | /// member class or member enumeration. |
616 | class MemberSpecializationInfo { |
617 | // The member declaration from which this member was instantiated, and the |
618 | // manner in which the instantiation occurred (in the lower two bits). |
619 | llvm::PointerIntPair<NamedDecl *, 2> MemberAndTSK; |
620 | |
621 | // The point at which this member was first instantiated. |
622 | SourceLocation PointOfInstantiation; |
623 | |
624 | public: |
625 | explicit |
626 | MemberSpecializationInfo(NamedDecl *IF, TemplateSpecializationKind TSK, |
627 | SourceLocation POI = SourceLocation()) |
628 | : MemberAndTSK(IF, TSK - 1), PointOfInstantiation(POI) { |
629 | assert(TSK != TSK_Undeclared && |
630 | "Cannot encode undeclared template specializations for members" ); |
631 | } |
632 | |
633 | /// Retrieve the member declaration from which this member was |
634 | /// instantiated. |
635 | NamedDecl *getInstantiatedFrom() const { return MemberAndTSK.getPointer(); } |
636 | |
637 | /// Determine what kind of template specialization this is. |
638 | TemplateSpecializationKind getTemplateSpecializationKind() const { |
639 | return (TemplateSpecializationKind)(MemberAndTSK.getInt() + 1); |
640 | } |
641 | |
642 | bool isExplicitSpecialization() const { |
643 | return getTemplateSpecializationKind() == TSK_ExplicitSpecialization; |
644 | } |
645 | |
646 | /// Set the template specialization kind. |
647 | void setTemplateSpecializationKind(TemplateSpecializationKind TSK) { |
648 | assert(TSK != TSK_Undeclared && |
649 | "Cannot encode undeclared template specializations for members" ); |
650 | MemberAndTSK.setInt(TSK - 1); |
651 | } |
652 | |
653 | /// Retrieve the first point of instantiation of this member. |
654 | /// If the point of instantiation is an invalid location, then this member |
655 | /// has not yet been instantiated. |
656 | SourceLocation getPointOfInstantiation() const { |
657 | return PointOfInstantiation; |
658 | } |
659 | |
660 | /// Set the first point of instantiation. |
661 | void setPointOfInstantiation(SourceLocation POI) { |
662 | PointOfInstantiation = POI; |
663 | } |
664 | }; |
665 | |
666 | /// Provides information about a dependent function-template |
667 | /// specialization declaration. |
668 | /// |
669 | /// This is used for function templates explicit specializations declared |
670 | /// within class templates: |
671 | /// |
672 | /// \code |
673 | /// template<typename> struct A { |
674 | /// template<typename> void f(); |
675 | /// template<> void f<int>(); // DependentFunctionTemplateSpecializationInfo |
676 | /// }; |
677 | /// \endcode |
678 | /// |
679 | /// As well as dependent friend declarations naming function template |
680 | /// specializations declared within class templates: |
681 | /// |
682 | /// \code |
683 | /// template \<class T> void foo(T); |
684 | /// template \<class T> class A { |
685 | /// friend void foo<>(T); // DependentFunctionTemplateSpecializationInfo |
686 | /// }; |
687 | /// \endcode |
688 | class DependentFunctionTemplateSpecializationInfo final |
689 | : private llvm::TrailingObjects<DependentFunctionTemplateSpecializationInfo, |
690 | FunctionTemplateDecl *> { |
691 | friend TrailingObjects; |
692 | |
693 | /// The number of candidates for the primary template. |
694 | unsigned NumCandidates; |
695 | |
696 | DependentFunctionTemplateSpecializationInfo( |
697 | const UnresolvedSetImpl &Candidates, |
698 | const ASTTemplateArgumentListInfo *TemplateArgsWritten); |
699 | |
700 | public: |
701 | /// The template arguments as written in the sources, if provided. |
702 | const ASTTemplateArgumentListInfo *TemplateArgumentsAsWritten; |
703 | |
704 | static DependentFunctionTemplateSpecializationInfo * |
705 | Create(ASTContext &Context, const UnresolvedSetImpl &Candidates, |
706 | const TemplateArgumentListInfo *TemplateArgs); |
707 | |
708 | /// Returns the candidates for the primary function template. |
709 | ArrayRef<FunctionTemplateDecl *> getCandidates() const { |
710 | return {getTrailingObjects<FunctionTemplateDecl *>(), NumCandidates}; |
711 | } |
712 | }; |
713 | |
714 | /// Declaration of a redeclarable template. |
715 | class RedeclarableTemplateDecl : public TemplateDecl, |
716 | public Redeclarable<RedeclarableTemplateDecl> |
717 | { |
718 | using redeclarable_base = Redeclarable<RedeclarableTemplateDecl>; |
719 | |
720 | RedeclarableTemplateDecl *getNextRedeclarationImpl() override { |
721 | return getNextRedeclaration(); |
722 | } |
723 | |
724 | RedeclarableTemplateDecl *getPreviousDeclImpl() override { |
725 | return getPreviousDecl(); |
726 | } |
727 | |
728 | RedeclarableTemplateDecl *getMostRecentDeclImpl() override { |
729 | return getMostRecentDecl(); |
730 | } |
731 | |
732 | void anchor() override; |
733 | protected: |
734 | template <typename EntryType> struct SpecEntryTraits { |
735 | using DeclType = EntryType; |
736 | |
737 | static DeclType *getDecl(EntryType *D) { |
738 | return D; |
739 | } |
740 | |
741 | static ArrayRef<TemplateArgument> getTemplateArgs(EntryType *D) { |
742 | return D->getTemplateArgs().asArray(); |
743 | } |
744 | }; |
745 | |
746 | template <typename EntryType, typename SETraits = SpecEntryTraits<EntryType>, |
747 | typename DeclType = typename SETraits::DeclType> |
748 | struct SpecIterator |
749 | : llvm::iterator_adaptor_base< |
750 | SpecIterator<EntryType, SETraits, DeclType>, |
751 | typename llvm::FoldingSetVector<EntryType>::iterator, |
752 | typename std::iterator_traits<typename llvm::FoldingSetVector< |
753 | EntryType>::iterator>::iterator_category, |
754 | DeclType *, ptrdiff_t, DeclType *, DeclType *> { |
755 | SpecIterator() = default; |
756 | explicit SpecIterator( |
757 | typename llvm::FoldingSetVector<EntryType>::iterator SetIter) |
758 | : SpecIterator::iterator_adaptor_base(std::move(SetIter)) {} |
759 | |
760 | DeclType *operator*() const { |
761 | return SETraits::getDecl(&*this->I)->getMostRecentDecl(); |
762 | } |
763 | |
764 | DeclType *operator->() const { return **this; } |
765 | }; |
766 | |
767 | template <typename EntryType> |
768 | static SpecIterator<EntryType> |
769 | makeSpecIterator(llvm::FoldingSetVector<EntryType> &Specs, bool isEnd) { |
770 | return SpecIterator<EntryType>(isEnd ? Specs.end() : Specs.begin()); |
771 | } |
772 | |
773 | void loadLazySpecializationsImpl() const; |
774 | |
775 | template <class EntryType, typename ...ProfileArguments> |
776 | typename SpecEntryTraits<EntryType>::DeclType* |
777 | findSpecializationImpl(llvm::FoldingSetVector<EntryType> &Specs, |
778 | void *&InsertPos, ProfileArguments &&...ProfileArgs); |
779 | |
780 | template <class Derived, class EntryType> |
781 | void addSpecializationImpl(llvm::FoldingSetVector<EntryType> &Specs, |
782 | EntryType *Entry, void *InsertPos); |
783 | |
784 | struct CommonBase { |
785 | CommonBase() : InstantiatedFromMember(nullptr, false) {} |
786 | |
787 | /// The template from which this was most |
788 | /// directly instantiated (or null). |
789 | /// |
790 | /// The boolean value indicates whether this template |
791 | /// was explicitly specialized. |
792 | llvm::PointerIntPair<RedeclarableTemplateDecl*, 1, bool> |
793 | InstantiatedFromMember; |
794 | |
795 | /// If non-null, points to an array of specializations (including |
796 | /// partial specializations) known only by their external declaration IDs. |
797 | /// |
798 | /// The first value in the array is the number of specializations/partial |
799 | /// specializations that follow. |
800 | uint32_t *LazySpecializations = nullptr; |
801 | |
802 | /// The set of "injected" template arguments used within this |
803 | /// template. |
804 | /// |
805 | /// This pointer refers to the template arguments (there are as |
806 | /// many template arguments as template parameters) for the |
807 | /// template, and is allocated lazily, since most templates do not |
808 | /// require the use of this information. |
809 | TemplateArgument *InjectedArgs = nullptr; |
810 | }; |
811 | |
812 | /// Pointer to the common data shared by all declarations of this |
813 | /// template. |
814 | mutable CommonBase *Common = nullptr; |
815 | |
816 | /// Retrieves the "common" pointer shared by all (re-)declarations of |
817 | /// the same template. Calling this routine may implicitly allocate memory |
818 | /// for the common pointer. |
819 | CommonBase *getCommonPtr() const; |
820 | |
821 | virtual CommonBase *newCommon(ASTContext &C) const = 0; |
822 | |
823 | // Construct a template decl with name, parameters, and templated element. |
824 | RedeclarableTemplateDecl(Kind DK, ASTContext &C, DeclContext *DC, |
825 | SourceLocation L, DeclarationName Name, |
826 | TemplateParameterList *Params, NamedDecl *Decl) |
827 | : TemplateDecl(DK, DC, L, Name, Params, Decl), redeclarable_base(C) {} |
828 | |
829 | public: |
830 | friend class ASTDeclReader; |
831 | friend class ASTDeclWriter; |
832 | friend class ASTReader; |
833 | template <class decl_type> friend class RedeclarableTemplate; |
834 | |
835 | /// Retrieves the canonical declaration of this template. |
836 | RedeclarableTemplateDecl *getCanonicalDecl() override { |
837 | return getFirstDecl(); |
838 | } |
839 | const RedeclarableTemplateDecl *getCanonicalDecl() const { |
840 | return getFirstDecl(); |
841 | } |
842 | |
843 | /// Determines whether this template was a specialization of a |
844 | /// member template. |
845 | /// |
846 | /// In the following example, the function template \c X<int>::f and the |
847 | /// member template \c X<int>::Inner are member specializations. |
848 | /// |
849 | /// \code |
850 | /// template<typename T> |
851 | /// struct X { |
852 | /// template<typename U> void f(T, U); |
853 | /// template<typename U> struct Inner; |
854 | /// }; |
855 | /// |
856 | /// template<> template<typename T> |
857 | /// void X<int>::f(int, T); |
858 | /// template<> template<typename T> |
859 | /// struct X<int>::Inner { /* ... */ }; |
860 | /// \endcode |
861 | bool isMemberSpecialization() const { |
862 | return getCommonPtr()->InstantiatedFromMember.getInt(); |
863 | } |
864 | |
865 | /// Note that this member template is a specialization. |
866 | void setMemberSpecialization() { |
867 | assert(getCommonPtr()->InstantiatedFromMember.getPointer() && |
868 | "Only member templates can be member template specializations" ); |
869 | getCommonPtr()->InstantiatedFromMember.setInt(true); |
870 | } |
871 | |
872 | /// Retrieve the member template from which this template was |
873 | /// instantiated, or nullptr if this template was not instantiated from a |
874 | /// member template. |
875 | /// |
876 | /// A template is instantiated from a member template when the member |
877 | /// template itself is part of a class template (or member thereof). For |
878 | /// example, given |
879 | /// |
880 | /// \code |
881 | /// template<typename T> |
882 | /// struct X { |
883 | /// template<typename U> void f(T, U); |
884 | /// }; |
885 | /// |
886 | /// void test(X<int> x) { |
887 | /// x.f(1, 'a'); |
888 | /// }; |
889 | /// \endcode |
890 | /// |
891 | /// \c X<int>::f is a FunctionTemplateDecl that describes the function |
892 | /// template |
893 | /// |
894 | /// \code |
895 | /// template<typename U> void X<int>::f(int, U); |
896 | /// \endcode |
897 | /// |
898 | /// which was itself created during the instantiation of \c X<int>. Calling |
899 | /// getInstantiatedFromMemberTemplate() on this FunctionTemplateDecl will |
900 | /// retrieve the FunctionTemplateDecl for the original template \c f within |
901 | /// the class template \c X<T>, i.e., |
902 | /// |
903 | /// \code |
904 | /// template<typename T> |
905 | /// template<typename U> |
906 | /// void X<T>::f(T, U); |
907 | /// \endcode |
908 | RedeclarableTemplateDecl *getInstantiatedFromMemberTemplate() const { |
909 | return getCommonPtr()->InstantiatedFromMember.getPointer(); |
910 | } |
911 | |
912 | void setInstantiatedFromMemberTemplate(RedeclarableTemplateDecl *TD) { |
913 | assert(!getCommonPtr()->InstantiatedFromMember.getPointer()); |
914 | getCommonPtr()->InstantiatedFromMember.setPointer(TD); |
915 | } |
916 | |
917 | /// Retrieve the "injected" template arguments that correspond to the |
918 | /// template parameters of this template. |
919 | /// |
920 | /// Although the C++ standard has no notion of the "injected" template |
921 | /// arguments for a template, the notion is convenient when |
922 | /// we need to perform substitutions inside the definition of a template. |
923 | ArrayRef<TemplateArgument> getInjectedTemplateArgs(); |
924 | |
925 | using redecl_range = redeclarable_base::redecl_range; |
926 | using redecl_iterator = redeclarable_base::redecl_iterator; |
927 | |
928 | using redeclarable_base::redecls_begin; |
929 | using redeclarable_base::redecls_end; |
930 | using redeclarable_base::redecls; |
931 | using redeclarable_base::getPreviousDecl; |
932 | using redeclarable_base::getMostRecentDecl; |
933 | using redeclarable_base::isFirstDecl; |
934 | |
935 | // Implement isa/cast/dyncast/etc. |
936 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
937 | |
938 | static bool classofKind(Kind K) { |
939 | return K >= firstRedeclarableTemplate && K <= lastRedeclarableTemplate; |
940 | } |
941 | }; |
942 | |
943 | template <> struct RedeclarableTemplateDecl:: |
944 | SpecEntryTraits<FunctionTemplateSpecializationInfo> { |
945 | using DeclType = FunctionDecl; |
946 | |
947 | static DeclType *getDecl(FunctionTemplateSpecializationInfo *I) { |
948 | return I->getFunction(); |
949 | } |
950 | |
951 | static ArrayRef<TemplateArgument> |
952 | getTemplateArgs(FunctionTemplateSpecializationInfo *I) { |
953 | return I->TemplateArguments->asArray(); |
954 | } |
955 | }; |
956 | |
957 | /// Declaration of a template function. |
958 | class FunctionTemplateDecl : public RedeclarableTemplateDecl { |
959 | protected: |
960 | friend class FunctionDecl; |
961 | |
962 | /// Data that is common to all of the declarations of a given |
963 | /// function template. |
964 | struct Common : CommonBase { |
965 | /// The function template specializations for this function |
966 | /// template, including explicit specializations and instantiations. |
967 | llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> Specializations; |
968 | |
969 | Common() = default; |
970 | }; |
971 | |
972 | FunctionTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L, |
973 | DeclarationName Name, TemplateParameterList *Params, |
974 | NamedDecl *Decl) |
975 | : RedeclarableTemplateDecl(FunctionTemplate, C, DC, L, Name, Params, |
976 | Decl) {} |
977 | |
978 | CommonBase *newCommon(ASTContext &C) const override; |
979 | |
980 | Common *getCommonPtr() const { |
981 | return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr()); |
982 | } |
983 | |
984 | /// Retrieve the set of function template specializations of this |
985 | /// function template. |
986 | llvm::FoldingSetVector<FunctionTemplateSpecializationInfo> & |
987 | getSpecializations() const; |
988 | |
989 | /// Add a specialization of this function template. |
990 | /// |
991 | /// \param InsertPos Insert position in the FoldingSetVector, must have been |
992 | /// retrieved by an earlier call to findSpecialization(). |
993 | void addSpecialization(FunctionTemplateSpecializationInfo* Info, |
994 | void *InsertPos); |
995 | |
996 | public: |
997 | friend class ASTDeclReader; |
998 | friend class ASTDeclWriter; |
999 | |
1000 | /// Load any lazily-loaded specializations from the external source. |
1001 | void LoadLazySpecializations() const; |
1002 | |
1003 | /// Get the underlying function declaration of the template. |
1004 | FunctionDecl *getTemplatedDecl() const { |
1005 | return static_cast<FunctionDecl *>(TemplatedDecl); |
1006 | } |
1007 | |
1008 | /// Returns whether this template declaration defines the primary |
1009 | /// pattern. |
1010 | bool isThisDeclarationADefinition() const { |
1011 | return getTemplatedDecl()->isThisDeclarationADefinition(); |
1012 | } |
1013 | |
1014 | /// Return the specialization with the provided arguments if it exists, |
1015 | /// otherwise return the insertion point. |
1016 | FunctionDecl *findSpecialization(ArrayRef<TemplateArgument> Args, |
1017 | void *&InsertPos); |
1018 | |
1019 | FunctionTemplateDecl *getCanonicalDecl() override { |
1020 | return cast<FunctionTemplateDecl>( |
1021 | RedeclarableTemplateDecl::getCanonicalDecl()); |
1022 | } |
1023 | const FunctionTemplateDecl *getCanonicalDecl() const { |
1024 | return cast<FunctionTemplateDecl>( |
1025 | RedeclarableTemplateDecl::getCanonicalDecl()); |
1026 | } |
1027 | |
1028 | /// Retrieve the previous declaration of this function template, or |
1029 | /// nullptr if no such declaration exists. |
1030 | FunctionTemplateDecl *getPreviousDecl() { |
1031 | return cast_or_null<FunctionTemplateDecl>( |
1032 | static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl()); |
1033 | } |
1034 | const FunctionTemplateDecl *getPreviousDecl() const { |
1035 | return cast_or_null<FunctionTemplateDecl>( |
1036 | static_cast<const RedeclarableTemplateDecl *>(this)->getPreviousDecl()); |
1037 | } |
1038 | |
1039 | FunctionTemplateDecl *getMostRecentDecl() { |
1040 | return cast<FunctionTemplateDecl>( |
1041 | static_cast<RedeclarableTemplateDecl *>(this) |
1042 | ->getMostRecentDecl()); |
1043 | } |
1044 | const FunctionTemplateDecl *getMostRecentDecl() const { |
1045 | return const_cast<FunctionTemplateDecl*>(this)->getMostRecentDecl(); |
1046 | } |
1047 | |
1048 | FunctionTemplateDecl *getInstantiatedFromMemberTemplate() const { |
1049 | return cast_or_null<FunctionTemplateDecl>( |
1050 | RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate()); |
1051 | } |
1052 | |
1053 | using spec_iterator = SpecIterator<FunctionTemplateSpecializationInfo>; |
1054 | using spec_range = llvm::iterator_range<spec_iterator>; |
1055 | |
1056 | spec_range specializations() const { |
1057 | return spec_range(spec_begin(), spec_end()); |
1058 | } |
1059 | |
1060 | spec_iterator spec_begin() const { |
1061 | return makeSpecIterator(Specs&: getSpecializations(), isEnd: false); |
1062 | } |
1063 | |
1064 | spec_iterator spec_end() const { |
1065 | return makeSpecIterator(Specs&: getSpecializations(), isEnd: true); |
1066 | } |
1067 | |
1068 | /// Return whether this function template is an abbreviated function template, |
1069 | /// e.g. `void foo(auto x)` or `template<typename T> void foo(auto x)` |
1070 | bool isAbbreviated() const { |
1071 | // Since the invented template parameters generated from 'auto' parameters |
1072 | // are either appended to the end of the explicit template parameter list or |
1073 | // form a new template parameter list, we can simply observe the last |
1074 | // parameter to determine if such a thing happened. |
1075 | const TemplateParameterList *TPL = getTemplateParameters(); |
1076 | return TPL->getParam(Idx: TPL->size() - 1)->isImplicit(); |
1077 | } |
1078 | |
1079 | /// Merge \p Prev with our RedeclarableTemplateDecl::Common. |
1080 | void mergePrevDecl(FunctionTemplateDecl *Prev); |
1081 | |
1082 | /// Create a function template node. |
1083 | static FunctionTemplateDecl *Create(ASTContext &C, DeclContext *DC, |
1084 | SourceLocation L, |
1085 | DeclarationName Name, |
1086 | TemplateParameterList *Params, |
1087 | NamedDecl *Decl); |
1088 | |
1089 | /// Create an empty function template node. |
1090 | static FunctionTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
1091 | |
1092 | // Implement isa/cast/dyncast support |
1093 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
1094 | static bool classofKind(Kind K) { return K == FunctionTemplate; } |
1095 | }; |
1096 | |
1097 | //===----------------------------------------------------------------------===// |
1098 | // Kinds of Template Parameters |
1099 | //===----------------------------------------------------------------------===// |
1100 | |
1101 | /// Defines the position of a template parameter within a template |
1102 | /// parameter list. |
1103 | /// |
1104 | /// Because template parameter can be listed |
1105 | /// sequentially for out-of-line template members, each template parameter is |
1106 | /// given a Depth - the nesting of template parameter scopes - and a Position - |
1107 | /// the occurrence within the parameter list. |
1108 | /// This class is inheritedly privately by different kinds of template |
1109 | /// parameters and is not part of the Decl hierarchy. Just a facility. |
1110 | class TemplateParmPosition { |
1111 | protected: |
1112 | enum { DepthWidth = 20, PositionWidth = 12 }; |
1113 | unsigned Depth : DepthWidth; |
1114 | unsigned Position : PositionWidth; |
1115 | |
1116 | static constexpr unsigned MaxDepth = (1U << DepthWidth) - 1; |
1117 | static constexpr unsigned MaxPosition = (1U << PositionWidth) - 1; |
1118 | |
1119 | TemplateParmPosition(unsigned D, unsigned P) : Depth(D), Position(P) { |
1120 | // The input may fill maximum values to show that it is invalid. |
1121 | // Add one here to convert it to zero. |
1122 | assert((D + 1) <= MaxDepth && |
1123 | "The depth of template parmeter position is more than 2^20!" ); |
1124 | assert((P + 1) <= MaxPosition && |
1125 | "The position of template parmeter position is more than 2^12!" ); |
1126 | } |
1127 | |
1128 | public: |
1129 | TemplateParmPosition() = delete; |
1130 | |
1131 | /// Get the nesting depth of the template parameter. |
1132 | unsigned getDepth() const { return Depth; } |
1133 | void setDepth(unsigned D) { |
1134 | assert((D + 1) <= MaxDepth && |
1135 | "The depth of template parmeter position is more than 2^20!" ); |
1136 | Depth = D; |
1137 | } |
1138 | |
1139 | /// Get the position of the template parameter within its parameter list. |
1140 | unsigned getPosition() const { return Position; } |
1141 | void setPosition(unsigned P) { |
1142 | assert((P + 1) <= MaxPosition && |
1143 | "The position of template parmeter position is more than 2^12!" ); |
1144 | Position = P; |
1145 | } |
1146 | |
1147 | /// Get the index of the template parameter within its parameter list. |
1148 | unsigned getIndex() const { return Position; } |
1149 | }; |
1150 | |
1151 | /// Declaration of a template type parameter. |
1152 | /// |
1153 | /// For example, "T" in |
1154 | /// \code |
1155 | /// template<typename T> class vector; |
1156 | /// \endcode |
1157 | class TemplateTypeParmDecl final : public TypeDecl, |
1158 | private llvm::TrailingObjects<TemplateTypeParmDecl, TypeConstraint> { |
1159 | /// Sema creates these on the stack during auto type deduction. |
1160 | friend class Sema; |
1161 | friend TrailingObjects; |
1162 | friend class ASTDeclReader; |
1163 | |
1164 | /// Whether this template type parameter was declaration with |
1165 | /// the 'typename' keyword. |
1166 | /// |
1167 | /// If false, it was declared with the 'class' keyword. |
1168 | bool Typename : 1; |
1169 | |
1170 | /// Whether this template type parameter has a type-constraint construct. |
1171 | bool HasTypeConstraint : 1; |
1172 | |
1173 | /// Whether the type constraint has been initialized. This can be false if the |
1174 | /// constraint was not initialized yet or if there was an error forming the |
1175 | /// type constraint. |
1176 | bool TypeConstraintInitialized : 1; |
1177 | |
1178 | /// Whether this type template parameter is an "expanded" |
1179 | /// parameter pack, meaning that its type is a pack expansion and we |
1180 | /// already know the set of types that expansion expands to. |
1181 | bool ExpandedParameterPack : 1; |
1182 | |
1183 | /// The number of type parameters in an expanded parameter pack. |
1184 | unsigned NumExpanded = 0; |
1185 | |
1186 | /// The default template argument, if any. |
1187 | using DefArgStorage = |
1188 | DefaultArgStorage<TemplateTypeParmDecl, TypeSourceInfo *>; |
1189 | DefArgStorage DefaultArgument; |
1190 | |
1191 | TemplateTypeParmDecl(DeclContext *DC, SourceLocation KeyLoc, |
1192 | SourceLocation IdLoc, IdentifierInfo *Id, bool Typename, |
1193 | bool HasTypeConstraint, |
1194 | std::optional<unsigned> NumExpanded) |
1195 | : TypeDecl(TemplateTypeParm, DC, IdLoc, Id, KeyLoc), Typename(Typename), |
1196 | HasTypeConstraint(HasTypeConstraint), TypeConstraintInitialized(false), |
1197 | ExpandedParameterPack(NumExpanded), |
1198 | NumExpanded(NumExpanded.value_or(u: 0)) {} |
1199 | |
1200 | public: |
1201 | static TemplateTypeParmDecl * |
1202 | Create(const ASTContext &C, DeclContext *DC, SourceLocation KeyLoc, |
1203 | SourceLocation NameLoc, unsigned D, unsigned P, IdentifierInfo *Id, |
1204 | bool Typename, bool ParameterPack, bool HasTypeConstraint = false, |
1205 | std::optional<unsigned> NumExpanded = std::nullopt); |
1206 | static TemplateTypeParmDecl *CreateDeserialized(const ASTContext &C, |
1207 | unsigned ID); |
1208 | static TemplateTypeParmDecl *CreateDeserialized(const ASTContext &C, |
1209 | unsigned ID, |
1210 | bool HasTypeConstraint); |
1211 | |
1212 | /// Whether this template type parameter was declared with |
1213 | /// the 'typename' keyword. |
1214 | /// |
1215 | /// If not, it was either declared with the 'class' keyword or with a |
1216 | /// type-constraint (see hasTypeConstraint()). |
1217 | bool wasDeclaredWithTypename() const { |
1218 | return Typename && !HasTypeConstraint; |
1219 | } |
1220 | |
1221 | const DefArgStorage &getDefaultArgStorage() const { return DefaultArgument; } |
1222 | |
1223 | /// Determine whether this template parameter has a default |
1224 | /// argument. |
1225 | bool hasDefaultArgument() const { return DefaultArgument.isSet(); } |
1226 | |
1227 | /// Retrieve the default argument, if any. |
1228 | QualType getDefaultArgument() const { |
1229 | return DefaultArgument.get()->getType(); |
1230 | } |
1231 | |
1232 | /// Retrieves the default argument's source information, if any. |
1233 | TypeSourceInfo *getDefaultArgumentInfo() const { |
1234 | return DefaultArgument.get(); |
1235 | } |
1236 | |
1237 | /// Retrieves the location of the default argument declaration. |
1238 | SourceLocation getDefaultArgumentLoc() const; |
1239 | |
1240 | /// Determines whether the default argument was inherited |
1241 | /// from a previous declaration of this template. |
1242 | bool defaultArgumentWasInherited() const { |
1243 | return DefaultArgument.isInherited(); |
1244 | } |
1245 | |
1246 | /// Set the default argument for this template parameter. |
1247 | void setDefaultArgument(TypeSourceInfo *DefArg) { |
1248 | DefaultArgument.set(DefArg); |
1249 | } |
1250 | |
1251 | /// Set that this default argument was inherited from another |
1252 | /// parameter. |
1253 | void setInheritedDefaultArgument(const ASTContext &C, |
1254 | TemplateTypeParmDecl *Prev) { |
1255 | DefaultArgument.setInherited(C, InheritedFrom: Prev); |
1256 | } |
1257 | |
1258 | /// Removes the default argument of this template parameter. |
1259 | void removeDefaultArgument() { |
1260 | DefaultArgument.clear(); |
1261 | } |
1262 | |
1263 | /// Set whether this template type parameter was declared with |
1264 | /// the 'typename' or 'class' keyword. |
1265 | void setDeclaredWithTypename(bool withTypename) { Typename = withTypename; } |
1266 | |
1267 | /// Retrieve the depth of the template parameter. |
1268 | unsigned getDepth() const; |
1269 | |
1270 | /// Retrieve the index of the template parameter. |
1271 | unsigned getIndex() const; |
1272 | |
1273 | /// Returns whether this is a parameter pack. |
1274 | bool isParameterPack() const; |
1275 | |
1276 | /// Whether this parameter pack is a pack expansion. |
1277 | /// |
1278 | /// A template type template parameter pack can be a pack expansion if its |
1279 | /// type-constraint contains an unexpanded parameter pack. |
1280 | bool isPackExpansion() const { |
1281 | if (!isParameterPack()) |
1282 | return false; |
1283 | if (const TypeConstraint *TC = getTypeConstraint()) |
1284 | if (TC->hasExplicitTemplateArgs()) |
1285 | for (const auto &ArgLoc : TC->getTemplateArgsAsWritten()->arguments()) |
1286 | if (ArgLoc.getArgument().containsUnexpandedParameterPack()) |
1287 | return true; |
1288 | return false; |
1289 | } |
1290 | |
1291 | /// Whether this parameter is a template type parameter pack that has a known |
1292 | /// list of different type-constraints at different positions. |
1293 | /// |
1294 | /// A parameter pack is an expanded parameter pack when the original |
1295 | /// parameter pack's type-constraint was itself a pack expansion, and that |
1296 | /// expansion has already been expanded. For example, given: |
1297 | /// |
1298 | /// \code |
1299 | /// template<typename ...Types> |
1300 | /// struct X { |
1301 | /// template<convertible_to<Types> ...Convertibles> |
1302 | /// struct Y { /* ... */ }; |
1303 | /// }; |
1304 | /// \endcode |
1305 | /// |
1306 | /// The parameter pack \c Convertibles has (convertible_to<Types> && ...) as |
1307 | /// its type-constraint. When \c Types is supplied with template arguments by |
1308 | /// instantiating \c X, the instantiation of \c Convertibles becomes an |
1309 | /// expanded parameter pack. For example, instantiating |
1310 | /// \c X<int, unsigned int> results in \c Convertibles being an expanded |
1311 | /// parameter pack of size 2 (use getNumExpansionTypes() to get this number). |
1312 | bool isExpandedParameterPack() const { return ExpandedParameterPack; } |
1313 | |
1314 | /// Retrieves the number of parameters in an expanded parameter pack. |
1315 | unsigned getNumExpansionParameters() const { |
1316 | assert(ExpandedParameterPack && "Not an expansion parameter pack" ); |
1317 | return NumExpanded; |
1318 | } |
1319 | |
1320 | /// Returns the type constraint associated with this template parameter (if |
1321 | /// any). |
1322 | const TypeConstraint *getTypeConstraint() const { |
1323 | return TypeConstraintInitialized ? getTrailingObjects<TypeConstraint>() : |
1324 | nullptr; |
1325 | } |
1326 | |
1327 | void setTypeConstraint(ConceptReference *CR, |
1328 | Expr *ImmediatelyDeclaredConstraint); |
1329 | |
1330 | /// Determine whether this template parameter has a type-constraint. |
1331 | bool hasTypeConstraint() const { |
1332 | return HasTypeConstraint; |
1333 | } |
1334 | |
1335 | /// \brief Get the associated-constraints of this template parameter. |
1336 | /// This will either be the immediately-introduced constraint or empty. |
1337 | /// |
1338 | /// Use this instead of getTypeConstraint for concepts APIs that |
1339 | /// accept an ArrayRef of constraint expressions. |
1340 | void getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const { |
1341 | if (HasTypeConstraint) |
1342 | AC.push_back(Elt: getTypeConstraint()->getImmediatelyDeclaredConstraint()); |
1343 | } |
1344 | |
1345 | SourceRange getSourceRange() const override LLVM_READONLY; |
1346 | |
1347 | // Implement isa/cast/dyncast/etc. |
1348 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
1349 | static bool classofKind(Kind K) { return K == TemplateTypeParm; } |
1350 | }; |
1351 | |
1352 | /// NonTypeTemplateParmDecl - Declares a non-type template parameter, |
1353 | /// e.g., "Size" in |
1354 | /// @code |
1355 | /// template<int Size> class array { }; |
1356 | /// @endcode |
1357 | class NonTypeTemplateParmDecl final |
1358 | : public DeclaratorDecl, |
1359 | protected TemplateParmPosition, |
1360 | private llvm::TrailingObjects<NonTypeTemplateParmDecl, |
1361 | std::pair<QualType, TypeSourceInfo *>, |
1362 | Expr *> { |
1363 | friend class ASTDeclReader; |
1364 | friend TrailingObjects; |
1365 | |
1366 | /// The default template argument, if any, and whether or not |
1367 | /// it was inherited. |
1368 | using DefArgStorage = DefaultArgStorage<NonTypeTemplateParmDecl, Expr *>; |
1369 | DefArgStorage DefaultArgument; |
1370 | |
1371 | // FIXME: Collapse this into TemplateParamPosition; or, just move depth/index |
1372 | // down here to save memory. |
1373 | |
1374 | /// Whether this non-type template parameter is a parameter pack. |
1375 | bool ParameterPack; |
1376 | |
1377 | /// Whether this non-type template parameter is an "expanded" |
1378 | /// parameter pack, meaning that its type is a pack expansion and we |
1379 | /// already know the set of types that expansion expands to. |
1380 | bool ExpandedParameterPack = false; |
1381 | |
1382 | /// The number of types in an expanded parameter pack. |
1383 | unsigned NumExpandedTypes = 0; |
1384 | |
1385 | size_t numTrailingObjects( |
1386 | OverloadToken<std::pair<QualType, TypeSourceInfo *>>) const { |
1387 | return NumExpandedTypes; |
1388 | } |
1389 | |
1390 | NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation StartLoc, |
1391 | SourceLocation IdLoc, unsigned D, unsigned P, |
1392 | IdentifierInfo *Id, QualType T, |
1393 | bool ParameterPack, TypeSourceInfo *TInfo) |
1394 | : DeclaratorDecl(NonTypeTemplateParm, DC, IdLoc, Id, T, TInfo, StartLoc), |
1395 | TemplateParmPosition(D, P), ParameterPack(ParameterPack) {} |
1396 | |
1397 | NonTypeTemplateParmDecl(DeclContext *DC, SourceLocation StartLoc, |
1398 | SourceLocation IdLoc, unsigned D, unsigned P, |
1399 | IdentifierInfo *Id, QualType T, |
1400 | TypeSourceInfo *TInfo, |
1401 | ArrayRef<QualType> ExpandedTypes, |
1402 | ArrayRef<TypeSourceInfo *> ExpandedTInfos); |
1403 | |
1404 | public: |
1405 | static NonTypeTemplateParmDecl * |
1406 | Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
1407 | SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id, |
1408 | QualType T, bool ParameterPack, TypeSourceInfo *TInfo); |
1409 | |
1410 | static NonTypeTemplateParmDecl * |
1411 | Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
1412 | SourceLocation IdLoc, unsigned D, unsigned P, IdentifierInfo *Id, |
1413 | QualType T, TypeSourceInfo *TInfo, ArrayRef<QualType> ExpandedTypes, |
1414 | ArrayRef<TypeSourceInfo *> ExpandedTInfos); |
1415 | |
1416 | static NonTypeTemplateParmDecl *CreateDeserialized(ASTContext &C, |
1417 | unsigned ID, |
1418 | bool HasTypeConstraint); |
1419 | static NonTypeTemplateParmDecl *CreateDeserialized(ASTContext &C, |
1420 | unsigned ID, |
1421 | unsigned NumExpandedTypes, |
1422 | bool HasTypeConstraint); |
1423 | |
1424 | using TemplateParmPosition::getDepth; |
1425 | using TemplateParmPosition::setDepth; |
1426 | using TemplateParmPosition::getPosition; |
1427 | using TemplateParmPosition::setPosition; |
1428 | using TemplateParmPosition::getIndex; |
1429 | |
1430 | SourceRange getSourceRange() const override LLVM_READONLY; |
1431 | |
1432 | const DefArgStorage &getDefaultArgStorage() const { return DefaultArgument; } |
1433 | |
1434 | /// Determine whether this template parameter has a default |
1435 | /// argument. |
1436 | bool hasDefaultArgument() const { return DefaultArgument.isSet(); } |
1437 | |
1438 | /// Retrieve the default argument, if any. |
1439 | Expr *getDefaultArgument() const { return DefaultArgument.get(); } |
1440 | |
1441 | /// Retrieve the location of the default argument, if any. |
1442 | SourceLocation getDefaultArgumentLoc() const; |
1443 | |
1444 | /// Determines whether the default argument was inherited |
1445 | /// from a previous declaration of this template. |
1446 | bool defaultArgumentWasInherited() const { |
1447 | return DefaultArgument.isInherited(); |
1448 | } |
1449 | |
1450 | /// Set the default argument for this template parameter, and |
1451 | /// whether that default argument was inherited from another |
1452 | /// declaration. |
1453 | void setDefaultArgument(Expr *DefArg) { DefaultArgument.set(DefArg); } |
1454 | void setInheritedDefaultArgument(const ASTContext &C, |
1455 | NonTypeTemplateParmDecl *Parm) { |
1456 | DefaultArgument.setInherited(C, InheritedFrom: Parm); |
1457 | } |
1458 | |
1459 | /// Removes the default argument of this template parameter. |
1460 | void removeDefaultArgument() { DefaultArgument.clear(); } |
1461 | |
1462 | /// Whether this parameter is a non-type template parameter pack. |
1463 | /// |
1464 | /// If the parameter is a parameter pack, the type may be a |
1465 | /// \c PackExpansionType. In the following example, the \c Dims parameter |
1466 | /// is a parameter pack (whose type is 'unsigned'). |
1467 | /// |
1468 | /// \code |
1469 | /// template<typename T, unsigned ...Dims> struct multi_array; |
1470 | /// \endcode |
1471 | bool isParameterPack() const { return ParameterPack; } |
1472 | |
1473 | /// Whether this parameter pack is a pack expansion. |
1474 | /// |
1475 | /// A non-type template parameter pack is a pack expansion if its type |
1476 | /// contains an unexpanded parameter pack. In this case, we will have |
1477 | /// built a PackExpansionType wrapping the type. |
1478 | bool isPackExpansion() const { |
1479 | return ParameterPack && getType()->getAs<PackExpansionType>(); |
1480 | } |
1481 | |
1482 | /// Whether this parameter is a non-type template parameter pack |
1483 | /// that has a known list of different types at different positions. |
1484 | /// |
1485 | /// A parameter pack is an expanded parameter pack when the original |
1486 | /// parameter pack's type was itself a pack expansion, and that expansion |
1487 | /// has already been expanded. For example, given: |
1488 | /// |
1489 | /// \code |
1490 | /// template<typename ...Types> |
1491 | /// struct X { |
1492 | /// template<Types ...Values> |
1493 | /// struct Y { /* ... */ }; |
1494 | /// }; |
1495 | /// \endcode |
1496 | /// |
1497 | /// The parameter pack \c Values has a \c PackExpansionType as its type, |
1498 | /// which expands \c Types. When \c Types is supplied with template arguments |
1499 | /// by instantiating \c X, the instantiation of \c Values becomes an |
1500 | /// expanded parameter pack. For example, instantiating |
1501 | /// \c X<int, unsigned int> results in \c Values being an expanded parameter |
1502 | /// pack with expansion types \c int and \c unsigned int. |
1503 | /// |
1504 | /// The \c getExpansionType() and \c getExpansionTypeSourceInfo() functions |
1505 | /// return the expansion types. |
1506 | bool isExpandedParameterPack() const { return ExpandedParameterPack; } |
1507 | |
1508 | /// Retrieves the number of expansion types in an expanded parameter |
1509 | /// pack. |
1510 | unsigned getNumExpansionTypes() const { |
1511 | assert(ExpandedParameterPack && "Not an expansion parameter pack" ); |
1512 | return NumExpandedTypes; |
1513 | } |
1514 | |
1515 | /// Retrieve a particular expansion type within an expanded parameter |
1516 | /// pack. |
1517 | QualType getExpansionType(unsigned I) const { |
1518 | assert(I < NumExpandedTypes && "Out-of-range expansion type index" ); |
1519 | auto TypesAndInfos = |
1520 | getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>(); |
1521 | return TypesAndInfos[I].first; |
1522 | } |
1523 | |
1524 | /// Retrieve a particular expansion type source info within an |
1525 | /// expanded parameter pack. |
1526 | TypeSourceInfo *getExpansionTypeSourceInfo(unsigned I) const { |
1527 | assert(I < NumExpandedTypes && "Out-of-range expansion type index" ); |
1528 | auto TypesAndInfos = |
1529 | getTrailingObjects<std::pair<QualType, TypeSourceInfo *>>(); |
1530 | return TypesAndInfos[I].second; |
1531 | } |
1532 | |
1533 | /// Return the constraint introduced by the placeholder type of this non-type |
1534 | /// template parameter (if any). |
1535 | Expr *getPlaceholderTypeConstraint() const { |
1536 | return hasPlaceholderTypeConstraint() ? *getTrailingObjects<Expr *>() : |
1537 | nullptr; |
1538 | } |
1539 | |
1540 | void setPlaceholderTypeConstraint(Expr *E) { |
1541 | *getTrailingObjects<Expr *>() = E; |
1542 | } |
1543 | |
1544 | /// Determine whether this non-type template parameter's type has a |
1545 | /// placeholder with a type-constraint. |
1546 | bool hasPlaceholderTypeConstraint() const { |
1547 | auto *AT = getType()->getContainedAutoType(); |
1548 | return AT && AT->isConstrained(); |
1549 | } |
1550 | |
1551 | /// \brief Get the associated-constraints of this template parameter. |
1552 | /// This will either be a vector of size 1 containing the immediately-declared |
1553 | /// constraint introduced by the placeholder type, or an empty vector. |
1554 | /// |
1555 | /// Use this instead of getPlaceholderImmediatelyDeclaredConstraint for |
1556 | /// concepts APIs that accept an ArrayRef of constraint expressions. |
1557 | void getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const { |
1558 | if (Expr *E = getPlaceholderTypeConstraint()) |
1559 | AC.push_back(Elt: E); |
1560 | } |
1561 | |
1562 | // Implement isa/cast/dyncast/etc. |
1563 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
1564 | static bool classofKind(Kind K) { return K == NonTypeTemplateParm; } |
1565 | }; |
1566 | |
1567 | /// TemplateTemplateParmDecl - Declares a template template parameter, |
1568 | /// e.g., "T" in |
1569 | /// @code |
1570 | /// template <template <typename> class T> class container { }; |
1571 | /// @endcode |
1572 | /// A template template parameter is a TemplateDecl because it defines the |
1573 | /// name of a template and the template parameters allowable for substitution. |
1574 | class TemplateTemplateParmDecl final |
1575 | : public TemplateDecl, |
1576 | protected TemplateParmPosition, |
1577 | private llvm::TrailingObjects<TemplateTemplateParmDecl, |
1578 | TemplateParameterList *> { |
1579 | /// The default template argument, if any. |
1580 | using DefArgStorage = |
1581 | DefaultArgStorage<TemplateTemplateParmDecl, TemplateArgumentLoc *>; |
1582 | DefArgStorage DefaultArgument; |
1583 | |
1584 | /// Whether this parameter is a parameter pack. |
1585 | bool ParameterPack; |
1586 | |
1587 | /// Whether this template template parameter is an "expanded" |
1588 | /// parameter pack, meaning that it is a pack expansion and we |
1589 | /// already know the set of template parameters that expansion expands to. |
1590 | bool ExpandedParameterPack = false; |
1591 | |
1592 | /// The number of parameters in an expanded parameter pack. |
1593 | unsigned NumExpandedParams = 0; |
1594 | |
1595 | TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L, |
1596 | unsigned D, unsigned P, bool ParameterPack, |
1597 | IdentifierInfo *Id, TemplateParameterList *Params) |
1598 | : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params), |
1599 | TemplateParmPosition(D, P), ParameterPack(ParameterPack) {} |
1600 | |
1601 | TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L, |
1602 | unsigned D, unsigned P, |
1603 | IdentifierInfo *Id, TemplateParameterList *Params, |
1604 | ArrayRef<TemplateParameterList *> Expansions); |
1605 | |
1606 | void anchor() override; |
1607 | |
1608 | public: |
1609 | friend class ASTDeclReader; |
1610 | friend class ASTDeclWriter; |
1611 | friend TrailingObjects; |
1612 | |
1613 | static TemplateTemplateParmDecl *Create(const ASTContext &C, DeclContext *DC, |
1614 | SourceLocation L, unsigned D, |
1615 | unsigned P, bool ParameterPack, |
1616 | IdentifierInfo *Id, |
1617 | TemplateParameterList *Params); |
1618 | static TemplateTemplateParmDecl *Create(const ASTContext &C, DeclContext *DC, |
1619 | SourceLocation L, unsigned D, |
1620 | unsigned P, |
1621 | IdentifierInfo *Id, |
1622 | TemplateParameterList *Params, |
1623 | ArrayRef<TemplateParameterList *> Expansions); |
1624 | |
1625 | static TemplateTemplateParmDecl *CreateDeserialized(ASTContext &C, |
1626 | unsigned ID); |
1627 | static TemplateTemplateParmDecl *CreateDeserialized(ASTContext &C, |
1628 | unsigned ID, |
1629 | unsigned NumExpansions); |
1630 | |
1631 | using TemplateParmPosition::getDepth; |
1632 | using TemplateParmPosition::setDepth; |
1633 | using TemplateParmPosition::getPosition; |
1634 | using TemplateParmPosition::setPosition; |
1635 | using TemplateParmPosition::getIndex; |
1636 | |
1637 | /// Whether this template template parameter is a template |
1638 | /// parameter pack. |
1639 | /// |
1640 | /// \code |
1641 | /// template<template <class T> ...MetaFunctions> struct Apply; |
1642 | /// \endcode |
1643 | bool isParameterPack() const { return ParameterPack; } |
1644 | |
1645 | /// Whether this parameter pack is a pack expansion. |
1646 | /// |
1647 | /// A template template parameter pack is a pack expansion if its template |
1648 | /// parameter list contains an unexpanded parameter pack. |
1649 | bool isPackExpansion() const { |
1650 | return ParameterPack && |
1651 | getTemplateParameters()->containsUnexpandedParameterPack(); |
1652 | } |
1653 | |
1654 | /// Whether this parameter is a template template parameter pack that |
1655 | /// has a known list of different template parameter lists at different |
1656 | /// positions. |
1657 | /// |
1658 | /// A parameter pack is an expanded parameter pack when the original parameter |
1659 | /// pack's template parameter list was itself a pack expansion, and that |
1660 | /// expansion has already been expanded. For exampe, given: |
1661 | /// |
1662 | /// \code |
1663 | /// template<typename...Types> struct Outer { |
1664 | /// template<template<Types> class...Templates> struct Inner; |
1665 | /// }; |
1666 | /// \endcode |
1667 | /// |
1668 | /// The parameter pack \c Templates is a pack expansion, which expands the |
1669 | /// pack \c Types. When \c Types is supplied with template arguments by |
1670 | /// instantiating \c Outer, the instantiation of \c Templates is an expanded |
1671 | /// parameter pack. |
1672 | bool isExpandedParameterPack() const { return ExpandedParameterPack; } |
1673 | |
1674 | /// Retrieves the number of expansion template parameters in |
1675 | /// an expanded parameter pack. |
1676 | unsigned getNumExpansionTemplateParameters() const { |
1677 | assert(ExpandedParameterPack && "Not an expansion parameter pack" ); |
1678 | return NumExpandedParams; |
1679 | } |
1680 | |
1681 | /// Retrieve a particular expansion type within an expanded parameter |
1682 | /// pack. |
1683 | TemplateParameterList *getExpansionTemplateParameters(unsigned I) const { |
1684 | assert(I < NumExpandedParams && "Out-of-range expansion type index" ); |
1685 | return getTrailingObjects<TemplateParameterList *>()[I]; |
1686 | } |
1687 | |
1688 | const DefArgStorage &getDefaultArgStorage() const { return DefaultArgument; } |
1689 | |
1690 | /// Determine whether this template parameter has a default |
1691 | /// argument. |
1692 | bool hasDefaultArgument() const { return DefaultArgument.isSet(); } |
1693 | |
1694 | /// Retrieve the default argument, if any. |
1695 | const TemplateArgumentLoc &getDefaultArgument() const { |
1696 | static const TemplateArgumentLoc NoneLoc; |
1697 | return DefaultArgument.isSet() ? *DefaultArgument.get() : NoneLoc; |
1698 | } |
1699 | |
1700 | /// Retrieve the location of the default argument, if any. |
1701 | SourceLocation getDefaultArgumentLoc() const; |
1702 | |
1703 | /// Determines whether the default argument was inherited |
1704 | /// from a previous declaration of this template. |
1705 | bool defaultArgumentWasInherited() const { |
1706 | return DefaultArgument.isInherited(); |
1707 | } |
1708 | |
1709 | /// Set the default argument for this template parameter, and |
1710 | /// whether that default argument was inherited from another |
1711 | /// declaration. |
1712 | void setDefaultArgument(const ASTContext &C, |
1713 | const TemplateArgumentLoc &DefArg); |
1714 | void setInheritedDefaultArgument(const ASTContext &C, |
1715 | TemplateTemplateParmDecl *Prev) { |
1716 | DefaultArgument.setInherited(C, InheritedFrom: Prev); |
1717 | } |
1718 | |
1719 | /// Removes the default argument of this template parameter. |
1720 | void removeDefaultArgument() { DefaultArgument.clear(); } |
1721 | |
1722 | SourceRange getSourceRange() const override LLVM_READONLY { |
1723 | SourceLocation End = getLocation(); |
1724 | if (hasDefaultArgument() && !defaultArgumentWasInherited()) |
1725 | End = getDefaultArgument().getSourceRange().getEnd(); |
1726 | return SourceRange(getTemplateParameters()->getTemplateLoc(), End); |
1727 | } |
1728 | |
1729 | // Implement isa/cast/dyncast/etc. |
1730 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
1731 | static bool classofKind(Kind K) { return K == TemplateTemplateParm; } |
1732 | }; |
1733 | |
1734 | /// Represents the builtin template declaration which is used to |
1735 | /// implement __make_integer_seq and other builtin templates. It serves |
1736 | /// no real purpose beyond existing as a place to hold template parameters. |
1737 | class BuiltinTemplateDecl : public TemplateDecl { |
1738 | BuiltinTemplateKind BTK; |
1739 | |
1740 | BuiltinTemplateDecl(const ASTContext &C, DeclContext *DC, |
1741 | DeclarationName Name, BuiltinTemplateKind BTK); |
1742 | |
1743 | void anchor() override; |
1744 | |
1745 | public: |
1746 | // Implement isa/cast/dyncast support |
1747 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
1748 | static bool classofKind(Kind K) { return K == BuiltinTemplate; } |
1749 | |
1750 | static BuiltinTemplateDecl *Create(const ASTContext &C, DeclContext *DC, |
1751 | DeclarationName Name, |
1752 | BuiltinTemplateKind BTK) { |
1753 | return new (C, DC) BuiltinTemplateDecl(C, DC, Name, BTK); |
1754 | } |
1755 | |
1756 | SourceRange getSourceRange() const override LLVM_READONLY { |
1757 | return {}; |
1758 | } |
1759 | |
1760 | BuiltinTemplateKind getBuiltinTemplateKind() const { return BTK; } |
1761 | }; |
1762 | |
1763 | /// Represents a class template specialization, which refers to |
1764 | /// a class template with a given set of template arguments. |
1765 | /// |
1766 | /// Class template specializations represent both explicit |
1767 | /// specialization of class templates, as in the example below, and |
1768 | /// implicit instantiations of class templates. |
1769 | /// |
1770 | /// \code |
1771 | /// template<typename T> class array; |
1772 | /// |
1773 | /// template<> |
1774 | /// class array<bool> { }; // class template specialization array<bool> |
1775 | /// \endcode |
1776 | class ClassTemplateSpecializationDecl |
1777 | : public CXXRecordDecl, public llvm::FoldingSetNode { |
1778 | /// Structure that stores information about a class template |
1779 | /// specialization that was instantiated from a class template partial |
1780 | /// specialization. |
1781 | struct SpecializedPartialSpecialization { |
1782 | /// The class template partial specialization from which this |
1783 | /// class template specialization was instantiated. |
1784 | ClassTemplatePartialSpecializationDecl *PartialSpecialization; |
1785 | |
1786 | /// The template argument list deduced for the class template |
1787 | /// partial specialization itself. |
1788 | const TemplateArgumentList *TemplateArgs; |
1789 | }; |
1790 | |
1791 | /// The template that this specialization specializes |
1792 | llvm::PointerUnion<ClassTemplateDecl *, SpecializedPartialSpecialization *> |
1793 | SpecializedTemplate; |
1794 | |
1795 | /// Further info for explicit template specialization/instantiation. |
1796 | struct ExplicitSpecializationInfo { |
1797 | /// The type-as-written. |
1798 | TypeSourceInfo *TypeAsWritten = nullptr; |
1799 | |
1800 | /// The location of the extern keyword. |
1801 | SourceLocation ExternLoc; |
1802 | |
1803 | /// The location of the template keyword. |
1804 | SourceLocation TemplateKeywordLoc; |
1805 | |
1806 | ExplicitSpecializationInfo() = default; |
1807 | }; |
1808 | |
1809 | /// Further info for explicit template specialization/instantiation. |
1810 | /// Does not apply to implicit specializations. |
1811 | ExplicitSpecializationInfo *ExplicitInfo = nullptr; |
1812 | |
1813 | /// The template arguments used to describe this specialization. |
1814 | const TemplateArgumentList *TemplateArgs; |
1815 | |
1816 | /// The point where this template was instantiated (if any) |
1817 | SourceLocation PointOfInstantiation; |
1818 | |
1819 | /// The kind of specialization this declaration refers to. |
1820 | LLVM_PREFERRED_TYPE(TemplateSpecializationKind) |
1821 | unsigned SpecializationKind : 3; |
1822 | |
1823 | protected: |
1824 | ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK, |
1825 | DeclContext *DC, SourceLocation StartLoc, |
1826 | SourceLocation IdLoc, |
1827 | ClassTemplateDecl *SpecializedTemplate, |
1828 | ArrayRef<TemplateArgument> Args, |
1829 | ClassTemplateSpecializationDecl *PrevDecl); |
1830 | |
1831 | explicit ClassTemplateSpecializationDecl(ASTContext &C, Kind DK); |
1832 | |
1833 | public: |
1834 | friend class ASTDeclReader; |
1835 | friend class ASTDeclWriter; |
1836 | |
1837 | static ClassTemplateSpecializationDecl * |
1838 | Create(ASTContext &Context, TagKind TK, DeclContext *DC, |
1839 | SourceLocation StartLoc, SourceLocation IdLoc, |
1840 | ClassTemplateDecl *SpecializedTemplate, |
1841 | ArrayRef<TemplateArgument> Args, |
1842 | ClassTemplateSpecializationDecl *PrevDecl); |
1843 | static ClassTemplateSpecializationDecl * |
1844 | CreateDeserialized(ASTContext &C, unsigned ID); |
1845 | |
1846 | void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy, |
1847 | bool Qualified) const override; |
1848 | |
1849 | // FIXME: This is broken. CXXRecordDecl::getMostRecentDecl() returns a |
1850 | // different "most recent" declaration from this function for the same |
1851 | // declaration, because we don't override getMostRecentDeclImpl(). But |
1852 | // it's not clear that we should override that, because the most recent |
1853 | // declaration as a CXXRecordDecl sometimes is the injected-class-name. |
1854 | ClassTemplateSpecializationDecl *getMostRecentDecl() { |
1855 | return cast<ClassTemplateSpecializationDecl>( |
1856 | getMostRecentNonInjectedDecl()); |
1857 | } |
1858 | |
1859 | /// Retrieve the template that this specialization specializes. |
1860 | ClassTemplateDecl *getSpecializedTemplate() const; |
1861 | |
1862 | /// Retrieve the template arguments of the class template |
1863 | /// specialization. |
1864 | const TemplateArgumentList &getTemplateArgs() const { |
1865 | return *TemplateArgs; |
1866 | } |
1867 | |
1868 | void setTemplateArgs(TemplateArgumentList *Args) { |
1869 | TemplateArgs = Args; |
1870 | } |
1871 | |
1872 | /// Determine the kind of specialization that this |
1873 | /// declaration represents. |
1874 | TemplateSpecializationKind getSpecializationKind() const { |
1875 | return static_cast<TemplateSpecializationKind>(SpecializationKind); |
1876 | } |
1877 | |
1878 | bool isExplicitSpecialization() const { |
1879 | return getSpecializationKind() == TSK_ExplicitSpecialization; |
1880 | } |
1881 | |
1882 | /// Is this an explicit specialization at class scope (within the class that |
1883 | /// owns the primary template)? For example: |
1884 | /// |
1885 | /// \code |
1886 | /// template<typename T> struct Outer { |
1887 | /// template<typename U> struct Inner; |
1888 | /// template<> struct Inner; // class-scope explicit specialization |
1889 | /// }; |
1890 | /// \endcode |
1891 | bool isClassScopeExplicitSpecialization() const { |
1892 | return isExplicitSpecialization() && |
1893 | isa<CXXRecordDecl>(getLexicalDeclContext()); |
1894 | } |
1895 | |
1896 | /// True if this declaration is an explicit specialization, |
1897 | /// explicit instantiation declaration, or explicit instantiation |
1898 | /// definition. |
1899 | bool isExplicitInstantiationOrSpecialization() const { |
1900 | return isTemplateExplicitInstantiationOrSpecialization( |
1901 | getTemplateSpecializationKind()); |
1902 | } |
1903 | |
1904 | void setSpecializedTemplate(ClassTemplateDecl *Specialized) { |
1905 | SpecializedTemplate = Specialized; |
1906 | } |
1907 | |
1908 | void setSpecializationKind(TemplateSpecializationKind TSK) { |
1909 | SpecializationKind = TSK; |
1910 | } |
1911 | |
1912 | /// Get the point of instantiation (if any), or null if none. |
1913 | SourceLocation getPointOfInstantiation() const { |
1914 | return PointOfInstantiation; |
1915 | } |
1916 | |
1917 | void setPointOfInstantiation(SourceLocation Loc) { |
1918 | assert(Loc.isValid() && "point of instantiation must be valid!" ); |
1919 | PointOfInstantiation = Loc; |
1920 | } |
1921 | |
1922 | /// If this class template specialization is an instantiation of |
1923 | /// a template (rather than an explicit specialization), return the |
1924 | /// class template or class template partial specialization from which it |
1925 | /// was instantiated. |
1926 | llvm::PointerUnion<ClassTemplateDecl *, |
1927 | ClassTemplatePartialSpecializationDecl *> |
1928 | getInstantiatedFrom() const { |
1929 | if (!isTemplateInstantiation(Kind: getSpecializationKind())) |
1930 | return llvm::PointerUnion<ClassTemplateDecl *, |
1931 | ClassTemplatePartialSpecializationDecl *>(); |
1932 | |
1933 | return getSpecializedTemplateOrPartial(); |
1934 | } |
1935 | |
1936 | /// Retrieve the class template or class template partial |
1937 | /// specialization which was specialized by this. |
1938 | llvm::PointerUnion<ClassTemplateDecl *, |
1939 | ClassTemplatePartialSpecializationDecl *> |
1940 | getSpecializedTemplateOrPartial() const { |
1941 | if (const auto *PartialSpec = |
1942 | SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>()) |
1943 | return PartialSpec->PartialSpecialization; |
1944 | |
1945 | return SpecializedTemplate.get<ClassTemplateDecl*>(); |
1946 | } |
1947 | |
1948 | /// Retrieve the set of template arguments that should be used |
1949 | /// to instantiate members of the class template or class template partial |
1950 | /// specialization from which this class template specialization was |
1951 | /// instantiated. |
1952 | /// |
1953 | /// \returns For a class template specialization instantiated from the primary |
1954 | /// template, this function will return the same template arguments as |
1955 | /// getTemplateArgs(). For a class template specialization instantiated from |
1956 | /// a class template partial specialization, this function will return the |
1957 | /// deduced template arguments for the class template partial specialization |
1958 | /// itself. |
1959 | const TemplateArgumentList &getTemplateInstantiationArgs() const { |
1960 | if (const auto *PartialSpec = |
1961 | SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>()) |
1962 | return *PartialSpec->TemplateArgs; |
1963 | |
1964 | return getTemplateArgs(); |
1965 | } |
1966 | |
1967 | /// Note that this class template specialization is actually an |
1968 | /// instantiation of the given class template partial specialization whose |
1969 | /// template arguments have been deduced. |
1970 | void setInstantiationOf(ClassTemplatePartialSpecializationDecl *PartialSpec, |
1971 | const TemplateArgumentList *TemplateArgs) { |
1972 | assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() && |
1973 | "Already set to a class template partial specialization!" ); |
1974 | auto *PS = new (getASTContext()) SpecializedPartialSpecialization(); |
1975 | PS->PartialSpecialization = PartialSpec; |
1976 | PS->TemplateArgs = TemplateArgs; |
1977 | SpecializedTemplate = PS; |
1978 | } |
1979 | |
1980 | /// Note that this class template specialization is an instantiation |
1981 | /// of the given class template. |
1982 | void setInstantiationOf(ClassTemplateDecl *TemplDecl) { |
1983 | assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() && |
1984 | "Previously set to a class template partial specialization!" ); |
1985 | SpecializedTemplate = TemplDecl; |
1986 | } |
1987 | |
1988 | /// Sets the type of this specialization as it was written by |
1989 | /// the user. This will be a class template specialization type. |
1990 | void setTypeAsWritten(TypeSourceInfo *T) { |
1991 | if (!ExplicitInfo) |
1992 | ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; |
1993 | ExplicitInfo->TypeAsWritten = T; |
1994 | } |
1995 | |
1996 | /// Gets the type of this specialization as it was written by |
1997 | /// the user, if it was so written. |
1998 | TypeSourceInfo *getTypeAsWritten() const { |
1999 | return ExplicitInfo ? ExplicitInfo->TypeAsWritten : nullptr; |
2000 | } |
2001 | |
2002 | /// Gets the location of the extern keyword, if present. |
2003 | SourceLocation getExternLoc() const { |
2004 | return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation(); |
2005 | } |
2006 | |
2007 | /// Sets the location of the extern keyword. |
2008 | void setExternLoc(SourceLocation Loc) { |
2009 | if (!ExplicitInfo) |
2010 | ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; |
2011 | ExplicitInfo->ExternLoc = Loc; |
2012 | } |
2013 | |
2014 | /// Sets the location of the template keyword. |
2015 | void setTemplateKeywordLoc(SourceLocation Loc) { |
2016 | if (!ExplicitInfo) |
2017 | ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; |
2018 | ExplicitInfo->TemplateKeywordLoc = Loc; |
2019 | } |
2020 | |
2021 | /// Gets the location of the template keyword, if present. |
2022 | SourceLocation getTemplateKeywordLoc() const { |
2023 | return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation(); |
2024 | } |
2025 | |
2026 | SourceRange getSourceRange() const override LLVM_READONLY; |
2027 | |
2028 | void Profile(llvm::FoldingSetNodeID &ID) const { |
2029 | Profile(ID, TemplateArgs->asArray(), getASTContext()); |
2030 | } |
2031 | |
2032 | static void |
2033 | Profile(llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs, |
2034 | const ASTContext &Context) { |
2035 | ID.AddInteger(I: TemplateArgs.size()); |
2036 | for (const TemplateArgument &TemplateArg : TemplateArgs) |
2037 | TemplateArg.Profile(ID, Context); |
2038 | } |
2039 | |
2040 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
2041 | |
2042 | static bool classofKind(Kind K) { |
2043 | return K >= firstClassTemplateSpecialization && |
2044 | K <= lastClassTemplateSpecialization; |
2045 | } |
2046 | }; |
2047 | |
2048 | class ClassTemplatePartialSpecializationDecl |
2049 | : public ClassTemplateSpecializationDecl { |
2050 | /// The list of template parameters |
2051 | TemplateParameterList* TemplateParams = nullptr; |
2052 | |
2053 | /// The source info for the template arguments as written. |
2054 | /// FIXME: redundant with TypeAsWritten? |
2055 | const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr; |
2056 | |
2057 | /// The class template partial specialization from which this |
2058 | /// class template partial specialization was instantiated. |
2059 | /// |
2060 | /// The boolean value will be true to indicate that this class template |
2061 | /// partial specialization was specialized at this level. |
2062 | llvm::PointerIntPair<ClassTemplatePartialSpecializationDecl *, 1, bool> |
2063 | InstantiatedFromMember; |
2064 | |
2065 | ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK, |
2066 | DeclContext *DC, |
2067 | SourceLocation StartLoc, |
2068 | SourceLocation IdLoc, |
2069 | TemplateParameterList *Params, |
2070 | ClassTemplateDecl *SpecializedTemplate, |
2071 | ArrayRef<TemplateArgument> Args, |
2072 | const ASTTemplateArgumentListInfo *ArgsAsWritten, |
2073 | ClassTemplatePartialSpecializationDecl *PrevDecl); |
2074 | |
2075 | ClassTemplatePartialSpecializationDecl(ASTContext &C) |
2076 | : ClassTemplateSpecializationDecl(C, ClassTemplatePartialSpecialization), |
2077 | InstantiatedFromMember(nullptr, false) {} |
2078 | |
2079 | void anchor() override; |
2080 | |
2081 | public: |
2082 | friend class ASTDeclReader; |
2083 | friend class ASTDeclWriter; |
2084 | |
2085 | static ClassTemplatePartialSpecializationDecl * |
2086 | Create(ASTContext &Context, TagKind TK, DeclContext *DC, |
2087 | SourceLocation StartLoc, SourceLocation IdLoc, |
2088 | TemplateParameterList *Params, |
2089 | ClassTemplateDecl *SpecializedTemplate, |
2090 | ArrayRef<TemplateArgument> Args, |
2091 | const TemplateArgumentListInfo &ArgInfos, |
2092 | QualType CanonInjectedType, |
2093 | ClassTemplatePartialSpecializationDecl *PrevDecl); |
2094 | |
2095 | static ClassTemplatePartialSpecializationDecl * |
2096 | CreateDeserialized(ASTContext &C, unsigned ID); |
2097 | |
2098 | ClassTemplatePartialSpecializationDecl *getMostRecentDecl() { |
2099 | return cast<ClassTemplatePartialSpecializationDecl>( |
2100 | static_cast<ClassTemplateSpecializationDecl *>( |
2101 | this)->getMostRecentDecl()); |
2102 | } |
2103 | |
2104 | /// Get the list of template parameters |
2105 | TemplateParameterList *getTemplateParameters() const { |
2106 | return TemplateParams; |
2107 | } |
2108 | |
2109 | /// \brief All associated constraints of this partial specialization, |
2110 | /// including the requires clause and any constraints derived from |
2111 | /// constrained-parameters. |
2112 | /// |
2113 | /// The constraints in the resulting list are to be treated as if in a |
2114 | /// conjunction ("and"). |
2115 | void getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const { |
2116 | TemplateParams->getAssociatedConstraints(AC); |
2117 | } |
2118 | |
2119 | bool hasAssociatedConstraints() const { |
2120 | return TemplateParams->hasAssociatedConstraints(); |
2121 | } |
2122 | |
2123 | /// Get the template arguments as written. |
2124 | const ASTTemplateArgumentListInfo *getTemplateArgsAsWritten() const { |
2125 | return ArgsAsWritten; |
2126 | } |
2127 | |
2128 | /// Retrieve the member class template partial specialization from |
2129 | /// which this particular class template partial specialization was |
2130 | /// instantiated. |
2131 | /// |
2132 | /// \code |
2133 | /// template<typename T> |
2134 | /// struct Outer { |
2135 | /// template<typename U> struct Inner; |
2136 | /// template<typename U> struct Inner<U*> { }; // #1 |
2137 | /// }; |
2138 | /// |
2139 | /// Outer<float>::Inner<int*> ii; |
2140 | /// \endcode |
2141 | /// |
2142 | /// In this example, the instantiation of \c Outer<float>::Inner<int*> will |
2143 | /// end up instantiating the partial specialization |
2144 | /// \c Outer<float>::Inner<U*>, which itself was instantiated from the class |
2145 | /// template partial specialization \c Outer<T>::Inner<U*>. Given |
2146 | /// \c Outer<float>::Inner<U*>, this function would return |
2147 | /// \c Outer<T>::Inner<U*>. |
2148 | ClassTemplatePartialSpecializationDecl *getInstantiatedFromMember() const { |
2149 | const auto *First = |
2150 | cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl()); |
2151 | return First->InstantiatedFromMember.getPointer(); |
2152 | } |
2153 | ClassTemplatePartialSpecializationDecl * |
2154 | getInstantiatedFromMemberTemplate() const { |
2155 | return getInstantiatedFromMember(); |
2156 | } |
2157 | |
2158 | void setInstantiatedFromMember( |
2159 | ClassTemplatePartialSpecializationDecl *PartialSpec) { |
2160 | auto *First = cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl()); |
2161 | First->InstantiatedFromMember.setPointer(PartialSpec); |
2162 | } |
2163 | |
2164 | /// Determines whether this class template partial specialization |
2165 | /// template was a specialization of a member partial specialization. |
2166 | /// |
2167 | /// In the following example, the member template partial specialization |
2168 | /// \c X<int>::Inner<T*> is a member specialization. |
2169 | /// |
2170 | /// \code |
2171 | /// template<typename T> |
2172 | /// struct X { |
2173 | /// template<typename U> struct Inner; |
2174 | /// template<typename U> struct Inner<U*>; |
2175 | /// }; |
2176 | /// |
2177 | /// template<> template<typename T> |
2178 | /// struct X<int>::Inner<T*> { /* ... */ }; |
2179 | /// \endcode |
2180 | bool isMemberSpecialization() { |
2181 | const auto *First = |
2182 | cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl()); |
2183 | return First->InstantiatedFromMember.getInt(); |
2184 | } |
2185 | |
2186 | /// Note that this member template is a specialization. |
2187 | void setMemberSpecialization() { |
2188 | auto *First = cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl()); |
2189 | assert(First->InstantiatedFromMember.getPointer() && |
2190 | "Only member templates can be member template specializations" ); |
2191 | return First->InstantiatedFromMember.setInt(true); |
2192 | } |
2193 | |
2194 | /// Retrieves the injected specialization type for this partial |
2195 | /// specialization. This is not the same as the type-decl-type for |
2196 | /// this partial specialization, which is an InjectedClassNameType. |
2197 | QualType getInjectedSpecializationType() const { |
2198 | assert(getTypeForDecl() && "partial specialization has no type set!" ); |
2199 | return cast<InjectedClassNameType>(getTypeForDecl()) |
2200 | ->getInjectedSpecializationType(); |
2201 | } |
2202 | |
2203 | void Profile(llvm::FoldingSetNodeID &ID) const { |
2204 | Profile(ID, getTemplateArgs().asArray(), getTemplateParameters(), |
2205 | getASTContext()); |
2206 | } |
2207 | |
2208 | static void |
2209 | Profile(llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs, |
2210 | TemplateParameterList *TPL, const ASTContext &Context); |
2211 | |
2212 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
2213 | |
2214 | static bool classofKind(Kind K) { |
2215 | return K == ClassTemplatePartialSpecialization; |
2216 | } |
2217 | }; |
2218 | |
2219 | /// Declaration of a class template. |
2220 | class ClassTemplateDecl : public RedeclarableTemplateDecl { |
2221 | protected: |
2222 | /// Data that is common to all of the declarations of a given |
2223 | /// class template. |
2224 | struct Common : CommonBase { |
2225 | /// The class template specializations for this class |
2226 | /// template, including explicit specializations and instantiations. |
2227 | llvm::FoldingSetVector<ClassTemplateSpecializationDecl> Specializations; |
2228 | |
2229 | /// The class template partial specializations for this class |
2230 | /// template. |
2231 | llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> |
2232 | PartialSpecializations; |
2233 | |
2234 | /// The injected-class-name type for this class template. |
2235 | QualType InjectedClassNameType; |
2236 | |
2237 | Common() = default; |
2238 | }; |
2239 | |
2240 | /// Retrieve the set of specializations of this class template. |
2241 | llvm::FoldingSetVector<ClassTemplateSpecializationDecl> & |
2242 | getSpecializations() const; |
2243 | |
2244 | /// Retrieve the set of partial specializations of this class |
2245 | /// template. |
2246 | llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> & |
2247 | getPartialSpecializations() const; |
2248 | |
2249 | ClassTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L, |
2250 | DeclarationName Name, TemplateParameterList *Params, |
2251 | NamedDecl *Decl) |
2252 | : RedeclarableTemplateDecl(ClassTemplate, C, DC, L, Name, Params, Decl) {} |
2253 | |
2254 | CommonBase *newCommon(ASTContext &C) const override; |
2255 | |
2256 | Common *getCommonPtr() const { |
2257 | return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr()); |
2258 | } |
2259 | |
2260 | void setCommonPtr(Common *C) { |
2261 | RedeclarableTemplateDecl::Common = C; |
2262 | } |
2263 | |
2264 | public: |
2265 | |
2266 | friend class ASTDeclReader; |
2267 | friend class ASTDeclWriter; |
2268 | friend class TemplateDeclInstantiator; |
2269 | |
2270 | /// Load any lazily-loaded specializations from the external source. |
2271 | void LoadLazySpecializations() const; |
2272 | |
2273 | /// Get the underlying class declarations of the template. |
2274 | CXXRecordDecl *getTemplatedDecl() const { |
2275 | return static_cast<CXXRecordDecl *>(TemplatedDecl); |
2276 | } |
2277 | |
2278 | /// Returns whether this template declaration defines the primary |
2279 | /// class pattern. |
2280 | bool isThisDeclarationADefinition() const { |
2281 | return getTemplatedDecl()->isThisDeclarationADefinition(); |
2282 | } |
2283 | |
2284 | /// \brief Create a class template node. |
2285 | static ClassTemplateDecl *Create(ASTContext &C, DeclContext *DC, |
2286 | SourceLocation L, |
2287 | DeclarationName Name, |
2288 | TemplateParameterList *Params, |
2289 | NamedDecl *Decl); |
2290 | |
2291 | /// Create an empty class template node. |
2292 | static ClassTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
2293 | |
2294 | /// Return the specialization with the provided arguments if it exists, |
2295 | /// otherwise return the insertion point. |
2296 | ClassTemplateSpecializationDecl * |
2297 | findSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos); |
2298 | |
2299 | /// Insert the specified specialization knowing that it is not already |
2300 | /// in. InsertPos must be obtained from findSpecialization. |
2301 | void AddSpecialization(ClassTemplateSpecializationDecl *D, void *InsertPos); |
2302 | |
2303 | ClassTemplateDecl *getCanonicalDecl() override { |
2304 | return cast<ClassTemplateDecl>( |
2305 | RedeclarableTemplateDecl::getCanonicalDecl()); |
2306 | } |
2307 | const ClassTemplateDecl *getCanonicalDecl() const { |
2308 | return cast<ClassTemplateDecl>( |
2309 | RedeclarableTemplateDecl::getCanonicalDecl()); |
2310 | } |
2311 | |
2312 | /// Retrieve the previous declaration of this class template, or |
2313 | /// nullptr if no such declaration exists. |
2314 | ClassTemplateDecl *getPreviousDecl() { |
2315 | return cast_or_null<ClassTemplateDecl>( |
2316 | static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl()); |
2317 | } |
2318 | const ClassTemplateDecl *getPreviousDecl() const { |
2319 | return cast_or_null<ClassTemplateDecl>( |
2320 | static_cast<const RedeclarableTemplateDecl *>( |
2321 | this)->getPreviousDecl()); |
2322 | } |
2323 | |
2324 | ClassTemplateDecl *getMostRecentDecl() { |
2325 | return cast<ClassTemplateDecl>( |
2326 | static_cast<RedeclarableTemplateDecl *>(this)->getMostRecentDecl()); |
2327 | } |
2328 | const ClassTemplateDecl *getMostRecentDecl() const { |
2329 | return const_cast<ClassTemplateDecl*>(this)->getMostRecentDecl(); |
2330 | } |
2331 | |
2332 | ClassTemplateDecl *getInstantiatedFromMemberTemplate() const { |
2333 | return cast_or_null<ClassTemplateDecl>( |
2334 | RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate()); |
2335 | } |
2336 | |
2337 | /// Return the partial specialization with the provided arguments if it |
2338 | /// exists, otherwise return the insertion point. |
2339 | ClassTemplatePartialSpecializationDecl * |
2340 | findPartialSpecialization(ArrayRef<TemplateArgument> Args, |
2341 | TemplateParameterList *TPL, void *&InsertPos); |
2342 | |
2343 | /// Insert the specified partial specialization knowing that it is not |
2344 | /// already in. InsertPos must be obtained from findPartialSpecialization. |
2345 | void AddPartialSpecialization(ClassTemplatePartialSpecializationDecl *D, |
2346 | void *InsertPos); |
2347 | |
2348 | /// Retrieve the partial specializations as an ordered list. |
2349 | void getPartialSpecializations( |
2350 | SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) const; |
2351 | |
2352 | /// Find a class template partial specialization with the given |
2353 | /// type T. |
2354 | /// |
2355 | /// \param T a dependent type that names a specialization of this class |
2356 | /// template. |
2357 | /// |
2358 | /// \returns the class template partial specialization that exactly matches |
2359 | /// the type \p T, or nullptr if no such partial specialization exists. |
2360 | ClassTemplatePartialSpecializationDecl *findPartialSpecialization(QualType T); |
2361 | |
2362 | /// Find a class template partial specialization which was instantiated |
2363 | /// from the given member partial specialization. |
2364 | /// |
2365 | /// \param D a member class template partial specialization. |
2366 | /// |
2367 | /// \returns the class template partial specialization which was instantiated |
2368 | /// from the given member partial specialization, or nullptr if no such |
2369 | /// partial specialization exists. |
2370 | ClassTemplatePartialSpecializationDecl * |
2371 | findPartialSpecInstantiatedFromMember( |
2372 | ClassTemplatePartialSpecializationDecl *D); |
2373 | |
2374 | /// Retrieve the template specialization type of the |
2375 | /// injected-class-name for this class template. |
2376 | /// |
2377 | /// The injected-class-name for a class template \c X is \c |
2378 | /// X<template-args>, where \c template-args is formed from the |
2379 | /// template arguments that correspond to the template parameters of |
2380 | /// \c X. For example: |
2381 | /// |
2382 | /// \code |
2383 | /// template<typename T, int N> |
2384 | /// struct array { |
2385 | /// typedef array this_type; // "array" is equivalent to "array<T, N>" |
2386 | /// }; |
2387 | /// \endcode |
2388 | QualType getInjectedClassNameSpecialization(); |
2389 | |
2390 | using spec_iterator = SpecIterator<ClassTemplateSpecializationDecl>; |
2391 | using spec_range = llvm::iterator_range<spec_iterator>; |
2392 | |
2393 | spec_range specializations() const { |
2394 | return spec_range(spec_begin(), spec_end()); |
2395 | } |
2396 | |
2397 | spec_iterator spec_begin() const { |
2398 | return makeSpecIterator(Specs&: getSpecializations(), isEnd: false); |
2399 | } |
2400 | |
2401 | spec_iterator spec_end() const { |
2402 | return makeSpecIterator(Specs&: getSpecializations(), isEnd: true); |
2403 | } |
2404 | |
2405 | // Implement isa/cast/dyncast support |
2406 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
2407 | static bool classofKind(Kind K) { return K == ClassTemplate; } |
2408 | }; |
2409 | |
2410 | /// Declaration of a friend template. |
2411 | /// |
2412 | /// For example: |
2413 | /// \code |
2414 | /// template \<typename T> class A { |
2415 | /// friend class MyVector<T>; // not a friend template |
2416 | /// template \<typename U> friend class B; // not a friend template |
2417 | /// template \<typename U> friend class Foo<T>::Nested; // friend template |
2418 | /// }; |
2419 | /// \endcode |
2420 | /// |
2421 | /// \note This class is not currently in use. All of the above |
2422 | /// will yield a FriendDecl, not a FriendTemplateDecl. |
2423 | class FriendTemplateDecl : public Decl { |
2424 | virtual void anchor(); |
2425 | |
2426 | public: |
2427 | using FriendUnion = llvm::PointerUnion<NamedDecl *,TypeSourceInfo *>; |
2428 | |
2429 | private: |
2430 | // The number of template parameters; always non-zero. |
2431 | unsigned NumParams = 0; |
2432 | |
2433 | // The parameter list. |
2434 | TemplateParameterList **Params = nullptr; |
2435 | |
2436 | // The declaration that's a friend of this class. |
2437 | FriendUnion Friend; |
2438 | |
2439 | // Location of the 'friend' specifier. |
2440 | SourceLocation FriendLoc; |
2441 | |
2442 | FriendTemplateDecl(DeclContext *DC, SourceLocation Loc, |
2443 | TemplateParameterList **Params, unsigned NumParams, |
2444 | FriendUnion Friend, SourceLocation FriendLoc) |
2445 | : Decl(Decl::FriendTemplate, DC, Loc), NumParams(NumParams), |
2446 | Params(Params), Friend(Friend), FriendLoc(FriendLoc) {} |
2447 | |
2448 | FriendTemplateDecl(EmptyShell Empty) : Decl(Decl::FriendTemplate, Empty) {} |
2449 | |
2450 | public: |
2451 | friend class ASTDeclReader; |
2452 | |
2453 | static FriendTemplateDecl * |
2454 | Create(ASTContext &Context, DeclContext *DC, SourceLocation Loc, |
2455 | MutableArrayRef<TemplateParameterList *> Params, FriendUnion Friend, |
2456 | SourceLocation FriendLoc); |
2457 | |
2458 | static FriendTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
2459 | |
2460 | /// If this friend declaration names a templated type (or |
2461 | /// a dependent member type of a templated type), return that |
2462 | /// type; otherwise return null. |
2463 | TypeSourceInfo *getFriendType() const { |
2464 | return Friend.dyn_cast<TypeSourceInfo*>(); |
2465 | } |
2466 | |
2467 | /// If this friend declaration names a templated function (or |
2468 | /// a member function of a templated type), return that type; |
2469 | /// otherwise return null. |
2470 | NamedDecl *getFriendDecl() const { |
2471 | return Friend.dyn_cast<NamedDecl*>(); |
2472 | } |
2473 | |
2474 | /// Retrieves the location of the 'friend' keyword. |
2475 | SourceLocation getFriendLoc() const { |
2476 | return FriendLoc; |
2477 | } |
2478 | |
2479 | TemplateParameterList *getTemplateParameterList(unsigned i) const { |
2480 | assert(i <= NumParams); |
2481 | return Params[i]; |
2482 | } |
2483 | |
2484 | unsigned getNumTemplateParameters() const { |
2485 | return NumParams; |
2486 | } |
2487 | |
2488 | // Implement isa/cast/dyncast/etc. |
2489 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
2490 | static bool classofKind(Kind K) { return K == Decl::FriendTemplate; } |
2491 | }; |
2492 | |
2493 | /// Declaration of an alias template. |
2494 | /// |
2495 | /// For example: |
2496 | /// \code |
2497 | /// template \<typename T> using V = std::map<T*, int, MyCompare<T>>; |
2498 | /// \endcode |
2499 | class TypeAliasTemplateDecl : public RedeclarableTemplateDecl { |
2500 | protected: |
2501 | using Common = CommonBase; |
2502 | |
2503 | TypeAliasTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L, |
2504 | DeclarationName Name, TemplateParameterList *Params, |
2505 | NamedDecl *Decl) |
2506 | : RedeclarableTemplateDecl(TypeAliasTemplate, C, DC, L, Name, Params, |
2507 | Decl) {} |
2508 | |
2509 | CommonBase *newCommon(ASTContext &C) const override; |
2510 | |
2511 | Common *getCommonPtr() { |
2512 | return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr()); |
2513 | } |
2514 | |
2515 | public: |
2516 | friend class ASTDeclReader; |
2517 | friend class ASTDeclWriter; |
2518 | |
2519 | /// Get the underlying function declaration of the template. |
2520 | TypeAliasDecl *getTemplatedDecl() const { |
2521 | return static_cast<TypeAliasDecl *>(TemplatedDecl); |
2522 | } |
2523 | |
2524 | |
2525 | TypeAliasTemplateDecl *getCanonicalDecl() override { |
2526 | return cast<TypeAliasTemplateDecl>( |
2527 | RedeclarableTemplateDecl::getCanonicalDecl()); |
2528 | } |
2529 | const TypeAliasTemplateDecl *getCanonicalDecl() const { |
2530 | return cast<TypeAliasTemplateDecl>( |
2531 | RedeclarableTemplateDecl::getCanonicalDecl()); |
2532 | } |
2533 | |
2534 | /// Retrieve the previous declaration of this function template, or |
2535 | /// nullptr if no such declaration exists. |
2536 | TypeAliasTemplateDecl *getPreviousDecl() { |
2537 | return cast_or_null<TypeAliasTemplateDecl>( |
2538 | static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl()); |
2539 | } |
2540 | const TypeAliasTemplateDecl *getPreviousDecl() const { |
2541 | return cast_or_null<TypeAliasTemplateDecl>( |
2542 | static_cast<const RedeclarableTemplateDecl *>( |
2543 | this)->getPreviousDecl()); |
2544 | } |
2545 | |
2546 | TypeAliasTemplateDecl *getInstantiatedFromMemberTemplate() const { |
2547 | return cast_or_null<TypeAliasTemplateDecl>( |
2548 | RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate()); |
2549 | } |
2550 | |
2551 | /// Create a function template node. |
2552 | static TypeAliasTemplateDecl *Create(ASTContext &C, DeclContext *DC, |
2553 | SourceLocation L, |
2554 | DeclarationName Name, |
2555 | TemplateParameterList *Params, |
2556 | NamedDecl *Decl); |
2557 | |
2558 | /// Create an empty alias template node. |
2559 | static TypeAliasTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
2560 | |
2561 | // Implement isa/cast/dyncast support |
2562 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
2563 | static bool classofKind(Kind K) { return K == TypeAliasTemplate; } |
2564 | }; |
2565 | |
2566 | /// Represents a variable template specialization, which refers to |
2567 | /// a variable template with a given set of template arguments. |
2568 | /// |
2569 | /// Variable template specializations represent both explicit |
2570 | /// specializations of variable templates, as in the example below, and |
2571 | /// implicit instantiations of variable templates. |
2572 | /// |
2573 | /// \code |
2574 | /// template<typename T> constexpr T pi = T(3.1415926535897932385); |
2575 | /// |
2576 | /// template<> |
2577 | /// constexpr float pi<float>; // variable template specialization pi<float> |
2578 | /// \endcode |
2579 | class VarTemplateSpecializationDecl : public VarDecl, |
2580 | public llvm::FoldingSetNode { |
2581 | |
2582 | /// Structure that stores information about a variable template |
2583 | /// specialization that was instantiated from a variable template partial |
2584 | /// specialization. |
2585 | struct SpecializedPartialSpecialization { |
2586 | /// The variable template partial specialization from which this |
2587 | /// variable template specialization was instantiated. |
2588 | VarTemplatePartialSpecializationDecl *PartialSpecialization; |
2589 | |
2590 | /// The template argument list deduced for the variable template |
2591 | /// partial specialization itself. |
2592 | const TemplateArgumentList *TemplateArgs; |
2593 | }; |
2594 | |
2595 | /// The template that this specialization specializes. |
2596 | llvm::PointerUnion<VarTemplateDecl *, SpecializedPartialSpecialization *> |
2597 | SpecializedTemplate; |
2598 | |
2599 | /// Further info for explicit template specialization/instantiation. |
2600 | struct ExplicitSpecializationInfo { |
2601 | /// The type-as-written. |
2602 | TypeSourceInfo *TypeAsWritten = nullptr; |
2603 | |
2604 | /// The location of the extern keyword. |
2605 | SourceLocation ExternLoc; |
2606 | |
2607 | /// The location of the template keyword. |
2608 | SourceLocation TemplateKeywordLoc; |
2609 | |
2610 | ExplicitSpecializationInfo() = default; |
2611 | }; |
2612 | |
2613 | /// Further info for explicit template specialization/instantiation. |
2614 | /// Does not apply to implicit specializations. |
2615 | ExplicitSpecializationInfo *ExplicitInfo = nullptr; |
2616 | |
2617 | /// The template arguments used to describe this specialization. |
2618 | const TemplateArgumentList *TemplateArgs; |
2619 | const ASTTemplateArgumentListInfo *TemplateArgsInfo = nullptr; |
2620 | |
2621 | /// The point where this template was instantiated (if any). |
2622 | SourceLocation PointOfInstantiation; |
2623 | |
2624 | /// The kind of specialization this declaration refers to. |
2625 | LLVM_PREFERRED_TYPE(TemplateSpecializationKind) |
2626 | unsigned SpecializationKind : 3; |
2627 | |
2628 | /// Whether this declaration is a complete definition of the |
2629 | /// variable template specialization. We can't otherwise tell apart |
2630 | /// an instantiated declaration from an instantiated definition with |
2631 | /// no initializer. |
2632 | LLVM_PREFERRED_TYPE(bool) |
2633 | unsigned IsCompleteDefinition : 1; |
2634 | |
2635 | protected: |
2636 | VarTemplateSpecializationDecl(Kind DK, ASTContext &Context, DeclContext *DC, |
2637 | SourceLocation StartLoc, SourceLocation IdLoc, |
2638 | VarTemplateDecl *SpecializedTemplate, |
2639 | QualType T, TypeSourceInfo *TInfo, |
2640 | StorageClass S, |
2641 | ArrayRef<TemplateArgument> Args); |
2642 | |
2643 | explicit VarTemplateSpecializationDecl(Kind DK, ASTContext &Context); |
2644 | |
2645 | public: |
2646 | friend class ASTDeclReader; |
2647 | friend class ASTDeclWriter; |
2648 | friend class VarDecl; |
2649 | |
2650 | static VarTemplateSpecializationDecl * |
2651 | Create(ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, |
2652 | SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T, |
2653 | TypeSourceInfo *TInfo, StorageClass S, |
2654 | ArrayRef<TemplateArgument> Args); |
2655 | static VarTemplateSpecializationDecl *CreateDeserialized(ASTContext &C, |
2656 | unsigned ID); |
2657 | |
2658 | void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy, |
2659 | bool Qualified) const override; |
2660 | |
2661 | VarTemplateSpecializationDecl *getMostRecentDecl() { |
2662 | VarDecl *Recent = static_cast<VarDecl *>(this)->getMostRecentDecl(); |
2663 | return cast<VarTemplateSpecializationDecl>(Val: Recent); |
2664 | } |
2665 | |
2666 | /// Retrieve the template that this specialization specializes. |
2667 | VarTemplateDecl *getSpecializedTemplate() const; |
2668 | |
2669 | /// Retrieve the template arguments of the variable template |
2670 | /// specialization. |
2671 | const TemplateArgumentList &getTemplateArgs() const { return *TemplateArgs; } |
2672 | |
2673 | // TODO: Always set this when creating the new specialization? |
2674 | void setTemplateArgsInfo(const TemplateArgumentListInfo &ArgsInfo); |
2675 | void setTemplateArgsInfo(const ASTTemplateArgumentListInfo *ArgsInfo); |
2676 | |
2677 | const ASTTemplateArgumentListInfo *getTemplateArgsInfo() const { |
2678 | return TemplateArgsInfo; |
2679 | } |
2680 | |
2681 | /// Determine the kind of specialization that this |
2682 | /// declaration represents. |
2683 | TemplateSpecializationKind getSpecializationKind() const { |
2684 | return static_cast<TemplateSpecializationKind>(SpecializationKind); |
2685 | } |
2686 | |
2687 | bool isExplicitSpecialization() const { |
2688 | return getSpecializationKind() == TSK_ExplicitSpecialization; |
2689 | } |
2690 | |
2691 | bool isClassScopeExplicitSpecialization() const { |
2692 | return isExplicitSpecialization() && |
2693 | isa<CXXRecordDecl>(getLexicalDeclContext()); |
2694 | } |
2695 | |
2696 | /// True if this declaration is an explicit specialization, |
2697 | /// explicit instantiation declaration, or explicit instantiation |
2698 | /// definition. |
2699 | bool isExplicitInstantiationOrSpecialization() const { |
2700 | return isTemplateExplicitInstantiationOrSpecialization( |
2701 | getTemplateSpecializationKind()); |
2702 | } |
2703 | |
2704 | void setSpecializationKind(TemplateSpecializationKind TSK) { |
2705 | SpecializationKind = TSK; |
2706 | } |
2707 | |
2708 | /// Get the point of instantiation (if any), or null if none. |
2709 | SourceLocation getPointOfInstantiation() const { |
2710 | return PointOfInstantiation; |
2711 | } |
2712 | |
2713 | void setPointOfInstantiation(SourceLocation Loc) { |
2714 | assert(Loc.isValid() && "point of instantiation must be valid!" ); |
2715 | PointOfInstantiation = Loc; |
2716 | } |
2717 | |
2718 | void setCompleteDefinition() { IsCompleteDefinition = true; } |
2719 | |
2720 | /// If this variable template specialization is an instantiation of |
2721 | /// a template (rather than an explicit specialization), return the |
2722 | /// variable template or variable template partial specialization from which |
2723 | /// it was instantiated. |
2724 | llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *> |
2725 | getInstantiatedFrom() const { |
2726 | if (!isTemplateInstantiation(Kind: getSpecializationKind())) |
2727 | return llvm::PointerUnion<VarTemplateDecl *, |
2728 | VarTemplatePartialSpecializationDecl *>(); |
2729 | |
2730 | return getSpecializedTemplateOrPartial(); |
2731 | } |
2732 | |
2733 | /// Retrieve the variable template or variable template partial |
2734 | /// specialization which was specialized by this. |
2735 | llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *> |
2736 | getSpecializedTemplateOrPartial() const { |
2737 | if (const auto *PartialSpec = |
2738 | SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>()) |
2739 | return PartialSpec->PartialSpecialization; |
2740 | |
2741 | return SpecializedTemplate.get<VarTemplateDecl *>(); |
2742 | } |
2743 | |
2744 | /// Retrieve the set of template arguments that should be used |
2745 | /// to instantiate the initializer of the variable template or variable |
2746 | /// template partial specialization from which this variable template |
2747 | /// specialization was instantiated. |
2748 | /// |
2749 | /// \returns For a variable template specialization instantiated from the |
2750 | /// primary template, this function will return the same template arguments |
2751 | /// as getTemplateArgs(). For a variable template specialization instantiated |
2752 | /// from a variable template partial specialization, this function will the |
2753 | /// return deduced template arguments for the variable template partial |
2754 | /// specialization itself. |
2755 | const TemplateArgumentList &getTemplateInstantiationArgs() const { |
2756 | if (const auto *PartialSpec = |
2757 | SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>()) |
2758 | return *PartialSpec->TemplateArgs; |
2759 | |
2760 | return getTemplateArgs(); |
2761 | } |
2762 | |
2763 | /// Note that this variable template specialization is actually an |
2764 | /// instantiation of the given variable template partial specialization whose |
2765 | /// template arguments have been deduced. |
2766 | void setInstantiationOf(VarTemplatePartialSpecializationDecl *PartialSpec, |
2767 | const TemplateArgumentList *TemplateArgs) { |
2768 | assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() && |
2769 | "Already set to a variable template partial specialization!" ); |
2770 | auto *PS = new (getASTContext()) SpecializedPartialSpecialization(); |
2771 | PS->PartialSpecialization = PartialSpec; |
2772 | PS->TemplateArgs = TemplateArgs; |
2773 | SpecializedTemplate = PS; |
2774 | } |
2775 | |
2776 | /// Note that this variable template specialization is an instantiation |
2777 | /// of the given variable template. |
2778 | void setInstantiationOf(VarTemplateDecl *TemplDecl) { |
2779 | assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() && |
2780 | "Previously set to a variable template partial specialization!" ); |
2781 | SpecializedTemplate = TemplDecl; |
2782 | } |
2783 | |
2784 | /// Sets the type of this specialization as it was written by |
2785 | /// the user. |
2786 | void setTypeAsWritten(TypeSourceInfo *T) { |
2787 | if (!ExplicitInfo) |
2788 | ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; |
2789 | ExplicitInfo->TypeAsWritten = T; |
2790 | } |
2791 | |
2792 | /// Gets the type of this specialization as it was written by |
2793 | /// the user, if it was so written. |
2794 | TypeSourceInfo *getTypeAsWritten() const { |
2795 | return ExplicitInfo ? ExplicitInfo->TypeAsWritten : nullptr; |
2796 | } |
2797 | |
2798 | /// Gets the location of the extern keyword, if present. |
2799 | SourceLocation getExternLoc() const { |
2800 | return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation(); |
2801 | } |
2802 | |
2803 | /// Sets the location of the extern keyword. |
2804 | void setExternLoc(SourceLocation Loc) { |
2805 | if (!ExplicitInfo) |
2806 | ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; |
2807 | ExplicitInfo->ExternLoc = Loc; |
2808 | } |
2809 | |
2810 | /// Sets the location of the template keyword. |
2811 | void setTemplateKeywordLoc(SourceLocation Loc) { |
2812 | if (!ExplicitInfo) |
2813 | ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; |
2814 | ExplicitInfo->TemplateKeywordLoc = Loc; |
2815 | } |
2816 | |
2817 | /// Gets the location of the template keyword, if present. |
2818 | SourceLocation getTemplateKeywordLoc() const { |
2819 | return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation(); |
2820 | } |
2821 | |
2822 | SourceRange getSourceRange() const override LLVM_READONLY; |
2823 | |
2824 | void Profile(llvm::FoldingSetNodeID &ID) const { |
2825 | Profile(ID, TemplateArgs->asArray(), getASTContext()); |
2826 | } |
2827 | |
2828 | static void Profile(llvm::FoldingSetNodeID &ID, |
2829 | ArrayRef<TemplateArgument> TemplateArgs, |
2830 | const ASTContext &Context) { |
2831 | ID.AddInteger(I: TemplateArgs.size()); |
2832 | for (const TemplateArgument &TemplateArg : TemplateArgs) |
2833 | TemplateArg.Profile(ID, Context); |
2834 | } |
2835 | |
2836 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
2837 | |
2838 | static bool classofKind(Kind K) { |
2839 | return K >= firstVarTemplateSpecialization && |
2840 | K <= lastVarTemplateSpecialization; |
2841 | } |
2842 | }; |
2843 | |
2844 | class VarTemplatePartialSpecializationDecl |
2845 | : public VarTemplateSpecializationDecl { |
2846 | /// The list of template parameters |
2847 | TemplateParameterList *TemplateParams = nullptr; |
2848 | |
2849 | /// The source info for the template arguments as written. |
2850 | /// FIXME: redundant with TypeAsWritten? |
2851 | const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr; |
2852 | |
2853 | /// The variable template partial specialization from which this |
2854 | /// variable template partial specialization was instantiated. |
2855 | /// |
2856 | /// The boolean value will be true to indicate that this variable template |
2857 | /// partial specialization was specialized at this level. |
2858 | llvm::PointerIntPair<VarTemplatePartialSpecializationDecl *, 1, bool> |
2859 | InstantiatedFromMember; |
2860 | |
2861 | VarTemplatePartialSpecializationDecl( |
2862 | ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, |
2863 | SourceLocation IdLoc, TemplateParameterList *Params, |
2864 | VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo, |
2865 | StorageClass S, ArrayRef<TemplateArgument> Args, |
2866 | const ASTTemplateArgumentListInfo *ArgInfos); |
2867 | |
2868 | VarTemplatePartialSpecializationDecl(ASTContext &Context) |
2869 | : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization, |
2870 | Context), |
2871 | InstantiatedFromMember(nullptr, false) {} |
2872 | |
2873 | void anchor() override; |
2874 | |
2875 | public: |
2876 | friend class ASTDeclReader; |
2877 | friend class ASTDeclWriter; |
2878 | |
2879 | static VarTemplatePartialSpecializationDecl * |
2880 | Create(ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, |
2881 | SourceLocation IdLoc, TemplateParameterList *Params, |
2882 | VarTemplateDecl *SpecializedTemplate, QualType T, |
2883 | TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args, |
2884 | const TemplateArgumentListInfo &ArgInfos); |
2885 | |
2886 | static VarTemplatePartialSpecializationDecl *CreateDeserialized(ASTContext &C, |
2887 | unsigned ID); |
2888 | |
2889 | VarTemplatePartialSpecializationDecl *getMostRecentDecl() { |
2890 | return cast<VarTemplatePartialSpecializationDecl>( |
2891 | static_cast<VarTemplateSpecializationDecl *>( |
2892 | this)->getMostRecentDecl()); |
2893 | } |
2894 | |
2895 | /// Get the list of template parameters |
2896 | TemplateParameterList *getTemplateParameters() const { |
2897 | return TemplateParams; |
2898 | } |
2899 | |
2900 | /// Get the template arguments as written. |
2901 | const ASTTemplateArgumentListInfo *getTemplateArgsAsWritten() const { |
2902 | return ArgsAsWritten; |
2903 | } |
2904 | |
2905 | /// \brief All associated constraints of this partial specialization, |
2906 | /// including the requires clause and any constraints derived from |
2907 | /// constrained-parameters. |
2908 | /// |
2909 | /// The constraints in the resulting list are to be treated as if in a |
2910 | /// conjunction ("and"). |
2911 | void getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const { |
2912 | TemplateParams->getAssociatedConstraints(AC); |
2913 | } |
2914 | |
2915 | bool hasAssociatedConstraints() const { |
2916 | return TemplateParams->hasAssociatedConstraints(); |
2917 | } |
2918 | |
2919 | /// \brief Retrieve the member variable template partial specialization from |
2920 | /// which this particular variable template partial specialization was |
2921 | /// instantiated. |
2922 | /// |
2923 | /// \code |
2924 | /// template<typename T> |
2925 | /// struct Outer { |
2926 | /// template<typename U> U Inner; |
2927 | /// template<typename U> U* Inner<U*> = (U*)(0); // #1 |
2928 | /// }; |
2929 | /// |
2930 | /// template int* Outer<float>::Inner<int*>; |
2931 | /// \endcode |
2932 | /// |
2933 | /// In this example, the instantiation of \c Outer<float>::Inner<int*> will |
2934 | /// end up instantiating the partial specialization |
2935 | /// \c Outer<float>::Inner<U*>, which itself was instantiated from the |
2936 | /// variable template partial specialization \c Outer<T>::Inner<U*>. Given |
2937 | /// \c Outer<float>::Inner<U*>, this function would return |
2938 | /// \c Outer<T>::Inner<U*>. |
2939 | VarTemplatePartialSpecializationDecl *getInstantiatedFromMember() const { |
2940 | const auto *First = |
2941 | cast<VarTemplatePartialSpecializationDecl>(getFirstDecl()); |
2942 | return First->InstantiatedFromMember.getPointer(); |
2943 | } |
2944 | |
2945 | void |
2946 | setInstantiatedFromMember(VarTemplatePartialSpecializationDecl *PartialSpec) { |
2947 | auto *First = cast<VarTemplatePartialSpecializationDecl>(getFirstDecl()); |
2948 | First->InstantiatedFromMember.setPointer(PartialSpec); |
2949 | } |
2950 | |
2951 | /// Determines whether this variable template partial specialization |
2952 | /// was a specialization of a member partial specialization. |
2953 | /// |
2954 | /// In the following example, the member template partial specialization |
2955 | /// \c X<int>::Inner<T*> is a member specialization. |
2956 | /// |
2957 | /// \code |
2958 | /// template<typename T> |
2959 | /// struct X { |
2960 | /// template<typename U> U Inner; |
2961 | /// template<typename U> U* Inner<U*> = (U*)(0); |
2962 | /// }; |
2963 | /// |
2964 | /// template<> template<typename T> |
2965 | /// U* X<int>::Inner<T*> = (T*)(0) + 1; |
2966 | /// \endcode |
2967 | bool isMemberSpecialization() { |
2968 | const auto *First = |
2969 | cast<VarTemplatePartialSpecializationDecl>(getFirstDecl()); |
2970 | return First->InstantiatedFromMember.getInt(); |
2971 | } |
2972 | |
2973 | /// Note that this member template is a specialization. |
2974 | void setMemberSpecialization() { |
2975 | auto *First = cast<VarTemplatePartialSpecializationDecl>(getFirstDecl()); |
2976 | assert(First->InstantiatedFromMember.getPointer() && |
2977 | "Only member templates can be member template specializations" ); |
2978 | return First->InstantiatedFromMember.setInt(true); |
2979 | } |
2980 | |
2981 | SourceRange getSourceRange() const override LLVM_READONLY; |
2982 | |
2983 | void Profile(llvm::FoldingSetNodeID &ID) const { |
2984 | Profile(ID, getTemplateArgs().asArray(), getTemplateParameters(), |
2985 | getASTContext()); |
2986 | } |
2987 | |
2988 | static void |
2989 | Profile(llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs, |
2990 | TemplateParameterList *TPL, const ASTContext &Context); |
2991 | |
2992 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
2993 | |
2994 | static bool classofKind(Kind K) { |
2995 | return K == VarTemplatePartialSpecialization; |
2996 | } |
2997 | }; |
2998 | |
2999 | /// Declaration of a variable template. |
3000 | class VarTemplateDecl : public RedeclarableTemplateDecl { |
3001 | protected: |
3002 | /// Data that is common to all of the declarations of a given |
3003 | /// variable template. |
3004 | struct Common : CommonBase { |
3005 | /// The variable template specializations for this variable |
3006 | /// template, including explicit specializations and instantiations. |
3007 | llvm::FoldingSetVector<VarTemplateSpecializationDecl> Specializations; |
3008 | |
3009 | /// The variable template partial specializations for this variable |
3010 | /// template. |
3011 | llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> |
3012 | PartialSpecializations; |
3013 | |
3014 | Common() = default; |
3015 | }; |
3016 | |
3017 | /// Retrieve the set of specializations of this variable template. |
3018 | llvm::FoldingSetVector<VarTemplateSpecializationDecl> & |
3019 | getSpecializations() const; |
3020 | |
3021 | /// Retrieve the set of partial specializations of this class |
3022 | /// template. |
3023 | llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> & |
3024 | getPartialSpecializations() const; |
3025 | |
3026 | VarTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L, |
3027 | DeclarationName Name, TemplateParameterList *Params, |
3028 | NamedDecl *Decl) |
3029 | : RedeclarableTemplateDecl(VarTemplate, C, DC, L, Name, Params, Decl) {} |
3030 | |
3031 | CommonBase *newCommon(ASTContext &C) const override; |
3032 | |
3033 | Common *getCommonPtr() const { |
3034 | return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr()); |
3035 | } |
3036 | |
3037 | public: |
3038 | friend class ASTDeclReader; |
3039 | friend class ASTDeclWriter; |
3040 | |
3041 | /// Load any lazily-loaded specializations from the external source. |
3042 | void LoadLazySpecializations() const; |
3043 | |
3044 | /// Get the underlying variable declarations of the template. |
3045 | VarDecl *getTemplatedDecl() const { |
3046 | return static_cast<VarDecl *>(TemplatedDecl); |
3047 | } |
3048 | |
3049 | /// Returns whether this template declaration defines the primary |
3050 | /// variable pattern. |
3051 | bool isThisDeclarationADefinition() const { |
3052 | return getTemplatedDecl()->isThisDeclarationADefinition(); |
3053 | } |
3054 | |
3055 | VarTemplateDecl *getDefinition(); |
3056 | |
3057 | /// Create a variable template node. |
3058 | static VarTemplateDecl *Create(ASTContext &C, DeclContext *DC, |
3059 | SourceLocation L, DeclarationName Name, |
3060 | TemplateParameterList *Params, |
3061 | VarDecl *Decl); |
3062 | |
3063 | /// Create an empty variable template node. |
3064 | static VarTemplateDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3065 | |
3066 | /// Return the specialization with the provided arguments if it exists, |
3067 | /// otherwise return the insertion point. |
3068 | VarTemplateSpecializationDecl * |
3069 | findSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos); |
3070 | |
3071 | /// Insert the specified specialization knowing that it is not already |
3072 | /// in. InsertPos must be obtained from findSpecialization. |
3073 | void AddSpecialization(VarTemplateSpecializationDecl *D, void *InsertPos); |
3074 | |
3075 | VarTemplateDecl *getCanonicalDecl() override { |
3076 | return cast<VarTemplateDecl>(RedeclarableTemplateDecl::getCanonicalDecl()); |
3077 | } |
3078 | const VarTemplateDecl *getCanonicalDecl() const { |
3079 | return cast<VarTemplateDecl>(RedeclarableTemplateDecl::getCanonicalDecl()); |
3080 | } |
3081 | |
3082 | /// Retrieve the previous declaration of this variable template, or |
3083 | /// nullptr if no such declaration exists. |
3084 | VarTemplateDecl *getPreviousDecl() { |
3085 | return cast_or_null<VarTemplateDecl>( |
3086 | static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl()); |
3087 | } |
3088 | const VarTemplateDecl *getPreviousDecl() const { |
3089 | return cast_or_null<VarTemplateDecl>( |
3090 | static_cast<const RedeclarableTemplateDecl *>( |
3091 | this)->getPreviousDecl()); |
3092 | } |
3093 | |
3094 | VarTemplateDecl *getMostRecentDecl() { |
3095 | return cast<VarTemplateDecl>( |
3096 | static_cast<RedeclarableTemplateDecl *>(this)->getMostRecentDecl()); |
3097 | } |
3098 | const VarTemplateDecl *getMostRecentDecl() const { |
3099 | return const_cast<VarTemplateDecl *>(this)->getMostRecentDecl(); |
3100 | } |
3101 | |
3102 | VarTemplateDecl *getInstantiatedFromMemberTemplate() const { |
3103 | return cast_or_null<VarTemplateDecl>( |
3104 | RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate()); |
3105 | } |
3106 | |
3107 | /// Return the partial specialization with the provided arguments if it |
3108 | /// exists, otherwise return the insertion point. |
3109 | VarTemplatePartialSpecializationDecl * |
3110 | findPartialSpecialization(ArrayRef<TemplateArgument> Args, |
3111 | TemplateParameterList *TPL, void *&InsertPos); |
3112 | |
3113 | /// Insert the specified partial specialization knowing that it is not |
3114 | /// already in. InsertPos must be obtained from findPartialSpecialization. |
3115 | void AddPartialSpecialization(VarTemplatePartialSpecializationDecl *D, |
3116 | void *InsertPos); |
3117 | |
3118 | /// Retrieve the partial specializations as an ordered list. |
3119 | void getPartialSpecializations( |
3120 | SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) const; |
3121 | |
3122 | /// Find a variable template partial specialization which was |
3123 | /// instantiated |
3124 | /// from the given member partial specialization. |
3125 | /// |
3126 | /// \param D a member variable template partial specialization. |
3127 | /// |
3128 | /// \returns the variable template partial specialization which was |
3129 | /// instantiated |
3130 | /// from the given member partial specialization, or nullptr if no such |
3131 | /// partial specialization exists. |
3132 | VarTemplatePartialSpecializationDecl *findPartialSpecInstantiatedFromMember( |
3133 | VarTemplatePartialSpecializationDecl *D); |
3134 | |
3135 | using spec_iterator = SpecIterator<VarTemplateSpecializationDecl>; |
3136 | using spec_range = llvm::iterator_range<spec_iterator>; |
3137 | |
3138 | spec_range specializations() const { |
3139 | return spec_range(spec_begin(), spec_end()); |
3140 | } |
3141 | |
3142 | spec_iterator spec_begin() const { |
3143 | return makeSpecIterator(Specs&: getSpecializations(), isEnd: false); |
3144 | } |
3145 | |
3146 | spec_iterator spec_end() const { |
3147 | return makeSpecIterator(Specs&: getSpecializations(), isEnd: true); |
3148 | } |
3149 | |
3150 | // Implement isa/cast/dyncast support |
3151 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
3152 | static bool classofKind(Kind K) { return K == VarTemplate; } |
3153 | }; |
3154 | |
3155 | /// Declaration of a C++20 concept. |
3156 | class ConceptDecl : public TemplateDecl, public Mergeable<ConceptDecl> { |
3157 | protected: |
3158 | Expr *ConstraintExpr; |
3159 | |
3160 | ConceptDecl(DeclContext *DC, SourceLocation L, DeclarationName Name, |
3161 | TemplateParameterList *Params, Expr *ConstraintExpr) |
3162 | : TemplateDecl(Concept, DC, L, Name, Params), |
3163 | ConstraintExpr(ConstraintExpr) {}; |
3164 | public: |
3165 | static ConceptDecl *Create(ASTContext &C, DeclContext *DC, |
3166 | SourceLocation L, DeclarationName Name, |
3167 | TemplateParameterList *Params, |
3168 | Expr *ConstraintExpr); |
3169 | static ConceptDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3170 | |
3171 | Expr *getConstraintExpr() const { |
3172 | return ConstraintExpr; |
3173 | } |
3174 | |
3175 | SourceRange getSourceRange() const override LLVM_READONLY { |
3176 | return SourceRange(getTemplateParameters()->getTemplateLoc(), |
3177 | ConstraintExpr->getEndLoc()); |
3178 | } |
3179 | |
3180 | bool isTypeConcept() const { |
3181 | return isa<TemplateTypeParmDecl>(getTemplateParameters()->getParam(0)); |
3182 | } |
3183 | |
3184 | ConceptDecl *getCanonicalDecl() override { |
3185 | return cast<ConceptDecl>(Val: getPrimaryMergedDecl(this)); |
3186 | } |
3187 | const ConceptDecl *getCanonicalDecl() const { |
3188 | return const_cast<ConceptDecl *>(this)->getCanonicalDecl(); |
3189 | } |
3190 | |
3191 | // Implement isa/cast/dyncast/etc. |
3192 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
3193 | static bool classofKind(Kind K) { return K == Concept; } |
3194 | |
3195 | friend class ASTReader; |
3196 | friend class ASTDeclReader; |
3197 | friend class ASTDeclWriter; |
3198 | }; |
3199 | |
3200 | // An implementation detail of ConceptSpecialicationExpr that holds the template |
3201 | // arguments, so we can later use this to reconstitute the template arguments |
3202 | // during constraint checking. |
3203 | class ImplicitConceptSpecializationDecl final |
3204 | : public Decl, |
3205 | private llvm::TrailingObjects<ImplicitConceptSpecializationDecl, |
3206 | TemplateArgument> { |
3207 | unsigned NumTemplateArgs; |
3208 | |
3209 | ImplicitConceptSpecializationDecl(DeclContext *DC, SourceLocation SL, |
3210 | ArrayRef<TemplateArgument> ConvertedArgs); |
3211 | ImplicitConceptSpecializationDecl(EmptyShell Empty, unsigned NumTemplateArgs); |
3212 | |
3213 | public: |
3214 | static ImplicitConceptSpecializationDecl * |
3215 | Create(const ASTContext &C, DeclContext *DC, SourceLocation SL, |
3216 | ArrayRef<TemplateArgument> ConvertedArgs); |
3217 | static ImplicitConceptSpecializationDecl * |
3218 | CreateDeserialized(const ASTContext &C, unsigned ID, |
3219 | unsigned NumTemplateArgs); |
3220 | |
3221 | ArrayRef<TemplateArgument> getTemplateArguments() const { |
3222 | return ArrayRef<TemplateArgument>(getTrailingObjects<TemplateArgument>(), |
3223 | NumTemplateArgs); |
3224 | } |
3225 | void setTemplateArguments(ArrayRef<TemplateArgument> Converted); |
3226 | |
3227 | static bool classofKind(Kind K) { return K == ImplicitConceptSpecialization; } |
3228 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
3229 | |
3230 | friend TrailingObjects; |
3231 | friend class ASTDeclReader; |
3232 | }; |
3233 | |
3234 | /// A template parameter object. |
3235 | /// |
3236 | /// Template parameter objects represent values of class type used as template |
3237 | /// arguments. There is one template parameter object for each such distinct |
3238 | /// value used as a template argument across the program. |
3239 | /// |
3240 | /// \code |
3241 | /// struct A { int x, y; }; |
3242 | /// template<A> struct S; |
3243 | /// S<A{1, 2}> s1; |
3244 | /// S<A{1, 2}> s2; // same type, argument is same TemplateParamObjectDecl. |
3245 | /// \endcode |
3246 | class TemplateParamObjectDecl : public ValueDecl, |
3247 | public Mergeable<TemplateParamObjectDecl>, |
3248 | public llvm::FoldingSetNode { |
3249 | private: |
3250 | /// The value of this template parameter object. |
3251 | APValue Value; |
3252 | |
3253 | TemplateParamObjectDecl(DeclContext *DC, QualType T, const APValue &V) |
3254 | : ValueDecl(TemplateParamObject, DC, SourceLocation(), DeclarationName(), |
3255 | T), |
3256 | Value(V) {} |
3257 | |
3258 | static TemplateParamObjectDecl *Create(const ASTContext &C, QualType T, |
3259 | const APValue &V); |
3260 | static TemplateParamObjectDecl *CreateDeserialized(ASTContext &C, |
3261 | unsigned ID); |
3262 | |
3263 | /// Only ASTContext::getTemplateParamObjectDecl and deserialization |
3264 | /// create these. |
3265 | friend class ASTContext; |
3266 | friend class ASTReader; |
3267 | friend class ASTDeclReader; |
3268 | |
3269 | public: |
3270 | /// Print this template parameter object in a human-readable format. |
3271 | void printName(llvm::raw_ostream &OS, |
3272 | const PrintingPolicy &Policy) const override; |
3273 | |
3274 | /// Print this object as an equivalent expression. |
3275 | void printAsExpr(llvm::raw_ostream &OS) const; |
3276 | void printAsExpr(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const; |
3277 | |
3278 | /// Print this object as an initializer suitable for a variable of the |
3279 | /// object's type. |
3280 | void printAsInit(llvm::raw_ostream &OS) const; |
3281 | void printAsInit(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const; |
3282 | |
3283 | const APValue &getValue() const { return Value; } |
3284 | |
3285 | static void Profile(llvm::FoldingSetNodeID &ID, QualType T, |
3286 | const APValue &V) { |
3287 | ID.AddPointer(Ptr: T.getCanonicalType().getAsOpaquePtr()); |
3288 | V.Profile(ID); |
3289 | } |
3290 | void Profile(llvm::FoldingSetNodeID &ID) { |
3291 | Profile(ID, getType(), getValue()); |
3292 | } |
3293 | |
3294 | TemplateParamObjectDecl *getCanonicalDecl() override { |
3295 | return getFirstDecl(); |
3296 | } |
3297 | const TemplateParamObjectDecl *getCanonicalDecl() const { |
3298 | return getFirstDecl(); |
3299 | } |
3300 | |
3301 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
3302 | static bool classofKind(Kind K) { return K == TemplateParamObject; } |
3303 | }; |
3304 | |
3305 | inline NamedDecl *getAsNamedDecl(TemplateParameter P) { |
3306 | if (auto *PD = P.dyn_cast<TemplateTypeParmDecl *>()) |
3307 | return PD; |
3308 | if (auto *PD = P.dyn_cast<NonTypeTemplateParmDecl *>()) |
3309 | return PD; |
3310 | return P.get<TemplateTemplateParmDecl *>(); |
3311 | } |
3312 | |
3313 | inline TemplateDecl *getAsTypeTemplateDecl(Decl *D) { |
3314 | auto *TD = dyn_cast<TemplateDecl>(Val: D); |
3315 | return TD && (isa<ClassTemplateDecl>(Val: TD) || |
3316 | isa<ClassTemplatePartialSpecializationDecl>(Val: TD) || |
3317 | isa<TypeAliasTemplateDecl>(Val: TD) || |
3318 | isa<TemplateTemplateParmDecl>(Val: TD)) |
3319 | ? TD |
3320 | : nullptr; |
3321 | } |
3322 | |
3323 | /// Check whether the template parameter is a pack expansion, and if so, |
3324 | /// determine the number of parameters produced by that expansion. For instance: |
3325 | /// |
3326 | /// \code |
3327 | /// template<typename ...Ts> struct A { |
3328 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
3329 | /// }; |
3330 | /// \endcode |
3331 | /// |
3332 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
3333 | /// is not a pack expansion, so returns an empty Optional. |
3334 | inline std::optional<unsigned> getExpandedPackSize(const NamedDecl *Param) { |
3335 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Val: Param)) { |
3336 | if (TTP->isExpandedParameterPack()) |
3337 | return TTP->getNumExpansionParameters(); |
3338 | } |
3339 | |
3340 | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Val: Param)) { |
3341 | if (NTTP->isExpandedParameterPack()) |
3342 | return NTTP->getNumExpansionTypes(); |
3343 | } |
3344 | |
3345 | if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Val: Param)) { |
3346 | if (TTP->isExpandedParameterPack()) |
3347 | return TTP->getNumExpansionTemplateParameters(); |
3348 | } |
3349 | |
3350 | return std::nullopt; |
3351 | } |
3352 | |
3353 | /// Internal helper used by Subst* nodes to retrieve the parameter list |
3354 | /// for their AssociatedDecl. |
3355 | TemplateParameterList *getReplacedTemplateParameterList(Decl *D); |
3356 | |
3357 | } // namespace clang |
3358 | |
3359 | #endif // LLVM_CLANG_AST_DECLTEMPLATE_H |
3360 | |