blob: 0eca0a455f801518b66acd2b89427ca5399b0015 [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
Sean Callanandc5fce12011-12-01 21:04:37 +0000506 if (!copied_type)
507 {
508 if (log)
509 log->Printf("ClangExpressionDeclMap::BuildIntegerVariable - Couldn't export the type for a constant integer result");
510
511 break;
512 }
513
Sean Callanan9394b5a2011-10-29 19:50:43 +0000514 context.AddTypeDecl(copied_type);
515 }
Sean Callanan673f3db2011-11-30 22:11:59 +0000516
Sean Callanan9394b5a2011-10-29 19:50:43 +0000517 } while(0);
518}
519
Sean Callanan9b714842011-11-09 19:33:21 +0000520void
521ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
522{
523 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
524
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000525 static unsigned int invocation_id = 0;
526 unsigned int current_id = invocation_id++;
527
Sean Callanan9b714842011-11-09 19:33:21 +0000528 const DeclarationName &decl_name(context.m_decl_name);
529 const DeclContext *decl_ctx(context.m_decl_context);
530
531 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
532
533 if (!interface_decl)
534 return;
535
536 StreamString ss;
537 if (decl_name.isObjCZeroArgSelector())
538 {
539 ss.Printf("%s", decl_name.getAsString().c_str());
540 }
541 else if (decl_name.isObjCOneArgSelector())
542 {
Sean Callanan1d9ffe22011-11-14 18:29:46 +0000543 ss.Printf("%s", decl_name.getAsString().c_str());
Sean Callanan9b714842011-11-09 19:33:21 +0000544 }
545 else
546 {
547 clang::Selector sel = decl_name.getObjCSelector();
548
549 for (unsigned i = 0, e = sel.getNumArgs();
550 i != e;
551 ++i)
552 {
553 llvm::StringRef r = sel.getNameForSlot(i);
554 ss.Printf("%s:", r.str().c_str());
555 }
556 }
557 ss.Flush();
558
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000559 ConstString selector_name(ss.GetData());
560
Sean Callanan9b714842011-11-09 19:33:21 +0000561 if (log)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000562 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
563 current_id,
564 m_ast_context,
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000565 interface_decl->getNameAsString().c_str(),
566 selector_name.AsCString());
567
568 SymbolContextList sc_list;
569
570 const bool include_symbols = false;
571 const bool append = false;
572
573 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, append, sc_list);
574
575 for (uint32_t i = 0, e = sc_list.GetSize();
576 i != e;
577 ++i)
578 {
579 SymbolContext sc;
580
581 if (!sc_list.GetContextAtIndex(i, sc))
582 continue;
583
584 if (!sc.function)
585 continue;
586
587 DeclContext *function_ctx = sc.function->GetClangDeclContext();
588
589 if (!function_ctx)
590 continue;
591
592 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
593
594 if (!method_decl)
595 continue;
596
597 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
598
599 if (!found_interface_decl)
600 continue;
601
602 if (found_interface_decl->getName() == interface_decl->getName())
603 {
Sean Callanan4938bd62011-11-16 18:20:47 +0000604 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000605
606 if (!copied_decl)
607 continue;
608
609 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
610
611 if (!copied_method_decl)
612 continue;
613
614 if (log)
615 {
616 ASTDumper dumper((Decl*)copied_method_decl);
617 log->Printf(" CAS::FOMD[%d] found %s", current_id, dumper.GetCString());
618 }
619
620 context.AddNamedDecl(copied_method_decl);
621 }
622 }
Sean Callanan9b714842011-11-09 19:33:21 +0000623}
624
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000625void
626ClangASTSource::FindObjCPropertyDecls (NameSearchContext &context)
627{
628 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
629
630 static unsigned int invocation_id = 0;
631 unsigned int current_id = invocation_id++;
632
633 const ObjCInterfaceDecl *iface_decl = cast<ObjCInterfaceDecl>(context.m_decl_context);
634 Decl *orig_decl;
635 ASTContext *orig_ast_ctx;
636
637 m_ast_importer->ResolveDeclOrigin(iface_decl, &orig_decl, &orig_ast_ctx);
638
639 if (!orig_decl)
640 return;
641
642 ObjCInterfaceDecl *orig_iface_decl = dyn_cast<ObjCInterfaceDecl>(orig_decl);
643
644 if (!orig_iface_decl)
645 return;
646
647 if (!ClangASTContext::GetCompleteDecl(orig_ast_ctx, orig_iface_decl))
648 return;
649
650 std::string property_name_str = context.m_decl_name.getAsString();
651 StringRef property_name(property_name_str.c_str());
652 ObjCPropertyDecl *property_decl = orig_iface_decl->FindPropertyDeclaration(&orig_ast_ctx->Idents.get(property_name));
653
654 if (log)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000655 log->Printf("ClangASTSource::FindObjCPropertyDecls[%d] on (ASTContext*)%p for property '%s.%s'",
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000656 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000657 m_ast_context,
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000658 iface_decl->getNameAsString().c_str(),
659 property_name_str.c_str());
660
661 if (!property_decl)
662 return;
663
Sean Callanan4938bd62011-11-16 18:20:47 +0000664 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, orig_ast_ctx, property_decl);
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000665
666 if (!copied_decl)
667 return;
668
669 ObjCPropertyDecl *copied_property_decl = dyn_cast<ObjCPropertyDecl>(copied_decl);
670
671 if (!copied_property_decl)
672 return;
673
674 if (log)
675 {
676 ASTDumper dumper((Decl*)copied_property_decl);
677 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
678 }
679
680 context.AddNamedDecl(copied_property_decl);
681}
682
Sean Callanan73b520f2011-10-29 01:58:46 +0000683void
684ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
685 const ConstString &name,
686 ClangASTImporter::NamespaceMapSP &parent_map) const
687{
688 static unsigned int invocation_id = 0;
689 unsigned int current_id = invocation_id++;
690
691 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
692
693 if (log)
694 {
695 if (parent_map && parent_map->size())
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000696 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +0000697 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000698 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +0000699 name.GetCString(),
700 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
701 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000702 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +0000703 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000704 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +0000705 name.GetCString());
706 }
707
708
709 if (parent_map)
710 {
711 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
712 i != e;
713 ++i)
714 {
715 ClangNamespaceDecl found_namespace_decl;
716
717 lldb::ModuleSP module_sp = i->first;
718 ClangNamespaceDecl module_parent_namespace_decl = i->second;
719
720 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
721
722 if (!symbol_vendor)
723 continue;
724
725 SymbolContext null_sc;
726
727 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
728
729 if (!found_namespace_decl)
730 continue;
731
732 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
733
734 if (log)
735 log->Printf(" CMN[%u] Found namespace %s in module %s",
736 current_id,
737 name.GetCString(),
738 module_sp->GetFileSpec().GetFilename().GetCString());
739 }
740 }
741 else
742 {
743 ModuleList &images = m_target->GetImages();
744 ClangNamespaceDecl null_namespace_decl;
745
746 for (uint32_t i = 0, e = images.GetSize();
747 i != e;
748 ++i)
749 {
750 lldb::ModuleSP image = images.GetModuleAtIndex(i);
751
752 if (!image)
753 continue;
754
755 ClangNamespaceDecl found_namespace_decl;
756
757 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
758
759 if (!symbol_vendor)
760 continue;
761
762 SymbolContext null_sc;
763
764 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
765
766 if (!found_namespace_decl)
767 continue;
768
769 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
770
771 if (log)
772 log->Printf(" CMN[%u] Found namespace %s in module %s",
773 current_id,
774 name.GetCString(),
775 image->GetFileSpec().GetFilename().GetCString());
776 }
777 }
778}
779
Sean Callananbb715f92011-10-29 02:28:18 +0000780NamespaceDecl *
781ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
782{
783 if (namespace_decls.empty())
784 return NULL;
785
786 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
787
788 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
789
Sean Callanan4938bd62011-11-16 18:20:47 +0000790 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
Sean Callananbb715f92011-10-29 02:28:18 +0000791
792 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
793
794 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
795
796 return dyn_cast<NamespaceDecl>(copied_decl);
797}
798
Sean Callanan9394b5a2011-10-29 19:50:43 +0000799void *
800ClangASTSource::GuardedCopyType (ASTContext *dest_context,
801 ASTContext *source_context,
802 void *clang_type)
803{
804 SetImportInProgress(true);
805
Sean Callanan4938bd62011-11-16 18:20:47 +0000806 QualType ret_qual_type = m_ast_importer->CopyType (m_ast_context, source_context, QualType::getFromOpaquePtr(clang_type));
Sean Callanan9394b5a2011-10-29 19:50:43 +0000807
808 void *ret = ret_qual_type.getAsOpaquePtr();
809
810 SetImportInProgress(false);
811
812 return ret;
813}
814
Greg Claytonb01000f2011-01-17 03:46:26 +0000815clang::NamedDecl *
816NameSearchContext::AddVarDecl(void *type)
817{
Greg Clayton8de27c72010-10-15 22:48:33 +0000818 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +0000819
820 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +0000821
Sean Callananf76afff2011-10-28 23:38:38 +0000822 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000823 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +0000824 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000825 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000826 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000827 QualType::getFromOpaquePtr(type),
828 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000829 SC_Static,
830 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +0000831 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000832
833 return Decl;
834}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000835
Greg Claytonb01000f2011-01-17 03:46:26 +0000836clang::NamedDecl *
837NameSearchContext::AddFunDecl (void *type)
838{
Sean Callananf76afff2011-10-28 23:38:38 +0000839 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000840 const_cast<DeclContext*>(m_decl_context),
841 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000842 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +0000843 m_decl_name.getAsIdentifierInfo(),
844 QualType::getFromOpaquePtr(type),
845 NULL,
846 SC_Static,
847 SC_Static,
848 false,
849 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000850
Sean Callananb291abe2010-08-12 23:45:38 +0000851 // We have to do more than just synthesize the FunctionDecl. We have to
852 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
853 // this, we raid the function's FunctionProtoType for types.
854
Greg Clayton8de27c72010-10-15 22:48:33 +0000855 QualType qual_type (QualType::getFromOpaquePtr(type));
Sean Callanandc5fce12011-12-01 21:04:37 +0000856 const FunctionProtoType *func_proto_type = dyn_cast<FunctionProtoType>(qual_type.getTypePtr());
Sean Callanan8f0dc342010-06-22 23:46:24 +0000857
Greg Clayton8de27c72010-10-15 22:48:33 +0000858 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +0000859 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000860 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000861 unsigned ArgIndex;
862
Sean Callananc1535182011-10-07 23:18:13 +0000863 SmallVector<ParmVarDecl *, 5> parm_var_decls;
864
Sean Callanan8f0dc342010-06-22 23:46:24 +0000865 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
866 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000867 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000868
Sean Callananf76afff2011-10-28 23:38:38 +0000869 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callananc1535182011-10-07 23:18:13 +0000870 const_cast<DeclContext*>(m_decl_context),
871 SourceLocation(),
872 SourceLocation(),
873 NULL,
874 arg_qual_type,
875 NULL,
876 SC_Static,
877 SC_Static,
878 NULL));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000879 }
880
Sean Callananc1535182011-10-07 23:18:13 +0000881 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000882 }
Sean Callanandc5fce12011-12-01 21:04:37 +0000883 else
884 {
885 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
886
887 log->Printf("Function type wasn't a FunctionProtoType");
888 }
Sean Callanan8f0dc342010-06-22 23:46:24 +0000889
Greg Clayton8de27c72010-10-15 22:48:33 +0000890 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000891
Greg Clayton8de27c72010-10-15 22:48:33 +0000892 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000893}
Sean Callanan0fc73582010-07-27 00:55:47 +0000894
Greg Claytonb01000f2011-01-17 03:46:26 +0000895clang::NamedDecl *
896NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +0000897{
Sean Callananad293092011-01-18 23:32:05 +0000898 FunctionProtoType::ExtProtoInfo proto_info;
899
900 proto_info.Variadic = true;
901
Sean Callananf76afff2011-10-28 23:38:38 +0000902 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
903 NULL, // argument types
904 0, // number of arguments
905 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +0000906
Sean Callanan0fc73582010-07-27 00:55:47 +0000907 return AddFunDecl(generic_function_type.getAsOpaquePtr());
908}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000909
Greg Claytonb01000f2011-01-17 03:46:26 +0000910clang::NamedDecl *
911NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000912{
Greg Claytona1aaaff2011-01-23 00:34:52 +0000913 if (type)
914 {
915 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000916
Sean Callanand5b3c352011-01-27 04:42:51 +0000917 if (const TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000918 {
919 TagDecl *tag_decl = tag_type->getDecl();
920
921 m_decls.push_back(tag_decl);
922
923 return tag_decl;
924 }
Sean Callanand5b3c352011-01-27 04:42:51 +0000925 else if (const ObjCObjectType *objc_object_type = dyn_cast<clang::ObjCObjectType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000926 {
927 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
928
929 m_decls.push_back((NamedDecl*)interface_decl);
930
931 return (NamedDecl*)interface_decl;
932 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000933 }
Greg Claytona1aaaff2011-01-23 00:34:52 +0000934 return NULL;
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000935}
Greg Claytone6d72ca2011-06-25 00:44:06 +0000936
937void
938NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
939{
940 for (clang::NamedDecl * const *decl_iterator = result.first;
941 decl_iterator != result.second;
942 ++decl_iterator)
943 m_decls.push_back (*decl_iterator);
944}
945
946void
947NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
948{
949 m_decls.push_back (decl);
950}