1 //===- TemplateBase.h - Core classes for C++ templates ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides definitions which are common for all kinds of
11 // template representation.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_AST_TEMPLATEBASE_H
16 #define LLVM_CLANG_AST_TEMPLATEBASE_H
18 #include "clang/AST/NestedNameSpecifier.h"
19 #include "clang/AST/TemplateName.h"
20 #include "clang/AST/Type.h"
21 #include "clang/Basic/LLVM.h"
22 #include "clang/Basic/SourceLocation.h"
23 #include "llvm/ADT/APInt.h"
24 #include "llvm/ADT/APSInt.h"
25 #include "llvm/ADT/ArrayRef.h"
26 #include "llvm/ADT/None.h"
27 #include "llvm/ADT/Optional.h"
28 #include "llvm/ADT/SmallVector.h"
29 #include "llvm/Support/Compiler.h"
30 #include "llvm/Support/TrailingObjects.h"
37 class FoldingSetNodeID;
44 class DiagnosticBuilder;
46 struct PrintingPolicy;
50 /// \brief Represents a template argument.
51 class TemplateArgument {
53 /// \brief The kind of template argument we're storing.
55 /// \brief Represents an empty template argument, e.g., one that has not
59 /// The template argument is a type.
62 /// The template argument is a declaration that was provided for a pointer,
63 /// reference, or pointer to member non-type template parameter.
66 /// The template argument is a null pointer or null pointer to member that
67 /// was provided for a non-type template parameter.
70 /// The template argument is an integral value stored in an llvm::APSInt
71 /// that was provided for an integral non-type template parameter.
74 /// The template argument is a template name that was provided for a
75 /// template template parameter.
78 /// The template argument is a pack expansion of a template name that was
79 /// provided for a template template parameter.
82 /// The template argument is an expression, and we've not resolved it to one
83 /// of the other forms yet, either because it's dependent or because we're
84 /// representing a non-canonical template argument (for instance, in a
85 /// TemplateSpecializationType). Also used to represent a non-dependent
86 /// __uuidof expression (a Microsoft extension).
89 /// The template argument is actually a parameter pack. Arguments are stored
90 /// in the Args struct.
95 /// \brief The kind of template argument we're storing.
104 // We store a decomposed APSInt with the data allocated by ASTContext if
105 // BitWidth > 64. The memory may be shared between multiple
106 // TemplateArgument instances.
107 unsigned BitWidth : 31;
108 unsigned IsUnsigned : 1;
110 /// Used to store the <= 64 bits integer value.
113 /// Used to store the >64 bits integer value.
114 const uint64_t *pVal;
121 const TemplateArgument *Args;
125 unsigned NumExpansions;
136 struct TA TemplateArg;
137 struct TV TypeOrValue;
141 /// \brief Construct an empty, invalid template argument.
142 constexpr TemplateArgument() : TypeOrValue({Null, 0}) {}
144 /// \brief Construct a template type argument.
145 TemplateArgument(QualType T, bool isNullPtr = false) {
146 TypeOrValue.Kind = isNullPtr ? NullPtr : Type;
147 TypeOrValue.V = reinterpret_cast<uintptr_t>(T.getAsOpaquePtr());
150 /// \brief Construct a template argument that refers to a
151 /// declaration, which is either an external declaration or a
152 /// template declaration.
153 TemplateArgument(ValueDecl *D, QualType QT) {
154 assert(D && "Expected decl");
155 DeclArg.Kind = Declaration;
156 DeclArg.QT = QT.getAsOpaquePtr();
160 /// \brief Construct an integral constant template argument. The memory to
161 /// store the value is allocated with Ctx.
162 TemplateArgument(ASTContext &Ctx, const llvm::APSInt &Value, QualType Type);
164 /// \brief Construct an integral constant template argument with the same
165 /// value as Other but a different type.
166 TemplateArgument(const TemplateArgument &Other, QualType Type) {
167 Integer = Other.Integer;
168 Integer.Type = Type.getAsOpaquePtr();
171 /// \brief Construct a template argument that is a template.
173 /// This form of template argument is generally used for template template
174 /// parameters. However, the template name could be a dependent template
175 /// name that ends up being instantiated to a function template whose address
178 /// \param Name The template name.
179 TemplateArgument(TemplateName Name) {
180 TemplateArg.Kind = Template;
181 TemplateArg.Name = Name.getAsVoidPointer();
182 TemplateArg.NumExpansions = 0;
185 /// \brief Construct a template argument that is a template pack expansion.
187 /// This form of template argument is generally used for template template
188 /// parameters. However, the template name could be a dependent template
189 /// name that ends up being instantiated to a function template whose address
192 /// \param Name The template name.
194 /// \param NumExpansions The number of expansions that will be generated by
196 TemplateArgument(TemplateName Name, Optional<unsigned> NumExpansions) {
197 TemplateArg.Kind = TemplateExpansion;
198 TemplateArg.Name = Name.getAsVoidPointer();
200 TemplateArg.NumExpansions = *NumExpansions + 1;
202 TemplateArg.NumExpansions = 0;
205 /// \brief Construct a template argument that is an expression.
207 /// This form of template argument only occurs in template argument
208 /// lists used for dependent types and for expression; it will not
209 /// occur in a non-dependent, canonical template argument list.
210 TemplateArgument(Expr *E) {
211 TypeOrValue.Kind = Expression;
212 TypeOrValue.V = reinterpret_cast<uintptr_t>(E);
215 /// \brief Construct a template argument that is a template argument pack.
217 /// We assume that storage for the template arguments provided
218 /// outlives the TemplateArgument itself.
219 explicit TemplateArgument(ArrayRef<TemplateArgument> Args) {
220 this->Args.Kind = Pack;
221 this->Args.Args = Args.data();
222 this->Args.NumArgs = Args.size();
225 TemplateArgument(TemplateName, bool) = delete;
227 static TemplateArgument getEmptyPack() { return TemplateArgument(None); }
229 /// \brief Create a new template argument pack by copying the given set of
230 /// template arguments.
231 static TemplateArgument CreatePackCopy(ASTContext &Context,
232 ArrayRef<TemplateArgument> Args);
234 /// \brief Return the kind of stored template argument.
235 ArgKind getKind() const { return (ArgKind)TypeOrValue.Kind; }
237 /// \brief Determine whether this template argument has no value.
238 bool isNull() const { return getKind() == Null; }
240 /// \brief Whether this template argument is dependent on a template
241 /// parameter such that its result can change from one instantiation to
243 bool isDependent() const;
245 /// \brief Whether this template argument is dependent on a template
247 bool isInstantiationDependent() const;
249 /// \brief Whether this template argument contains an unexpanded
251 bool containsUnexpandedParameterPack() const;
253 /// \brief Determine whether this template argument is a pack expansion.
254 bool isPackExpansion() const;
256 /// \brief Retrieve the type for a type template argument.
257 QualType getAsType() const {
258 assert(getKind() == Type && "Unexpected kind");
259 return QualType::getFromOpaquePtr(reinterpret_cast<void*>(TypeOrValue.V));
262 /// \brief Retrieve the declaration for a declaration non-type
263 /// template argument.
264 ValueDecl *getAsDecl() const {
265 assert(getKind() == Declaration && "Unexpected kind");
269 QualType getParamTypeForDecl() const {
270 assert(getKind() == Declaration && "Unexpected kind");
271 return QualType::getFromOpaquePtr(DeclArg.QT);
274 /// \brief Retrieve the type for null non-type template argument.
275 QualType getNullPtrType() const {
276 assert(getKind() == NullPtr && "Unexpected kind");
277 return QualType::getFromOpaquePtr(reinterpret_cast<void*>(TypeOrValue.V));
280 /// \brief Retrieve the template name for a template name argument.
281 TemplateName getAsTemplate() const {
282 assert(getKind() == Template && "Unexpected kind");
283 return TemplateName::getFromVoidPointer(TemplateArg.Name);
286 /// \brief Retrieve the template argument as a template name; if the argument
287 /// is a pack expansion, return the pattern as a template name.
288 TemplateName getAsTemplateOrTemplatePattern() const {
289 assert((getKind() == Template || getKind() == TemplateExpansion) &&
292 return TemplateName::getFromVoidPointer(TemplateArg.Name);
295 /// \brief Retrieve the number of expansions that a template template argument
296 /// expansion will produce, if known.
297 Optional<unsigned> getNumTemplateExpansions() const;
299 /// \brief Retrieve the template argument as an integral value.
300 // FIXME: Provide a way to read the integral data without copying the value.
301 llvm::APSInt getAsIntegral() const {
302 assert(getKind() == Integral && "Unexpected kind");
304 using namespace llvm;
306 if (Integer.BitWidth <= 64)
307 return APSInt(APInt(Integer.BitWidth, Integer.VAL), Integer.IsUnsigned);
309 unsigned NumWords = APInt::getNumWords(Integer.BitWidth);
310 return APSInt(APInt(Integer.BitWidth, makeArrayRef(Integer.pVal, NumWords)),
314 /// \brief Retrieve the type of the integral value.
315 QualType getIntegralType() const {
316 assert(getKind() == Integral && "Unexpected kind");
317 return QualType::getFromOpaquePtr(Integer.Type);
320 void setIntegralType(QualType T) {
321 assert(getKind() == Integral && "Unexpected kind");
322 Integer.Type = T.getAsOpaquePtr();
325 /// \brief If this is a non-type template argument, get its type. Otherwise,
326 /// returns a null QualType.
327 QualType getNonTypeTemplateArgumentType() const;
329 /// \brief Retrieve the template argument as an expression.
330 Expr *getAsExpr() const {
331 assert(getKind() == Expression && "Unexpected kind");
332 return reinterpret_cast<Expr *>(TypeOrValue.V);
335 /// \brief Iterator that traverses the elements of a template argument pack.
336 using pack_iterator = const TemplateArgument *;
338 /// \brief Iterator referencing the first argument of a template argument
340 pack_iterator pack_begin() const {
341 assert(getKind() == Pack);
345 /// \brief Iterator referencing one past the last argument of a template
347 pack_iterator pack_end() const {
348 assert(getKind() == Pack);
349 return Args.Args + Args.NumArgs;
352 /// \brief Iterator range referencing all of the elements of a template
354 ArrayRef<TemplateArgument> pack_elements() const {
355 return llvm::makeArrayRef(pack_begin(), pack_end());
358 /// \brief The number of template arguments in the given template argument
360 unsigned pack_size() const {
361 assert(getKind() == Pack);
365 /// \brief Return the array of arguments in this template argument pack.
366 ArrayRef<TemplateArgument> getPackAsArray() const {
367 assert(getKind() == Pack);
368 return llvm::makeArrayRef(Args.Args, Args.NumArgs);
371 /// \brief Determines whether two template arguments are superficially the
373 bool structurallyEquals(const TemplateArgument &Other) const;
375 /// \brief When the template argument is a pack expansion, returns
376 /// the pattern of the pack expansion.
377 TemplateArgument getPackExpansionPattern() const;
379 /// \brief Print this template argument to the given output stream.
380 void print(const PrintingPolicy &Policy, raw_ostream &Out) const;
382 /// \brief Debugging aid that dumps the template argument.
383 void dump(raw_ostream &Out) const;
385 /// \brief Debugging aid that dumps the template argument to standard error.
388 /// \brief Used to insert TemplateArguments into FoldingSets.
389 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) const;
392 /// Location information for a TemplateArgument.
393 struct TemplateArgumentLocInfo {
396 // FIXME: We'd like to just use the qualifier in the TemplateName,
397 // but template arguments get canonicalized too quickly.
398 NestedNameSpecifier *Qualifier;
399 void *QualifierLocData;
400 unsigned TemplateNameLoc;
401 unsigned EllipsisLoc;
407 TypeSourceInfo *Declarator;
411 constexpr TemplateArgumentLocInfo() : Template({nullptr, nullptr, 0, 0}) {}
413 TemplateArgumentLocInfo(TypeSourceInfo *TInfo) : Declarator(TInfo) {}
415 TemplateArgumentLocInfo(Expr *E) : Expression(E) {}
417 TemplateArgumentLocInfo(NestedNameSpecifierLoc QualifierLoc,
418 SourceLocation TemplateNameLoc,
419 SourceLocation EllipsisLoc) {
420 Template.Qualifier = QualifierLoc.getNestedNameSpecifier();
421 Template.QualifierLocData = QualifierLoc.getOpaqueData();
422 Template.TemplateNameLoc = TemplateNameLoc.getRawEncoding();
423 Template.EllipsisLoc = EllipsisLoc.getRawEncoding();
426 TypeSourceInfo *getAsTypeSourceInfo() const {
430 Expr *getAsExpr() const {
434 NestedNameSpecifierLoc getTemplateQualifierLoc() const {
435 return NestedNameSpecifierLoc(Template.Qualifier,
436 Template.QualifierLocData);
439 SourceLocation getTemplateNameLoc() const {
440 return SourceLocation::getFromRawEncoding(Template.TemplateNameLoc);
443 SourceLocation getTemplateEllipsisLoc() const {
444 return SourceLocation::getFromRawEncoding(Template.EllipsisLoc);
448 /// Location wrapper for a TemplateArgument. TemplateArgument is to
449 /// TemplateArgumentLoc as Type is to TypeLoc.
450 class TemplateArgumentLoc {
451 TemplateArgument Argument;
452 TemplateArgumentLocInfo LocInfo;
455 constexpr TemplateArgumentLoc() {}
457 TemplateArgumentLoc(const TemplateArgument &Argument,
458 TemplateArgumentLocInfo Opaque)
459 : Argument(Argument), LocInfo(Opaque) {}
461 TemplateArgumentLoc(const TemplateArgument &Argument, TypeSourceInfo *TInfo)
462 : Argument(Argument), LocInfo(TInfo) {
463 assert(Argument.getKind() == TemplateArgument::Type);
466 TemplateArgumentLoc(const TemplateArgument &Argument, Expr *E)
467 : Argument(Argument), LocInfo(E) {
468 assert(Argument.getKind() == TemplateArgument::Expression);
471 TemplateArgumentLoc(const TemplateArgument &Argument,
472 NestedNameSpecifierLoc QualifierLoc,
473 SourceLocation TemplateNameLoc,
474 SourceLocation EllipsisLoc = SourceLocation())
475 : Argument(Argument),
476 LocInfo(QualifierLoc, TemplateNameLoc, EllipsisLoc) {
477 assert(Argument.getKind() == TemplateArgument::Template ||
478 Argument.getKind() == TemplateArgument::TemplateExpansion);
481 /// \brief - Fetches the primary location of the argument.
482 SourceLocation getLocation() const {
483 if (Argument.getKind() == TemplateArgument::Template ||
484 Argument.getKind() == TemplateArgument::TemplateExpansion)
485 return getTemplateNameLoc();
487 return getSourceRange().getBegin();
490 /// \brief - Fetches the full source range of the argument.
491 SourceRange getSourceRange() const LLVM_READONLY;
493 const TemplateArgument &getArgument() const {
497 TemplateArgumentLocInfo getLocInfo() const {
501 TypeSourceInfo *getTypeSourceInfo() const {
502 assert(Argument.getKind() == TemplateArgument::Type);
503 return LocInfo.getAsTypeSourceInfo();
506 Expr *getSourceExpression() const {
507 assert(Argument.getKind() == TemplateArgument::Expression);
508 return LocInfo.getAsExpr();
511 Expr *getSourceDeclExpression() const {
512 assert(Argument.getKind() == TemplateArgument::Declaration);
513 return LocInfo.getAsExpr();
516 Expr *getSourceNullPtrExpression() const {
517 assert(Argument.getKind() == TemplateArgument::NullPtr);
518 return LocInfo.getAsExpr();
521 Expr *getSourceIntegralExpression() const {
522 assert(Argument.getKind() == TemplateArgument::Integral);
523 return LocInfo.getAsExpr();
526 NestedNameSpecifierLoc getTemplateQualifierLoc() const {
527 assert(Argument.getKind() == TemplateArgument::Template ||
528 Argument.getKind() == TemplateArgument::TemplateExpansion);
529 return LocInfo.getTemplateQualifierLoc();
532 SourceLocation getTemplateNameLoc() const {
533 assert(Argument.getKind() == TemplateArgument::Template ||
534 Argument.getKind() == TemplateArgument::TemplateExpansion);
535 return LocInfo.getTemplateNameLoc();
538 SourceLocation getTemplateEllipsisLoc() const {
539 assert(Argument.getKind() == TemplateArgument::TemplateExpansion);
540 return LocInfo.getTemplateEllipsisLoc();
544 /// A convenient class for passing around template argument
545 /// information. Designed to be passed by reference.
546 class TemplateArgumentListInfo {
547 SmallVector<TemplateArgumentLoc, 8> Arguments;
548 SourceLocation LAngleLoc;
549 SourceLocation RAngleLoc;
552 TemplateArgumentListInfo() = default;
554 TemplateArgumentListInfo(SourceLocation LAngleLoc,
555 SourceLocation RAngleLoc)
556 : LAngleLoc(LAngleLoc), RAngleLoc(RAngleLoc) {}
558 // This can leak if used in an AST node, use ASTTemplateArgumentListInfo
560 void *operator new(size_t bytes, ASTContext &C) = delete;
562 SourceLocation getLAngleLoc() const { return LAngleLoc; }
563 SourceLocation getRAngleLoc() const { return RAngleLoc; }
565 void setLAngleLoc(SourceLocation Loc) { LAngleLoc = Loc; }
566 void setRAngleLoc(SourceLocation Loc) { RAngleLoc = Loc; }
568 unsigned size() const { return Arguments.size(); }
570 const TemplateArgumentLoc *getArgumentArray() const {
571 return Arguments.data();
574 llvm::ArrayRef<TemplateArgumentLoc> arguments() const {
578 const TemplateArgumentLoc &operator[](unsigned I) const {
582 TemplateArgumentLoc &operator[](unsigned I) {
586 void addArgument(const TemplateArgumentLoc &Loc) {
587 Arguments.push_back(Loc);
591 /// \brief Represents an explicit template argument list in C++, e.g.,
592 /// the "<int>" in "sort<int>".
593 /// This is safe to be used inside an AST node, in contrast with
594 /// TemplateArgumentListInfo.
595 struct ASTTemplateArgumentListInfo final
596 : private llvm::TrailingObjects<ASTTemplateArgumentListInfo,
597 TemplateArgumentLoc> {
599 friend class ASTNodeImporter;
600 friend TrailingObjects;
602 ASTTemplateArgumentListInfo(const TemplateArgumentListInfo &List);
605 /// \brief The source location of the left angle bracket ('<').
606 SourceLocation LAngleLoc;
608 /// \brief The source location of the right angle bracket ('>').
609 SourceLocation RAngleLoc;
611 /// \brief The number of template arguments in TemplateArgs.
612 unsigned NumTemplateArgs;
614 /// \brief Retrieve the template arguments
615 const TemplateArgumentLoc *getTemplateArgs() const {
616 return getTrailingObjects<TemplateArgumentLoc>();
619 llvm::ArrayRef<TemplateArgumentLoc> arguments() const {
620 return llvm::makeArrayRef(getTemplateArgs(), NumTemplateArgs);
623 const TemplateArgumentLoc &operator[](unsigned I) const {
624 return getTemplateArgs()[I];
627 static const ASTTemplateArgumentListInfo *
628 Create(ASTContext &C, const TemplateArgumentListInfo &List);
631 /// \brief Represents an explicit template argument list in C++, e.g.,
632 /// the "<int>" in "sort<int>".
634 /// It is intended to be used as a trailing object on AST nodes, and
635 /// as such, doesn't contain the array of TemplateArgumentLoc itself,
636 /// but expects the containing object to also provide storage for
638 struct alignas(void *) ASTTemplateKWAndArgsInfo {
639 /// \brief The source location of the left angle bracket ('<').
640 SourceLocation LAngleLoc;
642 /// \brief The source location of the right angle bracket ('>').
643 SourceLocation RAngleLoc;
645 /// \brief The source location of the template keyword; this is used
646 /// as part of the representation of qualified identifiers, such as
647 /// S<T>::template apply<T>. Will be empty if this expression does
648 /// not have a template keyword.
649 SourceLocation TemplateKWLoc;
651 /// \brief The number of template arguments in TemplateArgs.
652 unsigned NumTemplateArgs;
654 void initializeFrom(SourceLocation TemplateKWLoc,
655 const TemplateArgumentListInfo &List,
656 TemplateArgumentLoc *OutArgArray);
657 void initializeFrom(SourceLocation TemplateKWLoc,
658 const TemplateArgumentListInfo &List,
659 TemplateArgumentLoc *OutArgArray, bool &Dependent,
660 bool &InstantiationDependent,
661 bool &ContainsUnexpandedParameterPack);
662 void initializeFrom(SourceLocation TemplateKWLoc);
664 void copyInto(const TemplateArgumentLoc *ArgArray,
665 TemplateArgumentListInfo &List) const;
668 const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
669 const TemplateArgument &Arg);
671 inline TemplateSpecializationType::iterator
672 TemplateSpecializationType::end() const {
673 return getArgs() + getNumArgs();
676 inline DependentTemplateSpecializationType::iterator
677 DependentTemplateSpecializationType::end() const {
678 return getArgs() + getNumArgs();
681 inline const TemplateArgument &
682 TemplateSpecializationType::getArg(unsigned Idx) const {
683 assert(Idx < getNumArgs() && "Template argument out of range");
684 return getArgs()[Idx];
687 inline const TemplateArgument &
688 DependentTemplateSpecializationType::getArg(unsigned Idx) const {
689 assert(Idx < getNumArgs() && "Template argument out of range");
690 return getArgs()[Idx];
695 #endif // LLVM_CLANG_AST_TEMPLATEBASE_H