blob: 80fa4c31b68813112d55bf4cb4c7ec4193822eef [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");
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000432 static ConstString Class_name("Class");
Sean Callanan9394b5a2011-10-29 19:50:43 +0000433
434 do
435 {
436 TypeList types;
437 SymbolContext null_sc;
438
439 if (module_sp && namespace_decl)
440 module_sp->FindTypes(null_sc, name, &namespace_decl, true, 1, types);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000441 else if(name != id_name && name != Class_name)
Sean Callanan9394b5a2011-10-29 19:50:43 +0000442 m_target->GetImages().FindTypes (null_sc, name, true, 1, types);
443 else
444 break;
445
446 if (types.GetSize())
447 {
448 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
449
450 if (log)
451 {
452 const char *name_string = type_sp->GetName().GetCString();
453
454 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
455 current_id,
456 name.GetCString(),
457 (name_string ? name_string : "<anonymous>"));
458 }
459
460 void *copied_type = GuardedCopyType(m_ast_context, type_sp->GetClangAST(), type_sp->GetClangFullType());
461
462 context.AddTypeDecl(copied_type);
463 }
464 } while(0);
465}
466
Sean Callanan9b714842011-11-09 19:33:21 +0000467void
468ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
469{
470 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
471
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000472 static unsigned int invocation_id = 0;
473 unsigned int current_id = invocation_id++;
474
Sean Callanan9b714842011-11-09 19:33:21 +0000475 const DeclarationName &decl_name(context.m_decl_name);
476 const DeclContext *decl_ctx(context.m_decl_context);
477
478 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
479
480 if (!interface_decl)
481 return;
482
483 StreamString ss;
484 if (decl_name.isObjCZeroArgSelector())
485 {
486 ss.Printf("%s", decl_name.getAsString().c_str());
487 }
488 else if (decl_name.isObjCOneArgSelector())
489 {
490 ss.Printf("%s:", decl_name.getAsString().c_str());
491 }
492 else
493 {
494 clang::Selector sel = decl_name.getObjCSelector();
495
496 for (unsigned i = 0, e = sel.getNumArgs();
497 i != e;
498 ++i)
499 {
500 llvm::StringRef r = sel.getNameForSlot(i);
501 ss.Printf("%s:", r.str().c_str());
502 }
503 }
504 ss.Flush();
505
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000506 ConstString selector_name(ss.GetData());
507
Sean Callanan9b714842011-11-09 19:33:21 +0000508 if (log)
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000509 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] for selector [%s %s]",
510 current_id,
511 interface_decl->getNameAsString().c_str(),
512 selector_name.AsCString());
513
514 SymbolContextList sc_list;
515
516 const bool include_symbols = false;
517 const bool append = false;
518
519 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, append, sc_list);
520
521 for (uint32_t i = 0, e = sc_list.GetSize();
522 i != e;
523 ++i)
524 {
525 SymbolContext sc;
526
527 if (!sc_list.GetContextAtIndex(i, sc))
528 continue;
529
530 if (!sc.function)
531 continue;
532
533 DeclContext *function_ctx = sc.function->GetClangDeclContext();
534
535 if (!function_ctx)
536 continue;
537
538 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
539
540 if (!method_decl)
541 continue;
542
543 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
544
545 if (!found_interface_decl)
546 continue;
547
548 if (found_interface_decl->getName() == interface_decl->getName())
549 {
550 Decl *copied_decl = m_ast_importer->CopyDecl(&method_decl->getASTContext(), method_decl);
551
552 if (!copied_decl)
553 continue;
554
555 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
556
557 if (!copied_method_decl)
558 continue;
559
560 if (log)
561 {
562 ASTDumper dumper((Decl*)copied_method_decl);
563 log->Printf(" CAS::FOMD[%d] found %s", current_id, dumper.GetCString());
564 }
565
566 context.AddNamedDecl(copied_method_decl);
567 }
568 }
Sean Callanan9b714842011-11-09 19:33:21 +0000569}
570
Sean Callanan73b520f2011-10-29 01:58:46 +0000571void
572ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
573 const ConstString &name,
574 ClangASTImporter::NamespaceMapSP &parent_map) const
575{
576 static unsigned int invocation_id = 0;
577 unsigned int current_id = invocation_id++;
578
579 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
580
581 if (log)
582 {
583 if (parent_map && parent_map->size())
584 log->Printf("CompleteNamespaceMap[%u] Searching for namespace %s in namespace %s",
585 current_id,
586 name.GetCString(),
587 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
588 else
589 log->Printf("CompleteNamespaceMap[%u] Searching for namespace %s",
590 current_id,
591 name.GetCString());
592 }
593
594
595 if (parent_map)
596 {
597 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
598 i != e;
599 ++i)
600 {
601 ClangNamespaceDecl found_namespace_decl;
602
603 lldb::ModuleSP module_sp = i->first;
604 ClangNamespaceDecl module_parent_namespace_decl = i->second;
605
606 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
607
608 if (!symbol_vendor)
609 continue;
610
611 SymbolContext null_sc;
612
613 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
614
615 if (!found_namespace_decl)
616 continue;
617
618 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
619
620 if (log)
621 log->Printf(" CMN[%u] Found namespace %s in module %s",
622 current_id,
623 name.GetCString(),
624 module_sp->GetFileSpec().GetFilename().GetCString());
625 }
626 }
627 else
628 {
629 ModuleList &images = m_target->GetImages();
630 ClangNamespaceDecl null_namespace_decl;
631
632 for (uint32_t i = 0, e = images.GetSize();
633 i != e;
634 ++i)
635 {
636 lldb::ModuleSP image = images.GetModuleAtIndex(i);
637
638 if (!image)
639 continue;
640
641 ClangNamespaceDecl found_namespace_decl;
642
643 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
644
645 if (!symbol_vendor)
646 continue;
647
648 SymbolContext null_sc;
649
650 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
651
652 if (!found_namespace_decl)
653 continue;
654
655 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
656
657 if (log)
658 log->Printf(" CMN[%u] Found namespace %s in module %s",
659 current_id,
660 name.GetCString(),
661 image->GetFileSpec().GetFilename().GetCString());
662 }
663 }
664}
665
Sean Callananbb715f92011-10-29 02:28:18 +0000666NamespaceDecl *
667ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
668{
669 if (namespace_decls.empty())
670 return NULL;
671
672 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
673
674 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
675
676 Decl *copied_decl = m_ast_importer->CopyDecl(namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
677
678 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
679
680 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
681
682 return dyn_cast<NamespaceDecl>(copied_decl);
683}
684
Sean Callanan9394b5a2011-10-29 19:50:43 +0000685void *
686ClangASTSource::GuardedCopyType (ASTContext *dest_context,
687 ASTContext *source_context,
688 void *clang_type)
689{
690 SetImportInProgress(true);
691
692 QualType ret_qual_type = m_ast_importer->CopyType (source_context, QualType::getFromOpaquePtr(clang_type));
693
694 void *ret = ret_qual_type.getAsOpaquePtr();
695
696 SetImportInProgress(false);
697
698 return ret;
699}
700
Greg Claytonb01000f2011-01-17 03:46:26 +0000701clang::NamedDecl *
702NameSearchContext::AddVarDecl(void *type)
703{
Greg Clayton8de27c72010-10-15 22:48:33 +0000704 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +0000705
706 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +0000707
Sean Callananf76afff2011-10-28 23:38:38 +0000708 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000709 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +0000710 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000711 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000712 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000713 QualType::getFromOpaquePtr(type),
714 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000715 SC_Static,
716 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +0000717 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000718
719 return Decl;
720}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000721
Greg Claytonb01000f2011-01-17 03:46:26 +0000722clang::NamedDecl *
723NameSearchContext::AddFunDecl (void *type)
724{
Sean Callananf76afff2011-10-28 23:38:38 +0000725 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000726 const_cast<DeclContext*>(m_decl_context),
727 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000728 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +0000729 m_decl_name.getAsIdentifierInfo(),
730 QualType::getFromOpaquePtr(type),
731 NULL,
732 SC_Static,
733 SC_Static,
734 false,
735 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000736
Sean Callananb291abe2010-08-12 23:45:38 +0000737 // We have to do more than just synthesize the FunctionDecl. We have to
738 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
739 // this, we raid the function's FunctionProtoType for types.
740
Greg Clayton8de27c72010-10-15 22:48:33 +0000741 QualType qual_type (QualType::getFromOpaquePtr(type));
742 const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000743
Greg Clayton8de27c72010-10-15 22:48:33 +0000744 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +0000745 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000746 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000747 unsigned ArgIndex;
748
Sean Callananc1535182011-10-07 23:18:13 +0000749 SmallVector<ParmVarDecl *, 5> parm_var_decls;
750
Sean Callanan8f0dc342010-06-22 23:46:24 +0000751 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
752 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000753 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000754
Sean Callananf76afff2011-10-28 23:38:38 +0000755 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callananc1535182011-10-07 23:18:13 +0000756 const_cast<DeclContext*>(m_decl_context),
757 SourceLocation(),
758 SourceLocation(),
759 NULL,
760 arg_qual_type,
761 NULL,
762 SC_Static,
763 SC_Static,
764 NULL));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000765 }
766
Sean Callananc1535182011-10-07 23:18:13 +0000767 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000768 }
769
Greg Clayton8de27c72010-10-15 22:48:33 +0000770 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000771
Greg Clayton8de27c72010-10-15 22:48:33 +0000772 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000773}
Sean Callanan0fc73582010-07-27 00:55:47 +0000774
Greg Claytonb01000f2011-01-17 03:46:26 +0000775clang::NamedDecl *
776NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +0000777{
Sean Callananad293092011-01-18 23:32:05 +0000778 FunctionProtoType::ExtProtoInfo proto_info;
779
780 proto_info.Variadic = true;
781
Sean Callananf76afff2011-10-28 23:38:38 +0000782 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
783 NULL, // argument types
784 0, // number of arguments
785 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +0000786
Sean Callanan0fc73582010-07-27 00:55:47 +0000787 return AddFunDecl(generic_function_type.getAsOpaquePtr());
788}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000789
Greg Claytonb01000f2011-01-17 03:46:26 +0000790clang::NamedDecl *
791NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000792{
Greg Claytona1aaaff2011-01-23 00:34:52 +0000793 if (type)
794 {
795 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000796
Sean Callanand5b3c352011-01-27 04:42:51 +0000797 if (const TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000798 {
799 TagDecl *tag_decl = tag_type->getDecl();
800
801 m_decls.push_back(tag_decl);
802
803 return tag_decl;
804 }
Sean Callanand5b3c352011-01-27 04:42:51 +0000805 else if (const ObjCObjectType *objc_object_type = dyn_cast<clang::ObjCObjectType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000806 {
807 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
808
809 m_decls.push_back((NamedDecl*)interface_decl);
810
811 return (NamedDecl*)interface_decl;
812 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000813 }
Greg Claytona1aaaff2011-01-23 00:34:52 +0000814 return NULL;
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000815}
Greg Claytone6d72ca2011-06-25 00:44:06 +0000816
817void
818NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
819{
820 for (clang::NamedDecl * const *decl_iterator = result.first;
821 decl_iterator != result.second;
822 ++decl_iterator)
823 m_decls.push_back (*decl_iterator);
824}
825
826void
827NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
828{
829 m_decls.push_back (decl);
830}