xref: /llvm-project/lldb/source/Plugins/SymbolFile/DWARF/DWARFFormValue.h (revision 2e425bf629f80c8f8582c266d25a384e7549198d)
1 //===-- DWARFFormValue.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 #ifndef LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFFORMVALUE_H
10 #define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFFORMVALUE_H
11 
12 #include "DWARFDataExtractor.h"
13 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
14 #include <optional>
15 
16 namespace lldb_private::plugin {
17 namespace dwarf {
18 class DWARFUnit;
19 class SymbolFileDWARF;
20 class DWARFDIE;
21 
22 class DWARFFormValue {
23 public:
24   typedef llvm::DWARFFormValue::ValueType ValueType;
25   enum {
26     eValueTypeInvalid = 0,
27     eValueTypeUnsigned,
28     eValueTypeSigned,
29     eValueTypeCStr,
30     eValueTypeBlock
31   };
32 
33   DWARFFormValue() = default;
34   DWARFFormValue(const DWARFUnit *unit) : m_unit(unit) {}
35   DWARFFormValue(const DWARFUnit *unit, dw_form_t form)
36       : m_unit(unit), m_form(form) {}
37   const DWARFUnit *GetUnit() const { return m_unit; }
38   void SetUnit(const DWARFUnit *unit) { m_unit = unit; }
39   dw_form_t Form() const { return m_form; }
40   dw_form_t &FormRef() { return m_form; }
41   void SetForm(dw_form_t form) { m_form = form; }
42   const ValueType &Value() const { return m_value; }
43   ValueType &ValueRef() { return m_value; }
44   void SetValue(const ValueType &val) { m_value = val; }
45 
46   void Dump(Stream &s) const;
47   bool ExtractValue(const DWARFDataExtractor &data, lldb::offset_t *offset_ptr);
48   const uint8_t *BlockData() const;
49   static std::optional<uint8_t> GetFixedSize(dw_form_t form,
50                                              const DWARFUnit *u);
51   std::optional<uint8_t> GetFixedSize() const;
52   DWARFDIE Reference() const;
53 
54   /// If this is a reference to another DIE, return the corresponding DWARFUnit
55   /// and DIE offset such that Unit->GetDIE(offset) produces the desired DIE.
56   /// Otherwise, a nullptr and unspecified offset are returned.
57   std::pair<DWARFUnit *, uint64_t> ReferencedUnitAndOffset() const;
58 
59   uint64_t Reference(dw_offset_t offset) const;
60   bool Boolean() const { return m_value.uval != 0; }
61   uint64_t Unsigned() const { return m_value.uval; }
62   void SetUnsigned(uint64_t uval) { m_value.uval = uval; }
63   int64_t Signed() const { return m_value.sval; }
64   void SetSigned(int64_t sval) { m_value.sval = sval; }
65   const char *AsCString() const;
66   dw_addr_t Address() const;
67   bool IsValid() const { return m_form != 0; }
68   bool SkipValue(const DWARFDataExtractor &debug_info_data,
69                  lldb::offset_t *offset_ptr) const;
70   static bool SkipValue(const dw_form_t form,
71                         const DWARFDataExtractor &debug_info_data,
72                         lldb::offset_t *offset_ptr, const DWARFUnit *unit);
73   static bool IsBlockForm(const dw_form_t form);
74   static bool IsDataForm(const dw_form_t form);
75   static int Compare(const DWARFFormValue &a, const DWARFFormValue &b);
76   void Clear();
77   static bool FormIsSupported(dw_form_t form);
78 
79   // The following methods use LLVM naming convension in order to be are used by
80   // LLVM libraries.
81   std::optional<uint64_t> getAsUnsignedConstant() const;
82   std::optional<int64_t> getAsSignedConstant() const;
83   const char *getAsCString() const { return AsCString(); }
84 
85 protected:
86   // Compile unit where m_value was located.
87   // It may be different from compile unit where m_value refers to.
88   const DWARFUnit *m_unit = nullptr; // Unit for this form
89   dw_form_t m_form = dw_form_t(0);   // Form for this value
90   ValueType m_value;                 // Contains all data for the form
91 };
92 } // namespace dwarf
93 } // namespace lldb_private::plugin
94 
95 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFFORMVALUE_H
96