blob: 316efdf9c14976ddd861622dddd2c4030be7a2ce [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
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
Greg Clayton57ee3062013-07-11 22:46:58 +0000349 TypeFromUser complete_type = TypeFromUser(complete_type_sp->GetClangFullType());
Sean Callanan2cb5e522012-09-20 23:21:16 +0000350 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
Greg Clayton57ee3062013-07-11 22:46:58 +0000699 ClangASTType full_type = type_sp->GetClangFullType();
Sean Callanan0eed0d42011-12-06 03:41:14 +0000700
Greg Clayton57ee3062013-07-11 22:46:58 +0000701 ClangASTType copied_clang_type (GuardedCopyType(full_type));
Sean Callanan0eed0d42011-12-06 03:41:14 +0000702
Greg Clayton57ee3062013-07-11 22:46:58 +0000703 if (!copied_clang_type)
Sean Callanane0a64f72011-12-01 21:04:37 +0000704 {
705 if (log)
Sean Callanan7be70e82012-12-19 23:05:01 +0000706 log->Printf(" CAS::FEVD[%u] - Couldn't export a type",
Sean Callanan0eed0d42011-12-06 03:41:14 +0000707 current_id);
708
Sean Callanane0a64f72011-12-01 21:04:37 +0000709 break;
710 }
711
Greg Clayton57ee3062013-07-11 22:46:58 +0000712 context.AddTypeDecl(copied_clang_type);
Sean Callananfb3e4302011-10-29 19:50:43 +0000713 }
Sean Callanan7be70e82012-12-19 23:05:01 +0000714 else
715 {
716 do
717 {
718 // Couldn't find any types elsewhere. Try the Objective-C runtime if one exists.
719
720 lldb::ProcessSP process(m_target->GetProcessSP());
721
722 if (!process)
723 break;
724
725 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
726
727 if (!language_runtime)
728 break;
729
730 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
731
732 if (!type_vendor)
733 break;
734
735 bool append = false;
736 uint32_t max_matches = 1;
737 std::vector <ClangASTType> types;
738
739 if (!type_vendor->FindTypes(name,
740 append,
741 max_matches,
742 types))
743 break;
744
745 if (log)
746 {
747 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\" in the runtime",
748 current_id,
749 name.GetCString());
750 }
751
Greg Clayton57ee3062013-07-11 22:46:58 +0000752 ClangASTType copied_clang_type (GuardedCopyType(types[0]));
Sean Callanan7be70e82012-12-19 23:05:01 +0000753
Greg Clayton57ee3062013-07-11 22:46:58 +0000754 if (!copied_clang_type)
Sean Callanan7be70e82012-12-19 23:05:01 +0000755 {
756 if (log)
757 log->Printf(" CAS::FEVD[%u] - Couldn't export a type from the runtime",
758 current_id);
759
760 break;
761 }
762
Greg Clayton57ee3062013-07-11 22:46:58 +0000763 context.AddTypeDecl(copied_clang_type);
Sean Callanan7be70e82012-12-19 23:05:01 +0000764 }
765 while(0);
766 }
Sean Callanan09ab4b72011-11-30 22:11:59 +0000767
Sean Callananfb3e4302011-10-29 19:50:43 +0000768 } while(0);
769}
770
Sean Callanan55400942012-11-02 17:09:58 +0000771template <class D> class TaggedASTDecl {
772public:
773 TaggedASTDecl() : decl(NULL) { }
774 TaggedASTDecl(D *_decl) : decl(_decl) { }
775 bool IsValid() const { return (decl != NULL); }
776 bool IsInvalid() const { return !IsValid(); }
777 D *operator->() const { return decl; }
778 D *decl;
779};
780
781template <class D2, template <class D> class TD, class D1>
782TD<D2>
783DynCast(TD<D1> source)
784{
785 return TD<D2> (dyn_cast<D2>(source.decl));
786}
787
788template <class D = Decl> class DeclFromParser;
789template <class D = Decl> class DeclFromUser;
790
791template <class D> class DeclFromParser : public TaggedASTDecl<D> {
792public:
793 DeclFromParser() : TaggedASTDecl<D>() { }
794 DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) { }
795
796 DeclFromUser<D> GetOrigin(ClangASTImporter *importer);
797};
798
799template <class D> class DeclFromUser : public TaggedASTDecl<D> {
800public:
801 DeclFromUser() : TaggedASTDecl<D>() { }
802 DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) { }
803
804 DeclFromParser<D> Import(ClangASTImporter *importer, ASTContext &dest_ctx);
805};
806
807template <class D>
808DeclFromUser<D>
809DeclFromParser<D>::GetOrigin(ClangASTImporter *importer)
810{
811 DeclFromUser <> origin_decl;
812 importer->ResolveDeclOrigin(this->decl, &origin_decl.decl, NULL);
813 if (origin_decl.IsInvalid())
814 return DeclFromUser<D>();
815 return DeclFromUser<D>(dyn_cast<D>(origin_decl.decl));
816}
817
818template <class D>
819DeclFromParser<D>
820DeclFromUser<D>::Import(ClangASTImporter *importer, ASTContext &dest_ctx)
821{
822 DeclFromParser <> parser_generic_decl(importer->CopyDecl(&dest_ctx, &this->decl->getASTContext(), this->decl));
823 if (parser_generic_decl.IsInvalid())
824 return DeclFromParser<D>();
825 return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
826}
827
Sean Callananc83e3412012-11-28 03:23:20 +0000828static bool
Sean Callananbc47dfc2012-09-11 21:44:01 +0000829FindObjCMethodDeclsWithOrigin (unsigned int current_id,
830 NameSearchContext &context,
831 ObjCInterfaceDecl *original_interface_decl,
832 clang::ASTContext *ast_context,
833 ClangASTImporter *ast_importer,
834 const char *log_info)
835{
836 const DeclarationName &decl_name(context.m_decl_name);
837 clang::ASTContext *original_ctx = &original_interface_decl->getASTContext();
838
839 Selector original_selector;
840
841 if (decl_name.isObjCZeroArgSelector())
842 {
843 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name.getAsString());
844 original_selector = original_ctx->Selectors.getSelector(0, &ident);
845 }
846 else if (decl_name.isObjCOneArgSelector())
847 {
848 const std::string &decl_name_string = decl_name.getAsString();
849 std::string decl_name_string_without_colon(decl_name_string.c_str(), decl_name_string.length() - 1);
850 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name_string_without_colon.c_str());
851 original_selector = original_ctx->Selectors.getSelector(1, &ident);
852 }
853 else
854 {
855 SmallVector<IdentifierInfo *, 4> idents;
856
857 clang::Selector sel = decl_name.getObjCSelector();
858
Andy Gibbsa297a972013-06-19 19:04:53 +0000859 unsigned num_args = sel.getNumArgs();
Sean Callananbc47dfc2012-09-11 21:44:01 +0000860
861 for (unsigned i = 0;
862 i != num_args;
863 ++i)
864 {
865 idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i)));
866 }
867
868 original_selector = original_ctx->Selectors.getSelector(num_args, idents.data());
869 }
870
871 DeclarationName original_decl_name(original_selector);
872
873 ObjCInterfaceDecl::lookup_result result = original_interface_decl->lookup(original_decl_name);
874
Sean Callanan5deaa4c2012-12-21 21:34:42 +0000875 if (result.empty())
Sean Callananc83e3412012-11-28 03:23:20 +0000876 return false;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000877
Sean Callanan5deaa4c2012-12-21 21:34:42 +0000878 if (!result[0])
Sean Callananc83e3412012-11-28 03:23:20 +0000879 return false;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000880
Sean Callanan01c8cb82013-09-04 23:25:26 +0000881 for (NamedDecl *named_decl : result)
Sean Callananbc47dfc2012-09-11 21:44:01 +0000882 {
Sean Callanan01c8cb82013-09-04 23:25:26 +0000883 ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(named_decl);
884
885 if (!result_method)
886 return false;
887
888 Decl *copied_decl = ast_importer->CopyDecl(ast_context, &result_method->getASTContext(), result_method);
889
890 if (!copied_decl)
891 return false;
892
893 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
894
895 if (!copied_method_decl)
896 return false;
897
898 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
899
900 if (log)
901 {
902 ASTDumper dumper((Decl*)copied_method_decl);
903 log->Printf(" CAS::FOMD[%d] found (%s) %s", current_id, log_info, dumper.GetCString());
904 }
905
906 context.AddNamedDecl(copied_method_decl);
Sean Callananbc47dfc2012-09-11 21:44:01 +0000907 }
908
Sean Callananc83e3412012-11-28 03:23:20 +0000909 return true;
Sean Callananbc47dfc2012-09-11 21:44:01 +0000910}
911
Sean Callanan0730e9c2011-11-09 19:33:21 +0000912void
913ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
914{
Greg Clayton5160ce52013-03-27 23:08:40 +0000915 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan0730e9c2011-11-09 19:33:21 +0000916
Sean Callanan46198ff2011-11-11 20:37:26 +0000917 static unsigned int invocation_id = 0;
918 unsigned int current_id = invocation_id++;
919
Sean Callanan0730e9c2011-11-09 19:33:21 +0000920 const DeclarationName &decl_name(context.m_decl_name);
921 const DeclContext *decl_ctx(context.m_decl_context);
922
923 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
924
925 if (!interface_decl)
926 return;
927
Sean Callanana6582262012-04-05 00:12:52 +0000928 do
929 {
930 Decl *original_decl = NULL;
931 ASTContext *original_ctx = NULL;
932
933 m_ast_importer->ResolveDeclOrigin(interface_decl, &original_decl, &original_ctx);
934
935 if (!original_decl)
936 break;
937
938 ObjCInterfaceDecl *original_interface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl);
939
Sean Callananc83e3412012-11-28 03:23:20 +0000940 if (FindObjCMethodDeclsWithOrigin(current_id,
941 context,
942 original_interface_decl,
943 m_ast_context,
944 m_ast_importer,
945 "at origin"))
946 return; // found it, no need to look any further
Sean Callanana6582262012-04-05 00:12:52 +0000947 } while (0);
948
Sean Callanan0730e9c2011-11-09 19:33:21 +0000949 StreamString ss;
Sean Callanan0eed0d42011-12-06 03:41:14 +0000950
Sean Callanan0730e9c2011-11-09 19:33:21 +0000951 if (decl_name.isObjCZeroArgSelector())
952 {
953 ss.Printf("%s", decl_name.getAsString().c_str());
954 }
955 else if (decl_name.isObjCOneArgSelector())
956 {
Sean Callanan4fb79b72011-11-14 18:29:46 +0000957 ss.Printf("%s", decl_name.getAsString().c_str());
Sean Callanan0730e9c2011-11-09 19:33:21 +0000958 }
959 else
960 {
961 clang::Selector sel = decl_name.getObjCSelector();
962
963 for (unsigned i = 0, e = sel.getNumArgs();
964 i != e;
965 ++i)
966 {
967 llvm::StringRef r = sel.getNameForSlot(i);
968 ss.Printf("%s:", r.str().c_str());
969 }
970 }
971 ss.Flush();
972
Sean Callanan46198ff2011-11-11 20:37:26 +0000973 ConstString selector_name(ss.GetData());
974
Sean Callanan0730e9c2011-11-09 19:33:21 +0000975 if (log)
Sean Callanan00f43622011-11-18 03:28:09 +0000976 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
977 current_id,
978 m_ast_context,
Sean Callanan46198ff2011-11-11 20:37:26 +0000979 interface_decl->getNameAsString().c_str(),
980 selector_name.AsCString());
Sean Callanan46198ff2011-11-11 20:37:26 +0000981 SymbolContextList sc_list;
982
983 const bool include_symbols = false;
Sean Callanan9df05fb2012-02-10 22:52:19 +0000984 const bool include_inlines = false;
Sean Callanan46198ff2011-11-11 20:37:26 +0000985 const bool append = false;
986
Sean Callanana9bc0652012-01-19 02:17:40 +0000987 std::string interface_name = interface_decl->getNameAsString();
988
989 do
990 {
991 StreamString ms;
992 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());
993 ms.Flush();
994 ConstString instance_method_name(ms.GetData());
995
Sean Callanan9df05fb2012-02-10 22:52:19 +0000996 m_target->GetImages().FindFunctions(instance_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanana9bc0652012-01-19 02:17:40 +0000997
998 if (sc_list.GetSize())
999 break;
1000
1001 ms.Clear();
1002 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());
1003 ms.Flush();
1004 ConstString class_method_name(ms.GetData());
1005
Sean Callanan9df05fb2012-02-10 22:52:19 +00001006 m_target->GetImages().FindFunctions(class_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanana9bc0652012-01-19 02:17:40 +00001007
1008 if (sc_list.GetSize())
1009 break;
1010
1011 // Fall back and check for methods in categories. If we find methods this way, we need to check that they're actually in
1012 // categories on the desired class.
1013
1014 SymbolContextList candidate_sc_list;
1015
Sean Callanan9df05fb2012-02-10 22:52:19 +00001016 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, include_inlines, append, candidate_sc_list);
Sean Callanana9bc0652012-01-19 02:17:40 +00001017
1018 for (uint32_t ci = 0, ce = candidate_sc_list.GetSize();
1019 ci != ce;
1020 ++ci)
1021 {
1022 SymbolContext candidate_sc;
1023
1024 if (!candidate_sc_list.GetContextAtIndex(ci, candidate_sc))
1025 continue;
1026
1027 if (!candidate_sc.function)
1028 continue;
1029
1030 const char *candidate_name = candidate_sc.function->GetName().AsCString();
1031
1032 const char *cursor = candidate_name;
1033
1034 if (*cursor != '+' && *cursor != '-')
1035 continue;
1036
1037 ++cursor;
1038
1039 if (*cursor != '[')
1040 continue;
1041
1042 ++cursor;
1043
1044 size_t interface_len = interface_name.length();
1045
1046 if (strncmp(cursor, interface_name.c_str(), interface_len))
1047 continue;
1048
1049 cursor += interface_len;
1050
1051 if (*cursor == ' ' || *cursor == '(')
1052 sc_list.Append(candidate_sc);
1053 }
1054 }
1055 while (0);
Sean Callanan46198ff2011-11-11 20:37:26 +00001056
Sean Callananbc47dfc2012-09-11 21:44:01 +00001057 if (sc_list.GetSize())
Sean Callanan46198ff2011-11-11 20:37:26 +00001058 {
Sean Callananbc47dfc2012-09-11 21:44:01 +00001059 // We found a good function symbol. Use that.
Sean Callanan46198ff2011-11-11 20:37:26 +00001060
Sean Callananbc47dfc2012-09-11 21:44:01 +00001061 for (uint32_t i = 0, e = sc_list.GetSize();
1062 i != e;
1063 ++i)
Sean Callanan46198ff2011-11-11 20:37:26 +00001064 {
Sean Callananbc47dfc2012-09-11 21:44:01 +00001065 SymbolContext sc;
Sean Callanan46198ff2011-11-11 20:37:26 +00001066
Sean Callananbc47dfc2012-09-11 21:44:01 +00001067 if (!sc_list.GetContextAtIndex(i, sc))
Sean Callanan46198ff2011-11-11 20:37:26 +00001068 continue;
1069
Sean Callananbc47dfc2012-09-11 21:44:01 +00001070 if (!sc.function)
Sean Callanan46198ff2011-11-11 20:37:26 +00001071 continue;
1072
Sean Callananbc47dfc2012-09-11 21:44:01 +00001073 DeclContext *function_ctx = sc.function->GetClangDeclContext();
1074
1075 if (!function_ctx)
1076 continue;
1077
1078 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
1079
1080 if (!method_decl)
1081 continue;
1082
1083 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
1084
1085 if (!found_interface_decl)
1086 continue;
1087
1088 if (found_interface_decl->getName() == interface_decl->getName())
Sean Callanan46198ff2011-11-11 20:37:26 +00001089 {
Sean Callananbc47dfc2012-09-11 21:44:01 +00001090 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
1091
1092 if (!copied_decl)
1093 continue;
1094
1095 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
1096
1097 if (!copied_method_decl)
1098 continue;
1099
1100 if (log)
1101 {
1102 ASTDumper dumper((Decl*)copied_method_decl);
Sean Callanan55400942012-11-02 17:09:58 +00001103 log->Printf(" CAS::FOMD[%d] found (in symbols) %s", current_id, dumper.GetCString());
Sean Callananbc47dfc2012-09-11 21:44:01 +00001104 }
1105
1106 context.AddNamedDecl(copied_method_decl);
Sean Callanan46198ff2011-11-11 20:37:26 +00001107 }
Sean Callanan46198ff2011-11-11 20:37:26 +00001108 }
Sean Callanan55400942012-11-02 17:09:58 +00001109
1110 return;
Sean Callanan46198ff2011-11-11 20:37:26 +00001111 }
Sean Callanan55400942012-11-02 17:09:58 +00001112
1113 // Try the debug information.
1114
1115 do
Sean Callananbc47dfc2012-09-11 21:44:01 +00001116 {
Sean Callanan55400942012-11-02 17:09:58 +00001117 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(interface_decl));
1118
1119 if (!complete_interface_decl)
1120 break;
1121
1122 // We found the complete interface. The runtime never needs to be queried in this scenario.
1123
1124 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
1125
1126 if (complete_interface_decl == interface_decl)
1127 break; // already checked this one
1128
1129 if (log)
1130 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1131 current_id,
1132 complete_interface_decl,
1133 &complete_iface_decl->getASTContext());
1134
1135 FindObjCMethodDeclsWithOrigin(current_id,
1136 context,
1137 complete_interface_decl,
1138 m_ast_context,
1139 m_ast_importer,
1140 "in debug info");
1141
1142 return;
Sean Callananbc47dfc2012-09-11 21:44:01 +00001143 }
Sean Callanan55400942012-11-02 17:09:58 +00001144 while (0);
Sean Callanan5b26f272012-02-04 08:49:35 +00001145
Sean Callanan55400942012-11-02 17:09:58 +00001146 do
1147 {
1148 // Check the runtime only if the debug information didn't have a complete interface.
1149
1150 lldb::ProcessSP process(m_target->GetProcessSP());
1151
1152 if (!process)
1153 break;
1154
1155 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1156
Sean Callanan7be70e82012-12-19 23:05:01 +00001157 if (!language_runtime)
1158 break;
1159
Sean Callanan55400942012-11-02 17:09:58 +00001160 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
1161
1162 if (!type_vendor)
1163 break;
1164
1165 ConstString interface_name(interface_decl->getNameAsString().c_str());
1166 bool append = false;
1167 uint32_t max_matches = 1;
1168 std::vector <ClangASTType> types;
1169
1170 if (!type_vendor->FindTypes(interface_name,
1171 append,
1172 max_matches,
1173 types))
1174 break;
1175
1176 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
1177
1178 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
1179
1180 if (!runtime_interface_type)
1181 break;
1182
1183 ObjCInterfaceDecl *runtime_interface_decl = runtime_interface_type->getDecl();
1184
1185 FindObjCMethodDeclsWithOrigin(current_id,
1186 context,
1187 runtime_interface_decl,
1188 m_ast_context,
1189 m_ast_importer,
1190 "in runtime");
1191 }
1192 while(0);
Sean Callanan5b26f272012-02-04 08:49:35 +00001193}
1194
Sean Callanan72772842012-02-22 23:57:45 +00001195static bool
1196FindObjCPropertyAndIvarDeclsWithOrigin (unsigned int current_id,
1197 NameSearchContext &context,
1198 clang::ASTContext &ast_context,
1199 ClangASTImporter *ast_importer,
1200 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl)
1201{
Greg Clayton5160ce52013-03-27 23:08:40 +00001202 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan72772842012-02-22 23:57:45 +00001203
1204 if (origin_iface_decl.IsInvalid())
1205 return false;
1206
1207 std::string name_str = context.m_decl_name.getAsString();
1208 StringRef name(name_str.c_str());
1209 IdentifierInfo &name_identifier(origin_iface_decl->getASTContext().Idents.get(name));
1210
1211 DeclFromUser<ObjCPropertyDecl> origin_property_decl(origin_iface_decl->FindPropertyDeclaration(&name_identifier));
1212
1213 bool found = false;
1214
1215 if (origin_property_decl.IsValid())
1216 {
1217 DeclFromParser<ObjCPropertyDecl> parser_property_decl(origin_property_decl.Import(ast_importer, ast_context));
1218 if (parser_property_decl.IsValid())
1219 {
1220 if (log)
1221 {
1222 ASTDumper dumper((Decl*)parser_property_decl.decl);
1223 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1224 }
1225
1226 context.AddNamedDecl(parser_property_decl.decl);
1227 found = true;
1228 }
1229 }
1230
1231 DeclFromUser<ObjCIvarDecl> origin_ivar_decl(origin_iface_decl->getIvarDecl(&name_identifier));
1232
1233 if (origin_ivar_decl.IsValid())
1234 {
1235 DeclFromParser<ObjCIvarDecl> parser_ivar_decl(origin_ivar_decl.Import(ast_importer, ast_context));
1236 if (parser_ivar_decl.IsValid())
1237 {
1238 if (log)
1239 {
1240 ASTDumper dumper((Decl*)parser_ivar_decl.decl);
1241 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1242 }
1243
1244 context.AddNamedDecl(parser_ivar_decl.decl);
1245 found = true;
1246 }
1247 }
1248
1249 return found;
1250}
1251
Sean Callanand5c17ed2011-11-15 02:11:17 +00001252void
Sean Callanan5b26f272012-02-04 08:49:35 +00001253ClangASTSource::FindObjCPropertyAndIvarDecls (NameSearchContext &context)
Sean Callanand5c17ed2011-11-15 02:11:17 +00001254{
Greg Clayton5160ce52013-03-27 23:08:40 +00001255 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanand5c17ed2011-11-15 02:11:17 +00001256
1257 static unsigned int invocation_id = 0;
1258 unsigned int current_id = invocation_id++;
1259
Sean Callanan5b26f272012-02-04 08:49:35 +00001260 DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl(cast<ObjCInterfaceDecl>(context.m_decl_context));
1261 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(parser_iface_decl.GetOrigin(m_ast_importer));
Sean Callanan72772842012-02-22 23:57:45 +00001262
1263 ConstString class_name(parser_iface_decl->getNameAsString().c_str());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001264
1265 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +00001266 log->Printf("ClangASTSource::FindObjCPropertyAndIvarDecls[%d] on (ASTContext*)%p for '%s.%s'",
Sean Callanand5c17ed2011-11-15 02:11:17 +00001267 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +00001268 m_ast_context,
Sean Callanan5b26f272012-02-04 08:49:35 +00001269 parser_iface_decl->getNameAsString().c_str(),
Sean Callanan72772842012-02-22 23:57:45 +00001270 context.m_decl_name.getAsString().c_str());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001271
Sean Callanan72772842012-02-22 23:57:45 +00001272 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1273 context,
1274 *m_ast_context,
1275 m_ast_importer,
1276 origin_iface_decl))
1277 return;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001278
Sean Callanan72772842012-02-22 23:57:45 +00001279 if (log)
1280 log->Printf("CAS::FOPD[%d] couldn't find the property on origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p, searching elsewhere...",
1281 current_id,
1282 origin_iface_decl.decl,
1283 &origin_iface_decl->getASTContext());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001284
Sean Callanan72772842012-02-22 23:57:45 +00001285 SymbolContext null_sc;
1286 TypeList type_list;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001287
Sean Callananbc47dfc2012-09-11 21:44:01 +00001288 do
1289 {
Sean Callanan2cb5e522012-09-20 23:21:16 +00001290 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(parser_iface_decl.decl));
Sean Callananbc47dfc2012-09-11 21:44:01 +00001291
Sean Callanan2cb5e522012-09-20 23:21:16 +00001292 if (!complete_interface_decl)
Sean Callananbc47dfc2012-09-11 21:44:01 +00001293 break;
1294
Sean Callanan55400942012-11-02 17:09:58 +00001295 // We found the complete interface. The runtime never needs to be queried in this scenario.
1296
Sean Callanan2cb5e522012-09-20 23:21:16 +00001297 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
Sean Callananbc47dfc2012-09-11 21:44:01 +00001298
1299 if (complete_iface_decl.decl == origin_iface_decl.decl)
1300 break; // already checked this one
1301
1302 if (log)
1303 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1304 current_id,
1305 complete_iface_decl.decl,
1306 &complete_iface_decl->getASTContext());
1307
Sean Callanan55400942012-11-02 17:09:58 +00001308 FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1309 context,
1310 *m_ast_context,
1311 m_ast_importer,
1312 complete_iface_decl);
1313
1314 return;
Sean Callananbc47dfc2012-09-11 21:44:01 +00001315 }
1316 while(0);
Sean Callanan72772842012-02-22 23:57:45 +00001317
Sean Callananbc47dfc2012-09-11 21:44:01 +00001318 do
1319 {
Sean Callanan55400942012-11-02 17:09:58 +00001320 // Check the runtime only if the debug information didn't have a complete interface.
1321
1322 lldb::ProcessSP process(m_target->GetProcessSP());
1323
1324 if (!process)
1325 return;
1326
1327 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1328
1329 if (!language_runtime)
1330 return;
Sean Callananbc47dfc2012-09-11 21:44:01 +00001331
1332 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
1333
1334 if (!type_vendor)
1335 break;
1336
1337 bool append = false;
1338 uint32_t max_matches = 1;
1339 std::vector <ClangASTType> types;
1340
1341 if (!type_vendor->FindTypes(class_name,
1342 append,
1343 max_matches,
1344 types))
1345 break;
1346
1347 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
1348
1349 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
1350
1351 if (!runtime_interface_type)
1352 break;
1353
1354 DeclFromUser<const ObjCInterfaceDecl> runtime_iface_decl(runtime_interface_type->getDecl());
1355
1356 if (log)
1357 log->Printf("CAS::FOPD[%d] trying runtime (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1358 current_id,
1359 runtime_iface_decl.decl,
1360 &runtime_iface_decl->getASTContext());
1361
1362 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1363 context,
1364 *m_ast_context,
1365 m_ast_importer,
1366 runtime_iface_decl))
1367 return;
1368 }
1369 while(0);
Sean Callanan5b26f272012-02-04 08:49:35 +00001370}
1371
1372typedef llvm::DenseMap <const FieldDecl *, uint64_t> FieldOffsetMap;
1373typedef llvm::DenseMap <const CXXRecordDecl *, CharUnits> BaseOffsetMap;
1374
1375template <class D, class O>
1376static bool
1377ImportOffsetMap (llvm::DenseMap <const D*, O> &destination_map,
1378 llvm::DenseMap <const D*, O> &source_map,
1379 ClangASTImporter *importer,
1380 ASTContext &dest_ctx)
1381{
1382 typedef llvm::DenseMap <const D*, O> MapType;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001383
Sean Callanan5b26f272012-02-04 08:49:35 +00001384 for (typename MapType::iterator fi = source_map.begin(), fe = source_map.end();
1385 fi != fe;
1386 ++fi)
1387 {
1388 DeclFromUser <D> user_decl(const_cast<D*>(fi->first));
1389 DeclFromParser <D> parser_decl(user_decl.Import(importer, dest_ctx));
1390 if (parser_decl.IsInvalid())
1391 return false;
1392 destination_map.insert(std::pair<const D *, O>(parser_decl.decl, fi->second));
1393 }
1394
1395 return true;
1396}
1397
1398template <bool IsVirtual> bool ExtractBaseOffsets (const ASTRecordLayout &record_layout,
1399 DeclFromUser<const CXXRecordDecl> &record,
1400 BaseOffsetMap &base_offsets)
1401{
1402 for (CXXRecordDecl::base_class_const_iterator
1403 bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()),
1404 be = (IsVirtual ? record->vbases_end() : record->bases_end());
1405 bi != be;
1406 ++bi)
1407 {
1408 if (!IsVirtual && bi->isVirtual())
1409 continue;
1410
1411 const clang::Type *origin_base_type = bi->getType().getTypePtr();
1412 const clang::RecordType *origin_base_record_type = origin_base_type->getAs<RecordType>();
1413
1414 if (!origin_base_record_type)
1415 return false;
1416
1417 DeclFromUser <RecordDecl> origin_base_record(origin_base_record_type->getDecl());
1418
1419 if (origin_base_record.IsInvalid())
1420 return false;
1421
1422 DeclFromUser <CXXRecordDecl> origin_base_cxx_record(DynCast<CXXRecordDecl>(origin_base_record));
1423
1424 if (origin_base_cxx_record.IsInvalid())
1425 return false;
1426
1427 CharUnits base_offset;
1428
1429 if (IsVirtual)
1430 base_offset = record_layout.getVBaseClassOffset(origin_base_cxx_record.decl);
1431 else
1432 base_offset = record_layout.getBaseClassOffset(origin_base_cxx_record.decl);
1433
1434 base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(origin_base_cxx_record.decl, base_offset));
1435 }
1436
1437 return true;
1438}
1439
1440bool
1441ClangASTSource::layoutRecordType(const RecordDecl *record,
1442 uint64_t &size,
1443 uint64_t &alignment,
1444 FieldOffsetMap &field_offsets,
1445 BaseOffsetMap &base_offsets,
1446 BaseOffsetMap &virtual_base_offsets)
1447{
Sean Callanan8106d802013-03-08 20:04:57 +00001448 ClangASTMetrics::RegisterRecordLayout();
1449
Sean Callanan5b26f272012-02-04 08:49:35 +00001450 static unsigned int invocation_id = 0;
1451 unsigned int current_id = invocation_id++;
1452
Greg Clayton5160ce52013-03-27 23:08:40 +00001453 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan5b26f272012-02-04 08:49:35 +00001454
Sean Callanand5c17ed2011-11-15 02:11:17 +00001455 if (log)
1456 {
Sean Callanan933ca2e2013-02-28 03:12:58 +00001457 log->Printf("LayoutRecordType[%u] on (ASTContext*)%p for (RecordDecl*)%p [name = '%s']",
Sean Callanan5b26f272012-02-04 08:49:35 +00001458 current_id,
1459 m_ast_context,
Sean Callanan933ca2e2013-02-28 03:12:58 +00001460 record,
Sean Callanan5b26f272012-02-04 08:49:35 +00001461 record->getNameAsString().c_str());
Sean Callanand5c17ed2011-11-15 02:11:17 +00001462 }
Sean Callanan5b26f272012-02-04 08:49:35 +00001463
1464
1465 DeclFromParser <const RecordDecl> parser_record(record);
1466 DeclFromUser <const RecordDecl> origin_record(parser_record.GetOrigin(m_ast_importer));
1467
1468 if (origin_record.IsInvalid())
1469 return false;
1470
1471 FieldOffsetMap origin_field_offsets;
1472 BaseOffsetMap origin_base_offsets;
1473 BaseOffsetMap origin_virtual_base_offsets;
1474
Sean Callananbf81ea52012-04-07 00:06:00 +00001475 ClangASTContext::GetCompleteDecl(&origin_record->getASTContext(), const_cast<RecordDecl*>(origin_record.decl));
1476
1477 if (!origin_record.decl->getDefinition())
1478 return false;
1479
Sean Callanan5b26f272012-02-04 08:49:35 +00001480 const ASTRecordLayout &record_layout(origin_record->getASTContext().getASTRecordLayout(origin_record.decl));
1481
Sean Callananeb7b27d2013-03-25 18:27:07 +00001482 int field_idx = 0, field_count = record_layout.getFieldCount();
Sean Callanan5b26f272012-02-04 08:49:35 +00001483
1484 for (RecordDecl::field_iterator fi = origin_record->field_begin(), fe = origin_record->field_end();
1485 fi != fe;
1486 ++fi)
1487 {
Sean Callananeb7b27d2013-03-25 18:27:07 +00001488 if (field_idx >= field_count)
1489 return false; // Layout didn't go well. Bail out.
1490
Sean Callanan5b26f272012-02-04 08:49:35 +00001491 uint64_t field_offset = record_layout.getFieldOffset(field_idx);
1492
1493 origin_field_offsets.insert(std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
1494
1495 field_idx++;
1496 }
1497
1498 ASTContext &parser_ast_context(record->getASTContext());
1499
1500 DeclFromUser <const CXXRecordDecl> origin_cxx_record(DynCast<const CXXRecordDecl>(origin_record));
1501
1502 if (origin_cxx_record.IsValid())
1503 {
1504 if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record, origin_base_offsets) ||
1505 !ExtractBaseOffsets<true>(record_layout, origin_cxx_record, origin_virtual_base_offsets))
1506 return false;
1507 }
1508
1509 if (!ImportOffsetMap(field_offsets, origin_field_offsets, m_ast_importer, parser_ast_context) ||
1510 !ImportOffsetMap(base_offsets, origin_base_offsets, m_ast_importer, parser_ast_context) ||
1511 !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets, m_ast_importer, parser_ast_context))
1512 return false;
1513
1514 size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth();
1515 alignment = record_layout.getAlignment().getQuantity() * m_ast_context->getCharWidth();
1516
1517 if (log)
1518 {
1519 log->Printf("LRT[%u] returned:", current_id);
1520 log->Printf("LRT[%u] Original = (RecordDecl*)%p", current_id, origin_record.decl);
Daniel Malead01b2952012-11-29 21:49:15 +00001521 log->Printf("LRT[%u] Size = %" PRId64, current_id, size);
1522 log->Printf("LRT[%u] Alignment = %" PRId64, current_id, alignment);
Sean Callanan5b26f272012-02-04 08:49:35 +00001523 log->Printf("LRT[%u] Fields:", current_id);
1524 for (RecordDecl::field_iterator fi = record->field_begin(), fe = record->field_end();
1525 fi != fe;
1526 ++fi)
1527 {
Daniel Malead01b2952012-11-29 21:49:15 +00001528 log->Printf("LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %" PRId64 " bits",
Sean Callanan5b26f272012-02-04 08:49:35 +00001529 current_id,
1530 *fi,
1531 fi->getNameAsString().c_str(),
1532 field_offsets[*fi]);
1533 }
1534 DeclFromParser <const CXXRecordDecl> parser_cxx_record = DynCast<const CXXRecordDecl>(parser_record);
1535 if (parser_cxx_record.IsValid())
1536 {
1537 log->Printf("LRT[%u] Bases:", current_id);
1538 for (CXXRecordDecl::base_class_const_iterator bi = parser_cxx_record->bases_begin(), be = parser_cxx_record->bases_end();
1539 bi != be;
1540 ++bi)
1541 {
1542 bool is_virtual = bi->isVirtual();
1543
1544 QualType base_type = bi->getType();
1545 const RecordType *base_record_type = base_type->getAs<RecordType>();
1546 DeclFromParser <RecordDecl> base_record(base_record_type->getDecl());
1547 DeclFromParser <CXXRecordDecl> base_cxx_record = DynCast<CXXRecordDecl>(base_record);
1548
Daniel Malead01b2952012-11-29 21:49:15 +00001549 log->Printf("LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %" PRId64 " chars",
Sean Callanan5b26f272012-02-04 08:49:35 +00001550 current_id,
1551 (is_virtual ? "Virtual " : ""),
1552 base_cxx_record.decl,
1553 base_cxx_record.decl->getNameAsString().c_str(),
1554 (is_virtual ? virtual_base_offsets[base_cxx_record.decl].getQuantity() :
1555 base_offsets[base_cxx_record.decl].getQuantity()));
1556 }
1557 }
1558 else
1559 {
1560 log->Printf("LRD[%u] Not a CXXRecord, so no bases", current_id);
1561 }
1562 }
1563
1564 return true;
Sean Callanand5c17ed2011-11-15 02:11:17 +00001565}
1566
Sean Callanan1ee44b72011-10-29 01:58:46 +00001567void
1568ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
1569 const ConstString &name,
1570 ClangASTImporter::NamespaceMapSP &parent_map) const
1571{
1572 static unsigned int invocation_id = 0;
1573 unsigned int current_id = invocation_id++;
1574
Greg Clayton5160ce52013-03-27 23:08:40 +00001575 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan1ee44b72011-10-29 01:58:46 +00001576
1577 if (log)
1578 {
1579 if (parent_map && parent_map->size())
Sean Callanan00f43622011-11-18 03:28:09 +00001580 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
Sean Callanan1ee44b72011-10-29 01:58:46 +00001581 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +00001582 m_ast_context,
Sean Callanan1ee44b72011-10-29 01:58:46 +00001583 name.GetCString(),
1584 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
1585 else
Sean Callanan00f43622011-11-18 03:28:09 +00001586 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
Sean Callanan1ee44b72011-10-29 01:58:46 +00001587 current_id,
Sean Callanan00f43622011-11-18 03:28:09 +00001588 m_ast_context,
Sean Callanan1ee44b72011-10-29 01:58:46 +00001589 name.GetCString());
1590 }
1591
1592
1593 if (parent_map)
1594 {
1595 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
1596 i != e;
1597 ++i)
1598 {
1599 ClangNamespaceDecl found_namespace_decl;
1600
1601 lldb::ModuleSP module_sp = i->first;
1602 ClangNamespaceDecl module_parent_namespace_decl = i->second;
1603
1604 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
1605
1606 if (!symbol_vendor)
1607 continue;
1608
1609 SymbolContext null_sc;
1610
1611 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
1612
1613 if (!found_namespace_decl)
1614 continue;
1615
1616 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
1617
1618 if (log)
1619 log->Printf(" CMN[%u] Found namespace %s in module %s",
1620 current_id,
1621 name.GetCString(),
1622 module_sp->GetFileSpec().GetFilename().GetCString());
1623 }
1624 }
1625 else
1626 {
Enrico Granata17598482012-11-08 02:22:02 +00001627 const ModuleList &target_images = m_target->GetImages();
Jim Ingham3ee12ef2012-05-30 02:19:25 +00001628 Mutex::Locker modules_locker(target_images.GetMutex());
1629
Sean Callanan1ee44b72011-10-29 01:58:46 +00001630 ClangNamespaceDecl null_namespace_decl;
1631
Greg Claytonc7bece562013-01-25 18:06:21 +00001632 for (size_t i = 0, e = target_images.GetSize(); i < e; ++i)
Sean Callanan1ee44b72011-10-29 01:58:46 +00001633 {
Jim Ingham3ee12ef2012-05-30 02:19:25 +00001634 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
Sean Callanan1ee44b72011-10-29 01:58:46 +00001635
1636 if (!image)
1637 continue;
1638
1639 ClangNamespaceDecl found_namespace_decl;
1640
1641 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
1642
1643 if (!symbol_vendor)
1644 continue;
1645
1646 SymbolContext null_sc;
1647
1648 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
1649
1650 if (!found_namespace_decl)
1651 continue;
1652
1653 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
1654
1655 if (log)
1656 log->Printf(" CMN[%u] Found namespace %s in module %s",
1657 current_id,
1658 name.GetCString(),
1659 image->GetFileSpec().GetFilename().GetCString());
1660 }
1661 }
1662}
1663
Sean Callananba0aca72011-10-29 02:28:18 +00001664NamespaceDecl *
1665ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
1666{
Greg Claytone1cd1be2012-01-29 20:56:30 +00001667 if (!namespace_decls)
Sean Callananba0aca72011-10-29 02:28:18 +00001668 return NULL;
1669
Sean Callananba0aca72011-10-29 02:28:18 +00001670 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
1671
Sean Callanan686b2312011-11-16 18:20:47 +00001672 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
Sean Callananba0aca72011-10-29 02:28:18 +00001673
Sean Callanan61b33f72012-03-20 21:11:12 +00001674 if (!copied_decl)
1675 return NULL;
Sean Callananeeffea42013-02-12 08:01:13 +00001676
Sean Callananba0aca72011-10-29 02:28:18 +00001677 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1678
Sean Callananeeffea42013-02-12 08:01:13 +00001679 if (!copied_namespace_decl)
1680 return NULL;
1681
1682 context.m_decls.push_back(copied_namespace_decl);
1683
Sean Callananba0aca72011-10-29 02:28:18 +00001684 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
1685
1686 return dyn_cast<NamespaceDecl>(copied_decl);
1687}
1688
Greg Clayton57ee3062013-07-11 22:46:58 +00001689ClangASTType
1690ClangASTSource::GuardedCopyType (const ClangASTType &src_type)
Sean Callanan8106d802013-03-08 20:04:57 +00001691{
1692 ClangASTMetrics::RegisterLLDBImport();
1693
Sean Callananfb3e4302011-10-29 19:50:43 +00001694 SetImportInProgress(true);
1695
Greg Clayton57ee3062013-07-11 22:46:58 +00001696 QualType copied_qual_type = m_ast_importer->CopyType (m_ast_context, src_type.GetASTContext(), src_type.GetQualType());
Sean Callananfb3e4302011-10-29 19:50:43 +00001697
1698 SetImportInProgress(false);
1699
Greg Clayton57ee3062013-07-11 22:46:58 +00001700 if (copied_qual_type.getAsOpaquePtr() && copied_qual_type->getCanonicalTypeInternal().isNull())
Sean Callanan8b665982012-02-27 19:22:39 +00001701 // this shouldn't happen, but we're hardening because the AST importer seems to be generating bad types
1702 // on occasion.
Greg Clayton57ee3062013-07-11 22:46:58 +00001703 return ClangASTType();
Sean Callanan8b665982012-02-27 19:22:39 +00001704
Greg Clayton57ee3062013-07-11 22:46:58 +00001705 return ClangASTType(m_ast_context, copied_qual_type);
Sean Callananfb3e4302011-10-29 19:50:43 +00001706}
1707
Greg Clayton6beaaa62011-01-17 03:46:26 +00001708clang::NamedDecl *
Greg Clayton57ee3062013-07-11 22:46:58 +00001709NameSearchContext::AddVarDecl(const ClangASTType &type)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001710{
Greg Clayton57ee3062013-07-11 22:46:58 +00001711 assert (type && "Type for variable must be valid!");
1712
1713 if (!type.IsValid())
1714 return NULL;
1715
Greg Clayton7b462cc2010-10-15 22:48:33 +00001716 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan348b5892010-11-30 00:27:43 +00001717
Greg Clayton57ee3062013-07-11 22:46:58 +00001718 clang::ASTContext *ast = type.GetASTContext();
1719
1720 clang::NamedDecl *Decl = VarDecl::Create(*ast,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001721 const_cast<DeclContext*>(m_decl_context),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001722 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00001723 SourceLocation(),
Sean Callanan44096b12010-09-14 21:59:34 +00001724 ii,
Greg Clayton57ee3062013-07-11 22:46:58 +00001725 type.GetQualType(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001726 0,
Sean Callanane2ef6e32010-09-23 03:01:22 +00001727 SC_Static);
Greg Clayton7b462cc2010-10-15 22:48:33 +00001728 m_decls.push_back(Decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001729
1730 return Decl;
1731}
Sean Callanan468574b2010-06-22 23:46:24 +00001732
Greg Clayton6beaaa62011-01-17 03:46:26 +00001733clang::NamedDecl *
Greg Clayton57ee3062013-07-11 22:46:58 +00001734NameSearchContext::AddFunDecl (const ClangASTType &type)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001735{
Greg Clayton57ee3062013-07-11 22:46:58 +00001736 assert (type && "Type for variable must be valid!");
Sean Callanane2d47482012-03-21 17:13:20 +00001737
Greg Clayton57ee3062013-07-11 22:46:58 +00001738 if (!type.IsValid())
1739 return NULL;
1740
Sean Callanan485f7322013-04-24 00:34:41 +00001741 if (m_function_types.count(type))
1742 return NULL;
1743
1744 m_function_types.insert(type);
1745
Greg Clayton57ee3062013-07-11 22:46:58 +00001746 QualType qual_type (type.GetQualType());
1747
1748 clang::ASTContext *ast = type.GetASTContext();
1749
Greg Clayton0d551042013-06-28 21:08:47 +00001750 const bool isInlineSpecified = false;
1751 const bool hasWrittenPrototype = true;
1752 const bool isConstexprSpecified = false;
1753
Greg Clayton57ee3062013-07-11 22:46:58 +00001754 clang::FunctionDecl *func_decl = FunctionDecl::Create (*ast,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001755 const_cast<DeclContext*>(m_decl_context),
1756 SourceLocation(),
Jim Ingham6843e592013-06-28 22:21:22 +00001757 SourceLocation(),
Greg Clayton7b462cc2010-10-15 22:48:33 +00001758 m_decl_name.getAsIdentifierInfo(),
Greg Clayton57ee3062013-07-11 22:46:58 +00001759 qual_type,
Greg Clayton7b462cc2010-10-15 22:48:33 +00001760 NULL,
1761 SC_Static,
Greg Clayton0d551042013-06-28 21:08:47 +00001762 isInlineSpecified,
1763 hasWrittenPrototype,
1764 isConstexprSpecified);
1765
Sean Callanan04949cc2010-08-12 23:45:38 +00001766 // We have to do more than just synthesize the FunctionDecl. We have to
1767 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
1768 // this, we raid the function's FunctionProtoType for types.
1769
Sean Callananfc4f2fb2011-12-14 01:13:04 +00001770 const FunctionProtoType *func_proto_type = qual_type.getTypePtr()->getAs<FunctionProtoType>();
Sean Callanan468574b2010-06-22 23:46:24 +00001771
Greg Clayton7b462cc2010-10-15 22:48:33 +00001772 if (func_proto_type)
Sean Callanand448c1b2010-06-23 18:58:10 +00001773 {
Greg Clayton7b462cc2010-10-15 22:48:33 +00001774 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan468574b2010-06-22 23:46:24 +00001775 unsigned ArgIndex;
1776
Sean Callanan880e6802011-10-07 23:18:13 +00001777 SmallVector<ParmVarDecl *, 5> parm_var_decls;
1778
Sean Callanan468574b2010-06-22 23:46:24 +00001779 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
1780 {
Greg Clayton7b462cc2010-10-15 22:48:33 +00001781 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan468574b2010-06-22 23:46:24 +00001782
Greg Clayton57ee3062013-07-11 22:46:58 +00001783 parm_var_decls.push_back(ParmVarDecl::Create (*ast,
Sean Callanan880e6802011-10-07 23:18:13 +00001784 const_cast<DeclContext*>(m_decl_context),
1785 SourceLocation(),
1786 SourceLocation(),
1787 NULL,
1788 arg_qual_type,
1789 NULL,
1790 SC_Static,
Sean Callanan880e6802011-10-07 23:18:13 +00001791 NULL));
Sean Callanan468574b2010-06-22 23:46:24 +00001792 }
1793
Sean Callanan880e6802011-10-07 23:18:13 +00001794 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan468574b2010-06-22 23:46:24 +00001795 }
Sean Callanane0a64f72011-12-01 21:04:37 +00001796 else
1797 {
Greg Clayton5160ce52013-03-27 23:08:40 +00001798 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanane0a64f72011-12-01 21:04:37 +00001799
Sean Callanan8ef19772013-04-25 18:50:43 +00001800 if (log)
1801 log->Printf("Function type wasn't a FunctionProtoType");
Sean Callanane0a64f72011-12-01 21:04:37 +00001802 }
Sean Callanan468574b2010-06-22 23:46:24 +00001803
Greg Clayton7b462cc2010-10-15 22:48:33 +00001804 m_decls.push_back(func_decl);
Sean Callanan468574b2010-06-22 23:46:24 +00001805
Greg Clayton7b462cc2010-10-15 22:48:33 +00001806 return func_decl;
Sean Callanan468574b2010-06-22 23:46:24 +00001807}
Sean Callanan8ade1042010-07-27 00:55:47 +00001808
Greg Clayton6beaaa62011-01-17 03:46:26 +00001809clang::NamedDecl *
1810NameSearchContext::AddGenericFunDecl()
Sean Callanan8ade1042010-07-27 00:55:47 +00001811{
Sean Callanan2c777c42011-01-18 23:32:05 +00001812 FunctionProtoType::ExtProtoInfo proto_info;
1813
1814 proto_info.Variadic = true;
1815
Sean Callananeddeb3b2011-10-28 23:38:38 +00001816 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 +00001817 ArrayRef<QualType>(), // argument types
Sean Callananeddeb3b2011-10-28 23:38:38 +00001818 proto_info));
Greg Clayton7b462cc2010-10-15 22:48:33 +00001819
Greg Clayton57ee3062013-07-11 22:46:58 +00001820 return AddFunDecl(ClangASTType (m_ast_source.m_ast_context, generic_function_type));
Sean Callanan8ade1042010-07-27 00:55:47 +00001821}
Sean Callanan5666b672010-08-04 01:02:13 +00001822
Greg Clayton6beaaa62011-01-17 03:46:26 +00001823clang::NamedDecl *
Greg Clayton57ee3062013-07-11 22:46:58 +00001824NameSearchContext::AddTypeDecl(const ClangASTType &clang_type)
Sean Callanan5666b672010-08-04 01:02:13 +00001825{
Greg Clayton57ee3062013-07-11 22:46:58 +00001826 if (clang_type)
Greg Clayton1b03cb52011-01-23 00:34:52 +00001827 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001828 QualType qual_type = clang_type.GetQualType();
Sean Callanan5666b672010-08-04 01:02:13 +00001829
Sean Callanan6b2f22d2013-06-25 22:36:17 +00001830 if (const TypedefType *typedef_type = llvm::dyn_cast<TypedefType>(qual_type))
1831 {
1832 TypedefNameDecl *typedef_name_decl = typedef_type->getDecl();
1833
1834 m_decls.push_back(typedef_name_decl);
1835
1836 return (NamedDecl*)typedef_name_decl;
1837 }
1838 else if (const TagType *tag_type = qual_type->getAs<TagType>())
Greg Clayton1b03cb52011-01-23 00:34:52 +00001839 {
1840 TagDecl *tag_decl = tag_type->getDecl();
1841
1842 m_decls.push_back(tag_decl);
1843
1844 return tag_decl;
1845 }
Sean Callananfc4f2fb2011-12-14 01:13:04 +00001846 else if (const ObjCObjectType *objc_object_type = qual_type->getAs<ObjCObjectType>())
Greg Clayton1b03cb52011-01-23 00:34:52 +00001847 {
1848 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
1849
1850 m_decls.push_back((NamedDecl*)interface_decl);
1851
1852 return (NamedDecl*)interface_decl;
1853 }
Sean Callanan5666b672010-08-04 01:02:13 +00001854 }
Greg Clayton1b03cb52011-01-23 00:34:52 +00001855 return NULL;
Sean Callanan5666b672010-08-04 01:02:13 +00001856}
Greg Claytona2721472011-06-25 00:44:06 +00001857
1858void
1859NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
1860{
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001861 for (clang::NamedDecl *decl : result)
1862 m_decls.push_back (decl);
Greg Claytona2721472011-06-25 00:44:06 +00001863}
1864
1865void
1866NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
1867{
1868 m_decls.push_back (decl);
1869}