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