Greg Clayton | 1e591ce | 2010-07-16 18:28:27 +0000 | [diff] [blame] | 1 | //===-- ClangASTSource.cpp ---------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 10 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTContext.h" |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Log.h" |
Greg Clayton | 6e0101c | 2011-09-17 06:21:20 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Module.h" |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 14 | #include "lldb/Core/ModuleList.h" |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 15 | #include "lldb/Expression/ASTDumper.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include "lldb/Expression/ClangASTSource.h" |
| 17 | #include "lldb/Expression/ClangExpression.h" |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 18 | #include "lldb/Symbol/ClangNamespaceDecl.h" |
| 19 | #include "lldb/Symbol/SymbolVendor.h" |
Sean Callanan | 673f3db | 2011-11-30 22:11:59 +0000 | [diff] [blame] | 20 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 21 | #include "lldb/Target/Target.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | using namespace lldb_private; |
| 25 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 26 | ClangASTSource::~ClangASTSource() |
| 27 | { |
Sean Callanan | a3d0447 | 2011-11-29 00:42:02 +0000 | [diff] [blame] | 28 | m_ast_importer->ForgetDestination(m_ast_context); |
| 29 | |
Johnny Chen | fa21ffd | 2011-11-30 23:18:53 +0000 | [diff] [blame] | 30 | // We are in the process of destruction, don't create clang ast context on demand |
| 31 | // by passing false to Target::GetScratchClangASTContext(create_on_demand). |
| 32 | ClangASTContext *scratch_clang_ast_context = m_target->GetScratchClangASTContext(false); |
Sean Callanan | a3d0447 | 2011-11-29 00:42:02 +0000 | [diff] [blame] | 33 | |
| 34 | if (!scratch_clang_ast_context) |
| 35 | return; |
| 36 | |
| 37 | clang::ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext(); |
| 38 | |
| 39 | if (!scratch_ast_context) |
| 40 | return; |
| 41 | |
| 42 | if (m_ast_context != scratch_ast_context) |
| 43 | m_ast_importer->ForgetSource(scratch_ast_context, m_ast_context); |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 44 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 45 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 46 | void |
| 47 | ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) |
| 48 | { |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 49 | if (!m_ast_context) |
| 50 | return; |
| 51 | |
| 52 | m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage(); |
| 53 | m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | // The core lookup interface. |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 57 | DeclContext::lookup_result |
| 58 | ClangASTSource::FindExternalVisibleDeclsByName |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 59 | ( |
| 60 | const DeclContext *decl_ctx, |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 61 | DeclarationName clang_decl_name |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 62 | ) |
| 63 | { |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 64 | if (!m_ast_context) |
| 65 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
| 66 | |
| 67 | if (GetImportInProgress()) |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 68 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
| 69 | |
| 70 | std::string decl_name (clang_decl_name.getAsString()); |
| 71 | |
| 72 | // if (m_decl_map.DoingASTImport ()) |
| 73 | // return DeclContext::lookup_result(); |
| 74 | // |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 75 | switch (clang_decl_name.getNameKind()) { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 76 | // Normal identifiers. |
| 77 | case DeclarationName::Identifier: |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 78 | if (clang_decl_name.getAsIdentifierInfo()->getBuiltinID() != 0) |
| 79 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
| 80 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | |
| 82 | // Operator names. Not important for now. |
| 83 | case DeclarationName::CXXOperatorName: |
| 84 | case DeclarationName::CXXLiteralOperatorName: |
| 85 | return DeclContext::lookup_result(); |
| 86 | |
| 87 | // Using directives found in this context. |
| 88 | // Tell Sema we didn't find any or we'll end up getting asked a *lot*. |
| 89 | case DeclarationName::CXXUsingDirective: |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 90 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 92 | case DeclarationName::ObjCZeroArgSelector: |
| 93 | case DeclarationName::ObjCOneArgSelector: |
| 94 | case DeclarationName::ObjCMultiArgSelector: |
Sean Callanan | 9b71484 | 2011-11-09 19:33:21 +0000 | [diff] [blame] | 95 | { |
| 96 | llvm::SmallVector<NamedDecl*, 1> method_decls; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 97 | |
Sean Callanan | 9b71484 | 2011-11-09 19:33:21 +0000 | [diff] [blame] | 98 | NameSearchContext method_search_context (*this, method_decls, clang_decl_name, decl_ctx); |
| 99 | |
| 100 | FindObjCMethodDecls(method_search_context); |
| 101 | |
| 102 | return SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, method_decls); |
| 103 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 104 | // These aren't possible in the global context. |
| 105 | case DeclarationName::CXXConstructorName: |
| 106 | case DeclarationName::CXXDestructorName: |
| 107 | case DeclarationName::CXXConversionFunctionName: |
| 108 | return DeclContext::lookup_result(); |
| 109 | } |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 110 | |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 111 | |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 112 | if (!GetLookupsEnabled()) |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 113 | { |
| 114 | // Wait until we see a '$' at the start of a name before we start doing |
| 115 | // any lookups so we can avoid lookup up all of the builtin types. |
| 116 | if (!decl_name.empty() && decl_name[0] == '$') |
| 117 | { |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 118 | SetLookupsEnabled (true); |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 119 | } |
| 120 | else |
| 121 | { |
| 122 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
| 123 | } |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 124 | } |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 125 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 126 | ConstString const_decl_name(decl_name.c_str()); |
Greg Clayton | 9ceed1e | 2010-11-13 04:18:24 +0000 | [diff] [blame] | 127 | |
| 128 | const char *uniqued_const_decl_name = const_decl_name.GetCString(); |
| 129 | if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end()) |
| 130 | { |
| 131 | // We are currently looking up this name... |
| 132 | return DeclContext::lookup_result(); |
| 133 | } |
| 134 | m_active_lookups.insert(uniqued_const_decl_name); |
Greg Clayton | a8b278a | 2010-11-15 01:34:18 +0000 | [diff] [blame] | 135 | // static uint32_t g_depth = 0; |
| 136 | // ++g_depth; |
| 137 | // printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name); |
Greg Clayton | 9ceed1e | 2010-11-13 04:18:24 +0000 | [diff] [blame] | 138 | llvm::SmallVector<NamedDecl*, 4> name_decls; |
| 139 | NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx); |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 140 | FindExternalVisibleDecls(name_search_context); |
Greg Clayton | 9ceed1e | 2010-11-13 04:18:24 +0000 | [diff] [blame] | 141 | DeclContext::lookup_result result (SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls)); |
Greg Clayton | a8b278a | 2010-11-15 01:34:18 +0000 | [diff] [blame] | 142 | // --g_depth; |
Greg Clayton | 9ceed1e | 2010-11-13 04:18:24 +0000 | [diff] [blame] | 143 | m_active_lookups.erase (uniqued_const_decl_name); |
| 144 | return result; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 147 | void |
| 148 | ClangASTSource::CompleteType (TagDecl *tag_decl) |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 149 | { |
| 150 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 151 | |
| 152 | if (log) |
| 153 | { |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 154 | log->Printf(" [CompleteTagDecl] on (ASTContext*)%p Completing a TagDecl named %s", m_ast_context, tag_decl->getName().str().c_str()); |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 155 | log->Printf(" [CTD] Before:"); |
| 156 | ASTDumper dumper((Decl*)tag_decl); |
| 157 | dumper.ToLog(log, " [CTD] "); |
| 158 | } |
| 159 | |
| 160 | m_ast_importer->CompleteTagDecl (tag_decl); |
| 161 | |
| 162 | if (log) |
| 163 | { |
| 164 | log->Printf(" [CTD] After:"); |
| 165 | ASTDumper dumper((Decl*)tag_decl); |
| 166 | dumper.ToLog(log, " [CTD] "); |
| 167 | } |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 171 | ClangASTSource::CompleteType (clang::ObjCInterfaceDecl *interface_decl) |
| 172 | { |
| 173 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 174 | |
| 175 | if (log) |
| 176 | { |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 177 | log->Printf(" [CompleteObjCInterfaceDecl] on (ASTContext*)%p Completing an ObjCInterfaceDecl named %s", m_ast_context, interface_decl->getName().str().c_str()); |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 178 | log->Printf(" [COID] Before:"); |
| 179 | ASTDumper dumper((Decl*)interface_decl); |
| 180 | dumper.ToLog(log, " [COID] "); |
| 181 | } |
| 182 | |
| 183 | m_ast_importer->CompleteObjCInterfaceDecl (interface_decl); |
| 184 | |
| 185 | if (log) |
| 186 | { |
| 187 | log->Printf(" [COID] After:"); |
| 188 | ASTDumper dumper((Decl*)interface_decl); |
| 189 | dumper.ToLog(log, " [COID] "); |
| 190 | } |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Sean Callanan | 9b6898f | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 193 | clang::ExternalLoadResult |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 194 | ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context, |
| 195 | bool (*predicate)(Decl::Kind), |
| 196 | llvm::SmallVectorImpl<Decl*> &decls) |
| 197 | { |
| 198 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 199 | |
| 200 | const Decl *context_decl = dyn_cast<Decl>(decl_context); |
| 201 | |
| 202 | if (!context_decl) |
| 203 | return ELR_Failure; |
| 204 | |
| 205 | static unsigned int invocation_id = 0; |
| 206 | unsigned int current_id = invocation_id++; |
| 207 | |
| 208 | if (log) |
| 209 | { |
| 210 | if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl)) |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 211 | log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p with %s predicate", |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 212 | current_id, |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 213 | m_ast_context, |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 214 | context_named_decl->getNameAsString().c_str(), |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 215 | context_decl->getDeclKindName(), |
| 216 | context_decl, |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 217 | (predicate ? "non-null" : "null")); |
| 218 | else if(context_decl) |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 219 | log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%sDecl*)%p with %s predicate", |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 220 | current_id, |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 221 | m_ast_context, |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 222 | context_decl->getDeclKindName(), |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 223 | context_decl, |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 224 | (predicate ? "non-null" : "null")); |
| 225 | else |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 226 | log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context with %s predicate", |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 227 | current_id, |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 228 | m_ast_context, |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 229 | (predicate ? "non-null" : "null")); |
| 230 | } |
| 231 | |
| 232 | Decl *original_decl = NULL; |
| 233 | ASTContext *original_ctx = NULL; |
| 234 | |
| 235 | if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx)) |
| 236 | return ELR_Failure; |
| 237 | |
| 238 | if (log) |
| 239 | { |
| 240 | log->Printf(" FELD[%u] Original decl:", current_id); |
| 241 | ASTDumper(original_decl).ToLog(log, " "); |
| 242 | } |
| 243 | |
| 244 | if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl)) |
| 245 | { |
| 246 | ExternalASTSource *external_source = original_ctx->getExternalSource(); |
| 247 | |
| 248 | if (external_source) |
| 249 | external_source->CompleteType (original_tag_decl); |
| 250 | } |
| 251 | |
| 252 | DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl); |
| 253 | |
| 254 | if (!original_decl_context) |
| 255 | return ELR_Failure; |
| 256 | |
| 257 | for (TagDecl::decl_iterator iter = original_decl_context->decls_begin(); |
| 258 | iter != original_decl_context->decls_end(); |
| 259 | ++iter) |
| 260 | { |
| 261 | Decl *decl = *iter; |
| 262 | |
| 263 | if (!predicate || predicate(decl->getKind())) |
| 264 | { |
| 265 | if (log) |
| 266 | { |
| 267 | ASTDumper ast_dumper(decl); |
| 268 | if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl)) |
| 269 | log->Printf(" FELD[%d] Adding [to %s] lexical decl %s", current_id, context_named_decl->getNameAsString().c_str(), ast_dumper.GetCString()); |
| 270 | else |
| 271 | log->Printf(" FELD[%d] Adding lexical decl %s", current_id, ast_dumper.GetCString()); |
| 272 | } |
| 273 | |
Sean Callanan | 4938bd6 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 274 | Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, original_ctx, decl); |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 275 | |
| 276 | decls.push_back(copied_decl); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | return ELR_AlreadyLoaded; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 283 | void |
| 284 | ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context) |
| 285 | { |
| 286 | assert (m_ast_context); |
| 287 | |
| 288 | const ConstString name(context.m_decl_name.getAsString().c_str()); |
| 289 | |
| 290 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 291 | |
| 292 | static unsigned int invocation_id = 0; |
| 293 | unsigned int current_id = invocation_id++; |
| 294 | |
| 295 | if (log) |
| 296 | { |
| 297 | if (!context.m_decl_context) |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 298 | log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in a NULL DeclContext", current_id, m_ast_context, name.GetCString()); |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 299 | else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context)) |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 300 | log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in '%s'", current_id, m_ast_context, name.GetCString(), context_named_decl->getNameAsString().c_str()); |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 301 | else |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 302 | log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in a '%s'", current_id, m_ast_context, name.GetCString(), context.m_decl_context->getDeclKindName()); |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap); |
| 306 | |
| 307 | if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context)) |
| 308 | { |
| 309 | ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context); |
| 310 | |
| 311 | if (log && log->GetVerbose()) |
| 312 | log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)", |
| 313 | current_id, |
| 314 | namespace_map.get(), |
| 315 | (int)namespace_map->size()); |
| 316 | |
| 317 | if (!namespace_map) |
| 318 | return; |
| 319 | |
| 320 | for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end(); |
| 321 | i != e; |
| 322 | ++i) |
| 323 | { |
| 324 | if (log) |
| 325 | log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s", |
| 326 | current_id, |
| 327 | i->second.GetNamespaceDecl()->getNameAsString().c_str(), |
| 328 | i->first->GetFileSpec().GetFilename().GetCString()); |
| 329 | |
| 330 | FindExternalVisibleDecls(context, |
| 331 | i->first, |
| 332 | i->second, |
| 333 | current_id); |
| 334 | } |
| 335 | } |
Sean Callanan | d3812fa | 2011-11-15 21:50:18 +0000 | [diff] [blame] | 336 | else if (isa<ObjCInterfaceDecl>(context.m_decl_context)) |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 337 | { |
| 338 | FindObjCPropertyDecls(context); |
| 339 | } |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 340 | else if (!isa<TranslationUnitDecl>(context.m_decl_context)) |
| 341 | { |
| 342 | // we shouldn't be getting FindExternalVisibleDecls calls for these |
| 343 | return; |
| 344 | } |
| 345 | else |
| 346 | { |
| 347 | ClangNamespaceDecl namespace_decl; |
| 348 | |
| 349 | if (log) |
| 350 | log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id); |
| 351 | |
| 352 | FindExternalVisibleDecls(context, |
| 353 | lldb::ModuleSP(), |
| 354 | namespace_decl, |
| 355 | current_id); |
| 356 | } |
| 357 | |
| 358 | if (!context.m_namespace_map->empty()) |
| 359 | { |
| 360 | if (log && log->GetVerbose()) |
| 361 | log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)", |
| 362 | current_id, |
| 363 | context.m_namespace_map.get(), |
| 364 | (int)context.m_namespace_map->size()); |
| 365 | |
| 366 | NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map); |
| 367 | |
| 368 | if (clang_namespace_decl) |
| 369 | clang_namespace_decl->setHasExternalVisibleStorage(); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | void |
| 374 | ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context, |
| 375 | lldb::ModuleSP module_sp, |
| 376 | ClangNamespaceDecl &namespace_decl, |
| 377 | unsigned int current_id) |
| 378 | { |
| 379 | assert (m_ast_context); |
| 380 | |
| 381 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 382 | |
| 383 | SymbolContextList sc_list; |
| 384 | |
| 385 | const ConstString name(context.m_decl_name.getAsString().c_str()); |
| 386 | |
| 387 | const char *name_unique_cstr = name.GetCString(); |
| 388 | |
| 389 | if (name_unique_cstr == NULL) |
| 390 | return; |
| 391 | |
| 392 | // The ClangASTSource is not responsible for finding $-names. |
| 393 | if (name_unique_cstr[0] == '$') |
| 394 | return; |
| 395 | |
| 396 | if (module_sp && namespace_decl) |
| 397 | { |
| 398 | ClangNamespaceDecl found_namespace_decl; |
| 399 | |
| 400 | SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor(); |
| 401 | |
| 402 | if (symbol_vendor) |
| 403 | { |
| 404 | SymbolContext null_sc; |
| 405 | |
| 406 | found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl); |
| 407 | |
| 408 | if (found_namespace_decl) |
| 409 | { |
| 410 | context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl)); |
| 411 | |
| 412 | if (log) |
| 413 | log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s", |
| 414 | current_id, |
| 415 | name.GetCString(), |
| 416 | module_sp->GetFileSpec().GetFilename().GetCString()); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | else |
| 421 | { |
| 422 | ModuleList &images = m_target->GetImages(); |
| 423 | |
| 424 | for (uint32_t i = 0, e = images.GetSize(); |
| 425 | i != e; |
| 426 | ++i) |
| 427 | { |
| 428 | lldb::ModuleSP image = images.GetModuleAtIndex(i); |
| 429 | |
| 430 | if (!image) |
| 431 | continue; |
| 432 | |
| 433 | ClangNamespaceDecl found_namespace_decl; |
| 434 | |
| 435 | SymbolVendor *symbol_vendor = image->GetSymbolVendor(); |
| 436 | |
| 437 | if (!symbol_vendor) |
| 438 | continue; |
| 439 | |
| 440 | SymbolContext null_sc; |
| 441 | |
| 442 | found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl); |
| 443 | |
| 444 | if (found_namespace_decl) |
| 445 | { |
| 446 | context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl)); |
| 447 | |
| 448 | if (log) |
| 449 | log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s", |
| 450 | current_id, |
| 451 | name.GetCString(), |
| 452 | image->GetFileSpec().GetFilename().GetCString()); |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | static ConstString id_name("id"); |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 458 | static ConstString Class_name("Class"); |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 459 | |
| 460 | do |
| 461 | { |
| 462 | TypeList types; |
| 463 | SymbolContext null_sc; |
| 464 | |
| 465 | if (module_sp && namespace_decl) |
| 466 | module_sp->FindTypes(null_sc, name, &namespace_decl, true, 1, types); |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 467 | else if(name != id_name && name != Class_name) |
Sean Callanan | e1301a6 | 2011-12-06 03:41:14 +0000 | [diff] [blame] | 468 | m_target->GetImages().FindTypes(null_sc, name, true, 1, types); |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 469 | else |
| 470 | break; |
| 471 | |
| 472 | if (types.GetSize()) |
| 473 | { |
| 474 | lldb::TypeSP type_sp = types.GetTypeAtIndex(0); |
| 475 | |
| 476 | if (log) |
| 477 | { |
| 478 | const char *name_string = type_sp->GetName().GetCString(); |
| 479 | |
| 480 | log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s", |
| 481 | current_id, |
| 482 | name.GetCString(), |
| 483 | (name_string ? name_string : "<anonymous>")); |
| 484 | } |
| 485 | |
Sean Callanan | e1301a6 | 2011-12-06 03:41:14 +0000 | [diff] [blame] | 486 | |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 487 | void *copied_type = GuardedCopyType(m_ast_context, type_sp->GetClangAST(), type_sp->GetClangFullType()); |
Sean Callanan | e1301a6 | 2011-12-06 03:41:14 +0000 | [diff] [blame] | 488 | |
Sean Callanan | dc5fce1 | 2011-12-01 21:04:37 +0000 | [diff] [blame] | 489 | if (!copied_type) |
| 490 | { |
| 491 | if (log) |
Sean Callanan | e1301a6 | 2011-12-06 03:41:14 +0000 | [diff] [blame] | 492 | log->Printf(" CAS::FEVD[%u] - Couldn't export the type for a constant integer result", |
| 493 | current_id); |
| 494 | |
Sean Callanan | dc5fce1 | 2011-12-01 21:04:37 +0000 | [diff] [blame] | 495 | break; |
| 496 | } |
| 497 | |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 498 | context.AddTypeDecl(copied_type); |
| 499 | } |
Sean Callanan | 673f3db | 2011-11-30 22:11:59 +0000 | [diff] [blame] | 500 | |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 501 | } while(0); |
| 502 | } |
| 503 | |
Sean Callanan | 9b71484 | 2011-11-09 19:33:21 +0000 | [diff] [blame] | 504 | void |
| 505 | ClangASTSource::FindObjCMethodDecls (NameSearchContext &context) |
| 506 | { |
| 507 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 508 | |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 509 | static unsigned int invocation_id = 0; |
| 510 | unsigned int current_id = invocation_id++; |
| 511 | |
Sean Callanan | 9b71484 | 2011-11-09 19:33:21 +0000 | [diff] [blame] | 512 | const DeclarationName &decl_name(context.m_decl_name); |
| 513 | const DeclContext *decl_ctx(context.m_decl_context); |
| 514 | |
| 515 | const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx); |
| 516 | |
| 517 | if (!interface_decl) |
| 518 | return; |
| 519 | |
| 520 | StreamString ss; |
Sean Callanan | e1301a6 | 2011-12-06 03:41:14 +0000 | [diff] [blame] | 521 | |
Sean Callanan | 9b71484 | 2011-11-09 19:33:21 +0000 | [diff] [blame] | 522 | if (decl_name.isObjCZeroArgSelector()) |
| 523 | { |
| 524 | ss.Printf("%s", decl_name.getAsString().c_str()); |
| 525 | } |
| 526 | else if (decl_name.isObjCOneArgSelector()) |
| 527 | { |
Sean Callanan | 1d9ffe2 | 2011-11-14 18:29:46 +0000 | [diff] [blame] | 528 | ss.Printf("%s", decl_name.getAsString().c_str()); |
Sean Callanan | 9b71484 | 2011-11-09 19:33:21 +0000 | [diff] [blame] | 529 | } |
| 530 | else |
| 531 | { |
| 532 | clang::Selector sel = decl_name.getObjCSelector(); |
| 533 | |
| 534 | for (unsigned i = 0, e = sel.getNumArgs(); |
| 535 | i != e; |
| 536 | ++i) |
| 537 | { |
| 538 | llvm::StringRef r = sel.getNameForSlot(i); |
| 539 | ss.Printf("%s:", r.str().c_str()); |
| 540 | } |
| 541 | } |
| 542 | ss.Flush(); |
| 543 | |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 544 | ConstString selector_name(ss.GetData()); |
| 545 | |
Sean Callanan | 9b71484 | 2011-11-09 19:33:21 +0000 | [diff] [blame] | 546 | if (log) |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 547 | log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]", |
| 548 | current_id, |
| 549 | m_ast_context, |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 550 | interface_decl->getNameAsString().c_str(), |
| 551 | selector_name.AsCString()); |
| 552 | |
Sean Callanan | e1301a6 | 2011-12-06 03:41:14 +0000 | [diff] [blame] | 553 | ClangASTImporter::ObjCInterfaceMapSP interface_map = m_ast_importer->GetObjCInterfaceMap(interface_decl); |
| 554 | |
| 555 | if (interface_map) |
| 556 | { |
| 557 | for (ClangASTImporter::ObjCInterfaceMap::iterator i = interface_map->begin(), e = interface_map->end(); |
| 558 | i != e; |
| 559 | ++i) |
| 560 | { |
| 561 | lldb::clang_type_t backing_type = i->GetOpaqueQualType(); |
| 562 | |
| 563 | if (!backing_type) |
| 564 | continue; |
| 565 | |
| 566 | QualType backing_qual_type = QualType::getFromOpaquePtr(backing_type); |
| 567 | |
| 568 | const ObjCInterfaceType *backing_interface_type = dyn_cast<ObjCInterfaceType>(backing_qual_type.getTypePtr()); |
| 569 | |
| 570 | if (!backing_interface_type) |
| 571 | continue; |
| 572 | |
| 573 | const ObjCInterfaceDecl *backing_interface_decl = backing_interface_type->getDecl(); |
| 574 | |
| 575 | if (!backing_interface_decl) |
| 576 | continue; |
| 577 | |
| 578 | if (backing_interface_decl->decls_begin() == backing_interface_decl->decls_end()) |
| 579 | continue; // don't waste time creating a DeclarationName here |
| 580 | |
| 581 | clang::ASTContext &backing_ast_context = backing_interface_decl->getASTContext(); |
| 582 | |
| 583 | llvm::SmallVector<clang::IdentifierInfo *, 3> selector_components; |
Sean Callanan | 3d8540a | 2011-12-07 22:39:39 +0000 | [diff] [blame] | 584 | int num_arguments = 0; |
Sean Callanan | e1301a6 | 2011-12-06 03:41:14 +0000 | [diff] [blame] | 585 | |
| 586 | if (decl_name.isObjCZeroArgSelector()) |
| 587 | { |
| 588 | selector_components.push_back (&backing_ast_context.Idents.get(decl_name.getAsString().c_str())); |
| 589 | } |
| 590 | else if (decl_name.isObjCOneArgSelector()) |
| 591 | { |
| 592 | selector_components.push_back (&backing_ast_context.Idents.get(decl_name.getAsString().c_str())); |
Sean Callanan | 3d8540a | 2011-12-07 22:39:39 +0000 | [diff] [blame] | 593 | num_arguments = 1; |
Sean Callanan | e1301a6 | 2011-12-06 03:41:14 +0000 | [diff] [blame] | 594 | } |
| 595 | else |
| 596 | { |
| 597 | clang::Selector sel = decl_name.getObjCSelector(); |
| 598 | |
| 599 | for (unsigned i = 0, e = sel.getNumArgs(); |
| 600 | i != e; |
| 601 | ++i) |
| 602 | { |
| 603 | llvm::StringRef r = sel.getNameForSlot(i); |
| 604 | |
| 605 | selector_components.push_back (&backing_ast_context.Idents.get(r.str().c_str())); |
Sean Callanan | 3d8540a | 2011-12-07 22:39:39 +0000 | [diff] [blame] | 606 | num_arguments++; |
Sean Callanan | e1301a6 | 2011-12-06 03:41:14 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
Sean Callanan | 3d8540a | 2011-12-07 22:39:39 +0000 | [diff] [blame] | 610 | Selector backing_selector = backing_interface_decl->getASTContext().Selectors.getSelector(num_arguments, selector_components.data()); |
Sean Callanan | e1301a6 | 2011-12-06 03:41:14 +0000 | [diff] [blame] | 611 | DeclarationName backing_decl_name = DeclarationName(backing_selector); |
| 612 | |
| 613 | DeclContext::lookup_const_result lookup_result = backing_interface_decl->lookup(backing_decl_name); |
| 614 | |
| 615 | if (lookup_result.first == lookup_result.second) |
| 616 | continue; |
| 617 | |
| 618 | ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(*lookup_result.first); |
| 619 | |
| 620 | if (!method_decl) |
| 621 | continue; |
| 622 | |
| 623 | Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &backing_ast_context, *lookup_result.first); |
| 624 | |
Sean Callanan | 3d8540a | 2011-12-07 22:39:39 +0000 | [diff] [blame] | 625 | if (!copied_decl) |
| 626 | { |
Greg Clayton | 2e87f6a | 2011-12-08 01:32:28 +0000 | [diff] [blame^] | 627 | if (log) |
| 628 | log->Printf(" CAS::FOMD[%d] couldn't import method from symbols", current_id); |
Sean Callanan | 3d8540a | 2011-12-07 22:39:39 +0000 | [diff] [blame] | 629 | continue; |
| 630 | } |
| 631 | |
Sean Callanan | e1301a6 | 2011-12-06 03:41:14 +0000 | [diff] [blame] | 632 | ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl> (copied_decl); |
| 633 | |
| 634 | if (!copied_method_decl) |
| 635 | continue; |
| 636 | |
| 637 | if (log) |
| 638 | { |
| 639 | ASTDumper dumper((Decl*)copied_method_decl); |
| 640 | log->Printf(" CAS::FOMD[%d] found (in symbols) %s", current_id, dumper.GetCString()); |
| 641 | } |
| 642 | |
| 643 | context.AddNamedDecl(copied_method_decl); |
| 644 | } |
| 645 | } |
| 646 | |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 647 | SymbolContextList sc_list; |
| 648 | |
| 649 | const bool include_symbols = false; |
| 650 | const bool append = false; |
| 651 | |
| 652 | m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, append, sc_list); |
| 653 | |
| 654 | for (uint32_t i = 0, e = sc_list.GetSize(); |
| 655 | i != e; |
| 656 | ++i) |
| 657 | { |
| 658 | SymbolContext sc; |
| 659 | |
| 660 | if (!sc_list.GetContextAtIndex(i, sc)) |
| 661 | continue; |
| 662 | |
| 663 | if (!sc.function) |
| 664 | continue; |
| 665 | |
| 666 | DeclContext *function_ctx = sc.function->GetClangDeclContext(); |
| 667 | |
| 668 | if (!function_ctx) |
| 669 | continue; |
| 670 | |
| 671 | ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx); |
| 672 | |
| 673 | if (!method_decl) |
| 674 | continue; |
| 675 | |
| 676 | ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface(); |
| 677 | |
| 678 | if (!found_interface_decl) |
| 679 | continue; |
| 680 | |
| 681 | if (found_interface_decl->getName() == interface_decl->getName()) |
| 682 | { |
Sean Callanan | 4938bd6 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 683 | Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl); |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 684 | |
| 685 | if (!copied_decl) |
| 686 | continue; |
| 687 | |
| 688 | ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl); |
| 689 | |
| 690 | if (!copied_method_decl) |
| 691 | continue; |
| 692 | |
| 693 | if (log) |
| 694 | { |
| 695 | ASTDumper dumper((Decl*)copied_method_decl); |
Sean Callanan | e1301a6 | 2011-12-06 03:41:14 +0000 | [diff] [blame] | 696 | log->Printf(" CAS::FOMD[%d] found (in debug info) %s", current_id, dumper.GetCString()); |
Sean Callanan | f2a0a5c | 2011-11-11 20:37:26 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | context.AddNamedDecl(copied_method_decl); |
| 700 | } |
| 701 | } |
Sean Callanan | 9b71484 | 2011-11-09 19:33:21 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 704 | void |
| 705 | ClangASTSource::FindObjCPropertyDecls (NameSearchContext &context) |
| 706 | { |
| 707 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 708 | |
| 709 | static unsigned int invocation_id = 0; |
| 710 | unsigned int current_id = invocation_id++; |
| 711 | |
| 712 | const ObjCInterfaceDecl *iface_decl = cast<ObjCInterfaceDecl>(context.m_decl_context); |
| 713 | Decl *orig_decl; |
| 714 | ASTContext *orig_ast_ctx; |
| 715 | |
| 716 | m_ast_importer->ResolveDeclOrigin(iface_decl, &orig_decl, &orig_ast_ctx); |
| 717 | |
| 718 | if (!orig_decl) |
| 719 | return; |
| 720 | |
| 721 | ObjCInterfaceDecl *orig_iface_decl = dyn_cast<ObjCInterfaceDecl>(orig_decl); |
| 722 | |
| 723 | if (!orig_iface_decl) |
| 724 | return; |
| 725 | |
| 726 | if (!ClangASTContext::GetCompleteDecl(orig_ast_ctx, orig_iface_decl)) |
| 727 | return; |
| 728 | |
| 729 | std::string property_name_str = context.m_decl_name.getAsString(); |
| 730 | StringRef property_name(property_name_str.c_str()); |
| 731 | ObjCPropertyDecl *property_decl = orig_iface_decl->FindPropertyDeclaration(&orig_ast_ctx->Idents.get(property_name)); |
| 732 | |
| 733 | if (log) |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 734 | log->Printf("ClangASTSource::FindObjCPropertyDecls[%d] on (ASTContext*)%p for property '%s.%s'", |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 735 | current_id, |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 736 | m_ast_context, |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 737 | iface_decl->getNameAsString().c_str(), |
| 738 | property_name_str.c_str()); |
| 739 | |
| 740 | if (!property_decl) |
| 741 | return; |
| 742 | |
Sean Callanan | 4938bd6 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 743 | Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, orig_ast_ctx, property_decl); |
Sean Callanan | e6ea5fe | 2011-11-15 02:11:17 +0000 | [diff] [blame] | 744 | |
| 745 | if (!copied_decl) |
| 746 | return; |
| 747 | |
| 748 | ObjCPropertyDecl *copied_property_decl = dyn_cast<ObjCPropertyDecl>(copied_decl); |
| 749 | |
| 750 | if (!copied_property_decl) |
| 751 | return; |
| 752 | |
| 753 | if (log) |
| 754 | { |
| 755 | ASTDumper dumper((Decl*)copied_property_decl); |
| 756 | log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString()); |
| 757 | } |
| 758 | |
| 759 | context.AddNamedDecl(copied_property_decl); |
| 760 | } |
| 761 | |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 762 | void |
| 763 | ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map, |
| 764 | const ConstString &name, |
| 765 | ClangASTImporter::NamespaceMapSP &parent_map) const |
| 766 | { |
| 767 | static unsigned int invocation_id = 0; |
| 768 | unsigned int current_id = invocation_id++; |
| 769 | |
| 770 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 771 | |
| 772 | if (log) |
| 773 | { |
| 774 | if (parent_map && parent_map->size()) |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 775 | log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s", |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 776 | current_id, |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 777 | m_ast_context, |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 778 | name.GetCString(), |
| 779 | parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str()); |
| 780 | else |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 781 | log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s", |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 782 | current_id, |
Sean Callanan | 5a55c7a | 2011-11-18 03:28:09 +0000 | [diff] [blame] | 783 | m_ast_context, |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 784 | name.GetCString()); |
| 785 | } |
| 786 | |
| 787 | |
| 788 | if (parent_map) |
| 789 | { |
| 790 | for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end(); |
| 791 | i != e; |
| 792 | ++i) |
| 793 | { |
| 794 | ClangNamespaceDecl found_namespace_decl; |
| 795 | |
| 796 | lldb::ModuleSP module_sp = i->first; |
| 797 | ClangNamespaceDecl module_parent_namespace_decl = i->second; |
| 798 | |
| 799 | SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor(); |
| 800 | |
| 801 | if (!symbol_vendor) |
| 802 | continue; |
| 803 | |
| 804 | SymbolContext null_sc; |
| 805 | |
| 806 | found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl); |
| 807 | |
| 808 | if (!found_namespace_decl) |
| 809 | continue; |
| 810 | |
| 811 | namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl)); |
| 812 | |
| 813 | if (log) |
| 814 | log->Printf(" CMN[%u] Found namespace %s in module %s", |
| 815 | current_id, |
| 816 | name.GetCString(), |
| 817 | module_sp->GetFileSpec().GetFilename().GetCString()); |
| 818 | } |
| 819 | } |
| 820 | else |
| 821 | { |
| 822 | ModuleList &images = m_target->GetImages(); |
| 823 | ClangNamespaceDecl null_namespace_decl; |
| 824 | |
| 825 | for (uint32_t i = 0, e = images.GetSize(); |
| 826 | i != e; |
| 827 | ++i) |
| 828 | { |
| 829 | lldb::ModuleSP image = images.GetModuleAtIndex(i); |
| 830 | |
| 831 | if (!image) |
| 832 | continue; |
| 833 | |
| 834 | ClangNamespaceDecl found_namespace_decl; |
| 835 | |
| 836 | SymbolVendor *symbol_vendor = image->GetSymbolVendor(); |
| 837 | |
| 838 | if (!symbol_vendor) |
| 839 | continue; |
| 840 | |
| 841 | SymbolContext null_sc; |
| 842 | |
| 843 | found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl); |
| 844 | |
| 845 | if (!found_namespace_decl) |
| 846 | continue; |
| 847 | |
| 848 | namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl)); |
| 849 | |
| 850 | if (log) |
| 851 | log->Printf(" CMN[%u] Found namespace %s in module %s", |
| 852 | current_id, |
| 853 | name.GetCString(), |
| 854 | image->GetFileSpec().GetFilename().GetCString()); |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | |
Sean Callanan | e1301a6 | 2011-12-06 03:41:14 +0000 | [diff] [blame] | 859 | void |
| 860 | ClangASTSource::CompleteObjCInterfaceMap (ClangASTImporter::ObjCInterfaceMapSP &objc_interface_map, |
| 861 | const ConstString &name) const |
| 862 | { |
| 863 | SymbolContext null_sc; |
| 864 | |
| 865 | TypeList types; |
| 866 | |
| 867 | m_target->GetImages().FindTypes(null_sc, name, true, UINT32_MAX, types); |
| 868 | |
| 869 | for (uint32_t i = 0, e = types.GetSize(); |
| 870 | i != e; |
| 871 | ++i) |
| 872 | { |
| 873 | lldb::TypeSP mapped_type_sp = types.GetTypeAtIndex(i); |
| 874 | |
| 875 | if (!mapped_type_sp || !mapped_type_sp->GetClangFullType()) |
| 876 | continue; |
| 877 | |
| 878 | objc_interface_map->push_back (ClangASTType(mapped_type_sp->GetClangAST(), mapped_type_sp->GetClangFullType())); |
| 879 | } |
| 880 | } |
| 881 | |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 882 | NamespaceDecl * |
| 883 | ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls) |
| 884 | { |
| 885 | if (namespace_decls.empty()) |
| 886 | return NULL; |
| 887 | |
| 888 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 889 | |
| 890 | const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second; |
| 891 | |
Sean Callanan | 4938bd6 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 892 | Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl()); |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame] | 893 | |
| 894 | NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl); |
| 895 | |
| 896 | m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls); |
| 897 | |
| 898 | return dyn_cast<NamespaceDecl>(copied_decl); |
| 899 | } |
| 900 | |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 901 | void * |
| 902 | ClangASTSource::GuardedCopyType (ASTContext *dest_context, |
| 903 | ASTContext *source_context, |
| 904 | void *clang_type) |
| 905 | { |
| 906 | SetImportInProgress(true); |
| 907 | |
Sean Callanan | 4938bd6 | 2011-11-16 18:20:47 +0000 | [diff] [blame] | 908 | QualType ret_qual_type = m_ast_importer->CopyType (m_ast_context, source_context, QualType::getFromOpaquePtr(clang_type)); |
Sean Callanan | 9394b5a | 2011-10-29 19:50:43 +0000 | [diff] [blame] | 909 | |
| 910 | void *ret = ret_qual_type.getAsOpaquePtr(); |
| 911 | |
| 912 | SetImportInProgress(false); |
| 913 | |
| 914 | return ret; |
| 915 | } |
| 916 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 917 | clang::NamedDecl * |
| 918 | NameSearchContext::AddVarDecl(void *type) |
| 919 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 920 | IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo(); |
Sean Callanan | 1ddd9fe | 2010-11-30 00:27:43 +0000 | [diff] [blame] | 921 | |
| 922 | assert (type && "Type for variable must be non-NULL!"); |
Sean Callanan | cc07462 | 2010-09-14 21:59:34 +0000 | [diff] [blame] | 923 | |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 924 | clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context, |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 925 | const_cast<DeclContext*>(m_decl_context), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 926 | SourceLocation(), |
Sean Callanan | 279584c | 2011-03-15 00:17:19 +0000 | [diff] [blame] | 927 | SourceLocation(), |
Sean Callanan | cc07462 | 2010-09-14 21:59:34 +0000 | [diff] [blame] | 928 | ii, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 929 | QualType::getFromOpaquePtr(type), |
| 930 | 0, |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 931 | SC_Static, |
| 932 | SC_Static); |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 933 | m_decls.push_back(Decl); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 934 | |
| 935 | return Decl; |
| 936 | } |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 937 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 938 | clang::NamedDecl * |
| 939 | NameSearchContext::AddFunDecl (void *type) |
| 940 | { |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 941 | clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context, |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 942 | const_cast<DeclContext*>(m_decl_context), |
| 943 | SourceLocation(), |
Sean Callanan | 279584c | 2011-03-15 00:17:19 +0000 | [diff] [blame] | 944 | SourceLocation(), |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 945 | m_decl_name.getAsIdentifierInfo(), |
| 946 | QualType::getFromOpaquePtr(type), |
| 947 | NULL, |
| 948 | SC_Static, |
| 949 | SC_Static, |
| 950 | false, |
| 951 | true); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 952 | |
Sean Callanan | b291abe | 2010-08-12 23:45:38 +0000 | [diff] [blame] | 953 | // We have to do more than just synthesize the FunctionDecl. We have to |
| 954 | // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do |
| 955 | // this, we raid the function's FunctionProtoType for types. |
| 956 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 957 | QualType qual_type (QualType::getFromOpaquePtr(type)); |
Sean Callanan | dc5fce1 | 2011-12-01 21:04:37 +0000 | [diff] [blame] | 958 | const FunctionProtoType *func_proto_type = dyn_cast<FunctionProtoType>(qual_type.getTypePtr()); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 959 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 960 | if (func_proto_type) |
Sean Callanan | 3c821cc | 2010-06-23 18:58:10 +0000 | [diff] [blame] | 961 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 962 | unsigned NumArgs = func_proto_type->getNumArgs(); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 963 | unsigned ArgIndex; |
| 964 | |
Sean Callanan | c153518 | 2011-10-07 23:18:13 +0000 | [diff] [blame] | 965 | SmallVector<ParmVarDecl *, 5> parm_var_decls; |
| 966 | |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 967 | for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex) |
| 968 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 969 | QualType arg_qual_type (func_proto_type->getArgType(ArgIndex)); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 970 | |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 971 | parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context, |
Sean Callanan | c153518 | 2011-10-07 23:18:13 +0000 | [diff] [blame] | 972 | const_cast<DeclContext*>(m_decl_context), |
| 973 | SourceLocation(), |
| 974 | SourceLocation(), |
| 975 | NULL, |
| 976 | arg_qual_type, |
| 977 | NULL, |
| 978 | SC_Static, |
| 979 | SC_Static, |
| 980 | NULL)); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Sean Callanan | c153518 | 2011-10-07 23:18:13 +0000 | [diff] [blame] | 983 | func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls)); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 984 | } |
Sean Callanan | dc5fce1 | 2011-12-01 21:04:37 +0000 | [diff] [blame] | 985 | else |
| 986 | { |
| 987 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 988 | |
| 989 | log->Printf("Function type wasn't a FunctionProtoType"); |
| 990 | } |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 991 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 992 | m_decls.push_back(func_decl); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 993 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 994 | return func_decl; |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 995 | } |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 996 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 997 | clang::NamedDecl * |
| 998 | NameSearchContext::AddGenericFunDecl() |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 999 | { |
Sean Callanan | ad29309 | 2011-01-18 23:32:05 +0000 | [diff] [blame] | 1000 | FunctionProtoType::ExtProtoInfo proto_info; |
| 1001 | |
| 1002 | proto_info.Variadic = true; |
| 1003 | |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 1004 | QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result |
| 1005 | NULL, // argument types |
| 1006 | 0, // number of arguments |
| 1007 | proto_info)); |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 1008 | |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 1009 | return AddFunDecl(generic_function_type.getAsOpaquePtr()); |
| 1010 | } |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 1011 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 1012 | clang::NamedDecl * |
| 1013 | NameSearchContext::AddTypeDecl(void *type) |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 1014 | { |
Greg Clayton | a1aaaff | 2011-01-23 00:34:52 +0000 | [diff] [blame] | 1015 | if (type) |
| 1016 | { |
| 1017 | QualType qual_type = QualType::getFromOpaquePtr(type); |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 1018 | |
Sean Callanan | d5b3c35 | 2011-01-27 04:42:51 +0000 | [diff] [blame] | 1019 | if (const TagType *tag_type = dyn_cast<clang::TagType>(qual_type)) |
Greg Clayton | a1aaaff | 2011-01-23 00:34:52 +0000 | [diff] [blame] | 1020 | { |
| 1021 | TagDecl *tag_decl = tag_type->getDecl(); |
| 1022 | |
| 1023 | m_decls.push_back(tag_decl); |
| 1024 | |
| 1025 | return tag_decl; |
| 1026 | } |
Sean Callanan | d5b3c35 | 2011-01-27 04:42:51 +0000 | [diff] [blame] | 1027 | else if (const ObjCObjectType *objc_object_type = dyn_cast<clang::ObjCObjectType>(qual_type)) |
Greg Clayton | a1aaaff | 2011-01-23 00:34:52 +0000 | [diff] [blame] | 1028 | { |
| 1029 | ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface(); |
| 1030 | |
| 1031 | m_decls.push_back((NamedDecl*)interface_decl); |
| 1032 | |
| 1033 | return (NamedDecl*)interface_decl; |
| 1034 | } |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 1035 | } |
Greg Clayton | a1aaaff | 2011-01-23 00:34:52 +0000 | [diff] [blame] | 1036 | return NULL; |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 1037 | } |
Greg Clayton | e6d72ca | 2011-06-25 00:44:06 +0000 | [diff] [blame] | 1038 | |
| 1039 | void |
| 1040 | NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result) |
| 1041 | { |
| 1042 | for (clang::NamedDecl * const *decl_iterator = result.first; |
| 1043 | decl_iterator != result.second; |
| 1044 | ++decl_iterator) |
| 1045 | m_decls.push_back (*decl_iterator); |
| 1046 | } |
| 1047 | |
| 1048 | void |
| 1049 | NameSearchContext::AddNamedDecl (clang::NamedDecl *decl) |
| 1050 | { |
| 1051 | m_decls.push_back (decl); |
| 1052 | } |