blob: 5f3c8b15b305605f08746c5903c65bd38052a341 [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{
Sean Callanana3d04472011-11-29 00:42:02 +000027 m_ast_importer->ForgetDestination(m_ast_context);
28
29 ClangASTContext *scratch_clang_ast_context = m_target->GetScratchClangASTContext();
30
31 if (!scratch_clang_ast_context)
32 return;
33
34 clang::ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
35
36 if (!scratch_ast_context)
37 return;
38
39 if (m_ast_context != scratch_ast_context)
40 m_ast_importer->ForgetSource(scratch_ast_context, m_ast_context);
Greg Claytonb01000f2011-01-17 03:46:26 +000041}
Chris Lattner24943d22010-06-08 16:52:24 +000042
Greg Claytonb01000f2011-01-17 03:46:26 +000043void
44ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer)
45{
Sean Callananf76afff2011-10-28 23:38:38 +000046 if (!m_ast_context)
47 return;
48
49 m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage();
50 m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage();
Chris Lattner24943d22010-06-08 16:52:24 +000051}
52
Chris Lattner24943d22010-06-08 16:52:24 +000053// The core lookup interface.
Greg Claytonb01000f2011-01-17 03:46:26 +000054DeclContext::lookup_result
55ClangASTSource::FindExternalVisibleDeclsByName
Greg Claytonf4c7ae02010-10-15 03:36:13 +000056(
57 const DeclContext *decl_ctx,
Greg Clayton8de27c72010-10-15 22:48:33 +000058 DeclarationName clang_decl_name
Greg Claytonf4c7ae02010-10-15 03:36:13 +000059)
60{
Sean Callananf76afff2011-10-28 23:38:38 +000061 if (!m_ast_context)
62 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
63
64 if (GetImportInProgress())
Greg Claytonb01000f2011-01-17 03:46:26 +000065 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
66
67 std::string decl_name (clang_decl_name.getAsString());
68
69// if (m_decl_map.DoingASTImport ())
70// return DeclContext::lookup_result();
71//
Greg Clayton8de27c72010-10-15 22:48:33 +000072 switch (clang_decl_name.getNameKind()) {
Chris Lattner24943d22010-06-08 16:52:24 +000073 // Normal identifiers.
74 case DeclarationName::Identifier:
Greg Clayton8de27c72010-10-15 22:48:33 +000075 if (clang_decl_name.getAsIdentifierInfo()->getBuiltinID() != 0)
76 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
77 break;
Chris Lattner24943d22010-06-08 16:52:24 +000078
79 // Operator names. Not important for now.
80 case DeclarationName::CXXOperatorName:
81 case DeclarationName::CXXLiteralOperatorName:
82 return DeclContext::lookup_result();
83
84 // Using directives found in this context.
85 // Tell Sema we didn't find any or we'll end up getting asked a *lot*.
86 case DeclarationName::CXXUsingDirective:
Greg Clayton8de27c72010-10-15 22:48:33 +000087 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
Chris Lattner24943d22010-06-08 16:52:24 +000088
Chris Lattner24943d22010-06-08 16:52:24 +000089 case DeclarationName::ObjCZeroArgSelector:
90 case DeclarationName::ObjCOneArgSelector:
91 case DeclarationName::ObjCMultiArgSelector:
Sean Callanan9b714842011-11-09 19:33:21 +000092 {
93 llvm::SmallVector<NamedDecl*, 1> method_decls;
Chris Lattner24943d22010-06-08 16:52:24 +000094
Sean Callanan9b714842011-11-09 19:33:21 +000095 NameSearchContext method_search_context (*this, method_decls, clang_decl_name, decl_ctx);
96
97 FindObjCMethodDecls(method_search_context);
98
99 return SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, method_decls);
100 }
Chris Lattner24943d22010-06-08 16:52:24 +0000101 // These aren't possible in the global context.
102 case DeclarationName::CXXConstructorName:
103 case DeclarationName::CXXDestructorName:
104 case DeclarationName::CXXConversionFunctionName:
105 return DeclContext::lookup_result();
106 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000107
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000108
Sean Callananf76afff2011-10-28 23:38:38 +0000109 if (!GetLookupsEnabled())
Greg Clayton8de27c72010-10-15 22:48:33 +0000110 {
111 // Wait until we see a '$' at the start of a name before we start doing
112 // any lookups so we can avoid lookup up all of the builtin types.
113 if (!decl_name.empty() && decl_name[0] == '$')
114 {
Sean Callananf76afff2011-10-28 23:38:38 +0000115 SetLookupsEnabled (true);
Greg Clayton8de27c72010-10-15 22:48:33 +0000116 }
117 else
118 {
119 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
120 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000121 }
Greg Clayton8de27c72010-10-15 22:48:33 +0000122
Greg Clayton8de27c72010-10-15 22:48:33 +0000123 ConstString const_decl_name(decl_name.c_str());
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000124
125 const char *uniqued_const_decl_name = const_decl_name.GetCString();
126 if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end())
127 {
128 // We are currently looking up this name...
129 return DeclContext::lookup_result();
130 }
131 m_active_lookups.insert(uniqued_const_decl_name);
Greg Claytona8b278a2010-11-15 01:34:18 +0000132// static uint32_t g_depth = 0;
133// ++g_depth;
134// printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000135 llvm::SmallVector<NamedDecl*, 4> name_decls;
136 NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx);
Sean Callananf76afff2011-10-28 23:38:38 +0000137 FindExternalVisibleDecls(name_search_context);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000138 DeclContext::lookup_result result (SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls));
Greg Claytona8b278a2010-11-15 01:34:18 +0000139// --g_depth;
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000140 m_active_lookups.erase (uniqued_const_decl_name);
141 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000142}
143
Greg Claytonb01000f2011-01-17 03:46:26 +0000144void
145ClangASTSource::CompleteType (TagDecl *tag_decl)
Sean Callananbb715f92011-10-29 02:28:18 +0000146{
147 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
148
149 if (log)
150 {
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000151 log->Printf(" [CompleteTagDecl] on (ASTContext*)%p Completing a TagDecl named %s", m_ast_context, tag_decl->getName().str().c_str());
Sean Callananbb715f92011-10-29 02:28:18 +0000152 log->Printf(" [CTD] Before:");
153 ASTDumper dumper((Decl*)tag_decl);
154 dumper.ToLog(log, " [CTD] ");
155 }
156
157 m_ast_importer->CompleteTagDecl (tag_decl);
158
159 if (log)
160 {
161 log->Printf(" [CTD] After:");
162 ASTDumper dumper((Decl*)tag_decl);
163 dumper.ToLog(log, " [CTD] ");
164 }
Greg Claytonb01000f2011-01-17 03:46:26 +0000165}
166
167void
Sean Callananbb715f92011-10-29 02:28:18 +0000168ClangASTSource::CompleteType (clang::ObjCInterfaceDecl *interface_decl)
169{
170 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
171
172 if (log)
173 {
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000174 log->Printf(" [CompleteObjCInterfaceDecl] on (ASTContext*)%p Completing an ObjCInterfaceDecl named %s", m_ast_context, interface_decl->getName().str().c_str());
Sean Callananbb715f92011-10-29 02:28:18 +0000175 log->Printf(" [COID] Before:");
176 ASTDumper dumper((Decl*)interface_decl);
177 dumper.ToLog(log, " [COID] ");
178 }
179
180 m_ast_importer->CompleteObjCInterfaceDecl (interface_decl);
181
182 if (log)
183 {
184 log->Printf(" [COID] After:");
185 ASTDumper dumper((Decl*)interface_decl);
186 dumper.ToLog(log, " [COID] ");
187 }
Greg Claytonb01000f2011-01-17 03:46:26 +0000188}
189
Sean Callanan9b6898f2011-07-30 02:42:06 +0000190clang::ExternalLoadResult
Sean Callananbb715f92011-10-29 02:28:18 +0000191ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context,
192 bool (*predicate)(Decl::Kind),
193 llvm::SmallVectorImpl<Decl*> &decls)
194{
195 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
196
197 const Decl *context_decl = dyn_cast<Decl>(decl_context);
198
199 if (!context_decl)
200 return ELR_Failure;
201
202 static unsigned int invocation_id = 0;
203 unsigned int current_id = invocation_id++;
204
205 if (log)
206 {
207 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000208 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000209 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000210 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000211 context_named_decl->getNameAsString().c_str(),
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000212 context_decl->getDeclKindName(),
213 context_decl,
Sean Callananbb715f92011-10-29 02:28:18 +0000214 (predicate ? "non-null" : "null"));
215 else if(context_decl)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000216 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%sDecl*)%p with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000217 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000218 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000219 context_decl->getDeclKindName(),
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000220 context_decl,
Sean Callananbb715f92011-10-29 02:28:18 +0000221 (predicate ? "non-null" : "null"));
222 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000223 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000224 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000225 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000226 (predicate ? "non-null" : "null"));
227 }
228
229 Decl *original_decl = NULL;
230 ASTContext *original_ctx = NULL;
231
232 if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx))
233 return ELR_Failure;
234
235 if (log)
236 {
237 log->Printf(" FELD[%u] Original decl:", current_id);
238 ASTDumper(original_decl).ToLog(log, " ");
239 }
240
241 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
242 {
243 ExternalASTSource *external_source = original_ctx->getExternalSource();
244
245 if (external_source)
246 external_source->CompleteType (original_tag_decl);
247 }
248
249 DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl);
250
251 if (!original_decl_context)
252 return ELR_Failure;
253
254 for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
255 iter != original_decl_context->decls_end();
256 ++iter)
257 {
258 Decl *decl = *iter;
259
260 if (!predicate || predicate(decl->getKind()))
261 {
262 if (log)
263 {
264 ASTDumper ast_dumper(decl);
265 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
266 log->Printf(" FELD[%d] Adding [to %s] lexical decl %s", current_id, context_named_decl->getNameAsString().c_str(), ast_dumper.GetCString());
267 else
268 log->Printf(" FELD[%d] Adding lexical decl %s", current_id, ast_dumper.GetCString());
269 }
270
Sean Callanan4938bd62011-11-16 18:20:47 +0000271 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, original_ctx, decl);
Sean Callananbb715f92011-10-29 02:28:18 +0000272
273 decls.push_back(copied_decl);
274 }
275 }
276
277 return ELR_AlreadyLoaded;
Chris Lattner24943d22010-06-08 16:52:24 +0000278}
279
Sean Callanan9394b5a2011-10-29 19:50:43 +0000280void
281ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
282{
283 assert (m_ast_context);
284
285 const ConstString name(context.m_decl_name.getAsString().c_str());
286
287 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
288
289 static unsigned int invocation_id = 0;
290 unsigned int current_id = invocation_id++;
291
292 if (log)
293 {
294 if (!context.m_decl_context)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000295 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in a NULL DeclContext", current_id, m_ast_context, name.GetCString());
Sean Callanan9394b5a2011-10-29 19:50:43 +0000296 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000297 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in '%s'", current_id, m_ast_context, name.GetCString(), context_named_decl->getNameAsString().c_str());
Sean Callanan9394b5a2011-10-29 19:50:43 +0000298 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000299 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in a '%s'", current_id, m_ast_context, name.GetCString(), context.m_decl_context->getDeclKindName());
Sean Callanan9394b5a2011-10-29 19:50:43 +0000300 }
301
302 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
303
304 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
305 {
306 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
307
308 if (log && log->GetVerbose())
309 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
310 current_id,
311 namespace_map.get(),
312 (int)namespace_map->size());
313
314 if (!namespace_map)
315 return;
316
317 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
318 i != e;
319 ++i)
320 {
321 if (log)
322 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
323 current_id,
324 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
325 i->first->GetFileSpec().GetFilename().GetCString());
326
327 FindExternalVisibleDecls(context,
328 i->first,
329 i->second,
330 current_id);
331 }
332 }
Sean Callanand3812fa2011-11-15 21:50:18 +0000333 else if (isa<ObjCInterfaceDecl>(context.m_decl_context))
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000334 {
335 FindObjCPropertyDecls(context);
336 }
Sean Callanan9394b5a2011-10-29 19:50:43 +0000337 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
338 {
339 // we shouldn't be getting FindExternalVisibleDecls calls for these
340 return;
341 }
342 else
343 {
344 ClangNamespaceDecl namespace_decl;
345
346 if (log)
347 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
348
349 FindExternalVisibleDecls(context,
350 lldb::ModuleSP(),
351 namespace_decl,
352 current_id);
353 }
354
355 if (!context.m_namespace_map->empty())
356 {
357 if (log && log->GetVerbose())
358 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
359 current_id,
360 context.m_namespace_map.get(),
361 (int)context.m_namespace_map->size());
362
363 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
364
365 if (clang_namespace_decl)
366 clang_namespace_decl->setHasExternalVisibleStorage();
367 }
368}
369
370void
371ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
372 lldb::ModuleSP module_sp,
373 ClangNamespaceDecl &namespace_decl,
374 unsigned int current_id)
375{
376 assert (m_ast_context);
377
378 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
379
380 SymbolContextList sc_list;
381
382 const ConstString name(context.m_decl_name.getAsString().c_str());
383
384 const char *name_unique_cstr = name.GetCString();
385
386 if (name_unique_cstr == NULL)
387 return;
388
389 // The ClangASTSource is not responsible for finding $-names.
390 if (name_unique_cstr[0] == '$')
391 return;
392
393 if (module_sp && namespace_decl)
394 {
395 ClangNamespaceDecl found_namespace_decl;
396
397 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
398
399 if (symbol_vendor)
400 {
401 SymbolContext null_sc;
402
403 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
404
405 if (found_namespace_decl)
406 {
407 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
408
409 if (log)
410 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
411 current_id,
412 name.GetCString(),
413 module_sp->GetFileSpec().GetFilename().GetCString());
414 }
415 }
416 }
417 else
418 {
419 ModuleList &images = m_target->GetImages();
420
421 for (uint32_t i = 0, e = images.GetSize();
422 i != e;
423 ++i)
424 {
425 lldb::ModuleSP image = images.GetModuleAtIndex(i);
426
427 if (!image)
428 continue;
429
430 ClangNamespaceDecl found_namespace_decl;
431
432 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
433
434 if (!symbol_vendor)
435 continue;
436
437 SymbolContext null_sc;
438
439 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
440
441 if (found_namespace_decl)
442 {
443 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
444
445 if (log)
446 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
447 current_id,
448 name.GetCString(),
449 image->GetFileSpec().GetFilename().GetCString());
450 }
451 }
452 }
453
454 static ConstString id_name("id");
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000455 static ConstString Class_name("Class");
Sean Callanan9394b5a2011-10-29 19:50:43 +0000456
457 do
458 {
459 TypeList types;
460 SymbolContext null_sc;
461
462 if (module_sp && namespace_decl)
463 module_sp->FindTypes(null_sc, name, &namespace_decl, true, 1, types);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000464 else if(name != id_name && name != Class_name)
Sean Callanan9394b5a2011-10-29 19:50:43 +0000465 m_target->GetImages().FindTypes (null_sc, name, true, 1, types);
466 else
467 break;
468
469 if (types.GetSize())
470 {
471 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
472
473 if (log)
474 {
475 const char *name_string = type_sp->GetName().GetCString();
476
477 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
478 current_id,
479 name.GetCString(),
480 (name_string ? name_string : "<anonymous>"));
481 }
482
483 void *copied_type = GuardedCopyType(m_ast_context, type_sp->GetClangAST(), type_sp->GetClangFullType());
484
485 context.AddTypeDecl(copied_type);
486 }
487 } while(0);
488}
489
Sean Callanan9b714842011-11-09 19:33:21 +0000490void
491ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
492{
493 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
494
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000495 static unsigned int invocation_id = 0;
496 unsigned int current_id = invocation_id++;
497
Sean Callanan9b714842011-11-09 19:33:21 +0000498 const DeclarationName &decl_name(context.m_decl_name);
499 const DeclContext *decl_ctx(context.m_decl_context);
500
501 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
502
503 if (!interface_decl)
504 return;
505
506 StreamString ss;
507 if (decl_name.isObjCZeroArgSelector())
508 {
509 ss.Printf("%s", decl_name.getAsString().c_str());
510 }
511 else if (decl_name.isObjCOneArgSelector())
512 {
Sean Callanan1d9ffe22011-11-14 18:29:46 +0000513 ss.Printf("%s", decl_name.getAsString().c_str());
Sean Callanan9b714842011-11-09 19:33:21 +0000514 }
515 else
516 {
517 clang::Selector sel = decl_name.getObjCSelector();
518
519 for (unsigned i = 0, e = sel.getNumArgs();
520 i != e;
521 ++i)
522 {
523 llvm::StringRef r = sel.getNameForSlot(i);
524 ss.Printf("%s:", r.str().c_str());
525 }
526 }
527 ss.Flush();
528
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000529 ConstString selector_name(ss.GetData());
530
Sean Callanan9b714842011-11-09 19:33:21 +0000531 if (log)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000532 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
533 current_id,
534 m_ast_context,
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000535 interface_decl->getNameAsString().c_str(),
536 selector_name.AsCString());
537
538 SymbolContextList sc_list;
539
540 const bool include_symbols = false;
541 const bool append = false;
542
543 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, append, sc_list);
544
545 for (uint32_t i = 0, e = sc_list.GetSize();
546 i != e;
547 ++i)
548 {
549 SymbolContext sc;
550
551 if (!sc_list.GetContextAtIndex(i, sc))
552 continue;
553
554 if (!sc.function)
555 continue;
556
557 DeclContext *function_ctx = sc.function->GetClangDeclContext();
558
559 if (!function_ctx)
560 continue;
561
562 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
563
564 if (!method_decl)
565 continue;
566
567 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
568
569 if (!found_interface_decl)
570 continue;
571
572 if (found_interface_decl->getName() == interface_decl->getName())
573 {
Sean Callanan4938bd62011-11-16 18:20:47 +0000574 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000575
576 if (!copied_decl)
577 continue;
578
579 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
580
581 if (!copied_method_decl)
582 continue;
583
584 if (log)
585 {
586 ASTDumper dumper((Decl*)copied_method_decl);
587 log->Printf(" CAS::FOMD[%d] found %s", current_id, dumper.GetCString());
588 }
589
590 context.AddNamedDecl(copied_method_decl);
591 }
592 }
Sean Callanan9b714842011-11-09 19:33:21 +0000593}
594
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000595void
596ClangASTSource::FindObjCPropertyDecls (NameSearchContext &context)
597{
598 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
599
600 static unsigned int invocation_id = 0;
601 unsigned int current_id = invocation_id++;
602
603 const ObjCInterfaceDecl *iface_decl = cast<ObjCInterfaceDecl>(context.m_decl_context);
604 Decl *orig_decl;
605 ASTContext *orig_ast_ctx;
606
607 m_ast_importer->ResolveDeclOrigin(iface_decl, &orig_decl, &orig_ast_ctx);
608
609 if (!orig_decl)
610 return;
611
612 ObjCInterfaceDecl *orig_iface_decl = dyn_cast<ObjCInterfaceDecl>(orig_decl);
613
614 if (!orig_iface_decl)
615 return;
616
617 if (!ClangASTContext::GetCompleteDecl(orig_ast_ctx, orig_iface_decl))
618 return;
619
620 std::string property_name_str = context.m_decl_name.getAsString();
621 StringRef property_name(property_name_str.c_str());
622 ObjCPropertyDecl *property_decl = orig_iface_decl->FindPropertyDeclaration(&orig_ast_ctx->Idents.get(property_name));
623
624 if (log)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000625 log->Printf("ClangASTSource::FindObjCPropertyDecls[%d] on (ASTContext*)%p for property '%s.%s'",
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000626 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000627 m_ast_context,
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000628 iface_decl->getNameAsString().c_str(),
629 property_name_str.c_str());
630
631 if (!property_decl)
632 return;
633
Sean Callanan4938bd62011-11-16 18:20:47 +0000634 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, orig_ast_ctx, property_decl);
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000635
636 if (!copied_decl)
637 return;
638
639 ObjCPropertyDecl *copied_property_decl = dyn_cast<ObjCPropertyDecl>(copied_decl);
640
641 if (!copied_property_decl)
642 return;
643
644 if (log)
645 {
646 ASTDumper dumper((Decl*)copied_property_decl);
647 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
648 }
649
650 context.AddNamedDecl(copied_property_decl);
651}
652
Sean Callanan73b520f2011-10-29 01:58:46 +0000653void
654ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
655 const ConstString &name,
656 ClangASTImporter::NamespaceMapSP &parent_map) const
657{
658 static unsigned int invocation_id = 0;
659 unsigned int current_id = invocation_id++;
660
661 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
662
663 if (log)
664 {
665 if (parent_map && parent_map->size())
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000666 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +0000667 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000668 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +0000669 name.GetCString(),
670 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
671 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000672 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +0000673 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000674 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +0000675 name.GetCString());
676 }
677
678
679 if (parent_map)
680 {
681 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
682 i != e;
683 ++i)
684 {
685 ClangNamespaceDecl found_namespace_decl;
686
687 lldb::ModuleSP module_sp = i->first;
688 ClangNamespaceDecl module_parent_namespace_decl = i->second;
689
690 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
691
692 if (!symbol_vendor)
693 continue;
694
695 SymbolContext null_sc;
696
697 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
698
699 if (!found_namespace_decl)
700 continue;
701
702 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
703
704 if (log)
705 log->Printf(" CMN[%u] Found namespace %s in module %s",
706 current_id,
707 name.GetCString(),
708 module_sp->GetFileSpec().GetFilename().GetCString());
709 }
710 }
711 else
712 {
713 ModuleList &images = m_target->GetImages();
714 ClangNamespaceDecl null_namespace_decl;
715
716 for (uint32_t i = 0, e = images.GetSize();
717 i != e;
718 ++i)
719 {
720 lldb::ModuleSP image = images.GetModuleAtIndex(i);
721
722 if (!image)
723 continue;
724
725 ClangNamespaceDecl found_namespace_decl;
726
727 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
728
729 if (!symbol_vendor)
730 continue;
731
732 SymbolContext null_sc;
733
734 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
735
736 if (!found_namespace_decl)
737 continue;
738
739 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
740
741 if (log)
742 log->Printf(" CMN[%u] Found namespace %s in module %s",
743 current_id,
744 name.GetCString(),
745 image->GetFileSpec().GetFilename().GetCString());
746 }
747 }
748}
749
Sean Callananbb715f92011-10-29 02:28:18 +0000750NamespaceDecl *
751ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
752{
753 if (namespace_decls.empty())
754 return NULL;
755
756 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
757
758 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
759
Sean Callanan4938bd62011-11-16 18:20:47 +0000760 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
Sean Callananbb715f92011-10-29 02:28:18 +0000761
762 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
763
764 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
765
766 return dyn_cast<NamespaceDecl>(copied_decl);
767}
768
Sean Callanan9394b5a2011-10-29 19:50:43 +0000769void *
770ClangASTSource::GuardedCopyType (ASTContext *dest_context,
771 ASTContext *source_context,
772 void *clang_type)
773{
774 SetImportInProgress(true);
775
Sean Callanan4938bd62011-11-16 18:20:47 +0000776 QualType ret_qual_type = m_ast_importer->CopyType (m_ast_context, source_context, QualType::getFromOpaquePtr(clang_type));
Sean Callanan9394b5a2011-10-29 19:50:43 +0000777
778 void *ret = ret_qual_type.getAsOpaquePtr();
779
780 SetImportInProgress(false);
781
782 return ret;
783}
784
Greg Claytonb01000f2011-01-17 03:46:26 +0000785clang::NamedDecl *
786NameSearchContext::AddVarDecl(void *type)
787{
Greg Clayton8de27c72010-10-15 22:48:33 +0000788 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +0000789
790 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +0000791
Sean Callananf76afff2011-10-28 23:38:38 +0000792 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000793 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +0000794 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000795 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000796 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000797 QualType::getFromOpaquePtr(type),
798 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000799 SC_Static,
800 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +0000801 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000802
803 return Decl;
804}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000805
Greg Claytonb01000f2011-01-17 03:46:26 +0000806clang::NamedDecl *
807NameSearchContext::AddFunDecl (void *type)
808{
Sean Callananf76afff2011-10-28 23:38:38 +0000809 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000810 const_cast<DeclContext*>(m_decl_context),
811 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000812 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +0000813 m_decl_name.getAsIdentifierInfo(),
814 QualType::getFromOpaquePtr(type),
815 NULL,
816 SC_Static,
817 SC_Static,
818 false,
819 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000820
Sean Callananb291abe2010-08-12 23:45:38 +0000821 // We have to do more than just synthesize the FunctionDecl. We have to
822 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
823 // this, we raid the function's FunctionProtoType for types.
824
Greg Clayton8de27c72010-10-15 22:48:33 +0000825 QualType qual_type (QualType::getFromOpaquePtr(type));
826 const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000827
Greg Clayton8de27c72010-10-15 22:48:33 +0000828 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +0000829 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000830 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000831 unsigned ArgIndex;
832
Sean Callananc1535182011-10-07 23:18:13 +0000833 SmallVector<ParmVarDecl *, 5> parm_var_decls;
834
Sean Callanan8f0dc342010-06-22 23:46:24 +0000835 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
836 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000837 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000838
Sean Callananf76afff2011-10-28 23:38:38 +0000839 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callananc1535182011-10-07 23:18:13 +0000840 const_cast<DeclContext*>(m_decl_context),
841 SourceLocation(),
842 SourceLocation(),
843 NULL,
844 arg_qual_type,
845 NULL,
846 SC_Static,
847 SC_Static,
848 NULL));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000849 }
850
Sean Callananc1535182011-10-07 23:18:13 +0000851 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000852 }
853
Greg Clayton8de27c72010-10-15 22:48:33 +0000854 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000855
Greg Clayton8de27c72010-10-15 22:48:33 +0000856 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000857}
Sean Callanan0fc73582010-07-27 00:55:47 +0000858
Greg Claytonb01000f2011-01-17 03:46:26 +0000859clang::NamedDecl *
860NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +0000861{
Sean Callananad293092011-01-18 23:32:05 +0000862 FunctionProtoType::ExtProtoInfo proto_info;
863
864 proto_info.Variadic = true;
865
Sean Callananf76afff2011-10-28 23:38:38 +0000866 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
867 NULL, // argument types
868 0, // number of arguments
869 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +0000870
Sean Callanan0fc73582010-07-27 00:55:47 +0000871 return AddFunDecl(generic_function_type.getAsOpaquePtr());
872}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000873
Greg Claytonb01000f2011-01-17 03:46:26 +0000874clang::NamedDecl *
875NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000876{
Greg Claytona1aaaff2011-01-23 00:34:52 +0000877 if (type)
878 {
879 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000880
Sean Callanand5b3c352011-01-27 04:42:51 +0000881 if (const TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000882 {
883 TagDecl *tag_decl = tag_type->getDecl();
884
885 m_decls.push_back(tag_decl);
886
887 return tag_decl;
888 }
Sean Callanand5b3c352011-01-27 04:42:51 +0000889 else if (const ObjCObjectType *objc_object_type = dyn_cast<clang::ObjCObjectType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000890 {
891 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
892
893 m_decls.push_back((NamedDecl*)interface_decl);
894
895 return (NamedDecl*)interface_decl;
896 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000897 }
Greg Claytona1aaaff2011-01-23 00:34:52 +0000898 return NULL;
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000899}
Greg Claytone6d72ca2011-06-25 00:44:06 +0000900
901void
902NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
903{
904 for (clang::NamedDecl * const *decl_iterator = result.first;
905 decl_iterator != result.second;
906 ++decl_iterator)
907 m_decls.push_back (*decl_iterator);
908}
909
910void
911NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
912{
913 m_decls.push_back (decl);
914}