blob: 69d7b059e77f7fa40158592faf6f9cb7fc7a4455 [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)
Sean Callanan8106d802013-03-08 20:04:57 +0000370{
371 ClangASTMetrics::RegisterLexicalQuery();
372
Sean Callananba0aca72011-10-29 02:28:18 +0000373 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
374
375 const Decl *context_decl = dyn_cast<Decl>(decl_context);
376
377 if (!context_decl)
378 return ELR_Failure;
379
380 static unsigned int invocation_id = 0;
381 unsigned int current_id = invocation_id++;
382
383 if (log)
384 {
385 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
Sean Callanan00f43622011-11-18 03:28:09 +0000386 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p with %s predicate",
Sean Callananba0aca72011-10-29 02:28:18 +0000387 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +0000388 m_ast_context,
Sean Callananba0aca72011-10-29 02:28:18 +0000389 context_named_decl->getNameAsString().c_str(),
Sean Callanan00f43622011-11-18 03:28:09 +0000390 context_decl->getDeclKindName(),
391 context_decl,
Sean Callananba0aca72011-10-29 02:28:18 +0000392 (predicate ? "non-null" : "null"));
393 else if(context_decl)
Sean Callanan00f43622011-11-18 03:28:09 +0000394 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%sDecl*)%p with %s predicate",
Sean Callananba0aca72011-10-29 02:28:18 +0000395 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +0000396 m_ast_context,
Sean Callananba0aca72011-10-29 02:28:18 +0000397 context_decl->getDeclKindName(),
Sean Callanan00f43622011-11-18 03:28:09 +0000398 context_decl,
Sean Callananba0aca72011-10-29 02:28:18 +0000399 (predicate ? "non-null" : "null"));
400 else
Sean Callanan00f43622011-11-18 03:28:09 +0000401 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context with %s predicate",
Sean Callananba0aca72011-10-29 02:28:18 +0000402 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +0000403 m_ast_context,
Sean Callananba0aca72011-10-29 02:28:18 +0000404 (predicate ? "non-null" : "null"));
405 }
406
407 Decl *original_decl = NULL;
408 ASTContext *original_ctx = NULL;
409
410 if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx))
411 return ELR_Failure;
412
413 if (log)
414 {
Sean Callanan933ca2e2013-02-28 03:12:58 +0000415 log->Printf(" FELD[%u] Original decl (ASTContext*)%p (Decl*)%p:", current_id, original_ctx, original_decl);
Sean Callananba0aca72011-10-29 02:28:18 +0000416 ASTDumper(original_decl).ToLog(log, " ");
417 }
418
Sean Callanan2cb5e522012-09-20 23:21:16 +0000419 if (ObjCInterfaceDecl *original_iface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl))
420 {
421 ObjCInterfaceDecl *complete_iface_decl = GetCompleteObjCInterface(original_iface_decl);
422
423 if (complete_iface_decl && (complete_iface_decl != original_iface_decl))
424 {
425 original_decl = complete_iface_decl;
426 original_ctx = &complete_iface_decl->getASTContext();
427
428 m_ast_importer->SetDeclOrigin(context_decl, original_iface_decl);
429 }
430 }
431
Sean Callananba0aca72011-10-29 02:28:18 +0000432 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
433 {
434 ExternalASTSource *external_source = original_ctx->getExternalSource();
435
436 if (external_source)
437 external_source->CompleteType (original_tag_decl);
438 }
439
Sean Callanan12014a02011-12-08 23:45:45 +0000440 const DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl);
Sean Callananba0aca72011-10-29 02:28:18 +0000441
442 if (!original_decl_context)
443 return ELR_Failure;
444
445 for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
446 iter != original_decl_context->decls_end();
447 ++iter)
448 {
449 Decl *decl = *iter;
450
451 if (!predicate || predicate(decl->getKind()))
452 {
453 if (log)
454 {
455 ASTDumper ast_dumper(decl);
456 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
Sean Callanan3d654b32012-09-24 22:25:51 +0000457 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 +0000458 else
Sean Callanan3d654b32012-09-24 22:25:51 +0000459 log->Printf(" FELD[%d] Adding lexical %sDecl %s", current_id, decl->getDeclKindName(), ast_dumper.GetCString());
Sean Callananba0aca72011-10-29 02:28:18 +0000460 }
461
Sean Callanan686b2312011-11-16 18:20:47 +0000462 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, original_ctx, decl);
Sean Callananba0aca72011-10-29 02:28:18 +0000463
Sean Callanan61b33f72012-03-20 21:11:12 +0000464 if (!copied_decl)
465 continue;
466
Sean Callanancf128622012-03-15 01:53:17 +0000467 if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl))
468 {
469 QualType copied_field_type = copied_field->getType();
470
471 if (const TagType *copied_field_tag_type = copied_field_type->getAs<TagType>())
472 m_ast_importer->CompleteTagDecl(copied_field_tag_type->getDecl());
473 if (const ObjCObjectType *copied_field_object_type = copied_field_type->getAs<ObjCObjectType>())
474 if (ObjCInterfaceDecl *copied_field_objc_interface_decl = copied_field_object_type->getInterface())
475 m_ast_importer->CompleteObjCInterfaceDecl(copied_field_objc_interface_decl);
476 }
477
Sean Callananba0aca72011-10-29 02:28:18 +0000478 decls.push_back(copied_decl);
479 }
480 }
481
482 return ELR_AlreadyLoaded;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483}
484
Sean Callananfb3e4302011-10-29 19:50:43 +0000485void
486ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
487{
488 assert (m_ast_context);
489
Sean Callanan8106d802013-03-08 20:04:57 +0000490 ClangASTMetrics::RegisterVisibleQuery();
491
Sean Callananfb3e4302011-10-29 19:50:43 +0000492 const ConstString name(context.m_decl_name.getAsString().c_str());
493
494 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
495
496 static unsigned int invocation_id = 0;
497 unsigned int current_id = invocation_id++;
498
499 if (log)
500 {
501 if (!context.m_decl_context)
Sean Callanana6e61a72012-01-13 22:05:55 +0000502 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 +0000503 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
Sean Callanan00f43622011-11-18 03:28:09 +0000504 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 +0000505 else
Sean Callanan00f43622011-11-18 03:28:09 +0000506 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 +0000507 }
508
509 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
510
511 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
512 {
513 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
514
515 if (log && log->GetVerbose())
516 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
517 current_id,
518 namespace_map.get(),
519 (int)namespace_map->size());
520
521 if (!namespace_map)
522 return;
523
524 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
525 i != e;
526 ++i)
527 {
528 if (log)
529 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
530 current_id,
531 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
532 i->first->GetFileSpec().GetFilename().GetCString());
533
534 FindExternalVisibleDecls(context,
535 i->first,
536 i->second,
537 current_id);
538 }
539 }
Sean Callanan100d74e2011-11-15 21:50:18 +0000540 else if (isa<ObjCInterfaceDecl>(context.m_decl_context))
Sean Callanand5c17ed2011-11-15 02:11:17 +0000541 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000542 FindObjCPropertyAndIvarDecls(context);
Sean Callanand5c17ed2011-11-15 02:11:17 +0000543 }
Sean Callananfb3e4302011-10-29 19:50:43 +0000544 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
545 {
546 // we shouldn't be getting FindExternalVisibleDecls calls for these
547 return;
548 }
549 else
550 {
551 ClangNamespaceDecl namespace_decl;
552
553 if (log)
554 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
555
556 FindExternalVisibleDecls(context,
557 lldb::ModuleSP(),
558 namespace_decl,
559 current_id);
560 }
561
562 if (!context.m_namespace_map->empty())
563 {
564 if (log && log->GetVerbose())
565 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
566 current_id,
567 context.m_namespace_map.get(),
568 (int)context.m_namespace_map->size());
569
570 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
571
572 if (clang_namespace_decl)
573 clang_namespace_decl->setHasExternalVisibleStorage();
574 }
575}
576
577void
578ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
579 lldb::ModuleSP module_sp,
580 ClangNamespaceDecl &namespace_decl,
581 unsigned int current_id)
582{
583 assert (m_ast_context);
584
585 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
586
587 SymbolContextList sc_list;
588
589 const ConstString name(context.m_decl_name.getAsString().c_str());
590
591 const char *name_unique_cstr = name.GetCString();
592
Sean Callanan5b26f272012-02-04 08:49:35 +0000593 static ConstString id_name("id");
594 static ConstString Class_name("Class");
595
596 if (name == id_name || name == Class_name)
597 return;
598
Sean Callananfb3e4302011-10-29 19:50:43 +0000599 if (name_unique_cstr == NULL)
600 return;
601
602 // The ClangASTSource is not responsible for finding $-names.
603 if (name_unique_cstr[0] == '$')
604 return;
605
606 if (module_sp && namespace_decl)
607 {
608 ClangNamespaceDecl found_namespace_decl;
609
610 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
611
612 if (symbol_vendor)
613 {
614 SymbolContext null_sc;
615
616 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
617
618 if (found_namespace_decl)
619 {
620 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
621
622 if (log)
623 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
624 current_id,
625 name.GetCString(),
626 module_sp->GetFileSpec().GetFilename().GetCString());
627 }
628 }
629 }
630 else
631 {
Enrico Granata17598482012-11-08 02:22:02 +0000632 const ModuleList &target_images = m_target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +0000633 Mutex::Locker modules_locker (target_images.GetMutex());
Sean Callananfb3e4302011-10-29 19:50:43 +0000634
Greg Claytonc7bece562013-01-25 18:06:21 +0000635 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i)
Sean Callananfb3e4302011-10-29 19:50:43 +0000636 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +0000637 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
Sean Callananfb3e4302011-10-29 19:50:43 +0000638
639 if (!image)
640 continue;
641
642 ClangNamespaceDecl found_namespace_decl;
643
644 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
645
646 if (!symbol_vendor)
647 continue;
648
649 SymbolContext null_sc;
650
651 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
652
653 if (found_namespace_decl)
654 {
655 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
656
657 if (log)
658 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
659 current_id,
660 name.GetCString(),
661 image->GetFileSpec().GetFilename().GetCString());
662 }
663 }
664 }
665
Sean Callananfb3e4302011-10-29 19:50:43 +0000666 do
667 {
668 TypeList types;
669 SymbolContext null_sc;
Greg Clayton84db9102012-03-26 23:03:23 +0000670 const bool exact_match = false;
Sean Callananbfb7c682011-12-19 19:38:39 +0000671
Sean Callananfb3e4302011-10-29 19:50:43 +0000672 if (module_sp && namespace_decl)
Greg Clayton84db9102012-03-26 23:03:23 +0000673 module_sp->FindTypesInNamespace(null_sc, name, &namespace_decl, 1, types);
Sean Callananbfb7c682011-12-19 19:38:39 +0000674 else
Greg Clayton29399a22012-04-06 17:41:13 +0000675 m_target->GetImages().FindTypes(null_sc, name, exact_match, 1, types);
Sean Callananfb3e4302011-10-29 19:50:43 +0000676
677 if (types.GetSize())
678 {
679 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
680
681 if (log)
682 {
683 const char *name_string = type_sp->GetName().GetCString();
684
685 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
686 current_id,
687 name.GetCString(),
688 (name_string ? name_string : "<anonymous>"));
689 }
Sean Callananbc47dfc2012-09-11 21:44:01 +0000690
691 clang::ASTContext *type_ast = type_sp->GetClangAST();
692 lldb::clang_type_t full_type = type_sp->GetClangFullType();
Sean Callanan0eed0d42011-12-06 03:41:14 +0000693
Sean Callananbc47dfc2012-09-11 21:44:01 +0000694 void *copied_type = GuardedCopyType(m_ast_context, type_ast, full_type);
Sean Callanan0eed0d42011-12-06 03:41:14 +0000695
Sean Callanane0a64f72011-12-01 21:04:37 +0000696 if (!copied_type)
697 {
698 if (log)
Sean Callanan7be70e82012-12-19 23:05:01 +0000699 log->Printf(" CAS::FEVD[%u] - Couldn't export a type",
Sean Callanan0eed0d42011-12-06 03:41:14 +0000700 current_id);
701
Sean Callanane0a64f72011-12-01 21:04:37 +0000702 break;
703 }
704
Sean Callananfb3e4302011-10-29 19:50:43 +0000705 context.AddTypeDecl(copied_type);
706 }
Sean Callanan7be70e82012-12-19 23:05:01 +0000707 else
708 {
709 do
710 {
711 // Couldn't find any types elsewhere. Try the Objective-C runtime if one exists.
712
713 lldb::ProcessSP process(m_target->GetProcessSP());
714
715 if (!process)
716 break;
717
718 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
719
720 if (!language_runtime)
721 break;
722
723 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
724
725 if (!type_vendor)
726 break;
727
728 bool append = false;
729 uint32_t max_matches = 1;
730 std::vector <ClangASTType> types;
731
732 if (!type_vendor->FindTypes(name,
733 append,
734 max_matches,
735 types))
736 break;
737
738 if (log)
739 {
740 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\" in the runtime",
741 current_id,
742 name.GetCString());
743 }
744
745 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
746
747 clang::QualType runtime_qual_type(runtime_clang_type, 0);
748
749 void *copied_type = GuardedCopyType(m_ast_context, type_vendor->GetClangASTContext(), runtime_qual_type.getAsOpaquePtr());
750
751 if (!copied_type)
752 {
753 if (log)
754 log->Printf(" CAS::FEVD[%u] - Couldn't export a type from the runtime",
755 current_id);
756
757 break;
758 }
759
760 context.AddTypeDecl(copied_type);
761 }
762 while(0);
763 }
Sean Callanan09ab4b72011-11-30 22:11:59 +0000764
Sean Callananfb3e4302011-10-29 19:50:43 +0000765 } while(0);
766}
767
Sean Callanan55400942012-11-02 17:09:58 +0000768template <class D> class TaggedASTDecl {
769public:
770 TaggedASTDecl() : decl(NULL) { }
771 TaggedASTDecl(D *_decl) : decl(_decl) { }
772 bool IsValid() const { return (decl != NULL); }
773 bool IsInvalid() const { return !IsValid(); }
774 D *operator->() const { return decl; }
775 D *decl;
776};
777
778template <class D2, template <class D> class TD, class D1>
779TD<D2>
780DynCast(TD<D1> source)
781{
782 return TD<D2> (dyn_cast<D2>(source.decl));
783}
784
785template <class D = Decl> class DeclFromParser;
786template <class D = Decl> class DeclFromUser;
787
788template <class D> class DeclFromParser : public TaggedASTDecl<D> {
789public:
790 DeclFromParser() : TaggedASTDecl<D>() { }
791 DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) { }
792
793 DeclFromUser<D> GetOrigin(ClangASTImporter *importer);
794};
795
796template <class D> class DeclFromUser : public TaggedASTDecl<D> {
797public:
798 DeclFromUser() : TaggedASTDecl<D>() { }
799 DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) { }
800
801 DeclFromParser<D> Import(ClangASTImporter *importer, ASTContext &dest_ctx);
802};
803
804template <class D>
805DeclFromUser<D>
806DeclFromParser<D>::GetOrigin(ClangASTImporter *importer)
807{
808 DeclFromUser <> origin_decl;
809 importer->ResolveDeclOrigin(this->decl, &origin_decl.decl, NULL);
810 if (origin_decl.IsInvalid())
811 return DeclFromUser<D>();
812 return DeclFromUser<D>(dyn_cast<D>(origin_decl.decl));
813}
814
815template <class D>
816DeclFromParser<D>
817DeclFromUser<D>::Import(ClangASTImporter *importer, ASTContext &dest_ctx)
818{
819 DeclFromParser <> parser_generic_decl(importer->CopyDecl(&dest_ctx, &this->decl->getASTContext(), this->decl));
820 if (parser_generic_decl.IsInvalid())
821 return DeclFromParser<D>();
822 return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
823}
824
Sean Callananc83e3412012-11-28 03:23:20 +0000825static bool
Sean Callananbc47dfc2012-09-11 21:44:01 +0000826FindObjCMethodDeclsWithOrigin (unsigned int current_id,
827 NameSearchContext &context,
828 ObjCInterfaceDecl *original_interface_decl,
829 clang::ASTContext *ast_context,
830 ClangASTImporter *ast_importer,
831 const char *log_info)
832{
833 const DeclarationName &decl_name(context.m_decl_name);
834 clang::ASTContext *original_ctx = &original_interface_decl->getASTContext();
835
836 Selector original_selector;
837
838 if (decl_name.isObjCZeroArgSelector())
839 {
840 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name.getAsString());
841 original_selector = original_ctx->Selectors.getSelector(0, &ident);
842 }
843 else if (decl_name.isObjCOneArgSelector())
844 {
845 const std::string &decl_name_string = decl_name.getAsString();
846 std::string decl_name_string_without_colon(decl_name_string.c_str(), decl_name_string.length() - 1);
847 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name_string_without_colon.c_str());
848 original_selector = original_ctx->Selectors.getSelector(1, &ident);
849 }
850 else
851 {
852 SmallVector<IdentifierInfo *, 4> idents;
853
854 clang::Selector sel = decl_name.getObjCSelector();
855
856 int num_args = sel.getNumArgs();
857
858 for (unsigned i = 0;
859 i != num_args;
860 ++i)
861 {
862 idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i)));
863 }
864
865 original_selector = original_ctx->Selectors.getSelector(num_args, idents.data());
866 }
867
868 DeclarationName original_decl_name(original_selector);
869
870 ObjCInterfaceDecl::lookup_result result = original_interface_decl->lookup(original_decl_name);
871
Sean Callanan5deaa4c2012-12-21 21:34:42 +0000872 if (result.empty())
Sean Callananc83e3412012-11-28 03:23:20 +0000873 return false;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000874
Sean Callanan5deaa4c2012-12-21 21:34:42 +0000875 if (!result[0])
Sean Callananc83e3412012-11-28 03:23:20 +0000876 return false;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000877
Sean Callanan5deaa4c2012-12-21 21:34:42 +0000878 ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(result[0]);
Sean Callananbc47dfc2012-09-11 21:44:01 +0000879
880 if (!result_method)
Sean Callananc83e3412012-11-28 03:23:20 +0000881 return false;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000882
883 Decl *copied_decl = ast_importer->CopyDecl(ast_context, &result_method->getASTContext(), result_method);
884
885 if (!copied_decl)
Sean Callananc83e3412012-11-28 03:23:20 +0000886 return false;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000887
888 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
889
890 if (!copied_method_decl)
Sean Callananc83e3412012-11-28 03:23:20 +0000891 return false;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000892
893 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
894
895 if (log)
896 {
897 ASTDumper dumper((Decl*)copied_method_decl);
898 log->Printf(" CAS::FOMD[%d] found (%s) %s", current_id, log_info, dumper.GetCString());
899 }
900
901 context.AddNamedDecl(copied_method_decl);
902
Sean Callananc83e3412012-11-28 03:23:20 +0000903 return true;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000904}
905
Sean Callanan0730e9c2011-11-09 19:33:21 +0000906void
907ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
908{
909 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
910
Sean Callanan46198ff2011-11-11 20:37:26 +0000911 static unsigned int invocation_id = 0;
912 unsigned int current_id = invocation_id++;
913
Sean Callanan0730e9c2011-11-09 19:33:21 +0000914 const DeclarationName &decl_name(context.m_decl_name);
915 const DeclContext *decl_ctx(context.m_decl_context);
916
917 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
918
919 if (!interface_decl)
920 return;
921
Sean Callanana6582262012-04-05 00:12:52 +0000922 do
923 {
924 Decl *original_decl = NULL;
925 ASTContext *original_ctx = NULL;
926
927 m_ast_importer->ResolveDeclOrigin(interface_decl, &original_decl, &original_ctx);
928
929 if (!original_decl)
930 break;
931
932 ObjCInterfaceDecl *original_interface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl);
933
Sean Callananc83e3412012-11-28 03:23:20 +0000934 if (FindObjCMethodDeclsWithOrigin(current_id,
935 context,
936 original_interface_decl,
937 m_ast_context,
938 m_ast_importer,
939 "at origin"))
940 return; // found it, no need to look any further
Sean Callanana6582262012-04-05 00:12:52 +0000941 } while (0);
942
Sean Callanan0730e9c2011-11-09 19:33:21 +0000943 StreamString ss;
Sean Callanan0eed0d42011-12-06 03:41:14 +0000944
Sean Callanan0730e9c2011-11-09 19:33:21 +0000945 if (decl_name.isObjCZeroArgSelector())
946 {
947 ss.Printf("%s", decl_name.getAsString().c_str());
948 }
949 else if (decl_name.isObjCOneArgSelector())
950 {
Sean Callanan4fb79b72011-11-14 18:29:46 +0000951 ss.Printf("%s", decl_name.getAsString().c_str());
Sean Callanan0730e9c2011-11-09 19:33:21 +0000952 }
953 else
954 {
955 clang::Selector sel = decl_name.getObjCSelector();
956
957 for (unsigned i = 0, e = sel.getNumArgs();
958 i != e;
959 ++i)
960 {
961 llvm::StringRef r = sel.getNameForSlot(i);
962 ss.Printf("%s:", r.str().c_str());
963 }
964 }
965 ss.Flush();
966
Sean Callanan46198ff2011-11-11 20:37:26 +0000967 ConstString selector_name(ss.GetData());
968
Sean Callanan0730e9c2011-11-09 19:33:21 +0000969 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000970 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
971 current_id,
972 m_ast_context,
Sean Callanan46198ff2011-11-11 20:37:26 +0000973 interface_decl->getNameAsString().c_str(),
974 selector_name.AsCString());
Sean Callanan46198ff2011-11-11 20:37:26 +0000975 SymbolContextList sc_list;
976
977 const bool include_symbols = false;
Sean Callanan9df05fb2012-02-10 22:52:19 +0000978 const bool include_inlines = false;
Sean Callanan46198ff2011-11-11 20:37:26 +0000979 const bool append = false;
980
Sean Callanana9bc0652012-01-19 02:17:40 +0000981 std::string interface_name = interface_decl->getNameAsString();
982
983 do
984 {
985 StreamString ms;
986 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());
987 ms.Flush();
988 ConstString instance_method_name(ms.GetData());
989
Sean Callanan9df05fb2012-02-10 22:52:19 +0000990 m_target->GetImages().FindFunctions(instance_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanana9bc0652012-01-19 02:17:40 +0000991
992 if (sc_list.GetSize())
993 break;
994
995 ms.Clear();
996 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());
997 ms.Flush();
998 ConstString class_method_name(ms.GetData());
999
Sean Callanan9df05fb2012-02-10 22:52:19 +00001000 m_target->GetImages().FindFunctions(class_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanana9bc0652012-01-19 02:17:40 +00001001
1002 if (sc_list.GetSize())
1003 break;
1004
1005 // Fall back and check for methods in categories. If we find methods this way, we need to check that they're actually in
1006 // categories on the desired class.
1007
1008 SymbolContextList candidate_sc_list;
1009
Sean Callanan9df05fb2012-02-10 22:52:19 +00001010 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, include_inlines, append, candidate_sc_list);
Sean Callanana9bc0652012-01-19 02:17:40 +00001011
1012 for (uint32_t ci = 0, ce = candidate_sc_list.GetSize();
1013 ci != ce;
1014 ++ci)
1015 {
1016 SymbolContext candidate_sc;
1017
1018 if (!candidate_sc_list.GetContextAtIndex(ci, candidate_sc))
1019 continue;
1020
1021 if (!candidate_sc.function)
1022 continue;
1023
1024 const char *candidate_name = candidate_sc.function->GetName().AsCString();
1025
1026 const char *cursor = candidate_name;
1027
1028 if (*cursor != '+' && *cursor != '-')
1029 continue;
1030
1031 ++cursor;
1032
1033 if (*cursor != '[')
1034 continue;
1035
1036 ++cursor;
1037
1038 size_t interface_len = interface_name.length();
1039
1040 if (strncmp(cursor, interface_name.c_str(), interface_len))
1041 continue;
1042
1043 cursor += interface_len;
1044
1045 if (*cursor == ' ' || *cursor == '(')
1046 sc_list.Append(candidate_sc);
1047 }
1048 }
1049 while (0);
Sean Callanan46198ff2011-11-11 20:37:26 +00001050
Sean Callananbc47dfc2012-09-11 21:44:01 +00001051 if (sc_list.GetSize())
Sean Callanan46198ff2011-11-11 20:37:26 +00001052 {
Sean Callananbc47dfc2012-09-11 21:44:01 +00001053 // We found a good function symbol. Use that.
Sean Callanan46198ff2011-11-11 20:37:26 +00001054
Sean Callananbc47dfc2012-09-11 21:44:01 +00001055 for (uint32_t i = 0, e = sc_list.GetSize();
1056 i != e;
1057 ++i)
Sean Callanan46198ff2011-11-11 20:37:26 +00001058 {
Sean Callananbc47dfc2012-09-11 21:44:01 +00001059 SymbolContext sc;
Sean Callanan46198ff2011-11-11 20:37:26 +00001060
Sean Callananbc47dfc2012-09-11 21:44:01 +00001061 if (!sc_list.GetContextAtIndex(i, sc))
Sean Callanan46198ff2011-11-11 20:37:26 +00001062 continue;
1063
Sean Callananbc47dfc2012-09-11 21:44:01 +00001064 if (!sc.function)
Sean Callanan46198ff2011-11-11 20:37:26 +00001065 continue;
1066
Sean Callananbc47dfc2012-09-11 21:44:01 +00001067 DeclContext *function_ctx = sc.function->GetClangDeclContext();
1068
1069 if (!function_ctx)
1070 continue;
1071
1072 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
1073
1074 if (!method_decl)
1075 continue;
1076
1077 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
1078
1079 if (!found_interface_decl)
1080 continue;
1081
1082 if (found_interface_decl->getName() == interface_decl->getName())
Sean Callanan46198ff2011-11-11 20:37:26 +00001083 {
Sean Callananbc47dfc2012-09-11 21:44:01 +00001084 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
1085
1086 if (!copied_decl)
1087 continue;
1088
1089 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
1090
1091 if (!copied_method_decl)
1092 continue;
1093
1094 if (log)
1095 {
1096 ASTDumper dumper((Decl*)copied_method_decl);
Sean Callanan55400942012-11-02 17:09:58 +00001097 log->Printf(" CAS::FOMD[%d] found (in symbols) %s", current_id, dumper.GetCString());
Sean Callananbc47dfc2012-09-11 21:44:01 +00001098 }
1099
1100 context.AddNamedDecl(copied_method_decl);
Sean Callanan46198ff2011-11-11 20:37:26 +00001101 }
Sean Callanan46198ff2011-11-11 20:37:26 +00001102 }
Sean Callanan55400942012-11-02 17:09:58 +00001103
1104 return;
Sean Callanan46198ff2011-11-11 20:37:26 +00001105 }
Sean Callanan55400942012-11-02 17:09:58 +00001106
1107 // Try the debug information.
1108
1109 do
Sean Callananbc47dfc2012-09-11 21:44:01 +00001110 {
Sean Callanan55400942012-11-02 17:09:58 +00001111 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(interface_decl));
1112
1113 if (!complete_interface_decl)
1114 break;
1115
1116 // We found the complete interface. The runtime never needs to be queried in this scenario.
1117
1118 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
1119
1120 if (complete_interface_decl == interface_decl)
1121 break; // already checked this one
1122
1123 if (log)
1124 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1125 current_id,
1126 complete_interface_decl,
1127 &complete_iface_decl->getASTContext());
1128
1129 FindObjCMethodDeclsWithOrigin(current_id,
1130 context,
1131 complete_interface_decl,
1132 m_ast_context,
1133 m_ast_importer,
1134 "in debug info");
1135
1136 return;
Sean Callananbc47dfc2012-09-11 21:44:01 +00001137 }
Sean Callanan55400942012-11-02 17:09:58 +00001138 while (0);
Sean Callanan5b26f272012-02-04 08:49:35 +00001139
Sean Callanan55400942012-11-02 17:09:58 +00001140 do
1141 {
1142 // Check the runtime only if the debug information didn't have a complete interface.
1143
1144 lldb::ProcessSP process(m_target->GetProcessSP());
1145
1146 if (!process)
1147 break;
1148
1149 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1150
Sean Callanan7be70e82012-12-19 23:05:01 +00001151 if (!language_runtime)
1152 break;
1153
Sean Callanan55400942012-11-02 17:09:58 +00001154 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
1155
1156 if (!type_vendor)
1157 break;
1158
1159 ConstString interface_name(interface_decl->getNameAsString().c_str());
1160 bool append = false;
1161 uint32_t max_matches = 1;
1162 std::vector <ClangASTType> types;
1163
1164 if (!type_vendor->FindTypes(interface_name,
1165 append,
1166 max_matches,
1167 types))
1168 break;
1169
1170 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
1171
1172 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
1173
1174 if (!runtime_interface_type)
1175 break;
1176
1177 ObjCInterfaceDecl *runtime_interface_decl = runtime_interface_type->getDecl();
1178
1179 FindObjCMethodDeclsWithOrigin(current_id,
1180 context,
1181 runtime_interface_decl,
1182 m_ast_context,
1183 m_ast_importer,
1184 "in runtime");
1185 }
1186 while(0);
Sean Callanan5b26f272012-02-04 08:49:35 +00001187}
1188
Sean Callanan72772842012-02-22 23:57:45 +00001189static bool
1190FindObjCPropertyAndIvarDeclsWithOrigin (unsigned int current_id,
1191 NameSearchContext &context,
1192 clang::ASTContext &ast_context,
1193 ClangASTImporter *ast_importer,
1194 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl)
1195{
1196 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1197
1198 if (origin_iface_decl.IsInvalid())
1199 return false;
1200
1201 std::string name_str = context.m_decl_name.getAsString();
1202 StringRef name(name_str.c_str());
1203 IdentifierInfo &name_identifier(origin_iface_decl->getASTContext().Idents.get(name));
1204
1205 DeclFromUser<ObjCPropertyDecl> origin_property_decl(origin_iface_decl->FindPropertyDeclaration(&name_identifier));
1206
1207 bool found = false;
1208
1209 if (origin_property_decl.IsValid())
1210 {
1211 DeclFromParser<ObjCPropertyDecl> parser_property_decl(origin_property_decl.Import(ast_importer, ast_context));
1212 if (parser_property_decl.IsValid())
1213 {
1214 if (log)
1215 {
1216 ASTDumper dumper((Decl*)parser_property_decl.decl);
1217 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1218 }
1219
1220 context.AddNamedDecl(parser_property_decl.decl);
1221 found = true;
1222 }
1223 }
1224
1225 DeclFromUser<ObjCIvarDecl> origin_ivar_decl(origin_iface_decl->getIvarDecl(&name_identifier));
1226
1227 if (origin_ivar_decl.IsValid())
1228 {
1229 DeclFromParser<ObjCIvarDecl> parser_ivar_decl(origin_ivar_decl.Import(ast_importer, ast_context));
1230 if (parser_ivar_decl.IsValid())
1231 {
1232 if (log)
1233 {
1234 ASTDumper dumper((Decl*)parser_ivar_decl.decl);
1235 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1236 }
1237
1238 context.AddNamedDecl(parser_ivar_decl.decl);
1239 found = true;
1240 }
1241 }
1242
1243 return found;
1244}
1245
Sean Callanand5c17ed2011-11-15 02:11:17 +00001246void
Sean Callanan5b26f272012-02-04 08:49:35 +00001247ClangASTSource::FindObjCPropertyAndIvarDecls (NameSearchContext &context)
Sean Callanand5c17ed2011-11-15 02:11:17 +00001248{
1249 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1250
1251 static unsigned int invocation_id = 0;
1252 unsigned int current_id = invocation_id++;
1253
Sean Callanan5b26f272012-02-04 08:49:35 +00001254 DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl(cast<ObjCInterfaceDecl>(context.m_decl_context));
1255 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(parser_iface_decl.GetOrigin(m_ast_importer));
Sean Callanan72772842012-02-22 23:57:45 +00001256
1257 ConstString class_name(parser_iface_decl->getNameAsString().c_str());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001258
1259 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +00001260 log->Printf("ClangASTSource::FindObjCPropertyAndIvarDecls[%d] on (ASTContext*)%p for '%s.%s'",
Sean Callanand5c17ed2011-11-15 02:11:17 +00001261 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +00001262 m_ast_context,
Sean Callanan5b26f272012-02-04 08:49:35 +00001263 parser_iface_decl->getNameAsString().c_str(),
Sean Callanan72772842012-02-22 23:57:45 +00001264 context.m_decl_name.getAsString().c_str());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001265
Sean Callanan72772842012-02-22 23:57:45 +00001266 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1267 context,
1268 *m_ast_context,
1269 m_ast_importer,
1270 origin_iface_decl))
1271 return;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001272
Sean Callanan72772842012-02-22 23:57:45 +00001273 if (log)
1274 log->Printf("CAS::FOPD[%d] couldn't find the property on origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p, searching elsewhere...",
1275 current_id,
1276 origin_iface_decl.decl,
1277 &origin_iface_decl->getASTContext());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001278
Sean Callanan72772842012-02-22 23:57:45 +00001279 SymbolContext null_sc;
1280 TypeList type_list;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001281
Sean Callananbc47dfc2012-09-11 21:44:01 +00001282 do
1283 {
Sean Callanan2cb5e522012-09-20 23:21:16 +00001284 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(parser_iface_decl.decl));
Sean Callananbc47dfc2012-09-11 21:44:01 +00001285
Sean Callanan2cb5e522012-09-20 23:21:16 +00001286 if (!complete_interface_decl)
Sean Callananbc47dfc2012-09-11 21:44:01 +00001287 break;
1288
Sean Callanan55400942012-11-02 17:09:58 +00001289 // We found the complete interface. The runtime never needs to be queried in this scenario.
1290
Sean Callanan2cb5e522012-09-20 23:21:16 +00001291 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
Sean Callananbc47dfc2012-09-11 21:44:01 +00001292
1293 if (complete_iface_decl.decl == origin_iface_decl.decl)
1294 break; // already checked this one
1295
1296 if (log)
1297 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1298 current_id,
1299 complete_iface_decl.decl,
1300 &complete_iface_decl->getASTContext());
1301
Sean Callanan55400942012-11-02 17:09:58 +00001302 FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1303 context,
1304 *m_ast_context,
1305 m_ast_importer,
1306 complete_iface_decl);
1307
1308 return;
Sean Callananbc47dfc2012-09-11 21:44:01 +00001309 }
1310 while(0);
Sean Callanan72772842012-02-22 23:57:45 +00001311
Sean Callananbc47dfc2012-09-11 21:44:01 +00001312 do
1313 {
Sean Callanan55400942012-11-02 17:09:58 +00001314 // Check the runtime only if the debug information didn't have a complete interface.
1315
1316 lldb::ProcessSP process(m_target->GetProcessSP());
1317
1318 if (!process)
1319 return;
1320
1321 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1322
1323 if (!language_runtime)
1324 return;
Sean Callananbc47dfc2012-09-11 21:44:01 +00001325
1326 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
1327
1328 if (!type_vendor)
1329 break;
1330
1331 bool append = false;
1332 uint32_t max_matches = 1;
1333 std::vector <ClangASTType> types;
1334
1335 if (!type_vendor->FindTypes(class_name,
1336 append,
1337 max_matches,
1338 types))
1339 break;
1340
1341 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
1342
1343 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
1344
1345 if (!runtime_interface_type)
1346 break;
1347
1348 DeclFromUser<const ObjCInterfaceDecl> runtime_iface_decl(runtime_interface_type->getDecl());
1349
1350 if (log)
1351 log->Printf("CAS::FOPD[%d] trying runtime (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1352 current_id,
1353 runtime_iface_decl.decl,
1354 &runtime_iface_decl->getASTContext());
1355
1356 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1357 context,
1358 *m_ast_context,
1359 m_ast_importer,
1360 runtime_iface_decl))
1361 return;
1362 }
1363 while(0);
Sean Callanan5b26f272012-02-04 08:49:35 +00001364}
1365
1366typedef llvm::DenseMap <const FieldDecl *, uint64_t> FieldOffsetMap;
1367typedef llvm::DenseMap <const CXXRecordDecl *, CharUnits> BaseOffsetMap;
1368
1369template <class D, class O>
1370static bool
1371ImportOffsetMap (llvm::DenseMap <const D*, O> &destination_map,
1372 llvm::DenseMap <const D*, O> &source_map,
1373 ClangASTImporter *importer,
1374 ASTContext &dest_ctx)
1375{
1376 typedef llvm::DenseMap <const D*, O> MapType;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001377
Sean Callanan5b26f272012-02-04 08:49:35 +00001378 for (typename MapType::iterator fi = source_map.begin(), fe = source_map.end();
1379 fi != fe;
1380 ++fi)
1381 {
1382 DeclFromUser <D> user_decl(const_cast<D*>(fi->first));
1383 DeclFromParser <D> parser_decl(user_decl.Import(importer, dest_ctx));
1384 if (parser_decl.IsInvalid())
1385 return false;
1386 destination_map.insert(std::pair<const D *, O>(parser_decl.decl, fi->second));
1387 }
1388
1389 return true;
1390}
1391
1392template <bool IsVirtual> bool ExtractBaseOffsets (const ASTRecordLayout &record_layout,
1393 DeclFromUser<const CXXRecordDecl> &record,
1394 BaseOffsetMap &base_offsets)
1395{
1396 for (CXXRecordDecl::base_class_const_iterator
1397 bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()),
1398 be = (IsVirtual ? record->vbases_end() : record->bases_end());
1399 bi != be;
1400 ++bi)
1401 {
1402 if (!IsVirtual && bi->isVirtual())
1403 continue;
1404
1405 const clang::Type *origin_base_type = bi->getType().getTypePtr();
1406 const clang::RecordType *origin_base_record_type = origin_base_type->getAs<RecordType>();
1407
1408 if (!origin_base_record_type)
1409 return false;
1410
1411 DeclFromUser <RecordDecl> origin_base_record(origin_base_record_type->getDecl());
1412
1413 if (origin_base_record.IsInvalid())
1414 return false;
1415
1416 DeclFromUser <CXXRecordDecl> origin_base_cxx_record(DynCast<CXXRecordDecl>(origin_base_record));
1417
1418 if (origin_base_cxx_record.IsInvalid())
1419 return false;
1420
1421 CharUnits base_offset;
1422
1423 if (IsVirtual)
1424 base_offset = record_layout.getVBaseClassOffset(origin_base_cxx_record.decl);
1425 else
1426 base_offset = record_layout.getBaseClassOffset(origin_base_cxx_record.decl);
1427
1428 base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(origin_base_cxx_record.decl, base_offset));
1429 }
1430
1431 return true;
1432}
1433
1434bool
1435ClangASTSource::layoutRecordType(const RecordDecl *record,
1436 uint64_t &size,
1437 uint64_t &alignment,
1438 FieldOffsetMap &field_offsets,
1439 BaseOffsetMap &base_offsets,
1440 BaseOffsetMap &virtual_base_offsets)
1441{
Sean Callanan8106d802013-03-08 20:04:57 +00001442 ClangASTMetrics::RegisterRecordLayout();
1443
Sean Callanan5b26f272012-02-04 08:49:35 +00001444 static unsigned int invocation_id = 0;
1445 unsigned int current_id = invocation_id++;
1446
1447 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1448
Sean Callanand5c17ed2011-11-15 02:11:17 +00001449 if (log)
1450 {
Sean Callanan933ca2e2013-02-28 03:12:58 +00001451 log->Printf("LayoutRecordType[%u] on (ASTContext*)%p for (RecordDecl*)%p [name = '%s']",
Sean Callanan5b26f272012-02-04 08:49:35 +00001452 current_id,
1453 m_ast_context,
Sean Callanan933ca2e2013-02-28 03:12:58 +00001454 record,
Sean Callanan5b26f272012-02-04 08:49:35 +00001455 record->getNameAsString().c_str());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001456 }
Sean Callanan5b26f272012-02-04 08:49:35 +00001457
1458
1459 DeclFromParser <const RecordDecl> parser_record(record);
1460 DeclFromUser <const RecordDecl> origin_record(parser_record.GetOrigin(m_ast_importer));
1461
1462 if (origin_record.IsInvalid())
1463 return false;
1464
1465 FieldOffsetMap origin_field_offsets;
1466 BaseOffsetMap origin_base_offsets;
1467 BaseOffsetMap origin_virtual_base_offsets;
1468
Sean Callananbf81ea52012-04-07 00:06:00 +00001469 ClangASTContext::GetCompleteDecl(&origin_record->getASTContext(), const_cast<RecordDecl*>(origin_record.decl));
1470
1471 if (!origin_record.decl->getDefinition())
1472 return false;
1473
Sean Callanan5b26f272012-02-04 08:49:35 +00001474 const ASTRecordLayout &record_layout(origin_record->getASTContext().getASTRecordLayout(origin_record.decl));
1475
1476 int field_idx = 0;
1477
1478 for (RecordDecl::field_iterator fi = origin_record->field_begin(), fe = origin_record->field_end();
1479 fi != fe;
1480 ++fi)
1481 {
1482 uint64_t field_offset = record_layout.getFieldOffset(field_idx);
1483
1484 origin_field_offsets.insert(std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
1485
1486 field_idx++;
1487 }
1488
1489 ASTContext &parser_ast_context(record->getASTContext());
1490
1491 DeclFromUser <const CXXRecordDecl> origin_cxx_record(DynCast<const CXXRecordDecl>(origin_record));
1492
1493 if (origin_cxx_record.IsValid())
1494 {
1495 if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record, origin_base_offsets) ||
1496 !ExtractBaseOffsets<true>(record_layout, origin_cxx_record, origin_virtual_base_offsets))
1497 return false;
1498 }
1499
1500 if (!ImportOffsetMap(field_offsets, origin_field_offsets, m_ast_importer, parser_ast_context) ||
1501 !ImportOffsetMap(base_offsets, origin_base_offsets, m_ast_importer, parser_ast_context) ||
1502 !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets, m_ast_importer, parser_ast_context))
1503 return false;
1504
1505 size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth();
1506 alignment = record_layout.getAlignment().getQuantity() * m_ast_context->getCharWidth();
1507
1508 if (log)
1509 {
1510 log->Printf("LRT[%u] returned:", current_id);
1511 log->Printf("LRT[%u] Original = (RecordDecl*)%p", current_id, origin_record.decl);
Daniel Malead01b2952012-11-29 21:49:15 +00001512 log->Printf("LRT[%u] Size = %" PRId64, current_id, size);
1513 log->Printf("LRT[%u] Alignment = %" PRId64, current_id, alignment);
Sean Callanan5b26f272012-02-04 08:49:35 +00001514 log->Printf("LRT[%u] Fields:", current_id);
1515 for (RecordDecl::field_iterator fi = record->field_begin(), fe = record->field_end();
1516 fi != fe;
1517 ++fi)
1518 {
Daniel Malead01b2952012-11-29 21:49:15 +00001519 log->Printf("LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %" PRId64 " bits",
Sean Callanan5b26f272012-02-04 08:49:35 +00001520 current_id,
1521 *fi,
1522 fi->getNameAsString().c_str(),
1523 field_offsets[*fi]);
1524 }
1525 DeclFromParser <const CXXRecordDecl> parser_cxx_record = DynCast<const CXXRecordDecl>(parser_record);
1526 if (parser_cxx_record.IsValid())
1527 {
1528 log->Printf("LRT[%u] Bases:", current_id);
1529 for (CXXRecordDecl::base_class_const_iterator bi = parser_cxx_record->bases_begin(), be = parser_cxx_record->bases_end();
1530 bi != be;
1531 ++bi)
1532 {
1533 bool is_virtual = bi->isVirtual();
1534
1535 QualType base_type = bi->getType();
1536 const RecordType *base_record_type = base_type->getAs<RecordType>();
1537 DeclFromParser <RecordDecl> base_record(base_record_type->getDecl());
1538 DeclFromParser <CXXRecordDecl> base_cxx_record = DynCast<CXXRecordDecl>(base_record);
1539
Daniel Malead01b2952012-11-29 21:49:15 +00001540 log->Printf("LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %" PRId64 " chars",
Sean Callanan5b26f272012-02-04 08:49:35 +00001541 current_id,
1542 (is_virtual ? "Virtual " : ""),
1543 base_cxx_record.decl,
1544 base_cxx_record.decl->getNameAsString().c_str(),
1545 (is_virtual ? virtual_base_offsets[base_cxx_record.decl].getQuantity() :
1546 base_offsets[base_cxx_record.decl].getQuantity()));
1547 }
1548 }
1549 else
1550 {
1551 log->Printf("LRD[%u] Not a CXXRecord, so no bases", current_id);
1552 }
1553 }
1554
1555 return true;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001556}
1557
Sean Callanan1ee44b72011-10-29 01:58:46 +00001558void
1559ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
1560 const ConstString &name,
1561 ClangASTImporter::NamespaceMapSP &parent_map) const
1562{
1563 static unsigned int invocation_id = 0;
1564 unsigned int current_id = invocation_id++;
1565
1566 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1567
1568 if (log)
1569 {
1570 if (parent_map && parent_map->size())
Sean Callanan00f43622011-11-18 03:28:09 +00001571 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
Sean Callanan1ee44b72011-10-29 01:58:46 +00001572 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +00001573 m_ast_context,
Sean Callanan1ee44b72011-10-29 01:58:46 +00001574 name.GetCString(),
1575 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
1576 else
Sean Callanan00f43622011-11-18 03:28:09 +00001577 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
Sean Callanan1ee44b72011-10-29 01:58:46 +00001578 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +00001579 m_ast_context,
Sean Callanan1ee44b72011-10-29 01:58:46 +00001580 name.GetCString());
1581 }
1582
1583
1584 if (parent_map)
1585 {
1586 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
1587 i != e;
1588 ++i)
1589 {
1590 ClangNamespaceDecl found_namespace_decl;
1591
1592 lldb::ModuleSP module_sp = i->first;
1593 ClangNamespaceDecl module_parent_namespace_decl = i->second;
1594
1595 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
1596
1597 if (!symbol_vendor)
1598 continue;
1599
1600 SymbolContext null_sc;
1601
1602 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
1603
1604 if (!found_namespace_decl)
1605 continue;
1606
1607 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
1608
1609 if (log)
1610 log->Printf(" CMN[%u] Found namespace %s in module %s",
1611 current_id,
1612 name.GetCString(),
1613 module_sp->GetFileSpec().GetFilename().GetCString());
1614 }
1615 }
1616 else
1617 {
Enrico Granata17598482012-11-08 02:22:02 +00001618 const ModuleList &target_images = m_target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00001619 Mutex::Locker modules_locker(target_images.GetMutex());
1620
Sean Callanan1ee44b72011-10-29 01:58:46 +00001621 ClangNamespaceDecl null_namespace_decl;
1622
Greg Claytonc7bece562013-01-25 18:06:21 +00001623 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i)
Sean Callanan1ee44b72011-10-29 01:58:46 +00001624 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00001625 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
Sean Callanan1ee44b72011-10-29 01:58:46 +00001626
1627 if (!image)
1628 continue;
1629
1630 ClangNamespaceDecl found_namespace_decl;
1631
1632 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
1633
1634 if (!symbol_vendor)
1635 continue;
1636
1637 SymbolContext null_sc;
1638
1639 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
1640
1641 if (!found_namespace_decl)
1642 continue;
1643
1644 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
1645
1646 if (log)
1647 log->Printf(" CMN[%u] Found namespace %s in module %s",
1648 current_id,
1649 name.GetCString(),
1650 image->GetFileSpec().GetFilename().GetCString());
1651 }
1652 }
1653}
1654
Sean Callananba0aca72011-10-29 02:28:18 +00001655NamespaceDecl *
1656ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
1657{
Greg Claytone1cd1be2012-01-29 20:56:30 +00001658 if (!namespace_decls)
Sean Callananba0aca72011-10-29 02:28:18 +00001659 return NULL;
1660
1661 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1662
1663 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
1664
Sean Callanan686b2312011-11-16 18:20:47 +00001665 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
Sean Callananba0aca72011-10-29 02:28:18 +00001666
Sean Callanan61b33f72012-03-20 21:11:12 +00001667 if (!copied_decl)
1668 return NULL;
Sean Callananeeffea42013-02-12 08:01:13 +00001669
Sean Callananba0aca72011-10-29 02:28:18 +00001670 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1671
Sean Callananeeffea42013-02-12 08:01:13 +00001672 if (!copied_namespace_decl)
1673 return NULL;
1674
1675 context.m_decls.push_back(copied_namespace_decl);
1676
Sean Callananba0aca72011-10-29 02:28:18 +00001677 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
1678
1679 return dyn_cast<NamespaceDecl>(copied_decl);
1680}
1681
Sean Callananfb3e4302011-10-29 19:50:43 +00001682void *
1683ClangASTSource::GuardedCopyType (ASTContext *dest_context,
1684 ASTContext *source_context,
1685 void *clang_type)
Sean Callanan8106d802013-03-08 20:04:57 +00001686{
1687 ClangASTMetrics::RegisterLLDBImport();
1688
Sean Callananfb3e4302011-10-29 19:50:43 +00001689 SetImportInProgress(true);
1690
Sean Callanan686b2312011-11-16 18:20:47 +00001691 QualType ret_qual_type = m_ast_importer->CopyType (m_ast_context, source_context, QualType::getFromOpaquePtr(clang_type));
Sean Callananfb3e4302011-10-29 19:50:43 +00001692
1693 void *ret = ret_qual_type.getAsOpaquePtr();
1694
1695 SetImportInProgress(false);
1696
Sean Callanan61b33f72012-03-20 21:11:12 +00001697 if (ret && ret_qual_type->getCanonicalTypeInternal().isNull())
Sean Callanan8b665982012-02-27 19:22:39 +00001698 // this shouldn't happen, but we're hardening because the AST importer seems to be generating bad types
1699 // on occasion.
1700 return NULL;
1701
Sean Callananfb3e4302011-10-29 19:50:43 +00001702 return ret;
1703}
1704
Greg Clayton6beaaa62011-01-17 03:46:26 +00001705clang::NamedDecl *
1706NameSearchContext::AddVarDecl(void *type)
1707{
Greg Clayton7b462cc2010-10-15 22:48:33 +00001708 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan348b5892010-11-30 00:27:43 +00001709
1710 assert (type && "Type for variable must be non-NULL!");
Sean Callanan44096b12010-09-14 21:59:34 +00001711
Sean Callananeddeb3b2011-10-28 23:38:38 +00001712 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001713 const_cast<DeclContext*>(m_decl_context),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001714 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00001715 SourceLocation(),
Sean Callanan44096b12010-09-14 21:59:34 +00001716 ii,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001717 QualType::getFromOpaquePtr(type),
1718 0,
Sean Callanane2ef6e32010-09-23 03:01:22 +00001719 SC_Static,
1720 SC_Static);
Greg Clayton7b462cc2010-10-15 22:48:33 +00001721 m_decls.push_back(Decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001722
1723 return Decl;
1724}
Sean Callanan468574b2010-06-22 23:46:24 +00001725
Greg Clayton6beaaa62011-01-17 03:46:26 +00001726clang::NamedDecl *
1727NameSearchContext::AddFunDecl (void *type)
1728{
Sean Callanane2d47482012-03-21 17:13:20 +00001729 assert (type && "Type for variable must be non-NULL!");
1730
Sean Callananeddeb3b2011-10-28 23:38:38 +00001731 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001732 const_cast<DeclContext*>(m_decl_context),
1733 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00001734 SourceLocation(),
Greg Clayton7b462cc2010-10-15 22:48:33 +00001735 m_decl_name.getAsIdentifierInfo(),
1736 QualType::getFromOpaquePtr(type),
1737 NULL,
1738 SC_Static,
1739 SC_Static,
1740 false,
1741 true);
Sean Callanan468574b2010-06-22 23:46:24 +00001742
Sean Callanan04949cc2010-08-12 23:45:38 +00001743 // We have to do more than just synthesize the FunctionDecl. We have to
1744 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
1745 // this, we raid the function's FunctionProtoType for types.
1746
Greg Clayton7b462cc2010-10-15 22:48:33 +00001747 QualType qual_type (QualType::getFromOpaquePtr(type));
Sean Callananfc4f2fb2011-12-14 01:13:04 +00001748 const FunctionProtoType *func_proto_type = qual_type.getTypePtr()->getAs<FunctionProtoType>();
Sean Callanan468574b2010-06-22 23:46:24 +00001749
Greg Clayton7b462cc2010-10-15 22:48:33 +00001750 if (func_proto_type)
Sean Callanand448c1b2010-06-23 18:58:10 +00001751 {
Greg Clayton7b462cc2010-10-15 22:48:33 +00001752 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan468574b2010-06-22 23:46:24 +00001753 unsigned ArgIndex;
1754
Sean Callanan880e6802011-10-07 23:18:13 +00001755 SmallVector<ParmVarDecl *, 5> parm_var_decls;
1756
Sean Callanan468574b2010-06-22 23:46:24 +00001757 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
1758 {
Greg Clayton7b462cc2010-10-15 22:48:33 +00001759 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan468574b2010-06-22 23:46:24 +00001760
Sean Callananeddeb3b2011-10-28 23:38:38 +00001761 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callanan880e6802011-10-07 23:18:13 +00001762 const_cast<DeclContext*>(m_decl_context),
1763 SourceLocation(),
1764 SourceLocation(),
1765 NULL,
1766 arg_qual_type,
1767 NULL,
1768 SC_Static,
1769 SC_Static,
1770 NULL));
Sean Callanan468574b2010-06-22 23:46:24 +00001771 }
1772
Sean Callanan880e6802011-10-07 23:18:13 +00001773 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan468574b2010-06-22 23:46:24 +00001774 }
Sean Callanane0a64f72011-12-01 21:04:37 +00001775 else
1776 {
1777 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1778
1779 log->Printf("Function type wasn't a FunctionProtoType");
1780 }
Sean Callanan468574b2010-06-22 23:46:24 +00001781
Greg Clayton7b462cc2010-10-15 22:48:33 +00001782 m_decls.push_back(func_decl);
Sean Callanan468574b2010-06-22 23:46:24 +00001783
Greg Clayton7b462cc2010-10-15 22:48:33 +00001784 return func_decl;
Sean Callanan468574b2010-06-22 23:46:24 +00001785}
Sean Callanan8ade1042010-07-27 00:55:47 +00001786
Greg Clayton6beaaa62011-01-17 03:46:26 +00001787clang::NamedDecl *
1788NameSearchContext::AddGenericFunDecl()
Sean Callanan8ade1042010-07-27 00:55:47 +00001789{
Sean Callanan2c777c42011-01-18 23:32:05 +00001790 FunctionProtoType::ExtProtoInfo proto_info;
1791
1792 proto_info.Variadic = true;
1793
Sean Callananeddeb3b2011-10-28 23:38:38 +00001794 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
Sylvestre Ledru1573b1b2013-03-10 20:13:16 +00001795 ArrayRef<QualType>(), // argument types
Sean Callananeddeb3b2011-10-28 23:38:38 +00001796 proto_info));
Greg Clayton7b462cc2010-10-15 22:48:33 +00001797
Sean Callanan8ade1042010-07-27 00:55:47 +00001798 return AddFunDecl(generic_function_type.getAsOpaquePtr());
1799}
Sean Callanan5666b672010-08-04 01:02:13 +00001800
Greg Clayton6beaaa62011-01-17 03:46:26 +00001801clang::NamedDecl *
1802NameSearchContext::AddTypeDecl(void *type)
Sean Callanan5666b672010-08-04 01:02:13 +00001803{
Greg Clayton1b03cb52011-01-23 00:34:52 +00001804 if (type)
1805 {
1806 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan5666b672010-08-04 01:02:13 +00001807
Sean Callananfc4f2fb2011-12-14 01:13:04 +00001808 if (const TagType *tag_type = qual_type->getAs<TagType>())
Greg Clayton1b03cb52011-01-23 00:34:52 +00001809 {
1810 TagDecl *tag_decl = tag_type->getDecl();
1811
1812 m_decls.push_back(tag_decl);
1813
1814 return tag_decl;
1815 }
Sean Callananfc4f2fb2011-12-14 01:13:04 +00001816 else if (const ObjCObjectType *objc_object_type = qual_type->getAs<ObjCObjectType>())
Greg Clayton1b03cb52011-01-23 00:34:52 +00001817 {
1818 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
1819
1820 m_decls.push_back((NamedDecl*)interface_decl);
1821
1822 return (NamedDecl*)interface_decl;
1823 }
Sean Callananc4b1ab42013-03-14 17:21:53 +00001824 else if (const TypedefType *typedef_type = qual_type->getAs<TypedefType>())
1825 {
1826 TypedefNameDecl *typedef_name_decl = typedef_type->getDecl();
1827
1828 m_decls.push_back(typedef_name_decl);
1829
1830 return (NamedDecl*)typedef_name_decl;
1831 }
Sean Callanan5666b672010-08-04 01:02:13 +00001832 }
Greg Clayton1b03cb52011-01-23 00:34:52 +00001833 return NULL;
Sean Callanan5666b672010-08-04 01:02:13 +00001834}
Greg Claytona2721472011-06-25 00:44:06 +00001835
1836void
1837NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
1838{
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001839 for (clang::NamedDecl *decl : result)
1840 m_decls.push_back (decl);
Greg Claytona2721472011-06-25 00:44:06 +00001841}
1842
1843void
1844NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
1845{
1846 m_decls.push_back (decl);
1847}