blob: 6b36d14a11417b1ccabf0220c57f527555f17999 [file] [log] [blame]
Jim Ingham642036f2010-09-23 02:01:19 +00001//===-- CPPLanguageRuntime.cpp -------------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/CPPLanguageRuntime.h"
Enrico Granatabad97532012-02-03 01:41:25 +000011
Jim Ingham642036f2010-09-23 02:01:19 +000012#include "lldb/Core/PluginManager.h"
Enrico Granatabad97532012-02-03 01:41:25 +000013#include "lldb/Core/UniqueCStringMap.h"
Jim Inghamb66cd072010-09-28 01:25:32 +000014#include "lldb/Target/ExecutionContext.h"
Jim Ingham642036f2010-09-23 02:01:19 +000015
16using namespace lldb;
17using namespace lldb_private;
18
Enrico Granatabad97532012-02-03 01:41:25 +000019class CPPRuntimeEquivalents
20{
21public:
22 CPPRuntimeEquivalents ()
23 {
24
25 m_impl.Append(ConstString("std::basic_string<char, std::char_traits<char>, std::allocator<char> >").AsCString(), ConstString("basic_string<char>"));
Enrico Granatabad97532012-02-03 01:41:25 +000026
27 // these two (with a prefixed std::) occur when c++stdlib string class occurs as a template argument in some STL container
28 m_impl.Append(ConstString("std::basic_string<char, std::char_traits<char>, std::allocator<char> >").AsCString(), ConstString("std::basic_string<char>"));
Enrico Granatabad97532012-02-03 01:41:25 +000029
30 m_impl.Sort();
31 }
32
33 void
34 Add (ConstString& type_name,
35 ConstString& type_equivalent)
36 {
37 m_impl.Insert(type_name.AsCString(), type_equivalent);
38 }
39
40 uint32_t
41 FindExactMatches (ConstString& type_name,
42 std::vector<ConstString>& equivalents)
43 {
44
45 uint32_t count = 0;
46
47 for (ImplData match = m_impl.FindFirstValueForName(type_name.AsCString());
48 match != NULL;
49 match = m_impl.FindNextValueForName(match))
50 {
51 equivalents.push_back(match->value);
52 count++;
53 }
54
55 return count;
56 }
57
58 // partial matches can occur when a name with equivalents is a template argument.
59 // e.g. we may have "class Foo" be a match for "struct Bar". if we have a typename
60 // such as "class Templatized<class Foo, Anything>" we want this to be replaced with
61 // "class Templatized<struct Bar, Anything>". Since partial matching is time consuming
62 // once we get a partial match, we add it to the exact matches list for faster retrieval
63 uint32_t
64 FindPartialMatches (ConstString& type_name,
65 std::vector<ConstString>& equivalents)
66 {
67
68 uint32_t count = 0;
69
70 const char* type_name_cstr = type_name.AsCString();
71
72 size_t items_count = m_impl.GetSize();
73
74 for (size_t item = 0; item < items_count; item++)
75 {
76 const char* key_cstr = m_impl.GetCStringAtIndex(item);
77 if ( strstr(type_name_cstr,key_cstr) )
78 {
79 count += AppendReplacements(type_name_cstr,
80 key_cstr,
81 equivalents);
82 }
83 }
84
85 return count;
86
87 }
88
89private:
90
91 std::string& replace (std::string& target,
92 std::string& pattern,
93 std::string& with)
94 {
95 size_t pos;
96 size_t pattern_len = pattern.size();
97
98 while ( (pos = target.find(pattern)) != std::string::npos )
99 target.replace(pos, pattern_len, with);
100
101 return target;
102 }
103
104 uint32_t
105 AppendReplacements (const char* original,
106 const char *matching_key,
107 std::vector<ConstString>& equivalents)
108 {
109
110 std::string matching_key_str(matching_key);
111 ConstString original_const(original);
112
113 uint32_t count = 0;
114
115 for (ImplData match = m_impl.FindFirstValueForName(matching_key);
116 match != NULL;
117 match = m_impl.FindNextValueForName(match))
118 {
119 std::string target(original);
120 std::string equiv_class(match->value.AsCString());
121
122 replace (target, matching_key_str, equiv_class);
123
124 ConstString target_const(target.c_str());
125
126// you will most probably want to leave this off since it might make this map grow indefinitely
127#ifdef ENABLE_CPP_EQUIVALENTS_MAP_TO_GROW
128 Add(original_const, target_const);
129#endif
130 equivalents.push_back(target_const);
131
132 count++;
133 }
134
135 return count;
136 }
137
138 typedef UniqueCStringMap<ConstString> Impl;
139 typedef const Impl::Entry* ImplData;
140 Impl m_impl;
141};
142
143static CPPRuntimeEquivalents&
144GetEquivalentsMap ()
145{
146 static CPPRuntimeEquivalents g_equivalents_map;
147 return g_equivalents_map;
148}
149
Jim Ingham642036f2010-09-23 02:01:19 +0000150//----------------------------------------------------------------------
151// Destructor
152//----------------------------------------------------------------------
153CPPLanguageRuntime::~CPPLanguageRuntime()
154{
155}
156
157CPPLanguageRuntime::CPPLanguageRuntime (Process *process) :
158 LanguageRuntime (process)
159{
160
Jim Inghamb66cd072010-09-28 01:25:32 +0000161}
162
163bool
Jim Ingham0de37192011-03-31 23:01:21 +0000164CPPLanguageRuntime::GetObjectDescription (Stream &str, ValueObject &object)
Jim Inghamb66cd072010-09-28 01:25:32 +0000165{
166 // C++ has no generic way to do this.
167 return false;
168}
Jim Ingham324067b2010-09-30 00:54:27 +0000169
170bool
171CPPLanguageRuntime::GetObjectDescription (Stream &str, Value &value, ExecutionContextScope *exe_scope)
172{
173 // C++ has no generic way to do this.
174 return false;
175}
Jim Ingham8b7b2272011-10-07 22:23:45 +0000176
177bool
178CPPLanguageRuntime::IsCPPMangledName (const char *name)
179{
180 // FIXME, we should really run through all the known C++ Language plugins and ask each one if
181 // this is a C++ mangled name, but we can put that off till there is actually more than one
182 // we care about.
183
184 if (name && name[0] == '_' && name[1] == 'Z')
185 return true;
186 else
187 return false;
188}
189
190bool
191CPPLanguageRuntime::StripNamespacesFromVariableName (const char *name, const char *&base_name_start, const char *&base_name_end)
192{
193 if (base_name_end == NULL)
194 base_name_end = name + strlen (name);
195
196 const char *last_colon = NULL;
197 for (const char *ptr = base_name_end; ptr != name; ptr--)
198 {
199 if (*ptr == ':')
200 {
201 last_colon = ptr;
202 break;
203 }
204 }
205
206 if (last_colon == NULL)
207 {
208 base_name_start = name;
209 return true;
210 }
211
212 // Can't have a C++ name that begins with a single ':', nor contains an internal single ':'
213 if (last_colon == name)
214 return false;
215 else if (last_colon[-1] != ':')
216 return false;
217 else
218 {
219 // FIXME: should check if there is
220 base_name_start = last_colon + 1;
221 return true;
222 }
223}
224bool
225CPPLanguageRuntime::IsPossibleCPPCall (const char *name, const char *&base_name_start, const char *&base_name_end)
226{
227 if (!name)
228 return false;
229 // For now, I really can't handle taking template names apart, so if you
230 // have < or > I'll say "could be CPP but leave the base_name empty which
231 // means I couldn't figure out what to use for that.
232 // FIXME: Do I need to do more sanity checking here?
233
234 if (strchr(name, '>') != NULL || strchr (name, '>') != NULL)
235 return true;
236
237 size_t name_len = strlen (name);
238
239 if (name[name_len - 1] == ')')
240 {
241 // We've got arguments.
242 base_name_end = strchr (name, '(');
243 if (base_name_end == NULL)
244 return false;
245
246 // FIXME: should check that this parenthesis isn't a template specialized
247 // on a function type or something gross like that...
248 }
249 else
250 base_name_end = name + strlen (name);
251
252 return StripNamespacesFromVariableName (name, base_name_start, base_name_end);
253}
Enrico Granatabad97532012-02-03 01:41:25 +0000254
255uint32_t
256CPPLanguageRuntime::FindEquivalentNames(ConstString type_name, std::vector<ConstString>& equivalents)
257{
258 uint32_t count = GetEquivalentsMap().FindExactMatches(type_name, equivalents);
259
260 bool might_have_partials=
261 ( count == 0 ) // if we have a full name match just use it
262 && (strchr(type_name.AsCString(), '<') != NULL // we should only have partial matches when templates are involved, check that we have
263 && strchr(type_name.AsCString(), '>') != NULL); // angle brackets in the type_name before trying to scan for partial matches
264
265 if ( might_have_partials )
266 count = GetEquivalentsMap().FindPartialMatches(type_name, equivalents);
267
268 return count;
269}