Greg Clayton | 05e8d19 | 2012-02-01 01:46:19 +0000 | [diff] [blame] | 1 | //===-- ObjCLanguageRuntime.cpp ---------------------------------*- C++ -*-===// |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 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 Ingham | 6c68fb4 | 2010-09-30 00:54:27 +0000 | [diff] [blame] | 9 | #include "clang/AST/Type.h" |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 10 | |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 11 | #include "lldb/Core/Log.h" |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 12 | #include "lldb/Core/PluginManager.h" |
Jim Ingham | 6c68fb4 | 2010-09-30 00:54:27 +0000 | [diff] [blame] | 13 | #include "lldb/Core/ValueObject.h" |
| 14 | #include "lldb/Symbol/ClangASTContext.h" |
Jim Ingham | 61be090 | 2011-05-02 18:13:59 +0000 | [diff] [blame] | 15 | #include "lldb/Symbol/Type.h" |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 16 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Sean Callanan | 7277284 | 2012-02-22 23:57:45 +0000 | [diff] [blame^] | 17 | #include "lldb/Target/Target.h" |
Jim Ingham | 2277701 | 2010-09-23 02:01:19 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace lldb; |
| 20 | using namespace lldb_private; |
| 21 | |
| 22 | //---------------------------------------------------------------------- |
| 23 | // Destructor |
| 24 | //---------------------------------------------------------------------- |
| 25 | ObjCLanguageRuntime::~ObjCLanguageRuntime() |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | ObjCLanguageRuntime::ObjCLanguageRuntime (Process *process) : |
| 30 | LanguageRuntime (process) |
| 31 | { |
| 32 | |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void |
| 36 | ObjCLanguageRuntime::AddToMethodCache (lldb::addr_t class_addr, lldb::addr_t selector, lldb::addr_t impl_addr) |
| 37 | { |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 38 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 5a36912 | 2010-09-28 01:25:32 +0000 | [diff] [blame] | 39 | if (log) |
| 40 | { |
| 41 | log->Printf ("Caching: class 0x%llx selector 0x%llx implementation 0x%llx.", class_addr, selector, impl_addr); |
| 42 | } |
| 43 | m_impl_cache.insert (std::pair<ClassAndSel,lldb::addr_t> (ClassAndSel(class_addr, selector), impl_addr)); |
| 44 | } |
| 45 | |
| 46 | lldb::addr_t |
| 47 | ObjCLanguageRuntime::LookupInMethodCache (lldb::addr_t class_addr, lldb::addr_t selector) |
| 48 | { |
| 49 | MsgImplMap::iterator pos, end = m_impl_cache.end(); |
| 50 | pos = m_impl_cache.find (ClassAndSel(class_addr, selector)); |
| 51 | if (pos != end) |
| 52 | return (*pos).second; |
| 53 | return LLDB_INVALID_ADDRESS; |
| 54 | } |
Jim Ingham | 61be090 | 2011-05-02 18:13:59 +0000 | [diff] [blame] | 55 | |
| 56 | void |
| 57 | ObjCLanguageRuntime::AddToClassNameCache (lldb::addr_t class_addr, const char *name, lldb::TypeSP type_sp) |
| 58 | { |
| 59 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
| 60 | if (log) |
| 61 | { |
| 62 | log->Printf ("Caching: class 0x%llx name: %s.", class_addr, name); |
| 63 | } |
| 64 | |
| 65 | TypeAndOrName class_type_or_name; |
| 66 | |
Greg Clayton | 85ae2e1 | 2011-10-18 23:36:41 +0000 | [diff] [blame] | 67 | if (type_sp) |
Jim Ingham | 61be090 | 2011-05-02 18:13:59 +0000 | [diff] [blame] | 68 | class_type_or_name.SetTypeSP (type_sp); |
| 69 | else if (name && *name != '\0') |
| 70 | class_type_or_name.SetName (name); |
| 71 | else |
| 72 | return; |
| 73 | m_class_name_cache.insert (std::pair<lldb::addr_t,TypeAndOrName> (class_addr, class_type_or_name)); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | ObjCLanguageRuntime::AddToClassNameCache (lldb::addr_t class_addr, const TypeAndOrName &class_type_or_name) |
| 78 | { |
| 79 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
| 80 | if (log) |
| 81 | { |
| 82 | log->Printf ("Caching: class 0x%llx name: %s.", class_addr, class_type_or_name.GetName().AsCString()); |
| 83 | } |
| 84 | |
| 85 | m_class_name_cache.insert (std::pair<lldb::addr_t,TypeAndOrName> (class_addr, class_type_or_name)); |
| 86 | } |
| 87 | |
| 88 | TypeAndOrName |
| 89 | ObjCLanguageRuntime::LookupInClassNameCache (lldb::addr_t class_addr) |
| 90 | { |
| 91 | ClassNameMap::iterator pos, end = m_class_name_cache.end(); |
| 92 | pos = m_class_name_cache.find (class_addr); |
| 93 | if (pos != end) |
| 94 | return (*pos).second; |
| 95 | return TypeAndOrName (); |
| 96 | } |
Jim Ingham | d555bac | 2011-06-24 22:03:24 +0000 | [diff] [blame] | 97 | |
Sean Callanan | 7277284 | 2012-02-22 23:57:45 +0000 | [diff] [blame^] | 98 | lldb::TypeSP |
| 99 | ObjCLanguageRuntime::LookupInCompleteClassCache (ConstString &name) |
| 100 | { |
| 101 | CompleteClassMap::iterator complete_class_iter = m_complete_class_cache.find(name); |
| 102 | |
| 103 | if (complete_class_iter != m_complete_class_cache.end()) |
| 104 | { |
| 105 | TypeSP ret(complete_class_iter->second); |
| 106 | |
| 107 | if (!ret) |
| 108 | m_complete_class_cache.erase(name); |
| 109 | else |
| 110 | return TypeSP(complete_class_iter->second); |
| 111 | } |
| 112 | |
| 113 | ModuleList &modules = m_process->GetTarget().GetImages(); |
| 114 | |
| 115 | SymbolContextList sc_list; |
| 116 | |
| 117 | modules.FindSymbolsWithNameAndType(name, eSymbolTypeObjCClass, sc_list); |
| 118 | |
| 119 | if (sc_list.GetSize() == 0) |
| 120 | return TypeSP(); |
| 121 | |
| 122 | SymbolContext sc; |
| 123 | |
| 124 | sc_list.GetContextAtIndex(0, sc); |
| 125 | |
| 126 | ModuleSP module_sp(sc.module_sp); |
| 127 | |
| 128 | if (!module_sp) |
| 129 | return TypeSP(); |
| 130 | |
| 131 | const SymbolContext null_sc; |
| 132 | const ClangNamespaceDecl *null_namespace_decl = NULL; |
| 133 | const bool append = false; |
| 134 | const uint32_t max_matches = UINT32_MAX; |
| 135 | TypeList types; |
| 136 | |
| 137 | module_sp->FindTypes(null_sc, |
| 138 | name, |
| 139 | null_namespace_decl, |
| 140 | append, |
| 141 | max_matches, |
| 142 | types); |
| 143 | |
| 144 | if (types.GetSize() == 1) |
| 145 | { |
| 146 | TypeSP candidate_type = types.GetTypeAtIndex(0); |
| 147 | |
| 148 | if (ClangASTContext::IsObjCClassType(candidate_type->GetClangForwardType())) |
| 149 | { |
| 150 | m_complete_class_cache[name] = TypeWP(candidate_type); |
| 151 | return candidate_type; |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | return TypeSP(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | for (uint32_t ti = 0, te = types.GetSize(); |
| 160 | ti < te; |
| 161 | ++ti) |
| 162 | { |
| 163 | TypeSP candidate_type = types.GetTypeAtIndex(ti); |
| 164 | |
| 165 | if (candidate_type->IsCompleteObjCClass() && |
| 166 | ClangASTContext::IsObjCClassType(candidate_type->GetClangForwardType())) |
| 167 | { |
| 168 | m_complete_class_cache[name] = TypeWP(candidate_type); |
| 169 | return candidate_type; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return TypeSP(); |
| 174 | } |
| 175 | |
Jim Ingham | d555bac | 2011-06-24 22:03:24 +0000 | [diff] [blame] | 176 | size_t |
| 177 | ObjCLanguageRuntime::GetByteOffsetForIvar (ClangASTType &parent_qual_type, const char *ivar_name) |
| 178 | { |
| 179 | return LLDB_INVALID_IVAR_OFFSET; |
| 180 | } |
| 181 | |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 182 | |
Greg Clayton | e42ae84 | 2012-01-19 03:24:53 +0000 | [diff] [blame] | 183 | uint32_t |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 184 | ObjCLanguageRuntime::ParseMethodName (const char *name, |
Greg Clayton | 278a16b | 2012-01-19 00:52:59 +0000 | [diff] [blame] | 185 | ConstString *class_name, // Class name (with category if any) |
| 186 | ConstString *selector_name, // selector on its own |
| 187 | ConstString *name_sans_category, // Full function prototype with no category |
| 188 | ConstString *class_name_sans_category)// Class name with no category (or empty if no category as answer will be in "class_name" |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 189 | { |
Greg Clayton | 278a16b | 2012-01-19 00:52:59 +0000 | [diff] [blame] | 190 | if (class_name) |
| 191 | class_name->Clear(); |
| 192 | if (selector_name) |
| 193 | selector_name->Clear(); |
| 194 | if (name_sans_category) |
| 195 | name_sans_category->Clear(); |
| 196 | if (class_name_sans_category) |
| 197 | class_name_sans_category->Clear(); |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 198 | |
Greg Clayton | e42ae84 | 2012-01-19 03:24:53 +0000 | [diff] [blame] | 199 | uint32_t result = 0; |
| 200 | |
Jim Ingham | b7f6b2f | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 201 | if (IsPossibleObjCMethodName (name)) |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 202 | { |
| 203 | int name_len = strlen (name); |
| 204 | // Objective C methods must have at least: |
| 205 | // "-[" or "+[" prefix |
| 206 | // One character for a class name |
| 207 | // One character for the space between the class name |
| 208 | // One character for the method name |
| 209 | // "]" suffix |
| 210 | if (name_len >= 6 && name[name_len - 1] == ']') |
| 211 | { |
Greg Clayton | 278a16b | 2012-01-19 00:52:59 +0000 | [diff] [blame] | 212 | const char *selector_name_ptr = strchr (name, ' '); |
Greg Clayton | 7f99513 | 2011-10-04 22:41:51 +0000 | [diff] [blame] | 213 | if (selector_name_ptr) |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 214 | { |
| 215 | if (class_name) |
Greg Clayton | e42ae84 | 2012-01-19 03:24:53 +0000 | [diff] [blame] | 216 | { |
Greg Clayton | 7f99513 | 2011-10-04 22:41:51 +0000 | [diff] [blame] | 217 | class_name->SetCStringWithLength (name + 2, selector_name_ptr - name - 2); |
Greg Clayton | e42ae84 | 2012-01-19 03:24:53 +0000 | [diff] [blame] | 218 | ++result; |
| 219 | } |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 220 | |
| 221 | // Skip the space |
Greg Clayton | 7f99513 | 2011-10-04 22:41:51 +0000 | [diff] [blame] | 222 | ++selector_name_ptr; |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 223 | // Extract the objective C basename and add it to the |
| 224 | // accelerator tables |
Greg Clayton | 7f99513 | 2011-10-04 22:41:51 +0000 | [diff] [blame] | 225 | size_t selector_name_len = name_len - (selector_name_ptr - name) - 1; |
| 226 | if (selector_name) |
Greg Clayton | e42ae84 | 2012-01-19 03:24:53 +0000 | [diff] [blame] | 227 | { |
Greg Clayton | 7f99513 | 2011-10-04 22:41:51 +0000 | [diff] [blame] | 228 | selector_name->SetCStringWithLength (selector_name_ptr, selector_name_len); |
Greg Clayton | e42ae84 | 2012-01-19 03:24:53 +0000 | [diff] [blame] | 229 | ++result; |
| 230 | } |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 231 | |
| 232 | // Also see if this is a "category" on our class. If so strip off the category name, |
| 233 | // and add the class name without it to the basename table. |
| 234 | |
Greg Clayton | 278a16b | 2012-01-19 00:52:59 +0000 | [diff] [blame] | 235 | if (name_sans_category || class_name_sans_category) |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 236 | { |
Greg Clayton | 278a16b | 2012-01-19 00:52:59 +0000 | [diff] [blame] | 237 | const char *open_paren = strchr (name, '('); |
Greg Clayton | 278a16b | 2012-01-19 00:52:59 +0000 | [diff] [blame] | 238 | if (open_paren) |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 239 | { |
Greg Clayton | 278a16b | 2012-01-19 00:52:59 +0000 | [diff] [blame] | 240 | if (class_name_sans_category) |
Greg Clayton | e42ae84 | 2012-01-19 03:24:53 +0000 | [diff] [blame] | 241 | { |
Greg Clayton | 278a16b | 2012-01-19 00:52:59 +0000 | [diff] [blame] | 242 | class_name_sans_category->SetCStringWithLength (name + 2, open_paren - name - 2); |
Greg Clayton | e42ae84 | 2012-01-19 03:24:53 +0000 | [diff] [blame] | 243 | ++result; |
| 244 | } |
Sean Callanan | b811e4b | 2012-01-19 01:10:27 +0000 | [diff] [blame] | 245 | |
| 246 | if (name_sans_category) |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 247 | { |
Greg Clayton | e42ae84 | 2012-01-19 03:24:53 +0000 | [diff] [blame] | 248 | const char *close_paren = strchr (open_paren, ')'); |
| 249 | if (open_paren < close_paren) |
Sean Callanan | b811e4b | 2012-01-19 01:10:27 +0000 | [diff] [blame] | 250 | { |
| 251 | std::string buffer (name, open_paren - name); |
| 252 | buffer.append (close_paren + 1); |
| 253 | name_sans_category->SetCString (buffer.c_str()); |
Greg Clayton | e42ae84 | 2012-01-19 03:24:53 +0000 | [diff] [blame] | 254 | ++result; |
Sean Callanan | b811e4b | 2012-01-19 01:10:27 +0000 | [diff] [blame] | 255 | } |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 260 | } |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 261 | } |
Greg Clayton | e42ae84 | 2012-01-19 03:24:53 +0000 | [diff] [blame] | 262 | return result; |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 263 | } |