1 //
2 //  TypeSystem.cpp
3 //  lldb
4 //
5 //  Created by Ryan Brown on 3/29/15.
6 //
7 //
8 
9 #include "lldb/Symbol/TypeSystem.h"
10 
11 #include <set>
12 
13 #include "lldb/Core/PluginManager.h"
14 #include "lldb/Symbol/CompilerType.h"
15 
16 using namespace lldb_private;
17 
18 TypeSystem::TypeSystem(LLVMCastKind kind) :
19     m_kind (kind),
20     m_sym_file (nullptr)
21 {
22 }
23 
24 TypeSystem::~TypeSystem()
25 {
26 }
27 
28 lldb::TypeSystemSP
29 TypeSystem::CreateInstance (lldb::LanguageType language, Module *module)
30 {
31     uint32_t i = 0;
32     TypeSystemCreateInstance create_callback;
33     while ((create_callback = PluginManager::GetTypeSystemCreateCallbackAtIndex (i++)) != nullptr)
34     {
35         lldb::TypeSystemSP type_system_sp = create_callback(language, module, nullptr);
36         if (type_system_sp)
37             return type_system_sp;
38     }
39 
40     return lldb::TypeSystemSP();
41 }
42 
43 lldb::TypeSystemSP
44 TypeSystem::CreateInstance (lldb::LanguageType language, Target *target)
45 {
46     uint32_t i = 0;
47     TypeSystemCreateInstance create_callback;
48     while ((create_callback = PluginManager::GetTypeSystemCreateCallbackAtIndex (i++)) != nullptr)
49     {
50         lldb::TypeSystemSP type_system_sp = create_callback(language, nullptr, target);
51         if (type_system_sp)
52             return type_system_sp;
53     }
54 
55     return lldb::TypeSystemSP();
56 }
57 
58 CompilerType
59 TypeSystem::GetLValueReferenceType (lldb::opaque_compiler_type_t type)
60 {
61     return CompilerType();
62 }
63 
64 CompilerType
65 TypeSystem::GetRValueReferenceType (lldb::opaque_compiler_type_t type)
66 {
67     return CompilerType();
68 }
69 
70 CompilerType
71 TypeSystem::AddConstModifier (lldb::opaque_compiler_type_t type)
72 {
73     return CompilerType();
74 }
75 
76 CompilerType
77 TypeSystem::AddVolatileModifier (lldb::opaque_compiler_type_t type)
78 {
79     return CompilerType();
80 }
81 
82 CompilerType
83 TypeSystem::AddRestrictModifier (lldb::opaque_compiler_type_t type)
84 {
85     return CompilerType();
86 }
87 
88 CompilerType
89 TypeSystem::CreateTypedef (lldb::opaque_compiler_type_t type, const char *name, const CompilerDeclContext &decl_ctx)
90 {
91     return CompilerType();
92 }
93 
94 CompilerType
95 TypeSystem::GetBuiltinTypeByName (const ConstString &name)
96 {
97     return CompilerType();
98 }
99 
100 CompilerType
101 TypeSystem::GetTypeForFormatters (void* type)
102 {
103     return CompilerType(this, type);
104 }
105 
106 LazyBool
107 TypeSystem::ShouldPrintAsOneLiner (void* type)
108 {
109     return eLazyBoolCalculate;
110 }
111 
112 #pragma mark TypeSystemMap
113 
114 TypeSystemMap::TypeSystemMap() :
115     m_mutex (),
116     m_map ()
117 {
118 }
119 
120 TypeSystemMap::~TypeSystemMap()
121 {
122 }
123 
124 void
125 TypeSystemMap::Clear ()
126 {
127     Mutex::Locker locker (m_mutex);
128     m_map.clear();
129 }
130 
131 
132 void
133 TypeSystemMap::ForEach (std::function <bool(TypeSystem *)> const &callback)
134 {
135     Mutex::Locker locker (m_mutex);
136     // Use a std::set so we only call the callback once for each unique
137     // TypeSystem instance
138     std::set<TypeSystem *> visited;
139     for (auto pair : m_map)
140     {
141         TypeSystem *type_system = pair.second.get();
142         if (type_system && !visited.count(type_system))
143         {
144             visited.insert(type_system);
145             if (callback (type_system) == false)
146                 break;
147         }
148     }
149 }
150 
151 TypeSystem *
152 TypeSystemMap::GetTypeSystemForLanguage (lldb::LanguageType language, Module *module, bool can_create)
153 {
154     Mutex::Locker locker (m_mutex);
155     collection::iterator pos = m_map.find(language);
156     if (pos != m_map.end())
157         return pos->second.get();
158 
159     for (const auto &pair : m_map)
160     {
161         if (pair.second && pair.second->SupportsLanguage(language))
162         {
163             // Add a new mapping for "language" to point to an already existing
164             // TypeSystem that supports this language
165             m_map[language] = pair.second;
166             return pair.second.get();
167         }
168     }
169 
170     if (!can_create)
171         return nullptr;
172 
173     // Cache even if we get a shared pointer that contains null type system back
174     lldb::TypeSystemSP type_system_sp = TypeSystem::CreateInstance (language, module);
175     m_map[language] = type_system_sp;
176     return type_system_sp.get();
177 }
178 
179 TypeSystem *
180 TypeSystemMap::GetTypeSystemForLanguage (lldb::LanguageType language, Target *target, bool can_create)
181 {
182     Mutex::Locker locker (m_mutex);
183     collection::iterator pos = m_map.find(language);
184     if (pos != m_map.end())
185         return pos->second.get();
186 
187     for (const auto &pair : m_map)
188     {
189         if (pair.second && pair.second->SupportsLanguage(language))
190         {
191             // Add a new mapping for "language" to point to an already existing
192             // TypeSystem that supports this language
193             m_map[language] = pair.second;
194             return pair.second.get();
195         }
196     }
197 
198     if (!can_create)
199         return nullptr;
200 
201     // Cache even if we get a shared pointer that contains null type system back
202     lldb::TypeSystemSP type_system_sp = TypeSystem::CreateInstance (language, target);
203     m_map[language] = type_system_sp;
204     return type_system_sp.get();
205 }
206