blob: a2597ba81cc9c0afcd369893140c7f483fbd1c7b [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 }
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000314 else if (const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(context.m_decl_context))
315 {
316 FindObjCPropertyDecls(context);
317 }
Sean Callanan9394b5a2011-10-29 19:50:43 +0000318 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
319 {
320 // we shouldn't be getting FindExternalVisibleDecls calls for these
321 return;
322 }
323 else
324 {
325 ClangNamespaceDecl namespace_decl;
326
327 if (log)
328 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
329
330 FindExternalVisibleDecls(context,
331 lldb::ModuleSP(),
332 namespace_decl,
333 current_id);
334 }
335
336 if (!context.m_namespace_map->empty())
337 {
338 if (log && log->GetVerbose())
339 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
340 current_id,
341 context.m_namespace_map.get(),
342 (int)context.m_namespace_map->size());
343
344 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
345
346 if (clang_namespace_decl)
347 clang_namespace_decl->setHasExternalVisibleStorage();
348 }
349}
350
351void
352ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
353 lldb::ModuleSP module_sp,
354 ClangNamespaceDecl &namespace_decl,
355 unsigned int current_id)
356{
357 assert (m_ast_context);
358
359 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
360
361 SymbolContextList sc_list;
362
363 const ConstString name(context.m_decl_name.getAsString().c_str());
364
365 const char *name_unique_cstr = name.GetCString();
366
367 if (name_unique_cstr == NULL)
368 return;
369
370 // The ClangASTSource is not responsible for finding $-names.
371 if (name_unique_cstr[0] == '$')
372 return;
373
374 if (module_sp && namespace_decl)
375 {
376 ClangNamespaceDecl found_namespace_decl;
377
378 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
379
380 if (symbol_vendor)
381 {
382 SymbolContext null_sc;
383
384 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
385
386 if (found_namespace_decl)
387 {
388 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
389
390 if (log)
391 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
392 current_id,
393 name.GetCString(),
394 module_sp->GetFileSpec().GetFilename().GetCString());
395 }
396 }
397 }
398 else
399 {
400 ModuleList &images = m_target->GetImages();
401
402 for (uint32_t i = 0, e = images.GetSize();
403 i != e;
404 ++i)
405 {
406 lldb::ModuleSP image = images.GetModuleAtIndex(i);
407
408 if (!image)
409 continue;
410
411 ClangNamespaceDecl found_namespace_decl;
412
413 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
414
415 if (!symbol_vendor)
416 continue;
417
418 SymbolContext null_sc;
419
420 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
421
422 if (found_namespace_decl)
423 {
424 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
425
426 if (log)
427 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
428 current_id,
429 name.GetCString(),
430 image->GetFileSpec().GetFilename().GetCString());
431 }
432 }
433 }
434
435 static ConstString id_name("id");
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000436 static ConstString Class_name("Class");
Sean Callanan9394b5a2011-10-29 19:50:43 +0000437
438 do
439 {
440 TypeList types;
441 SymbolContext null_sc;
442
443 if (module_sp && namespace_decl)
444 module_sp->FindTypes(null_sc, name, &namespace_decl, true, 1, types);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000445 else if(name != id_name && name != Class_name)
Sean Callanan9394b5a2011-10-29 19:50:43 +0000446 m_target->GetImages().FindTypes (null_sc, name, true, 1, types);
447 else
448 break;
449
450 if (types.GetSize())
451 {
452 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
453
454 if (log)
455 {
456 const char *name_string = type_sp->GetName().GetCString();
457
458 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
459 current_id,
460 name.GetCString(),
461 (name_string ? name_string : "<anonymous>"));
462 }
463
464 void *copied_type = GuardedCopyType(m_ast_context, type_sp->GetClangAST(), type_sp->GetClangFullType());
465
466 context.AddTypeDecl(copied_type);
467 }
468 } while(0);
469}
470
Sean Callanan9b714842011-11-09 19:33:21 +0000471void
472ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
473{
474 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
475
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000476 static unsigned int invocation_id = 0;
477 unsigned int current_id = invocation_id++;
478
Sean Callanan9b714842011-11-09 19:33:21 +0000479 const DeclarationName &decl_name(context.m_decl_name);
480 const DeclContext *decl_ctx(context.m_decl_context);
481
482 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
483
484 if (!interface_decl)
485 return;
486
487 StreamString ss;
488 if (decl_name.isObjCZeroArgSelector())
489 {
490 ss.Printf("%s", decl_name.getAsString().c_str());
491 }
492 else if (decl_name.isObjCOneArgSelector())
493 {
Sean Callanan1d9ffe22011-11-14 18:29:46 +0000494 ss.Printf("%s", decl_name.getAsString().c_str());
Sean Callanan9b714842011-11-09 19:33:21 +0000495 }
496 else
497 {
498 clang::Selector sel = decl_name.getObjCSelector();
499
500 for (unsigned i = 0, e = sel.getNumArgs();
501 i != e;
502 ++i)
503 {
504 llvm::StringRef r = sel.getNameForSlot(i);
505 ss.Printf("%s:", r.str().c_str());
506 }
507 }
508 ss.Flush();
509
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000510 ConstString selector_name(ss.GetData());
511
Sean Callanan9b714842011-11-09 19:33:21 +0000512 if (log)
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000513 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] for selector [%s %s]",
514 current_id,
515 interface_decl->getNameAsString().c_str(),
516 selector_name.AsCString());
517
518 SymbolContextList sc_list;
519
520 const bool include_symbols = false;
521 const bool append = false;
522
523 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, append, sc_list);
524
525 for (uint32_t i = 0, e = sc_list.GetSize();
526 i != e;
527 ++i)
528 {
529 SymbolContext sc;
530
531 if (!sc_list.GetContextAtIndex(i, sc))
532 continue;
533
534 if (!sc.function)
535 continue;
536
537 DeclContext *function_ctx = sc.function->GetClangDeclContext();
538
539 if (!function_ctx)
540 continue;
541
542 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
543
544 if (!method_decl)
545 continue;
546
547 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
548
549 if (!found_interface_decl)
550 continue;
551
552 if (found_interface_decl->getName() == interface_decl->getName())
553 {
554 Decl *copied_decl = m_ast_importer->CopyDecl(&method_decl->getASTContext(), method_decl);
555
556 if (!copied_decl)
557 continue;
558
559 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
560
561 if (!copied_method_decl)
562 continue;
563
564 if (log)
565 {
566 ASTDumper dumper((Decl*)copied_method_decl);
567 log->Printf(" CAS::FOMD[%d] found %s", current_id, dumper.GetCString());
568 }
569
570 context.AddNamedDecl(copied_method_decl);
571 }
572 }
Sean Callanan9b714842011-11-09 19:33:21 +0000573}
574
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000575void
576ClangASTSource::FindObjCPropertyDecls (NameSearchContext &context)
577{
578 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
579
580 static unsigned int invocation_id = 0;
581 unsigned int current_id = invocation_id++;
582
583 const ObjCInterfaceDecl *iface_decl = cast<ObjCInterfaceDecl>(context.m_decl_context);
584 Decl *orig_decl;
585 ASTContext *orig_ast_ctx;
586
587 m_ast_importer->ResolveDeclOrigin(iface_decl, &orig_decl, &orig_ast_ctx);
588
589 if (!orig_decl)
590 return;
591
592 ObjCInterfaceDecl *orig_iface_decl = dyn_cast<ObjCInterfaceDecl>(orig_decl);
593
594 if (!orig_iface_decl)
595 return;
596
597 if (!ClangASTContext::GetCompleteDecl(orig_ast_ctx, orig_iface_decl))
598 return;
599
600 std::string property_name_str = context.m_decl_name.getAsString();
601 StringRef property_name(property_name_str.c_str());
602 ObjCPropertyDecl *property_decl = orig_iface_decl->FindPropertyDeclaration(&orig_ast_ctx->Idents.get(property_name));
603
604 if (log)
605 log->Printf("ClangASTSource::FindObjCPropertyDecls[%d] for property '%s.%s'",
606 current_id,
607 iface_decl->getNameAsString().c_str(),
608 property_name_str.c_str());
609
610 if (!property_decl)
611 return;
612
613 Decl *copied_decl = m_ast_importer->CopyDecl(orig_ast_ctx, property_decl);
614
615 if (!copied_decl)
616 return;
617
618 ObjCPropertyDecl *copied_property_decl = dyn_cast<ObjCPropertyDecl>(copied_decl);
619
620 if (!copied_property_decl)
621 return;
622
623 if (log)
624 {
625 ASTDumper dumper((Decl*)copied_property_decl);
626 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
627 }
628
629 context.AddNamedDecl(copied_property_decl);
630}
631
Sean Callanan73b520f2011-10-29 01:58:46 +0000632void
633ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
634 const ConstString &name,
635 ClangASTImporter::NamespaceMapSP &parent_map) const
636{
637 static unsigned int invocation_id = 0;
638 unsigned int current_id = invocation_id++;
639
640 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
641
642 if (log)
643 {
644 if (parent_map && parent_map->size())
645 log->Printf("CompleteNamespaceMap[%u] Searching for namespace %s in namespace %s",
646 current_id,
647 name.GetCString(),
648 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
649 else
650 log->Printf("CompleteNamespaceMap[%u] Searching for namespace %s",
651 current_id,
652 name.GetCString());
653 }
654
655
656 if (parent_map)
657 {
658 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
659 i != e;
660 ++i)
661 {
662 ClangNamespaceDecl found_namespace_decl;
663
664 lldb::ModuleSP module_sp = i->first;
665 ClangNamespaceDecl module_parent_namespace_decl = i->second;
666
667 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
668
669 if (!symbol_vendor)
670 continue;
671
672 SymbolContext null_sc;
673
674 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
675
676 if (!found_namespace_decl)
677 continue;
678
679 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
680
681 if (log)
682 log->Printf(" CMN[%u] Found namespace %s in module %s",
683 current_id,
684 name.GetCString(),
685 module_sp->GetFileSpec().GetFilename().GetCString());
686 }
687 }
688 else
689 {
690 ModuleList &images = m_target->GetImages();
691 ClangNamespaceDecl null_namespace_decl;
692
693 for (uint32_t i = 0, e = images.GetSize();
694 i != e;
695 ++i)
696 {
697 lldb::ModuleSP image = images.GetModuleAtIndex(i);
698
699 if (!image)
700 continue;
701
702 ClangNamespaceDecl found_namespace_decl;
703
704 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
705
706 if (!symbol_vendor)
707 continue;
708
709 SymbolContext null_sc;
710
711 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
712
713 if (!found_namespace_decl)
714 continue;
715
716 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
717
718 if (log)
719 log->Printf(" CMN[%u] Found namespace %s in module %s",
720 current_id,
721 name.GetCString(),
722 image->GetFileSpec().GetFilename().GetCString());
723 }
724 }
725}
726
Sean Callananbb715f92011-10-29 02:28:18 +0000727NamespaceDecl *
728ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
729{
730 if (namespace_decls.empty())
731 return NULL;
732
733 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
734
735 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
736
737 Decl *copied_decl = m_ast_importer->CopyDecl(namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
738
739 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
740
741 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
742
743 return dyn_cast<NamespaceDecl>(copied_decl);
744}
745
Sean Callanan9394b5a2011-10-29 19:50:43 +0000746void *
747ClangASTSource::GuardedCopyType (ASTContext *dest_context,
748 ASTContext *source_context,
749 void *clang_type)
750{
751 SetImportInProgress(true);
752
753 QualType ret_qual_type = m_ast_importer->CopyType (source_context, QualType::getFromOpaquePtr(clang_type));
754
755 void *ret = ret_qual_type.getAsOpaquePtr();
756
757 SetImportInProgress(false);
758
759 return ret;
760}
761
Greg Claytonb01000f2011-01-17 03:46:26 +0000762clang::NamedDecl *
763NameSearchContext::AddVarDecl(void *type)
764{
Greg Clayton8de27c72010-10-15 22:48:33 +0000765 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +0000766
767 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +0000768
Sean Callananf76afff2011-10-28 23:38:38 +0000769 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000770 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +0000771 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000772 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000773 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000774 QualType::getFromOpaquePtr(type),
775 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000776 SC_Static,
777 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +0000778 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000779
780 return Decl;
781}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000782
Greg Claytonb01000f2011-01-17 03:46:26 +0000783clang::NamedDecl *
784NameSearchContext::AddFunDecl (void *type)
785{
Sean Callananf76afff2011-10-28 23:38:38 +0000786 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000787 const_cast<DeclContext*>(m_decl_context),
788 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000789 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +0000790 m_decl_name.getAsIdentifierInfo(),
791 QualType::getFromOpaquePtr(type),
792 NULL,
793 SC_Static,
794 SC_Static,
795 false,
796 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000797
Sean Callananb291abe2010-08-12 23:45:38 +0000798 // We have to do more than just synthesize the FunctionDecl. We have to
799 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
800 // this, we raid the function's FunctionProtoType for types.
801
Greg Clayton8de27c72010-10-15 22:48:33 +0000802 QualType qual_type (QualType::getFromOpaquePtr(type));
803 const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000804
Greg Clayton8de27c72010-10-15 22:48:33 +0000805 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +0000806 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000807 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000808 unsigned ArgIndex;
809
Sean Callananc1535182011-10-07 23:18:13 +0000810 SmallVector<ParmVarDecl *, 5> parm_var_decls;
811
Sean Callanan8f0dc342010-06-22 23:46:24 +0000812 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
813 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000814 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000815
Sean Callananf76afff2011-10-28 23:38:38 +0000816 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callananc1535182011-10-07 23:18:13 +0000817 const_cast<DeclContext*>(m_decl_context),
818 SourceLocation(),
819 SourceLocation(),
820 NULL,
821 arg_qual_type,
822 NULL,
823 SC_Static,
824 SC_Static,
825 NULL));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000826 }
827
Sean Callananc1535182011-10-07 23:18:13 +0000828 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000829 }
830
Greg Clayton8de27c72010-10-15 22:48:33 +0000831 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000832
Greg Clayton8de27c72010-10-15 22:48:33 +0000833 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000834}
Sean Callanan0fc73582010-07-27 00:55:47 +0000835
Greg Claytonb01000f2011-01-17 03:46:26 +0000836clang::NamedDecl *
837NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +0000838{
Sean Callananad293092011-01-18 23:32:05 +0000839 FunctionProtoType::ExtProtoInfo proto_info;
840
841 proto_info.Variadic = true;
842
Sean Callananf76afff2011-10-28 23:38:38 +0000843 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
844 NULL, // argument types
845 0, // number of arguments
846 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +0000847
Sean Callanan0fc73582010-07-27 00:55:47 +0000848 return AddFunDecl(generic_function_type.getAsOpaquePtr());
849}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000850
Greg Claytonb01000f2011-01-17 03:46:26 +0000851clang::NamedDecl *
852NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000853{
Greg Claytona1aaaff2011-01-23 00:34:52 +0000854 if (type)
855 {
856 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000857
Sean Callanand5b3c352011-01-27 04:42:51 +0000858 if (const TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000859 {
860 TagDecl *tag_decl = tag_type->getDecl();
861
862 m_decls.push_back(tag_decl);
863
864 return tag_decl;
865 }
Sean Callanand5b3c352011-01-27 04:42:51 +0000866 else if (const ObjCObjectType *objc_object_type = dyn_cast<clang::ObjCObjectType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000867 {
868 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
869
870 m_decls.push_back((NamedDecl*)interface_decl);
871
872 return (NamedDecl*)interface_decl;
873 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000874 }
Greg Claytona1aaaff2011-01-23 00:34:52 +0000875 return NULL;
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000876}
Greg Claytone6d72ca2011-06-25 00:44:06 +0000877
878void
879NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
880{
881 for (clang::NamedDecl * const *decl_iterator = result.first;
882 decl_iterator != result.second;
883 ++decl_iterator)
884 m_decls.push_back (*decl_iterator);
885}
886
887void
888NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
889{
890 m_decls.push_back (decl);
891}