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     Descriptor *Desc;
32   };
33 
34   /// Describes a base class.
35   struct Base {
36     const RecordDecl *Decl;
37     unsigned Offset;
38     Descriptor *Desc;
39     Record *R;
40   };
41 
42   /// Mapping from identifiers to field descriptors.
43   using FieldList = llvm::SmallVector<Field, 8>;
44   /// Mapping from identifiers to base classes.
45   using BaseList = llvm::SmallVector<Base, 8>;
46   /// List of virtual base classes.
47   using VirtualBaseList = llvm::SmallVector<Base, 2>;
48 
49 public:
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.
69   const CXXDestructorDecl *getDestructor() const {
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   const Base *getBase(unsigned I) const { 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   const Base *getVirtualBase(unsigned I) const { return &VirtualBases[I]; }
99 
100 private:
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 
106 private:
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
134