1cfca06d7SDimitry Andric //===-- SBCompileUnit.cpp -------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/API/SBCompileUnit.h"
10f034231aSEd Maste #include "lldb/API/SBLineEntry.h"
11f034231aSEd Maste #include "lldb/API/SBStream.h"
12f034231aSEd Maste #include "lldb/Core/Module.h"
13f034231aSEd Maste #include "lldb/Symbol/CompileUnit.h"
14f034231aSEd Maste #include "lldb/Symbol/LineEntry.h"
15f034231aSEd Maste #include "lldb/Symbol/LineTable.h"
16ead24645SDimitry Andric #include "lldb/Symbol/SymbolFile.h"
17f034231aSEd Maste #include "lldb/Symbol/Type.h"
18ead24645SDimitry Andric #include "lldb/Symbol/TypeList.h"
196f8fc217SDimitry Andric #include "lldb/Utility/Instrumentation.h"
20f034231aSEd Maste
21f034231aSEd Maste using namespace lldb;
22f034231aSEd Maste using namespace lldb_private;
23f034231aSEd Maste
SBCompileUnit()246f8fc217SDimitry Andric SBCompileUnit::SBCompileUnit() { LLDB_INSTRUMENT_VA(this); }
25f034231aSEd Maste
SBCompileUnit(lldb_private::CompileUnit * lldb_object_ptr)2614f1b3e8SDimitry Andric SBCompileUnit::SBCompileUnit(lldb_private::CompileUnit *lldb_object_ptr)
2714f1b3e8SDimitry Andric : m_opaque_ptr(lldb_object_ptr) {}
28f034231aSEd Maste
SBCompileUnit(const SBCompileUnit & rhs)2914f1b3e8SDimitry Andric SBCompileUnit::SBCompileUnit(const SBCompileUnit &rhs)
305f29bb8aSDimitry Andric : m_opaque_ptr(rhs.m_opaque_ptr) {
316f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
32f034231aSEd Maste }
33f034231aSEd Maste
operator =(const SBCompileUnit & rhs)345f29bb8aSDimitry Andric const SBCompileUnit &SBCompileUnit::operator=(const SBCompileUnit &rhs) {
356f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
365f29bb8aSDimitry Andric
375f29bb8aSDimitry Andric m_opaque_ptr = rhs.m_opaque_ptr;
386f8fc217SDimitry Andric return *this;
395f29bb8aSDimitry Andric }
405f29bb8aSDimitry Andric
~SBCompileUnit()415f29bb8aSDimitry Andric SBCompileUnit::~SBCompileUnit() { m_opaque_ptr = nullptr; }
42f034231aSEd Maste
GetFileSpec() const4314f1b3e8SDimitry Andric SBFileSpec SBCompileUnit::GetFileSpec() const {
446f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
455f29bb8aSDimitry Andric
46f034231aSEd Maste SBFileSpec file_spec;
47f034231aSEd Maste if (m_opaque_ptr)
48706b4fc4SDimitry Andric file_spec.SetFileSpec(m_opaque_ptr->GetPrimaryFile());
496f8fc217SDimitry Andric return file_spec;
50f034231aSEd Maste }
51f034231aSEd Maste
GetNumLineEntries() const5214f1b3e8SDimitry Andric uint32_t SBCompileUnit::GetNumLineEntries() const {
536f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
545f29bb8aSDimitry Andric
5514f1b3e8SDimitry Andric if (m_opaque_ptr) {
56f034231aSEd Maste LineTable *line_table = m_opaque_ptr->GetLineTable();
575f29bb8aSDimitry Andric if (line_table) {
58f034231aSEd Maste return line_table->GetSize();
59f034231aSEd Maste }
605f29bb8aSDimitry Andric }
61f034231aSEd Maste return 0;
62f034231aSEd Maste }
63f034231aSEd Maste
GetLineEntryAtIndex(uint32_t idx) const6414f1b3e8SDimitry Andric SBLineEntry SBCompileUnit::GetLineEntryAtIndex(uint32_t idx) const {
656f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, idx);
66f034231aSEd Maste
67f034231aSEd Maste SBLineEntry sb_line_entry;
6814f1b3e8SDimitry Andric if (m_opaque_ptr) {
69f034231aSEd Maste LineTable *line_table = m_opaque_ptr->GetLineTable();
7014f1b3e8SDimitry Andric if (line_table) {
71f034231aSEd Maste LineEntry line_entry;
72f034231aSEd Maste if (line_table->GetLineEntryAtIndex(idx, line_entry))
73f034231aSEd Maste sb_line_entry.SetLineEntry(line_entry);
74f034231aSEd Maste }
75f034231aSEd Maste }
76f034231aSEd Maste
776f8fc217SDimitry Andric return sb_line_entry;
78f034231aSEd Maste }
79f034231aSEd Maste
FindLineEntryIndex(lldb::SBLineEntry & line_entry,bool exact) const80145449b1SDimitry Andric uint32_t SBCompileUnit::FindLineEntryIndex(lldb::SBLineEntry &line_entry,
81145449b1SDimitry Andric bool exact) const {
82145449b1SDimitry Andric LLDB_INSTRUMENT_VA(this, line_entry, exact);
83145449b1SDimitry Andric
84145449b1SDimitry Andric if (!m_opaque_ptr || !line_entry.IsValid())
85145449b1SDimitry Andric return UINT32_MAX;
86145449b1SDimitry Andric
87145449b1SDimitry Andric LineEntry found_line_entry;
88145449b1SDimitry Andric
89145449b1SDimitry Andric return m_opaque_ptr->FindLineEntry(0, line_entry.GetLine(),
90145449b1SDimitry Andric line_entry.GetFileSpec().get(), exact,
91145449b1SDimitry Andric &line_entry.ref());
92145449b1SDimitry Andric }
93145449b1SDimitry Andric
FindLineEntryIndex(uint32_t start_idx,uint32_t line,SBFileSpec * inline_file_spec) const9414f1b3e8SDimitry Andric uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
9514f1b3e8SDimitry Andric SBFileSpec *inline_file_spec) const {
966f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, start_idx, line, inline_file_spec);
975f29bb8aSDimitry Andric
98f034231aSEd Maste const bool exact = true;
99f034231aSEd Maste return FindLineEntryIndex(start_idx, line, inline_file_spec, exact);
100f034231aSEd Maste }
101f034231aSEd Maste
FindLineEntryIndex(uint32_t start_idx,uint32_t line,SBFileSpec * inline_file_spec,bool exact) const10214f1b3e8SDimitry Andric uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
10314f1b3e8SDimitry Andric SBFileSpec *inline_file_spec,
10414f1b3e8SDimitry Andric bool exact) const {
1056f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, start_idx, line, inline_file_spec, exact);
106f034231aSEd Maste
107f034231aSEd Maste uint32_t index = UINT32_MAX;
10814f1b3e8SDimitry Andric if (m_opaque_ptr) {
109f034231aSEd Maste FileSpec file_spec;
110f034231aSEd Maste if (inline_file_spec && inline_file_spec->IsValid())
111f034231aSEd Maste file_spec = inline_file_spec->ref();
112f034231aSEd Maste else
113706b4fc4SDimitry Andric file_spec = m_opaque_ptr->GetPrimaryFile();
114f034231aSEd Maste
115344a3780SDimitry Andric LineEntry line_entry;
11614f1b3e8SDimitry Andric index = m_opaque_ptr->FindLineEntry(
1175f29bb8aSDimitry Andric start_idx, line, inline_file_spec ? inline_file_spec->get() : nullptr,
118344a3780SDimitry Andric exact, &line_entry);
119f034231aSEd Maste }
120f034231aSEd Maste
121f034231aSEd Maste return index;
122f034231aSEd Maste }
123f034231aSEd Maste
GetNumSupportFiles() const12414f1b3e8SDimitry Andric uint32_t SBCompileUnit::GetNumSupportFiles() const {
1256f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1265f29bb8aSDimitry Andric
1275f29bb8aSDimitry Andric if (m_opaque_ptr)
1285f29bb8aSDimitry Andric return m_opaque_ptr->GetSupportFiles().GetSize();
1295f29bb8aSDimitry Andric
130f034231aSEd Maste return 0;
131f034231aSEd Maste }
132f034231aSEd Maste
GetTypes(uint32_t type_mask)13314f1b3e8SDimitry Andric lldb::SBTypeList SBCompileUnit::GetTypes(uint32_t type_mask) {
1346f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, type_mask);
1355f29bb8aSDimitry Andric
136f034231aSEd Maste SBTypeList sb_type_list;
137f034231aSEd Maste
13894994d37SDimitry Andric if (!m_opaque_ptr)
1396f8fc217SDimitry Andric return sb_type_list;
14094994d37SDimitry Andric
141f034231aSEd Maste ModuleSP module_sp(m_opaque_ptr->GetModule());
14294994d37SDimitry Andric if (!module_sp)
1436f8fc217SDimitry Andric return sb_type_list;
14494994d37SDimitry Andric
145ead24645SDimitry Andric SymbolFile *symfile = module_sp->GetSymbolFile();
146ead24645SDimitry Andric if (!symfile)
1476f8fc217SDimitry Andric return sb_type_list;
14894994d37SDimitry Andric
14994994d37SDimitry Andric TypeClass type_class = static_cast<TypeClass>(type_mask);
150f034231aSEd Maste TypeList type_list;
151ead24645SDimitry Andric symfile->GetTypes(m_opaque_ptr, type_class, type_list);
1525f29bb8aSDimitry Andric sb_type_list.m_opaque_up->Append(type_list);
1536f8fc217SDimitry Andric return sb_type_list;
154f034231aSEd Maste }
155f034231aSEd Maste
GetSupportFileAtIndex(uint32_t idx) const15614f1b3e8SDimitry Andric SBFileSpec SBCompileUnit::GetSupportFileAtIndex(uint32_t idx) const {
1576f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, idx);
158f034231aSEd Maste
159f034231aSEd Maste SBFileSpec sb_file_spec;
16014f1b3e8SDimitry Andric if (m_opaque_ptr) {
1615f29bb8aSDimitry Andric FileSpec spec = m_opaque_ptr->GetSupportFiles().GetFileSpecAtIndex(idx);
1625f29bb8aSDimitry Andric sb_file_spec.SetFileSpec(spec);
163f034231aSEd Maste }
164f034231aSEd Maste
1656f8fc217SDimitry Andric return sb_file_spec;
166f034231aSEd Maste }
167f034231aSEd Maste
FindSupportFileIndex(uint32_t start_idx,const SBFileSpec & sb_file,bool full)16814f1b3e8SDimitry Andric uint32_t SBCompileUnit::FindSupportFileIndex(uint32_t start_idx,
16914f1b3e8SDimitry Andric const SBFileSpec &sb_file,
17014f1b3e8SDimitry Andric bool full) {
1716f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, start_idx, sb_file, full);
1725f29bb8aSDimitry Andric
17314f1b3e8SDimitry Andric if (m_opaque_ptr) {
174aca2e42cSDimitry Andric const SupportFileList &support_files = m_opaque_ptr->GetSupportFiles();
175f034231aSEd Maste return support_files.FindFileIndex(start_idx, sb_file.ref(), full);
176f034231aSEd Maste }
177f034231aSEd Maste return 0;
178f034231aSEd Maste }
179f034231aSEd Maste
GetLanguage()18014f1b3e8SDimitry Andric lldb::LanguageType SBCompileUnit::GetLanguage() {
1816f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1825f29bb8aSDimitry Andric
183205afe67SEd Maste if (m_opaque_ptr)
184205afe67SEd Maste return m_opaque_ptr->GetLanguage();
185205afe67SEd Maste return lldb::eLanguageTypeUnknown;
186205afe67SEd Maste }
187205afe67SEd Maste
IsValid() const1885f29bb8aSDimitry Andric bool SBCompileUnit::IsValid() const {
1896f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1905f29bb8aSDimitry Andric return this->operator bool();
1915f29bb8aSDimitry Andric }
operator bool() const1925f29bb8aSDimitry Andric SBCompileUnit::operator bool() const {
1936f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1945f29bb8aSDimitry Andric
1955f29bb8aSDimitry Andric return m_opaque_ptr != nullptr;
1965f29bb8aSDimitry Andric }
197f034231aSEd Maste
operator ==(const SBCompileUnit & rhs) const19814f1b3e8SDimitry Andric bool SBCompileUnit::operator==(const SBCompileUnit &rhs) const {
1996f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
2005f29bb8aSDimitry Andric
201f034231aSEd Maste return m_opaque_ptr == rhs.m_opaque_ptr;
202f034231aSEd Maste }
203f034231aSEd Maste
operator !=(const SBCompileUnit & rhs) const20414f1b3e8SDimitry Andric bool SBCompileUnit::operator!=(const SBCompileUnit &rhs) const {
2056f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
2065f29bb8aSDimitry Andric
207f034231aSEd Maste return m_opaque_ptr != rhs.m_opaque_ptr;
208f034231aSEd Maste }
209f034231aSEd Maste
operator ->() const21014f1b3e8SDimitry Andric const lldb_private::CompileUnit *SBCompileUnit::operator->() const {
211f034231aSEd Maste return m_opaque_ptr;
212f034231aSEd Maste }
213f034231aSEd Maste
operator *() const21414f1b3e8SDimitry Andric const lldb_private::CompileUnit &SBCompileUnit::operator*() const {
215f034231aSEd Maste return *m_opaque_ptr;
216f034231aSEd Maste }
217f034231aSEd Maste
get()21814f1b3e8SDimitry Andric lldb_private::CompileUnit *SBCompileUnit::get() { return m_opaque_ptr; }
219f034231aSEd Maste
reset(lldb_private::CompileUnit * lldb_object_ptr)22014f1b3e8SDimitry Andric void SBCompileUnit::reset(lldb_private::CompileUnit *lldb_object_ptr) {
221f034231aSEd Maste m_opaque_ptr = lldb_object_ptr;
222f034231aSEd Maste }
223f034231aSEd Maste
GetDescription(SBStream & description)22414f1b3e8SDimitry Andric bool SBCompileUnit::GetDescription(SBStream &description) {
2256f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, description);
2265f29bb8aSDimitry Andric
227f034231aSEd Maste Stream &strm = description.ref();
228f034231aSEd Maste
22914f1b3e8SDimitry Andric if (m_opaque_ptr) {
230f034231aSEd Maste m_opaque_ptr->Dump(&strm, false);
23114f1b3e8SDimitry Andric } else
232f034231aSEd Maste strm.PutCString("No value");
233f034231aSEd Maste
234f034231aSEd Maste return true;
235f034231aSEd Maste }
236