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