xref: /freebsd/contrib/llvm-project/lldb/source/API/SBCompileUnit.cpp (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
15ffd83dbSDimitry Andric //===-- SBCompileUnit.cpp -------------------------------------------------===//
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 
90b57cec5SDimitry Andric #include "lldb/API/SBCompileUnit.h"
100b57cec5SDimitry Andric #include "lldb/API/SBLineEntry.h"
110b57cec5SDimitry Andric #include "lldb/API/SBStream.h"
120b57cec5SDimitry Andric #include "lldb/Core/Module.h"
130b57cec5SDimitry Andric #include "lldb/Symbol/CompileUnit.h"
140b57cec5SDimitry Andric #include "lldb/Symbol/LineEntry.h"
150b57cec5SDimitry Andric #include "lldb/Symbol/LineTable.h"
169dba64beSDimitry Andric #include "lldb/Symbol/SymbolFile.h"
170b57cec5SDimitry Andric #include "lldb/Symbol/Type.h"
189dba64beSDimitry Andric #include "lldb/Symbol/TypeList.h"
1904eeddc0SDimitry Andric #include "lldb/Utility/Instrumentation.h"
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric using namespace lldb;
220b57cec5SDimitry Andric using namespace lldb_private;
230b57cec5SDimitry Andric 
2404eeddc0SDimitry Andric SBCompileUnit::SBCompileUnit() { LLDB_INSTRUMENT_VA(this); }
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric SBCompileUnit::SBCompileUnit(lldb_private::CompileUnit *lldb_object_ptr)
270b57cec5SDimitry Andric     : m_opaque_ptr(lldb_object_ptr) {}
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric SBCompileUnit::SBCompileUnit(const SBCompileUnit &rhs)
300b57cec5SDimitry Andric     : m_opaque_ptr(rhs.m_opaque_ptr) {
3104eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
320b57cec5SDimitry Andric }
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric const SBCompileUnit &SBCompileUnit::operator=(const SBCompileUnit &rhs) {
3504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric   m_opaque_ptr = rhs.m_opaque_ptr;
3804eeddc0SDimitry Andric   return *this;
390b57cec5SDimitry Andric }
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric SBCompileUnit::~SBCompileUnit() { m_opaque_ptr = nullptr; }
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric SBFileSpec SBCompileUnit::GetFileSpec() const {
4404eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric   SBFileSpec file_spec;
470b57cec5SDimitry Andric   if (m_opaque_ptr)
48480093f4SDimitry Andric     file_spec.SetFileSpec(m_opaque_ptr->GetPrimaryFile());
4904eeddc0SDimitry Andric   return file_spec;
500b57cec5SDimitry Andric }
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric uint32_t SBCompileUnit::GetNumLineEntries() const {
5304eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   if (m_opaque_ptr) {
560b57cec5SDimitry Andric     LineTable *line_table = m_opaque_ptr->GetLineTable();
570b57cec5SDimitry Andric     if (line_table) {
580b57cec5SDimitry Andric       return line_table->GetSize();
590b57cec5SDimitry Andric     }
600b57cec5SDimitry Andric   }
610b57cec5SDimitry Andric   return 0;
620b57cec5SDimitry Andric }
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric SBLineEntry SBCompileUnit::GetLineEntryAtIndex(uint32_t idx) const {
6504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, idx);
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric   SBLineEntry sb_line_entry;
680b57cec5SDimitry Andric   if (m_opaque_ptr) {
690b57cec5SDimitry Andric     LineTable *line_table = m_opaque_ptr->GetLineTable();
700b57cec5SDimitry Andric     if (line_table) {
710b57cec5SDimitry Andric       LineEntry line_entry;
720b57cec5SDimitry Andric       if (line_table->GetLineEntryAtIndex(idx, line_entry))
730b57cec5SDimitry Andric         sb_line_entry.SetLineEntry(line_entry);
740b57cec5SDimitry Andric     }
750b57cec5SDimitry Andric   }
760b57cec5SDimitry Andric 
7704eeddc0SDimitry Andric   return sb_line_entry;
780b57cec5SDimitry Andric }
790b57cec5SDimitry Andric 
8081ad6265SDimitry Andric uint32_t SBCompileUnit::FindLineEntryIndex(lldb::SBLineEntry &line_entry,
8181ad6265SDimitry Andric                                            bool exact) const {
8281ad6265SDimitry Andric   LLDB_INSTRUMENT_VA(this, line_entry, exact);
8381ad6265SDimitry Andric 
8481ad6265SDimitry Andric   if (!m_opaque_ptr || !line_entry.IsValid())
8581ad6265SDimitry Andric     return UINT32_MAX;
8681ad6265SDimitry Andric 
8781ad6265SDimitry Andric   LineEntry found_line_entry;
8881ad6265SDimitry Andric 
8981ad6265SDimitry Andric   return m_opaque_ptr->FindLineEntry(0, line_entry.GetLine(),
9081ad6265SDimitry Andric                                      line_entry.GetFileSpec().get(), exact,
9181ad6265SDimitry Andric                                      &line_entry.ref());
9281ad6265SDimitry Andric }
9381ad6265SDimitry Andric 
940b57cec5SDimitry Andric uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
950b57cec5SDimitry Andric                                            SBFileSpec *inline_file_spec) const {
9604eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, start_idx, line, inline_file_spec);
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric   const bool exact = true;
990b57cec5SDimitry Andric   return FindLineEntryIndex(start_idx, line, inline_file_spec, exact);
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
1030b57cec5SDimitry Andric                                            SBFileSpec *inline_file_spec,
1040b57cec5SDimitry Andric                                            bool exact) const {
10504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, start_idx, line, inline_file_spec, exact);
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric   uint32_t index = UINT32_MAX;
1080b57cec5SDimitry Andric   if (m_opaque_ptr) {
1090b57cec5SDimitry Andric     FileSpec file_spec;
1100b57cec5SDimitry Andric     if (inline_file_spec && inline_file_spec->IsValid())
1110b57cec5SDimitry Andric       file_spec = inline_file_spec->ref();
1120b57cec5SDimitry Andric     else
113480093f4SDimitry Andric       file_spec = m_opaque_ptr->GetPrimaryFile();
1140b57cec5SDimitry Andric 
115fe6060f1SDimitry Andric     LineEntry line_entry;
1160b57cec5SDimitry Andric     index = m_opaque_ptr->FindLineEntry(
1170b57cec5SDimitry Andric         start_idx, line, inline_file_spec ? inline_file_spec->get() : nullptr,
118fe6060f1SDimitry Andric         exact, &line_entry);
1190b57cec5SDimitry Andric   }
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric   return index;
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric uint32_t SBCompileUnit::GetNumSupportFiles() const {
12504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric   if (m_opaque_ptr)
1280b57cec5SDimitry Andric     return m_opaque_ptr->GetSupportFiles().GetSize();
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric   return 0;
1310b57cec5SDimitry Andric }
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric lldb::SBTypeList SBCompileUnit::GetTypes(uint32_t type_mask) {
13404eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, type_mask);
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric   SBTypeList sb_type_list;
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric   if (!m_opaque_ptr)
13904eeddc0SDimitry Andric     return sb_type_list;
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   ModuleSP module_sp(m_opaque_ptr->GetModule());
1420b57cec5SDimitry Andric   if (!module_sp)
14304eeddc0SDimitry Andric     return sb_type_list;
1440b57cec5SDimitry Andric 
1459dba64beSDimitry Andric   SymbolFile *symfile = module_sp->GetSymbolFile();
1469dba64beSDimitry Andric   if (!symfile)
14704eeddc0SDimitry Andric     return sb_type_list;
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric   TypeClass type_class = static_cast<TypeClass>(type_mask);
1500b57cec5SDimitry Andric   TypeList type_list;
1519dba64beSDimitry Andric   symfile->GetTypes(m_opaque_ptr, type_class, type_list);
1520b57cec5SDimitry Andric   sb_type_list.m_opaque_up->Append(type_list);
15304eeddc0SDimitry Andric   return sb_type_list;
1540b57cec5SDimitry Andric }
1550b57cec5SDimitry Andric 
1560b57cec5SDimitry Andric SBFileSpec SBCompileUnit::GetSupportFileAtIndex(uint32_t idx) const {
15704eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, idx);
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric   SBFileSpec sb_file_spec;
1600b57cec5SDimitry Andric   if (m_opaque_ptr) {
1610b57cec5SDimitry Andric     FileSpec spec = m_opaque_ptr->GetSupportFiles().GetFileSpecAtIndex(idx);
1620b57cec5SDimitry Andric     sb_file_spec.SetFileSpec(spec);
1630b57cec5SDimitry Andric   }
1640b57cec5SDimitry Andric 
16504eeddc0SDimitry Andric   return sb_file_spec;
1660b57cec5SDimitry Andric }
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric uint32_t SBCompileUnit::FindSupportFileIndex(uint32_t start_idx,
1690b57cec5SDimitry Andric                                              const SBFileSpec &sb_file,
1700b57cec5SDimitry Andric                                              bool full) {
17104eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, start_idx, sb_file, full);
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric   if (m_opaque_ptr) {
174*1db9f3b2SDimitry Andric     const SupportFileList &support_files = m_opaque_ptr->GetSupportFiles();
1750b57cec5SDimitry Andric     return support_files.FindFileIndex(start_idx, sb_file.ref(), full);
1760b57cec5SDimitry Andric   }
1770b57cec5SDimitry Andric   return 0;
1780b57cec5SDimitry Andric }
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric lldb::LanguageType SBCompileUnit::GetLanguage() {
18104eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric   if (m_opaque_ptr)
1840b57cec5SDimitry Andric     return m_opaque_ptr->GetLanguage();
1850b57cec5SDimitry Andric   return lldb::eLanguageTypeUnknown;
1860b57cec5SDimitry Andric }
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric bool SBCompileUnit::IsValid() const {
18904eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1900b57cec5SDimitry Andric   return this->operator bool();
1910b57cec5SDimitry Andric }
1920b57cec5SDimitry Andric SBCompileUnit::operator bool() const {
19304eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this);
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric   return m_opaque_ptr != nullptr;
1960b57cec5SDimitry Andric }
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric bool SBCompileUnit::operator==(const SBCompileUnit &rhs) const {
19904eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric   return m_opaque_ptr == rhs.m_opaque_ptr;
2020b57cec5SDimitry Andric }
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric bool SBCompileUnit::operator!=(const SBCompileUnit &rhs) const {
20504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, rhs);
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric   return m_opaque_ptr != rhs.m_opaque_ptr;
2080b57cec5SDimitry Andric }
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric const lldb_private::CompileUnit *SBCompileUnit::operator->() const {
2110b57cec5SDimitry Andric   return m_opaque_ptr;
2120b57cec5SDimitry Andric }
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric const lldb_private::CompileUnit &SBCompileUnit::operator*() const {
2150b57cec5SDimitry Andric   return *m_opaque_ptr;
2160b57cec5SDimitry Andric }
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric lldb_private::CompileUnit *SBCompileUnit::get() { return m_opaque_ptr; }
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric void SBCompileUnit::reset(lldb_private::CompileUnit *lldb_object_ptr) {
2210b57cec5SDimitry Andric   m_opaque_ptr = lldb_object_ptr;
2220b57cec5SDimitry Andric }
2230b57cec5SDimitry Andric 
2240b57cec5SDimitry Andric bool SBCompileUnit::GetDescription(SBStream &description) {
22504eeddc0SDimitry Andric   LLDB_INSTRUMENT_VA(this, description);
2260b57cec5SDimitry Andric 
2270b57cec5SDimitry Andric   Stream &strm = description.ref();
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric   if (m_opaque_ptr) {
2300b57cec5SDimitry Andric     m_opaque_ptr->Dump(&strm, false);
2310b57cec5SDimitry Andric   } else
2320b57cec5SDimitry Andric     strm.PutCString("No value");
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric   return true;
2350b57cec5SDimitry Andric }
236