blob: 36f0e84b6b57f01cbd2e0dde3fb0273c109ca41f [file] [log] [blame]
Greg Clayton1e591ce2010-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 Lattner24943d22010-06-08 16:52:24 +000010
Chris Lattner24943d22010-06-08 16:52:24 +000011#include "clang/AST/ASTContext.h"
Sean Callanan8f2e3922012-02-04 08:49:35 +000012#include "clang/AST/RecordLayout.h"
Greg Claytonf4c7ae02010-10-15 03:36:13 +000013#include "lldb/Core/Log.h"
Greg Clayton6e0101c2011-09-17 06:21:20 +000014#include "lldb/Core/Module.h"
Sean Callanan73b520f2011-10-29 01:58:46 +000015#include "lldb/Core/ModuleList.h"
Sean Callananbb715f92011-10-29 02:28:18 +000016#include "lldb/Expression/ASTDumper.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Expression/ClangASTSource.h"
18#include "lldb/Expression/ClangExpression.h"
Sean Callanan73b520f2011-10-29 01:58:46 +000019#include "lldb/Symbol/ClangNamespaceDecl.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000020#include "lldb/Symbol/Function.h"
Sean Callanan73b520f2011-10-29 01:58:46 +000021#include "lldb/Symbol/SymbolVendor.h"
Sean Callanan673f3db2011-11-30 22:11:59 +000022#include "lldb/Target/ObjCLanguageRuntime.h"
Sean Callanan73b520f2011-10-29 01:58:46 +000023#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024
25using namespace clang;
26using namespace lldb_private;
27
Greg Claytonb01000f2011-01-17 03:46:26 +000028ClangASTSource::~ClangASTSource()
29{
Sean Callanana3d04472011-11-29 00:42:02 +000030 m_ast_importer->ForgetDestination(m_ast_context);
31
Johnny Chenfa21ffd2011-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 Callanana3d04472011-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 Claytonb01000f2011-01-17 03:46:26 +000046}
Chris Lattner24943d22010-06-08 16:52:24 +000047
Greg Claytonb01000f2011-01-17 03:46:26 +000048void
49ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer)
50{
Sean Callananf76afff2011-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 Lattner24943d22010-06-08 16:52:24 +000056}
57
Chris Lattner24943d22010-06-08 16:52:24 +000058// The core lookup interface.
Greg Claytonb01000f2011-01-17 03:46:26 +000059DeclContext::lookup_result
60ClangASTSource::FindExternalVisibleDeclsByName
Greg Claytonf4c7ae02010-10-15 03:36:13 +000061(
62 const DeclContext *decl_ctx,
Greg Clayton8de27c72010-10-15 22:48:33 +000063 DeclarationName clang_decl_name
Greg Claytonf4c7ae02010-10-15 03:36:13 +000064)
65{
Sean Callananf76afff2011-10-28 23:38:38 +000066 if (!m_ast_context)
67 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
68
69 if (GetImportInProgress())
Greg Claytonb01000f2011-01-17 03:46:26 +000070 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
71
72 std::string decl_name (clang_decl_name.getAsString());
73
74// if (m_decl_map.DoingASTImport ())
75// return DeclContext::lookup_result();
76//
Greg Clayton8de27c72010-10-15 22:48:33 +000077 switch (clang_decl_name.getNameKind()) {
Chris Lattner24943d22010-06-08 16:52:24 +000078 // Normal identifiers.
79 case DeclarationName::Identifier:
Sean Callanan0ffff1e2012-04-25 17:46:01 +000080 {
81 clang::IdentifierInfo *identifier_info = clang_decl_name.getAsIdentifierInfo();
82
83 if (!identifier_info ||
84 identifier_info->getBuiltinID() != 0)
85 {
86 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
87 }
88 }
Greg Clayton8de27c72010-10-15 22:48:33 +000089 break;
Chris Lattner24943d22010-06-08 16:52:24 +000090
91 // Operator names. Not important for now.
92 case DeclarationName::CXXOperatorName:
93 case DeclarationName::CXXLiteralOperatorName:
94 return DeclContext::lookup_result();
95
96 // Using directives found in this context.
97 // Tell Sema we didn't find any or we'll end up getting asked a *lot*.
98 case DeclarationName::CXXUsingDirective:
Greg Clayton8de27c72010-10-15 22:48:33 +000099 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
Chris Lattner24943d22010-06-08 16:52:24 +0000100
Chris Lattner24943d22010-06-08 16:52:24 +0000101 case DeclarationName::ObjCZeroArgSelector:
102 case DeclarationName::ObjCOneArgSelector:
103 case DeclarationName::ObjCMultiArgSelector:
Sean Callanan9b714842011-11-09 19:33:21 +0000104 {
105 llvm::SmallVector<NamedDecl*, 1> method_decls;
Chris Lattner24943d22010-06-08 16:52:24 +0000106
Sean Callanan9b714842011-11-09 19:33:21 +0000107 NameSearchContext method_search_context (*this, method_decls, clang_decl_name, decl_ctx);
108
109 FindObjCMethodDecls(method_search_context);
110
111 return SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, method_decls);
112 }
Chris Lattner24943d22010-06-08 16:52:24 +0000113 // These aren't possible in the global context.
114 case DeclarationName::CXXConstructorName:
115 case DeclarationName::CXXDestructorName:
116 case DeclarationName::CXXConversionFunctionName:
117 return DeclContext::lookup_result();
118 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000119
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000120
Sean Callananf76afff2011-10-28 23:38:38 +0000121 if (!GetLookupsEnabled())
Greg Clayton8de27c72010-10-15 22:48:33 +0000122 {
123 // Wait until we see a '$' at the start of a name before we start doing
124 // any lookups so we can avoid lookup up all of the builtin types.
125 if (!decl_name.empty() && decl_name[0] == '$')
126 {
Sean Callananf76afff2011-10-28 23:38:38 +0000127 SetLookupsEnabled (true);
Greg Clayton8de27c72010-10-15 22:48:33 +0000128 }
129 else
130 {
131 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
132 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000133 }
Greg Clayton8de27c72010-10-15 22:48:33 +0000134
Greg Clayton8de27c72010-10-15 22:48:33 +0000135 ConstString const_decl_name(decl_name.c_str());
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000136
137 const char *uniqued_const_decl_name = const_decl_name.GetCString();
138 if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end())
139 {
140 // We are currently looking up this name...
141 return DeclContext::lookup_result();
142 }
143 m_active_lookups.insert(uniqued_const_decl_name);
Greg Claytona8b278a2010-11-15 01:34:18 +0000144// static uint32_t g_depth = 0;
145// ++g_depth;
146// printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000147 llvm::SmallVector<NamedDecl*, 4> name_decls;
148 NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx);
Sean Callananf76afff2011-10-28 23:38:38 +0000149 FindExternalVisibleDecls(name_search_context);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000150 DeclContext::lookup_result result (SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls));
Greg Claytona8b278a2010-11-15 01:34:18 +0000151// --g_depth;
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000152 m_active_lookups.erase (uniqued_const_decl_name);
153 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000154}
155
Greg Claytonb01000f2011-01-17 03:46:26 +0000156void
157ClangASTSource::CompleteType (TagDecl *tag_decl)
Sean Callananbb715f92011-10-29 02:28:18 +0000158{
159 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
160
Sean Callananb2027ec2011-12-08 23:45:45 +0000161 static unsigned int invocation_id = 0;
162 unsigned int current_id = invocation_id++;
163
Sean Callananbb715f92011-10-29 02:28:18 +0000164 if (log)
165 {
Sean Callanan6e22e8b2012-01-13 22:05:55 +0000166 log->Printf(" CompleteTagDecl[%u] on (ASTContext*)%p Completing (TagDecl*)%p named %s",
167 current_id,
Sean Callananb2027ec2011-12-08 23:45:45 +0000168 m_ast_context,
Sean Callanan6e22e8b2012-01-13 22:05:55 +0000169 tag_decl,
Sean Callananb2027ec2011-12-08 23:45:45 +0000170 tag_decl->getName().str().c_str());
171
172 log->Printf(" CTD[%u] Before:", current_id);
Sean Callananbb715f92011-10-29 02:28:18 +0000173 ASTDumper dumper((Decl*)tag_decl);
174 dumper.ToLog(log, " [CTD] ");
175 }
176
Sean Callananb2027ec2011-12-08 23:45:45 +0000177 if (!m_ast_importer->CompleteTagDecl (tag_decl))
178 {
179 // We couldn't complete the type. Maybe there's a definition
180 // somewhere else that can be completed.
181
182 if (log)
183 log->Printf(" CTD[%u] Type could not be completed in the module in which it was first found.", current_id);
184
185 bool found = false;
186
187 DeclContext *decl_ctx = tag_decl->getDeclContext();
188
189 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(decl_ctx))
190 {
191 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
192
193 if (log && log->GetVerbose())
194 log->Printf(" CTD[%u] Inspecting namespace map %p (%d entries)",
195 current_id,
196 namespace_map.get(),
197 (int)namespace_map->size());
198
199 if (!namespace_map)
200 return;
201
202 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
203 i != e && !found;
204 ++i)
205 {
206 if (log)
207 log->Printf(" CTD[%u] Searching namespace %s in module %s",
208 current_id,
209 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
210 i->first->GetFileSpec().GetFilename().GetCString());
211
212 TypeList types;
213
214 SymbolContext null_sc;
215 ConstString name(tag_decl->getName().str().c_str());
216
Greg Claytondc0a38c2012-03-26 23:03:23 +0000217 i->first->FindTypesInNamespace(null_sc, name, &i->second, UINT32_MAX, types);
Sean Callananb2027ec2011-12-08 23:45:45 +0000218
219 for (uint32_t ti = 0, te = types.GetSize();
220 ti != te && !found;
221 ++ti)
222 {
223 lldb::TypeSP type = types.GetTypeAtIndex(ti);
224
225 if (!type)
226 continue;
227
228 lldb::clang_type_t opaque_type = type->GetClangFullType();
229
230 if (!opaque_type)
231 continue;
232
Sean Callanan21f2e192011-12-14 01:13:04 +0000233 const TagType *tag_type = QualType::getFromOpaquePtr(opaque_type)->getAs<TagType>();
Sean Callananb2027ec2011-12-08 23:45:45 +0000234
235 if (!tag_type)
236 continue;
237
238 TagDecl *candidate_tag_decl = const_cast<TagDecl*>(tag_type->getDecl());
239
240 if (m_ast_importer->CompleteTagDeclWithOrigin (tag_decl, candidate_tag_decl))
241 found = true;
242 }
243 }
244 }
245 else
246 {
247 TypeList types;
248
249 SymbolContext null_sc;
250 ConstString name(tag_decl->getName().str().c_str());
251 ClangNamespaceDecl namespace_decl;
252
Enrico Granata146d9522012-11-08 02:22:02 +0000253 const ModuleList &module_list = m_target->GetImages();
Sean Callananb2027ec2011-12-08 23:45:45 +0000254
Greg Claytondc0a38c2012-03-26 23:03:23 +0000255 bool exact_match = false;
Greg Clayton9f95fb62012-04-06 17:41:13 +0000256 module_list.FindTypes (null_sc, name, exact_match, UINT32_MAX, types);
Sean Callananb2027ec2011-12-08 23:45:45 +0000257
258 for (uint32_t ti = 0, te = types.GetSize();
259 ti != te && !found;
260 ++ti)
261 {
262 lldb::TypeSP type = types.GetTypeAtIndex(ti);
263
264 if (!type)
265 continue;
266
267 lldb::clang_type_t opaque_type = type->GetClangFullType();
268
269 if (!opaque_type)
270 continue;
271
Sean Callanan21f2e192011-12-14 01:13:04 +0000272 const TagType *tag_type = QualType::getFromOpaquePtr(opaque_type)->getAs<TagType>();
Sean Callananb2027ec2011-12-08 23:45:45 +0000273
274 if (!tag_type)
275 continue;
276
277 TagDecl *candidate_tag_decl = const_cast<TagDecl*>(tag_type->getDecl());
278
279 if (m_ast_importer->CompleteTagDeclWithOrigin (tag_decl, candidate_tag_decl))
280 found = true;
281 }
282 }
283 }
Sean Callananbb715f92011-10-29 02:28:18 +0000284
285 if (log)
286 {
287 log->Printf(" [CTD] After:");
288 ASTDumper dumper((Decl*)tag_decl);
289 dumper.ToLog(log, " [CTD] ");
290 }
Greg Claytonb01000f2011-01-17 03:46:26 +0000291}
292
293void
Sean Callananbb715f92011-10-29 02:28:18 +0000294ClangASTSource::CompleteType (clang::ObjCInterfaceDecl *interface_decl)
295{
296 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
297
298 if (log)
299 {
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000300 log->Printf(" [CompleteObjCInterfaceDecl] on (ASTContext*)%p Completing an ObjCInterfaceDecl named %s", m_ast_context, interface_decl->getName().str().c_str());
Sean Callananbb715f92011-10-29 02:28:18 +0000301 log->Printf(" [COID] Before:");
302 ASTDumper dumper((Decl*)interface_decl);
303 dumper.ToLog(log, " [COID] ");
304 }
305
306 m_ast_importer->CompleteObjCInterfaceDecl (interface_decl);
307
308 if (log)
309 {
310 log->Printf(" [COID] After:");
311 ASTDumper dumper((Decl*)interface_decl);
312 dumper.ToLog(log, " [COID] ");
313 }
Greg Claytonb01000f2011-01-17 03:46:26 +0000314}
315
Sean Callananbf346eb2012-09-20 23:21:16 +0000316clang::ObjCInterfaceDecl *
317ClangASTSource::GetCompleteObjCInterface (clang::ObjCInterfaceDecl *interface_decl)
318{
319 lldb::ProcessSP process(m_target->GetProcessSP());
320
321 if (!process)
322 return NULL;
323
324 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
325
326 if (!language_runtime)
327 return NULL;
328
329 ConstString class_name(interface_decl->getNameAsString().c_str());
330
331 lldb::TypeSP complete_type_sp(language_runtime->LookupInCompleteClassCache(class_name));
332
333 if (!complete_type_sp)
334 return NULL;
335
336 TypeFromUser complete_type = TypeFromUser(complete_type_sp->GetClangFullType(), complete_type_sp->GetClangAST());
337 lldb::clang_type_t complete_opaque_type = complete_type.GetOpaqueQualType();
338
339 if (!complete_opaque_type)
340 return NULL;
341
342 const clang::Type *complete_clang_type = QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr();
343 const ObjCInterfaceType *complete_interface_type = dyn_cast<ObjCInterfaceType>(complete_clang_type);
344
345 if (!complete_interface_type)
346 return NULL;
347
348 ObjCInterfaceDecl *complete_iface_decl(complete_interface_type->getDecl());
349
350 return complete_iface_decl;
351}
352
Sean Callanan9b6898f2011-07-30 02:42:06 +0000353clang::ExternalLoadResult
Sean Callananbf346eb2012-09-20 23:21:16 +0000354ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000355 bool (*predicate)(Decl::Kind),
356 llvm::SmallVectorImpl<Decl*> &decls)
357{
358 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
359
360 const Decl *context_decl = dyn_cast<Decl>(decl_context);
361
362 if (!context_decl)
363 return ELR_Failure;
364
365 static unsigned int invocation_id = 0;
366 unsigned int current_id = invocation_id++;
367
368 if (log)
369 {
370 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000371 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000372 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000373 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000374 context_named_decl->getNameAsString().c_str(),
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000375 context_decl->getDeclKindName(),
376 context_decl,
Sean Callananbb715f92011-10-29 02:28:18 +0000377 (predicate ? "non-null" : "null"));
378 else if(context_decl)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000379 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%sDecl*)%p with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000380 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000381 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000382 context_decl->getDeclKindName(),
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000383 context_decl,
Sean Callananbb715f92011-10-29 02:28:18 +0000384 (predicate ? "non-null" : "null"));
385 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000386 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000387 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000388 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000389 (predicate ? "non-null" : "null"));
390 }
391
392 Decl *original_decl = NULL;
393 ASTContext *original_ctx = NULL;
394
395 if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx))
396 return ELR_Failure;
397
398 if (log)
399 {
Sean Callanan6e22e8b2012-01-13 22:05:55 +0000400 log->Printf(" FELD[%u] Original decl (Decl*)%p:", current_id, original_decl);
Sean Callananbb715f92011-10-29 02:28:18 +0000401 ASTDumper(original_decl).ToLog(log, " ");
402 }
403
Sean Callananbf346eb2012-09-20 23:21:16 +0000404 if (ObjCInterfaceDecl *original_iface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl))
405 {
406 ObjCInterfaceDecl *complete_iface_decl = GetCompleteObjCInterface(original_iface_decl);
407
408 if (complete_iface_decl && (complete_iface_decl != original_iface_decl))
409 {
410 original_decl = complete_iface_decl;
411 original_ctx = &complete_iface_decl->getASTContext();
412
413 m_ast_importer->SetDeclOrigin(context_decl, original_iface_decl);
414 }
415 }
416
Sean Callananbb715f92011-10-29 02:28:18 +0000417 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
418 {
419 ExternalASTSource *external_source = original_ctx->getExternalSource();
420
421 if (external_source)
422 external_source->CompleteType (original_tag_decl);
423 }
424
Sean Callananb2027ec2011-12-08 23:45:45 +0000425 const DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl);
Sean Callananbb715f92011-10-29 02:28:18 +0000426
427 if (!original_decl_context)
428 return ELR_Failure;
429
430 for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
431 iter != original_decl_context->decls_end();
432 ++iter)
433 {
434 Decl *decl = *iter;
435
436 if (!predicate || predicate(decl->getKind()))
437 {
438 if (log)
439 {
440 ASTDumper ast_dumper(decl);
441 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
Sean Callanan06dc17f2012-09-24 22:25:51 +0000442 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 Callananbb715f92011-10-29 02:28:18 +0000443 else
Sean Callanan06dc17f2012-09-24 22:25:51 +0000444 log->Printf(" FELD[%d] Adding lexical %sDecl %s", current_id, decl->getDeclKindName(), ast_dumper.GetCString());
Sean Callananbb715f92011-10-29 02:28:18 +0000445 }
446
Sean Callanan4938bd62011-11-16 18:20:47 +0000447 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, original_ctx, decl);
Sean Callananbb715f92011-10-29 02:28:18 +0000448
Sean Callananddb59332012-03-20 21:11:12 +0000449 if (!copied_decl)
450 continue;
451
Sean Callanane9478392012-03-15 01:53:17 +0000452 if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl))
453 {
454 QualType copied_field_type = copied_field->getType();
455
456 if (const TagType *copied_field_tag_type = copied_field_type->getAs<TagType>())
457 m_ast_importer->CompleteTagDecl(copied_field_tag_type->getDecl());
458 if (const ObjCObjectType *copied_field_object_type = copied_field_type->getAs<ObjCObjectType>())
459 if (ObjCInterfaceDecl *copied_field_objc_interface_decl = copied_field_object_type->getInterface())
460 m_ast_importer->CompleteObjCInterfaceDecl(copied_field_objc_interface_decl);
461 }
462
Sean Callananbb715f92011-10-29 02:28:18 +0000463 decls.push_back(copied_decl);
464 }
465 }
466
467 return ELR_AlreadyLoaded;
Chris Lattner24943d22010-06-08 16:52:24 +0000468}
469
Sean Callanan9394b5a2011-10-29 19:50:43 +0000470void
471ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
472{
473 assert (m_ast_context);
474
475 const ConstString name(context.m_decl_name.getAsString().c_str());
476
477 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
478
479 static unsigned int invocation_id = 0;
480 unsigned int current_id = invocation_id++;
481
482 if (log)
483 {
484 if (!context.m_decl_context)
Sean Callanan6e22e8b2012-01-13 22:05:55 +0000485 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] on (ASTContext*)%p for '%s' in a NULL DeclContext", current_id, m_ast_context, name.GetCString());
Sean Callanan9394b5a2011-10-29 19:50:43 +0000486 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000487 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 Callanan9394b5a2011-10-29 19:50:43 +0000488 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000489 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 Callanan9394b5a2011-10-29 19:50:43 +0000490 }
491
492 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
493
494 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
495 {
496 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
497
498 if (log && log->GetVerbose())
499 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
500 current_id,
501 namespace_map.get(),
502 (int)namespace_map->size());
503
504 if (!namespace_map)
505 return;
506
507 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
508 i != e;
509 ++i)
510 {
511 if (log)
512 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
513 current_id,
514 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
515 i->first->GetFileSpec().GetFilename().GetCString());
516
517 FindExternalVisibleDecls(context,
518 i->first,
519 i->second,
520 current_id);
521 }
522 }
Sean Callanand3812fa2011-11-15 21:50:18 +0000523 else if (isa<ObjCInterfaceDecl>(context.m_decl_context))
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000524 {
Sean Callanan8f2e3922012-02-04 08:49:35 +0000525 FindObjCPropertyAndIvarDecls(context);
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000526 }
Sean Callanan9394b5a2011-10-29 19:50:43 +0000527 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
528 {
529 // we shouldn't be getting FindExternalVisibleDecls calls for these
530 return;
531 }
532 else
533 {
534 ClangNamespaceDecl namespace_decl;
535
536 if (log)
537 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
538
539 FindExternalVisibleDecls(context,
540 lldb::ModuleSP(),
541 namespace_decl,
542 current_id);
543 }
544
545 if (!context.m_namespace_map->empty())
546 {
547 if (log && log->GetVerbose())
548 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
549 current_id,
550 context.m_namespace_map.get(),
551 (int)context.m_namespace_map->size());
552
553 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
554
555 if (clang_namespace_decl)
556 clang_namespace_decl->setHasExternalVisibleStorage();
557 }
558}
559
560void
561ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
562 lldb::ModuleSP module_sp,
563 ClangNamespaceDecl &namespace_decl,
564 unsigned int current_id)
565{
566 assert (m_ast_context);
567
568 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
569
570 SymbolContextList sc_list;
571
572 const ConstString name(context.m_decl_name.getAsString().c_str());
573
574 const char *name_unique_cstr = name.GetCString();
575
Sean Callanan8f2e3922012-02-04 08:49:35 +0000576 static ConstString id_name("id");
577 static ConstString Class_name("Class");
578
579 if (name == id_name || name == Class_name)
580 return;
581
Sean Callanan9394b5a2011-10-29 19:50:43 +0000582 if (name_unique_cstr == NULL)
583 return;
584
585 // The ClangASTSource is not responsible for finding $-names.
586 if (name_unique_cstr[0] == '$')
587 return;
588
589 if (module_sp && namespace_decl)
590 {
591 ClangNamespaceDecl found_namespace_decl;
592
593 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
594
595 if (symbol_vendor)
596 {
597 SymbolContext null_sc;
598
599 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
600
601 if (found_namespace_decl)
602 {
603 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
604
605 if (log)
606 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
607 current_id,
608 name.GetCString(),
609 module_sp->GetFileSpec().GetFilename().GetCString());
610 }
611 }
612 }
613 else
614 {
Enrico Granata146d9522012-11-08 02:22:02 +0000615 const ModuleList &target_images = m_target->GetImages();
Jim Ingham93367902012-05-30 02:19:25 +0000616 Mutex::Locker modules_locker (target_images.GetMutex());
Sean Callanan9394b5a2011-10-29 19:50:43 +0000617
Jim Ingham93367902012-05-30 02:19:25 +0000618 for (uint32_t i = 0, e = target_images.GetSize();
Sean Callanan9394b5a2011-10-29 19:50:43 +0000619 i != e;
620 ++i)
621 {
Jim Ingham93367902012-05-30 02:19:25 +0000622 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
Sean Callanan9394b5a2011-10-29 19:50:43 +0000623
624 if (!image)
625 continue;
626
627 ClangNamespaceDecl found_namespace_decl;
628
629 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
630
631 if (!symbol_vendor)
632 continue;
633
634 SymbolContext null_sc;
635
636 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
637
638 if (found_namespace_decl)
639 {
640 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
641
642 if (log)
643 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
644 current_id,
645 name.GetCString(),
646 image->GetFileSpec().GetFilename().GetCString());
647 }
648 }
649 }
650
Sean Callanan9394b5a2011-10-29 19:50:43 +0000651 do
652 {
653 TypeList types;
654 SymbolContext null_sc;
Greg Claytondc0a38c2012-03-26 23:03:23 +0000655 const bool exact_match = false;
Sean Callanan0f71d192011-12-19 19:38:39 +0000656
Sean Callanan9394b5a2011-10-29 19:50:43 +0000657 if (module_sp && namespace_decl)
Greg Claytondc0a38c2012-03-26 23:03:23 +0000658 module_sp->FindTypesInNamespace(null_sc, name, &namespace_decl, 1, types);
Sean Callanan0f71d192011-12-19 19:38:39 +0000659 else
Greg Clayton9f95fb62012-04-06 17:41:13 +0000660 m_target->GetImages().FindTypes(null_sc, name, exact_match, 1, types);
Sean Callanan9394b5a2011-10-29 19:50:43 +0000661
662 if (types.GetSize())
663 {
664 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
665
666 if (log)
667 {
668 const char *name_string = type_sp->GetName().GetCString();
669
670 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
671 current_id,
672 name.GetCString(),
673 (name_string ? name_string : "<anonymous>"));
674 }
Sean Callananc718b962012-09-11 21:44:01 +0000675
676 clang::ASTContext *type_ast = type_sp->GetClangAST();
677 lldb::clang_type_t full_type = type_sp->GetClangFullType();
Sean Callanane1301a62011-12-06 03:41:14 +0000678
Sean Callananc718b962012-09-11 21:44:01 +0000679 void *copied_type = GuardedCopyType(m_ast_context, type_ast, full_type);
Sean Callanane1301a62011-12-06 03:41:14 +0000680
Sean Callanandc5fce12011-12-01 21:04:37 +0000681 if (!copied_type)
682 {
683 if (log)
Sean Callanan282c22c2012-12-19 23:05:01 +0000684 log->Printf(" CAS::FEVD[%u] - Couldn't export a type",
Sean Callanane1301a62011-12-06 03:41:14 +0000685 current_id);
686
Sean Callanandc5fce12011-12-01 21:04:37 +0000687 break;
688 }
689
Sean Callanan9394b5a2011-10-29 19:50:43 +0000690 context.AddTypeDecl(copied_type);
691 }
Sean Callanan282c22c2012-12-19 23:05:01 +0000692 else
693 {
694 do
695 {
696 // Couldn't find any types elsewhere. Try the Objective-C runtime if one exists.
697
698 lldb::ProcessSP process(m_target->GetProcessSP());
699
700 if (!process)
701 break;
702
703 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
704
705 if (!language_runtime)
706 break;
707
708 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
709
710 if (!type_vendor)
711 break;
712
713 bool append = false;
714 uint32_t max_matches = 1;
715 std::vector <ClangASTType> types;
716
717 if (!type_vendor->FindTypes(name,
718 append,
719 max_matches,
720 types))
721 break;
722
723 if (log)
724 {
725 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\" in the runtime",
726 current_id,
727 name.GetCString());
728 }
729
730 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
731
732 clang::QualType runtime_qual_type(runtime_clang_type, 0);
733
734 void *copied_type = GuardedCopyType(m_ast_context, type_vendor->GetClangASTContext(), runtime_qual_type.getAsOpaquePtr());
735
736 if (!copied_type)
737 {
738 if (log)
739 log->Printf(" CAS::FEVD[%u] - Couldn't export a type from the runtime",
740 current_id);
741
742 break;
743 }
744
745 context.AddTypeDecl(copied_type);
746 }
747 while(0);
748 }
Sean Callanan673f3db2011-11-30 22:11:59 +0000749
Sean Callanan9394b5a2011-10-29 19:50:43 +0000750 } while(0);
751}
752
Sean Callanan4cbeeff2012-11-02 17:09:58 +0000753template <class D> class TaggedASTDecl {
754public:
755 TaggedASTDecl() : decl(NULL) { }
756 TaggedASTDecl(D *_decl) : decl(_decl) { }
757 bool IsValid() const { return (decl != NULL); }
758 bool IsInvalid() const { return !IsValid(); }
759 D *operator->() const { return decl; }
760 D *decl;
761};
762
763template <class D2, template <class D> class TD, class D1>
764TD<D2>
765DynCast(TD<D1> source)
766{
767 return TD<D2> (dyn_cast<D2>(source.decl));
768}
769
770template <class D = Decl> class DeclFromParser;
771template <class D = Decl> class DeclFromUser;
772
773template <class D> class DeclFromParser : public TaggedASTDecl<D> {
774public:
775 DeclFromParser() : TaggedASTDecl<D>() { }
776 DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) { }
777
778 DeclFromUser<D> GetOrigin(ClangASTImporter *importer);
779};
780
781template <class D> class DeclFromUser : public TaggedASTDecl<D> {
782public:
783 DeclFromUser() : TaggedASTDecl<D>() { }
784 DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) { }
785
786 DeclFromParser<D> Import(ClangASTImporter *importer, ASTContext &dest_ctx);
787};
788
789template <class D>
790DeclFromUser<D>
791DeclFromParser<D>::GetOrigin(ClangASTImporter *importer)
792{
793 DeclFromUser <> origin_decl;
794 importer->ResolveDeclOrigin(this->decl, &origin_decl.decl, NULL);
795 if (origin_decl.IsInvalid())
796 return DeclFromUser<D>();
797 return DeclFromUser<D>(dyn_cast<D>(origin_decl.decl));
798}
799
800template <class D>
801DeclFromParser<D>
802DeclFromUser<D>::Import(ClangASTImporter *importer, ASTContext &dest_ctx)
803{
804 DeclFromParser <> parser_generic_decl(importer->CopyDecl(&dest_ctx, &this->decl->getASTContext(), this->decl));
805 if (parser_generic_decl.IsInvalid())
806 return DeclFromParser<D>();
807 return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
808}
809
Sean Callanan14571fc2012-11-28 03:23:20 +0000810static bool
Sean Callananc718b962012-09-11 21:44:01 +0000811FindObjCMethodDeclsWithOrigin (unsigned int current_id,
812 NameSearchContext &context,
813 ObjCInterfaceDecl *original_interface_decl,
814 clang::ASTContext *ast_context,
815 ClangASTImporter *ast_importer,
816 const char *log_info)
817{
818 const DeclarationName &decl_name(context.m_decl_name);
819 clang::ASTContext *original_ctx = &original_interface_decl->getASTContext();
820
821 Selector original_selector;
822
823 if (decl_name.isObjCZeroArgSelector())
824 {
825 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name.getAsString());
826 original_selector = original_ctx->Selectors.getSelector(0, &ident);
827 }
828 else if (decl_name.isObjCOneArgSelector())
829 {
830 const std::string &decl_name_string = decl_name.getAsString();
831 std::string decl_name_string_without_colon(decl_name_string.c_str(), decl_name_string.length() - 1);
832 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name_string_without_colon.c_str());
833 original_selector = original_ctx->Selectors.getSelector(1, &ident);
834 }
835 else
836 {
837 SmallVector<IdentifierInfo *, 4> idents;
838
839 clang::Selector sel = decl_name.getObjCSelector();
840
841 int num_args = sel.getNumArgs();
842
843 for (unsigned i = 0;
844 i != num_args;
845 ++i)
846 {
847 idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i)));
848 }
849
850 original_selector = original_ctx->Selectors.getSelector(num_args, idents.data());
851 }
852
853 DeclarationName original_decl_name(original_selector);
854
855 ObjCInterfaceDecl::lookup_result result = original_interface_decl->lookup(original_decl_name);
856
Sean Callananca8fb172012-12-21 21:34:42 +0000857 if (result.empty())
Sean Callanan14571fc2012-11-28 03:23:20 +0000858 return false;
Sean Callananc718b962012-09-11 21:44:01 +0000859
Sean Callananca8fb172012-12-21 21:34:42 +0000860 if (!result[0])
Sean Callanan14571fc2012-11-28 03:23:20 +0000861 return false;
Sean Callananc718b962012-09-11 21:44:01 +0000862
Sean Callananca8fb172012-12-21 21:34:42 +0000863 ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(result[0]);
Sean Callananc718b962012-09-11 21:44:01 +0000864
865 if (!result_method)
Sean Callanan14571fc2012-11-28 03:23:20 +0000866 return false;
Sean Callananc718b962012-09-11 21:44:01 +0000867
868 Decl *copied_decl = ast_importer->CopyDecl(ast_context, &result_method->getASTContext(), result_method);
869
870 if (!copied_decl)
Sean Callanan14571fc2012-11-28 03:23:20 +0000871 return false;
Sean Callananc718b962012-09-11 21:44:01 +0000872
873 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
874
875 if (!copied_method_decl)
Sean Callanan14571fc2012-11-28 03:23:20 +0000876 return false;
Sean Callananc718b962012-09-11 21:44:01 +0000877
878 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
879
880 if (log)
881 {
882 ASTDumper dumper((Decl*)copied_method_decl);
883 log->Printf(" CAS::FOMD[%d] found (%s) %s", current_id, log_info, dumper.GetCString());
884 }
885
886 context.AddNamedDecl(copied_method_decl);
887
Sean Callanan14571fc2012-11-28 03:23:20 +0000888 return true;
Sean Callananc718b962012-09-11 21:44:01 +0000889}
890
Sean Callanan9b714842011-11-09 19:33:21 +0000891void
892ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
893{
894 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
895
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000896 static unsigned int invocation_id = 0;
897 unsigned int current_id = invocation_id++;
898
Sean Callanan9b714842011-11-09 19:33:21 +0000899 const DeclarationName &decl_name(context.m_decl_name);
900 const DeclContext *decl_ctx(context.m_decl_context);
901
902 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
903
904 if (!interface_decl)
905 return;
906
Sean Callanan8d611562012-04-05 00:12:52 +0000907 do
908 {
909 Decl *original_decl = NULL;
910 ASTContext *original_ctx = NULL;
911
912 m_ast_importer->ResolveDeclOrigin(interface_decl, &original_decl, &original_ctx);
913
914 if (!original_decl)
915 break;
916
917 ObjCInterfaceDecl *original_interface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl);
918
Sean Callanan14571fc2012-11-28 03:23:20 +0000919 if (FindObjCMethodDeclsWithOrigin(current_id,
920 context,
921 original_interface_decl,
922 m_ast_context,
923 m_ast_importer,
924 "at origin"))
925 return; // found it, no need to look any further
Sean Callanan8d611562012-04-05 00:12:52 +0000926 } while (0);
927
Sean Callanan9b714842011-11-09 19:33:21 +0000928 StreamString ss;
Sean Callanane1301a62011-12-06 03:41:14 +0000929
Sean Callanan9b714842011-11-09 19:33:21 +0000930 if (decl_name.isObjCZeroArgSelector())
931 {
932 ss.Printf("%s", decl_name.getAsString().c_str());
933 }
934 else if (decl_name.isObjCOneArgSelector())
935 {
Sean Callanan1d9ffe22011-11-14 18:29:46 +0000936 ss.Printf("%s", decl_name.getAsString().c_str());
Sean Callanan9b714842011-11-09 19:33:21 +0000937 }
938 else
939 {
940 clang::Selector sel = decl_name.getObjCSelector();
941
942 for (unsigned i = 0, e = sel.getNumArgs();
943 i != e;
944 ++i)
945 {
946 llvm::StringRef r = sel.getNameForSlot(i);
947 ss.Printf("%s:", r.str().c_str());
948 }
949 }
950 ss.Flush();
951
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000952 ConstString selector_name(ss.GetData());
953
Sean Callanan9b714842011-11-09 19:33:21 +0000954 if (log)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000955 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
956 current_id,
957 m_ast_context,
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000958 interface_decl->getNameAsString().c_str(),
959 selector_name.AsCString());
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000960 SymbolContextList sc_list;
961
962 const bool include_symbols = false;
Sean Callanan302d78c2012-02-10 22:52:19 +0000963 const bool include_inlines = false;
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000964 const bool append = false;
965
Sean Callanane0028b82012-01-19 02:17:40 +0000966 std::string interface_name = interface_decl->getNameAsString();
967
968 do
969 {
970 StreamString ms;
971 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());
972 ms.Flush();
973 ConstString instance_method_name(ms.GetData());
974
Sean Callanan302d78c2012-02-10 22:52:19 +0000975 m_target->GetImages().FindFunctions(instance_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanane0028b82012-01-19 02:17:40 +0000976
977 if (sc_list.GetSize())
978 break;
979
980 ms.Clear();
981 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());
982 ms.Flush();
983 ConstString class_method_name(ms.GetData());
984
Sean Callanan302d78c2012-02-10 22:52:19 +0000985 m_target->GetImages().FindFunctions(class_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanane0028b82012-01-19 02:17:40 +0000986
987 if (sc_list.GetSize())
988 break;
989
990 // Fall back and check for methods in categories. If we find methods this way, we need to check that they're actually in
991 // categories on the desired class.
992
993 SymbolContextList candidate_sc_list;
994
Sean Callanan302d78c2012-02-10 22:52:19 +0000995 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, include_inlines, append, candidate_sc_list);
Sean Callanane0028b82012-01-19 02:17:40 +0000996
997 for (uint32_t ci = 0, ce = candidate_sc_list.GetSize();
998 ci != ce;
999 ++ci)
1000 {
1001 SymbolContext candidate_sc;
1002
1003 if (!candidate_sc_list.GetContextAtIndex(ci, candidate_sc))
1004 continue;
1005
1006 if (!candidate_sc.function)
1007 continue;
1008
1009 const char *candidate_name = candidate_sc.function->GetName().AsCString();
1010
1011 const char *cursor = candidate_name;
1012
1013 if (*cursor != '+' && *cursor != '-')
1014 continue;
1015
1016 ++cursor;
1017
1018 if (*cursor != '[')
1019 continue;
1020
1021 ++cursor;
1022
1023 size_t interface_len = interface_name.length();
1024
1025 if (strncmp(cursor, interface_name.c_str(), interface_len))
1026 continue;
1027
1028 cursor += interface_len;
1029
1030 if (*cursor == ' ' || *cursor == '(')
1031 sc_list.Append(candidate_sc);
1032 }
1033 }
1034 while (0);
Sean Callananf2a0a5c2011-11-11 20:37:26 +00001035
Sean Callananc718b962012-09-11 21:44:01 +00001036 if (sc_list.GetSize())
Sean Callananf2a0a5c2011-11-11 20:37:26 +00001037 {
Sean Callananc718b962012-09-11 21:44:01 +00001038 // We found a good function symbol. Use that.
Sean Callananf2a0a5c2011-11-11 20:37:26 +00001039
Sean Callananc718b962012-09-11 21:44:01 +00001040 for (uint32_t i = 0, e = sc_list.GetSize();
1041 i != e;
1042 ++i)
Sean Callananf2a0a5c2011-11-11 20:37:26 +00001043 {
Sean Callananc718b962012-09-11 21:44:01 +00001044 SymbolContext sc;
Sean Callananf2a0a5c2011-11-11 20:37:26 +00001045
Sean Callananc718b962012-09-11 21:44:01 +00001046 if (!sc_list.GetContextAtIndex(i, sc))
Sean Callananf2a0a5c2011-11-11 20:37:26 +00001047 continue;
1048
Sean Callananc718b962012-09-11 21:44:01 +00001049 if (!sc.function)
Sean Callananf2a0a5c2011-11-11 20:37:26 +00001050 continue;
1051
Sean Callananc718b962012-09-11 21:44:01 +00001052 DeclContext *function_ctx = sc.function->GetClangDeclContext();
1053
1054 if (!function_ctx)
1055 continue;
1056
1057 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
1058
1059 if (!method_decl)
1060 continue;
1061
1062 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
1063
1064 if (!found_interface_decl)
1065 continue;
1066
1067 if (found_interface_decl->getName() == interface_decl->getName())
Sean Callananf2a0a5c2011-11-11 20:37:26 +00001068 {
Sean Callananc718b962012-09-11 21:44:01 +00001069 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
1070
1071 if (!copied_decl)
1072 continue;
1073
1074 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
1075
1076 if (!copied_method_decl)
1077 continue;
1078
1079 if (log)
1080 {
1081 ASTDumper dumper((Decl*)copied_method_decl);
Sean Callanan4cbeeff2012-11-02 17:09:58 +00001082 log->Printf(" CAS::FOMD[%d] found (in symbols) %s", current_id, dumper.GetCString());
Sean Callananc718b962012-09-11 21:44:01 +00001083 }
1084
1085 context.AddNamedDecl(copied_method_decl);
Sean Callananf2a0a5c2011-11-11 20:37:26 +00001086 }
Sean Callananf2a0a5c2011-11-11 20:37:26 +00001087 }
Sean Callanan4cbeeff2012-11-02 17:09:58 +00001088
1089 return;
Sean Callananf2a0a5c2011-11-11 20:37:26 +00001090 }
Sean Callanan4cbeeff2012-11-02 17:09:58 +00001091
1092 // Try the debug information.
1093
1094 do
Sean Callananc718b962012-09-11 21:44:01 +00001095 {
Sean Callanan4cbeeff2012-11-02 17:09:58 +00001096 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(interface_decl));
1097
1098 if (!complete_interface_decl)
1099 break;
1100
1101 // We found the complete interface. The runtime never needs to be queried in this scenario.
1102
1103 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
1104
1105 if (complete_interface_decl == interface_decl)
1106 break; // already checked this one
1107
1108 if (log)
1109 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1110 current_id,
1111 complete_interface_decl,
1112 &complete_iface_decl->getASTContext());
1113
1114 FindObjCMethodDeclsWithOrigin(current_id,
1115 context,
1116 complete_interface_decl,
1117 m_ast_context,
1118 m_ast_importer,
1119 "in debug info");
1120
1121 return;
Sean Callananc718b962012-09-11 21:44:01 +00001122 }
Sean Callanan4cbeeff2012-11-02 17:09:58 +00001123 while (0);
Sean Callanan8f2e3922012-02-04 08:49:35 +00001124
Sean Callanan4cbeeff2012-11-02 17:09:58 +00001125 do
1126 {
1127 // Check the runtime only if the debug information didn't have a complete interface.
1128
1129 lldb::ProcessSP process(m_target->GetProcessSP());
1130
1131 if (!process)
1132 break;
1133
1134 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1135
Sean Callanan282c22c2012-12-19 23:05:01 +00001136 if (!language_runtime)
1137 break;
1138
Sean Callanan4cbeeff2012-11-02 17:09:58 +00001139 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
1140
1141 if (!type_vendor)
1142 break;
1143
1144 ConstString interface_name(interface_decl->getNameAsString().c_str());
1145 bool append = false;
1146 uint32_t max_matches = 1;
1147 std::vector <ClangASTType> types;
1148
1149 if (!type_vendor->FindTypes(interface_name,
1150 append,
1151 max_matches,
1152 types))
1153 break;
1154
1155 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
1156
1157 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
1158
1159 if (!runtime_interface_type)
1160 break;
1161
1162 ObjCInterfaceDecl *runtime_interface_decl = runtime_interface_type->getDecl();
1163
1164 FindObjCMethodDeclsWithOrigin(current_id,
1165 context,
1166 runtime_interface_decl,
1167 m_ast_context,
1168 m_ast_importer,
1169 "in runtime");
1170 }
1171 while(0);
Sean Callanan8f2e3922012-02-04 08:49:35 +00001172}
1173
Sean Callanan931acec2012-02-22 23:57:45 +00001174static bool
1175FindObjCPropertyAndIvarDeclsWithOrigin (unsigned int current_id,
1176 NameSearchContext &context,
1177 clang::ASTContext &ast_context,
1178 ClangASTImporter *ast_importer,
1179 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl)
1180{
1181 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1182
1183 if (origin_iface_decl.IsInvalid())
1184 return false;
1185
1186 std::string name_str = context.m_decl_name.getAsString();
1187 StringRef name(name_str.c_str());
1188 IdentifierInfo &name_identifier(origin_iface_decl->getASTContext().Idents.get(name));
1189
1190 DeclFromUser<ObjCPropertyDecl> origin_property_decl(origin_iface_decl->FindPropertyDeclaration(&name_identifier));
1191
1192 bool found = false;
1193
1194 if (origin_property_decl.IsValid())
1195 {
1196 DeclFromParser<ObjCPropertyDecl> parser_property_decl(origin_property_decl.Import(ast_importer, ast_context));
1197 if (parser_property_decl.IsValid())
1198 {
1199 if (log)
1200 {
1201 ASTDumper dumper((Decl*)parser_property_decl.decl);
1202 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1203 }
1204
1205 context.AddNamedDecl(parser_property_decl.decl);
1206 found = true;
1207 }
1208 }
1209
1210 DeclFromUser<ObjCIvarDecl> origin_ivar_decl(origin_iface_decl->getIvarDecl(&name_identifier));
1211
1212 if (origin_ivar_decl.IsValid())
1213 {
1214 DeclFromParser<ObjCIvarDecl> parser_ivar_decl(origin_ivar_decl.Import(ast_importer, ast_context));
1215 if (parser_ivar_decl.IsValid())
1216 {
1217 if (log)
1218 {
1219 ASTDumper dumper((Decl*)parser_ivar_decl.decl);
1220 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1221 }
1222
1223 context.AddNamedDecl(parser_ivar_decl.decl);
1224 found = true;
1225 }
1226 }
1227
1228 return found;
1229}
1230
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001231void
Sean Callanan8f2e3922012-02-04 08:49:35 +00001232ClangASTSource::FindObjCPropertyAndIvarDecls (NameSearchContext &context)
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001233{
1234 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1235
1236 static unsigned int invocation_id = 0;
1237 unsigned int current_id = invocation_id++;
1238
Sean Callanan8f2e3922012-02-04 08:49:35 +00001239 DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl(cast<ObjCInterfaceDecl>(context.m_decl_context));
1240 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(parser_iface_decl.GetOrigin(m_ast_importer));
Sean Callanan931acec2012-02-22 23:57:45 +00001241
1242 ConstString class_name(parser_iface_decl->getNameAsString().c_str());
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001243
1244 if (log)
Sean Callanan8f2e3922012-02-04 08:49:35 +00001245 log->Printf("ClangASTSource::FindObjCPropertyAndIvarDecls[%d] on (ASTContext*)%p for '%s.%s'",
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001246 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001247 m_ast_context,
Sean Callanan8f2e3922012-02-04 08:49:35 +00001248 parser_iface_decl->getNameAsString().c_str(),
Sean Callanan931acec2012-02-22 23:57:45 +00001249 context.m_decl_name.getAsString().c_str());
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001250
Sean Callanan931acec2012-02-22 23:57:45 +00001251 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1252 context,
1253 *m_ast_context,
1254 m_ast_importer,
1255 origin_iface_decl))
1256 return;
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001257
Sean Callanan931acec2012-02-22 23:57:45 +00001258 if (log)
1259 log->Printf("CAS::FOPD[%d] couldn't find the property on origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p, searching elsewhere...",
1260 current_id,
1261 origin_iface_decl.decl,
1262 &origin_iface_decl->getASTContext());
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001263
Sean Callanan931acec2012-02-22 23:57:45 +00001264 SymbolContext null_sc;
1265 TypeList type_list;
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001266
Sean Callananc718b962012-09-11 21:44:01 +00001267 do
1268 {
Sean Callananbf346eb2012-09-20 23:21:16 +00001269 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(const_cast<ObjCInterfaceDecl*>(parser_iface_decl.decl));
Sean Callananc718b962012-09-11 21:44:01 +00001270
Sean Callananbf346eb2012-09-20 23:21:16 +00001271 if (!complete_interface_decl)
Sean Callananc718b962012-09-11 21:44:01 +00001272 break;
1273
Sean Callanan4cbeeff2012-11-02 17:09:58 +00001274 // We found the complete interface. The runtime never needs to be queried in this scenario.
1275
Sean Callananbf346eb2012-09-20 23:21:16 +00001276 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_decl);
Sean Callananc718b962012-09-11 21:44:01 +00001277
1278 if (complete_iface_decl.decl == origin_iface_decl.decl)
1279 break; // already checked this one
1280
1281 if (log)
1282 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1283 current_id,
1284 complete_iface_decl.decl,
1285 &complete_iface_decl->getASTContext());
1286
Sean Callanan4cbeeff2012-11-02 17:09:58 +00001287 FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1288 context,
1289 *m_ast_context,
1290 m_ast_importer,
1291 complete_iface_decl);
1292
1293 return;
Sean Callananc718b962012-09-11 21:44:01 +00001294 }
1295 while(0);
Sean Callanan931acec2012-02-22 23:57:45 +00001296
Sean Callananc718b962012-09-11 21:44:01 +00001297 do
1298 {
Sean Callanan4cbeeff2012-11-02 17:09:58 +00001299 // Check the runtime only if the debug information didn't have a complete interface.
1300
1301 lldb::ProcessSP process(m_target->GetProcessSP());
1302
1303 if (!process)
1304 return;
1305
1306 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1307
1308 if (!language_runtime)
1309 return;
Sean Callananc718b962012-09-11 21:44:01 +00001310
1311 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
1312
1313 if (!type_vendor)
1314 break;
1315
1316 bool append = false;
1317 uint32_t max_matches = 1;
1318 std::vector <ClangASTType> types;
1319
1320 if (!type_vendor->FindTypes(class_name,
1321 append,
1322 max_matches,
1323 types))
1324 break;
1325
1326 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
1327
1328 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
1329
1330 if (!runtime_interface_type)
1331 break;
1332
1333 DeclFromUser<const ObjCInterfaceDecl> runtime_iface_decl(runtime_interface_type->getDecl());
1334
1335 if (log)
1336 log->Printf("CAS::FOPD[%d] trying runtime (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1337 current_id,
1338 runtime_iface_decl.decl,
1339 &runtime_iface_decl->getASTContext());
1340
1341 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1342 context,
1343 *m_ast_context,
1344 m_ast_importer,
1345 runtime_iface_decl))
1346 return;
1347 }
1348 while(0);
Sean Callanan8f2e3922012-02-04 08:49:35 +00001349}
1350
1351typedef llvm::DenseMap <const FieldDecl *, uint64_t> FieldOffsetMap;
1352typedef llvm::DenseMap <const CXXRecordDecl *, CharUnits> BaseOffsetMap;
1353
1354template <class D, class O>
1355static bool
1356ImportOffsetMap (llvm::DenseMap <const D*, O> &destination_map,
1357 llvm::DenseMap <const D*, O> &source_map,
1358 ClangASTImporter *importer,
1359 ASTContext &dest_ctx)
1360{
1361 typedef llvm::DenseMap <const D*, O> MapType;
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001362
Sean Callanan8f2e3922012-02-04 08:49:35 +00001363 for (typename MapType::iterator fi = source_map.begin(), fe = source_map.end();
1364 fi != fe;
1365 ++fi)
1366 {
1367 DeclFromUser <D> user_decl(const_cast<D*>(fi->first));
1368 DeclFromParser <D> parser_decl(user_decl.Import(importer, dest_ctx));
1369 if (parser_decl.IsInvalid())
1370 return false;
1371 destination_map.insert(std::pair<const D *, O>(parser_decl.decl, fi->second));
1372 }
1373
1374 return true;
1375}
1376
1377template <bool IsVirtual> bool ExtractBaseOffsets (const ASTRecordLayout &record_layout,
1378 DeclFromUser<const CXXRecordDecl> &record,
1379 BaseOffsetMap &base_offsets)
1380{
1381 for (CXXRecordDecl::base_class_const_iterator
1382 bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()),
1383 be = (IsVirtual ? record->vbases_end() : record->bases_end());
1384 bi != be;
1385 ++bi)
1386 {
1387 if (!IsVirtual && bi->isVirtual())
1388 continue;
1389
1390 const clang::Type *origin_base_type = bi->getType().getTypePtr();
1391 const clang::RecordType *origin_base_record_type = origin_base_type->getAs<RecordType>();
1392
1393 if (!origin_base_record_type)
1394 return false;
1395
1396 DeclFromUser <RecordDecl> origin_base_record(origin_base_record_type->getDecl());
1397
1398 if (origin_base_record.IsInvalid())
1399 return false;
1400
1401 DeclFromUser <CXXRecordDecl> origin_base_cxx_record(DynCast<CXXRecordDecl>(origin_base_record));
1402
1403 if (origin_base_cxx_record.IsInvalid())
1404 return false;
1405
1406 CharUnits base_offset;
1407
1408 if (IsVirtual)
1409 base_offset = record_layout.getVBaseClassOffset(origin_base_cxx_record.decl);
1410 else
1411 base_offset = record_layout.getBaseClassOffset(origin_base_cxx_record.decl);
1412
1413 base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(origin_base_cxx_record.decl, base_offset));
1414 }
1415
1416 return true;
1417}
1418
1419bool
1420ClangASTSource::layoutRecordType(const RecordDecl *record,
1421 uint64_t &size,
1422 uint64_t &alignment,
1423 FieldOffsetMap &field_offsets,
1424 BaseOffsetMap &base_offsets,
1425 BaseOffsetMap &virtual_base_offsets)
1426{
1427 static unsigned int invocation_id = 0;
1428 unsigned int current_id = invocation_id++;
1429
1430 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1431
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001432 if (log)
1433 {
Sean Callanan8f2e3922012-02-04 08:49:35 +00001434 log->Printf("LayoutRecordType[%u] on (RecordDecl*)%p [name = '%s']",
1435 current_id,
1436 m_ast_context,
1437 record->getNameAsString().c_str());
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001438 }
Sean Callanan8f2e3922012-02-04 08:49:35 +00001439
1440
1441 DeclFromParser <const RecordDecl> parser_record(record);
1442 DeclFromUser <const RecordDecl> origin_record(parser_record.GetOrigin(m_ast_importer));
1443
1444 if (origin_record.IsInvalid())
1445 return false;
1446
1447 FieldOffsetMap origin_field_offsets;
1448 BaseOffsetMap origin_base_offsets;
1449 BaseOffsetMap origin_virtual_base_offsets;
1450
Sean Callanan33bff9a2012-04-07 00:06:00 +00001451 ClangASTContext::GetCompleteDecl(&origin_record->getASTContext(), const_cast<RecordDecl*>(origin_record.decl));
1452
1453 if (!origin_record.decl->getDefinition())
1454 return false;
1455
Sean Callanan8f2e3922012-02-04 08:49:35 +00001456 const ASTRecordLayout &record_layout(origin_record->getASTContext().getASTRecordLayout(origin_record.decl));
1457
1458 int field_idx = 0;
1459
1460 for (RecordDecl::field_iterator fi = origin_record->field_begin(), fe = origin_record->field_end();
1461 fi != fe;
1462 ++fi)
1463 {
1464 uint64_t field_offset = record_layout.getFieldOffset(field_idx);
1465
1466 origin_field_offsets.insert(std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
1467
1468 field_idx++;
1469 }
1470
1471 ASTContext &parser_ast_context(record->getASTContext());
1472
1473 DeclFromUser <const CXXRecordDecl> origin_cxx_record(DynCast<const CXXRecordDecl>(origin_record));
1474
1475 if (origin_cxx_record.IsValid())
1476 {
1477 if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record, origin_base_offsets) ||
1478 !ExtractBaseOffsets<true>(record_layout, origin_cxx_record, origin_virtual_base_offsets))
1479 return false;
1480 }
1481
1482 if (!ImportOffsetMap(field_offsets, origin_field_offsets, m_ast_importer, parser_ast_context) ||
1483 !ImportOffsetMap(base_offsets, origin_base_offsets, m_ast_importer, parser_ast_context) ||
1484 !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets, m_ast_importer, parser_ast_context))
1485 return false;
1486
1487 size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth();
1488 alignment = record_layout.getAlignment().getQuantity() * m_ast_context->getCharWidth();
1489
1490 if (log)
1491 {
1492 log->Printf("LRT[%u] returned:", current_id);
1493 log->Printf("LRT[%u] Original = (RecordDecl*)%p", current_id, origin_record.decl);
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001494 log->Printf("LRT[%u] Size = %" PRId64, current_id, size);
1495 log->Printf("LRT[%u] Alignment = %" PRId64, current_id, alignment);
Sean Callanan8f2e3922012-02-04 08:49:35 +00001496 log->Printf("LRT[%u] Fields:", current_id);
1497 for (RecordDecl::field_iterator fi = record->field_begin(), fe = record->field_end();
1498 fi != fe;
1499 ++fi)
1500 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001501 log->Printf("LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %" PRId64 " bits",
Sean Callanan8f2e3922012-02-04 08:49:35 +00001502 current_id,
1503 *fi,
1504 fi->getNameAsString().c_str(),
1505 field_offsets[*fi]);
1506 }
1507 DeclFromParser <const CXXRecordDecl> parser_cxx_record = DynCast<const CXXRecordDecl>(parser_record);
1508 if (parser_cxx_record.IsValid())
1509 {
1510 log->Printf("LRT[%u] Bases:", current_id);
1511 for (CXXRecordDecl::base_class_const_iterator bi = parser_cxx_record->bases_begin(), be = parser_cxx_record->bases_end();
1512 bi != be;
1513 ++bi)
1514 {
1515 bool is_virtual = bi->isVirtual();
1516
1517 QualType base_type = bi->getType();
1518 const RecordType *base_record_type = base_type->getAs<RecordType>();
1519 DeclFromParser <RecordDecl> base_record(base_record_type->getDecl());
1520 DeclFromParser <CXXRecordDecl> base_cxx_record = DynCast<CXXRecordDecl>(base_record);
1521
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001522 log->Printf("LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %" PRId64 " chars",
Sean Callanan8f2e3922012-02-04 08:49:35 +00001523 current_id,
1524 (is_virtual ? "Virtual " : ""),
1525 base_cxx_record.decl,
1526 base_cxx_record.decl->getNameAsString().c_str(),
1527 (is_virtual ? virtual_base_offsets[base_cxx_record.decl].getQuantity() :
1528 base_offsets[base_cxx_record.decl].getQuantity()));
1529 }
1530 }
1531 else
1532 {
1533 log->Printf("LRD[%u] Not a CXXRecord, so no bases", current_id);
1534 }
1535 }
1536
1537 return true;
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001538}
1539
Sean Callanan73b520f2011-10-29 01:58:46 +00001540void
1541ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
1542 const ConstString &name,
1543 ClangASTImporter::NamespaceMapSP &parent_map) const
1544{
1545 static unsigned int invocation_id = 0;
1546 unsigned int current_id = invocation_id++;
1547
1548 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1549
1550 if (log)
1551 {
1552 if (parent_map && parent_map->size())
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001553 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +00001554 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001555 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +00001556 name.GetCString(),
1557 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
1558 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001559 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +00001560 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001561 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +00001562 name.GetCString());
1563 }
1564
1565
1566 if (parent_map)
1567 {
1568 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
1569 i != e;
1570 ++i)
1571 {
1572 ClangNamespaceDecl found_namespace_decl;
1573
1574 lldb::ModuleSP module_sp = i->first;
1575 ClangNamespaceDecl module_parent_namespace_decl = i->second;
1576
1577 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
1578
1579 if (!symbol_vendor)
1580 continue;
1581
1582 SymbolContext null_sc;
1583
1584 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
1585
1586 if (!found_namespace_decl)
1587 continue;
1588
1589 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
1590
1591 if (log)
1592 log->Printf(" CMN[%u] Found namespace %s in module %s",
1593 current_id,
1594 name.GetCString(),
1595 module_sp->GetFileSpec().GetFilename().GetCString());
1596 }
1597 }
1598 else
1599 {
Enrico Granata146d9522012-11-08 02:22:02 +00001600 const ModuleList &target_images = m_target->GetImages();
Jim Ingham93367902012-05-30 02:19:25 +00001601 Mutex::Locker modules_locker(target_images.GetMutex());
1602
Sean Callanan73b520f2011-10-29 01:58:46 +00001603 ClangNamespaceDecl null_namespace_decl;
1604
Jim Ingham93367902012-05-30 02:19:25 +00001605 for (uint32_t i = 0, e = target_images.GetSize();
Sean Callanan73b520f2011-10-29 01:58:46 +00001606 i != e;
1607 ++i)
1608 {
Jim Ingham93367902012-05-30 02:19:25 +00001609 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
Sean Callanan73b520f2011-10-29 01:58:46 +00001610
1611 if (!image)
1612 continue;
1613
1614 ClangNamespaceDecl found_namespace_decl;
1615
1616 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
1617
1618 if (!symbol_vendor)
1619 continue;
1620
1621 SymbolContext null_sc;
1622
1623 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
1624
1625 if (!found_namespace_decl)
1626 continue;
1627
1628 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
1629
1630 if (log)
1631 log->Printf(" CMN[%u] Found namespace %s in module %s",
1632 current_id,
1633 name.GetCString(),
1634 image->GetFileSpec().GetFilename().GetCString());
1635 }
1636 }
1637}
1638
Sean Callananbb715f92011-10-29 02:28:18 +00001639NamespaceDecl *
1640ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
1641{
Greg Clayton13d24fb2012-01-29 20:56:30 +00001642 if (!namespace_decls)
Sean Callananbb715f92011-10-29 02:28:18 +00001643 return NULL;
1644
1645 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1646
1647 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
1648
Sean Callanan4938bd62011-11-16 18:20:47 +00001649 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
Sean Callananbb715f92011-10-29 02:28:18 +00001650
Sean Callananddb59332012-03-20 21:11:12 +00001651 if (!copied_decl)
1652 return NULL;
1653
Sean Callananbb715f92011-10-29 02:28:18 +00001654 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1655
1656 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
1657
1658 return dyn_cast<NamespaceDecl>(copied_decl);
1659}
1660
Sean Callanan9394b5a2011-10-29 19:50:43 +00001661void *
1662ClangASTSource::GuardedCopyType (ASTContext *dest_context,
1663 ASTContext *source_context,
1664 void *clang_type)
1665{
1666 SetImportInProgress(true);
1667
Sean Callanan4938bd62011-11-16 18:20:47 +00001668 QualType ret_qual_type = m_ast_importer->CopyType (m_ast_context, source_context, QualType::getFromOpaquePtr(clang_type));
Sean Callanan9394b5a2011-10-29 19:50:43 +00001669
1670 void *ret = ret_qual_type.getAsOpaquePtr();
1671
1672 SetImportInProgress(false);
1673
Sean Callananddb59332012-03-20 21:11:12 +00001674 if (ret && ret_qual_type->getCanonicalTypeInternal().isNull())
Sean Callananaa11af82012-02-27 19:22:39 +00001675 // this shouldn't happen, but we're hardening because the AST importer seems to be generating bad types
1676 // on occasion.
1677 return NULL;
1678
Sean Callanan9394b5a2011-10-29 19:50:43 +00001679 return ret;
1680}
1681
Greg Claytonb01000f2011-01-17 03:46:26 +00001682clang::NamedDecl *
1683NameSearchContext::AddVarDecl(void *type)
1684{
Greg Clayton8de27c72010-10-15 22:48:33 +00001685 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +00001686
1687 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +00001688
Sean Callananf76afff2011-10-28 23:38:38 +00001689 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +00001690 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +00001691 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +00001692 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +00001693 ii,
Chris Lattner24943d22010-06-08 16:52:24 +00001694 QualType::getFromOpaquePtr(type),
1695 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +00001696 SC_Static,
1697 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +00001698 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +00001699
1700 return Decl;
1701}
Sean Callanan8f0dc342010-06-22 23:46:24 +00001702
Greg Claytonb01000f2011-01-17 03:46:26 +00001703clang::NamedDecl *
1704NameSearchContext::AddFunDecl (void *type)
1705{
Sean Callanan30a5dd52012-03-21 17:13:20 +00001706 assert (type && "Type for variable must be non-NULL!");
1707
Sean Callananf76afff2011-10-28 23:38:38 +00001708 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +00001709 const_cast<DeclContext*>(m_decl_context),
1710 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +00001711 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +00001712 m_decl_name.getAsIdentifierInfo(),
1713 QualType::getFromOpaquePtr(type),
1714 NULL,
1715 SC_Static,
1716 SC_Static,
1717 false,
1718 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +00001719
Sean Callananb291abe2010-08-12 23:45:38 +00001720 // We have to do more than just synthesize the FunctionDecl. We have to
1721 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
1722 // this, we raid the function's FunctionProtoType for types.
1723
Greg Clayton8de27c72010-10-15 22:48:33 +00001724 QualType qual_type (QualType::getFromOpaquePtr(type));
Sean Callanan21f2e192011-12-14 01:13:04 +00001725 const FunctionProtoType *func_proto_type = qual_type.getTypePtr()->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +00001726
Greg Clayton8de27c72010-10-15 22:48:33 +00001727 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +00001728 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001729 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +00001730 unsigned ArgIndex;
1731
Sean Callananc1535182011-10-07 23:18:13 +00001732 SmallVector<ParmVarDecl *, 5> parm_var_decls;
1733
Sean Callanan8f0dc342010-06-22 23:46:24 +00001734 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
1735 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001736 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001737
Sean Callananf76afff2011-10-28 23:38:38 +00001738 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callananc1535182011-10-07 23:18:13 +00001739 const_cast<DeclContext*>(m_decl_context),
1740 SourceLocation(),
1741 SourceLocation(),
1742 NULL,
1743 arg_qual_type,
1744 NULL,
1745 SC_Static,
1746 SC_Static,
1747 NULL));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001748 }
1749
Sean Callananc1535182011-10-07 23:18:13 +00001750 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001751 }
Sean Callanandc5fce12011-12-01 21:04:37 +00001752 else
1753 {
1754 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1755
1756 log->Printf("Function type wasn't a FunctionProtoType");
1757 }
Sean Callanan8f0dc342010-06-22 23:46:24 +00001758
Greg Clayton8de27c72010-10-15 22:48:33 +00001759 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +00001760
Greg Clayton8de27c72010-10-15 22:48:33 +00001761 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +00001762}
Sean Callanan0fc73582010-07-27 00:55:47 +00001763
Greg Claytonb01000f2011-01-17 03:46:26 +00001764clang::NamedDecl *
1765NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +00001766{
Sean Callananad293092011-01-18 23:32:05 +00001767 FunctionProtoType::ExtProtoInfo proto_info;
1768
1769 proto_info.Variadic = true;
1770
Sean Callananf76afff2011-10-28 23:38:38 +00001771 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
1772 NULL, // argument types
1773 0, // number of arguments
1774 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +00001775
Sean Callanan0fc73582010-07-27 00:55:47 +00001776 return AddFunDecl(generic_function_type.getAsOpaquePtr());
1777}
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001778
Greg Claytonb01000f2011-01-17 03:46:26 +00001779clang::NamedDecl *
1780NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001781{
Greg Claytona1aaaff2011-01-23 00:34:52 +00001782 if (type)
1783 {
1784 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001785
Sean Callanan21f2e192011-12-14 01:13:04 +00001786 if (const TagType *tag_type = qual_type->getAs<TagType>())
Greg Claytona1aaaff2011-01-23 00:34:52 +00001787 {
1788 TagDecl *tag_decl = tag_type->getDecl();
1789
1790 m_decls.push_back(tag_decl);
1791
1792 return tag_decl;
1793 }
Sean Callanan21f2e192011-12-14 01:13:04 +00001794 else if (const ObjCObjectType *objc_object_type = qual_type->getAs<ObjCObjectType>())
Greg Claytona1aaaff2011-01-23 00:34:52 +00001795 {
1796 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
1797
1798 m_decls.push_back((NamedDecl*)interface_decl);
1799
1800 return (NamedDecl*)interface_decl;
1801 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001802 }
Greg Claytona1aaaff2011-01-23 00:34:52 +00001803 return NULL;
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001804}
Greg Claytone6d72ca2011-06-25 00:44:06 +00001805
1806void
1807NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
1808{
Sean Callananca8fb172012-12-21 21:34:42 +00001809 for (clang::NamedDecl *decl : result)
1810 m_decls.push_back (decl);
Greg Claytone6d72ca2011-06-25 00:44:06 +00001811}
1812
1813void
1814NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
1815{
1816 m_decls.push_back (decl);
1817}