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