blob: b020e0af18a516835ac883e69b5a44395ad6604b [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{
Greg Clayton296b06d2013-04-03 02:00:15 +0000193 if (base_name_end == NULL)
194 base_name_end = name + strlen (name);
Jim Ingham8b7b2272011-10-07 22:23:45 +0000195
Greg Clayton296b06d2013-04-03 02:00:15 +0000196 const char *last_colon = NULL;
197 for (const char *ptr = base_name_end; ptr != name; ptr--)
Jim Ingham8b7b2272011-10-07 22:23:45 +0000198 {
Greg Clayton296b06d2013-04-03 02:00:15 +0000199 if (*ptr == ':')
Jim Ingham8b7b2272011-10-07 22:23:45 +0000200 {
Greg Clayton296b06d2013-04-03 02:00:15 +0000201 last_colon = ptr;
202 break;
Jim Ingham8b7b2272011-10-07 22:23:45 +0000203 }
204 }
Greg Clayton296b06d2013-04-03 02:00:15 +0000205
206 if (last_colon == NULL)
Jim Ingham8b7b2272011-10-07 22:23:45 +0000207 {
Greg Clayton296b06d2013-04-03 02:00:15 +0000208 base_name_start = name;
209 return true;
Jim Ingham8b7b2272011-10-07 22:23:45 +0000210 }
Greg Clayton296b06d2013-04-03 02:00:15 +0000211
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
Jim Ingham8b7b2272011-10-07 22:23:45 +0000218 {
Greg Clayton296b06d2013-04-03 02:00:15 +0000219 // FIXME: should check if there is
220 base_name_start = last_colon + 1;
221 return true;
Jim Ingham8b7b2272011-10-07 22:23:45 +0000222 }
223}
224bool
225CPPLanguageRuntime::IsPossibleCPPCall (const char *name, const char *&base_name_start, const char *&base_name_end)
226{
227 if (!name)
Greg Clayton296b06d2013-04-03 02:00:15 +0000228 return false;
Jim Ingham8b7b2272011-10-07 22:23:45 +0000229 // 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?
Greg Clayton296b06d2013-04-03 02:00:15 +0000233
Jim Ingham8b7b2272011-10-07 22:23:45 +0000234 if (strchr(name, '>') != NULL || strchr (name, '>') != NULL)
Greg Clayton296b06d2013-04-03 02:00:15 +0000235 return true;
236
Jim Ingham8b7b2272011-10-07 22:23:45 +0000237 size_t name_len = strlen (name);
Greg Clayton296b06d2013-04-03 02:00:15 +0000238
Jim Ingham8b7b2272011-10-07 22:23:45 +0000239 if (name[name_len - 1] == ')')
240 {
241 // We've got arguments.
242 base_name_end = strchr (name, '(');
243 if (base_name_end == NULL)
Greg Clayton296b06d2013-04-03 02:00:15 +0000244 return false;
245
Jim Ingham8b7b2272011-10-07 22:23:45 +0000246 // 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);
Greg Clayton296b06d2013-04-03 02:00:15 +0000251
Jim Ingham8b7b2272011-10-07 22:23:45 +0000252 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}
Greg Clayton296b06d2013-04-03 02:00:15 +0000270
271void
272CPPLanguageRuntime::MethodName::Clear()
273{
274 m_full.Clear();
275 m_basename.Clear();
276 m_context = llvm::StringRef();
277 m_arguments = llvm::StringRef();
278 m_qualifiers = llvm::StringRef();
279 m_type = eTypeInvalid;
280 m_parsed = false;
281 m_parse_error = false;
282}
283
284bool
285ReverseFindMatchingChars (const llvm::StringRef &s,
286 const llvm::StringRef &left_right_chars,
287 size_t &left_pos,
288 size_t &right_pos,
289 size_t pos = llvm::StringRef::npos)
290{
291 assert (left_right_chars.size() == 2);
292 left_pos = llvm::StringRef::npos;
293 const char left_char = left_right_chars[0];
294 const char right_char = left_right_chars[1];
295 pos = s.find_last_of(left_right_chars, pos);
296 if (pos == llvm::StringRef::npos || s[pos] == left_char)
297 return false;
298 right_pos = pos;
299 uint32_t depth = 1;
300 while (pos > 0 && depth > 0)
301 {
302 pos = s.find_last_of(left_right_chars, pos);
303 if (pos == llvm::StringRef::npos)
304 return false;
305 if (s[pos] == left_char)
306 {
307 if (--depth == 0)
308 {
309 left_pos = pos;
310 return left_pos < right_pos;
311 }
312 }
313 else if (s[pos] == right_char)
314 {
315 ++depth;
316 }
317 }
318 return false;
319}
320
321void
322CPPLanguageRuntime::MethodName::Parse()
323{
324 if (!m_parsed && m_full)
325 {
326// ConstString mangled;
327// m_full.GetMangledCounterpart(mangled);
328// printf ("\n parsing = '%s'\n", m_full.GetCString());
329// if (mangled)
330// printf (" mangled = '%s'\n", mangled.GetCString());
331 m_parse_error = false;
332 m_parsed = true;
333 llvm::StringRef full (m_full.GetCString());
334
335 size_t arg_start, arg_end;
336 llvm::StringRef parens("()", 2);
337 if (ReverseFindMatchingChars (full, parens, arg_start, arg_end))
338 {
339 m_arguments = full.substr(arg_start, arg_end - arg_start + 1);
340 if (arg_end + 1 < full.size())
341 m_qualifiers = full.substr(arg_end + 1);
342 if (arg_start > 0)
343 {
344 size_t basename_end = arg_start;
345 size_t context_end = llvm::StringRef::npos;
346 if (basename_end > 0 && full[basename_end-1] == '>')
347 {
348 // TODO: handle template junk...
349 // Templated function
350 size_t template_start, template_end;
351 llvm::StringRef lt_gt("<>", 2);
352 if (ReverseFindMatchingChars (full, lt_gt, template_start, template_end, basename_end))
353 context_end = full.rfind(':', template_start);
354 }
355 if (context_end == llvm::StringRef::npos)
356 context_end = full.rfind(':', basename_end);
357
358 if (context_end == llvm::StringRef::npos)
359 m_basename.SetString(full.substr(0, basename_end));
360 else
361 {
362 m_context = full.substr(0, context_end - 1);
363 const size_t basename_begin = context_end + 1;
364 m_basename.SetString(full.substr(basename_begin, basename_end - basename_begin));
365 }
366 m_type = eTypeUnknownMethod;
367 }
368 else
369 {
370 m_parse_error = true;
371 return;
372 }
373
374// if (!m_context.empty())
375// printf (" context = '%s'\n", m_context.str().c_str());
376// if (m_basename)
377// printf (" basename = '%s'\n", m_basename.GetCString());
378// if (!m_arguments.empty())
379// printf (" arguments = '%s'\n", m_arguments.str().c_str());
380// if (!m_qualifiers.empty())
381// printf ("qualifiers = '%s'\n", m_qualifiers.str().c_str());
382 }
383 else
384 {
385 m_parse_error = true;
386// printf ("error: didn't find matching parens for arguments\n");
387 }
388 }
389}
390
391const ConstString &
392CPPLanguageRuntime::MethodName::GetBasename ()
393{
394 if (!m_parsed)
395 Parse();
396 return m_basename;
397}
398
399llvm::StringRef
400CPPLanguageRuntime::MethodName::GetContext ()
401{
402 if (!m_parsed)
403 Parse();
404 return m_context;
405}
406
407llvm::StringRef
408CPPLanguageRuntime::MethodName::GetArguments ()
409{
410 if (!m_parsed)
411 Parse();
412 return m_arguments;
413}
414
415llvm::StringRef
416CPPLanguageRuntime::MethodName::GetQualifiers ()
417{
418 if (!m_parsed)
419 Parse();
420 return m_qualifiers;
421}
422