1//===-- SBCompileUnit.cpp ---------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/API/SBCompileUnit.h"
11#include "lldb/API/SBLineEntry.h"
12#include "lldb/API/SBStream.h"
13#include "lldb/Core/Log.h"
14#include "lldb/Core/Module.h"
15#include "lldb/Symbol/CompileUnit.h"
16#include "lldb/Symbol/LineEntry.h"
17#include "lldb/Symbol/LineTable.h"
18#include "lldb/Symbol/SymbolVendor.h"
19#include "lldb/Symbol/Type.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
24
25SBCompileUnit::SBCompileUnit () :
26    m_opaque_ptr (NULL)
27{
28}
29
30SBCompileUnit::SBCompileUnit (lldb_private::CompileUnit *lldb_object_ptr) :
31    m_opaque_ptr (lldb_object_ptr)
32{
33}
34
35SBCompileUnit::SBCompileUnit(const SBCompileUnit &rhs) :
36    m_opaque_ptr (rhs.m_opaque_ptr)
37{
38}
39
40const SBCompileUnit &
41SBCompileUnit::operator = (const SBCompileUnit &rhs)
42{
43    m_opaque_ptr = rhs.m_opaque_ptr;
44    return *this;
45}
46
47
48SBCompileUnit::~SBCompileUnit ()
49{
50    m_opaque_ptr = NULL;
51}
52
53SBFileSpec
54SBCompileUnit::GetFileSpec () const
55{
56    SBFileSpec file_spec;
57    if (m_opaque_ptr)
58        file_spec.SetFileSpec(*m_opaque_ptr);
59    return file_spec;
60}
61
62uint32_t
63SBCompileUnit::GetNumLineEntries () const
64{
65    if (m_opaque_ptr)
66    {
67        LineTable *line_table = m_opaque_ptr->GetLineTable ();
68        if (line_table)
69            return line_table->GetSize();
70    }
71    return 0;
72}
73
74SBLineEntry
75SBCompileUnit::GetLineEntryAtIndex (uint32_t idx) const
76{
77    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
78
79    SBLineEntry sb_line_entry;
80    if (m_opaque_ptr)
81    {
82        LineTable *line_table = m_opaque_ptr->GetLineTable ();
83        if (line_table)
84        {
85            LineEntry line_entry;
86            if (line_table->GetLineEntryAtIndex(idx, line_entry))
87                sb_line_entry.SetLineEntry(line_entry);
88        }
89    }
90
91    if (log)
92    {
93        SBStream sstr;
94        sb_line_entry.GetDescription (sstr);
95        log->Printf ("SBCompileUnit(%p)::GetLineEntryAtIndex (idx=%u) => SBLineEntry(%p): '%s'",
96                     m_opaque_ptr, idx, sb_line_entry.get(), sstr.GetData());
97    }
98
99    return sb_line_entry;
100}
101
102uint32_t
103SBCompileUnit::FindLineEntryIndex (uint32_t start_idx, uint32_t line, SBFileSpec *inline_file_spec) const
104{
105    const bool exact = true;
106    return FindLineEntryIndex (start_idx, line, inline_file_spec, exact);
107}
108
109uint32_t
110SBCompileUnit::FindLineEntryIndex (uint32_t start_idx, uint32_t line, SBFileSpec *inline_file_spec, bool exact) const
111{
112    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
113
114    uint32_t index = UINT32_MAX;
115    if (m_opaque_ptr)
116    {
117        FileSpec file_spec;
118        if (inline_file_spec && inline_file_spec->IsValid())
119            file_spec = inline_file_spec->ref();
120        else
121            file_spec = *m_opaque_ptr;
122
123
124        index = m_opaque_ptr->FindLineEntry (start_idx,
125                                             line,
126                                             inline_file_spec ? inline_file_spec->get() : NULL,
127                                             exact,
128                                             NULL);
129    }
130
131    if (log)
132    {
133        SBStream sstr;
134        if (index == UINT32_MAX)
135        {
136            log->Printf ("SBCompileUnit(%p)::FindLineEntryIndex (start_idx=%u, line=%u, SBFileSpec(%p)) => NOT FOUND",
137                         m_opaque_ptr, start_idx, line, inline_file_spec ? inline_file_spec->get() : NULL);
138        }
139        else
140        {
141            log->Printf ("SBCompileUnit(%p)::FindLineEntryIndex (start_idx=%u, line=%u, SBFileSpec(%p)) => %u",
142                         m_opaque_ptr, start_idx, line, inline_file_spec ? inline_file_spec->get() : NULL, index);
143        }
144    }
145
146    return index;
147}
148
149uint32_t
150SBCompileUnit::GetNumSupportFiles () const
151{
152    if (m_opaque_ptr)
153    {
154        FileSpecList& support_files = m_opaque_ptr->GetSupportFiles ();
155        return support_files.GetSize();
156    }
157    return 0;
158}
159
160
161
162lldb::SBTypeList
163SBCompileUnit::GetTypes (uint32_t type_mask)
164{
165    SBTypeList sb_type_list;
166
167    if (m_opaque_ptr)
168    {
169        ModuleSP module_sp (m_opaque_ptr->GetModule());
170        if (module_sp)
171        {
172            SymbolVendor* vendor = module_sp->GetSymbolVendor();
173            if (vendor)
174            {
175                TypeList type_list;
176                vendor->GetTypes (m_opaque_ptr, type_mask, type_list);
177                sb_type_list.m_opaque_ap->Append(type_list);
178            }
179        }
180    }
181    return sb_type_list;
182}
183
184
185
186
187SBFileSpec
188SBCompileUnit::GetSupportFileAtIndex (uint32_t idx) const
189{
190    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
191
192    SBFileSpec sb_file_spec;
193    if (m_opaque_ptr)
194    {
195        FileSpecList &support_files = m_opaque_ptr->GetSupportFiles ();
196        FileSpec file_spec = support_files.GetFileSpecAtIndex(idx);
197        sb_file_spec.SetFileSpec(file_spec);
198    }
199
200    if (log)
201    {
202        SBStream sstr;
203        sb_file_spec.GetDescription (sstr);
204        log->Printf ("SBCompileUnit(%p)::GetGetFileSpecAtIndex (idx=%u) => SBFileSpec(%p): '%s'",
205                     m_opaque_ptr, idx, sb_file_spec.get(), sstr.GetData());
206    }
207
208    return sb_file_spec;
209}
210
211uint32_t
212SBCompileUnit::FindSupportFileIndex (uint32_t start_idx, const SBFileSpec &sb_file, bool full)
213{
214    if (m_opaque_ptr)
215    {
216	FileSpecList &support_files = m_opaque_ptr->GetSupportFiles ();
217	return support_files.FindFileIndex(start_idx, sb_file.ref(), full);
218    }
219    return 0;
220}
221
222bool
223SBCompileUnit::IsValid () const
224{
225    return m_opaque_ptr != NULL;
226}
227
228bool
229SBCompileUnit::operator == (const SBCompileUnit &rhs) const
230{
231    return m_opaque_ptr == rhs.m_opaque_ptr;
232}
233
234bool
235SBCompileUnit::operator != (const SBCompileUnit &rhs) const
236{
237    return m_opaque_ptr != rhs.m_opaque_ptr;
238}
239
240const lldb_private::CompileUnit *
241SBCompileUnit::operator->() const
242{
243    return m_opaque_ptr;
244}
245
246const lldb_private::CompileUnit &
247SBCompileUnit::operator*() const
248{
249    return *m_opaque_ptr;
250}
251
252lldb_private::CompileUnit *
253SBCompileUnit::get ()
254{
255    return m_opaque_ptr;
256}
257
258void
259SBCompileUnit::reset (lldb_private::CompileUnit *lldb_object_ptr)
260{
261    m_opaque_ptr = lldb_object_ptr;
262}
263
264
265bool
266SBCompileUnit::GetDescription (SBStream &description)
267{
268    Stream &strm = description.ref();
269
270    if (m_opaque_ptr)
271    {
272        m_opaque_ptr->Dump (&strm, false);
273    }
274    else
275        strm.PutCString ("No value");
276
277    return true;
278}
279