clang 17.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 "Descriptor.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclCXX.h"
19
20namespace clang {
21namespace interp {
22class Program;
23
24/// Structure/Class descriptor.
25class Record final {
26public:
27 /// Describes a record field.
28 struct Field {
30 unsigned Offset;
32 };
33
34 /// Describes a base class.
35 struct Base {
37 unsigned Offset;
40 };
41
42 /// Mapping from identifiers to field descriptors.
44 /// Mapping from identifiers to base classes.
46 /// List of virtual base classes.
48
49public:
50 /// Returns the underlying declaration.
51 const RecordDecl *getDecl() const { return Decl; }
52 /// Returns the name of the underlying declaration.
53 const std::string getName() const { return Decl->getNameAsString(); }
54 /// Checks if the record is a union.
55 bool isUnion() const { return getDecl()->isUnion(); }
56 /// Returns the size of the record.
57 unsigned getSize() const { return BaseSize; }
58 /// Returns the full size of the record, including records.
59 unsigned getFullSize() const { return BaseSize + VirtualSize; }
60 /// Returns a field.
61 const Field *getField(const FieldDecl *FD) const;
62 /// Returns a base descriptor.
63 const Base *getBase(const RecordDecl *FD) const;
64 /// Returns a virtual base descriptor.
65 const Base *getVirtualBase(const RecordDecl *RD) const;
66 // Returns the destructor of the record, if any.
68 if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(Decl))
69 return CXXDecl->getDestructor();
70 return nullptr;
71 }
72
73 using const_field_iter = FieldList::const_iterator;
74 llvm::iterator_range<const_field_iter> fields() const {
75 return llvm::make_range(Fields.begin(), Fields.end());
76 }
77
78 unsigned getNumFields() const { return Fields.size(); }
79 const Field *getField(unsigned I) const { return &Fields[I]; }
80 Field *getField(unsigned I) { return &Fields[I]; }
81
82 using const_base_iter = BaseList::const_iterator;
83 llvm::iterator_range<const_base_iter> bases() const {
84 return llvm::make_range(Bases.begin(), Bases.end());
85 }
86
87 unsigned getNumBases() const { return Bases.size(); }
88 Base *getBase(unsigned I) { return &Bases[I]; }
89
90 using const_virtual_iter = VirtualBaseList::const_iterator;
91 llvm::iterator_range<const_virtual_iter> virtual_bases() const {
92 return llvm::make_range(VirtualBases.begin(), VirtualBases.end());
93 }
94
95 unsigned getNumVirtualBases() const { return VirtualBases.size(); }
96 Base *getVirtualBase(unsigned I) { return &VirtualBases[I]; }
97
98private:
99 /// Constructor used by Program to create record descriptors.
100 Record(const RecordDecl *, BaseList &&Bases, FieldList &&Fields,
101 VirtualBaseList &&VirtualBases, unsigned VirtualSize,
102 unsigned BaseSize);
103
104private:
105 friend class Program;
106
107 /// Original declaration.
108 const RecordDecl *Decl;
109 /// List of all base classes.
110 BaseList Bases;
111 /// List of all the fields in the record.
112 FieldList Fields;
113 /// List o fall virtual bases.
114 VirtualBaseList VirtualBases;
115
116 /// Mapping from declarations to bases.
117 llvm::DenseMap<const RecordDecl *, Base *> BaseMap;
118 /// Mapping from field identifiers to descriptors.
119 llvm::DenseMap<const FieldDecl *, Field *> FieldMap;
120 /// Mapping from declarations to virtual bases.
121 llvm::DenseMap<const RecordDecl *, Base *> VirtualBaseMap;
122 /// Size of the structure.
123 unsigned BaseSize;
124 /// Size of all virtual bases.
125 unsigned VirtualSize;
126};
127
128} // namespace interp
129} // namespace clang
130
131#endif
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2715
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
Represents a member of a struct/union/class.
Definition: Decl.h:2943
Represents a struct/union/class.
Definition: Decl.h:3996
bool isUnion() const
Definition: Decl.h:3642
The program contains and links the bytecode for all functions.
Definition: Program.h:40
Structure/Class descriptor.
Definition: Record.h:25
const RecordDecl * getDecl() const
Returns the underlying declaration.
Definition: Record.h:51
const std::string getName() const
Returns the name of the underlying declaration.
Definition: Record.h:53
bool isUnion() const
Checks if the record is a union.
Definition: Record.h:55
llvm::SmallVector< Base, 8 > BaseList
Mapping from identifiers to base classes.
Definition: Record.h:45
const Field * getField(unsigned I) const
Definition: Record.h:79
Base * getVirtualBase(unsigned I)
Definition: Record.h:96
BaseList::const_iterator const_base_iter
Definition: Record.h:82
unsigned getNumBases() const
Definition: Record.h:87
const CXXDestructorDecl * getDestructor() const
Definition: Record.h:67
const Field * getField(const FieldDecl *FD) const
Returns a field.
Definition: Record.cpp:30
llvm::iterator_range< const_virtual_iter > virtual_bases() const
Definition: Record.h:91
Field * getField(unsigned I)
Definition: Record.h:80
llvm::SmallVector< Field, 8 > FieldList
Mapping from identifiers to field descriptors.
Definition: Record.h:43
Base * getBase(unsigned I)
Definition: Record.h:88
llvm::SmallVector< Base, 2 > VirtualBaseList
List of virtual base classes.
Definition: Record.h:47
VirtualBaseList::const_iterator const_virtual_iter
Definition: Record.h:90
FieldList::const_iterator const_field_iter
Definition: Record.h:73
unsigned getSize() const
Returns the size of the record.
Definition: Record.h:57
llvm::iterator_range< const_base_iter > bases() const
Definition: Record.h:83
const Base * getVirtualBase(const RecordDecl *RD) const
Returns a virtual base descriptor.
Definition: Record.cpp:42
unsigned getFullSize() const
Returns the full size of the record, including records.
Definition: Record.h:59
unsigned getNumFields() const
Definition: Record.h:78
unsigned getNumVirtualBases() const
Definition: Record.h:95
llvm::iterator_range< const_field_iter > fields() const
Definition: Record.h:74
const Base * getBase(const RecordDecl *FD) const
Returns a base descriptor.
Definition: Record.cpp:36
Describes a memory block created by an allocation site.
Definition: Descriptor.h:76
Describes a base class.
Definition: Record.h:35
const RecordDecl * Decl
Definition: Record.h:36
Describes a record field.
Definition: Record.h:28
const FieldDecl * Decl
Definition: Record.h:29