blob: 5252eecf879e125362f5ba559b405e8c93d00319 [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"
20#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021
22using namespace clang;
23using namespace lldb_private;
24
Greg Claytonb01000f2011-01-17 03:46:26 +000025ClangASTSource::~ClangASTSource()
26{
27}
Chris Lattner24943d22010-06-08 16:52:24 +000028
Greg Claytonb01000f2011-01-17 03:46:26 +000029void
30ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer)
31{
Sean Callananf76afff2011-10-28 23:38:38 +000032 if (!m_ast_context)
33 return;
34
35 m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage();
36 m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage();
Chris Lattner24943d22010-06-08 16:52:24 +000037}
38
Chris Lattner24943d22010-06-08 16:52:24 +000039// The core lookup interface.
Greg Claytonb01000f2011-01-17 03:46:26 +000040DeclContext::lookup_result
41ClangASTSource::FindExternalVisibleDeclsByName
Greg Claytonf4c7ae02010-10-15 03:36:13 +000042(
43 const DeclContext *decl_ctx,
Greg Clayton8de27c72010-10-15 22:48:33 +000044 DeclarationName clang_decl_name
Greg Claytonf4c7ae02010-10-15 03:36:13 +000045)
46{
Sean Callananf76afff2011-10-28 23:38:38 +000047 if (!m_ast_context)
48 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
49
50 if (GetImportInProgress())
Greg Claytonb01000f2011-01-17 03:46:26 +000051 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
52
53 std::string decl_name (clang_decl_name.getAsString());
54
55// if (m_decl_map.DoingASTImport ())
56// return DeclContext::lookup_result();
57//
Greg Clayton8de27c72010-10-15 22:48:33 +000058 switch (clang_decl_name.getNameKind()) {
Chris Lattner24943d22010-06-08 16:52:24 +000059 // Normal identifiers.
60 case DeclarationName::Identifier:
Greg Clayton8de27c72010-10-15 22:48:33 +000061 if (clang_decl_name.getAsIdentifierInfo()->getBuiltinID() != 0)
62 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
63 break;
Chris Lattner24943d22010-06-08 16:52:24 +000064
65 // Operator names. Not important for now.
66 case DeclarationName::CXXOperatorName:
67 case DeclarationName::CXXLiteralOperatorName:
68 return DeclContext::lookup_result();
69
70 // Using directives found in this context.
71 // Tell Sema we didn't find any or we'll end up getting asked a *lot*.
72 case DeclarationName::CXXUsingDirective:
Greg Clayton8de27c72010-10-15 22:48:33 +000073 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
Chris Lattner24943d22010-06-08 16:52:24 +000074
75 // These aren't looked up like this.
76 case DeclarationName::ObjCZeroArgSelector:
77 case DeclarationName::ObjCOneArgSelector:
78 case DeclarationName::ObjCMultiArgSelector:
79 return DeclContext::lookup_result();
80
81 // These aren't possible in the global context.
82 case DeclarationName::CXXConstructorName:
83 case DeclarationName::CXXDestructorName:
84 case DeclarationName::CXXConversionFunctionName:
85 return DeclContext::lookup_result();
86 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +000087
Greg Claytonf4c7ae02010-10-15 03:36:13 +000088
Sean Callananf76afff2011-10-28 23:38:38 +000089 if (!GetLookupsEnabled())
Greg Clayton8de27c72010-10-15 22:48:33 +000090 {
91 // Wait until we see a '$' at the start of a name before we start doing
92 // any lookups so we can avoid lookup up all of the builtin types.
93 if (!decl_name.empty() && decl_name[0] == '$')
94 {
Sean Callananf76afff2011-10-28 23:38:38 +000095 SetLookupsEnabled (true);
Greg Clayton8de27c72010-10-15 22:48:33 +000096 }
97 else
98 {
99 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
100 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000101 }
Greg Clayton8de27c72010-10-15 22:48:33 +0000102
Greg Clayton8de27c72010-10-15 22:48:33 +0000103 ConstString const_decl_name(decl_name.c_str());
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000104
105 const char *uniqued_const_decl_name = const_decl_name.GetCString();
106 if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end())
107 {
108 // We are currently looking up this name...
109 return DeclContext::lookup_result();
110 }
111 m_active_lookups.insert(uniqued_const_decl_name);
Greg Claytona8b278a2010-11-15 01:34:18 +0000112// static uint32_t g_depth = 0;
113// ++g_depth;
114// printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000115 llvm::SmallVector<NamedDecl*, 4> name_decls;
116 NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx);
Sean Callananf76afff2011-10-28 23:38:38 +0000117 FindExternalVisibleDecls(name_search_context);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000118 DeclContext::lookup_result result (SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls));
Greg Claytona8b278a2010-11-15 01:34:18 +0000119// --g_depth;
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000120 m_active_lookups.erase (uniqued_const_decl_name);
121 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000122}
123
Greg Claytonb01000f2011-01-17 03:46:26 +0000124void
125ClangASTSource::CompleteType (TagDecl *tag_decl)
Sean Callananbb715f92011-10-29 02:28:18 +0000126{
127 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
128
129 if (log)
130 {
131 log->Printf(" [CompleteTagDecl] Completing a TagDecl named %s", tag_decl->getName().str().c_str());
132 log->Printf(" [CTD] Before:");
133 ASTDumper dumper((Decl*)tag_decl);
134 dumper.ToLog(log, " [CTD] ");
135 }
136
137 m_ast_importer->CompleteTagDecl (tag_decl);
138
139 if (log)
140 {
141 log->Printf(" [CTD] After:");
142 ASTDumper dumper((Decl*)tag_decl);
143 dumper.ToLog(log, " [CTD] ");
144 }
Greg Claytonb01000f2011-01-17 03:46:26 +0000145}
146
147void
Sean Callananbb715f92011-10-29 02:28:18 +0000148ClangASTSource::CompleteType (clang::ObjCInterfaceDecl *interface_decl)
149{
150 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
151
152 if (log)
153 {
154 log->Printf(" [CompleteObjCInterfaceDecl] Completing an ObjCInterfaceDecl named %s", interface_decl->getName().str().c_str());
155 log->Printf(" [COID] Before:");
156 ASTDumper dumper((Decl*)interface_decl);
157 dumper.ToLog(log, " [COID] ");
158 }
159
160 m_ast_importer->CompleteObjCInterfaceDecl (interface_decl);
161
162 if (log)
163 {
164 log->Printf(" [COID] After:");
165 ASTDumper dumper((Decl*)interface_decl);
166 dumper.ToLog(log, " [COID] ");
167 }
Greg Claytonb01000f2011-01-17 03:46:26 +0000168}
169
Sean Callanan9b6898f2011-07-30 02:42:06 +0000170clang::ExternalLoadResult
Sean Callananbb715f92011-10-29 02:28:18 +0000171ClangASTSource::FindExternalLexicalDecls (const DeclContext *decl_context,
172 bool (*predicate)(Decl::Kind),
173 llvm::SmallVectorImpl<Decl*> &decls)
174{
175 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
176
177 const Decl *context_decl = dyn_cast<Decl>(decl_context);
178
179 if (!context_decl)
180 return ELR_Failure;
181
182 static unsigned int invocation_id = 0;
183 unsigned int current_id = invocation_id++;
184
185 if (log)
186 {
187 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
188 log->Printf("FindExternalLexicalDecls[%u] in '%s' (a %s) with %s predicate",
189 current_id,
190 context_named_decl->getNameAsString().c_str(),
191 context_decl->getDeclKindName(),
192 (predicate ? "non-null" : "null"));
193 else if(context_decl)
194 log->Printf("FindExternalLexicalDecls[%u] in a %s with %s predicate",
195 current_id,
196 context_decl->getDeclKindName(),
197 (predicate ? "non-null" : "null"));
198 else
199 log->Printf("FindExternalLexicalDecls[%u] in a NULL context with %s predicate",
200 current_id,
201 (predicate ? "non-null" : "null"));
202 }
203
204 Decl *original_decl = NULL;
205 ASTContext *original_ctx = NULL;
206
207 if (!m_ast_importer->ResolveDeclOrigin(context_decl, &original_decl, &original_ctx))
208 return ELR_Failure;
209
210 if (log)
211 {
212 log->Printf(" FELD[%u] Original decl:", current_id);
213 ASTDumper(original_decl).ToLog(log, " ");
214 }
215
216 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl))
217 {
218 ExternalASTSource *external_source = original_ctx->getExternalSource();
219
220 if (external_source)
221 external_source->CompleteType (original_tag_decl);
222 }
223
224 DeclContext *original_decl_context = dyn_cast<DeclContext>(original_decl);
225
226 if (!original_decl_context)
227 return ELR_Failure;
228
229 for (TagDecl::decl_iterator iter = original_decl_context->decls_begin();
230 iter != original_decl_context->decls_end();
231 ++iter)
232 {
233 Decl *decl = *iter;
234
235 if (!predicate || predicate(decl->getKind()))
236 {
237 if (log)
238 {
239 ASTDumper ast_dumper(decl);
240 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
241 log->Printf(" FELD[%d] Adding [to %s] lexical decl %s", current_id, context_named_decl->getNameAsString().c_str(), ast_dumper.GetCString());
242 else
243 log->Printf(" FELD[%d] Adding lexical decl %s", current_id, ast_dumper.GetCString());
244 }
245
246 Decl *copied_decl = m_ast_importer->CopyDecl(original_ctx, decl);
247
248 decls.push_back(copied_decl);
249 }
250 }
251
252 return ELR_AlreadyLoaded;
Chris Lattner24943d22010-06-08 16:52:24 +0000253}
254
Sean Callanan9394b5a2011-10-29 19:50:43 +0000255void
256ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
257{
258 assert (m_ast_context);
259
260 const ConstString name(context.m_decl_name.getAsString().c_str());
261
262 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
263
264 static unsigned int invocation_id = 0;
265 unsigned int current_id = invocation_id++;
266
267 if (log)
268 {
269 if (!context.m_decl_context)
270 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] for '%s' in a NULL DeclContext", current_id, name.GetCString());
271 else if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context.m_decl_context))
272 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] for '%s' in '%s'", current_id, name.GetCString(), context_named_decl->getNameAsString().c_str());
273 else
274 log->Printf("ClangASTSource::FindExternalVisibleDecls[%u] for '%s' in a '%s'", current_id, name.GetCString(), context.m_decl_context->getDeclKindName());
275 }
276
277 context.m_namespace_map.reset(new ClangASTImporter::NamespaceMap);
278
279 if (const NamespaceDecl *namespace_context = dyn_cast<NamespaceDecl>(context.m_decl_context))
280 {
281 ClangASTImporter::NamespaceMapSP namespace_map = m_ast_importer->GetNamespaceMap(namespace_context);
282
283 if (log && log->GetVerbose())
284 log->Printf(" CAS::FEVD[%u] Inspecting namespace map %p (%d entries)",
285 current_id,
286 namespace_map.get(),
287 (int)namespace_map->size());
288
289 if (!namespace_map)
290 return;
291
292 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), e = namespace_map->end();
293 i != e;
294 ++i)
295 {
296 if (log)
297 log->Printf(" CAS::FEVD[%u] Searching namespace %s in module %s",
298 current_id,
299 i->second.GetNamespaceDecl()->getNameAsString().c_str(),
300 i->first->GetFileSpec().GetFilename().GetCString());
301
302 FindExternalVisibleDecls(context,
303 i->first,
304 i->second,
305 current_id);
306 }
307 }
308 else if (!isa<TranslationUnitDecl>(context.m_decl_context))
309 {
310 // we shouldn't be getting FindExternalVisibleDecls calls for these
311 return;
312 }
313 else
314 {
315 ClangNamespaceDecl namespace_decl;
316
317 if (log)
318 log->Printf(" CAS::FEVD[%u] Searching the root namespace", current_id);
319
320 FindExternalVisibleDecls(context,
321 lldb::ModuleSP(),
322 namespace_decl,
323 current_id);
324 }
325
326 if (!context.m_namespace_map->empty())
327 {
328 if (log && log->GetVerbose())
329 log->Printf(" CAS::FEVD[%u] Registering namespace map %p (%d entries)",
330 current_id,
331 context.m_namespace_map.get(),
332 (int)context.m_namespace_map->size());
333
334 NamespaceDecl *clang_namespace_decl = AddNamespace(context, context.m_namespace_map);
335
336 if (clang_namespace_decl)
337 clang_namespace_decl->setHasExternalVisibleStorage();
338 }
339}
340
341void
342ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context,
343 lldb::ModuleSP module_sp,
344 ClangNamespaceDecl &namespace_decl,
345 unsigned int current_id)
346{
347 assert (m_ast_context);
348
349 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
350
351 SymbolContextList sc_list;
352
353 const ConstString name(context.m_decl_name.getAsString().c_str());
354
355 const char *name_unique_cstr = name.GetCString();
356
357 if (name_unique_cstr == NULL)
358 return;
359
360 // The ClangASTSource is not responsible for finding $-names.
361 if (name_unique_cstr[0] == '$')
362 return;
363
364 if (module_sp && namespace_decl)
365 {
366 ClangNamespaceDecl found_namespace_decl;
367
368 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
369
370 if (symbol_vendor)
371 {
372 SymbolContext null_sc;
373
374 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
375
376 if (found_namespace_decl)
377 {
378 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
379
380 if (log)
381 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
382 current_id,
383 name.GetCString(),
384 module_sp->GetFileSpec().GetFilename().GetCString());
385 }
386 }
387 }
388 else
389 {
390 ModuleList &images = m_target->GetImages();
391
392 for (uint32_t i = 0, e = images.GetSize();
393 i != e;
394 ++i)
395 {
396 lldb::ModuleSP image = images.GetModuleAtIndex(i);
397
398 if (!image)
399 continue;
400
401 ClangNamespaceDecl found_namespace_decl;
402
403 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
404
405 if (!symbol_vendor)
406 continue;
407
408 SymbolContext null_sc;
409
410 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &namespace_decl);
411
412 if (found_namespace_decl)
413 {
414 context.m_namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
415
416 if (log)
417 log->Printf(" CAS::FEVD[%u] Found namespace %s in module %s",
418 current_id,
419 name.GetCString(),
420 image->GetFileSpec().GetFilename().GetCString());
421 }
422 }
423 }
424
425 static ConstString id_name("id");
426
427 do
428 {
429 TypeList types;
430 SymbolContext null_sc;
431
432 if (module_sp && namespace_decl)
433 module_sp->FindTypes(null_sc, name, &namespace_decl, true, 1, types);
434 else if(name != id_name)
435 m_target->GetImages().FindTypes (null_sc, name, true, 1, types);
436 else
437 break;
438
439 if (types.GetSize())
440 {
441 lldb::TypeSP type_sp = types.GetTypeAtIndex(0);
442
443 if (log)
444 {
445 const char *name_string = type_sp->GetName().GetCString();
446
447 log->Printf(" CAS::FEVD[%u] Matching type found for \"%s\": %s",
448 current_id,
449 name.GetCString(),
450 (name_string ? name_string : "<anonymous>"));
451 }
452
453 void *copied_type = GuardedCopyType(m_ast_context, type_sp->GetClangAST(), type_sp->GetClangFullType());
454
455 context.AddTypeDecl(copied_type);
456 }
457 } while(0);
458}
459
Sean Callanan73b520f2011-10-29 01:58:46 +0000460void
461ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
462 const ConstString &name,
463 ClangASTImporter::NamespaceMapSP &parent_map) const
464{
465 static unsigned int invocation_id = 0;
466 unsigned int current_id = invocation_id++;
467
468 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
469
470 if (log)
471 {
472 if (parent_map && parent_map->size())
473 log->Printf("CompleteNamespaceMap[%u] Searching for namespace %s in namespace %s",
474 current_id,
475 name.GetCString(),
476 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
477 else
478 log->Printf("CompleteNamespaceMap[%u] Searching for namespace %s",
479 current_id,
480 name.GetCString());
481 }
482
483
484 if (parent_map)
485 {
486 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
487 i != e;
488 ++i)
489 {
490 ClangNamespaceDecl found_namespace_decl;
491
492 lldb::ModuleSP module_sp = i->first;
493 ClangNamespaceDecl module_parent_namespace_decl = i->second;
494
495 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
496
497 if (!symbol_vendor)
498 continue;
499
500 SymbolContext null_sc;
501
502 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
503
504 if (!found_namespace_decl)
505 continue;
506
507 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
508
509 if (log)
510 log->Printf(" CMN[%u] Found namespace %s in module %s",
511 current_id,
512 name.GetCString(),
513 module_sp->GetFileSpec().GetFilename().GetCString());
514 }
515 }
516 else
517 {
518 ModuleList &images = m_target->GetImages();
519 ClangNamespaceDecl null_namespace_decl;
520
521 for (uint32_t i = 0, e = images.GetSize();
522 i != e;
523 ++i)
524 {
525 lldb::ModuleSP image = images.GetModuleAtIndex(i);
526
527 if (!image)
528 continue;
529
530 ClangNamespaceDecl found_namespace_decl;
531
532 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
533
534 if (!symbol_vendor)
535 continue;
536
537 SymbolContext null_sc;
538
539 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
540
541 if (!found_namespace_decl)
542 continue;
543
544 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
545
546 if (log)
547 log->Printf(" CMN[%u] Found namespace %s in module %s",
548 current_id,
549 name.GetCString(),
550 image->GetFileSpec().GetFilename().GetCString());
551 }
552 }
553}
554
Sean Callananbb715f92011-10-29 02:28:18 +0000555NamespaceDecl *
556ClangASTSource::AddNamespace (NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
557{
558 if (namespace_decls.empty())
559 return NULL;
560
561 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
562
563 const ClangNamespaceDecl &namespace_decl = namespace_decls->begin()->second;
564
565 Decl *copied_decl = m_ast_importer->CopyDecl(namespace_decl.GetASTContext(), namespace_decl.GetNamespaceDecl());
566
567 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
568
569 m_ast_importer->RegisterNamespaceMap(copied_namespace_decl, namespace_decls);
570
571 return dyn_cast<NamespaceDecl>(copied_decl);
572}
573
Sean Callanan9394b5a2011-10-29 19:50:43 +0000574void *
575ClangASTSource::GuardedCopyType (ASTContext *dest_context,
576 ASTContext *source_context,
577 void *clang_type)
578{
579 SetImportInProgress(true);
580
581 QualType ret_qual_type = m_ast_importer->CopyType (source_context, QualType::getFromOpaquePtr(clang_type));
582
583 void *ret = ret_qual_type.getAsOpaquePtr();
584
585 SetImportInProgress(false);
586
587 return ret;
588}
589
Greg Claytonb01000f2011-01-17 03:46:26 +0000590clang::NamedDecl *
591NameSearchContext::AddVarDecl(void *type)
592{
Greg Clayton8de27c72010-10-15 22:48:33 +0000593 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +0000594
595 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +0000596
Sean Callananf76afff2011-10-28 23:38:38 +0000597 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000598 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +0000599 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000600 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000601 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000602 QualType::getFromOpaquePtr(type),
603 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000604 SC_Static,
605 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +0000606 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000607
608 return Decl;
609}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000610
Greg Claytonb01000f2011-01-17 03:46:26 +0000611clang::NamedDecl *
612NameSearchContext::AddFunDecl (void *type)
613{
Sean Callananf76afff2011-10-28 23:38:38 +0000614 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000615 const_cast<DeclContext*>(m_decl_context),
616 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000617 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +0000618 m_decl_name.getAsIdentifierInfo(),
619 QualType::getFromOpaquePtr(type),
620 NULL,
621 SC_Static,
622 SC_Static,
623 false,
624 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000625
Sean Callananb291abe2010-08-12 23:45:38 +0000626 // We have to do more than just synthesize the FunctionDecl. We have to
627 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
628 // this, we raid the function's FunctionProtoType for types.
629
Greg Clayton8de27c72010-10-15 22:48:33 +0000630 QualType qual_type (QualType::getFromOpaquePtr(type));
631 const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000632
Greg Clayton8de27c72010-10-15 22:48:33 +0000633 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +0000634 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000635 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000636 unsigned ArgIndex;
637
Sean Callananc1535182011-10-07 23:18:13 +0000638 SmallVector<ParmVarDecl *, 5> parm_var_decls;
639
Sean Callanan8f0dc342010-06-22 23:46:24 +0000640 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
641 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000642 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000643
Sean Callananf76afff2011-10-28 23:38:38 +0000644 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callananc1535182011-10-07 23:18:13 +0000645 const_cast<DeclContext*>(m_decl_context),
646 SourceLocation(),
647 SourceLocation(),
648 NULL,
649 arg_qual_type,
650 NULL,
651 SC_Static,
652 SC_Static,
653 NULL));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000654 }
655
Sean Callananc1535182011-10-07 23:18:13 +0000656 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000657 }
658
Greg Clayton8de27c72010-10-15 22:48:33 +0000659 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000660
Greg Clayton8de27c72010-10-15 22:48:33 +0000661 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000662}
Sean Callanan0fc73582010-07-27 00:55:47 +0000663
Greg Claytonb01000f2011-01-17 03:46:26 +0000664clang::NamedDecl *
665NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +0000666{
Sean Callananad293092011-01-18 23:32:05 +0000667 FunctionProtoType::ExtProtoInfo proto_info;
668
669 proto_info.Variadic = true;
670
Sean Callananf76afff2011-10-28 23:38:38 +0000671 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
672 NULL, // argument types
673 0, // number of arguments
674 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +0000675
Sean Callanan0fc73582010-07-27 00:55:47 +0000676 return AddFunDecl(generic_function_type.getAsOpaquePtr());
677}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000678
Greg Claytonb01000f2011-01-17 03:46:26 +0000679clang::NamedDecl *
680NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000681{
Greg Claytona1aaaff2011-01-23 00:34:52 +0000682 if (type)
683 {
684 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000685
Sean Callanand5b3c352011-01-27 04:42:51 +0000686 if (const TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000687 {
688 TagDecl *tag_decl = tag_type->getDecl();
689
690 m_decls.push_back(tag_decl);
691
692 return tag_decl;
693 }
Sean Callanand5b3c352011-01-27 04:42:51 +0000694 else if (const ObjCObjectType *objc_object_type = dyn_cast<clang::ObjCObjectType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000695 {
696 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
697
698 m_decls.push_back((NamedDecl*)interface_decl);
699
700 return (NamedDecl*)interface_decl;
701 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000702 }
Greg Claytona1aaaff2011-01-23 00:34:52 +0000703 return NULL;
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000704}
Greg Claytone6d72ca2011-06-25 00:44:06 +0000705
706void
707NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
708{
709 for (clang::NamedDecl * const *decl_iterator = result.first;
710 decl_iterator != result.second;
711 ++decl_iterator)
712 m_decls.push_back (*decl_iterator);
713}
714
715void
716NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
717{
718 m_decls.push_back (decl);
719}