Greg Clayton | 1e591ce | 2010-07-16 18:28:27 +0000 | [diff] [blame] | 1 | //===-- ClangASTSource.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 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 10 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTContext.h" |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 12 | #include "clang/AST/RecordLayout.h" |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Log.h" |
Greg Clayton | 6e0101c | 2011-09-17 06:21:20 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Module.h" |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 15 | #include "lldb/Core/ModuleList.h" |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 16 | #include "lldb/Expression/ASTDumper.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Expression/ClangASTSource.h" |
| 18 | #include "lldb/Expression/ClangExpression.h" |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 19 | #include "lldb/Symbol/ClangNamespaceDecl.h" |
Greg Clayton | 49ce896 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 20 | #include "lldb/Symbol/Function.h" |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 21 | #include "lldb/Symbol/SymbolVendor.h" |
Sean Callanan | 673f3db | 2011-11-30 22:11:59 +0000 | [diff] [blame] | 22 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 23 | #include "lldb/Target/Target.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | using namespace lldb_private; |
| 27 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 28 | ClangASTSource::~ClangASTSource() |
| 29 | { |
Sean Callanan | a3d0447 | 2011-11-29 00:42:02 +0000 | [diff] [blame] | 30 | m_ast_importer->ForgetDestination(m_ast_context); |
| 31 | |
Johnny Chen | fa21ffd | 2011-11-30 23:18:53 +0000 | [diff] [blame] | 32 | // We are in the process of destruction, don't create clang ast context on demand |
| 33 | // by passing false to Target::GetScratchClangASTContext(create_on_demand). |
| 34 | ClangASTContext *scratch_clang_ast_context = m_target->GetScratchClangASTContext(false); |
Sean Callanan | a3d0447 | 2011-11-29 00:42:02 +0000 | [diff] [blame] | 35 | |
| 36 | if (!scratch_clang_ast_context) |
| 37 | return; |
| 38 | |
| 39 | clang::ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext(); |
| 40 | |
| 41 | if (!scratch_ast_context) |
| 42 | return; |
| 43 | |
| 44 | if (m_ast_context != scratch_ast_context) |
| 45 | m_ast_importer->ForgetSource(scratch_ast_context, m_ast_context); |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 46 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 48 | void |
| 49 | ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) |
| 50 | { |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 51 | if (!m_ast_context) |
| 52 | return; |
| 53 | |
| 54 | m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage(); |
| 55 | m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 58 | // The core lookup interface. |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 59 | DeclContext::lookup_result |
| 60 | ClangASTSource::FindExternalVisibleDeclsByName |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 61 | ( |
| 62 | const DeclContext *decl_ctx, |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 63 | DeclarationName clang_decl_name |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 64 | ) |
| 65 | { |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 66 | if (!m_ast_context) |
| 67 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
| 68 | |
| 69 | if (GetImportInProgress()) |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 70 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
| 71 | |
| 72 | std::string decl_name (clang_decl_name.getAsString()); |
| 73 | |
| 74 | // if (m_decl_map.DoingASTImport ()) |
| 75 | // return DeclContext::lookup_result(); |
| 76 | // |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 77 | switch (clang_decl_name.getNameKind()) { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 78 | // Normal identifiers. |
| 79 | case DeclarationName::Identifier: |
Sean Callanan | 0ffff1e | 2012-04-25 17:46:01 +0000 | [diff] [blame] | 80 | { |
| 81 | clang::IdentifierInfo *identifier_info = clang_decl_name.getAsIdentifierInfo(); |
| 82 | |
| 83 | if (!identifier_info || |
| 84 | identifier_info->getBuiltinID() != 0) |
| 85 | { |
| 86 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
| 87 | } |
| 88 | } |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 89 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 90 | |
| 91 | // Operator names. Not important for now. |
| 92 | case DeclarationName::CXXOperatorName: |
| 93 | case DeclarationName::CXXLiteralOperatorName: |
| 94 | return DeclContext::lookup_result(); |
| 95 | |
| 96 | // Using directives found in this context. |
| 97 | // Tell Sema we didn't find any or we'll end up getting asked a *lot*. |
| 98 | case DeclarationName::CXXUsingDirective: |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 99 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 101 | case DeclarationName::ObjCZeroArgSelector: |
| 102 | case DeclarationName::ObjCOneArgSelector: |
| 103 | case DeclarationName::ObjCMultiArgSelector: |
Sean Callanan | 9b71484 | 2011-11-09 19:33:21 +0000 | [diff] [blame] | 104 | { |
| 105 | llvm::SmallVector<NamedDecl*, 1> method_decls; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | |
Sean Callanan | 9b71484 | 2011-11-09 19:33:21 +0000 | [diff] [blame] | 107 | NameSearchContext method_search_context (*this, method_decls, clang_decl_name, decl_ctx); |
| 108 | |
| 109 | FindObjCMethodDecls(method_search_context); |
| 110 | |
| 111 | return SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, method_decls); |
| 112 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 113 | // These aren't possible in the global context. |
| 114 | case DeclarationName::CXXConstructorName: |
| 115 | case DeclarationName::CXXDestructorName: |
| 116 | case DeclarationName::CXXConversionFunctionName: |
| 117 | return DeclContext::lookup_result(); |
| 118 | } |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 119 | |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 120 | |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 121 | if (!GetLookupsEnabled()) |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 122 | { |
| 123 | // Wait until we see a '$' at the start of a name before we start doing |
| 124 | // any lookups so we can avoid lookup up all of the builtin types. |
| 125 | if (!decl_name.empty() && decl_name[0] == '$') |
| 126 | { |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 127 | SetLookupsEnabled (true); |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 128 | } |
| 129 | else |
| 130 | { |
| 131 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
| 132 | } |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 133 | } |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 134 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 135 | ConstString const_decl_name(decl_name.c_str()); |
Greg Clayton | 9ceed1e | 2010-11-13 04:18:24 +0000 | [diff] [blame] | 136 | |
| 137 | const char *uniqued_const_decl_name = const_decl_name.GetCString(); |
| 138 | if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end()) |
| 139 | { |
| 140 | // We are currently looking up this name... |
| 141 | return DeclContext::lookup_result(); |
| 142 | } |
| 143 | m_active_lookups.insert(uniqued_const_decl_name); |
Greg Clayton | a8b278a | 2010-11-15 01:34:18 +0000 | [diff] [blame] | 144 | // static uint32_t g_depth = 0; |
| 145 | // ++g_depth; |
| 146 | // printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name); |
Greg Clayton | 9ceed1e | 2010-11-13 04:18:24 +0000 | [diff] [blame] | 147 | llvm::SmallVector<NamedDecl*, 4> name_decls; |
| 148 | NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx); |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 149 | FindExternalVisibleDecls(name_search_context); |
Greg Clayton | 9ceed1e | 2010-11-13 04:18:24 +0000 | [diff] [blame] | 150 | DeclContext::lookup_result result (SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls)); |
Greg Clayton | a8b278a | 2010-11-15 01:34:18 +0000 | [diff] [blame] | 151 | // --g_depth; |
Greg Clayton | 9ceed1e | 2010-11-13 04:18:24 +0000 | [diff] [blame] | 152 | m_active_lookups.erase (uniqued_const_decl_name); |
| 153 | return result; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 156 | void |
| 157 | ClangASTSource::CompleteType (TagDecl *tag_decl) |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 158 | { |
| 159 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 160 | |
Sean Callanan | b2027ec | 2011-12-08 23:45:45 +0000 | [diff] [blame] | 161 | static unsigned int invocation_id = 0; |
| 162 | unsigned int current_id = invocation_id++; |
| 163 | |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 164 | if (log) |
| 165 | { |
Sean Callanan | 6e22e8b | 2012-01-13 22:05:55 +0000 | [diff] [blame] | 166 | log->Printf(" CompleteTagDecl[%u] on (ASTContext*)%p Completing (TagDecl*)%p named %s", |
| 167 | current_id, |
Sean Callanan | b2027ec | 2011-12-08 23:45:45 +0000 | [diff] [blame] | 168 | m_ast_context, |
Sean Callanan | 6e22e8b | 2012-01-13 22:05:55 +0000 | [diff] [blame] | 169 | tag_decl, |
Sean Callanan | b2027ec | 2011-12-08 23:45:45 +0000 | [diff] [blame] | 170 | tag_decl->getName().str().c_str()); |
| 171 | |
| 172 | log->Printf(" CTD[%u] Before:", current_id); |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 173 | ASTDumper dumper((Decl*)tag_decl); |
| 174 | dumper.ToLog(log, " [CTD] "); |
| 175 | } |
| 176 | |
Sean Callanan | b2027ec | 2011-12-08 23:45:45 +0000 | [diff] [blame] | 177 | if (!m_ast_importer->CompleteTagDecl (tag_decl)) |
| 178 | { |
| 179 | // We couldn't complete the type. Maybe there's a definition |
| 180 | // somewhere else that can be completed. |
| 181 | |
| 182 | if (log) |
| 183 | log->Printf(" CTD[%u] Type could not be completed in the module in which it was first found.", current_id); |
| 184 | |
| 185 | bool found = false; |
| 186 | |
| 187 | DeclContext *decl_ctx = tag_decl->getDeclContext(); |
| 188 | |
| 189 | if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(decl_ctx)) |
| 190 | { |
| 191 | ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context); |
| 192 | |
| 193 | if (log && log->GetVerbose()) |
| 194 | log->Printf(" CTD[%u] Inspecting namespace map %p (%d entries)", |
| 195 | current_id, |
| 196 | namespace_map.get(), |
| 197 | (int)namespace_map->size()); |
| 198 | |
| 199 | if (!namespace_map) |
| 200 | return; |
| 201 | |
| 202 | for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end(); |
| 203 | i != e && !found; |
| 204 | ++i) |
| 205 | { |
| 206 | if (log) |
| 207 | log->Printf(" CTD[%u] Searching namespace %s in module %s", |
| 208 | current_id, |
| 209 | i->second.GetNamespaceDecl()->getNameAsString().c_str(), |
| 210 | i->first->GetFileSpec().GetFilename().GetCString()); |
| 211 | |
| 212 | TypeList types; |
| 213 | |
| 214 | SymbolContext null_sc; |
| 215 | ConstString name(tag_decl->getName().str().c_str()); |
| 216 | |
Greg Clayton | dc0a38c | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 217 | i->first->FindTypesInNamespace(null_sc, name, &i->second, UINT32_MAX, types); |
Sean Callanan | b2027ec | 2011-12-08 23:45:45 +0000 | [diff] [blame] | 218 | |
| 219 | for (uint32_t ti = 0, te = types.GetSize(); |
| 220 | ti != te && !found; |
| 221 | ++ti) |
| 222 | { |
| 223 | lldb::TypeSP type = types.GetTypeAtIndex(ti); |
| 224 | |
| 225 | if (!type) |
| 226 | continue; |
| 227 | |
| 228 | lldb::clang_type_t opaque_type = type->GetClangFullType(); |
| 229 | |
| 230 | if (!opaque_type) |
| 231 | continue; |
| 232 | |
Sean Callanan | 21f2e19 | 2011-12-14 01:13:04 +0000 | [diff] [blame] | 233 | const TagType *tag_type = QualType::getFromOpaquePtr(opaque_type)->getAs<TagType>(); |
Sean Callanan | b2027ec | 2011-12-08 23:45:45 +0000 | [diff] [blame] | 234 | |
| 235 | if (!tag_type) |
| 236 | continue; |
| 237 | |
| 238 | TagDecl *candidate_tag_decl = const_cast<TagDecl*>(tag_type->getDecl()); |
| 239 | |
| 240 | if (m_ast_importer->CompleteTagDeclWithOrigin (tag_decl, candidate_tag_decl)) |
| 241 | found = true; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | else |
| 246 | { |
| 247 | TypeList types; |
| 248 | |
| 249 | SymbolContext null_sc; |
| 250 | ConstString name(tag_decl->getName().str().c_str()); |
| 251 | ClangNamespaceDecl namespace_decl; |
| 252 | |
Enrico Granata | 146d952 | 2012-11-08 02:22:02 +0000 | [diff] [blame] | 253 | const ModuleList &module_list = m_target->GetImages(); |
Sean Callanan | b2027ec | 2011-12-08 23:45:45 +0000 | [diff] [blame] | 254 | |
Greg Clayton | dc0a38c | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 255 | bool exact_match = false; |
Greg Clayton | 9f95fb6 | 2012-04-06 17:41:13 +0000 | [diff] [blame] | 256 | module_list.FindTypes (null_sc, name, exact_match, UINT32_MAX, types); |
Sean Callanan | b2027ec | 2011-12-08 23:45:45 +0000 | [diff] [blame] | 257 | |
| 258 | for (uint32_t ti = 0, te = types.GetSize(); |
| 259 | ti != te && !found; |
| 260 | ++ti) |
| 261 | { |
| 262 | lldb::TypeSP type = types.GetTypeAtIndex(ti); |
| 263 | |
| 264 | if (!type) |
| 265 | continue; |
| 266 | |
| 267 | lldb::clang_type_t opaque_type = type->GetClangFullType(); |
| 268 | |
| 269 | if (!opaque_type) |
| 270 | continue; |
| 271 | |
Sean Callanan | 21f2e19 | 2011-12-14 01:13:04 +0000 | [diff] [blame] | 272 | const TagType *tag_type = QualType::getFromOpaquePtr(opaque_type)->getAs<TagType>(); |
Sean Callanan | b2027ec | 2011-12-08 23:45:45 +0000 | [diff] [blame] | 273 | |
| 274 | if (!tag_type) |
| 275 | continue; |
| 276 | |
| 277 | TagDecl *candidate_tag_decl = const_cast<TagDecl*>(tag_type->getDecl()); |
| 278 | |
| 279 | if (m_ast_importer->CompleteTagDeclWithOrigin (tag_decl, candidate_tag_decl)) |
| 280 | found = true; |
| 281 | } |
| 282 | } |
| 283 | } |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 284 | |
| 285 | if (log) |
| 286 | { |
| 287 | log->Printf(" [CTD] After:"); |
| 288 | ASTDumper dumper((Decl*)tag_decl); |
| 289 | dumper.ToLog(log, " [CTD] "); |
| 290 | } |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 294 | ClangASTSource::CompleteType (clang::ObjCInterfaceDecl *interface_decl) |
| 295 | { |
| 296 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 297 | |
| 298 | if (log) |
| 299 | { |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 300 | log->Printf(" [CompleteObjCInterfaceDecl] on (ASTContext*)%p Completing an ObjCInterfaceDecl named %s", m_ast_context, interface_decl->getName().str().c_str()); |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 301 | log->Printf(" [COID] Before:"); |
| 302 | ASTDumper dumper((Decl*)interface_decl); |
| 303 | dumper.ToLog(log, " [COID] "); |
| 304 | } |
| 305 | |
| 306 | m_ast_importer->CompleteObjCInterfaceDecl (interface_decl); |
| 307 | |
| 308 | if (log) |
| 309 | { |
| 310 | log->Printf(" [COID] After:"); |
| 311 | ASTDumper dumper((Decl*)interface_decl); |
| 312 | dumper.ToLog(log, " [COID] "); |
| 313 | } |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Sean Callanan | bf346eb | 2012-09-20 23:21:16 +0000 | [diff] [blame] | 316 | clang::ObjCInterfaceDecl * |
| 317 | ClangASTSource::GetCompleteObjCInterface (clang::ObjCInterfaceDecl *interface_decl) |
| 318 | { |
| 319 | lldb::ProcessSP process(m_target->GetProcessSP()); |
| 320 | |
| 321 | if (!process) |
| 322 | return NULL; |
| 323 | |
| 324 | ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime()); |
| 325 | |
| 326 | if (!language_runtime) |
| 327 | return NULL; |
| 328 | |
| 329 | ConstString class_name(interface_decl->getNameAsString().c_str()); |
| 330 | |
| 331 | lldb::TypeSP complete_type_sp(language_runtime->LookupInCompleteClassCache(class_name)); |
| 332 | |
| 333 | if (!complete_type_sp) |
| 334 | return NULL; |
| 335 | |
| 336 | TypeFromUser complete_type = TypeFromUser(complete_type_sp->GetClangFullType(), complete_type_sp->GetClangAST()); |
| 337 | lldb::clang_type_t complete_opaque_type = complete_type.GetOpaqueQualType(); |
| 338 | |
| 339 | if (!complete_opaque_type) |
| 340 | return NULL; |
| 341 | |
| 342 | const clang::Type *complete_clang_type = QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr(); |
| 343 | const ObjCInterfaceType *complete_interface_type = dyn_cast<ObjCInterfaceType>(complete_clang_type); |
| 344 | |
| 345 | if (!complete_interface_type) |
| 346 | return NULL; |
| 347 | |
| 348 | ObjCInterfaceDecl *complete_iface_decl(complete_interface_type->getDecl()); |
| 349 | |
| 350 | return complete_iface_decl; |
| 351 | } |
| 352 | |
Sean Callanan | 9b6898f | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 353 | clang::ExternalLoadResult |
Sean Callanan | bf346eb | 2012-09-20 23:21:16 +0000 | [diff] [blame] | 354 | ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context, |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 355 | bool (*predicate)(Decl::Kind), |
| 356 | llvm::SmallVectorImpl<Decl*> &decls) |
| 357 | { |
| 358 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 359 | |
| 360 | const Decl *context_decl = dyn_cast<Decl>(decl_context); |
| 361 | |
| 362 | if (!context_decl) |
| 363 | return ELR_Failure; |
| 364 | |
| 365 | static unsigned int invocation_id = 0; |
| 366 | unsigned int current_id = invocation_id++; |
| 367 | |
| 368 | if (log) |
| 369 | { |
| 370 | if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl)) |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 371 | log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p with %s predicate", |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 372 | current_id, |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 373 | m_ast_context, |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 374 | context_named_decl->getNameAsString().c_str(), |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 375 | context_decl->getDeclKindName(), |
| 376 | context_decl, |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 377 | (predicate ? "non-null" : "null")); |
| 378 | else if(context_decl) |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 379 | log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%sDecl*)%p with %s predicate", |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 380 | current_id, |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 381 | m_ast_context, |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 382 | context_decl->getDeclKindName(), |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 383 | context_decl, |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 384 | (predicate ? "non-null" : "null")); |
| 385 | else |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 386 | log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context with %s predicate", |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 387 | current_id, |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 388 | m_ast_context, |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 389 | (predicate ? "non-null" : "null")); |
| 390 | } |
| 391 | |
| 392 | Decl *original_decl = NULL; |
| 393 | ASTContext *original_ctx = NULL; |
| 394 | |
| 395 | if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx)) |
| 396 | return ELR_Failure; |
| 397 | |
| 398 | if (log) |
| 399 | { |
Sean Callanan | 6e22e8b | 2012-01-13 22:05:55 +0000 | [diff] [blame] | 400 | log->Printf(" FELD[%u] Original decl (Decl*)%p:", current_id, original_decl); |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 401 | ASTDumper(original_decl).ToLog(log, " "); |
| 402 | } |
| 403 | |
Sean Callanan | bf346eb | 2012-09-20 23:21:16 +0000 | [diff] [blame] | 404 | if (ObjCInterfaceDecl *original_iface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl)) |
| 405 | { |
| 406 | ObjCInterfaceDecl *complete_iface_decl = GetCompleteObjCInterface(original_iface_decl); |
| 407 | |
| 408 | if (complete_iface_decl && (complete_iface_decl != original_iface_decl)) |
| 409 | { |
| 410 | original_decl = complete_iface_decl; |
| 411 | original_ctx = &complete_iface_decl->getASTContext(); |
| 412 | |
| 413 | m_ast_importer->SetDeclOrigin(context_decl, original_iface_decl); |
| 414 | } |
| 415 | } |
| 416 | |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 417 | if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl)) |
| 418 | { |
| 419 | ExternalASTSource *external_source = original_ctx->getExternalSource(); |
| 420 | |
| 421 | if (external_source) |
| 422 | external_source->CompleteType (original_tag_decl); |
| 423 | } |
| 424 | |
Sean Callanan | b2027ec | 2011-12-08 23:45:45 +0000 | [diff] [blame] | 425 | const DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl); |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 426 | |
| 427 | if (!original_decl_context) |
| 428 | return ELR_Failure; |
| 429 | |
| 430 | for (TagDecl::decl_iterator iter = original_decl_context->decls_begin(); |
| 431 | iter != original_decl_context->decls_end(); |
| 432 | ++iter) |
| 433 | { |
| 434 | Decl *decl = *iter; |
| 435 | |
| 436 | if (!predicate || predicate(decl->getKind())) |
| 437 | { |
| 438 | if (log) |
| 439 | { |
| 440 | ASTDumper ast_dumper(decl); |
| 441 | if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl)) |
Sean Callanan | 06dc17f | 2012-09-24 22:25:51 +0000 | [diff] [blame] | 442 | log->Printf(" FELD[%d] Adding [to %sDecl %s] lexical %sDecl %s", current_id, context_named_decl->getDeclKindName(), context_named_decl->getNameAsString().c_str(), decl->getDeclKindName(), ast_dumper.GetCString()); |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 443 | else |
Sean Callanan | 06dc17f | 2012-09-24 22:25:51 +0000 | [diff] [blame] | 444 | log->Printf(" FELD[%d] Adding lexical %sDecl %s", current_id, decl->getDeclKindName(), ast_dumper.GetCString()); |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Sean Callanan | 4938bd6 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 447 | Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, original_ctx, decl); |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 448 | |
Sean Callanan | ddb5933 | 2012-03-20 21:11:12 +0000 | [diff] [blame] | 449 | if (!copied_decl) |
| 450 | continue; |
| 451 | |
Sean Callanan | e947839 | 2012-03-15 01:53:17 +0000 | [diff] [blame] | 452 | if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl)) |
| 453 | { |
| 454 | QualType copied_field_type = copied_field->getType(); |
| 455 | |
| 456 | if (const TagType *copied_field_tag_type = copied_field_type->getAs<TagType>()) |
| 457 | m_ast_importer->CompleteTagDecl(copied_field_tag_type->getDecl()); |
| 458 | if (const ObjCObjectType *copied_field_object_type = copied_field_type->getAs<ObjCObjectType>()) |
| 459 | if (ObjCInterfaceDecl *copied_field_objc_interface_decl = copied_field_object_type->getInterface()) |
| 460 | m_ast_importer->CompleteObjCInterfaceDecl(copied_field_objc_interface_decl); |
| 461 | } |
| 462 | |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 463 | decls.push_back(copied_decl); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | return ELR_AlreadyLoaded; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 470 | void |
| 471 | ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context) |
| 472 | { |
| 473 | assert (m_ast_context); |
| 474 | |
| 475 | const ConstString name(context.m_decl_name.getAsString().c_str()); |
| 476 | |
| 477 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 478 | |
| 479 | static unsigned int invocation_id = 0; |
| 480 | unsigned int current_id = invocation_id++; |
| 481 | |
| 482 | if (log) |
| 483 | { |
| 484 | if (!context.m_decl_context) |
Sean Callanan | 6e22e8b | 2012-01-13 22:05:55 +0000 | [diff] [blame] | 485 | log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in a NULL DeclContext", current_id, m_ast_context, name.GetCString()); |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 486 | else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context)) |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 487 | log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in '%s'", current_id, m_ast_context, name.GetCString(), context_named_decl->getNameAsString().c_str()); |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 488 | else |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 489 | log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in a '%s'", current_id, m_ast_context, name.GetCString(), context.m_decl_context->getDeclKindName()); |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap); |
| 493 | |
| 494 | if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context)) |
| 495 | { |
| 496 | ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context); |
| 497 | |
| 498 | if (log && log->GetVerbose()) |
| 499 | log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)", |
| 500 | current_id, |
| 501 | namespace_map.get(), |
| 502 | (int)namespace_map->size()); |
| 503 | |
| 504 | if (!namespace_map) |
| 505 | return; |
| 506 | |
| 507 | for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end(); |
| 508 | i != e; |
| 509 | ++i) |
| 510 | { |
| 511 | if (log) |
| 512 | log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s", |
| 513 | current_id, |
| 514 | i->second.GetNamespaceDecl()->getNameAsString().c_str(), |
| 515 | i->first->GetFileSpec().GetFilename().GetCString()); |
| 516 | |
| 517 | FindExternalVisibleDecls(context, |
| 518 | i->first, |
| 519 | i->second, |
| 520 | current_id); |
| 521 | } |
| 522 | } |
Sean Callanan | d3812fa | 2011-11-15 21:50:18 +0000 | [diff] [blame] | 523 | else if (isa<ObjCInterfaceDecl>(context.m_decl_context)) |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 524 | { |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 525 | FindObjCPropertyAndIvarDecls(context); |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 526 | } |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 527 | else if (!isa<TranslationUnitDecl>(context.m_decl_context)) |
| 528 | { |
| 529 | // we shouldn't be getting FindExternalVisibleDecls calls for these |
| 530 | return; |
| 531 | } |
| 532 | else |
| 533 | { |
| 534 | ClangNamespaceDecl namespace_decl; |
| 535 | |
| 536 | if (log) |
| 537 | log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id); |
| 538 | |
| 539 | FindExternalVisibleDecls(context, |
| 540 | lldb::ModuleSP(), |
| 541 | namespace_decl, |
| 542 | current_id); |
| 543 | } |
| 544 | |
| 545 | if (!context.m_namespace_map->empty()) |
| 546 | { |
| 547 | if (log && log->GetVerbose()) |
| 548 | log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)", |
| 549 | current_id, |
| 550 | context.m_namespace_map.get(), |
| 551 | (int)context.m_namespace_map->size()); |
| 552 | |
| 553 | NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map); |
| 554 | |
| 555 | if (clang_namespace_decl) |
| 556 | clang_namespace_decl->setHasExternalVisibleStorage(); |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | void |
| 561 | ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context, |
| 562 | lldb::ModuleSP module_sp, |
| 563 | ClangNamespaceDecl &namespace_decl, |
| 564 | unsigned int current_id) |
| 565 | { |
| 566 | assert (m_ast_context); |
| 567 | |
| 568 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 569 | |
| 570 | SymbolContextList sc_list; |
| 571 | |
| 572 | const ConstString name(context.m_decl_name.getAsString().c_str()); |
| 573 | |
| 574 | const char *name_unique_cstr = name.GetCString(); |
| 575 | |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 576 | static ConstString id_name("id"); |
| 577 | static ConstString Class_name("Class"); |
| 578 | |
| 579 | if (name == id_name || name == Class_name) |
| 580 | return; |
| 581 | |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 582 | if (name_unique_cstr == NULL) |
| 583 | return; |
| 584 | |
| 585 | // The ClangASTSource is not responsible for finding $-names. |
| 586 | if (name_unique_cstr[0] == '$') |
| 587 | return; |
| 588 | |
| 589 | if (module_sp && namespace_decl) |
| 590 | { |
| 591 | ClangNamespaceDecl found_namespace_decl; |
| 592 | |
| 593 | SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor(); |
| 594 | |
| 595 | if (symbol_vendor) |
| 596 | { |
| 597 | SymbolContext null_sc; |
| 598 | |
| 599 | found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl); |
| 600 | |
| 601 | if (found_namespace_decl) |
| 602 | { |
| 603 | context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl)); |
| 604 | |
| 605 | if (log) |
| 606 | log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s", |
| 607 | current_id, |
| 608 | name.GetCString(), |
| 609 | module_sp->GetFileSpec().GetFilename().GetCString()); |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | else |
| 614 | { |
Enrico Granata | 146d952 | 2012-11-08 02:22:02 +0000 | [diff] [blame] | 615 | const ModuleList &target_images = m_target->GetImages(); |
Jim Ingham | 9336790 | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 616 | Mutex::Locker modules_locker (target_images.GetMutex()); |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 617 | |
Jim Ingham | 9336790 | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 618 | for (uint32_t i = 0, e = target_images.GetSize(); |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 619 | i != e; |
| 620 | ++i) |
| 621 | { |
Jim Ingham | 9336790 | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 622 | lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i); |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 623 | |
| 624 | if (!image) |
| 625 | continue; |
| 626 | |
| 627 | ClangNamespaceDecl found_namespace_decl; |
| 628 | |
| 629 | SymbolVendor *symbol_vendor = image->GetSymbolVendor(); |
| 630 | |
| 631 | if (!symbol_vendor) |
| 632 | continue; |
| 633 | |
| 634 | SymbolContext null_sc; |
| 635 | |
| 636 | found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl); |
| 637 | |
| 638 | if (found_namespace_decl) |
| 639 | { |
| 640 | context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl)); |
| 641 | |
| 642 | if (log) |
| 643 | log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s", |
| 644 | current_id, |
| 645 | name.GetCString(), |
| 646 | image->GetFileSpec().GetFilename().GetCString()); |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 651 | do |
| 652 | { |
| 653 | TypeList types; |
| 654 | SymbolContext null_sc; |
Greg Clayton | dc0a38c | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 655 | const bool exact_match = false; |
Sean Callanan | 0f71d19 | 2011-12-19 19:38:39 +0000 | [diff] [blame] | 656 | |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 657 | if (module_sp && namespace_decl) |
Greg Clayton | dc0a38c | 2012-03-26 23:03:23 +0000 | [diff] [blame] | 658 | module_sp->FindTypesInNamespace(null_sc, name, &namespace_decl, 1, types); |
Sean Callanan | 0f71d19 | 2011-12-19 19:38:39 +0000 | [diff] [blame] | 659 | else |
Greg Clayton | 9f95fb6 | 2012-04-06 17:41:13 +0000 | [diff] [blame] | 660 | m_target->GetImages().FindTypes(null_sc, name, exact_match, 1, types); |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 661 | |
| 662 | if (types.GetSize()) |
| 663 | { |
| 664 | lldb::TypeSP type_sp = types.GetTypeAtIndex(0); |
| 665 | |
| 666 | if (log) |
| 667 | { |
| 668 | const char *name_string = type_sp->GetName().GetCString(); |
| 669 | |
| 670 | log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s", |
| 671 | current_id, |
| 672 | name.GetCString(), |
| 673 | (name_string ? name_string : "<anonymous>")); |
| 674 | } |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 675 | |
| 676 | clang::ASTContext *type_ast = type_sp->GetClangAST(); |
| 677 | lldb::clang_type_t full_type = type_sp->GetClangFullType(); |
Sean Callanan | e1301a6 | 2011-12-06 03:41:14 +0000 | [diff] [blame] | 678 | |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 679 | void *copied_type = GuardedCopyType(m_ast_context, type_ast, full_type); |
Sean Callanan | e1301a6 | 2011-12-06 03:41:14 +0000 | [diff] [blame] | 680 | |
Sean Callanan | dc5fce1 | 2011-12-01 21:04:37 +0000 | [diff] [blame] | 681 | if (!copied_type) |
| 682 | { |
| 683 | if (log) |
Sean Callanan | e1301a6 | 2011-12-06 03:41:14 +0000 | [diff] [blame] | 684 | log->Printf(" CAS::FEVD[%u] - Couldn't export the type for a constant integer result", |
| 685 | current_id); |
| 686 | |
Sean Callanan | dc5fce1 | 2011-12-01 21:04:37 +0000 | [diff] [blame] | 687 | break; |
| 688 | } |
| 689 | |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 690 | context.AddTypeDecl(copied_type); |
| 691 | } |
Sean Callanan | 673f3db | 2011-11-30 22:11:59 +0000 | [diff] [blame] | 692 | |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 693 | } while(0); |
| 694 | } |
| 695 | |
Sean Callanan | 4cbeeff | 2012-11-02 17:09:58 +0000 | [diff] [blame] | 696 | template <class D> class TaggedASTDecl { |
| 697 | public: |
| 698 | TaggedASTDecl() : decl(NULL) { } |
| 699 | TaggedASTDecl(D *_decl) : decl(_decl) { } |
| 700 | bool IsValid() const { return (decl != NULL); } |
| 701 | bool IsInvalid() const { return !IsValid(); } |
| 702 | D *operator->() const { return decl; } |
| 703 | D *decl; |
| 704 | }; |
| 705 | |
| 706 | template <class D2, template <class D> class TD, class D1> |
| 707 | TD<D2> |
| 708 | DynCast(TD<D1> source) |
| 709 | { |
| 710 | return TD<D2> (dyn_cast<D2>(source.decl)); |
| 711 | } |
| 712 | |
| 713 | template <class D = Decl> class DeclFromParser; |
| 714 | template <class D = Decl> class DeclFromUser; |
| 715 | |
| 716 | template <class D> class DeclFromParser : public TaggedASTDecl<D> { |
| 717 | public: |
| 718 | DeclFromParser() : TaggedASTDecl<D>() { } |
| 719 | DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) { } |
| 720 | |
| 721 | DeclFromUser<D> GetOrigin(ClangASTImporter *importer); |
| 722 | }; |
| 723 | |
| 724 | template <class D> class DeclFromUser : public TaggedASTDecl<D> { |
| 725 | public: |
| 726 | DeclFromUser() : TaggedASTDecl<D>() { } |
| 727 | DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) { } |
| 728 | |
| 729 | DeclFromParser<D> Import(ClangASTImporter *importer, ASTContext &dest_ctx); |
| 730 | }; |
| 731 | |
| 732 | template <class D> |
| 733 | DeclFromUser<D> |
| 734 | DeclFromParser<D>::GetOrigin(ClangASTImporter *importer) |
| 735 | { |
| 736 | DeclFromUser <> origin_decl; |
| 737 | importer->ResolveDeclOrigin(this->decl, &origin_decl.decl, NULL); |
| 738 | if (origin_decl.IsInvalid()) |
| 739 | return DeclFromUser<D>(); |
| 740 | return DeclFromUser<D>(dyn_cast<D>(origin_decl.decl)); |
| 741 | } |
| 742 | |
| 743 | template <class D> |
| 744 | DeclFromParser<D> |
| 745 | DeclFromUser<D>::Import(ClangASTImporter *importer, ASTContext &dest_ctx) |
| 746 | { |
| 747 | DeclFromParser <> parser_generic_decl(importer->CopyDecl(&dest_ctx, &this->decl->getASTContext(), this->decl)); |
| 748 | if (parser_generic_decl.IsInvalid()) |
| 749 | return DeclFromParser<D>(); |
| 750 | return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl)); |
| 751 | } |
| 752 | |
Sean Callanan | 14571fc | 2012-11-28 03:23:20 +0000 | [diff] [blame] | 753 | static bool |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 754 | FindObjCMethodDeclsWithOrigin (unsigned int current_id, |
| 755 | NameSearchContext &context, |
| 756 | ObjCInterfaceDecl *original_interface_decl, |
| 757 | clang::ASTContext *ast_context, |
| 758 | ClangASTImporter *ast_importer, |
| 759 | const char *log_info) |
| 760 | { |
| 761 | const DeclarationName &decl_name(context.m_decl_name); |
| 762 | clang::ASTContext *original_ctx = &original_interface_decl->getASTContext(); |
| 763 | |
| 764 | Selector original_selector; |
| 765 | |
| 766 | if (decl_name.isObjCZeroArgSelector()) |
| 767 | { |
| 768 | IdentifierInfo *ident = &original_ctx->Idents.get(decl_name.getAsString()); |
| 769 | original_selector = original_ctx->Selectors.getSelector(0, &ident); |
| 770 | } |
| 771 | else if (decl_name.isObjCOneArgSelector()) |
| 772 | { |
| 773 | const std::string &decl_name_string = decl_name.getAsString(); |
| 774 | std::string decl_name_string_without_colon(decl_name_string.c_str(), decl_name_string.length() - 1); |
| 775 | IdentifierInfo *ident = &original_ctx->Idents.get(decl_name_string_without_colon.c_str()); |
| 776 | original_selector = original_ctx->Selectors.getSelector(1, &ident); |
| 777 | } |
| 778 | else |
| 779 | { |
| 780 | SmallVector<IdentifierInfo *, 4> idents; |
| 781 | |
| 782 | clang::Selector sel = decl_name.getObjCSelector(); |
| 783 | |
| 784 | int num_args = sel.getNumArgs(); |
| 785 | |
| 786 | for (unsigned i = 0; |
| 787 | i != num_args; |
| 788 | ++i) |
| 789 | { |
| 790 | idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i))); |
| 791 | } |
| 792 | |
| 793 | original_selector = original_ctx->Selectors.getSelector(num_args, idents.data()); |
| 794 | } |
| 795 | |
| 796 | DeclarationName original_decl_name(original_selector); |
| 797 | |
| 798 | ObjCInterfaceDecl::lookup_result result = original_interface_decl->lookup(original_decl_name); |
| 799 | |
| 800 | if (result.first == result.second) |
Sean Callanan | 14571fc | 2012-11-28 03:23:20 +0000 | [diff] [blame] | 801 | return false; |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 802 | |
| 803 | if (!*result.first) |
Sean Callanan | 14571fc | 2012-11-28 03:23:20 +0000 | [diff] [blame] | 804 | return false; |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 805 | |
| 806 | ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(*result.first); |
| 807 | |
| 808 | if (!result_method) |
Sean Callanan | 14571fc | 2012-11-28 03:23:20 +0000 | [diff] [blame] | 809 | return false; |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 810 | |
| 811 | Decl *copied_decl = ast_importer->CopyDecl(ast_context, &result_method->getASTContext(), result_method); |
| 812 | |
| 813 | if (!copied_decl) |
Sean Callanan | 14571fc | 2012-11-28 03:23:20 +0000 | [diff] [blame] | 814 | return false; |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 815 | |
| 816 | ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl); |
| 817 | |
| 818 | if (!copied_method_decl) |
Sean Callanan | 14571fc | 2012-11-28 03:23:20 +0000 | [diff] [blame] | 819 | return false; |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 820 | |
| 821 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 822 | |
| 823 | if (log) |
| 824 | { |
| 825 | ASTDumper dumper((Decl*)copied_method_decl); |
| 826 | log->Printf(" CAS::FOMD[%d] found (%s) %s", current_id, log_info, dumper.GetCString()); |
| 827 | } |
| 828 | |
| 829 | context.AddNamedDecl(copied_method_decl); |
| 830 | |
Sean Callanan | 14571fc | 2012-11-28 03:23:20 +0000 | [diff] [blame] | 831 | return true; |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Sean Callanan | 9b71484 | 2011-11-09 19:33:21 +0000 | [diff] [blame] | 834 | void |
| 835 | ClangASTSource::FindObjCMethodDecls (NameSearchContext &context) |
| 836 | { |
| 837 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 838 | |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 839 | static unsigned int invocation_id = 0; |
| 840 | unsigned int current_id = invocation_id++; |
| 841 | |
Sean Callanan | 9b71484 | 2011-11-09 19:33:21 +0000 | [diff] [blame] | 842 | const DeclarationName &decl_name(context.m_decl_name); |
| 843 | const DeclContext *decl_ctx(context.m_decl_context); |
| 844 | |
| 845 | const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx); |
| 846 | |
| 847 | if (!interface_decl) |
| 848 | return; |
| 849 | |
Sean Callanan | 8d61156 | 2012-04-05 00:12:52 +0000 | [diff] [blame] | 850 | do |
| 851 | { |
| 852 | Decl *original_decl = NULL; |
| 853 | ASTContext *original_ctx = NULL; |
| 854 | |
| 855 | m_ast_importer->ResolveDeclOrigin(interface_decl, &original_decl, &original_ctx); |
| 856 | |
| 857 | if (!original_decl) |
| 858 | break; |
| 859 | |
| 860 | ObjCInterfaceDecl *original_interface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl); |
| 861 | |
Sean Callanan | 14571fc | 2012-11-28 03:23:20 +0000 | [diff] [blame] | 862 | if (FindObjCMethodDeclsWithOrigin(current_id, |
| 863 | context, |
| 864 | original_interface_decl, |
| 865 | m_ast_context, |
| 866 | m_ast_importer, |
| 867 | "at origin")) |
| 868 | return; // found it, no need to look any further |
Sean Callanan | 8d61156 | 2012-04-05 00:12:52 +0000 | [diff] [blame] | 869 | } while (0); |
| 870 | |
Sean Callanan | 9b71484 | 2011-11-09 19:33:21 +0000 | [diff] [blame] | 871 | StreamString ss; |
Sean Callanan | e1301a6 | 2011-12-06 03:41:14 +0000 | [diff] [blame] | 872 | |
Sean Callanan | 9b71484 | 2011-11-09 19:33:21 +0000 | [diff] [blame] | 873 | if (decl_name.isObjCZeroArgSelector()) |
| 874 | { |
| 875 | ss.Printf("%s", decl_name.getAsString().c_str()); |
| 876 | } |
| 877 | else if (decl_name.isObjCOneArgSelector()) |
| 878 | { |
Sean Callanan | 1d9ffe2 | 2011-11-14 18:29:46 +0000 | [diff] [blame] | 879 | ss.Printf("%s", decl_name.getAsString().c_str()); |
Sean Callanan | 9b71484 | 2011-11-09 19:33:21 +0000 | [diff] [blame] | 880 | } |
| 881 | else |
| 882 | { |
| 883 | clang::Selector sel = decl_name.getObjCSelector(); |
| 884 | |
| 885 | for (unsigned i = 0, e = sel.getNumArgs(); |
| 886 | i != e; |
| 887 | ++i) |
| 888 | { |
| 889 | llvm::StringRef r = sel.getNameForSlot(i); |
| 890 | ss.Printf("%s:", r.str().c_str()); |
| 891 | } |
| 892 | } |
| 893 | ss.Flush(); |
| 894 | |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 895 | ConstString selector_name(ss.GetData()); |
| 896 | |
Sean Callanan | 9b71484 | 2011-11-09 19:33:21 +0000 | [diff] [blame] | 897 | if (log) |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 898 | log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]", |
| 899 | current_id, |
| 900 | m_ast_context, |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 901 | interface_decl->getNameAsString().c_str(), |
| 902 | selector_name.AsCString()); |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 903 | SymbolContextList sc_list; |
| 904 | |
| 905 | const bool include_symbols = false; |
Sean Callanan | 302d78c | 2012-02-10 22:52:19 +0000 | [diff] [blame] | 906 | const bool include_inlines = false; |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 907 | const bool append = false; |
| 908 | |
Sean Callanan | e0028b8 | 2012-01-19 02:17:40 +0000 | [diff] [blame] | 909 | std::string interface_name = interface_decl->getNameAsString(); |
| 910 | |
| 911 | do |
| 912 | { |
| 913 | StreamString ms; |
| 914 | ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString()); |
| 915 | ms.Flush(); |
| 916 | ConstString instance_method_name(ms.GetData()); |
| 917 | |
Sean Callanan | 302d78c | 2012-02-10 22:52:19 +0000 | [diff] [blame] | 918 | m_target->GetImages().FindFunctions(instance_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list); |
Sean Callanan | e0028b8 | 2012-01-19 02:17:40 +0000 | [diff] [blame] | 919 | |
| 920 | if (sc_list.GetSize()) |
| 921 | break; |
| 922 | |
| 923 | ms.Clear(); |
| 924 | ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString()); |
| 925 | ms.Flush(); |
| 926 | ConstString class_method_name(ms.GetData()); |
| 927 | |
Sean Callanan | 302d78c | 2012-02-10 22:52:19 +0000 | [diff] [blame] | 928 | m_target->GetImages().FindFunctions(class_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list); |
Sean Callanan | e0028b8 | 2012-01-19 02:17:40 +0000 | [diff] [blame] | 929 | |
| 930 | if (sc_list.GetSize()) |
| 931 | break; |
| 932 | |
| 933 | // Fall back and check for methods in categories. If we find methods this way, we need to check that they're actually in |
| 934 | // categories on the desired class. |
| 935 | |
| 936 | SymbolContextList candidate_sc_list; |
| 937 | |
Sean Callanan | 302d78c | 2012-02-10 22:52:19 +0000 | [diff] [blame] | 938 | m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, include_inlines, append, candidate_sc_list); |
Sean Callanan | e0028b8 | 2012-01-19 02:17:40 +0000 | [diff] [blame] | 939 | |
| 940 | for (uint32_t ci = 0, ce = candidate_sc_list.GetSize(); |
| 941 | ci != ce; |
| 942 | ++ci) |
| 943 | { |
| 944 | SymbolContext candidate_sc; |
| 945 | |
| 946 | if (!candidate_sc_list.GetContextAtIndex(ci, candidate_sc)) |
| 947 | continue; |
| 948 | |
| 949 | if (!candidate_sc.function) |
| 950 | continue; |
| 951 | |
| 952 | const char *candidate_name = candidate_sc.function->GetName().AsCString(); |
| 953 | |
| 954 | const char *cursor = candidate_name; |
| 955 | |
| 956 | if (*cursor != '+' && *cursor != '-') |
| 957 | continue; |
| 958 | |
| 959 | ++cursor; |
| 960 | |
| 961 | if (*cursor != '[') |
| 962 | continue; |
| 963 | |
| 964 | ++cursor; |
| 965 | |
| 966 | size_t interface_len = interface_name.length(); |
| 967 | |
| 968 | if (strncmp(cursor, interface_name.c_str(), interface_len)) |
| 969 | continue; |
| 970 | |
| 971 | cursor += interface_len; |
| 972 | |
| 973 | if (*cursor == ' ' || *cursor == '(') |
| 974 | sc_list.Append(candidate_sc); |
| 975 | } |
| 976 | } |
| 977 | while (0); |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 978 | |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 979 | if (sc_list.GetSize()) |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 980 | { |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 981 | // We found a good function symbol. Use that. |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 982 | |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 983 | for (uint32_t i = 0, e = sc_list.GetSize(); |
| 984 | i != e; |
| 985 | ++i) |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 986 | { |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 987 | SymbolContext sc; |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 988 | |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 989 | if (!sc_list.GetContextAtIndex(i, sc)) |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 990 | continue; |
| 991 | |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 992 | if (!sc.function) |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 993 | continue; |
| 994 | |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 995 | DeclContext *function_ctx = sc.function->GetClangDeclContext(); |
| 996 | |
| 997 | if (!function_ctx) |
| 998 | continue; |
| 999 | |
| 1000 | ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx); |
| 1001 | |
| 1002 | if (!method_decl) |
| 1003 | continue; |
| 1004 | |
| 1005 | ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface(); |
| 1006 | |
| 1007 | if (!found_interface_decl) |
| 1008 | continue; |
| 1009 | |
| 1010 | if (found_interface_decl->getName() == interface_decl->getName()) |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 1011 | { |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 1012 | Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl); |
| 1013 | |
| 1014 | if (!copied_decl) |
| 1015 | continue; |
| 1016 | |
| 1017 | ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl); |
| 1018 | |
| 1019 | if (!copied_method_decl) |
| 1020 | continue; |
| 1021 | |
| 1022 | if (log) |
| 1023 | { |
| 1024 | ASTDumper dumper((Decl*)copied_method_decl); |
Sean Callanan | 4cbeeff | 2012-11-02 17:09:58 +0000 | [diff] [blame] | 1025 | log->Printf(" CAS::FOMD[%d] found (in symbols) %s", current_id, dumper.GetCString()); |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | context.AddNamedDecl(copied_method_decl); |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 1029 | } |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 1030 | } |
Sean Callanan | 4cbeeff | 2012-11-02 17:09:58 +0000 | [diff] [blame] | 1031 | |
| 1032 | return; |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 1033 | } |
Sean Callanan | 4cbeeff | 2012-11-02 17:09:58 +0000 | [diff] [blame] | 1034 | |
| 1035 | // Try the debug information. |
| 1036 | |
| 1037 | do |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 1038 | { |
Sean Callanan | 4cbeeff | 2012-11-02 17:09:58 +0000 | [diff] [blame] | 1039 | ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(interface_decl)); |
| 1040 | |
| 1041 | if (!complete_interface_decl) |
| 1042 | break; |
| 1043 | |
| 1044 | // We found the complete interface. The runtime never needs to be queried in this scenario. |
| 1045 | |
| 1046 | DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl); |
| 1047 | |
| 1048 | if (complete_interface_decl == interface_decl) |
| 1049 | break; // already checked this one |
| 1050 | |
| 1051 | if (log) |
| 1052 | log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...", |
| 1053 | current_id, |
| 1054 | complete_interface_decl, |
| 1055 | &complete_iface_decl->getASTContext()); |
| 1056 | |
| 1057 | FindObjCMethodDeclsWithOrigin(current_id, |
| 1058 | context, |
| 1059 | complete_interface_decl, |
| 1060 | m_ast_context, |
| 1061 | m_ast_importer, |
| 1062 | "in debug info"); |
| 1063 | |
| 1064 | return; |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 1065 | } |
Sean Callanan | 4cbeeff | 2012-11-02 17:09:58 +0000 | [diff] [blame] | 1066 | while (0); |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 1067 | |
Sean Callanan | 4cbeeff | 2012-11-02 17:09:58 +0000 | [diff] [blame] | 1068 | do |
| 1069 | { |
| 1070 | // Check the runtime only if the debug information didn't have a complete interface. |
| 1071 | |
| 1072 | lldb::ProcessSP process(m_target->GetProcessSP()); |
| 1073 | |
| 1074 | if (!process) |
| 1075 | break; |
| 1076 | |
| 1077 | ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime()); |
| 1078 | |
| 1079 | TypeVendor *type_vendor = language_runtime->GetTypeVendor(); |
| 1080 | |
| 1081 | if (!type_vendor) |
| 1082 | break; |
| 1083 | |
| 1084 | ConstString interface_name(interface_decl->getNameAsString().c_str()); |
| 1085 | bool append = false; |
| 1086 | uint32_t max_matches = 1; |
| 1087 | std::vector <ClangASTType> types; |
| 1088 | |
| 1089 | if (!type_vendor->FindTypes(interface_name, |
| 1090 | append, |
| 1091 | max_matches, |
| 1092 | types)) |
| 1093 | break; |
| 1094 | |
| 1095 | const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr(); |
| 1096 | |
| 1097 | const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type); |
| 1098 | |
| 1099 | if (!runtime_interface_type) |
| 1100 | break; |
| 1101 | |
| 1102 | ObjCInterfaceDecl *runtime_interface_decl = runtime_interface_type->getDecl(); |
| 1103 | |
| 1104 | FindObjCMethodDeclsWithOrigin(current_id, |
| 1105 | context, |
| 1106 | runtime_interface_decl, |
| 1107 | m_ast_context, |
| 1108 | m_ast_importer, |
| 1109 | "in runtime"); |
| 1110 | } |
| 1111 | while(0); |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 1114 | static bool |
| 1115 | FindObjCPropertyAndIvarDeclsWithOrigin (unsigned int current_id, |
| 1116 | NameSearchContext &context, |
| 1117 | clang::ASTContext &ast_context, |
| 1118 | ClangASTImporter *ast_importer, |
| 1119 | DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl) |
| 1120 | { |
| 1121 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 1122 | |
| 1123 | if (origin_iface_decl.IsInvalid()) |
| 1124 | return false; |
| 1125 | |
| 1126 | std::string name_str = context.m_decl_name.getAsString(); |
| 1127 | StringRef name(name_str.c_str()); |
| 1128 | IdentifierInfo &name_identifier(origin_iface_decl->getASTContext().Idents.get(name)); |
| 1129 | |
| 1130 | DeclFromUser<ObjCPropertyDecl> origin_property_decl(origin_iface_decl->FindPropertyDeclaration(&name_identifier)); |
| 1131 | |
| 1132 | bool found = false; |
| 1133 | |
| 1134 | if (origin_property_decl.IsValid()) |
| 1135 | { |
| 1136 | DeclFromParser<ObjCPropertyDecl> parser_property_decl(origin_property_decl.Import(ast_importer, ast_context)); |
| 1137 | if (parser_property_decl.IsValid()) |
| 1138 | { |
| 1139 | if (log) |
| 1140 | { |
| 1141 | ASTDumper dumper((Decl*)parser_property_decl.decl); |
| 1142 | log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString()); |
| 1143 | } |
| 1144 | |
| 1145 | context.AddNamedDecl(parser_property_decl.decl); |
| 1146 | found = true; |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | DeclFromUser<ObjCIvarDecl> origin_ivar_decl(origin_iface_decl->getIvarDecl(&name_identifier)); |
| 1151 | |
| 1152 | if (origin_ivar_decl.IsValid()) |
| 1153 | { |
| 1154 | DeclFromParser<ObjCIvarDecl> parser_ivar_decl(origin_ivar_decl.Import(ast_importer, ast_context)); |
| 1155 | if (parser_ivar_decl.IsValid()) |
| 1156 | { |
| 1157 | if (log) |
| 1158 | { |
| 1159 | ASTDumper dumper((Decl*)parser_ivar_decl.decl); |
| 1160 | log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString()); |
| 1161 | } |
| 1162 | |
| 1163 | context.AddNamedDecl(parser_ivar_decl.decl); |
| 1164 | found = true; |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | return found; |
| 1169 | } |
| 1170 | |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 1171 | void |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 1172 | ClangASTSource::FindObjCPropertyAndIvarDecls (NameSearchContext &context) |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 1173 | { |
| 1174 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 1175 | |
| 1176 | static unsigned int invocation_id = 0; |
| 1177 | unsigned int current_id = invocation_id++; |
| 1178 | |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 1179 | DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl(cast<ObjCInterfaceDecl>(context.m_decl_context)); |
| 1180 | DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(parser_iface_decl.GetOrigin(m_ast_importer)); |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 1181 | |
| 1182 | ConstString class_name(parser_iface_decl->getNameAsString().c_str()); |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 1183 | |
| 1184 | if (log) |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 1185 | log->Printf("ClangASTSource::FindObjCPropertyAndIvarDecls[%d] on (ASTContext*)%p for '%s.%s'", |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 1186 | current_id, |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 1187 | m_ast_context, |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 1188 | parser_iface_decl->getNameAsString().c_str(), |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 1189 | context.m_decl_name.getAsString().c_str()); |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 1190 | |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 1191 | if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id, |
| 1192 | context, |
| 1193 | *m_ast_context, |
| 1194 | m_ast_importer, |
| 1195 | origin_iface_decl)) |
| 1196 | return; |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 1197 | |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 1198 | if (log) |
| 1199 | log->Printf("CAS::FOPD[%d] couldn't find the property on origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p, searching elsewhere...", |
| 1200 | current_id, |
| 1201 | origin_iface_decl.decl, |
| 1202 | &origin_iface_decl->getASTContext()); |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 1203 | |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 1204 | SymbolContext null_sc; |
| 1205 | TypeList type_list; |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 1206 | |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 1207 | do |
| 1208 | { |
Sean Callanan | bf346eb | 2012-09-20 23:21:16 +0000 | [diff] [blame] | 1209 | ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(parser_iface_decl.decl)); |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 1210 | |
Sean Callanan | bf346eb | 2012-09-20 23:21:16 +0000 | [diff] [blame] | 1211 | if (!complete_interface_decl) |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 1212 | break; |
| 1213 | |
Sean Callanan | 4cbeeff | 2012-11-02 17:09:58 +0000 | [diff] [blame] | 1214 | // We found the complete interface. The runtime never needs to be queried in this scenario. |
| 1215 | |
Sean Callanan | bf346eb | 2012-09-20 23:21:16 +0000 | [diff] [blame] | 1216 | DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl); |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 1217 | |
| 1218 | if (complete_iface_decl.decl == origin_iface_decl.decl) |
| 1219 | break; // already checked this one |
| 1220 | |
| 1221 | if (log) |
| 1222 | log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...", |
| 1223 | current_id, |
| 1224 | complete_iface_decl.decl, |
| 1225 | &complete_iface_decl->getASTContext()); |
| 1226 | |
Sean Callanan | 4cbeeff | 2012-11-02 17:09:58 +0000 | [diff] [blame] | 1227 | FindObjCPropertyAndIvarDeclsWithOrigin(current_id, |
| 1228 | context, |
| 1229 | *m_ast_context, |
| 1230 | m_ast_importer, |
| 1231 | complete_iface_decl); |
| 1232 | |
| 1233 | return; |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 1234 | } |
| 1235 | while(0); |
Sean Callanan | 931acec | 2012-02-22 23:57:45 +0000 | [diff] [blame] | 1236 | |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 1237 | do |
| 1238 | { |
Sean Callanan | 4cbeeff | 2012-11-02 17:09:58 +0000 | [diff] [blame] | 1239 | // Check the runtime only if the debug information didn't have a complete interface. |
| 1240 | |
| 1241 | lldb::ProcessSP process(m_target->GetProcessSP()); |
| 1242 | |
| 1243 | if (!process) |
| 1244 | return; |
| 1245 | |
| 1246 | ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime()); |
| 1247 | |
| 1248 | if (!language_runtime) |
| 1249 | return; |
Sean Callanan | c718b96 | 2012-09-11 21:44:01 +0000 | [diff] [blame] | 1250 | |
| 1251 | TypeVendor *type_vendor = language_runtime->GetTypeVendor(); |
| 1252 | |
| 1253 | if (!type_vendor) |
| 1254 | break; |
| 1255 | |
| 1256 | bool append = false; |
| 1257 | uint32_t max_matches = 1; |
| 1258 | std::vector <ClangASTType> types; |
| 1259 | |
| 1260 | if (!type_vendor->FindTypes(class_name, |
| 1261 | append, |
| 1262 | max_matches, |
| 1263 | types)) |
| 1264 | break; |
| 1265 | |
| 1266 | const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr(); |
| 1267 | |
| 1268 | const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type); |
| 1269 | |
| 1270 | if (!runtime_interface_type) |
| 1271 | break; |
| 1272 | |
| 1273 | DeclFromUser<const ObjCInterfaceDecl> runtime_iface_decl(runtime_interface_type->getDecl()); |
| 1274 | |
| 1275 | if (log) |
| 1276 | log->Printf("CAS::FOPD[%d] trying runtime (ObjCInterfaceDecl*)%p/(ASTContext*)%p...", |
| 1277 | current_id, |
| 1278 | runtime_iface_decl.decl, |
| 1279 | &runtime_iface_decl->getASTContext()); |
| 1280 | |
| 1281 | if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id, |
| 1282 | context, |
| 1283 | *m_ast_context, |
| 1284 | m_ast_importer, |
| 1285 | runtime_iface_decl)) |
| 1286 | return; |
| 1287 | } |
| 1288 | while(0); |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | typedef llvm::DenseMap <const FieldDecl *, uint64_t> FieldOffsetMap; |
| 1292 | typedef llvm::DenseMap <const CXXRecordDecl *, CharUnits> BaseOffsetMap; |
| 1293 | |
| 1294 | template <class D, class O> |
| 1295 | static bool |
| 1296 | ImportOffsetMap (llvm::DenseMap <const D*, O> &destination_map, |
| 1297 | llvm::DenseMap <const D*, O> &source_map, |
| 1298 | ClangASTImporter *importer, |
| 1299 | ASTContext &dest_ctx) |
| 1300 | { |
| 1301 | typedef llvm::DenseMap <const D*, O> MapType; |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 1302 | |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 1303 | for (typename MapType::iterator fi = source_map.begin(), fe = source_map.end(); |
| 1304 | fi != fe; |
| 1305 | ++fi) |
| 1306 | { |
| 1307 | DeclFromUser <D> user_decl(const_cast<D*>(fi->first)); |
| 1308 | DeclFromParser <D> parser_decl(user_decl.Import(importer, dest_ctx)); |
| 1309 | if (parser_decl.IsInvalid()) |
| 1310 | return false; |
| 1311 | destination_map.insert(std::pair<const D *, O>(parser_decl.decl, fi->second)); |
| 1312 | } |
| 1313 | |
| 1314 | return true; |
| 1315 | } |
| 1316 | |
| 1317 | template <bool IsVirtual> bool ExtractBaseOffsets (const ASTRecordLayout &record_layout, |
| 1318 | DeclFromUser<const CXXRecordDecl> &record, |
| 1319 | BaseOffsetMap &base_offsets) |
| 1320 | { |
| 1321 | for (CXXRecordDecl::base_class_const_iterator |
| 1322 | bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()), |
| 1323 | be = (IsVirtual ? record->vbases_end() : record->bases_end()); |
| 1324 | bi != be; |
| 1325 | ++bi) |
| 1326 | { |
| 1327 | if (!IsVirtual && bi->isVirtual()) |
| 1328 | continue; |
| 1329 | |
| 1330 | const clang::Type *origin_base_type = bi->getType().getTypePtr(); |
| 1331 | const clang::RecordType *origin_base_record_type = origin_base_type->getAs<RecordType>(); |
| 1332 | |
| 1333 | if (!origin_base_record_type) |
| 1334 | return false; |
| 1335 | |
| 1336 | DeclFromUser <RecordDecl> origin_base_record(origin_base_record_type->getDecl()); |
| 1337 | |
| 1338 | if (origin_base_record.IsInvalid()) |
| 1339 | return false; |
| 1340 | |
| 1341 | DeclFromUser <CXXRecordDecl> origin_base_cxx_record(DynCast<CXXRecordDecl>(origin_base_record)); |
| 1342 | |
| 1343 | if (origin_base_cxx_record.IsInvalid()) |
| 1344 | return false; |
| 1345 | |
| 1346 | CharUnits base_offset; |
| 1347 | |
| 1348 | if (IsVirtual) |
| 1349 | base_offset = record_layout.getVBaseClassOffset(origin_base_cxx_record.decl); |
| 1350 | else |
| 1351 | base_offset = record_layout.getBaseClassOffset(origin_base_cxx_record.decl); |
| 1352 | |
| 1353 | base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(origin_base_cxx_record.decl, base_offset)); |
| 1354 | } |
| 1355 | |
| 1356 | return true; |
| 1357 | } |
| 1358 | |
| 1359 | bool |
| 1360 | ClangASTSource::layoutRecordType(const RecordDecl *record, |
| 1361 | uint64_t &size, |
| 1362 | uint64_t &alignment, |
| 1363 | FieldOffsetMap &field_offsets, |
| 1364 | BaseOffsetMap &base_offsets, |
| 1365 | BaseOffsetMap &virtual_base_offsets) |
| 1366 | { |
| 1367 | static unsigned int invocation_id = 0; |
| 1368 | unsigned int current_id = invocation_id++; |
| 1369 | |
| 1370 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 1371 | |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 1372 | if (log) |
| 1373 | { |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 1374 | log->Printf("LayoutRecordType[%u] on (RecordDecl*)%p [name = '%s']", |
| 1375 | current_id, |
| 1376 | m_ast_context, |
| 1377 | record->getNameAsString().c_str()); |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 1378 | } |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 1379 | |
| 1380 | |
| 1381 | DeclFromParser <const RecordDecl> parser_record(record); |
| 1382 | DeclFromUser <const RecordDecl> origin_record(parser_record.GetOrigin(m_ast_importer)); |
| 1383 | |
| 1384 | if (origin_record.IsInvalid()) |
| 1385 | return false; |
| 1386 | |
| 1387 | FieldOffsetMap origin_field_offsets; |
| 1388 | BaseOffsetMap origin_base_offsets; |
| 1389 | BaseOffsetMap origin_virtual_base_offsets; |
| 1390 | |
Sean Callanan | 33bff9a | 2012-04-07 00:06:00 +0000 | [diff] [blame] | 1391 | ClangASTContext::GetCompleteDecl(&origin_record->getASTContext(), const_cast<RecordDecl*>(origin_record.decl)); |
| 1392 | |
| 1393 | if (!origin_record.decl->getDefinition()) |
| 1394 | return false; |
| 1395 | |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 1396 | const ASTRecordLayout &record_layout(origin_record->getASTContext().getASTRecordLayout(origin_record.decl)); |
| 1397 | |
| 1398 | int field_idx = 0; |
| 1399 | |
| 1400 | for (RecordDecl::field_iterator fi = origin_record->field_begin(), fe = origin_record->field_end(); |
| 1401 | fi != fe; |
| 1402 | ++fi) |
| 1403 | { |
| 1404 | uint64_t field_offset = record_layout.getFieldOffset(field_idx); |
| 1405 | |
| 1406 | origin_field_offsets.insert(std::pair<const FieldDecl *, uint64_t>(*fi, field_offset)); |
| 1407 | |
| 1408 | field_idx++; |
| 1409 | } |
| 1410 | |
| 1411 | ASTContext &parser_ast_context(record->getASTContext()); |
| 1412 | |
| 1413 | DeclFromUser <const CXXRecordDecl> origin_cxx_record(DynCast<const CXXRecordDecl>(origin_record)); |
| 1414 | |
| 1415 | if (origin_cxx_record.IsValid()) |
| 1416 | { |
| 1417 | if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record, origin_base_offsets) || |
| 1418 | !ExtractBaseOffsets<true>(record_layout, origin_cxx_record, origin_virtual_base_offsets)) |
| 1419 | return false; |
| 1420 | } |
| 1421 | |
| 1422 | if (!ImportOffsetMap(field_offsets, origin_field_offsets, m_ast_importer, parser_ast_context) || |
| 1423 | !ImportOffsetMap(base_offsets, origin_base_offsets, m_ast_importer, parser_ast_context) || |
| 1424 | !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets, m_ast_importer, parser_ast_context)) |
| 1425 | return false; |
| 1426 | |
| 1427 | size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth(); |
| 1428 | alignment = record_layout.getAlignment().getQuantity() * m_ast_context->getCharWidth(); |
| 1429 | |
| 1430 | if (log) |
| 1431 | { |
| 1432 | log->Printf("LRT[%u] returned:", current_id); |
| 1433 | log->Printf("LRT[%u] Original = (RecordDecl*)%p", current_id, origin_record.decl); |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame^] | 1434 | log->Printf("LRT[%u] Size = %" PRId64, current_id, size); |
| 1435 | log->Printf("LRT[%u] Alignment = %" PRId64, current_id, alignment); |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 1436 | log->Printf("LRT[%u] Fields:", current_id); |
| 1437 | for (RecordDecl::field_iterator fi = record->field_begin(), fe = record->field_end(); |
| 1438 | fi != fe; |
| 1439 | ++fi) |
| 1440 | { |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame^] | 1441 | log->Printf("LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %" PRId64 " bits", |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 1442 | current_id, |
| 1443 | *fi, |
| 1444 | fi->getNameAsString().c_str(), |
| 1445 | field_offsets[*fi]); |
| 1446 | } |
| 1447 | DeclFromParser <const CXXRecordDecl> parser_cxx_record = DynCast<const CXXRecordDecl>(parser_record); |
| 1448 | if (parser_cxx_record.IsValid()) |
| 1449 | { |
| 1450 | log->Printf("LRT[%u] Bases:", current_id); |
| 1451 | for (CXXRecordDecl::base_class_const_iterator bi = parser_cxx_record->bases_begin(), be = parser_cxx_record->bases_end(); |
| 1452 | bi != be; |
| 1453 | ++bi) |
| 1454 | { |
| 1455 | bool is_virtual = bi->isVirtual(); |
| 1456 | |
| 1457 | QualType base_type = bi->getType(); |
| 1458 | const RecordType *base_record_type = base_type->getAs<RecordType>(); |
| 1459 | DeclFromParser <RecordDecl> base_record(base_record_type->getDecl()); |
| 1460 | DeclFromParser <CXXRecordDecl> base_cxx_record = DynCast<CXXRecordDecl>(base_record); |
| 1461 | |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame^] | 1462 | log->Printf("LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %" PRId64 " chars", |
Sean Callanan | 8f2e392 | 2012-02-04 08:49:35 +0000 | [diff] [blame] | 1463 | current_id, |
| 1464 | (is_virtual ? "Virtual " : ""), |
| 1465 | base_cxx_record.decl, |
| 1466 | base_cxx_record.decl->getNameAsString().c_str(), |
| 1467 | (is_virtual ? virtual_base_offsets[base_cxx_record.decl].getQuantity() : |
| 1468 | base_offsets[base_cxx_record.decl].getQuantity())); |
| 1469 | } |
| 1470 | } |
| 1471 | else |
| 1472 | { |
| 1473 | log->Printf("LRD[%u] Not a CXXRecord, so no bases", current_id); |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | return true; |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 1480 | void |
| 1481 | ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map, |
| 1482 | const ConstString &name, |
| 1483 | ClangASTImporter::NamespaceMapSP &parent_map) const |
| 1484 | { |
| 1485 | static unsigned int invocation_id = 0; |
| 1486 | unsigned int current_id = invocation_id++; |
| 1487 | |
| 1488 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 1489 | |
| 1490 | if (log) |
| 1491 | { |
| 1492 | if (parent_map && parent_map->size()) |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 1493 | log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s", |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 1494 | current_id, |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 1495 | m_ast_context, |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 1496 | name.GetCString(), |
| 1497 | parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str()); |
| 1498 | else |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 1499 | log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s", |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 1500 | current_id, |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 1501 | m_ast_context, |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 1502 | name.GetCString()); |
| 1503 | } |
| 1504 | |
| 1505 | |
| 1506 | if (parent_map) |
| 1507 | { |
| 1508 | for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end(); |
| 1509 | i != e; |
| 1510 | ++i) |
| 1511 | { |
| 1512 | ClangNamespaceDecl found_namespace_decl; |
| 1513 | |
| 1514 | lldb::ModuleSP module_sp = i->first; |
| 1515 | ClangNamespaceDecl module_parent_namespace_decl = i->second; |
| 1516 | |
| 1517 | SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor(); |
| 1518 | |
| 1519 | if (!symbol_vendor) |
| 1520 | continue; |
| 1521 | |
| 1522 | SymbolContext null_sc; |
| 1523 | |
| 1524 | found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl); |
| 1525 | |
| 1526 | if (!found_namespace_decl) |
| 1527 | continue; |
| 1528 | |
| 1529 | namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl)); |
| 1530 | |
| 1531 | if (log) |
| 1532 | log->Printf(" CMN[%u] Found namespace %s in module %s", |
| 1533 | current_id, |
| 1534 | name.GetCString(), |
| 1535 | module_sp->GetFileSpec().GetFilename().GetCString()); |
| 1536 | } |
| 1537 | } |
| 1538 | else |
| 1539 | { |
Enrico Granata | 146d952 | 2012-11-08 02:22:02 +0000 | [diff] [blame] | 1540 | const ModuleList &target_images = m_target->GetImages(); |
Jim Ingham | 9336790 | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 1541 | Mutex::Locker modules_locker(target_images.GetMutex()); |
| 1542 | |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 1543 | ClangNamespaceDecl null_namespace_decl; |
| 1544 | |
Jim Ingham | 9336790 | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 1545 | for (uint32_t i = 0, e = target_images.GetSize(); |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 1546 | i != e; |
| 1547 | ++i) |
| 1548 | { |
Jim Ingham | 9336790 | 2012-05-30 02:19:25 +0000 | [diff] [blame] | 1549 | lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i); |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 1550 | |
| 1551 | if (!image) |
| 1552 | continue; |
| 1553 | |
| 1554 | ClangNamespaceDecl found_namespace_decl; |
| 1555 | |
| 1556 | SymbolVendor *symbol_vendor = image->GetSymbolVendor(); |
| 1557 | |
| 1558 | if (!symbol_vendor) |
| 1559 | continue; |
| 1560 | |
| 1561 | SymbolContext null_sc; |
| 1562 | |
| 1563 | found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl); |
| 1564 | |
| 1565 | if (!found_namespace_decl) |
| 1566 | continue; |
| 1567 | |
| 1568 | namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl)); |
| 1569 | |
| 1570 | if (log) |
| 1571 | log->Printf(" CMN[%u] Found namespace %s in module %s", |
| 1572 | current_id, |
| 1573 | name.GetCString(), |
| 1574 | image->GetFileSpec().GetFilename().GetCString()); |
| 1575 | } |
| 1576 | } |
| 1577 | } |
| 1578 | |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 1579 | NamespaceDecl * |
| 1580 | ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls) |
| 1581 | { |
Greg Clayton | 13d24fb | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 1582 | if (!namespace_decls) |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 1583 | return NULL; |
| 1584 | |
| 1585 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 1586 | |
| 1587 | const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second; |
| 1588 | |
Sean Callanan | 4938bd6 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 1589 | Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl()); |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 1590 | |
Sean Callanan | ddb5933 | 2012-03-20 21:11:12 +0000 | [diff] [blame] | 1591 | if (!copied_decl) |
| 1592 | return NULL; |
| 1593 | |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 1594 | NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl); |
| 1595 | |
| 1596 | m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls); |
| 1597 | |
| 1598 | return dyn_cast<NamespaceDecl>(copied_decl); |
| 1599 | } |
| 1600 | |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 1601 | void * |
| 1602 | ClangASTSource::GuardedCopyType (ASTContext *dest_context, |
| 1603 | ASTContext *source_context, |
| 1604 | void *clang_type) |
| 1605 | { |
| 1606 | SetImportInProgress(true); |
| 1607 | |
Sean Callanan | 4938bd6 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 1608 | QualType ret_qual_type = m_ast_importer->CopyType (m_ast_context, source_context, QualType::getFromOpaquePtr(clang_type)); |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 1609 | |
| 1610 | void *ret = ret_qual_type.getAsOpaquePtr(); |
| 1611 | |
| 1612 | SetImportInProgress(false); |
| 1613 | |
Sean Callanan | ddb5933 | 2012-03-20 21:11:12 +0000 | [diff] [blame] | 1614 | if (ret && ret_qual_type->getCanonicalTypeInternal().isNull()) |
Sean Callanan | aa11af8 | 2012-02-27 19:22:39 +0000 | [diff] [blame] | 1615 | // this shouldn't happen, but we're hardening because the AST importer seems to be generating bad types |
| 1616 | // on occasion. |
| 1617 | return NULL; |
| 1618 | |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 1619 | return ret; |
| 1620 | } |
| 1621 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 1622 | clang::NamedDecl * |
| 1623 | NameSearchContext::AddVarDecl(void *type) |
| 1624 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 1625 | IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo(); |
Sean Callanan | 1ddd9fe | 2010-11-30 00:27:43 +0000 | [diff] [blame] | 1626 | |
| 1627 | assert (type && "Type for variable must be non-NULL!"); |
Sean Callanan | cc07462 | 2010-09-14 21:59:34 +0000 | [diff] [blame] | 1628 | |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 1629 | clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context, |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 1630 | const_cast<DeclContext*>(m_decl_context), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1631 | SourceLocation(), |
Sean Callanan | 279584c | 2011-03-15 00:17:19 +0000 | [diff] [blame] | 1632 | SourceLocation(), |
Sean Callanan | cc07462 | 2010-09-14 21:59:34 +0000 | [diff] [blame] | 1633 | ii, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1634 | QualType::getFromOpaquePtr(type), |
| 1635 | 0, |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 1636 | SC_Static, |
| 1637 | SC_Static); |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 1638 | m_decls.push_back(Decl); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1639 | |
| 1640 | return Decl; |
| 1641 | } |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 1642 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 1643 | clang::NamedDecl * |
| 1644 | NameSearchContext::AddFunDecl (void *type) |
| 1645 | { |
Sean Callanan | 30a5dd5 | 2012-03-21 17:13:20 +0000 | [diff] [blame] | 1646 | assert (type && "Type for variable must be non-NULL!"); |
| 1647 | |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 1648 | clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context, |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 1649 | const_cast<DeclContext*>(m_decl_context), |
| 1650 | SourceLocation(), |
Sean Callanan | 279584c | 2011-03-15 00:17:19 +0000 | [diff] [blame] | 1651 | SourceLocation(), |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 1652 | m_decl_name.getAsIdentifierInfo(), |
| 1653 | QualType::getFromOpaquePtr(type), |
| 1654 | NULL, |
| 1655 | SC_Static, |
| 1656 | SC_Static, |
| 1657 | false, |
| 1658 | true); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 1659 | |
Sean Callanan | b291abe | 2010-08-12 23:45:38 +0000 | [diff] [blame] | 1660 | // We have to do more than just synthesize the FunctionDecl. We have to |
| 1661 | // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do |
| 1662 | // this, we raid the function's FunctionProtoType for types. |
| 1663 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 1664 | QualType qual_type (QualType::getFromOpaquePtr(type)); |
Sean Callanan | 21f2e19 | 2011-12-14 01:13:04 +0000 | [diff] [blame] | 1665 | const FunctionProtoType *func_proto_type = qual_type.getTypePtr()->getAs<FunctionProtoType>(); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 1666 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 1667 | if (func_proto_type) |
Sean Callanan | 3c821cc | 2010-06-23 18:58:10 +0000 | [diff] [blame] | 1668 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 1669 | unsigned NumArgs = func_proto_type->getNumArgs(); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 1670 | unsigned ArgIndex; |
| 1671 | |
Sean Callanan | c153518 | 2011-10-07 23:18:13 +0000 | [diff] [blame] | 1672 | SmallVector<ParmVarDecl *, 5> parm_var_decls; |
| 1673 | |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 1674 | for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex) |
| 1675 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 1676 | QualType arg_qual_type (func_proto_type->getArgType(ArgIndex)); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 1677 | |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 1678 | parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context, |
Sean Callanan | c153518 | 2011-10-07 23:18:13 +0000 | [diff] [blame] | 1679 | const_cast<DeclContext*>(m_decl_context), |
| 1680 | SourceLocation(), |
| 1681 | SourceLocation(), |
| 1682 | NULL, |
| 1683 | arg_qual_type, |
| 1684 | NULL, |
| 1685 | SC_Static, |
| 1686 | SC_Static, |
| 1687 | NULL)); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
Sean Callanan | c153518 | 2011-10-07 23:18:13 +0000 | [diff] [blame] | 1690 | func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls)); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 1691 | } |
Sean Callanan | dc5fce1 | 2011-12-01 21:04:37 +0000 | [diff] [blame] | 1692 | else |
| 1693 | { |
| 1694 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 1695 | |
| 1696 | log->Printf("Function type wasn't a FunctionProtoType"); |
| 1697 | } |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 1698 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 1699 | m_decls.push_back(func_decl); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 1700 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 1701 | return func_decl; |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 1702 | } |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 1703 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 1704 | clang::NamedDecl * |
| 1705 | NameSearchContext::AddGenericFunDecl() |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 1706 | { |
Sean Callanan | ad29309 | 2011-01-18 23:32:05 +0000 | [diff] [blame] | 1707 | FunctionProtoType::ExtProtoInfo proto_info; |
| 1708 | |
| 1709 | proto_info.Variadic = true; |
| 1710 | |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 1711 | QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result |
| 1712 | NULL, // argument types |
| 1713 | 0, // number of arguments |
| 1714 | proto_info)); |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 1715 | |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 1716 | return AddFunDecl(generic_function_type.getAsOpaquePtr()); |
| 1717 | } |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 1718 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 1719 | clang::NamedDecl * |
| 1720 | NameSearchContext::AddTypeDecl(void *type) |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 1721 | { |
Greg Clayton | a1aaaff | 2011-01-23 00:34:52 +0000 | [diff] [blame] | 1722 | if (type) |
| 1723 | { |
| 1724 | QualType qual_type = QualType::getFromOpaquePtr(type); |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 1725 | |
Sean Callanan | 21f2e19 | 2011-12-14 01:13:04 +0000 | [diff] [blame] | 1726 | if (const TagType *tag_type = qual_type->getAs<TagType>()) |
Greg Clayton | a1aaaff | 2011-01-23 00:34:52 +0000 | [diff] [blame] | 1727 | { |
| 1728 | TagDecl *tag_decl = tag_type->getDecl(); |
| 1729 | |
| 1730 | m_decls.push_back(tag_decl); |
| 1731 | |
| 1732 | return tag_decl; |
| 1733 | } |
Sean Callanan | 21f2e19 | 2011-12-14 01:13:04 +0000 | [diff] [blame] | 1734 | else if (const ObjCObjectType *objc_object_type = qual_type->getAs<ObjCObjectType>()) |
Greg Clayton | a1aaaff | 2011-01-23 00:34:52 +0000 | [diff] [blame] | 1735 | { |
| 1736 | ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface(); |
| 1737 | |
| 1738 | m_decls.push_back((NamedDecl*)interface_decl); |
| 1739 | |
| 1740 | return (NamedDecl*)interface_decl; |
| 1741 | } |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 1742 | } |
Greg Clayton | a1aaaff | 2011-01-23 00:34:52 +0000 | [diff] [blame] | 1743 | return NULL; |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 1744 | } |
Greg Clayton | e6d72ca | 2011-06-25 00:44:06 +0000 | [diff] [blame] | 1745 | |
| 1746 | void |
| 1747 | NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result) |
| 1748 | { |
| 1749 | for (clang::NamedDecl * const *decl_iterator = result.first; |
| 1750 | decl_iterator != result.second; |
| 1751 | ++decl_iterator) |
| 1752 | m_decls.push_back (*decl_iterator); |
| 1753 | } |
| 1754 | |
| 1755 | void |
| 1756 | NameSearchContext::AddNamedDecl (clang::NamedDecl *decl) |
| 1757 | { |
| 1758 | m_decls.push_back (decl); |
| 1759 | } |