]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/llvm/tools/clang/include/clang/AST/DeclGroup.h
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / llvm / tools / clang / include / clang / AST / DeclGroup.h
1 //===--- DeclGroup.h - Classes for representing groups of Decls -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the DeclGroup, DeclGroupRef, and OwningDeclGroup classes.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_AST_DECLGROUP_H
15 #define LLVM_CLANG_AST_DECLGROUP_H
16
17 #include "llvm/Support/DataTypes.h"
18 #include <cassert>
19
20 namespace clang {
21
22 class ASTContext;
23 class Decl;
24 class DeclGroup;
25 class DeclGroupIterator;
26
27 class DeclGroup {
28   // FIXME: Include a TypeSpecifier object.
29   unsigned NumDecls;
30
31 private:
32   DeclGroup() : NumDecls(0) {}
33   DeclGroup(unsigned numdecls, Decl** decls);
34
35 public:
36   static DeclGroup *Create(ASTContext &C, Decl **Decls, unsigned NumDecls);
37
38   unsigned size() const { return NumDecls; }
39
40   Decl*& operator[](unsigned i) {
41     assert (i < NumDecls && "Out-of-bounds access.");
42     return ((Decl**) (this+1))[i];
43   }
44
45   Decl* const& operator[](unsigned i) const {
46     assert (i < NumDecls && "Out-of-bounds access.");
47     return ((Decl* const*) (this+1))[i];
48   }
49 };
50
51 class DeclGroupRef {
52   // Note this is not a PointerIntPair because we need the address of the
53   // non-group case to be valid as a Decl** for iteration.
54   enum Kind { SingleDeclKind=0x0, DeclGroupKind=0x1, Mask=0x1 };
55   Decl* D;
56
57   Kind getKind() const {
58     return (Kind) (reinterpret_cast<uintptr_t>(D) & Mask);
59   }
60
61 public:
62   DeclGroupRef() : D(0) {}
63
64   explicit DeclGroupRef(Decl* d) : D(d) {}
65   explicit DeclGroupRef(DeclGroup* dg)
66     : D((Decl*) (reinterpret_cast<uintptr_t>(dg) | DeclGroupKind)) {}
67
68   static DeclGroupRef Create(ASTContext &C, Decl **Decls, unsigned NumDecls) {
69     if (NumDecls == 0)
70       return DeclGroupRef();
71     if (NumDecls == 1)
72       return DeclGroupRef(Decls[0]);
73     return DeclGroupRef(DeclGroup::Create(C, Decls, NumDecls));
74   }
75
76   typedef Decl** iterator;
77   typedef Decl* const * const_iterator;
78
79   bool isNull() const { return D == 0; }
80   bool isSingleDecl() const { return getKind() == SingleDeclKind; }
81   bool isDeclGroup() const { return getKind() == DeclGroupKind; }
82
83   Decl *getSingleDecl() {
84     assert(isSingleDecl() && "Isn't a declgroup");
85     return D;
86   }
87   const Decl *getSingleDecl() const {
88     return const_cast<DeclGroupRef*>(this)->getSingleDecl();
89   }
90
91   DeclGroup &getDeclGroup() {
92     assert(isDeclGroup() && "Isn't a declgroup");
93     return *((DeclGroup*)(reinterpret_cast<uintptr_t>(D) & ~Mask));
94   }
95   const DeclGroup &getDeclGroup() const {
96     return const_cast<DeclGroupRef*>(this)->getDeclGroup();
97   }
98
99   iterator begin() {
100     if (isSingleDecl())
101       return D ? &D : 0;
102     return &getDeclGroup()[0];
103   }
104
105   iterator end() {
106     if (isSingleDecl())
107       return D ? &D+1 : 0;
108     DeclGroup &G = getDeclGroup();
109     return &G[0] + G.size();
110   }
111
112   const_iterator begin() const {
113     if (isSingleDecl())
114       return D ? &D : 0;
115     return &getDeclGroup()[0];
116   }
117
118   const_iterator end() const {
119     if (isSingleDecl())
120       return D ? &D+1 : 0;
121     const DeclGroup &G = getDeclGroup();
122     return &G[0] + G.size();
123   }
124
125   void *getAsOpaquePtr() const { return D; }
126   static DeclGroupRef getFromOpaquePtr(void *Ptr) {
127     DeclGroupRef X;
128     X.D = static_cast<Decl*>(Ptr);
129     return X;
130   }
131 };
132
133 } // end clang namespace
134
135 namespace llvm {
136   // DeclGroupRef is "like a pointer", implement PointerLikeTypeTraits.
137   template <typename T>
138   class PointerLikeTypeTraits;
139   template <>
140   class PointerLikeTypeTraits<clang::DeclGroupRef> {
141   public:
142     static inline void *getAsVoidPointer(clang::DeclGroupRef P) {
143       return P.getAsOpaquePtr();
144     }
145     static inline clang::DeclGroupRef getFromVoidPointer(void *P) {
146       return clang::DeclGroupRef::getFromOpaquePtr(P);
147     }
148     enum { NumLowBitsAvailable = 0 };
149   };
150 }
151 #endif