1//===-- SBDeclaration.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/SBDeclaration.h"
11#include "lldb/API/SBStream.h"
12#include "lldb/Core/Log.h"
13#include "lldb/Core/Stream.h"
14#include "lldb/Symbol/Declaration.h"
15
16#include <limits.h>
17
18using namespace lldb;
19using namespace lldb_private;
20
21
22SBDeclaration::SBDeclaration () :
23    m_opaque_ap ()
24{
25}
26
27SBDeclaration::SBDeclaration (const SBDeclaration &rhs) :
28    m_opaque_ap ()
29{
30    if (rhs.IsValid())
31        ref() = rhs.ref();
32}
33
34SBDeclaration::SBDeclaration (const lldb_private::Declaration *lldb_object_ptr) :
35    m_opaque_ap ()
36{
37    if (lldb_object_ptr)
38        ref() = *lldb_object_ptr;
39}
40
41const SBDeclaration &
42SBDeclaration::operator = (const SBDeclaration &rhs)
43{
44    if (this != &rhs)
45    {
46        if (rhs.IsValid())
47            ref() = rhs.ref();
48        else
49            m_opaque_ap.reset();
50    }
51    return *this;
52}
53
54void
55SBDeclaration::SetDeclaration (const lldb_private::Declaration &lldb_object_ref)
56{
57    ref() = lldb_object_ref;
58}
59
60
61SBDeclaration::~SBDeclaration ()
62{
63}
64
65
66bool
67SBDeclaration::IsValid () const
68{
69    return m_opaque_ap.get() && m_opaque_ap->IsValid();
70}
71
72
73SBFileSpec
74SBDeclaration::GetFileSpec () const
75{
76    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
77
78    SBFileSpec sb_file_spec;
79    if (m_opaque_ap.get() && m_opaque_ap->GetFile())
80        sb_file_spec.SetFileSpec(m_opaque_ap->GetFile());
81
82    if (log)
83    {
84        SBStream sstr;
85        sb_file_spec.GetDescription (sstr);
86        log->Printf ("SBLineEntry(%p)::GetFileSpec () => SBFileSpec(%p): %s", m_opaque_ap.get(),
87                     sb_file_spec.get(), sstr.GetData());
88    }
89
90    return sb_file_spec;
91}
92
93uint32_t
94SBDeclaration::GetLine () const
95{
96    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
97
98    uint32_t line = 0;
99    if (m_opaque_ap.get())
100        line = m_opaque_ap->GetLine();
101
102    if (log)
103        log->Printf ("SBLineEntry(%p)::GetLine () => %u", m_opaque_ap.get(), line);
104
105    return line;
106}
107
108
109uint32_t
110SBDeclaration::GetColumn () const
111{
112    if (m_opaque_ap.get())
113        return m_opaque_ap->GetColumn();
114    return 0;
115}
116
117void
118SBDeclaration::SetFileSpec (lldb::SBFileSpec filespec)
119{
120    if (filespec.IsValid())
121        ref().SetFile(filespec.ref());
122    else
123        ref().SetFile(FileSpec());
124}
125void
126SBDeclaration::SetLine (uint32_t line)
127{
128    ref().SetLine(line);
129}
130
131void
132SBDeclaration::SetColumn (uint32_t column)
133{
134    ref().SetColumn(column);
135}
136
137
138
139bool
140SBDeclaration::operator == (const SBDeclaration &rhs) const
141{
142    lldb_private::Declaration *lhs_ptr = m_opaque_ap.get();
143    lldb_private::Declaration *rhs_ptr = rhs.m_opaque_ap.get();
144
145    if (lhs_ptr && rhs_ptr)
146        return lldb_private::Declaration::Compare (*lhs_ptr, *rhs_ptr) == 0;
147
148    return lhs_ptr == rhs_ptr;
149}
150
151bool
152SBDeclaration::operator != (const SBDeclaration &rhs) const
153{
154    lldb_private::Declaration *lhs_ptr = m_opaque_ap.get();
155    lldb_private::Declaration *rhs_ptr = rhs.m_opaque_ap.get();
156
157    if (lhs_ptr && rhs_ptr)
158        return lldb_private::Declaration::Compare (*lhs_ptr, *rhs_ptr) != 0;
159
160    return lhs_ptr != rhs_ptr;
161}
162
163const lldb_private::Declaration *
164SBDeclaration::operator->() const
165{
166    return m_opaque_ap.get();
167}
168
169lldb_private::Declaration &
170SBDeclaration::ref()
171{
172    if (m_opaque_ap.get() == NULL)
173        m_opaque_ap.reset (new lldb_private::Declaration ());
174    return *m_opaque_ap;
175}
176
177const lldb_private::Declaration &
178SBDeclaration::ref() const
179{
180    return *m_opaque_ap;
181}
182
183bool
184SBDeclaration::GetDescription (SBStream &description)
185{
186    Stream &strm = description.ref();
187
188    if (m_opaque_ap.get())
189    {
190        char file_path[PATH_MAX*2];
191        m_opaque_ap->GetFile().GetPath (file_path, sizeof (file_path));
192        strm.Printf ("%s:%u", file_path, GetLine());
193        if (GetColumn() > 0)
194            strm.Printf (":%u", GetColumn());
195    }
196    else
197        strm.PutCString ("No value");
198
199    return true;
200}
201
202lldb_private::Declaration *
203SBDeclaration::get ()
204{
205    return m_opaque_ap.get();
206}
207