blob: d664b1e8587a6e1e449717e770f32f77c6c0bfb1 [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{
Greg Clayton5160ce52013-03-27 23:08:40 +0000172 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananba0aca72011-10-29 02:28:18 +0000173
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{
Greg Clayton5160ce52013-03-27 23:08:40 +0000309 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananba0aca72011-10-29 02:28:18 +0000310
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
Greg Clayton5160ce52013-03-27 23:08:40 +0000373 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananba0aca72011-10-29 02:28:18 +0000374
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);
Sean Callanan04b2bfa2013-05-09 01:09:49 +0000475
476 DeclContext *decl_context_non_const = const_cast<DeclContext *>(decl_context);
477
478 if (copied_decl->getDeclContext() != decl_context)
479 {
480 if (copied_decl->getDeclContext()->containsDecl(copied_decl))
481 copied_decl->getDeclContext()->removeDecl(copied_decl);
482 copied_decl->setDeclContext(decl_context_non_const);
483 }
484
485 if (!decl_context_non_const->containsDecl(copied_decl))
486 decl_context_non_const->addDeclInternal(copied_decl);
Sean Callananba0aca72011-10-29 02:28:18 +0000487 }
488 }
489
490 return ELR_AlreadyLoaded;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491}
492
Sean Callananfb3e4302011-10-29 19:50:43 +0000493void
494ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
495{
496 assert (m_ast_context);
497
Sean Callanan8106d802013-03-08 20:04:57 +0000498 ClangASTMetrics::RegisterVisibleQuery();
499
Sean Callananfb3e4302011-10-29 19:50:43 +0000500 const ConstString name(context.m_decl_name.getAsString().c_str());
501
Greg Clayton5160ce52013-03-27 23:08:40 +0000502 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfb3e4302011-10-29 19:50:43 +0000503
504 static unsigned int invocation_id = 0;
505 unsigned int current_id = invocation_id++;
506
507 if (log)
508 {
509 if (!context.m_decl_context)
Sean Callanana6e61a72012-01-13 22:05:55 +0000510 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 +0000511 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
Sean Callanan00f43622011-11-18 03:28:09 +0000512 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 +0000513 else
Sean Callanan00f43622011-11-18 03:28:09 +0000514 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 +0000515 }
516
517 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
518
519 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
520 {
521 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
522
523 if (log && log->GetVerbose())
524 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
525 current_id,
526 namespace_map.get(),
527 (int)namespace_map->size());
528
529 if (!namespace_map)
530 return;
531
532 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
533 i != e;
534 ++i)
535 {
536 if (log)
537 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
538 current_id,
539 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
540 i->first->GetFileSpec().GetFilename().GetCString());
541
542 FindExternalVisibleDecls(context,
543 i->first,
544 i->second,
545 current_id);
546 }
547 }
Sean Callanan100d74e2011-11-15 21:50:18 +0000548 else if (isa<ObjCInterfaceDecl>(context.m_decl_context))
Sean Callanand5c17ed2011-11-15 02:11:17 +0000549 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000550 FindObjCPropertyAndIvarDecls(context);
Sean Callanand5c17ed2011-11-15 02:11:17 +0000551 }
Sean Callananfb3e4302011-10-29 19:50:43 +0000552 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
553 {
554 // we shouldn't be getting FindExternalVisibleDecls calls for these
555 return;
556 }
557 else
558 {
559 ClangNamespaceDecl namespace_decl;
560
561 if (log)
562 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
563
564 FindExternalVisibleDecls(context,
565 lldb::ModuleSP(),
566 namespace_decl,
567 current_id);
568 }
569
570 if (!context.m_namespace_map->empty())
571 {
572 if (log && log->GetVerbose())
573 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
574 current_id,
575 context.m_namespace_map.get(),
576 (int)context.m_namespace_map->size());
577
578 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
579
580 if (clang_namespace_decl)
581 clang_namespace_decl->setHasExternalVisibleStorage();
582 }
583}
584
585void
586ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
587 lldb::ModuleSP module_sp,
588 ClangNamespaceDecl &namespace_decl,
589 unsigned int current_id)
590{
591 assert (m_ast_context);
592
Greg Clayton5160ce52013-03-27 23:08:40 +0000593 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfb3e4302011-10-29 19:50:43 +0000594
595 SymbolContextList sc_list;
596
597 const ConstString name(context.m_decl_name.getAsString().c_str());
598
599 const char *name_unique_cstr = name.GetCString();
600
Sean Callanan5b26f272012-02-04 08:49:35 +0000601 static ConstString id_name("id");
602 static ConstString Class_name("Class");
603
604 if (name == id_name || name == Class_name)
605 return;
606
Sean Callananfb3e4302011-10-29 19:50:43 +0000607 if (name_unique_cstr == NULL)
608 return;
609
610 // The ClangASTSource is not responsible for finding $-names.
611 if (name_unique_cstr[0] == '$')
612 return;
613
614 if (module_sp && namespace_decl)
615 {
616 ClangNamespaceDecl found_namespace_decl;
617
618 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
619
620 if (symbol_vendor)
621 {
622 SymbolContext null_sc;
623
624 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
625
626 if (found_namespace_decl)
627 {
628 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
629
630 if (log)
631 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
632 current_id,
633 name.GetCString(),
634 module_sp->GetFileSpec().GetFilename().GetCString());
635 }
636 }
637 }
638 else
639 {
Enrico Granata17598482012-11-08 02:22:02 +0000640 const ModuleList &target_images = m_target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +0000641 Mutex::Locker modules_locker (target_images.GetMutex());
Sean Callananfb3e4302011-10-29 19:50:43 +0000642
Greg Claytonc7bece562013-01-25 18:06:21 +0000643 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i)
Sean Callananfb3e4302011-10-29 19:50:43 +0000644 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +0000645 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
Sean Callananfb3e4302011-10-29 19:50:43 +0000646
647 if (!image)
648 continue;
649
650 ClangNamespaceDecl found_namespace_decl;
651
652 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
653
654 if (!symbol_vendor)
655 continue;
656
657 SymbolContext null_sc;
658
659 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
660
661 if (found_namespace_decl)
662 {
663 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
664
665 if (log)
666 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
667 current_id,
668 name.GetCString(),
669 image->GetFileSpec().GetFilename().GetCString());
670 }
671 }
672 }
673
Sean Callananfb3e4302011-10-29 19:50:43 +0000674 do
675 {
676 TypeList types;
677 SymbolContext null_sc;
Greg Clayton84db9102012-03-26 23:03:23 +0000678 const bool exact_match = false;
Sean Callananbfb7c682011-12-19 19:38:39 +0000679
Sean Callananfb3e4302011-10-29 19:50:43 +0000680 if (module_sp && namespace_decl)
Greg Clayton84db9102012-03-26 23:03:23 +0000681 module_sp->FindTypesInNamespace(null_sc, name, &namespace_decl, 1, types);
Sean Callananbfb7c682011-12-19 19:38:39 +0000682 else
Greg Clayton29399a22012-04-06 17:41:13 +0000683 m_target->GetImages().FindTypes(null_sc, name, exact_match, 1, types);
Sean Callananfb3e4302011-10-29 19:50:43 +0000684
685 if (types.GetSize())
686 {
687 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
688
689 if (log)
690 {
691 const char *name_string = type_sp->GetName().GetCString();
692
693 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
694 current_id,
695 name.GetCString(),
696 (name_string ? name_string : "<anonymous>"));
697 }
Sean Callananbc47dfc2012-09-11 21:44:01 +0000698
699 clang::ASTContext *type_ast = type_sp->GetClangAST();
700 lldb::clang_type_t full_type = type_sp->GetClangFullType();
Sean Callanan0eed0d42011-12-06 03:41:14 +0000701
Sean Callananbc47dfc2012-09-11 21:44:01 +0000702 void *copied_type = GuardedCopyType(m_ast_context, type_ast, full_type);
Sean Callanan0eed0d42011-12-06 03:41:14 +0000703
Sean Callanane0a64f72011-12-01 21:04:37 +0000704 if (!copied_type)
705 {
706 if (log)
Sean Callanan7be70e82012-12-19 23:05:01 +0000707 log->Printf(" CAS::FEVD[%u] - Couldn't export a type",
Sean Callanan0eed0d42011-12-06 03:41:14 +0000708 current_id);
709
Sean Callanane0a64f72011-12-01 21:04:37 +0000710 break;
711 }
712
Sean Callananfb3e4302011-10-29 19:50:43 +0000713 context.AddTypeDecl(copied_type);
714 }
Sean Callanan7be70e82012-12-19 23:05:01 +0000715 else
716 {
717 do
718 {
719 // Couldn't find any types elsewhere. Try the Objective-C runtime if one exists.
720
721 lldb::ProcessSP process(m_target->GetProcessSP());
722
723 if (!process)
724 break;
725
726 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
727
728 if (!language_runtime)
729 break;
730
731 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
732
733 if (!type_vendor)
734 break;
735
736 bool append = false;
737 uint32_t max_matches = 1;
738 std::vector <ClangASTType> types;
739
740 if (!type_vendor->FindTypes(name,
741 append,
742 max_matches,
743 types))
744 break;
745
746 if (log)
747 {
748 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\" in the runtime",
749 current_id,
750 name.GetCString());
751 }
752
753 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
754
755 clang::QualType runtime_qual_type(runtime_clang_type, 0);
756
757 void *copied_type = GuardedCopyType(m_ast_context, type_vendor->GetClangASTContext(), runtime_qual_type.getAsOpaquePtr());
758
759 if (!copied_type)
760 {
761 if (log)
762 log->Printf(" CAS::FEVD[%u] - Couldn't export a type from the runtime",
763 current_id);
764
765 break;
766 }
767
768 context.AddTypeDecl(copied_type);
769 }
770 while(0);
771 }
Sean Callanan09ab4b72011-11-30 22:11:59 +0000772
Sean Callananfb3e4302011-10-29 19:50:43 +0000773 } while(0);
774}
775
Sean Callanan55400942012-11-02 17:09:58 +0000776template <class D> class TaggedASTDecl {
777public:
778 TaggedASTDecl() : decl(NULL) { }
779 TaggedASTDecl(D *_decl) : decl(_decl) { }
780 bool IsValid() const { return (decl != NULL); }
781 bool IsInvalid() const { return !IsValid(); }
782 D *operator->() const { return decl; }
783 D *decl;
784};
785
786template <class D2, template <class D> class TD, class D1>
787TD<D2>
788DynCast(TD<D1> source)
789{
790 return TD<D2> (dyn_cast<D2>(source.decl));
791}
792
793template <class D = Decl> class DeclFromParser;
794template <class D = Decl> class DeclFromUser;
795
796template <class D> class DeclFromParser : public TaggedASTDecl<D> {
797public:
798 DeclFromParser() : TaggedASTDecl<D>() { }
799 DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) { }
800
801 DeclFromUser<D> GetOrigin(ClangASTImporter *importer);
802};
803
804template <class D> class DeclFromUser : public TaggedASTDecl<D> {
805public:
806 DeclFromUser() : TaggedASTDecl<D>() { }
807 DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) { }
808
809 DeclFromParser<D> Import(ClangASTImporter *importer, ASTContext &dest_ctx);
810};
811
812template <class D>
813DeclFromUser<D>
814DeclFromParser<D>::GetOrigin(ClangASTImporter *importer)
815{
816 DeclFromUser <> origin_decl;
817 importer->ResolveDeclOrigin(this->decl, &origin_decl.decl, NULL);
818 if (origin_decl.IsInvalid())
819 return DeclFromUser<D>();
820 return DeclFromUser<D>(dyn_cast<D>(origin_decl.decl));
821}
822
823template <class D>
824DeclFromParser<D>
825DeclFromUser<D>::Import(ClangASTImporter *importer, ASTContext &dest_ctx)
826{
827 DeclFromParser <> parser_generic_decl(importer->CopyDecl(&dest_ctx, &this->decl->getASTContext(), this->decl));
828 if (parser_generic_decl.IsInvalid())
829 return DeclFromParser<D>();
830 return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
831}
832
Sean Callananc83e3412012-11-28 03:23:20 +0000833static bool
Sean Callananbc47dfc2012-09-11 21:44:01 +0000834FindObjCMethodDeclsWithOrigin (unsigned int current_id,
835 NameSearchContext &context,
836 ObjCInterfaceDecl *original_interface_decl,
837 clang::ASTContext *ast_context,
838 ClangASTImporter *ast_importer,
839 const char *log_info)
840{
841 const DeclarationName &decl_name(context.m_decl_name);
842 clang::ASTContext *original_ctx = &original_interface_decl->getASTContext();
843
844 Selector original_selector;
845
846 if (decl_name.isObjCZeroArgSelector())
847 {
848 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name.getAsString());
849 original_selector = original_ctx->Selectors.getSelector(0, &ident);
850 }
851 else if (decl_name.isObjCOneArgSelector())
852 {
853 const std::string &decl_name_string = decl_name.getAsString();
854 std::string decl_name_string_without_colon(decl_name_string.c_str(), decl_name_string.length() - 1);
855 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name_string_without_colon.c_str());
856 original_selector = original_ctx->Selectors.getSelector(1, &ident);
857 }
858 else
859 {
860 SmallVector<IdentifierInfo *, 4> idents;
861
862 clang::Selector sel = decl_name.getObjCSelector();
863
864 int num_args = sel.getNumArgs();
865
866 for (unsigned i = 0;
867 i != num_args;
868 ++i)
869 {
870 idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i)));
871 }
872
873 original_selector = original_ctx->Selectors.getSelector(num_args, idents.data());
874 }
875
876 DeclarationName original_decl_name(original_selector);
877
878 ObjCInterfaceDecl::lookup_result result = original_interface_decl->lookup(original_decl_name);
879
Sean Callanan5deaa4c2012-12-21 21:34:42 +0000880 if (result.empty())
Sean Callananc83e3412012-11-28 03:23:20 +0000881 return false;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000882
Sean Callanan5deaa4c2012-12-21 21:34:42 +0000883 if (!result[0])
Sean Callananc83e3412012-11-28 03:23:20 +0000884 return false;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000885
Sean Callanan5deaa4c2012-12-21 21:34:42 +0000886 ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(result[0]);
Sean Callananbc47dfc2012-09-11 21:44:01 +0000887
888 if (!result_method)
Sean Callananc83e3412012-11-28 03:23:20 +0000889 return false;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000890
891 Decl *copied_decl = ast_importer->CopyDecl(ast_context, &result_method->getASTContext(), result_method);
892
893 if (!copied_decl)
Sean Callananc83e3412012-11-28 03:23:20 +0000894 return false;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000895
896 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
897
898 if (!copied_method_decl)
Sean Callananc83e3412012-11-28 03:23:20 +0000899 return false;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000900
Greg Clayton5160ce52013-03-27 23:08:40 +0000901 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananbc47dfc2012-09-11 21:44:01 +0000902
903 if (log)
904 {
905 ASTDumper dumper((Decl*)copied_method_decl);
906 log->Printf(" CAS::FOMD[%d] found (%s) %s", current_id, log_info, dumper.GetCString());
907 }
908
909 context.AddNamedDecl(copied_method_decl);
910
Sean Callananc83e3412012-11-28 03:23:20 +0000911 return true;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000912}
913
Sean Callanan0730e9c2011-11-09 19:33:21 +0000914void
915ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
916{
Greg Clayton5160ce52013-03-27 23:08:40 +0000917 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan0730e9c2011-11-09 19:33:21 +0000918
Sean Callanan46198ff2011-11-11 20:37:26 +0000919 static unsigned int invocation_id = 0;
920 unsigned int current_id = invocation_id++;
921
Sean Callanan0730e9c2011-11-09 19:33:21 +0000922 const DeclarationName &decl_name(context.m_decl_name);
923 const DeclContext *decl_ctx(context.m_decl_context);
924
925 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
926
927 if (!interface_decl)
928 return;
929
Sean Callanana6582262012-04-05 00:12:52 +0000930 do
931 {
932 Decl *original_decl = NULL;
933 ASTContext *original_ctx = NULL;
934
935 m_ast_importer->ResolveDeclOrigin(interface_decl, &original_decl, &original_ctx);
936
937 if (!original_decl)
938 break;
939
940 ObjCInterfaceDecl *original_interface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl);
941
Sean Callananc83e3412012-11-28 03:23:20 +0000942 if (FindObjCMethodDeclsWithOrigin(current_id,
943 context,
944 original_interface_decl,
945 m_ast_context,
946 m_ast_importer,
947 "at origin"))
948 return; // found it, no need to look any further
Sean Callanana6582262012-04-05 00:12:52 +0000949 } while (0);
950
Sean Callanan0730e9c2011-11-09 19:33:21 +0000951 StreamString ss;
Sean Callanan0eed0d42011-12-06 03:41:14 +0000952
Sean Callanan0730e9c2011-11-09 19:33:21 +0000953 if (decl_name.isObjCZeroArgSelector())
954 {
955 ss.Printf("%s", decl_name.getAsString().c_str());
956 }
957 else if (decl_name.isObjCOneArgSelector())
958 {
Sean Callanan4fb79b72011-11-14 18:29:46 +0000959 ss.Printf("%s", decl_name.getAsString().c_str());
Sean Callanan0730e9c2011-11-09 19:33:21 +0000960 }
961 else
962 {
963 clang::Selector sel = decl_name.getObjCSelector();
964
965 for (unsigned i = 0, e = sel.getNumArgs();
966 i != e;
967 ++i)
968 {
969 llvm::StringRef r = sel.getNameForSlot(i);
970 ss.Printf("%s:", r.str().c_str());
971 }
972 }
973 ss.Flush();
974
Sean Callanan46198ff2011-11-11 20:37:26 +0000975 ConstString selector_name(ss.GetData());
976
Sean Callanan0730e9c2011-11-09 19:33:21 +0000977 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000978 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
979 current_id,
980 m_ast_context,
Sean Callanan46198ff2011-11-11 20:37:26 +0000981 interface_decl->getNameAsString().c_str(),
982 selector_name.AsCString());
Sean Callanan46198ff2011-11-11 20:37:26 +0000983 SymbolContextList sc_list;
984
985 const bool include_symbols = false;
Sean Callanan9df05fb2012-02-10 22:52:19 +0000986 const bool include_inlines = false;
Sean Callanan46198ff2011-11-11 20:37:26 +0000987 const bool append = false;
988
Sean Callanana9bc0652012-01-19 02:17:40 +0000989 std::string interface_name = interface_decl->getNameAsString();
990
991 do
992 {
993 StreamString ms;
994 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());
995 ms.Flush();
996 ConstString instance_method_name(ms.GetData());
997
Sean Callanan9df05fb2012-02-10 22:52:19 +0000998 m_target->GetImages().FindFunctions(instance_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanana9bc0652012-01-19 02:17:40 +0000999
1000 if (sc_list.GetSize())
1001 break;
1002
1003 ms.Clear();
1004 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());
1005 ms.Flush();
1006 ConstString class_method_name(ms.GetData());
1007
Sean Callanan9df05fb2012-02-10 22:52:19 +00001008 m_target->GetImages().FindFunctions(class_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanana9bc0652012-01-19 02:17:40 +00001009
1010 if (sc_list.GetSize())
1011 break;
1012
1013 // Fall back and check for methods in categories. If we find methods this way, we need to check that they're actually in
1014 // categories on the desired class.
1015
1016 SymbolContextList candidate_sc_list;
1017
Sean Callanan9df05fb2012-02-10 22:52:19 +00001018 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, include_inlines, append, candidate_sc_list);
Sean Callanana9bc0652012-01-19 02:17:40 +00001019
1020 for (uint32_t ci = 0, ce = candidate_sc_list.GetSize();
1021 ci != ce;
1022 ++ci)
1023 {
1024 SymbolContext candidate_sc;
1025
1026 if (!candidate_sc_list.GetContextAtIndex(ci, candidate_sc))
1027 continue;
1028
1029 if (!candidate_sc.function)
1030 continue;
1031
1032 const char *candidate_name = candidate_sc.function->GetName().AsCString();
1033
1034 const char *cursor = candidate_name;
1035
1036 if (*cursor != '+' && *cursor != '-')
1037 continue;
1038
1039 ++cursor;
1040
1041 if (*cursor != '[')
1042 continue;
1043
1044 ++cursor;
1045
1046 size_t interface_len = interface_name.length();
1047
1048 if (strncmp(cursor, interface_name.c_str(), interface_len))
1049 continue;
1050
1051 cursor += interface_len;
1052
1053 if (*cursor == ' ' || *cursor == '(')
1054 sc_list.Append(candidate_sc);
1055 }
1056 }
1057 while (0);
Sean Callanan46198ff2011-11-11 20:37:26 +00001058
Sean Callananbc47dfc2012-09-11 21:44:01 +00001059 if (sc_list.GetSize())
Sean Callanan46198ff2011-11-11 20:37:26 +00001060 {
Sean Callananbc47dfc2012-09-11 21:44:01 +00001061 // We found a good function symbol. Use that.
Sean Callanan46198ff2011-11-11 20:37:26 +00001062
Sean Callananbc47dfc2012-09-11 21:44:01 +00001063 for (uint32_t i = 0, e = sc_list.GetSize();
1064 i != e;
1065 ++i)
Sean Callanan46198ff2011-11-11 20:37:26 +00001066 {
Sean Callananbc47dfc2012-09-11 21:44:01 +00001067 SymbolContext sc;
Sean Callanan46198ff2011-11-11 20:37:26 +00001068
Sean Callananbc47dfc2012-09-11 21:44:01 +00001069 if (!sc_list.GetContextAtIndex(i, sc))
Sean Callanan46198ff2011-11-11 20:37:26 +00001070 continue;
1071
Sean Callananbc47dfc2012-09-11 21:44:01 +00001072 if (!sc.function)
Sean Callanan46198ff2011-11-11 20:37:26 +00001073 continue;
1074
Sean Callananbc47dfc2012-09-11 21:44:01 +00001075 DeclContext *function_ctx = sc.function->GetClangDeclContext();
1076
1077 if (!function_ctx)
1078 continue;
1079
1080 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
1081
1082 if (!method_decl)
1083 continue;
1084
1085 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
1086
1087 if (!found_interface_decl)
1088 continue;
1089
1090 if (found_interface_decl->getName() == interface_decl->getName())
Sean Callanan46198ff2011-11-11 20:37:26 +00001091 {
Sean Callananbc47dfc2012-09-11 21:44:01 +00001092 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
1093
1094 if (!copied_decl)
1095 continue;
1096
1097 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
1098
1099 if (!copied_method_decl)
1100 continue;
1101
1102 if (log)
1103 {
1104 ASTDumper dumper((Decl*)copied_method_decl);
Sean Callanan55400942012-11-02 17:09:58 +00001105 log->Printf(" CAS::FOMD[%d] found (in symbols) %s", current_id, dumper.GetCString());
Sean Callananbc47dfc2012-09-11 21:44:01 +00001106 }
1107
1108 context.AddNamedDecl(copied_method_decl);
Sean Callanan46198ff2011-11-11 20:37:26 +00001109 }
Sean Callanan46198ff2011-11-11 20:37:26 +00001110 }
Sean Callanan55400942012-11-02 17:09:58 +00001111
1112 return;
Sean Callanan46198ff2011-11-11 20:37:26 +00001113 }
Sean Callanan55400942012-11-02 17:09:58 +00001114
1115 // Try the debug information.
1116
1117 do
Sean Callananbc47dfc2012-09-11 21:44:01 +00001118 {
Sean Callanan55400942012-11-02 17:09:58 +00001119 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(interface_decl));
1120
1121 if (!complete_interface_decl)
1122 break;
1123
1124 // We found the complete interface. The runtime never needs to be queried in this scenario.
1125
1126 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
1127
1128 if (complete_interface_decl == interface_decl)
1129 break; // already checked this one
1130
1131 if (log)
1132 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1133 current_id,
1134 complete_interface_decl,
1135 &complete_iface_decl->getASTContext());
1136
1137 FindObjCMethodDeclsWithOrigin(current_id,
1138 context,
1139 complete_interface_decl,
1140 m_ast_context,
1141 m_ast_importer,
1142 "in debug info");
1143
1144 return;
Sean Callananbc47dfc2012-09-11 21:44:01 +00001145 }
Sean Callanan55400942012-11-02 17:09:58 +00001146 while (0);
Sean Callanan5b26f272012-02-04 08:49:35 +00001147
Sean Callanan55400942012-11-02 17:09:58 +00001148 do
1149 {
1150 // Check the runtime only if the debug information didn't have a complete interface.
1151
1152 lldb::ProcessSP process(m_target->GetProcessSP());
1153
1154 if (!process)
1155 break;
1156
1157 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1158
Sean Callanan7be70e82012-12-19 23:05:01 +00001159 if (!language_runtime)
1160 break;
1161
Sean Callanan55400942012-11-02 17:09:58 +00001162 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
1163
1164 if (!type_vendor)
1165 break;
1166
1167 ConstString interface_name(interface_decl->getNameAsString().c_str());
1168 bool append = false;
1169 uint32_t max_matches = 1;
1170 std::vector <ClangASTType> types;
1171
1172 if (!type_vendor->FindTypes(interface_name,
1173 append,
1174 max_matches,
1175 types))
1176 break;
1177
1178 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
1179
1180 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
1181
1182 if (!runtime_interface_type)
1183 break;
1184
1185 ObjCInterfaceDecl *runtime_interface_decl = runtime_interface_type->getDecl();
1186
1187 FindObjCMethodDeclsWithOrigin(current_id,
1188 context,
1189 runtime_interface_decl,
1190 m_ast_context,
1191 m_ast_importer,
1192 "in runtime");
1193 }
1194 while(0);
Sean Callanan5b26f272012-02-04 08:49:35 +00001195}
1196
Sean Callanan72772842012-02-22 23:57:45 +00001197static bool
1198FindObjCPropertyAndIvarDeclsWithOrigin (unsigned int current_id,
1199 NameSearchContext &context,
1200 clang::ASTContext &ast_context,
1201 ClangASTImporter *ast_importer,
1202 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl)
1203{
Greg Clayton5160ce52013-03-27 23:08:40 +00001204 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan72772842012-02-22 23:57:45 +00001205
1206 if (origin_iface_decl.IsInvalid())
1207 return false;
1208
1209 std::string name_str = context.m_decl_name.getAsString();
1210 StringRef name(name_str.c_str());
1211 IdentifierInfo &name_identifier(origin_iface_decl->getASTContext().Idents.get(name));
1212
1213 DeclFromUser<ObjCPropertyDecl> origin_property_decl(origin_iface_decl->FindPropertyDeclaration(&name_identifier));
1214
1215 bool found = false;
1216
1217 if (origin_property_decl.IsValid())
1218 {
1219 DeclFromParser<ObjCPropertyDecl> parser_property_decl(origin_property_decl.Import(ast_importer, ast_context));
1220 if (parser_property_decl.IsValid())
1221 {
1222 if (log)
1223 {
1224 ASTDumper dumper((Decl*)parser_property_decl.decl);
1225 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1226 }
1227
1228 context.AddNamedDecl(parser_property_decl.decl);
1229 found = true;
1230 }
1231 }
1232
1233 DeclFromUser<ObjCIvarDecl> origin_ivar_decl(origin_iface_decl->getIvarDecl(&name_identifier));
1234
1235 if (origin_ivar_decl.IsValid())
1236 {
1237 DeclFromParser<ObjCIvarDecl> parser_ivar_decl(origin_ivar_decl.Import(ast_importer, ast_context));
1238 if (parser_ivar_decl.IsValid())
1239 {
1240 if (log)
1241 {
1242 ASTDumper dumper((Decl*)parser_ivar_decl.decl);
1243 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1244 }
1245
1246 context.AddNamedDecl(parser_ivar_decl.decl);
1247 found = true;
1248 }
1249 }
1250
1251 return found;
1252}
1253
Sean Callanand5c17ed2011-11-15 02:11:17 +00001254void
Sean Callanan5b26f272012-02-04 08:49:35 +00001255ClangASTSource::FindObjCPropertyAndIvarDecls (NameSearchContext &context)
Sean Callanand5c17ed2011-11-15 02:11:17 +00001256{
Greg Clayton5160ce52013-03-27 23:08:40 +00001257 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanand5c17ed2011-11-15 02:11:17 +00001258
1259 static unsigned int invocation_id = 0;
1260 unsigned int current_id = invocation_id++;
1261
Sean Callanan5b26f272012-02-04 08:49:35 +00001262 DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl(cast<ObjCInterfaceDecl>(context.m_decl_context));
1263 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(parser_iface_decl.GetOrigin(m_ast_importer));
Sean Callanan72772842012-02-22 23:57:45 +00001264
1265 ConstString class_name(parser_iface_decl->getNameAsString().c_str());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001266
1267 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +00001268 log->Printf("ClangASTSource::FindObjCPropertyAndIvarDecls[%d] on (ASTContext*)%p for '%s.%s'",
Sean Callanand5c17ed2011-11-15 02:11:17 +00001269 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +00001270 m_ast_context,
Sean Callanan5b26f272012-02-04 08:49:35 +00001271 parser_iface_decl->getNameAsString().c_str(),
Sean Callanan72772842012-02-22 23:57:45 +00001272 context.m_decl_name.getAsString().c_str());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001273
Sean Callanan72772842012-02-22 23:57:45 +00001274 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1275 context,
1276 *m_ast_context,
1277 m_ast_importer,
1278 origin_iface_decl))
1279 return;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001280
Sean Callanan72772842012-02-22 23:57:45 +00001281 if (log)
1282 log->Printf("CAS::FOPD[%d] couldn't find the property on origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p, searching elsewhere...",
1283 current_id,
1284 origin_iface_decl.decl,
1285 &origin_iface_decl->getASTContext());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001286
Sean Callanan72772842012-02-22 23:57:45 +00001287 SymbolContext null_sc;
1288 TypeList type_list;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001289
Sean Callananbc47dfc2012-09-11 21:44:01 +00001290 do
1291 {
Sean Callanan2cb5e522012-09-20 23:21:16 +00001292 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(parser_iface_decl.decl));
Sean Callananbc47dfc2012-09-11 21:44:01 +00001293
Sean Callanan2cb5e522012-09-20 23:21:16 +00001294 if (!complete_interface_decl)
Sean Callananbc47dfc2012-09-11 21:44:01 +00001295 break;
1296
Sean Callanan55400942012-11-02 17:09:58 +00001297 // We found the complete interface. The runtime never needs to be queried in this scenario.
1298
Sean Callanan2cb5e522012-09-20 23:21:16 +00001299 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
Sean Callananbc47dfc2012-09-11 21:44:01 +00001300
1301 if (complete_iface_decl.decl == origin_iface_decl.decl)
1302 break; // already checked this one
1303
1304 if (log)
1305 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1306 current_id,
1307 complete_iface_decl.decl,
1308 &complete_iface_decl->getASTContext());
1309
Sean Callanan55400942012-11-02 17:09:58 +00001310 FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1311 context,
1312 *m_ast_context,
1313 m_ast_importer,
1314 complete_iface_decl);
1315
1316 return;
Sean Callananbc47dfc2012-09-11 21:44:01 +00001317 }
1318 while(0);
Sean Callanan72772842012-02-22 23:57:45 +00001319
Sean Callananbc47dfc2012-09-11 21:44:01 +00001320 do
1321 {
Sean Callanan55400942012-11-02 17:09:58 +00001322 // Check the runtime only if the debug information didn't have a complete interface.
1323
1324 lldb::ProcessSP process(m_target->GetProcessSP());
1325
1326 if (!process)
1327 return;
1328
1329 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1330
1331 if (!language_runtime)
1332 return;
Sean Callananbc47dfc2012-09-11 21:44:01 +00001333
1334 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
1335
1336 if (!type_vendor)
1337 break;
1338
1339 bool append = false;
1340 uint32_t max_matches = 1;
1341 std::vector <ClangASTType> types;
1342
1343 if (!type_vendor->FindTypes(class_name,
1344 append,
1345 max_matches,
1346 types))
1347 break;
1348
1349 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
1350
1351 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
1352
1353 if (!runtime_interface_type)
1354 break;
1355
1356 DeclFromUser<const ObjCInterfaceDecl> runtime_iface_decl(runtime_interface_type->getDecl());
1357
1358 if (log)
1359 log->Printf("CAS::FOPD[%d] trying runtime (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1360 current_id,
1361 runtime_iface_decl.decl,
1362 &runtime_iface_decl->getASTContext());
1363
1364 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1365 context,
1366 *m_ast_context,
1367 m_ast_importer,
1368 runtime_iface_decl))
1369 return;
1370 }
1371 while(0);
Sean Callanan5b26f272012-02-04 08:49:35 +00001372}
1373
1374typedef llvm::DenseMap <const FieldDecl *, uint64_t> FieldOffsetMap;
1375typedef llvm::DenseMap <const CXXRecordDecl *, CharUnits> BaseOffsetMap;
1376
1377template <class D, class O>
1378static bool
1379ImportOffsetMap (llvm::DenseMap <const D*, O> &destination_map,
1380 llvm::DenseMap <const D*, O> &source_map,
1381 ClangASTImporter *importer,
1382 ASTContext &dest_ctx)
1383{
1384 typedef llvm::DenseMap <const D*, O> MapType;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001385
Sean Callanan5b26f272012-02-04 08:49:35 +00001386 for (typename MapType::iterator fi = source_map.begin(), fe = source_map.end();
1387 fi != fe;
1388 ++fi)
1389 {
1390 DeclFromUser <D> user_decl(const_cast<D*>(fi->first));
1391 DeclFromParser <D> parser_decl(user_decl.Import(importer, dest_ctx));
1392 if (parser_decl.IsInvalid())
1393 return false;
1394 destination_map.insert(std::pair<const D *, O>(parser_decl.decl, fi->second));
1395 }
1396
1397 return true;
1398}
1399
1400template <bool IsVirtual> bool ExtractBaseOffsets (const ASTRecordLayout &record_layout,
1401 DeclFromUser<const CXXRecordDecl> &record,
1402 BaseOffsetMap &base_offsets)
1403{
1404 for (CXXRecordDecl::base_class_const_iterator
1405 bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()),
1406 be = (IsVirtual ? record->vbases_end() : record->bases_end());
1407 bi != be;
1408 ++bi)
1409 {
1410 if (!IsVirtual && bi->isVirtual())
1411 continue;
1412
1413 const clang::Type *origin_base_type = bi->getType().getTypePtr();
1414 const clang::RecordType *origin_base_record_type = origin_base_type->getAs<RecordType>();
1415
1416 if (!origin_base_record_type)
1417 return false;
1418
1419 DeclFromUser <RecordDecl> origin_base_record(origin_base_record_type->getDecl());
1420
1421 if (origin_base_record.IsInvalid())
1422 return false;
1423
1424 DeclFromUser <CXXRecordDecl> origin_base_cxx_record(DynCast<CXXRecordDecl>(origin_base_record));
1425
1426 if (origin_base_cxx_record.IsInvalid())
1427 return false;
1428
1429 CharUnits base_offset;
1430
1431 if (IsVirtual)
1432 base_offset = record_layout.getVBaseClassOffset(origin_base_cxx_record.decl);
1433 else
1434 base_offset = record_layout.getBaseClassOffset(origin_base_cxx_record.decl);
1435
1436 base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(origin_base_cxx_record.decl, base_offset));
1437 }
1438
1439 return true;
1440}
1441
1442bool
1443ClangASTSource::layoutRecordType(const RecordDecl *record,
1444 uint64_t &size,
1445 uint64_t &alignment,
1446 FieldOffsetMap &field_offsets,
1447 BaseOffsetMap &base_offsets,
1448 BaseOffsetMap &virtual_base_offsets)
1449{
Sean Callanan8106d802013-03-08 20:04:57 +00001450 ClangASTMetrics::RegisterRecordLayout();
1451
Sean Callanan5b26f272012-02-04 08:49:35 +00001452 static unsigned int invocation_id = 0;
1453 unsigned int current_id = invocation_id++;
1454
Greg Clayton5160ce52013-03-27 23:08:40 +00001455 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5b26f272012-02-04 08:49:35 +00001456
Sean Callanand5c17ed2011-11-15 02:11:17 +00001457 if (log)
1458 {
Sean Callanan933ca2e2013-02-28 03:12:58 +00001459 log->Printf("LayoutRecordType[%u] on (ASTContext*)%p for (RecordDecl*)%p [name = '%s']",
Sean Callanan5b26f272012-02-04 08:49:35 +00001460 current_id,
1461 m_ast_context,
Sean Callanan933ca2e2013-02-28 03:12:58 +00001462 record,
Sean Callanan5b26f272012-02-04 08:49:35 +00001463 record->getNameAsString().c_str());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001464 }
Sean Callanan5b26f272012-02-04 08:49:35 +00001465
1466
1467 DeclFromParser <const RecordDecl> parser_record(record);
1468 DeclFromUser <const RecordDecl> origin_record(parser_record.GetOrigin(m_ast_importer));
1469
1470 if (origin_record.IsInvalid())
1471 return false;
1472
1473 FieldOffsetMap origin_field_offsets;
1474 BaseOffsetMap origin_base_offsets;
1475 BaseOffsetMap origin_virtual_base_offsets;
1476
Sean Callananbf81ea52012-04-07 00:06:00 +00001477 ClangASTContext::GetCompleteDecl(&origin_record->getASTContext(), const_cast<RecordDecl*>(origin_record.decl));
1478
1479 if (!origin_record.decl->getDefinition())
1480 return false;
1481
Sean Callanan5b26f272012-02-04 08:49:35 +00001482 const ASTRecordLayout &record_layout(origin_record->getASTContext().getASTRecordLayout(origin_record.decl));
1483
Sean Callananeb7b27d2013-03-25 18:27:07 +00001484 int field_idx = 0, field_count = record_layout.getFieldCount();
Sean Callanan5b26f272012-02-04 08:49:35 +00001485
1486 for (RecordDecl::field_iterator fi = origin_record->field_begin(), fe = origin_record->field_end();
1487 fi != fe;
1488 ++fi)
1489 {
Sean Callananeb7b27d2013-03-25 18:27:07 +00001490 if (field_idx >= field_count)
1491 return false; // Layout didn't go well. Bail out.
1492
Sean Callanan5b26f272012-02-04 08:49:35 +00001493 uint64_t field_offset = record_layout.getFieldOffset(field_idx);
1494
1495 origin_field_offsets.insert(std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
1496
1497 field_idx++;
1498 }
1499
1500 ASTContext &parser_ast_context(record->getASTContext());
1501
1502 DeclFromUser <const CXXRecordDecl> origin_cxx_record(DynCast<const CXXRecordDecl>(origin_record));
1503
1504 if (origin_cxx_record.IsValid())
1505 {
1506 if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record, origin_base_offsets) ||
1507 !ExtractBaseOffsets<true>(record_layout, origin_cxx_record, origin_virtual_base_offsets))
1508 return false;
1509 }
1510
1511 if (!ImportOffsetMap(field_offsets, origin_field_offsets, m_ast_importer, parser_ast_context) ||
1512 !ImportOffsetMap(base_offsets, origin_base_offsets, m_ast_importer, parser_ast_context) ||
1513 !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets, m_ast_importer, parser_ast_context))
1514 return false;
1515
1516 size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth();
1517 alignment = record_layout.getAlignment().getQuantity() * m_ast_context->getCharWidth();
1518
1519 if (log)
1520 {
1521 log->Printf("LRT[%u] returned:", current_id);
1522 log->Printf("LRT[%u] Original = (RecordDecl*)%p", current_id, origin_record.decl);
Daniel Malead01b2952012-11-29 21:49:15 +00001523 log->Printf("LRT[%u] Size = %" PRId64, current_id, size);
1524 log->Printf("LRT[%u] Alignment = %" PRId64, current_id, alignment);
Sean Callanan5b26f272012-02-04 08:49:35 +00001525 log->Printf("LRT[%u] Fields:", current_id);
1526 for (RecordDecl::field_iterator fi = record->field_begin(), fe = record->field_end();
1527 fi != fe;
1528 ++fi)
1529 {
Daniel Malead01b2952012-11-29 21:49:15 +00001530 log->Printf("LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %" PRId64 " bits",
Sean Callanan5b26f272012-02-04 08:49:35 +00001531 current_id,
1532 *fi,
1533 fi->getNameAsString().c_str(),
1534 field_offsets[*fi]);
1535 }
1536 DeclFromParser <const CXXRecordDecl> parser_cxx_record = DynCast<const CXXRecordDecl>(parser_record);
1537 if (parser_cxx_record.IsValid())
1538 {
1539 log->Printf("LRT[%u] Bases:", current_id);
1540 for (CXXRecordDecl::base_class_const_iterator bi = parser_cxx_record->bases_begin(), be = parser_cxx_record->bases_end();
1541 bi != be;
1542 ++bi)
1543 {
1544 bool is_virtual = bi->isVirtual();
1545
1546 QualType base_type = bi->getType();
1547 const RecordType *base_record_type = base_type->getAs<RecordType>();
1548 DeclFromParser <RecordDecl> base_record(base_record_type->getDecl());
1549 DeclFromParser <CXXRecordDecl> base_cxx_record = DynCast<CXXRecordDecl>(base_record);
1550
Daniel Malead01b2952012-11-29 21:49:15 +00001551 log->Printf("LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %" PRId64 " chars",
Sean Callanan5b26f272012-02-04 08:49:35 +00001552 current_id,
1553 (is_virtual ? "Virtual " : ""),
1554 base_cxx_record.decl,
1555 base_cxx_record.decl->getNameAsString().c_str(),
1556 (is_virtual ? virtual_base_offsets[base_cxx_record.decl].getQuantity() :
1557 base_offsets[base_cxx_record.decl].getQuantity()));
1558 }
1559 }
1560 else
1561 {
1562 log->Printf("LRD[%u] Not a CXXRecord, so no bases", current_id);
1563 }
1564 }
1565
1566 return true;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001567}
1568
Sean Callanan1ee44b72011-10-29 01:58:46 +00001569void
1570ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
1571 const ConstString &name,
1572 ClangASTImporter::NamespaceMapSP &parent_map) const
1573{
1574 static unsigned int invocation_id = 0;
1575 unsigned int current_id = invocation_id++;
1576
Greg Clayton5160ce52013-03-27 23:08:40 +00001577 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1ee44b72011-10-29 01:58:46 +00001578
1579 if (log)
1580 {
1581 if (parent_map && parent_map->size())
Sean Callanan00f43622011-11-18 03:28:09 +00001582 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
Sean Callanan1ee44b72011-10-29 01:58:46 +00001583 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +00001584 m_ast_context,
Sean Callanan1ee44b72011-10-29 01:58:46 +00001585 name.GetCString(),
1586 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
1587 else
Sean Callanan00f43622011-11-18 03:28:09 +00001588 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
Sean Callanan1ee44b72011-10-29 01:58:46 +00001589 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +00001590 m_ast_context,
Sean Callanan1ee44b72011-10-29 01:58:46 +00001591 name.GetCString());
1592 }
1593
1594
1595 if (parent_map)
1596 {
1597 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
1598 i != e;
1599 ++i)
1600 {
1601 ClangNamespaceDecl found_namespace_decl;
1602
1603 lldb::ModuleSP module_sp = i->first;
1604 ClangNamespaceDecl module_parent_namespace_decl = i->second;
1605
1606 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
1607
1608 if (!symbol_vendor)
1609 continue;
1610
1611 SymbolContext null_sc;
1612
1613 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
1614
1615 if (!found_namespace_decl)
1616 continue;
1617
1618 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
1619
1620 if (log)
1621 log->Printf(" CMN[%u] Found namespace %s in module %s",
1622 current_id,
1623 name.GetCString(),
1624 module_sp->GetFileSpec().GetFilename().GetCString());
1625 }
1626 }
1627 else
1628 {
Enrico Granata17598482012-11-08 02:22:02 +00001629 const ModuleList &target_images = m_target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00001630 Mutex::Locker modules_locker(target_images.GetMutex());
1631
Sean Callanan1ee44b72011-10-29 01:58:46 +00001632 ClangNamespaceDecl null_namespace_decl;
1633
Greg Claytonc7bece562013-01-25 18:06:21 +00001634 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i)
Sean Callanan1ee44b72011-10-29 01:58:46 +00001635 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00001636 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
Sean Callanan1ee44b72011-10-29 01:58:46 +00001637
1638 if (!image)
1639 continue;
1640
1641 ClangNamespaceDecl found_namespace_decl;
1642
1643 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
1644
1645 if (!symbol_vendor)
1646 continue;
1647
1648 SymbolContext null_sc;
1649
1650 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
1651
1652 if (!found_namespace_decl)
1653 continue;
1654
1655 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
1656
1657 if (log)
1658 log->Printf(" CMN[%u] Found namespace %s in module %s",
1659 current_id,
1660 name.GetCString(),
1661 image->GetFileSpec().GetFilename().GetCString());
1662 }
1663 }
1664}
1665
Sean Callananba0aca72011-10-29 02:28:18 +00001666NamespaceDecl *
1667ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
1668{
Greg Claytone1cd1be2012-01-29 20:56:30 +00001669 if (!namespace_decls)
Sean Callananba0aca72011-10-29 02:28:18 +00001670 return NULL;
1671
Sean Callananba0aca72011-10-29 02:28:18 +00001672 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
1673
Sean Callanan686b2312011-11-16 18:20:47 +00001674 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
Sean Callananba0aca72011-10-29 02:28:18 +00001675
Sean Callanan61b33f72012-03-20 21:11:12 +00001676 if (!copied_decl)
1677 return NULL;
Sean Callananeeffea42013-02-12 08:01:13 +00001678
Sean Callananba0aca72011-10-29 02:28:18 +00001679 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1680
Sean Callananeeffea42013-02-12 08:01:13 +00001681 if (!copied_namespace_decl)
1682 return NULL;
1683
1684 context.m_decls.push_back(copied_namespace_decl);
1685
Sean Callananba0aca72011-10-29 02:28:18 +00001686 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
1687
1688 return dyn_cast<NamespaceDecl>(copied_decl);
1689}
1690
Sean Callananfb3e4302011-10-29 19:50:43 +00001691void *
1692ClangASTSource::GuardedCopyType (ASTContext *dest_context,
1693 ASTContext *source_context,
1694 void *clang_type)
Sean Callanan8106d802013-03-08 20:04:57 +00001695{
1696 ClangASTMetrics::RegisterLLDBImport();
1697
Sean Callananfb3e4302011-10-29 19:50:43 +00001698 SetImportInProgress(true);
1699
Sean Callanan686b2312011-11-16 18:20:47 +00001700 QualType ret_qual_type = m_ast_importer->CopyType (m_ast_context, source_context, QualType::getFromOpaquePtr(clang_type));
Sean Callananfb3e4302011-10-29 19:50:43 +00001701
1702 void *ret = ret_qual_type.getAsOpaquePtr();
1703
1704 SetImportInProgress(false);
1705
Sean Callanan61b33f72012-03-20 21:11:12 +00001706 if (ret && ret_qual_type->getCanonicalTypeInternal().isNull())
Sean Callanan8b665982012-02-27 19:22:39 +00001707 // this shouldn't happen, but we're hardening because the AST importer seems to be generating bad types
1708 // on occasion.
1709 return NULL;
1710
Sean Callananfb3e4302011-10-29 19:50:43 +00001711 return ret;
1712}
1713
Greg Clayton6beaaa62011-01-17 03:46:26 +00001714clang::NamedDecl *
1715NameSearchContext::AddVarDecl(void *type)
1716{
Greg Clayton7b462cc2010-10-15 22:48:33 +00001717 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan348b5892010-11-30 00:27:43 +00001718
1719 assert (type && "Type for variable must be non-NULL!");
Sean Callanan44096b12010-09-14 21:59:34 +00001720
Sean Callananeddeb3b2011-10-28 23:38:38 +00001721 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001722 const_cast<DeclContext*>(m_decl_context),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001723 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00001724 SourceLocation(),
Sean Callanan44096b12010-09-14 21:59:34 +00001725 ii,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001726 QualType::getFromOpaquePtr(type),
1727 0,
Sean Callanane2ef6e32010-09-23 03:01:22 +00001728 SC_Static);
Greg Clayton7b462cc2010-10-15 22:48:33 +00001729 m_decls.push_back(Decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001730
1731 return Decl;
1732}
Sean Callanan468574b2010-06-22 23:46:24 +00001733
Greg Clayton6beaaa62011-01-17 03:46:26 +00001734clang::NamedDecl *
1735NameSearchContext::AddFunDecl (void *type)
1736{
Sean Callanane2d47482012-03-21 17:13:20 +00001737 assert (type && "Type for variable must be non-NULL!");
1738
Sean Callanan485f7322013-04-24 00:34:41 +00001739 if (m_function_types.count(type))
1740 return NULL;
1741
1742 m_function_types.insert(type);
1743
Sean Callananeddeb3b2011-10-28 23:38:38 +00001744 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001745 const_cast<DeclContext*>(m_decl_context),
1746 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00001747 SourceLocation(),
Greg Clayton7b462cc2010-10-15 22:48:33 +00001748 m_decl_name.getAsIdentifierInfo(),
1749 QualType::getFromOpaquePtr(type),
1750 NULL,
1751 SC_Static,
1752 SC_Static,
1753 false,
1754 true);
Sean Callanan468574b2010-06-22 23:46:24 +00001755
Sean Callanan04949cc2010-08-12 23:45:38 +00001756 // We have to do more than just synthesize the FunctionDecl. We have to
1757 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
1758 // this, we raid the function's FunctionProtoType for types.
1759
Greg Clayton7b462cc2010-10-15 22:48:33 +00001760 QualType qual_type (QualType::getFromOpaquePtr(type));
Sean Callananfc4f2fb2011-12-14 01:13:04 +00001761 const FunctionProtoType *func_proto_type = qual_type.getTypePtr()->getAs<FunctionProtoType>();
Sean Callanan468574b2010-06-22 23:46:24 +00001762
Greg Clayton7b462cc2010-10-15 22:48:33 +00001763 if (func_proto_type)
Sean Callanand448c1b2010-06-23 18:58:10 +00001764 {
Greg Clayton7b462cc2010-10-15 22:48:33 +00001765 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan468574b2010-06-22 23:46:24 +00001766 unsigned ArgIndex;
1767
Sean Callanan880e6802011-10-07 23:18:13 +00001768 SmallVector<ParmVarDecl *, 5> parm_var_decls;
1769
Sean Callanan468574b2010-06-22 23:46:24 +00001770 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
1771 {
Greg Clayton7b462cc2010-10-15 22:48:33 +00001772 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan468574b2010-06-22 23:46:24 +00001773
Sean Callananeddeb3b2011-10-28 23:38:38 +00001774 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callanan880e6802011-10-07 23:18:13 +00001775 const_cast<DeclContext*>(m_decl_context),
1776 SourceLocation(),
1777 SourceLocation(),
1778 NULL,
1779 arg_qual_type,
1780 NULL,
1781 SC_Static,
Sean Callanan880e6802011-10-07 23:18:13 +00001782 NULL));
Sean Callanan468574b2010-06-22 23:46:24 +00001783 }
1784
Sean Callanan880e6802011-10-07 23:18:13 +00001785 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan468574b2010-06-22 23:46:24 +00001786 }
Sean Callanane0a64f72011-12-01 21:04:37 +00001787 else
1788 {
Greg Clayton5160ce52013-03-27 23:08:40 +00001789 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanane0a64f72011-12-01 21:04:37 +00001790
Sean Callanan8ef19772013-04-25 18:50:43 +00001791 if (log)
1792 log->Printf("Function type wasn't a FunctionProtoType");
Sean Callanane0a64f72011-12-01 21:04:37 +00001793 }
Sean Callanan468574b2010-06-22 23:46:24 +00001794
Greg Clayton7b462cc2010-10-15 22:48:33 +00001795 m_decls.push_back(func_decl);
Sean Callanan468574b2010-06-22 23:46:24 +00001796
Greg Clayton7b462cc2010-10-15 22:48:33 +00001797 return func_decl;
Sean Callanan468574b2010-06-22 23:46:24 +00001798}
Sean Callanan8ade1042010-07-27 00:55:47 +00001799
Greg Clayton6beaaa62011-01-17 03:46:26 +00001800clang::NamedDecl *
1801NameSearchContext::AddGenericFunDecl()
Sean Callanan8ade1042010-07-27 00:55:47 +00001802{
Sean Callanan2c777c42011-01-18 23:32:05 +00001803 FunctionProtoType::ExtProtoInfo proto_info;
1804
1805 proto_info.Variadic = true;
1806
Sean Callananeddeb3b2011-10-28 23:38:38 +00001807 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 +00001808 ArrayRef<QualType>(), // argument types
Sean Callananeddeb3b2011-10-28 23:38:38 +00001809 proto_info));
Greg Clayton7b462cc2010-10-15 22:48:33 +00001810
Sean Callanan8ade1042010-07-27 00:55:47 +00001811 return AddFunDecl(generic_function_type.getAsOpaquePtr());
1812}
Sean Callanan5666b672010-08-04 01:02:13 +00001813
Greg Clayton6beaaa62011-01-17 03:46:26 +00001814clang::NamedDecl *
1815NameSearchContext::AddTypeDecl(void *type)
Sean Callanan5666b672010-08-04 01:02:13 +00001816{
Greg Clayton1b03cb52011-01-23 00:34:52 +00001817 if (type)
1818 {
1819 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan5666b672010-08-04 01:02:13 +00001820
Sean Callananfc4f2fb2011-12-14 01:13:04 +00001821 if (const TagType *tag_type = qual_type->getAs<TagType>())
Greg Clayton1b03cb52011-01-23 00:34:52 +00001822 {
1823 TagDecl *tag_decl = tag_type->getDecl();
1824
1825 m_decls.push_back(tag_decl);
1826
1827 return tag_decl;
1828 }
Sean Callananfc4f2fb2011-12-14 01:13:04 +00001829 else if (const ObjCObjectType *objc_object_type = qual_type->getAs<ObjCObjectType>())
Greg Clayton1b03cb52011-01-23 00:34:52 +00001830 {
1831 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
1832
1833 m_decls.push_back((NamedDecl*)interface_decl);
1834
1835 return (NamedDecl*)interface_decl;
1836 }
Sean Callananc4b1ab42013-03-14 17:21:53 +00001837 else if (const TypedefType *typedef_type = qual_type->getAs<TypedefType>())
1838 {
1839 TypedefNameDecl *typedef_name_decl = typedef_type->getDecl();
1840
1841 m_decls.push_back(typedef_name_decl);
1842
1843 return (NamedDecl*)typedef_name_decl;
1844 }
Sean Callanan5666b672010-08-04 01:02:13 +00001845 }
Greg Clayton1b03cb52011-01-23 00:34:52 +00001846 return NULL;
Sean Callanan5666b672010-08-04 01:02:13 +00001847}
Greg Claytona2721472011-06-25 00:44:06 +00001848
1849void
1850NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
1851{
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001852 for (clang::NamedDecl *decl : result)
1853 m_decls.push_back (decl);
Greg Claytona2721472011-06-25 00:44:06 +00001854}
1855
1856void
1857NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
1858{
1859 m_decls.push_back (decl);
1860}