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 "Pointer.h"
17 
18 namespace clang {
19 namespace interp {
20 class Program;
21 
22 /// Structure/Class descriptor.
23 class Record {
24 public:
25   /// Describes a record field.
26   struct Field {
27     const FieldDecl *Decl;
28     unsigned Offset;
29     Descriptor *Desc;
30   };
31 
32   /// Describes a base class.
33   struct Base {
34     const RecordDecl *Decl;
35     unsigned Offset;
36     Descriptor *Desc;
37     Record *R;
38   };
39 
40   /// Mapping from identifiers to field descriptors.
41   using FieldList = llvm::SmallVector<Field, 8>;
42   /// Mapping from identifiers to base classes.
43   using BaseList = llvm::SmallVector<Base, 8>;
44   /// List of virtual base classes.
45   using VirtualBaseList = llvm::SmallVector<Base, 2>;
46 
47 public:
48   /// Returns the underlying declaration.
getDecl()49   const RecordDecl *getDecl() const { return Decl; }
50   /// Checks if the record is a union.
isUnion()51   bool isUnion() const { return getDecl()->isUnion(); }
52   /// Returns the size of the record.
getSize()53   unsigned getSize() const { return BaseSize; }
54   /// Returns the full size of the record, including records.
getFullSize()55   unsigned getFullSize() const { return BaseSize + VirtualSize; }
56   /// Returns a field.
57   const Field *getField(const FieldDecl *FD) const;
58   /// Returns a base descriptor.
59   const Base *getBase(const RecordDecl *FD) const;
60   /// Returns a virtual base descriptor.
61   const Base *getVirtualBase(const RecordDecl *RD) const;
62 
63   using const_field_iter = FieldList::const_iterator;
fields()64   llvm::iterator_range<const_field_iter> fields() const {
65     return llvm::make_range(Fields.begin(), Fields.end());
66   }
67 
getNumFields()68   unsigned getNumFields() { return Fields.size(); }
getField(unsigned I)69   Field *getField(unsigned I) { return &Fields[I]; }
70 
71   using const_base_iter = BaseList::const_iterator;
bases()72   llvm::iterator_range<const_base_iter> bases() const {
73     return llvm::make_range(Bases.begin(), Bases.end());
74   }
75 
getNumBases()76   unsigned getNumBases() { return Bases.size(); }
getBase(unsigned I)77   Base *getBase(unsigned I) { return &Bases[I]; }
78 
79   using const_virtual_iter = VirtualBaseList::const_iterator;
virtual_bases()80   llvm::iterator_range<const_virtual_iter> virtual_bases() const {
81     return llvm::make_range(VirtualBases.begin(), VirtualBases.end());
82   }
83 
getNumVirtualBases()84   unsigned getNumVirtualBases() { return VirtualBases.size(); }
getVirtualBase(unsigned I)85   Base *getVirtualBase(unsigned I) { return &VirtualBases[I]; }
86 
87 private:
88   /// Constructor used by Program to create record descriptors.
89   Record(const RecordDecl *, BaseList &&Bases, FieldList &&Fields,
90          VirtualBaseList &&VirtualBases, unsigned VirtualSize,
91          unsigned BaseSize);
92 
93 private:
94   friend class Program;
95 
96   /// Original declaration.
97   const RecordDecl *Decl;
98   /// List of all base classes.
99   BaseList Bases;
100   /// List of all the fields in the record.
101   FieldList Fields;
102   /// List o fall virtual bases.
103   VirtualBaseList VirtualBases;
104 
105   /// Mapping from declarations to bases.
106   llvm::DenseMap<const RecordDecl *, Base *> BaseMap;
107   /// Mapping from field identifiers to descriptors.
108   llvm::DenseMap<const FieldDecl *, Field *> FieldMap;
109   /// Mapping from declarations to virtual bases.
110   llvm::DenseMap<const RecordDecl *, Base *> VirtualBaseMap;
111   /// Mapping from
112   /// Size of the structure.
113   unsigned BaseSize;
114   /// Size of all virtual bases.
115   unsigned VirtualSize;
116 };
117 
118 } // namespace interp
119 } // namespace clang
120 
121 #endif
122