blob: a508c48d4494c8c290da7a824512fb12671296d1 [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
Sean Callanan6b200d02013-03-21 22:15:41 +0000471 m_ast_importer->RequireCompleteType(copied_field_type);
Sean Callanancf128622012-03-15 01:53:17 +0000472 }
473
Sean Callananba0aca72011-10-29 02:28:18 +0000474 decls.push_back(copied_decl);
475 }
476 }
477
478 return ELR_AlreadyLoaded;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479}
480
Sean Callananfb3e4302011-10-29 19:50:43 +0000481void
482ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
483{
484 assert (m_ast_context);
485
Sean Callanan8106d802013-03-08 20:04:57 +0000486 ClangASTMetrics::RegisterVisibleQuery();
487
Sean Callananfb3e4302011-10-29 19:50:43 +0000488 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{
Sean Callanan8106d802013-03-08 20:04:57 +00001438 ClangASTMetrics::RegisterRecordLayout();
1439
Sean Callanan5b26f272012-02-04 08:49:35 +00001440 static unsigned int invocation_id = 0;
1441 unsigned int current_id = invocation_id++;
1442
1443 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1444
Sean Callanand5c17ed2011-11-15 02:11:17 +00001445 if (log)
1446 {
Sean Callanan933ca2e2013-02-28 03:12:58 +00001447 log->Printf("LayoutRecordType[%u] on (ASTContext*)%p for (RecordDecl*)%p [name = '%s']",
Sean Callanan5b26f272012-02-04 08:49:35 +00001448 current_id,
1449 m_ast_context,
Sean Callanan933ca2e2013-02-28 03:12:58 +00001450 record,
Sean Callanan5b26f272012-02-04 08:49:35 +00001451 record->getNameAsString().c_str());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001452 }
Sean Callanan5b26f272012-02-04 08:49:35 +00001453
1454
1455 DeclFromParser <const RecordDecl> parser_record(record);
1456 DeclFromUser <const RecordDecl> origin_record(parser_record.GetOrigin(m_ast_importer));
1457
1458 if (origin_record.IsInvalid())
1459 return false;
1460
1461 FieldOffsetMap origin_field_offsets;
1462 BaseOffsetMap origin_base_offsets;
1463 BaseOffsetMap origin_virtual_base_offsets;
1464
Sean Callananbf81ea52012-04-07 00:06:00 +00001465 ClangASTContext::GetCompleteDecl(&origin_record->getASTContext(), const_cast<RecordDecl*>(origin_record.decl));
1466
1467 if (!origin_record.decl->getDefinition())
1468 return false;
1469
Sean Callanan5b26f272012-02-04 08:49:35 +00001470 const ASTRecordLayout &record_layout(origin_record->getASTContext().getASTRecordLayout(origin_record.decl));
1471
Sean Callananeb7b27d2013-03-25 18:27:07 +00001472 int field_idx = 0, field_count = record_layout.getFieldCount();
Sean Callanan5b26f272012-02-04 08:49:35 +00001473
1474 for (RecordDecl::field_iterator fi = origin_record->field_begin(), fe = origin_record->field_end();
1475 fi != fe;
1476 ++fi)
1477 {
Sean Callananeb7b27d2013-03-25 18:27:07 +00001478 if (field_idx >= field_count)
1479 return false; // Layout didn't go well. Bail out.
1480
Sean Callanan5b26f272012-02-04 08:49:35 +00001481 uint64_t field_offset = record_layout.getFieldOffset(field_idx);
1482
1483 origin_field_offsets.insert(std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
1484
1485 field_idx++;
1486 }
1487
1488 ASTContext &parser_ast_context(record->getASTContext());
1489
1490 DeclFromUser <const CXXRecordDecl> origin_cxx_record(DynCast<const CXXRecordDecl>(origin_record));
1491
1492 if (origin_cxx_record.IsValid())
1493 {
1494 if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record, origin_base_offsets) ||
1495 !ExtractBaseOffsets<true>(record_layout, origin_cxx_record, origin_virtual_base_offsets))
1496 return false;
1497 }
1498
1499 if (!ImportOffsetMap(field_offsets, origin_field_offsets, m_ast_importer, parser_ast_context) ||
1500 !ImportOffsetMap(base_offsets, origin_base_offsets, m_ast_importer, parser_ast_context) ||
1501 !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets, m_ast_importer, parser_ast_context))
1502 return false;
1503
1504 size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth();
1505 alignment = record_layout.getAlignment().getQuantity() * m_ast_context->getCharWidth();
1506
1507 if (log)
1508 {
1509 log->Printf("LRT[%u] returned:", current_id);
1510 log->Printf("LRT[%u] Original = (RecordDecl*)%p", current_id, origin_record.decl);
Daniel Malead01b2952012-11-29 21:49:15 +00001511 log->Printf("LRT[%u] Size = %" PRId64, current_id, size);
1512 log->Printf("LRT[%u] Alignment = %" PRId64, current_id, alignment);
Sean Callanan5b26f272012-02-04 08:49:35 +00001513 log->Printf("LRT[%u] Fields:", current_id);
1514 for (RecordDecl::field_iterator fi = record->field_begin(), fe = record->field_end();
1515 fi != fe;
1516 ++fi)
1517 {
Daniel Malead01b2952012-11-29 21:49:15 +00001518 log->Printf("LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %" PRId64 " bits",
Sean Callanan5b26f272012-02-04 08:49:35 +00001519 current_id,
1520 *fi,
1521 fi->getNameAsString().c_str(),
1522 field_offsets[*fi]);
1523 }
1524 DeclFromParser <const CXXRecordDecl> parser_cxx_record = DynCast<const CXXRecordDecl>(parser_record);
1525 if (parser_cxx_record.IsValid())
1526 {
1527 log->Printf("LRT[%u] Bases:", current_id);
1528 for (CXXRecordDecl::base_class_const_iterator bi = parser_cxx_record->bases_begin(), be = parser_cxx_record->bases_end();
1529 bi != be;
1530 ++bi)
1531 {
1532 bool is_virtual = bi->isVirtual();
1533
1534 QualType base_type = bi->getType();
1535 const RecordType *base_record_type = base_type->getAs<RecordType>();
1536 DeclFromParser <RecordDecl> base_record(base_record_type->getDecl());
1537 DeclFromParser <CXXRecordDecl> base_cxx_record = DynCast<CXXRecordDecl>(base_record);
1538
Daniel Malead01b2952012-11-29 21:49:15 +00001539 log->Printf("LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %" PRId64 " chars",
Sean Callanan5b26f272012-02-04 08:49:35 +00001540 current_id,
1541 (is_virtual ? "Virtual " : ""),
1542 base_cxx_record.decl,
1543 base_cxx_record.decl->getNameAsString().c_str(),
1544 (is_virtual ? virtual_base_offsets[base_cxx_record.decl].getQuantity() :
1545 base_offsets[base_cxx_record.decl].getQuantity()));
1546 }
1547 }
1548 else
1549 {
1550 log->Printf("LRD[%u] Not a CXXRecord, so no bases", current_id);
1551 }
1552 }
1553
1554 return true;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001555}
1556
Sean Callanan1ee44b72011-10-29 01:58:46 +00001557void
1558ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
1559 const ConstString &name,
1560 ClangASTImporter::NamespaceMapSP &parent_map) const
1561{
1562 static unsigned int invocation_id = 0;
1563 unsigned int current_id = invocation_id++;
1564
1565 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1566
1567 if (log)
1568 {
1569 if (parent_map && parent_map->size())
Sean Callanan00f43622011-11-18 03:28:09 +00001570 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in 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 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
1575 else
Sean Callanan00f43622011-11-18 03:28:09 +00001576 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
Sean Callanan1ee44b72011-10-29 01:58:46 +00001577 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +00001578 m_ast_context,
Sean Callanan1ee44b72011-10-29 01:58:46 +00001579 name.GetCString());
1580 }
1581
1582
1583 if (parent_map)
1584 {
1585 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
1586 i != e;
1587 ++i)
1588 {
1589 ClangNamespaceDecl found_namespace_decl;
1590
1591 lldb::ModuleSP module_sp = i->first;
1592 ClangNamespaceDecl module_parent_namespace_decl = i->second;
1593
1594 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
1595
1596 if (!symbol_vendor)
1597 continue;
1598
1599 SymbolContext null_sc;
1600
1601 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
1602
1603 if (!found_namespace_decl)
1604 continue;
1605
1606 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
1607
1608 if (log)
1609 log->Printf(" CMN[%u] Found namespace %s in module %s",
1610 current_id,
1611 name.GetCString(),
1612 module_sp->GetFileSpec().GetFilename().GetCString());
1613 }
1614 }
1615 else
1616 {
Enrico Granata17598482012-11-08 02:22:02 +00001617 const ModuleList &target_images = m_target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00001618 Mutex::Locker modules_locker(target_images.GetMutex());
1619
Sean Callanan1ee44b72011-10-29 01:58:46 +00001620 ClangNamespaceDecl null_namespace_decl;
1621
Greg Claytonc7bece562013-01-25 18:06:21 +00001622 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i)
Sean Callanan1ee44b72011-10-29 01:58:46 +00001623 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00001624 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
Sean Callanan1ee44b72011-10-29 01:58:46 +00001625
1626 if (!image)
1627 continue;
1628
1629 ClangNamespaceDecl found_namespace_decl;
1630
1631 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
1632
1633 if (!symbol_vendor)
1634 continue;
1635
1636 SymbolContext null_sc;
1637
1638 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
1639
1640 if (!found_namespace_decl)
1641 continue;
1642
1643 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
1644
1645 if (log)
1646 log->Printf(" CMN[%u] Found namespace %s in module %s",
1647 current_id,
1648 name.GetCString(),
1649 image->GetFileSpec().GetFilename().GetCString());
1650 }
1651 }
1652}
1653
Sean Callananba0aca72011-10-29 02:28:18 +00001654NamespaceDecl *
1655ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
1656{
Greg Claytone1cd1be2012-01-29 20:56:30 +00001657 if (!namespace_decls)
Sean Callananba0aca72011-10-29 02:28:18 +00001658 return NULL;
1659
1660 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1661
1662 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
1663
Sean Callanan686b2312011-11-16 18:20:47 +00001664 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
Sean Callananba0aca72011-10-29 02:28:18 +00001665
Sean Callanan61b33f72012-03-20 21:11:12 +00001666 if (!copied_decl)
1667 return NULL;
Sean Callananeeffea42013-02-12 08:01:13 +00001668
Sean Callananba0aca72011-10-29 02:28:18 +00001669 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1670
Sean Callananeeffea42013-02-12 08:01:13 +00001671 if (!copied_namespace_decl)
1672 return NULL;
1673
1674 context.m_decls.push_back(copied_namespace_decl);
1675
Sean Callananba0aca72011-10-29 02:28:18 +00001676 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
1677
1678 return dyn_cast<NamespaceDecl>(copied_decl);
1679}
1680
Sean Callananfb3e4302011-10-29 19:50:43 +00001681void *
1682ClangASTSource::GuardedCopyType (ASTContext *dest_context,
1683 ASTContext *source_context,
1684 void *clang_type)
Sean Callanan8106d802013-03-08 20:04:57 +00001685{
1686 ClangASTMetrics::RegisterLLDBImport();
1687
Sean Callananfb3e4302011-10-29 19:50:43 +00001688 SetImportInProgress(true);
1689
Sean Callanan686b2312011-11-16 18:20:47 +00001690 QualType ret_qual_type = m_ast_importer->CopyType (m_ast_context, source_context, QualType::getFromOpaquePtr(clang_type));
Sean Callananfb3e4302011-10-29 19:50:43 +00001691
1692 void *ret = ret_qual_type.getAsOpaquePtr();
1693
1694 SetImportInProgress(false);
1695
Sean Callanan61b33f72012-03-20 21:11:12 +00001696 if (ret && ret_qual_type->getCanonicalTypeInternal().isNull())
Sean Callanan8b665982012-02-27 19:22:39 +00001697 // this shouldn't happen, but we're hardening because the AST importer seems to be generating bad types
1698 // on occasion.
1699 return NULL;
1700
Sean Callananfb3e4302011-10-29 19:50:43 +00001701 return ret;
1702}
1703
Greg Clayton6beaaa62011-01-17 03:46:26 +00001704clang::NamedDecl *
1705NameSearchContext::AddVarDecl(void *type)
1706{
Greg Clayton7b462cc2010-10-15 22:48:33 +00001707 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan348b5892010-11-30 00:27:43 +00001708
1709 assert (type && "Type for variable must be non-NULL!");
Sean Callanan44096b12010-09-14 21:59:34 +00001710
Sean Callananeddeb3b2011-10-28 23:38:38 +00001711 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001712 const_cast<DeclContext*>(m_decl_context),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001713 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00001714 SourceLocation(),
Sean Callanan44096b12010-09-14 21:59:34 +00001715 ii,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001716 QualType::getFromOpaquePtr(type),
1717 0,
Sean Callanane2ef6e32010-09-23 03:01:22 +00001718 SC_Static,
1719 SC_Static);
Greg Clayton7b462cc2010-10-15 22:48:33 +00001720 m_decls.push_back(Decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001721
1722 return Decl;
1723}
Sean Callanan468574b2010-06-22 23:46:24 +00001724
Greg Clayton6beaaa62011-01-17 03:46:26 +00001725clang::NamedDecl *
1726NameSearchContext::AddFunDecl (void *type)
1727{
Sean Callanane2d47482012-03-21 17:13:20 +00001728 assert (type && "Type for variable must be non-NULL!");
1729
Sean Callananeddeb3b2011-10-28 23:38:38 +00001730 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001731 const_cast<DeclContext*>(m_decl_context),
1732 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00001733 SourceLocation(),
Greg Clayton7b462cc2010-10-15 22:48:33 +00001734 m_decl_name.getAsIdentifierInfo(),
1735 QualType::getFromOpaquePtr(type),
1736 NULL,
1737 SC_Static,
1738 SC_Static,
1739 false,
1740 true);
Sean Callanan468574b2010-06-22 23:46:24 +00001741
Sean Callanan04949cc2010-08-12 23:45:38 +00001742 // We have to do more than just synthesize the FunctionDecl. We have to
1743 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
1744 // this, we raid the function's FunctionProtoType for types.
1745
Greg Clayton7b462cc2010-10-15 22:48:33 +00001746 QualType qual_type (QualType::getFromOpaquePtr(type));
Sean Callananfc4f2fb2011-12-14 01:13:04 +00001747 const FunctionProtoType *func_proto_type = qual_type.getTypePtr()->getAs<FunctionProtoType>();
Sean Callanan468574b2010-06-22 23:46:24 +00001748
Greg Clayton7b462cc2010-10-15 22:48:33 +00001749 if (func_proto_type)
Sean Callanand448c1b2010-06-23 18:58:10 +00001750 {
Greg Clayton7b462cc2010-10-15 22:48:33 +00001751 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan468574b2010-06-22 23:46:24 +00001752 unsigned ArgIndex;
1753
Sean Callanan880e6802011-10-07 23:18:13 +00001754 SmallVector<ParmVarDecl *, 5> parm_var_decls;
1755
Sean Callanan468574b2010-06-22 23:46:24 +00001756 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
1757 {
Greg Clayton7b462cc2010-10-15 22:48:33 +00001758 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan468574b2010-06-22 23:46:24 +00001759
Sean Callananeddeb3b2011-10-28 23:38:38 +00001760 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callanan880e6802011-10-07 23:18:13 +00001761 const_cast<DeclContext*>(m_decl_context),
1762 SourceLocation(),
1763 SourceLocation(),
1764 NULL,
1765 arg_qual_type,
1766 NULL,
1767 SC_Static,
1768 SC_Static,
1769 NULL));
Sean Callanan468574b2010-06-22 23:46:24 +00001770 }
1771
Sean Callanan880e6802011-10-07 23:18:13 +00001772 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan468574b2010-06-22 23:46:24 +00001773 }
Sean Callanane0a64f72011-12-01 21:04:37 +00001774 else
1775 {
1776 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1777
1778 log->Printf("Function type wasn't a FunctionProtoType");
1779 }
Sean Callanan468574b2010-06-22 23:46:24 +00001780
Greg Clayton7b462cc2010-10-15 22:48:33 +00001781 m_decls.push_back(func_decl);
Sean Callanan468574b2010-06-22 23:46:24 +00001782
Greg Clayton7b462cc2010-10-15 22:48:33 +00001783 return func_decl;
Sean Callanan468574b2010-06-22 23:46:24 +00001784}
Sean Callanan8ade1042010-07-27 00:55:47 +00001785
Greg Clayton6beaaa62011-01-17 03:46:26 +00001786clang::NamedDecl *
1787NameSearchContext::AddGenericFunDecl()
Sean Callanan8ade1042010-07-27 00:55:47 +00001788{
Sean Callanan2c777c42011-01-18 23:32:05 +00001789 FunctionProtoType::ExtProtoInfo proto_info;
1790
1791 proto_info.Variadic = true;
1792
Sean Callananeddeb3b2011-10-28 23:38:38 +00001793 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 +00001794 ArrayRef<QualType>(), // argument types
Sean Callananeddeb3b2011-10-28 23:38:38 +00001795 proto_info));
Greg Clayton7b462cc2010-10-15 22:48:33 +00001796
Sean Callanan8ade1042010-07-27 00:55:47 +00001797 return AddFunDecl(generic_function_type.getAsOpaquePtr());
1798}
Sean Callanan5666b672010-08-04 01:02:13 +00001799
Greg Clayton6beaaa62011-01-17 03:46:26 +00001800clang::NamedDecl *
1801NameSearchContext::AddTypeDecl(void *type)
Sean Callanan5666b672010-08-04 01:02:13 +00001802{
Greg Clayton1b03cb52011-01-23 00:34:52 +00001803 if (type)
1804 {
1805 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan5666b672010-08-04 01:02:13 +00001806
Sean Callananfc4f2fb2011-12-14 01:13:04 +00001807 if (const TagType *tag_type = qual_type->getAs<TagType>())
Greg Clayton1b03cb52011-01-23 00:34:52 +00001808 {
1809 TagDecl *tag_decl = tag_type->getDecl();
1810
1811 m_decls.push_back(tag_decl);
1812
1813 return tag_decl;
1814 }
Sean Callananfc4f2fb2011-12-14 01:13:04 +00001815 else if (const ObjCObjectType *objc_object_type = qual_type->getAs<ObjCObjectType>())
Greg Clayton1b03cb52011-01-23 00:34:52 +00001816 {
1817 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
1818
1819 m_decls.push_back((NamedDecl*)interface_decl);
1820
1821 return (NamedDecl*)interface_decl;
1822 }
Sean Callananc4b1ab42013-03-14 17:21:53 +00001823 else if (const TypedefType *typedef_type = qual_type->getAs<TypedefType>())
1824 {
1825 TypedefNameDecl *typedef_name_decl = typedef_type->getDecl();
1826
1827 m_decls.push_back(typedef_name_decl);
1828
1829 return (NamedDecl*)typedef_name_decl;
1830 }
Sean Callanan5666b672010-08-04 01:02:13 +00001831 }
Greg Clayton1b03cb52011-01-23 00:34:52 +00001832 return NULL;
Sean Callanan5666b672010-08-04 01:02:13 +00001833}
Greg Claytona2721472011-06-25 00:44:06 +00001834
1835void
1836NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
1837{
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001838 for (clang::NamedDecl *decl : result)
1839 m_decls.push_back (decl);
Greg Claytona2721472011-06-25 00:44:06 +00001840}
1841
1842void
1843NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
1844{
1845 m_decls.push_back (decl);
1846}