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