]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Target/CPPLanguageRuntime.cpp
Upgrade to Unbound 1.5.7.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Target / CPPLanguageRuntime.cpp
1 //===-- 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"
11
12 #include <string.h>
13
14 #include "llvm/ADT/StringRef.h"
15
16 #include "lldb/Core/PluginManager.h"
17 #include "lldb/Core/UniqueCStringMap.h"
18 #include "lldb/Target/ExecutionContext.h"
19
20 using namespace lldb;
21 using namespace lldb_private;
22
23 class CPPRuntimeEquivalents
24 {
25 public:
26     CPPRuntimeEquivalents ()
27     {
28         
29         m_impl.Append(ConstString("std::basic_string<char, std::char_traits<char>, std::allocator<char> >").AsCString(), ConstString("basic_string<char>"));
30
31         // these two (with a prefixed std::) occur when c++stdlib string class occurs as a template argument in some STL container
32         m_impl.Append(ConstString("std::basic_string<char, std::char_traits<char>, std::allocator<char> >").AsCString(), ConstString("std::basic_string<char>"));
33         
34         m_impl.Sort();
35     }
36     
37     void
38     Add (ConstString& type_name,
39          ConstString& type_equivalent)
40     {
41         m_impl.Insert(type_name.AsCString(), type_equivalent);
42     }
43     
44     uint32_t
45     FindExactMatches (ConstString& type_name,
46                       std::vector<ConstString>& equivalents)
47     {
48         
49         uint32_t count = 0;
50
51         for (ImplData match = m_impl.FindFirstValueForName(type_name.AsCString());
52              match != NULL;
53              match = m_impl.FindNextValueForName(match))
54         {
55             equivalents.push_back(match->value);
56             count++;
57         }
58
59         return count;        
60     }
61     
62     // partial matches can occur when a name with equivalents is a template argument.
63     // e.g. we may have "class Foo" be a match for "struct Bar". if we have a typename
64     // such as "class Templatized<class Foo, Anything>" we want this to be replaced with
65     // "class Templatized<struct Bar, Anything>". Since partial matching is time consuming
66     // once we get a partial match, we add it to the exact matches list for faster retrieval
67     uint32_t
68     FindPartialMatches (ConstString& type_name,
69                         std::vector<ConstString>& equivalents)
70     {
71         
72         uint32_t count = 0;
73         
74         const char* type_name_cstr = type_name.AsCString();
75         
76         size_t items_count = m_impl.GetSize();
77         
78         for (size_t item = 0; item < items_count; item++)
79         {
80             const char* key_cstr = m_impl.GetCStringAtIndex(item);
81             if ( strstr(type_name_cstr,key_cstr) )
82             {
83                 count += AppendReplacements(type_name_cstr,
84                                             key_cstr,
85                                             equivalents);
86             }
87         }
88         
89         return count;
90         
91     }
92     
93 private:
94     
95     std::string& replace (std::string& target,
96                           std::string& pattern,
97                           std::string& with)
98     {
99         size_t pos;
100         size_t pattern_len = pattern.size();
101         
102         while ( (pos = target.find(pattern)) != std::string::npos )
103             target.replace(pos, pattern_len, with);
104         
105         return target;
106     }
107     
108     uint32_t
109     AppendReplacements (const char* original,
110                         const char *matching_key,
111                         std::vector<ConstString>& equivalents)
112     {
113         
114         std::string matching_key_str(matching_key);
115         ConstString original_const(original);
116         
117         uint32_t count = 0;
118         
119         for (ImplData match = m_impl.FindFirstValueForName(matching_key);
120              match != NULL;
121              match = m_impl.FindNextValueForName(match))
122         {
123             std::string target(original);
124             std::string equiv_class(match->value.AsCString());
125             
126             replace (target, matching_key_str, equiv_class);
127             
128             ConstString target_const(target.c_str());
129
130 // you will most probably want to leave this off since it might make this map grow indefinitely
131 #ifdef ENABLE_CPP_EQUIVALENTS_MAP_TO_GROW
132             Add(original_const, target_const);
133 #endif
134             equivalents.push_back(target_const);
135             
136             count++;
137         }
138         
139         return count;
140     }
141     
142     typedef UniqueCStringMap<ConstString> Impl;
143     typedef const Impl::Entry* ImplData;
144     Impl m_impl;
145 };
146
147 static CPPRuntimeEquivalents&
148 GetEquivalentsMap ()
149 {
150     static CPPRuntimeEquivalents g_equivalents_map;
151     return g_equivalents_map;
152 }
153
154 //----------------------------------------------------------------------
155 // Destructor
156 //----------------------------------------------------------------------
157 CPPLanguageRuntime::~CPPLanguageRuntime()
158 {
159 }
160
161 CPPLanguageRuntime::CPPLanguageRuntime (Process *process) :
162     LanguageRuntime (process)
163 {
164
165 }
166
167 bool
168 CPPLanguageRuntime::GetObjectDescription (Stream &str, ValueObject &object)
169 {
170     // C++ has no generic way to do this.
171     return false;
172 }
173
174 bool
175 CPPLanguageRuntime::GetObjectDescription (Stream &str, Value &value, ExecutionContextScope *exe_scope)
176 {
177     // C++ has no generic way to do this.
178     return false;
179 }
180
181 bool
182 CPPLanguageRuntime::IsCPPMangledName (const char *name)
183 {
184     // FIXME, we should really run through all the known C++ Language plugins and ask each one if
185     // this is a C++ mangled name, but we can put that off till there is actually more than one
186     // we care about.
187     
188     if (name && name[0] == '_' && name[1] == 'Z')
189         return true;
190     else
191         return false;
192 }
193
194 bool
195 CPPLanguageRuntime::ExtractContextAndIdentifier (const char *name, llvm::StringRef &context, llvm::StringRef &identifier)
196 {
197     static RegularExpression g_basename_regex("^(([A-Za-z_][A-Za-z_0-9]*::)*)([A-Za-z_][A-Za-z_0-9]*)$");
198     RegularExpression::Match match(4);
199     if (g_basename_regex.Execute (name, &match))
200     {
201         match.GetMatchAtIndex(name, 1, context);
202         match.GetMatchAtIndex(name, 3, identifier);
203         return true;
204     }
205     return false;
206 }
207
208 uint32_t
209 CPPLanguageRuntime::FindEquivalentNames(ConstString type_name, std::vector<ConstString>& equivalents)
210 {
211     uint32_t count = GetEquivalentsMap().FindExactMatches(type_name, equivalents);
212
213     bool might_have_partials= 
214         ( count == 0 )  // if we have a full name match just use it
215         && (strchr(type_name.AsCString(), '<') != NULL  // we should only have partial matches when templates are involved, check that we have
216             && strchr(type_name.AsCString(), '>') != NULL); // angle brackets in the type_name before trying to scan for partial matches
217     
218     if ( might_have_partials )
219         count = GetEquivalentsMap().FindPartialMatches(type_name, equivalents);
220     
221     return count;
222 }
223
224 void
225 CPPLanguageRuntime::MethodName::Clear()
226 {
227     m_full.Clear();
228     m_basename = llvm::StringRef();
229     m_context = llvm::StringRef();
230     m_arguments = llvm::StringRef();
231     m_qualifiers = llvm::StringRef();
232     m_type = eTypeInvalid;
233     m_parsed = false;
234     m_parse_error = false;
235 }
236
237 bool
238 ReverseFindMatchingChars (const llvm::StringRef &s,
239                           const llvm::StringRef &left_right_chars,
240                           size_t &left_pos,
241                           size_t &right_pos,
242                           size_t pos = llvm::StringRef::npos)
243 {
244     assert (left_right_chars.size() == 2);
245     left_pos = llvm::StringRef::npos;
246     const char left_char = left_right_chars[0];
247     const char right_char = left_right_chars[1];
248     pos = s.find_last_of(left_right_chars, pos);
249     if (pos == llvm::StringRef::npos || s[pos] == left_char)
250         return false;
251     right_pos = pos;
252     uint32_t depth = 1;
253     while (pos > 0 && depth > 0)
254     {
255         pos = s.find_last_of(left_right_chars, pos);
256         if (pos == llvm::StringRef::npos)
257             return false;
258         if (s[pos] == left_char)
259         {
260             if (--depth == 0)
261             {
262                 left_pos = pos;
263                 return left_pos < right_pos;
264             }            
265         }
266         else if (s[pos] == right_char)
267         {
268             ++depth;
269         }
270     }
271     return false;
272 }
273
274
275 void
276 CPPLanguageRuntime::MethodName::Parse()
277 {
278     if (!m_parsed && m_full)
279     {
280 //        ConstString mangled;
281 //        m_full.GetMangledCounterpart(mangled);
282 //        printf ("\n   parsing = '%s'\n", m_full.GetCString());
283 //        if (mangled)
284 //            printf ("   mangled = '%s'\n", mangled.GetCString());
285         m_parse_error = false;
286         m_parsed = true;
287         llvm::StringRef full (m_full.GetCString());
288         
289         size_t arg_start, arg_end;
290         llvm::StringRef parens("()", 2);
291         if (ReverseFindMatchingChars (full, parens, arg_start, arg_end))
292         {
293             m_arguments = full.substr(arg_start, arg_end - arg_start + 1);
294             if (arg_end + 1 < full.size())
295                 m_qualifiers = full.substr(arg_end + 1);
296             if (arg_start > 0)
297             {
298                 size_t basename_end = arg_start;
299                 size_t context_start = 0;
300                 size_t context_end = llvm::StringRef::npos;
301                 if (basename_end > 0 && full[basename_end-1] == '>')
302                 {
303                     // TODO: handle template junk...
304                     // Templated function
305                     size_t template_start, template_end;
306                     llvm::StringRef lt_gt("<>", 2);
307                     if (ReverseFindMatchingChars (full, lt_gt, template_start, template_end, basename_end))
308                     {
309                         context_end = full.rfind(':', template_start);
310                         if (context_end == llvm::StringRef::npos)
311                         {
312                             // Check for templated functions that include return type like:
313                             // 'void foo<Int>()'
314                             context_end = full.rfind(' ', template_start);
315                             if (context_end != llvm::StringRef::npos)
316                             {
317                                 context_start = context_end;
318                             }
319                         }
320                     }
321                     else
322                     {
323                         context_end = full.rfind(':', basename_end);
324                     }
325                 }
326                 else if (context_end == llvm::StringRef::npos)
327                 {
328                     context_end = full.rfind(':', basename_end);
329                 }
330
331                 if (context_end == llvm::StringRef::npos)
332                     m_basename = full.substr(0, basename_end);
333                 else
334                 {
335                     if (context_start < context_end)
336                         m_context = full.substr(context_start, context_end - 1);
337                     const size_t basename_begin = context_end + 1;
338                     m_basename = full.substr(basename_begin, basename_end - basename_begin);
339                 }
340                 m_type = eTypeUnknownMethod;
341             }
342             else
343             {
344                 m_parse_error = true;
345                 return;
346             }
347         
348 //            if (!m_context.empty())
349 //                printf ("   context = '%s'\n", m_context.str().c_str());
350 //            if (m_basename)
351 //                printf ("  basename = '%s'\n", m_basename.GetCString());
352 //            if (!m_arguments.empty())
353 //                printf (" arguments = '%s'\n", m_arguments.str().c_str());
354 //            if (!m_qualifiers.empty())
355 //                printf ("qualifiers = '%s'\n", m_qualifiers.str().c_str());
356
357             // Make sure we have a valid C++ basename with optional template args
358             static RegularExpression g_identifier_regex("^~?([A-Za-z_][A-Za-z_0-9]*)(<.*>)?$");
359             std::string basename_str(m_basename.str());
360             bool basename_is_valid = g_identifier_regex.Execute (basename_str.c_str(), NULL);
361             if (!basename_is_valid)
362             {
363                 // Check for C++ operators
364                 if (m_basename.startswith("operator"))
365                 {
366                     static RegularExpression g_operator_regex("^(operator)( ?)([A-Za-z_][A-Za-z_0-9]*|\\(\\)|\\[\\]|[\\^<>=!\\/*+-]+)(<.*>)?(\\[\\])?$");
367                     basename_is_valid = g_operator_regex.Execute(basename_str.c_str(), NULL);
368                 }
369             }
370             if (!basename_is_valid)
371             {
372                 // The C++ basename doesn't match our regular expressions so this can't
373                 // be a valid C++ method, clear everything out and indicate an error
374                 m_context = llvm::StringRef();
375                 m_basename = llvm::StringRef();
376                 m_arguments = llvm::StringRef();
377                 m_qualifiers = llvm::StringRef();
378                 m_parse_error = true;
379             }
380         }
381         else
382         {
383             m_parse_error = true;
384 //            printf ("error: didn't find matching parens for arguments\n");
385         }
386     }
387 }
388
389 llvm::StringRef
390 CPPLanguageRuntime::MethodName::GetBasename ()
391 {
392     if (!m_parsed)
393         Parse();
394     return m_basename;
395 }
396
397 llvm::StringRef
398 CPPLanguageRuntime::MethodName::GetContext ()
399 {
400     if (!m_parsed)
401         Parse();
402     return m_context;
403 }
404
405 llvm::StringRef
406 CPPLanguageRuntime::MethodName::GetArguments ()
407 {
408     if (!m_parsed)
409         Parse();
410     return m_arguments;
411 }
412
413 llvm::StringRef
414 CPPLanguageRuntime::MethodName::GetQualifiers ()
415 {
416     if (!m_parsed)
417         Parse();
418     return m_qualifiers;
419 }
420