blob: 80a9ea3dd6a0602e79eb6a590b0e130dd4afa5b6 [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
253 ModuleList &module_list = m_target->GetImages();
254
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 Callanan9b6898f2011-07-30 02:42:06 +0000316clang::ExternalLoadResult
Sean Callananbb715f92011-10-29 02:28:18 +0000317ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context,
318 bool (*predicate)(Decl::Kind),
319 llvm::SmallVectorImpl<Decl*> &decls)
320{
321 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
322
323 const Decl *context_decl = dyn_cast<Decl>(decl_context);
324
325 if (!context_decl)
326 return ELR_Failure;
327
328 static unsigned int invocation_id = 0;
329 unsigned int current_id = invocation_id++;
330
331 if (log)
332 {
333 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000334 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000335 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000336 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000337 context_named_decl->getNameAsString().c_str(),
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000338 context_decl->getDeclKindName(),
339 context_decl,
Sean Callananbb715f92011-10-29 02:28:18 +0000340 (predicate ? "non-null" : "null"));
341 else if(context_decl)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000342 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%sDecl*)%p with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000343 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000344 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000345 context_decl->getDeclKindName(),
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000346 context_decl,
Sean Callananbb715f92011-10-29 02:28:18 +0000347 (predicate ? "non-null" : "null"));
348 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000349 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000350 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000351 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000352 (predicate ? "non-null" : "null"));
353 }
354
355 Decl *original_decl = NULL;
356 ASTContext *original_ctx = NULL;
357
358 if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx))
359 return ELR_Failure;
360
361 if (log)
362 {
Sean Callanan6e22e8b2012-01-13 22:05:55 +0000363 log->Printf(" FELD[%u] Original decl (Decl*)%p:", current_id, original_decl);
Sean Callananbb715f92011-10-29 02:28:18 +0000364 ASTDumper(original_decl).ToLog(log, " ");
365 }
366
367 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
368 {
369 ExternalASTSource *external_source = original_ctx->getExternalSource();
370
371 if (external_source)
372 external_source->CompleteType (original_tag_decl);
373 }
374
Sean Callananb2027ec2011-12-08 23:45:45 +0000375 const DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl);
Sean Callananbb715f92011-10-29 02:28:18 +0000376
377 if (!original_decl_context)
378 return ELR_Failure;
379
380 for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
381 iter != original_decl_context->decls_end();
382 ++iter)
383 {
384 Decl *decl = *iter;
385
386 if (!predicate || predicate(decl->getKind()))
387 {
388 if (log)
389 {
390 ASTDumper ast_dumper(decl);
391 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
392 log->Printf(" FELD[%d] Adding [to %s] lexical decl %s", current_id, context_named_decl->getNameAsString().c_str(), ast_dumper.GetCString());
393 else
394 log->Printf(" FELD[%d] Adding lexical decl %s", current_id, ast_dumper.GetCString());
395 }
396
Sean Callanan4938bd62011-11-16 18:20:47 +0000397 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, original_ctx, decl);
Sean Callananbb715f92011-10-29 02:28:18 +0000398
Sean Callananddb59332012-03-20 21:11:12 +0000399 if (!copied_decl)
400 continue;
401
Sean Callanane9478392012-03-15 01:53:17 +0000402 if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl))
403 {
404 QualType copied_field_type = copied_field->getType();
405
406 if (const TagType *copied_field_tag_type = copied_field_type->getAs<TagType>())
407 m_ast_importer->CompleteTagDecl(copied_field_tag_type->getDecl());
408 if (const ObjCObjectType *copied_field_object_type = copied_field_type->getAs<ObjCObjectType>())
409 if (ObjCInterfaceDecl *copied_field_objc_interface_decl = copied_field_object_type->getInterface())
410 m_ast_importer->CompleteObjCInterfaceDecl(copied_field_objc_interface_decl);
411 }
412
Sean Callananbb715f92011-10-29 02:28:18 +0000413 decls.push_back(copied_decl);
414 }
415 }
416
417 return ELR_AlreadyLoaded;
Chris Lattner24943d22010-06-08 16:52:24 +0000418}
419
Sean Callanan9394b5a2011-10-29 19:50:43 +0000420void
421ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
422{
423 assert (m_ast_context);
424
425 const ConstString name(context.m_decl_name.getAsString().c_str());
426
427 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
428
429 static unsigned int invocation_id = 0;
430 unsigned int current_id = invocation_id++;
431
432 if (log)
433 {
434 if (!context.m_decl_context)
Sean Callanan6e22e8b2012-01-13 22:05:55 +0000435 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 +0000436 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000437 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 +0000438 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000439 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 +0000440 }
441
442 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
443
444 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
445 {
446 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
447
448 if (log && log->GetVerbose())
449 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
450 current_id,
451 namespace_map.get(),
452 (int)namespace_map->size());
453
454 if (!namespace_map)
455 return;
456
457 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
458 i != e;
459 ++i)
460 {
461 if (log)
462 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
463 current_id,
464 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
465 i->first->GetFileSpec().GetFilename().GetCString());
466
467 FindExternalVisibleDecls(context,
468 i->first,
469 i->second,
470 current_id);
471 }
472 }
Sean Callanand3812fa2011-11-15 21:50:18 +0000473 else if (isa<ObjCInterfaceDecl>(context.m_decl_context))
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000474 {
Sean Callanan8f2e3922012-02-04 08:49:35 +0000475 FindObjCPropertyAndIvarDecls(context);
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000476 }
Sean Callanan9394b5a2011-10-29 19:50:43 +0000477 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
478 {
479 // we shouldn't be getting FindExternalVisibleDecls calls for these
480 return;
481 }
482 else
483 {
484 ClangNamespaceDecl namespace_decl;
485
486 if (log)
487 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
488
489 FindExternalVisibleDecls(context,
490 lldb::ModuleSP(),
491 namespace_decl,
492 current_id);
493 }
494
495 if (!context.m_namespace_map->empty())
496 {
497 if (log && log->GetVerbose())
498 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
499 current_id,
500 context.m_namespace_map.get(),
501 (int)context.m_namespace_map->size());
502
503 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
504
505 if (clang_namespace_decl)
506 clang_namespace_decl->setHasExternalVisibleStorage();
507 }
508}
509
510void
511ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
512 lldb::ModuleSP module_sp,
513 ClangNamespaceDecl &namespace_decl,
514 unsigned int current_id)
515{
516 assert (m_ast_context);
517
518 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
519
520 SymbolContextList sc_list;
521
522 const ConstString name(context.m_decl_name.getAsString().c_str());
523
524 const char *name_unique_cstr = name.GetCString();
525
Sean Callanan8f2e3922012-02-04 08:49:35 +0000526 static ConstString id_name("id");
527 static ConstString Class_name("Class");
528
529 if (name == id_name || name == Class_name)
530 return;
531
Sean Callanan9394b5a2011-10-29 19:50:43 +0000532 if (name_unique_cstr == NULL)
533 return;
534
535 // The ClangASTSource is not responsible for finding $-names.
536 if (name_unique_cstr[0] == '$')
537 return;
538
539 if (module_sp && namespace_decl)
540 {
541 ClangNamespaceDecl found_namespace_decl;
542
543 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
544
545 if (symbol_vendor)
546 {
547 SymbolContext null_sc;
548
549 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
550
551 if (found_namespace_decl)
552 {
553 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
554
555 if (log)
556 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
557 current_id,
558 name.GetCString(),
559 module_sp->GetFileSpec().GetFilename().GetCString());
560 }
561 }
562 }
563 else
564 {
Jim Ingham93367902012-05-30 02:19:25 +0000565 ModuleList &target_images = m_target->GetImages();
566 Mutex::Locker modules_locker (target_images.GetMutex());
Sean Callanan9394b5a2011-10-29 19:50:43 +0000567
Jim Ingham93367902012-05-30 02:19:25 +0000568 for (uint32_t i = 0, e = target_images.GetSize();
Sean Callanan9394b5a2011-10-29 19:50:43 +0000569 i != e;
570 ++i)
571 {
Jim Ingham93367902012-05-30 02:19:25 +0000572 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
Sean Callanan9394b5a2011-10-29 19:50:43 +0000573
574 if (!image)
575 continue;
576
577 ClangNamespaceDecl found_namespace_decl;
578
579 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
580
581 if (!symbol_vendor)
582 continue;
583
584 SymbolContext null_sc;
585
586 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
587
588 if (found_namespace_decl)
589 {
590 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
591
592 if (log)
593 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
594 current_id,
595 name.GetCString(),
596 image->GetFileSpec().GetFilename().GetCString());
597 }
598 }
599 }
600
Sean Callanan9394b5a2011-10-29 19:50:43 +0000601 do
602 {
603 TypeList types;
604 SymbolContext null_sc;
Greg Claytondc0a38c2012-03-26 23:03:23 +0000605 const bool exact_match = false;
Sean Callanan0f71d192011-12-19 19:38:39 +0000606
Sean Callanan9394b5a2011-10-29 19:50:43 +0000607 if (module_sp && namespace_decl)
Greg Claytondc0a38c2012-03-26 23:03:23 +0000608 module_sp->FindTypesInNamespace(null_sc, name, &namespace_decl, 1, types);
Sean Callanan0f71d192011-12-19 19:38:39 +0000609 else
Greg Clayton9f95fb62012-04-06 17:41:13 +0000610 m_target->GetImages().FindTypes(null_sc, name, exact_match, 1, types);
Sean Callanan9394b5a2011-10-29 19:50:43 +0000611
612 if (types.GetSize())
613 {
614 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
615
616 if (log)
617 {
618 const char *name_string = type_sp->GetName().GetCString();
619
620 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
621 current_id,
622 name.GetCString(),
623 (name_string ? name_string : "<anonymous>"));
624 }
Sean Callananc718b962012-09-11 21:44:01 +0000625
626 clang::ASTContext *type_ast = type_sp->GetClangAST();
627 lldb::clang_type_t full_type = type_sp->GetClangFullType();
Sean Callanane1301a62011-12-06 03:41:14 +0000628
Sean Callananc718b962012-09-11 21:44:01 +0000629 void *copied_type = GuardedCopyType(m_ast_context, type_ast, full_type);
Sean Callanane1301a62011-12-06 03:41:14 +0000630
Sean Callanandc5fce12011-12-01 21:04:37 +0000631 if (!copied_type)
632 {
633 if (log)
Sean Callanane1301a62011-12-06 03:41:14 +0000634 log->Printf(" CAS::FEVD[%u] - Couldn't export the type for a constant integer result",
635 current_id);
636
Sean Callanandc5fce12011-12-01 21:04:37 +0000637 break;
638 }
639
Sean Callanan9394b5a2011-10-29 19:50:43 +0000640 context.AddTypeDecl(copied_type);
641 }
Sean Callanan673f3db2011-11-30 22:11:59 +0000642
Sean Callanan9394b5a2011-10-29 19:50:43 +0000643 } while(0);
644}
645
Sean Callananc718b962012-09-11 21:44:01 +0000646static void
647FindObjCMethodDeclsWithOrigin (unsigned int current_id,
648 NameSearchContext &context,
649 ObjCInterfaceDecl *original_interface_decl,
650 clang::ASTContext *ast_context,
651 ClangASTImporter *ast_importer,
652 const char *log_info)
653{
654 const DeclarationName &decl_name(context.m_decl_name);
655 clang::ASTContext *original_ctx = &original_interface_decl->getASTContext();
656
657 Selector original_selector;
658
659 if (decl_name.isObjCZeroArgSelector())
660 {
661 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name.getAsString());
662 original_selector = original_ctx->Selectors.getSelector(0, &ident);
663 }
664 else if (decl_name.isObjCOneArgSelector())
665 {
666 const std::string &decl_name_string = decl_name.getAsString();
667 std::string decl_name_string_without_colon(decl_name_string.c_str(), decl_name_string.length() - 1);
668 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name_string_without_colon.c_str());
669 original_selector = original_ctx->Selectors.getSelector(1, &ident);
670 }
671 else
672 {
673 SmallVector<IdentifierInfo *, 4> idents;
674
675 clang::Selector sel = decl_name.getObjCSelector();
676
677 int num_args = sel.getNumArgs();
678
679 for (unsigned i = 0;
680 i != num_args;
681 ++i)
682 {
683 idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i)));
684 }
685
686 original_selector = original_ctx->Selectors.getSelector(num_args, idents.data());
687 }
688
689 DeclarationName original_decl_name(original_selector);
690
691 ObjCInterfaceDecl::lookup_result result = original_interface_decl->lookup(original_decl_name);
692
693 if (result.first == result.second)
694 return;
695
696 if (!*result.first)
697 return;
698
699 ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(*result.first);
700
701 if (!result_method)
702 return;
703
704 Decl *copied_decl = ast_importer->CopyDecl(ast_context, &result_method->getASTContext(), result_method);
705
706 if (!copied_decl)
707 return;
708
709 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
710
711 if (!copied_method_decl)
712 return;
713
714 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
715
716 if (log)
717 {
718 ASTDumper dumper((Decl*)copied_method_decl);
719 log->Printf(" CAS::FOMD[%d] found (%s) %s", current_id, log_info, dumper.GetCString());
720 }
721
722 context.AddNamedDecl(copied_method_decl);
723
724 return;
725}
726
Sean Callanan9b714842011-11-09 19:33:21 +0000727void
728ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
729{
730 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
731
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000732 static unsigned int invocation_id = 0;
733 unsigned int current_id = invocation_id++;
734
Sean Callanan9b714842011-11-09 19:33:21 +0000735 const DeclarationName &decl_name(context.m_decl_name);
736 const DeclContext *decl_ctx(context.m_decl_context);
737
738 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
739
740 if (!interface_decl)
741 return;
742
Sean Callanan8d611562012-04-05 00:12:52 +0000743 do
744 {
745 Decl *original_decl = NULL;
746 ASTContext *original_ctx = NULL;
747
748 m_ast_importer->ResolveDeclOrigin(interface_decl, &original_decl, &original_ctx);
749
750 if (!original_decl)
751 break;
752
753 ObjCInterfaceDecl *original_interface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl);
754
Sean Callananc718b962012-09-11 21:44:01 +0000755 FindObjCMethodDeclsWithOrigin(current_id,
756 context,
757 original_interface_decl,
758 m_ast_context,
759 m_ast_importer,
760 "in debug info");
Sean Callanan8d611562012-04-05 00:12:52 +0000761 } while (0);
762
Sean Callanan9b714842011-11-09 19:33:21 +0000763 StreamString ss;
Sean Callanane1301a62011-12-06 03:41:14 +0000764
Sean Callanan9b714842011-11-09 19:33:21 +0000765 if (decl_name.isObjCZeroArgSelector())
766 {
767 ss.Printf("%s", decl_name.getAsString().c_str());
768 }
769 else if (decl_name.isObjCOneArgSelector())
770 {
Sean Callanan1d9ffe22011-11-14 18:29:46 +0000771 ss.Printf("%s", decl_name.getAsString().c_str());
Sean Callanan9b714842011-11-09 19:33:21 +0000772 }
773 else
774 {
775 clang::Selector sel = decl_name.getObjCSelector();
776
777 for (unsigned i = 0, e = sel.getNumArgs();
778 i != e;
779 ++i)
780 {
781 llvm::StringRef r = sel.getNameForSlot(i);
782 ss.Printf("%s:", r.str().c_str());
783 }
784 }
785 ss.Flush();
786
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000787 ConstString selector_name(ss.GetData());
788
Sean Callanan9b714842011-11-09 19:33:21 +0000789 if (log)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000790 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
791 current_id,
792 m_ast_context,
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000793 interface_decl->getNameAsString().c_str(),
794 selector_name.AsCString());
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000795 SymbolContextList sc_list;
796
797 const bool include_symbols = false;
Sean Callanan302d78c2012-02-10 22:52:19 +0000798 const bool include_inlines = false;
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000799 const bool append = false;
800
Sean Callanane0028b82012-01-19 02:17:40 +0000801 std::string interface_name = interface_decl->getNameAsString();
802
803 do
804 {
805 StreamString ms;
806 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());
807 ms.Flush();
808 ConstString instance_method_name(ms.GetData());
809
Sean Callanan302d78c2012-02-10 22:52:19 +0000810 m_target->GetImages().FindFunctions(instance_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanane0028b82012-01-19 02:17:40 +0000811
812 if (sc_list.GetSize())
813 break;
814
815 ms.Clear();
816 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());
817 ms.Flush();
818 ConstString class_method_name(ms.GetData());
819
Sean Callanan302d78c2012-02-10 22:52:19 +0000820 m_target->GetImages().FindFunctions(class_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanane0028b82012-01-19 02:17:40 +0000821
822 if (sc_list.GetSize())
823 break;
824
825 // Fall back and check for methods in categories. If we find methods this way, we need to check that they're actually in
826 // categories on the desired class.
827
828 SymbolContextList candidate_sc_list;
829
Sean Callanan302d78c2012-02-10 22:52:19 +0000830 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, include_inlines, append, candidate_sc_list);
Sean Callanane0028b82012-01-19 02:17:40 +0000831
832 for (uint32_t ci = 0, ce = candidate_sc_list.GetSize();
833 ci != ce;
834 ++ci)
835 {
836 SymbolContext candidate_sc;
837
838 if (!candidate_sc_list.GetContextAtIndex(ci, candidate_sc))
839 continue;
840
841 if (!candidate_sc.function)
842 continue;
843
844 const char *candidate_name = candidate_sc.function->GetName().AsCString();
845
846 const char *cursor = candidate_name;
847
848 if (*cursor != '+' && *cursor != '-')
849 continue;
850
851 ++cursor;
852
853 if (*cursor != '[')
854 continue;
855
856 ++cursor;
857
858 size_t interface_len = interface_name.length();
859
860 if (strncmp(cursor, interface_name.c_str(), interface_len))
861 continue;
862
863 cursor += interface_len;
864
865 if (*cursor == ' ' || *cursor == '(')
866 sc_list.Append(candidate_sc);
867 }
868 }
869 while (0);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000870
Sean Callananc718b962012-09-11 21:44:01 +0000871 if (sc_list.GetSize())
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000872 {
Sean Callananc718b962012-09-11 21:44:01 +0000873 // We found a good function symbol. Use that.
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000874
Sean Callananc718b962012-09-11 21:44:01 +0000875 for (uint32_t i = 0, e = sc_list.GetSize();
876 i != e;
877 ++i)
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000878 {
Sean Callananc718b962012-09-11 21:44:01 +0000879 SymbolContext sc;
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000880
Sean Callananc718b962012-09-11 21:44:01 +0000881 if (!sc_list.GetContextAtIndex(i, sc))
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000882 continue;
883
Sean Callananc718b962012-09-11 21:44:01 +0000884 if (!sc.function)
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000885 continue;
886
Sean Callananc718b962012-09-11 21:44:01 +0000887 DeclContext *function_ctx = sc.function->GetClangDeclContext();
888
889 if (!function_ctx)
890 continue;
891
892 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
893
894 if (!method_decl)
895 continue;
896
897 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
898
899 if (!found_interface_decl)
900 continue;
901
902 if (found_interface_decl->getName() == interface_decl->getName())
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000903 {
Sean Callananc718b962012-09-11 21:44:01 +0000904 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
905
906 if (!copied_decl)
907 continue;
908
909 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
910
911 if (!copied_method_decl)
912 continue;
913
914 if (log)
915 {
916 ASTDumper dumper((Decl*)copied_method_decl);
917 log->Printf(" CAS::FOMD[%d] found (in debug info) %s", current_id, dumper.GetCString());
918 }
919
920 context.AddNamedDecl(copied_method_decl);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000921 }
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000922 }
923 }
Sean Callananc718b962012-09-11 21:44:01 +0000924 else
925 {
926 do
927 {
928 // We need to look at the runtime.
929
930 lldb::ProcessSP process(m_target->GetProcessSP());
931
932 if (!process)
933 break;
934
935 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
936
937 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
938
939 if (!type_vendor)
940 break;
941
942 ConstString interface_name(interface_decl->getNameAsString().c_str());
943 bool append = false;
944 uint32_t max_matches = 1;
945 std::vector <ClangASTType> types;
946
947 if (!type_vendor->FindTypes(interface_name,
948 append,
949 max_matches,
950 types))
951 break;
952
953 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
954
955 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
956
957 if (!runtime_interface_type)
958 break;
959
960 ObjCInterfaceDecl *runtime_interface_decl = runtime_interface_type->getDecl();
961
962 FindObjCMethodDeclsWithOrigin(current_id,
963 context,
964 runtime_interface_decl,
965 m_ast_context,
966 m_ast_importer,
967 "in runtime");
968 }
969 while(0);
970 }
Sean Callanan9b714842011-11-09 19:33:21 +0000971}
972
Sean Callanan8f2e3922012-02-04 08:49:35 +0000973template <class D> class TaggedASTDecl {
974public:
975 TaggedASTDecl() : decl(NULL) { }
976 TaggedASTDecl(D *_decl) : decl(_decl) { }
977 bool IsValid() const { return (decl != NULL); }
978 bool IsInvalid() const { return !IsValid(); }
979 D *operator->() const { return decl; }
980 D *decl;
981};
982
983template <class D2, template <class D> class TD, class D1>
984TD<D2>
985DynCast(TD<D1> source)
986{
987 return TD<D2> (dyn_cast<D2>(source.decl));
988}
989
990template <class D = Decl> class DeclFromParser;
991template <class D = Decl> class DeclFromUser;
992
993template <class D> class DeclFromParser : public TaggedASTDecl<D> {
994public:
995 DeclFromParser() : TaggedASTDecl<D>() { }
996 DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) { }
997
998 DeclFromUser<D> GetOrigin(ClangASTImporter *importer);
999};
1000
1001template <class D> class DeclFromUser : public TaggedASTDecl<D> {
1002public:
1003 DeclFromUser() : TaggedASTDecl<D>() { }
1004 DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) { }
1005
1006 DeclFromParser<D> Import(ClangASTImporter *importer, ASTContext &dest_ctx);
1007};
1008
1009template <class D>
1010DeclFromUser<D>
1011DeclFromParser<D>::GetOrigin(ClangASTImporter *importer)
1012{
1013 DeclFromUser <> origin_decl;
1014 importer->ResolveDeclOrigin(this->decl, &origin_decl.decl, NULL);
1015 if (origin_decl.IsInvalid())
1016 return DeclFromUser<D>();
1017 return DeclFromUser<D>(dyn_cast<D>(origin_decl.decl));
1018}
1019
1020template <class D>
1021DeclFromParser<D>
1022DeclFromUser<D>::Import(ClangASTImporter *importer, ASTContext &dest_ctx)
1023{
1024 DeclFromParser <> parser_generic_decl(importer->CopyDecl(&dest_ctx, &this->decl->getASTContext(), this->decl));
1025 if (parser_generic_decl.IsInvalid())
1026 return DeclFromParser<D>();
1027 return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
1028}
1029
Sean Callanan931acec2012-02-22 23:57:45 +00001030static bool
1031FindObjCPropertyAndIvarDeclsWithOrigin (unsigned int current_id,
1032 NameSearchContext &context,
1033 clang::ASTContext &ast_context,
1034 ClangASTImporter *ast_importer,
1035 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl)
1036{
1037 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1038
1039 if (origin_iface_decl.IsInvalid())
1040 return false;
1041
1042 std::string name_str = context.m_decl_name.getAsString();
1043 StringRef name(name_str.c_str());
1044 IdentifierInfo &name_identifier(origin_iface_decl->getASTContext().Idents.get(name));
1045
1046 DeclFromUser<ObjCPropertyDecl> origin_property_decl(origin_iface_decl->FindPropertyDeclaration(&name_identifier));
1047
1048 bool found = false;
1049
1050 if (origin_property_decl.IsValid())
1051 {
1052 DeclFromParser<ObjCPropertyDecl> parser_property_decl(origin_property_decl.Import(ast_importer, ast_context));
1053 if (parser_property_decl.IsValid())
1054 {
1055 if (log)
1056 {
1057 ASTDumper dumper((Decl*)parser_property_decl.decl);
1058 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1059 }
1060
1061 context.AddNamedDecl(parser_property_decl.decl);
1062 found = true;
1063 }
1064 }
1065
1066 DeclFromUser<ObjCIvarDecl> origin_ivar_decl(origin_iface_decl->getIvarDecl(&name_identifier));
1067
1068 if (origin_ivar_decl.IsValid())
1069 {
1070 DeclFromParser<ObjCIvarDecl> parser_ivar_decl(origin_ivar_decl.Import(ast_importer, ast_context));
1071 if (parser_ivar_decl.IsValid())
1072 {
1073 if (log)
1074 {
1075 ASTDumper dumper((Decl*)parser_ivar_decl.decl);
1076 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1077 }
1078
1079 context.AddNamedDecl(parser_ivar_decl.decl);
1080 found = true;
1081 }
1082 }
1083
1084 return found;
1085}
1086
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001087void
Sean Callanan8f2e3922012-02-04 08:49:35 +00001088ClangASTSource::FindObjCPropertyAndIvarDecls (NameSearchContext &context)
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001089{
1090 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1091
1092 static unsigned int invocation_id = 0;
1093 unsigned int current_id = invocation_id++;
1094
Sean Callanan8f2e3922012-02-04 08:49:35 +00001095 DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl(cast<ObjCInterfaceDecl>(context.m_decl_context));
1096 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(parser_iface_decl.GetOrigin(m_ast_importer));
Sean Callanan931acec2012-02-22 23:57:45 +00001097
1098 ConstString class_name(parser_iface_decl->getNameAsString().c_str());
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001099
1100 if (log)
Sean Callanan8f2e3922012-02-04 08:49:35 +00001101 log->Printf("ClangASTSource::FindObjCPropertyAndIvarDecls[%d] on (ASTContext*)%p for '%s.%s'",
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001102 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001103 m_ast_context,
Sean Callanan8f2e3922012-02-04 08:49:35 +00001104 parser_iface_decl->getNameAsString().c_str(),
Sean Callanan931acec2012-02-22 23:57:45 +00001105 context.m_decl_name.getAsString().c_str());
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001106
Sean Callanan931acec2012-02-22 23:57:45 +00001107 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1108 context,
1109 *m_ast_context,
1110 m_ast_importer,
1111 origin_iface_decl))
1112 return;
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001113
Sean Callanan931acec2012-02-22 23:57:45 +00001114 if (log)
1115 log->Printf("CAS::FOPD[%d] couldn't find the property on origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p, searching elsewhere...",
1116 current_id,
1117 origin_iface_decl.decl,
1118 &origin_iface_decl->getASTContext());
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001119
Sean Callanan931acec2012-02-22 23:57:45 +00001120 SymbolContext null_sc;
1121 TypeList type_list;
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001122
Sean Callanan931acec2012-02-22 23:57:45 +00001123 lldb::ProcessSP process(m_target->GetProcessSP());
1124
1125 if (!process)
1126 return;
1127
1128 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1129
1130 if (!language_runtime)
1131 return;
1132
Sean Callananc718b962012-09-11 21:44:01 +00001133 do
1134 {
1135 // First see if any other debug information has this property/ivar.
1136
1137 lldb::TypeSP complete_type_sp(language_runtime->LookupInCompleteClassCache(class_name));
1138
1139 if (!complete_type_sp)
1140 break;
1141
1142 TypeFromUser complete_type = TypeFromUser(complete_type_sp->GetClangFullType(), complete_type_sp->GetClangAST());
1143 lldb::clang_type_t complete_opaque_type = complete_type.GetOpaqueQualType();
1144
1145 if (!complete_opaque_type)
1146 break;
1147
1148 const clang::Type *complete_clang_type = QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr();
1149 const ObjCInterfaceType *complete_interface_type = dyn_cast<ObjCInterfaceType>(complete_clang_type);
1150
1151 if (!complete_interface_type)
1152 break;
1153
1154 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_type->getDecl());
1155
1156 if (complete_iface_decl.decl == origin_iface_decl.decl)
1157 break; // already checked this one
1158
1159 if (log)
1160 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1161 current_id,
1162 complete_iface_decl.decl,
1163 &complete_iface_decl->getASTContext());
1164
1165 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1166 context,
1167 *m_ast_context,
1168 m_ast_importer,
1169 complete_iface_decl))
1170 return;
1171 }
1172 while(0);
Sean Callanan931acec2012-02-22 23:57:45 +00001173
Sean Callananc718b962012-09-11 21:44:01 +00001174 do
1175 {
1176 // Now check the runtime.
1177
1178 TypeVendor *type_vendor = language_runtime->GetTypeVendor();
1179
1180 if (!type_vendor)
1181 break;
1182
1183 bool append = false;
1184 uint32_t max_matches = 1;
1185 std::vector <ClangASTType> types;
1186
1187 if (!type_vendor->FindTypes(class_name,
1188 append,
1189 max_matches,
1190 types))
1191 break;
1192
1193 const clang::Type *runtime_clang_type = QualType::getFromOpaquePtr(types[0].GetOpaqueQualType()).getTypePtr();
1194
1195 const ObjCInterfaceType *runtime_interface_type = dyn_cast<ObjCInterfaceType>(runtime_clang_type);
1196
1197 if (!runtime_interface_type)
1198 break;
1199
1200 DeclFromUser<const ObjCInterfaceDecl> runtime_iface_decl(runtime_interface_type->getDecl());
1201
1202 if (log)
1203 log->Printf("CAS::FOPD[%d] trying runtime (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1204 current_id,
1205 runtime_iface_decl.decl,
1206 &runtime_iface_decl->getASTContext());
1207
1208 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1209 context,
1210 *m_ast_context,
1211 m_ast_importer,
1212 runtime_iface_decl))
1213 return;
1214 }
1215 while(0);
Sean Callanan8f2e3922012-02-04 08:49:35 +00001216}
1217
1218typedef llvm::DenseMap <const FieldDecl *, uint64_t> FieldOffsetMap;
1219typedef llvm::DenseMap <const CXXRecordDecl *, CharUnits> BaseOffsetMap;
1220
1221template <class D, class O>
1222static bool
1223ImportOffsetMap (llvm::DenseMap <const D*, O> &destination_map,
1224 llvm::DenseMap <const D*, O> &source_map,
1225 ClangASTImporter *importer,
1226 ASTContext &dest_ctx)
1227{
1228 typedef llvm::DenseMap <const D*, O> MapType;
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001229
Sean Callanan8f2e3922012-02-04 08:49:35 +00001230 for (typename MapType::iterator fi = source_map.begin(), fe = source_map.end();
1231 fi != fe;
1232 ++fi)
1233 {
1234 DeclFromUser <D> user_decl(const_cast<D*>(fi->first));
1235 DeclFromParser <D> parser_decl(user_decl.Import(importer, dest_ctx));
1236 if (parser_decl.IsInvalid())
1237 return false;
1238 destination_map.insert(std::pair<const D *, O>(parser_decl.decl, fi->second));
1239 }
1240
1241 return true;
1242}
1243
1244template <bool IsVirtual> bool ExtractBaseOffsets (const ASTRecordLayout &record_layout,
1245 DeclFromUser<const CXXRecordDecl> &record,
1246 BaseOffsetMap &base_offsets)
1247{
1248 for (CXXRecordDecl::base_class_const_iterator
1249 bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()),
1250 be = (IsVirtual ? record->vbases_end() : record->bases_end());
1251 bi != be;
1252 ++bi)
1253 {
1254 if (!IsVirtual && bi->isVirtual())
1255 continue;
1256
1257 const clang::Type *origin_base_type = bi->getType().getTypePtr();
1258 const clang::RecordType *origin_base_record_type = origin_base_type->getAs<RecordType>();
1259
1260 if (!origin_base_record_type)
1261 return false;
1262
1263 DeclFromUser <RecordDecl> origin_base_record(origin_base_record_type->getDecl());
1264
1265 if (origin_base_record.IsInvalid())
1266 return false;
1267
1268 DeclFromUser <CXXRecordDecl> origin_base_cxx_record(DynCast<CXXRecordDecl>(origin_base_record));
1269
1270 if (origin_base_cxx_record.IsInvalid())
1271 return false;
1272
1273 CharUnits base_offset;
1274
1275 if (IsVirtual)
1276 base_offset = record_layout.getVBaseClassOffset(origin_base_cxx_record.decl);
1277 else
1278 base_offset = record_layout.getBaseClassOffset(origin_base_cxx_record.decl);
1279
1280 base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(origin_base_cxx_record.decl, base_offset));
1281 }
1282
1283 return true;
1284}
1285
1286bool
1287ClangASTSource::layoutRecordType(const RecordDecl *record,
1288 uint64_t &size,
1289 uint64_t &alignment,
1290 FieldOffsetMap &field_offsets,
1291 BaseOffsetMap &base_offsets,
1292 BaseOffsetMap &virtual_base_offsets)
1293{
1294 static unsigned int invocation_id = 0;
1295 unsigned int current_id = invocation_id++;
1296
1297 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1298
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001299 if (log)
1300 {
Sean Callanan8f2e3922012-02-04 08:49:35 +00001301 log->Printf("LayoutRecordType[%u] on (RecordDecl*)%p [name = '%s']",
1302 current_id,
1303 m_ast_context,
1304 record->getNameAsString().c_str());
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001305 }
Sean Callanan8f2e3922012-02-04 08:49:35 +00001306
1307
1308 DeclFromParser <const RecordDecl> parser_record(record);
1309 DeclFromUser <const RecordDecl> origin_record(parser_record.GetOrigin(m_ast_importer));
1310
1311 if (origin_record.IsInvalid())
1312 return false;
1313
1314 FieldOffsetMap origin_field_offsets;
1315 BaseOffsetMap origin_base_offsets;
1316 BaseOffsetMap origin_virtual_base_offsets;
1317
Sean Callanan33bff9a2012-04-07 00:06:00 +00001318 ClangASTContext::GetCompleteDecl(&origin_record->getASTContext(), const_cast<RecordDecl*>(origin_record.decl));
1319
1320 if (!origin_record.decl->getDefinition())
1321 return false;
1322
Sean Callanan8f2e3922012-02-04 08:49:35 +00001323 const ASTRecordLayout &record_layout(origin_record->getASTContext().getASTRecordLayout(origin_record.decl));
1324
1325 int field_idx = 0;
1326
1327 for (RecordDecl::field_iterator fi = origin_record->field_begin(), fe = origin_record->field_end();
1328 fi != fe;
1329 ++fi)
1330 {
1331 uint64_t field_offset = record_layout.getFieldOffset(field_idx);
1332
1333 origin_field_offsets.insert(std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
1334
1335 field_idx++;
1336 }
1337
1338 ASTContext &parser_ast_context(record->getASTContext());
1339
1340 DeclFromUser <const CXXRecordDecl> origin_cxx_record(DynCast<const CXXRecordDecl>(origin_record));
1341
1342 if (origin_cxx_record.IsValid())
1343 {
1344 if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record, origin_base_offsets) ||
1345 !ExtractBaseOffsets<true>(record_layout, origin_cxx_record, origin_virtual_base_offsets))
1346 return false;
1347 }
1348
1349 if (!ImportOffsetMap(field_offsets, origin_field_offsets, m_ast_importer, parser_ast_context) ||
1350 !ImportOffsetMap(base_offsets, origin_base_offsets, m_ast_importer, parser_ast_context) ||
1351 !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets, m_ast_importer, parser_ast_context))
1352 return false;
1353
1354 size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth();
1355 alignment = record_layout.getAlignment().getQuantity() * m_ast_context->getCharWidth();
1356
1357 if (log)
1358 {
1359 log->Printf("LRT[%u] returned:", current_id);
1360 log->Printf("LRT[%u] Original = (RecordDecl*)%p", current_id, origin_record.decl);
1361 log->Printf("LRT[%u] Size = %lld", current_id, size);
1362 log->Printf("LRT[%u] Alignment = %lld", current_id, alignment);
1363 log->Printf("LRT[%u] Fields:", current_id);
1364 for (RecordDecl::field_iterator fi = record->field_begin(), fe = record->field_end();
1365 fi != fe;
1366 ++fi)
1367 {
1368 log->Printf("LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %lld bits",
1369 current_id,
1370 *fi,
1371 fi->getNameAsString().c_str(),
1372 field_offsets[*fi]);
1373 }
1374 DeclFromParser <const CXXRecordDecl> parser_cxx_record = DynCast<const CXXRecordDecl>(parser_record);
1375 if (parser_cxx_record.IsValid())
1376 {
1377 log->Printf("LRT[%u] Bases:", current_id);
1378 for (CXXRecordDecl::base_class_const_iterator bi = parser_cxx_record->bases_begin(), be = parser_cxx_record->bases_end();
1379 bi != be;
1380 ++bi)
1381 {
1382 bool is_virtual = bi->isVirtual();
1383
1384 QualType base_type = bi->getType();
1385 const RecordType *base_record_type = base_type->getAs<RecordType>();
1386 DeclFromParser <RecordDecl> base_record(base_record_type->getDecl());
1387 DeclFromParser <CXXRecordDecl> base_cxx_record = DynCast<CXXRecordDecl>(base_record);
1388
1389 log->Printf("LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %lld chars",
1390 current_id,
1391 (is_virtual ? "Virtual " : ""),
1392 base_cxx_record.decl,
1393 base_cxx_record.decl->getNameAsString().c_str(),
1394 (is_virtual ? virtual_base_offsets[base_cxx_record.decl].getQuantity() :
1395 base_offsets[base_cxx_record.decl].getQuantity()));
1396 }
1397 }
1398 else
1399 {
1400 log->Printf("LRD[%u] Not a CXXRecord, so no bases", current_id);
1401 }
1402 }
1403
1404 return true;
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001405}
1406
Sean Callanan73b520f2011-10-29 01:58:46 +00001407void
1408ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
1409 const ConstString &name,
1410 ClangASTImporter::NamespaceMapSP &parent_map) const
1411{
1412 static unsigned int invocation_id = 0;
1413 unsigned int current_id = invocation_id++;
1414
1415 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1416
1417 if (log)
1418 {
1419 if (parent_map && parent_map->size())
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001420 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +00001421 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001422 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +00001423 name.GetCString(),
1424 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
1425 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001426 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +00001427 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001428 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +00001429 name.GetCString());
1430 }
1431
1432
1433 if (parent_map)
1434 {
1435 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
1436 i != e;
1437 ++i)
1438 {
1439 ClangNamespaceDecl found_namespace_decl;
1440
1441 lldb::ModuleSP module_sp = i->first;
1442 ClangNamespaceDecl module_parent_namespace_decl = i->second;
1443
1444 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
1445
1446 if (!symbol_vendor)
1447 continue;
1448
1449 SymbolContext null_sc;
1450
1451 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
1452
1453 if (!found_namespace_decl)
1454 continue;
1455
1456 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
1457
1458 if (log)
1459 log->Printf(" CMN[%u] Found namespace %s in module %s",
1460 current_id,
1461 name.GetCString(),
1462 module_sp->GetFileSpec().GetFilename().GetCString());
1463 }
1464 }
1465 else
1466 {
Jim Ingham93367902012-05-30 02:19:25 +00001467 ModuleList &target_images = m_target->GetImages();
1468 Mutex::Locker modules_locker(target_images.GetMutex());
1469
Sean Callanan73b520f2011-10-29 01:58:46 +00001470 ClangNamespaceDecl null_namespace_decl;
1471
Jim Ingham93367902012-05-30 02:19:25 +00001472 for (uint32_t i = 0, e = target_images.GetSize();
Sean Callanan73b520f2011-10-29 01:58:46 +00001473 i != e;
1474 ++i)
1475 {
Jim Ingham93367902012-05-30 02:19:25 +00001476 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
Sean Callanan73b520f2011-10-29 01:58:46 +00001477
1478 if (!image)
1479 continue;
1480
1481 ClangNamespaceDecl found_namespace_decl;
1482
1483 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
1484
1485 if (!symbol_vendor)
1486 continue;
1487
1488 SymbolContext null_sc;
1489
1490 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
1491
1492 if (!found_namespace_decl)
1493 continue;
1494
1495 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
1496
1497 if (log)
1498 log->Printf(" CMN[%u] Found namespace %s in module %s",
1499 current_id,
1500 name.GetCString(),
1501 image->GetFileSpec().GetFilename().GetCString());
1502 }
1503 }
1504}
1505
Sean Callananbb715f92011-10-29 02:28:18 +00001506NamespaceDecl *
1507ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
1508{
Greg Clayton13d24fb2012-01-29 20:56:30 +00001509 if (!namespace_decls)
Sean Callananbb715f92011-10-29 02:28:18 +00001510 return NULL;
1511
1512 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1513
1514 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
1515
Sean Callanan4938bd62011-11-16 18:20:47 +00001516 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
Sean Callananbb715f92011-10-29 02:28:18 +00001517
Sean Callananddb59332012-03-20 21:11:12 +00001518 if (!copied_decl)
1519 return NULL;
1520
Sean Callananbb715f92011-10-29 02:28:18 +00001521 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1522
1523 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
1524
1525 return dyn_cast<NamespaceDecl>(copied_decl);
1526}
1527
Sean Callanan9394b5a2011-10-29 19:50:43 +00001528void *
1529ClangASTSource::GuardedCopyType (ASTContext *dest_context,
1530 ASTContext *source_context,
1531 void *clang_type)
1532{
1533 SetImportInProgress(true);
1534
Sean Callanan4938bd62011-11-16 18:20:47 +00001535 QualType ret_qual_type = m_ast_importer->CopyType (m_ast_context, source_context, QualType::getFromOpaquePtr(clang_type));
Sean Callanan9394b5a2011-10-29 19:50:43 +00001536
1537 void *ret = ret_qual_type.getAsOpaquePtr();
1538
1539 SetImportInProgress(false);
1540
Sean Callananddb59332012-03-20 21:11:12 +00001541 if (ret && ret_qual_type->getCanonicalTypeInternal().isNull())
Sean Callananaa11af82012-02-27 19:22:39 +00001542 // this shouldn't happen, but we're hardening because the AST importer seems to be generating bad types
1543 // on occasion.
1544 return NULL;
1545
Sean Callanan9394b5a2011-10-29 19:50:43 +00001546 return ret;
1547}
1548
Greg Claytonb01000f2011-01-17 03:46:26 +00001549clang::NamedDecl *
1550NameSearchContext::AddVarDecl(void *type)
1551{
Greg Clayton8de27c72010-10-15 22:48:33 +00001552 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +00001553
1554 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +00001555
Sean Callananf76afff2011-10-28 23:38:38 +00001556 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +00001557 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +00001558 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +00001559 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +00001560 ii,
Chris Lattner24943d22010-06-08 16:52:24 +00001561 QualType::getFromOpaquePtr(type),
1562 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +00001563 SC_Static,
1564 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +00001565 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +00001566
1567 return Decl;
1568}
Sean Callanan8f0dc342010-06-22 23:46:24 +00001569
Greg Claytonb01000f2011-01-17 03:46:26 +00001570clang::NamedDecl *
1571NameSearchContext::AddFunDecl (void *type)
1572{
Sean Callanan30a5dd52012-03-21 17:13:20 +00001573 assert (type && "Type for variable must be non-NULL!");
1574
Sean Callananf76afff2011-10-28 23:38:38 +00001575 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +00001576 const_cast<DeclContext*>(m_decl_context),
1577 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +00001578 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +00001579 m_decl_name.getAsIdentifierInfo(),
1580 QualType::getFromOpaquePtr(type),
1581 NULL,
1582 SC_Static,
1583 SC_Static,
1584 false,
1585 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +00001586
Sean Callananb291abe2010-08-12 23:45:38 +00001587 // We have to do more than just synthesize the FunctionDecl. We have to
1588 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
1589 // this, we raid the function's FunctionProtoType for types.
1590
Greg Clayton8de27c72010-10-15 22:48:33 +00001591 QualType qual_type (QualType::getFromOpaquePtr(type));
Sean Callanan21f2e192011-12-14 01:13:04 +00001592 const FunctionProtoType *func_proto_type = qual_type.getTypePtr()->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +00001593
Greg Clayton8de27c72010-10-15 22:48:33 +00001594 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +00001595 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001596 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +00001597 unsigned ArgIndex;
1598
Sean Callananc1535182011-10-07 23:18:13 +00001599 SmallVector<ParmVarDecl *, 5> parm_var_decls;
1600
Sean Callanan8f0dc342010-06-22 23:46:24 +00001601 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
1602 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001603 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001604
Sean Callananf76afff2011-10-28 23:38:38 +00001605 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callananc1535182011-10-07 23:18:13 +00001606 const_cast<DeclContext*>(m_decl_context),
1607 SourceLocation(),
1608 SourceLocation(),
1609 NULL,
1610 arg_qual_type,
1611 NULL,
1612 SC_Static,
1613 SC_Static,
1614 NULL));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001615 }
1616
Sean Callananc1535182011-10-07 23:18:13 +00001617 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001618 }
Sean Callanandc5fce12011-12-01 21:04:37 +00001619 else
1620 {
1621 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1622
1623 log->Printf("Function type wasn't a FunctionProtoType");
1624 }
Sean Callanan8f0dc342010-06-22 23:46:24 +00001625
Greg Clayton8de27c72010-10-15 22:48:33 +00001626 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +00001627
Greg Clayton8de27c72010-10-15 22:48:33 +00001628 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +00001629}
Sean Callanan0fc73582010-07-27 00:55:47 +00001630
Greg Claytonb01000f2011-01-17 03:46:26 +00001631clang::NamedDecl *
1632NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +00001633{
Sean Callananad293092011-01-18 23:32:05 +00001634 FunctionProtoType::ExtProtoInfo proto_info;
1635
1636 proto_info.Variadic = true;
1637
Sean Callananf76afff2011-10-28 23:38:38 +00001638 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
1639 NULL, // argument types
1640 0, // number of arguments
1641 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +00001642
Sean Callanan0fc73582010-07-27 00:55:47 +00001643 return AddFunDecl(generic_function_type.getAsOpaquePtr());
1644}
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001645
Greg Claytonb01000f2011-01-17 03:46:26 +00001646clang::NamedDecl *
1647NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001648{
Greg Claytona1aaaff2011-01-23 00:34:52 +00001649 if (type)
1650 {
1651 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001652
Sean Callanan21f2e192011-12-14 01:13:04 +00001653 if (const TagType *tag_type = qual_type->getAs<TagType>())
Greg Claytona1aaaff2011-01-23 00:34:52 +00001654 {
1655 TagDecl *tag_decl = tag_type->getDecl();
1656
1657 m_decls.push_back(tag_decl);
1658
1659 return tag_decl;
1660 }
Sean Callanan21f2e192011-12-14 01:13:04 +00001661 else if (const ObjCObjectType *objc_object_type = qual_type->getAs<ObjCObjectType>())
Greg Claytona1aaaff2011-01-23 00:34:52 +00001662 {
1663 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
1664
1665 m_decls.push_back((NamedDecl*)interface_decl);
1666
1667 return (NamedDecl*)interface_decl;
1668 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001669 }
Greg Claytona1aaaff2011-01-23 00:34:52 +00001670 return NULL;
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001671}
Greg Claytone6d72ca2011-06-25 00:44:06 +00001672
1673void
1674NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
1675{
1676 for (clang::NamedDecl * const *decl_iterator = result.first;
1677 decl_iterator != result.second;
1678 ++decl_iterator)
1679 m_decls.push_back (*decl_iterator);
1680}
1681
1682void
1683NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
1684{
1685 m_decls.push_back (decl);
1686}