blob: 4c6ba7eedc7191e03cdad86247075bd80b851f4f [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 {
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000138 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 +0000139 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 {
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000161 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 +0000162 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))
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000195 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000196 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000197 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000198 context_named_decl->getNameAsString().c_str(),
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000199 context_decl->getDeclKindName(),
200 context_decl,
Sean Callananbb715f92011-10-29 02:28:18 +0000201 (predicate ? "non-null" : "null"));
202 else if(context_decl)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000203 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%sDecl*)%p with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000204 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000205 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000206 context_decl->getDeclKindName(),
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000207 context_decl,
Sean Callananbb715f92011-10-29 02:28:18 +0000208 (predicate ? "non-null" : "null"));
209 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000210 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000211 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000212 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000213 (predicate ? "non-null" : "null"));
214 }
215
216 Decl *original_decl = NULL;
217 ASTContext *original_ctx = NULL;
218
219 if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx))
220 return ELR_Failure;
221
222 if (log)
223 {
224 log->Printf(" FELD[%u] Original decl:", current_id);
225 ASTDumper(original_decl).ToLog(log, " ");
226 }
227
228 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
229 {
230 ExternalASTSource *external_source = original_ctx->getExternalSource();
231
232 if (external_source)
233 external_source->CompleteType (original_tag_decl);
234 }
235
236 DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl);
237
238 if (!original_decl_context)
239 return ELR_Failure;
240
241 for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
242 iter != original_decl_context->decls_end();
243 ++iter)
244 {
245 Decl *decl = *iter;
246
247 if (!predicate || predicate(decl->getKind()))
248 {
249 if (log)
250 {
251 ASTDumper ast_dumper(decl);
252 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
253 log->Printf(" FELD[%d] Adding [to %s] lexical decl %s", current_id, context_named_decl->getNameAsString().c_str(), ast_dumper.GetCString());
254 else
255 log->Printf(" FELD[%d] Adding lexical decl %s", current_id, ast_dumper.GetCString());
256 }
257
Sean Callanan4938bd62011-11-16 18:20:47 +0000258 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, original_ctx, decl);
Sean Callananbb715f92011-10-29 02:28:18 +0000259
260 decls.push_back(copied_decl);
261 }
262 }
263
264 return ELR_AlreadyLoaded;
Chris Lattner24943d22010-06-08 16:52:24 +0000265}
266
Sean Callanan9394b5a2011-10-29 19:50:43 +0000267void
268ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
269{
270 assert (m_ast_context);
271
272 const ConstString name(context.m_decl_name.getAsString().c_str());
273
274 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
275
276 static unsigned int invocation_id = 0;
277 unsigned int current_id = invocation_id++;
278
279 if (log)
280 {
281 if (!context.m_decl_context)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000282 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 +0000283 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000284 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 +0000285 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000286 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 +0000287 }
288
289 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
290
291 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
292 {
293 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
294
295 if (log && log->GetVerbose())
296 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
297 current_id,
298 namespace_map.get(),
299 (int)namespace_map->size());
300
301 if (!namespace_map)
302 return;
303
304 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
305 i != e;
306 ++i)
307 {
308 if (log)
309 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
310 current_id,
311 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
312 i->first->GetFileSpec().GetFilename().GetCString());
313
314 FindExternalVisibleDecls(context,
315 i->first,
316 i->second,
317 current_id);
318 }
319 }
Sean Callanand3812fa2011-11-15 21:50:18 +0000320 else if (isa<ObjCInterfaceDecl>(context.m_decl_context))
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000321 {
322 FindObjCPropertyDecls(context);
323 }
Sean Callanan9394b5a2011-10-29 19:50:43 +0000324 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
325 {
326 // we shouldn't be getting FindExternalVisibleDecls calls for these
327 return;
328 }
329 else
330 {
331 ClangNamespaceDecl namespace_decl;
332
333 if (log)
334 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
335
336 FindExternalVisibleDecls(context,
337 lldb::ModuleSP(),
338 namespace_decl,
339 current_id);
340 }
341
342 if (!context.m_namespace_map->empty())
343 {
344 if (log && log->GetVerbose())
345 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
346 current_id,
347 context.m_namespace_map.get(),
348 (int)context.m_namespace_map->size());
349
350 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
351
352 if (clang_namespace_decl)
353 clang_namespace_decl->setHasExternalVisibleStorage();
354 }
355}
356
357void
358ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
359 lldb::ModuleSP module_sp,
360 ClangNamespaceDecl &namespace_decl,
361 unsigned int current_id)
362{
363 assert (m_ast_context);
364
365 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
366
367 SymbolContextList sc_list;
368
369 const ConstString name(context.m_decl_name.getAsString().c_str());
370
371 const char *name_unique_cstr = name.GetCString();
372
373 if (name_unique_cstr == NULL)
374 return;
375
376 // The ClangASTSource is not responsible for finding $-names.
377 if (name_unique_cstr[0] == '$')
378 return;
379
380 if (module_sp && namespace_decl)
381 {
382 ClangNamespaceDecl found_namespace_decl;
383
384 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
385
386 if (symbol_vendor)
387 {
388 SymbolContext null_sc;
389
390 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
391
392 if (found_namespace_decl)
393 {
394 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
395
396 if (log)
397 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
398 current_id,
399 name.GetCString(),
400 module_sp->GetFileSpec().GetFilename().GetCString());
401 }
402 }
403 }
404 else
405 {
406 ModuleList &images = m_target->GetImages();
407
408 for (uint32_t i = 0, e = images.GetSize();
409 i != e;
410 ++i)
411 {
412 lldb::ModuleSP image = images.GetModuleAtIndex(i);
413
414 if (!image)
415 continue;
416
417 ClangNamespaceDecl found_namespace_decl;
418
419 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
420
421 if (!symbol_vendor)
422 continue;
423
424 SymbolContext null_sc;
425
426 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
427
428 if (found_namespace_decl)
429 {
430 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
431
432 if (log)
433 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
434 current_id,
435 name.GetCString(),
436 image->GetFileSpec().GetFilename().GetCString());
437 }
438 }
439 }
440
441 static ConstString id_name("id");
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000442 static ConstString Class_name("Class");
Sean Callanan9394b5a2011-10-29 19:50:43 +0000443
444 do
445 {
446 TypeList types;
447 SymbolContext null_sc;
448
449 if (module_sp && namespace_decl)
450 module_sp->FindTypes(null_sc, name, &namespace_decl, true, 1, types);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000451 else if(name != id_name && name != Class_name)
Sean Callanan9394b5a2011-10-29 19:50:43 +0000452 m_target->GetImages().FindTypes (null_sc, name, true, 1, types);
453 else
454 break;
455
456 if (types.GetSize())
457 {
458 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
459
460 if (log)
461 {
462 const char *name_string = type_sp->GetName().GetCString();
463
464 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
465 current_id,
466 name.GetCString(),
467 (name_string ? name_string : "<anonymous>"));
468 }
469
470 void *copied_type = GuardedCopyType(m_ast_context, type_sp->GetClangAST(), type_sp->GetClangFullType());
471
472 context.AddTypeDecl(copied_type);
473 }
474 } while(0);
475}
476
Sean Callanan9b714842011-11-09 19:33:21 +0000477void
478ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
479{
480 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
481
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000482 static unsigned int invocation_id = 0;
483 unsigned int current_id = invocation_id++;
484
Sean Callanan9b714842011-11-09 19:33:21 +0000485 const DeclarationName &decl_name(context.m_decl_name);
486 const DeclContext *decl_ctx(context.m_decl_context);
487
488 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
489
490 if (!interface_decl)
491 return;
492
493 StreamString ss;
494 if (decl_name.isObjCZeroArgSelector())
495 {
496 ss.Printf("%s", decl_name.getAsString().c_str());
497 }
498 else if (decl_name.isObjCOneArgSelector())
499 {
Sean Callanan1d9ffe22011-11-14 18:29:46 +0000500 ss.Printf("%s", decl_name.getAsString().c_str());
Sean Callanan9b714842011-11-09 19:33:21 +0000501 }
502 else
503 {
504 clang::Selector sel = decl_name.getObjCSelector();
505
506 for (unsigned i = 0, e = sel.getNumArgs();
507 i != e;
508 ++i)
509 {
510 llvm::StringRef r = sel.getNameForSlot(i);
511 ss.Printf("%s:", r.str().c_str());
512 }
513 }
514 ss.Flush();
515
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000516 ConstString selector_name(ss.GetData());
517
Sean Callanan9b714842011-11-09 19:33:21 +0000518 if (log)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000519 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
520 current_id,
521 m_ast_context,
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000522 interface_decl->getNameAsString().c_str(),
523 selector_name.AsCString());
524
525 SymbolContextList sc_list;
526
527 const bool include_symbols = false;
528 const bool append = false;
529
530 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, append, sc_list);
531
532 for (uint32_t i = 0, e = sc_list.GetSize();
533 i != e;
534 ++i)
535 {
536 SymbolContext sc;
537
538 if (!sc_list.GetContextAtIndex(i, sc))
539 continue;
540
541 if (!sc.function)
542 continue;
543
544 DeclContext *function_ctx = sc.function->GetClangDeclContext();
545
546 if (!function_ctx)
547 continue;
548
549 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
550
551 if (!method_decl)
552 continue;
553
554 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
555
556 if (!found_interface_decl)
557 continue;
558
559 if (found_interface_decl->getName() == interface_decl->getName())
560 {
Sean Callanan4938bd62011-11-16 18:20:47 +0000561 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000562
563 if (!copied_decl)
564 continue;
565
566 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
567
568 if (!copied_method_decl)
569 continue;
570
571 if (log)
572 {
573 ASTDumper dumper((Decl*)copied_method_decl);
574 log->Printf(" CAS::FOMD[%d] found %s", current_id, dumper.GetCString());
575 }
576
577 context.AddNamedDecl(copied_method_decl);
578 }
579 }
Sean Callanan9b714842011-11-09 19:33:21 +0000580}
581
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000582void
583ClangASTSource::FindObjCPropertyDecls (NameSearchContext &context)
584{
585 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
586
587 static unsigned int invocation_id = 0;
588 unsigned int current_id = invocation_id++;
589
590 const ObjCInterfaceDecl *iface_decl = cast<ObjCInterfaceDecl>(context.m_decl_context);
591 Decl *orig_decl;
592 ASTContext *orig_ast_ctx;
593
594 m_ast_importer->ResolveDeclOrigin(iface_decl, &orig_decl, &orig_ast_ctx);
595
596 if (!orig_decl)
597 return;
598
599 ObjCInterfaceDecl *orig_iface_decl = dyn_cast<ObjCInterfaceDecl>(orig_decl);
600
601 if (!orig_iface_decl)
602 return;
603
604 if (!ClangASTContext::GetCompleteDecl(orig_ast_ctx, orig_iface_decl))
605 return;
606
607 std::string property_name_str = context.m_decl_name.getAsString();
608 StringRef property_name(property_name_str.c_str());
609 ObjCPropertyDecl *property_decl = orig_iface_decl->FindPropertyDeclaration(&orig_ast_ctx->Idents.get(property_name));
610
611 if (log)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000612 log->Printf("ClangASTSource::FindObjCPropertyDecls[%d] on (ASTContext*)%p for property '%s.%s'",
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000613 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000614 m_ast_context,
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000615 iface_decl->getNameAsString().c_str(),
616 property_name_str.c_str());
617
618 if (!property_decl)
619 return;
620
Sean Callanan4938bd62011-11-16 18:20:47 +0000621 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, orig_ast_ctx, property_decl);
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000622
623 if (!copied_decl)
624 return;
625
626 ObjCPropertyDecl *copied_property_decl = dyn_cast<ObjCPropertyDecl>(copied_decl);
627
628 if (!copied_property_decl)
629 return;
630
631 if (log)
632 {
633 ASTDumper dumper((Decl*)copied_property_decl);
634 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
635 }
636
637 context.AddNamedDecl(copied_property_decl);
638}
639
Sean Callanan73b520f2011-10-29 01:58:46 +0000640void
641ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
642 const ConstString &name,
643 ClangASTImporter::NamespaceMapSP &parent_map) const
644{
645 static unsigned int invocation_id = 0;
646 unsigned int current_id = invocation_id++;
647
648 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
649
650 if (log)
651 {
652 if (parent_map && parent_map->size())
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000653 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +0000654 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000655 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +0000656 name.GetCString(),
657 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
658 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000659 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +0000660 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000661 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +0000662 name.GetCString());
663 }
664
665
666 if (parent_map)
667 {
668 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
669 i != e;
670 ++i)
671 {
672 ClangNamespaceDecl found_namespace_decl;
673
674 lldb::ModuleSP module_sp = i->first;
675 ClangNamespaceDecl module_parent_namespace_decl = i->second;
676
677 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
678
679 if (!symbol_vendor)
680 continue;
681
682 SymbolContext null_sc;
683
684 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
685
686 if (!found_namespace_decl)
687 continue;
688
689 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
690
691 if (log)
692 log->Printf(" CMN[%u] Found namespace %s in module %s",
693 current_id,
694 name.GetCString(),
695 module_sp->GetFileSpec().GetFilename().GetCString());
696 }
697 }
698 else
699 {
700 ModuleList &images = m_target->GetImages();
701 ClangNamespaceDecl null_namespace_decl;
702
703 for (uint32_t i = 0, e = images.GetSize();
704 i != e;
705 ++i)
706 {
707 lldb::ModuleSP image = images.GetModuleAtIndex(i);
708
709 if (!image)
710 continue;
711
712 ClangNamespaceDecl found_namespace_decl;
713
714 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
715
716 if (!symbol_vendor)
717 continue;
718
719 SymbolContext null_sc;
720
721 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
722
723 if (!found_namespace_decl)
724 continue;
725
726 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
727
728 if (log)
729 log->Printf(" CMN[%u] Found namespace %s in module %s",
730 current_id,
731 name.GetCString(),
732 image->GetFileSpec().GetFilename().GetCString());
733 }
734 }
735}
736
Sean Callananbb715f92011-10-29 02:28:18 +0000737NamespaceDecl *
738ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
739{
740 if (namespace_decls.empty())
741 return NULL;
742
743 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
744
745 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
746
Sean Callanan4938bd62011-11-16 18:20:47 +0000747 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
Sean Callananbb715f92011-10-29 02:28:18 +0000748
749 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
750
751 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
752
753 return dyn_cast<NamespaceDecl>(copied_decl);
754}
755
Sean Callanan9394b5a2011-10-29 19:50:43 +0000756void *
757ClangASTSource::GuardedCopyType (ASTContext *dest_context,
758 ASTContext *source_context,
759 void *clang_type)
760{
761 SetImportInProgress(true);
762
Sean Callanan4938bd62011-11-16 18:20:47 +0000763 QualType ret_qual_type = m_ast_importer->CopyType (m_ast_context, source_context, QualType::getFromOpaquePtr(clang_type));
Sean Callanan9394b5a2011-10-29 19:50:43 +0000764
765 void *ret = ret_qual_type.getAsOpaquePtr();
766
767 SetImportInProgress(false);
768
769 return ret;
770}
771
Greg Claytonb01000f2011-01-17 03:46:26 +0000772clang::NamedDecl *
773NameSearchContext::AddVarDecl(void *type)
774{
Greg Clayton8de27c72010-10-15 22:48:33 +0000775 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +0000776
777 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +0000778
Sean Callananf76afff2011-10-28 23:38:38 +0000779 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000780 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +0000781 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000782 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000783 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000784 QualType::getFromOpaquePtr(type),
785 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000786 SC_Static,
787 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +0000788 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000789
790 return Decl;
791}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000792
Greg Claytonb01000f2011-01-17 03:46:26 +0000793clang::NamedDecl *
794NameSearchContext::AddFunDecl (void *type)
795{
Sean Callananf76afff2011-10-28 23:38:38 +0000796 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000797 const_cast<DeclContext*>(m_decl_context),
798 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000799 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +0000800 m_decl_name.getAsIdentifierInfo(),
801 QualType::getFromOpaquePtr(type),
802 NULL,
803 SC_Static,
804 SC_Static,
805 false,
806 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000807
Sean Callananb291abe2010-08-12 23:45:38 +0000808 // We have to do more than just synthesize the FunctionDecl. We have to
809 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
810 // this, we raid the function's FunctionProtoType for types.
811
Greg Clayton8de27c72010-10-15 22:48:33 +0000812 QualType qual_type (QualType::getFromOpaquePtr(type));
813 const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000814
Greg Clayton8de27c72010-10-15 22:48:33 +0000815 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +0000816 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000817 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000818 unsigned ArgIndex;
819
Sean Callananc1535182011-10-07 23:18:13 +0000820 SmallVector<ParmVarDecl *, 5> parm_var_decls;
821
Sean Callanan8f0dc342010-06-22 23:46:24 +0000822 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
823 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000824 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000825
Sean Callananf76afff2011-10-28 23:38:38 +0000826 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callananc1535182011-10-07 23:18:13 +0000827 const_cast<DeclContext*>(m_decl_context),
828 SourceLocation(),
829 SourceLocation(),
830 NULL,
831 arg_qual_type,
832 NULL,
833 SC_Static,
834 SC_Static,
835 NULL));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000836 }
837
Sean Callananc1535182011-10-07 23:18:13 +0000838 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000839 }
840
Greg Clayton8de27c72010-10-15 22:48:33 +0000841 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000842
Greg Clayton8de27c72010-10-15 22:48:33 +0000843 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000844}
Sean Callanan0fc73582010-07-27 00:55:47 +0000845
Greg Claytonb01000f2011-01-17 03:46:26 +0000846clang::NamedDecl *
847NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +0000848{
Sean Callananad293092011-01-18 23:32:05 +0000849 FunctionProtoType::ExtProtoInfo proto_info;
850
851 proto_info.Variadic = true;
852
Sean Callananf76afff2011-10-28 23:38:38 +0000853 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
854 NULL, // argument types
855 0, // number of arguments
856 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +0000857
Sean Callanan0fc73582010-07-27 00:55:47 +0000858 return AddFunDecl(generic_function_type.getAsOpaquePtr());
859}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000860
Greg Claytonb01000f2011-01-17 03:46:26 +0000861clang::NamedDecl *
862NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000863{
Greg Claytona1aaaff2011-01-23 00:34:52 +0000864 if (type)
865 {
866 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000867
Sean Callanand5b3c352011-01-27 04:42:51 +0000868 if (const TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000869 {
870 TagDecl *tag_decl = tag_type->getDecl();
871
872 m_decls.push_back(tag_decl);
873
874 return tag_decl;
875 }
Sean Callanand5b3c352011-01-27 04:42:51 +0000876 else if (const ObjCObjectType *objc_object_type = dyn_cast<clang::ObjCObjectType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000877 {
878 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
879
880 m_decls.push_back((NamedDecl*)interface_decl);
881
882 return (NamedDecl*)interface_decl;
883 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000884 }
Greg Claytona1aaaff2011-01-23 00:34:52 +0000885 return NULL;
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000886}
Greg Claytone6d72ca2011-06-25 00:44:06 +0000887
888void
889NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
890{
891 for (clang::NamedDecl * const *decl_iterator = result.first;
892 decl_iterator != result.second;
893 ++decl_iterator)
894 m_decls.push_back (*decl_iterator);
895}
896
897void
898NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
899{
900 m_decls.push_back (decl);
901}