blob: f94eb9c8e6c8a5767f47f9c2e03a4612719fb204 [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 }
625
Sean Callanane1301a62011-12-06 03:41:14 +0000626
Sean Callanan9394b5a2011-10-29 19:50:43 +0000627 void *copied_type = GuardedCopyType(m_ast_context, type_sp->GetClangAST(), type_sp->GetClangFullType());
Sean Callanane1301a62011-12-06 03:41:14 +0000628
Sean Callanandc5fce12011-12-01 21:04:37 +0000629 if (!copied_type)
630 {
631 if (log)
Sean Callanane1301a62011-12-06 03:41:14 +0000632 log->Printf(" CAS::FEVD[%u] - Couldn't export the type for a constant integer result",
633 current_id);
634
Sean Callanandc5fce12011-12-01 21:04:37 +0000635 break;
636 }
637
Sean Callanan9394b5a2011-10-29 19:50:43 +0000638 context.AddTypeDecl(copied_type);
639 }
Sean Callanan673f3db2011-11-30 22:11:59 +0000640
Sean Callanan9394b5a2011-10-29 19:50:43 +0000641 } while(0);
642}
643
Sean Callanan9b714842011-11-09 19:33:21 +0000644void
645ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
646{
647 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
648
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000649 static unsigned int invocation_id = 0;
650 unsigned int current_id = invocation_id++;
651
Sean Callanan9b714842011-11-09 19:33:21 +0000652 const DeclarationName &decl_name(context.m_decl_name);
653 const DeclContext *decl_ctx(context.m_decl_context);
654
655 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
656
657 if (!interface_decl)
658 return;
659
Sean Callanan8d611562012-04-05 00:12:52 +0000660 do
661 {
662 Decl *original_decl = NULL;
663 ASTContext *original_ctx = NULL;
664
665 m_ast_importer->ResolveDeclOrigin(interface_decl, &original_decl, &original_ctx);
666
667 if (!original_decl)
668 break;
669
670 ObjCInterfaceDecl *original_interface_decl = dyn_cast<ObjCInterfaceDecl>(original_decl);
671
672 Selector original_selector;
673
674 if (decl_name.isObjCZeroArgSelector())
675 {
676 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name.getAsString());
677 original_selector = original_ctx->Selectors.getSelector(0, &ident);
678 }
679 else if (decl_name.isObjCOneArgSelector())
680 {
681 const std::string &decl_name_string = decl_name.getAsString();
682 std::string decl_name_string_without_colon(decl_name_string.c_str(), decl_name_string.length() - 1);
683 IdentifierInfo *ident = &original_ctx->Idents.get(decl_name_string_without_colon.c_str());
684 original_selector = original_ctx->Selectors.getSelector(1, &ident);
685 }
686 else
687 {
688 SmallVector<IdentifierInfo *, 4> idents;
689
690 clang::Selector sel = decl_name.getObjCSelector();
691
692 int num_args = sel.getNumArgs();
693
694 for (unsigned i = 0;
695 i != num_args;
696 ++i)
697 {
698 idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i)));
699 }
700
701 original_selector = original_ctx->Selectors.getSelector(num_args, idents.data());
702 }
703
704 DeclarationName original_decl_name(original_selector);
705
706 ObjCInterfaceDecl::lookup_result result = original_interface_decl->lookup(original_decl_name);
707
708 if (result.first == result.second)
709 break;
710
711 if (!*result.first)
712 break;
713
714 ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(*result.first);
715
716 if (!result_method)
717 break;
718
719 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &result_method->getASTContext(), result_method);
720
721 if (!copied_decl)
722 continue;
723
724 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
725
726 if (!copied_method_decl)
727 continue;
728
729 if (log)
730 {
731 ASTDumper dumper((Decl*)copied_method_decl);
732 log->Printf(" CAS::FOMD[%d] found (in debug info) %s", current_id, dumper.GetCString());
733 }
734
735 context.AddNamedDecl(copied_method_decl);
736
737 return;
738 } while (0);
739
Sean Callanan9b714842011-11-09 19:33:21 +0000740 StreamString ss;
Sean Callanane1301a62011-12-06 03:41:14 +0000741
Sean Callanan9b714842011-11-09 19:33:21 +0000742 if (decl_name.isObjCZeroArgSelector())
743 {
744 ss.Printf("%s", decl_name.getAsString().c_str());
745 }
746 else if (decl_name.isObjCOneArgSelector())
747 {
Sean Callanan1d9ffe22011-11-14 18:29:46 +0000748 ss.Printf("%s", decl_name.getAsString().c_str());
Sean Callanan9b714842011-11-09 19:33:21 +0000749 }
750 else
751 {
752 clang::Selector sel = decl_name.getObjCSelector();
753
754 for (unsigned i = 0, e = sel.getNumArgs();
755 i != e;
756 ++i)
757 {
758 llvm::StringRef r = sel.getNameForSlot(i);
759 ss.Printf("%s:", r.str().c_str());
760 }
761 }
762 ss.Flush();
763
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000764 ConstString selector_name(ss.GetData());
765
Sean Callanan9b714842011-11-09 19:33:21 +0000766 if (log)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000767 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
768 current_id,
769 m_ast_context,
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000770 interface_decl->getNameAsString().c_str(),
771 selector_name.AsCString());
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000772 SymbolContextList sc_list;
773
774 const bool include_symbols = false;
Sean Callanan302d78c2012-02-10 22:52:19 +0000775 const bool include_inlines = false;
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000776 const bool append = false;
777
Sean Callanane0028b82012-01-19 02:17:40 +0000778 std::string interface_name = interface_decl->getNameAsString();
779
780 do
781 {
782 StreamString ms;
783 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());
784 ms.Flush();
785 ConstString instance_method_name(ms.GetData());
786
Sean Callanan302d78c2012-02-10 22:52:19 +0000787 m_target->GetImages().FindFunctions(instance_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanane0028b82012-01-19 02:17:40 +0000788
789 if (sc_list.GetSize())
790 break;
791
792 ms.Clear();
793 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());
794 ms.Flush();
795 ConstString class_method_name(ms.GetData());
796
Sean Callanan302d78c2012-02-10 22:52:19 +0000797 m_target->GetImages().FindFunctions(class_method_name, lldb::eFunctionNameTypeFull, include_symbols, include_inlines, append, sc_list);
Sean Callanane0028b82012-01-19 02:17:40 +0000798
799 if (sc_list.GetSize())
800 break;
801
802 // Fall back and check for methods in categories. If we find methods this way, we need to check that they're actually in
803 // categories on the desired class.
804
805 SymbolContextList candidate_sc_list;
806
Sean Callanan302d78c2012-02-10 22:52:19 +0000807 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, include_inlines, append, candidate_sc_list);
Sean Callanane0028b82012-01-19 02:17:40 +0000808
809 for (uint32_t ci = 0, ce = candidate_sc_list.GetSize();
810 ci != ce;
811 ++ci)
812 {
813 SymbolContext candidate_sc;
814
815 if (!candidate_sc_list.GetContextAtIndex(ci, candidate_sc))
816 continue;
817
818 if (!candidate_sc.function)
819 continue;
820
821 const char *candidate_name = candidate_sc.function->GetName().AsCString();
822
823 const char *cursor = candidate_name;
824
825 if (*cursor != '+' && *cursor != '-')
826 continue;
827
828 ++cursor;
829
830 if (*cursor != '[')
831 continue;
832
833 ++cursor;
834
835 size_t interface_len = interface_name.length();
836
837 if (strncmp(cursor, interface_name.c_str(), interface_len))
838 continue;
839
840 cursor += interface_len;
841
842 if (*cursor == ' ' || *cursor == '(')
843 sc_list.Append(candidate_sc);
844 }
845 }
846 while (0);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000847
848 for (uint32_t i = 0, e = sc_list.GetSize();
849 i != e;
850 ++i)
851 {
852 SymbolContext sc;
853
854 if (!sc_list.GetContextAtIndex(i, sc))
855 continue;
856
857 if (!sc.function)
858 continue;
859
860 DeclContext *function_ctx = sc.function->GetClangDeclContext();
861
862 if (!function_ctx)
863 continue;
864
865 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
866
867 if (!method_decl)
868 continue;
869
870 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
871
872 if (!found_interface_decl)
873 continue;
874
875 if (found_interface_decl->getName() == interface_decl->getName())
876 {
Sean Callanan4938bd62011-11-16 18:20:47 +0000877 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000878
879 if (!copied_decl)
880 continue;
881
882 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
883
884 if (!copied_method_decl)
885 continue;
886
887 if (log)
888 {
889 ASTDumper dumper((Decl*)copied_method_decl);
Sean Callanane1301a62011-12-06 03:41:14 +0000890 log->Printf(" CAS::FOMD[%d] found (in debug info) %s", current_id, dumper.GetCString());
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000891 }
Sean Callanane0028b82012-01-19 02:17:40 +0000892
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000893 context.AddNamedDecl(copied_method_decl);
894 }
895 }
Sean Callanan9b714842011-11-09 19:33:21 +0000896}
897
Sean Callanan8f2e3922012-02-04 08:49:35 +0000898template <class D> class TaggedASTDecl {
899public:
900 TaggedASTDecl() : decl(NULL) { }
901 TaggedASTDecl(D *_decl) : decl(_decl) { }
902 bool IsValid() const { return (decl != NULL); }
903 bool IsInvalid() const { return !IsValid(); }
904 D *operator->() const { return decl; }
905 D *decl;
906};
907
908template <class D2, template <class D> class TD, class D1>
909TD<D2>
910DynCast(TD<D1> source)
911{
912 return TD<D2> (dyn_cast<D2>(source.decl));
913}
914
915template <class D = Decl> class DeclFromParser;
916template <class D = Decl> class DeclFromUser;
917
918template <class D> class DeclFromParser : public TaggedASTDecl<D> {
919public:
920 DeclFromParser() : TaggedASTDecl<D>() { }
921 DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) { }
922
923 DeclFromUser<D> GetOrigin(ClangASTImporter *importer);
924};
925
926template <class D> class DeclFromUser : public TaggedASTDecl<D> {
927public:
928 DeclFromUser() : TaggedASTDecl<D>() { }
929 DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) { }
930
931 DeclFromParser<D> Import(ClangASTImporter *importer, ASTContext &dest_ctx);
932};
933
934template <class D>
935DeclFromUser<D>
936DeclFromParser<D>::GetOrigin(ClangASTImporter *importer)
937{
938 DeclFromUser <> origin_decl;
939 importer->ResolveDeclOrigin(this->decl, &origin_decl.decl, NULL);
940 if (origin_decl.IsInvalid())
941 return DeclFromUser<D>();
942 return DeclFromUser<D>(dyn_cast<D>(origin_decl.decl));
943}
944
945template <class D>
946DeclFromParser<D>
947DeclFromUser<D>::Import(ClangASTImporter *importer, ASTContext &dest_ctx)
948{
949 DeclFromParser <> parser_generic_decl(importer->CopyDecl(&dest_ctx, &this->decl->getASTContext(), this->decl));
950 if (parser_generic_decl.IsInvalid())
951 return DeclFromParser<D>();
952 return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
953}
954
Sean Callanan931acec2012-02-22 23:57:45 +0000955static bool
956FindObjCPropertyAndIvarDeclsWithOrigin (unsigned int current_id,
957 NameSearchContext &context,
958 clang::ASTContext &ast_context,
959 ClangASTImporter *ast_importer,
960 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl)
961{
962 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
963
964 if (origin_iface_decl.IsInvalid())
965 return false;
966
967 std::string name_str = context.m_decl_name.getAsString();
968 StringRef name(name_str.c_str());
969 IdentifierInfo &name_identifier(origin_iface_decl->getASTContext().Idents.get(name));
970
971 DeclFromUser<ObjCPropertyDecl> origin_property_decl(origin_iface_decl->FindPropertyDeclaration(&name_identifier));
972
973 bool found = false;
974
975 if (origin_property_decl.IsValid())
976 {
977 DeclFromParser<ObjCPropertyDecl> parser_property_decl(origin_property_decl.Import(ast_importer, ast_context));
978 if (parser_property_decl.IsValid())
979 {
980 if (log)
981 {
982 ASTDumper dumper((Decl*)parser_property_decl.decl);
983 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
984 }
985
986 context.AddNamedDecl(parser_property_decl.decl);
987 found = true;
988 }
989 }
990
991 DeclFromUser<ObjCIvarDecl> origin_ivar_decl(origin_iface_decl->getIvarDecl(&name_identifier));
992
993 if (origin_ivar_decl.IsValid())
994 {
995 DeclFromParser<ObjCIvarDecl> parser_ivar_decl(origin_ivar_decl.Import(ast_importer, ast_context));
996 if (parser_ivar_decl.IsValid())
997 {
998 if (log)
999 {
1000 ASTDumper dumper((Decl*)parser_ivar_decl.decl);
1001 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
1002 }
1003
1004 context.AddNamedDecl(parser_ivar_decl.decl);
1005 found = true;
1006 }
1007 }
1008
1009 return found;
1010}
1011
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001012void
Sean Callanan8f2e3922012-02-04 08:49:35 +00001013ClangASTSource::FindObjCPropertyAndIvarDecls (NameSearchContext &context)
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001014{
1015 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1016
1017 static unsigned int invocation_id = 0;
1018 unsigned int current_id = invocation_id++;
1019
Sean Callanan8f2e3922012-02-04 08:49:35 +00001020 DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl(cast<ObjCInterfaceDecl>(context.m_decl_context));
1021 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(parser_iface_decl.GetOrigin(m_ast_importer));
Sean Callanan931acec2012-02-22 23:57:45 +00001022
1023 ConstString class_name(parser_iface_decl->getNameAsString().c_str());
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001024
1025 if (log)
Sean Callanan8f2e3922012-02-04 08:49:35 +00001026 log->Printf("ClangASTSource::FindObjCPropertyAndIvarDecls[%d] on (ASTContext*)%p for '%s.%s'",
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001027 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001028 m_ast_context,
Sean Callanan8f2e3922012-02-04 08:49:35 +00001029 parser_iface_decl->getNameAsString().c_str(),
Sean Callanan931acec2012-02-22 23:57:45 +00001030 context.m_decl_name.getAsString().c_str());
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001031
Sean Callanan931acec2012-02-22 23:57:45 +00001032 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1033 context,
1034 *m_ast_context,
1035 m_ast_importer,
1036 origin_iface_decl))
1037 return;
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001038
Sean Callanan931acec2012-02-22 23:57:45 +00001039 if (log)
1040 log->Printf("CAS::FOPD[%d] couldn't find the property on origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p, searching elsewhere...",
1041 current_id,
1042 origin_iface_decl.decl,
1043 &origin_iface_decl->getASTContext());
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001044
Sean Callanan931acec2012-02-22 23:57:45 +00001045 SymbolContext null_sc;
1046 TypeList type_list;
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001047
Sean Callanan931acec2012-02-22 23:57:45 +00001048 lldb::ProcessSP process(m_target->GetProcessSP());
1049
1050 if (!process)
1051 return;
1052
1053 ObjCLanguageRuntime *language_runtime(process->GetObjCLanguageRuntime());
1054
1055 if (!language_runtime)
1056 return;
1057
1058 lldb::TypeSP complete_type_sp(language_runtime->LookupInCompleteClassCache(class_name));
1059
1060 if (!complete_type_sp)
1061 return;
1062
1063 TypeFromUser complete_type = TypeFromUser(complete_type_sp->GetClangFullType(), complete_type_sp->GetClangAST());
1064 lldb::clang_type_t complete_opaque_type = complete_type.GetOpaqueQualType();
1065
1066 if (!complete_opaque_type)
1067 return;
1068
1069 const clang::Type *complete_clang_type = QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr();
1070 const ObjCInterfaceType *complete_interface_type = dyn_cast<ObjCInterfaceType>(complete_clang_type);
1071
1072 if (!complete_interface_type)
1073 return;
1074
1075 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(complete_interface_type->getDecl());
1076
1077 if (complete_iface_decl.decl == origin_iface_decl.decl)
1078 return; // already checked this one
1079
1080 if (log)
1081 log->Printf("CAS::FOPD[%d] trying origin (ObjCInterfaceDecl*)%p/(ASTContext*)%p...",
1082 current_id,
1083 complete_iface_decl.decl,
1084 &complete_iface_decl->getASTContext());
1085
1086
1087 if (FindObjCPropertyAndIvarDeclsWithOrigin(current_id,
1088 context,
1089 *m_ast_context,
1090 m_ast_importer,
1091 complete_iface_decl))
1092 return;
Sean Callanan8f2e3922012-02-04 08:49:35 +00001093}
1094
1095typedef llvm::DenseMap <const FieldDecl *, uint64_t> FieldOffsetMap;
1096typedef llvm::DenseMap <const CXXRecordDecl *, CharUnits> BaseOffsetMap;
1097
1098template <class D, class O>
1099static bool
1100ImportOffsetMap (llvm::DenseMap <const D*, O> &destination_map,
1101 llvm::DenseMap <const D*, O> &source_map,
1102 ClangASTImporter *importer,
1103 ASTContext &dest_ctx)
1104{
1105 typedef llvm::DenseMap <const D*, O> MapType;
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001106
Sean Callanan8f2e3922012-02-04 08:49:35 +00001107 for (typename MapType::iterator fi = source_map.begin(), fe = source_map.end();
1108 fi != fe;
1109 ++fi)
1110 {
1111 DeclFromUser <D> user_decl(const_cast<D*>(fi->first));
1112 DeclFromParser <D> parser_decl(user_decl.Import(importer, dest_ctx));
1113 if (parser_decl.IsInvalid())
1114 return false;
1115 destination_map.insert(std::pair<const D *, O>(parser_decl.decl, fi->second));
1116 }
1117
1118 return true;
1119}
1120
1121template <bool IsVirtual> bool ExtractBaseOffsets (const ASTRecordLayout &record_layout,
1122 DeclFromUser<const CXXRecordDecl> &record,
1123 BaseOffsetMap &base_offsets)
1124{
1125 for (CXXRecordDecl::base_class_const_iterator
1126 bi = (IsVirtual ? record->vbases_begin() : record->bases_begin()),
1127 be = (IsVirtual ? record->vbases_end() : record->bases_end());
1128 bi != be;
1129 ++bi)
1130 {
1131 if (!IsVirtual && bi->isVirtual())
1132 continue;
1133
1134 const clang::Type *origin_base_type = bi->getType().getTypePtr();
1135 const clang::RecordType *origin_base_record_type = origin_base_type->getAs<RecordType>();
1136
1137 if (!origin_base_record_type)
1138 return false;
1139
1140 DeclFromUser <RecordDecl> origin_base_record(origin_base_record_type->getDecl());
1141
1142 if (origin_base_record.IsInvalid())
1143 return false;
1144
1145 DeclFromUser <CXXRecordDecl> origin_base_cxx_record(DynCast<CXXRecordDecl>(origin_base_record));
1146
1147 if (origin_base_cxx_record.IsInvalid())
1148 return false;
1149
1150 CharUnits base_offset;
1151
1152 if (IsVirtual)
1153 base_offset = record_layout.getVBaseClassOffset(origin_base_cxx_record.decl);
1154 else
1155 base_offset = record_layout.getBaseClassOffset(origin_base_cxx_record.decl);
1156
1157 base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(origin_base_cxx_record.decl, base_offset));
1158 }
1159
1160 return true;
1161}
1162
1163bool
1164ClangASTSource::layoutRecordType(const RecordDecl *record,
1165 uint64_t &size,
1166 uint64_t &alignment,
1167 FieldOffsetMap &field_offsets,
1168 BaseOffsetMap &base_offsets,
1169 BaseOffsetMap &virtual_base_offsets)
1170{
1171 static unsigned int invocation_id = 0;
1172 unsigned int current_id = invocation_id++;
1173
1174 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1175
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001176 if (log)
1177 {
Sean Callanan8f2e3922012-02-04 08:49:35 +00001178 log->Printf("LayoutRecordType[%u] on (RecordDecl*)%p [name = '%s']",
1179 current_id,
1180 m_ast_context,
1181 record->getNameAsString().c_str());
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001182 }
Sean Callanan8f2e3922012-02-04 08:49:35 +00001183
1184
1185 DeclFromParser <const RecordDecl> parser_record(record);
1186 DeclFromUser <const RecordDecl> origin_record(parser_record.GetOrigin(m_ast_importer));
1187
1188 if (origin_record.IsInvalid())
1189 return false;
1190
1191 FieldOffsetMap origin_field_offsets;
1192 BaseOffsetMap origin_base_offsets;
1193 BaseOffsetMap origin_virtual_base_offsets;
1194
Sean Callanan33bff9a2012-04-07 00:06:00 +00001195 ClangASTContext::GetCompleteDecl(&origin_record->getASTContext(), const_cast<RecordDecl*>(origin_record.decl));
1196
1197 if (!origin_record.decl->getDefinition())
1198 return false;
1199
Sean Callanan8f2e3922012-02-04 08:49:35 +00001200 const ASTRecordLayout &record_layout(origin_record->getASTContext().getASTRecordLayout(origin_record.decl));
1201
1202 int field_idx = 0;
1203
1204 for (RecordDecl::field_iterator fi = origin_record->field_begin(), fe = origin_record->field_end();
1205 fi != fe;
1206 ++fi)
1207 {
1208 uint64_t field_offset = record_layout.getFieldOffset(field_idx);
1209
1210 origin_field_offsets.insert(std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
1211
1212 field_idx++;
1213 }
1214
1215 ASTContext &parser_ast_context(record->getASTContext());
1216
1217 DeclFromUser <const CXXRecordDecl> origin_cxx_record(DynCast<const CXXRecordDecl>(origin_record));
1218
1219 if (origin_cxx_record.IsValid())
1220 {
1221 if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record, origin_base_offsets) ||
1222 !ExtractBaseOffsets<true>(record_layout, origin_cxx_record, origin_virtual_base_offsets))
1223 return false;
1224 }
1225
1226 if (!ImportOffsetMap(field_offsets, origin_field_offsets, m_ast_importer, parser_ast_context) ||
1227 !ImportOffsetMap(base_offsets, origin_base_offsets, m_ast_importer, parser_ast_context) ||
1228 !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets, m_ast_importer, parser_ast_context))
1229 return false;
1230
1231 size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth();
1232 alignment = record_layout.getAlignment().getQuantity() * m_ast_context->getCharWidth();
1233
1234 if (log)
1235 {
1236 log->Printf("LRT[%u] returned:", current_id);
1237 log->Printf("LRT[%u] Original = (RecordDecl*)%p", current_id, origin_record.decl);
1238 log->Printf("LRT[%u] Size = %lld", current_id, size);
1239 log->Printf("LRT[%u] Alignment = %lld", current_id, alignment);
1240 log->Printf("LRT[%u] Fields:", current_id);
1241 for (RecordDecl::field_iterator fi = record->field_begin(), fe = record->field_end();
1242 fi != fe;
1243 ++fi)
1244 {
1245 log->Printf("LRT[%u] (FieldDecl*)%p, Name = '%s', Offset = %lld bits",
1246 current_id,
1247 *fi,
1248 fi->getNameAsString().c_str(),
1249 field_offsets[*fi]);
1250 }
1251 DeclFromParser <const CXXRecordDecl> parser_cxx_record = DynCast<const CXXRecordDecl>(parser_record);
1252 if (parser_cxx_record.IsValid())
1253 {
1254 log->Printf("LRT[%u] Bases:", current_id);
1255 for (CXXRecordDecl::base_class_const_iterator bi = parser_cxx_record->bases_begin(), be = parser_cxx_record->bases_end();
1256 bi != be;
1257 ++bi)
1258 {
1259 bool is_virtual = bi->isVirtual();
1260
1261 QualType base_type = bi->getType();
1262 const RecordType *base_record_type = base_type->getAs<RecordType>();
1263 DeclFromParser <RecordDecl> base_record(base_record_type->getDecl());
1264 DeclFromParser <CXXRecordDecl> base_cxx_record = DynCast<CXXRecordDecl>(base_record);
1265
1266 log->Printf("LRT[%u] %s(CXXRecordDecl*)%p, Name = '%s', Offset = %lld chars",
1267 current_id,
1268 (is_virtual ? "Virtual " : ""),
1269 base_cxx_record.decl,
1270 base_cxx_record.decl->getNameAsString().c_str(),
1271 (is_virtual ? virtual_base_offsets[base_cxx_record.decl].getQuantity() :
1272 base_offsets[base_cxx_record.decl].getQuantity()));
1273 }
1274 }
1275 else
1276 {
1277 log->Printf("LRD[%u] Not a CXXRecord, so no bases", current_id);
1278 }
1279 }
1280
1281 return true;
Sean Callanane6ea5fe2011-11-15 02:11:17 +00001282}
1283
Sean Callanan73b520f2011-10-29 01:58:46 +00001284void
1285ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
1286 const ConstString &name,
1287 ClangASTImporter::NamespaceMapSP &parent_map) const
1288{
1289 static unsigned int invocation_id = 0;
1290 unsigned int current_id = invocation_id++;
1291
1292 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1293
1294 if (log)
1295 {
1296 if (parent_map && parent_map->size())
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001297 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +00001298 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001299 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +00001300 name.GetCString(),
1301 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
1302 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001303 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +00001304 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +00001305 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +00001306 name.GetCString());
1307 }
1308
1309
1310 if (parent_map)
1311 {
1312 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
1313 i != e;
1314 ++i)
1315 {
1316 ClangNamespaceDecl found_namespace_decl;
1317
1318 lldb::ModuleSP module_sp = i->first;
1319 ClangNamespaceDecl module_parent_namespace_decl = i->second;
1320
1321 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
1322
1323 if (!symbol_vendor)
1324 continue;
1325
1326 SymbolContext null_sc;
1327
1328 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
1329
1330 if (!found_namespace_decl)
1331 continue;
1332
1333 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
1334
1335 if (log)
1336 log->Printf(" CMN[%u] Found namespace %s in module %s",
1337 current_id,
1338 name.GetCString(),
1339 module_sp->GetFileSpec().GetFilename().GetCString());
1340 }
1341 }
1342 else
1343 {
Jim Ingham93367902012-05-30 02:19:25 +00001344 ModuleList &target_images = m_target->GetImages();
1345 Mutex::Locker modules_locker(target_images.GetMutex());
1346
Sean Callanan73b520f2011-10-29 01:58:46 +00001347 ClangNamespaceDecl null_namespace_decl;
1348
Jim Ingham93367902012-05-30 02:19:25 +00001349 for (uint32_t i = 0, e = target_images.GetSize();
Sean Callanan73b520f2011-10-29 01:58:46 +00001350 i != e;
1351 ++i)
1352 {
Jim Ingham93367902012-05-30 02:19:25 +00001353 lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i);
Sean Callanan73b520f2011-10-29 01:58:46 +00001354
1355 if (!image)
1356 continue;
1357
1358 ClangNamespaceDecl found_namespace_decl;
1359
1360 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
1361
1362 if (!symbol_vendor)
1363 continue;
1364
1365 SymbolContext null_sc;
1366
1367 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
1368
1369 if (!found_namespace_decl)
1370 continue;
1371
1372 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
1373
1374 if (log)
1375 log->Printf(" CMN[%u] Found namespace %s in module %s",
1376 current_id,
1377 name.GetCString(),
1378 image->GetFileSpec().GetFilename().GetCString());
1379 }
1380 }
1381}
1382
Sean Callananbb715f92011-10-29 02:28:18 +00001383NamespaceDecl *
1384ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
1385{
Greg Clayton13d24fb2012-01-29 20:56:30 +00001386 if (!namespace_decls)
Sean Callananbb715f92011-10-29 02:28:18 +00001387 return NULL;
1388
1389 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1390
1391 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
1392
Sean Callanan4938bd62011-11-16 18:20:47 +00001393 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
Sean Callananbb715f92011-10-29 02:28:18 +00001394
Sean Callananddb59332012-03-20 21:11:12 +00001395 if (!copied_decl)
1396 return NULL;
1397
Sean Callananbb715f92011-10-29 02:28:18 +00001398 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1399
1400 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
1401
1402 return dyn_cast<NamespaceDecl>(copied_decl);
1403}
1404
Sean Callanan9394b5a2011-10-29 19:50:43 +00001405void *
1406ClangASTSource::GuardedCopyType (ASTContext *dest_context,
1407 ASTContext *source_context,
1408 void *clang_type)
1409{
1410 SetImportInProgress(true);
1411
Sean Callanan4938bd62011-11-16 18:20:47 +00001412 QualType ret_qual_type = m_ast_importer->CopyType (m_ast_context, source_context, QualType::getFromOpaquePtr(clang_type));
Sean Callanan9394b5a2011-10-29 19:50:43 +00001413
1414 void *ret = ret_qual_type.getAsOpaquePtr();
1415
1416 SetImportInProgress(false);
1417
Sean Callananddb59332012-03-20 21:11:12 +00001418 if (ret && ret_qual_type->getCanonicalTypeInternal().isNull())
Sean Callananaa11af82012-02-27 19:22:39 +00001419 // this shouldn't happen, but we're hardening because the AST importer seems to be generating bad types
1420 // on occasion.
1421 return NULL;
1422
Sean Callanan9394b5a2011-10-29 19:50:43 +00001423 return ret;
1424}
1425
Greg Claytonb01000f2011-01-17 03:46:26 +00001426clang::NamedDecl *
1427NameSearchContext::AddVarDecl(void *type)
1428{
Greg Clayton8de27c72010-10-15 22:48:33 +00001429 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +00001430
1431 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +00001432
Sean Callananf76afff2011-10-28 23:38:38 +00001433 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +00001434 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +00001435 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +00001436 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +00001437 ii,
Chris Lattner24943d22010-06-08 16:52:24 +00001438 QualType::getFromOpaquePtr(type),
1439 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +00001440 SC_Static,
1441 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +00001442 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +00001443
1444 return Decl;
1445}
Sean Callanan8f0dc342010-06-22 23:46:24 +00001446
Greg Claytonb01000f2011-01-17 03:46:26 +00001447clang::NamedDecl *
1448NameSearchContext::AddFunDecl (void *type)
1449{
Sean Callanan30a5dd52012-03-21 17:13:20 +00001450 assert (type && "Type for variable must be non-NULL!");
1451
Sean Callananf76afff2011-10-28 23:38:38 +00001452 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +00001453 const_cast<DeclContext*>(m_decl_context),
1454 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +00001455 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +00001456 m_decl_name.getAsIdentifierInfo(),
1457 QualType::getFromOpaquePtr(type),
1458 NULL,
1459 SC_Static,
1460 SC_Static,
1461 false,
1462 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +00001463
Sean Callananb291abe2010-08-12 23:45:38 +00001464 // We have to do more than just synthesize the FunctionDecl. We have to
1465 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
1466 // this, we raid the function's FunctionProtoType for types.
1467
Greg Clayton8de27c72010-10-15 22:48:33 +00001468 QualType qual_type (QualType::getFromOpaquePtr(type));
Sean Callanan21f2e192011-12-14 01:13:04 +00001469 const FunctionProtoType *func_proto_type = qual_type.getTypePtr()->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +00001470
Greg Clayton8de27c72010-10-15 22:48:33 +00001471 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +00001472 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001473 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +00001474 unsigned ArgIndex;
1475
Sean Callananc1535182011-10-07 23:18:13 +00001476 SmallVector<ParmVarDecl *, 5> parm_var_decls;
1477
Sean Callanan8f0dc342010-06-22 23:46:24 +00001478 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
1479 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001480 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001481
Sean Callananf76afff2011-10-28 23:38:38 +00001482 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callananc1535182011-10-07 23:18:13 +00001483 const_cast<DeclContext*>(m_decl_context),
1484 SourceLocation(),
1485 SourceLocation(),
1486 NULL,
1487 arg_qual_type,
1488 NULL,
1489 SC_Static,
1490 SC_Static,
1491 NULL));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001492 }
1493
Sean Callananc1535182011-10-07 23:18:13 +00001494 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001495 }
Sean Callanandc5fce12011-12-01 21:04:37 +00001496 else
1497 {
1498 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1499
1500 log->Printf("Function type wasn't a FunctionProtoType");
1501 }
Sean Callanan8f0dc342010-06-22 23:46:24 +00001502
Greg Clayton8de27c72010-10-15 22:48:33 +00001503 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +00001504
Greg Clayton8de27c72010-10-15 22:48:33 +00001505 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +00001506}
Sean Callanan0fc73582010-07-27 00:55:47 +00001507
Greg Claytonb01000f2011-01-17 03:46:26 +00001508clang::NamedDecl *
1509NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +00001510{
Sean Callananad293092011-01-18 23:32:05 +00001511 FunctionProtoType::ExtProtoInfo proto_info;
1512
1513 proto_info.Variadic = true;
1514
Sean Callananf76afff2011-10-28 23:38:38 +00001515 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
1516 NULL, // argument types
1517 0, // number of arguments
1518 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +00001519
Sean Callanan0fc73582010-07-27 00:55:47 +00001520 return AddFunDecl(generic_function_type.getAsOpaquePtr());
1521}
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001522
Greg Claytonb01000f2011-01-17 03:46:26 +00001523clang::NamedDecl *
1524NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001525{
Greg Claytona1aaaff2011-01-23 00:34:52 +00001526 if (type)
1527 {
1528 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001529
Sean Callanan21f2e192011-12-14 01:13:04 +00001530 if (const TagType *tag_type = qual_type->getAs<TagType>())
Greg Claytona1aaaff2011-01-23 00:34:52 +00001531 {
1532 TagDecl *tag_decl = tag_type->getDecl();
1533
1534 m_decls.push_back(tag_decl);
1535
1536 return tag_decl;
1537 }
Sean Callanan21f2e192011-12-14 01:13:04 +00001538 else if (const ObjCObjectType *objc_object_type = qual_type->getAs<ObjCObjectType>())
Greg Claytona1aaaff2011-01-23 00:34:52 +00001539 {
1540 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
1541
1542 m_decls.push_back((NamedDecl*)interface_decl);
1543
1544 return (NamedDecl*)interface_decl;
1545 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001546 }
Greg Claytona1aaaff2011-01-23 00:34:52 +00001547 return NULL;
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001548}
Greg Claytone6d72ca2011-06-25 00:44:06 +00001549
1550void
1551NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
1552{
1553 for (clang::NamedDecl * const *decl_iterator = result.first;
1554 decl_iterator != result.second;
1555 ++decl_iterator)
1556 m_decls.push_back (*decl_iterator);
1557}
1558
1559void
1560NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
1561{
1562 m_decls.push_back (decl);
1563}