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 | { |
| 27 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 29 | void |
| 30 | ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) |
| 31 | { |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 32 | if (!m_ast_context) |
| 33 | return; |
| 34 | |
| 35 | m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage(); |
| 36 | m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | // The core lookup interface. |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 40 | DeclContext::lookup_result |
| 41 | ClangASTSource::FindExternalVisibleDeclsByName |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 42 | ( |
| 43 | const DeclContext *decl_ctx, |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 44 | DeclarationName clang_decl_name |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 45 | ) |
| 46 | { |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 47 | if (!m_ast_context) |
| 48 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
| 49 | |
| 50 | if (GetImportInProgress()) |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 51 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
| 52 | |
| 53 | std::string decl_name (clang_decl_name.getAsString()); |
| 54 | |
| 55 | // if (m_decl_map.DoingASTImport ()) |
| 56 | // return DeclContext::lookup_result(); |
| 57 | // |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 58 | switch (clang_decl_name.getNameKind()) { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 59 | // Normal identifiers. |
| 60 | case DeclarationName::Identifier: |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 61 | if (clang_decl_name.getAsIdentifierInfo()->getBuiltinID() != 0) |
| 62 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
| 63 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 64 | |
| 65 | // Operator names. Not important for now. |
| 66 | case DeclarationName::CXXOperatorName: |
| 67 | case DeclarationName::CXXLiteralOperatorName: |
| 68 | return DeclContext::lookup_result(); |
| 69 | |
| 70 | // Using directives found in this context. |
| 71 | // Tell Sema we didn't find any or we'll end up getting asked a *lot*. |
| 72 | case DeclarationName::CXXUsingDirective: |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 73 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 74 | |
| 75 | // These aren't looked up like this. |
| 76 | case DeclarationName::ObjCZeroArgSelector: |
| 77 | case DeclarationName::ObjCOneArgSelector: |
| 78 | case DeclarationName::ObjCMultiArgSelector: |
| 79 | return DeclContext::lookup_result(); |
| 80 | |
| 81 | // These aren't possible in the global context. |
| 82 | case DeclarationName::CXXConstructorName: |
| 83 | case DeclarationName::CXXDestructorName: |
| 84 | case DeclarationName::CXXConversionFunctionName: |
| 85 | return DeclContext::lookup_result(); |
| 86 | } |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 87 | |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 88 | |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 89 | if (!GetLookupsEnabled()) |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 90 | { |
| 91 | // Wait until we see a '$' at the start of a name before we start doing |
| 92 | // any lookups so we can avoid lookup up all of the builtin types. |
| 93 | if (!decl_name.empty() && decl_name[0] == '$') |
| 94 | { |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 95 | SetLookupsEnabled (true); |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 96 | } |
| 97 | else |
| 98 | { |
| 99 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
| 100 | } |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 101 | } |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 102 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 103 | ConstString const_decl_name(decl_name.c_str()); |
Greg Clayton | 9ceed1e | 2010-11-13 04:18:24 +0000 | [diff] [blame] | 104 | |
| 105 | const char *uniqued_const_decl_name = const_decl_name.GetCString(); |
| 106 | if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end()) |
| 107 | { |
| 108 | // We are currently looking up this name... |
| 109 | return DeclContext::lookup_result(); |
| 110 | } |
| 111 | m_active_lookups.insert(uniqued_const_decl_name); |
Greg Clayton | a8b278a | 2010-11-15 01:34:18 +0000 | [diff] [blame] | 112 | // static uint32_t g_depth = 0; |
| 113 | // ++g_depth; |
| 114 | // printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name); |
Greg Clayton | 9ceed1e | 2010-11-13 04:18:24 +0000 | [diff] [blame] | 115 | llvm::SmallVector<NamedDecl*, 4> name_decls; |
| 116 | NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx); |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 117 | FindExternalVisibleDecls(name_search_context); |
Greg Clayton | 9ceed1e | 2010-11-13 04:18:24 +0000 | [diff] [blame] | 118 | DeclContext::lookup_result result (SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls)); |
Greg Clayton | a8b278a | 2010-11-15 01:34:18 +0000 | [diff] [blame] | 119 | // --g_depth; |
Greg Clayton | 9ceed1e | 2010-11-13 04:18:24 +0000 | [diff] [blame] | 120 | m_active_lookups.erase (uniqued_const_decl_name); |
| 121 | return result; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 124 | void |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 125 | ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context) |
| 126 | { |
| 127 | } |
| 128 | |
| 129 | void |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 130 | ClangASTSource::CompleteType (TagDecl *tag_decl) |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame^] | 131 | { |
| 132 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 133 | |
| 134 | if (log) |
| 135 | { |
| 136 | log->Printf(" [CompleteTagDecl] Completing a TagDecl named %s", tag_decl->getName().str().c_str()); |
| 137 | log->Printf(" [CTD] Before:"); |
| 138 | ASTDumper dumper((Decl*)tag_decl); |
| 139 | dumper.ToLog(log, " [CTD] "); |
| 140 | } |
| 141 | |
| 142 | m_ast_importer->CompleteTagDecl (tag_decl); |
| 143 | |
| 144 | if (log) |
| 145 | { |
| 146 | log->Printf(" [CTD] After:"); |
| 147 | ASTDumper dumper((Decl*)tag_decl); |
| 148 | dumper.ToLog(log, " [CTD] "); |
| 149 | } |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame^] | 153 | ClangASTSource::CompleteType (clang::ObjCInterfaceDecl *interface_decl) |
| 154 | { |
| 155 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 156 | |
| 157 | if (log) |
| 158 | { |
| 159 | log->Printf(" [CompleteObjCInterfaceDecl] Completing an ObjCInterfaceDecl named %s", interface_decl->getName().str().c_str()); |
| 160 | log->Printf(" [COID] Before:"); |
| 161 | ASTDumper dumper((Decl*)interface_decl); |
| 162 | dumper.ToLog(log, " [COID] "); |
| 163 | } |
| 164 | |
| 165 | m_ast_importer->CompleteObjCInterfaceDecl (interface_decl); |
| 166 | |
| 167 | if (log) |
| 168 | { |
| 169 | log->Printf(" [COID] After:"); |
| 170 | ASTDumper dumper((Decl*)interface_decl); |
| 171 | dumper.ToLog(log, " [COID] "); |
| 172 | } |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Sean Callanan | 9b6898f | 2011-07-30 02:42:06 +0000 | [diff] [blame] | 175 | clang::ExternalLoadResult |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame^] | 176 | ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context, |
| 177 | bool (*predicate)(Decl::Kind), |
| 178 | llvm::SmallVectorImpl<Decl*> &decls) |
| 179 | { |
| 180 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 181 | |
| 182 | const Decl *context_decl = dyn_cast<Decl>(decl_context); |
| 183 | |
| 184 | if (!context_decl) |
| 185 | return ELR_Failure; |
| 186 | |
| 187 | static unsigned int invocation_id = 0; |
| 188 | unsigned int current_id = invocation_id++; |
| 189 | |
| 190 | if (log) |
| 191 | { |
| 192 | if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl)) |
| 193 | log->Printf("FindExternalLexicalDecls[%u] in '%s' (a %s) with %s predicate", |
| 194 | current_id, |
| 195 | context_named_decl->getNameAsString().c_str(), |
| 196 | context_decl->getDeclKindName(), |
| 197 | (predicate ? "non-null" : "null")); |
| 198 | else if(context_decl) |
| 199 | log->Printf("FindExternalLexicalDecls[%u] in a %s with %s predicate", |
| 200 | current_id, |
| 201 | context_decl->getDeclKindName(), |
| 202 | (predicate ? "non-null" : "null")); |
| 203 | else |
| 204 | log->Printf("FindExternalLexicalDecls[%u] in a NULL context with %s predicate", |
| 205 | current_id, |
| 206 | (predicate ? "non-null" : "null")); |
| 207 | } |
| 208 | |
| 209 | Decl *original_decl = NULL; |
| 210 | ASTContext *original_ctx = NULL; |
| 211 | |
| 212 | if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx)) |
| 213 | return ELR_Failure; |
| 214 | |
| 215 | if (log) |
| 216 | { |
| 217 | log->Printf(" FELD[%u] Original decl:", current_id); |
| 218 | ASTDumper(original_decl).ToLog(log, " "); |
| 219 | } |
| 220 | |
| 221 | if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl)) |
| 222 | { |
| 223 | ExternalASTSource *external_source = original_ctx->getExternalSource(); |
| 224 | |
| 225 | if (external_source) |
| 226 | external_source->CompleteType (original_tag_decl); |
| 227 | } |
| 228 | |
| 229 | DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl); |
| 230 | |
| 231 | if (!original_decl_context) |
| 232 | return ELR_Failure; |
| 233 | |
| 234 | for (TagDecl::decl_iterator iter = original_decl_context->decls_begin(); |
| 235 | iter != original_decl_context->decls_end(); |
| 236 | ++iter) |
| 237 | { |
| 238 | Decl *decl = *iter; |
| 239 | |
| 240 | if (!predicate || predicate(decl->getKind())) |
| 241 | { |
| 242 | if (log) |
| 243 | { |
| 244 | ASTDumper ast_dumper(decl); |
| 245 | if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl)) |
| 246 | log->Printf(" FELD[%d] Adding [to %s] lexical decl %s", current_id, context_named_decl->getNameAsString().c_str(), ast_dumper.GetCString()); |
| 247 | else |
| 248 | log->Printf(" FELD[%d] Adding lexical decl %s", current_id, ast_dumper.GetCString()); |
| 249 | } |
| 250 | |
| 251 | Decl *copied_decl = m_ast_importer->CopyDecl(original_ctx, decl); |
| 252 | |
| 253 | decls.push_back(copied_decl); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | return ELR_AlreadyLoaded; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Sean Callanan | 73b520f | 2011-10-29 01:58:46 +0000 | [diff] [blame] | 260 | void |
| 261 | ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map, |
| 262 | const ConstString &name, |
| 263 | ClangASTImporter::NamespaceMapSP &parent_map) const |
| 264 | { |
| 265 | static unsigned int invocation_id = 0; |
| 266 | unsigned int current_id = invocation_id++; |
| 267 | |
| 268 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 269 | |
| 270 | if (log) |
| 271 | { |
| 272 | if (parent_map && parent_map->size()) |
| 273 | log->Printf("CompleteNamespaceMap[%u] Searching for namespace %s in namespace %s", |
| 274 | current_id, |
| 275 | name.GetCString(), |
| 276 | parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str()); |
| 277 | else |
| 278 | log->Printf("CompleteNamespaceMap[%u] Searching for namespace %s", |
| 279 | current_id, |
| 280 | name.GetCString()); |
| 281 | } |
| 282 | |
| 283 | |
| 284 | if (parent_map) |
| 285 | { |
| 286 | for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end(); |
| 287 | i != e; |
| 288 | ++i) |
| 289 | { |
| 290 | ClangNamespaceDecl found_namespace_decl; |
| 291 | |
| 292 | lldb::ModuleSP module_sp = i->first; |
| 293 | ClangNamespaceDecl module_parent_namespace_decl = i->second; |
| 294 | |
| 295 | SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor(); |
| 296 | |
| 297 | if (!symbol_vendor) |
| 298 | continue; |
| 299 | |
| 300 | SymbolContext null_sc; |
| 301 | |
| 302 | found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl); |
| 303 | |
| 304 | if (!found_namespace_decl) |
| 305 | continue; |
| 306 | |
| 307 | namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl)); |
| 308 | |
| 309 | if (log) |
| 310 | log->Printf(" CMN[%u] Found namespace %s in module %s", |
| 311 | current_id, |
| 312 | name.GetCString(), |
| 313 | module_sp->GetFileSpec().GetFilename().GetCString()); |
| 314 | } |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | ModuleList &images = m_target->GetImages(); |
| 319 | ClangNamespaceDecl null_namespace_decl; |
| 320 | |
| 321 | for (uint32_t i = 0, e = images.GetSize(); |
| 322 | i != e; |
| 323 | ++i) |
| 324 | { |
| 325 | lldb::ModuleSP image = images.GetModuleAtIndex(i); |
| 326 | |
| 327 | if (!image) |
| 328 | continue; |
| 329 | |
| 330 | ClangNamespaceDecl found_namespace_decl; |
| 331 | |
| 332 | SymbolVendor *symbol_vendor = image->GetSymbolVendor(); |
| 333 | |
| 334 | if (!symbol_vendor) |
| 335 | continue; |
| 336 | |
| 337 | SymbolContext null_sc; |
| 338 | |
| 339 | found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl); |
| 340 | |
| 341 | if (!found_namespace_decl) |
| 342 | continue; |
| 343 | |
| 344 | namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl)); |
| 345 | |
| 346 | if (log) |
| 347 | log->Printf(" CMN[%u] Found namespace %s in module %s", |
| 348 | current_id, |
| 349 | name.GetCString(), |
| 350 | image->GetFileSpec().GetFilename().GetCString()); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
Sean Callanan | bb715f9 | 2011-10-29 02:28:18 +0000 | [diff] [blame^] | 355 | NamespaceDecl * |
| 356 | ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls) |
| 357 | { |
| 358 | if (namespace_decls.empty()) |
| 359 | return NULL; |
| 360 | |
| 361 | lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
| 362 | |
| 363 | const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second; |
| 364 | |
| 365 | Decl *copied_decl = m_ast_importer->CopyDecl(namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl()); |
| 366 | |
| 367 | NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl); |
| 368 | |
| 369 | m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls); |
| 370 | |
| 371 | return dyn_cast<NamespaceDecl>(copied_decl); |
| 372 | } |
| 373 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 374 | clang::NamedDecl * |
| 375 | NameSearchContext::AddVarDecl(void *type) |
| 376 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 377 | IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo(); |
Sean Callanan | 1ddd9fe | 2010-11-30 00:27:43 +0000 | [diff] [blame] | 378 | |
| 379 | assert (type && "Type for variable must be non-NULL!"); |
Sean Callanan | cc07462 | 2010-09-14 21:59:34 +0000 | [diff] [blame] | 380 | |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 381 | clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context, |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 382 | const_cast<DeclContext*>(m_decl_context), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 383 | SourceLocation(), |
Sean Callanan | 279584c | 2011-03-15 00:17:19 +0000 | [diff] [blame] | 384 | SourceLocation(), |
Sean Callanan | cc07462 | 2010-09-14 21:59:34 +0000 | [diff] [blame] | 385 | ii, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 386 | QualType::getFromOpaquePtr(type), |
| 387 | 0, |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 388 | SC_Static, |
| 389 | SC_Static); |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 390 | m_decls.push_back(Decl); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 391 | |
| 392 | return Decl; |
| 393 | } |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 394 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 395 | clang::NamedDecl * |
| 396 | NameSearchContext::AddFunDecl (void *type) |
| 397 | { |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 398 | clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context, |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 399 | const_cast<DeclContext*>(m_decl_context), |
| 400 | SourceLocation(), |
Sean Callanan | 279584c | 2011-03-15 00:17:19 +0000 | [diff] [blame] | 401 | SourceLocation(), |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 402 | m_decl_name.getAsIdentifierInfo(), |
| 403 | QualType::getFromOpaquePtr(type), |
| 404 | NULL, |
| 405 | SC_Static, |
| 406 | SC_Static, |
| 407 | false, |
| 408 | true); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 409 | |
Sean Callanan | b291abe | 2010-08-12 23:45:38 +0000 | [diff] [blame] | 410 | // We have to do more than just synthesize the FunctionDecl. We have to |
| 411 | // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do |
| 412 | // this, we raid the function's FunctionProtoType for types. |
| 413 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 414 | QualType qual_type (QualType::getFromOpaquePtr(type)); |
| 415 | const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>(); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 416 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 417 | if (func_proto_type) |
Sean Callanan | 3c821cc | 2010-06-23 18:58:10 +0000 | [diff] [blame] | 418 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 419 | unsigned NumArgs = func_proto_type->getNumArgs(); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 420 | unsigned ArgIndex; |
| 421 | |
Sean Callanan | c153518 | 2011-10-07 23:18:13 +0000 | [diff] [blame] | 422 | SmallVector<ParmVarDecl *, 5> parm_var_decls; |
| 423 | |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 424 | for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex) |
| 425 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 426 | QualType arg_qual_type (func_proto_type->getArgType(ArgIndex)); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 427 | |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 428 | 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] | 429 | const_cast<DeclContext*>(m_decl_context), |
| 430 | SourceLocation(), |
| 431 | SourceLocation(), |
| 432 | NULL, |
| 433 | arg_qual_type, |
| 434 | NULL, |
| 435 | SC_Static, |
| 436 | SC_Static, |
| 437 | NULL)); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Sean Callanan | c153518 | 2011-10-07 23:18:13 +0000 | [diff] [blame] | 440 | func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls)); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 443 | m_decls.push_back(func_decl); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 444 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 445 | return func_decl; |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 446 | } |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 447 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 448 | clang::NamedDecl * |
| 449 | NameSearchContext::AddGenericFunDecl() |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 450 | { |
Sean Callanan | ad29309 | 2011-01-18 23:32:05 +0000 | [diff] [blame] | 451 | FunctionProtoType::ExtProtoInfo proto_info; |
| 452 | |
| 453 | proto_info.Variadic = true; |
| 454 | |
Sean Callanan | f76afff | 2011-10-28 23:38:38 +0000 | [diff] [blame] | 455 | QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result |
| 456 | NULL, // argument types |
| 457 | 0, // number of arguments |
| 458 | proto_info)); |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 459 | |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 460 | return AddFunDecl(generic_function_type.getAsOpaquePtr()); |
| 461 | } |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 462 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 463 | clang::NamedDecl * |
| 464 | NameSearchContext::AddTypeDecl(void *type) |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 465 | { |
Greg Clayton | a1aaaff | 2011-01-23 00:34:52 +0000 | [diff] [blame] | 466 | if (type) |
| 467 | { |
| 468 | QualType qual_type = QualType::getFromOpaquePtr(type); |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 469 | |
Sean Callanan | d5b3c35 | 2011-01-27 04:42:51 +0000 | [diff] [blame] | 470 | if (const TagType *tag_type = dyn_cast<clang::TagType>(qual_type)) |
Greg Clayton | a1aaaff | 2011-01-23 00:34:52 +0000 | [diff] [blame] | 471 | { |
| 472 | TagDecl *tag_decl = tag_type->getDecl(); |
| 473 | |
| 474 | m_decls.push_back(tag_decl); |
| 475 | |
| 476 | return tag_decl; |
| 477 | } |
Sean Callanan | d5b3c35 | 2011-01-27 04:42:51 +0000 | [diff] [blame] | 478 | 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] | 479 | { |
| 480 | ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface(); |
| 481 | |
| 482 | m_decls.push_back((NamedDecl*)interface_decl); |
| 483 | |
| 484 | return (NamedDecl*)interface_decl; |
| 485 | } |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 486 | } |
Greg Clayton | a1aaaff | 2011-01-23 00:34:52 +0000 | [diff] [blame] | 487 | return NULL; |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 488 | } |
Greg Clayton | e6d72ca | 2011-06-25 00:44:06 +0000 | [diff] [blame] | 489 | |
| 490 | void |
| 491 | NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result) |
| 492 | { |
| 493 | for (clang::NamedDecl * const *decl_iterator = result.first; |
| 494 | decl_iterator != result.second; |
| 495 | ++decl_iterator) |
| 496 | m_decls.push_back (*decl_iterator); |
| 497 | } |
| 498 | |
| 499 | void |
| 500 | NameSearchContext::AddNamedDecl (clang::NamedDecl *decl) |
| 501 | { |
| 502 | m_decls.push_back (decl); |
| 503 | } |