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 {
29 const FieldDecl *Decl;
30 const Descriptor *Desc;
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 {
42 const RecordDecl *Decl;
43 const Descriptor *Desc;
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.
53 using FieldList = llvm::SmallVector<Field, 8>;
54 /// Mapping from identifiers to base classes.
55 using BaseList = llvm::SmallVector<Base, 8>;
56 /// List of virtual base classes.
57 using VirtualBaseList = llvm::SmallVector<Base, 0>;
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.
73 const CXXDestructorDecl *getDestructor() const {
74 if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(Val: 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(x: Fields.begin(), y: 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(x: Bases.begin(), y: 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(x: VirtualBases.begin(), y: 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(OS&: 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
164