1f678e45dSDimitry Andric //===-- SBDeclaration.cpp ----------------------------------------*- C++-*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
10ac7ddfbfSEd Maste #include "lldb/API/SBDeclaration.h"
11ac7ddfbfSEd Maste #include "lldb/API/SBStream.h"
12f678e45dSDimitry Andric #include "lldb/Host/PosixApi.h"
13ac7ddfbfSEd Maste #include "lldb/Symbol/Declaration.h"
14f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
15f678e45dSDimitry Andric #include "lldb/Utility/Stream.h"
16ac7ddfbfSEd Maste 
17ac7ddfbfSEd Maste #include <limits.h>
18ac7ddfbfSEd Maste 
19ac7ddfbfSEd Maste using namespace lldb;
20ac7ddfbfSEd Maste using namespace lldb_private;
21ac7ddfbfSEd Maste 
SBDeclaration()22435933ddSDimitry Andric SBDeclaration::SBDeclaration() : m_opaque_ap() {}
23ac7ddfbfSEd Maste 
SBDeclaration(const SBDeclaration & rhs)24435933ddSDimitry Andric SBDeclaration::SBDeclaration(const SBDeclaration &rhs) : m_opaque_ap() {
25ac7ddfbfSEd Maste   if (rhs.IsValid())
26ac7ddfbfSEd Maste     ref() = rhs.ref();
27ac7ddfbfSEd Maste }
28ac7ddfbfSEd Maste 
SBDeclaration(const lldb_private::Declaration * lldb_object_ptr)29435933ddSDimitry Andric SBDeclaration::SBDeclaration(const lldb_private::Declaration *lldb_object_ptr)
30435933ddSDimitry Andric     : m_opaque_ap() {
31ac7ddfbfSEd Maste   if (lldb_object_ptr)
32ac7ddfbfSEd Maste     ref() = *lldb_object_ptr;
33ac7ddfbfSEd Maste }
34ac7ddfbfSEd Maste 
operator =(const SBDeclaration & rhs)35435933ddSDimitry Andric const SBDeclaration &SBDeclaration::operator=(const SBDeclaration &rhs) {
36435933ddSDimitry Andric   if (this != &rhs) {
37ac7ddfbfSEd Maste     if (rhs.IsValid())
38ac7ddfbfSEd Maste       ref() = rhs.ref();
39ac7ddfbfSEd Maste     else
40ac7ddfbfSEd Maste       m_opaque_ap.reset();
41ac7ddfbfSEd Maste   }
42ac7ddfbfSEd Maste   return *this;
43ac7ddfbfSEd Maste }
44ac7ddfbfSEd Maste 
SetDeclaration(const lldb_private::Declaration & lldb_object_ref)45435933ddSDimitry Andric void SBDeclaration::SetDeclaration(
46435933ddSDimitry Andric     const lldb_private::Declaration &lldb_object_ref) {
47ac7ddfbfSEd Maste   ref() = lldb_object_ref;
48ac7ddfbfSEd Maste }
49ac7ddfbfSEd Maste 
~SBDeclaration()50435933ddSDimitry Andric SBDeclaration::~SBDeclaration() {}
51ac7ddfbfSEd Maste 
IsValid() const52435933ddSDimitry Andric bool SBDeclaration::IsValid() const {
53ac7ddfbfSEd Maste   return m_opaque_ap.get() && m_opaque_ap->IsValid();
54ac7ddfbfSEd Maste }
55ac7ddfbfSEd Maste 
GetFileSpec() const56435933ddSDimitry Andric SBFileSpec SBDeclaration::GetFileSpec() const {
57ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
58ac7ddfbfSEd Maste 
59ac7ddfbfSEd Maste   SBFileSpec sb_file_spec;
60ac7ddfbfSEd Maste   if (m_opaque_ap.get() && m_opaque_ap->GetFile())
61ac7ddfbfSEd Maste     sb_file_spec.SetFileSpec(m_opaque_ap->GetFile());
62ac7ddfbfSEd Maste 
63435933ddSDimitry Andric   if (log) {
64ac7ddfbfSEd Maste     SBStream sstr;
65ac7ddfbfSEd Maste     sb_file_spec.GetDescription(sstr);
660127ef0fSEd Maste     log->Printf("SBLineEntry(%p)::GetFileSpec () => SBFileSpec(%p): %s",
670127ef0fSEd Maste                 static_cast<void *>(m_opaque_ap.get()),
68435933ddSDimitry Andric                 static_cast<const void *>(sb_file_spec.get()), sstr.GetData());
69ac7ddfbfSEd Maste   }
70ac7ddfbfSEd Maste 
71ac7ddfbfSEd Maste   return sb_file_spec;
72ac7ddfbfSEd Maste }
73ac7ddfbfSEd Maste 
GetLine() const74435933ddSDimitry Andric uint32_t SBDeclaration::GetLine() const {
75ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
76ac7ddfbfSEd Maste 
77ac7ddfbfSEd Maste   uint32_t line = 0;
78*b5893f02SDimitry Andric   if (m_opaque_ap)
79ac7ddfbfSEd Maste     line = m_opaque_ap->GetLine();
80ac7ddfbfSEd Maste 
81ac7ddfbfSEd Maste   if (log)
820127ef0fSEd Maste     log->Printf("SBLineEntry(%p)::GetLine () => %u",
830127ef0fSEd Maste                 static_cast<void *>(m_opaque_ap.get()), line);
84ac7ddfbfSEd Maste 
85ac7ddfbfSEd Maste   return line;
86ac7ddfbfSEd Maste }
87ac7ddfbfSEd Maste 
GetColumn() const88435933ddSDimitry Andric uint32_t SBDeclaration::GetColumn() const {
89*b5893f02SDimitry Andric   if (m_opaque_ap)
90ac7ddfbfSEd Maste     return m_opaque_ap->GetColumn();
91ac7ddfbfSEd Maste   return 0;
92ac7ddfbfSEd Maste }
93ac7ddfbfSEd Maste 
SetFileSpec(lldb::SBFileSpec filespec)94435933ddSDimitry Andric void SBDeclaration::SetFileSpec(lldb::SBFileSpec filespec) {
95ac7ddfbfSEd Maste   if (filespec.IsValid())
96ac7ddfbfSEd Maste     ref().SetFile(filespec.ref());
97ac7ddfbfSEd Maste   else
98ac7ddfbfSEd Maste     ref().SetFile(FileSpec());
99ac7ddfbfSEd Maste }
SetLine(uint32_t line)100435933ddSDimitry Andric void SBDeclaration::SetLine(uint32_t line) { ref().SetLine(line); }
101ac7ddfbfSEd Maste 
SetColumn(uint32_t column)102435933ddSDimitry Andric void SBDeclaration::SetColumn(uint32_t column) { ref().SetColumn(column); }
103ac7ddfbfSEd Maste 
operator ==(const SBDeclaration & rhs) const104435933ddSDimitry Andric bool SBDeclaration::operator==(const SBDeclaration &rhs) const {
105ac7ddfbfSEd Maste   lldb_private::Declaration *lhs_ptr = m_opaque_ap.get();
106ac7ddfbfSEd Maste   lldb_private::Declaration *rhs_ptr = rhs.m_opaque_ap.get();
107ac7ddfbfSEd Maste 
108ac7ddfbfSEd Maste   if (lhs_ptr && rhs_ptr)
109ac7ddfbfSEd Maste     return lldb_private::Declaration::Compare(*lhs_ptr, *rhs_ptr) == 0;
110ac7ddfbfSEd Maste 
111ac7ddfbfSEd Maste   return lhs_ptr == rhs_ptr;
112ac7ddfbfSEd Maste }
113ac7ddfbfSEd Maste 
operator !=(const SBDeclaration & rhs) const114435933ddSDimitry Andric bool SBDeclaration::operator!=(const SBDeclaration &rhs) const {
115ac7ddfbfSEd Maste   lldb_private::Declaration *lhs_ptr = m_opaque_ap.get();
116ac7ddfbfSEd Maste   lldb_private::Declaration *rhs_ptr = rhs.m_opaque_ap.get();
117ac7ddfbfSEd Maste 
118ac7ddfbfSEd Maste   if (lhs_ptr && rhs_ptr)
119ac7ddfbfSEd Maste     return lldb_private::Declaration::Compare(*lhs_ptr, *rhs_ptr) != 0;
120ac7ddfbfSEd Maste 
121ac7ddfbfSEd Maste   return lhs_ptr != rhs_ptr;
122ac7ddfbfSEd Maste }
123ac7ddfbfSEd Maste 
operator ->() const124435933ddSDimitry Andric const lldb_private::Declaration *SBDeclaration::operator->() const {
125ac7ddfbfSEd Maste   return m_opaque_ap.get();
126ac7ddfbfSEd Maste }
127ac7ddfbfSEd Maste 
ref()128435933ddSDimitry Andric lldb_private::Declaration &SBDeclaration::ref() {
129*b5893f02SDimitry Andric   if (m_opaque_ap == NULL)
130ac7ddfbfSEd Maste     m_opaque_ap.reset(new lldb_private::Declaration());
131ac7ddfbfSEd Maste   return *m_opaque_ap;
132ac7ddfbfSEd Maste }
133ac7ddfbfSEd Maste 
ref() const134435933ddSDimitry Andric const lldb_private::Declaration &SBDeclaration::ref() const {
135ac7ddfbfSEd Maste   return *m_opaque_ap;
136ac7ddfbfSEd Maste }
137ac7ddfbfSEd Maste 
GetDescription(SBStream & description)138435933ddSDimitry Andric bool SBDeclaration::GetDescription(SBStream &description) {
139ac7ddfbfSEd Maste   Stream &strm = description.ref();
140ac7ddfbfSEd Maste 
141*b5893f02SDimitry Andric   if (m_opaque_ap) {
142ac7ddfbfSEd Maste     char file_path[PATH_MAX * 2];
143ac7ddfbfSEd Maste     m_opaque_ap->GetFile().GetPath(file_path, sizeof(file_path));
144ac7ddfbfSEd Maste     strm.Printf("%s:%u", file_path, GetLine());
145ac7ddfbfSEd Maste     if (GetColumn() > 0)
146ac7ddfbfSEd Maste       strm.Printf(":%u", GetColumn());
147435933ddSDimitry Andric   } else
148ac7ddfbfSEd Maste     strm.PutCString("No value");
149ac7ddfbfSEd Maste 
150ac7ddfbfSEd Maste   return true;
151ac7ddfbfSEd Maste }
152ac7ddfbfSEd Maste 
get()153435933ddSDimitry Andric lldb_private::Declaration *SBDeclaration::get() { return m_opaque_ap.get(); }
154