clang 20.0.0git
DeclAccessPair.h
Go to the documentation of this file.
1//===--- DeclAccessPair.h - A decl bundled with its path access -*- 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 DeclAccessPair class, which provides an
10// efficient representation of a pair of a NamedDecl* and an
11// AccessSpecifier. Generally the access specifier gives the
12// natural access of a declaration when named in a class, as
13// defined in C++ [class.access.base]p1.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_CLANG_AST_DECLACCESSPAIR_H
18#define LLVM_CLANG_AST_DECLACCESSPAIR_H
19
21#include "llvm/Support/DataTypes.h"
22#include "llvm/Support/Endian.h"
23
24namespace clang {
25
26class NamedDecl;
27
28/// A POD class for pairing a NamedDecl* with an access specifier.
29/// Can be put into unions.
31 /// Use the lower 2 bit to store AccessSpecifier. Use the higher
32 /// 61 bit to store the pointer to a NamedDecl or the DeclID to
33 /// a NamedDecl. If the 3rd bit is set, storing the DeclID, otherwise
34 /// storing the pointer.
35 llvm::support::detail::packed_endian_specific_integral<
36 uint64_t, llvm::endianness::native, alignof(void *)>
37 Ptr;
38
39 enum { ASMask = 0x3, Mask = 0x7 };
40
41 bool isDeclID() const { return (Ptr >> 2) & 0x1; }
42
43public:
46 p.set(D, AS);
47 return p;
48 }
49
52 p.Ptr = (ID << 3) | (0x1 << 2) | uint64_t(AS);
53 return p;
54 }
55
56 uint64_t getDeclID() const {
57 assert(isDeclID());
58 return (~Mask & Ptr) >> 3;
59 }
60
61 NamedDecl *getDecl() const {
62 assert(!isDeclID());
63 return reinterpret_cast<NamedDecl*>(~Mask & Ptr);
64 }
65 AccessSpecifier getAccess() const { return AccessSpecifier(ASMask & Ptr); }
66
68 set(D, getAccess());
69 }
71 set(getDecl(), AS);
72 }
74 Ptr = uint64_t(AS) | reinterpret_cast<uint64_t>(D);
75 }
76
77 operator NamedDecl*() const { return getDecl(); }
78 NamedDecl *operator->() const { return getDecl(); }
79};
80
81// Make sure DeclAccessPair is pointer-aligned types.
82static_assert(alignof(DeclAccessPair) == alignof(void *));
83// Make sure DeclAccessPair is still POD.
84static_assert(std::is_standard_layout_v<DeclAccessPair> &&
85 std::is_trivial_v<DeclAccessPair>);
86}
87
88#endif
static char ID
Definition: Arena.cpp:183
const Decl * D
Defines various enumerations that describe declaration and type specifiers.
A POD class for pairing a NamedDecl* with an access specifier.
uint64_t getDeclID() const
static DeclAccessPair make(NamedDecl *D, AccessSpecifier AS)
void setDecl(NamedDecl *D)
static DeclAccessPair makeLazy(uint64_t ID, AccessSpecifier AS)
NamedDecl * getDecl() const
void set(NamedDecl *D, AccessSpecifier AS)
NamedDecl * operator->() const
void setAccess(AccessSpecifier AS)
AccessSpecifier getAccess() const
This represents a decl that may have a name.
Definition: Decl.h:249
The JSON file list parser is used to communicate input to InstallAPI.
AccessSpecifier
A C++ access specifier (public, private, protected), plus the special value "none" which means differ...
Definition: Specifiers.h:123