1 //===- DeclGroup.h - Classes for representing groups of Decls ---*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the DeclGroup, DeclGroupRef, and OwningDeclGroup classes.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_AST_DECLGROUP_H
14 #define LLVM_CLANG_AST_DECLGROUP_H
16 #include "llvm/Support/TrailingObjects.h"
25 class DeclGroup final : private llvm::TrailingObjects<DeclGroup, Decl *> {
26 // FIXME: Include a TypeSpecifier object.
27 unsigned NumDecls = 0;
30 DeclGroup() = default;
31 DeclGroup(unsigned numdecls, Decl** decls);
34 friend TrailingObjects;
36 static DeclGroup *Create(ASTContext &C, Decl **Decls, unsigned NumDecls);
38 unsigned size() const { return NumDecls; }
40 Decl*& operator[](unsigned i) {
41 assert (i < NumDecls && "Out-of-bounds access.");
42 return getTrailingObjects<Decl *>()[i];
45 Decl* const& operator[](unsigned i) const {
46 assert (i < NumDecls && "Out-of-bounds access.");
47 return getTrailingObjects<Decl *>()[i];
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 };
58 Kind getKind() const {
59 return (Kind) (reinterpret_cast<uintptr_t>(D) & Mask);
63 DeclGroupRef() = default;
64 explicit DeclGroupRef(Decl* d) : D(d) {}
65 explicit DeclGroupRef(DeclGroup* dg)
66 : D((Decl*) (reinterpret_cast<uintptr_t>(dg) | DeclGroupKind)) {}
68 static DeclGroupRef Create(ASTContext &C, Decl **Decls, unsigned NumDecls) {
70 return DeclGroupRef();
72 return DeclGroupRef(Decls[0]);
73 return DeclGroupRef(DeclGroup::Create(C, Decls, NumDecls));
76 using iterator = Decl **;
77 using const_iterator = Decl * const *;
79 bool isNull() const { return D == nullptr; }
80 bool isSingleDecl() const { return getKind() == SingleDeclKind; }
81 bool isDeclGroup() const { return getKind() == DeclGroupKind; }
83 Decl *getSingleDecl() {
84 assert(isSingleDecl() && "Isn't a single decl");
87 const Decl *getSingleDecl() const {
88 return const_cast<DeclGroupRef*>(this)->getSingleDecl();
91 DeclGroup &getDeclGroup() {
92 assert(isDeclGroup() && "Isn't a declgroup");
93 return *((DeclGroup*)(reinterpret_cast<uintptr_t>(D) & ~Mask));
95 const DeclGroup &getDeclGroup() const {
96 return const_cast<DeclGroupRef*>(this)->getDeclGroup();
101 return D ? &D : nullptr;
102 return &getDeclGroup()[0];
107 return D ? &D+1 : nullptr;
108 DeclGroup &G = getDeclGroup();
109 return &G[0] + G.size();
112 const_iterator begin() const {
114 return D ? &D : nullptr;
115 return &getDeclGroup()[0];
118 const_iterator end() const {
120 return D ? &D+1 : nullptr;
121 const DeclGroup &G = getDeclGroup();
122 return &G[0] + G.size();
125 void *getAsOpaquePtr() const { return D; }
126 static DeclGroupRef getFromOpaquePtr(void *Ptr) {
128 X.D = static_cast<Decl*>(Ptr);
137 // DeclGroupRef is "like a pointer", implement PointerLikeTypeTraits.
138 template <typename T>
139 struct PointerLikeTypeTraits;
141 struct PointerLikeTypeTraits<clang::DeclGroupRef> {
142 static inline void *getAsVoidPointer(clang::DeclGroupRef P) {
143 return P.getAsOpaquePtr();
146 static inline clang::DeclGroupRef getFromVoidPointer(void *P) {
147 return clang::DeclGroupRef::getFromOpaquePtr(P);
150 enum { NumLowBitsAvailable = 0 };
155 #endif // LLVM_CLANG_AST_DECLGROUP_H