1327952Sdim//===- DeclGroup.h - Classes for representing groups of Decls ---*- C++ -*-===//
2193326Sed//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6193326Sed//
7193326Sed//===----------------------------------------------------------------------===//
8193326Sed//
9193326Sed//  This file defines the DeclGroup, DeclGroupRef, and OwningDeclGroup classes.
10193326Sed//
11193326Sed//===----------------------------------------------------------------------===//
12193326Sed
13193326Sed#ifndef LLVM_CLANG_AST_DECLGROUP_H
14193326Sed#define LLVM_CLANG_AST_DECLGROUP_H
15193326Sed
16296417Sdim#include "llvm/Support/TrailingObjects.h"
17193326Sed#include <cassert>
18327952Sdim#include <cstdint>
19193326Sed
20193326Sednamespace clang {
21198092Srdivacky
22193326Sedclass ASTContext;
23193326Sedclass Decl;
24193326Sed
25296417Sdimclass DeclGroup final : private llvm::TrailingObjects<DeclGroup, Decl *> {
26193326Sed  // FIXME: Include a TypeSpecifier object.
27327952Sdim  unsigned NumDecls = 0;
28198092Srdivacky
29193326Sedprivate:
30327952Sdim  DeclGroup() = default;
31193326Sed  DeclGroup(unsigned numdecls, Decl** decls);
32193326Sed
33193326Sedpublic:
34327952Sdim  friend TrailingObjects;
35327952Sdim
36193326Sed  static DeclGroup *Create(ASTContext &C, Decl **Decls, unsigned NumDecls);
37193326Sed
38193326Sed  unsigned size() const { return NumDecls; }
39193326Sed
40198092Srdivacky  Decl*& operator[](unsigned i) {
41193326Sed    assert (i < NumDecls && "Out-of-bounds access.");
42296417Sdim    return getTrailingObjects<Decl *>()[i];
43193326Sed  }
44198092Srdivacky
45198092Srdivacky  Decl* const& operator[](unsigned i) const {
46193326Sed    assert (i < NumDecls && "Out-of-bounds access.");
47296417Sdim    return getTrailingObjects<Decl *>()[i];
48193326Sed  }
49193326Sed};
50198092Srdivacky
51193326Sedclass DeclGroupRef {
52193326Sed  // Note this is not a PointerIntPair because we need the address of the
53193326Sed  // non-group case to be valid as a Decl** for iteration.
54198092Srdivacky  enum Kind { SingleDeclKind=0x0, DeclGroupKind=0x1, Mask=0x1 };
55193326Sed
56327952Sdim  Decl* D = nullptr;
57327952Sdim
58193326Sed  Kind getKind() const {
59193326Sed    return (Kind) (reinterpret_cast<uintptr_t>(D) & Mask);
60198092Srdivacky  }
61198092Srdivacky
62198092Srdivackypublic:
63327952Sdim  DeclGroupRef() = default;
64193326Sed  explicit DeclGroupRef(Decl* d) : D(d) {}
65193326Sed  explicit DeclGroupRef(DeclGroup* dg)
66193326Sed    : D((Decl*) (reinterpret_cast<uintptr_t>(dg) | DeclGroupKind)) {}
67198092Srdivacky
68193326Sed  static DeclGroupRef Create(ASTContext &C, Decl **Decls, unsigned NumDecls) {
69193326Sed    if (NumDecls == 0)
70193326Sed      return DeclGroupRef();
71193326Sed    if (NumDecls == 1)
72193326Sed      return DeclGroupRef(Decls[0]);
73193326Sed    return DeclGroupRef(DeclGroup::Create(C, Decls, NumDecls));
74193326Sed  }
75198092Srdivacky
76327952Sdim  using iterator = Decl **;
77327952Sdim  using const_iterator = Decl * const *;
78198092Srdivacky
79276479Sdim  bool isNull() const { return D == nullptr; }
80193326Sed  bool isSingleDecl() const { return getKind() == SingleDeclKind; }
81193326Sed  bool isDeclGroup() const { return getKind() == DeclGroupKind; }
82193326Sed
83193326Sed  Decl *getSingleDecl() {
84314564Sdim    assert(isSingleDecl() && "Isn't a single decl");
85193326Sed    return D;
86193326Sed  }
87193326Sed  const Decl *getSingleDecl() const {
88193326Sed    return const_cast<DeclGroupRef*>(this)->getSingleDecl();
89193326Sed  }
90198092Srdivacky
91193326Sed  DeclGroup &getDeclGroup() {
92193326Sed    assert(isDeclGroup() && "Isn't a declgroup");
93193326Sed    return *((DeclGroup*)(reinterpret_cast<uintptr_t>(D) & ~Mask));
94193326Sed  }
95193326Sed  const DeclGroup &getDeclGroup() const {
96193326Sed    return const_cast<DeclGroupRef*>(this)->getDeclGroup();
97193326Sed  }
98198092Srdivacky
99193326Sed  iterator begin() {
100193326Sed    if (isSingleDecl())
101276479Sdim      return D ? &D : nullptr;
102193326Sed    return &getDeclGroup()[0];
103193326Sed  }
104193326Sed
105193326Sed  iterator end() {
106193326Sed    if (isSingleDecl())
107276479Sdim      return D ? &D+1 : nullptr;
108193326Sed    DeclGroup &G = getDeclGroup();
109193326Sed    return &G[0] + G.size();
110193326Sed  }
111198092Srdivacky
112193326Sed  const_iterator begin() const {
113193326Sed    if (isSingleDecl())
114276479Sdim      return D ? &D : nullptr;
115193326Sed    return &getDeclGroup()[0];
116193326Sed  }
117198092Srdivacky
118193326Sed  const_iterator end() const {
119193326Sed    if (isSingleDecl())
120276479Sdim      return D ? &D+1 : nullptr;
121193326Sed    const DeclGroup &G = getDeclGroup();
122193326Sed    return &G[0] + G.size();
123193326Sed  }
124193326Sed
125193326Sed  void *getAsOpaquePtr() const { return D; }
126193326Sed  static DeclGroupRef getFromOpaquePtr(void *Ptr) {
127193326Sed    DeclGroupRef X;
128193326Sed    X.D = static_cast<Decl*>(Ptr);
129193326Sed    return X;
130193326Sed  }
131193326Sed};
132198092Srdivacky
133327952Sdim} // namespace clang
134193326Sed
135193326Sednamespace llvm {
136327952Sdim
137193326Sed  // DeclGroupRef is "like a pointer", implement PointerLikeTypeTraits.
138193326Sed  template <typename T>
139327952Sdim  struct PointerLikeTypeTraits;
140193326Sed  template <>
141327952Sdim  struct PointerLikeTypeTraits<clang::DeclGroupRef> {
142193326Sed    static inline void *getAsVoidPointer(clang::DeclGroupRef P) {
143193326Sed      return P.getAsOpaquePtr();
144193326Sed    }
145327952Sdim
146193326Sed    static inline clang::DeclGroupRef getFromVoidPointer(void *P) {
147193326Sed      return clang::DeclGroupRef::getFromOpaquePtr(P);
148193326Sed    }
149327952Sdim
150193326Sed    enum { NumLowBitsAvailable = 0 };
151193326Sed  };
152327952Sdim
153327952Sdim} // namespace llvm
154327952Sdim
155327952Sdim#endif // LLVM_CLANG_AST_DECLGROUP_H
156