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 for (auto create_callback : PluginManager::GetTypeSystemCreateCallbacks()) {
45 if (auto type_system_sp = create_callback(language, module, target))
46 return type_system_sp;
47 }
48 return {};
49}
50
52 Module *module) {
53 return CreateInstanceHelper(language, module, nullptr);
54}
55
57 Target *target) {
58 return CreateInstanceHelper(language, nullptr, target);
59}
60
61#ifndef NDEBUG
63#endif
64
68
73
78
83
87
91
96
101
106
108 const char *name,
109 const CompilerDeclContext &decl_ctx,
110 uint32_t opaque_payload) {
111 return CompilerType();
112}
113
117
119 return CompilerType(weak_from_this(), type);
120}
121
125
130
134
136 bool expand_pack) {
137 return 0;
138}
139
142 bool expand_pack) {
144}
145
147 size_t idx, bool expand_pack) {
148 return CompilerType();
149}
150
151std::optional<CompilerType::IntegralTemplateArgument>
153 bool expand_pack) {
154 return std::nullopt;
155}
156
160
162 return false;
163}
164
166 return GetTypeName(type, false);
167}
168
170 return ConstString();
171}
172
176
180
181size_t TypeSystem::DeclGetFunctionNumArguments(void *opaque_decl) { return 0; }
182
184 size_t arg_idx) {
185 return CompilerType();
186}
187
188std::vector<lldb_private::CompilerContext>
190 return {};
191}
192
193std::vector<lldb_private::CompilerContext>
195 return {};
196}
197
198std::vector<CompilerDecl>
200 bool ignore_imported_decls) {
201 return std::vector<CompilerDecl>();
202}
203
204std::unique_ptr<UtilityFunction>
205TypeSystem::CreateUtilityFunction(std::string text, std::string name) {
206 return {};
207}
208
209std::optional<llvm::json::Value> TypeSystem::ReportStatistics() {
210 return std::nullopt;
211}
212
217
218#pragma mark TypeSystemMap
219
221
223
225 collection map;
226 {
227 std::lock_guard<std::mutex> guard(m_mutex);
228 map = m_map;
229 m_clear_in_progress = true;
230 }
231 llvm::DenseSet<TypeSystem *> visited;
232 for (auto &pair : map) {
233 if (visited.count(pair.second.get()))
234 continue;
235 visited.insert(pair.second.get());
236 if (lldb::TypeSystemSP type_system = pair.second)
237 type_system->Finalize();
238 }
239 map.clear();
240 {
241 std::lock_guard<std::mutex> guard(m_mutex);
242 m_map.clear();
243 m_clear_in_progress = false;
244 }
245}
246
248 std::function<bool(lldb::TypeSystemSP)> const &callback) {
249
250 // The callback may call into this function again causing
251 // us to lock m_mutex twice if we held it across the callback.
252 // Since we just care about guarding access to 'm_map', make
253 // a local copy and iterate over that instead.
254 collection map_snapshot;
255 {
256 std::lock_guard<std::mutex> guard(m_mutex);
257 map_snapshot = m_map;
258 }
259
260 // Use a std::set so we only call the callback once for each unique
261 // TypeSystem instance.
262 llvm::DenseSet<TypeSystem *> visited;
263 for (auto &pair : map_snapshot) {
264 TypeSystem *type_system = pair.second.get();
265 if (!type_system || visited.count(type_system))
266 continue;
267 visited.insert(type_system);
268 assert(type_system);
269 if (!callback(pair.second))
270 break;
271 }
272}
273
274llvm::Expected<lldb::TypeSystemSP> TypeSystemMap::GetTypeSystemForLanguage(
275 lldb::LanguageType language,
276 std::optional<CreateCallback> create_callback) {
277 std::lock_guard<std::mutex> guard(m_mutex);
279 return llvm::createStringError(
280 "Unable to get TypeSystem because TypeSystemMap is being cleared");
281
282 collection::iterator pos = m_map.find(language);
283 if (pos != m_map.end()) {
284 if (pos->second) {
285 assert(!pos->second->weak_from_this().expired());
286 return pos->second;
287 }
288 return llvm::createStringError(
289 "TypeSystem for language " +
290 llvm::StringRef(Language::GetNameForLanguageType(language)) +
291 " doesn't exist");
292 }
293
294 for (const auto &pair : m_map) {
295 if (pair.second && pair.second->SupportsLanguage(language)) {
296 // Add a new mapping for "language" to point to an already existing
297 // TypeSystem that supports this language
298 m_map[language] = pair.second;
299 if (pair.second)
300 return pair.second;
301 return llvm::createStringError(
302 "TypeSystem for language " +
303 llvm::StringRef(Language::GetNameForLanguageType(language)) +
304 " doesn't exist");
305 }
306 }
307
308 if (!create_callback)
309 return llvm::createStringError(
310 "Unable to find type system for language " +
311 llvm::StringRef(Language::GetNameForLanguageType(language)));
312 // Cache even if we get a shared pointer that contains a null type system
313 // back.
314 TypeSystemSP type_system_sp = (*create_callback)();
315 m_map[language] = type_system_sp;
316 if (type_system_sp)
317 return type_system_sp;
318 return llvm::createStringError(
319 "TypeSystem for language " +
320 llvm::StringRef(Language::GetNameForLanguageType(language)) +
321 " doesn't exist");
322}
323
324llvm::Expected<lldb::TypeSystemSP>
326 Module *module, bool can_create) {
327 if (can_create) {
329 language, std::optional<CreateCallback>([language, module]() {
330 return TypeSystem::CreateInstance(language, module);
331 }));
332 }
333 return GetTypeSystemForLanguage(language);
334}
335
336llvm::Expected<lldb::TypeSystemSP>
338 Target *target, bool can_create) {
339 if (can_create) {
341 language, std::optional<CreateCallback>([language, target]() {
342 return TypeSystem::CreateInstance(language, target);
343 }));
344 }
345 return GetTypeSystemForLanguage(language);
346}
347
349 if (language == eLanguageTypeUnknown || language >= eNumLanguageTypes)
350 return false;
351
352 LanguageSet languages =
354 if (languages.Empty())
355 return false;
356 return languages[language];
357}
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...
static TypeSystemSP CreateInstanceHelper(lldb::LanguageType language, Module *module, Target *target)
Represents a generic declaration context in a program.
Generic representation of a type in a programming language.
A uniqued constant string class.
Definition ConstString.h:40
static const char * GetNameForLanguageType(lldb::LanguageType language)
Returns the internal LLDB name for the specified language.
Definition Language.cpp:305
A class that describes an executable image and its associated object and symbol files.
Definition Module.h:90
static llvm::SmallVector< TypeSystemCreateInstance > GetTypeSystemCreateCallbacks()
static LanguageSet GetAllTypeSystemSupportedLanguagesForTypes()
llvm::DenseMap< uint16_t, lldb::TypeSystemSP > collection
Definition TypeSystem.h:596
std::mutex m_mutex
A mutex to keep this object happy in multi-threaded environments.
Definition TypeSystem.h:597
llvm::Expected< lldb::TypeSystemSP > GetTypeSystemForLanguage(lldb::LanguageType language, Module *module, bool can_create)
void ForEach(std::function< bool(lldb::TypeSystemSP)> const &callback)
Interface for representing a type system.
Definition TypeSystem.h:70
virtual std::unique_ptr< UtilityFunction > CreateUtilityFunction(std::string text, std::string name)
virtual bool IsMeaninglessWithoutDynamicResolution(void *type)
virtual size_t GetNumTemplateArguments(lldb::opaque_compiler_type_t type, bool expand_pack)
virtual CompilerType GetBuiltinTypeByName(ConstString name)
virtual CompilerType GetTypeTemplateArgument(lldb::opaque_compiler_type_t type, size_t idx, bool expand_pack)
virtual CompilerType GetTypeForFormatters(void *type)
virtual CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type)
virtual CompilerType GetAtomicType(lldb::opaque_compiler_type_t type)
virtual ConstString GetMangledTypeName(lldb::opaque_compiler_type_t type)
Defaults to GetTypeName(type).
virtual CompilerType GetLValueReferenceType(lldb::opaque_compiler_type_t type)
virtual std::vector< CompilerDecl > DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name, const bool ignore_imported_decls)
virtual ConstString GetTypeName(lldb::opaque_compiler_type_t type, bool BaseOnly)=0
virtual bool IsPromotableIntegerType(lldb::opaque_compiler_type_t type)
virtual std::optional< llvm::json::Value > ReportStatistics()
virtual CompilerType CreateTypedef(lldb::opaque_compiler_type_t type, const char *name, const CompilerDeclContext &decl_ctx, uint32_t opaque_payload)
virtual LazyBool ShouldPrintAsOneLiner(void *type, ValueObject *valobj)
virtual CompilerType AddConstModifier(lldb::opaque_compiler_type_t type)
virtual CompilerType AddPtrAuthModifier(lldb::opaque_compiler_type_t type, uint32_t payload)
virtual CompilerType DeclGetFunctionReturnType(void *opaque_decl)
virtual CompilerType DeclGetFunctionArgumentType(void *opaque_decl, size_t arg_idx)
virtual CompilerType GetRValueReferenceType(lldb::opaque_compiler_type_t type)
virtual std::vector< lldb_private::CompilerContext > DeclGetCompilerContext(void *opaque_decl)
virtual std::vector< lldb_private::CompilerContext > DeclContextGetCompilerContext(void *opaque_decl_ctx)
virtual bool IsAnonymousType(lldb::opaque_compiler_type_t type)
virtual ConstString DeclGetMangledName(void *opaque_decl)
virtual CompilerType GetArrayType(lldb::opaque_compiler_type_t type, uint64_t size)
static bool SupportsLanguageStatic(lldb::LanguageType language)
virtual CompilerDeclContext GetCompilerDeclContextForType(const CompilerType &type)
Returns the direct parent context of specified type.
virtual CompilerType GetPromotedIntegerType(lldb::opaque_compiler_type_t type)
virtual std::optional< CompilerType::IntegralTemplateArgument > GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, size_t idx, bool expand_pack)
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 ...
virtual lldb::TemplateArgumentKind GetTemplateArgumentKind(lldb::opaque_compiler_type_t type, size_t idx, bool expand_pack)
static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language, Module *module)
virtual bool IsTemplateType(lldb::opaque_compiler_type_t type)
virtual size_t DeclGetFunctionNumArguments(void *opaque_decl)
virtual CompilerDeclContext DeclGetDeclContext(void *opaque_decl)
virtual CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type)
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::TypeSystem > TypeSystemSP
void * opaque_compiler_type_t
Definition lldb-types.h:90
LanguageType
Programming language type.
@ eLanguageTypeUnknown
Unknown or invalid language value.
@ 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.
bool operator[](unsigned i) const
void Insert(lldb::LanguageType language)