xref: /freebsd/contrib/llvm-project/lldb/source/Plugins/SymbolFile/DWARF/DWARFFormValue.h (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
10b57cec5SDimitry Andric //===-- DWARFFormValue.h ----------------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
95ffd83dbSDimitry Andric #ifndef LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFFORMVALUE_H
105ffd83dbSDimitry Andric #define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFFORMVALUE_H
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "DWARFDataExtractor.h"
13fe6060f1SDimitry Andric #include <cstddef>
14bdd1243dSDimitry Andric #include <optional>
150b57cec5SDimitry Andric 
165f757f3fSDimitry Andric namespace lldb_private::plugin {
175f757f3fSDimitry Andric namespace dwarf {
180b57cec5SDimitry Andric class DWARFUnit;
190b57cec5SDimitry Andric class SymbolFileDWARF;
200b57cec5SDimitry Andric class DWARFDIE;
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric class DWARFFormValue {
230b57cec5SDimitry Andric public:
240b57cec5SDimitry Andric   typedef struct ValueTypeTag {
25fe6060f1SDimitry Andric     ValueTypeTag() : value() { value.uval = 0; }
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric     union {
280b57cec5SDimitry Andric       uint64_t uval;
290b57cec5SDimitry Andric       int64_t sval;
300b57cec5SDimitry Andric       const char *cstr;
310b57cec5SDimitry Andric     } value;
32fe6060f1SDimitry Andric     const uint8_t *data = nullptr;
330b57cec5SDimitry Andric   } ValueType;
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric   enum {
360b57cec5SDimitry Andric     eValueTypeInvalid = 0,
370b57cec5SDimitry Andric     eValueTypeUnsigned,
380b57cec5SDimitry Andric     eValueTypeSigned,
390b57cec5SDimitry Andric     eValueTypeCStr,
400b57cec5SDimitry Andric     eValueTypeBlock
410b57cec5SDimitry Andric   };
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric   DWARFFormValue() = default;
440b57cec5SDimitry Andric   DWARFFormValue(const DWARFUnit *unit) : m_unit(unit) {}
450b57cec5SDimitry Andric   DWARFFormValue(const DWARFUnit *unit, dw_form_t form)
460b57cec5SDimitry Andric       : m_unit(unit), m_form(form) {}
47e8d8bef9SDimitry Andric   const DWARFUnit *GetUnit() const { return m_unit; }
480b57cec5SDimitry Andric   void SetUnit(const DWARFUnit *unit) { m_unit = unit; }
490b57cec5SDimitry Andric   dw_form_t Form() const { return m_form; }
500b57cec5SDimitry Andric   dw_form_t &FormRef() { return m_form; }
510b57cec5SDimitry Andric   void SetForm(dw_form_t form) { m_form = form; }
520b57cec5SDimitry Andric   const ValueType &Value() const { return m_value; }
530b57cec5SDimitry Andric   ValueType &ValueRef() { return m_value; }
540b57cec5SDimitry Andric   void SetValue(const ValueType &val) { m_value = val; }
550b57cec5SDimitry Andric 
565f757f3fSDimitry Andric   void Dump(Stream &s) const;
575f757f3fSDimitry Andric   bool ExtractValue(const DWARFDataExtractor &data, lldb::offset_t *offset_ptr);
580b57cec5SDimitry Andric   const uint8_t *BlockData() const;
59bdd1243dSDimitry Andric   static std::optional<uint8_t> GetFixedSize(dw_form_t form,
600b57cec5SDimitry Andric                                              const DWARFUnit *u);
61bdd1243dSDimitry Andric   std::optional<uint8_t> GetFixedSize() const;
620b57cec5SDimitry Andric   DWARFDIE Reference() const;
63*7a6dacacSDimitry Andric 
64*7a6dacacSDimitry Andric   /// If this is a reference to another DIE, return the corresponding DWARFUnit
65*7a6dacacSDimitry Andric   /// and DIE offset such that Unit->GetDIE(offset) produces the desired DIE.
66*7a6dacacSDimitry Andric   /// Otherwise, a nullptr and unspecified offset are returned.
67*7a6dacacSDimitry Andric   std::pair<DWARFUnit *, uint64_t> ReferencedUnitAndOffset() const;
68*7a6dacacSDimitry Andric 
690b57cec5SDimitry Andric   uint64_t Reference(dw_offset_t offset) const;
700b57cec5SDimitry Andric   bool Boolean() const { return m_value.value.uval != 0; }
710b57cec5SDimitry Andric   uint64_t Unsigned() const { return m_value.value.uval; }
720b57cec5SDimitry Andric   void SetUnsigned(uint64_t uval) { m_value.value.uval = uval; }
730b57cec5SDimitry Andric   int64_t Signed() const { return m_value.value.sval; }
740b57cec5SDimitry Andric   void SetSigned(int64_t sval) { m_value.value.sval = sval; }
750b57cec5SDimitry Andric   const char *AsCString() const;
760b57cec5SDimitry Andric   dw_addr_t Address() const;
770b57cec5SDimitry Andric   bool IsValid() const { return m_form != 0; }
785f757f3fSDimitry Andric   bool SkipValue(const DWARFDataExtractor &debug_info_data,
790b57cec5SDimitry Andric                  lldb::offset_t *offset_ptr) const;
800b57cec5SDimitry Andric   static bool SkipValue(const dw_form_t form,
815f757f3fSDimitry Andric                         const DWARFDataExtractor &debug_info_data,
820b57cec5SDimitry Andric                         lldb::offset_t *offset_ptr, const DWARFUnit *unit);
830b57cec5SDimitry Andric   static bool IsBlockForm(const dw_form_t form);
840b57cec5SDimitry Andric   static bool IsDataForm(const dw_form_t form);
850b57cec5SDimitry Andric   static int Compare(const DWARFFormValue &a, const DWARFFormValue &b);
860b57cec5SDimitry Andric   void Clear();
870b57cec5SDimitry Andric   static bool FormIsSupported(dw_form_t form);
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric protected:
900b57cec5SDimitry Andric   // Compile unit where m_value was located.
910b57cec5SDimitry Andric   // It may be different from compile unit where m_value refers to.
920b57cec5SDimitry Andric   const DWARFUnit *m_unit = nullptr; // Unit for this form
9306c3fb27SDimitry Andric   dw_form_t m_form = dw_form_t(0);   // Form for this value
940b57cec5SDimitry Andric   ValueType m_value;                 // Contains all data for the form
950b57cec5SDimitry Andric };
965f757f3fSDimitry Andric } // namespace dwarf
975f757f3fSDimitry Andric } // namespace lldb_private::plugin
980b57cec5SDimitry Andric 
995ffd83dbSDimitry Andric #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFFORMVALUE_H
100