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