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/DataTypes.h"
25 class DeclGroupIterator;
28 // FIXME: Include a TypeSpecifier object.
36 DeclGroup() : NumDecls(0) {}
37 DeclGroup(unsigned numdecls, Decl** decls);
40 static DeclGroup *Create(ASTContext &C, Decl **Decls, unsigned NumDecls);
42 unsigned size() const { return NumDecls; }
44 Decl*& operator[](unsigned i) {
45 assert (i < NumDecls && "Out-of-bounds access.");
46 return ((Decl**) (this+1))[i];
49 Decl* const& operator[](unsigned i) const {
50 assert (i < NumDecls && "Out-of-bounds access.");
51 return ((Decl* const*) (this+1))[i];
56 // Note this is not a PointerIntPair because we need the address of the
57 // non-group case to be valid as a Decl** for iteration.
58 enum Kind { SingleDeclKind=0x0, DeclGroupKind=0x1, Mask=0x1 };
61 Kind getKind() const {
62 return (Kind) (reinterpret_cast<uintptr_t>(D) & Mask);
66 DeclGroupRef() : D(0) {}
68 explicit DeclGroupRef(Decl* d) : D(d) {}
69 explicit DeclGroupRef(DeclGroup* dg)
70 : D((Decl*) (reinterpret_cast<uintptr_t>(dg) | DeclGroupKind)) {}
72 static DeclGroupRef Create(ASTContext &C, Decl **Decls, unsigned NumDecls) {
74 return DeclGroupRef();
76 return DeclGroupRef(Decls[0]);
77 return DeclGroupRef(DeclGroup::Create(C, Decls, NumDecls));
80 typedef Decl** iterator;
81 typedef Decl* const * const_iterator;
83 bool isNull() const { return D == 0; }
84 bool isSingleDecl() const { return getKind() == SingleDeclKind; }
85 bool isDeclGroup() const { return getKind() == DeclGroupKind; }
87 Decl *getSingleDecl() {
88 assert(isSingleDecl() && "Isn't a declgroup");
91 const Decl *getSingleDecl() const {
92 return const_cast<DeclGroupRef*>(this)->getSingleDecl();
95 DeclGroup &getDeclGroup() {
96 assert(isDeclGroup() && "Isn't a declgroup");
97 return *((DeclGroup*)(reinterpret_cast<uintptr_t>(D) & ~Mask));
99 const DeclGroup &getDeclGroup() const {
100 return const_cast<DeclGroupRef*>(this)->getDeclGroup();
106 return &getDeclGroup()[0];
112 DeclGroup &G = getDeclGroup();
113 return &G[0] + G.size();
116 const_iterator begin() const {
119 return &getDeclGroup()[0];
122 const_iterator end() const {
125 const DeclGroup &G = getDeclGroup();
126 return &G[0] + G.size();
129 void *getAsOpaquePtr() const { return D; }
130 static DeclGroupRef getFromOpaquePtr(void *Ptr) {
132 X.D = static_cast<Decl*>(Ptr);
137 } // end clang namespace
140 // DeclGroupRef is "like a pointer", implement PointerLikeTypeTraits.
141 template <typename T>
142 class PointerLikeTypeTraits;
144 class PointerLikeTypeTraits<clang::DeclGroupRef> {
146 static inline void *getAsVoidPointer(clang::DeclGroupRef P) {
147 return P.getAsOpaquePtr();
149 static inline clang::DeclGroupRef getFromVoidPointer(void *P) {
150 return clang::DeclGroupRef::getFromOpaquePtr(P);
152 enum { NumLowBitsAvailable = 0 };