blob: d368408f1f5d2ff964626f1429a9fdd19d6d3753 [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"
Greg Claytonf4c7ae02010-10-15 03:36:13 +000012#include "lldb/Core/Log.h"
Greg Clayton6e0101c2011-09-17 06:21:20 +000013#include "lldb/Core/Module.h"
Sean Callanan73b520f2011-10-29 01:58:46 +000014#include "lldb/Core/ModuleList.h"
Sean Callananbb715f92011-10-29 02:28:18 +000015#include "lldb/Expression/ASTDumper.h"
Chris Lattner24943d22010-06-08 16:52:24 +000016#include "lldb/Expression/ClangASTSource.h"
17#include "lldb/Expression/ClangExpression.h"
Sean Callanan73b520f2011-10-29 01:58:46 +000018#include "lldb/Symbol/ClangNamespaceDecl.h"
19#include "lldb/Symbol/SymbolVendor.h"
Sean Callanan673f3db2011-11-30 22:11:59 +000020#include "lldb/Target/ObjCLanguageRuntime.h"
Sean Callanan73b520f2011-10-29 01:58:46 +000021#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022
23using namespace clang;
24using namespace lldb_private;
25
Greg Claytonb01000f2011-01-17 03:46:26 +000026ClangASTSource::~ClangASTSource()
27{
Sean Callanana3d04472011-11-29 00:42:02 +000028 m_ast_importer->ForgetDestination(m_ast_context);
29
Johnny Chenfa21ffd2011-11-30 23:18:53 +000030 // We are in the process of destruction, don't create clang ast context on demand
31 // by passing false to Target::GetScratchClangASTContext(create_on_demand).
32 ClangASTContext *scratch_clang_ast_context = m_target->GetScratchClangASTContext(false);
Sean Callanana3d04472011-11-29 00:42:02 +000033
34 if (!scratch_clang_ast_context)
35 return;
36
37 clang::ASTContext *scratch_ast_context = scratch_clang_ast_context->getASTContext();
38
39 if (!scratch_ast_context)
40 return;
41
42 if (m_ast_context != scratch_ast_context)
43 m_ast_importer->ForgetSource(scratch_ast_context, m_ast_context);
Greg Claytonb01000f2011-01-17 03:46:26 +000044}
Chris Lattner24943d22010-06-08 16:52:24 +000045
Greg Claytonb01000f2011-01-17 03:46:26 +000046void
47ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer)
48{
Sean Callananf76afff2011-10-28 23:38:38 +000049 if (!m_ast_context)
50 return;
51
52 m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage();
53 m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage();
Chris Lattner24943d22010-06-08 16:52:24 +000054}
55
Chris Lattner24943d22010-06-08 16:52:24 +000056// The core lookup interface.
Greg Claytonb01000f2011-01-17 03:46:26 +000057DeclContext::lookup_result
58ClangASTSource::FindExternalVisibleDeclsByName
Greg Claytonf4c7ae02010-10-15 03:36:13 +000059(
60 const DeclContext *decl_ctx,
Greg Clayton8de27c72010-10-15 22:48:33 +000061 DeclarationName clang_decl_name
Greg Claytonf4c7ae02010-10-15 03:36:13 +000062)
63{
Sean Callananf76afff2011-10-28 23:38:38 +000064 if (!m_ast_context)
65 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
66
67 if (GetImportInProgress())
Greg Claytonb01000f2011-01-17 03:46:26 +000068 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
69
70 std::string decl_name (clang_decl_name.getAsString());
71
72// if (m_decl_map.DoingASTImport ())
73// return DeclContext::lookup_result();
74//
Greg Clayton8de27c72010-10-15 22:48:33 +000075 switch (clang_decl_name.getNameKind()) {
Chris Lattner24943d22010-06-08 16:52:24 +000076 // Normal identifiers.
77 case DeclarationName::Identifier:
Greg Clayton8de27c72010-10-15 22:48:33 +000078 if (clang_decl_name.getAsIdentifierInfo()->getBuiltinID() != 0)
79 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
80 break;
Chris Lattner24943d22010-06-08 16:52:24 +000081
82 // Operator names. Not important for now.
83 case DeclarationName::CXXOperatorName:
84 case DeclarationName::CXXLiteralOperatorName:
85 return DeclContext::lookup_result();
86
87 // Using directives found in this context.
88 // Tell Sema we didn't find any or we'll end up getting asked a *lot*.
89 case DeclarationName::CXXUsingDirective:
Greg Clayton8de27c72010-10-15 22:48:33 +000090 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
Chris Lattner24943d22010-06-08 16:52:24 +000091
Chris Lattner24943d22010-06-08 16:52:24 +000092 case DeclarationName::ObjCZeroArgSelector:
93 case DeclarationName::ObjCOneArgSelector:
94 case DeclarationName::ObjCMultiArgSelector:
Sean Callanan9b714842011-11-09 19:33:21 +000095 {
96 llvm::SmallVector<NamedDecl*, 1> method_decls;
Chris Lattner24943d22010-06-08 16:52:24 +000097
Sean Callanan9b714842011-11-09 19:33:21 +000098 NameSearchContext method_search_context (*this, method_decls, clang_decl_name, decl_ctx);
99
100 FindObjCMethodDecls(method_search_context);
101
102 return SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, method_decls);
103 }
Chris Lattner24943d22010-06-08 16:52:24 +0000104 // These aren't possible in the global context.
105 case DeclarationName::CXXConstructorName:
106 case DeclarationName::CXXDestructorName:
107 case DeclarationName::CXXConversionFunctionName:
108 return DeclContext::lookup_result();
109 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000110
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000111
Sean Callananf76afff2011-10-28 23:38:38 +0000112 if (!GetLookupsEnabled())
Greg Clayton8de27c72010-10-15 22:48:33 +0000113 {
114 // Wait until we see a '$' at the start of a name before we start doing
115 // any lookups so we can avoid lookup up all of the builtin types.
116 if (!decl_name.empty() && decl_name[0] == '$')
117 {
Sean Callananf76afff2011-10-28 23:38:38 +0000118 SetLookupsEnabled (true);
Greg Clayton8de27c72010-10-15 22:48:33 +0000119 }
120 else
121 {
122 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
123 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000124 }
Greg Clayton8de27c72010-10-15 22:48:33 +0000125
Greg Clayton8de27c72010-10-15 22:48:33 +0000126 ConstString const_decl_name(decl_name.c_str());
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000127
128 const char *uniqued_const_decl_name = const_decl_name.GetCString();
129 if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end())
130 {
131 // We are currently looking up this name...
132 return DeclContext::lookup_result();
133 }
134 m_active_lookups.insert(uniqued_const_decl_name);
Greg Claytona8b278a2010-11-15 01:34:18 +0000135// static uint32_t g_depth = 0;
136// ++g_depth;
137// printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000138 llvm::SmallVector<NamedDecl*, 4> name_decls;
139 NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx);
Sean Callananf76afff2011-10-28 23:38:38 +0000140 FindExternalVisibleDecls(name_search_context);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000141 DeclContext::lookup_result result (SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls));
Greg Claytona8b278a2010-11-15 01:34:18 +0000142// --g_depth;
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000143 m_active_lookups.erase (uniqued_const_decl_name);
144 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000145}
146
Greg Claytonb01000f2011-01-17 03:46:26 +0000147void
148ClangASTSource::CompleteType (TagDecl *tag_decl)
Sean Callananbb715f92011-10-29 02:28:18 +0000149{
150 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
151
Sean Callananb2027ec2011-12-08 23:45:45 +0000152 static unsigned int invocation_id = 0;
153 unsigned int current_id = invocation_id++;
154
Sean Callananbb715f92011-10-29 02:28:18 +0000155 if (log)
156 {
Sean Callananb2027ec2011-12-08 23:45:45 +0000157 log->Printf(" CompleteTagDecl[%u] on (ASTContext*)%p Completing a TagDecl named %s",
158 invocation_id,
159 m_ast_context,
160 tag_decl->getName().str().c_str());
161
162 log->Printf(" CTD[%u] Before:", current_id);
Sean Callananbb715f92011-10-29 02:28:18 +0000163 ASTDumper dumper((Decl*)tag_decl);
164 dumper.ToLog(log, " [CTD] ");
165 }
166
Sean Callananb2027ec2011-12-08 23:45:45 +0000167 if (!m_ast_importer->CompleteTagDecl (tag_decl))
168 {
169 // We couldn't complete the type. Maybe there's a definition
170 // somewhere else that can be completed.
171
172 if (log)
173 log->Printf(" CTD[%u] Type could not be completed in the module in which it was first found.", current_id);
174
175 bool found = false;
176
177 DeclContext *decl_ctx = tag_decl->getDeclContext();
178
179 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(decl_ctx))
180 {
181 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
182
183 if (log && log->GetVerbose())
184 log->Printf(" CTD[%u] Inspecting namespace map %p (%d entries)",
185 current_id,
186 namespace_map.get(),
187 (int)namespace_map->size());
188
189 if (!namespace_map)
190 return;
191
192 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
193 i != e && !found;
194 ++i)
195 {
196 if (log)
197 log->Printf(" CTD[%u] Searching namespace %s in module %s",
198 current_id,
199 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
200 i->first->GetFileSpec().GetFilename().GetCString());
201
202 TypeList types;
203
204 SymbolContext null_sc;
205 ConstString name(tag_decl->getName().str().c_str());
206
207 i->first->FindTypes(null_sc, name, &i->second, true, UINT32_MAX, types);
208
209 for (uint32_t ti = 0, te = types.GetSize();
210 ti != te && !found;
211 ++ti)
212 {
213 lldb::TypeSP type = types.GetTypeAtIndex(ti);
214
215 if (!type)
216 continue;
217
218 lldb::clang_type_t opaque_type = type->GetClangFullType();
219
220 if (!opaque_type)
221 continue;
222
Sean Callanan21f2e192011-12-14 01:13:04 +0000223 const TagType *tag_type = QualType::getFromOpaquePtr(opaque_type)->getAs<TagType>();
Sean Callananb2027ec2011-12-08 23:45:45 +0000224
225 if (!tag_type)
226 continue;
227
228 TagDecl *candidate_tag_decl = const_cast<TagDecl*>(tag_type->getDecl());
229
230 if (m_ast_importer->CompleteTagDeclWithOrigin (tag_decl, candidate_tag_decl))
231 found = true;
232 }
233 }
234 }
235 else
236 {
237 TypeList types;
238
239 SymbolContext null_sc;
240 ConstString name(tag_decl->getName().str().c_str());
241 ClangNamespaceDecl namespace_decl;
242
243 ModuleList &module_list = m_target->GetImages();
244
245 module_list.FindTypes(null_sc, name, true, UINT32_MAX, types);
246
247 for (uint32_t ti = 0, te = types.GetSize();
248 ti != te && !found;
249 ++ti)
250 {
251 lldb::TypeSP type = types.GetTypeAtIndex(ti);
252
253 if (!type)
254 continue;
255
256 lldb::clang_type_t opaque_type = type->GetClangFullType();
257
258 if (!opaque_type)
259 continue;
260
Sean Callanan21f2e192011-12-14 01:13:04 +0000261 const TagType *tag_type = QualType::getFromOpaquePtr(opaque_type)->getAs<TagType>();
Sean Callananb2027ec2011-12-08 23:45:45 +0000262
263 if (!tag_type)
264 continue;
265
266 TagDecl *candidate_tag_decl = const_cast<TagDecl*>(tag_type->getDecl());
267
268 if (m_ast_importer->CompleteTagDeclWithOrigin (tag_decl, candidate_tag_decl))
269 found = true;
270 }
271 }
272 }
Sean Callananbb715f92011-10-29 02:28:18 +0000273
274 if (log)
275 {
276 log->Printf(" [CTD] After:");
277 ASTDumper dumper((Decl*)tag_decl);
278 dumper.ToLog(log, " [CTD] ");
279 }
Greg Claytonb01000f2011-01-17 03:46:26 +0000280}
281
282void
Sean Callananbb715f92011-10-29 02:28:18 +0000283ClangASTSource::CompleteType (clang::ObjCInterfaceDecl *interface_decl)
284{
285 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
286
287 if (log)
288 {
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000289 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 +0000290 log->Printf(" [COID] Before:");
291 ASTDumper dumper((Decl*)interface_decl);
292 dumper.ToLog(log, " [COID] ");
293 }
294
295 m_ast_importer->CompleteObjCInterfaceDecl (interface_decl);
296
297 if (log)
298 {
299 log->Printf(" [COID] After:");
300 ASTDumper dumper((Decl*)interface_decl);
301 dumper.ToLog(log, " [COID] ");
302 }
Greg Claytonb01000f2011-01-17 03:46:26 +0000303}
304
Sean Callanan9b6898f2011-07-30 02:42:06 +0000305clang::ExternalLoadResult
Sean Callananbb715f92011-10-29 02:28:18 +0000306ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context,
307 bool (*predicate)(Decl::Kind),
308 llvm::SmallVectorImpl<Decl*> &decls)
309{
310 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
311
312 const Decl *context_decl = dyn_cast<Decl>(decl_context);
313
314 if (!context_decl)
315 return ELR_Failure;
316
317 static unsigned int invocation_id = 0;
318 unsigned int current_id = invocation_id++;
319
320 if (log)
321 {
322 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000323 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in '%s' (%sDecl*)%p with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000324 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000325 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000326 context_named_decl->getNameAsString().c_str(),
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000327 context_decl->getDeclKindName(),
328 context_decl,
Sean Callananbb715f92011-10-29 02:28:18 +0000329 (predicate ? "non-null" : "null"));
330 else if(context_decl)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000331 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in (%sDecl*)%p with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000332 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000333 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000334 context_decl->getDeclKindName(),
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000335 context_decl,
Sean Callananbb715f92011-10-29 02:28:18 +0000336 (predicate ? "non-null" : "null"));
337 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000338 log->Printf("FindExternalLexicalDecls[%u] on (ASTContext*)%p in a NULL context with %s predicate",
Sean Callananbb715f92011-10-29 02:28:18 +0000339 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000340 m_ast_context,
Sean Callananbb715f92011-10-29 02:28:18 +0000341 (predicate ? "non-null" : "null"));
342 }
343
344 Decl *original_decl = NULL;
345 ASTContext *original_ctx = NULL;
346
347 if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx))
348 return ELR_Failure;
349
350 if (log)
351 {
352 log->Printf(" FELD[%u] Original decl:", current_id);
353 ASTDumper(original_decl).ToLog(log, " ");
354 }
355
356 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
357 {
358 ExternalASTSource *external_source = original_ctx->getExternalSource();
359
360 if (external_source)
361 external_source->CompleteType (original_tag_decl);
362 }
363
Sean Callananb2027ec2011-12-08 23:45:45 +0000364 const DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl);
Sean Callananbb715f92011-10-29 02:28:18 +0000365
366 if (!original_decl_context)
367 return ELR_Failure;
368
369 for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
370 iter != original_decl_context->decls_end();
371 ++iter)
372 {
373 Decl *decl = *iter;
374
375 if (!predicate || predicate(decl->getKind()))
376 {
377 if (log)
378 {
379 ASTDumper ast_dumper(decl);
380 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
381 log->Printf(" FELD[%d] Adding [to %s] lexical decl %s", current_id, context_named_decl->getNameAsString().c_str(), ast_dumper.GetCString());
382 else
383 log->Printf(" FELD[%d] Adding lexical decl %s", current_id, ast_dumper.GetCString());
384 }
385
Sean Callanan4938bd62011-11-16 18:20:47 +0000386 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, original_ctx, decl);
Sean Callananbb715f92011-10-29 02:28:18 +0000387
388 decls.push_back(copied_decl);
389 }
390 }
391
392 return ELR_AlreadyLoaded;
Chris Lattner24943d22010-06-08 16:52:24 +0000393}
394
Sean Callanan9394b5a2011-10-29 19:50:43 +0000395void
396ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
397{
398 assert (m_ast_context);
399
400 const ConstString name(context.m_decl_name.getAsString().c_str());
401
402 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
403
404 static unsigned int invocation_id = 0;
405 unsigned int current_id = invocation_id++;
406
407 if (log)
408 {
409 if (!context.m_decl_context)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000410 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 +0000411 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000412 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 +0000413 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000414 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 +0000415 }
416
417 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
418
419 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
420 {
421 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
422
423 if (log && log->GetVerbose())
424 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
425 current_id,
426 namespace_map.get(),
427 (int)namespace_map->size());
428
429 if (!namespace_map)
430 return;
431
432 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
433 i != e;
434 ++i)
435 {
436 if (log)
437 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
438 current_id,
439 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
440 i->first->GetFileSpec().GetFilename().GetCString());
441
442 FindExternalVisibleDecls(context,
443 i->first,
444 i->second,
445 current_id);
446 }
447 }
Sean Callanand3812fa2011-11-15 21:50:18 +0000448 else if (isa<ObjCInterfaceDecl>(context.m_decl_context))
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000449 {
450 FindObjCPropertyDecls(context);
451 }
Sean Callanan9394b5a2011-10-29 19:50:43 +0000452 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
453 {
454 // we shouldn't be getting FindExternalVisibleDecls calls for these
455 return;
456 }
457 else
458 {
459 ClangNamespaceDecl namespace_decl;
460
461 if (log)
462 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
463
464 FindExternalVisibleDecls(context,
465 lldb::ModuleSP(),
466 namespace_decl,
467 current_id);
468 }
469
470 if (!context.m_namespace_map->empty())
471 {
472 if (log && log->GetVerbose())
473 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
474 current_id,
475 context.m_namespace_map.get(),
476 (int)context.m_namespace_map->size());
477
478 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
479
480 if (clang_namespace_decl)
481 clang_namespace_decl->setHasExternalVisibleStorage();
482 }
483}
484
485void
486ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
487 lldb::ModuleSP module_sp,
488 ClangNamespaceDecl &namespace_decl,
489 unsigned int current_id)
490{
491 assert (m_ast_context);
492
493 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
494
495 SymbolContextList sc_list;
496
497 const ConstString name(context.m_decl_name.getAsString().c_str());
498
499 const char *name_unique_cstr = name.GetCString();
500
501 if (name_unique_cstr == NULL)
502 return;
503
504 // The ClangASTSource is not responsible for finding $-names.
505 if (name_unique_cstr[0] == '$')
506 return;
507
508 if (module_sp && namespace_decl)
509 {
510 ClangNamespaceDecl found_namespace_decl;
511
512 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
513
514 if (symbol_vendor)
515 {
516 SymbolContext null_sc;
517
518 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
519
520 if (found_namespace_decl)
521 {
522 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
523
524 if (log)
525 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
526 current_id,
527 name.GetCString(),
528 module_sp->GetFileSpec().GetFilename().GetCString());
529 }
530 }
531 }
532 else
533 {
534 ModuleList &images = m_target->GetImages();
535
536 for (uint32_t i = 0, e = images.GetSize();
537 i != e;
538 ++i)
539 {
540 lldb::ModuleSP image = images.GetModuleAtIndex(i);
541
542 if (!image)
543 continue;
544
545 ClangNamespaceDecl found_namespace_decl;
546
547 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
548
549 if (!symbol_vendor)
550 continue;
551
552 SymbolContext null_sc;
553
554 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
555
556 if (found_namespace_decl)
557 {
558 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
559
560 if (log)
561 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
562 current_id,
563 name.GetCString(),
564 image->GetFileSpec().GetFilename().GetCString());
565 }
566 }
567 }
568
569 static ConstString id_name("id");
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000570 static ConstString Class_name("Class");
Sean Callanan9394b5a2011-10-29 19:50:43 +0000571
572 do
573 {
574 TypeList types;
575 SymbolContext null_sc;
576
Sean Callanan0f71d192011-12-19 19:38:39 +0000577 if (name == id_name || name == Class_name)
578 break;
579
Sean Callanan9394b5a2011-10-29 19:50:43 +0000580 if (module_sp && namespace_decl)
581 module_sp->FindTypes(null_sc, name, &namespace_decl, true, 1, types);
Sean Callanan0f71d192011-12-19 19:38:39 +0000582 else
Sean Callanane1301a62011-12-06 03:41:14 +0000583 m_target->GetImages().FindTypes(null_sc, name, true, 1, types);
Sean Callanan9394b5a2011-10-29 19:50:43 +0000584
585 if (types.GetSize())
586 {
587 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
588
589 if (log)
590 {
591 const char *name_string = type_sp->GetName().GetCString();
592
593 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
594 current_id,
595 name.GetCString(),
596 (name_string ? name_string : "<anonymous>"));
597 }
598
Sean Callanane1301a62011-12-06 03:41:14 +0000599
Sean Callanan9394b5a2011-10-29 19:50:43 +0000600 void *copied_type = GuardedCopyType(m_ast_context, type_sp->GetClangAST(), type_sp->GetClangFullType());
Sean Callanane1301a62011-12-06 03:41:14 +0000601
Sean Callanandc5fce12011-12-01 21:04:37 +0000602 if (!copied_type)
603 {
604 if (log)
Sean Callanane1301a62011-12-06 03:41:14 +0000605 log->Printf(" CAS::FEVD[%u] - Couldn't export the type for a constant integer result",
606 current_id);
607
Sean Callanandc5fce12011-12-01 21:04:37 +0000608 break;
609 }
610
Sean Callanan9394b5a2011-10-29 19:50:43 +0000611 context.AddTypeDecl(copied_type);
612 }
Sean Callanan673f3db2011-11-30 22:11:59 +0000613
Sean Callanan9394b5a2011-10-29 19:50:43 +0000614 } while(0);
615}
616
Sean Callanan9b714842011-11-09 19:33:21 +0000617void
618ClangASTSource::FindObjCMethodDecls (NameSearchContext &context)
619{
620 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
621
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000622 static unsigned int invocation_id = 0;
623 unsigned int current_id = invocation_id++;
624
Sean Callanan9b714842011-11-09 19:33:21 +0000625 const DeclarationName &decl_name(context.m_decl_name);
626 const DeclContext *decl_ctx(context.m_decl_context);
627
628 const ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl_ctx);
629
630 if (!interface_decl)
631 return;
632
633 StreamString ss;
Sean Callanane1301a62011-12-06 03:41:14 +0000634
Sean Callanan9b714842011-11-09 19:33:21 +0000635 if (decl_name.isObjCZeroArgSelector())
636 {
637 ss.Printf("%s", decl_name.getAsString().c_str());
638 }
639 else if (decl_name.isObjCOneArgSelector())
640 {
Sean Callanan1d9ffe22011-11-14 18:29:46 +0000641 ss.Printf("%s", decl_name.getAsString().c_str());
Sean Callanan9b714842011-11-09 19:33:21 +0000642 }
643 else
644 {
645 clang::Selector sel = decl_name.getObjCSelector();
646
647 for (unsigned i = 0, e = sel.getNumArgs();
648 i != e;
649 ++i)
650 {
651 llvm::StringRef r = sel.getNameForSlot(i);
652 ss.Printf("%s:", r.str().c_str());
653 }
654 }
655 ss.Flush();
656
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000657 ConstString selector_name(ss.GetData());
658
Sean Callanan9b714842011-11-09 19:33:21 +0000659 if (log)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000660 log->Printf("ClangASTSource::FindObjCMethodDecls[%d] on (ASTContext*)%p for selector [%s %s]",
661 current_id,
662 m_ast_context,
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000663 interface_decl->getNameAsString().c_str(),
664 selector_name.AsCString());
665
Sean Callanane1301a62011-12-06 03:41:14 +0000666 ClangASTImporter::ObjCInterfaceMapSP interface_map = m_ast_importer->GetObjCInterfaceMap(interface_decl);
667
668 if (interface_map)
669 {
670 for (ClangASTImporter::ObjCInterfaceMap::iterator i = interface_map->begin(), e = interface_map->end();
671 i != e;
672 ++i)
673 {
674 lldb::clang_type_t backing_type = i->GetOpaqueQualType();
675
676 if (!backing_type)
677 continue;
678
679 QualType backing_qual_type = QualType::getFromOpaquePtr(backing_type);
680
Sean Callanan21f2e192011-12-14 01:13:04 +0000681 const ObjCInterfaceType *backing_interface_type = backing_qual_type.getTypePtr()->getAs<ObjCInterfaceType>();
Sean Callanane1301a62011-12-06 03:41:14 +0000682
683 if (!backing_interface_type)
684 continue;
685
686 const ObjCInterfaceDecl *backing_interface_decl = backing_interface_type->getDecl();
687
688 if (!backing_interface_decl)
689 continue;
690
691 if (backing_interface_decl->decls_begin() == backing_interface_decl->decls_end())
692 continue; // don't waste time creating a DeclarationName here
693
694 clang::ASTContext &backing_ast_context = backing_interface_decl->getASTContext();
695
696 llvm::SmallVector<clang::IdentifierInfo *, 3> selector_components;
Sean Callanan3d8540a2011-12-07 22:39:39 +0000697 int num_arguments = 0;
Sean Callanane1301a62011-12-06 03:41:14 +0000698
699 if (decl_name.isObjCZeroArgSelector())
700 {
701 selector_components.push_back (&backing_ast_context.Idents.get(decl_name.getAsString().c_str()));
702 }
703 else if (decl_name.isObjCOneArgSelector())
704 {
705 selector_components.push_back (&backing_ast_context.Idents.get(decl_name.getAsString().c_str()));
Sean Callanan3d8540a2011-12-07 22:39:39 +0000706 num_arguments = 1;
Sean Callanane1301a62011-12-06 03:41:14 +0000707 }
708 else
709 {
710 clang::Selector sel = decl_name.getObjCSelector();
711
712 for (unsigned i = 0, e = sel.getNumArgs();
713 i != e;
714 ++i)
715 {
716 llvm::StringRef r = sel.getNameForSlot(i);
717
718 selector_components.push_back (&backing_ast_context.Idents.get(r.str().c_str()));
Sean Callanan3d8540a2011-12-07 22:39:39 +0000719 num_arguments++;
Sean Callanane1301a62011-12-06 03:41:14 +0000720 }
721 }
722
Sean Callanan3d8540a2011-12-07 22:39:39 +0000723 Selector backing_selector = backing_interface_decl->getASTContext().Selectors.getSelector(num_arguments, selector_components.data());
Sean Callanane1301a62011-12-06 03:41:14 +0000724 DeclarationName backing_decl_name = DeclarationName(backing_selector);
725
726 DeclContext::lookup_const_result lookup_result = backing_interface_decl->lookup(backing_decl_name);
727
728 if (lookup_result.first == lookup_result.second)
729 continue;
730
731 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(*lookup_result.first);
732
733 if (!method_decl)
734 continue;
735
736 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &backing_ast_context, *lookup_result.first);
737
Sean Callanan3d8540a2011-12-07 22:39:39 +0000738 if (!copied_decl)
739 {
Greg Clayton2e87f6a2011-12-08 01:32:28 +0000740 if (log)
741 log->Printf(" CAS::FOMD[%d] couldn't import method from symbols", current_id);
Sean Callanan3d8540a2011-12-07 22:39:39 +0000742 continue;
743 }
744
Sean Callanane1301a62011-12-06 03:41:14 +0000745 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl> (copied_decl);
746
747 if (!copied_method_decl)
748 continue;
749
750 if (log)
751 {
752 ASTDumper dumper((Decl*)copied_method_decl);
753 log->Printf(" CAS::FOMD[%d] found (in symbols) %s", current_id, dumper.GetCString());
754 }
755
756 context.AddNamedDecl(copied_method_decl);
757 }
758 }
759
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000760 SymbolContextList sc_list;
761
762 const bool include_symbols = false;
763 const bool append = false;
764
765 m_target->GetImages().FindFunctions(selector_name, lldb::eFunctionNameTypeSelector, include_symbols, append, sc_list);
766
767 for (uint32_t i = 0, e = sc_list.GetSize();
768 i != e;
769 ++i)
770 {
771 SymbolContext sc;
772
773 if (!sc_list.GetContextAtIndex(i, sc))
774 continue;
775
776 if (!sc.function)
777 continue;
778
779 DeclContext *function_ctx = sc.function->GetClangDeclContext();
780
781 if (!function_ctx)
782 continue;
783
784 ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(function_ctx);
785
786 if (!method_decl)
787 continue;
788
789 ObjCInterfaceDecl *found_interface_decl = method_decl->getClassInterface();
790
791 if (!found_interface_decl)
792 continue;
793
794 if (found_interface_decl->getName() == interface_decl->getName())
795 {
Sean Callanan4938bd62011-11-16 18:20:47 +0000796 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, &method_decl->getASTContext(), method_decl);
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000797
798 if (!copied_decl)
799 continue;
800
801 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
802
803 if (!copied_method_decl)
804 continue;
805
806 if (log)
807 {
808 ASTDumper dumper((Decl*)copied_method_decl);
Sean Callanane1301a62011-12-06 03:41:14 +0000809 log->Printf(" CAS::FOMD[%d] found (in debug info) %s", current_id, dumper.GetCString());
Sean Callananf2a0a5c2011-11-11 20:37:26 +0000810 }
811
812 context.AddNamedDecl(copied_method_decl);
813 }
814 }
Sean Callanan9b714842011-11-09 19:33:21 +0000815}
816
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000817void
818ClangASTSource::FindObjCPropertyDecls (NameSearchContext &context)
819{
820 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
821
822 static unsigned int invocation_id = 0;
823 unsigned int current_id = invocation_id++;
824
825 const ObjCInterfaceDecl *iface_decl = cast<ObjCInterfaceDecl>(context.m_decl_context);
826 Decl *orig_decl;
827 ASTContext *orig_ast_ctx;
828
829 m_ast_importer->ResolveDeclOrigin(iface_decl, &orig_decl, &orig_ast_ctx);
830
831 if (!orig_decl)
832 return;
833
834 ObjCInterfaceDecl *orig_iface_decl = dyn_cast<ObjCInterfaceDecl>(orig_decl);
835
836 if (!orig_iface_decl)
837 return;
838
839 if (!ClangASTContext::GetCompleteDecl(orig_ast_ctx, orig_iface_decl))
840 return;
841
842 std::string property_name_str = context.m_decl_name.getAsString();
843 StringRef property_name(property_name_str.c_str());
844 ObjCPropertyDecl *property_decl = orig_iface_decl->FindPropertyDeclaration(&orig_ast_ctx->Idents.get(property_name));
845
846 if (log)
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000847 log->Printf("ClangASTSource::FindObjCPropertyDecls[%d] on (ASTContext*)%p for property '%s.%s'",
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000848 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000849 m_ast_context,
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000850 iface_decl->getNameAsString().c_str(),
851 property_name_str.c_str());
852
853 if (!property_decl)
854 return;
855
Sean Callanan4938bd62011-11-16 18:20:47 +0000856 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, orig_ast_ctx, property_decl);
Sean Callanane6ea5fe2011-11-15 02:11:17 +0000857
858 if (!copied_decl)
859 return;
860
861 ObjCPropertyDecl *copied_property_decl = dyn_cast<ObjCPropertyDecl>(copied_decl);
862
863 if (!copied_property_decl)
864 return;
865
866 if (log)
867 {
868 ASTDumper dumper((Decl*)copied_property_decl);
869 log->Printf(" CAS::FOPD[%d] found %s", current_id, dumper.GetCString());
870 }
871
872 context.AddNamedDecl(copied_property_decl);
873}
874
Sean Callanan73b520f2011-10-29 01:58:46 +0000875void
876ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
877 const ConstString &name,
878 ClangASTImporter::NamespaceMapSP &parent_map) const
879{
880 static unsigned int invocation_id = 0;
881 unsigned int current_id = invocation_id++;
882
883 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
884
885 if (log)
886 {
887 if (parent_map && parent_map->size())
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000888 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s in namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +0000889 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000890 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +0000891 name.GetCString(),
892 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
893 else
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000894 log->Printf("CompleteNamespaceMap[%u] on (ASTContext*)%p Searching for namespace %s",
Sean Callanan73b520f2011-10-29 01:58:46 +0000895 current_id,
Sean Callanan5a55c7a2011-11-18 03:28:09 +0000896 m_ast_context,
Sean Callanan73b520f2011-10-29 01:58:46 +0000897 name.GetCString());
898 }
899
900
901 if (parent_map)
902 {
903 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
904 i != e;
905 ++i)
906 {
907 ClangNamespaceDecl found_namespace_decl;
908
909 lldb::ModuleSP module_sp = i->first;
910 ClangNamespaceDecl module_parent_namespace_decl = i->second;
911
912 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
913
914 if (!symbol_vendor)
915 continue;
916
917 SymbolContext null_sc;
918
919 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
920
921 if (!found_namespace_decl)
922 continue;
923
924 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
925
926 if (log)
927 log->Printf(" CMN[%u] Found namespace %s in module %s",
928 current_id,
929 name.GetCString(),
930 module_sp->GetFileSpec().GetFilename().GetCString());
931 }
932 }
933 else
934 {
935 ModuleList &images = m_target->GetImages();
936 ClangNamespaceDecl null_namespace_decl;
937
938 for (uint32_t i = 0, e = images.GetSize();
939 i != e;
940 ++i)
941 {
942 lldb::ModuleSP image = images.GetModuleAtIndex(i);
943
944 if (!image)
945 continue;
946
947 ClangNamespaceDecl found_namespace_decl;
948
949 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
950
951 if (!symbol_vendor)
952 continue;
953
954 SymbolContext null_sc;
955
956 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
957
958 if (!found_namespace_decl)
959 continue;
960
961 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
962
963 if (log)
964 log->Printf(" CMN[%u] Found namespace %s in module %s",
965 current_id,
966 name.GetCString(),
967 image->GetFileSpec().GetFilename().GetCString());
968 }
969 }
970}
971
Sean Callanane1301a62011-12-06 03:41:14 +0000972void
973ClangASTSource::CompleteObjCInterfaceMap (ClangASTImporter::ObjCInterfaceMapSP &objc_interface_map,
974 const ConstString &name) const
975{
976 SymbolContext null_sc;
977
978 TypeList types;
979
980 m_target->GetImages().FindTypes(null_sc, name, true, UINT32_MAX, types);
981
982 for (uint32_t i = 0, e = types.GetSize();
983 i != e;
984 ++i)
985 {
986 lldb::TypeSP mapped_type_sp = types.GetTypeAtIndex(i);
987
988 if (!mapped_type_sp || !mapped_type_sp->GetClangFullType())
989 continue;
990
991 objc_interface_map->push_back (ClangASTType(mapped_type_sp->GetClangAST(), mapped_type_sp->GetClangFullType()));
992 }
993}
994
Sean Callananbb715f92011-10-29 02:28:18 +0000995NamespaceDecl *
996ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
997{
998 if (namespace_decls.empty())
999 return NULL;
1000
1001 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1002
1003 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
1004
Sean Callanan4938bd62011-11-16 18:20:47 +00001005 Decl *copied_decl = m_ast_importer->CopyDecl(m_ast_context, namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
Sean Callananbb715f92011-10-29 02:28:18 +00001006
1007 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1008
1009 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
1010
1011 return dyn_cast<NamespaceDecl>(copied_decl);
1012}
1013
Sean Callanan9394b5a2011-10-29 19:50:43 +00001014void *
1015ClangASTSource::GuardedCopyType (ASTContext *dest_context,
1016 ASTContext *source_context,
1017 void *clang_type)
1018{
1019 SetImportInProgress(true);
1020
Sean Callanan4938bd62011-11-16 18:20:47 +00001021 QualType ret_qual_type = m_ast_importer->CopyType (m_ast_context, source_context, QualType::getFromOpaquePtr(clang_type));
Sean Callanan9394b5a2011-10-29 19:50:43 +00001022
1023 void *ret = ret_qual_type.getAsOpaquePtr();
1024
1025 SetImportInProgress(false);
1026
1027 return ret;
1028}
1029
Greg Claytonb01000f2011-01-17 03:46:26 +00001030clang::NamedDecl *
1031NameSearchContext::AddVarDecl(void *type)
1032{
Greg Clayton8de27c72010-10-15 22:48:33 +00001033 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +00001034
1035 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +00001036
Sean Callananf76afff2011-10-28 23:38:38 +00001037 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +00001038 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +00001039 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +00001040 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +00001041 ii,
Chris Lattner24943d22010-06-08 16:52:24 +00001042 QualType::getFromOpaquePtr(type),
1043 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +00001044 SC_Static,
1045 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +00001046 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +00001047
1048 return Decl;
1049}
Sean Callanan8f0dc342010-06-22 23:46:24 +00001050
Greg Claytonb01000f2011-01-17 03:46:26 +00001051clang::NamedDecl *
1052NameSearchContext::AddFunDecl (void *type)
1053{
Sean Callananf76afff2011-10-28 23:38:38 +00001054 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +00001055 const_cast<DeclContext*>(m_decl_context),
1056 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +00001057 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +00001058 m_decl_name.getAsIdentifierInfo(),
1059 QualType::getFromOpaquePtr(type),
1060 NULL,
1061 SC_Static,
1062 SC_Static,
1063 false,
1064 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +00001065
Sean Callananb291abe2010-08-12 23:45:38 +00001066 // We have to do more than just synthesize the FunctionDecl. We have to
1067 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
1068 // this, we raid the function's FunctionProtoType for types.
1069
Greg Clayton8de27c72010-10-15 22:48:33 +00001070 QualType qual_type (QualType::getFromOpaquePtr(type));
Sean Callanan21f2e192011-12-14 01:13:04 +00001071 const FunctionProtoType *func_proto_type = qual_type.getTypePtr()->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +00001072
Greg Clayton8de27c72010-10-15 22:48:33 +00001073 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +00001074 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001075 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +00001076 unsigned ArgIndex;
1077
Sean Callananc1535182011-10-07 23:18:13 +00001078 SmallVector<ParmVarDecl *, 5> parm_var_decls;
1079
Sean Callanan8f0dc342010-06-22 23:46:24 +00001080 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
1081 {
Greg Clayton8de27c72010-10-15 22:48:33 +00001082 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001083
Sean Callananf76afff2011-10-28 23:38:38 +00001084 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callananc1535182011-10-07 23:18:13 +00001085 const_cast<DeclContext*>(m_decl_context),
1086 SourceLocation(),
1087 SourceLocation(),
1088 NULL,
1089 arg_qual_type,
1090 NULL,
1091 SC_Static,
1092 SC_Static,
1093 NULL));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001094 }
1095
Sean Callananc1535182011-10-07 23:18:13 +00001096 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan8f0dc342010-06-22 23:46:24 +00001097 }
Sean Callanandc5fce12011-12-01 21:04:37 +00001098 else
1099 {
1100 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1101
1102 log->Printf("Function type wasn't a FunctionProtoType");
1103 }
Sean Callanan8f0dc342010-06-22 23:46:24 +00001104
Greg Clayton8de27c72010-10-15 22:48:33 +00001105 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +00001106
Greg Clayton8de27c72010-10-15 22:48:33 +00001107 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +00001108}
Sean Callanan0fc73582010-07-27 00:55:47 +00001109
Greg Claytonb01000f2011-01-17 03:46:26 +00001110clang::NamedDecl *
1111NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +00001112{
Sean Callananad293092011-01-18 23:32:05 +00001113 FunctionProtoType::ExtProtoInfo proto_info;
1114
1115 proto_info.Variadic = true;
1116
Sean Callananf76afff2011-10-28 23:38:38 +00001117 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
1118 NULL, // argument types
1119 0, // number of arguments
1120 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +00001121
Sean Callanan0fc73582010-07-27 00:55:47 +00001122 return AddFunDecl(generic_function_type.getAsOpaquePtr());
1123}
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001124
Greg Claytonb01000f2011-01-17 03:46:26 +00001125clang::NamedDecl *
1126NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001127{
Greg Claytona1aaaff2011-01-23 00:34:52 +00001128 if (type)
1129 {
1130 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001131
Sean Callanan21f2e192011-12-14 01:13:04 +00001132 if (const TagType *tag_type = qual_type->getAs<TagType>())
Greg Claytona1aaaff2011-01-23 00:34:52 +00001133 {
1134 TagDecl *tag_decl = tag_type->getDecl();
1135
1136 m_decls.push_back(tag_decl);
1137
1138 return tag_decl;
1139 }
Sean Callanan21f2e192011-12-14 01:13:04 +00001140 else if (const ObjCObjectType *objc_object_type = qual_type->getAs<ObjCObjectType>())
Greg Claytona1aaaff2011-01-23 00:34:52 +00001141 {
1142 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
1143
1144 m_decls.push_back((NamedDecl*)interface_decl);
1145
1146 return (NamedDecl*)interface_decl;
1147 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001148 }
Greg Claytona1aaaff2011-01-23 00:34:52 +00001149 return NULL;
Sean Callanan93a4b1a2010-08-04 01:02:13 +00001150}
Greg Claytone6d72ca2011-06-25 00:44:06 +00001151
1152void
1153NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
1154{
1155 for (clang::NamedDecl * const *decl_iterator = result.first;
1156 decl_iterator != result.second;
1157 ++decl_iterator)
1158 m_decls.push_back (*decl_iterator);
1159}
1160
1161void
1162NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
1163{
1164 m_decls.push_back (decl);
1165}