blob: 678504646a6d6fa6f43d7bcd8b372a77c9038970 [file] [log] [blame]
Greg Clayton1e591ce2010-07-16 18:28:27 +00001//===-- ClangASTSource.cpp ---------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chris Lattner24943d22010-06-08 16:52:24 +000010
Chris Lattner24943d22010-06-08 16:52:24 +000011#include "clang/AST/ASTContext.h"
Greg Claytonf4c7ae02010-10-15 03:36:13 +000012#include "lldb/Core/Log.h"
Greg Clayton6e0101c2011-09-17 06:21:20 +000013#include "lldb/Core/Module.h"
Sean Callanan73b520f2011-10-29 01:58:46 +000014#include "lldb/Core/ModuleList.h"
Sean Callananbb715f92011-10-29 02:28:18 +000015#include "lldb/Expression/ASTDumper.h"
Chris Lattner24943d22010-06-08 16:52:24 +000016#include "lldb/Expression/ClangASTSource.h"
17#include "lldb/Expression/ClangExpression.h"
Sean Callanan73b520f2011-10-29 01:58:46 +000018#include "lldb/Symbol/ClangNamespaceDecl.h"
19#include "lldb/Symbol/SymbolVendor.h"
20#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021
22using namespace clang;
23using namespace lldb_private;
24
Greg Claytonb01000f2011-01-17 03:46:26 +000025ClangASTSource::~ClangASTSource()
26{
Sean Callanan4938bd62011-11-16 18:20:47 +000027 m_ast_importer->PurgeMaps(m_ast_context);
Greg Claytonb01000f2011-01-17 03:46:26 +000028}
Chris Lattner24943d22010-06-08 16:52:24 +000029
Greg Claytonb01000f2011-01-17 03:46:26 +000030void
31ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer)
32{
Sean Callananf76afff2011-10-28 23:38:38 +000033 if (!m_ast_context)
34 return;
35
36 m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage();
37 m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage();
Chris Lattner24943d22010-06-08 16:52:24 +000038}
39
Chris Lattner24943d22010-06-08 16:52:24 +000040// The core lookup interface.
Greg Claytonb01000f2011-01-17 03:46:26 +000041DeclContext::lookup_result
42ClangASTSource::FindExternalVisibleDeclsByName
Greg Claytonf4c7ae02010-10-15 03:36:13 +000043(
44 const DeclContext *decl_ctx,
Greg Clayton8de27c72010-10-15 22:48:33 +000045 DeclarationName clang_decl_name
Greg Claytonf4c7ae02010-10-15 03:36:13 +000046)
47{
Sean Callananf76afff2011-10-28 23:38:38 +000048 if (!m_ast_context)
49 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
50
51 if (GetImportInProgress())
Greg Claytonb01000f2011-01-17 03:46:26 +000052 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
53
54 std::string decl_name (clang_decl_name.getAsString());
55
56// if (m_decl_map.DoingASTImport ())
57// return DeclContext::lookup_result();
58//
Greg Clayton8de27c72010-10-15 22:48:33 +000059 switch (clang_decl_name.getNameKind()) {
Chris Lattner24943d22010-06-08 16:52:24 +000060 // Normal identifiers.
61 case DeclarationName::Identifier:
Greg Clayton8de27c72010-10-15 22:48:33 +000062 if (clang_decl_name.getAsIdentifierInfo()->getBuiltinID() != 0)
63 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
64 break;
Chris Lattner24943d22010-06-08 16:52:24 +000065
66 // Operator names. Not important for now.
67 case DeclarationName::CXXOperatorName:
68 case DeclarationName::CXXLiteralOperatorName:
69 return DeclContext::lookup_result();
70
71 // Using directives found in this context.
72 // Tell Sema we didn't find any or we'll end up getting asked a *lot*.
73 case DeclarationName::CXXUsingDirective:
Greg Clayton8de27c72010-10-15 22:48:33 +000074 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
Chris Lattner24943d22010-06-08 16:52:24 +000075
Chris Lattner24943d22010-06-08 16:52:24 +000076 case DeclarationName::ObjCZeroArgSelector:
77 case DeclarationName::ObjCOneArgSelector:
78 case DeclarationName::ObjCMultiArgSelector:
Sean Callanan9b714842011-11-09 19:33:21 +000079 {
80 llvm::SmallVector<NamedDecl*, 1> method_decls;
Chris Lattner24943d22010-06-08 16:52:24 +000081
Sean Callanan9b714842011-11-09 19:33:21 +000082 NameSearchContext method_search_context (*this, method_decls, clang_decl_name, decl_ctx);
83
84 FindObjCMethodDecls(method_search_context);
85
86 return SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, method_decls);
87 }
Chris Lattner24943d22010-06-08 16:52:24 +000088 // These aren't possible in the global context.
89 case DeclarationName::CXXConstructorName:
90 case DeclarationName::CXXDestructorName:
91 case DeclarationName::CXXConversionFunctionName:
92 return DeclContext::lookup_result();
93 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +000094
Greg Claytonf4c7ae02010-10-15 03:36:13 +000095
Sean Callananf76afff2011-10-28 23:38:38 +000096 if (!GetLookupsEnabled())
Greg Clayton8de27c72010-10-15 22:48:33 +000097 {
98 // Wait until we see a '$' at the start of a name before we start doing
99 // any lookups so we can avoid lookup up all of the builtin types.
100 if (!decl_name.empty() && decl_name[0] == '$')
101 {
Sean Callananf76afff2011-10-28 23:38:38 +0000102 SetLookupsEnabled (true);
Greg Clayton8de27c72010-10-15 22:48:33 +0000103 }
104 else
105 {
106 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
107 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000108 }
Greg Clayton8de27c72010-10-15 22:48:33 +0000109
Greg Clayton8de27c72010-10-15 22:48:33 +0000110 ConstString const_decl_name(decl_name.c_str());
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000111
112 const char *uniqued_const_decl_name = const_decl_name.GetCString();
113 if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end())
114 {
115 // We are currently looking up this name...
116 return DeclContext::lookup_result();
117 }
118 m_active_lookups.insert(uniqued_const_decl_name);
Greg Claytona8b278a2010-11-15 01:34:18 +0000119// static uint32_t g_depth = 0;
120// ++g_depth;
121// printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000122 llvm::SmallVector<NamedDecl*, 4> name_decls;
123 NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx);
Sean Callananf76afff2011-10-28 23:38:38 +0000124 FindExternalVisibleDecls(name_search_context);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000125 DeclContext::lookup_result result (SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls));
Greg Claytona8b278a2010-11-15 01:34:18 +0000126// --g_depth;
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000127 m_active_lookups.erase (uniqued_const_decl_name);
128 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000129}
130
Greg Claytonb01000f2011-01-17 03:46:26 +0000131void
132ClangASTSource::CompleteType (TagDecl *tag_decl)
Sean Callananbb715f92011-10-29 02:28:18 +0000133{
134 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
135
136 if (log)
137 {
138 log->Printf(" [CompleteTagDecl] Completing a TagDecl named %s", tag_decl->getName().str().c_str());
139 log->Printf(" [CTD] Before:");
140 ASTDumper dumper((Decl*)tag_decl);
141 dumper.ToLog(log, " [CTD] ");
142 }
143
144 m_ast_importer->CompleteTagDecl (tag_decl);
145
146 if (log)
147 {
148 log->Printf(" [CTD] After:");
149 ASTDumper dumper((Decl*)tag_decl);
150 dumper.ToLog(log, " [CTD] ");
151 }
Greg Claytonb01000f2011-01-17 03:46:26 +0000152}
153
154void
Sean Callananbb715f92011-10-29 02:28:18 +0000155ClangASTSource::CompleteType (clang::ObjCInterfaceDecl *interface_decl)
156{
157 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
158
159 if (log)
160 {
161 log->Printf(" [CompleteObjCInterfaceDecl] Completing an ObjCInterfaceDecl named %s", interface_decl->getName().str().c_str());
162 log->Printf(" [COID] Before:");
163 ASTDumper dumper((Decl*)interface_decl);
164 dumper.ToLog(log, " [COID] ");
165 }
166
167 m_ast_importer->CompleteObjCInterfaceDecl (interface_decl);
168
169 if (log)
170 {
171 log->Printf(" [COID] After:");
172 ASTDumper dumper((Decl*)interface_decl);
173 dumper.ToLog(log, " [COID] ");
174 }
Greg Claytonb01000f2011-01-17 03:46:26 +0000175}
176
Sean Callanan9b6898f2011-07-30 02:42:06 +0000177clang::ExternalLoadResult
Sean Callananbb715f92011-10-29 02:28:18 +0000178ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context,
179 bool (*predicate)(Decl::Kind),
180 llvm::SmallVectorImpl<Decl*> &decls)
181{
182 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
183
184 const Decl *context_decl = dyn_cast<Decl>(decl_context);
185
186 if (!context_decl)
187 return ELR_Failure;
188
189 static unsigned int invocation_id = 0;
190 unsigned int current_id = invocation_id++;
191
192 if (log)
193 {
194 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
195 log->Printf("FindExternalLexicalDecls[%u] in '%s' (a %s) with %s predicate",
196 current_id,
197 context_named_decl->getNameAsString().c_str(),
198 context_decl->getDeclKindName(),
199 (predicate ? "non-null" : "null"));
200 else if(context_decl)
201 log->Printf("FindExternalLexicalDecls[%u] in a %s with %s predicate",
202 current_id,
203 context_decl->getDeclKindName(),
204 (predicate ? "non-null" : "null"));
205 else
206 log->Printf("FindExternalLexicalDecls[%u] in a NULL context with %s predicate",
207 current_id,
208 (predicate ? "non-null" : "null"));
209 }
210
211 Decl *original_decl = NULL;
212 ASTContext *original_ctx = NULL;
213
214 if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx))
215 return ELR_Failure;
216
217 if (log)
218 {
219 log->Printf(" FELD[%u] Original decl:", current_id);
220 ASTDumper(original_decl).ToLog(log, " ");
221 }
222
223 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
224 {
225 ExternalASTSource *external_source = original_ctx->getExternalSource();
226
227 if (external_source)
228 external_source->CompleteType (original_tag_decl);
229 }
230
231 DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl);
232
233 if (!original_decl_context)
234 return ELR_Failure;
235
236 for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
237 iter != original_decl_context->decls_end();
238 ++iter)
239 {
240 Decl *decl = *iter;
241
242 if (!predicate || predicate(decl->getKind()))
243 {
244 if (log)
245 {
246 ASTDumper ast_dumper(decl);
247 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
248 log->Printf(" FELD[%d] Adding [to %s] lexical decl %s", current_id, context_named_decl->getNameAsString().c_str(), ast_dumper.GetCString());
249 else
250 log->Printf(" FELD[%d] Adding lexical decl %s", current_id, ast_dumper.GetCString());
251 }
252
Sean Callanan4938bd62011-11-16 18:20:47 +0000253 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, original_ctx, decl);
Sean Callananbb715f92011-10-29 02:28:18 +0000254
255 decls.push_back(copied_decl);
256 }
257 }
258
259 return ELR_AlreadyLoaded;
Chris Lattner24943d22010-06-08 16:52:24 +0000260}
261
Sean Callanan9394b5a2011-10-29 19:50:43 +0000262void
263ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
264{
265 assert (m_ast_context);
266
267 const ConstString name(context.m_decl_name.getAsString().c_str());
268
269 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
270
271 static unsigned int invocation_id = 0;
272 unsigned int current_id = invocation_id++;
273
274 if (log)
275 {
276 if (!context.m_decl_context)
277 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] for '%s' in a NULL DeclContext", current_id, name.GetCString());
278 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
279 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] for '%s' in '%s'", current_id, name.GetCString(), context_named_decl->getNameAsString().c_str());
280 else
281 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] for '%s' in a '%s'", current_id, name.GetCString(), context.m_decl_context->getDeclKindName());
282 }
283
284 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
285
286 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
287 {
288 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
289
290 if (log && log->GetVerbose())
291 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
292 current_id,
293 namespace_map.get(),
294 (int)namespace_map->size());
295
296 if (!namespace_map)
297 return;
298
299 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
300 i != e;
301 ++i)
302 {
303 if (log)
304 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
305 current_id,
306 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
307 i->first->GetFileSpec().GetFilename().GetCString());
308
309 FindExternalVisibleDecls(context,
310 i->first,
311 i->second,
312 current_id);
313 }
314 }
Sean Callanand3812fa2011-11-15 21:50:18 +0000315 else if (isa<ObjCInterfaceDecl>(context.m_decl_context))
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000316 {
317 FindObjCPropertyDecls(context);
318 }
Sean Callanan9394b5a2011-10-29 19:50:43 +0000319 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
320 {
321 // we shouldn't be getting FindExternalVisibleDecls calls for these
322 return;
323 }
324 else
325 {
326 ClangNamespaceDecl namespace_decl;
327
328 if (log)
329 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
330
331 FindExternalVisibleDecls(context,
332 lldb::ModuleSP(),
333 namespace_decl,
334 current_id);
335 }
336
337 if (!context.m_namespace_map->empty())
338 {
339 if (log && log->GetVerbose())
340 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
341 current_id,
342 context.m_namespace_map.get(),
343 (int)context.m_namespace_map->size());
344
345 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
346
347 if (clang_namespace_decl)
348 clang_namespace_decl->setHasExternalVisibleStorage();
349 }
350}
351
352void
353ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
354 lldb::ModuleSP module_sp,
355 ClangNamespaceDecl &namespace_decl,
356 unsigned int current_id)
357{
358 assert (m_ast_context);
359
360 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
361
362 SymbolContextList sc_list;
363
364 const ConstString name(context.m_decl_name.getAsString().c_str());
365
366 const char *name_unique_cstr = name.GetCString();
367
368 if (name_unique_cstr == NULL)
369 return;
370
371 // The ClangASTSource is not responsible for finding $-names.
372 if (name_unique_cstr[0] == '$')
373 return;
374
375 if (module_sp && namespace_decl)
376 {
377 ClangNamespaceDecl found_namespace_decl;
378
379 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
380
381 if (symbol_vendor)
382 {
383 SymbolContext null_sc;
384
385 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
386
387 if (found_namespace_decl)
388 {
389 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
390
391 if (log)
392 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
393 current_id,
394 name.GetCString(),
395 module_sp->GetFileSpec().GetFilename().GetCString());
396 }
397 }
398 }
399 else
400 {
401 ModuleList &images = m_target->GetImages();
402
403 for (uint32_t i = 0, e = images.GetSize();
404 i != e;
405 ++i)
406 {
407 lldb::ModuleSP image = images.GetModuleAtIndex(i);
408
409 if (!image)
410 continue;
411
412 ClangNamespaceDecl found_namespace_decl;
413
414 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
415
416 if (!symbol_vendor)
417 continue;
418
419 SymbolContext null_sc;
420
421 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
422
423 if (found_namespace_decl)
424 {
425 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
426
427 if (log)
428 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
429 current_id,
430 name.GetCString(),
431 image->GetFileSpec().GetFilename().GetCString());
432 }
433 }
434 }
435
436 static ConstString id_name("id");
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000437 static ConstString Class_name("Class");
Sean Callanan9394b5a2011-10-29 19:50:43 +0000438
439 do
440 {
441 TypeList types;
442 SymbolContext null_sc;
443
444 if (module_sp && namespace_decl)
445 module_sp->FindTypes(null_sc, name, &namespace_decl, true, 1, types);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000446 else if(name != id_name && name != Class_name)
Sean Callanan9394b5a2011-10-29 19:50:43 +0000447 m_target->GetImages().FindTypes (null_sc, name, true, 1, types);
448 else
449 break;
450
451 if (types.GetSize())
452 {
453 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
454
455 if (log)
456 {
457 const char *name_string = type_sp->GetName().GetCString();
458
459 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
460 current_id,
461 name.GetCString(),
462 (name_string ? name_string : "<anonymous>"));
463 }
464
465 void *copied_type = GuardedCopyType(m_ast_context, type_sp->GetClangAST(), type_sp->GetClangFullType());
466
467 context.AddTypeDecl(copied_type);
468 }
469 } while(0);
470}
471
Sean Callanan9b714842011-11-09 19:33:21 +0000472void
473ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
474{
475 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
476
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000477 static unsigned int invocation_id = 0;
478 unsigned int current_id = invocation_id++;
479
Sean Callanan9b714842011-11-09 19:33:21 +0000480 const DeclarationName &decl_name(context.m_decl_name);
481 const DeclContext *decl_ctx(context.m_decl_context);
482
483 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
484
485 if (!interface_decl)
486 return;
487
488 StreamString ss;
489 if (decl_name.isObjCZeroArgSelector())
490 {
491 ss.Printf("%s", decl_name.getAsString().c_str());
492 }
493 else if (decl_name.isObjCOneArgSelector())
494 {
Sean Callanan1d9ffe22011-11-14 18:29:46 +0000495 ss.Printf("%s", decl_name.getAsString().c_str());
Sean Callanan9b714842011-11-09 19:33:21 +0000496 }
497 else
498 {
499 clang::Selector sel = decl_name.getObjCSelector();
500
501 for (unsigned i = 0, e = sel.getNumArgs();
502 i != e;
503 ++i)
504 {
505 llvm::StringRef r = sel.getNameForSlot(i);
506 ss.Printf("%s:", r.str().c_str());
507 }
508 }
509 ss.Flush();
510
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000511 ConstString selector_name(ss.GetData());
512
Sean Callanan9b714842011-11-09 19:33:21 +0000513 if (log)
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000514 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] for selector [%s %s]",
515 current_id,
516 interface_decl->getNameAsString().c_str(),
517 selector_name.AsCString());
518
519 SymbolContextList sc_list;
520
521 const bool include_symbols = false;
522 const bool append = false;
523
524 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, append, sc_list);
525
526 for (uint32_t i = 0, e = sc_list.GetSize();
527 i != e;
528 ++i)
529 {
530 SymbolContext sc;
531
532 if (!sc_list.GetContextAtIndex(i, sc))
533 continue;
534
535 if (!sc.function)
536 continue;
537
538 DeclContext *function_ctx = sc.function->GetClangDeclContext();
539
540 if (!function_ctx)
541 continue;
542
543 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
544
545 if (!method_decl)
546 continue;
547
548 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
549
550 if (!found_interface_decl)
551 continue;
552
553 if (found_interface_decl->getName() == interface_decl->getName())
554 {
Sean Callanan4938bd62011-11-16 18:20:47 +0000555 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000556
557 if (!copied_decl)
558 continue;
559
560 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
561
562 if (!copied_method_decl)
563 continue;
564
565 if (log)
566 {
567 ASTDumper dumper((Decl*)copied_method_decl);
568 log->Printf(" CAS::FOMD[%d] found %s", current_id, dumper.GetCString());
569 }
570
571 context.AddNamedDecl(copied_method_decl);
572 }
573 }
Sean Callanan9b714842011-11-09 19:33:21 +0000574}
575
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000576void
577ClangASTSource::FindObjCPropertyDecls (NameSearchContext &context)
578{
579 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
580
581 static unsigned int invocation_id = 0;
582 unsigned int current_id = invocation_id++;
583
584 const ObjCInterfaceDecl *iface_decl = cast<ObjCInterfaceDecl>(context.m_decl_context);
585 Decl *orig_decl;
586 ASTContext *orig_ast_ctx;
587
588 m_ast_importer->ResolveDeclOrigin(iface_decl, &orig_decl, &orig_ast_ctx);
589
590 if (!orig_decl)
591 return;
592
593 ObjCInterfaceDecl *orig_iface_decl = dyn_cast<ObjCInterfaceDecl>(orig_decl);
594
595 if (!orig_iface_decl)
596 return;
597
598 if (!ClangASTContext::GetCompleteDecl(orig_ast_ctx, orig_iface_decl))
599 return;
600
601 std::string property_name_str = context.m_decl_name.getAsString();
602 StringRef property_name(property_name_str.c_str());
603 ObjCPropertyDecl *property_decl = orig_iface_decl->FindPropertyDeclaration(&orig_ast_ctx->Idents.get(property_name));
604
605 if (log)
606 log->Printf("ClangASTSource::FindObjCPropertyDecls[%d] for property '%s.%s'",
607 current_id,
608 iface_decl->getNameAsString().c_str(),
609 property_name_str.c_str());
610
611 if (!property_decl)
612 return;
613
Sean Callanan4938bd62011-11-16 18:20:47 +0000614 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, orig_ast_ctx, property_decl);
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000615
616 if (!copied_decl)
617 return;
618
619 ObjCPropertyDecl *copied_property_decl = dyn_cast<ObjCPropertyDecl>(copied_decl);
620
621 if (!copied_property_decl)
622 return;
623
624 if (log)
625 {
626 ASTDumper dumper((Decl*)copied_property_decl);
627 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
628 }
629
630 context.AddNamedDecl(copied_property_decl);
631}
632
Sean Callanan73b520f2011-10-29 01:58:46 +0000633void
634ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
635 const ConstString &name,
636 ClangASTImporter::NamespaceMapSP &parent_map) const
637{
638 static unsigned int invocation_id = 0;
639 unsigned int current_id = invocation_id++;
640
641 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
642
643 if (log)
644 {
645 if (parent_map && parent_map->size())
646 log->Printf("CompleteNamespaceMap[%u] Searching for namespace %s in namespace %s",
647 current_id,
648 name.GetCString(),
649 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
650 else
651 log->Printf("CompleteNamespaceMap[%u] Searching for namespace %s",
652 current_id,
653 name.GetCString());
654 }
655
656
657 if (parent_map)
658 {
659 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
660 i != e;
661 ++i)
662 {
663 ClangNamespaceDecl found_namespace_decl;
664
665 lldb::ModuleSP module_sp = i->first;
666 ClangNamespaceDecl module_parent_namespace_decl = i->second;
667
668 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
669
670 if (!symbol_vendor)
671 continue;
672
673 SymbolContext null_sc;
674
675 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
676
677 if (!found_namespace_decl)
678 continue;
679
680 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
681
682 if (log)
683 log->Printf(" CMN[%u] Found namespace %s in module %s",
684 current_id,
685 name.GetCString(),
686 module_sp->GetFileSpec().GetFilename().GetCString());
687 }
688 }
689 else
690 {
691 ModuleList &images = m_target->GetImages();
692 ClangNamespaceDecl null_namespace_decl;
693
694 for (uint32_t i = 0, e = images.GetSize();
695 i != e;
696 ++i)
697 {
698 lldb::ModuleSP image = images.GetModuleAtIndex(i);
699
700 if (!image)
701 continue;
702
703 ClangNamespaceDecl found_namespace_decl;
704
705 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
706
707 if (!symbol_vendor)
708 continue;
709
710 SymbolContext null_sc;
711
712 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
713
714 if (!found_namespace_decl)
715 continue;
716
717 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
718
719 if (log)
720 log->Printf(" CMN[%u] Found namespace %s in module %s",
721 current_id,
722 name.GetCString(),
723 image->GetFileSpec().GetFilename().GetCString());
724 }
725 }
726}
727
Sean Callananbb715f92011-10-29 02:28:18 +0000728NamespaceDecl *
729ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
730{
731 if (namespace_decls.empty())
732 return NULL;
733
734 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
735
736 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
737
Sean Callanan4938bd62011-11-16 18:20:47 +0000738 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
Sean Callananbb715f92011-10-29 02:28:18 +0000739
740 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
741
742 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
743
744 return dyn_cast<NamespaceDecl>(copied_decl);
745}
746
Sean Callanan9394b5a2011-10-29 19:50:43 +0000747void *
748ClangASTSource::GuardedCopyType (ASTContext *dest_context,
749 ASTContext *source_context,
750 void *clang_type)
751{
752 SetImportInProgress(true);
753
Sean Callanan4938bd62011-11-16 18:20:47 +0000754 QualType ret_qual_type = m_ast_importer->CopyType (m_ast_context, source_context, QualType::getFromOpaquePtr(clang_type));
Sean Callanan9394b5a2011-10-29 19:50:43 +0000755
756 void *ret = ret_qual_type.getAsOpaquePtr();
757
758 SetImportInProgress(false);
759
760 return ret;
761}
762
Greg Claytonb01000f2011-01-17 03:46:26 +0000763clang::NamedDecl *
764NameSearchContext::AddVarDecl(void *type)
765{
Greg Clayton8de27c72010-10-15 22:48:33 +0000766 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +0000767
768 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +0000769
Sean Callananf76afff2011-10-28 23:38:38 +0000770 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000771 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +0000772 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000773 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000774 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000775 QualType::getFromOpaquePtr(type),
776 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000777 SC_Static,
778 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +0000779 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000780
781 return Decl;
782}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000783
Greg Claytonb01000f2011-01-17 03:46:26 +0000784clang::NamedDecl *
785NameSearchContext::AddFunDecl (void *type)
786{
Sean Callananf76afff2011-10-28 23:38:38 +0000787 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000788 const_cast<DeclContext*>(m_decl_context),
789 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000790 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +0000791 m_decl_name.getAsIdentifierInfo(),
792 QualType::getFromOpaquePtr(type),
793 NULL,
794 SC_Static,
795 SC_Static,
796 false,
797 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000798
Sean Callananb291abe2010-08-12 23:45:38 +0000799 // We have to do more than just synthesize the FunctionDecl. We have to
800 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
801 // this, we raid the function's FunctionProtoType for types.
802
Greg Clayton8de27c72010-10-15 22:48:33 +0000803 QualType qual_type (QualType::getFromOpaquePtr(type));
804 const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000805
Greg Clayton8de27c72010-10-15 22:48:33 +0000806 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +0000807 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000808 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000809 unsigned ArgIndex;
810
Sean Callananc1535182011-10-07 23:18:13 +0000811 SmallVector<ParmVarDecl *, 5> parm_var_decls;
812
Sean Callanan8f0dc342010-06-22 23:46:24 +0000813 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
814 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000815 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000816
Sean Callananf76afff2011-10-28 23:38:38 +0000817 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callananc1535182011-10-07 23:18:13 +0000818 const_cast<DeclContext*>(m_decl_context),
819 SourceLocation(),
820 SourceLocation(),
821 NULL,
822 arg_qual_type,
823 NULL,
824 SC_Static,
825 SC_Static,
826 NULL));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000827 }
828
Sean Callananc1535182011-10-07 23:18:13 +0000829 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000830 }
831
Greg Clayton8de27c72010-10-15 22:48:33 +0000832 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000833
Greg Clayton8de27c72010-10-15 22:48:33 +0000834 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000835}
Sean Callanan0fc73582010-07-27 00:55:47 +0000836
Greg Claytonb01000f2011-01-17 03:46:26 +0000837clang::NamedDecl *
838NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +0000839{
Sean Callananad293092011-01-18 23:32:05 +0000840 FunctionProtoType::ExtProtoInfo proto_info;
841
842 proto_info.Variadic = true;
843
Sean Callananf76afff2011-10-28 23:38:38 +0000844 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
845 NULL, // argument types
846 0, // number of arguments
847 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +0000848
Sean Callanan0fc73582010-07-27 00:55:47 +0000849 return AddFunDecl(generic_function_type.getAsOpaquePtr());
850}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000851
Greg Claytonb01000f2011-01-17 03:46:26 +0000852clang::NamedDecl *
853NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000854{
Greg Claytona1aaaff2011-01-23 00:34:52 +0000855 if (type)
856 {
857 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000858
Sean Callanand5b3c352011-01-27 04:42:51 +0000859 if (const TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000860 {
861 TagDecl *tag_decl = tag_type->getDecl();
862
863 m_decls.push_back(tag_decl);
864
865 return tag_decl;
866 }
Sean Callanand5b3c352011-01-27 04:42:51 +0000867 else if (const ObjCObjectType *objc_object_type = dyn_cast<clang::ObjCObjectType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000868 {
869 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
870
871 m_decls.push_back((NamedDecl*)interface_decl);
872
873 return (NamedDecl*)interface_decl;
874 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000875 }
Greg Claytona1aaaff2011-01-23 00:34:52 +0000876 return NULL;
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000877}
Greg Claytone6d72ca2011-06-25 00:44:06 +0000878
879void
880NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
881{
882 for (clang::NamedDecl * const *decl_iterator = result.first;
883 decl_iterator != result.second;
884 ++decl_iterator)
885 m_decls.push_back (*decl_iterator);
886}
887
888void
889NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
890{
891 m_decls.push_back (decl);
892}