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