xref: /freebsd/contrib/llvm-project/llvm/include/llvm/DebugInfo/PDB/UDTLayout.h (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
1 //===- UDTLayout.h - UDT layout info ----------------------------*- 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 #ifndef LLVM_DEBUGINFO_PDB_UDTLAYOUT_H
10 #define LLVM_DEBUGINFO_PDB_UDTLAYOUT_H
11 
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/BitVector.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/DebugInfo/PDB/PDBSymbol.h"
16 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
17 #include "llvm/DebugInfo/PDB/PDBSymbolTypeBaseClass.h"
18 #include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h"
19 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
20 #include "llvm/DebugInfo/PDB/PDBSymbolTypeVTable.h"
21 #include <cstdint>
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 namespace llvm {
27 namespace pdb {
28 
29 class BaseClassLayout;
30 class ClassLayout;
31 class UDTLayoutBase;
32 
33 class LayoutItemBase {
34 public:
35   LayoutItemBase(const UDTLayoutBase *Parent, const PDBSymbol *Symbol,
36                  const std::string &Name, uint32_t OffsetInParent,
37                  uint32_t Size, bool IsElided);
38   virtual ~LayoutItemBase() = default;
39 
40   uint32_t deepPaddingSize() const;
41   virtual uint32_t immediatePadding() const { return 0; }
42   virtual uint32_t tailPadding() const;
43 
44   const UDTLayoutBase *getParent() const { return Parent; }
45   StringRef getName() const { return Name; }
46   uint32_t getOffsetInParent() const { return OffsetInParent; }
47   uint32_t getSize() const { return SizeOf; }
48   uint32_t getLayoutSize() const { return LayoutSize; }
49   const PDBSymbol *getSymbol() const { return Symbol; }
50   const BitVector &usedBytes() const { return UsedBytes; }
51   bool isElided() const { return IsElided; }
52   virtual bool isVBPtr() const { return false; }
53 
54   uint32_t containsOffset(uint32_t Off) const {
55     uint32_t Begin = getOffsetInParent();
56     uint32_t End = Begin + getSize();
57     return (Off >= Begin && Off < End);
58   }
59 
60 protected:
61   const PDBSymbol *Symbol = nullptr;
62   const UDTLayoutBase *Parent = nullptr;
63   BitVector UsedBytes;
64   std::string Name;
65   uint32_t OffsetInParent = 0;
66   uint32_t SizeOf = 0;
67   uint32_t LayoutSize = 0;
68   bool IsElided = false;
69 };
70 
71 class VBPtrLayoutItem : public LayoutItemBase {
72 public:
73   VBPtrLayoutItem(const UDTLayoutBase &Parent,
74                   std::unique_ptr<PDBSymbolTypeBuiltin> Sym, uint32_t Offset,
75                   uint32_t Size);
76 
77   bool isVBPtr() const override { return true; }
78 
79 private:
80   std::unique_ptr<PDBSymbolTypeBuiltin> Type;
81 };
82 
83 class DataMemberLayoutItem : public LayoutItemBase {
84 public:
85   DataMemberLayoutItem(const UDTLayoutBase &Parent,
86                        std::unique_ptr<PDBSymbolData> DataMember);
87 
88   const PDBSymbolData &getDataMember();
89   bool hasUDTLayout() const;
90   const ClassLayout &getUDTLayout() const;
91 
92 private:
93   std::unique_ptr<PDBSymbolData> DataMember;
94   std::unique_ptr<ClassLayout> UdtLayout;
95 };
96 
97 class VTableLayoutItem : public LayoutItemBase {
98 public:
99   VTableLayoutItem(const UDTLayoutBase &Parent,
100                    std::unique_ptr<PDBSymbolTypeVTable> VTable);
101 
102   uint32_t getElementSize() const { return ElementSize; }
103 
104 private:
105   uint32_t ElementSize = 0;
106   std::unique_ptr<PDBSymbolTypeVTable> VTable;
107 };
108 
109 class UDTLayoutBase : public LayoutItemBase {
110   template <typename T> using UniquePtrVector = std::vector<std::unique_ptr<T>>;
111 
112 public:
113   UDTLayoutBase(const UDTLayoutBase *Parent, const PDBSymbol &Sym,
114                 const std::string &Name, uint32_t OffsetInParent, uint32_t Size,
115                 bool IsElided);
116 
117   uint32_t tailPadding() const override;
118   ArrayRef<LayoutItemBase *> layout_items() const { return LayoutItems; }
119   ArrayRef<BaseClassLayout *> bases() const { return AllBases; }
120   ArrayRef<BaseClassLayout *> regular_bases() const { return NonVirtualBases; }
121   ArrayRef<BaseClassLayout *> virtual_bases() const { return VirtualBases; }
122   uint32_t directVirtualBaseCount() const { return DirectVBaseCount; }
123   ArrayRef<std::unique_ptr<PDBSymbolFunc>> funcs() const { return Funcs; }
124   ArrayRef<std::unique_ptr<PDBSymbol>> other_items() const { return Other; }
125 
126 protected:
127   bool hasVBPtrAtOffset(uint32_t Off) const;
128   void initializeChildren(const PDBSymbol &Sym);
129 
130   void addChildToLayout(std::unique_ptr<LayoutItemBase> Child);
131 
132   uint32_t DirectVBaseCount = 0;
133 
134   UniquePtrVector<PDBSymbol> Other;
135   UniquePtrVector<PDBSymbolFunc> Funcs;
136   UniquePtrVector<LayoutItemBase> ChildStorage;
137   std::vector<LayoutItemBase *> LayoutItems;
138 
139   std::vector<BaseClassLayout *> AllBases;
140   ArrayRef<BaseClassLayout *> NonVirtualBases;
141   ArrayRef<BaseClassLayout *> VirtualBases;
142 
143   VTableLayoutItem *VTable = nullptr;
144   VBPtrLayoutItem *VBPtr = nullptr;
145 };
146 
147 class BaseClassLayout : public UDTLayoutBase {
148 public:
149   BaseClassLayout(const UDTLayoutBase &Parent, uint32_t OffsetInParent,
150                   bool Elide, std::unique_ptr<PDBSymbolTypeBaseClass> Base);
151 
152   const PDBSymbolTypeBaseClass &getBase() const { return *Base; }
153   bool isVirtualBase() const { return IsVirtualBase; }
154   bool isEmptyBase() { return SizeOf == 1 && LayoutSize == 0; }
155 
156 private:
157   std::unique_ptr<PDBSymbolTypeBaseClass> Base;
158   bool IsVirtualBase;
159 };
160 
161 class ClassLayout : public UDTLayoutBase {
162 public:
163   explicit ClassLayout(const PDBSymbolTypeUDT &UDT);
164   explicit ClassLayout(std::unique_ptr<PDBSymbolTypeUDT> UDT);
165 
166   ClassLayout(ClassLayout &&Other) = default;
167 
168   const PDBSymbolTypeUDT &getClass() const { return UDT; }
169   uint32_t immediatePadding() const override;
170 
171 private:
172   BitVector ImmediateUsedBytes;
173   std::unique_ptr<PDBSymbolTypeUDT> OwnedStorage;
174   const PDBSymbolTypeUDT &UDT;
175 };
176 
177 } // end namespace pdb
178 } // end namespace llvm
179 
180 #endif // LLVM_DEBUGINFO_PDB_UDTLAYOUT_H
181