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 | Decl::DeclID *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, DeclID 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 | DeclID ID); |
1208 | static TemplateTypeParmDecl *CreateDeserialized(const ASTContext &C, |
1209 | DeclID 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 | const 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 | const 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, const 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, const IdentifierInfo *Id, |
1413 | QualType T, TypeSourceInfo *TInfo, ArrayRef<QualType> ExpandedTypes, |
1414 | ArrayRef<TypeSourceInfo *> ExpandedTInfos); |
1415 | |
1416 | static NonTypeTemplateParmDecl *CreateDeserialized(ASTContext &C, |
1417 | DeclID ID, |
1418 | bool HasTypeConstraint); |
1419 | static NonTypeTemplateParmDecl *CreateDeserialized(ASTContext &C, |
1420 | DeclID 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 template template parameter was declaration with |
1585 | /// the 'typename' keyword. |
1586 | /// |
1587 | /// If false, it was declared with the 'class' keyword. |
1588 | LLVM_PREFERRED_TYPE(bool) |
1589 | unsigned Typename : 1; |
1590 | |
1591 | /// Whether this parameter is a parameter pack. |
1592 | LLVM_PREFERRED_TYPE(bool) |
1593 | unsigned ParameterPack : 1; |
1594 | |
1595 | /// Whether this template template parameter is an "expanded" |
1596 | /// parameter pack, meaning that it is a pack expansion and we |
1597 | /// already know the set of template parameters that expansion expands to. |
1598 | LLVM_PREFERRED_TYPE(bool) |
1599 | unsigned ExpandedParameterPack : 1; |
1600 | |
1601 | /// The number of parameters in an expanded parameter pack. |
1602 | unsigned NumExpandedParams = 0; |
1603 | |
1604 | TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L, unsigned D, |
1605 | unsigned P, bool ParameterPack, IdentifierInfo *Id, |
1606 | bool Typename, TemplateParameterList *Params) |
1607 | : TemplateDecl(TemplateTemplateParm, DC, L, Id, Params), |
1608 | TemplateParmPosition(D, P), Typename(Typename), |
1609 | ParameterPack(ParameterPack), ExpandedParameterPack(false) {} |
1610 | |
1611 | TemplateTemplateParmDecl(DeclContext *DC, SourceLocation L, unsigned D, |
1612 | unsigned P, IdentifierInfo *Id, bool Typename, |
1613 | TemplateParameterList *Params, |
1614 | ArrayRef<TemplateParameterList *> Expansions); |
1615 | |
1616 | void anchor() override; |
1617 | |
1618 | public: |
1619 | friend class ASTDeclReader; |
1620 | friend class ASTDeclWriter; |
1621 | friend TrailingObjects; |
1622 | |
1623 | static TemplateTemplateParmDecl *Create(const ASTContext &C, DeclContext *DC, |
1624 | SourceLocation L, unsigned D, |
1625 | unsigned P, bool ParameterPack, |
1626 | IdentifierInfo *Id, bool Typename, |
1627 | TemplateParameterList *Params); |
1628 | static TemplateTemplateParmDecl * |
1629 | Create(const ASTContext &C, DeclContext *DC, SourceLocation L, unsigned D, |
1630 | unsigned P, IdentifierInfo *Id, bool Typename, |
1631 | TemplateParameterList *Params, |
1632 | ArrayRef<TemplateParameterList *> Expansions); |
1633 | |
1634 | static TemplateTemplateParmDecl *CreateDeserialized(ASTContext &C, |
1635 | DeclID ID); |
1636 | static TemplateTemplateParmDecl *CreateDeserialized(ASTContext &C, |
1637 | DeclID ID, |
1638 | unsigned NumExpansions); |
1639 | |
1640 | using TemplateParmPosition::getDepth; |
1641 | using TemplateParmPosition::setDepth; |
1642 | using TemplateParmPosition::getPosition; |
1643 | using TemplateParmPosition::setPosition; |
1644 | using TemplateParmPosition::getIndex; |
1645 | |
1646 | /// Whether this template template parameter was declared with |
1647 | /// the 'typename' keyword. |
1648 | bool wasDeclaredWithTypename() const { return Typename; } |
1649 | |
1650 | /// Set whether this template template parameter was declared with |
1651 | /// the 'typename' or 'class' keyword. |
1652 | void setDeclaredWithTypename(bool withTypename) { Typename = withTypename; } |
1653 | |
1654 | /// Whether this template template parameter is a template |
1655 | /// parameter pack. |
1656 | /// |
1657 | /// \code |
1658 | /// template<template <class T> ...MetaFunctions> struct Apply; |
1659 | /// \endcode |
1660 | bool isParameterPack() const { return ParameterPack; } |
1661 | |
1662 | /// Whether this parameter pack is a pack expansion. |
1663 | /// |
1664 | /// A template template parameter pack is a pack expansion if its template |
1665 | /// parameter list contains an unexpanded parameter pack. |
1666 | bool isPackExpansion() const { |
1667 | return ParameterPack && |
1668 | getTemplateParameters()->containsUnexpandedParameterPack(); |
1669 | } |
1670 | |
1671 | /// Whether this parameter is a template template parameter pack that |
1672 | /// has a known list of different template parameter lists at different |
1673 | /// positions. |
1674 | /// |
1675 | /// A parameter pack is an expanded parameter pack when the original parameter |
1676 | /// pack's template parameter list was itself a pack expansion, and that |
1677 | /// expansion has already been expanded. For exampe, given: |
1678 | /// |
1679 | /// \code |
1680 | /// template<typename...Types> struct Outer { |
1681 | /// template<template<Types> class...Templates> struct Inner; |
1682 | /// }; |
1683 | /// \endcode |
1684 | /// |
1685 | /// The parameter pack \c Templates is a pack expansion, which expands the |
1686 | /// pack \c Types. When \c Types is supplied with template arguments by |
1687 | /// instantiating \c Outer, the instantiation of \c Templates is an expanded |
1688 | /// parameter pack. |
1689 | bool isExpandedParameterPack() const { return ExpandedParameterPack; } |
1690 | |
1691 | /// Retrieves the number of expansion template parameters in |
1692 | /// an expanded parameter pack. |
1693 | unsigned getNumExpansionTemplateParameters() const { |
1694 | assert(ExpandedParameterPack && "Not an expansion parameter pack" ); |
1695 | return NumExpandedParams; |
1696 | } |
1697 | |
1698 | /// Retrieve a particular expansion type within an expanded parameter |
1699 | /// pack. |
1700 | TemplateParameterList *getExpansionTemplateParameters(unsigned I) const { |
1701 | assert(I < NumExpandedParams && "Out-of-range expansion type index" ); |
1702 | return getTrailingObjects<TemplateParameterList *>()[I]; |
1703 | } |
1704 | |
1705 | const DefArgStorage &getDefaultArgStorage() const { return DefaultArgument; } |
1706 | |
1707 | /// Determine whether this template parameter has a default |
1708 | /// argument. |
1709 | bool hasDefaultArgument() const { return DefaultArgument.isSet(); } |
1710 | |
1711 | /// Retrieve the default argument, if any. |
1712 | const TemplateArgumentLoc &getDefaultArgument() const { |
1713 | static const TemplateArgumentLoc NoneLoc; |
1714 | return DefaultArgument.isSet() ? *DefaultArgument.get() : NoneLoc; |
1715 | } |
1716 | |
1717 | /// Retrieve the location of the default argument, if any. |
1718 | SourceLocation getDefaultArgumentLoc() const; |
1719 | |
1720 | /// Determines whether the default argument was inherited |
1721 | /// from a previous declaration of this template. |
1722 | bool defaultArgumentWasInherited() const { |
1723 | return DefaultArgument.isInherited(); |
1724 | } |
1725 | |
1726 | /// Set the default argument for this template parameter, and |
1727 | /// whether that default argument was inherited from another |
1728 | /// declaration. |
1729 | void setDefaultArgument(const ASTContext &C, |
1730 | const TemplateArgumentLoc &DefArg); |
1731 | void setInheritedDefaultArgument(const ASTContext &C, |
1732 | TemplateTemplateParmDecl *Prev) { |
1733 | DefaultArgument.setInherited(C, InheritedFrom: Prev); |
1734 | } |
1735 | |
1736 | /// Removes the default argument of this template parameter. |
1737 | void removeDefaultArgument() { DefaultArgument.clear(); } |
1738 | |
1739 | SourceRange getSourceRange() const override LLVM_READONLY { |
1740 | SourceLocation End = getLocation(); |
1741 | if (hasDefaultArgument() && !defaultArgumentWasInherited()) |
1742 | End = getDefaultArgument().getSourceRange().getEnd(); |
1743 | return SourceRange(getTemplateParameters()->getTemplateLoc(), End); |
1744 | } |
1745 | |
1746 | // Implement isa/cast/dyncast/etc. |
1747 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
1748 | static bool classofKind(Kind K) { return K == TemplateTemplateParm; } |
1749 | }; |
1750 | |
1751 | /// Represents the builtin template declaration which is used to |
1752 | /// implement __make_integer_seq and other builtin templates. It serves |
1753 | /// no real purpose beyond existing as a place to hold template parameters. |
1754 | class BuiltinTemplateDecl : public TemplateDecl { |
1755 | BuiltinTemplateKind BTK; |
1756 | |
1757 | BuiltinTemplateDecl(const ASTContext &C, DeclContext *DC, |
1758 | DeclarationName Name, BuiltinTemplateKind BTK); |
1759 | |
1760 | void anchor() override; |
1761 | |
1762 | public: |
1763 | // Implement isa/cast/dyncast support |
1764 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
1765 | static bool classofKind(Kind K) { return K == BuiltinTemplate; } |
1766 | |
1767 | static BuiltinTemplateDecl *Create(const ASTContext &C, DeclContext *DC, |
1768 | DeclarationName Name, |
1769 | BuiltinTemplateKind BTK) { |
1770 | return new (C, DC) BuiltinTemplateDecl(C, DC, Name, BTK); |
1771 | } |
1772 | |
1773 | SourceRange getSourceRange() const override LLVM_READONLY { |
1774 | return {}; |
1775 | } |
1776 | |
1777 | BuiltinTemplateKind getBuiltinTemplateKind() const { return BTK; } |
1778 | }; |
1779 | |
1780 | /// Represents a class template specialization, which refers to |
1781 | /// a class template with a given set of template arguments. |
1782 | /// |
1783 | /// Class template specializations represent both explicit |
1784 | /// specialization of class templates, as in the example below, and |
1785 | /// implicit instantiations of class templates. |
1786 | /// |
1787 | /// \code |
1788 | /// template<typename T> class array; |
1789 | /// |
1790 | /// template<> |
1791 | /// class array<bool> { }; // class template specialization array<bool> |
1792 | /// \endcode |
1793 | class ClassTemplateSpecializationDecl |
1794 | : public CXXRecordDecl, public llvm::FoldingSetNode { |
1795 | /// Structure that stores information about a class template |
1796 | /// specialization that was instantiated from a class template partial |
1797 | /// specialization. |
1798 | struct SpecializedPartialSpecialization { |
1799 | /// The class template partial specialization from which this |
1800 | /// class template specialization was instantiated. |
1801 | ClassTemplatePartialSpecializationDecl *PartialSpecialization; |
1802 | |
1803 | /// The template argument list deduced for the class template |
1804 | /// partial specialization itself. |
1805 | const TemplateArgumentList *TemplateArgs; |
1806 | }; |
1807 | |
1808 | /// The template that this specialization specializes |
1809 | llvm::PointerUnion<ClassTemplateDecl *, SpecializedPartialSpecialization *> |
1810 | SpecializedTemplate; |
1811 | |
1812 | /// Further info for explicit template specialization/instantiation. |
1813 | struct ExplicitSpecializationInfo { |
1814 | /// The type-as-written. |
1815 | TypeSourceInfo *TypeAsWritten = nullptr; |
1816 | |
1817 | /// The location of the extern keyword. |
1818 | SourceLocation ExternLoc; |
1819 | |
1820 | /// The location of the template keyword. |
1821 | SourceLocation TemplateKeywordLoc; |
1822 | |
1823 | ExplicitSpecializationInfo() = default; |
1824 | }; |
1825 | |
1826 | /// Further info for explicit template specialization/instantiation. |
1827 | /// Does not apply to implicit specializations. |
1828 | ExplicitSpecializationInfo *ExplicitInfo = nullptr; |
1829 | |
1830 | /// The template arguments used to describe this specialization. |
1831 | const TemplateArgumentList *TemplateArgs; |
1832 | |
1833 | /// The point where this template was instantiated (if any) |
1834 | SourceLocation PointOfInstantiation; |
1835 | |
1836 | /// The kind of specialization this declaration refers to. |
1837 | LLVM_PREFERRED_TYPE(TemplateSpecializationKind) |
1838 | unsigned SpecializationKind : 3; |
1839 | |
1840 | protected: |
1841 | ClassTemplateSpecializationDecl(ASTContext &Context, Kind DK, TagKind TK, |
1842 | DeclContext *DC, SourceLocation StartLoc, |
1843 | SourceLocation IdLoc, |
1844 | ClassTemplateDecl *SpecializedTemplate, |
1845 | ArrayRef<TemplateArgument> Args, |
1846 | ClassTemplateSpecializationDecl *PrevDecl); |
1847 | |
1848 | explicit ClassTemplateSpecializationDecl(ASTContext &C, Kind DK); |
1849 | |
1850 | public: |
1851 | friend class ASTDeclReader; |
1852 | friend class ASTDeclWriter; |
1853 | |
1854 | static ClassTemplateSpecializationDecl * |
1855 | Create(ASTContext &Context, TagKind TK, DeclContext *DC, |
1856 | SourceLocation StartLoc, SourceLocation IdLoc, |
1857 | ClassTemplateDecl *SpecializedTemplate, |
1858 | ArrayRef<TemplateArgument> Args, |
1859 | ClassTemplateSpecializationDecl *PrevDecl); |
1860 | static ClassTemplateSpecializationDecl * |
1861 | CreateDeserialized(ASTContext &C, DeclID ID); |
1862 | |
1863 | void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy, |
1864 | bool Qualified) const override; |
1865 | |
1866 | // FIXME: This is broken. CXXRecordDecl::getMostRecentDecl() returns a |
1867 | // different "most recent" declaration from this function for the same |
1868 | // declaration, because we don't override getMostRecentDeclImpl(). But |
1869 | // it's not clear that we should override that, because the most recent |
1870 | // declaration as a CXXRecordDecl sometimes is the injected-class-name. |
1871 | ClassTemplateSpecializationDecl *getMostRecentDecl() { |
1872 | return cast<ClassTemplateSpecializationDecl>( |
1873 | getMostRecentNonInjectedDecl()); |
1874 | } |
1875 | |
1876 | /// Retrieve the template that this specialization specializes. |
1877 | ClassTemplateDecl *getSpecializedTemplate() const; |
1878 | |
1879 | /// Retrieve the template arguments of the class template |
1880 | /// specialization. |
1881 | const TemplateArgumentList &getTemplateArgs() const { |
1882 | return *TemplateArgs; |
1883 | } |
1884 | |
1885 | void setTemplateArgs(TemplateArgumentList *Args) { |
1886 | TemplateArgs = Args; |
1887 | } |
1888 | |
1889 | /// Determine the kind of specialization that this |
1890 | /// declaration represents. |
1891 | TemplateSpecializationKind getSpecializationKind() const { |
1892 | return static_cast<TemplateSpecializationKind>(SpecializationKind); |
1893 | } |
1894 | |
1895 | bool isExplicitSpecialization() const { |
1896 | return getSpecializationKind() == TSK_ExplicitSpecialization; |
1897 | } |
1898 | |
1899 | /// Is this an explicit specialization at class scope (within the class that |
1900 | /// owns the primary template)? For example: |
1901 | /// |
1902 | /// \code |
1903 | /// template<typename T> struct Outer { |
1904 | /// template<typename U> struct Inner; |
1905 | /// template<> struct Inner; // class-scope explicit specialization |
1906 | /// }; |
1907 | /// \endcode |
1908 | bool isClassScopeExplicitSpecialization() const { |
1909 | return isExplicitSpecialization() && |
1910 | isa<CXXRecordDecl>(getLexicalDeclContext()); |
1911 | } |
1912 | |
1913 | /// True if this declaration is an explicit specialization, |
1914 | /// explicit instantiation declaration, or explicit instantiation |
1915 | /// definition. |
1916 | bool isExplicitInstantiationOrSpecialization() const { |
1917 | return isTemplateExplicitInstantiationOrSpecialization( |
1918 | getTemplateSpecializationKind()); |
1919 | } |
1920 | |
1921 | void setSpecializedTemplate(ClassTemplateDecl *Specialized) { |
1922 | SpecializedTemplate = Specialized; |
1923 | } |
1924 | |
1925 | void setSpecializationKind(TemplateSpecializationKind TSK) { |
1926 | SpecializationKind = TSK; |
1927 | } |
1928 | |
1929 | /// Get the point of instantiation (if any), or null if none. |
1930 | SourceLocation getPointOfInstantiation() const { |
1931 | return PointOfInstantiation; |
1932 | } |
1933 | |
1934 | void setPointOfInstantiation(SourceLocation Loc) { |
1935 | assert(Loc.isValid() && "point of instantiation must be valid!" ); |
1936 | PointOfInstantiation = Loc; |
1937 | } |
1938 | |
1939 | /// If this class template specialization is an instantiation of |
1940 | /// a template (rather than an explicit specialization), return the |
1941 | /// class template or class template partial specialization from which it |
1942 | /// was instantiated. |
1943 | llvm::PointerUnion<ClassTemplateDecl *, |
1944 | ClassTemplatePartialSpecializationDecl *> |
1945 | getInstantiatedFrom() const { |
1946 | if (!isTemplateInstantiation(Kind: getSpecializationKind())) |
1947 | return llvm::PointerUnion<ClassTemplateDecl *, |
1948 | ClassTemplatePartialSpecializationDecl *>(); |
1949 | |
1950 | return getSpecializedTemplateOrPartial(); |
1951 | } |
1952 | |
1953 | /// Retrieve the class template or class template partial |
1954 | /// specialization which was specialized by this. |
1955 | llvm::PointerUnion<ClassTemplateDecl *, |
1956 | ClassTemplatePartialSpecializationDecl *> |
1957 | getSpecializedTemplateOrPartial() const { |
1958 | if (const auto *PartialSpec = |
1959 | SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>()) |
1960 | return PartialSpec->PartialSpecialization; |
1961 | |
1962 | return SpecializedTemplate.get<ClassTemplateDecl*>(); |
1963 | } |
1964 | |
1965 | /// Retrieve the set of template arguments that should be used |
1966 | /// to instantiate members of the class template or class template partial |
1967 | /// specialization from which this class template specialization was |
1968 | /// instantiated. |
1969 | /// |
1970 | /// \returns For a class template specialization instantiated from the primary |
1971 | /// template, this function will return the same template arguments as |
1972 | /// getTemplateArgs(). For a class template specialization instantiated from |
1973 | /// a class template partial specialization, this function will return the |
1974 | /// deduced template arguments for the class template partial specialization |
1975 | /// itself. |
1976 | const TemplateArgumentList &getTemplateInstantiationArgs() const { |
1977 | if (const auto *PartialSpec = |
1978 | SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>()) |
1979 | return *PartialSpec->TemplateArgs; |
1980 | |
1981 | return getTemplateArgs(); |
1982 | } |
1983 | |
1984 | /// Note that this class template specialization is actually an |
1985 | /// instantiation of the given class template partial specialization whose |
1986 | /// template arguments have been deduced. |
1987 | void setInstantiationOf(ClassTemplatePartialSpecializationDecl *PartialSpec, |
1988 | const TemplateArgumentList *TemplateArgs) { |
1989 | assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() && |
1990 | "Already set to a class template partial specialization!" ); |
1991 | auto *PS = new (getASTContext()) SpecializedPartialSpecialization(); |
1992 | PS->PartialSpecialization = PartialSpec; |
1993 | PS->TemplateArgs = TemplateArgs; |
1994 | SpecializedTemplate = PS; |
1995 | } |
1996 | |
1997 | /// Note that this class template specialization is an instantiation |
1998 | /// of the given class template. |
1999 | void setInstantiationOf(ClassTemplateDecl *TemplDecl) { |
2000 | assert(!SpecializedTemplate.is<SpecializedPartialSpecialization*>() && |
2001 | "Previously set to a class template partial specialization!" ); |
2002 | SpecializedTemplate = TemplDecl; |
2003 | } |
2004 | |
2005 | /// Sets the type of this specialization as it was written by |
2006 | /// the user. This will be a class template specialization type. |
2007 | void setTypeAsWritten(TypeSourceInfo *T) { |
2008 | if (!ExplicitInfo) |
2009 | ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; |
2010 | ExplicitInfo->TypeAsWritten = T; |
2011 | } |
2012 | |
2013 | /// Gets the type of this specialization as it was written by |
2014 | /// the user, if it was so written. |
2015 | TypeSourceInfo *getTypeAsWritten() const { |
2016 | return ExplicitInfo ? ExplicitInfo->TypeAsWritten : nullptr; |
2017 | } |
2018 | |
2019 | /// Gets the location of the extern keyword, if present. |
2020 | SourceLocation getExternLoc() const { |
2021 | return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation(); |
2022 | } |
2023 | |
2024 | /// Sets the location of the extern keyword. |
2025 | void setExternLoc(SourceLocation Loc) { |
2026 | if (!ExplicitInfo) |
2027 | ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; |
2028 | ExplicitInfo->ExternLoc = Loc; |
2029 | } |
2030 | |
2031 | /// Sets the location of the template keyword. |
2032 | void setTemplateKeywordLoc(SourceLocation Loc) { |
2033 | if (!ExplicitInfo) |
2034 | ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; |
2035 | ExplicitInfo->TemplateKeywordLoc = Loc; |
2036 | } |
2037 | |
2038 | /// Gets the location of the template keyword, if present. |
2039 | SourceLocation getTemplateKeywordLoc() const { |
2040 | return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation(); |
2041 | } |
2042 | |
2043 | SourceRange getSourceRange() const override LLVM_READONLY; |
2044 | |
2045 | void Profile(llvm::FoldingSetNodeID &ID) const { |
2046 | Profile(ID, TemplateArgs->asArray(), getASTContext()); |
2047 | } |
2048 | |
2049 | static void |
2050 | Profile(llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs, |
2051 | const ASTContext &Context) { |
2052 | ID.AddInteger(I: TemplateArgs.size()); |
2053 | for (const TemplateArgument &TemplateArg : TemplateArgs) |
2054 | TemplateArg.Profile(ID, Context); |
2055 | } |
2056 | |
2057 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
2058 | |
2059 | static bool classofKind(Kind K) { |
2060 | return K >= firstClassTemplateSpecialization && |
2061 | K <= lastClassTemplateSpecialization; |
2062 | } |
2063 | }; |
2064 | |
2065 | class ClassTemplatePartialSpecializationDecl |
2066 | : public ClassTemplateSpecializationDecl { |
2067 | /// The list of template parameters |
2068 | TemplateParameterList* TemplateParams = nullptr; |
2069 | |
2070 | /// The source info for the template arguments as written. |
2071 | /// FIXME: redundant with TypeAsWritten? |
2072 | const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr; |
2073 | |
2074 | /// The class template partial specialization from which this |
2075 | /// class template partial specialization was instantiated. |
2076 | /// |
2077 | /// The boolean value will be true to indicate that this class template |
2078 | /// partial specialization was specialized at this level. |
2079 | llvm::PointerIntPair<ClassTemplatePartialSpecializationDecl *, 1, bool> |
2080 | InstantiatedFromMember; |
2081 | |
2082 | ClassTemplatePartialSpecializationDecl(ASTContext &Context, TagKind TK, |
2083 | DeclContext *DC, |
2084 | SourceLocation StartLoc, |
2085 | SourceLocation IdLoc, |
2086 | TemplateParameterList *Params, |
2087 | ClassTemplateDecl *SpecializedTemplate, |
2088 | ArrayRef<TemplateArgument> Args, |
2089 | const ASTTemplateArgumentListInfo *ArgsAsWritten, |
2090 | ClassTemplatePartialSpecializationDecl *PrevDecl); |
2091 | |
2092 | ClassTemplatePartialSpecializationDecl(ASTContext &C) |
2093 | : ClassTemplateSpecializationDecl(C, ClassTemplatePartialSpecialization), |
2094 | InstantiatedFromMember(nullptr, false) {} |
2095 | |
2096 | void anchor() override; |
2097 | |
2098 | public: |
2099 | friend class ASTDeclReader; |
2100 | friend class ASTDeclWriter; |
2101 | |
2102 | static ClassTemplatePartialSpecializationDecl * |
2103 | Create(ASTContext &Context, TagKind TK, DeclContext *DC, |
2104 | SourceLocation StartLoc, SourceLocation IdLoc, |
2105 | TemplateParameterList *Params, |
2106 | ClassTemplateDecl *SpecializedTemplate, |
2107 | ArrayRef<TemplateArgument> Args, |
2108 | const TemplateArgumentListInfo &ArgInfos, |
2109 | QualType CanonInjectedType, |
2110 | ClassTemplatePartialSpecializationDecl *PrevDecl); |
2111 | |
2112 | static ClassTemplatePartialSpecializationDecl * |
2113 | CreateDeserialized(ASTContext &C, DeclID ID); |
2114 | |
2115 | ClassTemplatePartialSpecializationDecl *getMostRecentDecl() { |
2116 | return cast<ClassTemplatePartialSpecializationDecl>( |
2117 | static_cast<ClassTemplateSpecializationDecl *>( |
2118 | this)->getMostRecentDecl()); |
2119 | } |
2120 | |
2121 | /// Get the list of template parameters |
2122 | TemplateParameterList *getTemplateParameters() const { |
2123 | return TemplateParams; |
2124 | } |
2125 | |
2126 | /// \brief All associated constraints of this partial specialization, |
2127 | /// including the requires clause and any constraints derived from |
2128 | /// constrained-parameters. |
2129 | /// |
2130 | /// The constraints in the resulting list are to be treated as if in a |
2131 | /// conjunction ("and"). |
2132 | void getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const { |
2133 | TemplateParams->getAssociatedConstraints(AC); |
2134 | } |
2135 | |
2136 | bool hasAssociatedConstraints() const { |
2137 | return TemplateParams->hasAssociatedConstraints(); |
2138 | } |
2139 | |
2140 | /// Get the template arguments as written. |
2141 | const ASTTemplateArgumentListInfo *getTemplateArgsAsWritten() const { |
2142 | return ArgsAsWritten; |
2143 | } |
2144 | |
2145 | /// Retrieve the member class template partial specialization from |
2146 | /// which this particular class template partial specialization was |
2147 | /// instantiated. |
2148 | /// |
2149 | /// \code |
2150 | /// template<typename T> |
2151 | /// struct Outer { |
2152 | /// template<typename U> struct Inner; |
2153 | /// template<typename U> struct Inner<U*> { }; // #1 |
2154 | /// }; |
2155 | /// |
2156 | /// Outer<float>::Inner<int*> ii; |
2157 | /// \endcode |
2158 | /// |
2159 | /// In this example, the instantiation of \c Outer<float>::Inner<int*> will |
2160 | /// end up instantiating the partial specialization |
2161 | /// \c Outer<float>::Inner<U*>, which itself was instantiated from the class |
2162 | /// template partial specialization \c Outer<T>::Inner<U*>. Given |
2163 | /// \c Outer<float>::Inner<U*>, this function would return |
2164 | /// \c Outer<T>::Inner<U*>. |
2165 | ClassTemplatePartialSpecializationDecl *getInstantiatedFromMember() const { |
2166 | const auto *First = |
2167 | cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl()); |
2168 | return First->InstantiatedFromMember.getPointer(); |
2169 | } |
2170 | ClassTemplatePartialSpecializationDecl * |
2171 | getInstantiatedFromMemberTemplate() const { |
2172 | return getInstantiatedFromMember(); |
2173 | } |
2174 | |
2175 | void setInstantiatedFromMember( |
2176 | ClassTemplatePartialSpecializationDecl *PartialSpec) { |
2177 | auto *First = cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl()); |
2178 | First->InstantiatedFromMember.setPointer(PartialSpec); |
2179 | } |
2180 | |
2181 | /// Determines whether this class template partial specialization |
2182 | /// template was a specialization of a member partial specialization. |
2183 | /// |
2184 | /// In the following example, the member template partial specialization |
2185 | /// \c X<int>::Inner<T*> is a member specialization. |
2186 | /// |
2187 | /// \code |
2188 | /// template<typename T> |
2189 | /// struct X { |
2190 | /// template<typename U> struct Inner; |
2191 | /// template<typename U> struct Inner<U*>; |
2192 | /// }; |
2193 | /// |
2194 | /// template<> template<typename T> |
2195 | /// struct X<int>::Inner<T*> { /* ... */ }; |
2196 | /// \endcode |
2197 | bool isMemberSpecialization() { |
2198 | const auto *First = |
2199 | cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl()); |
2200 | return First->InstantiatedFromMember.getInt(); |
2201 | } |
2202 | |
2203 | /// Note that this member template is a specialization. |
2204 | void setMemberSpecialization() { |
2205 | auto *First = cast<ClassTemplatePartialSpecializationDecl>(getFirstDecl()); |
2206 | assert(First->InstantiatedFromMember.getPointer() && |
2207 | "Only member templates can be member template specializations" ); |
2208 | return First->InstantiatedFromMember.setInt(true); |
2209 | } |
2210 | |
2211 | /// Retrieves the injected specialization type for this partial |
2212 | /// specialization. This is not the same as the type-decl-type for |
2213 | /// this partial specialization, which is an InjectedClassNameType. |
2214 | QualType getInjectedSpecializationType() const { |
2215 | assert(getTypeForDecl() && "partial specialization has no type set!" ); |
2216 | return cast<InjectedClassNameType>(getTypeForDecl()) |
2217 | ->getInjectedSpecializationType(); |
2218 | } |
2219 | |
2220 | void Profile(llvm::FoldingSetNodeID &ID) const { |
2221 | Profile(ID, getTemplateArgs().asArray(), getTemplateParameters(), |
2222 | getASTContext()); |
2223 | } |
2224 | |
2225 | static void |
2226 | Profile(llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs, |
2227 | TemplateParameterList *TPL, const ASTContext &Context); |
2228 | |
2229 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
2230 | |
2231 | static bool classofKind(Kind K) { |
2232 | return K == ClassTemplatePartialSpecialization; |
2233 | } |
2234 | }; |
2235 | |
2236 | /// Declaration of a class template. |
2237 | class ClassTemplateDecl : public RedeclarableTemplateDecl { |
2238 | protected: |
2239 | /// Data that is common to all of the declarations of a given |
2240 | /// class template. |
2241 | struct Common : CommonBase { |
2242 | /// The class template specializations for this class |
2243 | /// template, including explicit specializations and instantiations. |
2244 | llvm::FoldingSetVector<ClassTemplateSpecializationDecl> Specializations; |
2245 | |
2246 | /// The class template partial specializations for this class |
2247 | /// template. |
2248 | llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> |
2249 | PartialSpecializations; |
2250 | |
2251 | /// The injected-class-name type for this class template. |
2252 | QualType InjectedClassNameType; |
2253 | |
2254 | Common() = default; |
2255 | }; |
2256 | |
2257 | /// Retrieve the set of specializations of this class template. |
2258 | llvm::FoldingSetVector<ClassTemplateSpecializationDecl> & |
2259 | getSpecializations() const; |
2260 | |
2261 | /// Retrieve the set of partial specializations of this class |
2262 | /// template. |
2263 | llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl> & |
2264 | getPartialSpecializations() const; |
2265 | |
2266 | ClassTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L, |
2267 | DeclarationName Name, TemplateParameterList *Params, |
2268 | NamedDecl *Decl) |
2269 | : RedeclarableTemplateDecl(ClassTemplate, C, DC, L, Name, Params, Decl) {} |
2270 | |
2271 | CommonBase *newCommon(ASTContext &C) const override; |
2272 | |
2273 | Common *getCommonPtr() const { |
2274 | return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr()); |
2275 | } |
2276 | |
2277 | void setCommonPtr(Common *C) { |
2278 | RedeclarableTemplateDecl::Common = C; |
2279 | } |
2280 | |
2281 | public: |
2282 | |
2283 | friend class ASTDeclReader; |
2284 | friend class ASTDeclWriter; |
2285 | friend class TemplateDeclInstantiator; |
2286 | |
2287 | /// Load any lazily-loaded specializations from the external source. |
2288 | void LoadLazySpecializations() const; |
2289 | |
2290 | /// Get the underlying class declarations of the template. |
2291 | CXXRecordDecl *getTemplatedDecl() const { |
2292 | return static_cast<CXXRecordDecl *>(TemplatedDecl); |
2293 | } |
2294 | |
2295 | /// Returns whether this template declaration defines the primary |
2296 | /// class pattern. |
2297 | bool isThisDeclarationADefinition() const { |
2298 | return getTemplatedDecl()->isThisDeclarationADefinition(); |
2299 | } |
2300 | |
2301 | /// \brief Create a class template node. |
2302 | static ClassTemplateDecl *Create(ASTContext &C, DeclContext *DC, |
2303 | SourceLocation L, |
2304 | DeclarationName Name, |
2305 | TemplateParameterList *Params, |
2306 | NamedDecl *Decl); |
2307 | |
2308 | /// Create an empty class template node. |
2309 | static ClassTemplateDecl *CreateDeserialized(ASTContext &C, DeclID ID); |
2310 | |
2311 | /// Return the specialization with the provided arguments if it exists, |
2312 | /// otherwise return the insertion point. |
2313 | ClassTemplateSpecializationDecl * |
2314 | findSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos); |
2315 | |
2316 | /// Insert the specified specialization knowing that it is not already |
2317 | /// in. InsertPos must be obtained from findSpecialization. |
2318 | void AddSpecialization(ClassTemplateSpecializationDecl *D, void *InsertPos); |
2319 | |
2320 | ClassTemplateDecl *getCanonicalDecl() override { |
2321 | return cast<ClassTemplateDecl>( |
2322 | RedeclarableTemplateDecl::getCanonicalDecl()); |
2323 | } |
2324 | const ClassTemplateDecl *getCanonicalDecl() const { |
2325 | return cast<ClassTemplateDecl>( |
2326 | RedeclarableTemplateDecl::getCanonicalDecl()); |
2327 | } |
2328 | |
2329 | /// Retrieve the previous declaration of this class template, or |
2330 | /// nullptr if no such declaration exists. |
2331 | ClassTemplateDecl *getPreviousDecl() { |
2332 | return cast_or_null<ClassTemplateDecl>( |
2333 | static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl()); |
2334 | } |
2335 | const ClassTemplateDecl *getPreviousDecl() const { |
2336 | return cast_or_null<ClassTemplateDecl>( |
2337 | static_cast<const RedeclarableTemplateDecl *>( |
2338 | this)->getPreviousDecl()); |
2339 | } |
2340 | |
2341 | ClassTemplateDecl *getMostRecentDecl() { |
2342 | return cast<ClassTemplateDecl>( |
2343 | static_cast<RedeclarableTemplateDecl *>(this)->getMostRecentDecl()); |
2344 | } |
2345 | const ClassTemplateDecl *getMostRecentDecl() const { |
2346 | return const_cast<ClassTemplateDecl*>(this)->getMostRecentDecl(); |
2347 | } |
2348 | |
2349 | ClassTemplateDecl *getInstantiatedFromMemberTemplate() const { |
2350 | return cast_or_null<ClassTemplateDecl>( |
2351 | RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate()); |
2352 | } |
2353 | |
2354 | /// Return the partial specialization with the provided arguments if it |
2355 | /// exists, otherwise return the insertion point. |
2356 | ClassTemplatePartialSpecializationDecl * |
2357 | findPartialSpecialization(ArrayRef<TemplateArgument> Args, |
2358 | TemplateParameterList *TPL, void *&InsertPos); |
2359 | |
2360 | /// Insert the specified partial specialization knowing that it is not |
2361 | /// already in. InsertPos must be obtained from findPartialSpecialization. |
2362 | void AddPartialSpecialization(ClassTemplatePartialSpecializationDecl *D, |
2363 | void *InsertPos); |
2364 | |
2365 | /// Retrieve the partial specializations as an ordered list. |
2366 | void getPartialSpecializations( |
2367 | SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) const; |
2368 | |
2369 | /// Find a class template partial specialization with the given |
2370 | /// type T. |
2371 | /// |
2372 | /// \param T a dependent type that names a specialization of this class |
2373 | /// template. |
2374 | /// |
2375 | /// \returns the class template partial specialization that exactly matches |
2376 | /// the type \p T, or nullptr if no such partial specialization exists. |
2377 | ClassTemplatePartialSpecializationDecl *findPartialSpecialization(QualType T); |
2378 | |
2379 | /// Find a class template partial specialization which was instantiated |
2380 | /// from the given member partial specialization. |
2381 | /// |
2382 | /// \param D a member class template partial specialization. |
2383 | /// |
2384 | /// \returns the class template partial specialization which was instantiated |
2385 | /// from the given member partial specialization, or nullptr if no such |
2386 | /// partial specialization exists. |
2387 | ClassTemplatePartialSpecializationDecl * |
2388 | findPartialSpecInstantiatedFromMember( |
2389 | ClassTemplatePartialSpecializationDecl *D); |
2390 | |
2391 | /// Retrieve the template specialization type of the |
2392 | /// injected-class-name for this class template. |
2393 | /// |
2394 | /// The injected-class-name for a class template \c X is \c |
2395 | /// X<template-args>, where \c template-args is formed from the |
2396 | /// template arguments that correspond to the template parameters of |
2397 | /// \c X. For example: |
2398 | /// |
2399 | /// \code |
2400 | /// template<typename T, int N> |
2401 | /// struct array { |
2402 | /// typedef array this_type; // "array" is equivalent to "array<T, N>" |
2403 | /// }; |
2404 | /// \endcode |
2405 | QualType getInjectedClassNameSpecialization(); |
2406 | |
2407 | using spec_iterator = SpecIterator<ClassTemplateSpecializationDecl>; |
2408 | using spec_range = llvm::iterator_range<spec_iterator>; |
2409 | |
2410 | spec_range specializations() const { |
2411 | return spec_range(spec_begin(), spec_end()); |
2412 | } |
2413 | |
2414 | spec_iterator spec_begin() const { |
2415 | return makeSpecIterator(Specs&: getSpecializations(), isEnd: false); |
2416 | } |
2417 | |
2418 | spec_iterator spec_end() const { |
2419 | return makeSpecIterator(Specs&: getSpecializations(), isEnd: true); |
2420 | } |
2421 | |
2422 | // Implement isa/cast/dyncast support |
2423 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
2424 | static bool classofKind(Kind K) { return K == ClassTemplate; } |
2425 | }; |
2426 | |
2427 | /// Declaration of a friend template. |
2428 | /// |
2429 | /// For example: |
2430 | /// \code |
2431 | /// template \<typename T> class A { |
2432 | /// friend class MyVector<T>; // not a friend template |
2433 | /// template \<typename U> friend class B; // not a friend template |
2434 | /// template \<typename U> friend class Foo<T>::Nested; // friend template |
2435 | /// }; |
2436 | /// \endcode |
2437 | /// |
2438 | /// \note This class is not currently in use. All of the above |
2439 | /// will yield a FriendDecl, not a FriendTemplateDecl. |
2440 | class FriendTemplateDecl : public Decl { |
2441 | virtual void anchor(); |
2442 | |
2443 | public: |
2444 | using FriendUnion = llvm::PointerUnion<NamedDecl *,TypeSourceInfo *>; |
2445 | |
2446 | private: |
2447 | // The number of template parameters; always non-zero. |
2448 | unsigned NumParams = 0; |
2449 | |
2450 | // The parameter list. |
2451 | TemplateParameterList **Params = nullptr; |
2452 | |
2453 | // The declaration that's a friend of this class. |
2454 | FriendUnion Friend; |
2455 | |
2456 | // Location of the 'friend' specifier. |
2457 | SourceLocation FriendLoc; |
2458 | |
2459 | FriendTemplateDecl(DeclContext *DC, SourceLocation Loc, |
2460 | TemplateParameterList **Params, unsigned NumParams, |
2461 | FriendUnion Friend, SourceLocation FriendLoc) |
2462 | : Decl(Decl::FriendTemplate, DC, Loc), NumParams(NumParams), |
2463 | Params(Params), Friend(Friend), FriendLoc(FriendLoc) {} |
2464 | |
2465 | FriendTemplateDecl(EmptyShell Empty) : Decl(Decl::FriendTemplate, Empty) {} |
2466 | |
2467 | public: |
2468 | friend class ASTDeclReader; |
2469 | |
2470 | static FriendTemplateDecl * |
2471 | Create(ASTContext &Context, DeclContext *DC, SourceLocation Loc, |
2472 | MutableArrayRef<TemplateParameterList *> Params, FriendUnion Friend, |
2473 | SourceLocation FriendLoc); |
2474 | |
2475 | static FriendTemplateDecl *CreateDeserialized(ASTContext &C, DeclID ID); |
2476 | |
2477 | /// If this friend declaration names a templated type (or |
2478 | /// a dependent member type of a templated type), return that |
2479 | /// type; otherwise return null. |
2480 | TypeSourceInfo *getFriendType() const { |
2481 | return Friend.dyn_cast<TypeSourceInfo*>(); |
2482 | } |
2483 | |
2484 | /// If this friend declaration names a templated function (or |
2485 | /// a member function of a templated type), return that type; |
2486 | /// otherwise return null. |
2487 | NamedDecl *getFriendDecl() const { |
2488 | return Friend.dyn_cast<NamedDecl*>(); |
2489 | } |
2490 | |
2491 | /// Retrieves the location of the 'friend' keyword. |
2492 | SourceLocation getFriendLoc() const { |
2493 | return FriendLoc; |
2494 | } |
2495 | |
2496 | TemplateParameterList *getTemplateParameterList(unsigned i) const { |
2497 | assert(i <= NumParams); |
2498 | return Params[i]; |
2499 | } |
2500 | |
2501 | unsigned getNumTemplateParameters() const { |
2502 | return NumParams; |
2503 | } |
2504 | |
2505 | // Implement isa/cast/dyncast/etc. |
2506 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
2507 | static bool classofKind(Kind K) { return K == Decl::FriendTemplate; } |
2508 | }; |
2509 | |
2510 | /// Declaration of an alias template. |
2511 | /// |
2512 | /// For example: |
2513 | /// \code |
2514 | /// template \<typename T> using V = std::map<T*, int, MyCompare<T>>; |
2515 | /// \endcode |
2516 | class TypeAliasTemplateDecl : public RedeclarableTemplateDecl { |
2517 | protected: |
2518 | using Common = CommonBase; |
2519 | |
2520 | TypeAliasTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L, |
2521 | DeclarationName Name, TemplateParameterList *Params, |
2522 | NamedDecl *Decl) |
2523 | : RedeclarableTemplateDecl(TypeAliasTemplate, C, DC, L, Name, Params, |
2524 | Decl) {} |
2525 | |
2526 | CommonBase *newCommon(ASTContext &C) const override; |
2527 | |
2528 | Common *getCommonPtr() { |
2529 | return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr()); |
2530 | } |
2531 | |
2532 | public: |
2533 | friend class ASTDeclReader; |
2534 | friend class ASTDeclWriter; |
2535 | |
2536 | /// Get the underlying function declaration of the template. |
2537 | TypeAliasDecl *getTemplatedDecl() const { |
2538 | return static_cast<TypeAliasDecl *>(TemplatedDecl); |
2539 | } |
2540 | |
2541 | |
2542 | TypeAliasTemplateDecl *getCanonicalDecl() override { |
2543 | return cast<TypeAliasTemplateDecl>( |
2544 | RedeclarableTemplateDecl::getCanonicalDecl()); |
2545 | } |
2546 | const TypeAliasTemplateDecl *getCanonicalDecl() const { |
2547 | return cast<TypeAliasTemplateDecl>( |
2548 | RedeclarableTemplateDecl::getCanonicalDecl()); |
2549 | } |
2550 | |
2551 | /// Retrieve the previous declaration of this function template, or |
2552 | /// nullptr if no such declaration exists. |
2553 | TypeAliasTemplateDecl *getPreviousDecl() { |
2554 | return cast_or_null<TypeAliasTemplateDecl>( |
2555 | static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl()); |
2556 | } |
2557 | const TypeAliasTemplateDecl *getPreviousDecl() const { |
2558 | return cast_or_null<TypeAliasTemplateDecl>( |
2559 | static_cast<const RedeclarableTemplateDecl *>( |
2560 | this)->getPreviousDecl()); |
2561 | } |
2562 | |
2563 | TypeAliasTemplateDecl *getInstantiatedFromMemberTemplate() const { |
2564 | return cast_or_null<TypeAliasTemplateDecl>( |
2565 | RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate()); |
2566 | } |
2567 | |
2568 | /// Create a function template node. |
2569 | static TypeAliasTemplateDecl *Create(ASTContext &C, DeclContext *DC, |
2570 | SourceLocation L, |
2571 | DeclarationName Name, |
2572 | TemplateParameterList *Params, |
2573 | NamedDecl *Decl); |
2574 | |
2575 | /// Create an empty alias template node. |
2576 | static TypeAliasTemplateDecl *CreateDeserialized(ASTContext &C, DeclID ID); |
2577 | |
2578 | // Implement isa/cast/dyncast support |
2579 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
2580 | static bool classofKind(Kind K) { return K == TypeAliasTemplate; } |
2581 | }; |
2582 | |
2583 | /// Represents a variable template specialization, which refers to |
2584 | /// a variable template with a given set of template arguments. |
2585 | /// |
2586 | /// Variable template specializations represent both explicit |
2587 | /// specializations of variable templates, as in the example below, and |
2588 | /// implicit instantiations of variable templates. |
2589 | /// |
2590 | /// \code |
2591 | /// template<typename T> constexpr T pi = T(3.1415926535897932385); |
2592 | /// |
2593 | /// template<> |
2594 | /// constexpr float pi<float>; // variable template specialization pi<float> |
2595 | /// \endcode |
2596 | class VarTemplateSpecializationDecl : public VarDecl, |
2597 | public llvm::FoldingSetNode { |
2598 | |
2599 | /// Structure that stores information about a variable template |
2600 | /// specialization that was instantiated from a variable template partial |
2601 | /// specialization. |
2602 | struct SpecializedPartialSpecialization { |
2603 | /// The variable template partial specialization from which this |
2604 | /// variable template specialization was instantiated. |
2605 | VarTemplatePartialSpecializationDecl *PartialSpecialization; |
2606 | |
2607 | /// The template argument list deduced for the variable template |
2608 | /// partial specialization itself. |
2609 | const TemplateArgumentList *TemplateArgs; |
2610 | }; |
2611 | |
2612 | /// The template that this specialization specializes. |
2613 | llvm::PointerUnion<VarTemplateDecl *, SpecializedPartialSpecialization *> |
2614 | SpecializedTemplate; |
2615 | |
2616 | /// Further info for explicit template specialization/instantiation. |
2617 | struct ExplicitSpecializationInfo { |
2618 | /// The type-as-written. |
2619 | TypeSourceInfo *TypeAsWritten = nullptr; |
2620 | |
2621 | /// The location of the extern keyword. |
2622 | SourceLocation ExternLoc; |
2623 | |
2624 | /// The location of the template keyword. |
2625 | SourceLocation TemplateKeywordLoc; |
2626 | |
2627 | ExplicitSpecializationInfo() = default; |
2628 | }; |
2629 | |
2630 | /// Further info for explicit template specialization/instantiation. |
2631 | /// Does not apply to implicit specializations. |
2632 | ExplicitSpecializationInfo *ExplicitInfo = nullptr; |
2633 | |
2634 | /// The template arguments used to describe this specialization. |
2635 | const TemplateArgumentList *TemplateArgs; |
2636 | const ASTTemplateArgumentListInfo *TemplateArgsInfo = nullptr; |
2637 | |
2638 | /// The point where this template was instantiated (if any). |
2639 | SourceLocation PointOfInstantiation; |
2640 | |
2641 | /// The kind of specialization this declaration refers to. |
2642 | LLVM_PREFERRED_TYPE(TemplateSpecializationKind) |
2643 | unsigned SpecializationKind : 3; |
2644 | |
2645 | /// Whether this declaration is a complete definition of the |
2646 | /// variable template specialization. We can't otherwise tell apart |
2647 | /// an instantiated declaration from an instantiated definition with |
2648 | /// no initializer. |
2649 | LLVM_PREFERRED_TYPE(bool) |
2650 | unsigned IsCompleteDefinition : 1; |
2651 | |
2652 | protected: |
2653 | VarTemplateSpecializationDecl(Kind DK, ASTContext &Context, DeclContext *DC, |
2654 | SourceLocation StartLoc, SourceLocation IdLoc, |
2655 | VarTemplateDecl *SpecializedTemplate, |
2656 | QualType T, TypeSourceInfo *TInfo, |
2657 | StorageClass S, |
2658 | ArrayRef<TemplateArgument> Args); |
2659 | |
2660 | explicit VarTemplateSpecializationDecl(Kind DK, ASTContext &Context); |
2661 | |
2662 | public: |
2663 | friend class ASTDeclReader; |
2664 | friend class ASTDeclWriter; |
2665 | friend class VarDecl; |
2666 | |
2667 | static VarTemplateSpecializationDecl * |
2668 | Create(ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, |
2669 | SourceLocation IdLoc, VarTemplateDecl *SpecializedTemplate, QualType T, |
2670 | TypeSourceInfo *TInfo, StorageClass S, |
2671 | ArrayRef<TemplateArgument> Args); |
2672 | static VarTemplateSpecializationDecl *CreateDeserialized(ASTContext &C, |
2673 | DeclID ID); |
2674 | |
2675 | void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy, |
2676 | bool Qualified) const override; |
2677 | |
2678 | VarTemplateSpecializationDecl *getMostRecentDecl() { |
2679 | VarDecl *Recent = static_cast<VarDecl *>(this)->getMostRecentDecl(); |
2680 | return cast<VarTemplateSpecializationDecl>(Val: Recent); |
2681 | } |
2682 | |
2683 | /// Retrieve the template that this specialization specializes. |
2684 | VarTemplateDecl *getSpecializedTemplate() const; |
2685 | |
2686 | /// Retrieve the template arguments of the variable template |
2687 | /// specialization. |
2688 | const TemplateArgumentList &getTemplateArgs() const { return *TemplateArgs; } |
2689 | |
2690 | // TODO: Always set this when creating the new specialization? |
2691 | void setTemplateArgsInfo(const TemplateArgumentListInfo &ArgsInfo); |
2692 | void setTemplateArgsInfo(const ASTTemplateArgumentListInfo *ArgsInfo); |
2693 | |
2694 | const ASTTemplateArgumentListInfo *getTemplateArgsInfo() const { |
2695 | return TemplateArgsInfo; |
2696 | } |
2697 | |
2698 | /// Determine the kind of specialization that this |
2699 | /// declaration represents. |
2700 | TemplateSpecializationKind getSpecializationKind() const { |
2701 | return static_cast<TemplateSpecializationKind>(SpecializationKind); |
2702 | } |
2703 | |
2704 | bool isExplicitSpecialization() const { |
2705 | return getSpecializationKind() == TSK_ExplicitSpecialization; |
2706 | } |
2707 | |
2708 | bool isClassScopeExplicitSpecialization() const { |
2709 | return isExplicitSpecialization() && |
2710 | isa<CXXRecordDecl>(getLexicalDeclContext()); |
2711 | } |
2712 | |
2713 | /// True if this declaration is an explicit specialization, |
2714 | /// explicit instantiation declaration, or explicit instantiation |
2715 | /// definition. |
2716 | bool isExplicitInstantiationOrSpecialization() const { |
2717 | return isTemplateExplicitInstantiationOrSpecialization( |
2718 | getTemplateSpecializationKind()); |
2719 | } |
2720 | |
2721 | void setSpecializationKind(TemplateSpecializationKind TSK) { |
2722 | SpecializationKind = TSK; |
2723 | } |
2724 | |
2725 | /// Get the point of instantiation (if any), or null if none. |
2726 | SourceLocation getPointOfInstantiation() const { |
2727 | return PointOfInstantiation; |
2728 | } |
2729 | |
2730 | void setPointOfInstantiation(SourceLocation Loc) { |
2731 | assert(Loc.isValid() && "point of instantiation must be valid!" ); |
2732 | PointOfInstantiation = Loc; |
2733 | } |
2734 | |
2735 | void setCompleteDefinition() { IsCompleteDefinition = true; } |
2736 | |
2737 | /// If this variable template specialization is an instantiation of |
2738 | /// a template (rather than an explicit specialization), return the |
2739 | /// variable template or variable template partial specialization from which |
2740 | /// it was instantiated. |
2741 | llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *> |
2742 | getInstantiatedFrom() const { |
2743 | if (!isTemplateInstantiation(Kind: getSpecializationKind())) |
2744 | return llvm::PointerUnion<VarTemplateDecl *, |
2745 | VarTemplatePartialSpecializationDecl *>(); |
2746 | |
2747 | return getSpecializedTemplateOrPartial(); |
2748 | } |
2749 | |
2750 | /// Retrieve the variable template or variable template partial |
2751 | /// specialization which was specialized by this. |
2752 | llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *> |
2753 | getSpecializedTemplateOrPartial() const { |
2754 | if (const auto *PartialSpec = |
2755 | SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>()) |
2756 | return PartialSpec->PartialSpecialization; |
2757 | |
2758 | return SpecializedTemplate.get<VarTemplateDecl *>(); |
2759 | } |
2760 | |
2761 | /// Retrieve the set of template arguments that should be used |
2762 | /// to instantiate the initializer of the variable template or variable |
2763 | /// template partial specialization from which this variable template |
2764 | /// specialization was instantiated. |
2765 | /// |
2766 | /// \returns For a variable template specialization instantiated from the |
2767 | /// primary template, this function will return the same template arguments |
2768 | /// as getTemplateArgs(). For a variable template specialization instantiated |
2769 | /// from a variable template partial specialization, this function will the |
2770 | /// return deduced template arguments for the variable template partial |
2771 | /// specialization itself. |
2772 | const TemplateArgumentList &getTemplateInstantiationArgs() const { |
2773 | if (const auto *PartialSpec = |
2774 | SpecializedTemplate.dyn_cast<SpecializedPartialSpecialization *>()) |
2775 | return *PartialSpec->TemplateArgs; |
2776 | |
2777 | return getTemplateArgs(); |
2778 | } |
2779 | |
2780 | /// Note that this variable template specialization is actually an |
2781 | /// instantiation of the given variable template partial specialization whose |
2782 | /// template arguments have been deduced. |
2783 | void setInstantiationOf(VarTemplatePartialSpecializationDecl *PartialSpec, |
2784 | const TemplateArgumentList *TemplateArgs) { |
2785 | assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() && |
2786 | "Already set to a variable template partial specialization!" ); |
2787 | auto *PS = new (getASTContext()) SpecializedPartialSpecialization(); |
2788 | PS->PartialSpecialization = PartialSpec; |
2789 | PS->TemplateArgs = TemplateArgs; |
2790 | SpecializedTemplate = PS; |
2791 | } |
2792 | |
2793 | /// Note that this variable template specialization is an instantiation |
2794 | /// of the given variable template. |
2795 | void setInstantiationOf(VarTemplateDecl *TemplDecl) { |
2796 | assert(!SpecializedTemplate.is<SpecializedPartialSpecialization *>() && |
2797 | "Previously set to a variable template partial specialization!" ); |
2798 | SpecializedTemplate = TemplDecl; |
2799 | } |
2800 | |
2801 | /// Sets the type of this specialization as it was written by |
2802 | /// the user. |
2803 | void setTypeAsWritten(TypeSourceInfo *T) { |
2804 | if (!ExplicitInfo) |
2805 | ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; |
2806 | ExplicitInfo->TypeAsWritten = T; |
2807 | } |
2808 | |
2809 | /// Gets the type of this specialization as it was written by |
2810 | /// the user, if it was so written. |
2811 | TypeSourceInfo *getTypeAsWritten() const { |
2812 | return ExplicitInfo ? ExplicitInfo->TypeAsWritten : nullptr; |
2813 | } |
2814 | |
2815 | /// Gets the location of the extern keyword, if present. |
2816 | SourceLocation getExternLoc() const { |
2817 | return ExplicitInfo ? ExplicitInfo->ExternLoc : SourceLocation(); |
2818 | } |
2819 | |
2820 | /// Sets the location of the extern keyword. |
2821 | void setExternLoc(SourceLocation Loc) { |
2822 | if (!ExplicitInfo) |
2823 | ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; |
2824 | ExplicitInfo->ExternLoc = Loc; |
2825 | } |
2826 | |
2827 | /// Sets the location of the template keyword. |
2828 | void setTemplateKeywordLoc(SourceLocation Loc) { |
2829 | if (!ExplicitInfo) |
2830 | ExplicitInfo = new (getASTContext()) ExplicitSpecializationInfo; |
2831 | ExplicitInfo->TemplateKeywordLoc = Loc; |
2832 | } |
2833 | |
2834 | /// Gets the location of the template keyword, if present. |
2835 | SourceLocation getTemplateKeywordLoc() const { |
2836 | return ExplicitInfo ? ExplicitInfo->TemplateKeywordLoc : SourceLocation(); |
2837 | } |
2838 | |
2839 | SourceRange getSourceRange() const override LLVM_READONLY; |
2840 | |
2841 | void Profile(llvm::FoldingSetNodeID &ID) const { |
2842 | Profile(ID, TemplateArgs->asArray(), getASTContext()); |
2843 | } |
2844 | |
2845 | static void Profile(llvm::FoldingSetNodeID &ID, |
2846 | ArrayRef<TemplateArgument> TemplateArgs, |
2847 | const ASTContext &Context) { |
2848 | ID.AddInteger(I: TemplateArgs.size()); |
2849 | for (const TemplateArgument &TemplateArg : TemplateArgs) |
2850 | TemplateArg.Profile(ID, Context); |
2851 | } |
2852 | |
2853 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
2854 | |
2855 | static bool classofKind(Kind K) { |
2856 | return K >= firstVarTemplateSpecialization && |
2857 | K <= lastVarTemplateSpecialization; |
2858 | } |
2859 | }; |
2860 | |
2861 | class VarTemplatePartialSpecializationDecl |
2862 | : public VarTemplateSpecializationDecl { |
2863 | /// The list of template parameters |
2864 | TemplateParameterList *TemplateParams = nullptr; |
2865 | |
2866 | /// The source info for the template arguments as written. |
2867 | /// FIXME: redundant with TypeAsWritten? |
2868 | const ASTTemplateArgumentListInfo *ArgsAsWritten = nullptr; |
2869 | |
2870 | /// The variable template partial specialization from which this |
2871 | /// variable template partial specialization was instantiated. |
2872 | /// |
2873 | /// The boolean value will be true to indicate that this variable template |
2874 | /// partial specialization was specialized at this level. |
2875 | llvm::PointerIntPair<VarTemplatePartialSpecializationDecl *, 1, bool> |
2876 | InstantiatedFromMember; |
2877 | |
2878 | VarTemplatePartialSpecializationDecl( |
2879 | ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, |
2880 | SourceLocation IdLoc, TemplateParameterList *Params, |
2881 | VarTemplateDecl *SpecializedTemplate, QualType T, TypeSourceInfo *TInfo, |
2882 | StorageClass S, ArrayRef<TemplateArgument> Args, |
2883 | const ASTTemplateArgumentListInfo *ArgInfos); |
2884 | |
2885 | VarTemplatePartialSpecializationDecl(ASTContext &Context) |
2886 | : VarTemplateSpecializationDecl(VarTemplatePartialSpecialization, |
2887 | Context), |
2888 | InstantiatedFromMember(nullptr, false) {} |
2889 | |
2890 | void anchor() override; |
2891 | |
2892 | public: |
2893 | friend class ASTDeclReader; |
2894 | friend class ASTDeclWriter; |
2895 | |
2896 | static VarTemplatePartialSpecializationDecl * |
2897 | Create(ASTContext &Context, DeclContext *DC, SourceLocation StartLoc, |
2898 | SourceLocation IdLoc, TemplateParameterList *Params, |
2899 | VarTemplateDecl *SpecializedTemplate, QualType T, |
2900 | TypeSourceInfo *TInfo, StorageClass S, ArrayRef<TemplateArgument> Args, |
2901 | const TemplateArgumentListInfo &ArgInfos); |
2902 | |
2903 | static VarTemplatePartialSpecializationDecl *CreateDeserialized(ASTContext &C, |
2904 | DeclID ID); |
2905 | |
2906 | VarTemplatePartialSpecializationDecl *getMostRecentDecl() { |
2907 | return cast<VarTemplatePartialSpecializationDecl>( |
2908 | static_cast<VarTemplateSpecializationDecl *>( |
2909 | this)->getMostRecentDecl()); |
2910 | } |
2911 | |
2912 | /// Get the list of template parameters |
2913 | TemplateParameterList *getTemplateParameters() const { |
2914 | return TemplateParams; |
2915 | } |
2916 | |
2917 | /// Get the template arguments as written. |
2918 | const ASTTemplateArgumentListInfo *getTemplateArgsAsWritten() const { |
2919 | return ArgsAsWritten; |
2920 | } |
2921 | |
2922 | /// \brief All associated constraints of this partial specialization, |
2923 | /// including the requires clause and any constraints derived from |
2924 | /// constrained-parameters. |
2925 | /// |
2926 | /// The constraints in the resulting list are to be treated as if in a |
2927 | /// conjunction ("and"). |
2928 | void getAssociatedConstraints(llvm::SmallVectorImpl<const Expr *> &AC) const { |
2929 | TemplateParams->getAssociatedConstraints(AC); |
2930 | } |
2931 | |
2932 | bool hasAssociatedConstraints() const { |
2933 | return TemplateParams->hasAssociatedConstraints(); |
2934 | } |
2935 | |
2936 | /// \brief Retrieve the member variable template partial specialization from |
2937 | /// which this particular variable template partial specialization was |
2938 | /// instantiated. |
2939 | /// |
2940 | /// \code |
2941 | /// template<typename T> |
2942 | /// struct Outer { |
2943 | /// template<typename U> U Inner; |
2944 | /// template<typename U> U* Inner<U*> = (U*)(0); // #1 |
2945 | /// }; |
2946 | /// |
2947 | /// template int* Outer<float>::Inner<int*>; |
2948 | /// \endcode |
2949 | /// |
2950 | /// In this example, the instantiation of \c Outer<float>::Inner<int*> will |
2951 | /// end up instantiating the partial specialization |
2952 | /// \c Outer<float>::Inner<U*>, which itself was instantiated from the |
2953 | /// variable template partial specialization \c Outer<T>::Inner<U*>. Given |
2954 | /// \c Outer<float>::Inner<U*>, this function would return |
2955 | /// \c Outer<T>::Inner<U*>. |
2956 | VarTemplatePartialSpecializationDecl *getInstantiatedFromMember() const { |
2957 | const auto *First = |
2958 | cast<VarTemplatePartialSpecializationDecl>(getFirstDecl()); |
2959 | return First->InstantiatedFromMember.getPointer(); |
2960 | } |
2961 | |
2962 | void |
2963 | setInstantiatedFromMember(VarTemplatePartialSpecializationDecl *PartialSpec) { |
2964 | auto *First = cast<VarTemplatePartialSpecializationDecl>(getFirstDecl()); |
2965 | First->InstantiatedFromMember.setPointer(PartialSpec); |
2966 | } |
2967 | |
2968 | /// Determines whether this variable template partial specialization |
2969 | /// was a specialization of a member partial specialization. |
2970 | /// |
2971 | /// In the following example, the member template partial specialization |
2972 | /// \c X<int>::Inner<T*> is a member specialization. |
2973 | /// |
2974 | /// \code |
2975 | /// template<typename T> |
2976 | /// struct X { |
2977 | /// template<typename U> U Inner; |
2978 | /// template<typename U> U* Inner<U*> = (U*)(0); |
2979 | /// }; |
2980 | /// |
2981 | /// template<> template<typename T> |
2982 | /// U* X<int>::Inner<T*> = (T*)(0) + 1; |
2983 | /// \endcode |
2984 | bool isMemberSpecialization() { |
2985 | const auto *First = |
2986 | cast<VarTemplatePartialSpecializationDecl>(getFirstDecl()); |
2987 | return First->InstantiatedFromMember.getInt(); |
2988 | } |
2989 | |
2990 | /// Note that this member template is a specialization. |
2991 | void setMemberSpecialization() { |
2992 | auto *First = cast<VarTemplatePartialSpecializationDecl>(getFirstDecl()); |
2993 | assert(First->InstantiatedFromMember.getPointer() && |
2994 | "Only member templates can be member template specializations" ); |
2995 | return First->InstantiatedFromMember.setInt(true); |
2996 | } |
2997 | |
2998 | SourceRange getSourceRange() const override LLVM_READONLY; |
2999 | |
3000 | void Profile(llvm::FoldingSetNodeID &ID) const { |
3001 | Profile(ID, getTemplateArgs().asArray(), getTemplateParameters(), |
3002 | getASTContext()); |
3003 | } |
3004 | |
3005 | static void |
3006 | Profile(llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs, |
3007 | TemplateParameterList *TPL, const ASTContext &Context); |
3008 | |
3009 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
3010 | |
3011 | static bool classofKind(Kind K) { |
3012 | return K == VarTemplatePartialSpecialization; |
3013 | } |
3014 | }; |
3015 | |
3016 | /// Declaration of a variable template. |
3017 | class VarTemplateDecl : public RedeclarableTemplateDecl { |
3018 | protected: |
3019 | /// Data that is common to all of the declarations of a given |
3020 | /// variable template. |
3021 | struct Common : CommonBase { |
3022 | /// The variable template specializations for this variable |
3023 | /// template, including explicit specializations and instantiations. |
3024 | llvm::FoldingSetVector<VarTemplateSpecializationDecl> Specializations; |
3025 | |
3026 | /// The variable template partial specializations for this variable |
3027 | /// template. |
3028 | llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> |
3029 | PartialSpecializations; |
3030 | |
3031 | Common() = default; |
3032 | }; |
3033 | |
3034 | /// Retrieve the set of specializations of this variable template. |
3035 | llvm::FoldingSetVector<VarTemplateSpecializationDecl> & |
3036 | getSpecializations() const; |
3037 | |
3038 | /// Retrieve the set of partial specializations of this class |
3039 | /// template. |
3040 | llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl> & |
3041 | getPartialSpecializations() const; |
3042 | |
3043 | VarTemplateDecl(ASTContext &C, DeclContext *DC, SourceLocation L, |
3044 | DeclarationName Name, TemplateParameterList *Params, |
3045 | NamedDecl *Decl) |
3046 | : RedeclarableTemplateDecl(VarTemplate, C, DC, L, Name, Params, Decl) {} |
3047 | |
3048 | CommonBase *newCommon(ASTContext &C) const override; |
3049 | |
3050 | Common *getCommonPtr() const { |
3051 | return static_cast<Common *>(RedeclarableTemplateDecl::getCommonPtr()); |
3052 | } |
3053 | |
3054 | public: |
3055 | friend class ASTDeclReader; |
3056 | friend class ASTDeclWriter; |
3057 | |
3058 | /// Load any lazily-loaded specializations from the external source. |
3059 | void LoadLazySpecializations() const; |
3060 | |
3061 | /// Get the underlying variable declarations of the template. |
3062 | VarDecl *getTemplatedDecl() const { |
3063 | return static_cast<VarDecl *>(TemplatedDecl); |
3064 | } |
3065 | |
3066 | /// Returns whether this template declaration defines the primary |
3067 | /// variable pattern. |
3068 | bool isThisDeclarationADefinition() const { |
3069 | return getTemplatedDecl()->isThisDeclarationADefinition(); |
3070 | } |
3071 | |
3072 | VarTemplateDecl *getDefinition(); |
3073 | |
3074 | /// Create a variable template node. |
3075 | static VarTemplateDecl *Create(ASTContext &C, DeclContext *DC, |
3076 | SourceLocation L, DeclarationName Name, |
3077 | TemplateParameterList *Params, |
3078 | VarDecl *Decl); |
3079 | |
3080 | /// Create an empty variable template node. |
3081 | static VarTemplateDecl *CreateDeserialized(ASTContext &C, DeclID ID); |
3082 | |
3083 | /// Return the specialization with the provided arguments if it exists, |
3084 | /// otherwise return the insertion point. |
3085 | VarTemplateSpecializationDecl * |
3086 | findSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos); |
3087 | |
3088 | /// Insert the specified specialization knowing that it is not already |
3089 | /// in. InsertPos must be obtained from findSpecialization. |
3090 | void AddSpecialization(VarTemplateSpecializationDecl *D, void *InsertPos); |
3091 | |
3092 | VarTemplateDecl *getCanonicalDecl() override { |
3093 | return cast<VarTemplateDecl>(RedeclarableTemplateDecl::getCanonicalDecl()); |
3094 | } |
3095 | const VarTemplateDecl *getCanonicalDecl() const { |
3096 | return cast<VarTemplateDecl>(RedeclarableTemplateDecl::getCanonicalDecl()); |
3097 | } |
3098 | |
3099 | /// Retrieve the previous declaration of this variable template, or |
3100 | /// nullptr if no such declaration exists. |
3101 | VarTemplateDecl *getPreviousDecl() { |
3102 | return cast_or_null<VarTemplateDecl>( |
3103 | static_cast<RedeclarableTemplateDecl *>(this)->getPreviousDecl()); |
3104 | } |
3105 | const VarTemplateDecl *getPreviousDecl() const { |
3106 | return cast_or_null<VarTemplateDecl>( |
3107 | static_cast<const RedeclarableTemplateDecl *>( |
3108 | this)->getPreviousDecl()); |
3109 | } |
3110 | |
3111 | VarTemplateDecl *getMostRecentDecl() { |
3112 | return cast<VarTemplateDecl>( |
3113 | static_cast<RedeclarableTemplateDecl *>(this)->getMostRecentDecl()); |
3114 | } |
3115 | const VarTemplateDecl *getMostRecentDecl() const { |
3116 | return const_cast<VarTemplateDecl *>(this)->getMostRecentDecl(); |
3117 | } |
3118 | |
3119 | VarTemplateDecl *getInstantiatedFromMemberTemplate() const { |
3120 | return cast_or_null<VarTemplateDecl>( |
3121 | RedeclarableTemplateDecl::getInstantiatedFromMemberTemplate()); |
3122 | } |
3123 | |
3124 | /// Return the partial specialization with the provided arguments if it |
3125 | /// exists, otherwise return the insertion point. |
3126 | VarTemplatePartialSpecializationDecl * |
3127 | findPartialSpecialization(ArrayRef<TemplateArgument> Args, |
3128 | TemplateParameterList *TPL, void *&InsertPos); |
3129 | |
3130 | /// Insert the specified partial specialization knowing that it is not |
3131 | /// already in. InsertPos must be obtained from findPartialSpecialization. |
3132 | void AddPartialSpecialization(VarTemplatePartialSpecializationDecl *D, |
3133 | void *InsertPos); |
3134 | |
3135 | /// Retrieve the partial specializations as an ordered list. |
3136 | void getPartialSpecializations( |
3137 | SmallVectorImpl<VarTemplatePartialSpecializationDecl *> &PS) const; |
3138 | |
3139 | /// Find a variable template partial specialization which was |
3140 | /// instantiated |
3141 | /// from the given member partial specialization. |
3142 | /// |
3143 | /// \param D a member variable template partial specialization. |
3144 | /// |
3145 | /// \returns the variable template partial specialization which was |
3146 | /// instantiated |
3147 | /// from the given member partial specialization, or nullptr if no such |
3148 | /// partial specialization exists. |
3149 | VarTemplatePartialSpecializationDecl *findPartialSpecInstantiatedFromMember( |
3150 | VarTemplatePartialSpecializationDecl *D); |
3151 | |
3152 | using spec_iterator = SpecIterator<VarTemplateSpecializationDecl>; |
3153 | using spec_range = llvm::iterator_range<spec_iterator>; |
3154 | |
3155 | spec_range specializations() const { |
3156 | return spec_range(spec_begin(), spec_end()); |
3157 | } |
3158 | |
3159 | spec_iterator spec_begin() const { |
3160 | return makeSpecIterator(Specs&: getSpecializations(), isEnd: false); |
3161 | } |
3162 | |
3163 | spec_iterator spec_end() const { |
3164 | return makeSpecIterator(Specs&: getSpecializations(), isEnd: true); |
3165 | } |
3166 | |
3167 | // Implement isa/cast/dyncast support |
3168 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
3169 | static bool classofKind(Kind K) { return K == VarTemplate; } |
3170 | }; |
3171 | |
3172 | /// Declaration of a C++20 concept. |
3173 | class ConceptDecl : public TemplateDecl, public Mergeable<ConceptDecl> { |
3174 | protected: |
3175 | Expr *ConstraintExpr; |
3176 | |
3177 | ConceptDecl(DeclContext *DC, SourceLocation L, DeclarationName Name, |
3178 | TemplateParameterList *Params, Expr *ConstraintExpr) |
3179 | : TemplateDecl(Concept, DC, L, Name, Params), |
3180 | ConstraintExpr(ConstraintExpr) {}; |
3181 | public: |
3182 | static ConceptDecl *Create(ASTContext &C, DeclContext *DC, |
3183 | SourceLocation L, DeclarationName Name, |
3184 | TemplateParameterList *Params, |
3185 | Expr *ConstraintExpr); |
3186 | static ConceptDecl *CreateDeserialized(ASTContext &C, DeclID ID); |
3187 | |
3188 | Expr *getConstraintExpr() const { |
3189 | return ConstraintExpr; |
3190 | } |
3191 | |
3192 | SourceRange getSourceRange() const override LLVM_READONLY { |
3193 | return SourceRange(getTemplateParameters()->getTemplateLoc(), |
3194 | ConstraintExpr->getEndLoc()); |
3195 | } |
3196 | |
3197 | bool isTypeConcept() const { |
3198 | return isa<TemplateTypeParmDecl>(getTemplateParameters()->getParam(0)); |
3199 | } |
3200 | |
3201 | ConceptDecl *getCanonicalDecl() override { |
3202 | return cast<ConceptDecl>(Val: getPrimaryMergedDecl(this)); |
3203 | } |
3204 | const ConceptDecl *getCanonicalDecl() const { |
3205 | return const_cast<ConceptDecl *>(this)->getCanonicalDecl(); |
3206 | } |
3207 | |
3208 | // Implement isa/cast/dyncast/etc. |
3209 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
3210 | static bool classofKind(Kind K) { return K == Concept; } |
3211 | |
3212 | friend class ASTReader; |
3213 | friend class ASTDeclReader; |
3214 | friend class ASTDeclWriter; |
3215 | }; |
3216 | |
3217 | // An implementation detail of ConceptSpecialicationExpr that holds the template |
3218 | // arguments, so we can later use this to reconstitute the template arguments |
3219 | // during constraint checking. |
3220 | class ImplicitConceptSpecializationDecl final |
3221 | : public Decl, |
3222 | private llvm::TrailingObjects<ImplicitConceptSpecializationDecl, |
3223 | TemplateArgument> { |
3224 | unsigned NumTemplateArgs; |
3225 | |
3226 | ImplicitConceptSpecializationDecl(DeclContext *DC, SourceLocation SL, |
3227 | ArrayRef<TemplateArgument> ConvertedArgs); |
3228 | ImplicitConceptSpecializationDecl(EmptyShell Empty, unsigned NumTemplateArgs); |
3229 | |
3230 | public: |
3231 | static ImplicitConceptSpecializationDecl * |
3232 | Create(const ASTContext &C, DeclContext *DC, SourceLocation SL, |
3233 | ArrayRef<TemplateArgument> ConvertedArgs); |
3234 | static ImplicitConceptSpecializationDecl * |
3235 | CreateDeserialized(const ASTContext &C, DeclID ID, |
3236 | unsigned NumTemplateArgs); |
3237 | |
3238 | ArrayRef<TemplateArgument> getTemplateArguments() const { |
3239 | return ArrayRef<TemplateArgument>(getTrailingObjects<TemplateArgument>(), |
3240 | NumTemplateArgs); |
3241 | } |
3242 | void setTemplateArguments(ArrayRef<TemplateArgument> Converted); |
3243 | |
3244 | static bool classofKind(Kind K) { return K == ImplicitConceptSpecialization; } |
3245 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
3246 | |
3247 | friend TrailingObjects; |
3248 | friend class ASTDeclReader; |
3249 | }; |
3250 | |
3251 | /// A template parameter object. |
3252 | /// |
3253 | /// Template parameter objects represent values of class type used as template |
3254 | /// arguments. There is one template parameter object for each such distinct |
3255 | /// value used as a template argument across the program. |
3256 | /// |
3257 | /// \code |
3258 | /// struct A { int x, y; }; |
3259 | /// template<A> struct S; |
3260 | /// S<A{1, 2}> s1; |
3261 | /// S<A{1, 2}> s2; // same type, argument is same TemplateParamObjectDecl. |
3262 | /// \endcode |
3263 | class TemplateParamObjectDecl : public ValueDecl, |
3264 | public Mergeable<TemplateParamObjectDecl>, |
3265 | public llvm::FoldingSetNode { |
3266 | private: |
3267 | /// The value of this template parameter object. |
3268 | APValue Value; |
3269 | |
3270 | TemplateParamObjectDecl(DeclContext *DC, QualType T, const APValue &V) |
3271 | : ValueDecl(TemplateParamObject, DC, SourceLocation(), DeclarationName(), |
3272 | T), |
3273 | Value(V) {} |
3274 | |
3275 | static TemplateParamObjectDecl *Create(const ASTContext &C, QualType T, |
3276 | const APValue &V); |
3277 | static TemplateParamObjectDecl *CreateDeserialized(ASTContext &C, |
3278 | DeclID ID); |
3279 | |
3280 | /// Only ASTContext::getTemplateParamObjectDecl and deserialization |
3281 | /// create these. |
3282 | friend class ASTContext; |
3283 | friend class ASTReader; |
3284 | friend class ASTDeclReader; |
3285 | |
3286 | public: |
3287 | /// Print this template parameter object in a human-readable format. |
3288 | void printName(llvm::raw_ostream &OS, |
3289 | const PrintingPolicy &Policy) const override; |
3290 | |
3291 | /// Print this object as an equivalent expression. |
3292 | void printAsExpr(llvm::raw_ostream &OS) const; |
3293 | void printAsExpr(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const; |
3294 | |
3295 | /// Print this object as an initializer suitable for a variable of the |
3296 | /// object's type. |
3297 | void printAsInit(llvm::raw_ostream &OS) const; |
3298 | void printAsInit(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const; |
3299 | |
3300 | const APValue &getValue() const { return Value; } |
3301 | |
3302 | static void Profile(llvm::FoldingSetNodeID &ID, QualType T, |
3303 | const APValue &V) { |
3304 | ID.AddPointer(Ptr: T.getCanonicalType().getAsOpaquePtr()); |
3305 | V.Profile(ID); |
3306 | } |
3307 | void Profile(llvm::FoldingSetNodeID &ID) { |
3308 | Profile(ID, getType(), getValue()); |
3309 | } |
3310 | |
3311 | TemplateParamObjectDecl *getCanonicalDecl() override { |
3312 | return getFirstDecl(); |
3313 | } |
3314 | const TemplateParamObjectDecl *getCanonicalDecl() const { |
3315 | return getFirstDecl(); |
3316 | } |
3317 | |
3318 | static bool classof(const Decl *D) { return classofKind(K: D->getKind()); } |
3319 | static bool classofKind(Kind K) { return K == TemplateParamObject; } |
3320 | }; |
3321 | |
3322 | inline NamedDecl *getAsNamedDecl(TemplateParameter P) { |
3323 | if (auto *PD = P.dyn_cast<TemplateTypeParmDecl *>()) |
3324 | return PD; |
3325 | if (auto *PD = P.dyn_cast<NonTypeTemplateParmDecl *>()) |
3326 | return PD; |
3327 | return P.get<TemplateTemplateParmDecl *>(); |
3328 | } |
3329 | |
3330 | inline TemplateDecl *getAsTypeTemplateDecl(Decl *D) { |
3331 | auto *TD = dyn_cast<TemplateDecl>(Val: D); |
3332 | return TD && (isa<ClassTemplateDecl>(Val: TD) || |
3333 | isa<ClassTemplatePartialSpecializationDecl>(Val: TD) || |
3334 | isa<TypeAliasTemplateDecl>(Val: TD) || |
3335 | isa<TemplateTemplateParmDecl>(Val: TD)) |
3336 | ? TD |
3337 | : nullptr; |
3338 | } |
3339 | |
3340 | /// Check whether the template parameter is a pack expansion, and if so, |
3341 | /// determine the number of parameters produced by that expansion. For instance: |
3342 | /// |
3343 | /// \code |
3344 | /// template<typename ...Ts> struct A { |
3345 | /// template<Ts ...NTs, template<Ts> class ...TTs, typename ...Us> struct B; |
3346 | /// }; |
3347 | /// \endcode |
3348 | /// |
3349 | /// In \c A<int,int>::B, \c NTs and \c TTs have expanded pack size 2, and \c Us |
3350 | /// is not a pack expansion, so returns an empty Optional. |
3351 | inline std::optional<unsigned> getExpandedPackSize(const NamedDecl *Param) { |
3352 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Val: Param)) { |
3353 | if (TTP->isExpandedParameterPack()) |
3354 | return TTP->getNumExpansionParameters(); |
3355 | } |
3356 | |
3357 | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Val: Param)) { |
3358 | if (NTTP->isExpandedParameterPack()) |
3359 | return NTTP->getNumExpansionTypes(); |
3360 | } |
3361 | |
3362 | if (const auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Val: Param)) { |
3363 | if (TTP->isExpandedParameterPack()) |
3364 | return TTP->getNumExpansionTemplateParameters(); |
3365 | } |
3366 | |
3367 | return std::nullopt; |
3368 | } |
3369 | |
3370 | /// Internal helper used by Subst* nodes to retrieve the parameter list |
3371 | /// for their AssociatedDecl. |
3372 | TemplateParameterList *getReplacedTemplateParameterList(Decl *D); |
3373 | |
3374 | } // namespace clang |
3375 | |
3376 | #endif // LLVM_CLANG_AST_DECLTEMPLATE_H |
3377 | |