blob: ca97fc19d34f2ab45641452573933e272a375e4a [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"
Sean Callanan673f3db2011-11-30 22:11:59 +000020#include "lldb/Target/ObjCLanguageRuntime.h"
Sean Callanan73b520f2011-10-29 01:58:46 +000021#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022
23using namespace clang;
24using namespace lldb_private;
25
Greg Claytonb01000f2011-01-17 03:46:26 +000026ClangASTSource::~ClangASTSource()
27{
Sean Callanana3d04472011-11-29 00:42:02 +000028 m_ast_importer->ForgetDestination(m_ast_context);
29
Johnny Chenfa21ffd2011-11-30 23:18:53 +000030 // We are in the process of destruction, don't create clang ast context on demand
31 // by passing false to Target::GetScratchClangASTContext(create_on_demand).
32 ClangASTContext *scratch_clang_ast_context = m_target->GetScratchClangASTContext(false);
Sean Callanana3d04472011-11-29 00:42:02 +000033
34 if (!scratch_clang_ast_context)
35 return;
36
37 clang::ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
38
39 if (!scratch_ast_context)
40 return;
41
42 if (m_ast_context != scratch_ast_context)
43 m_ast_importer->ForgetSource(scratch_ast_context, m_ast_context);
Greg Claytonb01000f2011-01-17 03:46:26 +000044}
Chris Lattner24943d22010-06-08 16:52:24 +000045
Greg Claytonb01000f2011-01-17 03:46:26 +000046void
47ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer)
48{
Sean Callananf76afff2011-10-28 23:38:38 +000049 if (!m_ast_context)
50 return;
51
52 m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage();
53 m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage();
Chris Lattner24943d22010-06-08 16:52:24 +000054}
55
Chris Lattner24943d22010-06-08 16:52:24 +000056// The core lookup interface.
Greg Claytonb01000f2011-01-17 03:46:26 +000057DeclContext::lookup_result
58ClangASTSource::FindExternalVisibleDeclsByName
Greg Claytonf4c7ae02010-10-15 03:36:13 +000059(
60 const DeclContext *decl_ctx,
Greg Clayton8de27c72010-10-15 22:48:33 +000061 DeclarationName clang_decl_name
Greg Claytonf4c7ae02010-10-15 03:36:13 +000062)
63{
Sean Callananf76afff2011-10-28 23:38:38 +000064 if (!m_ast_context)
65 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
66
67 if (GetImportInProgress())
Greg Claytonb01000f2011-01-17 03:46:26 +000068 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
69
70 std::string decl_name (clang_decl_name.getAsString());
71
72// if (m_decl_map.DoingASTImport ())
73// return DeclContext::lookup_result();
74//
Greg Clayton8de27c72010-10-15 22:48:33 +000075 switch (clang_decl_name.getNameKind()) {
Chris Lattner24943d22010-06-08 16:52:24 +000076 // Normal identifiers.
77 case DeclarationName::Identifier:
Greg Clayton8de27c72010-10-15 22:48:33 +000078 if (clang_decl_name.getAsIdentifierInfo()->getBuiltinID() != 0)
79 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
80 break;
Chris Lattner24943d22010-06-08 16:52:24 +000081
82 // Operator names. Not important for now.
83 case DeclarationName::CXXOperatorName:
84 case DeclarationName::CXXLiteralOperatorName:
85 return DeclContext::lookup_result();
86
87 // Using directives found in this context.
88 // Tell Sema we didn't find any or we'll end up getting asked a *lot*.
89 case DeclarationName::CXXUsingDirective:
Greg Clayton8de27c72010-10-15 22:48:33 +000090 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
Chris Lattner24943d22010-06-08 16:52:24 +000091
Chris Lattner24943d22010-06-08 16:52:24 +000092 case DeclarationName::ObjCZeroArgSelector:
93 case DeclarationName::ObjCOneArgSelector:
94 case DeclarationName::ObjCMultiArgSelector:
Sean Callanan9b714842011-11-09 19:33:21 +000095 {
96 llvm::SmallVector<NamedDecl*, 1> method_decls;
Chris Lattner24943d22010-06-08 16:52:24 +000097
Sean Callanan9b714842011-11-09 19:33:21 +000098 NameSearchContext method_search_context (*this, method_decls, clang_decl_name, decl_ctx);
99
100 FindObjCMethodDecls(method_search_context);
101
102 return SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, method_decls);
103 }
Chris Lattner24943d22010-06-08 16:52:24 +0000104 // These aren't possible in the global context.
105 case DeclarationName::CXXConstructorName:
106 case DeclarationName::CXXDestructorName:
107 case DeclarationName::CXXConversionFunctionName:
108 return DeclContext::lookup_result();
109 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000110
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000111
Sean Callananf76afff2011-10-28 23:38:38 +0000112 if (!GetLookupsEnabled())
Greg Clayton8de27c72010-10-15 22:48:33 +0000113 {
114 // Wait until we see a '$' at the start of a name before we start doing
115 // any lookups so we can avoid lookup up all of the builtin types.
116 if (!decl_name.empty() && decl_name[0] == '$')
117 {
Sean Callananf76afff2011-10-28 23:38:38 +0000118 SetLookupsEnabled (true);
Greg Clayton8de27c72010-10-15 22:48:33 +0000119 }
120 else
121 {
122 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
123 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000124 }
Greg Clayton8de27c72010-10-15 22:48:33 +0000125
Greg Clayton8de27c72010-10-15 22:48:33 +0000126 ConstString const_decl_name(decl_name.c_str());
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000127
128 const char *uniqued_const_decl_name = const_decl_name.GetCString();
129 if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end())
130 {
131 // We are currently looking up this name...
132 return DeclContext::lookup_result();
133 }
134 m_active_lookups.insert(uniqued_const_decl_name);
Greg Claytona8b278a2010-11-15 01:34:18 +0000135// static uint32_t g_depth = 0;
136// ++g_depth;
137// printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000138 llvm::SmallVector<NamedDecl*, 4> name_decls;
139 NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx);
Sean Callananf76afff2011-10-28 23:38:38 +0000140 FindExternalVisibleDecls(name_search_context);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000141 DeclContext::lookup_result result (SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls));
Greg Claytona8b278a2010-11-15 01:34:18 +0000142// --g_depth;
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000143 m_active_lookups.erase (uniqued_const_decl_name);
144 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000145}
146
Greg Claytonb01000f2011-01-17 03:46:26 +0000147void
148ClangASTSource::CompleteType (TagDecl *tag_decl)
Sean Callananbb715f92011-10-29 02:28:18 +0000149{
150 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
151
152 if (log)
153 {
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000154 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 +0000155 log->Printf(" [CTD] Before:");
156 ASTDumper dumper((Decl*)tag_decl);
157 dumper.ToLog(log, " [CTD] ");
158 }
159
160 m_ast_importer->CompleteTagDecl (tag_decl);
161
162 if (log)
163 {
164 log->Printf(" [CTD] After:");
165 ASTDumper dumper((Decl*)tag_decl);
166 dumper.ToLog(log, " [CTD] ");
167 }
Greg Claytonb01000f2011-01-17 03:46:26 +0000168}
169
170void
Sean Callananbb715f92011-10-29 02:28:18 +0000171ClangASTSource::CompleteType (clang::ObjCInterfaceDecl *interface_decl)
172{
173 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
174
175 if (log)
176 {
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000177 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 +0000178 log->Printf(" [COID] Before:");
179 ASTDumper dumper((Decl*)interface_decl);
180 dumper.ToLog(log, " [COID] ");
181 }
182
183 m_ast_importer->CompleteObjCInterfaceDecl (interface_decl);
184
185 if (log)
186 {
187 log->Printf(" [COID] After:");
188 ASTDumper dumper((Decl*)interface_decl);
189 dumper.ToLog(log, " [COID] ");
190 }
Greg Claytonb01000f2011-01-17 03:46:26 +0000191}
192
Sean Callanan9b6898f2011-07-30 02:42:06 +0000193clang::ExternalLoadResult
Sean Callananbb715f92011-10-29 02:28:18 +0000194ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context,
195 bool (*predicate)(Decl::Kind),
196 llvm::SmallVectorImpl<Decl*> &decls)
197{
198 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
199
200 const Decl *context_decl = dyn_cast<Decl>(decl_context);
201
202 if (!context_decl)
203 return ELR_Failure;
204
205 static unsigned int invocation_id = 0;
206 unsigned int current_id = invocation_id++;
207
208 if (log)
209 {
210 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000211 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000212 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000213 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000214 context_named_decl->getNameAsString().c_str(),
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000215 context_decl->getDeclKindName(),
216 context_decl,
Sean Callananbb715f92011-10-29 02:28:18 +0000217 (predicate ? "non-null" : "null"));
218 else if(context_decl)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000219 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%sDecl*)%p with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000220 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000221 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000222 context_decl->getDeclKindName(),
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000223 context_decl,
Sean Callananbb715f92011-10-29 02:28:18 +0000224 (predicate ? "non-null" : "null"));
225 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000226 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000227 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000228 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000229 (predicate ? "non-null" : "null"));
230 }
231
232 Decl *original_decl = NULL;
233 ASTContext *original_ctx = NULL;
234
235 if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx))
236 return ELR_Failure;
237
238 if (log)
239 {
240 log->Printf(" FELD[%u] Original decl:", current_id);
241 ASTDumper(original_decl).ToLog(log, " ");
242 }
243
244 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
245 {
246 ExternalASTSource *external_source = original_ctx->getExternalSource();
247
248 if (external_source)
249 external_source->CompleteType (original_tag_decl);
250 }
251
252 DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl);
253
254 if (!original_decl_context)
255 return ELR_Failure;
256
257 for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
258 iter != original_decl_context->decls_end();
259 ++iter)
260 {
261 Decl *decl = *iter;
262
263 if (!predicate || predicate(decl->getKind()))
264 {
265 if (log)
266 {
267 ASTDumper ast_dumper(decl);
268 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
269 log->Printf(" FELD[%d] Adding [to %s] lexical decl %s", current_id, context_named_decl->getNameAsString().c_str(), ast_dumper.GetCString());
270 else
271 log->Printf(" FELD[%d] Adding lexical decl %s", current_id, ast_dumper.GetCString());
272 }
273
Sean Callanan4938bd62011-11-16 18:20:47 +0000274 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, original_ctx, decl);
Sean Callananbb715f92011-10-29 02:28:18 +0000275
276 decls.push_back(copied_decl);
277 }
278 }
279
280 return ELR_AlreadyLoaded;
Chris Lattner24943d22010-06-08 16:52:24 +0000281}
282
Sean Callanan9394b5a2011-10-29 19:50:43 +0000283void
284ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
285{
286 assert (m_ast_context);
287
288 const ConstString name(context.m_decl_name.getAsString().c_str());
289
290 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
291
292 static unsigned int invocation_id = 0;
293 unsigned int current_id = invocation_id++;
294
295 if (log)
296 {
297 if (!context.m_decl_context)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000298 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 +0000299 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000300 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 +0000301 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000302 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 +0000303 }
304
305 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
306
307 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
308 {
309 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
310
311 if (log && log->GetVerbose())
312 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
313 current_id,
314 namespace_map.get(),
315 (int)namespace_map->size());
316
317 if (!namespace_map)
318 return;
319
320 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
321 i != e;
322 ++i)
323 {
324 if (log)
325 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
326 current_id,
327 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
328 i->first->GetFileSpec().GetFilename().GetCString());
329
330 FindExternalVisibleDecls(context,
331 i->first,
332 i->second,
333 current_id);
334 }
335 }
Sean Callanand3812fa2011-11-15 21:50:18 +0000336 else if (isa<ObjCInterfaceDecl>(context.m_decl_context))
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000337 {
338 FindObjCPropertyDecls(context);
339 }
Sean Callanan9394b5a2011-10-29 19:50:43 +0000340 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
341 {
342 // we shouldn't be getting FindExternalVisibleDecls calls for these
343 return;
344 }
345 else
346 {
347 ClangNamespaceDecl namespace_decl;
348
349 if (log)
350 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
351
352 FindExternalVisibleDecls(context,
353 lldb::ModuleSP(),
354 namespace_decl,
355 current_id);
356 }
357
358 if (!context.m_namespace_map->empty())
359 {
360 if (log && log->GetVerbose())
361 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
362 current_id,
363 context.m_namespace_map.get(),
364 (int)context.m_namespace_map->size());
365
366 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
367
368 if (clang_namespace_decl)
369 clang_namespace_decl->setHasExternalVisibleStorage();
370 }
371}
372
373void
374ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
375 lldb::ModuleSP module_sp,
376 ClangNamespaceDecl &namespace_decl,
377 unsigned int current_id)
378{
379 assert (m_ast_context);
380
381 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
382
383 SymbolContextList sc_list;
384
385 const ConstString name(context.m_decl_name.getAsString().c_str());
386
387 const char *name_unique_cstr = name.GetCString();
388
389 if (name_unique_cstr == NULL)
390 return;
391
392 // The ClangASTSource is not responsible for finding $-names.
393 if (name_unique_cstr[0] == '$')
394 return;
395
396 if (module_sp && namespace_decl)
397 {
398 ClangNamespaceDecl found_namespace_decl;
399
400 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
401
402 if (symbol_vendor)
403 {
404 SymbolContext null_sc;
405
406 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
407
408 if (found_namespace_decl)
409 {
410 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
411
412 if (log)
413 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
414 current_id,
415 name.GetCString(),
416 module_sp->GetFileSpec().GetFilename().GetCString());
417 }
418 }
419 }
420 else
421 {
422 ModuleList &images = m_target->GetImages();
423
424 for (uint32_t i = 0, e = images.GetSize();
425 i != e;
426 ++i)
427 {
428 lldb::ModuleSP image = images.GetModuleAtIndex(i);
429
430 if (!image)
431 continue;
432
433 ClangNamespaceDecl found_namespace_decl;
434
435 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
436
437 if (!symbol_vendor)
438 continue;
439
440 SymbolContext null_sc;
441
442 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
443
444 if (found_namespace_decl)
445 {
446 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
447
448 if (log)
449 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
450 current_id,
451 name.GetCString(),
452 image->GetFileSpec().GetFilename().GetCString());
453 }
454 }
455 }
456
457 static ConstString id_name("id");
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000458 static ConstString Class_name("Class");
Sean Callanan9394b5a2011-10-29 19:50:43 +0000459
460 do
461 {
462 TypeList types;
463 SymbolContext null_sc;
464
465 if (module_sp && namespace_decl)
Sean Callanan673f3db2011-11-30 22:11:59 +0000466 {
Sean Callanan9394b5a2011-10-29 19:50:43 +0000467 module_sp->FindTypes(null_sc, name, &namespace_decl, true, 1, types);
Sean Callanan673f3db2011-11-30 22:11:59 +0000468 }
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000469 else if(name != id_name && name != Class_name)
Sean Callanan673f3db2011-11-30 22:11:59 +0000470 {
Sean Callanan9394b5a2011-10-29 19:50:43 +0000471 m_target->GetImages().FindTypes (null_sc, name, true, 1, types);
Sean Callanan673f3db2011-11-30 22:11:59 +0000472
473 if (!types.GetSize())
474 {
475 lldb::ProcessSP process = m_target->GetProcessSP();
476
477 if (process && process->GetObjCLanguageRuntime())
478 {
479 SymbolVendor *objc_symbol_vendor = process->GetObjCLanguageRuntime()->GetSymbolVendor();
480
481 objc_symbol_vendor->FindTypes(null_sc, name, NULL, true, 1, types);
482 }
483 }
484 }
Sean Callanan9394b5a2011-10-29 19:50:43 +0000485 else
Sean Callanan673f3db2011-11-30 22:11:59 +0000486 {
Sean Callanan9394b5a2011-10-29 19:50:43 +0000487 break;
Sean Callanan673f3db2011-11-30 22:11:59 +0000488 }
Sean Callanan9394b5a2011-10-29 19:50:43 +0000489
490 if (types.GetSize())
491 {
492 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
493
494 if (log)
495 {
496 const char *name_string = type_sp->GetName().GetCString();
497
498 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
499 current_id,
500 name.GetCString(),
501 (name_string ? name_string : "<anonymous>"));
502 }
503
504 void *copied_type = GuardedCopyType(m_ast_context, type_sp->GetClangAST(), type_sp->GetClangFullType());
505
506 context.AddTypeDecl(copied_type);
507 }
Sean Callanan673f3db2011-11-30 22:11:59 +0000508
Sean Callanan9394b5a2011-10-29 19:50:43 +0000509 } while(0);
510}
511
Sean Callanan9b714842011-11-09 19:33:21 +0000512void
513ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
514{
515 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
516
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000517 static unsigned int invocation_id = 0;
518 unsigned int current_id = invocation_id++;
519
Sean Callanan9b714842011-11-09 19:33:21 +0000520 const DeclarationName &decl_name(context.m_decl_name);
521 const DeclContext *decl_ctx(context.m_decl_context);
522
523 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
524
525 if (!interface_decl)
526 return;
527
528 StreamString ss;
529 if (decl_name.isObjCZeroArgSelector())
530 {
531 ss.Printf("%s", decl_name.getAsString().c_str());
532 }
533 else if (decl_name.isObjCOneArgSelector())
534 {
Sean Callanan1d9ffe22011-11-14 18:29:46 +0000535 ss.Printf("%s", decl_name.getAsString().c_str());
Sean Callanan9b714842011-11-09 19:33:21 +0000536 }
537 else
538 {
539 clang::Selector sel = decl_name.getObjCSelector();
540
541 for (unsigned i = 0, e = sel.getNumArgs();
542 i != e;
543 ++i)
544 {
545 llvm::StringRef r = sel.getNameForSlot(i);
546 ss.Printf("%s:", r.str().c_str());
547 }
548 }
549 ss.Flush();
550
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000551 ConstString selector_name(ss.GetData());
552
Sean Callanan9b714842011-11-09 19:33:21 +0000553 if (log)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000554 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
555 current_id,
556 m_ast_context,
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000557 interface_decl->getNameAsString().c_str(),
558 selector_name.AsCString());
559
560 SymbolContextList sc_list;
561
562 const bool include_symbols = false;
563 const bool append = false;
564
565 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, append, sc_list);
566
567 for (uint32_t i = 0, e = sc_list.GetSize();
568 i != e;
569 ++i)
570 {
571 SymbolContext sc;
572
573 if (!sc_list.GetContextAtIndex(i, sc))
574 continue;
575
576 if (!sc.function)
577 continue;
578
579 DeclContext *function_ctx = sc.function->GetClangDeclContext();
580
581 if (!function_ctx)
582 continue;
583
584 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
585
586 if (!method_decl)
587 continue;
588
589 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
590
591 if (!found_interface_decl)
592 continue;
593
594 if (found_interface_decl->getName() == interface_decl->getName())
595 {
Sean Callanan4938bd62011-11-16 18:20:47 +0000596 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000597
598 if (!copied_decl)
599 continue;
600
601 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
602
603 if (!copied_method_decl)
604 continue;
605
606 if (log)
607 {
608 ASTDumper dumper((Decl*)copied_method_decl);
609 log->Printf(" CAS::FOMD[%d] found %s", current_id, dumper.GetCString());
610 }
611
612 context.AddNamedDecl(copied_method_decl);
613 }
614 }
Sean Callanan9b714842011-11-09 19:33:21 +0000615}
616
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000617void
618ClangASTSource::FindObjCPropertyDecls (NameSearchContext &context)
619{
620 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
621
622 static unsigned int invocation_id = 0;
623 unsigned int current_id = invocation_id++;
624
625 const ObjCInterfaceDecl *iface_decl = cast<ObjCInterfaceDecl>(context.m_decl_context);
626 Decl *orig_decl;
627 ASTContext *orig_ast_ctx;
628
629 m_ast_importer->ResolveDeclOrigin(iface_decl, &orig_decl, &orig_ast_ctx);
630
631 if (!orig_decl)
632 return;
633
634 ObjCInterfaceDecl *orig_iface_decl = dyn_cast<ObjCInterfaceDecl>(orig_decl);
635
636 if (!orig_iface_decl)
637 return;
638
639 if (!ClangASTContext::GetCompleteDecl(orig_ast_ctx, orig_iface_decl))
640 return;
641
642 std::string property_name_str = context.m_decl_name.getAsString();
643 StringRef property_name(property_name_str.c_str());
644 ObjCPropertyDecl *property_decl = orig_iface_decl->FindPropertyDeclaration(&orig_ast_ctx->Idents.get(property_name));
645
646 if (log)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000647 log->Printf("ClangASTSource::FindObjCPropertyDecls[%d] on (ASTContext*)%p for property '%s.%s'",
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000648 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000649 m_ast_context,
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000650 iface_decl->getNameAsString().c_str(),
651 property_name_str.c_str());
652
653 if (!property_decl)
654 return;
655
Sean Callanan4938bd62011-11-16 18:20:47 +0000656 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, orig_ast_ctx, property_decl);
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000657
658 if (!copied_decl)
659 return;
660
661 ObjCPropertyDecl *copied_property_decl = dyn_cast<ObjCPropertyDecl>(copied_decl);
662
663 if (!copied_property_decl)
664 return;
665
666 if (log)
667 {
668 ASTDumper dumper((Decl*)copied_property_decl);
669 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
670 }
671
672 context.AddNamedDecl(copied_property_decl);
673}
674
Sean Callanan73b520f2011-10-29 01:58:46 +0000675void
676ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
677 const ConstString &name,
678 ClangASTImporter::NamespaceMapSP &parent_map) const
679{
680 static unsigned int invocation_id = 0;
681 unsigned int current_id = invocation_id++;
682
683 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
684
685 if (log)
686 {
687 if (parent_map && parent_map->size())
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000688 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +0000689 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000690 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +0000691 name.GetCString(),
692 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
693 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000694 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +0000695 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000696 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +0000697 name.GetCString());
698 }
699
700
701 if (parent_map)
702 {
703 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
704 i != e;
705 ++i)
706 {
707 ClangNamespaceDecl found_namespace_decl;
708
709 lldb::ModuleSP module_sp = i->first;
710 ClangNamespaceDecl module_parent_namespace_decl = i->second;
711
712 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
713
714 if (!symbol_vendor)
715 continue;
716
717 SymbolContext null_sc;
718
719 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
720
721 if (!found_namespace_decl)
722 continue;
723
724 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
725
726 if (log)
727 log->Printf(" CMN[%u] Found namespace %s in module %s",
728 current_id,
729 name.GetCString(),
730 module_sp->GetFileSpec().GetFilename().GetCString());
731 }
732 }
733 else
734 {
735 ModuleList &images = m_target->GetImages();
736 ClangNamespaceDecl null_namespace_decl;
737
738 for (uint32_t i = 0, e = images.GetSize();
739 i != e;
740 ++i)
741 {
742 lldb::ModuleSP image = images.GetModuleAtIndex(i);
743
744 if (!image)
745 continue;
746
747 ClangNamespaceDecl found_namespace_decl;
748
749 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
750
751 if (!symbol_vendor)
752 continue;
753
754 SymbolContext null_sc;
755
756 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
757
758 if (!found_namespace_decl)
759 continue;
760
761 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
762
763 if (log)
764 log->Printf(" CMN[%u] Found namespace %s in module %s",
765 current_id,
766 name.GetCString(),
767 image->GetFileSpec().GetFilename().GetCString());
768 }
769 }
770}
771
Sean Callananbb715f92011-10-29 02:28:18 +0000772NamespaceDecl *
773ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
774{
775 if (namespace_decls.empty())
776 return NULL;
777
778 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
779
780 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
781
Sean Callanan4938bd62011-11-16 18:20:47 +0000782 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
Sean Callananbb715f92011-10-29 02:28:18 +0000783
784 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
785
786 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
787
788 return dyn_cast<NamespaceDecl>(copied_decl);
789}
790
Sean Callanan9394b5a2011-10-29 19:50:43 +0000791void *
792ClangASTSource::GuardedCopyType (ASTContext *dest_context,
793 ASTContext *source_context,
794 void *clang_type)
795{
796 SetImportInProgress(true);
797
Sean Callanan4938bd62011-11-16 18:20:47 +0000798 QualType ret_qual_type = m_ast_importer->CopyType (m_ast_context, source_context, QualType::getFromOpaquePtr(clang_type));
Sean Callanan9394b5a2011-10-29 19:50:43 +0000799
800 void *ret = ret_qual_type.getAsOpaquePtr();
801
802 SetImportInProgress(false);
803
804 return ret;
805}
806
Greg Claytonb01000f2011-01-17 03:46:26 +0000807clang::NamedDecl *
808NameSearchContext::AddVarDecl(void *type)
809{
Greg Clayton8de27c72010-10-15 22:48:33 +0000810 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +0000811
812 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +0000813
Sean Callananf76afff2011-10-28 23:38:38 +0000814 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000815 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +0000816 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000817 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000818 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000819 QualType::getFromOpaquePtr(type),
820 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000821 SC_Static,
822 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +0000823 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000824
825 return Decl;
826}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000827
Greg Claytonb01000f2011-01-17 03:46:26 +0000828clang::NamedDecl *
829NameSearchContext::AddFunDecl (void *type)
830{
Sean Callananf76afff2011-10-28 23:38:38 +0000831 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000832 const_cast<DeclContext*>(m_decl_context),
833 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000834 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +0000835 m_decl_name.getAsIdentifierInfo(),
836 QualType::getFromOpaquePtr(type),
837 NULL,
838 SC_Static,
839 SC_Static,
840 false,
841 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000842
Sean Callananb291abe2010-08-12 23:45:38 +0000843 // We have to do more than just synthesize the FunctionDecl. We have to
844 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
845 // this, we raid the function's FunctionProtoType for types.
846
Greg Clayton8de27c72010-10-15 22:48:33 +0000847 QualType qual_type (QualType::getFromOpaquePtr(type));
848 const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000849
Greg Clayton8de27c72010-10-15 22:48:33 +0000850 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +0000851 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000852 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000853 unsigned ArgIndex;
854
Sean Callananc1535182011-10-07 23:18:13 +0000855 SmallVector<ParmVarDecl *, 5> parm_var_decls;
856
Sean Callanan8f0dc342010-06-22 23:46:24 +0000857 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
858 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000859 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000860
Sean Callananf76afff2011-10-28 23:38:38 +0000861 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callananc1535182011-10-07 23:18:13 +0000862 const_cast<DeclContext*>(m_decl_context),
863 SourceLocation(),
864 SourceLocation(),
865 NULL,
866 arg_qual_type,
867 NULL,
868 SC_Static,
869 SC_Static,
870 NULL));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000871 }
872
Sean Callananc1535182011-10-07 23:18:13 +0000873 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000874 }
875
Greg Clayton8de27c72010-10-15 22:48:33 +0000876 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000877
Greg Clayton8de27c72010-10-15 22:48:33 +0000878 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000879}
Sean Callanan0fc73582010-07-27 00:55:47 +0000880
Greg Claytonb01000f2011-01-17 03:46:26 +0000881clang::NamedDecl *
882NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +0000883{
Sean Callananad293092011-01-18 23:32:05 +0000884 FunctionProtoType::ExtProtoInfo proto_info;
885
886 proto_info.Variadic = true;
887
Sean Callananf76afff2011-10-28 23:38:38 +0000888 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
889 NULL, // argument types
890 0, // number of arguments
891 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +0000892
Sean Callanan0fc73582010-07-27 00:55:47 +0000893 return AddFunDecl(generic_function_type.getAsOpaquePtr());
894}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000895
Greg Claytonb01000f2011-01-17 03:46:26 +0000896clang::NamedDecl *
897NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000898{
Greg Claytona1aaaff2011-01-23 00:34:52 +0000899 if (type)
900 {
901 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000902
Sean Callanand5b3c352011-01-27 04:42:51 +0000903 if (const TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000904 {
905 TagDecl *tag_decl = tag_type->getDecl();
906
907 m_decls.push_back(tag_decl);
908
909 return tag_decl;
910 }
Sean Callanand5b3c352011-01-27 04:42:51 +0000911 else if (const ObjCObjectType *objc_object_type = dyn_cast<clang::ObjCObjectType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000912 {
913 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
914
915 m_decls.push_back((NamedDecl*)interface_decl);
916
917 return (NamedDecl*)interface_decl;
918 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000919 }
Greg Claytona1aaaff2011-01-23 00:34:52 +0000920 return NULL;
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000921}
Greg Claytone6d72ca2011-06-25 00:44:06 +0000922
923void
924NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
925{
926 for (clang::NamedDecl * const *decl_iterator = result.first;
927 decl_iterator != result.second;
928 ++decl_iterator)
929 m_decls.push_back (*decl_iterator);
930}
931
932void
933NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
934{
935 m_decls.push_back (decl);
936}