xref: /freebsd/contrib/llvm-project/llvm/include/llvm/DebugInfo/LogicalView/Core/LVType.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===-- LVType.h ------------------------------------------------*- 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 // This file defines the LVType class, which is used to describe a debug
10 // information type.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVTYPE_H
15 #define LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVTYPE_H
16 
17 #include "llvm/DebugInfo/LogicalView/Core/LVElement.h"
18 #include "llvm/Support/Compiler.h"
19 
20 namespace llvm {
21 namespace logicalview {
22 
23 enum class LVTypeKind {
24   IsBase,
25   IsConst,
26   IsEnumerator,
27   IsImport,
28   IsImportDeclaration,
29   IsImportModule,
30   IsPointer,
31   IsPointerMember,
32   IsReference,
33   IsRestrict,
34   IsRvalueReference,
35   IsSubrange,
36   IsTemplateParam,
37   IsTemplateTemplateParam,
38   IsTemplateTypeParam,
39   IsTemplateValueParam,
40   IsTypedef,
41   IsUnaligned,
42   IsUnspecified,
43   IsVolatile,
44   IsModifier, // CodeView - LF_MODIFIER
45   LastEntry
46 };
47 using LVTypeKindSelection = std::set<LVTypeKind>;
48 using LVTypeDispatch = std::map<LVTypeKind, LVTypeGetFunction>;
49 using LVTypeRequest = std::vector<LVTypeGetFunction>;
50 
51 // Class to represent a DWARF Type.
52 class LLVM_ABI LVType : public LVElement {
53   enum class Property { IsSubrangeCount, LastEntry };
54 
55   // Typed bitvector with kinds and properties for this type.
56   LVProperties<LVTypeKind> Kinds;
57   LVProperties<Property> Properties;
58   static LVTypeDispatch Dispatch;
59 
60   // Size in bits of a symbol of this type.
61   uint32_t BitSize = 0;
62 
63   // Find the current type in the given 'Targets'.
64   LVType *findIn(const LVTypes *Targets) const;
65 
66 public:
LVType()67   LVType() : LVElement(LVSubclassID::LV_TYPE) { setIsType(); }
68   LVType(const LVType &) = delete;
69   LVType &operator=(const LVType &) = delete;
70   virtual ~LVType() = default;
71 
classof(const LVElement * Element)72   static bool classof(const LVElement *Element) {
73     return Element->getSubclassID() == LVSubclassID::LV_TYPE;
74   }
75 
76   KIND(LVTypeKind, IsBase);
77   KIND(LVTypeKind, IsConst);
78   KIND(LVTypeKind, IsEnumerator);
79   KIND(LVTypeKind, IsImport);
80   KIND_1(LVTypeKind, IsImportDeclaration, IsImport);
81   KIND_1(LVTypeKind, IsImportModule, IsImport);
82   KIND(LVTypeKind, IsPointer);
83   KIND(LVTypeKind, IsPointerMember);
84   KIND(LVTypeKind, IsReference);
85   KIND(LVTypeKind, IsRestrict);
86   KIND(LVTypeKind, IsRvalueReference);
87   KIND(LVTypeKind, IsSubrange);
88   KIND(LVTypeKind, IsTemplateParam);
89   KIND_1(LVTypeKind, IsTemplateTemplateParam, IsTemplateParam);
90   KIND_1(LVTypeKind, IsTemplateTypeParam, IsTemplateParam);
91   KIND_1(LVTypeKind, IsTemplateValueParam, IsTemplateParam);
92   KIND(LVTypeKind, IsTypedef);
93   KIND(LVTypeKind, IsUnaligned);
94   KIND(LVTypeKind, IsUnspecified);
95   KIND(LVTypeKind, IsVolatile);
96   KIND(LVTypeKind, IsModifier);
97 
98   PROPERTY(Property, IsSubrangeCount);
99 
100   const char *kind() const override;
101 
102   // Follow a chain of references given by DW_AT_abstract_origin and/or
103   // DW_AT_specification and update the type name.
104   StringRef resolveReferencesChain();
105 
isBase()106   bool isBase() const override { return getIsBase(); }
isTemplateParam()107   bool isTemplateParam() const override { return getIsTemplateParam(); }
108 
109   // Encode the specific template argument.
encodeTemplateArgument(std::string & Name)110   virtual void encodeTemplateArgument(std::string &Name) const {}
111 
112   // Return the underlying type for a type definition.
getUnderlyingType()113   virtual LVElement *getUnderlyingType() { return nullptr; }
setUnderlyingType(LVElement * Element)114   virtual void setUnderlyingType(LVElement *Element) {}
115 
116   // Return the size in bits of an entity of this type.
getBitSize()117   uint32_t getBitSize() const override { return BitSize; }
setBitSize(uint32_t Size)118   void setBitSize(uint32_t Size) override { BitSize = Size; }
119 
120   void resolveName() override;
121   void resolveReferences() override;
122 
getDispatch()123   static LVTypeDispatch &getDispatch() { return Dispatch; }
124 
125   static bool parametersMatch(const LVTypes *References,
126                               const LVTypes *Targets);
127 
128   static void getParameters(const LVTypes *Types, LVTypes *TypesParam,
129                             LVScopes *ScopesParam);
130 
131   // Iterate through the 'References' set and check that all its elements
132   // are present in the 'Targets' set. For a missing element, mark its
133   // parents as missing.
134   static void markMissingParents(const LVTypes *References,
135                                  const LVTypes *Targets);
136 
137   // Returns true if current type is logically equal to the given 'Type'.
138   virtual bool equals(const LVType *Type) const;
139 
140   // Returns true if the given 'References' are logically equal to the
141   // given 'Targets'.
142   static bool equals(const LVTypes *References, const LVTypes *Targets);
143 
144   // Report the current type as missing or added during comparison.
145   void report(LVComparePass Pass) override;
146 
147   void print(raw_ostream &OS, bool Full = true) const override;
148   void printExtra(raw_ostream &OS, bool Full = true) const override;
149 };
150 
151 // Class to represent DW_TAG_typedef_type.
152 class LLVM_ABI LVTypeDefinition final : public LVType {
153 public:
LVTypeDefinition()154   LVTypeDefinition() : LVType() {
155     setIsTypedef();
156     setIncludeInPrint();
157   }
158   LVTypeDefinition(const LVTypeDefinition &) = delete;
159   LVTypeDefinition &operator=(const LVTypeDefinition &) = delete;
160   ~LVTypeDefinition() = default;
161 
162   // Return the underlying type for a type definition.
163   LVElement *getUnderlyingType() override;
setUnderlyingType(LVElement * Element)164   void setUnderlyingType(LVElement *Element) override { setType(Element); }
165 
166   void resolveExtra() override;
167 
168   // Returns true if current type is logically equal to the given 'Type'.
169   bool equals(const LVType *Type) const override;
170 
171   void printExtra(raw_ostream &OS, bool Full = true) const override;
172 };
173 
174 // Class to represent a DW_TAG_enumerator.
175 class LLVM_ABI LVTypeEnumerator final : public LVType {
176   // Index in the String pool representing any initial value.
177   size_t ValueIndex = 0;
178 
179 public:
LVTypeEnumerator()180   LVTypeEnumerator() : LVType() {
181     setIsEnumerator();
182     setIncludeInPrint();
183   }
184   LVTypeEnumerator(const LVTypeEnumerator &) = delete;
185   LVTypeEnumerator &operator=(const LVTypeEnumerator &) = delete;
186   ~LVTypeEnumerator() = default;
187 
188   // Process the values for a DW_TAG_enumerator.
getValue()189   StringRef getValue() const override {
190     return getStringPool().getString(ValueIndex);
191   }
setValue(StringRef Value)192   void setValue(StringRef Value) override {
193     ValueIndex = getStringPool().getIndex(Value);
194   }
getValueIndex()195   size_t getValueIndex() const override { return ValueIndex; }
196 
197   // Returns true if current type is logically equal to the given 'Type'.
198   bool equals(const LVType *Type) const override;
199 
200   void printExtra(raw_ostream &OS, bool Full = true) const override;
201 };
202 
203 // Class to represent DW_TAG_imported_module / DW_TAG_imported_declaration.
204 class LLVM_ABI LVTypeImport final : public LVType {
205 public:
LVTypeImport()206   LVTypeImport() : LVType() { setIncludeInPrint(); }
207   LVTypeImport(const LVTypeImport &) = delete;
208   LVTypeImport &operator=(const LVTypeImport &) = delete;
209   ~LVTypeImport() = default;
210 
211   // Returns true if current type is logically equal to the given 'Type'.
212   bool equals(const LVType *Type) const override;
213 
214   void printExtra(raw_ostream &OS, bool Full = true) const override;
215 };
216 
217 // Class to represent a DWARF Template parameter holder (type or param).
218 class LLVM_ABI LVTypeParam final : public LVType {
219   // Index in the String pool representing any initial value.
220   size_t ValueIndex = 0;
221 
222 public:
223   LVTypeParam();
224   LVTypeParam(const LVTypeParam &) = delete;
225   LVTypeParam &operator=(const LVTypeParam &) = delete;
226   ~LVTypeParam() = default;
227 
228   // Template parameter value.
getValue()229   StringRef getValue() const override {
230     return getStringPool().getString(ValueIndex);
231   }
setValue(StringRef Value)232   void setValue(StringRef Value) override {
233     ValueIndex = getStringPool().getIndex(Value);
234   }
getValueIndex()235   size_t getValueIndex() const override { return ValueIndex; }
236 
237   // Encode the specific template argument.
238   void encodeTemplateArgument(std::string &Name) const override;
239 
240   // Returns true if current type is logically equal to the given 'Type'.
241   bool equals(const LVType *Type) const override;
242 
243   void printExtra(raw_ostream &OS, bool Full = true) const override;
244 };
245 
246 // Class to represent a DW_TAG_subrange_type.
247 class LLVM_ABI LVTypeSubrange final : public LVType {
248   // Values describing the subrange bounds.
249   int64_t LowerBound = 0; // DW_AT_lower_bound or DW_AT_count value.
250   int64_t UpperBound = 0; // DW_AT_upper_bound value.
251 
252 public:
LVTypeSubrange()253   LVTypeSubrange() : LVType() {
254     setIsSubrange();
255     setIncludeInPrint();
256   }
257   LVTypeSubrange(const LVTypeSubrange &) = delete;
258   LVTypeSubrange &operator=(const LVTypeSubrange &) = delete;
259   ~LVTypeSubrange() = default;
260 
getCount()261   int64_t getCount() const override {
262     return getIsSubrangeCount() ? LowerBound : 0;
263   }
setCount(int64_t Value)264   void setCount(int64_t Value) override {
265     LowerBound = Value;
266     setIsSubrangeCount();
267   }
268 
getLowerBound()269   int64_t getLowerBound() const override { return LowerBound; }
setLowerBound(int64_t Value)270   void setLowerBound(int64_t Value) override { LowerBound = Value; }
271 
getUpperBound()272   int64_t getUpperBound() const override { return UpperBound; }
setUpperBound(int64_t Value)273   void setUpperBound(int64_t Value) override { UpperBound = Value; }
274 
getBounds()275   std::pair<unsigned, unsigned> getBounds() const override {
276     return {LowerBound, UpperBound};
277   }
setBounds(unsigned Lower,unsigned Upper)278   void setBounds(unsigned Lower, unsigned Upper) override {
279     LowerBound = Lower;
280     UpperBound = Upper;
281   }
282 
283   void resolveExtra() override;
284 
285   // Returns true if current type is logically equal to the given 'Type'.
286   bool equals(const LVType *Type) const override;
287 
288   void printExtra(raw_ostream &OS, bool Full = true) const override;
289 };
290 
291 } // end namespace logicalview
292 } // end namespace llvm
293 
294 #endif // LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVTYPE_H
295