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