blob: 7a3bda13012a80a1f1bd3f894b8090273b5815ac [file] [log] [blame]
Greg Claytonea3b0ae2010-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 Lattner30fdc8d2010-06-08 16:52:24 +000010
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011#include "clang/AST/ASTContext.h"
Sean Callanan5b26f272012-02-04 08:49:35 +000012#include "clang/AST/RecordLayout.h"
Greg Claytonee4b5dd2010-10-15 03:36:13 +000013#include "lldb/Core/Log.h"
Greg Clayton747bcb02011-09-17 06:21:20 +000014#include "lldb/Core/Module.h"
Sean Callanan1ee44b72011-10-29 01:58:46 +000015#include "lldb/Core/ModuleList.h"
Sean Callananba0aca72011-10-29 02:28:18 +000016#include "lldb/Expression/ASTDumper.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Expression/ClangASTSource.h"
18#include "lldb/Expression/ClangExpression.h"
Sean Callanan1ee44b72011-10-29 01:58:46 +000019#include "lldb/Symbol/ClangNamespaceDecl.h"
Greg Clayton1f746072012-08-29 21:13:06 +000020#include "lldb/Symbol/Function.h"
Sean Callanan1ee44b72011-10-29 01:58:46 +000021#include "lldb/Symbol/SymbolVendor.h"
Sean Callanan09ab4b72011-11-30 22:11:59 +000022#include "lldb/Target/ObjCLanguageRuntime.h"
Sean Callanan1ee44b72011-10-29 01:58:46 +000023#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024
25using namespace clang;
26using namespace lldb_private;
27
Greg Clayton6beaaa62011-01-17 03:46:26 +000028ClangASTSource::~ClangASTSource()
29{
Sean Callanan99732312011-11-29 00:42:02 +000030 m_ast_importer->ForgetDestination(m_ast_context);
31
Johnny Chen60e2c6a2011-11-30 23:18:53 +000032 // We are in the process of destruction, don't create clang ast context on demand
33 // by passing false to Target::GetScratchClangASTContext(create_on_demand).
34 ClangASTContext *scratch_clang_ast_context = m_target->GetScratchClangASTContext(false);
Sean Callanan99732312011-11-29 00:42:02 +000035
36 if (!scratch_clang_ast_context)
37 return;
38
39 clang::ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
40
41 if (!scratch_ast_context)
42 return;
43
44 if (m_ast_context != scratch_ast_context)
45 m_ast_importer->ForgetSource(scratch_ast_context, m_ast_context);
Greg Clayton6beaaa62011-01-17 03:46:26 +000046}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047
Greg Clayton6beaaa62011-01-17 03:46:26 +000048void
49ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer)
50{
Sean Callananeddeb3b2011-10-28 23:38:38 +000051 if (!m_ast_context)
52 return;
53
54 m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage();
55 m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056}
57
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058// The core lookup interface.
Sean Callananeeffea42013-02-12 08:01:13 +000059bool
Greg Clayton6beaaa62011-01-17 03:46:26 +000060ClangASTSource::FindExternalVisibleDeclsByName
Greg Claytonee4b5dd2010-10-15 03:36:13 +000061(
62 const DeclContext *decl_ctx,
Greg Clayton7b462cc2010-10-15 22:48:33 +000063 DeclarationName clang_decl_name
Greg Claytonee4b5dd2010-10-15 03:36:13 +000064)
65{
Sean Callananeddeb3b2011-10-28 23:38:38 +000066 if (!m_ast_context)
Sean Callananeeffea42013-02-12 08:01:13 +000067 {
68 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
69 return false;
70 }
Sean Callananeddeb3b2011-10-28 23:38:38 +000071
72 if (GetImportInProgress())
Sean Callananeeffea42013-02-12 08:01:13 +000073 {
74 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
75 return false;
76 }
77
Greg Clayton6beaaa62011-01-17 03:46:26 +000078 std::string decl_name (clang_decl_name.getAsString());
79
80// if (m_decl_map.DoingASTImport ())
81// return DeclContext::lookup_result();
82//
Greg Clayton7b462cc2010-10-15 22:48:33 +000083 switch (clang_decl_name.getNameKind()) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084 // Normal identifiers.
85 case DeclarationName::Identifier:
Sean Callanane8dea982012-04-25 17:46:01 +000086 {
87 clang::IdentifierInfo *identifier_info = clang_decl_name.getAsIdentifierInfo();
88
89 if (!identifier_info ||
90 identifier_info->getBuiltinID() != 0)
91 {
Sean Callananeeffea42013-02-12 08:01:13 +000092 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
93 return false;
Sean Callanane8dea982012-04-25 17:46:01 +000094 }
95 }
Greg Clayton7b462cc2010-10-15 22:48:33 +000096 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097
98 // Operator names. Not important for now.
99 case DeclarationName::CXXOperatorName:
100 case DeclarationName::CXXLiteralOperatorName:
Sean Callananeeffea42013-02-12 08:01:13 +0000101 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
102 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103
104 // Using directives found in this context.
105 // Tell Sema we didn't find any or we'll end up getting asked a *lot*.
106 case DeclarationName::CXXUsingDirective:
Sean Callananeeffea42013-02-12 08:01:13 +0000107 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
108 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110 case DeclarationName::ObjCZeroArgSelector:
111 case DeclarationName::ObjCOneArgSelector:
112 case DeclarationName::ObjCMultiArgSelector:
Sean Callanan0730e9c2011-11-09 19:33:21 +0000113 {
114 llvm::SmallVector<NamedDecl*, 1> method_decls;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115
Sean Callanan0730e9c2011-11-09 19:33:21 +0000116 NameSearchContext method_search_context (*this, method_decls, clang_decl_name, decl_ctx);
117
118 FindObjCMethodDecls(method_search_context);
119
Sean Callananeeffea42013-02-12 08:01:13 +0000120 SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, method_decls);
121 return (method_decls.size() > 0);
Sean Callanan0730e9c2011-11-09 19:33:21 +0000122 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123 // These aren't possible in the global context.
124 case DeclarationName::CXXConstructorName:
125 case DeclarationName::CXXDestructorName:
126 case DeclarationName::CXXConversionFunctionName:
Sean Callananeeffea42013-02-12 08:01:13 +0000127 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
128 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129 }
Greg Claytonee4b5dd2010-10-15 03:36:13 +0000130
Greg Claytonee4b5dd2010-10-15 03:36:13 +0000131
Sean Callananeddeb3b2011-10-28 23:38:38 +0000132 if (!GetLookupsEnabled())
Greg Clayton7b462cc2010-10-15 22:48:33 +0000133 {
134 // Wait until we see a '$' at the start of a name before we start doing
135 // any lookups so we can avoid lookup up all of the builtin types.
136 if (!decl_name.empty() && decl_name[0] == '$')
137 {
Sean Callananeddeb3b2011-10-28 23:38:38 +0000138 SetLookupsEnabled (true);
Greg Clayton7b462cc2010-10-15 22:48:33 +0000139 }
140 else
141 {
Sean Callananeeffea42013-02-12 08:01:13 +0000142 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
143 return false;
Greg Clayton7b462cc2010-10-15 22:48:33 +0000144 }
Greg Claytonee4b5dd2010-10-15 03:36:13 +0000145 }
Greg Clayton7b462cc2010-10-15 22:48:33 +0000146
Greg Clayton7b462cc2010-10-15 22:48:33 +0000147 ConstString const_decl_name(decl_name.c_str());
Greg Clayton580c5da2010-11-13 04:18:24 +0000148
149 const char *uniqued_const_decl_name = const_decl_name.GetCString();
150 if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end())
151 {
152 // We are currently looking up this name...
Sean Callananeeffea42013-02-12 08:01:13 +0000153 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
154 return false;
Greg Clayton580c5da2010-11-13 04:18:24 +0000155 }
156 m_active_lookups.insert(uniqued_const_decl_name);
Greg Clayton471da242010-11-15 01:34:18 +0000157// static uint32_t g_depth = 0;
158// ++g_depth;
159// printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name);
Greg Clayton580c5da2010-11-13 04:18:24 +0000160 llvm::SmallVector<NamedDecl*, 4> name_decls;
161 NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx);
Sean Callananeddeb3b2011-10-28 23:38:38 +0000162 FindExternalVisibleDecls(name_search_context);
Sean Callananeeffea42013-02-12 08:01:13 +0000163 SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls);
Greg Clayton471da242010-11-15 01:34:18 +0000164// --g_depth;
Greg Clayton580c5da2010-11-13 04:18:24 +0000165 m_active_lookups.erase (uniqued_const_decl_name);
Sean Callananeeffea42013-02-12 08:01:13 +0000166 return (name_decls.size() != 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167}
168
Greg Clayton6beaaa62011-01-17 03:46:26 +0000169void
170ClangASTSource::CompleteType (TagDecl *tag_decl)
Sean Callananba0aca72011-10-29 02:28:18 +0000171{
172 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
173
Sean Callanan12014a02011-12-08 23:45:45 +0000174 static unsigned int invocation_id = 0;
175 unsigned int current_id = invocation_id++;
176
Sean Callananba0aca72011-10-29 02:28:18 +0000177 if (log)
178 {
Sean Callanana6e61a72012-01-13 22:05:55 +0000179 log->Printf(" CompleteTagDecl[%u] on (ASTContext*)%p Completing (TagDecl*)%p named %s",
180 current_id,
Sean Callanan12014a02011-12-08 23:45:45 +0000181 m_ast_context,
Sean Callanana6e61a72012-01-13 22:05:55 +0000182 tag_decl,
Sean Callanan12014a02011-12-08 23:45:45 +0000183 tag_decl->getName().str().c_str());
184
185 log->Printf(" CTD[%u] Before:", current_id);
Sean Callananba0aca72011-10-29 02:28:18 +0000186 ASTDumper dumper((Decl*)tag_decl);
187 dumper.ToLog(log, " [CTD] ");
188 }
189
Sean Callanan12014a02011-12-08 23:45:45 +0000190 if (!m_ast_importer->CompleteTagDecl (tag_decl))
191 {
192 // We couldn't complete the type. Maybe there's a definition
193 // somewhere else that can be completed.
194
195 if (log)
196 log->Printf(" CTD[%u] Type could not be completed in the module in which it was first found.", current_id);
197
198 bool found = false;
199
200 DeclContext *decl_ctx = tag_decl->getDeclContext();
201
202 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(decl_ctx))
203 {
204 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
205
206 if (log && log->GetVerbose())
207 log->Printf(" CTD[%u] Inspecting namespace map %p (%d entries)",
208 current_id,
209 namespace_map.get(),
210 (int)namespace_map->size());
211
212 if (!namespace_map)
213 return;
214
215 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
216 i != e && !found;
217 ++i)
218 {
219 if (log)
220 log->Printf(" CTD[%u] Searching namespace %s in module %s",
221 current_id,
222 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
223 i->first->GetFileSpec().GetFilename().GetCString());
224
225 TypeList types;
226
227 SymbolContext null_sc;
228 ConstString name(tag_decl->getName().str().c_str());
229
Greg Clayton84db9102012-03-26 23:03:23 +0000230 i->first->FindTypesInNamespace(null_sc, name, &i->second, UINT32_MAX, types);
Sean Callanan12014a02011-12-08 23:45:45 +0000231
232 for (uint32_t ti = 0, te = types.GetSize();
233 ti != te && !found;
234 ++ti)
235 {
236 lldb::TypeSP type = types.GetTypeAtIndex(ti);
237
238 if (!type)
239 continue;
240
241 lldb::clang_type_t opaque_type = type->GetClangFullType();
242
243 if (!opaque_type)
244 continue;
245
Sean Callananfc4f2fb2011-12-14 01:13:04 +0000246 const TagType *tag_type = QualType::getFromOpaquePtr(opaque_type)->getAs<TagType>();
Sean Callanan12014a02011-12-08 23:45:45 +0000247
248 if (!tag_type)
249 continue;
250
251 TagDecl *candidate_tag_decl = const_cast<TagDecl*>(tag_type->getDecl());
252
253 if (m_ast_importer->CompleteTagDeclWithOrigin (tag_decl, candidate_tag_decl))
254 found = true;
255 }
256 }
257 }
258 else
259 {
260 TypeList types;
261
262 SymbolContext null_sc;
263 ConstString name(tag_decl->getName().str().c_str());
264 ClangNamespaceDecl namespace_decl;
265
Enrico Granata17598482012-11-08 02:22:02 +0000266 const ModuleList &module_list = m_target->GetImages();
Sean Callanan12014a02011-12-08 23:45:45 +0000267
Greg Clayton84db9102012-03-26 23:03:23 +0000268 bool exact_match = false;
Greg Clayton29399a22012-04-06 17:41:13 +0000269 module_list.FindTypes (null_sc, name, exact_match, UINT32_MAX, types);
Sean Callanan12014a02011-12-08 23:45:45 +0000270
271 for (uint32_t ti = 0, te = types.GetSize();
272 ti != te && !found;
273 ++ti)
274 {
275 lldb::TypeSP type = types.GetTypeAtIndex(ti);
276
277 if (!type)
278 continue;
279
280 lldb::clang_type_t opaque_type = type->GetClangFullType();
281
282 if (!opaque_type)
283 continue;
284
Sean Callananfc4f2fb2011-12-14 01:13:04 +0000285 const TagType *tag_type = QualType::getFromOpaquePtr(opaque_type)->getAs<TagType>();
Sean Callanan12014a02011-12-08 23:45:45 +0000286
287 if (!tag_type)
288 continue;
289
290 TagDecl *candidate_tag_decl = const_cast<TagDecl*>(tag_type->getDecl());
291
292 if (m_ast_importer->CompleteTagDeclWithOrigin (tag_decl, candidate_tag_decl))
293 found = true;
294 }
295 }
296 }
Sean Callananba0aca72011-10-29 02:28:18 +0000297
298 if (log)
299 {
300 log->Printf(" [CTD] After:");
301 ASTDumper dumper((Decl*)tag_decl);
302 dumper.ToLog(log, " [CTD] ");
303 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000304}
305
306void
Sean Callananba0aca72011-10-29 02:28:18 +0000307ClangASTSource::CompleteType (clang::ObjCInterfaceDecl *interface_decl)
308{
309 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
310
311 if (log)
312 {
Sean Callanan00f43622011-11-18 03:28:09 +0000313 log->Printf(" [CompleteObjCInterfaceDecl] on (ASTContext*)%p Completing an ObjCInterfaceDecl named %s", m_ast_context, interface_decl->getName().str().c_str());
Sean Callananba0aca72011-10-29 02:28:18 +0000314 log->Printf(" [COID] Before:");
315 ASTDumper dumper((Decl*)interface_decl);
316 dumper.ToLog(log, " [COID] ");
317 }
318
319 m_ast_importer->CompleteObjCInterfaceDecl (interface_decl);
320
321 if (log)
322 {
323 log->Printf(" [COID] After:");
324 ASTDumper dumper((Decl*)interface_decl);
325 dumper.ToLog(log, " [COID] ");
326 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000327}
328
Sean Callanan2cb5e522012-09-20 23:21:16 +0000329clang::ObjCInterfaceDecl *
330ClangASTSource::GetCompleteObjCInterface (clang::ObjCInterfaceDecl *interface_decl)
331{
332 lldb::ProcessSP process(m_target->GetProcessSP());
333
334 if (!process)
335 return NULL;
336
337 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
338
339 if (!language_runtime)
340 return NULL;
341
342 ConstString class_name(interface_decl->getNameAsString().c_str());
343
344 lldb::TypeSP complete_type_sp(language_runtime->LookupInCompleteClassCache(class_name));
345
346 if (!complete_type_sp)
347 return NULL;
348
349 TypeFromUser complete_type = TypeFromUser(complete_type_sp->GetClangFullType(), complete_type_sp->GetClangAST());
350 lldb::clang_type_t complete_opaque_type = complete_type.GetOpaqueQualType();
351
352 if (!complete_opaque_type)
353 return NULL;
354
355 const clang::Type *complete_clang_type = QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr();
356 const ObjCInterfaceType *complete_interface_type = dyn_cast<ObjCInterfaceType>(complete_clang_type);
357
358 if (!complete_interface_type)
359 return NULL;
360
361 ObjCInterfaceDecl *complete_iface_decl(complete_interface_type->getDecl());
362
363 return complete_iface_decl;
364}
365
Sean Callanancc427fa2011-07-30 02:42:06 +0000366clang::ExternalLoadResult
Sean Callanan2cb5e522012-09-20 23:21:16 +0000367ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context,
Sean Callananba0aca72011-10-29 02:28:18 +0000368 bool (*predicate)(Decl::Kind),
369 llvm::SmallVectorImpl<Decl*> &decls)
370{
371 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
372
373 const Decl *context_decl = dyn_cast<Decl>(decl_context);
374
375 if (!context_decl)
376 return ELR_Failure;
377
378 static unsigned int invocation_id = 0;
379 unsigned int current_id = invocation_id++;
380
381 if (log)
382 {
383 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
Sean Callanan00f43622011-11-18 03:28:09 +0000384 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p with %s predicate",
Sean Callananba0aca72011-10-29 02:28:18 +0000385 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +0000386 m_ast_context,
Sean Callananba0aca72011-10-29 02:28:18 +0000387 context_named_decl->getNameAsString().c_str(),
Sean Callanan00f43622011-11-18 03:28:09 +0000388 context_decl->getDeclKindName(),
389 context_decl,
Sean Callananba0aca72011-10-29 02:28:18 +0000390 (predicate ? "non-null" : "null"));
391 else if(context_decl)
Sean Callanan00f43622011-11-18 03:28:09 +0000392 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%sDecl*)%p with %s predicate",
Sean Callananba0aca72011-10-29 02:28:18 +0000393 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +0000394 m_ast_context,
Sean Callananba0aca72011-10-29 02:28:18 +0000395 context_decl->getDeclKindName(),
Sean Callanan00f43622011-11-18 03:28:09 +0000396 context_decl,
Sean Callananba0aca72011-10-29 02:28:18 +0000397 (predicate ? "non-null" : "null"));
398 else
Sean Callanan00f43622011-11-18 03:28:09 +0000399 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context with %s predicate",
Sean Callananba0aca72011-10-29 02:28:18 +0000400 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +0000401 m_ast_context,
Sean Callananba0aca72011-10-29 02:28:18 +0000402 (predicate ? "non-null" : "null"));
403 }
404
405 Decl *original_decl = NULL;
406 ASTContext *original_ctx = NULL;
407
408 if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx))
409 return ELR_Failure;
410
411 if (log)
412 {
Sean Callanana6e61a72012-01-13 22:05:55 +0000413 log->Printf(" FELD[%u] Original decl (Decl*)%p:", current_id, original_decl);
Sean Callananba0aca72011-10-29 02:28:18 +0000414 ASTDumper(original_decl).ToLog(log, " ");
415 }
416
Sean Callanan2cb5e522012-09-20 23:21:16 +0000417 if (ObjCInterfaceDecl *original_iface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl))
418 {
419 ObjCInterfaceDecl *complete_iface_decl = GetCompleteObjCInterface(original_iface_decl);
420
421 if (complete_iface_decl && (complete_iface_decl != original_iface_decl))
422 {
423 original_decl = complete_iface_decl;
424 original_ctx = &complete_iface_decl->getASTContext();
425
426 m_ast_importer->SetDeclOrigin(context_decl, original_iface_decl);
427 }
428 }
429
Sean Callananba0aca72011-10-29 02:28:18 +0000430 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
431 {
432 ExternalASTSource *external_source = original_ctx->getExternalSource();
433
434 if (external_source)
435 external_source->CompleteType (original_tag_decl);
436 }
437
Sean Callanan12014a02011-12-08 23:45:45 +0000438 const DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl);
Sean Callananba0aca72011-10-29 02:28:18 +0000439
440 if (!original_decl_context)
441 return ELR_Failure;
442
443 for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
444 iter != original_decl_context->decls_end();
445 ++iter)
446 {
447 Decl *decl = *iter;
448
449 if (!predicate || predicate(decl->getKind()))
450 {
451 if (log)
452 {
453 ASTDumper ast_dumper(decl);
454 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
Sean Callanan3d654b32012-09-24 22:25:51 +0000455 log->Printf(" FELD[%d] Adding [to %sDecl %s] lexical %sDecl %s", current_id, context_named_decl->getDeclKindName(), context_named_decl->getNameAsString().c_str(), decl->getDeclKindName(), ast_dumper.GetCString());
Sean Callananba0aca72011-10-29 02:28:18 +0000456 else
Sean Callanan3d654b32012-09-24 22:25:51 +0000457 log->Printf(" FELD[%d] Adding lexical %sDecl %s", current_id, decl->getDeclKindName(), ast_dumper.GetCString());
Sean Callananba0aca72011-10-29 02:28:18 +0000458 }
459
Sean Callanan686b2312011-11-16 18:20:47 +0000460 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, original_ctx, decl);
Sean Callananba0aca72011-10-29 02:28:18 +0000461
Sean Callanan61b33f72012-03-20 21:11:12 +0000462 if (!copied_decl)
463 continue;
464
Sean Callanancf128622012-03-15 01:53:17 +0000465 if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl))
466 {
467 QualType copied_field_type = copied_field->getType();
468
469 if (const TagType *copied_field_tag_type = copied_field_type->getAs<TagType>())
470 m_ast_importer->CompleteTagDecl(copied_field_tag_type->getDecl());
471 if (const ObjCObjectType *copied_field_object_type = copied_field_type->getAs<ObjCObjectType>())
472 if (ObjCInterfaceDecl *copied_field_objc_interface_decl = copied_field_object_type->getInterface())
473 m_ast_importer->CompleteObjCInterfaceDecl(copied_field_objc_interface_decl);
474 }
475
Sean Callananba0aca72011-10-29 02:28:18 +0000476 decls.push_back(copied_decl);
477 }
478 }
479
480 return ELR_AlreadyLoaded;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481}
482
Sean Callananfb3e4302011-10-29 19:50:43 +0000483void
484ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
485{
486 assert (m_ast_context);
487
488 const ConstString name(context.m_decl_name.getAsString().c_str());
489
490 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
491
492 static unsigned int invocation_id = 0;
493 unsigned int current_id = invocation_id++;
494
495 if (log)
496 {
497 if (!context.m_decl_context)
Sean Callanana6e61a72012-01-13 22:05:55 +0000498 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in a NULL DeclContext", current_id, m_ast_context, name.GetCString());
Sean Callananfb3e4302011-10-29 19:50:43 +0000499 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
Sean Callanan00f43622011-11-18 03:28:09 +0000500 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 Callananfb3e4302011-10-29 19:50:43 +0000501 else
Sean Callanan00f43622011-11-18 03:28:09 +0000502 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 Callananfb3e4302011-10-29 19:50:43 +0000503 }
504
505 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
506
507 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
508 {
509 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
510
511 if (log && log->GetVerbose())
512 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
513 current_id,
514 namespace_map.get(),
515 (int)namespace_map->size());
516
517 if (!namespace_map)
518 return;
519
520 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
521 i != e;
522 ++i)
523 {
524 if (log)
525 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
526 current_id,
527 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
528 i->first->GetFileSpec().GetFilename().GetCString());
529
530 FindExternalVisibleDecls(context,
531 i->first,
532 i->second,
533 current_id);
534 }
535 }
Sean Callanan100d74e2011-11-15 21:50:18 +0000536 else if (isa<ObjCInterfaceDecl>(context.m_decl_context))
Sean Callanand5c17ed2011-11-15 02:11:17 +0000537 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000538 FindObjCPropertyAndIvarDecls(context);
Sean Callanand5c17ed2011-11-15 02:11:17 +0000539 }
Sean Callananfb3e4302011-10-29 19:50:43 +0000540 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
541 {
542 // we shouldn't be getting FindExternalVisibleDecls calls for these
543 return;
544 }
545 else
546 {
547 ClangNamespaceDecl namespace_decl;
548
549 if (log)
550 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
551
552 FindExternalVisibleDecls(context,
553 lldb::ModuleSP(),
554 namespace_decl,
555 current_id);
556 }
557
558 if (!context.m_namespace_map->empty())
559 {
560 if (log && log->GetVerbose())
561 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
562 current_id,
563 context.m_namespace_map.get(),
564 (int)context.m_namespace_map->size());
565
566 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
567
568 if (clang_namespace_decl)
569 clang_namespace_decl->setHasExternalVisibleStorage();
570 }
571}
572
573void
574ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
575 lldb::ModuleSP module_sp,
576 ClangNamespaceDecl &namespace_decl,
577 unsigned int current_id)
578{
579 assert (m_ast_context);
580
581 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
582
583 SymbolContextList sc_list;
584
585 const ConstString name(context.m_decl_name.getAsString().c_str());
586
587 const char *name_unique_cstr = name.GetCString();
588
Sean Callanan5b26f272012-02-04 08:49:35 +0000589 static ConstString id_name("id");
590 static ConstString Class_name("Class");
591
592 if (name == id_name || name == Class_name)
593 return;
594
Sean Callananfb3e4302011-10-29 19:50:43 +0000595 if (name_unique_cstr == NULL)
596 return;
597
598 // The ClangASTSource is not responsible for finding $-names.
599 if (name_unique_cstr[0] == '$')
600 return;
601
602 if (module_sp && namespace_decl)
603 {
604 ClangNamespaceDecl found_namespace_decl;
605
606 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
607
608 if (symbol_vendor)
609 {
610 SymbolContext null_sc;
611
612 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
613
614 if (found_namespace_decl)
615 {
616 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
617
618 if (log)
619 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
620 current_id,
621 name.GetCString(),
622 module_sp->GetFileSpec().GetFilename().GetCString());
623 }
624 }
625 }
626 else
627 {
Enrico Granata17598482012-11-08 02:22:02 +0000628 const ModuleList &target_images = m_target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +0000629 Mutex::Locker modules_locker (target_images.GetMutex());
Sean Callananfb3e4302011-10-29 19:50:43 +0000630
Greg Claytonc7bece562013-01-25 18:06:21 +0000631 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i)
Sean Callananfb3e4302011-10-29 19:50:43 +0000632 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +0000633 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
Sean Callananfb3e4302011-10-29 19:50:43 +0000634
635 if (!image)
636 continue;
637
638 ClangNamespaceDecl found_namespace_decl;
639
640 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
641
642 if (!symbol_vendor)
643 continue;
644
645 SymbolContext null_sc;
646
647 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
648
649 if (found_namespace_decl)
650 {
651 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
652
653 if (log)
654 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
655 current_id,
656 name.GetCString(),
657 image->GetFileSpec().GetFilename().GetCString());
658 }
659 }
660 }
661
Sean Callananfb3e4302011-10-29 19:50:43 +0000662 do
663 {
664 TypeList types;
665 SymbolContext null_sc;
Greg Clayton84db9102012-03-26 23:03:23 +0000666 const bool exact_match = false;
Sean Callananbfb7c682011-12-19 19:38:39 +0000667
Sean Callananfb3e4302011-10-29 19:50:43 +0000668 if (module_sp && namespace_decl)
Greg Clayton84db9102012-03-26 23:03:23 +0000669 module_sp->FindTypesInNamespace(null_sc, name, &namespace_decl, 1, types);
Sean Callananbfb7c682011-12-19 19:38:39 +0000670 else
Greg Clayton29399a22012-04-06 17:41:13 +0000671 m_target->GetImages().FindTypes(null_sc, name, exact_match, 1, types);
Sean Callananfb3e4302011-10-29 19:50:43 +0000672
673 if (types.GetSize())
674 {
675 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
676
677 if (log)
678 {
679 const char *name_string = type_sp->GetName().GetCString();
680
681 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
682 current_id,
683 name.GetCString(),
684 (name_string ? name_string : "<anonymous>"));
685 }
Sean Callananbc47dfc2012-09-11 21:44:01 +0000686
687 clang::ASTContext *type_ast = type_sp->GetClangAST();
688 lldb::clang_type_t full_type = type_sp->GetClangFullType();
Sean Callanan0eed0d42011-12-06 03:41:14 +0000689
Sean Callananbc47dfc2012-09-11 21:44:01 +0000690 void *copied_type = GuardedCopyType(m_ast_context, type_ast, full_type);
Sean Callanan0eed0d42011-12-06 03:41:14 +0000691
Sean Callanane0a64f72011-12-01 21:04:37 +0000692 if (!copied_type)
693 {
694 if (log)
Sean Callanan7be70e82012-12-19 23:05:01 +0000695 log->Printf(" CAS::FEVD[%u] - Couldn't export a type",
Sean Callanan0eed0d42011-12-06 03:41:14 +0000696 current_id);
697
Sean Callanane0a64f72011-12-01 21:04:37 +0000698 break;
699 }
700
Sean Callananfb3e4302011-10-29 19:50:43 +0000701 context.AddTypeDecl(copied_type);
702 }
Sean Callanan7be70e82012-12-19 23:05:01 +0000703 else
704 {
705 do
706 {
707 // Couldn't find any types elsewhere. Try the Objective-C runtime if one exists.
708
709 lldb::ProcessSP process(m_target->GetProcessSP());
710
711 if (!process)
712 break;
713
714 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
715
716 if (!language_runtime)
717 break;
718
719 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
720
721 if (!type_vendor)
722 break;
723
724 bool append = false;
725 uint32_t max_matches = 1;
726 std::vector <ClangASTType> types;
727
728 if (!type_vendor->FindTypes(name,
729 append,
730 max_matches,
731 types))
732 break;
733
734 if (log)
735 {
736 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\" in the runtime",
737 current_id,
738 name.GetCString());
739 }
740
741 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
742
743 clang::QualType runtime_qual_type(runtime_clang_type, 0);
744
745 void *copied_type = GuardedCopyType(m_ast_context, type_vendor->GetClangASTContext(), runtime_qual_type.getAsOpaquePtr());
746
747 if (!copied_type)
748 {
749 if (log)
750 log->Printf(" CAS::FEVD[%u] - Couldn't export a type from the runtime",
751 current_id);
752
753 break;
754 }
755
756 context.AddTypeDecl(copied_type);
757 }
758 while(0);
759 }
Sean Callanan09ab4b72011-11-30 22:11:59 +0000760
Sean Callananfb3e4302011-10-29 19:50:43 +0000761 } while(0);
762}
763
Sean Callanan55400942012-11-02 17:09:58 +0000764template <class D> class TaggedASTDecl {
765public:
766 TaggedASTDecl() : decl(NULL) { }
767 TaggedASTDecl(D *_decl) : decl(_decl) { }
768 bool IsValid() const { return (decl != NULL); }
769 bool IsInvalid() const { return !IsValid(); }
770 D *operator->() const { return decl; }
771 D *decl;
772};
773
774template <class D2, template <class D> class TD, class D1>
775TD<D2>
776DynCast(TD<D1> source)
777{
778 return TD<D2> (dyn_cast<D2>(source.decl));
779}
780
781template <class D = Decl> class DeclFromParser;
782template <class D = Decl> class DeclFromUser;
783
784template <class D> class DeclFromParser : public TaggedASTDecl<D> {
785public:
786 DeclFromParser() : TaggedASTDecl<D>() { }
787 DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) { }
788
789 DeclFromUser<D> GetOrigin(ClangASTImporter *importer);
790};
791
792template <class D> class DeclFromUser : public TaggedASTDecl<D> {
793public:
794 DeclFromUser() : TaggedASTDecl<D>() { }
795 DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) { }
796
797 DeclFromParser<D> Import(ClangASTImporter *importer, ASTContext &dest_ctx);
798};
799
800template <class D>
801DeclFromUser<D>
802DeclFromParser<D>::GetOrigin(ClangASTImporter *importer)
803{
804 DeclFromUser <> origin_decl;
805 importer->ResolveDeclOrigin(this->decl, &origin_decl.decl, NULL);
806 if (origin_decl.IsInvalid())
807 return DeclFromUser<D>();
808 return DeclFromUser<D>(dyn_cast<D>(origin_decl.decl));
809}
810
811template <class D>
812DeclFromParser<D>
813DeclFromUser<D>::Import(ClangASTImporter *importer, ASTContext &dest_ctx)
814{
815 DeclFromParser <> parser_generic_decl(importer->CopyDecl(&dest_ctx, &this->decl->getASTContext(), this->decl));
816 if (parser_generic_decl.IsInvalid())
817 return DeclFromParser<D>();
818 return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
819}
820
Sean Callananc83e3412012-11-28 03:23:20 +0000821static bool
Sean Callananbc47dfc2012-09-11 21:44:01 +0000822FindObjCMethodDeclsWithOrigin (unsigned int current_id,
823 NameSearchContext &context,
824 ObjCInterfaceDecl *original_interface_decl,
825 clang::ASTContext *ast_context,
826 ClangASTImporter *ast_importer,
827 const char *log_info)
828{
829 const DeclarationName &decl_name(context.m_decl_name);
830 clang::ASTContext *original_ctx = &original_interface_decl->getASTContext();
831
832 Selector original_selector;
833
834 if (decl_name.isObjCZeroArgSelector())
835 {
836 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name.getAsString());
837 original_selector = original_ctx->Selectors.getSelector(0, &ident);
838 }
839 else if (decl_name.isObjCOneArgSelector())
840 {
841 const std::string &decl_name_string = decl_name.getAsString();
842 std::string decl_name_string_without_colon(decl_name_string.c_str(), decl_name_string.length() - 1);
843 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name_string_without_colon.c_str());
844 original_selector = original_ctx->Selectors.getSelector(1, &ident);
845 }
846 else
847 {
848 SmallVector<IdentifierInfo *, 4> idents;
849
850 clang::Selector sel = decl_name.getObjCSelector();
851
852 int num_args = sel.getNumArgs();
853
854 for (unsigned i = 0;
855 i != num_args;
856 ++i)
857 {
858 idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i)));
859 }
860
861 original_selector = original_ctx->Selectors.getSelector(num_args, idents.data());
862 }
863
864 DeclarationName original_decl_name(original_selector);
865
866 ObjCInterfaceDecl::lookup_result result = original_interface_decl->lookup(original_decl_name);
867
Sean Callanan5deaa4c2012-12-21 21:34:42 +0000868 if (result.empty())
Sean Callananc83e3412012-11-28 03:23:20 +0000869 return false;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000870
Sean Callanan5deaa4c2012-12-21 21:34:42 +0000871 if (!result[0])
Sean Callananc83e3412012-11-28 03:23:20 +0000872 return false;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000873
Sean Callanan5deaa4c2012-12-21 21:34:42 +0000874 ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(result[0]);
Sean Callananbc47dfc2012-09-11 21:44:01 +0000875
876 if (!result_method)
Sean Callananc83e3412012-11-28 03:23:20 +0000877 return false;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000878
879 Decl *copied_decl = ast_importer->CopyDecl(ast_context, &result_method->getASTContext(), result_method);
880
881 if (!copied_decl)
Sean Callananc83e3412012-11-28 03:23:20 +0000882 return false;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000883
884 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
885
886 if (!copied_method_decl)
Sean Callananc83e3412012-11-28 03:23:20 +0000887 return false;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000888
889 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
890
891 if (log)
892 {
893 ASTDumper dumper((Decl*)copied_method_decl);
894 log->Printf(" CAS::FOMD[%d] found (%s) %s", current_id, log_info, dumper.GetCString());
895 }
896
897 context.AddNamedDecl(copied_method_decl);
898
Sean Callananc83e3412012-11-28 03:23:20 +0000899 return true;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000900}
901
Sean Callanan0730e9c2011-11-09 19:33:21 +0000902void
903ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
904{
905 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
906
Sean Callanan46198ff2011-11-11 20:37:26 +0000907 static unsigned int invocation_id = 0;
908 unsigned int current_id = invocation_id++;
909
Sean Callanan0730e9c2011-11-09 19:33:21 +0000910 const DeclarationName &decl_name(context.m_decl_name);
911 const DeclContext *decl_ctx(context.m_decl_context);
912
913 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
914
915 if (!interface_decl)
916 return;
917
Sean Callanana6582262012-04-05 00:12:52 +0000918 do
919 {
920 Decl *original_decl = NULL;
921 ASTContext *original_ctx = NULL;
922
923 m_ast_importer->ResolveDeclOrigin(interface_decl, &original_decl, &original_ctx);
924
925 if (!original_decl)
926 break;
927
928 ObjCInterfaceDecl *original_interface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl);
929
Sean Callananc83e3412012-11-28 03:23:20 +0000930 if (FindObjCMethodDeclsWithOrigin(current_id,
931 context,
932 original_interface_decl,
933 m_ast_context,
934 m_ast_importer,
935 "at origin"))
936 return; // found it, no need to look any further
Sean Callanana6582262012-04-05 00:12:52 +0000937 } while (0);
938
Sean Callanan0730e9c2011-11-09 19:33:21 +0000939 StreamString ss;
Sean Callanan0eed0d42011-12-06 03:41:14 +0000940
Sean Callanan0730e9c2011-11-09 19:33:21 +0000941 if (decl_name.isObjCZeroArgSelector())
942 {
943 ss.Printf("%s", decl_name.getAsString().c_str());
944 }
945 else if (decl_name.isObjCOneArgSelector())
946 {
Sean Callanan4fb79b72011-11-14 18:29:46 +0000947 ss.Printf("%s", decl_name.getAsString().c_str());
Sean Callanan0730e9c2011-11-09 19:33:21 +0000948 }
949 else
950 {
951 clang::Selector sel = decl_name.getObjCSelector();
952
953 for (unsigned i = 0, e = sel.getNumArgs();
954 i != e;
955 ++i)
956 {
957 llvm::StringRef r = sel.getNameForSlot(i);
958 ss.Printf("%s:", r.str().c_str());
959 }
960 }
961 ss.Flush();
962
Sean Callanan46198ff2011-11-11 20:37:26 +0000963 ConstString selector_name(ss.GetData());
964
Sean Callanan0730e9c2011-11-09 19:33:21 +0000965 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000966 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
967 current_id,
968 m_ast_context,
Sean Callanan46198ff2011-11-11 20:37:26 +0000969 interface_decl->getNameAsString().c_str(),
970 selector_name.AsCString());
Sean Callanan46198ff2011-11-11 20:37:26 +0000971 SymbolContextList sc_list;
972
973 const bool include_symbols = false;
Sean Callanan9df05fb2012-02-10 22:52:19 +0000974 const bool include_inlines = false;
Sean Callanan46198ff2011-11-11 20:37:26 +0000975 const bool append = false;
976
Sean Callanana9bc0652012-01-19 02:17:40 +0000977 std::string interface_name = interface_decl->getNameAsString();
978
979 do
980 {
981 StreamString ms;
982 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());
983 ms.Flush();
984 ConstString instance_method_name(ms.GetData());
985
Sean Callanan9df05fb2012-02-10 22:52:19 +0000986 m_target->GetImages().FindFunctions(instance_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanana9bc0652012-01-19 02:17:40 +0000987
988 if (sc_list.GetSize())
989 break;
990
991 ms.Clear();
992 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());
993 ms.Flush();
994 ConstString class_method_name(ms.GetData());
995
Sean Callanan9df05fb2012-02-10 22:52:19 +0000996 m_target->GetImages().FindFunctions(class_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanana9bc0652012-01-19 02:17:40 +0000997
998 if (sc_list.GetSize())
999 break;
1000
1001 // Fall back and check for methods in categories. If we find methods this way, we need to check that they're actually in
1002 // categories on the desired class.
1003
1004 SymbolContextList candidate_sc_list;
1005
Sean Callanan9df05fb2012-02-10 22:52:19 +00001006 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, include_inlines, append, candidate_sc_list);
Sean Callanana9bc0652012-01-19 02:17:40 +00001007
1008 for (uint32_t ci = 0, ce = candidate_sc_list.GetSize();
1009 ci != ce;
1010 ++ci)
1011 {
1012 SymbolContext candidate_sc;
1013
1014 if (!candidate_sc_list.GetContextAtIndex(ci, candidate_sc))
1015 continue;
1016
1017 if (!candidate_sc.function)
1018 continue;
1019
1020 const char *candidate_name = candidate_sc.function->GetName().AsCString();
1021
1022 const char *cursor = candidate_name;
1023
1024 if (*cursor != '+' && *cursor != '-')
1025 continue;
1026
1027 ++cursor;
1028
1029 if (*cursor != '[')
1030 continue;
1031
1032 ++cursor;
1033
1034 size_t interface_len = interface_name.length();
1035
1036 if (strncmp(cursor, interface_name.c_str(), interface_len))
1037 continue;
1038
1039 cursor += interface_len;
1040
1041 if (*cursor == ' ' || *cursor == '(')
1042 sc_list.Append(candidate_sc);
1043 }
1044 }
1045 while (0);
Sean Callanan46198ff2011-11-11 20:37:26 +00001046
Sean Callananbc47dfc2012-09-11 21:44:01 +00001047 if (sc_list.GetSize())
Sean Callanan46198ff2011-11-11 20:37:26 +00001048 {
Sean Callananbc47dfc2012-09-11 21:44:01 +00001049 // We found a good function symbol. Use that.
Sean Callanan46198ff2011-11-11 20:37:26 +00001050
Sean Callananbc47dfc2012-09-11 21:44:01 +00001051 for (uint32_t i = 0, e = sc_list.GetSize();
1052 i != e;
1053 ++i)
Sean Callanan46198ff2011-11-11 20:37:26 +00001054 {
Sean Callananbc47dfc2012-09-11 21:44:01 +00001055 SymbolContext sc;
Sean Callanan46198ff2011-11-11 20:37:26 +00001056
Sean Callananbc47dfc2012-09-11 21:44:01 +00001057 if (!sc_list.GetContextAtIndex(i, sc))
Sean Callanan46198ff2011-11-11 20:37:26 +00001058 continue;
1059
Sean Callananbc47dfc2012-09-11 21:44:01 +00001060 if (!sc.function)
Sean Callanan46198ff2011-11-11 20:37:26 +00001061 continue;
1062
Sean Callananbc47dfc2012-09-11 21:44:01 +00001063 DeclContext *function_ctx = sc.function->GetClangDeclContext();
1064
1065 if (!function_ctx)
1066 continue;
1067
1068 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
1069
1070 if (!method_decl)
1071 continue;
1072
1073 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
1074
1075 if (!found_interface_decl)
1076 continue;
1077
1078 if (found_interface_decl->getName() == interface_decl->getName())
Sean Callanan46198ff2011-11-11 20:37:26 +00001079 {
Sean Callananbc47dfc2012-09-11 21:44:01 +00001080 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
1081
1082 if (!copied_decl)
1083 continue;
1084
1085 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
1086
1087 if (!copied_method_decl)
1088 continue;
1089
1090 if (log)
1091 {
1092 ASTDumper dumper((Decl*)copied_method_decl);
Sean Callanan55400942012-11-02 17:09:58 +00001093 log->Printf(" CAS::FOMD[%d] found (in symbols) %s", current_id, dumper.GetCString());
Sean Callananbc47dfc2012-09-11 21:44:01 +00001094 }
1095
1096 context.AddNamedDecl(copied_method_decl);
Sean Callanan46198ff2011-11-11 20:37:26 +00001097 }
Sean Callanan46198ff2011-11-11 20:37:26 +00001098 }
Sean Callanan55400942012-11-02 17:09:58 +00001099
1100 return;
Sean Callanan46198ff2011-11-11 20:37:26 +00001101 }
Sean Callanan55400942012-11-02 17:09:58 +00001102
1103 // Try the debug information.
1104
1105 do
Sean Callananbc47dfc2012-09-11 21:44:01 +00001106 {
Sean Callanan55400942012-11-02 17:09:58 +00001107 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(interface_decl));
1108
1109 if (!complete_interface_decl)
1110 break;
1111
1112 // We found the complete interface. The runtime never needs to be queried in this scenario.
1113
1114 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
1115
1116 if (complete_interface_decl == interface_decl)
1117 break; // already checked this one
1118
1119 if (log)
1120 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1121 current_id,
1122 complete_interface_decl,
1123 &complete_iface_decl->getASTContext());
1124
1125 FindObjCMethodDeclsWithOrigin(current_id,
1126 context,
1127 complete_interface_decl,
1128 m_ast_context,
1129 m_ast_importer,
1130 "in debug info");
1131
1132 return;
Sean Callananbc47dfc2012-09-11 21:44:01 +00001133 }
Sean Callanan55400942012-11-02 17:09:58 +00001134 while (0);
Sean Callanan5b26f272012-02-04 08:49:35 +00001135
Sean Callanan55400942012-11-02 17:09:58 +00001136 do
1137 {
1138 // Check the runtime only if the debug information didn't have a complete interface.
1139
1140 lldb::ProcessSP process(m_target->GetProcessSP());
1141
1142 if (!process)
1143 break;
1144
1145 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1146
Sean Callanan7be70e82012-12-19 23:05:01 +00001147 if (!language_runtime)
1148 break;
1149
Sean Callanan55400942012-11-02 17:09:58 +00001150 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
1151
1152 if (!type_vendor)
1153 break;
1154
1155 ConstString interface_name(interface_decl->getNameAsString().c_str());
1156 bool append = false;
1157 uint32_t max_matches = 1;
1158 std::vector <ClangASTType> types;
1159
1160 if (!type_vendor->FindTypes(interface_name,
1161 append,
1162 max_matches,
1163 types))
1164 break;
1165
1166 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
1167
1168 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
1169
1170 if (!runtime_interface_type)
1171 break;
1172
1173 ObjCInterfaceDecl *runtime_interface_decl = runtime_interface_type->getDecl();
1174
1175 FindObjCMethodDeclsWithOrigin(current_id,
1176 context,
1177 runtime_interface_decl,
1178 m_ast_context,
1179 m_ast_importer,
1180 "in runtime");
1181 }
1182 while(0);
Sean Callanan5b26f272012-02-04 08:49:35 +00001183}
1184
Sean Callanan72772842012-02-22 23:57:45 +00001185static bool
1186FindObjCPropertyAndIvarDeclsWithOrigin (unsigned int current_id,
1187 NameSearchContext &context,
1188 clang::ASTContext &ast_context,
1189 ClangASTImporter *ast_importer,
1190 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl)
1191{
1192 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1193
1194 if (origin_iface_decl.IsInvalid())
1195 return false;
1196
1197 std::string name_str = context.m_decl_name.getAsString();
1198 StringRef name(name_str.c_str());
1199 IdentifierInfo &name_identifier(origin_iface_decl->getASTContext().Idents.get(name));
1200
1201 DeclFromUser<ObjCPropertyDecl> origin_property_decl(origin_iface_decl->FindPropertyDeclaration(&name_identifier));
1202
1203 bool found = false;
1204
1205 if (origin_property_decl.IsValid())
1206 {
1207 DeclFromParser<ObjCPropertyDecl> parser_property_decl(origin_property_decl.Import(ast_importer, ast_context));
1208 if (parser_property_decl.IsValid())
1209 {
1210 if (log)
1211 {
1212 ASTDumper dumper((Decl*)parser_property_decl.decl);
1213 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1214 }
1215
1216 context.AddNamedDecl(parser_property_decl.decl);
1217 found = true;
1218 }
1219 }
1220
1221 DeclFromUser<ObjCIvarDecl> origin_ivar_decl(origin_iface_decl->getIvarDecl(&name_identifier));
1222
1223 if (origin_ivar_decl.IsValid())
1224 {
1225 DeclFromParser<ObjCIvarDecl> parser_ivar_decl(origin_ivar_decl.Import(ast_importer, ast_context));
1226 if (parser_ivar_decl.IsValid())
1227 {
1228 if (log)
1229 {
1230 ASTDumper dumper((Decl*)parser_ivar_decl.decl);
1231 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1232 }
1233
1234 context.AddNamedDecl(parser_ivar_decl.decl);
1235 found = true;
1236 }
1237 }
1238
1239 return found;
1240}
1241
Sean Callanand5c17ed2011-11-15 02:11:17 +00001242void
Sean Callanan5b26f272012-02-04 08:49:35 +00001243ClangASTSource::FindObjCPropertyAndIvarDecls (NameSearchContext &context)
Sean Callanand5c17ed2011-11-15 02:11:17 +00001244{
1245 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1246
1247 static unsigned int invocation_id = 0;
1248 unsigned int current_id = invocation_id++;
1249
Sean Callanan5b26f272012-02-04 08:49:35 +00001250 DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl(cast<ObjCInterfaceDecl>(context.m_decl_context));
1251 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(parser_iface_decl.GetOrigin(m_ast_importer));
Sean Callanan72772842012-02-22 23:57:45 +00001252
1253 ConstString class_name(parser_iface_decl->getNameAsString().c_str());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001254
1255 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +00001256 log->Printf("ClangASTSource::FindObjCPropertyAndIvarDecls[%d] on (ASTContext*)%p for '%s.%s'",
Sean Callanand5c17ed2011-11-15 02:11:17 +00001257 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +00001258 m_ast_context,
Sean Callanan5b26f272012-02-04 08:49:35 +00001259 parser_iface_decl->getNameAsString().c_str(),
Sean Callanan72772842012-02-22 23:57:45 +00001260 context.m_decl_name.getAsString().c_str());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001261
Sean Callanan72772842012-02-22 23:57:45 +00001262 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1263 context,
1264 *m_ast_context,
1265 m_ast_importer,
1266 origin_iface_decl))
1267 return;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001268
Sean Callanan72772842012-02-22 23:57:45 +00001269 if (log)
1270 log->Printf("CAS::FOPD[%d] couldn't find the property on origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p, searching elsewhere...",
1271 current_id,
1272 origin_iface_decl.decl,
1273 &origin_iface_decl->getASTContext());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001274
Sean Callanan72772842012-02-22 23:57:45 +00001275 SymbolContext null_sc;
1276 TypeList type_list;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001277
Sean Callananbc47dfc2012-09-11 21:44:01 +00001278 do
1279 {
Sean Callanan2cb5e522012-09-20 23:21:16 +00001280 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(parser_iface_decl.decl));
Sean Callananbc47dfc2012-09-11 21:44:01 +00001281
Sean Callanan2cb5e522012-09-20 23:21:16 +00001282 if (!complete_interface_decl)
Sean Callananbc47dfc2012-09-11 21:44:01 +00001283 break;
1284
Sean Callanan55400942012-11-02 17:09:58 +00001285 // We found the complete interface. The runtime never needs to be queried in this scenario.
1286
Sean Callanan2cb5e522012-09-20 23:21:16 +00001287 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
Sean Callananbc47dfc2012-09-11 21:44:01 +00001288
1289 if (complete_iface_decl.decl == origin_iface_decl.decl)
1290 break; // already checked this one
1291
1292 if (log)
1293 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1294 current_id,
1295 complete_iface_decl.decl,
1296 &complete_iface_decl->getASTContext());
1297
Sean Callanan55400942012-11-02 17:09:58 +00001298 FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1299 context,
1300 *m_ast_context,
1301 m_ast_importer,
1302 complete_iface_decl);
1303
1304 return;
Sean Callananbc47dfc2012-09-11 21:44:01 +00001305 }
1306 while(0);
Sean Callanan72772842012-02-22 23:57:45 +00001307
Sean Callananbc47dfc2012-09-11 21:44:01 +00001308 do
1309 {
Sean Callanan55400942012-11-02 17:09:58 +00001310 // Check the runtime only if the debug information didn't have a complete interface.
1311
1312 lldb::ProcessSP process(m_target->GetProcessSP());
1313
1314 if (!process)
1315 return;
1316
1317 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1318
1319 if (!language_runtime)
1320 return;
Sean Callananbc47dfc2012-09-11 21:44:01 +00001321
1322 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
1323
1324 if (!type_vendor)
1325 break;
1326
1327 bool append = false;
1328 uint32_t max_matches = 1;
1329 std::vector <ClangASTType> types;
1330
1331 if (!type_vendor->FindTypes(class_name,
1332 append,
1333 max_matches,
1334 types))
1335 break;
1336
1337 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
1338
1339 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
1340
1341 if (!runtime_interface_type)
1342 break;
1343
1344 DeclFromUser<const ObjCInterfaceDecl> runtime_iface_decl(runtime_interface_type->getDecl());
1345
1346 if (log)
1347 log->Printf("CAS::FOPD[%d] trying runtime (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1348 current_id,
1349 runtime_iface_decl.decl,
1350 &runtime_iface_decl->getASTContext());
1351
1352 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1353 context,
1354 *m_ast_context,
1355 m_ast_importer,
1356 runtime_iface_decl))
1357 return;
1358 }
1359 while(0);
Sean Callanan5b26f272012-02-04 08:49:35 +00001360}
1361
1362typedef llvm::DenseMap <const FieldDecl *, uint64_t> FieldOffsetMap;
1363typedef llvm::DenseMap <const CXXRecordDecl *, CharUnits> BaseOffsetMap;
1364
1365template <class D, class O>
1366static bool
1367ImportOffsetMap (llvm::DenseMap <const D*, O> &destination_map,
1368 llvm::DenseMap <const D*, O> &source_map,
1369 ClangASTImporter *importer,
1370 ASTContext &dest_ctx)
1371{
1372 typedef llvm::DenseMap <const D*, O> MapType;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001373
Sean Callanan5b26f272012-02-04 08:49:35 +00001374 for (typename MapType::iterator fi = source_map.begin(), fe = source_map.end();
1375 fi != fe;
1376 ++fi)
1377 {
1378 DeclFromUser <D> user_decl(const_cast<D*>(fi->first));
1379 DeclFromParser <D> parser_decl(user_decl.Import(importer, dest_ctx));
1380 if (parser_decl.IsInvalid())
1381 return false;
1382 destination_map.insert(std::pair<const D *, O>(parser_decl.decl, fi->second));
1383 }
1384
1385 return true;
1386}
1387
1388template <bool IsVirtual> bool ExtractBaseOffsets (const ASTRecordLayout &record_layout,
1389 DeclFromUser<const CXXRecordDecl> &record,
1390 BaseOffsetMap &base_offsets)
1391{
1392 for (CXXRecordDecl::base_class_const_iterator
1393 bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()),
1394 be = (IsVirtual ? record->vbases_end() : record->bases_end());
1395 bi != be;
1396 ++bi)
1397 {
1398 if (!IsVirtual && bi->isVirtual())
1399 continue;
1400
1401 const clang::Type *origin_base_type = bi->getType().getTypePtr();
1402 const clang::RecordType *origin_base_record_type = origin_base_type->getAs<RecordType>();
1403
1404 if (!origin_base_record_type)
1405 return false;
1406
1407 DeclFromUser <RecordDecl> origin_base_record(origin_base_record_type->getDecl());
1408
1409 if (origin_base_record.IsInvalid())
1410 return false;
1411
1412 DeclFromUser <CXXRecordDecl> origin_base_cxx_record(DynCast<CXXRecordDecl>(origin_base_record));
1413
1414 if (origin_base_cxx_record.IsInvalid())
1415 return false;
1416
1417 CharUnits base_offset;
1418
1419 if (IsVirtual)
1420 base_offset = record_layout.getVBaseClassOffset(origin_base_cxx_record.decl);
1421 else
1422 base_offset = record_layout.getBaseClassOffset(origin_base_cxx_record.decl);
1423
1424 base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(origin_base_cxx_record.decl, base_offset));
1425 }
1426
1427 return true;
1428}
1429
1430bool
1431ClangASTSource::layoutRecordType(const RecordDecl *record,
1432 uint64_t &size,
1433 uint64_t &alignment,
1434 FieldOffsetMap &field_offsets,
1435 BaseOffsetMap &base_offsets,
1436 BaseOffsetMap &virtual_base_offsets)
1437{
1438 static unsigned int invocation_id = 0;
1439 unsigned int current_id = invocation_id++;
1440
1441 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1442
Sean Callanand5c17ed2011-11-15 02:11:17 +00001443 if (log)
1444 {
Sean Callanan5b26f272012-02-04 08:49:35 +00001445 log->Printf("LayoutRecordType[%u] on (RecordDecl*)%p [name = '%s']",
1446 current_id,
1447 m_ast_context,
1448 record->getNameAsString().c_str());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001449 }
Sean Callanan5b26f272012-02-04 08:49:35 +00001450
1451
1452 DeclFromParser <const RecordDecl> parser_record(record);
1453 DeclFromUser <const RecordDecl> origin_record(parser_record.GetOrigin(m_ast_importer));
1454
1455 if (origin_record.IsInvalid())
1456 return false;
1457
1458 FieldOffsetMap origin_field_offsets;
1459 BaseOffsetMap origin_base_offsets;
1460 BaseOffsetMap origin_virtual_base_offsets;
1461
Sean Callananbf81ea52012-04-07 00:06:00 +00001462 ClangASTContext::GetCompleteDecl(&origin_record->getASTContext(), const_cast<RecordDecl*>(origin_record.decl));
1463
1464 if (!origin_record.decl->getDefinition())
1465 return false;
1466
Sean Callanan5b26f272012-02-04 08:49:35 +00001467 const ASTRecordLayout &record_layout(origin_record->getASTContext().getASTRecordLayout(origin_record.decl));
1468
1469 int field_idx = 0;
1470
1471 for (RecordDecl::field_iterator fi = origin_record->field_begin(), fe = origin_record->field_end();
1472 fi != fe;
1473 ++fi)
1474 {
1475 uint64_t field_offset = record_layout.getFieldOffset(field_idx);
1476
1477 origin_field_offsets.insert(std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
1478
1479 field_idx++;
1480 }
1481
1482 ASTContext &parser_ast_context(record->getASTContext());
1483
1484 DeclFromUser <const CXXRecordDecl> origin_cxx_record(DynCast<const CXXRecordDecl>(origin_record));
1485
1486 if (origin_cxx_record.IsValid())
1487 {
1488 if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record, origin_base_offsets) ||
1489 !ExtractBaseOffsets<true>(record_layout, origin_cxx_record, origin_virtual_base_offsets))
1490 return false;
1491 }
1492
1493 if (!ImportOffsetMap(field_offsets, origin_field_offsets, m_ast_importer, parser_ast_context) ||
1494 !ImportOffsetMap(base_offsets, origin_base_offsets, m_ast_importer, parser_ast_context) ||
1495 !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets, m_ast_importer, parser_ast_context))
1496 return false;
1497
1498 size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth();
1499 alignment = record_layout.getAlignment().getQuantity() * m_ast_context->getCharWidth();
1500
1501 if (log)
1502 {
1503 log->Printf("LRT[%u] returned:", current_id);
1504 log->Printf("LRT[%u] Original = (RecordDecl*)%p", current_id, origin_record.decl);
Daniel Malead01b2952012-11-29 21:49:15 +00001505 log->Printf("LRT[%u] Size = %" PRId64, current_id, size);
1506 log->Printf("LRT[%u] Alignment = %" PRId64, current_id, alignment);
Sean Callanan5b26f272012-02-04 08:49:35 +00001507 log->Printf("LRT[%u] Fields:", current_id);
1508 for (RecordDecl::field_iterator fi = record->field_begin(), fe = record->field_end();
1509 fi != fe;
1510 ++fi)
1511 {
Daniel Malead01b2952012-11-29 21:49:15 +00001512 log->Printf("LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %" PRId64 " bits",
Sean Callanan5b26f272012-02-04 08:49:35 +00001513 current_id,
1514 *fi,
1515 fi->getNameAsString().c_str(),
1516 field_offsets[*fi]);
1517 }
1518 DeclFromParser <const CXXRecordDecl> parser_cxx_record = DynCast<const CXXRecordDecl>(parser_record);
1519 if (parser_cxx_record.IsValid())
1520 {
1521 log->Printf("LRT[%u] Bases:", current_id);
1522 for (CXXRecordDecl::base_class_const_iterator bi = parser_cxx_record->bases_begin(), be = parser_cxx_record->bases_end();
1523 bi != be;
1524 ++bi)
1525 {
1526 bool is_virtual = bi->isVirtual();
1527
1528 QualType base_type = bi->getType();
1529 const RecordType *base_record_type = base_type->getAs<RecordType>();
1530 DeclFromParser <RecordDecl> base_record(base_record_type->getDecl());
1531 DeclFromParser <CXXRecordDecl> base_cxx_record = DynCast<CXXRecordDecl>(base_record);
1532
Daniel Malead01b2952012-11-29 21:49:15 +00001533 log->Printf("LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %" PRId64 " chars",
Sean Callanan5b26f272012-02-04 08:49:35 +00001534 current_id,
1535 (is_virtual ? "Virtual " : ""),
1536 base_cxx_record.decl,
1537 base_cxx_record.decl->getNameAsString().c_str(),
1538 (is_virtual ? virtual_base_offsets[base_cxx_record.decl].getQuantity() :
1539 base_offsets[base_cxx_record.decl].getQuantity()));
1540 }
1541 }
1542 else
1543 {
1544 log->Printf("LRD[%u] Not a CXXRecord, so no bases", current_id);
1545 }
1546 }
1547
1548 return true;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001549}
1550
Sean Callanan1ee44b72011-10-29 01:58:46 +00001551void
1552ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
1553 const ConstString &name,
1554 ClangASTImporter::NamespaceMapSP &parent_map) const
1555{
1556 static unsigned int invocation_id = 0;
1557 unsigned int current_id = invocation_id++;
1558
1559 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1560
1561 if (log)
1562 {
1563 if (parent_map && parent_map->size())
Sean Callanan00f43622011-11-18 03:28:09 +00001564 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
Sean Callanan1ee44b72011-10-29 01:58:46 +00001565 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +00001566 m_ast_context,
Sean Callanan1ee44b72011-10-29 01:58:46 +00001567 name.GetCString(),
1568 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
1569 else
Sean Callanan00f43622011-11-18 03:28:09 +00001570 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
Sean Callanan1ee44b72011-10-29 01:58:46 +00001571 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +00001572 m_ast_context,
Sean Callanan1ee44b72011-10-29 01:58:46 +00001573 name.GetCString());
1574 }
1575
1576
1577 if (parent_map)
1578 {
1579 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
1580 i != e;
1581 ++i)
1582 {
1583 ClangNamespaceDecl found_namespace_decl;
1584
1585 lldb::ModuleSP module_sp = i->first;
1586 ClangNamespaceDecl module_parent_namespace_decl = i->second;
1587
1588 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
1589
1590 if (!symbol_vendor)
1591 continue;
1592
1593 SymbolContext null_sc;
1594
1595 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
1596
1597 if (!found_namespace_decl)
1598 continue;
1599
1600 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
1601
1602 if (log)
1603 log->Printf(" CMN[%u] Found namespace %s in module %s",
1604 current_id,
1605 name.GetCString(),
1606 module_sp->GetFileSpec().GetFilename().GetCString());
1607 }
1608 }
1609 else
1610 {
Enrico Granata17598482012-11-08 02:22:02 +00001611 const ModuleList &target_images = m_target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00001612 Mutex::Locker modules_locker(target_images.GetMutex());
1613
Sean Callanan1ee44b72011-10-29 01:58:46 +00001614 ClangNamespaceDecl null_namespace_decl;
1615
Greg Claytonc7bece562013-01-25 18:06:21 +00001616 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i)
Sean Callanan1ee44b72011-10-29 01:58:46 +00001617 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00001618 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
Sean Callanan1ee44b72011-10-29 01:58:46 +00001619
1620 if (!image)
1621 continue;
1622
1623 ClangNamespaceDecl found_namespace_decl;
1624
1625 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
1626
1627 if (!symbol_vendor)
1628 continue;
1629
1630 SymbolContext null_sc;
1631
1632 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
1633
1634 if (!found_namespace_decl)
1635 continue;
1636
1637 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
1638
1639 if (log)
1640 log->Printf(" CMN[%u] Found namespace %s in module %s",
1641 current_id,
1642 name.GetCString(),
1643 image->GetFileSpec().GetFilename().GetCString());
1644 }
1645 }
1646}
1647
Sean Callananba0aca72011-10-29 02:28:18 +00001648NamespaceDecl *
1649ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
1650{
Greg Claytone1cd1be2012-01-29 20:56:30 +00001651 if (!namespace_decls)
Sean Callananba0aca72011-10-29 02:28:18 +00001652 return NULL;
1653
1654 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1655
1656 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
1657
Sean Callanan686b2312011-11-16 18:20:47 +00001658 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
Sean Callananba0aca72011-10-29 02:28:18 +00001659
Sean Callanan61b33f72012-03-20 21:11:12 +00001660 if (!copied_decl)
1661 return NULL;
Sean Callananeeffea42013-02-12 08:01:13 +00001662
Sean Callananba0aca72011-10-29 02:28:18 +00001663 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1664
Sean Callananeeffea42013-02-12 08:01:13 +00001665 if (!copied_namespace_decl)
1666 return NULL;
1667
1668 context.m_decls.push_back(copied_namespace_decl);
1669
Sean Callananba0aca72011-10-29 02:28:18 +00001670 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
1671
1672 return dyn_cast<NamespaceDecl>(copied_decl);
1673}
1674
Sean Callananfb3e4302011-10-29 19:50:43 +00001675void *
1676ClangASTSource::GuardedCopyType (ASTContext *dest_context,
1677 ASTContext *source_context,
1678 void *clang_type)
1679{
1680 SetImportInProgress(true);
1681
Sean Callanan686b2312011-11-16 18:20:47 +00001682 QualType ret_qual_type = m_ast_importer->CopyType (m_ast_context, source_context, QualType::getFromOpaquePtr(clang_type));
Sean Callananfb3e4302011-10-29 19:50:43 +00001683
1684 void *ret = ret_qual_type.getAsOpaquePtr();
1685
1686 SetImportInProgress(false);
1687
Sean Callanan61b33f72012-03-20 21:11:12 +00001688 if (ret && ret_qual_type->getCanonicalTypeInternal().isNull())
Sean Callanan8b665982012-02-27 19:22:39 +00001689 // this shouldn't happen, but we're hardening because the AST importer seems to be generating bad types
1690 // on occasion.
1691 return NULL;
1692
Sean Callananfb3e4302011-10-29 19:50:43 +00001693 return ret;
1694}
1695
Greg Clayton6beaaa62011-01-17 03:46:26 +00001696clang::NamedDecl *
1697NameSearchContext::AddVarDecl(void *type)
1698{
Greg Clayton7b462cc2010-10-15 22:48:33 +00001699 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan348b5892010-11-30 00:27:43 +00001700
1701 assert (type && "Type for variable must be non-NULL!");
Sean Callanan44096b12010-09-14 21:59:34 +00001702
Sean Callananeddeb3b2011-10-28 23:38:38 +00001703 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001704 const_cast<DeclContext*>(m_decl_context),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001705 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00001706 SourceLocation(),
Sean Callanan44096b12010-09-14 21:59:34 +00001707 ii,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001708 QualType::getFromOpaquePtr(type),
1709 0,
Sean Callanane2ef6e32010-09-23 03:01:22 +00001710 SC_Static,
1711 SC_Static);
Greg Clayton7b462cc2010-10-15 22:48:33 +00001712 m_decls.push_back(Decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001713
1714 return Decl;
1715}
Sean Callanan468574b2010-06-22 23:46:24 +00001716
Greg Clayton6beaaa62011-01-17 03:46:26 +00001717clang::NamedDecl *
1718NameSearchContext::AddFunDecl (void *type)
1719{
Sean Callanane2d47482012-03-21 17:13:20 +00001720 assert (type && "Type for variable must be non-NULL!");
1721
Sean Callananeddeb3b2011-10-28 23:38:38 +00001722 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001723 const_cast<DeclContext*>(m_decl_context),
1724 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00001725 SourceLocation(),
Greg Clayton7b462cc2010-10-15 22:48:33 +00001726 m_decl_name.getAsIdentifierInfo(),
1727 QualType::getFromOpaquePtr(type),
1728 NULL,
1729 SC_Static,
1730 SC_Static,
1731 false,
1732 true);
Sean Callanan468574b2010-06-22 23:46:24 +00001733
Sean Callanan04949cc2010-08-12 23:45:38 +00001734 // We have to do more than just synthesize the FunctionDecl. We have to
1735 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
1736 // this, we raid the function's FunctionProtoType for types.
1737
Greg Clayton7b462cc2010-10-15 22:48:33 +00001738 QualType qual_type (QualType::getFromOpaquePtr(type));
Sean Callananfc4f2fb2011-12-14 01:13:04 +00001739 const FunctionProtoType *func_proto_type = qual_type.getTypePtr()->getAs<FunctionProtoType>();
Sean Callanan468574b2010-06-22 23:46:24 +00001740
Greg Clayton7b462cc2010-10-15 22:48:33 +00001741 if (func_proto_type)
Sean Callanand448c1b2010-06-23 18:58:10 +00001742 {
Greg Clayton7b462cc2010-10-15 22:48:33 +00001743 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan468574b2010-06-22 23:46:24 +00001744 unsigned ArgIndex;
1745
Sean Callanan880e6802011-10-07 23:18:13 +00001746 SmallVector<ParmVarDecl *, 5> parm_var_decls;
1747
Sean Callanan468574b2010-06-22 23:46:24 +00001748 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
1749 {
Greg Clayton7b462cc2010-10-15 22:48:33 +00001750 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan468574b2010-06-22 23:46:24 +00001751
Sean Callananeddeb3b2011-10-28 23:38:38 +00001752 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callanan880e6802011-10-07 23:18:13 +00001753 const_cast<DeclContext*>(m_decl_context),
1754 SourceLocation(),
1755 SourceLocation(),
1756 NULL,
1757 arg_qual_type,
1758 NULL,
1759 SC_Static,
1760 SC_Static,
1761 NULL));
Sean Callanan468574b2010-06-22 23:46:24 +00001762 }
1763
Sean Callanan880e6802011-10-07 23:18:13 +00001764 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan468574b2010-06-22 23:46:24 +00001765 }
Sean Callanane0a64f72011-12-01 21:04:37 +00001766 else
1767 {
1768 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1769
1770 log->Printf("Function type wasn't a FunctionProtoType");
1771 }
Sean Callanan468574b2010-06-22 23:46:24 +00001772
Greg Clayton7b462cc2010-10-15 22:48:33 +00001773 m_decls.push_back(func_decl);
Sean Callanan468574b2010-06-22 23:46:24 +00001774
Greg Clayton7b462cc2010-10-15 22:48:33 +00001775 return func_decl;
Sean Callanan468574b2010-06-22 23:46:24 +00001776}
Sean Callanan8ade1042010-07-27 00:55:47 +00001777
Greg Clayton6beaaa62011-01-17 03:46:26 +00001778clang::NamedDecl *
1779NameSearchContext::AddGenericFunDecl()
Sean Callanan8ade1042010-07-27 00:55:47 +00001780{
Sean Callanan2c777c42011-01-18 23:32:05 +00001781 FunctionProtoType::ExtProtoInfo proto_info;
1782
1783 proto_info.Variadic = true;
1784
Sean Callananeddeb3b2011-10-28 23:38:38 +00001785 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
1786 NULL, // argument types
1787 0, // number of arguments
1788 proto_info));
Greg Clayton7b462cc2010-10-15 22:48:33 +00001789
Sean Callanan8ade1042010-07-27 00:55:47 +00001790 return AddFunDecl(generic_function_type.getAsOpaquePtr());
1791}
Sean Callanan5666b672010-08-04 01:02:13 +00001792
Greg Clayton6beaaa62011-01-17 03:46:26 +00001793clang::NamedDecl *
1794NameSearchContext::AddTypeDecl(void *type)
Sean Callanan5666b672010-08-04 01:02:13 +00001795{
Greg Clayton1b03cb52011-01-23 00:34:52 +00001796 if (type)
1797 {
1798 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan5666b672010-08-04 01:02:13 +00001799
Sean Callananfc4f2fb2011-12-14 01:13:04 +00001800 if (const TagType *tag_type = qual_type->getAs<TagType>())
Greg Clayton1b03cb52011-01-23 00:34:52 +00001801 {
1802 TagDecl *tag_decl = tag_type->getDecl();
1803
1804 m_decls.push_back(tag_decl);
1805
1806 return tag_decl;
1807 }
Sean Callananfc4f2fb2011-12-14 01:13:04 +00001808 else if (const ObjCObjectType *objc_object_type = qual_type->getAs<ObjCObjectType>())
Greg Clayton1b03cb52011-01-23 00:34:52 +00001809 {
1810 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
1811
1812 m_decls.push_back((NamedDecl*)interface_decl);
1813
1814 return (NamedDecl*)interface_decl;
1815 }
Sean Callanan5666b672010-08-04 01:02:13 +00001816 }
Greg Clayton1b03cb52011-01-23 00:34:52 +00001817 return NULL;
Sean Callanan5666b672010-08-04 01:02:13 +00001818}
Greg Claytona2721472011-06-25 00:44:06 +00001819
1820void
1821NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
1822{
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001823 for (clang::NamedDecl *decl : result)
1824 m_decls.push_back (decl);
Greg Claytona2721472011-06-25 00:44:06 +00001825}
1826
1827void
1828NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
1829{
1830 m_decls.push_back (decl);
1831}