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