blob: 4326910f1cd512e2b10d67ec6348f5642ff6b0e1 [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//===----------------------------------------------------------------------===//
Jim Ingham324067b2010-09-30 00:54:27 +00009#include "clang/AST/Type.h"
Jim Ingham642036f2010-09-23 02:01:19 +000010
Jim Inghamb66cd072010-09-28 01:25:32 +000011#include "lldb/Core/Log.h"
Jim Ingham642036f2010-09-23 02:01:19 +000012#include "lldb/Core/PluginManager.h"
Jim Ingham324067b2010-09-30 00:54:27 +000013#include "lldb/Core/ValueObject.h"
14#include "lldb/Symbol/ClangASTContext.h"
Jim Inghamef80aab2011-05-02 18:13:59 +000015#include "lldb/Symbol/Type.h"
Jim Inghamb66cd072010-09-28 01:25:32 +000016#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham642036f2010-09-23 02:01:19 +000017
18using namespace lldb;
19using namespace lldb_private;
20
21//----------------------------------------------------------------------
22// Destructor
23//----------------------------------------------------------------------
24ObjCLanguageRuntime::~ObjCLanguageRuntime()
25{
26}
27
28ObjCLanguageRuntime::ObjCLanguageRuntime (Process *process) :
29 LanguageRuntime (process)
30{
31
Jim Inghamb66cd072010-09-28 01:25:32 +000032}
33
34void
35ObjCLanguageRuntime::AddToMethodCache (lldb::addr_t class_addr, lldb::addr_t selector, lldb::addr_t impl_addr)
36{
Greg Claytone005f2c2010-11-06 01:53:30 +000037 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamb66cd072010-09-28 01:25:32 +000038 if (log)
39 {
40 log->Printf ("Caching: class 0x%llx selector 0x%llx implementation 0x%llx.", class_addr, selector, impl_addr);
41 }
42 m_impl_cache.insert (std::pair<ClassAndSel,lldb::addr_t> (ClassAndSel(class_addr, selector), impl_addr));
43}
44
45lldb::addr_t
46ObjCLanguageRuntime::LookupInMethodCache (lldb::addr_t class_addr, lldb::addr_t selector)
47{
48 MsgImplMap::iterator pos, end = m_impl_cache.end();
49 pos = m_impl_cache.find (ClassAndSel(class_addr, selector));
50 if (pos != end)
51 return (*pos).second;
52 return LLDB_INVALID_ADDRESS;
53}
Jim Inghamef80aab2011-05-02 18:13:59 +000054
55void
56ObjCLanguageRuntime::AddToClassNameCache (lldb::addr_t class_addr, const char *name, lldb::TypeSP type_sp)
57{
58 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
59 if (log)
60 {
61 log->Printf ("Caching: class 0x%llx name: %s.", class_addr, name);
62 }
63
64 TypeAndOrName class_type_or_name;
65
66 if (type_sp != NULL)
67 class_type_or_name.SetTypeSP (type_sp);
68 else if (name && *name != '\0')
69 class_type_or_name.SetName (name);
70 else
71 return;
72 m_class_name_cache.insert (std::pair<lldb::addr_t,TypeAndOrName> (class_addr, class_type_or_name));
73}
74
75void
76ObjCLanguageRuntime::AddToClassNameCache (lldb::addr_t class_addr, const TypeAndOrName &class_type_or_name)
77{
78 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
79 if (log)
80 {
81 log->Printf ("Caching: class 0x%llx name: %s.", class_addr, class_type_or_name.GetName().AsCString());
82 }
83
84 m_class_name_cache.insert (std::pair<lldb::addr_t,TypeAndOrName> (class_addr, class_type_or_name));
85}
86
87TypeAndOrName
88ObjCLanguageRuntime::LookupInClassNameCache (lldb::addr_t class_addr)
89{
90 ClassNameMap::iterator pos, end = m_class_name_cache.end();
91 pos = m_class_name_cache.find (class_addr);
92 if (pos != end)
93 return (*pos).second;
94 return TypeAndOrName ();
95}
Jim Ingham58513662011-06-24 22:03:24 +000096
97size_t
98ObjCLanguageRuntime::GetByteOffsetForIvar (ClangASTType &parent_qual_type, const char *ivar_name)
99{
100 return LLDB_INVALID_IVAR_OFFSET;
101}
102
Jim Ingham3ad4da02011-08-15 01:32:22 +0000103
104bool
105ObjCLanguageRuntime::ParseMethodName (const char *name,
106 ConstString *class_name,
107 ConstString *method_name,
108 ConstString *base_name)
109{
110 if (class_name) { class_name->Clear(); }
111 if (method_name) { method_name->Clear(); }
112 if (base_name) { base_name->Clear(); }
113
114 if (name && (name[0] == '-' || name[0] == '+') && name[1] == '[')
115 {
116 int name_len = strlen (name);
117 // Objective C methods must have at least:
118 // "-[" or "+[" prefix
119 // One character for a class name
120 // One character for the space between the class name
121 // One character for the method name
122 // "]" suffix
123 if (name_len >= 6 && name[name_len - 1] == ']')
124 {
125 const char *method_name_ptr;
126 method_name_ptr = strchr (name, ' ');
127 if (method_name_ptr)
128 {
129 if (class_name)
130 class_name->SetCStringWithLength (name + 2, method_name_ptr - name - 2);
131
132 // Skip the space
133 ++method_name_ptr;
134 // Extract the objective C basename and add it to the
135 // accelerator tables
136 size_t method_name_len = name_len - (method_name_ptr - name) - 1;
137 if (method_name)
138 method_name->SetCStringWithLength (method_name_ptr, method_name_len);
139
140 // Also see if this is a "category" on our class. If so strip off the category name,
141 // and add the class name without it to the basename table.
142
143 if (base_name)
144 {
145 const char *first_paren = (char *) memchr (name, '(', method_name_ptr - name);
146 if (first_paren)
147 {
148 const char *second_paren = (char *) memchr (first_paren, ')', method_name_ptr - first_paren);
149 if (second_paren)
150 {
151 std::string buffer (name, first_paren - name);
152 buffer.append (second_paren + 1);
153 base_name->SetCString (buffer.c_str());
154
155 }
156 }
157 }
158 }
159 return true;
160 }
161 return false;
162 }
163 else
164 return false;
165}