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
72
77
82
87
91
95
100
105
110
112 const char *name,
113 const CompilerDeclContext &decl_ctx,
114 uint32_t opaque_payload) {
115 return CompilerType();
116}
117
121
123 return CompilerType(weak_from_this(), type);
124}
125
129
130llvm::Expected<CompilerType>
132 ExecutionContextScope *exe_scope) {
133 return llvm::createStringError(
134 "Integral promotion is not implemented for this TypeSystem");
135}
136
140
142 bool expand_pack) {
143 return 0;
144}
145
148 bool expand_pack) {
150}
151
153 size_t idx, bool expand_pack) {
154 return CompilerType();
155}
156
157std::optional<CompilerType::IntegralTemplateArgument>
159 bool expand_pack) {
160 return std::nullopt;
161}
162
166
168 return false;
169}
170
172 return GetTypeName(type, false);
173}
174
176 return ConstString();
177}
178
182
186
187size_t TypeSystem::DeclGetFunctionNumArguments(void *opaque_decl) { return 0; }
188
190 size_t arg_idx) {
191 return CompilerType();
192}
193
194std::vector<lldb_private::CompilerContext>
196 return {};
197}
198
199std::vector<lldb_private::CompilerContext>
201 return {};
202}
203
204std::vector<CompilerDecl>
206 bool ignore_imported_decls) {
207 return std::vector<CompilerDecl>();
208}
209
210std::unique_ptr<UtilityFunction>
211TypeSystem::CreateUtilityFunction(std::string text, std::string name) {
212 return {};
213}
214
215std::optional<llvm::json::Value> TypeSystem::ReportStatistics() {
216 return std::nullopt;
217}
218
223
224#pragma mark TypeSystemMap
225
227
229
231 collection map;
232 {
233 std::lock_guard<std::mutex> guard(m_mutex);
234 map = m_map;
235 m_clear_in_progress = true;
236 }
237 llvm::DenseSet<TypeSystem *> visited;
238 for (auto &pair : map) {
239 if (visited.count(pair.second.get()))
240 continue;
241 visited.insert(pair.second.get());
242 if (lldb::TypeSystemSP type_system = pair.second)
243 type_system->Finalize();
244 }
245 map.clear();
246 {
247 std::lock_guard<std::mutex> guard(m_mutex);
248 m_map.clear();
249 m_clear_in_progress = false;
250 }
251}
252
254 std::function<bool(lldb::TypeSystemSP)> const &callback) {
255
256 // The callback may call into this function again causing
257 // us to lock m_mutex twice if we held it across the callback.
258 // Since we just care about guarding access to 'm_map', make
259 // a local copy and iterate over that instead.
260 collection map_snapshot;
261 {
262 std::lock_guard<std::mutex> guard(m_mutex);
263 map_snapshot = m_map;
264 }
265
266 // Use a std::set so we only call the callback once for each unique
267 // TypeSystem instance.
268 llvm::DenseSet<TypeSystem *> visited;
269 for (auto &pair : map_snapshot) {
270 TypeSystem *type_system = pair.second.get();
271 if (!type_system || visited.count(type_system))
272 continue;
273 visited.insert(type_system);
274 assert(type_system);
275 if (!callback(pair.second))
276 break;
277 }
278}
279
280llvm::Expected<lldb::TypeSystemSP> TypeSystemMap::GetTypeSystemForLanguage(
281 lldb::LanguageType language,
282 std::optional<CreateCallback> create_callback) {
283 std::lock_guard<std::mutex> guard(m_mutex);
285 return llvm::createStringError(
286 "Unable to get TypeSystem because TypeSystemMap is being cleared");
287
288 collection::iterator pos = m_map.find(language);
289 if (pos != m_map.end()) {
290 if (pos->second) {
291 assert(!pos->second->weak_from_this().expired());
292 return pos->second;
293 }
294 return llvm::createStringError(
295 "TypeSystem for language " +
296 llvm::StringRef(Language::GetNameForLanguageType(language)) +
297 " doesn't exist");
298 }
299
300 for (const auto &pair : m_map) {
301 if (pair.second && pair.second->SupportsLanguage(language)) {
302 // Add a new mapping for "language" to point to an already existing
303 // TypeSystem that supports this language
304 m_map[language] = pair.second;
305 if (pair.second)
306 return pair.second;
307 return llvm::createStringError(
308 "TypeSystem for language " +
309 llvm::StringRef(Language::GetNameForLanguageType(language)) +
310 " doesn't exist");
311 }
312 }
313
314 if (!create_callback)
315 return llvm::createStringError(
316 "Unable to find type system for language " +
317 llvm::StringRef(Language::GetNameForLanguageType(language)));
318 // Cache even if we get a shared pointer that contains a null type system
319 // back.
320 TypeSystemSP type_system_sp = (*create_callback)();
321 m_map[language] = type_system_sp;
322 if (type_system_sp)
323 return type_system_sp;
324 return llvm::createStringError(
325 "TypeSystem for language " +
326 llvm::StringRef(Language::GetNameForLanguageType(language)) +
327 " doesn't exist");
328}
329
330llvm::Expected<lldb::TypeSystemSP>
332 Module *module, bool can_create) {
333 if (can_create) {
335 language, std::optional<CreateCallback>([language, module]() {
336 return TypeSystem::CreateInstance(language, module);
337 }));
338 }
339 return GetTypeSystemForLanguage(language);
340}
341
342llvm::Expected<lldb::TypeSystemSP>
344 Target *target, bool can_create) {
345 if (can_create) {
347 language, std::optional<CreateCallback>([language, target]() {
348 return TypeSystem::CreateInstance(language, target);
349 }));
350 }
351 return GetTypeSystemForLanguage(language);
352}
353
355 if (language == eLanguageTypeUnknown || language >= eNumLanguageTypes)
356 return false;
357
358 LanguageSet languages =
360 if (languages.Empty())
361 return false;
362 return languages[language];
363}
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
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
static const char * GetNameForLanguageType(lldb::LanguageType language)
Returns the internal LLDB name for the specified language.
Definition Language.cpp:309
A class that describes an executable image and its associated object and symbol files.
Definition Module.h:90
static TypeSystemCreateInstance GetTypeSystemCreateCallbackAtIndex(uint32_t idx)
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)
Checks if the type is eligible for integral promotion.
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 llvm::Expected< CompilerType > DoIntegralPromotion(CompilerType from, ExecutionContextScope *exe_scope)
Perform integral promotion on a given type.
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 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.
lldb::TypeSystemSP(* TypeSystemCreateInstance)(lldb::LanguageType language, Module *module, Target *target)
std::shared_ptr< lldb_private::TypeSystem > TypeSystemSP
void * opaque_compiler_type_t
Definition lldb-types.h:89
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)