clang  6.0.0svn
DeclGroup.h
Go to the documentation of this file.
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/TrailingObjects.h"
18 #include <cassert>
19 #include <cstdint>
20 
21 namespace clang {
22 
23 class ASTContext;
24 class Decl;
25 
26 class DeclGroup final : private llvm::TrailingObjects<DeclGroup, Decl *> {
27  // FIXME: Include a TypeSpecifier object.
28  unsigned NumDecls = 0;
29 
30 private:
31  DeclGroup() = default;
32  DeclGroup(unsigned numdecls, Decl** decls);
33 
34 public:
36 
37  static DeclGroup *Create(ASTContext &C, Decl **Decls, unsigned NumDecls);
38 
39  unsigned size() const { return NumDecls; }
40 
41  Decl*& operator[](unsigned i) {
42  assert (i < NumDecls && "Out-of-bounds access.");
43  return getTrailingObjects<Decl *>()[i];
44  }
45 
46  Decl* const& operator[](unsigned i) const {
47  assert (i < NumDecls && "Out-of-bounds access.");
48  return getTrailingObjects<Decl *>()[i];
49  }
50 };
51 
52 class DeclGroupRef {
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 };
56 
57  Decl* D = nullptr;
58 
59  Kind getKind() const {
60  return (Kind) (reinterpret_cast<uintptr_t>(D) & Mask);
61  }
62 
63 public:
64  DeclGroupRef() = default;
65  explicit DeclGroupRef(Decl* d) : D(d) {}
66  explicit DeclGroupRef(DeclGroup* dg)
67  : D((Decl*) (reinterpret_cast<uintptr_t>(dg) | DeclGroupKind)) {}
68 
69  static DeclGroupRef Create(ASTContext &C, Decl **Decls, unsigned NumDecls) {
70  if (NumDecls == 0)
71  return DeclGroupRef();
72  if (NumDecls == 1)
73  return DeclGroupRef(Decls[0]);
74  return DeclGroupRef(DeclGroup::Create(C, Decls, NumDecls));
75  }
76 
77  using iterator = Decl **;
78  using const_iterator = Decl * const *;
79 
80  bool isNull() const { return D == nullptr; }
81  bool isSingleDecl() const { return getKind() == SingleDeclKind; }
82  bool isDeclGroup() const { return getKind() == DeclGroupKind; }
83 
85  assert(isSingleDecl() && "Isn't a single decl");
86  return D;
87  }
88  const Decl *getSingleDecl() const {
89  return const_cast<DeclGroupRef*>(this)->getSingleDecl();
90  }
91 
93  assert(isDeclGroup() && "Isn't a declgroup");
94  return *((DeclGroup*)(reinterpret_cast<uintptr_t>(D) & ~Mask));
95  }
96  const DeclGroup &getDeclGroup() const {
97  return const_cast<DeclGroupRef*>(this)->getDeclGroup();
98  }
99 
101  if (isSingleDecl())
102  return D ? &D : nullptr;
103  return &getDeclGroup()[0];
104  }
105 
107  if (isSingleDecl())
108  return D ? &D+1 : nullptr;
109  DeclGroup &G = getDeclGroup();
110  return &G[0] + G.size();
111  }
112 
114  if (isSingleDecl())
115  return D ? &D : nullptr;
116  return &getDeclGroup()[0];
117  }
118 
119  const_iterator end() const {
120  if (isSingleDecl())
121  return D ? &D+1 : nullptr;
122  const DeclGroup &G = getDeclGroup();
123  return &G[0] + G.size();
124  }
125 
126  void *getAsOpaquePtr() const { return D; }
127  static DeclGroupRef getFromOpaquePtr(void *Ptr) {
128  DeclGroupRef X;
129  X.D = static_cast<Decl*>(Ptr);
130  return X;
131  }
132 };
133 
134 } // namespace clang
135 
136 namespace llvm {
137 
138  // DeclGroupRef is "like a pointer", implement PointerLikeTypeTraits.
139  template <typename T>
141  template <>
142  struct PointerLikeTypeTraits<clang::DeclGroupRef> {
143  static inline void *getAsVoidPointer(clang::DeclGroupRef P) {
144  return P.getAsOpaquePtr();
145  }
146 
147  static inline clang::DeclGroupRef getFromVoidPointer(void *P) {
149  }
150 
151  enum { NumLowBitsAvailable = 0 };
152  };
153 
154 } // namespace llvm
155 
156 #endif // LLVM_CLANG_AST_DECLGROUP_H
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:26
void * getAsOpaquePtr() const
Definition: DeclGroup.h:126
DeclGroup & getDeclGroup()
Definition: DeclGroup.h:92
Decl *const & operator[](unsigned i) const
Definition: DeclGroup.h:46
iterator end()
Definition: DeclGroup.h:106
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
StringRef P
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
Decl *& operator[](unsigned i)
Definition: DeclGroup.h:41
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:149
const_iterator begin() const
Definition: DeclGroup.h:113
Decl * getSingleDecl()
Definition: DeclGroup.h:84
iterator begin()
Definition: DeclGroup.h:100
bool isNull() const
Definition: DeclGroup.h:80
unsigned size() const
Definition: DeclGroup.h:39
const Decl * getSingleDecl() const
Definition: DeclGroup.h:88
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
Definition: opencl-c.h:82
const_iterator end() const
Definition: DeclGroup.h:119
static void * getAsVoidPointer(clang::DeclGroupRef P)
Definition: DeclGroup.h:143
static DeclGroupRef getFromOpaquePtr(void *Ptr)
Definition: DeclGroup.h:127
DeclGroupRef(DeclGroup *dg)
Definition: DeclGroup.h:66
static DeclGroup * Create(ASTContext &C, Decl **Decls, unsigned NumDecls)
Definition: DeclGroup.cpp:21
bool isDeclGroup() const
Definition: DeclGroup.h:82
static DeclGroupRef Create(ASTContext &C, Decl **Decls, unsigned NumDecls)
Definition: DeclGroup.h:69
Dataflow Directional Tag Classes.
bool isSingleDecl() const
Definition: DeclGroup.h:81
const DeclGroup & getDeclGroup() const
Definition: DeclGroup.h:96
static clang::DeclGroupRef getFromVoidPointer(void *P)
Definition: DeclGroup.h:147
friend TrailingObjects
Definition: OpenMPClause.h:84
DeclGroupRef(Decl *d)
Definition: DeclGroup.h:65
Decl *const * const_iterator
Definition: DeclGroup.h:78
X
Add a minimal nested name specifier fixit hint to allow lookup of a tag name from an outer enclosing ...
Definition: SemaDecl.cpp:13010
static Decl::Kind getKind(const Decl *D)
Definition: DeclBase.cpp:915
friend TrailingObjects
Definition: DeclGroup.h:35