blob: 765957d680c9c9b49cbf61134c6ffaaa0b1e4a65 [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- SBCompileUnit.cpp -------------------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/API/SBCompileUnit.h"
Jonas Devliegherebaf56642019-03-06 00:06:00 +000010#include "SBReproducerPrivate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011#include "lldb/API/SBLineEntry.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000012#include "lldb/API/SBStream.h"
Greg Claytonf02500c2013-06-18 22:51:05 +000013#include "lldb/Core/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Symbol/CompileUnit.h"
15#include "lldb/Symbol/LineEntry.h"
16#include "lldb/Symbol/LineTable.h"
Pavel Labath465eae32019-08-06 09:12:42 +000017#include "lldb/Symbol/SymbolFile.h"
Greg Claytonf02500c2013-06-18 22:51:05 +000018#include "lldb/Symbol/Type.h"
Pavel Labathf46e8972019-07-25 08:22:05 +000019#include "lldb/Symbol/TypeList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020
21using namespace lldb;
22using namespace lldb_private;
23
Konrad Kleine248a1302019-05-23 11:14:47 +000024SBCompileUnit::SBCompileUnit() : m_opaque_ptr(nullptr) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000025 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBCompileUnit);
26}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
Kate Stoneb9c1b512016-09-06 20:57:50 +000028SBCompileUnit::SBCompileUnit(lldb_private::CompileUnit *lldb_object_ptr)
29 : m_opaque_ptr(lldb_object_ptr) {}
30
31SBCompileUnit::SBCompileUnit(const SBCompileUnit &rhs)
Jonas Devliegherebaf56642019-03-06 00:06:00 +000032 : m_opaque_ptr(rhs.m_opaque_ptr) {
33 LLDB_RECORD_CONSTRUCTOR(SBCompileUnit, (const lldb::SBCompileUnit &), rhs);
34}
Kate Stoneb9c1b512016-09-06 20:57:50 +000035
36const SBCompileUnit &SBCompileUnit::operator=(const SBCompileUnit &rhs) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000037 LLDB_RECORD_METHOD(const lldb::SBCompileUnit &,
38 SBCompileUnit, operator=,(const lldb::SBCompileUnit &),
39 rhs);
40
Kate Stoneb9c1b512016-09-06 20:57:50 +000041 m_opaque_ptr = rhs.m_opaque_ptr;
Jonas Devlieghere306809f2019-04-03 21:31:22 +000042 return LLDB_RECORD_RESULT(*this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043}
44
Konrad Kleine248a1302019-05-23 11:14:47 +000045SBCompileUnit::~SBCompileUnit() { m_opaque_ptr = nullptr; }
Kate Stoneb9c1b512016-09-06 20:57:50 +000046
47SBFileSpec SBCompileUnit::GetFileSpec() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000048 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBFileSpec, SBCompileUnit,
49 GetFileSpec);
50
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 SBFileSpec file_spec;
52 if (m_opaque_ptr)
Pavel Labath38870af2019-11-28 16:22:44 +010053 file_spec.SetFileSpec(m_opaque_ptr->GetPrimaryFile());
Jonas Devliegherebaf56642019-03-06 00:06:00 +000054 return LLDB_RECORD_RESULT(file_spec);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055}
56
Kate Stoneb9c1b512016-09-06 20:57:50 +000057uint32_t SBCompileUnit::GetNumLineEntries() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000058 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBCompileUnit, GetNumLineEntries);
59
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 if (m_opaque_ptr) {
61 LineTable *line_table = m_opaque_ptr->GetLineTable();
Jason Molendab459f182019-03-06 02:32:45 +000062 if (line_table) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 return line_table->GetSize();
Jason Molendab459f182019-03-06 02:32:45 +000064 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 }
66 return 0;
Greg Claytonefabb122010-11-05 23:17:00 +000067}
68
Kate Stoneb9c1b512016-09-06 20:57:50 +000069SBLineEntry SBCompileUnit::GetLineEntryAtIndex(uint32_t idx) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000070 LLDB_RECORD_METHOD_CONST(lldb::SBLineEntry, SBCompileUnit,
71 GetLineEntryAtIndex, (uint32_t), idx);
72
Kate Stoneb9c1b512016-09-06 20:57:50 +000073 SBLineEntry sb_line_entry;
74 if (m_opaque_ptr) {
75 LineTable *line_table = m_opaque_ptr->GetLineTable();
76 if (line_table) {
77 LineEntry line_entry;
78 if (line_table->GetLineEntryAtIndex(idx, line_entry))
79 sb_line_entry.SetLineEntry(line_entry);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 }
82
Jonas Devliegherebaf56642019-03-06 00:06:00 +000083 return LLDB_RECORD_RESULT(sb_line_entry);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084}
85
Kate Stoneb9c1b512016-09-06 20:57:50 +000086uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
87 SBFileSpec *inline_file_spec) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000088 LLDB_RECORD_METHOD_CONST(uint32_t, SBCompileUnit, FindLineEntryIndex,
89 (uint32_t, uint32_t, lldb::SBFileSpec *), start_idx,
90 line, inline_file_spec);
91
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 const bool exact = true;
93 return FindLineEntryIndex(start_idx, line, inline_file_spec, exact);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094}
95
Kate Stoneb9c1b512016-09-06 20:57:50 +000096uint32_t SBCompileUnit::FindLineEntryIndex(uint32_t start_idx, uint32_t line,
97 SBFileSpec *inline_file_spec,
98 bool exact) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000099 LLDB_RECORD_METHOD_CONST(uint32_t, SBCompileUnit, FindLineEntryIndex,
100 (uint32_t, uint32_t, lldb::SBFileSpec *, bool),
101 start_idx, line, inline_file_spec, exact);
102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 uint32_t index = UINT32_MAX;
104 if (m_opaque_ptr) {
105 FileSpec file_spec;
106 if (inline_file_spec && inline_file_spec->IsValid())
107 file_spec = inline_file_spec->ref();
Caroline Ticedde9cff2010-09-20 05:20:02 +0000108 else
Pavel Labath38870af2019-11-28 16:22:44 +0100109 file_spec = m_opaque_ptr->GetPrimaryFile();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110
111 index = m_opaque_ptr->FindLineEntry(
Konrad Kleine248a1302019-05-23 11:14:47 +0000112 start_idx, line, inline_file_spec ? inline_file_spec->get() : nullptr,
113 exact, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 }
115
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 return index;
117}
118
119uint32_t SBCompileUnit::GetNumSupportFiles() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000120 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBCompileUnit, GetNumSupportFiles);
121
Pavel Labath833dba02019-05-30 08:21:25 +0000122 if (m_opaque_ptr)
123 return m_opaque_ptr->GetSupportFiles().GetSize();
124
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125 return 0;
126}
127
128lldb::SBTypeList SBCompileUnit::GetTypes(uint32_t type_mask) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000129 LLDB_RECORD_METHOD(lldb::SBTypeList, SBCompileUnit, GetTypes, (uint32_t),
130 type_mask);
131
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 SBTypeList sb_type_list;
133
Zachary Turner117b1fa2018-10-25 20:45:40 +0000134 if (!m_opaque_ptr)
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000135 return LLDB_RECORD_RESULT(sb_type_list);
Zachary Turner117b1fa2018-10-25 20:45:40 +0000136
137 ModuleSP module_sp(m_opaque_ptr->GetModule());
138 if (!module_sp)
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000139 return LLDB_RECORD_RESULT(sb_type_list);
Zachary Turner117b1fa2018-10-25 20:45:40 +0000140
Pavel Labath465eae32019-08-06 09:12:42 +0000141 SymbolFile *symfile = module_sp->GetSymbolFile();
142 if (!symfile)
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000143 return LLDB_RECORD_RESULT(sb_type_list);
Zachary Turner117b1fa2018-10-25 20:45:40 +0000144
145 TypeClass type_class = static_cast<TypeClass>(type_mask);
146 TypeList type_list;
Pavel Labath465eae32019-08-06 09:12:42 +0000147 symfile->GetTypes(m_opaque_ptr, type_class, type_list);
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000148 sb_type_list.m_opaque_up->Append(type_list);
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000149 return LLDB_RECORD_RESULT(sb_type_list);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150}
151
152SBFileSpec SBCompileUnit::GetSupportFileAtIndex(uint32_t idx) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000153 LLDB_RECORD_METHOD_CONST(lldb::SBFileSpec, SBCompileUnit,
154 GetSupportFileAtIndex, (uint32_t), idx);
155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 SBFileSpec sb_file_spec;
157 if (m_opaque_ptr) {
Pavel Labath833dba02019-05-30 08:21:25 +0000158 FileSpec spec = m_opaque_ptr->GetSupportFiles().GetFileSpecAtIndex(idx);
159 sb_file_spec.SetFileSpec(spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 }
161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000163 return LLDB_RECORD_RESULT(sb_file_spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164}
165
166uint32_t SBCompileUnit::FindSupportFileIndex(uint32_t start_idx,
167 const SBFileSpec &sb_file,
168 bool full) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000169 LLDB_RECORD_METHOD(uint32_t, SBCompileUnit, FindSupportFileIndex,
170 (uint32_t, const lldb::SBFileSpec &, bool), start_idx,
171 sb_file, full);
172
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173 if (m_opaque_ptr) {
Pavel Labath833dba02019-05-30 08:21:25 +0000174 const FileSpecList &support_files = m_opaque_ptr->GetSupportFiles();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 return support_files.FindFileIndex(start_idx, sb_file.ref(), full);
176 }
177 return 0;
178}
179
180lldb::LanguageType SBCompileUnit::GetLanguage() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000181 LLDB_RECORD_METHOD_NO_ARGS(lldb::LanguageType, SBCompileUnit, GetLanguage);
182
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183 if (m_opaque_ptr)
184 return m_opaque_ptr->GetLanguage();
185 return lldb::eLanguageTypeUnknown;
186}
187
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000188bool SBCompileUnit::IsValid() const {
189 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCompileUnit, IsValid);
Pavel Labath7f5237b2019-03-11 13:58:46 +0000190 return this->operator bool();
191}
192SBCompileUnit::operator bool() const {
193 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBCompileUnit, operator bool);
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000194
Konrad Kleine248a1302019-05-23 11:14:47 +0000195 return m_opaque_ptr != nullptr;
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000196}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197
198bool SBCompileUnit::operator==(const SBCompileUnit &rhs) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000199 LLDB_RECORD_METHOD_CONST(
200 bool, SBCompileUnit, operator==,(const lldb::SBCompileUnit &), rhs);
201
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 return m_opaque_ptr == rhs.m_opaque_ptr;
203}
204
205bool SBCompileUnit::operator!=(const SBCompileUnit &rhs) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000206 LLDB_RECORD_METHOD_CONST(
207 bool, SBCompileUnit, operator!=,(const lldb::SBCompileUnit &), rhs);
208
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 return m_opaque_ptr != rhs.m_opaque_ptr;
210}
211
212const lldb_private::CompileUnit *SBCompileUnit::operator->() const {
213 return m_opaque_ptr;
214}
215
216const lldb_private::CompileUnit &SBCompileUnit::operator*() const {
217 return *m_opaque_ptr;
218}
219
220lldb_private::CompileUnit *SBCompileUnit::get() { return m_opaque_ptr; }
221
222void SBCompileUnit::reset(lldb_private::CompileUnit *lldb_object_ptr) {
223 m_opaque_ptr = lldb_object_ptr;
224}
225
226bool SBCompileUnit::GetDescription(SBStream &description) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000227 LLDB_RECORD_METHOD(bool, SBCompileUnit, GetDescription, (lldb::SBStream &),
228 description);
229
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230 Stream &strm = description.ref();
231
232 if (m_opaque_ptr) {
233 m_opaque_ptr->Dump(&strm, false);
234 } else
235 strm.PutCString("No value");
236
237 return true;
Caroline Ticedde9cff2010-09-20 05:20:02 +0000238}
Michal Gornyae211ec2019-03-19 17:13:13 +0000239
240namespace lldb_private {
241namespace repro {
242
243template <>
244void RegisterMethods<SBCompileUnit>(Registry &R) {
245 LLDB_REGISTER_CONSTRUCTOR(SBCompileUnit, ());
246 LLDB_REGISTER_CONSTRUCTOR(SBCompileUnit, (const lldb::SBCompileUnit &));
247 LLDB_REGISTER_METHOD(
248 const lldb::SBCompileUnit &,
249 SBCompileUnit, operator=,(const lldb::SBCompileUnit &));
250 LLDB_REGISTER_METHOD_CONST(lldb::SBFileSpec, SBCompileUnit, GetFileSpec,
251 ());
252 LLDB_REGISTER_METHOD_CONST(uint32_t, SBCompileUnit, GetNumLineEntries, ());
253 LLDB_REGISTER_METHOD_CONST(lldb::SBLineEntry, SBCompileUnit,
254 GetLineEntryAtIndex, (uint32_t));
255 LLDB_REGISTER_METHOD_CONST(uint32_t, SBCompileUnit, FindLineEntryIndex,
256 (uint32_t, uint32_t, lldb::SBFileSpec *));
257 LLDB_REGISTER_METHOD_CONST(uint32_t, SBCompileUnit, FindLineEntryIndex,
258 (uint32_t, uint32_t, lldb::SBFileSpec *, bool));
259 LLDB_REGISTER_METHOD_CONST(uint32_t, SBCompileUnit, GetNumSupportFiles, ());
260 LLDB_REGISTER_METHOD(lldb::SBTypeList, SBCompileUnit, GetTypes, (uint32_t));
261 LLDB_REGISTER_METHOD_CONST(lldb::SBFileSpec, SBCompileUnit,
262 GetSupportFileAtIndex, (uint32_t));
263 LLDB_REGISTER_METHOD(uint32_t, SBCompileUnit, FindSupportFileIndex,
264 (uint32_t, const lldb::SBFileSpec &, bool));
265 LLDB_REGISTER_METHOD(lldb::LanguageType, SBCompileUnit, GetLanguage, ());
266 LLDB_REGISTER_METHOD_CONST(bool, SBCompileUnit, IsValid, ());
267 LLDB_REGISTER_METHOD_CONST(bool, SBCompileUnit, operator bool, ());
268 LLDB_REGISTER_METHOD_CONST(
269 bool, SBCompileUnit, operator==,(const lldb::SBCompileUnit &));
270 LLDB_REGISTER_METHOD_CONST(
271 bool, SBCompileUnit, operator!=,(const lldb::SBCompileUnit &));
272 LLDB_REGISTER_METHOD(bool, SBCompileUnit, GetDescription,
273 (lldb::SBStream &));
274}
275
276}
277}