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 "PrimType.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclCXX.h"
19#include "llvm/ADT/ArrayRef.h"
20
21namespace clang {
22namespace interp {
23class Program;
24struct Descriptor;
25
26/// Structure/Class descriptor.
27class Record final {
28public:
29 /// Describes a record field.
30 struct Field {
31 const FieldDecl *Decl;
32 const Descriptor *Desc;
33 unsigned Offset;
34 OptPrimType T;
35 bool IsBitField;
36 bool IsUnnamedBitField;
37
38 bool isBitField() const { return IsBitField; }
39 bool isUnnamedBitField() const { return IsUnnamedBitField; }
40 unsigned bitWidth() const {
41 assert(isBitField());
42 return Decl->getBitWidthValue();
43 }
44
45 Field(const FieldDecl *D, const Descriptor *Desc, unsigned Offset,
46 OptPrimType T)
47 : Decl(D), Desc(Desc), Offset(Offset), T(T) {
48 IsBitField = Decl->isBitField();
49 IsUnnamedBitField = IsBitField && Decl->isUnnamedBitField();
50 }
51 };
52
53 /// Describes a base class.
54 struct Base {
55 const RecordDecl *Decl;
56 const Descriptor *Desc;
57 const Record *R;
58 unsigned Offset;
59
60 Base(const RecordDecl *D, const Descriptor *Desc, const Record *R,
61 unsigned Offset)
62 : Decl(D), Desc(Desc), R(R), Offset(Offset) {}
63 };
64
65public:
66 /// Returns the underlying declaration.
67 const RecordDecl *getDecl() const { return Decl; }
68 /// Returns the name of the underlying declaration.
69 std::string getName() const;
70 /// Checks if the record is a union.
71 bool isUnion() const { return IsUnion; }
72 /// Checks if the record is an anonymous union.
73 bool isAnonymousUnion() const { return IsAnonymousUnion; }
74 /// Returns the size of the record.
75 unsigned getSize() const { return BaseSize; }
76 /// Returns the full size of the record, including records.
77 unsigned getFullSize() const { return BaseSize + VirtualSize; }
78 /// Returns the destructor of the record, if any.
79 const CXXDestructorDecl *getDestructor() const {
80 if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(Val: Decl))
81 return CXXDecl->getDestructor();
82 return nullptr;
83 }
84 /// If this record (or any of its bases) contains a field of type PT_Ptr.
85 bool hasPtrField() const { return HasPtrField; }
86 /// Whether this record has no fields, no bases and no virtual bases.
87 bool isEmpty() const {
88 return Fields.empty() && Bases.empty() && VirtualBases.empty();
89 }
90 /// Returns true for anonymous unions and records
91 /// with no destructor or for those with a trivial destructor.
92 bool hasTrivialDtor() const;
93
94 using const_field_iter = ArrayRef<Field>::const_iterator;
95 llvm::iterator_range<const_field_iter> fields() const {
96 return llvm::make_range(x: Fields.begin(), y: Fields.end());
97 }
98
99 unsigned getNumFields() const { return Fields.size(); }
100 const Field *getField(unsigned I) const { return &Fields[I]; }
101 /// Find a field with the given offset.
102 /// This does a linear search, so use sparingly.
103 const Field *findField(unsigned Offset) const;
104 /// Returns a field.
105 const Field *getField(const FieldDecl *FD) const {
106 return &Fields[FD->getFieldIndex()];
107 }
108
109 using const_base_iter = ArrayRef<Base>::const_iterator;
110 llvm::iterator_range<const_base_iter> bases() const {
111 return llvm::make_range(x: Bases.begin(), y: Bases.end());
112 }
113
114 unsigned getNumBases() const { return Bases.size(); }
115 const Base *getBase(unsigned I) const {
116 assert(I < getNumBases());
117 return &Bases[I];
118 }
119 /// Returns a base descriptor.
120 const Base *getBase(QualType T) const;
121 /// Returns a base descriptor.
122 const Base *getBase(const RecordDecl *RD) const;
123 const Base *getBaseOrNull(const RecordDecl *RD) const;
124 const Base *findBase(unsigned Offset) const;
125
126 llvm::iterator_range<const_base_iter> virtual_bases() const {
127 return llvm::make_range(x: VirtualBases.begin(), y: VirtualBases.end());
128 }
129
130 unsigned getNumVirtualBases() const { return VirtualBases.size(); }
131 const Base *getVirtualBase(unsigned I) const { return &VirtualBases[I]; }
132 /// Returns a virtual base descriptor.
133 const Base *findVirtualBase(const RecordDecl *RD) const;
134
135 void dump(llvm::raw_ostream &OS, unsigned Indentation = 0,
136 unsigned Offset = 0) const;
137 void dump() const { dump(OS&: llvm::errs()); }
138
139private:
140 /// Constructor used by Program to create record descriptors.
141 Record(const RecordDecl *, ArrayRef<Base> Bases, ArrayRef<Field> Fields,
142 ArrayRef<Base> VirtualBases, unsigned VirtualSize, unsigned BaseSize,
143 bool HasPtrField = true);
144
145private:
146 friend class Program;
147
148 /// Original declaration.
149 const RecordDecl *Decl;
150 /// List of all base classes.
151 ArrayRef<Base> Bases;
152 /// List of all the fields in the record.
153 ArrayRef<Field> Fields;
154 /// List of all virtual bases.
155 ArrayRef<Base> VirtualBases;
156
157 /// Mapping from declarations to bases.
158 llvm::DenseMap<const RecordDecl *, const Base *> BaseMap;
159 /// Size of the structure.
160 unsigned BaseSize;
161 /// Size of all virtual bases.
162 unsigned VirtualSize;
163 /// If this record is a union.
164 bool IsUnion;
165 /// If this is an anonymous union.
166 bool IsAnonymousUnion;
167 /// If any of the fields are pointers (or references).
168 bool HasPtrField = false;
169};
170
171} // namespace interp
172} // namespace clang
173
174#endif
175