1 //===--- ASTTypeTraits.h ----------------------------------------*- 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 // Provides a dynamic type identifier and a dynamically typed node container
11 // that can be used to store an AST base node at runtime in the same storage in
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_AST_ASTTYPETRAITS_H
17 #define LLVM_CLANG_AST_ASTTYPETRAITS_H
19 #include "clang/AST/ASTFwd.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/NestedNameSpecifier.h"
22 #include "clang/AST/Stmt.h"
23 #include "clang/AST/TemplateBase.h"
24 #include "clang/AST/TypeLoc.h"
25 #include "clang/Basic/LLVM.h"
26 #include "llvm/ADT/DenseMapInfo.h"
27 #include "llvm/Support/AlignOf.h"
37 struct PrintingPolicy;
39 namespace ast_type_traits {
41 /// \brief Kind identifier.
43 /// It can be constructed from any node kind and allows for runtime type
45 /// Use getFromNodeKind<T>() to construct them.
48 /// \brief Empty identifier. It matches nothing.
49 ASTNodeKind() : KindId(NKI_None) {}
51 /// \brief Construct an identifier for T.
53 static ASTNodeKind getFromNodeKind() {
54 return ASTNodeKind(KindToKindId<T>::Id);
58 /// \brief Construct an identifier for the dynamic type of the node
59 static ASTNodeKind getFromNode(const Decl &D);
60 static ASTNodeKind getFromNode(const Stmt &S);
61 static ASTNodeKind getFromNode(const Type &T);
64 /// \brief Returns \c true if \c this and \c Other represent the same kind.
65 bool isSame(ASTNodeKind Other) const;
67 /// \brief Returns \c true only for the default \c ASTNodeKind()
68 bool isNone() const { return KindId == NKI_None; }
70 /// \brief Returns \c true if \c this is a base kind of (or same as) \c Other.
71 /// \param Distance If non-null, used to return the distance between \c this
72 /// and \c Other in the class hierarchy.
73 bool isBaseOf(ASTNodeKind Other, unsigned *Distance = nullptr) const;
75 /// \brief String representation of the kind.
76 StringRef asStringRef() const;
78 /// \brief Strict weak ordering for ASTNodeKind.
79 bool operator<(const ASTNodeKind &Other) const {
80 return KindId < Other.KindId;
83 /// \brief Return the most derived type between \p Kind1 and \p Kind2.
85 /// Return ASTNodeKind() if they are not related.
86 static ASTNodeKind getMostDerivedType(ASTNodeKind Kind1, ASTNodeKind Kind2);
88 /// \brief Return the most derived common ancestor between Kind1 and Kind2.
90 /// Return ASTNodeKind() if they are not related.
91 static ASTNodeKind getMostDerivedCommonAncestor(ASTNodeKind Kind1,
94 /// \brief Hooks for using ASTNodeKind as a key in a DenseMap.
96 // ASTNodeKind() is a good empty key because it is represented as a 0.
97 static inline ASTNodeKind getEmptyKey() { return ASTNodeKind(); }
98 // NKI_NumberOfKinds is not a valid value, so it is good for a
100 static inline ASTNodeKind getTombstoneKey() {
101 return ASTNodeKind(NKI_NumberOfKinds);
103 static unsigned getHashValue(const ASTNodeKind &Val) { return Val.KindId; }
104 static bool isEqual(const ASTNodeKind &LHS, const ASTNodeKind &RHS) {
105 return LHS.KindId == RHS.KindId;
112 /// Includes all possible base and derived kinds.
115 NKI_CXXCtorInitializer,
116 NKI_TemplateArgument,
117 NKI_NestedNameSpecifier,
118 NKI_NestedNameSpecifierLoc,
122 #define DECL(DERIVED, BASE) NKI_##DERIVED##Decl,
123 #include "clang/AST/DeclNodes.inc"
125 #define STMT(DERIVED, BASE) NKI_##DERIVED,
126 #include "clang/AST/StmtNodes.inc"
128 #define TYPE(DERIVED, BASE) NKI_##DERIVED##Type,
129 #include "clang/AST/TypeNodes.def"
133 /// \brief Use getFromNodeKind<T>() to construct the kind.
134 ASTNodeKind(NodeKindId KindId) : KindId(KindId) {}
136 /// \brief Returns \c true if \c Base is a base kind of (or same as) \c
138 /// \param Distance If non-null, used to return the distance between \c Base
139 /// and \c Derived in the class hierarchy.
140 static bool isBaseOf(NodeKindId Base, NodeKindId Derived, unsigned *Distance);
142 /// \brief Helper meta-function to convert a kind T to its enum value.
144 /// This struct is specialized below for all known kinds.
145 template <class T> struct KindToKindId {
146 static const NodeKindId Id = NKI_None;
149 struct KindToKindId<const T> : KindToKindId<T> {};
151 /// \brief Per kind info.
153 /// \brief The id of the parent kind, or None if it has no parent.
155 /// \brief Name of the kind.
158 static const KindInfo AllKindInfo[NKI_NumberOfKinds];
163 #define KIND_TO_KIND_ID(Class) \
164 template <> struct ASTNodeKind::KindToKindId<Class> { \
165 static const NodeKindId Id = NKI_##Class; \
167 KIND_TO_KIND_ID(CXXCtorInitializer)
168 KIND_TO_KIND_ID(TemplateArgument)
169 KIND_TO_KIND_ID(NestedNameSpecifier)
170 KIND_TO_KIND_ID(NestedNameSpecifierLoc)
171 KIND_TO_KIND_ID(QualType)
172 KIND_TO_KIND_ID(TypeLoc)
173 KIND_TO_KIND_ID(Decl)
174 KIND_TO_KIND_ID(Stmt)
175 KIND_TO_KIND_ID(Type)
176 #define DECL(DERIVED, BASE) KIND_TO_KIND_ID(DERIVED##Decl)
177 #include "clang/AST/DeclNodes.inc"
178 #define STMT(DERIVED, BASE) KIND_TO_KIND_ID(DERIVED)
179 #include "clang/AST/StmtNodes.inc"
180 #define TYPE(DERIVED, BASE) KIND_TO_KIND_ID(DERIVED##Type)
181 #include "clang/AST/TypeNodes.def"
182 #undef KIND_TO_KIND_ID
184 inline raw_ostream &operator<<(raw_ostream &OS, ASTNodeKind K) {
185 OS << K.asStringRef();
189 /// \brief A dynamically typed AST node container.
191 /// Stores an AST node in a type safe way. This allows writing code that
192 /// works with different kinds of AST nodes, despite the fact that they don't
193 /// have a common base class.
195 /// Use \c create(Node) to create a \c DynTypedNode from an AST node,
196 /// and \c get<T>() to retrieve the node as type T if the types match.
198 /// See \c ASTNodeKind for which node base types are currently supported;
199 /// You can create DynTypedNodes for all nodes in the inheritance hierarchy of
200 /// the supported base types.
203 /// \brief Creates a \c DynTypedNode from \c Node.
204 template <typename T>
205 static DynTypedNode create(const T &Node) {
206 return BaseConverter<T>::create(Node);
209 /// \brief Retrieve the stored node as type \c T.
211 /// Returns NULL if the stored node does not have a type that is
212 /// convertible to \c T.
214 /// For types that have identity via their pointer in the AST
215 /// (like \c Stmt, \c Decl, \c Type and \c NestedNameSpecifier) the returned
216 /// pointer points to the referenced AST node.
217 /// For other types (like \c QualType) the value is stored directly
218 /// in the \c DynTypedNode, and the returned pointer points at
219 /// the storage inside DynTypedNode. For those nodes, do not
220 /// use the pointer outside the scope of the DynTypedNode.
221 template <typename T>
222 const T *get() const {
223 return BaseConverter<T>::get(NodeKind, Storage.buffer);
226 /// \brief Retrieve the stored node as type \c T.
228 /// Similar to \c get(), but asserts that the type is what we are expecting.
229 template <typename T>
230 const T &getUnchecked() const {
231 return BaseConverter<T>::getUnchecked(NodeKind, Storage.buffer);
234 ASTNodeKind getNodeKind() const { return NodeKind; }
236 /// \brief Returns a pointer that identifies the stored AST node.
238 /// Note that this is not supported by all AST nodes. For AST nodes
239 /// that don't have a pointer-defined identity inside the AST, this
240 /// method returns NULL.
241 const void *getMemoizationData() const { return MemoizationData; }
243 /// \brief Prints the node to the given output stream.
244 void print(llvm::raw_ostream &OS, const PrintingPolicy &PP) const;
246 /// \brief Dumps the node to the given output stream.
247 void dump(llvm::raw_ostream &OS, SourceManager &SM) const;
249 /// \brief For nodes which represent textual entities in the source code,
250 /// return their SourceRange. For all other nodes, return SourceRange().
251 SourceRange getSourceRange() const;
254 /// \brief Imposes an order on \c DynTypedNode.
256 /// Supports comparison of nodes that support memoization.
257 /// FIXME: Implement comparsion for other node types (currently
258 /// only Stmt, Decl, Type and NestedNameSpecifier return memoization data).
259 bool operator<(const DynTypedNode &Other) const {
260 assert(getMemoizationData() && Other.getMemoizationData());
261 return getMemoizationData() < Other.getMemoizationData();
263 bool operator==(const DynTypedNode &Other) const {
264 // DynTypedNode::create() stores the exact kind of the node in NodeKind.
265 // If they contain the same node, their NodeKind must be the same.
266 if (!NodeKind.isSame(Other.NodeKind))
269 // FIXME: Implement for other types.
270 if (ASTNodeKind::getFromNodeKind<QualType>().isSame(NodeKind))
271 return getUnchecked<QualType>() == Other.getUnchecked<QualType>();
273 assert(getMemoizationData() && Other.getMemoizationData());
274 return getMemoizationData() == Other.getMemoizationData();
276 bool operator!=(const DynTypedNode &Other) const {
277 return !operator==(Other);
282 /// \brief Takes care of converting from and to \c T.
283 template <typename T, typename EnablerT = void> struct BaseConverter;
285 /// \brief Converter that uses dyn_cast<T> from a stored BaseT*.
286 template <typename T, typename BaseT> struct DynCastPtrConverter {
287 static const T *get(ASTNodeKind NodeKind, const char Storage[]) {
288 if (ASTNodeKind::getFromNodeKind<T>().isBaseOf(NodeKind))
289 return cast<T>(*reinterpret_cast<BaseT *const *>(Storage));
292 static const T &getUnchecked(ASTNodeKind NodeKind, const char Storage[]) {
293 assert(ASTNodeKind::getFromNodeKind<T>().isBaseOf(NodeKind));
294 return *cast<T>(*reinterpret_cast<BaseT *const *>(Storage));
296 static DynTypedNode create(const BaseT &Node) {
298 Result.NodeKind = ASTNodeKind::getFromNode(Node);
299 Result.MemoizationData = &Node;
300 new (Result.Storage.buffer) const BaseT * (&Node);
305 /// \brief Converter that stores T* (by pointer).
306 template <typename T> struct PtrConverter {
307 static const T *get(ASTNodeKind NodeKind, const char Storage[]) {
308 if (ASTNodeKind::getFromNodeKind<T>().isSame(NodeKind))
309 return *reinterpret_cast<T *const *>(Storage);
312 static const T &getUnchecked(ASTNodeKind NodeKind, const char Storage[]) {
313 assert(ASTNodeKind::getFromNodeKind<T>().isSame(NodeKind));
314 return **reinterpret_cast<T *const *>(Storage);
316 static DynTypedNode create(const T &Node) {
318 Result.NodeKind = ASTNodeKind::getFromNodeKind<T>();
319 Result.MemoizationData = &Node;
320 new (Result.Storage.buffer) const T * (&Node);
325 /// \brief Converter that stores T (by value).
326 template <typename T> struct ValueConverter {
327 static const T *get(ASTNodeKind NodeKind, const char Storage[]) {
328 if (ASTNodeKind::getFromNodeKind<T>().isSame(NodeKind))
329 return reinterpret_cast<const T *>(Storage);
332 static const T &getUnchecked(ASTNodeKind NodeKind, const char Storage[]) {
333 assert(ASTNodeKind::getFromNodeKind<T>().isSame(NodeKind));
334 return *reinterpret_cast<const T *>(Storage);
336 static DynTypedNode create(const T &Node) {
338 Result.NodeKind = ASTNodeKind::getFromNodeKind<T>();
339 Result.MemoizationData = nullptr;
340 new (Result.Storage.buffer) T(Node);
345 ASTNodeKind NodeKind;
346 const void *MemoizationData;
348 /// \brief Stores the data of the node.
350 /// Note that we can store \c Decls, \c Stmts, \c Types,
351 /// \c NestedNameSpecifiers and \c CXXCtorInitializer by pointer as they are
352 /// guaranteed to be unique pointers pointing to dedicated storage in the AST.
353 /// \c QualTypes, \c NestedNameSpecifierLocs, \c TypeLocs and
354 /// \c TemplateArguments on the other hand do not have storage or unique
355 /// pointers and thus need to be stored by value.
356 typedef llvm::AlignedCharArrayUnion<
357 Decl *, Stmt *, Type *, NestedNameSpecifier *, CXXCtorInitializer *>
359 llvm::AlignedCharArrayUnion<KindsByPointer, TemplateArgument,
360 NestedNameSpecifierLoc, QualType, TypeLoc>
364 template <typename T>
365 struct DynTypedNode::BaseConverter<
366 T, typename std::enable_if<std::is_base_of<Decl, T>::value>::type>
367 : public DynCastPtrConverter<T, Decl> {};
369 template <typename T>
370 struct DynTypedNode::BaseConverter<
371 T, typename std::enable_if<std::is_base_of<Stmt, T>::value>::type>
372 : public DynCastPtrConverter<T, Stmt> {};
374 template <typename T>
375 struct DynTypedNode::BaseConverter<
376 T, typename std::enable_if<std::is_base_of<Type, T>::value>::type>
377 : public DynCastPtrConverter<T, Type> {};
380 struct DynTypedNode::BaseConverter<
381 NestedNameSpecifier, void> : public PtrConverter<NestedNameSpecifier> {};
384 struct DynTypedNode::BaseConverter<
385 CXXCtorInitializer, void> : public PtrConverter<CXXCtorInitializer> {};
388 struct DynTypedNode::BaseConverter<
389 TemplateArgument, void> : public ValueConverter<TemplateArgument> {};
392 struct DynTypedNode::BaseConverter<
393 NestedNameSpecifierLoc,
394 void> : public ValueConverter<NestedNameSpecifierLoc> {};
397 struct DynTypedNode::BaseConverter<QualType,
398 void> : public ValueConverter<QualType> {};
401 struct DynTypedNode::BaseConverter<
402 TypeLoc, void> : public ValueConverter<TypeLoc> {};
404 // The only operation we allow on unsupported types is \c get.
405 // This allows to conveniently use \c DynTypedNode when having an arbitrary
406 // AST node that is not supported, but prevents misuse - a user cannot create
407 // a DynTypedNode from arbitrary types.
408 template <typename T, typename EnablerT> struct DynTypedNode::BaseConverter {
409 static const T *get(ASTNodeKind NodeKind, const char Storage[]) {
414 } // end namespace ast_type_traits
415 } // end namespace clang
420 struct DenseMapInfo<clang::ast_type_traits::ASTNodeKind>
421 : clang::ast_type_traits::ASTNodeKind::DenseMapInfo {};
423 } // end namespace llvm