clang 24.0.0git
Record.h
Go to the documentation of this file.
1//===--- Record.h - struct and class metadata for the VM --------*- 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// A record is part of a program to describe the layout and methods of a struct.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_INTERP_RECORD_H
14#define LLVM_CLANG_AST_INTERP_RECORD_H
15
16#include "PrimType.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclCXX.h"
19
20namespace clang {
21namespace interp {
22class Program;
23struct Descriptor;
24
25/// Structure/Class descriptor.
26class Record final {
27public:
28 /// Describes a record field.
29 struct Field {
32 unsigned Offset;
36
37 bool isBitField() const { return IsBitField; }
38 bool isUnnamedBitField() const { return IsUnnamedBitField; }
39 unsigned bitWidth() const {
40 assert(isBitField());
41 return Decl->getBitWidthValue();
42 }
43
44 Field(const FieldDecl *D, const Descriptor *Desc, unsigned Offset,
46 : Decl(D), Desc(Desc), Offset(Offset), T(T) {
47 IsBitField = Decl->isBitField();
48 IsUnnamedBitField = IsBitField && Decl->isUnnamedBitField();
49 }
50 };
51
52 /// Describes a base class.
53 struct Base {
56 const Record *R;
57 unsigned Offset;
58
59 Base(const RecordDecl *D, const Descriptor *Desc, const Record *R,
60 unsigned Offset)
61 : Decl(D), Desc(Desc), R(R), Offset(Offset) {}
62 };
63
64 /// Mapping from identifiers to field descriptors.
66 /// Mapping from identifiers to base classes.
68 /// List of virtual base classes.
70
71public:
72 /// Returns the underlying declaration.
73 const RecordDecl *getDecl() const { return Decl; }
74 /// Returns the name of the underlying declaration.
75 std::string getName() const;
76 /// Checks if the record is a union.
77 bool isUnion() const { return IsUnion; }
78 /// Checks if the record is an anonymous union.
79 bool isAnonymousUnion() const { return IsAnonymousUnion; }
80 /// Returns the size of the record.
81 unsigned getSize() const { return BaseSize; }
82 /// Returns the full size of the record, including records.
83 unsigned getFullSize() const { return BaseSize + VirtualSize; }
84 /// Returns the destructor of the record, if any.
86 if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(Decl))
87 return CXXDecl->getDestructor();
88 return nullptr;
89 }
90 /// If this record (or any of its bases) contains a field of type PT_Ptr.
91 bool hasPtrField() const { return HasPtrField; }
92
93 /// Returns true for anonymous unions and records
94 /// with no destructor or for those with a trivial destructor.
95 bool hasTrivialDtor() const;
96
97 using const_field_iter = FieldList::const_iterator;
98 llvm::iterator_range<const_field_iter> fields() const {
99 return llvm::make_range(Fields.begin(), Fields.end());
100 }
101
102 unsigned getNumFields() const { return Fields.size(); }
103 const Field *getField(unsigned I) const { return &Fields[I]; }
104 /// Find a field with the given offset.
105 /// This does a linear search, so use sparingly.
106 const Field *findField(unsigned Offset) const;
107 /// Returns a field.
108 const Field *getField(const FieldDecl *FD) const {
109 return &Fields[FD->getFieldIndex()];
110 }
111
112 using const_base_iter = BaseList::const_iterator;
113 llvm::iterator_range<const_base_iter> bases() const {
114 return llvm::make_range(Bases.begin(), Bases.end());
115 }
116
117 unsigned getNumBases() const { return Bases.size(); }
118 const Base *getBase(unsigned I) const {
119 assert(I < getNumBases());
120 return &Bases[I];
121 }
122 /// Returns a base descriptor.
123 const Base *getBase(QualType T) const;
124 /// Returns a base descriptor.
125 const Base *getBase(const RecordDecl *RD) const;
126 const Base *getBaseOrNull(const RecordDecl *RD) const;
127 const Base *findBase(unsigned Offset) const;
128
129 using const_virtual_iter = VirtualBaseList::const_iterator;
130 llvm::iterator_range<const_virtual_iter> virtual_bases() const {
131 return llvm::make_range(VirtualBases.begin(), VirtualBases.end());
132 }
133
134 unsigned getNumVirtualBases() const { return VirtualBases.size(); }
135 const Base *getVirtualBase(unsigned I) const { return &VirtualBases[I]; }
136 /// Returns a virtual base descriptor.
137 const Base *findVirtualBase(const RecordDecl *RD) const;
138
139 void dump(llvm::raw_ostream &OS, unsigned Indentation = 0,
140 unsigned Offset = 0) const;
141 void dump() const { dump(llvm::errs()); }
142
143private:
144 /// Constructor used by Program to create record descriptors.
145 Record(const RecordDecl *, BaseList &&Bases, FieldList &&Fields,
146 VirtualBaseList &&VirtualBases, unsigned VirtualSize,
147 unsigned BaseSize, bool HasPtrField = true);
148
149private:
150 friend class Program;
151
152 /// Original declaration.
153 const RecordDecl *Decl;
154 /// List of all base classes.
155 BaseList Bases;
156 /// List of all the fields in the record.
157 FieldList Fields;
158 /// List of all virtual bases.
159 VirtualBaseList VirtualBases;
160
161 /// Mapping from declarations to bases.
162 llvm::DenseMap<const RecordDecl *, const Base *> BaseMap;
163 /// Size of the structure.
164 unsigned BaseSize;
165 /// Size of all virtual bases.
166 unsigned VirtualSize;
167 /// If this record is a union.
168 bool IsUnion;
169 /// If this is an anonymous union.
170 bool IsAnonymousUnion;
171 /// If any of the fields are pointers (or references).
172 bool HasPtrField = false;
173};
174
175} // namespace interp
176} // namespace clang
177
178#endif
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
llvm::MachO::Record Record
Definition MachO.h:31
Represents a C++ destructor within a class.
Definition DeclCXX.h:2906
Represents a member of a struct/union/class.
Definition Decl.h:3295
unsigned getFieldIndex() const
Returns the index of this field within its record, as appropriate for passing to ASTRecordLayout::get...
Definition Decl.h:3380
A (possibly-)qualified type.
Definition TypeBase.h:938
Represents a struct/union/class.
Definition Decl.h:4460
The program contains and links the bytecode for all functions.
Definition Program.h:37
const RecordDecl * getDecl() const
Returns the underlying declaration.
Definition Record.h:73
friend class Program
Definition Record.h:150
const Base * findBase(unsigned Offset) const
Definition Record.cpp:76
bool isUnion() const
Checks if the record is a union.
Definition Record.h:77
llvm::SmallVector< Base, 8 > BaseList
Mapping from identifiers to base classes.
Definition Record.h:67
std::string getName() const
Returns the name of the underlying declaration.
Definition Record.cpp:36
const Field * getField(unsigned I) const
Definition Record.h:103
bool hasPtrField() const
If this record (or any of its bases) contains a field of type PT_Ptr.
Definition Record.h:91
BaseList::const_iterator const_base_iter
Definition Record.h:112
unsigned getNumBases() const
Definition Record.h:117
const Base * getBaseOrNull(const RecordDecl *RD) const
Definition Record.cpp:66
const CXXDestructorDecl * getDestructor() const
Returns the destructor of the record, if any.
Definition Record.h:85
const Base * getBase(unsigned I) const
Definition Record.h:118
const Field * getField(const FieldDecl *FD) const
Returns a field.
Definition Record.h:108
llvm::iterator_range< const_virtual_iter > virtual_bases() const
Definition Record.h:130
llvm::SmallVector< Field, 8 > FieldList
Mapping from identifiers to field descriptors.
Definition Record.h:65
bool hasTrivialDtor() const
Returns true for anonymous unions and records with no destructor or for those with a trivial destruct...
Definition Record.cpp:44
const Field * findField(unsigned Offset) const
Find a field with the given offset.
Definition Record.cpp:51
VirtualBaseList::const_iterator const_virtual_iter
Definition Record.h:129
llvm::SmallVector< Base, 0 > VirtualBaseList
List of virtual base classes.
Definition Record.h:69
const Base * getVirtualBase(unsigned I) const
Definition Record.h:135
FieldList::const_iterator const_field_iter
Definition Record.h:97
unsigned getSize() const
Returns the size of the record.
Definition Record.h:81
llvm::iterator_range< const_base_iter > bases() const
Definition Record.h:113
unsigned getFullSize() const
Returns the full size of the record, including records.
Definition Record.h:83
unsigned getNumFields() const
Definition Record.h:102
unsigned getNumVirtualBases() const
Definition Record.h:134
bool isAnonymousUnion() const
Checks if the record is an anonymous union.
Definition Record.h:79
llvm::iterator_range< const_field_iter > fields() const
Definition Record.h:98
void dump() const
Definition Record.h:141
const Base * findVirtualBase(const RecordDecl *RD) const
Returns a virtual base descriptor.
Definition Record.cpp:85
Top level wrappers for InstallAPI frontend operations.
const FunctionProtoType * T
Describes a memory block created by an allocation site.
Definition Descriptor.h:122
Describes a base class.
Definition Record.h:53
const RecordDecl * Decl
Definition Record.h:54
const Descriptor * Desc
Definition Record.h:55
Base(const RecordDecl *D, const Descriptor *Desc, const Record *R, unsigned Offset)
Definition Record.h:59
Describes a record field.
Definition Record.h:29
const Descriptor * Desc
Definition Record.h:31
Field(const FieldDecl *D, const Descriptor *Desc, unsigned Offset, OptPrimType T)
Definition Record.h:44
const FieldDecl * Decl
Definition Record.h:30
unsigned bitWidth() const
Definition Record.h:39
bool isUnnamedBitField() const
Definition Record.h:38
bool isBitField() const
Definition Record.h:37