LLDB  mainline
TypeSystem.cpp
Go to the documentation of this file.
1 //===-- TypeSystem.cpp ------------------------------------------*- C++ -*-===//
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 
9 //
10 // TypeSystem.cpp
11 // lldb
12 //
13 // Created by Ryan Brown on 3/29/15.
14 //
15 //
16 
17 #include "lldb/Symbol/TypeSystem.h"
18 
19 #include <set>
20 
23 
24 using namespace lldb_private;
25 using namespace lldb;
26 
27 TypeSystem::TypeSystem(LLVMCastKind kind) : m_kind(kind), m_sym_file(nullptr) {}
28 
30 
31 static lldb::TypeSystemSP CreateInstanceHelper(lldb::LanguageType language,
32  Module *module, Target *target) {
33  uint32_t i = 0;
34  TypeSystemCreateInstance create_callback;
35  while ((create_callback = PluginManager::GetTypeSystemCreateCallbackAtIndex(
36  i++)) != nullptr) {
37  lldb::TypeSystemSP type_system_sp =
38  create_callback(language, module, target);
39  if (type_system_sp)
40  return type_system_sp;
41  }
42 
43  return lldb::TypeSystemSP();
44 }
45 
46 lldb::TypeSystemSP TypeSystem::CreateInstance(lldb::LanguageType language,
47  Module *module) {
48  return CreateInstanceHelper(language, module, nullptr);
49 }
50 
51 lldb::TypeSystemSP TypeSystem::CreateInstance(lldb::LanguageType language,
52  Target *target) {
53  return CreateInstanceHelper(language, nullptr, target);
54 }
55 
57  return false;
58 }
59 
61  uint64_t size) {
62  return CompilerType();
63 }
64 
67  return CompilerType();
68 }
69 
72  return CompilerType();
73 }
74 
76  return CompilerType();
77 }
78 
81  return CompilerType();
82 }
83 
86  return CompilerType();
87 }
88 
90  const char *name,
91  const CompilerDeclContext &decl_ctx) {
92  return CompilerType();
93 }
94 
96  return CompilerType();
97 }
98 
100  return CompilerType(this, type);
101 }
102 
104  return 0;
105 }
106 
110 }
111 
113  size_t idx) {
114  return CompilerType();
115 }
116 
117 llvm::Optional<CompilerType::IntegralTemplateArgument>
119  size_t idx) {
120  return llvm::None;
121 }
122 
124  return eLazyBoolCalculate;
125 }
126 
128  return false;
129 }
130 
132  return ConstString();
133 }
134 
136  return CompilerDeclContext();
137 }
138 
140  return CompilerType();
141 }
142 
143 size_t TypeSystem::DeclGetFunctionNumArguments(void *opaque_decl) { return 0; }
144 
146  size_t arg_idx) {
147  return CompilerType();
148 }
149 
150 std::vector<CompilerDecl>
152  bool ignore_imported_decls) {
153  return std::vector<CompilerDecl>();
154 }
155 
156 #pragma mark TypeSystemMap
157 
159  : m_mutex(), m_map(), m_clear_in_progress(false) {}
160 
162 
164  collection map;
165  {
166  std::lock_guard<std::mutex> guard(m_mutex);
167  map = m_map;
168  m_clear_in_progress = true;
169  }
170  std::set<TypeSystem *> visited;
171  for (auto pair : map) {
172  TypeSystem *type_system = pair.second.get();
173  if (type_system && !visited.count(type_system)) {
174  visited.insert(type_system);
175  type_system->Finalize();
176  }
177  }
178  map.clear();
179  {
180  std::lock_guard<std::mutex> guard(m_mutex);
181  m_map.clear();
182  m_clear_in_progress = false;
183  }
184 }
185 
186 void TypeSystemMap::ForEach(std::function<bool(TypeSystem *)> const &callback) {
187  std::lock_guard<std::mutex> guard(m_mutex);
188  // Use a std::set so we only call the callback once for each unique
189  // TypeSystem instance
190  std::set<TypeSystem *> visited;
191  for (auto pair : m_map) {
192  TypeSystem *type_system = pair.second.get();
193  if (type_system && !visited.count(type_system)) {
194  visited.insert(type_system);
195  if (!callback(type_system))
196  break;
197  }
198  }
199 }
200 
202  Module *module,
203  bool can_create) {
204  std::lock_guard<std::mutex> guard(m_mutex);
205  collection::iterator pos = m_map.find(language);
206  if (pos != m_map.end())
207  return pos->second.get();
208 
209  for (const auto &pair : m_map) {
210  if (pair.second && pair.second->SupportsLanguage(language)) {
211  // Add a new mapping for "language" to point to an already existing
212  // TypeSystem that supports this language
213  AddToMap(language, pair.second);
214  return pair.second.get();
215  }
216  }
217 
218  if (!can_create)
219  return nullptr;
220 
221  // Cache even if we get a shared pointer that contains null type system back
222  lldb::TypeSystemSP type_system_sp =
223  TypeSystem::CreateInstance(language, module);
224  AddToMap(language, type_system_sp);
225  return type_system_sp.get();
226 }
227 
229  Target *target,
230  bool can_create) {
231  std::lock_guard<std::mutex> guard(m_mutex);
232  collection::iterator pos = m_map.find(language);
233  if (pos != m_map.end())
234  return pos->second.get();
235 
236  for (const auto &pair : m_map) {
237  if (pair.second && pair.second->SupportsLanguage(language)) {
238  // Add a new mapping for "language" to point to an already existing
239  // TypeSystem that supports this language
240 
241  AddToMap(language, pair.second);
242  return pair.second.get();
243  }
244  }
245 
246  if (!can_create)
247  return nullptr;
248 
249  // Cache even if we get a shared pointer that contains null type system back
250  lldb::TypeSystemSP type_system_sp;
251  if (!m_clear_in_progress)
252  type_system_sp = TypeSystem::CreateInstance(language, target);
253 
254  AddToMap(language, type_system_sp);
255  return type_system_sp.get();
256 }
257 
259  lldb::TypeSystemSP const &type_system_sp) {
260  if (!m_clear_in_progress)
261  m_map[language] = type_system_sp;
262 }
virtual bool IsAnonymousType(lldb::opaque_compiler_type_t type)
Definition: TypeSystem.cpp:56
static TypeSystemCreateInstance GetTypeSystemCreateCallbackAtIndex(uint32_t idx)
virtual CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type)
Definition: TypeSystem.cpp:80
virtual bool IsMeaninglessWithoutDynamicResolution(void *type)
Definition: TypeSystem.cpp:127
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
TypeSystem * GetTypeSystemForLanguage(lldb::LanguageType language, Module *module, bool can_create)
Definition: TypeSystem.cpp:201
virtual CompilerType GetLValueReferenceType(lldb::opaque_compiler_type_t type)
Definition: TypeSystem.cpp:66
void * opaque_compiler_type_t
Definition: lldb-types.h:90
virtual lldb::TemplateArgumentKind GetTemplateArgumentKind(lldb::opaque_compiler_type_t type, size_t idx)
Definition: TypeSystem.cpp:108
TypeSystem(LLVMCastKind kind)
Definition: TypeSystem.cpp:27
virtual CompilerType DeclGetFunctionReturnType(void *opaque_decl)
Definition: TypeSystem.cpp:139
LanguageType
Programming language type.
virtual CompilerType GetTypeForFormatters(void *type)
Definition: TypeSystem.cpp:99
virtual CompilerType GetArrayType(lldb::opaque_compiler_type_t type, uint64_t size)
Definition: TypeSystem.cpp:60
A class that describes an executable image and its associated object and symbol files.
Definition: Module.h:109
virtual CompilerType DeclGetFunctionArgumentType(void *opaque_decl, size_t arg_idx)
Definition: TypeSystem.cpp:145
static lldb::TypeSystemSP CreateInstanceHelper(lldb::LanguageType language, Module *module, Target *target)
Definition: TypeSystem.cpp:31
static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language, Module *module)
Definition: TypeSystem.cpp:46
virtual CompilerDeclContext DeclGetDeclContext(void *opaque_decl)
Definition: TypeSystem.cpp:135
virtual llvm::Optional< CompilerType::IntegralTemplateArgument > GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, size_t idx)
Definition: TypeSystem.cpp:118
void AddToMap(lldb::LanguageType language, lldb::TypeSystemSP const &type_system_sp)
Definition: TypeSystem.cpp:258
virtual void Finalize()
Definition: TypeSystem.h:86
virtual CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type)
Definition: TypeSystem.cpp:85
virtual CompilerType AddConstModifier(lldb::opaque_compiler_type_t type)
Definition: TypeSystem.cpp:75
A uniqued constant string class.
Definition: ConstString.h:38
virtual CompilerType CreateTypedef(lldb::opaque_compiler_type_t type, const char *name, const CompilerDeclContext &decl_ctx)
Definition: TypeSystem.cpp:89
Definition: SBAddress.h:15
virtual ConstString DeclGetMangledName(void *opaque_decl)
Definition: TypeSystem.cpp:131
virtual size_t DeclGetFunctionNumArguments(void *opaque_decl)
Definition: TypeSystem.cpp:143
virtual CompilerType GetTypeTemplateArgument(lldb::opaque_compiler_type_t type, size_t idx)
Definition: TypeSystem.cpp:112
std::map< lldb::LanguageType, lldb::TypeSystemSP > collection
Definition: TypeSystem.h:504
void ForEach(std::function< bool(TypeSystem *)> const &callback)
Definition: TypeSystem.cpp:186
virtual CompilerType GetRValueReferenceType(lldb::opaque_compiler_type_t type)
Definition: TypeSystem.cpp:71
virtual std::vector< CompilerDecl > DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name, const bool ignore_imported_decls)
Definition: TypeSystem.cpp:151
virtual CompilerType GetBuiltinTypeByName(ConstString name)
Definition: TypeSystem.cpp:95
std::mutex m_mutex
A mutex to keep this object happy in multi-threaded environments.
Definition: TypeSystem.h:505
virtual size_t GetNumTemplateArguments(lldb::opaque_compiler_type_t type)
Definition: TypeSystem.cpp:103
virtual LazyBool ShouldPrintAsOneLiner(void *type, ValueObject *valobj)
Definition: TypeSystem.cpp:123