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