1 //===- DeclGroup.h - Classes for representing groups of Decls ---*- 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 defines the DeclGroup, DeclGroupRef, and OwningDeclGroup classes.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_DECLGROUP_H
15 #define LLVM_CLANG_AST_DECLGROUP_H
17 #include "llvm/Support/TrailingObjects.h"
26 class DeclGroup final : private llvm::TrailingObjects<DeclGroup, Decl *> {
27 // FIXME: Include a TypeSpecifier object.
28 unsigned NumDecls = 0;
31 DeclGroup() = default;
32 DeclGroup(unsigned numdecls, Decl** decls);
35 friend TrailingObjects;
37 static DeclGroup *Create(ASTContext &C, Decl **Decls, unsigned NumDecls);
39 unsigned size() const { return NumDecls; }
41 Decl*& operator[](unsigned i) {
42 assert (i < NumDecls && "Out-of-bounds access.");
43 return getTrailingObjects<Decl *>()[i];
46 Decl* const& operator[](unsigned i) const {
47 assert (i < NumDecls && "Out-of-bounds access.");
48 return getTrailingObjects<Decl *>()[i];
53 // Note this is not a PointerIntPair because we need the address of the
54 // non-group case to be valid as a Decl** for iteration.
55 enum Kind { SingleDeclKind=0x0, DeclGroupKind=0x1, Mask=0x1 };
59 Kind getKind() const {
60 return (Kind) (reinterpret_cast<uintptr_t>(D) & Mask);
64 DeclGroupRef() = default;
65 explicit DeclGroupRef(Decl* d) : D(d) {}
66 explicit DeclGroupRef(DeclGroup* dg)
67 : D((Decl*) (reinterpret_cast<uintptr_t>(dg) | DeclGroupKind)) {}
69 static DeclGroupRef Create(ASTContext &C, Decl **Decls, unsigned NumDecls) {
71 return DeclGroupRef();
73 return DeclGroupRef(Decls[0]);
74 return DeclGroupRef(DeclGroup::Create(C, Decls, NumDecls));
77 using iterator = Decl **;
78 using const_iterator = Decl * const *;
80 bool isNull() const { return D == nullptr; }
81 bool isSingleDecl() const { return getKind() == SingleDeclKind; }
82 bool isDeclGroup() const { return getKind() == DeclGroupKind; }
84 Decl *getSingleDecl() {
85 assert(isSingleDecl() && "Isn't a single decl");
88 const Decl *getSingleDecl() const {
89 return const_cast<DeclGroupRef*>(this)->getSingleDecl();
92 DeclGroup &getDeclGroup() {
93 assert(isDeclGroup() && "Isn't a declgroup");
94 return *((DeclGroup*)(reinterpret_cast<uintptr_t>(D) & ~Mask));
96 const DeclGroup &getDeclGroup() const {
97 return const_cast<DeclGroupRef*>(this)->getDeclGroup();
102 return D ? &D : nullptr;
103 return &getDeclGroup()[0];
108 return D ? &D+1 : nullptr;
109 DeclGroup &G = getDeclGroup();
110 return &G[0] + G.size();
113 const_iterator begin() const {
115 return D ? &D : nullptr;
116 return &getDeclGroup()[0];
119 const_iterator end() const {
121 return D ? &D+1 : nullptr;
122 const DeclGroup &G = getDeclGroup();
123 return &G[0] + G.size();
126 void *getAsOpaquePtr() const { return D; }
127 static DeclGroupRef getFromOpaquePtr(void *Ptr) {
129 X.D = static_cast<Decl*>(Ptr);
138 // DeclGroupRef is "like a pointer", implement PointerLikeTypeTraits.
139 template <typename T>
140 struct PointerLikeTypeTraits;
142 struct PointerLikeTypeTraits<clang::DeclGroupRef> {
143 static inline void *getAsVoidPointer(clang::DeclGroupRef P) {
144 return P.getAsOpaquePtr();
147 static inline clang::DeclGroupRef getFromVoidPointer(void *P) {
148 return clang::DeclGroupRef::getFromOpaquePtr(P);
151 enum { NumLowBitsAvailable = 0 };
156 #endif // LLVM_CLANG_AST_DECLGROUP_H