blob: 8b82a885329be39a5379b0b3a016609b19952968 [file] [log] [blame]
Greg Clayton1e591ce2010-07-16 18:28:27 +00001//===-- 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 Lattner24943d22010-06-08 16:52:24 +000010
Chris Lattner24943d22010-06-08 16:52:24 +000011#include "clang/AST/ASTContext.h"
Greg Claytonf4c7ae02010-10-15 03:36:13 +000012#include "lldb/Core/Log.h"
Greg Clayton6e0101c2011-09-17 06:21:20 +000013#include "lldb/Core/Module.h"
Sean Callanan73b520f2011-10-29 01:58:46 +000014#include "lldb/Core/ModuleList.h"
Sean Callananbb715f92011-10-29 02:28:18 +000015#include "lldb/Expression/ASTDumper.h"
Chris Lattner24943d22010-06-08 16:52:24 +000016#include "lldb/Expression/ClangASTSource.h"
17#include "lldb/Expression/ClangExpression.h"
Sean Callanan73b520f2011-10-29 01:58:46 +000018#include "lldb/Symbol/ClangNamespaceDecl.h"
19#include "lldb/Symbol/SymbolVendor.h"
20#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021
22using namespace clang;
23using namespace lldb_private;
24
Greg Claytonb01000f2011-01-17 03:46:26 +000025ClangASTSource::~ClangASTSource()
26{
27}
Chris Lattner24943d22010-06-08 16:52:24 +000028
Greg Claytonb01000f2011-01-17 03:46:26 +000029void
30ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer)
31{
Sean Callananf76afff2011-10-28 23:38:38 +000032 if (!m_ast_context)
33 return;
34
35 m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage();
36 m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage();
Chris Lattner24943d22010-06-08 16:52:24 +000037}
38
Chris Lattner24943d22010-06-08 16:52:24 +000039// The core lookup interface.
Greg Claytonb01000f2011-01-17 03:46:26 +000040DeclContext::lookup_result
41ClangASTSource::FindExternalVisibleDeclsByName
Greg Claytonf4c7ae02010-10-15 03:36:13 +000042(
43 const DeclContext *decl_ctx,
Greg Clayton8de27c72010-10-15 22:48:33 +000044 DeclarationName clang_decl_name
Greg Claytonf4c7ae02010-10-15 03:36:13 +000045)
46{
Sean Callananf76afff2011-10-28 23:38:38 +000047 if (!m_ast_context)
48 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
49
50 if (GetImportInProgress())
Greg Claytonb01000f2011-01-17 03:46:26 +000051 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 Clayton8de27c72010-10-15 22:48:33 +000058 switch (clang_decl_name.getNameKind()) {
Chris Lattner24943d22010-06-08 16:52:24 +000059 // Normal identifiers.
60 case DeclarationName::Identifier:
Greg Clayton8de27c72010-10-15 22:48:33 +000061 if (clang_decl_name.getAsIdentifierInfo()->getBuiltinID() != 0)
62 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
63 break;
Chris Lattner24943d22010-06-08 16:52:24 +000064
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 Clayton8de27c72010-10-15 22:48:33 +000073 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
Chris Lattner24943d22010-06-08 16:52:24 +000074
Chris Lattner24943d22010-06-08 16:52:24 +000075 case DeclarationName::ObjCZeroArgSelector:
76 case DeclarationName::ObjCOneArgSelector:
77 case DeclarationName::ObjCMultiArgSelector:
Sean Callanan9b714842011-11-09 19:33:21 +000078 {
79 llvm::SmallVector<NamedDecl*, 1> method_decls;
Chris Lattner24943d22010-06-08 16:52:24 +000080
Sean Callanan9b714842011-11-09 19:33:21 +000081 NameSearchContext method_search_context (*this, method_decls, clang_decl_name, decl_ctx);
82
83 FindObjCMethodDecls(method_search_context);
84
85 return SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, method_decls);
86 }
Chris Lattner24943d22010-06-08 16:52:24 +000087 // These aren't possible in the global context.
88 case DeclarationName::CXXConstructorName:
89 case DeclarationName::CXXDestructorName:
90 case DeclarationName::CXXConversionFunctionName:
91 return DeclContext::lookup_result();
92 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +000093
Greg Claytonf4c7ae02010-10-15 03:36:13 +000094
Sean Callananf76afff2011-10-28 23:38:38 +000095 if (!GetLookupsEnabled())
Greg Clayton8de27c72010-10-15 22:48:33 +000096 {
97 // Wait until we see a '$' at the start of a name before we start doing
98 // any lookups so we can avoid lookup up all of the builtin types.
99 if (!decl_name.empty() && decl_name[0] == '$')
100 {
Sean Callananf76afff2011-10-28 23:38:38 +0000101 SetLookupsEnabled (true);
Greg Clayton8de27c72010-10-15 22:48:33 +0000102 }
103 else
104 {
105 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
106 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000107 }
Greg Clayton8de27c72010-10-15 22:48:33 +0000108
Greg Clayton8de27c72010-10-15 22:48:33 +0000109 ConstString const_decl_name(decl_name.c_str());
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000110
111 const char *uniqued_const_decl_name = const_decl_name.GetCString();
112 if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end())
113 {
114 // We are currently looking up this name...
115 return DeclContext::lookup_result();
116 }
117 m_active_lookups.insert(uniqued_const_decl_name);
Greg Claytona8b278a2010-11-15 01:34:18 +0000118// static uint32_t g_depth = 0;
119// ++g_depth;
120// printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000121 llvm::SmallVector<NamedDecl*, 4> name_decls;
122 NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx);
Sean Callananf76afff2011-10-28 23:38:38 +0000123 FindExternalVisibleDecls(name_search_context);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000124 DeclContext::lookup_result result (SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls));
Greg Claytona8b278a2010-11-15 01:34:18 +0000125// --g_depth;
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000126 m_active_lookups.erase (uniqued_const_decl_name);
127 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000128}
129
Greg Claytonb01000f2011-01-17 03:46:26 +0000130void
131ClangASTSource::CompleteType (TagDecl *tag_decl)
Sean Callananbb715f92011-10-29 02:28:18 +0000132{
133 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
134
135 if (log)
136 {
137 log->Printf(" [CompleteTagDecl] Completing a TagDecl named %s", tag_decl->getName().str().c_str());
138 log->Printf(" [CTD] Before:");
139 ASTDumper dumper((Decl*)tag_decl);
140 dumper.ToLog(log, " [CTD] ");
141 }
142
143 m_ast_importer->CompleteTagDecl (tag_decl);
144
145 if (log)
146 {
147 log->Printf(" [CTD] After:");
148 ASTDumper dumper((Decl*)tag_decl);
149 dumper.ToLog(log, " [CTD] ");
150 }
Greg Claytonb01000f2011-01-17 03:46:26 +0000151}
152
153void
Sean Callananbb715f92011-10-29 02:28:18 +0000154ClangASTSource::CompleteType (clang::ObjCInterfaceDecl *interface_decl)
155{
156 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
157
158 if (log)
159 {
160 log->Printf(" [CompleteObjCInterfaceDecl] Completing an ObjCInterfaceDecl named %s", interface_decl->getName().str().c_str());
161 log->Printf(" [COID] Before:");
162 ASTDumper dumper((Decl*)interface_decl);
163 dumper.ToLog(log, " [COID] ");
164 }
165
166 m_ast_importer->CompleteObjCInterfaceDecl (interface_decl);
167
168 if (log)
169 {
170 log->Printf(" [COID] After:");
171 ASTDumper dumper((Decl*)interface_decl);
172 dumper.ToLog(log, " [COID] ");
173 }
Greg Claytonb01000f2011-01-17 03:46:26 +0000174}
175
Sean Callanan9b6898f2011-07-30 02:42:06 +0000176clang::ExternalLoadResult
Sean Callananbb715f92011-10-29 02:28:18 +0000177ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context,
178 bool (*predicate)(Decl::Kind),
179 llvm::SmallVectorImpl<Decl*> &decls)
180{
181 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
182
183 const Decl *context_decl = dyn_cast<Decl>(decl_context);
184
185 if (!context_decl)
186 return ELR_Failure;
187
188 static unsigned int invocation_id = 0;
189 unsigned int current_id = invocation_id++;
190
191 if (log)
192 {
193 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
194 log->Printf("FindExternalLexicalDecls[%u] in '%s' (a %s) with %s predicate",
195 current_id,
196 context_named_decl->getNameAsString().c_str(),
197 context_decl->getDeclKindName(),
198 (predicate ? "non-null" : "null"));
199 else if(context_decl)
200 log->Printf("FindExternalLexicalDecls[%u] in a %s with %s predicate",
201 current_id,
202 context_decl->getDeclKindName(),
203 (predicate ? "non-null" : "null"));
204 else
205 log->Printf("FindExternalLexicalDecls[%u] in a NULL context with %s predicate",
206 current_id,
207 (predicate ? "non-null" : "null"));
208 }
209
210 Decl *original_decl = NULL;
211 ASTContext *original_ctx = NULL;
212
213 if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx))
214 return ELR_Failure;
215
216 if (log)
217 {
218 log->Printf(" FELD[%u] Original decl:", current_id);
219 ASTDumper(original_decl).ToLog(log, " ");
220 }
221
222 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
223 {
224 ExternalASTSource *external_source = original_ctx->getExternalSource();
225
226 if (external_source)
227 external_source->CompleteType (original_tag_decl);
228 }
229
230 DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl);
231
232 if (!original_decl_context)
233 return ELR_Failure;
234
235 for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
236 iter != original_decl_context->decls_end();
237 ++iter)
238 {
239 Decl *decl = *iter;
240
241 if (!predicate || predicate(decl->getKind()))
242 {
243 if (log)
244 {
245 ASTDumper ast_dumper(decl);
246 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
247 log->Printf(" FELD[%d] Adding [to %s] lexical decl %s", current_id, context_named_decl->getNameAsString().c_str(), ast_dumper.GetCString());
248 else
249 log->Printf(" FELD[%d] Adding lexical decl %s", current_id, ast_dumper.GetCString());
250 }
251
252 Decl *copied_decl = m_ast_importer->CopyDecl(original_ctx, decl);
253
254 decls.push_back(copied_decl);
255 }
256 }
257
258 return ELR_AlreadyLoaded;
Chris Lattner24943d22010-06-08 16:52:24 +0000259}
260
Sean Callanan9394b5a2011-10-29 19:50:43 +0000261void
262ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
263{
264 assert (m_ast_context);
265
266 const ConstString name(context.m_decl_name.getAsString().c_str());
267
268 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
269
270 static unsigned int invocation_id = 0;
271 unsigned int current_id = invocation_id++;
272
273 if (log)
274 {
275 if (!context.m_decl_context)
276 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] for '%s' in a NULL DeclContext", current_id, name.GetCString());
277 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
278 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] for '%s' in '%s'", current_id, name.GetCString(), context_named_decl->getNameAsString().c_str());
279 else
280 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] for '%s' in a '%s'", current_id, name.GetCString(), context.m_decl_context->getDeclKindName());
281 }
282
283 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
284
285 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
286 {
287 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
288
289 if (log && log->GetVerbose())
290 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
291 current_id,
292 namespace_map.get(),
293 (int)namespace_map->size());
294
295 if (!namespace_map)
296 return;
297
298 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
299 i != e;
300 ++i)
301 {
302 if (log)
303 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
304 current_id,
305 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
306 i->first->GetFileSpec().GetFilename().GetCString());
307
308 FindExternalVisibleDecls(context,
309 i->first,
310 i->second,
311 current_id);
312 }
313 }
314 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
315 {
316 // we shouldn't be getting FindExternalVisibleDecls calls for these
317 return;
318 }
319 else
320 {
321 ClangNamespaceDecl namespace_decl;
322
323 if (log)
324 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
325
326 FindExternalVisibleDecls(context,
327 lldb::ModuleSP(),
328 namespace_decl,
329 current_id);
330 }
331
332 if (!context.m_namespace_map->empty())
333 {
334 if (log && log->GetVerbose())
335 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
336 current_id,
337 context.m_namespace_map.get(),
338 (int)context.m_namespace_map->size());
339
340 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
341
342 if (clang_namespace_decl)
343 clang_namespace_decl->setHasExternalVisibleStorage();
344 }
345}
346
347void
348ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
349 lldb::ModuleSP module_sp,
350 ClangNamespaceDecl &namespace_decl,
351 unsigned int current_id)
352{
353 assert (m_ast_context);
354
355 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
356
357 SymbolContextList sc_list;
358
359 const ConstString name(context.m_decl_name.getAsString().c_str());
360
361 const char *name_unique_cstr = name.GetCString();
362
363 if (name_unique_cstr == NULL)
364 return;
365
366 // The ClangASTSource is not responsible for finding $-names.
367 if (name_unique_cstr[0] == '$')
368 return;
369
370 if (module_sp && namespace_decl)
371 {
372 ClangNamespaceDecl found_namespace_decl;
373
374 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
375
376 if (symbol_vendor)
377 {
378 SymbolContext null_sc;
379
380 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
381
382 if (found_namespace_decl)
383 {
384 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
385
386 if (log)
387 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
388 current_id,
389 name.GetCString(),
390 module_sp->GetFileSpec().GetFilename().GetCString());
391 }
392 }
393 }
394 else
395 {
396 ModuleList &images = m_target->GetImages();
397
398 for (uint32_t i = 0, e = images.GetSize();
399 i != e;
400 ++i)
401 {
402 lldb::ModuleSP image = images.GetModuleAtIndex(i);
403
404 if (!image)
405 continue;
406
407 ClangNamespaceDecl found_namespace_decl;
408
409 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
410
411 if (!symbol_vendor)
412 continue;
413
414 SymbolContext null_sc;
415
416 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
417
418 if (found_namespace_decl)
419 {
420 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
421
422 if (log)
423 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
424 current_id,
425 name.GetCString(),
426 image->GetFileSpec().GetFilename().GetCString());
427 }
428 }
429 }
430
431 static ConstString id_name("id");
432
433 do
434 {
435 TypeList types;
436 SymbolContext null_sc;
437
438 if (module_sp && namespace_decl)
439 module_sp->FindTypes(null_sc, name, &namespace_decl, true, 1, types);
440 else if(name != id_name)
441 m_target->GetImages().FindTypes (null_sc, name, true, 1, types);
442 else
443 break;
444
445 if (types.GetSize())
446 {
447 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
448
449 if (log)
450 {
451 const char *name_string = type_sp->GetName().GetCString();
452
453 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
454 current_id,
455 name.GetCString(),
456 (name_string ? name_string : "<anonymous>"));
457 }
458
459 void *copied_type = GuardedCopyType(m_ast_context, type_sp->GetClangAST(), type_sp->GetClangFullType());
460
461 context.AddTypeDecl(copied_type);
462 }
463 } while(0);
464}
465
Sean Callanan9b714842011-11-09 19:33:21 +0000466void
467ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
468{
469 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
470
471 const DeclarationName &decl_name(context.m_decl_name);
472 const DeclContext *decl_ctx(context.m_decl_context);
473
474 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
475
476 if (!interface_decl)
477 return;
478
479 StreamString ss;
480 if (decl_name.isObjCZeroArgSelector())
481 {
482 ss.Printf("%s", decl_name.getAsString().c_str());
483 }
484 else if (decl_name.isObjCOneArgSelector())
485 {
486 ss.Printf("%s:", decl_name.getAsString().c_str());
487 }
488 else
489 {
490 clang::Selector sel = decl_name.getObjCSelector();
491
492 for (unsigned i = 0, e = sel.getNumArgs();
493 i != e;
494 ++i)
495 {
496 llvm::StringRef r = sel.getNameForSlot(i);
497 ss.Printf("%s:", r.str().c_str());
498 }
499 }
500 ss.Flush();
501
502 if (log)
503 log->Printf("ClangASTSource::FindObjCMethodDecls for selector [%s %s]", interface_decl->getNameAsString().c_str(), ss.GetData());
504}
505
Sean Callanan73b520f2011-10-29 01:58:46 +0000506void
507ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
508 const ConstString &name,
509 ClangASTImporter::NamespaceMapSP &parent_map) const
510{
511 static unsigned int invocation_id = 0;
512 unsigned int current_id = invocation_id++;
513
514 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
515
516 if (log)
517 {
518 if (parent_map && parent_map->size())
519 log->Printf("CompleteNamespaceMap[%u] Searching for namespace %s in namespace %s",
520 current_id,
521 name.GetCString(),
522 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
523 else
524 log->Printf("CompleteNamespaceMap[%u] Searching for namespace %s",
525 current_id,
526 name.GetCString());
527 }
528
529
530 if (parent_map)
531 {
532 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
533 i != e;
534 ++i)
535 {
536 ClangNamespaceDecl found_namespace_decl;
537
538 lldb::ModuleSP module_sp = i->first;
539 ClangNamespaceDecl module_parent_namespace_decl = i->second;
540
541 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
542
543 if (!symbol_vendor)
544 continue;
545
546 SymbolContext null_sc;
547
548 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
549
550 if (!found_namespace_decl)
551 continue;
552
553 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
554
555 if (log)
556 log->Printf(" CMN[%u] Found namespace %s in module %s",
557 current_id,
558 name.GetCString(),
559 module_sp->GetFileSpec().GetFilename().GetCString());
560 }
561 }
562 else
563 {
564 ModuleList &images = m_target->GetImages();
565 ClangNamespaceDecl null_namespace_decl;
566
567 for (uint32_t i = 0, e = images.GetSize();
568 i != e;
569 ++i)
570 {
571 lldb::ModuleSP image = images.GetModuleAtIndex(i);
572
573 if (!image)
574 continue;
575
576 ClangNamespaceDecl found_namespace_decl;
577
578 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
579
580 if (!symbol_vendor)
581 continue;
582
583 SymbolContext null_sc;
584
585 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
586
587 if (!found_namespace_decl)
588 continue;
589
590 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
591
592 if (log)
593 log->Printf(" CMN[%u] Found namespace %s in module %s",
594 current_id,
595 name.GetCString(),
596 image->GetFileSpec().GetFilename().GetCString());
597 }
598 }
599}
600
Sean Callananbb715f92011-10-29 02:28:18 +0000601NamespaceDecl *
602ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
603{
604 if (namespace_decls.empty())
605 return NULL;
606
607 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
608
609 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
610
611 Decl *copied_decl = m_ast_importer->CopyDecl(namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
612
613 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
614
615 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
616
617 return dyn_cast<NamespaceDecl>(copied_decl);
618}
619
Sean Callanan9394b5a2011-10-29 19:50:43 +0000620void *
621ClangASTSource::GuardedCopyType (ASTContext *dest_context,
622 ASTContext *source_context,
623 void *clang_type)
624{
625 SetImportInProgress(true);
626
627 QualType ret_qual_type = m_ast_importer->CopyType (source_context, QualType::getFromOpaquePtr(clang_type));
628
629 void *ret = ret_qual_type.getAsOpaquePtr();
630
631 SetImportInProgress(false);
632
633 return ret;
634}
635
Greg Claytonb01000f2011-01-17 03:46:26 +0000636clang::NamedDecl *
637NameSearchContext::AddVarDecl(void *type)
638{
Greg Clayton8de27c72010-10-15 22:48:33 +0000639 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +0000640
641 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +0000642
Sean Callananf76afff2011-10-28 23:38:38 +0000643 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000644 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +0000645 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000646 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000647 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000648 QualType::getFromOpaquePtr(type),
649 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000650 SC_Static,
651 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +0000652 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000653
654 return Decl;
655}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000656
Greg Claytonb01000f2011-01-17 03:46:26 +0000657clang::NamedDecl *
658NameSearchContext::AddFunDecl (void *type)
659{
Sean Callananf76afff2011-10-28 23:38:38 +0000660 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000661 const_cast<DeclContext*>(m_decl_context),
662 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000663 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +0000664 m_decl_name.getAsIdentifierInfo(),
665 QualType::getFromOpaquePtr(type),
666 NULL,
667 SC_Static,
668 SC_Static,
669 false,
670 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000671
Sean Callananb291abe2010-08-12 23:45:38 +0000672 // We have to do more than just synthesize the FunctionDecl. We have to
673 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
674 // this, we raid the function's FunctionProtoType for types.
675
Greg Clayton8de27c72010-10-15 22:48:33 +0000676 QualType qual_type (QualType::getFromOpaquePtr(type));
677 const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000678
Greg Clayton8de27c72010-10-15 22:48:33 +0000679 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +0000680 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000681 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000682 unsigned ArgIndex;
683
Sean Callananc1535182011-10-07 23:18:13 +0000684 SmallVector<ParmVarDecl *, 5> parm_var_decls;
685
Sean Callanan8f0dc342010-06-22 23:46:24 +0000686 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
687 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000688 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000689
Sean Callananf76afff2011-10-28 23:38:38 +0000690 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callananc1535182011-10-07 23:18:13 +0000691 const_cast<DeclContext*>(m_decl_context),
692 SourceLocation(),
693 SourceLocation(),
694 NULL,
695 arg_qual_type,
696 NULL,
697 SC_Static,
698 SC_Static,
699 NULL));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000700 }
701
Sean Callananc1535182011-10-07 23:18:13 +0000702 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000703 }
704
Greg Clayton8de27c72010-10-15 22:48:33 +0000705 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000706
Greg Clayton8de27c72010-10-15 22:48:33 +0000707 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000708}
Sean Callanan0fc73582010-07-27 00:55:47 +0000709
Greg Claytonb01000f2011-01-17 03:46:26 +0000710clang::NamedDecl *
711NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +0000712{
Sean Callananad293092011-01-18 23:32:05 +0000713 FunctionProtoType::ExtProtoInfo proto_info;
714
715 proto_info.Variadic = true;
716
Sean Callananf76afff2011-10-28 23:38:38 +0000717 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
718 NULL, // argument types
719 0, // number of arguments
720 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +0000721
Sean Callanan0fc73582010-07-27 00:55:47 +0000722 return AddFunDecl(generic_function_type.getAsOpaquePtr());
723}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000724
Greg Claytonb01000f2011-01-17 03:46:26 +0000725clang::NamedDecl *
726NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000727{
Greg Claytona1aaaff2011-01-23 00:34:52 +0000728 if (type)
729 {
730 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000731
Sean Callanand5b3c352011-01-27 04:42:51 +0000732 if (const TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000733 {
734 TagDecl *tag_decl = tag_type->getDecl();
735
736 m_decls.push_back(tag_decl);
737
738 return tag_decl;
739 }
Sean Callanand5b3c352011-01-27 04:42:51 +0000740 else if (const ObjCObjectType *objc_object_type = dyn_cast<clang::ObjCObjectType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000741 {
742 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
743
744 m_decls.push_back((NamedDecl*)interface_decl);
745
746 return (NamedDecl*)interface_decl;
747 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000748 }
Greg Claytona1aaaff2011-01-23 00:34:52 +0000749 return NULL;
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000750}
Greg Claytone6d72ca2011-06-25 00:44:06 +0000751
752void
753NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
754{
755 for (clang::NamedDecl * const *decl_iterator = result.first;
756 decl_iterator != result.second;
757 ++decl_iterator)
758 m_decls.push_back (*decl_iterator);
759}
760
761void
762NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
763{
764 m_decls.push_back (decl);
765}