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 base descriptor.
65 const Base *getBase(QualType T) const;
66 /// Returns a virtual base descriptor.
67 const Base *getVirtualBase(const RecordDecl *RD) const;
68 // Returns the destructor of the record, if any.
70 if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(Decl))
71 return CXXDecl->getDestructor();
72 return nullptr;
73 }
74
75 using const_field_iter = FieldList::const_iterator;
76 llvm::iterator_range<const_field_iter> fields() const {
77 return llvm::make_range(Fields.begin(), Fields.end());
78 }
79
80 unsigned getNumFields() const { return Fields.size(); }
81 const Field *getField(unsigned I) const { return &Fields[I]; }
82 Field *getField(unsigned I) { return &Fields[I]; }
83
84 using const_base_iter = BaseList::const_iterator;
85 llvm::iterator_range<const_base_iter> bases() const {
86 return llvm::make_range(Bases.begin(), Bases.end());
87 }
88
89 unsigned getNumBases() const { return Bases.size(); }
90 Base *getBase(unsigned I) { return &Bases[I]; }
91
92 using const_virtual_iter = VirtualBaseList::const_iterator;
93 llvm::iterator_range<const_virtual_iter> virtual_bases() const {
94 return llvm::make_range(VirtualBases.begin(), VirtualBases.end());
95 }
96
97 unsigned getNumVirtualBases() const { return VirtualBases.size(); }
98 Base *getVirtualBase(unsigned I) { return &VirtualBases[I]; }
99
100private:
101 /// Constructor used by Program to create record descriptors.
102 Record(const RecordDecl *, BaseList &&Bases, FieldList &&Fields,
103 VirtualBaseList &&VirtualBases, unsigned VirtualSize,
104 unsigned BaseSize);
105
106private:
107 friend class Program;
108
109 /// Original declaration.
110 const RecordDecl *Decl;
111 /// List of all base classes.
112 BaseList Bases;
113 /// List of all the fields in the record.
114 FieldList Fields;
115 /// List o fall virtual bases.
116 VirtualBaseList VirtualBases;
117
118 /// Mapping from declarations to bases.
119 llvm::DenseMap<const RecordDecl *, Base *> BaseMap;
120 /// Mapping from field identifiers to descriptors.
121 llvm::DenseMap<const FieldDecl *, Field *> FieldMap;
122 /// Mapping from declarations to virtual bases.
123 llvm::DenseMap<const RecordDecl *, Base *> VirtualBaseMap;
124 /// Size of the structure.
125 unsigned BaseSize;
126 /// Size of all virtual bases.
127 unsigned VirtualSize;
128};
129
130} // namespace interp
131} // namespace clang
132
133#endif
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2755
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
Represents a member of a struct/union/class.
Definition: Decl.h:2945
A (possibly-)qualified type.
Definition: Type.h:736
Represents a struct/union/class.
Definition: Decl.h:4012
bool isUnion() const
Definition: Decl.h:3658
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:81
Base * getVirtualBase(unsigned I)
Definition: Record.h:98
BaseList::const_iterator const_base_iter
Definition: Record.h:84
unsigned getNumBases() const
Definition: Record.h:89
const CXXDestructorDecl * getDestructor() const
Definition: Record.h:69
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:93
Field * getField(unsigned I)
Definition: Record.h:82
llvm::SmallVector< Field, 8 > FieldList
Mapping from identifiers to field descriptors.
Definition: Record.h:43
Base * getBase(unsigned I)
Definition: Record.h:90
llvm::SmallVector< Base, 2 > VirtualBaseList
List of virtual base classes.
Definition: Record.h:47
VirtualBaseList::const_iterator const_virtual_iter
Definition: Record.h:92
FieldList::const_iterator const_field_iter
Definition: Record.h:75
unsigned getSize() const
Returns the size of the record.
Definition: Record.h:57
llvm::iterator_range< const_base_iter > bases() const
Definition: Record.h:85
const Base * getVirtualBase(const RecordDecl *RD) const
Returns a virtual base descriptor.
Definition: Record.cpp:52
unsigned getFullSize() const
Returns the full size of the record, including records.
Definition: Record.h:59
unsigned getNumFields() const
Definition: Record.h:80
unsigned getNumVirtualBases() const
Definition: Record.h:97
llvm::iterator_range< const_field_iter > fields() const
Definition: Record.h:76
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