clang  14.0.0git
DeclGroup.h
Go to the documentation of this file.
1 //===- DeclGroup.h - Classes for representing groups of Decls ---*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the DeclGroup, DeclGroupRef, and OwningDeclGroup classes.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_AST_DECLGROUP_H
14 #define LLVM_CLANG_AST_DECLGROUP_H
15 
16 #include "llvm/Support/TrailingObjects.h"
17 #include <cassert>
18 #include <cstdint>
19 
20 namespace clang {
21 
22 class ASTContext;
23 class Decl;
24 
25 class DeclGroup final : private llvm::TrailingObjects<DeclGroup, Decl *> {
26  // FIXME: Include a TypeSpecifier object.
27  unsigned NumDecls = 0;
28 
29 private:
30  DeclGroup() = default;
31  DeclGroup(unsigned numdecls, Decl** decls);
32 
33 public:
35 
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 getTrailingObjects<Decl *>()[i];
43  }
44 
45  Decl* const& operator[](unsigned i) const {
46  assert (i < NumDecls && "Out-of-bounds access.");
47  return getTrailingObjects<Decl *>()[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 
56  Decl* D = nullptr;
57 
58  Kind getKind() const {
59  return (Kind) (reinterpret_cast<uintptr_t>(D) & Mask);
60  }
61 
62 public:
63  DeclGroupRef() = default;
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  using iterator = Decl **;
77  using const_iterator = Decl * const *;
78 
79  bool isNull() const { return D == nullptr; }
80  bool isSingleDecl() const { return getKind() == SingleDeclKind; }
81  bool isDeclGroup() const { return getKind() == DeclGroupKind; }
82 
84  assert(isSingleDecl() && "Isn't a single decl");
85  return D;
86  }
87  const Decl *getSingleDecl() const {
88  return const_cast<DeclGroupRef*>(this)->getSingleDecl();
89  }
90 
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 
100  if (isSingleDecl())
101  return D ? &D : nullptr;
102  return &getDeclGroup()[0];
103  }
104 
106  if (isSingleDecl())
107  return D ? &D+1 : nullptr;
108  DeclGroup &G = getDeclGroup();
109  return &G[0] + G.size();
110  }
111 
113  if (isSingleDecl())
114  return D ? &D : nullptr;
115  return &getDeclGroup()[0];
116  }
117 
118  const_iterator end() const {
119  if (isSingleDecl())
120  return D ? &D+1 : nullptr;
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 } // namespace clang
134 
135 namespace llvm {
136 
137  // DeclGroupRef is "like a pointer", implement PointerLikeTypeTraits.
138  template <typename T>
140  template <>
141  struct PointerLikeTypeTraits<clang::DeclGroupRef> {
142  static inline void *getAsVoidPointer(clang::DeclGroupRef P) {
143  return P.getAsOpaquePtr();
144  }
145 
146  static inline clang::DeclGroupRef getFromVoidPointer(void *P) {
148  }
149 
150  static constexpr int NumLowBitsAvailable = 0;
151  };
152 
153 } // namespace llvm
154 
155 #endif // LLVM_CLANG_AST_DECLGROUP_H
llvm
Definition: Dominators.h:30
clang::DeclGroup::size
unsigned size() const
Definition: DeclGroup.h:38
clang::DeclGroup::TrailingObjects
friend TrailingObjects
Definition: DeclGroup.h:34
clang::DeclGroup::Create
static DeclGroup * Create(ASTContext &C, Decl **Decls, unsigned NumDecls)
Definition: DeclGroup.cpp:20
clang::DeclGroupRef::Create
static DeclGroupRef Create(ASTContext &C, Decl **Decls, unsigned NumDecls)
Definition: DeclGroup.h:68
clang::DeclGroup::operator[]
Decl *& operator[](unsigned i)
Definition: DeclGroup.h:40
clang::DeclGroupRef::DeclGroupRef
DeclGroupRef()=default
clang::DeclGroupRef::isSingleDecl
bool isSingleDecl() const
Definition: DeclGroup.h:80
clang::DeclGroupRef::begin
iterator begin()
Definition: DeclGroup.h:99
clang::tooling::X
static ToolExecutorPluginRegistry::Add< AllTUsToolExecutorPlugin > X("all-TUs", "Runs FrontendActions on all TUs in the compilation database. " "Tool results are stored in memory.")
clang::DeclGroupRef::end
iterator end()
Definition: DeclGroup.h:105
clang::DeclGroupRef::isDeclGroup
bool isDeclGroup() const
Definition: DeclGroup.h:81
llvm::PointerLikeTypeTraits< clang::DeclGroupRef >::getAsVoidPointer
static void * getAsVoidPointer(clang::DeclGroupRef P)
Definition: DeclGroup.h:142
clang::DeclGroupRef::getDeclGroup
DeclGroup & getDeclGroup()
Definition: DeclGroup.h:91
clang::CodeGen::AlignmentSource::Decl
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
clang::DeclGroupRef::getSingleDecl
const Decl * getSingleDecl() const
Definition: DeclGroup.h:87
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:211
clang::DeclGroupRef::getFromOpaquePtr
static DeclGroupRef getFromOpaquePtr(void *Ptr)
Definition: DeclGroup.h:126
uintptr_t
__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-base.h:124
clang::DeclGroupRef::getSingleDecl
Decl * getSingleDecl()
Definition: DeclGroup.h:83
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
clang::DeclGroupRef::end
const_iterator end() const
Definition: DeclGroup.h:118
clang::DeclGroupRef::DeclGroupRef
DeclGroupRef(DeclGroup *dg)
Definition: DeclGroup.h:65
clang::DeclGroup::operator[]
Decl *const & operator[](unsigned i) const
Definition: DeclGroup.h:45
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
clang::DeclGroupRef
Definition: DeclGroup.h:51
clang::DeclGroupRef::DeclGroupRef
DeclGroupRef(Decl *d)
Definition: DeclGroup.h:64
clang::DeclGroup
Definition: DeclGroup.h:25
clang::DeclGroupRef::getDeclGroup
const DeclGroup & getDeclGroup() const
Definition: DeclGroup.h:95
clang
Definition: CalledOnceCheck.h:17
llvm::PointerLikeTypeTraits
Definition: DeclGroup.h:139
llvm::PointerLikeTypeTraits< clang::DeclGroupRef >::getFromVoidPointer
static clang::DeclGroupRef getFromVoidPointer(void *P)
Definition: DeclGroup.h:146
clang::DeclGroupRef::isNull
bool isNull() const
Definition: DeclGroup.h:79
clang::DeclGroupRef::getAsOpaquePtr
void * getAsOpaquePtr() const
Definition: DeclGroup.h:125
clang::DeclGroupRef::begin
const_iterator begin() const
Definition: DeclGroup.h:112
clang::DeclGroupRef::const_iterator
Decl *const * const_iterator
Definition: DeclGroup.h:77