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 "SBReproducerPrivate.h" 110b57cec5SDimitry Andric #include "lldb/API/SBLineEntry.h" 120b57cec5SDimitry Andric #include "lldb/API/SBStream.h" 130b57cec5SDimitry Andric #include "lldb/Core/Module.h" 140b57cec5SDimitry Andric #include "lldb/Symbol/CompileUnit.h" 150b57cec5SDimitry Andric #include "lldb/Symbol/LineEntry.h" 160b57cec5SDimitry Andric #include "lldb/Symbol/LineTable.h" 179dba64beSDimitry Andric #include "lldb/Symbol/SymbolFile.h" 180b57cec5SDimitry Andric #include "lldb/Symbol/Type.h" 199dba64beSDimitry Andric #include "lldb/Symbol/TypeList.h" 200b57cec5SDimitry Andric 210b57cec5SDimitry Andric using namespace lldb; 220b57cec5SDimitry Andric using namespace lldb_private; 230b57cec5SDimitry Andric 24*fe6060f1SDimitry Andric SBCompileUnit::SBCompileUnit() { 250b57cec5SDimitry Andric LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBCompileUnit); 260b57cec5SDimitry Andric } 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric SBCompileUnit::SBCompileUnit(lldb_private::CompileUnit *lldb_object_ptr) 290b57cec5SDimitry Andric : m_opaque_ptr(lldb_object_ptr) {} 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric SBCompileUnit::SBCompileUnit(const SBCompileUnit &rhs) 320b57cec5SDimitry Andric : m_opaque_ptr(rhs.m_opaque_ptr) { 330b57cec5SDimitry Andric LLDB_RECORD_CONSTRUCTOR(SBCompileUnit, (const lldb::SBCompileUnit &), rhs); 340b57cec5SDimitry Andric } 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric const SBCompileUnit &SBCompileUnit::operator=(const SBCompileUnit &rhs) { 370b57cec5SDimitry Andric LLDB_RECORD_METHOD(const lldb::SBCompileUnit &, 380b57cec5SDimitry Andric SBCompileUnit, operator=,(const lldb::SBCompileUnit &), 390b57cec5SDimitry Andric rhs); 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric m_opaque_ptr = rhs.m_opaque_ptr; 420b57cec5SDimitry Andric return LLDB_RECORD_RESULT(*this); 430b57cec5SDimitry Andric } 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric SBCompileUnit::~SBCompileUnit() { m_opaque_ptr = nullptr; } 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric SBFileSpec SBCompileUnit::GetFileSpec() const { 480b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBFileSpec, SBCompileUnit, 490b57cec5SDimitry Andric GetFileSpec); 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric SBFileSpec file_spec; 520b57cec5SDimitry Andric if (m_opaque_ptr) 53480093f4SDimitry Andric file_spec.SetFileSpec(m_opaque_ptr->GetPrimaryFile()); 540b57cec5SDimitry Andric return LLDB_RECORD_RESULT(file_spec); 550b57cec5SDimitry Andric } 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric uint32_t SBCompileUnit::GetNumLineEntries() const { 580b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBCompileUnit, GetNumLineEntries); 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric if (m_opaque_ptr) { 610b57cec5SDimitry Andric LineTable *line_table = m_opaque_ptr->GetLineTable(); 620b57cec5SDimitry Andric if (line_table) { 630b57cec5SDimitry Andric return line_table->GetSize(); 640b57cec5SDimitry Andric } 650b57cec5SDimitry Andric } 660b57cec5SDimitry Andric return 0; 670b57cec5SDimitry Andric } 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric SBLineEntry SBCompileUnit::GetLineEntryAtIndex(uint32_t idx) const { 700b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST(lldb::SBLineEntry, SBCompileUnit, 710b57cec5SDimitry Andric GetLineEntryAtIndex, (uint32_t), idx); 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric SBLineEntry sb_line_entry; 740b57cec5SDimitry Andric if (m_opaque_ptr) { 750b57cec5SDimitry Andric LineTable *line_table = m_opaque_ptr->GetLineTable(); 760b57cec5SDimitry Andric if (line_table) { 770b57cec5SDimitry Andric LineEntry line_entry; 780b57cec5SDimitry Andric if (line_table->GetLineEntryAtIndex(idx, line_entry)) 790b57cec5SDimitry Andric sb_line_entry.SetLineEntry(line_entry); 800b57cec5SDimitry Andric } 810b57cec5SDimitry Andric } 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric return LLDB_RECORD_RESULT(sb_line_entry); 840b57cec5SDimitry Andric } 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line, 870b57cec5SDimitry Andric SBFileSpec *inline_file_spec) const { 880b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST(uint32_t, SBCompileUnit, FindLineEntryIndex, 890b57cec5SDimitry Andric (uint32_t, uint32_t, lldb::SBFileSpec *), start_idx, 900b57cec5SDimitry Andric line, inline_file_spec); 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric const bool exact = true; 930b57cec5SDimitry Andric return FindLineEntryIndex(start_idx, line, inline_file_spec, exact); 940b57cec5SDimitry Andric } 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line, 970b57cec5SDimitry Andric SBFileSpec *inline_file_spec, 980b57cec5SDimitry Andric bool exact) const { 990b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST(uint32_t, SBCompileUnit, FindLineEntryIndex, 1000b57cec5SDimitry Andric (uint32_t, uint32_t, lldb::SBFileSpec *, bool), 1010b57cec5SDimitry Andric start_idx, line, inline_file_spec, exact); 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric uint32_t index = UINT32_MAX; 1040b57cec5SDimitry Andric if (m_opaque_ptr) { 1050b57cec5SDimitry Andric FileSpec file_spec; 1060b57cec5SDimitry Andric if (inline_file_spec && inline_file_spec->IsValid()) 1070b57cec5SDimitry Andric file_spec = inline_file_spec->ref(); 1080b57cec5SDimitry Andric else 109480093f4SDimitry Andric file_spec = m_opaque_ptr->GetPrimaryFile(); 1100b57cec5SDimitry Andric 111*fe6060f1SDimitry Andric LineEntry line_entry; 1120b57cec5SDimitry Andric index = m_opaque_ptr->FindLineEntry( 1130b57cec5SDimitry Andric start_idx, line, inline_file_spec ? inline_file_spec->get() : nullptr, 114*fe6060f1SDimitry Andric exact, &line_entry); 1150b57cec5SDimitry Andric } 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric return index; 1180b57cec5SDimitry Andric } 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric uint32_t SBCompileUnit::GetNumSupportFiles() const { 1210b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBCompileUnit, GetNumSupportFiles); 1220b57cec5SDimitry Andric 1230b57cec5SDimitry Andric if (m_opaque_ptr) 1240b57cec5SDimitry Andric return m_opaque_ptr->GetSupportFiles().GetSize(); 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric return 0; 1270b57cec5SDimitry Andric } 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric lldb::SBTypeList SBCompileUnit::GetTypes(uint32_t type_mask) { 1300b57cec5SDimitry Andric LLDB_RECORD_METHOD(lldb::SBTypeList, SBCompileUnit, GetTypes, (uint32_t), 1310b57cec5SDimitry Andric type_mask); 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric SBTypeList sb_type_list; 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric if (!m_opaque_ptr) 1360b57cec5SDimitry Andric return LLDB_RECORD_RESULT(sb_type_list); 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric ModuleSP module_sp(m_opaque_ptr->GetModule()); 1390b57cec5SDimitry Andric if (!module_sp) 1400b57cec5SDimitry Andric return LLDB_RECORD_RESULT(sb_type_list); 1410b57cec5SDimitry Andric 1429dba64beSDimitry Andric SymbolFile *symfile = module_sp->GetSymbolFile(); 1439dba64beSDimitry Andric if (!symfile) 1440b57cec5SDimitry Andric return LLDB_RECORD_RESULT(sb_type_list); 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric TypeClass type_class = static_cast<TypeClass>(type_mask); 1470b57cec5SDimitry Andric TypeList type_list; 1489dba64beSDimitry Andric symfile->GetTypes(m_opaque_ptr, type_class, type_list); 1490b57cec5SDimitry Andric sb_type_list.m_opaque_up->Append(type_list); 1500b57cec5SDimitry Andric return LLDB_RECORD_RESULT(sb_type_list); 1510b57cec5SDimitry Andric } 1520b57cec5SDimitry Andric 1530b57cec5SDimitry Andric SBFileSpec SBCompileUnit::GetSupportFileAtIndex(uint32_t idx) const { 1540b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST(lldb::SBFileSpec, SBCompileUnit, 1550b57cec5SDimitry Andric GetSupportFileAtIndex, (uint32_t), idx); 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric SBFileSpec sb_file_spec; 1580b57cec5SDimitry Andric if (m_opaque_ptr) { 1590b57cec5SDimitry Andric FileSpec spec = m_opaque_ptr->GetSupportFiles().GetFileSpecAtIndex(idx); 1600b57cec5SDimitry Andric sb_file_spec.SetFileSpec(spec); 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric return LLDB_RECORD_RESULT(sb_file_spec); 1650b57cec5SDimitry Andric } 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric uint32_t SBCompileUnit::FindSupportFileIndex(uint32_t start_idx, 1680b57cec5SDimitry Andric const SBFileSpec &sb_file, 1690b57cec5SDimitry Andric bool full) { 1700b57cec5SDimitry Andric LLDB_RECORD_METHOD(uint32_t, SBCompileUnit, FindSupportFileIndex, 1710b57cec5SDimitry Andric (uint32_t, const lldb::SBFileSpec &, bool), start_idx, 1720b57cec5SDimitry Andric sb_file, full); 1730b57cec5SDimitry Andric 1740b57cec5SDimitry Andric if (m_opaque_ptr) { 1750b57cec5SDimitry Andric const FileSpecList &support_files = m_opaque_ptr->GetSupportFiles(); 1760b57cec5SDimitry Andric return support_files.FindFileIndex(start_idx, sb_file.ref(), full); 1770b57cec5SDimitry Andric } 1780b57cec5SDimitry Andric return 0; 1790b57cec5SDimitry Andric } 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric lldb::LanguageType SBCompileUnit::GetLanguage() { 1820b57cec5SDimitry Andric LLDB_RECORD_METHOD_NO_ARGS(lldb::LanguageType, SBCompileUnit, GetLanguage); 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric if (m_opaque_ptr) 1850b57cec5SDimitry Andric return m_opaque_ptr->GetLanguage(); 1860b57cec5SDimitry Andric return lldb::eLanguageTypeUnknown; 1870b57cec5SDimitry Andric } 1880b57cec5SDimitry Andric 1890b57cec5SDimitry Andric bool SBCompileUnit::IsValid() const { 1900b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCompileUnit, IsValid); 1910b57cec5SDimitry Andric return this->operator bool(); 1920b57cec5SDimitry Andric } 1930b57cec5SDimitry Andric SBCompileUnit::operator bool() const { 1940b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCompileUnit, operator bool); 1950b57cec5SDimitry Andric 1960b57cec5SDimitry Andric return m_opaque_ptr != nullptr; 1970b57cec5SDimitry Andric } 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric bool SBCompileUnit::operator==(const SBCompileUnit &rhs) const { 2000b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST( 2010b57cec5SDimitry Andric bool, SBCompileUnit, operator==,(const lldb::SBCompileUnit &), rhs); 2020b57cec5SDimitry Andric 2030b57cec5SDimitry Andric return m_opaque_ptr == rhs.m_opaque_ptr; 2040b57cec5SDimitry Andric } 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric bool SBCompileUnit::operator!=(const SBCompileUnit &rhs) const { 2070b57cec5SDimitry Andric LLDB_RECORD_METHOD_CONST( 2080b57cec5SDimitry Andric bool, SBCompileUnit, operator!=,(const lldb::SBCompileUnit &), rhs); 2090b57cec5SDimitry Andric 2100b57cec5SDimitry Andric return m_opaque_ptr != rhs.m_opaque_ptr; 2110b57cec5SDimitry Andric } 2120b57cec5SDimitry Andric 2130b57cec5SDimitry Andric const lldb_private::CompileUnit *SBCompileUnit::operator->() const { 2140b57cec5SDimitry Andric return m_opaque_ptr; 2150b57cec5SDimitry Andric } 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric const lldb_private::CompileUnit &SBCompileUnit::operator*() const { 2180b57cec5SDimitry Andric return *m_opaque_ptr; 2190b57cec5SDimitry Andric } 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric lldb_private::CompileUnit *SBCompileUnit::get() { return m_opaque_ptr; } 2220b57cec5SDimitry Andric 2230b57cec5SDimitry Andric void SBCompileUnit::reset(lldb_private::CompileUnit *lldb_object_ptr) { 2240b57cec5SDimitry Andric m_opaque_ptr = lldb_object_ptr; 2250b57cec5SDimitry Andric } 2260b57cec5SDimitry Andric 2270b57cec5SDimitry Andric bool SBCompileUnit::GetDescription(SBStream &description) { 2280b57cec5SDimitry Andric LLDB_RECORD_METHOD(bool, SBCompileUnit, GetDescription, (lldb::SBStream &), 2290b57cec5SDimitry Andric description); 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric Stream &strm = description.ref(); 2320b57cec5SDimitry Andric 2330b57cec5SDimitry Andric if (m_opaque_ptr) { 2340b57cec5SDimitry Andric m_opaque_ptr->Dump(&strm, false); 2350b57cec5SDimitry Andric } else 2360b57cec5SDimitry Andric strm.PutCString("No value"); 2370b57cec5SDimitry Andric 2380b57cec5SDimitry Andric return true; 2390b57cec5SDimitry Andric } 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric namespace lldb_private { 2420b57cec5SDimitry Andric namespace repro { 2430b57cec5SDimitry Andric 2440b57cec5SDimitry Andric template <> 2450b57cec5SDimitry Andric void RegisterMethods<SBCompileUnit>(Registry &R) { 2460b57cec5SDimitry Andric LLDB_REGISTER_CONSTRUCTOR(SBCompileUnit, ()); 2470b57cec5SDimitry Andric LLDB_REGISTER_CONSTRUCTOR(SBCompileUnit, (const lldb::SBCompileUnit &)); 2480b57cec5SDimitry Andric LLDB_REGISTER_METHOD( 2490b57cec5SDimitry Andric const lldb::SBCompileUnit &, 2500b57cec5SDimitry Andric SBCompileUnit, operator=,(const lldb::SBCompileUnit &)); 2510b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(lldb::SBFileSpec, SBCompileUnit, GetFileSpec, 2520b57cec5SDimitry Andric ()); 2530b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(uint32_t, SBCompileUnit, GetNumLineEntries, ()); 2540b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(lldb::SBLineEntry, SBCompileUnit, 2550b57cec5SDimitry Andric GetLineEntryAtIndex, (uint32_t)); 2560b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(uint32_t, SBCompileUnit, FindLineEntryIndex, 2570b57cec5SDimitry Andric (uint32_t, uint32_t, lldb::SBFileSpec *)); 2580b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(uint32_t, SBCompileUnit, FindLineEntryIndex, 2590b57cec5SDimitry Andric (uint32_t, uint32_t, lldb::SBFileSpec *, bool)); 2600b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(uint32_t, SBCompileUnit, GetNumSupportFiles, ()); 2610b57cec5SDimitry Andric LLDB_REGISTER_METHOD(lldb::SBTypeList, SBCompileUnit, GetTypes, (uint32_t)); 2620b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(lldb::SBFileSpec, SBCompileUnit, 2630b57cec5SDimitry Andric GetSupportFileAtIndex, (uint32_t)); 2640b57cec5SDimitry Andric LLDB_REGISTER_METHOD(uint32_t, SBCompileUnit, FindSupportFileIndex, 2650b57cec5SDimitry Andric (uint32_t, const lldb::SBFileSpec &, bool)); 2660b57cec5SDimitry Andric LLDB_REGISTER_METHOD(lldb::LanguageType, SBCompileUnit, GetLanguage, ()); 2670b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(bool, SBCompileUnit, IsValid, ()); 2680b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST(bool, SBCompileUnit, operator bool, ()); 2690b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST( 2700b57cec5SDimitry Andric bool, SBCompileUnit, operator==,(const lldb::SBCompileUnit &)); 2710b57cec5SDimitry Andric LLDB_REGISTER_METHOD_CONST( 2720b57cec5SDimitry Andric bool, SBCompileUnit, operator!=,(const lldb::SBCompileUnit &)); 2730b57cec5SDimitry Andric LLDB_REGISTER_METHOD(bool, SBCompileUnit, GetDescription, 2740b57cec5SDimitry Andric (lldb::SBStream &)); 2750b57cec5SDimitry Andric } 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric } 2780b57cec5SDimitry Andric } 279