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