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 bool
59 TypeSystem::IsAnonymousType (lldb::opaque_compiler_type_t type)
60 {
61     return false;
62 }
63 
64 CompilerType
65 TypeSystem::GetLValueReferenceType (lldb::opaque_compiler_type_t type)
66 {
67     return CompilerType();
68 }
69 
70 CompilerType
71 TypeSystem::GetRValueReferenceType (lldb::opaque_compiler_type_t type)
72 {
73     return CompilerType();
74 }
75 
76 CompilerType
77 TypeSystem::AddConstModifier (lldb::opaque_compiler_type_t type)
78 {
79     return CompilerType();
80 }
81 
82 CompilerType
83 TypeSystem::AddVolatileModifier (lldb::opaque_compiler_type_t type)
84 {
85     return CompilerType();
86 }
87 
88 CompilerType
89 TypeSystem::AddRestrictModifier (lldb::opaque_compiler_type_t type)
90 {
91     return CompilerType();
92 }
93 
94 CompilerType
95 TypeSystem::CreateTypedef (lldb::opaque_compiler_type_t type, const char *name, const CompilerDeclContext &decl_ctx)
96 {
97     return CompilerType();
98 }
99 
100 CompilerType
101 TypeSystem::GetBuiltinTypeByName (const ConstString &name)
102 {
103     return CompilerType();
104 }
105 
106 CompilerType
107 TypeSystem::GetTypeForFormatters (void* type)
108 {
109     return CompilerType(this, type);
110 }
111 
112 LazyBool
113 TypeSystem::ShouldPrintAsOneLiner (void* type, ValueObject* valobj)
114 {
115     return eLazyBoolCalculate;
116 }
117 
118 bool
119 TypeSystem::IsMeaninglessWithoutDynamicResolution (void* type)
120 {
121     return false;
122 }
123 
124 #pragma mark TypeSystemMap
125 
126 TypeSystemMap::TypeSystemMap() :
127     m_mutex (),
128     m_map ()
129 {
130 }
131 
132 TypeSystemMap::~TypeSystemMap()
133 {
134 }
135 
136 void
137 TypeSystemMap::Clear ()
138 {
139     Mutex::Locker locker (m_mutex);
140     m_map.clear();
141 }
142 
143 
144 void
145 TypeSystemMap::ForEach (std::function <bool(TypeSystem *)> const &callback)
146 {
147     Mutex::Locker locker (m_mutex);
148     // Use a std::set so we only call the callback once for each unique
149     // TypeSystem instance
150     std::set<TypeSystem *> visited;
151     for (auto pair : m_map)
152     {
153         TypeSystem *type_system = pair.second.get();
154         if (type_system && !visited.count(type_system))
155         {
156             visited.insert(type_system);
157             if (callback (type_system) == false)
158                 break;
159         }
160     }
161 }
162 
163 TypeSystem *
164 TypeSystemMap::GetTypeSystemForLanguage (lldb::LanguageType language, Module *module, bool can_create)
165 {
166     Mutex::Locker locker (m_mutex);
167     collection::iterator pos = m_map.find(language);
168     if (pos != m_map.end())
169         return pos->second.get();
170 
171     for (const auto &pair : m_map)
172     {
173         if (pair.second && pair.second->SupportsLanguage(language))
174         {
175             // Add a new mapping for "language" to point to an already existing
176             // TypeSystem that supports this language
177             m_map[language] = pair.second;
178             return pair.second.get();
179         }
180     }
181 
182     if (!can_create)
183         return nullptr;
184 
185     // Cache even if we get a shared pointer that contains null type system back
186     lldb::TypeSystemSP type_system_sp = TypeSystem::CreateInstance (language, module);
187     m_map[language] = type_system_sp;
188     return type_system_sp.get();
189 }
190 
191 TypeSystem *
192 TypeSystemMap::GetTypeSystemForLanguage (lldb::LanguageType language, Target *target, bool can_create)
193 {
194     Mutex::Locker locker (m_mutex);
195     collection::iterator pos = m_map.find(language);
196     if (pos != m_map.end())
197         return pos->second.get();
198 
199     for (const auto &pair : m_map)
200     {
201         if (pair.second && pair.second->SupportsLanguage(language))
202         {
203             // Add a new mapping for "language" to point to an already existing
204             // TypeSystem that supports this language
205             m_map[language] = pair.second;
206             return pair.second.get();
207         }
208     }
209 
210     if (!can_create)
211         return nullptr;
212 
213     // Cache even if we get a shared pointer that contains null type system back
214     lldb::TypeSystemSP type_system_sp = TypeSystem::CreateInstance (language, target);
215     m_map[language] = type_system_sp;
216     return type_system_sp.get();
217 }
218