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