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 | |
20 | namespace clang { |
21 | namespace interp { |
22 | class Program; |
23 | |
24 | /// Structure/Class descriptor. |
25 | class Record final { |
26 | public: |
27 | /// Describes a record field. |
28 | struct Field { |
29 | const FieldDecl *Decl; |
30 | unsigned Offset; |
31 | const Descriptor *Desc; |
32 | bool isBitField() const { return Decl->isBitField(); } |
33 | }; |
34 | |
35 | /// Describes a base class. |
36 | struct Base { |
37 | const RecordDecl *Decl; |
38 | unsigned Offset; |
39 | const Descriptor *Desc; |
40 | const Record *R; |
41 | }; |
42 | |
43 | /// Mapping from identifiers to field descriptors. |
44 | using FieldList = llvm::SmallVector<Field, 8>; |
45 | /// Mapping from identifiers to base classes. |
46 | using BaseList = llvm::SmallVector<Base, 8>; |
47 | /// List of virtual base classes. |
48 | using VirtualBaseList = llvm::SmallVector<Base, 2>; |
49 | |
50 | public: |
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; |
55 | /// Checks if the record is a union. |
56 | bool isUnion() const { return 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. |
70 | const CXXDestructorDecl *getDestructor() const { |
71 | if (const auto *CXXDecl = dyn_cast<CXXRecordDecl>(Val: 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(x: Fields.begin(), y: 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(x: Bases.begin(), y: 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(x: VirtualBases.begin(), y: VirtualBases.end()); |
98 | } |
99 | |
100 | unsigned getNumVirtualBases() const { return VirtualBases.size(); } |
101 | const Base *getVirtualBase(unsigned I) const { return &VirtualBases[I]; } |
102 | |
103 | void dump(llvm::raw_ostream &OS, unsigned Indentation = 0, |
104 | unsigned Offset = 0) const; |
105 | void dump() const { dump(OS&: llvm::errs()); } |
106 | |
107 | private: |
108 | /// Constructor used by Program to create record descriptors. |
109 | Record(const RecordDecl *, BaseList &&Bases, FieldList &&Fields, |
110 | VirtualBaseList &&VirtualBases, unsigned VirtualSize, |
111 | unsigned BaseSize); |
112 | |
113 | private: |
114 | friend class Program; |
115 | |
116 | /// Original declaration. |
117 | const RecordDecl *Decl; |
118 | /// List of all base classes. |
119 | BaseList Bases; |
120 | /// List of all the fields in the record. |
121 | FieldList Fields; |
122 | /// List o fall virtual bases. |
123 | VirtualBaseList VirtualBases; |
124 | |
125 | /// Mapping from declarations to bases. |
126 | llvm::DenseMap<const RecordDecl *, const Base *> BaseMap; |
127 | /// Mapping from field identifiers to descriptors. |
128 | llvm::DenseMap<const FieldDecl *, const Field *> FieldMap; |
129 | /// Mapping from declarations to virtual bases. |
130 | llvm::DenseMap<const RecordDecl *, Base *> VirtualBaseMap; |
131 | /// Size of the structure. |
132 | unsigned BaseSize; |
133 | /// Size of all virtual bases. |
134 | unsigned VirtualSize; |
135 | /// If this record is a union. |
136 | bool IsUnion; |
137 | }; |
138 | |
139 | } // namespace interp |
140 | } // namespace clang |
141 | |
142 | #endif |
143 | |