blob: db08539b9776146613d55dac897c5c0ca596284a [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
30 ClangASTContext *scratch_clang_ast_context = m_target->GetScratchClangASTContext();
31
32 if (!scratch_clang_ast_context)
33 return;
34
35 clang::ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
36
37 if (!scratch_ast_context)
38 return;
39
40 if (m_ast_context != scratch_ast_context)
41 m_ast_importer->ForgetSource(scratch_ast_context, m_ast_context);
Greg Claytonb01000f2011-01-17 03:46:26 +000042}
Chris Lattner24943d22010-06-08 16:52:24 +000043
Greg Claytonb01000f2011-01-17 03:46:26 +000044void
45ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer)
46{
Sean Callananf76afff2011-10-28 23:38:38 +000047 if (!m_ast_context)
48 return;
49
50 m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage();
51 m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage();
Chris Lattner24943d22010-06-08 16:52:24 +000052}
53
Chris Lattner24943d22010-06-08 16:52:24 +000054// The core lookup interface.
Greg Claytonb01000f2011-01-17 03:46:26 +000055DeclContext::lookup_result
56ClangASTSource::FindExternalVisibleDeclsByName
Greg Claytonf4c7ae02010-10-15 03:36:13 +000057(
58 const DeclContext *decl_ctx,
Greg Clayton8de27c72010-10-15 22:48:33 +000059 DeclarationName clang_decl_name
Greg Claytonf4c7ae02010-10-15 03:36:13 +000060)
61{
Sean Callananf76afff2011-10-28 23:38:38 +000062 if (!m_ast_context)
63 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
64
65 if (GetImportInProgress())
Greg Claytonb01000f2011-01-17 03:46:26 +000066 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
67
68 std::string decl_name (clang_decl_name.getAsString());
69
70// if (m_decl_map.DoingASTImport ())
71// return DeclContext::lookup_result();
72//
Greg Clayton8de27c72010-10-15 22:48:33 +000073 switch (clang_decl_name.getNameKind()) {
Chris Lattner24943d22010-06-08 16:52:24 +000074 // Normal identifiers.
75 case DeclarationName::Identifier:
Greg Clayton8de27c72010-10-15 22:48:33 +000076 if (clang_decl_name.getAsIdentifierInfo()->getBuiltinID() != 0)
77 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
78 break;
Chris Lattner24943d22010-06-08 16:52:24 +000079
80 // Operator names. Not important for now.
81 case DeclarationName::CXXOperatorName:
82 case DeclarationName::CXXLiteralOperatorName:
83 return DeclContext::lookup_result();
84
85 // Using directives found in this context.
86 // Tell Sema we didn't find any or we'll end up getting asked a *lot*.
87 case DeclarationName::CXXUsingDirective:
Greg Clayton8de27c72010-10-15 22:48:33 +000088 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
Chris Lattner24943d22010-06-08 16:52:24 +000089
Chris Lattner24943d22010-06-08 16:52:24 +000090 case DeclarationName::ObjCZeroArgSelector:
91 case DeclarationName::ObjCOneArgSelector:
92 case DeclarationName::ObjCMultiArgSelector:
Sean Callanan9b714842011-11-09 19:33:21 +000093 {
94 llvm::SmallVector<NamedDecl*, 1> method_decls;
Chris Lattner24943d22010-06-08 16:52:24 +000095
Sean Callanan9b714842011-11-09 19:33:21 +000096 NameSearchContext method_search_context (*this, method_decls, clang_decl_name, decl_ctx);
97
98 FindObjCMethodDecls(method_search_context);
99
100 return SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, method_decls);
101 }
Chris Lattner24943d22010-06-08 16:52:24 +0000102 // These aren't possible in the global context.
103 case DeclarationName::CXXConstructorName:
104 case DeclarationName::CXXDestructorName:
105 case DeclarationName::CXXConversionFunctionName:
106 return DeclContext::lookup_result();
107 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000108
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000109
Sean Callananf76afff2011-10-28 23:38:38 +0000110 if (!GetLookupsEnabled())
Greg Clayton8de27c72010-10-15 22:48:33 +0000111 {
112 // Wait until we see a '$' at the start of a name before we start doing
113 // any lookups so we can avoid lookup up all of the builtin types.
114 if (!decl_name.empty() && decl_name[0] == '$')
115 {
Sean Callananf76afff2011-10-28 23:38:38 +0000116 SetLookupsEnabled (true);
Greg Clayton8de27c72010-10-15 22:48:33 +0000117 }
118 else
119 {
120 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
121 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000122 }
Greg Clayton8de27c72010-10-15 22:48:33 +0000123
Greg Clayton8de27c72010-10-15 22:48:33 +0000124 ConstString const_decl_name(decl_name.c_str());
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000125
126 const char *uniqued_const_decl_name = const_decl_name.GetCString();
127 if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end())
128 {
129 // We are currently looking up this name...
130 return DeclContext::lookup_result();
131 }
132 m_active_lookups.insert(uniqued_const_decl_name);
Greg Claytona8b278a2010-11-15 01:34:18 +0000133// static uint32_t g_depth = 0;
134// ++g_depth;
135// printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000136 llvm::SmallVector<NamedDecl*, 4> name_decls;
137 NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx);
Sean Callananf76afff2011-10-28 23:38:38 +0000138 FindExternalVisibleDecls(name_search_context);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000139 DeclContext::lookup_result result (SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls));
Greg Claytona8b278a2010-11-15 01:34:18 +0000140// --g_depth;
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000141 m_active_lookups.erase (uniqued_const_decl_name);
142 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000143}
144
Greg Claytonb01000f2011-01-17 03:46:26 +0000145void
146ClangASTSource::CompleteType (TagDecl *tag_decl)
Sean Callananbb715f92011-10-29 02:28:18 +0000147{
148 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
149
150 if (log)
151 {
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000152 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 +0000153 log->Printf(" [CTD] Before:");
154 ASTDumper dumper((Decl*)tag_decl);
155 dumper.ToLog(log, " [CTD] ");
156 }
157
158 m_ast_importer->CompleteTagDecl (tag_decl);
159
160 if (log)
161 {
162 log->Printf(" [CTD] After:");
163 ASTDumper dumper((Decl*)tag_decl);
164 dumper.ToLog(log, " [CTD] ");
165 }
Greg Claytonb01000f2011-01-17 03:46:26 +0000166}
167
168void
Sean Callananbb715f92011-10-29 02:28:18 +0000169ClangASTSource::CompleteType (clang::ObjCInterfaceDecl *interface_decl)
170{
171 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
172
173 if (log)
174 {
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000175 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 +0000176 log->Printf(" [COID] Before:");
177 ASTDumper dumper((Decl*)interface_decl);
178 dumper.ToLog(log, " [COID] ");
179 }
180
181 m_ast_importer->CompleteObjCInterfaceDecl (interface_decl);
182
183 if (log)
184 {
185 log->Printf(" [COID] After:");
186 ASTDumper dumper((Decl*)interface_decl);
187 dumper.ToLog(log, " [COID] ");
188 }
Greg Claytonb01000f2011-01-17 03:46:26 +0000189}
190
Sean Callanan9b6898f2011-07-30 02:42:06 +0000191clang::ExternalLoadResult
Sean Callananbb715f92011-10-29 02:28:18 +0000192ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context,
193 bool (*predicate)(Decl::Kind),
194 llvm::SmallVectorImpl<Decl*> &decls)
195{
196 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
197
198 const Decl *context_decl = dyn_cast<Decl>(decl_context);
199
200 if (!context_decl)
201 return ELR_Failure;
202
203 static unsigned int invocation_id = 0;
204 unsigned int current_id = invocation_id++;
205
206 if (log)
207 {
208 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000209 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000210 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000211 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000212 context_named_decl->getNameAsString().c_str(),
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000213 context_decl->getDeclKindName(),
214 context_decl,
Sean Callananbb715f92011-10-29 02:28:18 +0000215 (predicate ? "non-null" : "null"));
216 else if(context_decl)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000217 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%sDecl*)%p with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000218 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000219 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000220 context_decl->getDeclKindName(),
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000221 context_decl,
Sean Callananbb715f92011-10-29 02:28:18 +0000222 (predicate ? "non-null" : "null"));
223 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000224 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000225 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000226 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000227 (predicate ? "non-null" : "null"));
228 }
229
230 Decl *original_decl = NULL;
231 ASTContext *original_ctx = NULL;
232
233 if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx))
234 return ELR_Failure;
235
236 if (log)
237 {
238 log->Printf(" FELD[%u] Original decl:", current_id);
239 ASTDumper(original_decl).ToLog(log, " ");
240 }
241
242 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
243 {
244 ExternalASTSource *external_source = original_ctx->getExternalSource();
245
246 if (external_source)
247 external_source->CompleteType (original_tag_decl);
248 }
249
250 DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl);
251
252 if (!original_decl_context)
253 return ELR_Failure;
254
255 for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
256 iter != original_decl_context->decls_end();
257 ++iter)
258 {
259 Decl *decl = *iter;
260
261 if (!predicate || predicate(decl->getKind()))
262 {
263 if (log)
264 {
265 ASTDumper ast_dumper(decl);
266 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
267 log->Printf(" FELD[%d] Adding [to %s] lexical decl %s", current_id, context_named_decl->getNameAsString().c_str(), ast_dumper.GetCString());
268 else
269 log->Printf(" FELD[%d] Adding lexical decl %s", current_id, ast_dumper.GetCString());
270 }
271
Sean Callanan4938bd62011-11-16 18:20:47 +0000272 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, original_ctx, decl);
Sean Callananbb715f92011-10-29 02:28:18 +0000273
274 decls.push_back(copied_decl);
275 }
276 }
277
278 return ELR_AlreadyLoaded;
Chris Lattner24943d22010-06-08 16:52:24 +0000279}
280
Sean Callanan9394b5a2011-10-29 19:50:43 +0000281void
282ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
283{
284 assert (m_ast_context);
285
286 const ConstString name(context.m_decl_name.getAsString().c_str());
287
288 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
289
290 static unsigned int invocation_id = 0;
291 unsigned int current_id = invocation_id++;
292
293 if (log)
294 {
295 if (!context.m_decl_context)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000296 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 +0000297 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000298 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 +0000299 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000300 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 +0000301 }
302
303 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
304
305 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
306 {
307 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
308
309 if (log && log->GetVerbose())
310 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
311 current_id,
312 namespace_map.get(),
313 (int)namespace_map->size());
314
315 if (!namespace_map)
316 return;
317
318 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
319 i != e;
320 ++i)
321 {
322 if (log)
323 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
324 current_id,
325 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
326 i->first->GetFileSpec().GetFilename().GetCString());
327
328 FindExternalVisibleDecls(context,
329 i->first,
330 i->second,
331 current_id);
332 }
333 }
Sean Callanand3812fa2011-11-15 21:50:18 +0000334 else if (isa<ObjCInterfaceDecl>(context.m_decl_context))
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000335 {
336 FindObjCPropertyDecls(context);
337 }
Sean Callanan9394b5a2011-10-29 19:50:43 +0000338 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
339 {
340 // we shouldn't be getting FindExternalVisibleDecls calls for these
341 return;
342 }
343 else
344 {
345 ClangNamespaceDecl namespace_decl;
346
347 if (log)
348 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
349
350 FindExternalVisibleDecls(context,
351 lldb::ModuleSP(),
352 namespace_decl,
353 current_id);
354 }
355
356 if (!context.m_namespace_map->empty())
357 {
358 if (log && log->GetVerbose())
359 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
360 current_id,
361 context.m_namespace_map.get(),
362 (int)context.m_namespace_map->size());
363
364 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
365
366 if (clang_namespace_decl)
367 clang_namespace_decl->setHasExternalVisibleStorage();
368 }
369}
370
371void
372ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
373 lldb::ModuleSP module_sp,
374 ClangNamespaceDecl &namespace_decl,
375 unsigned int current_id)
376{
377 assert (m_ast_context);
378
379 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
380
381 SymbolContextList sc_list;
382
383 const ConstString name(context.m_decl_name.getAsString().c_str());
384
385 const char *name_unique_cstr = name.GetCString();
386
387 if (name_unique_cstr == NULL)
388 return;
389
390 // The ClangASTSource is not responsible for finding $-names.
391 if (name_unique_cstr[0] == '$')
392 return;
393
394 if (module_sp && namespace_decl)
395 {
396 ClangNamespaceDecl found_namespace_decl;
397
398 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
399
400 if (symbol_vendor)
401 {
402 SymbolContext null_sc;
403
404 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
405
406 if (found_namespace_decl)
407 {
408 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
409
410 if (log)
411 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
412 current_id,
413 name.GetCString(),
414 module_sp->GetFileSpec().GetFilename().GetCString());
415 }
416 }
417 }
418 else
419 {
420 ModuleList &images = m_target->GetImages();
421
422 for (uint32_t i = 0, e = images.GetSize();
423 i != e;
424 ++i)
425 {
426 lldb::ModuleSP image = images.GetModuleAtIndex(i);
427
428 if (!image)
429 continue;
430
431 ClangNamespaceDecl found_namespace_decl;
432
433 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
434
435 if (!symbol_vendor)
436 continue;
437
438 SymbolContext null_sc;
439
440 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
441
442 if (found_namespace_decl)
443 {
444 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
445
446 if (log)
447 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
448 current_id,
449 name.GetCString(),
450 image->GetFileSpec().GetFilename().GetCString());
451 }
452 }
453 }
454
455 static ConstString id_name("id");
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000456 static ConstString Class_name("Class");
Sean Callanan9394b5a2011-10-29 19:50:43 +0000457
458 do
459 {
460 TypeList types;
461 SymbolContext null_sc;
462
463 if (module_sp && namespace_decl)
Sean Callanan673f3db2011-11-30 22:11:59 +0000464 {
Sean Callanan9394b5a2011-10-29 19:50:43 +0000465 module_sp->FindTypes(null_sc, name, &namespace_decl, true, 1, types);
Sean Callanan673f3db2011-11-30 22:11:59 +0000466 }
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000467 else if(name != id_name && name != Class_name)
Sean Callanan673f3db2011-11-30 22:11:59 +0000468 {
Sean Callanan9394b5a2011-10-29 19:50:43 +0000469 m_target->GetImages().FindTypes (null_sc, name, true, 1, types);
Sean Callanan673f3db2011-11-30 22:11:59 +0000470
471 if (!types.GetSize())
472 {
473 lldb::ProcessSP process = m_target->GetProcessSP();
474
475 if (process && process->GetObjCLanguageRuntime())
476 {
477 SymbolVendor *objc_symbol_vendor = process->GetObjCLanguageRuntime()->GetSymbolVendor();
478
479 objc_symbol_vendor->FindTypes(null_sc, name, NULL, true, 1, types);
480 }
481 }
482 }
Sean Callanan9394b5a2011-10-29 19:50:43 +0000483 else
Sean Callanan673f3db2011-11-30 22:11:59 +0000484 {
Sean Callanan9394b5a2011-10-29 19:50:43 +0000485 break;
Sean Callanan673f3db2011-11-30 22:11:59 +0000486 }
Sean Callanan9394b5a2011-10-29 19:50:43 +0000487
488 if (types.GetSize())
489 {
490 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
491
492 if (log)
493 {
494 const char *name_string = type_sp->GetName().GetCString();
495
496 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
497 current_id,
498 name.GetCString(),
499 (name_string ? name_string : "<anonymous>"));
500 }
501
502 void *copied_type = GuardedCopyType(m_ast_context, type_sp->GetClangAST(), type_sp->GetClangFullType());
503
504 context.AddTypeDecl(copied_type);
505 }
Sean Callanan673f3db2011-11-30 22:11:59 +0000506
Sean Callanan9394b5a2011-10-29 19:50:43 +0000507 } while(0);
508}
509
Sean Callanan9b714842011-11-09 19:33:21 +0000510void
511ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
512{
513 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
514
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000515 static unsigned int invocation_id = 0;
516 unsigned int current_id = invocation_id++;
517
Sean Callanan9b714842011-11-09 19:33:21 +0000518 const DeclarationName &decl_name(context.m_decl_name);
519 const DeclContext *decl_ctx(context.m_decl_context);
520
521 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
522
523 if (!interface_decl)
524 return;
525
526 StreamString ss;
527 if (decl_name.isObjCZeroArgSelector())
528 {
529 ss.Printf("%s", decl_name.getAsString().c_str());
530 }
531 else if (decl_name.isObjCOneArgSelector())
532 {
Sean Callanan1d9ffe22011-11-14 18:29:46 +0000533 ss.Printf("%s", decl_name.getAsString().c_str());
Sean Callanan9b714842011-11-09 19:33:21 +0000534 }
535 else
536 {
537 clang::Selector sel = decl_name.getObjCSelector();
538
539 for (unsigned i = 0, e = sel.getNumArgs();
540 i != e;
541 ++i)
542 {
543 llvm::StringRef r = sel.getNameForSlot(i);
544 ss.Printf("%s:", r.str().c_str());
545 }
546 }
547 ss.Flush();
548
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000549 ConstString selector_name(ss.GetData());
550
Sean Callanan9b714842011-11-09 19:33:21 +0000551 if (log)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000552 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
553 current_id,
554 m_ast_context,
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000555 interface_decl->getNameAsString().c_str(),
556 selector_name.AsCString());
557
558 SymbolContextList sc_list;
559
560 const bool include_symbols = false;
561 const bool append = false;
562
563 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, append, sc_list);
564
565 for (uint32_t i = 0, e = sc_list.GetSize();
566 i != e;
567 ++i)
568 {
569 SymbolContext sc;
570
571 if (!sc_list.GetContextAtIndex(i, sc))
572 continue;
573
574 if (!sc.function)
575 continue;
576
577 DeclContext *function_ctx = sc.function->GetClangDeclContext();
578
579 if (!function_ctx)
580 continue;
581
582 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
583
584 if (!method_decl)
585 continue;
586
587 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
588
589 if (!found_interface_decl)
590 continue;
591
592 if (found_interface_decl->getName() == interface_decl->getName())
593 {
Sean Callanan4938bd62011-11-16 18:20:47 +0000594 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000595
596 if (!copied_decl)
597 continue;
598
599 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
600
601 if (!copied_method_decl)
602 continue;
603
604 if (log)
605 {
606 ASTDumper dumper((Decl*)copied_method_decl);
607 log->Printf(" CAS::FOMD[%d] found %s", current_id, dumper.GetCString());
608 }
609
610 context.AddNamedDecl(copied_method_decl);
611 }
612 }
Sean Callanan9b714842011-11-09 19:33:21 +0000613}
614
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000615void
616ClangASTSource::FindObjCPropertyDecls (NameSearchContext &context)
617{
618 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
619
620 static unsigned int invocation_id = 0;
621 unsigned int current_id = invocation_id++;
622
623 const ObjCInterfaceDecl *iface_decl = cast<ObjCInterfaceDecl>(context.m_decl_context);
624 Decl *orig_decl;
625 ASTContext *orig_ast_ctx;
626
627 m_ast_importer->ResolveDeclOrigin(iface_decl, &orig_decl, &orig_ast_ctx);
628
629 if (!orig_decl)
630 return;
631
632 ObjCInterfaceDecl *orig_iface_decl = dyn_cast<ObjCInterfaceDecl>(orig_decl);
633
634 if (!orig_iface_decl)
635 return;
636
637 if (!ClangASTContext::GetCompleteDecl(orig_ast_ctx, orig_iface_decl))
638 return;
639
640 std::string property_name_str = context.m_decl_name.getAsString();
641 StringRef property_name(property_name_str.c_str());
642 ObjCPropertyDecl *property_decl = orig_iface_decl->FindPropertyDeclaration(&orig_ast_ctx->Idents.get(property_name));
643
644 if (log)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000645 log->Printf("ClangASTSource::FindObjCPropertyDecls[%d] on (ASTContext*)%p for property '%s.%s'",
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000646 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000647 m_ast_context,
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000648 iface_decl->getNameAsString().c_str(),
649 property_name_str.c_str());
650
651 if (!property_decl)
652 return;
653
Sean Callanan4938bd62011-11-16 18:20:47 +0000654 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, orig_ast_ctx, property_decl);
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000655
656 if (!copied_decl)
657 return;
658
659 ObjCPropertyDecl *copied_property_decl = dyn_cast<ObjCPropertyDecl>(copied_decl);
660
661 if (!copied_property_decl)
662 return;
663
664 if (log)
665 {
666 ASTDumper dumper((Decl*)copied_property_decl);
667 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
668 }
669
670 context.AddNamedDecl(copied_property_decl);
671}
672
Sean Callanan73b520f2011-10-29 01:58:46 +0000673void
674ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
675 const ConstString &name,
676 ClangASTImporter::NamespaceMapSP &parent_map) const
677{
678 static unsigned int invocation_id = 0;
679 unsigned int current_id = invocation_id++;
680
681 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
682
683 if (log)
684 {
685 if (parent_map && parent_map->size())
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000686 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +0000687 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000688 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +0000689 name.GetCString(),
690 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
691 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000692 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +0000693 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000694 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +0000695 name.GetCString());
696 }
697
698
699 if (parent_map)
700 {
701 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
702 i != e;
703 ++i)
704 {
705 ClangNamespaceDecl found_namespace_decl;
706
707 lldb::ModuleSP module_sp = i->first;
708 ClangNamespaceDecl module_parent_namespace_decl = i->second;
709
710 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
711
712 if (!symbol_vendor)
713 continue;
714
715 SymbolContext null_sc;
716
717 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
718
719 if (!found_namespace_decl)
720 continue;
721
722 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
723
724 if (log)
725 log->Printf(" CMN[%u] Found namespace %s in module %s",
726 current_id,
727 name.GetCString(),
728 module_sp->GetFileSpec().GetFilename().GetCString());
729 }
730 }
731 else
732 {
733 ModuleList &images = m_target->GetImages();
734 ClangNamespaceDecl null_namespace_decl;
735
736 for (uint32_t i = 0, e = images.GetSize();
737 i != e;
738 ++i)
739 {
740 lldb::ModuleSP image = images.GetModuleAtIndex(i);
741
742 if (!image)
743 continue;
744
745 ClangNamespaceDecl found_namespace_decl;
746
747 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
748
749 if (!symbol_vendor)
750 continue;
751
752 SymbolContext null_sc;
753
754 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
755
756 if (!found_namespace_decl)
757 continue;
758
759 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
760
761 if (log)
762 log->Printf(" CMN[%u] Found namespace %s in module %s",
763 current_id,
764 name.GetCString(),
765 image->GetFileSpec().GetFilename().GetCString());
766 }
767 }
768}
769
Sean Callananbb715f92011-10-29 02:28:18 +0000770NamespaceDecl *
771ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
772{
773 if (namespace_decls.empty())
774 return NULL;
775
776 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
777
778 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
779
Sean Callanan4938bd62011-11-16 18:20:47 +0000780 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
Sean Callananbb715f92011-10-29 02:28:18 +0000781
782 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
783
784 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
785
786 return dyn_cast<NamespaceDecl>(copied_decl);
787}
788
Sean Callanan9394b5a2011-10-29 19:50:43 +0000789void *
790ClangASTSource::GuardedCopyType (ASTContext *dest_context,
791 ASTContext *source_context,
792 void *clang_type)
793{
794 SetImportInProgress(true);
795
Sean Callanan4938bd62011-11-16 18:20:47 +0000796 QualType ret_qual_type = m_ast_importer->CopyType (m_ast_context, source_context, QualType::getFromOpaquePtr(clang_type));
Sean Callanan9394b5a2011-10-29 19:50:43 +0000797
798 void *ret = ret_qual_type.getAsOpaquePtr();
799
800 SetImportInProgress(false);
801
802 return ret;
803}
804
Greg Claytonb01000f2011-01-17 03:46:26 +0000805clang::NamedDecl *
806NameSearchContext::AddVarDecl(void *type)
807{
Greg Clayton8de27c72010-10-15 22:48:33 +0000808 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +0000809
810 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +0000811
Sean Callananf76afff2011-10-28 23:38:38 +0000812 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000813 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +0000814 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000815 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000816 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000817 QualType::getFromOpaquePtr(type),
818 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000819 SC_Static,
820 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +0000821 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000822
823 return Decl;
824}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000825
Greg Claytonb01000f2011-01-17 03:46:26 +0000826clang::NamedDecl *
827NameSearchContext::AddFunDecl (void *type)
828{
Sean Callananf76afff2011-10-28 23:38:38 +0000829 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000830 const_cast<DeclContext*>(m_decl_context),
831 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000832 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +0000833 m_decl_name.getAsIdentifierInfo(),
834 QualType::getFromOpaquePtr(type),
835 NULL,
836 SC_Static,
837 SC_Static,
838 false,
839 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000840
Sean Callananb291abe2010-08-12 23:45:38 +0000841 // We have to do more than just synthesize the FunctionDecl. We have to
842 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
843 // this, we raid the function's FunctionProtoType for types.
844
Greg Clayton8de27c72010-10-15 22:48:33 +0000845 QualType qual_type (QualType::getFromOpaquePtr(type));
846 const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000847
Greg Clayton8de27c72010-10-15 22:48:33 +0000848 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +0000849 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000850 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000851 unsigned ArgIndex;
852
Sean Callananc1535182011-10-07 23:18:13 +0000853 SmallVector<ParmVarDecl *, 5> parm_var_decls;
854
Sean Callanan8f0dc342010-06-22 23:46:24 +0000855 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
856 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000857 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000858
Sean Callananf76afff2011-10-28 23:38:38 +0000859 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callananc1535182011-10-07 23:18:13 +0000860 const_cast<DeclContext*>(m_decl_context),
861 SourceLocation(),
862 SourceLocation(),
863 NULL,
864 arg_qual_type,
865 NULL,
866 SC_Static,
867 SC_Static,
868 NULL));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000869 }
870
Sean Callananc1535182011-10-07 23:18:13 +0000871 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000872 }
873
Greg Clayton8de27c72010-10-15 22:48:33 +0000874 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000875
Greg Clayton8de27c72010-10-15 22:48:33 +0000876 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000877}
Sean Callanan0fc73582010-07-27 00:55:47 +0000878
Greg Claytonb01000f2011-01-17 03:46:26 +0000879clang::NamedDecl *
880NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +0000881{
Sean Callananad293092011-01-18 23:32:05 +0000882 FunctionProtoType::ExtProtoInfo proto_info;
883
884 proto_info.Variadic = true;
885
Sean Callananf76afff2011-10-28 23:38:38 +0000886 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
887 NULL, // argument types
888 0, // number of arguments
889 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +0000890
Sean Callanan0fc73582010-07-27 00:55:47 +0000891 return AddFunDecl(generic_function_type.getAsOpaquePtr());
892}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000893
Greg Claytonb01000f2011-01-17 03:46:26 +0000894clang::NamedDecl *
895NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000896{
Greg Claytona1aaaff2011-01-23 00:34:52 +0000897 if (type)
898 {
899 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000900
Sean Callanand5b3c352011-01-27 04:42:51 +0000901 if (const TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000902 {
903 TagDecl *tag_decl = tag_type->getDecl();
904
905 m_decls.push_back(tag_decl);
906
907 return tag_decl;
908 }
Sean Callanand5b3c352011-01-27 04:42:51 +0000909 else if (const ObjCObjectType *objc_object_type = dyn_cast<clang::ObjCObjectType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000910 {
911 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
912
913 m_decls.push_back((NamedDecl*)interface_decl);
914
915 return (NamedDecl*)interface_decl;
916 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000917 }
Greg Claytona1aaaff2011-01-23 00:34:52 +0000918 return NULL;
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000919}
Greg Claytone6d72ca2011-06-25 00:44:06 +0000920
921void
922NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
923{
924 for (clang::NamedDecl * const *decl_iterator = result.first;
925 decl_iterator != result.second;
926 ++decl_iterator)
927 m_decls.push_back (*decl_iterator);
928}
929
930void
931NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
932{
933 m_decls.push_back (decl);
934}