blob: b55292d84a8dcc795bdadf42eb6471e031b1da73 [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
Greg Clayton57ee3062013-07-11 22:46:58 +0000241 ClangASTType clang_type (type->GetClangFullType());
Sean Callanan12014a02011-12-08 23:45:45 +0000242
Greg Clayton57ee3062013-07-11 22:46:58 +0000243 if (!clang_type)
Sean Callanan12014a02011-12-08 23:45:45 +0000244 continue;
245
Greg Clayton57ee3062013-07-11 22:46:58 +0000246 const TagType *tag_type = clang_type.GetQualType()->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
Greg Clayton57ee3062013-07-11 22:46:58 +0000280 ClangASTType clang_type (type->GetClangFullType());
Sean Callanan12014a02011-12-08 23:45:45 +0000281
Greg Clayton57ee3062013-07-11 22:46:58 +0000282 if (!clang_type)
Sean Callanan12014a02011-12-08 23:45:45 +0000283 continue;
284
Greg Clayton57ee3062013-07-11 22:46:58 +0000285 const TagType *tag_type = clang_type.GetQualType()->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
Sean Callananf4c0a222013-12-20 04:09:05 +0000321 if (interface_decl->getSuperClass() &&
322 interface_decl->getSuperClass() != interface_decl)
323 CompleteType(interface_decl->getSuperClass());
324
Sean Callananba0aca72011-10-29 02:28:18 +0000325 if (log)
326 {
327 log->Printf(" [COID] After:");
328 ASTDumper dumper((Decl*)interface_decl);
329 dumper.ToLog(log, " [COID] ");
330 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000331}
332
Sean Callanan2cb5e522012-09-20 23:21:16 +0000333clang::ObjCInterfaceDecl *
334ClangASTSource::GetCompleteObjCInterface (clang::ObjCInterfaceDecl *interface_decl)
335{
336 lldb::ProcessSP process(m_target->GetProcessSP());
337
338 if (!process)
339 return NULL;
340
341 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
342
343 if (!language_runtime)
344 return NULL;
345
346 ConstString class_name(interface_decl->getNameAsString().c_str());
347
348 lldb::TypeSP complete_type_sp(language_runtime->LookupInCompleteClassCache(class_name));
349
350 if (!complete_type_sp)
351 return NULL;
352
Greg Clayton57ee3062013-07-11 22:46:58 +0000353 TypeFromUser complete_type = TypeFromUser(complete_type_sp->GetClangFullType());
Sean Callanan2cb5e522012-09-20 23:21:16 +0000354 lldb::clang_type_t complete_opaque_type = complete_type.GetOpaqueQualType();
355
356 if (!complete_opaque_type)
357 return NULL;
358
359 const clang::Type *complete_clang_type = QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr();
360 const ObjCInterfaceType *complete_interface_type = dyn_cast<ObjCInterfaceType>(complete_clang_type);
361
362 if (!complete_interface_type)
363 return NULL;
364
365 ObjCInterfaceDecl *complete_iface_decl(complete_interface_type->getDecl());
366
367 return complete_iface_decl;
368}
369
Sean Callanancc427fa2011-07-30 02:42:06 +0000370clang::ExternalLoadResult
Sean Callanan2cb5e522012-09-20 23:21:16 +0000371ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context,
Sean Callananba0aca72011-10-29 02:28:18 +0000372 bool (*predicate)(Decl::Kind),
373 llvm::SmallVectorImpl<Decl*> &decls)
Sean Callanan8106d802013-03-08 20:04:57 +0000374{
375 ClangASTMetrics::RegisterLexicalQuery();
376
Greg Clayton5160ce52013-03-27 23:08:40 +0000377 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananba0aca72011-10-29 02:28:18 +0000378
379 const Decl *context_decl = dyn_cast<Decl>(decl_context);
380
381 if (!context_decl)
382 return ELR_Failure;
383
384 static unsigned int invocation_id = 0;
385 unsigned int current_id = invocation_id++;
386
387 if (log)
388 {
389 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
Sean Callanan00f43622011-11-18 03:28:09 +0000390 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p with %s predicate",
Sean Callananba0aca72011-10-29 02:28:18 +0000391 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +0000392 m_ast_context,
Sean Callananba0aca72011-10-29 02:28:18 +0000393 context_named_decl->getNameAsString().c_str(),
Sean Callanan00f43622011-11-18 03:28:09 +0000394 context_decl->getDeclKindName(),
395 context_decl,
Sean Callananba0aca72011-10-29 02:28:18 +0000396 (predicate ? "non-null" : "null"));
397 else if(context_decl)
Sean Callanan00f43622011-11-18 03:28:09 +0000398 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%sDecl*)%p with %s predicate",
Sean Callananba0aca72011-10-29 02:28:18 +0000399 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +0000400 m_ast_context,
Sean Callananba0aca72011-10-29 02:28:18 +0000401 context_decl->getDeclKindName(),
Sean Callanan00f43622011-11-18 03:28:09 +0000402 context_decl,
Sean Callananba0aca72011-10-29 02:28:18 +0000403 (predicate ? "non-null" : "null"));
404 else
Sean Callanan00f43622011-11-18 03:28:09 +0000405 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context with %s predicate",
Sean Callananba0aca72011-10-29 02:28:18 +0000406 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +0000407 m_ast_context,
Sean Callananba0aca72011-10-29 02:28:18 +0000408 (predicate ? "non-null" : "null"));
409 }
410
411 Decl *original_decl = NULL;
412 ASTContext *original_ctx = NULL;
413
414 if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx))
415 return ELR_Failure;
416
417 if (log)
418 {
Sean Callanan933ca2e2013-02-28 03:12:58 +0000419 log->Printf(" FELD[%u] Original decl (ASTContext*)%p (Decl*)%p:", current_id, original_ctx, original_decl);
Sean Callananba0aca72011-10-29 02:28:18 +0000420 ASTDumper(original_decl).ToLog(log, " ");
421 }
422
Sean Callanan2cb5e522012-09-20 23:21:16 +0000423 if (ObjCInterfaceDecl *original_iface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl))
424 {
425 ObjCInterfaceDecl *complete_iface_decl = GetCompleteObjCInterface(original_iface_decl);
426
427 if (complete_iface_decl && (complete_iface_decl != original_iface_decl))
428 {
429 original_decl = complete_iface_decl;
430 original_ctx = &complete_iface_decl->getASTContext();
431
432 m_ast_importer->SetDeclOrigin(context_decl, original_iface_decl);
433 }
434 }
435
Sean Callananba0aca72011-10-29 02:28:18 +0000436 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
437 {
438 ExternalASTSource *external_source = original_ctx->getExternalSource();
439
440 if (external_source)
441 external_source->CompleteType (original_tag_decl);
442 }
443
Sean Callanan12014a02011-12-08 23:45:45 +0000444 const DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl);
Sean Callananba0aca72011-10-29 02:28:18 +0000445
446 if (!original_decl_context)
447 return ELR_Failure;
448
449 for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
450 iter != original_decl_context->decls_end();
451 ++iter)
452 {
453 Decl *decl = *iter;
454
455 if (!predicate || predicate(decl->getKind()))
456 {
457 if (log)
458 {
459 ASTDumper ast_dumper(decl);
460 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
Sean Callanan3d654b32012-09-24 22:25:51 +0000461 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 +0000462 else
Sean Callanan3d654b32012-09-24 22:25:51 +0000463 log->Printf(" FELD[%d] Adding lexical %sDecl %s", current_id, decl->getDeclKindName(), ast_dumper.GetCString());
Sean Callananba0aca72011-10-29 02:28:18 +0000464 }
465
Sean Callanan686b2312011-11-16 18:20:47 +0000466 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, original_ctx, decl);
Sean Callananba0aca72011-10-29 02:28:18 +0000467
Sean Callanan61b33f72012-03-20 21:11:12 +0000468 if (!copied_decl)
469 continue;
470
Sean Callanancf128622012-03-15 01:53:17 +0000471 if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl))
472 {
473 QualType copied_field_type = copied_field->getType();
474
Sean Callanan6b200d02013-03-21 22:15:41 +0000475 m_ast_importer->RequireCompleteType(copied_field_type);
Sean Callanancf128622012-03-15 01:53:17 +0000476 }
477
Sean Callananba0aca72011-10-29 02:28:18 +0000478 decls.push_back(copied_decl);
Sean Callanan04b2bfa2013-05-09 01:09:49 +0000479
480 DeclContext *decl_context_non_const = const_cast<DeclContext *>(decl_context);
481
482 if (copied_decl->getDeclContext() != decl_context)
483 {
484 if (copied_decl->getDeclContext()->containsDecl(copied_decl))
485 copied_decl->getDeclContext()->removeDecl(copied_decl);
486 copied_decl->setDeclContext(decl_context_non_const);
487 }
488
489 if (!decl_context_non_const->containsDecl(copied_decl))
490 decl_context_non_const->addDeclInternal(copied_decl);
Sean Callananba0aca72011-10-29 02:28:18 +0000491 }
492 }
493
494 return ELR_AlreadyLoaded;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495}
496
Sean Callananfb3e4302011-10-29 19:50:43 +0000497void
498ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
499{
500 assert (m_ast_context);
501
Sean Callanan8106d802013-03-08 20:04:57 +0000502 ClangASTMetrics::RegisterVisibleQuery();
503
Sean Callananfb3e4302011-10-29 19:50:43 +0000504 const ConstString name(context.m_decl_name.getAsString().c_str());
505
Greg Clayton5160ce52013-03-27 23:08:40 +0000506 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfb3e4302011-10-29 19:50:43 +0000507
508 static unsigned int invocation_id = 0;
509 unsigned int current_id = invocation_id++;
510
511 if (log)
512 {
513 if (!context.m_decl_context)
Sean Callanana6e61a72012-01-13 22:05:55 +0000514 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 +0000515 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
Sean Callanan00f43622011-11-18 03:28:09 +0000516 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 +0000517 else
Sean Callanan00f43622011-11-18 03:28:09 +0000518 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 +0000519 }
520
521 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
522
523 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
524 {
525 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
526
527 if (log && log->GetVerbose())
528 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
529 current_id,
530 namespace_map.get(),
531 (int)namespace_map->size());
532
533 if (!namespace_map)
534 return;
535
536 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
537 i != e;
538 ++i)
539 {
540 if (log)
541 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
542 current_id,
543 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
544 i->first->GetFileSpec().GetFilename().GetCString());
545
546 FindExternalVisibleDecls(context,
547 i->first,
548 i->second,
549 current_id);
550 }
551 }
Sean Callanan100d74e2011-11-15 21:50:18 +0000552 else if (isa<ObjCInterfaceDecl>(context.m_decl_context))
Sean Callanand5c17ed2011-11-15 02:11:17 +0000553 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000554 FindObjCPropertyAndIvarDecls(context);
Sean Callanand5c17ed2011-11-15 02:11:17 +0000555 }
Sean Callananfb3e4302011-10-29 19:50:43 +0000556 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
557 {
558 // we shouldn't be getting FindExternalVisibleDecls calls for these
559 return;
560 }
561 else
562 {
563 ClangNamespaceDecl namespace_decl;
564
565 if (log)
566 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
567
568 FindExternalVisibleDecls(context,
569 lldb::ModuleSP(),
570 namespace_decl,
571 current_id);
572 }
573
574 if (!context.m_namespace_map->empty())
575 {
576 if (log && log->GetVerbose())
577 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
578 current_id,
579 context.m_namespace_map.get(),
580 (int)context.m_namespace_map->size());
581
582 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
583
584 if (clang_namespace_decl)
585 clang_namespace_decl->setHasExternalVisibleStorage();
586 }
587}
588
589void
590ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
591 lldb::ModuleSP module_sp,
592 ClangNamespaceDecl &namespace_decl,
593 unsigned int current_id)
594{
595 assert (m_ast_context);
596
Greg Clayton5160ce52013-03-27 23:08:40 +0000597 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callananfb3e4302011-10-29 19:50:43 +0000598
599 SymbolContextList sc_list;
600
601 const ConstString name(context.m_decl_name.getAsString().c_str());
602
603 const char *name_unique_cstr = name.GetCString();
604
Sean Callanan5b26f272012-02-04 08:49:35 +0000605 static ConstString id_name("id");
606 static ConstString Class_name("Class");
607
608 if (name == id_name || name == Class_name)
609 return;
610
Sean Callananfb3e4302011-10-29 19:50:43 +0000611 if (name_unique_cstr == NULL)
612 return;
613
614 // The ClangASTSource is not responsible for finding $-names.
615 if (name_unique_cstr[0] == '$')
616 return;
617
618 if (module_sp && namespace_decl)
619 {
620 ClangNamespaceDecl found_namespace_decl;
621
622 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
623
624 if (symbol_vendor)
625 {
626 SymbolContext null_sc;
627
628 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
629
630 if (found_namespace_decl)
631 {
632 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
633
634 if (log)
635 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
636 current_id,
637 name.GetCString(),
638 module_sp->GetFileSpec().GetFilename().GetCString());
639 }
640 }
641 }
642 else
643 {
Enrico Granata17598482012-11-08 02:22:02 +0000644 const ModuleList &target_images = m_target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +0000645 Mutex::Locker modules_locker (target_images.GetMutex());
Sean Callananfb3e4302011-10-29 19:50:43 +0000646
Greg Claytonc7bece562013-01-25 18:06:21 +0000647 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i)
Sean Callananfb3e4302011-10-29 19:50:43 +0000648 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +0000649 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
Sean Callananfb3e4302011-10-29 19:50:43 +0000650
651 if (!image)
652 continue;
653
654 ClangNamespaceDecl found_namespace_decl;
655
656 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
657
658 if (!symbol_vendor)
659 continue;
660
661 SymbolContext null_sc;
662
663 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
664
665 if (found_namespace_decl)
666 {
667 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
668
669 if (log)
670 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
671 current_id,
672 name.GetCString(),
673 image->GetFileSpec().GetFilename().GetCString());
674 }
675 }
676 }
677
Sean Callananfb3e4302011-10-29 19:50:43 +0000678 do
679 {
680 TypeList types;
681 SymbolContext null_sc;
Greg Clayton84db9102012-03-26 23:03:23 +0000682 const bool exact_match = false;
Sean Callananbfb7c682011-12-19 19:38:39 +0000683
Sean Callananfb3e4302011-10-29 19:50:43 +0000684 if (module_sp && namespace_decl)
Greg Clayton84db9102012-03-26 23:03:23 +0000685 module_sp->FindTypesInNamespace(null_sc, name, &namespace_decl, 1, types);
Sean Callananbfb7c682011-12-19 19:38:39 +0000686 else
Greg Clayton29399a22012-04-06 17:41:13 +0000687 m_target->GetImages().FindTypes(null_sc, name, exact_match, 1, types);
Sean Callananfb3e4302011-10-29 19:50:43 +0000688
689 if (types.GetSize())
690 {
691 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
692
693 if (log)
694 {
695 const char *name_string = type_sp->GetName().GetCString();
696
697 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
698 current_id,
699 name.GetCString(),
700 (name_string ? name_string : "<anonymous>"));
701 }
Sean Callananbc47dfc2012-09-11 21:44:01 +0000702
Greg Clayton57ee3062013-07-11 22:46:58 +0000703 ClangASTType full_type = type_sp->GetClangFullType();
Sean Callanan0eed0d42011-12-06 03:41:14 +0000704
Greg Clayton57ee3062013-07-11 22:46:58 +0000705 ClangASTType copied_clang_type (GuardedCopyType(full_type));
Sean Callanan0eed0d42011-12-06 03:41:14 +0000706
Greg Clayton57ee3062013-07-11 22:46:58 +0000707 if (!copied_clang_type)
Sean Callanane0a64f72011-12-01 21:04:37 +0000708 {
709 if (log)
Sean Callanan7be70e82012-12-19 23:05:01 +0000710 log->Printf(" CAS::FEVD[%u] - Couldn't export a type",
Sean Callanan0eed0d42011-12-06 03:41:14 +0000711 current_id);
712
Sean Callanane0a64f72011-12-01 21:04:37 +0000713 break;
714 }
715
Greg Clayton57ee3062013-07-11 22:46:58 +0000716 context.AddTypeDecl(copied_clang_type);
Sean Callananfb3e4302011-10-29 19:50:43 +0000717 }
Sean Callanan7be70e82012-12-19 23:05:01 +0000718 else
719 {
720 do
721 {
722 // Couldn't find any types elsewhere. Try the Objective-C runtime if one exists.
723
724 lldb::ProcessSP process(m_target->GetProcessSP());
725
726 if (!process)
727 break;
728
729 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
730
731 if (!language_runtime)
732 break;
733
734 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
735
736 if (!type_vendor)
737 break;
738
739 bool append = false;
740 uint32_t max_matches = 1;
741 std::vector <ClangASTType> types;
742
743 if (!type_vendor->FindTypes(name,
744 append,
745 max_matches,
746 types))
747 break;
748
749 if (log)
750 {
751 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\" in the runtime",
752 current_id,
753 name.GetCString());
754 }
755
Greg Clayton57ee3062013-07-11 22:46:58 +0000756 ClangASTType copied_clang_type (GuardedCopyType(types[0]));
Sean Callanan7be70e82012-12-19 23:05:01 +0000757
Greg Clayton57ee3062013-07-11 22:46:58 +0000758 if (!copied_clang_type)
Sean Callanan7be70e82012-12-19 23:05:01 +0000759 {
760 if (log)
761 log->Printf(" CAS::FEVD[%u] - Couldn't export a type from the runtime",
762 current_id);
763
764 break;
765 }
766
Greg Clayton57ee3062013-07-11 22:46:58 +0000767 context.AddTypeDecl(copied_clang_type);
Sean Callanan7be70e82012-12-19 23:05:01 +0000768 }
769 while(0);
770 }
Sean Callanan09ab4b72011-11-30 22:11:59 +0000771
Sean Callananfb3e4302011-10-29 19:50:43 +0000772 } while(0);
773}
774
Sean Callanan55400942012-11-02 17:09:58 +0000775template <class D> class TaggedASTDecl {
776public:
777 TaggedASTDecl() : decl(NULL) { }
778 TaggedASTDecl(D *_decl) : decl(_decl) { }
779 bool IsValid() const { return (decl != NULL); }
780 bool IsInvalid() const { return !IsValid(); }
781 D *operator->() const { return decl; }
782 D *decl;
783};
784
785template <class D2, template <class D> class TD, class D1>
786TD<D2>
787DynCast(TD<D1> source)
788{
789 return TD<D2> (dyn_cast<D2>(source.decl));
790}
791
792template <class D = Decl> class DeclFromParser;
793template <class D = Decl> class DeclFromUser;
794
795template <class D> class DeclFromParser : public TaggedASTDecl<D> {
796public:
797 DeclFromParser() : TaggedASTDecl<D>() { }
798 DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) { }
799
800 DeclFromUser<D> GetOrigin(ClangASTImporter *importer);
801};
802
803template <class D> class DeclFromUser : public TaggedASTDecl<D> {
804public:
805 DeclFromUser() : TaggedASTDecl<D>() { }
806 DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) { }
807
808 DeclFromParser<D> Import(ClangASTImporter *importer, ASTContext &dest_ctx);
809};
810
811template <class D>
812DeclFromUser<D>
813DeclFromParser<D>::GetOrigin(ClangASTImporter *importer)
814{
815 DeclFromUser <> origin_decl;
816 importer->ResolveDeclOrigin(this->decl, &origin_decl.decl, NULL);
817 if (origin_decl.IsInvalid())
818 return DeclFromUser<D>();
819 return DeclFromUser<D>(dyn_cast<D>(origin_decl.decl));
820}
821
822template <class D>
823DeclFromParser<D>
824DeclFromUser<D>::Import(ClangASTImporter *importer, ASTContext &dest_ctx)
825{
826 DeclFromParser <> parser_generic_decl(importer->CopyDecl(&dest_ctx, &this->decl->getASTContext(), this->decl));
827 if (parser_generic_decl.IsInvalid())
828 return DeclFromParser<D>();
829 return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
830}
831
Sean Callananc83e3412012-11-28 03:23:20 +0000832static bool
Sean Callananbc47dfc2012-09-11 21:44:01 +0000833FindObjCMethodDeclsWithOrigin (unsigned int current_id,
834 NameSearchContext &context,
835 ObjCInterfaceDecl *original_interface_decl,
836 clang::ASTContext *ast_context,
837 ClangASTImporter *ast_importer,
838 const char *log_info)
839{
840 const DeclarationName &decl_name(context.m_decl_name);
841 clang::ASTContext *original_ctx = &original_interface_decl->getASTContext();
842
843 Selector original_selector;
844
845 if (decl_name.isObjCZeroArgSelector())
846 {
847 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name.getAsString());
848 original_selector = original_ctx->Selectors.getSelector(0, &ident);
849 }
850 else if (decl_name.isObjCOneArgSelector())
851 {
852 const std::string &decl_name_string = decl_name.getAsString();
853 std::string decl_name_string_without_colon(decl_name_string.c_str(), decl_name_string.length() - 1);
854 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name_string_without_colon.c_str());
855 original_selector = original_ctx->Selectors.getSelector(1, &ident);
856 }
857 else
858 {
859 SmallVector<IdentifierInfo *, 4> idents;
860
861 clang::Selector sel = decl_name.getObjCSelector();
862
Andy Gibbsa297a972013-06-19 19:04:53 +0000863 unsigned num_args = sel.getNumArgs();
Sean Callananbc47dfc2012-09-11 21:44:01 +0000864
865 for (unsigned i = 0;
866 i != num_args;
867 ++i)
868 {
869 idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i)));
870 }
871
872 original_selector = original_ctx->Selectors.getSelector(num_args, idents.data());
873 }
874
875 DeclarationName original_decl_name(original_selector);
876
877 ObjCInterfaceDecl::lookup_result result = original_interface_decl->lookup(original_decl_name);
878
Sean Callanan5deaa4c2012-12-21 21:34:42 +0000879 if (result.empty())
Sean Callananc83e3412012-11-28 03:23:20 +0000880 return false;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000881
Sean Callanan5deaa4c2012-12-21 21:34:42 +0000882 if (!result[0])
Sean Callananc83e3412012-11-28 03:23:20 +0000883 return false;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000884
Sean Callanan01c8cb82013-09-04 23:25:26 +0000885 for (NamedDecl *named_decl : result)
Sean Callananbc47dfc2012-09-11 21:44:01 +0000886 {
Sean Callanan01c8cb82013-09-04 23:25:26 +0000887 ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(named_decl);
888
889 if (!result_method)
890 return false;
891
892 Decl *copied_decl = ast_importer->CopyDecl(ast_context, &result_method->getASTContext(), result_method);
893
894 if (!copied_decl)
895 return false;
896
897 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
898
899 if (!copied_method_decl)
900 return false;
901
902 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
903
904 if (log)
905 {
906 ASTDumper dumper((Decl*)copied_method_decl);
907 log->Printf(" CAS::FOMD[%d] found (%s) %s", current_id, log_info, dumper.GetCString());
908 }
909
910 context.AddNamedDecl(copied_method_decl);
Sean Callananbc47dfc2012-09-11 21:44:01 +0000911 }
912
Sean Callananc83e3412012-11-28 03:23:20 +0000913 return true;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000914}
915
Sean Callanan0730e9c2011-11-09 19:33:21 +0000916void
917ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
918{
Greg Clayton5160ce52013-03-27 23:08:40 +0000919 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan0730e9c2011-11-09 19:33:21 +0000920
Sean Callanan46198ff2011-11-11 20:37:26 +0000921 static unsigned int invocation_id = 0;
922 unsigned int current_id = invocation_id++;
923
Sean Callanan0730e9c2011-11-09 19:33:21 +0000924 const DeclarationName &decl_name(context.m_decl_name);
925 const DeclContext *decl_ctx(context.m_decl_context);
926
927 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
928
929 if (!interface_decl)
930 return;
931
Sean Callanana6582262012-04-05 00:12:52 +0000932 do
933 {
934 Decl *original_decl = NULL;
935 ASTContext *original_ctx = NULL;
936
937 m_ast_importer->ResolveDeclOrigin(interface_decl, &original_decl, &original_ctx);
938
939 if (!original_decl)
940 break;
941
942 ObjCInterfaceDecl *original_interface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl);
943
Sean Callananc83e3412012-11-28 03:23:20 +0000944 if (FindObjCMethodDeclsWithOrigin(current_id,
945 context,
946 original_interface_decl,
947 m_ast_context,
948 m_ast_importer,
949 "at origin"))
950 return; // found it, no need to look any further
Sean Callanana6582262012-04-05 00:12:52 +0000951 } while (0);
952
Sean Callanan0730e9c2011-11-09 19:33:21 +0000953 StreamString ss;
Sean Callanan0eed0d42011-12-06 03:41:14 +0000954
Sean Callanan0730e9c2011-11-09 19:33:21 +0000955 if (decl_name.isObjCZeroArgSelector())
956 {
957 ss.Printf("%s", decl_name.getAsString().c_str());
958 }
959 else if (decl_name.isObjCOneArgSelector())
960 {
Sean Callanan4fb79b72011-11-14 18:29:46 +0000961 ss.Printf("%s", decl_name.getAsString().c_str());
Sean Callanan0730e9c2011-11-09 19:33:21 +0000962 }
963 else
964 {
965 clang::Selector sel = decl_name.getObjCSelector();
966
967 for (unsigned i = 0, e = sel.getNumArgs();
968 i != e;
969 ++i)
970 {
971 llvm::StringRef r = sel.getNameForSlot(i);
972 ss.Printf("%s:", r.str().c_str());
973 }
974 }
975 ss.Flush();
976
Sean Callanan46198ff2011-11-11 20:37:26 +0000977 ConstString selector_name(ss.GetData());
978
Sean Callanan0730e9c2011-11-09 19:33:21 +0000979 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000980 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
981 current_id,
982 m_ast_context,
Sean Callanan46198ff2011-11-11 20:37:26 +0000983 interface_decl->getNameAsString().c_str(),
984 selector_name.AsCString());
Sean Callanan46198ff2011-11-11 20:37:26 +0000985 SymbolContextList sc_list;
986
987 const bool include_symbols = false;
Sean Callanan9df05fb2012-02-10 22:52:19 +0000988 const bool include_inlines = false;
Sean Callanan46198ff2011-11-11 20:37:26 +0000989 const bool append = false;
990
Sean Callanana9bc0652012-01-19 02:17:40 +0000991 std::string interface_name = interface_decl->getNameAsString();
992
993 do
994 {
995 StreamString ms;
996 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());
997 ms.Flush();
998 ConstString instance_method_name(ms.GetData());
999
Sean Callanan9df05fb2012-02-10 22:52:19 +00001000 m_target->GetImages().FindFunctions(instance_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanana9bc0652012-01-19 02:17:40 +00001001
1002 if (sc_list.GetSize())
1003 break;
1004
1005 ms.Clear();
1006 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());
1007 ms.Flush();
1008 ConstString class_method_name(ms.GetData());
1009
Sean Callanan9df05fb2012-02-10 22:52:19 +00001010 m_target->GetImages().FindFunctions(class_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanana9bc0652012-01-19 02:17:40 +00001011
1012 if (sc_list.GetSize())
1013 break;
1014
1015 // Fall back and check for methods in categories. If we find methods this way, we need to check that they're actually in
1016 // categories on the desired class.
1017
1018 SymbolContextList candidate_sc_list;
1019
Sean Callanan9df05fb2012-02-10 22:52:19 +00001020 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, include_inlines, append, candidate_sc_list);
Sean Callanana9bc0652012-01-19 02:17:40 +00001021
1022 for (uint32_t ci = 0, ce = candidate_sc_list.GetSize();
1023 ci != ce;
1024 ++ci)
1025 {
1026 SymbolContext candidate_sc;
1027
1028 if (!candidate_sc_list.GetContextAtIndex(ci, candidate_sc))
1029 continue;
1030
1031 if (!candidate_sc.function)
1032 continue;
1033
1034 const char *candidate_name = candidate_sc.function->GetName().AsCString();
1035
1036 const char *cursor = candidate_name;
1037
1038 if (*cursor != '+' && *cursor != '-')
1039 continue;
1040
1041 ++cursor;
1042
1043 if (*cursor != '[')
1044 continue;
1045
1046 ++cursor;
1047
1048 size_t interface_len = interface_name.length();
1049
1050 if (strncmp(cursor, interface_name.c_str(), interface_len))
1051 continue;
1052
1053 cursor += interface_len;
1054
1055 if (*cursor == ' ' || *cursor == '(')
1056 sc_list.Append(candidate_sc);
1057 }
1058 }
1059 while (0);
Sean Callanan46198ff2011-11-11 20:37:26 +00001060
Sean Callananbc47dfc2012-09-11 21:44:01 +00001061 if (sc_list.GetSize())
Sean Callanan46198ff2011-11-11 20:37:26 +00001062 {
Sean Callananbc47dfc2012-09-11 21:44:01 +00001063 // We found a good function symbol. Use that.
Sean Callanan46198ff2011-11-11 20:37:26 +00001064
Sean Callananbc47dfc2012-09-11 21:44:01 +00001065 for (uint32_t i = 0, e = sc_list.GetSize();
1066 i != e;
1067 ++i)
Sean Callanan46198ff2011-11-11 20:37:26 +00001068 {
Sean Callananbc47dfc2012-09-11 21:44:01 +00001069 SymbolContext sc;
Sean Callanan46198ff2011-11-11 20:37:26 +00001070
Sean Callananbc47dfc2012-09-11 21:44:01 +00001071 if (!sc_list.GetContextAtIndex(i, sc))
Sean Callanan46198ff2011-11-11 20:37:26 +00001072 continue;
1073
Sean Callananbc47dfc2012-09-11 21:44:01 +00001074 if (!sc.function)
Sean Callanan46198ff2011-11-11 20:37:26 +00001075 continue;
1076
Sean Callananbc47dfc2012-09-11 21:44:01 +00001077 DeclContext *function_ctx = sc.function->GetClangDeclContext();
1078
1079 if (!function_ctx)
1080 continue;
1081
1082 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
1083
1084 if (!method_decl)
1085 continue;
1086
1087 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
1088
1089 if (!found_interface_decl)
1090 continue;
1091
1092 if (found_interface_decl->getName() == interface_decl->getName())
Sean Callanan46198ff2011-11-11 20:37:26 +00001093 {
Sean Callananbc47dfc2012-09-11 21:44:01 +00001094 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
1095
1096 if (!copied_decl)
1097 continue;
1098
1099 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
1100
1101 if (!copied_method_decl)
1102 continue;
1103
1104 if (log)
1105 {
1106 ASTDumper dumper((Decl*)copied_method_decl);
Sean Callanan55400942012-11-02 17:09:58 +00001107 log->Printf(" CAS::FOMD[%d] found (in symbols) %s", current_id, dumper.GetCString());
Sean Callananbc47dfc2012-09-11 21:44:01 +00001108 }
1109
1110 context.AddNamedDecl(copied_method_decl);
Sean Callanan46198ff2011-11-11 20:37:26 +00001111 }
Sean Callanan46198ff2011-11-11 20:37:26 +00001112 }
Sean Callanan55400942012-11-02 17:09:58 +00001113
1114 return;
Sean Callanan46198ff2011-11-11 20:37:26 +00001115 }
Sean Callanan55400942012-11-02 17:09:58 +00001116
1117 // Try the debug information.
1118
1119 do
Sean Callananbc47dfc2012-09-11 21:44:01 +00001120 {
Sean Callanan55400942012-11-02 17:09:58 +00001121 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(interface_decl));
1122
1123 if (!complete_interface_decl)
1124 break;
1125
1126 // We found the complete interface. The runtime never needs to be queried in this scenario.
1127
1128 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
1129
1130 if (complete_interface_decl == interface_decl)
1131 break; // already checked this one
1132
1133 if (log)
1134 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1135 current_id,
1136 complete_interface_decl,
1137 &complete_iface_decl->getASTContext());
1138
1139 FindObjCMethodDeclsWithOrigin(current_id,
1140 context,
1141 complete_interface_decl,
1142 m_ast_context,
1143 m_ast_importer,
1144 "in debug info");
1145
1146 return;
Sean Callananbc47dfc2012-09-11 21:44:01 +00001147 }
Sean Callanan55400942012-11-02 17:09:58 +00001148 while (0);
Sean Callanan5b26f272012-02-04 08:49:35 +00001149
Sean Callanan55400942012-11-02 17:09:58 +00001150 do
1151 {
1152 // Check the runtime only if the debug information didn't have a complete interface.
1153
1154 lldb::ProcessSP process(m_target->GetProcessSP());
1155
1156 if (!process)
1157 break;
1158
1159 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1160
Sean Callanan7be70e82012-12-19 23:05:01 +00001161 if (!language_runtime)
1162 break;
1163
Sean Callanan55400942012-11-02 17:09:58 +00001164 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
1165
1166 if (!type_vendor)
1167 break;
1168
1169 ConstString interface_name(interface_decl->getNameAsString().c_str());
1170 bool append = false;
1171 uint32_t max_matches = 1;
1172 std::vector <ClangASTType> types;
1173
1174 if (!type_vendor->FindTypes(interface_name,
1175 append,
1176 max_matches,
1177 types))
1178 break;
1179
1180 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
1181
1182 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
1183
1184 if (!runtime_interface_type)
1185 break;
1186
1187 ObjCInterfaceDecl *runtime_interface_decl = runtime_interface_type->getDecl();
1188
1189 FindObjCMethodDeclsWithOrigin(current_id,
1190 context,
1191 runtime_interface_decl,
1192 m_ast_context,
1193 m_ast_importer,
1194 "in runtime");
1195 }
1196 while(0);
Sean Callanan5b26f272012-02-04 08:49:35 +00001197}
1198
Sean Callanan72772842012-02-22 23:57:45 +00001199static bool
1200FindObjCPropertyAndIvarDeclsWithOrigin (unsigned int current_id,
1201 NameSearchContext &context,
1202 clang::ASTContext &ast_context,
1203 ClangASTImporter *ast_importer,
1204 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl)
1205{
Greg Clayton5160ce52013-03-27 23:08:40 +00001206 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan72772842012-02-22 23:57:45 +00001207
1208 if (origin_iface_decl.IsInvalid())
1209 return false;
1210
1211 std::string name_str = context.m_decl_name.getAsString();
1212 StringRef name(name_str.c_str());
1213 IdentifierInfo &name_identifier(origin_iface_decl->getASTContext().Idents.get(name));
1214
1215 DeclFromUser<ObjCPropertyDecl> origin_property_decl(origin_iface_decl->FindPropertyDeclaration(&name_identifier));
1216
1217 bool found = false;
1218
1219 if (origin_property_decl.IsValid())
1220 {
1221 DeclFromParser<ObjCPropertyDecl> parser_property_decl(origin_property_decl.Import(ast_importer, ast_context));
1222 if (parser_property_decl.IsValid())
1223 {
1224 if (log)
1225 {
1226 ASTDumper dumper((Decl*)parser_property_decl.decl);
1227 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1228 }
1229
1230 context.AddNamedDecl(parser_property_decl.decl);
1231 found = true;
1232 }
1233 }
1234
1235 DeclFromUser<ObjCIvarDecl> origin_ivar_decl(origin_iface_decl->getIvarDecl(&name_identifier));
1236
1237 if (origin_ivar_decl.IsValid())
1238 {
1239 DeclFromParser<ObjCIvarDecl> parser_ivar_decl(origin_ivar_decl.Import(ast_importer, ast_context));
1240 if (parser_ivar_decl.IsValid())
1241 {
1242 if (log)
1243 {
1244 ASTDumper dumper((Decl*)parser_ivar_decl.decl);
1245 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1246 }
1247
1248 context.AddNamedDecl(parser_ivar_decl.decl);
1249 found = true;
1250 }
1251 }
1252
1253 return found;
1254}
1255
Sean Callanand5c17ed2011-11-15 02:11:17 +00001256void
Sean Callanan5b26f272012-02-04 08:49:35 +00001257ClangASTSource::FindObjCPropertyAndIvarDecls (NameSearchContext &context)
Sean Callanand5c17ed2011-11-15 02:11:17 +00001258{
Greg Clayton5160ce52013-03-27 23:08:40 +00001259 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanand5c17ed2011-11-15 02:11:17 +00001260
1261 static unsigned int invocation_id = 0;
1262 unsigned int current_id = invocation_id++;
1263
Sean Callanan5b26f272012-02-04 08:49:35 +00001264 DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl(cast<ObjCInterfaceDecl>(context.m_decl_context));
1265 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(parser_iface_decl.GetOrigin(m_ast_importer));
Sean Callanan72772842012-02-22 23:57:45 +00001266
1267 ConstString class_name(parser_iface_decl->getNameAsString().c_str());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001268
1269 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +00001270 log->Printf("ClangASTSource::FindObjCPropertyAndIvarDecls[%d] on (ASTContext*)%p for '%s.%s'",
Sean Callanand5c17ed2011-11-15 02:11:17 +00001271 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +00001272 m_ast_context,
Sean Callanan5b26f272012-02-04 08:49:35 +00001273 parser_iface_decl->getNameAsString().c_str(),
Sean Callanan72772842012-02-22 23:57:45 +00001274 context.m_decl_name.getAsString().c_str());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001275
Sean Callanan72772842012-02-22 23:57:45 +00001276 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1277 context,
1278 *m_ast_context,
1279 m_ast_importer,
1280 origin_iface_decl))
1281 return;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001282
Sean Callanan72772842012-02-22 23:57:45 +00001283 if (log)
1284 log->Printf("CAS::FOPD[%d] couldn't find the property on origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p, searching elsewhere...",
1285 current_id,
1286 origin_iface_decl.decl,
1287 &origin_iface_decl->getASTContext());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001288
Sean Callanan72772842012-02-22 23:57:45 +00001289 SymbolContext null_sc;
1290 TypeList type_list;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001291
Sean Callananbc47dfc2012-09-11 21:44:01 +00001292 do
1293 {
Sean Callanan2cb5e522012-09-20 23:21:16 +00001294 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(parser_iface_decl.decl));
Sean Callananbc47dfc2012-09-11 21:44:01 +00001295
Sean Callanan2cb5e522012-09-20 23:21:16 +00001296 if (!complete_interface_decl)
Sean Callananbc47dfc2012-09-11 21:44:01 +00001297 break;
1298
Sean Callanan55400942012-11-02 17:09:58 +00001299 // We found the complete interface. The runtime never needs to be queried in this scenario.
1300
Sean Callanan2cb5e522012-09-20 23:21:16 +00001301 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
Sean Callananbc47dfc2012-09-11 21:44:01 +00001302
1303 if (complete_iface_decl.decl == origin_iface_decl.decl)
1304 break; // already checked this one
1305
1306 if (log)
1307 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1308 current_id,
1309 complete_iface_decl.decl,
1310 &complete_iface_decl->getASTContext());
1311
Sean Callanan55400942012-11-02 17:09:58 +00001312 FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1313 context,
1314 *m_ast_context,
1315 m_ast_importer,
1316 complete_iface_decl);
1317
1318 return;
Sean Callananbc47dfc2012-09-11 21:44:01 +00001319 }
1320 while(0);
Sean Callanan72772842012-02-22 23:57:45 +00001321
Sean Callananbc47dfc2012-09-11 21:44:01 +00001322 do
1323 {
Sean Callanan55400942012-11-02 17:09:58 +00001324 // Check the runtime only if the debug information didn't have a complete interface.
1325
1326 lldb::ProcessSP process(m_target->GetProcessSP());
1327
1328 if (!process)
1329 return;
1330
1331 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1332
1333 if (!language_runtime)
1334 return;
Sean Callananbc47dfc2012-09-11 21:44:01 +00001335
1336 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
1337
1338 if (!type_vendor)
1339 break;
1340
1341 bool append = false;
1342 uint32_t max_matches = 1;
1343 std::vector <ClangASTType> types;
1344
1345 if (!type_vendor->FindTypes(class_name,
1346 append,
1347 max_matches,
1348 types))
1349 break;
1350
1351 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
1352
1353 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
1354
1355 if (!runtime_interface_type)
1356 break;
1357
1358 DeclFromUser<const ObjCInterfaceDecl> runtime_iface_decl(runtime_interface_type->getDecl());
1359
1360 if (log)
1361 log->Printf("CAS::FOPD[%d] trying runtime (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1362 current_id,
1363 runtime_iface_decl.decl,
1364 &runtime_iface_decl->getASTContext());
1365
1366 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1367 context,
1368 *m_ast_context,
1369 m_ast_importer,
1370 runtime_iface_decl))
1371 return;
1372 }
1373 while(0);
Sean Callanan5b26f272012-02-04 08:49:35 +00001374}
1375
1376typedef llvm::DenseMap <const FieldDecl *, uint64_t> FieldOffsetMap;
1377typedef llvm::DenseMap <const CXXRecordDecl *, CharUnits> BaseOffsetMap;
1378
1379template <class D, class O>
1380static bool
1381ImportOffsetMap (llvm::DenseMap <const D*, O> &destination_map,
1382 llvm::DenseMap <const D*, O> &source_map,
1383 ClangASTImporter *importer,
1384 ASTContext &dest_ctx)
1385{
1386 typedef llvm::DenseMap <const D*, O> MapType;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001387
Sean Callanan5b26f272012-02-04 08:49:35 +00001388 for (typename MapType::iterator fi = source_map.begin(), fe = source_map.end();
1389 fi != fe;
1390 ++fi)
1391 {
1392 DeclFromUser <D> user_decl(const_cast<D*>(fi->first));
1393 DeclFromParser <D> parser_decl(user_decl.Import(importer, dest_ctx));
1394 if (parser_decl.IsInvalid())
1395 return false;
1396 destination_map.insert(std::pair<const D *, O>(parser_decl.decl, fi->second));
1397 }
1398
1399 return true;
1400}
1401
1402template <bool IsVirtual> bool ExtractBaseOffsets (const ASTRecordLayout &record_layout,
1403 DeclFromUser<const CXXRecordDecl> &record,
1404 BaseOffsetMap &base_offsets)
1405{
1406 for (CXXRecordDecl::base_class_const_iterator
1407 bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()),
1408 be = (IsVirtual ? record->vbases_end() : record->bases_end());
1409 bi != be;
1410 ++bi)
1411 {
1412 if (!IsVirtual && bi->isVirtual())
1413 continue;
1414
1415 const clang::Type *origin_base_type = bi->getType().getTypePtr();
1416 const clang::RecordType *origin_base_record_type = origin_base_type->getAs<RecordType>();
1417
1418 if (!origin_base_record_type)
1419 return false;
1420
1421 DeclFromUser <RecordDecl> origin_base_record(origin_base_record_type->getDecl());
1422
1423 if (origin_base_record.IsInvalid())
1424 return false;
1425
1426 DeclFromUser <CXXRecordDecl> origin_base_cxx_record(DynCast<CXXRecordDecl>(origin_base_record));
1427
1428 if (origin_base_cxx_record.IsInvalid())
1429 return false;
1430
1431 CharUnits base_offset;
1432
1433 if (IsVirtual)
1434 base_offset = record_layout.getVBaseClassOffset(origin_base_cxx_record.decl);
1435 else
1436 base_offset = record_layout.getBaseClassOffset(origin_base_cxx_record.decl);
1437
1438 base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(origin_base_cxx_record.decl, base_offset));
1439 }
1440
1441 return true;
1442}
1443
1444bool
1445ClangASTSource::layoutRecordType(const RecordDecl *record,
1446 uint64_t &size,
1447 uint64_t &alignment,
1448 FieldOffsetMap &field_offsets,
1449 BaseOffsetMap &base_offsets,
1450 BaseOffsetMap &virtual_base_offsets)
1451{
Sean Callanan8106d802013-03-08 20:04:57 +00001452 ClangASTMetrics::RegisterRecordLayout();
1453
Sean Callanan5b26f272012-02-04 08:49:35 +00001454 static unsigned int invocation_id = 0;
1455 unsigned int current_id = invocation_id++;
1456
Greg Clayton5160ce52013-03-27 23:08:40 +00001457 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5b26f272012-02-04 08:49:35 +00001458
Sean Callanand5c17ed2011-11-15 02:11:17 +00001459 if (log)
1460 {
Sean Callanan933ca2e2013-02-28 03:12:58 +00001461 log->Printf("LayoutRecordType[%u] on (ASTContext*)%p for (RecordDecl*)%p [name = '%s']",
Sean Callanan5b26f272012-02-04 08:49:35 +00001462 current_id,
1463 m_ast_context,
Sean Callanan933ca2e2013-02-28 03:12:58 +00001464 record,
Sean Callanan5b26f272012-02-04 08:49:35 +00001465 record->getNameAsString().c_str());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001466 }
Sean Callanan5b26f272012-02-04 08:49:35 +00001467
1468
1469 DeclFromParser <const RecordDecl> parser_record(record);
1470 DeclFromUser <const RecordDecl> origin_record(parser_record.GetOrigin(m_ast_importer));
1471
1472 if (origin_record.IsInvalid())
1473 return false;
1474
1475 FieldOffsetMap origin_field_offsets;
1476 BaseOffsetMap origin_base_offsets;
1477 BaseOffsetMap origin_virtual_base_offsets;
1478
Sean Callananbf81ea52012-04-07 00:06:00 +00001479 ClangASTContext::GetCompleteDecl(&origin_record->getASTContext(), const_cast<RecordDecl*>(origin_record.decl));
1480
1481 if (!origin_record.decl->getDefinition())
1482 return false;
1483
Sean Callanan5b26f272012-02-04 08:49:35 +00001484 const ASTRecordLayout &record_layout(origin_record->getASTContext().getASTRecordLayout(origin_record.decl));
1485
Sean Callananeb7b27d2013-03-25 18:27:07 +00001486 int field_idx = 0, field_count = record_layout.getFieldCount();
Sean Callanan5b26f272012-02-04 08:49:35 +00001487
1488 for (RecordDecl::field_iterator fi = origin_record->field_begin(), fe = origin_record->field_end();
1489 fi != fe;
1490 ++fi)
1491 {
Sean Callananeb7b27d2013-03-25 18:27:07 +00001492 if (field_idx >= field_count)
1493 return false; // Layout didn't go well. Bail out.
1494
Sean Callanan5b26f272012-02-04 08:49:35 +00001495 uint64_t field_offset = record_layout.getFieldOffset(field_idx);
1496
1497 origin_field_offsets.insert(std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
1498
1499 field_idx++;
1500 }
1501
1502 ASTContext &parser_ast_context(record->getASTContext());
1503
1504 DeclFromUser <const CXXRecordDecl> origin_cxx_record(DynCast<const CXXRecordDecl>(origin_record));
1505
1506 if (origin_cxx_record.IsValid())
1507 {
1508 if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record, origin_base_offsets) ||
1509 !ExtractBaseOffsets<true>(record_layout, origin_cxx_record, origin_virtual_base_offsets))
1510 return false;
1511 }
1512
1513 if (!ImportOffsetMap(field_offsets, origin_field_offsets, m_ast_importer, parser_ast_context) ||
1514 !ImportOffsetMap(base_offsets, origin_base_offsets, m_ast_importer, parser_ast_context) ||
1515 !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets, m_ast_importer, parser_ast_context))
1516 return false;
1517
1518 size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth();
1519 alignment = record_layout.getAlignment().getQuantity() * m_ast_context->getCharWidth();
1520
1521 if (log)
1522 {
1523 log->Printf("LRT[%u] returned:", current_id);
1524 log->Printf("LRT[%u] Original = (RecordDecl*)%p", current_id, origin_record.decl);
Daniel Malead01b2952012-11-29 21:49:15 +00001525 log->Printf("LRT[%u] Size = %" PRId64, current_id, size);
1526 log->Printf("LRT[%u] Alignment = %" PRId64, current_id, alignment);
Sean Callanan5b26f272012-02-04 08:49:35 +00001527 log->Printf("LRT[%u] Fields:", current_id);
1528 for (RecordDecl::field_iterator fi = record->field_begin(), fe = record->field_end();
1529 fi != fe;
1530 ++fi)
1531 {
Daniel Malead01b2952012-11-29 21:49:15 +00001532 log->Printf("LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %" PRId64 " bits",
Sean Callanan5b26f272012-02-04 08:49:35 +00001533 current_id,
1534 *fi,
1535 fi->getNameAsString().c_str(),
1536 field_offsets[*fi]);
1537 }
1538 DeclFromParser <const CXXRecordDecl> parser_cxx_record = DynCast<const CXXRecordDecl>(parser_record);
1539 if (parser_cxx_record.IsValid())
1540 {
1541 log->Printf("LRT[%u] Bases:", current_id);
1542 for (CXXRecordDecl::base_class_const_iterator bi = parser_cxx_record->bases_begin(), be = parser_cxx_record->bases_end();
1543 bi != be;
1544 ++bi)
1545 {
1546 bool is_virtual = bi->isVirtual();
1547
1548 QualType base_type = bi->getType();
1549 const RecordType *base_record_type = base_type->getAs<RecordType>();
1550 DeclFromParser <RecordDecl> base_record(base_record_type->getDecl());
1551 DeclFromParser <CXXRecordDecl> base_cxx_record = DynCast<CXXRecordDecl>(base_record);
1552
Daniel Malead01b2952012-11-29 21:49:15 +00001553 log->Printf("LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %" PRId64 " chars",
Sean Callanan5b26f272012-02-04 08:49:35 +00001554 current_id,
1555 (is_virtual ? "Virtual " : ""),
1556 base_cxx_record.decl,
1557 base_cxx_record.decl->getNameAsString().c_str(),
1558 (is_virtual ? virtual_base_offsets[base_cxx_record.decl].getQuantity() :
1559 base_offsets[base_cxx_record.decl].getQuantity()));
1560 }
1561 }
1562 else
1563 {
1564 log->Printf("LRD[%u] Not a CXXRecord, so no bases", current_id);
1565 }
1566 }
1567
1568 return true;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001569}
1570
Sean Callanan1ee44b72011-10-29 01:58:46 +00001571void
1572ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
1573 const ConstString &name,
1574 ClangASTImporter::NamespaceMapSP &parent_map) const
1575{
1576 static unsigned int invocation_id = 0;
1577 unsigned int current_id = invocation_id++;
1578
Greg Clayton5160ce52013-03-27 23:08:40 +00001579 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1ee44b72011-10-29 01:58:46 +00001580
1581 if (log)
1582 {
1583 if (parent_map && parent_map->size())
Sean Callanan00f43622011-11-18 03:28:09 +00001584 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
Sean Callanan1ee44b72011-10-29 01:58:46 +00001585 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +00001586 m_ast_context,
Sean Callanan1ee44b72011-10-29 01:58:46 +00001587 name.GetCString(),
1588 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
1589 else
Sean Callanan00f43622011-11-18 03:28:09 +00001590 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
Sean Callanan1ee44b72011-10-29 01:58:46 +00001591 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +00001592 m_ast_context,
Sean Callanan1ee44b72011-10-29 01:58:46 +00001593 name.GetCString());
1594 }
1595
1596
1597 if (parent_map)
1598 {
1599 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
1600 i != e;
1601 ++i)
1602 {
1603 ClangNamespaceDecl found_namespace_decl;
1604
1605 lldb::ModuleSP module_sp = i->first;
1606 ClangNamespaceDecl module_parent_namespace_decl = i->second;
1607
1608 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
1609
1610 if (!symbol_vendor)
1611 continue;
1612
1613 SymbolContext null_sc;
1614
1615 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
1616
1617 if (!found_namespace_decl)
1618 continue;
1619
1620 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
1621
1622 if (log)
1623 log->Printf(" CMN[%u] Found namespace %s in module %s",
1624 current_id,
1625 name.GetCString(),
1626 module_sp->GetFileSpec().GetFilename().GetCString());
1627 }
1628 }
1629 else
1630 {
Enrico Granata17598482012-11-08 02:22:02 +00001631 const ModuleList &target_images = m_target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00001632 Mutex::Locker modules_locker(target_images.GetMutex());
1633
Sean Callanan1ee44b72011-10-29 01:58:46 +00001634 ClangNamespaceDecl null_namespace_decl;
1635
Greg Claytonc7bece562013-01-25 18:06:21 +00001636 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i)
Sean Callanan1ee44b72011-10-29 01:58:46 +00001637 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00001638 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
Sean Callanan1ee44b72011-10-29 01:58:46 +00001639
1640 if (!image)
1641 continue;
1642
1643 ClangNamespaceDecl found_namespace_decl;
1644
1645 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
1646
1647 if (!symbol_vendor)
1648 continue;
1649
1650 SymbolContext null_sc;
1651
1652 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
1653
1654 if (!found_namespace_decl)
1655 continue;
1656
1657 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
1658
1659 if (log)
1660 log->Printf(" CMN[%u] Found namespace %s in module %s",
1661 current_id,
1662 name.GetCString(),
1663 image->GetFileSpec().GetFilename().GetCString());
1664 }
1665 }
1666}
1667
Sean Callananba0aca72011-10-29 02:28:18 +00001668NamespaceDecl *
1669ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
1670{
Greg Claytone1cd1be2012-01-29 20:56:30 +00001671 if (!namespace_decls)
Sean Callananba0aca72011-10-29 02:28:18 +00001672 return NULL;
1673
Sean Callananba0aca72011-10-29 02:28:18 +00001674 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
1675
Sean Callanan686b2312011-11-16 18:20:47 +00001676 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
Sean Callananba0aca72011-10-29 02:28:18 +00001677
Sean Callanan61b33f72012-03-20 21:11:12 +00001678 if (!copied_decl)
1679 return NULL;
Sean Callananeeffea42013-02-12 08:01:13 +00001680
Sean Callananba0aca72011-10-29 02:28:18 +00001681 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1682
Sean Callananeeffea42013-02-12 08:01:13 +00001683 if (!copied_namespace_decl)
1684 return NULL;
1685
1686 context.m_decls.push_back(copied_namespace_decl);
1687
Sean Callananba0aca72011-10-29 02:28:18 +00001688 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
1689
1690 return dyn_cast<NamespaceDecl>(copied_decl);
1691}
1692
Greg Clayton57ee3062013-07-11 22:46:58 +00001693ClangASTType
1694ClangASTSource::GuardedCopyType (const ClangASTType &src_type)
Sean Callanan8106d802013-03-08 20:04:57 +00001695{
1696 ClangASTMetrics::RegisterLLDBImport();
1697
Sean Callananfb3e4302011-10-29 19:50:43 +00001698 SetImportInProgress(true);
1699
Greg Clayton57ee3062013-07-11 22:46:58 +00001700 QualType copied_qual_type = m_ast_importer->CopyType (m_ast_context, src_type.GetASTContext(), src_type.GetQualType());
Sean Callananfb3e4302011-10-29 19:50:43 +00001701
1702 SetImportInProgress(false);
1703
Greg Clayton57ee3062013-07-11 22:46:58 +00001704 if (copied_qual_type.getAsOpaquePtr() && copied_qual_type->getCanonicalTypeInternal().isNull())
Sean Callanan8b665982012-02-27 19:22:39 +00001705 // this shouldn't happen, but we're hardening because the AST importer seems to be generating bad types
1706 // on occasion.
Greg Clayton57ee3062013-07-11 22:46:58 +00001707 return ClangASTType();
Sean Callanan8b665982012-02-27 19:22:39 +00001708
Greg Clayton57ee3062013-07-11 22:46:58 +00001709 return ClangASTType(m_ast_context, copied_qual_type);
Sean Callananfb3e4302011-10-29 19:50:43 +00001710}
1711
Greg Clayton6beaaa62011-01-17 03:46:26 +00001712clang::NamedDecl *
Greg Clayton57ee3062013-07-11 22:46:58 +00001713NameSearchContext::AddVarDecl(const ClangASTType &type)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001714{
Greg Clayton57ee3062013-07-11 22:46:58 +00001715 assert (type && "Type for variable must be valid!");
1716
1717 if (!type.IsValid())
1718 return NULL;
1719
Greg Clayton7b462cc2010-10-15 22:48:33 +00001720 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan348b5892010-11-30 00:27:43 +00001721
Greg Clayton57ee3062013-07-11 22:46:58 +00001722 clang::ASTContext *ast = type.GetASTContext();
1723
1724 clang::NamedDecl *Decl = VarDecl::Create(*ast,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001725 const_cast<DeclContext*>(m_decl_context),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001726 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00001727 SourceLocation(),
Sean Callanan44096b12010-09-14 21:59:34 +00001728 ii,
Greg Clayton57ee3062013-07-11 22:46:58 +00001729 type.GetQualType(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001730 0,
Sean Callanane2ef6e32010-09-23 03:01:22 +00001731 SC_Static);
Greg Clayton7b462cc2010-10-15 22:48:33 +00001732 m_decls.push_back(Decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001733
1734 return Decl;
1735}
Sean Callanan468574b2010-06-22 23:46:24 +00001736
Greg Clayton6beaaa62011-01-17 03:46:26 +00001737clang::NamedDecl *
Greg Clayton57ee3062013-07-11 22:46:58 +00001738NameSearchContext::AddFunDecl (const ClangASTType &type)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001739{
Greg Clayton57ee3062013-07-11 22:46:58 +00001740 assert (type && "Type for variable must be valid!");
Sean Callanane2d47482012-03-21 17:13:20 +00001741
Greg Clayton57ee3062013-07-11 22:46:58 +00001742 if (!type.IsValid())
1743 return NULL;
1744
Sean Callanan485f7322013-04-24 00:34:41 +00001745 if (m_function_types.count(type))
1746 return NULL;
1747
1748 m_function_types.insert(type);
1749
Greg Clayton57ee3062013-07-11 22:46:58 +00001750 QualType qual_type (type.GetQualType());
1751
1752 clang::ASTContext *ast = type.GetASTContext();
1753
Greg Clayton0d551042013-06-28 21:08:47 +00001754 const bool isInlineSpecified = false;
1755 const bool hasWrittenPrototype = true;
1756 const bool isConstexprSpecified = false;
1757
Greg Clayton57ee3062013-07-11 22:46:58 +00001758 clang::FunctionDecl *func_decl = FunctionDecl::Create (*ast,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001759 const_cast<DeclContext*>(m_decl_context),
1760 SourceLocation(),
Jim Ingham6843e592013-06-28 22:21:22 +00001761 SourceLocation(),
Greg Clayton7b462cc2010-10-15 22:48:33 +00001762 m_decl_name.getAsIdentifierInfo(),
Greg Clayton57ee3062013-07-11 22:46:58 +00001763 qual_type,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001764 NULL,
1765 SC_Static,
Greg Clayton0d551042013-06-28 21:08:47 +00001766 isInlineSpecified,
1767 hasWrittenPrototype,
1768 isConstexprSpecified);
1769
Sean Callanan04949cc2010-08-12 23:45:38 +00001770 // We have to do more than just synthesize the FunctionDecl. We have to
1771 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
1772 // this, we raid the function's FunctionProtoType for types.
1773
Sean Callananfc4f2fb2011-12-14 01:13:04 +00001774 const FunctionProtoType *func_proto_type = qual_type.getTypePtr()->getAs<FunctionProtoType>();
Sean Callanan468574b2010-06-22 23:46:24 +00001775
Greg Clayton7b462cc2010-10-15 22:48:33 +00001776 if (func_proto_type)
Sean Callanand448c1b2010-06-23 18:58:10 +00001777 {
Greg Clayton7b462cc2010-10-15 22:48:33 +00001778 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan468574b2010-06-22 23:46:24 +00001779 unsigned ArgIndex;
1780
Sean Callanan880e6802011-10-07 23:18:13 +00001781 SmallVector<ParmVarDecl *, 5> parm_var_decls;
1782
Sean Callanan468574b2010-06-22 23:46:24 +00001783 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
1784 {
Greg Clayton7b462cc2010-10-15 22:48:33 +00001785 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan468574b2010-06-22 23:46:24 +00001786
Greg Clayton57ee3062013-07-11 22:46:58 +00001787 parm_var_decls.push_back(ParmVarDecl::Create (*ast,
Sean Callanan880e6802011-10-07 23:18:13 +00001788 const_cast<DeclContext*>(m_decl_context),
1789 SourceLocation(),
1790 SourceLocation(),
1791 NULL,
1792 arg_qual_type,
1793 NULL,
1794 SC_Static,
Sean Callanan880e6802011-10-07 23:18:13 +00001795 NULL));
Sean Callanan468574b2010-06-22 23:46:24 +00001796 }
1797
Sean Callanan880e6802011-10-07 23:18:13 +00001798 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan468574b2010-06-22 23:46:24 +00001799 }
Sean Callanane0a64f72011-12-01 21:04:37 +00001800 else
1801 {
Greg Clayton5160ce52013-03-27 23:08:40 +00001802 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanane0a64f72011-12-01 21:04:37 +00001803
Sean Callanan8ef19772013-04-25 18:50:43 +00001804 if (log)
1805 log->Printf("Function type wasn't a FunctionProtoType");
Sean Callanane0a64f72011-12-01 21:04:37 +00001806 }
Sean Callanan468574b2010-06-22 23:46:24 +00001807
Greg Clayton7b462cc2010-10-15 22:48:33 +00001808 m_decls.push_back(func_decl);
Sean Callanan468574b2010-06-22 23:46:24 +00001809
Greg Clayton7b462cc2010-10-15 22:48:33 +00001810 return func_decl;
Sean Callanan468574b2010-06-22 23:46:24 +00001811}
Sean Callanan8ade1042010-07-27 00:55:47 +00001812
Greg Clayton6beaaa62011-01-17 03:46:26 +00001813clang::NamedDecl *
1814NameSearchContext::AddGenericFunDecl()
Sean Callanan8ade1042010-07-27 00:55:47 +00001815{
Sean Callanan2c777c42011-01-18 23:32:05 +00001816 FunctionProtoType::ExtProtoInfo proto_info;
1817
1818 proto_info.Variadic = true;
1819
Sean Callananeddeb3b2011-10-28 23:38:38 +00001820 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 +00001821 ArrayRef<QualType>(), // argument types
Sean Callananeddeb3b2011-10-28 23:38:38 +00001822 proto_info));
Greg Clayton7b462cc2010-10-15 22:48:33 +00001823
Greg Clayton57ee3062013-07-11 22:46:58 +00001824 return AddFunDecl(ClangASTType (m_ast_source.m_ast_context, generic_function_type));
Sean Callanan8ade1042010-07-27 00:55:47 +00001825}
Sean Callanan5666b672010-08-04 01:02:13 +00001826
Greg Clayton6beaaa62011-01-17 03:46:26 +00001827clang::NamedDecl *
Greg Clayton57ee3062013-07-11 22:46:58 +00001828NameSearchContext::AddTypeDecl(const ClangASTType &clang_type)
Sean Callanan5666b672010-08-04 01:02:13 +00001829{
Greg Clayton57ee3062013-07-11 22:46:58 +00001830 if (clang_type)
Greg Clayton1b03cb52011-01-23 00:34:52 +00001831 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001832 QualType qual_type = clang_type.GetQualType();
Sean Callanan5666b672010-08-04 01:02:13 +00001833
Sean Callanan6b2f22d2013-06-25 22:36:17 +00001834 if (const TypedefType *typedef_type = llvm::dyn_cast<TypedefType>(qual_type))
1835 {
1836 TypedefNameDecl *typedef_name_decl = typedef_type->getDecl();
1837
1838 m_decls.push_back(typedef_name_decl);
1839
1840 return (NamedDecl*)typedef_name_decl;
1841 }
1842 else if (const TagType *tag_type = qual_type->getAs<TagType>())
Greg Clayton1b03cb52011-01-23 00:34:52 +00001843 {
1844 TagDecl *tag_decl = tag_type->getDecl();
1845
1846 m_decls.push_back(tag_decl);
1847
1848 return tag_decl;
1849 }
Sean Callananfc4f2fb2011-12-14 01:13:04 +00001850 else if (const ObjCObjectType *objc_object_type = qual_type->getAs<ObjCObjectType>())
Greg Clayton1b03cb52011-01-23 00:34:52 +00001851 {
1852 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
1853
1854 m_decls.push_back((NamedDecl*)interface_decl);
1855
1856 return (NamedDecl*)interface_decl;
1857 }
Sean Callanan5666b672010-08-04 01:02:13 +00001858 }
Greg Clayton1b03cb52011-01-23 00:34:52 +00001859 return NULL;
Sean Callanan5666b672010-08-04 01:02:13 +00001860}
Greg Claytona2721472011-06-25 00:44:06 +00001861
1862void
1863NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
1864{
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001865 for (clang::NamedDecl *decl : result)
1866 m_decls.push_back (decl);
Greg Claytona2721472011-06-25 00:44:06 +00001867}
1868
1869void
1870NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
1871{
1872 m_decls.push_back (decl);
1873}