LLDB mainline
TypeSystem.cpp
Go to the documentation of this file.
1//===-- TypeSystem.cpp ----------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
14
15#include "llvm/ADT/DenseSet.h"
16#include <optional>
17
18using namespace lldb_private;
19using namespace lldb;
20
21/// A 64-bit SmallBitVector is only small up to 64-7 bits, and the
22/// setBitsInMask interface wants to write full bytes.
23static const size_t g_num_small_bitvector_bits = 64 - 8;
25 "Languages bit vector is no longer small on 64 bit systems");
27
28std::optional<LanguageType> LanguageSet::GetSingularLanguage() {
29 if (bitvector.count() == 1)
30 return (LanguageType)bitvector.find_first();
31 return {};
32}
33
34void LanguageSet::Insert(LanguageType language) { bitvector.set(language); }
35size_t LanguageSet::Size() const { return bitvector.count(); }
36bool LanguageSet::Empty() const { return bitvector.none(); }
37bool LanguageSet::operator[](unsigned i) const { return bitvector[i]; }
38
39TypeSystem::TypeSystem() = default;
40TypeSystem::~TypeSystem() = default;
41
43 Module *module, Target *target) {
44 uint32_t i = 0;
45 TypeSystemCreateInstance create_callback;
47 i++)) != nullptr) {
48 if (auto type_system_sp = create_callback(language, module, target))
49 return type_system_sp;
50 }
51
52 return {};
53}
54
56 Module *module) {
57 return CreateInstanceHelper(language, module, nullptr);
58}
59
61 Target *target) {
62 return CreateInstanceHelper(language, nullptr, target);
63}
64
65#ifndef NDEBUG
67#endif
68
70 return false;
71}
72
74 uint64_t size) {
75 return CompilerType();
76}
77
80 return CompilerType();
81}
82
85 return CompilerType();
86}
87
89 return CompilerType();
90}
91
93 return CompilerType();
94}
95
97 uint32_t payload) {
98 return CompilerType();
99}
100
103 return CompilerType();
104}
105
108 return CompilerType();
109}
110
112 const char *name,
113 const CompilerDeclContext &decl_ctx,
114 uint32_t opaque_payload) {
115 return CompilerType();
116}
117
119 return CompilerType();
120}
121
123 return CompilerType(weak_from_this(), type);
124}
125
127 return false;
128}
129
131 bool expand_pack) {
132 return 0;
133}
134
137 bool expand_pack) {
139}
140
142 size_t idx, bool expand_pack) {
143 return CompilerType();
144}
145
146std::optional<CompilerType::IntegralTemplateArgument>
148 bool expand_pack) {
149 return std::nullopt;
150}
151
153 return eLazyBoolCalculate;
154}
155
157 return false;
158}
159
161 return GetTypeName(type, false);
162}
163
165 return ConstString();
166}
167
169 return CompilerDeclContext();
170}
171
173 return CompilerType();
174}
175
176size_t TypeSystem::DeclGetFunctionNumArguments(void *opaque_decl) { return 0; }
177
179 size_t arg_idx) {
180 return CompilerType();
181}
182
183std::vector<lldb_private::CompilerContext>
185 return {};
186}
187
188std::vector<lldb_private::CompilerContext>
190 return {};
191}
192
193std::vector<CompilerDecl>
195 bool ignore_imported_decls) {
196 return std::vector<CompilerDecl>();
197}
198
199std::unique_ptr<UtilityFunction>
200TypeSystem::CreateUtilityFunction(std::string text, std::string name) {
201 return {};
202}
203
204std::optional<llvm::json::Value> TypeSystem::ReportStatistics() {
205 return std::nullopt;
206}
207
210 return CompilerDeclContext();
211}
212
213#pragma mark TypeSystemMap
214
215TypeSystemMap::TypeSystemMap() : m_mutex(), m_map() {}
216
218
220 collection map;
221 {
222 std::lock_guard<std::mutex> guard(m_mutex);
223 map = m_map;
224 m_clear_in_progress = true;
225 }
226 llvm::DenseSet<TypeSystem *> visited;
227 for (auto &pair : map) {
228 if (visited.count(pair.second.get()))
229 continue;
230 visited.insert(pair.second.get());
231 if (lldb::TypeSystemSP type_system = pair.second)
232 type_system->Finalize();
233 }
234 map.clear();
235 {
236 std::lock_guard<std::mutex> guard(m_mutex);
237 m_map.clear();
238 m_clear_in_progress = false;
239 }
240}
241
243 std::function<bool(lldb::TypeSystemSP)> const &callback) {
244
245 // The callback may call into this function again causing
246 // us to lock m_mutex twice if we held it across the callback.
247 // Since we just care about guarding access to 'm_map', make
248 // a local copy and iterate over that instead.
249 collection map_snapshot;
250 {
251 std::lock_guard<std::mutex> guard(m_mutex);
252 map_snapshot = m_map;
253 }
254
255 // Use a std::set so we only call the callback once for each unique
256 // TypeSystem instance.
257 llvm::DenseSet<TypeSystem *> visited;
258 for (auto &pair : map_snapshot) {
259 TypeSystem *type_system = pair.second.get();
260 if (!type_system || visited.count(type_system))
261 continue;
262 visited.insert(type_system);
263 assert(type_system);
264 if (!callback(pair.second))
265 break;
266 }
267}
268
269llvm::Expected<lldb::TypeSystemSP> TypeSystemMap::GetTypeSystemForLanguage(
270 lldb::LanguageType language,
271 std::optional<CreateCallback> create_callback) {
272 std::lock_guard<std::mutex> guard(m_mutex);
274 return llvm::createStringError(
275 "Unable to get TypeSystem because TypeSystemMap is being cleared");
276
277 collection::iterator pos = m_map.find(language);
278 if (pos != m_map.end()) {
279 if (pos->second) {
280 assert(!pos->second->weak_from_this().expired());
281 return pos->second;
282 }
283 return llvm::createStringError(
284 "TypeSystem for language " +
285 llvm::StringRef(Language::GetNameForLanguageType(language)) +
286 " doesn't exist");
287 }
288
289 for (const auto &pair : m_map) {
290 if (pair.second && pair.second->SupportsLanguage(language)) {
291 // Add a new mapping for "language" to point to an already existing
292 // TypeSystem that supports this language
293 m_map[language] = pair.second;
294 if (pair.second)
295 return pair.second;
296 return llvm::createStringError(
297 "TypeSystem for language " +
298 llvm::StringRef(Language::GetNameForLanguageType(language)) +
299 " doesn't exist");
300 }
301 }
302
303 if (!create_callback)
304 return llvm::createStringError(
305 "Unable to find type system for language " +
306 llvm::StringRef(Language::GetNameForLanguageType(language)));
307 // Cache even if we get a shared pointer that contains a null type system
308 // back.
309 TypeSystemSP type_system_sp = (*create_callback)();
310 m_map[language] = type_system_sp;
311 if (type_system_sp)
312 return type_system_sp;
313 return llvm::createStringError(
314 "TypeSystem for language " +
315 llvm::StringRef(Language::GetNameForLanguageType(language)) +
316 " doesn't exist");
317}
318
319llvm::Expected<lldb::TypeSystemSP>
321 Module *module, bool can_create) {
322 if (can_create) {
324 language, std::optional<CreateCallback>([language, module]() {
325 return TypeSystem::CreateInstance(language, module);
326 }));
327 }
328 return GetTypeSystemForLanguage(language);
329}
330
331llvm::Expected<lldb::TypeSystemSP>
333 Target *target, bool can_create) {
334 if (can_create) {
336 language, std::optional<CreateCallback>([language, target]() {
337 return TypeSystem::CreateInstance(language, target);
338 }));
339 }
340 return GetTypeSystemForLanguage(language);
341}
342
344 if (language == eLanguageTypeUnknown || language >= eNumLanguageTypes)
345 return false;
346
347 LanguageSet languages =
349 if (languages.Empty())
350 return false;
351 return languages[language];
352}
static const size_t g_num_small_bitvector_bits
A 64-bit SmallBitVector is only small up to 64-7 bits, and the setBitsInMask interface wants to write...
Definition: TypeSystem.cpp:23
static TypeSystemSP CreateInstanceHelper(lldb::LanguageType language, Module *module, Target *target)
Definition: TypeSystem.cpp:42
Represents a generic declaration context in a program.
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
A uniqued constant string class.
Definition: ConstString.h:40
static const char * GetNameForLanguageType(lldb::LanguageType language)
Definition: Language.cpp:266
A class that describes an executable image and its associated object and symbol files.
Definition: Module.h:89
static TypeSystemCreateInstance GetTypeSystemCreateCallbackAtIndex(uint32_t idx)
static LanguageSet GetAllTypeSystemSupportedLanguagesForTypes()
llvm::DenseMap< uint16_t, lldb::TypeSystemSP > collection
Definition: TypeSystem.h:577
std::mutex m_mutex
A mutex to keep this object happy in multi-threaded environments.
Definition: TypeSystem.h:578
llvm::Expected< lldb::TypeSystemSP > GetTypeSystemForLanguage(lldb::LanguageType language, Module *module, bool can_create)
Definition: TypeSystem.cpp:320
void ForEach(std::function< bool(lldb::TypeSystemSP)> const &callback)
Definition: TypeSystem.cpp:242
Interface for representing a type system.
Definition: TypeSystem.h:70
virtual std::unique_ptr< UtilityFunction > CreateUtilityFunction(std::string text, std::string name)
Definition: TypeSystem.cpp:200
virtual bool IsMeaninglessWithoutDynamicResolution(void *type)
Definition: TypeSystem.cpp:156
virtual size_t GetNumTemplateArguments(lldb::opaque_compiler_type_t type, bool expand_pack)
Definition: TypeSystem.cpp:130
virtual CompilerType GetBuiltinTypeByName(ConstString name)
Definition: TypeSystem.cpp:118
virtual CompilerType GetTypeTemplateArgument(lldb::opaque_compiler_type_t type, size_t idx, bool expand_pack)
Definition: TypeSystem.cpp:141
virtual CompilerType GetTypeForFormatters(void *type)
Definition: TypeSystem.cpp:122
virtual CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type)
Definition: TypeSystem.cpp:107
virtual CompilerType GetAtomicType(lldb::opaque_compiler_type_t type)
Definition: TypeSystem.cpp:88
virtual ConstString GetMangledTypeName(lldb::opaque_compiler_type_t type)
Defaults to GetTypeName(type).
Definition: TypeSystem.cpp:160
virtual CompilerType GetLValueReferenceType(lldb::opaque_compiler_type_t type)
Definition: TypeSystem.cpp:79
virtual std::vector< CompilerDecl > DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name, const bool ignore_imported_decls)
Definition: TypeSystem.cpp:194
virtual ConstString GetTypeName(lldb::opaque_compiler_type_t type, bool BaseOnly)=0
virtual std::optional< llvm::json::Value > ReportStatistics()
Definition: TypeSystem.cpp:204
virtual CompilerType CreateTypedef(lldb::opaque_compiler_type_t type, const char *name, const CompilerDeclContext &decl_ctx, uint32_t opaque_payload)
Definition: TypeSystem.cpp:111
virtual LazyBool ShouldPrintAsOneLiner(void *type, ValueObject *valobj)
Definition: TypeSystem.cpp:152
virtual CompilerType AddConstModifier(lldb::opaque_compiler_type_t type)
Definition: TypeSystem.cpp:92
virtual CompilerType AddPtrAuthModifier(lldb::opaque_compiler_type_t type, uint32_t payload)
Definition: TypeSystem.cpp:96
virtual CompilerType DeclGetFunctionReturnType(void *opaque_decl)
Definition: TypeSystem.cpp:172
virtual CompilerType DeclGetFunctionArgumentType(void *opaque_decl, size_t arg_idx)
Definition: TypeSystem.cpp:178
virtual CompilerType GetRValueReferenceType(lldb::opaque_compiler_type_t type)
Definition: TypeSystem.cpp:84
virtual std::vector< lldb_private::CompilerContext > DeclGetCompilerContext(void *opaque_decl)
Definition: TypeSystem.cpp:184
virtual std::vector< lldb_private::CompilerContext > DeclContextGetCompilerContext(void *opaque_decl_ctx)
Definition: TypeSystem.cpp:189
virtual bool IsAnonymousType(lldb::opaque_compiler_type_t type)
Definition: TypeSystem.cpp:69
virtual ConstString DeclGetMangledName(void *opaque_decl)
Definition: TypeSystem.cpp:164
virtual CompilerType GetArrayType(lldb::opaque_compiler_type_t type, uint64_t size)
Definition: TypeSystem.cpp:73
static bool SupportsLanguageStatic(lldb::LanguageType language)
Definition: TypeSystem.cpp:343
virtual CompilerDeclContext GetCompilerDeclContextForType(const CompilerType &type)
Returns the direct parent context of specified type.
Definition: TypeSystem.cpp:209
virtual std::optional< CompilerType::IntegralTemplateArgument > GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, size_t idx, bool expand_pack)
Definition: TypeSystem.cpp:147
virtual bool Verify(lldb::opaque_compiler_type_t type)=0
Verify the integrity of the type to catch CompilerTypes that mix and match invalid TypeSystem/Opaque ...
Definition: TypeSystem.cpp:66
virtual lldb::TemplateArgumentKind GetTemplateArgumentKind(lldb::opaque_compiler_type_t type, size_t idx, bool expand_pack)
Definition: TypeSystem.cpp:136
static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language, Module *module)
Definition: TypeSystem.cpp:55
virtual bool IsTemplateType(lldb::opaque_compiler_type_t type)
Definition: TypeSystem.cpp:126
virtual size_t DeclGetFunctionNumArguments(void *opaque_decl)
Definition: TypeSystem.cpp:176
virtual CompilerDeclContext DeclGetDeclContext(void *opaque_decl)
Definition: TypeSystem.cpp:168
virtual CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type)
Definition: TypeSystem.cpp:102
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::TypeSystem > TypeSystemSP
Definition: lldb-forward.h:469
void * opaque_compiler_type_t
Definition: lldb-types.h:89
LanguageType
Programming language type.
@ eLanguageTypeUnknown
Unknown or invalid language value.
@ eNumLanguageTypes
@ eTemplateArgumentKindNull
A SmallBitVector that represents a set of source languages (lldb::LanguageType).
Definition: Type.h:38
llvm::SmallBitVector bitvector
Definition: Type.h:39
std::optional< lldb::LanguageType > GetSingularLanguage()
If the set contains a single language only, return it.
Definition: TypeSystem.cpp:28
bool operator[](unsigned i) const
Definition: TypeSystem.cpp:37
void Insert(lldb::LanguageType language)
Definition: TypeSystem.cpp:34