130fdc8d8SChris Lattner //===-- SBCompileUnit.cpp ---------------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
830fdc8d8SChris Lattner 
930fdc8d8SChris Lattner #include "lldb/API/SBCompileUnit.h"
10baf5664fSJonas Devlieghere #include "SBReproducerPrivate.h"
1130fdc8d8SChris Lattner #include "lldb/API/SBLineEntry.h"
12dde9cff3SCaroline Tice #include "lldb/API/SBStream.h"
13f02500c7SGreg Clayton #include "lldb/Core/Module.h"
1430fdc8d8SChris Lattner #include "lldb/Symbol/CompileUnit.h"
1530fdc8d8SChris Lattner #include "lldb/Symbol/LineEntry.h"
1630fdc8d8SChris Lattner #include "lldb/Symbol/LineTable.h"
17*465eae36SPavel Labath #include "lldb/Symbol/SymbolFile.h"
18f02500c7SGreg Clayton #include "lldb/Symbol/Type.h"
19f46e8974SPavel Labath #include "lldb/Symbol/TypeList.h"
2030fdc8d8SChris Lattner 
2130fdc8d8SChris Lattner using namespace lldb;
2230fdc8d8SChris Lattner using namespace lldb_private;
2330fdc8d8SChris Lattner 
24248a1305SKonrad Kleine SBCompileUnit::SBCompileUnit() : m_opaque_ptr(nullptr) {
25baf5664fSJonas Devlieghere   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBCompileUnit);
26baf5664fSJonas Devlieghere }
2730fdc8d8SChris Lattner 
28b9c1b51eSKate Stone SBCompileUnit::SBCompileUnit(lldb_private::CompileUnit *lldb_object_ptr)
29b9c1b51eSKate Stone     : m_opaque_ptr(lldb_object_ptr) {}
3030fdc8d8SChris Lattner 
31b9c1b51eSKate Stone SBCompileUnit::SBCompileUnit(const SBCompileUnit &rhs)
32baf5664fSJonas Devlieghere     : m_opaque_ptr(rhs.m_opaque_ptr) {
33baf5664fSJonas Devlieghere   LLDB_RECORD_CONSTRUCTOR(SBCompileUnit, (const lldb::SBCompileUnit &), rhs);
34baf5664fSJonas Devlieghere }
3530fdc8d8SChris Lattner 
36b9c1b51eSKate Stone const SBCompileUnit &SBCompileUnit::operator=(const SBCompileUnit &rhs) {
37baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(const lldb::SBCompileUnit &,
38baf5664fSJonas Devlieghere                      SBCompileUnit, operator=,(const lldb::SBCompileUnit &),
39baf5664fSJonas Devlieghere                      rhs);
40baf5664fSJonas Devlieghere 
41efabb123SGreg Clayton   m_opaque_ptr = rhs.m_opaque_ptr;
42306809f2SJonas Devlieghere   return LLDB_RECORD_RESULT(*this);
43efabb123SGreg Clayton }
44efabb123SGreg Clayton 
45248a1305SKonrad Kleine SBCompileUnit::~SBCompileUnit() { m_opaque_ptr = nullptr; }
46efabb123SGreg Clayton 
47b9c1b51eSKate Stone SBFileSpec SBCompileUnit::GetFileSpec() const {
48baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBFileSpec, SBCompileUnit,
49baf5664fSJonas Devlieghere                                    GetFileSpec);
50baf5664fSJonas Devlieghere 
5130fdc8d8SChris Lattner   SBFileSpec file_spec;
526611103cSGreg Clayton   if (m_opaque_ptr)
536611103cSGreg Clayton     file_spec.SetFileSpec(*m_opaque_ptr);
54baf5664fSJonas Devlieghere   return LLDB_RECORD_RESULT(file_spec);
5530fdc8d8SChris Lattner }
5630fdc8d8SChris Lattner 
57b9c1b51eSKate Stone uint32_t SBCompileUnit::GetNumLineEntries() const {
58baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBCompileUnit, GetNumLineEntries);
59baf5664fSJonas Devlieghere 
60b9c1b51eSKate Stone   if (m_opaque_ptr) {
616611103cSGreg Clayton     LineTable *line_table = m_opaque_ptr->GetLineTable();
62b459f182SJason Molenda     if (line_table) {
6330fdc8d8SChris Lattner       return line_table->GetSize();
6430fdc8d8SChris Lattner     }
65b459f182SJason Molenda   }
6630fdc8d8SChris Lattner   return 0;
6730fdc8d8SChris Lattner }
6830fdc8d8SChris Lattner 
69b9c1b51eSKate Stone SBLineEntry SBCompileUnit::GetLineEntryAtIndex(uint32_t idx) const {
70baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_CONST(lldb::SBLineEntry, SBCompileUnit,
71baf5664fSJonas Devlieghere                            GetLineEntryAtIndex, (uint32_t), idx);
72baf5664fSJonas Devlieghere 
7330fdc8d8SChris Lattner   SBLineEntry sb_line_entry;
74b9c1b51eSKate Stone   if (m_opaque_ptr) {
756611103cSGreg Clayton     LineTable *line_table = m_opaque_ptr->GetLineTable();
76b9c1b51eSKate Stone     if (line_table) {
7730fdc8d8SChris Lattner       LineEntry line_entry;
7830fdc8d8SChris Lattner       if (line_table->GetLineEntryAtIndex(idx, line_entry))
7930fdc8d8SChris Lattner         sb_line_entry.SetLineEntry(line_entry);
8030fdc8d8SChris Lattner     }
8130fdc8d8SChris Lattner   }
82ceb6b139SCaroline Tice 
83baf5664fSJonas Devlieghere   return LLDB_RECORD_RESULT(sb_line_entry);
8430fdc8d8SChris Lattner }
8530fdc8d8SChris Lattner 
86b9c1b51eSKate Stone uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
87b9c1b51eSKate Stone                                            SBFileSpec *inline_file_spec) const {
88baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_CONST(uint32_t, SBCompileUnit, FindLineEntryIndex,
89baf5664fSJonas Devlieghere                            (uint32_t, uint32_t, lldb::SBFileSpec *), start_idx,
90baf5664fSJonas Devlieghere                            line, inline_file_spec);
91baf5664fSJonas Devlieghere 
9287df91b8SJim Ingham   const bool exact = true;
9387df91b8SJim Ingham   return FindLineEntryIndex(start_idx, line, inline_file_spec, exact);
9487df91b8SJim Ingham }
9587df91b8SJim Ingham 
96b9c1b51eSKate Stone uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
97b9c1b51eSKate Stone                                            SBFileSpec *inline_file_spec,
98b9c1b51eSKate Stone                                            bool exact) const {
99baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_CONST(uint32_t, SBCompileUnit, FindLineEntryIndex,
100baf5664fSJonas Devlieghere                            (uint32_t, uint32_t, lldb::SBFileSpec *, bool),
101baf5664fSJonas Devlieghere                            start_idx, line, inline_file_spec, exact);
102baf5664fSJonas Devlieghere 
103cfd1acedSGreg Clayton   uint32_t index = UINT32_MAX;
104b9c1b51eSKate Stone   if (m_opaque_ptr) {
10530fdc8d8SChris Lattner     FileSpec file_spec;
10630fdc8d8SChris Lattner     if (inline_file_spec && inline_file_spec->IsValid())
10730fdc8d8SChris Lattner       file_spec = inline_file_spec->ref();
10830fdc8d8SChris Lattner     else
1096611103cSGreg Clayton       file_spec = *m_opaque_ptr;
11030fdc8d8SChris Lattner 
111b9c1b51eSKate Stone     index = m_opaque_ptr->FindLineEntry(
112248a1305SKonrad Kleine         start_idx, line, inline_file_spec ? inline_file_spec->get() : nullptr,
113248a1305SKonrad Kleine         exact, nullptr);
11430fdc8d8SChris Lattner   }
115ceb6b139SCaroline Tice 
116cfd1acedSGreg Clayton   return index;
11730fdc8d8SChris Lattner }
11830fdc8d8SChris Lattner 
119b9c1b51eSKate Stone uint32_t SBCompileUnit::GetNumSupportFiles() const {
120baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBCompileUnit, GetNumSupportFiles);
121baf5664fSJonas Devlieghere 
122833dba01SPavel Labath   if (m_opaque_ptr)
123833dba01SPavel Labath     return m_opaque_ptr->GetSupportFiles().GetSize();
124833dba01SPavel Labath 
125873a7a4bSJohnny Chen   return 0;
126873a7a4bSJohnny Chen }
127873a7a4bSJohnny Chen 
128b9c1b51eSKate Stone lldb::SBTypeList SBCompileUnit::GetTypes(uint32_t type_mask) {
129baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(lldb::SBTypeList, SBCompileUnit, GetTypes, (uint32_t),
130baf5664fSJonas Devlieghere                      type_mask);
131baf5664fSJonas Devlieghere 
132f02500c7SGreg Clayton   SBTypeList sb_type_list;
133f02500c7SGreg Clayton 
134117b1fa1SZachary Turner   if (!m_opaque_ptr)
135baf5664fSJonas Devlieghere     return LLDB_RECORD_RESULT(sb_type_list);
136117b1fa1SZachary Turner 
137f02500c7SGreg Clayton   ModuleSP module_sp(m_opaque_ptr->GetModule());
138117b1fa1SZachary Turner   if (!module_sp)
139baf5664fSJonas Devlieghere     return LLDB_RECORD_RESULT(sb_type_list);
140117b1fa1SZachary Turner 
141*465eae36SPavel Labath   SymbolFile *symfile = module_sp->GetSymbolFile();
142*465eae36SPavel Labath   if (!symfile)
143baf5664fSJonas Devlieghere     return LLDB_RECORD_RESULT(sb_type_list);
144117b1fa1SZachary Turner 
145117b1fa1SZachary Turner   TypeClass type_class = static_cast<TypeClass>(type_mask);
146f02500c7SGreg Clayton   TypeList type_list;
147*465eae36SPavel Labath   symfile->GetTypes(m_opaque_ptr, type_class, type_list);
148d5b44036SJonas Devlieghere   sb_type_list.m_opaque_up->Append(type_list);
149baf5664fSJonas Devlieghere   return LLDB_RECORD_RESULT(sb_type_list);
150f02500c7SGreg Clayton }
151f02500c7SGreg Clayton 
152b9c1b51eSKate Stone SBFileSpec SBCompileUnit::GetSupportFileAtIndex(uint32_t idx) const {
153baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_CONST(lldb::SBFileSpec, SBCompileUnit,
154baf5664fSJonas Devlieghere                            GetSupportFileAtIndex, (uint32_t), idx);
155baf5664fSJonas Devlieghere 
156873a7a4bSJohnny Chen   SBFileSpec sb_file_spec;
157b9c1b51eSKate Stone   if (m_opaque_ptr) {
158833dba01SPavel Labath     FileSpec spec = m_opaque_ptr->GetSupportFiles().GetFileSpecAtIndex(idx);
159833dba01SPavel Labath     sb_file_spec.SetFileSpec(spec);
160873a7a4bSJohnny Chen   }
161873a7a4bSJohnny Chen 
162873a7a4bSJohnny Chen 
163baf5664fSJonas Devlieghere   return LLDB_RECORD_RESULT(sb_file_spec);
164873a7a4bSJohnny Chen }
165873a7a4bSJohnny Chen 
166b9c1b51eSKate Stone uint32_t SBCompileUnit::FindSupportFileIndex(uint32_t start_idx,
167b9c1b51eSKate Stone                                              const SBFileSpec &sb_file,
168b9c1b51eSKate Stone                                              bool full) {
169baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(uint32_t, SBCompileUnit, FindSupportFileIndex,
170baf5664fSJonas Devlieghere                      (uint32_t, const lldb::SBFileSpec &, bool), start_idx,
171baf5664fSJonas Devlieghere                      sb_file, full);
172baf5664fSJonas Devlieghere 
173b9c1b51eSKate Stone   if (m_opaque_ptr) {
174833dba01SPavel Labath     const FileSpecList &support_files = m_opaque_ptr->GetSupportFiles();
175873a7a4bSJohnny Chen     return support_files.FindFileIndex(start_idx, sb_file.ref(), full);
176873a7a4bSJohnny Chen   }
177873a7a4bSJohnny Chen   return 0;
178873a7a4bSJohnny Chen }
179873a7a4bSJohnny Chen 
180b9c1b51eSKate Stone lldb::LanguageType SBCompileUnit::GetLanguage() {
181baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_NO_ARGS(lldb::LanguageType, SBCompileUnit, GetLanguage);
182baf5664fSJonas Devlieghere 
1836cd8e0c9SEnrico Granata   if (m_opaque_ptr)
1846cd8e0c9SEnrico Granata     return m_opaque_ptr->GetLanguage();
1856cd8e0c9SEnrico Granata   return lldb::eLanguageTypeUnknown;
1866cd8e0c9SEnrico Granata }
1876cd8e0c9SEnrico Granata 
188baf5664fSJonas Devlieghere bool SBCompileUnit::IsValid() const {
189baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCompileUnit, IsValid);
1907f5237bcSPavel Labath   return this->operator bool();
1917f5237bcSPavel Labath }
1927f5237bcSPavel Labath SBCompileUnit::operator bool() const {
1937f5237bcSPavel Labath   LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCompileUnit, operator bool);
194baf5664fSJonas Devlieghere 
195248a1305SKonrad Kleine   return m_opaque_ptr != nullptr;
196baf5664fSJonas Devlieghere }
19730fdc8d8SChris Lattner 
198b9c1b51eSKate Stone bool SBCompileUnit::operator==(const SBCompileUnit &rhs) const {
199baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_CONST(
200baf5664fSJonas Devlieghere       bool, SBCompileUnit, operator==,(const lldb::SBCompileUnit &), rhs);
201baf5664fSJonas Devlieghere 
2026611103cSGreg Clayton   return m_opaque_ptr == rhs.m_opaque_ptr;
20330fdc8d8SChris Lattner }
20430fdc8d8SChris Lattner 
205b9c1b51eSKate Stone bool SBCompileUnit::operator!=(const SBCompileUnit &rhs) const {
206baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD_CONST(
207baf5664fSJonas Devlieghere       bool, SBCompileUnit, operator!=,(const lldb::SBCompileUnit &), rhs);
208baf5664fSJonas Devlieghere 
2096611103cSGreg Clayton   return m_opaque_ptr != rhs.m_opaque_ptr;
21030fdc8d8SChris Lattner }
21130fdc8d8SChris Lattner 
212b9c1b51eSKate Stone const lldb_private::CompileUnit *SBCompileUnit::operator->() const {
2136611103cSGreg Clayton   return m_opaque_ptr;
21430fdc8d8SChris Lattner }
21530fdc8d8SChris Lattner 
216b9c1b51eSKate Stone const lldb_private::CompileUnit &SBCompileUnit::operator*() const {
2176611103cSGreg Clayton   return *m_opaque_ptr;
21830fdc8d8SChris Lattner }
219dde9cff3SCaroline Tice 
220b9c1b51eSKate Stone lldb_private::CompileUnit *SBCompileUnit::get() { return m_opaque_ptr; }
22172eff18aSGreg Clayton 
222b9c1b51eSKate Stone void SBCompileUnit::reset(lldb_private::CompileUnit *lldb_object_ptr) {
22372eff18aSGreg Clayton   m_opaque_ptr = lldb_object_ptr;
22472eff18aSGreg Clayton }
22572eff18aSGreg Clayton 
226b9c1b51eSKate Stone bool SBCompileUnit::GetDescription(SBStream &description) {
227baf5664fSJonas Devlieghere   LLDB_RECORD_METHOD(bool, SBCompileUnit, GetDescription, (lldb::SBStream &),
228baf5664fSJonas Devlieghere                      description);
229baf5664fSJonas Devlieghere 
230da7bc7d0SGreg Clayton   Stream &strm = description.ref();
231da7bc7d0SGreg Clayton 
232b9c1b51eSKate Stone   if (m_opaque_ptr) {
233da7bc7d0SGreg Clayton     m_opaque_ptr->Dump(&strm, false);
234b9c1b51eSKate Stone   } else
235da7bc7d0SGreg Clayton     strm.PutCString("No value");
236dde9cff3SCaroline Tice 
237dde9cff3SCaroline Tice   return true;
238dde9cff3SCaroline Tice }
239ae211eceSMichal Gorny 
240ae211eceSMichal Gorny namespace lldb_private {
241ae211eceSMichal Gorny namespace repro {
242ae211eceSMichal Gorny 
243ae211eceSMichal Gorny template <>
244ae211eceSMichal Gorny void RegisterMethods<SBCompileUnit>(Registry &R) {
245ae211eceSMichal Gorny   LLDB_REGISTER_CONSTRUCTOR(SBCompileUnit, ());
246ae211eceSMichal Gorny   LLDB_REGISTER_CONSTRUCTOR(SBCompileUnit, (const lldb::SBCompileUnit &));
247ae211eceSMichal Gorny   LLDB_REGISTER_METHOD(
248ae211eceSMichal Gorny       const lldb::SBCompileUnit &,
249ae211eceSMichal Gorny       SBCompileUnit, operator=,(const lldb::SBCompileUnit &));
250ae211eceSMichal Gorny   LLDB_REGISTER_METHOD_CONST(lldb::SBFileSpec, SBCompileUnit, GetFileSpec,
251ae211eceSMichal Gorny                              ());
252ae211eceSMichal Gorny   LLDB_REGISTER_METHOD_CONST(uint32_t, SBCompileUnit, GetNumLineEntries, ());
253ae211eceSMichal Gorny   LLDB_REGISTER_METHOD_CONST(lldb::SBLineEntry, SBCompileUnit,
254ae211eceSMichal Gorny                              GetLineEntryAtIndex, (uint32_t));
255ae211eceSMichal Gorny   LLDB_REGISTER_METHOD_CONST(uint32_t, SBCompileUnit, FindLineEntryIndex,
256ae211eceSMichal Gorny                              (uint32_t, uint32_t, lldb::SBFileSpec *));
257ae211eceSMichal Gorny   LLDB_REGISTER_METHOD_CONST(uint32_t, SBCompileUnit, FindLineEntryIndex,
258ae211eceSMichal Gorny                              (uint32_t, uint32_t, lldb::SBFileSpec *, bool));
259ae211eceSMichal Gorny   LLDB_REGISTER_METHOD_CONST(uint32_t, SBCompileUnit, GetNumSupportFiles, ());
260ae211eceSMichal Gorny   LLDB_REGISTER_METHOD(lldb::SBTypeList, SBCompileUnit, GetTypes, (uint32_t));
261ae211eceSMichal Gorny   LLDB_REGISTER_METHOD_CONST(lldb::SBFileSpec, SBCompileUnit,
262ae211eceSMichal Gorny                              GetSupportFileAtIndex, (uint32_t));
263ae211eceSMichal Gorny   LLDB_REGISTER_METHOD(uint32_t, SBCompileUnit, FindSupportFileIndex,
264ae211eceSMichal Gorny                        (uint32_t, const lldb::SBFileSpec &, bool));
265ae211eceSMichal Gorny   LLDB_REGISTER_METHOD(lldb::LanguageType, SBCompileUnit, GetLanguage, ());
266ae211eceSMichal Gorny   LLDB_REGISTER_METHOD_CONST(bool, SBCompileUnit, IsValid, ());
267ae211eceSMichal Gorny   LLDB_REGISTER_METHOD_CONST(bool, SBCompileUnit, operator bool, ());
268ae211eceSMichal Gorny   LLDB_REGISTER_METHOD_CONST(
269ae211eceSMichal Gorny       bool, SBCompileUnit, operator==,(const lldb::SBCompileUnit &));
270ae211eceSMichal Gorny   LLDB_REGISTER_METHOD_CONST(
271ae211eceSMichal Gorny       bool, SBCompileUnit, operator!=,(const lldb::SBCompileUnit &));
272ae211eceSMichal Gorny   LLDB_REGISTER_METHOD(bool, SBCompileUnit, GetDescription,
273ae211eceSMichal Gorny                        (lldb::SBStream &));
274ae211eceSMichal Gorny }
275ae211eceSMichal Gorny 
276ae211eceSMichal Gorny }
277ae211eceSMichal Gorny }
278