1226cce25SGreg Clayton //===-- SBModuleSpec.cpp ----------------------------------------*- C++ -*-===//
2226cce25SGreg Clayton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6226cce25SGreg Clayton //
7226cce25SGreg Clayton //===----------------------------------------------------------------------===//
8226cce25SGreg Clayton 
9226cce25SGreg Clayton #include "lldb/API/SBModuleSpec.h"
10*bd4bf82aSJonas Devlieghere #include "Utils.h"
11226cce25SGreg Clayton #include "lldb/API/SBStream.h"
12226cce25SGreg Clayton #include "lldb/Core/Module.h"
13226cce25SGreg Clayton #include "lldb/Core/ModuleSpec.h"
14226cce25SGreg Clayton #include "lldb/Host/Host.h"
15226cce25SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
16bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
17226cce25SGreg Clayton 
18226cce25SGreg Clayton using namespace lldb;
19226cce25SGreg Clayton using namespace lldb_private;
20226cce25SGreg Clayton 
21d5b44036SJonas Devlieghere SBModuleSpec::SBModuleSpec() : m_opaque_up(new lldb_private::ModuleSpec()) {}
22226cce25SGreg Clayton 
23*bd4bf82aSJonas Devlieghere SBModuleSpec::SBModuleSpec(const SBModuleSpec &rhs) : m_opaque_up() {
24*bd4bf82aSJonas Devlieghere   m_opaque_up = clone(rhs.m_opaque_up);
25*bd4bf82aSJonas Devlieghere }
26226cce25SGreg Clayton 
27b9c1b51eSKate Stone const SBModuleSpec &SBModuleSpec::operator=(const SBModuleSpec &rhs) {
28226cce25SGreg Clayton   if (this != &rhs)
29*bd4bf82aSJonas Devlieghere     m_opaque_up = clone(rhs.m_opaque_up);
30226cce25SGreg Clayton   return *this;
31226cce25SGreg Clayton }
32226cce25SGreg Clayton 
33b9c1b51eSKate Stone SBModuleSpec::~SBModuleSpec() {}
34226cce25SGreg Clayton 
35d5b44036SJonas Devlieghere bool SBModuleSpec::IsValid() const { return m_opaque_up->operator bool(); }
36226cce25SGreg Clayton 
37d5b44036SJonas Devlieghere void SBModuleSpec::Clear() { m_opaque_up->Clear(); }
38226cce25SGreg Clayton 
39b9c1b51eSKate Stone SBFileSpec SBModuleSpec::GetFileSpec() {
40d5b44036SJonas Devlieghere   SBFileSpec sb_spec(m_opaque_up->GetFileSpec());
41226cce25SGreg Clayton   return sb_spec;
42226cce25SGreg Clayton }
43226cce25SGreg Clayton 
44b9c1b51eSKate Stone void SBModuleSpec::SetFileSpec(const lldb::SBFileSpec &sb_spec) {
45d5b44036SJonas Devlieghere   m_opaque_up->GetFileSpec() = *sb_spec;
46226cce25SGreg Clayton }
47226cce25SGreg Clayton 
48b9c1b51eSKate Stone lldb::SBFileSpec SBModuleSpec::GetPlatformFileSpec() {
49d5b44036SJonas Devlieghere   return SBFileSpec(m_opaque_up->GetPlatformFileSpec());
50226cce25SGreg Clayton }
51226cce25SGreg Clayton 
52b9c1b51eSKate Stone void SBModuleSpec::SetPlatformFileSpec(const lldb::SBFileSpec &sb_spec) {
53d5b44036SJonas Devlieghere   m_opaque_up->GetPlatformFileSpec() = *sb_spec;
54226cce25SGreg Clayton }
55226cce25SGreg Clayton 
56b9c1b51eSKate Stone lldb::SBFileSpec SBModuleSpec::GetSymbolFileSpec() {
57d5b44036SJonas Devlieghere   return SBFileSpec(m_opaque_up->GetSymbolFileSpec());
58226cce25SGreg Clayton }
59226cce25SGreg Clayton 
60b9c1b51eSKate Stone void SBModuleSpec::SetSymbolFileSpec(const lldb::SBFileSpec &sb_spec) {
61d5b44036SJonas Devlieghere   m_opaque_up->GetSymbolFileSpec() = *sb_spec;
62226cce25SGreg Clayton }
63226cce25SGreg Clayton 
64b9c1b51eSKate Stone const char *SBModuleSpec::GetObjectName() {
65d5b44036SJonas Devlieghere   return m_opaque_up->GetObjectName().GetCString();
66226cce25SGreg Clayton }
67226cce25SGreg Clayton 
68b9c1b51eSKate Stone void SBModuleSpec::SetObjectName(const char *name) {
69d5b44036SJonas Devlieghere   m_opaque_up->GetObjectName().SetCString(name);
70226cce25SGreg Clayton }
71226cce25SGreg Clayton 
72b9c1b51eSKate Stone const char *SBModuleSpec::GetTriple() {
73d5b44036SJonas Devlieghere   std::string triple(m_opaque_up->GetArchitecture().GetTriple().str());
7405097246SAdrian Prantl   // Unique the string so we don't run into ownership issues since the const
7505097246SAdrian Prantl   // strings put the string into the string pool once and the strings never
7605097246SAdrian Prantl   // comes out
77226cce25SGreg Clayton   ConstString const_triple(triple.c_str());
78226cce25SGreg Clayton   return const_triple.GetCString();
79226cce25SGreg Clayton }
80226cce25SGreg Clayton 
81b9c1b51eSKate Stone void SBModuleSpec::SetTriple(const char *triple) {
82d5b44036SJonas Devlieghere   m_opaque_up->GetArchitecture().SetTriple(triple);
83226cce25SGreg Clayton }
84226cce25SGreg Clayton 
85b9c1b51eSKate Stone const uint8_t *SBModuleSpec::GetUUIDBytes() {
86d5b44036SJonas Devlieghere   return m_opaque_up->GetUUID().GetBytes().data();
87226cce25SGreg Clayton }
88226cce25SGreg Clayton 
89b9c1b51eSKate Stone size_t SBModuleSpec::GetUUIDLength() {
90d5b44036SJonas Devlieghere   return m_opaque_up->GetUUID().GetBytes().size();
91226cce25SGreg Clayton }
92226cce25SGreg Clayton 
93b9c1b51eSKate Stone bool SBModuleSpec::SetUUIDBytes(const uint8_t *uuid, size_t uuid_len) {
94d5b44036SJonas Devlieghere   m_opaque_up->GetUUID() = UUID::fromOptionalData(uuid, uuid_len);
95d5b44036SJonas Devlieghere   return m_opaque_up->GetUUID().IsValid();
96226cce25SGreg Clayton }
97226cce25SGreg Clayton 
98b9c1b51eSKate Stone bool SBModuleSpec::GetDescription(lldb::SBStream &description) {
99d5b44036SJonas Devlieghere   m_opaque_up->Dump(description.ref());
100226cce25SGreg Clayton   return true;
101226cce25SGreg Clayton }
102226cce25SGreg Clayton 
103d5b44036SJonas Devlieghere SBModuleSpecList::SBModuleSpecList() : m_opaque_up(new ModuleSpecList()) {}
104226cce25SGreg Clayton 
105b9c1b51eSKate Stone SBModuleSpecList::SBModuleSpecList(const SBModuleSpecList &rhs)
106d5b44036SJonas Devlieghere     : m_opaque_up(new ModuleSpecList(*rhs.m_opaque_up)) {}
107226cce25SGreg Clayton 
108b9c1b51eSKate Stone SBModuleSpecList &SBModuleSpecList::operator=(const SBModuleSpecList &rhs) {
109226cce25SGreg Clayton   if (this != &rhs)
110d5b44036SJonas Devlieghere     *m_opaque_up = *rhs.m_opaque_up;
111226cce25SGreg Clayton   return *this;
112226cce25SGreg Clayton }
113226cce25SGreg Clayton 
114b9c1b51eSKate Stone SBModuleSpecList::~SBModuleSpecList() {}
115226cce25SGreg Clayton 
116b9c1b51eSKate Stone SBModuleSpecList SBModuleSpecList::GetModuleSpecifications(const char *path) {
117226cce25SGreg Clayton   SBModuleSpecList specs;
1188f3be7a3SJonas Devlieghere   FileSpec file_spec(path);
1198f3be7a3SJonas Devlieghere   FileSystem::Instance().Resolve(file_spec);
120226cce25SGreg Clayton   Host::ResolveExecutableInBundle(file_spec);
121d5b44036SJonas Devlieghere   ObjectFile::GetModuleSpecifications(file_spec, 0, 0, *specs.m_opaque_up);
122226cce25SGreg Clayton   return specs;
123226cce25SGreg Clayton }
124226cce25SGreg Clayton 
125b9c1b51eSKate Stone void SBModuleSpecList::Append(const SBModuleSpec &spec) {
126d5b44036SJonas Devlieghere   m_opaque_up->Append(*spec.m_opaque_up);
127226cce25SGreg Clayton }
128226cce25SGreg Clayton 
129b9c1b51eSKate Stone void SBModuleSpecList::Append(const SBModuleSpecList &spec_list) {
130d5b44036SJonas Devlieghere   m_opaque_up->Append(*spec_list.m_opaque_up);
131226cce25SGreg Clayton }
132226cce25SGreg Clayton 
133d5b44036SJonas Devlieghere size_t SBModuleSpecList::GetSize() { return m_opaque_up->GetSize(); }
134226cce25SGreg Clayton 
135b9c1b51eSKate Stone SBModuleSpec SBModuleSpecList::GetSpecAtIndex(size_t i) {
136226cce25SGreg Clayton   SBModuleSpec sb_module_spec;
137d5b44036SJonas Devlieghere   m_opaque_up->GetModuleSpecAtIndex(i, *sb_module_spec.m_opaque_up);
138226cce25SGreg Clayton   return sb_module_spec;
139226cce25SGreg Clayton }
140226cce25SGreg Clayton 
141226cce25SGreg Clayton SBModuleSpec
142b9c1b51eSKate Stone SBModuleSpecList::FindFirstMatchingSpec(const SBModuleSpec &match_spec) {
143226cce25SGreg Clayton   SBModuleSpec sb_module_spec;
144d5b44036SJonas Devlieghere   m_opaque_up->FindMatchingModuleSpec(*match_spec.m_opaque_up,
145d5b44036SJonas Devlieghere                                       *sb_module_spec.m_opaque_up);
146226cce25SGreg Clayton   return sb_module_spec;
147226cce25SGreg Clayton }
148226cce25SGreg Clayton 
149226cce25SGreg Clayton SBModuleSpecList
150b9c1b51eSKate Stone SBModuleSpecList::FindMatchingSpecs(const SBModuleSpec &match_spec) {
151226cce25SGreg Clayton   SBModuleSpecList specs;
152d5b44036SJonas Devlieghere   m_opaque_up->FindMatchingModuleSpecs(*match_spec.m_opaque_up,
153d5b44036SJonas Devlieghere                                        *specs.m_opaque_up);
154226cce25SGreg Clayton   return specs;
155226cce25SGreg Clayton }
156226cce25SGreg Clayton 
157b9c1b51eSKate Stone bool SBModuleSpecList::GetDescription(lldb::SBStream &description) {
158d5b44036SJonas Devlieghere   m_opaque_up->Dump(description.ref());
159226cce25SGreg Clayton   return true;
160226cce25SGreg Clayton }
161