1//===-- SBSymbolContext.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/SBSymbolContext.h"
11#include "lldb/API/SBStream.h"
12#include "lldb/Core/Log.h"
13#include "lldb/Core/Module.h"
14#include "lldb/Symbol/Function.h"
15#include "lldb/Symbol/Symbol.h"
16#include "lldb/Symbol/SymbolContext.h"
17
18using namespace lldb;
19using namespace lldb_private;
20
21
22
23SBSymbolContext::SBSymbolContext () :
24    m_opaque_ap ()
25{
26}
27
28SBSymbolContext::SBSymbolContext (const SymbolContext *sc_ptr) :
29    m_opaque_ap ()
30{
31    if (sc_ptr)
32        m_opaque_ap.reset (new SymbolContext (*sc_ptr));
33}
34
35SBSymbolContext::SBSymbolContext (const SBSymbolContext& rhs) :
36    m_opaque_ap ()
37{
38    if (rhs.IsValid())
39    {
40        if (m_opaque_ap.get())
41            *m_opaque_ap = *rhs.m_opaque_ap;
42        else
43            ref() = *rhs.m_opaque_ap;
44    }
45}
46
47SBSymbolContext::~SBSymbolContext ()
48{
49}
50
51const SBSymbolContext &
52SBSymbolContext::operator = (const SBSymbolContext &rhs)
53{
54    if (this != &rhs)
55    {
56        if (rhs.IsValid())
57            m_opaque_ap.reset (new lldb_private::SymbolContext(*rhs.m_opaque_ap.get()));
58    }
59    return *this;
60}
61
62void
63SBSymbolContext::SetSymbolContext (const SymbolContext *sc_ptr)
64{
65    if (sc_ptr)
66    {
67        if (m_opaque_ap.get())
68            *m_opaque_ap = *sc_ptr;
69        else
70            m_opaque_ap.reset (new SymbolContext (*sc_ptr));
71    }
72    else
73    {
74        if (m_opaque_ap.get())
75            m_opaque_ap->Clear(true);
76    }
77}
78
79bool
80SBSymbolContext::IsValid () const
81{
82    return m_opaque_ap.get() != NULL;
83}
84
85
86
87SBModule
88SBSymbolContext::GetModule ()
89{
90    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
91
92    SBModule sb_module;
93    ModuleSP module_sp;
94    if (m_opaque_ap.get())
95    {
96        module_sp = m_opaque_ap->module_sp;
97        sb_module.SetSP (module_sp);
98    }
99
100    if (log)
101    {
102        SBStream sstr;
103        sb_module.GetDescription (sstr);
104        log->Printf ("SBSymbolContext(%p)::GetModule () => SBModule(%p): %s",
105                     m_opaque_ap.get(), module_sp.get(), sstr.GetData());
106    }
107
108    return sb_module;
109}
110
111SBCompileUnit
112SBSymbolContext::GetCompileUnit ()
113{
114    return SBCompileUnit (m_opaque_ap.get() ? m_opaque_ap->comp_unit : NULL);
115}
116
117SBFunction
118SBSymbolContext::GetFunction ()
119{
120    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
121
122    Function *function = NULL;
123
124    if (m_opaque_ap.get())
125        function = m_opaque_ap->function;
126
127    SBFunction sb_function (function);
128
129    if (log)
130        log->Printf ("SBSymbolContext(%p)::GetFunction () => SBFunction(%p)",
131                     m_opaque_ap.get(), function);
132
133    return sb_function;
134}
135
136SBBlock
137SBSymbolContext::GetBlock ()
138{
139    return SBBlock (m_opaque_ap.get() ? m_opaque_ap->block : NULL);
140}
141
142SBLineEntry
143SBSymbolContext::GetLineEntry ()
144{
145    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
146
147    SBLineEntry sb_line_entry;
148    if (m_opaque_ap.get())
149        sb_line_entry.SetLineEntry (m_opaque_ap->line_entry);
150
151    if (log)
152    {
153        log->Printf ("SBSymbolContext(%p)::GetLineEntry () => SBLineEntry(%p)",
154                     m_opaque_ap.get(), sb_line_entry.get());
155    }
156
157    return sb_line_entry;
158}
159
160SBSymbol
161SBSymbolContext::GetSymbol ()
162{
163    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
164
165    Symbol *symbol = NULL;
166
167    if (m_opaque_ap.get())
168        symbol = m_opaque_ap->symbol;
169
170    SBSymbol sb_symbol (symbol);
171
172    if (log)
173    {
174        log->Printf ("SBSymbolContext(%p)::GetSymbol () => SBSymbol(%p)",
175                     m_opaque_ap.get(), symbol);
176    }
177
178    return sb_symbol;
179}
180
181void
182SBSymbolContext::SetModule (lldb::SBModule module)
183{
184    ref().module_sp = module.GetSP();
185}
186
187void
188SBSymbolContext::SetCompileUnit (lldb::SBCompileUnit compile_unit)
189{
190    ref().comp_unit = compile_unit.get();
191}
192
193void
194SBSymbolContext::SetFunction (lldb::SBFunction function)
195{
196    ref().function = function.get();
197}
198
199void
200SBSymbolContext::SetBlock (lldb::SBBlock block)
201{
202    ref().block = block.GetPtr();
203}
204
205void
206SBSymbolContext::SetLineEntry (lldb::SBLineEntry line_entry)
207{
208    if (line_entry.IsValid())
209        ref().line_entry = line_entry.ref();
210    else
211        ref().line_entry.Clear();
212}
213
214void
215SBSymbolContext::SetSymbol (lldb::SBSymbol symbol)
216{
217    ref().symbol = symbol.get();
218}
219
220
221lldb_private::SymbolContext*
222SBSymbolContext::operator->() const
223{
224    return m_opaque_ap.get();
225}
226
227
228const lldb_private::SymbolContext&
229SBSymbolContext::operator*() const
230{
231    assert (m_opaque_ap.get());
232    return *m_opaque_ap.get();
233}
234
235
236lldb_private::SymbolContext&
237SBSymbolContext::operator*()
238{
239    if (m_opaque_ap.get() == NULL)
240        m_opaque_ap.reset (new SymbolContext);
241    return *m_opaque_ap.get();
242}
243
244lldb_private::SymbolContext&
245SBSymbolContext::ref()
246{
247    if (m_opaque_ap.get() == NULL)
248        m_opaque_ap.reset (new SymbolContext);
249    return *m_opaque_ap.get();
250}
251
252lldb_private::SymbolContext *
253SBSymbolContext::get() const
254{
255    return m_opaque_ap.get();
256}
257
258bool
259SBSymbolContext::GetDescription (SBStream &description)
260{
261    Stream &strm = description.ref();
262
263    if (m_opaque_ap.get())
264    {
265        m_opaque_ap->GetDescription (&strm, lldb::eDescriptionLevelFull, NULL);
266    }
267    else
268        strm.PutCString ("No value");
269
270    return true;
271}
272
273SBSymbolContext
274SBSymbolContext::GetParentOfInlinedScope (const SBAddress &curr_frame_pc,
275                                          SBAddress &parent_frame_addr) const
276{
277    SBSymbolContext sb_sc;
278    if (m_opaque_ap.get() && curr_frame_pc.IsValid())
279    {
280        if (m_opaque_ap->GetParentOfInlinedScope (curr_frame_pc.ref(), sb_sc.ref(), parent_frame_addr.ref()))
281            return sb_sc;
282    }
283    return SBSymbolContext();
284}
285
286